* tree-core.h (enum omp_clause_code): Add OMP_CLAUSE_DEFAULTMAP.
[official-gcc.git] / gcc / cp / parser.c
blobc4b5595eb4fb0300970cbb128756149e6cf669d5
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "print-tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "trans-mem.h"
34 #include "cp-tree.h"
35 #include "intl.h"
36 #include "c-family/c-pragma.h"
37 #include "decl.h"
38 #include "flags.h"
39 #include "diagnostic-core.h"
40 #include "target.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "cgraph.h"
44 #include "c-family/c-common.h"
45 #include "c-family/c-objc.h"
46 #include "plugin.h"
47 #include "tree-pretty-print.h"
48 #include "parser.h"
49 #include "type-utils.h"
50 #include "omp-low.h"
51 #include "gomp-constants.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.c) and the C++ parser. */
59 static cp_token eof_token =
61 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
64 /* The various kinds of non integral constant we encounter. */
65 typedef enum non_integral_constant {
66 NIC_NONE,
67 /* floating-point literal */
68 NIC_FLOAT,
69 /* %<this%> */
70 NIC_THIS,
71 /* %<__FUNCTION__%> */
72 NIC_FUNC_NAME,
73 /* %<__PRETTY_FUNCTION__%> */
74 NIC_PRETTY_FUNC,
75 /* %<__func__%> */
76 NIC_C99_FUNC,
77 /* "%<va_arg%> */
78 NIC_VA_ARG,
79 /* a cast */
80 NIC_CAST,
81 /* %<typeid%> operator */
82 NIC_TYPEID,
83 /* non-constant compound literals */
84 NIC_NCC,
85 /* a function call */
86 NIC_FUNC_CALL,
87 /* an increment */
88 NIC_INC,
89 /* an decrement */
90 NIC_DEC,
91 /* an array reference */
92 NIC_ARRAY_REF,
93 /* %<->%> */
94 NIC_ARROW,
95 /* %<.%> */
96 NIC_POINT,
97 /* the address of a label */
98 NIC_ADDR_LABEL,
99 /* %<*%> */
100 NIC_STAR,
101 /* %<&%> */
102 NIC_ADDR,
103 /* %<++%> */
104 NIC_PREINCREMENT,
105 /* %<--%> */
106 NIC_PREDECREMENT,
107 /* %<new%> */
108 NIC_NEW,
109 /* %<delete%> */
110 NIC_DEL,
111 /* calls to overloaded operators */
112 NIC_OVERLOADED,
113 /* an assignment */
114 NIC_ASSIGNMENT,
115 /* a comma operator */
116 NIC_COMMA,
117 /* a call to a constructor */
118 NIC_CONSTRUCTOR,
119 /* a transaction expression */
120 NIC_TRANSACTION
121 } non_integral_constant;
123 /* The various kinds of errors about name-lookup failing. */
124 typedef enum name_lookup_error {
125 /* NULL */
126 NLE_NULL,
127 /* is not a type */
128 NLE_TYPE,
129 /* is not a class or namespace */
130 NLE_CXX98,
131 /* is not a class, namespace, or enumeration */
132 NLE_NOT_CXX98
133 } name_lookup_error;
135 /* The various kinds of required token */
136 typedef enum required_token {
137 RT_NONE,
138 RT_SEMICOLON, /* ';' */
139 RT_OPEN_PAREN, /* '(' */
140 RT_CLOSE_BRACE, /* '}' */
141 RT_OPEN_BRACE, /* '{' */
142 RT_CLOSE_SQUARE, /* ']' */
143 RT_OPEN_SQUARE, /* '[' */
144 RT_COMMA, /* ',' */
145 RT_SCOPE, /* '::' */
146 RT_LESS, /* '<' */
147 RT_GREATER, /* '>' */
148 RT_EQ, /* '=' */
149 RT_ELLIPSIS, /* '...' */
150 RT_MULT, /* '*' */
151 RT_COMPL, /* '~' */
152 RT_COLON, /* ':' */
153 RT_COLON_SCOPE, /* ':' or '::' */
154 RT_CLOSE_PAREN, /* ')' */
155 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
156 RT_PRAGMA_EOL, /* end of line */
157 RT_NAME, /* identifier */
159 /* The type is CPP_KEYWORD */
160 RT_NEW, /* new */
161 RT_DELETE, /* delete */
162 RT_RETURN, /* return */
163 RT_WHILE, /* while */
164 RT_EXTERN, /* extern */
165 RT_STATIC_ASSERT, /* static_assert */
166 RT_DECLTYPE, /* decltype */
167 RT_OPERATOR, /* operator */
168 RT_CLASS, /* class */
169 RT_TEMPLATE, /* template */
170 RT_NAMESPACE, /* namespace */
171 RT_USING, /* using */
172 RT_ASM, /* asm */
173 RT_TRY, /* try */
174 RT_CATCH, /* catch */
175 RT_THROW, /* throw */
176 RT_LABEL, /* __label__ */
177 RT_AT_TRY, /* @try */
178 RT_AT_SYNCHRONIZED, /* @synchronized */
179 RT_AT_THROW, /* @throw */
181 RT_SELECT, /* selection-statement */
182 RT_INTERATION, /* iteration-statement */
183 RT_JUMP, /* jump-statement */
184 RT_CLASS_KEY, /* class-key */
185 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
186 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
187 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
188 RT_TRANSACTION_CANCEL /* __transaction_cancel */
189 } required_token;
191 /* Prototypes. */
193 static cp_lexer *cp_lexer_new_main
194 (void);
195 static cp_lexer *cp_lexer_new_from_tokens
196 (cp_token_cache *tokens);
197 static void cp_lexer_destroy
198 (cp_lexer *);
199 static int cp_lexer_saving_tokens
200 (const cp_lexer *);
201 static cp_token *cp_lexer_token_at
202 (cp_lexer *, cp_token_position);
203 static void cp_lexer_get_preprocessor_token
204 (cp_lexer *, cp_token *);
205 static inline cp_token *cp_lexer_peek_token
206 (cp_lexer *);
207 static cp_token *cp_lexer_peek_nth_token
208 (cp_lexer *, size_t);
209 static inline bool cp_lexer_next_token_is
210 (cp_lexer *, enum cpp_ttype);
211 static bool cp_lexer_next_token_is_not
212 (cp_lexer *, enum cpp_ttype);
213 static bool cp_lexer_next_token_is_keyword
214 (cp_lexer *, enum rid);
215 static cp_token *cp_lexer_consume_token
216 (cp_lexer *);
217 static void cp_lexer_purge_token
218 (cp_lexer *);
219 static void cp_lexer_purge_tokens_after
220 (cp_lexer *, cp_token_position);
221 static void cp_lexer_save_tokens
222 (cp_lexer *);
223 static void cp_lexer_commit_tokens
224 (cp_lexer *);
225 static void cp_lexer_rollback_tokens
226 (cp_lexer *);
227 static void cp_lexer_print_token
228 (FILE *, cp_token *);
229 static inline bool cp_lexer_debugging_p
230 (cp_lexer *);
231 static void cp_lexer_start_debugging
232 (cp_lexer *) ATTRIBUTE_UNUSED;
233 static void cp_lexer_stop_debugging
234 (cp_lexer *) ATTRIBUTE_UNUSED;
236 static cp_token_cache *cp_token_cache_new
237 (cp_token *, cp_token *);
239 static void cp_parser_initial_pragma
240 (cp_token *);
242 static tree cp_literal_operator_id
243 (const char *);
245 static void cp_parser_cilk_simd
246 (cp_parser *, cp_token *);
247 static tree cp_parser_cilk_for
248 (cp_parser *, tree);
249 static bool cp_parser_omp_declare_reduction_exprs
250 (tree, cp_parser *);
251 static tree cp_parser_cilk_simd_vectorlength
252 (cp_parser *, tree, bool);
254 /* Manifest constants. */
255 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
256 #define CP_SAVED_TOKEN_STACK 5
258 /* Variables. */
260 /* The stream to which debugging output should be written. */
261 static FILE *cp_lexer_debug_stream;
263 /* Nonzero if we are parsing an unevaluated operand: an operand to
264 sizeof, typeof, or alignof. */
265 int cp_unevaluated_operand;
267 /* Dump up to NUM tokens in BUFFER to FILE starting with token
268 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
269 first token in BUFFER. If NUM is 0, dump all the tokens. If
270 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
271 highlighted by surrounding it in [[ ]]. */
273 static void
274 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
275 cp_token *start_token, unsigned num,
276 cp_token *curr_token)
278 unsigned i, nprinted;
279 cp_token *token;
280 bool do_print;
282 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
284 if (buffer == NULL)
285 return;
287 if (num == 0)
288 num = buffer->length ();
290 if (start_token == NULL)
291 start_token = buffer->address ();
293 if (start_token > buffer->address ())
295 cp_lexer_print_token (file, &(*buffer)[0]);
296 fprintf (file, " ... ");
299 do_print = false;
300 nprinted = 0;
301 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
303 if (token == start_token)
304 do_print = true;
306 if (!do_print)
307 continue;
309 nprinted++;
310 if (token == curr_token)
311 fprintf (file, "[[");
313 cp_lexer_print_token (file, token);
315 if (token == curr_token)
316 fprintf (file, "]]");
318 switch (token->type)
320 case CPP_SEMICOLON:
321 case CPP_OPEN_BRACE:
322 case CPP_CLOSE_BRACE:
323 case CPP_EOF:
324 fputc ('\n', file);
325 break;
327 default:
328 fputc (' ', file);
332 if (i == num && i < buffer->length ())
334 fprintf (file, " ... ");
335 cp_lexer_print_token (file, &buffer->last ());
338 fprintf (file, "\n");
342 /* Dump all tokens in BUFFER to stderr. */
344 void
345 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
347 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
350 DEBUG_FUNCTION void
351 debug (vec<cp_token, va_gc> &ref)
353 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> *ptr)
359 if (ptr)
360 debug (*ptr);
361 else
362 fprintf (stderr, "<nil>\n");
366 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
367 description for T. */
369 static void
370 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
372 if (t)
374 fprintf (file, "%s: ", desc);
375 print_node_brief (file, "", t, 0);
380 /* Dump parser context C to FILE. */
382 static void
383 cp_debug_print_context (FILE *file, cp_parser_context *c)
385 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
386 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
387 print_node_brief (file, "", c->object_type, 0);
388 fprintf (file, "}\n");
392 /* Print the stack of parsing contexts to FILE starting with FIRST. */
394 static void
395 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
397 unsigned i;
398 cp_parser_context *c;
400 fprintf (file, "Parsing context stack:\n");
401 for (i = 0, c = first; c; c = c->next, i++)
403 fprintf (file, "\t#%u: ", i);
404 cp_debug_print_context (file, c);
409 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
411 static void
412 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
414 if (flag)
415 fprintf (file, "%s: true\n", desc);
419 /* Print an unparsed function entry UF to FILE. */
421 static void
422 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
424 unsigned i;
425 cp_default_arg_entry *default_arg_fn;
426 tree fn;
428 fprintf (file, "\tFunctions with default args:\n");
429 for (i = 0;
430 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
431 i++)
433 fprintf (file, "\t\tClass type: ");
434 print_node_brief (file, "", default_arg_fn->class_type, 0);
435 fprintf (file, "\t\tDeclaration: ");
436 print_node_brief (file, "", default_arg_fn->decl, 0);
437 fprintf (file, "\n");
440 fprintf (file, "\n\tFunctions with definitions that require "
441 "post-processing\n\t\t");
442 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
444 print_node_brief (file, "", fn, 0);
445 fprintf (file, " ");
447 fprintf (file, "\n");
449 fprintf (file, "\n\tNon-static data members with initializers that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
460 /* Print the stack of unparsed member functions S to FILE. */
462 static void
463 cp_debug_print_unparsed_queues (FILE *file,
464 vec<cp_unparsed_functions_entry, va_gc> *s)
466 unsigned i;
467 cp_unparsed_functions_entry *uf;
469 fprintf (file, "Unparsed functions\n");
470 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
472 fprintf (file, "#%u:\n", i);
473 cp_debug_print_unparsed_function (file, uf);
478 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
479 the given PARSER. If FILE is NULL, the output is printed on stderr. */
481 static void
482 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
484 cp_token *next_token, *first_token, *start_token;
486 if (file == NULL)
487 file = stderr;
489 next_token = parser->lexer->next_token;
490 first_token = parser->lexer->buffer->address ();
491 start_token = (next_token > first_token + window_size / 2)
492 ? next_token - window_size / 2
493 : first_token;
494 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
495 next_token);
499 /* Dump debugging information for the given PARSER. If FILE is NULL,
500 the output is printed on stderr. */
502 void
503 cp_debug_parser (FILE *file, cp_parser *parser)
505 const size_t window_size = 20;
506 cp_token *token;
507 expanded_location eloc;
509 if (file == NULL)
510 file = stderr;
512 fprintf (file, "Parser state\n\n");
513 fprintf (file, "Number of tokens: %u\n",
514 vec_safe_length (parser->lexer->buffer));
515 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
516 cp_debug_print_tree_if_set (file, "Object scope",
517 parser->object_scope);
518 cp_debug_print_tree_if_set (file, "Qualifying scope",
519 parser->qualifying_scope);
520 cp_debug_print_context_stack (file, parser->context);
521 cp_debug_print_flag (file, "Allow GNU extensions",
522 parser->allow_gnu_extensions_p);
523 cp_debug_print_flag (file, "'>' token is greater-than",
524 parser->greater_than_is_operator_p);
525 cp_debug_print_flag (file, "Default args allowed in current "
526 "parameter list", parser->default_arg_ok_p);
527 cp_debug_print_flag (file, "Parsing integral constant-expression",
528 parser->integral_constant_expression_p);
529 cp_debug_print_flag (file, "Allow non-constant expression in current "
530 "constant-expression",
531 parser->allow_non_integral_constant_expression_p);
532 cp_debug_print_flag (file, "Seen non-constant expression",
533 parser->non_integral_constant_expression_p);
534 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
535 "current context",
536 parser->local_variables_forbidden_p);
537 cp_debug_print_flag (file, "In unbraced linkage specification",
538 parser->in_unbraced_linkage_specification_p);
539 cp_debug_print_flag (file, "Parsing a declarator",
540 parser->in_declarator_p);
541 cp_debug_print_flag (file, "In template argument list",
542 parser->in_template_argument_list_p);
543 cp_debug_print_flag (file, "Parsing an iteration statement",
544 parser->in_statement & IN_ITERATION_STMT);
545 cp_debug_print_flag (file, "Parsing a switch statement",
546 parser->in_statement & IN_SWITCH_STMT);
547 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
548 parser->in_statement & IN_OMP_BLOCK);
549 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
550 parser->in_statement & IN_CILK_SIMD_FOR);
551 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
552 parser->in_statement & IN_OMP_FOR);
553 cp_debug_print_flag (file, "Parsing an if statement",
554 parser->in_statement & IN_IF_STMT);
555 cp_debug_print_flag (file, "Parsing a type-id in an expression "
556 "context", parser->in_type_id_in_expr_p);
557 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
558 parser->implicit_extern_c);
559 cp_debug_print_flag (file, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
590 cp_debug_parser (stderr, &ref);
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
604 static cp_lexer *
605 cp_lexer_alloc (void)
607 cp_lexer *lexer;
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622 return lexer;
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
629 static cp_lexer *
630 cp_lexer_new_main (void)
632 cp_lexer *lexer;
633 cp_token token;
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
640 lexer = cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
691 /* Frees all resources associated with LEXER. */
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
701 /* Returns nonzero if debugging information should be output. */
703 static inline bool
704 cp_lexer_debugging_p (cp_lexer *lexer)
706 return lexer->debugging_p;
710 static inline cp_token_position
711 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
713 gcc_assert (!previous_p || lexer->next_token != &eof_token);
715 return lexer->next_token - previous_p;
718 static inline cp_token *
719 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
721 return pos;
724 static inline void
725 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
727 lexer->next_token = cp_lexer_token_at (lexer, pos);
730 static inline cp_token_position
731 cp_lexer_previous_token_position (cp_lexer *lexer)
733 if (lexer->next_token == &eof_token)
734 return lexer->last_token - 1;
735 else
736 return cp_lexer_token_position (lexer, true);
739 static inline cp_token *
740 cp_lexer_previous_token (cp_lexer *lexer)
742 cp_token_position tp = cp_lexer_previous_token_position (lexer);
744 return cp_lexer_token_at (lexer, tp);
747 /* nonzero if we are presently saving tokens. */
749 static inline int
750 cp_lexer_saving_tokens (const cp_lexer* lexer)
752 return lexer->saved_tokens.length () != 0;
755 /* Store the next token from the preprocessor in *TOKEN. Return true
756 if we reach EOF. If LEXER is NULL, assume we are handling an
757 initial #pragma pch_preprocess, and thus want the lexer to return
758 processed strings. */
760 static void
761 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
763 static int is_extern_c = 0;
765 /* Get a new token from the preprocessor. */
766 token->type
767 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
768 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
769 token->keyword = RID_MAX;
770 token->pragma_kind = PRAGMA_NONE;
771 token->purged_p = false;
772 token->error_reported = false;
774 /* On some systems, some header files are surrounded by an
775 implicit extern "C" block. Set a flag in the token if it
776 comes from such a header. */
777 is_extern_c += pending_lang_change;
778 pending_lang_change = 0;
779 token->implicit_extern_c = is_extern_c > 0;
781 /* Check to see if this token is a keyword. */
782 if (token->type == CPP_NAME)
784 if (C_IS_RESERVED_WORD (token->u.value))
786 /* Mark this token as a keyword. */
787 token->type = CPP_KEYWORD;
788 /* Record which keyword. */
789 token->keyword = C_RID_CODE (token->u.value);
791 else
793 if (warn_cxx11_compat
794 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
797 /* Warn about the C++0x keyword (but still treat it as
798 an identifier). */
799 warning (OPT_Wc__11_compat,
800 "identifier %qE is a keyword in C++11",
801 token->u.value);
803 /* Clear out the C_RID_CODE so we don't warn about this
804 particular identifier-turned-keyword again. */
805 C_SET_RID_CODE (token->u.value, RID_MAX);
808 token->keyword = RID_MAX;
811 else if (token->type == CPP_AT_NAME)
813 /* This only happens in Objective-C++; it must be a keyword. */
814 token->type = CPP_KEYWORD;
815 switch (C_RID_CODE (token->u.value))
817 /* Replace 'class' with '@class', 'private' with '@private',
818 etc. This prevents confusion with the C++ keyword
819 'class', and makes the tokens consistent with other
820 Objective-C 'AT' keywords. For example '@class' is
821 reported as RID_AT_CLASS which is consistent with
822 '@synchronized', which is reported as
823 RID_AT_SYNCHRONIZED.
825 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
826 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
827 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
828 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
829 case RID_THROW: token->keyword = RID_AT_THROW; break;
830 case RID_TRY: token->keyword = RID_AT_TRY; break;
831 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
832 default: token->keyword = C_RID_CODE (token->u.value);
835 else if (token->type == CPP_PRAGMA)
837 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
838 token->pragma_kind = ((enum pragma_kind)
839 TREE_INT_CST_LOW (token->u.value));
840 token->u.value = NULL_TREE;
844 /* Update the globals input_location and the input file stack from TOKEN. */
845 static inline void
846 cp_lexer_set_source_position_from_token (cp_token *token)
848 if (token->type != CPP_EOF)
850 input_location = token->location;
854 /* Update the globals input_location and the input file stack from LEXER. */
855 static inline void
856 cp_lexer_set_source_position (cp_lexer *lexer)
858 cp_token *token = cp_lexer_peek_token (lexer);
859 cp_lexer_set_source_position_from_token (token);
862 /* Return a pointer to the next token in the token stream, but do not
863 consume it. */
865 static inline cp_token *
866 cp_lexer_peek_token (cp_lexer *lexer)
868 if (cp_lexer_debugging_p (lexer))
870 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
871 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
872 putc ('\n', cp_lexer_debug_stream);
874 return lexer->next_token;
877 /* Return true if the next token has the indicated TYPE. */
879 static inline bool
880 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
882 return cp_lexer_peek_token (lexer)->type == type;
885 /* Return true if the next token does not have the indicated TYPE. */
887 static inline bool
888 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
890 return !cp_lexer_next_token_is (lexer, type);
893 /* Return true if the next token is the indicated KEYWORD. */
895 static inline bool
896 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
898 return cp_lexer_peek_token (lexer)->keyword == keyword;
901 static inline bool
902 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
904 return cp_lexer_peek_nth_token (lexer, n)->type == type;
907 static inline bool
908 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
910 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
913 /* Return true if the next token is not the indicated KEYWORD. */
915 static inline bool
916 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
918 return cp_lexer_peek_token (lexer)->keyword != keyword;
921 /* Return true if the next token is a keyword for a decl-specifier. */
923 static bool
924 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
926 cp_token *token;
928 token = cp_lexer_peek_token (lexer);
929 switch (token->keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 return true;
968 default:
969 if (token->keyword >= RID_FIRST_INT_N
970 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
977 /* Returns TRUE iff the token T begins a decltype type. */
979 static bool
980 token_is_decltype (cp_token *t)
982 return (t->keyword == RID_DECLTYPE
983 || t->type == CPP_DECLTYPE);
986 /* Returns TRUE iff the next token begins a decltype type. */
988 static bool
989 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
991 cp_token *t = cp_lexer_peek_token (lexer);
992 return token_is_decltype (t);
995 /* Return a pointer to the Nth token in the token stream. If N is 1,
996 then this is precisely equivalent to cp_lexer_peek_token (except
997 that it is not inline). One would like to disallow that case, but
998 there is one case (cp_parser_nth_token_starts_template_id) where
999 the caller passes a variable for N and it might be 1. */
1001 static cp_token *
1002 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1004 cp_token *token;
1006 /* N is 1-based, not zero-based. */
1007 gcc_assert (n > 0);
1009 if (cp_lexer_debugging_p (lexer))
1010 fprintf (cp_lexer_debug_stream,
1011 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1013 --n;
1014 token = lexer->next_token;
1015 gcc_assert (!n || token != &eof_token);
1016 while (n != 0)
1018 ++token;
1019 if (token == lexer->last_token)
1021 token = &eof_token;
1022 break;
1025 if (!token->purged_p)
1026 --n;
1029 if (cp_lexer_debugging_p (lexer))
1031 cp_lexer_print_token (cp_lexer_debug_stream, token);
1032 putc ('\n', cp_lexer_debug_stream);
1035 return token;
1038 /* Return the next token, and advance the lexer's next_token pointer
1039 to point to the next non-purged token. */
1041 static cp_token *
1042 cp_lexer_consume_token (cp_lexer* lexer)
1044 cp_token *token = lexer->next_token;
1046 gcc_assert (token != &eof_token);
1047 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1051 lexer->next_token++;
1052 if (lexer->next_token == lexer->last_token)
1054 lexer->next_token = &eof_token;
1055 break;
1059 while (lexer->next_token->purged_p);
1061 cp_lexer_set_source_position_from_token (token);
1063 /* Provide debugging output. */
1064 if (cp_lexer_debugging_p (lexer))
1066 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1067 cp_lexer_print_token (cp_lexer_debug_stream, token);
1068 putc ('\n', cp_lexer_debug_stream);
1071 return token;
1074 /* Permanently remove the next token from the token stream, and
1075 advance the next_token pointer to refer to the next non-purged
1076 token. */
1078 static void
1079 cp_lexer_purge_token (cp_lexer *lexer)
1081 cp_token *tok = lexer->next_token;
1083 gcc_assert (tok != &eof_token);
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1091 tok++;
1092 if (tok == lexer->last_token)
1094 tok = &eof_token;
1095 break;
1098 while (tok->purged_p);
1099 lexer->next_token = tok;
1102 /* Permanently remove all tokens after TOK, up to, but not
1103 including, the token that will be returned next by
1104 cp_lexer_peek_token. */
1106 static void
1107 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1109 cp_token *peek = lexer->next_token;
1111 if (peek == &eof_token)
1112 peek = lexer->last_token;
1114 gcc_assert (tok < peek);
1116 for ( tok += 1; tok != peek; tok += 1)
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 /* Begin saving tokens. All tokens consumed after this point will be
1126 preserved. */
1128 static void
1129 cp_lexer_save_tokens (cp_lexer* lexer)
1131 /* Provide debugging output. */
1132 if (cp_lexer_debugging_p (lexer))
1133 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1135 lexer->saved_tokens.safe_push (lexer->next_token);
1138 /* Commit to the portion of the token stream most recently saved. */
1140 static void
1141 cp_lexer_commit_tokens (cp_lexer* lexer)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1147 lexer->saved_tokens.pop ();
1150 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1151 to the token stream. Stop saving tokens. */
1153 static void
1154 cp_lexer_rollback_tokens (cp_lexer* lexer)
1156 /* Provide debugging output. */
1157 if (cp_lexer_debugging_p (lexer))
1158 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1160 lexer->next_token = lexer->saved_tokens.pop ();
1163 /* RAII wrapper around the above functions, with sanity checking. Creating
1164 a variable saves tokens, which are committed when the variable is
1165 destroyed unless they are explicitly rolled back by calling the rollback
1166 member function. */
1168 struct saved_token_sentinel
1170 cp_lexer *lexer;
1171 unsigned len;
1172 bool commit;
1173 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1175 len = lexer->saved_tokens.length ();
1176 cp_lexer_save_tokens (lexer);
1178 void rollback ()
1180 cp_lexer_rollback_tokens (lexer);
1181 commit = false;
1183 ~saved_token_sentinel()
1185 if (commit)
1186 cp_lexer_commit_tokens (lexer);
1187 gcc_assert (lexer->saved_tokens.length () == len);
1191 /* Print a representation of the TOKEN on the STREAM. */
1193 static void
1194 cp_lexer_print_token (FILE * stream, cp_token *token)
1196 /* We don't use cpp_type2name here because the parser defines
1197 a few tokens of its own. */
1198 static const char *const token_names[] = {
1199 /* cpplib-defined token types */
1200 #define OP(e, s) #e,
1201 #define TK(e, s) #e,
1202 TTYPE_TABLE
1203 #undef OP
1204 #undef TK
1205 /* C++ parser token types - see "Manifest constants", above. */
1206 "KEYWORD",
1207 "TEMPLATE_ID",
1208 "NESTED_NAME_SPECIFIER",
1211 /* For some tokens, print the associated data. */
1212 switch (token->type)
1214 case CPP_KEYWORD:
1215 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1216 For example, `struct' is mapped to an INTEGER_CST. */
1217 if (!identifier_p (token->u.value))
1218 break;
1219 /* else fall through */
1220 case CPP_NAME:
1221 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1222 break;
1224 case CPP_STRING:
1225 case CPP_STRING16:
1226 case CPP_STRING32:
1227 case CPP_WSTRING:
1228 case CPP_UTF8STRING:
1229 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1230 break;
1232 case CPP_NUMBER:
1233 print_generic_expr (stream, token->u.value, 0);
1234 break;
1236 default:
1237 /* If we have a name for the token, print it out. Otherwise, we
1238 simply give the numeric code. */
1239 if (token->type < ARRAY_SIZE(token_names))
1240 fputs (token_names[token->type], stream);
1241 else
1242 fprintf (stream, "[%d]", token->type);
1243 break;
1247 DEBUG_FUNCTION void
1248 debug (cp_token &ref)
1250 cp_lexer_print_token (stderr, &ref);
1251 fprintf (stderr, "\n");
1254 DEBUG_FUNCTION void
1255 debug (cp_token *ptr)
1257 if (ptr)
1258 debug (*ptr);
1259 else
1260 fprintf (stderr, "<nil>\n");
1264 /* Start emitting debugging information. */
1266 static void
1267 cp_lexer_start_debugging (cp_lexer* lexer)
1269 lexer->debugging_p = true;
1270 cp_lexer_debug_stream = stderr;
1273 /* Stop emitting debugging information. */
1275 static void
1276 cp_lexer_stop_debugging (cp_lexer* lexer)
1278 lexer->debugging_p = false;
1279 cp_lexer_debug_stream = NULL;
1282 /* Create a new cp_token_cache, representing a range of tokens. */
1284 static cp_token_cache *
1285 cp_token_cache_new (cp_token *first, cp_token *last)
1287 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1288 cache->first = first;
1289 cache->last = last;
1290 return cache;
1293 /* Diagnose if #pragma omp declare simd isn't followed immediately
1294 by function declaration or definition. */
1296 static inline void
1297 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1299 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1301 error ("%<#pragma omp declare simd%> not immediately followed by "
1302 "function declaration or definition");
1303 parser->omp_declare_simd = NULL;
1307 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1308 and put that into "omp declare simd" attribute. */
1310 static inline void
1311 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1313 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1315 if (fndecl == error_mark_node)
1317 parser->omp_declare_simd = NULL;
1318 return;
1320 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1322 cp_ensure_no_omp_declare_simd (parser);
1323 return;
1328 /* Decl-specifiers. */
1330 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1332 static void
1333 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1335 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1338 /* Declarators. */
1340 /* Nothing other than the parser should be creating declarators;
1341 declarators are a semi-syntactic representation of C++ entities.
1342 Other parts of the front end that need to create entities (like
1343 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1345 static cp_declarator *make_call_declarator
1346 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1347 static cp_declarator *make_array_declarator
1348 (cp_declarator *, tree);
1349 static cp_declarator *make_pointer_declarator
1350 (cp_cv_quals, cp_declarator *, tree);
1351 static cp_declarator *make_reference_declarator
1352 (cp_cv_quals, cp_declarator *, bool, tree);
1353 static cp_parameter_declarator *make_parameter_declarator
1354 (cp_decl_specifier_seq *, cp_declarator *, tree);
1355 static cp_declarator *make_ptrmem_declarator
1356 (cp_cv_quals, tree, cp_declarator *, tree);
1358 /* An erroneous declarator. */
1359 static cp_declarator *cp_error_declarator;
1361 /* The obstack on which declarators and related data structures are
1362 allocated. */
1363 static struct obstack declarator_obstack;
1365 /* Alloc BYTES from the declarator memory pool. */
1367 static inline void *
1368 alloc_declarator (size_t bytes)
1370 return obstack_alloc (&declarator_obstack, bytes);
1373 /* Allocate a declarator of the indicated KIND. Clear fields that are
1374 common to all declarators. */
1376 static cp_declarator *
1377 make_declarator (cp_declarator_kind kind)
1379 cp_declarator *declarator;
1381 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1382 declarator->kind = kind;
1383 declarator->attributes = NULL_TREE;
1384 declarator->std_attributes = NULL_TREE;
1385 declarator->declarator = NULL;
1386 declarator->parameter_pack_p = false;
1387 declarator->id_loc = UNKNOWN_LOCATION;
1389 return declarator;
1392 /* Make a declarator for a generalized identifier. If
1393 QUALIFYING_SCOPE is non-NULL, the identifier is
1394 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1395 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1396 is, if any. */
1398 static cp_declarator *
1399 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1400 special_function_kind sfk)
1402 cp_declarator *declarator;
1404 /* It is valid to write:
1406 class C { void f(); };
1407 typedef C D;
1408 void D::f();
1410 The standard is not clear about whether `typedef const C D' is
1411 legal; as of 2002-09-15 the committee is considering that
1412 question. EDG 3.0 allows that syntax. Therefore, we do as
1413 well. */
1414 if (qualifying_scope && TYPE_P (qualifying_scope))
1415 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1417 gcc_assert (identifier_p (unqualified_name)
1418 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1419 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1421 declarator = make_declarator (cdk_id);
1422 declarator->u.id.qualifying_scope = qualifying_scope;
1423 declarator->u.id.unqualified_name = unqualified_name;
1424 declarator->u.id.sfk = sfk;
1426 return declarator;
1429 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1430 of modifiers such as const or volatile to apply to the pointer
1431 type, represented as identifiers. ATTRIBUTES represent the attributes that
1432 appertain to the pointer or reference. */
1434 cp_declarator *
1435 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1436 tree attributes)
1438 cp_declarator *declarator;
1440 declarator = make_declarator (cdk_pointer);
1441 declarator->declarator = target;
1442 declarator->u.pointer.qualifiers = cv_qualifiers;
1443 declarator->u.pointer.class_type = NULL_TREE;
1444 if (target)
1446 declarator->id_loc = target->id_loc;
1447 declarator->parameter_pack_p = target->parameter_pack_p;
1448 target->parameter_pack_p = false;
1450 else
1451 declarator->parameter_pack_p = false;
1453 declarator->std_attributes = attributes;
1455 return declarator;
1458 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1459 represent the attributes that appertain to the pointer or
1460 reference. */
1462 cp_declarator *
1463 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1464 bool rvalue_ref, tree attributes)
1466 cp_declarator *declarator;
1468 declarator = make_declarator (cdk_reference);
1469 declarator->declarator = target;
1470 declarator->u.reference.qualifiers = cv_qualifiers;
1471 declarator->u.reference.rvalue_ref = rvalue_ref;
1472 if (target)
1474 declarator->id_loc = target->id_loc;
1475 declarator->parameter_pack_p = target->parameter_pack_p;
1476 target->parameter_pack_p = false;
1478 else
1479 declarator->parameter_pack_p = false;
1481 declarator->std_attributes = attributes;
1483 return declarator;
1486 /* Like make_pointer_declarator -- but for a pointer to a non-static
1487 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1490 cp_declarator *
1491 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1492 cp_declarator *pointee,
1493 tree attributes)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_ptrmem);
1498 declarator->declarator = pointee;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = class_type;
1502 if (pointee)
1504 declarator->parameter_pack_p = pointee->parameter_pack_p;
1505 pointee->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 declarator->std_attributes = attributes;
1512 return declarator;
1515 /* Make a declarator for the function given by TARGET, with the
1516 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1517 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1518 indicates what exceptions can be thrown. */
1520 cp_declarator *
1521 make_call_declarator (cp_declarator *target,
1522 tree parms,
1523 cp_cv_quals cv_qualifiers,
1524 cp_virt_specifiers virt_specifiers,
1525 cp_ref_qualifier ref_qualifier,
1526 tree exception_specification,
1527 tree late_return_type)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_function);
1532 declarator->declarator = target;
1533 declarator->u.function.parameters = parms;
1534 declarator->u.function.qualifiers = cv_qualifiers;
1535 declarator->u.function.virt_specifiers = virt_specifiers;
1536 declarator->u.function.ref_qualifier = ref_qualifier;
1537 declarator->u.function.exception_specification = exception_specification;
1538 declarator->u.function.late_return_type = late_return_type;
1539 if (target)
1541 declarator->id_loc = target->id_loc;
1542 declarator->parameter_pack_p = target->parameter_pack_p;
1543 target->parameter_pack_p = false;
1545 else
1546 declarator->parameter_pack_p = false;
1548 return declarator;
1551 /* Make a declarator for an array of BOUNDS elements, each of which is
1552 defined by ELEMENT. */
1554 cp_declarator *
1555 make_array_declarator (cp_declarator *element, tree bounds)
1557 cp_declarator *declarator;
1559 declarator = make_declarator (cdk_array);
1560 declarator->declarator = element;
1561 declarator->u.array.bounds = bounds;
1562 if (element)
1564 declarator->id_loc = element->id_loc;
1565 declarator->parameter_pack_p = element->parameter_pack_p;
1566 element->parameter_pack_p = false;
1568 else
1569 declarator->parameter_pack_p = false;
1571 return declarator;
1574 /* Determine whether the declarator we've seen so far can be a
1575 parameter pack, when followed by an ellipsis. */
1576 static bool
1577 declarator_can_be_parameter_pack (cp_declarator *declarator)
1579 /* Search for a declarator name, or any other declarator that goes
1580 after the point where the ellipsis could appear in a parameter
1581 pack. If we find any of these, then this declarator can not be
1582 made into a parameter pack. */
1583 bool found = false;
1584 while (declarator && !found)
1586 switch ((int)declarator->kind)
1588 case cdk_id:
1589 case cdk_array:
1590 found = true;
1591 break;
1593 case cdk_error:
1594 return true;
1596 default:
1597 declarator = declarator->declarator;
1598 break;
1602 return !found;
1605 cp_parameter_declarator *no_parameters;
1607 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1608 DECLARATOR and DEFAULT_ARGUMENT. */
1610 cp_parameter_declarator *
1611 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1612 cp_declarator *declarator,
1613 tree default_argument)
1615 cp_parameter_declarator *parameter;
1617 parameter = ((cp_parameter_declarator *)
1618 alloc_declarator (sizeof (cp_parameter_declarator)));
1619 parameter->next = NULL;
1620 if (decl_specifiers)
1621 parameter->decl_specifiers = *decl_specifiers;
1622 else
1623 clear_decl_specs (&parameter->decl_specifiers);
1624 parameter->declarator = declarator;
1625 parameter->default_argument = default_argument;
1626 parameter->ellipsis_p = false;
1628 return parameter;
1631 /* Returns true iff DECLARATOR is a declaration for a function. */
1633 static bool
1634 function_declarator_p (const cp_declarator *declarator)
1636 while (declarator)
1638 if (declarator->kind == cdk_function
1639 && declarator->declarator->kind == cdk_id)
1640 return true;
1641 if (declarator->kind == cdk_id
1642 || declarator->kind == cdk_error)
1643 return false;
1644 declarator = declarator->declarator;
1646 return false;
1649 /* The parser. */
1651 /* Overview
1652 --------
1654 A cp_parser parses the token stream as specified by the C++
1655 grammar. Its job is purely parsing, not semantic analysis. For
1656 example, the parser breaks the token stream into declarators,
1657 expressions, statements, and other similar syntactic constructs.
1658 It does not check that the types of the expressions on either side
1659 of an assignment-statement are compatible, or that a function is
1660 not declared with a parameter of type `void'.
1662 The parser invokes routines elsewhere in the compiler to perform
1663 semantic analysis and to build up the abstract syntax tree for the
1664 code processed.
1666 The parser (and the template instantiation code, which is, in a
1667 way, a close relative of parsing) are the only parts of the
1668 compiler that should be calling push_scope and pop_scope, or
1669 related functions. The parser (and template instantiation code)
1670 keeps track of what scope is presently active; everything else
1671 should simply honor that. (The code that generates static
1672 initializers may also need to set the scope, in order to check
1673 access control correctly when emitting the initializers.)
1675 Methodology
1676 -----------
1678 The parser is of the standard recursive-descent variety. Upcoming
1679 tokens in the token stream are examined in order to determine which
1680 production to use when parsing a non-terminal. Some C++ constructs
1681 require arbitrary look ahead to disambiguate. For example, it is
1682 impossible, in the general case, to tell whether a statement is an
1683 expression or declaration without scanning the entire statement.
1684 Therefore, the parser is capable of "parsing tentatively." When the
1685 parser is not sure what construct comes next, it enters this mode.
1686 Then, while we attempt to parse the construct, the parser queues up
1687 error messages, rather than issuing them immediately, and saves the
1688 tokens it consumes. If the construct is parsed successfully, the
1689 parser "commits", i.e., it issues any queued error messages and
1690 the tokens that were being preserved are permanently discarded.
1691 If, however, the construct is not parsed successfully, the parser
1692 rolls back its state completely so that it can resume parsing using
1693 a different alternative.
1695 Future Improvements
1696 -------------------
1698 The performance of the parser could probably be improved substantially.
1699 We could often eliminate the need to parse tentatively by looking ahead
1700 a little bit. In some places, this approach might not entirely eliminate
1701 the need to parse tentatively, but it might still speed up the average
1702 case. */
1704 /* Flags that are passed to some parsing functions. These values can
1705 be bitwise-ored together. */
1707 enum
1709 /* No flags. */
1710 CP_PARSER_FLAGS_NONE = 0x0,
1711 /* The construct is optional. If it is not present, then no error
1712 should be issued. */
1713 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1714 /* When parsing a type-specifier, treat user-defined type-names
1715 as non-type identifiers. */
1716 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1717 /* When parsing a type-specifier, do not try to parse a class-specifier
1718 or enum-specifier. */
1719 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1720 /* When parsing a decl-specifier-seq, only allow type-specifier or
1721 constexpr. */
1722 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1725 /* This type is used for parameters and variables which hold
1726 combinations of the above flags. */
1727 typedef int cp_parser_flags;
1729 /* The different kinds of declarators we want to parse. */
1731 typedef enum cp_parser_declarator_kind
1733 /* We want an abstract declarator. */
1734 CP_PARSER_DECLARATOR_ABSTRACT,
1735 /* We want a named declarator. */
1736 CP_PARSER_DECLARATOR_NAMED,
1737 /* We don't mind, but the name must be an unqualified-id. */
1738 CP_PARSER_DECLARATOR_EITHER
1739 } cp_parser_declarator_kind;
1741 /* The precedence values used to parse binary expressions. The minimum value
1742 of PREC must be 1, because zero is reserved to quickly discriminate
1743 binary operators from other tokens. */
1745 enum cp_parser_prec
1747 PREC_NOT_OPERATOR,
1748 PREC_LOGICAL_OR_EXPRESSION,
1749 PREC_LOGICAL_AND_EXPRESSION,
1750 PREC_INCLUSIVE_OR_EXPRESSION,
1751 PREC_EXCLUSIVE_OR_EXPRESSION,
1752 PREC_AND_EXPRESSION,
1753 PREC_EQUALITY_EXPRESSION,
1754 PREC_RELATIONAL_EXPRESSION,
1755 PREC_SHIFT_EXPRESSION,
1756 PREC_ADDITIVE_EXPRESSION,
1757 PREC_MULTIPLICATIVE_EXPRESSION,
1758 PREC_PM_EXPRESSION,
1759 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1762 /* A mapping from a token type to a corresponding tree node type, with a
1763 precedence value. */
1765 typedef struct cp_parser_binary_operations_map_node
1767 /* The token type. */
1768 enum cpp_ttype token_type;
1769 /* The corresponding tree code. */
1770 enum tree_code tree_type;
1771 /* The precedence of this operator. */
1772 enum cp_parser_prec prec;
1773 } cp_parser_binary_operations_map_node;
1775 typedef struct cp_parser_expression_stack_entry
1777 /* Left hand side of the binary operation we are currently
1778 parsing. */
1779 tree lhs;
1780 /* Original tree code for left hand side, if it was a binary
1781 expression itself (used for -Wparentheses). */
1782 enum tree_code lhs_type;
1783 /* Tree code for the binary operation we are parsing. */
1784 enum tree_code tree_type;
1785 /* Precedence of the binary operation we are parsing. */
1786 enum cp_parser_prec prec;
1787 /* Location of the binary operation we are parsing. */
1788 location_t loc;
1789 } cp_parser_expression_stack_entry;
1791 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1792 entries because precedence levels on the stack are monotonically
1793 increasing. */
1794 typedef struct cp_parser_expression_stack_entry
1795 cp_parser_expression_stack[NUM_PREC_VALUES];
1797 /* Prototypes. */
1799 /* Constructors and destructors. */
1801 static cp_parser_context *cp_parser_context_new
1802 (cp_parser_context *);
1804 /* Class variables. */
1806 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1808 /* The operator-precedence table used by cp_parser_binary_expression.
1809 Transformed into an associative array (binops_by_token) by
1810 cp_parser_new. */
1812 static const cp_parser_binary_operations_map_node binops[] = {
1813 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1814 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1816 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1817 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1818 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1820 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1821 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1823 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1824 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1826 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1827 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1828 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1829 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1831 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1832 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1834 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1836 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1838 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1840 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1842 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1845 /* The same as binops, but initialized by cp_parser_new so that
1846 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1847 for speed. */
1848 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1850 /* Constructors and destructors. */
1852 /* Construct a new context. The context below this one on the stack
1853 is given by NEXT. */
1855 static cp_parser_context *
1856 cp_parser_context_new (cp_parser_context* next)
1858 cp_parser_context *context;
1860 /* Allocate the storage. */
1861 if (cp_parser_context_free_list != NULL)
1863 /* Pull the first entry from the free list. */
1864 context = cp_parser_context_free_list;
1865 cp_parser_context_free_list = context->next;
1866 memset (context, 0, sizeof (*context));
1868 else
1869 context = ggc_cleared_alloc<cp_parser_context> ();
1871 /* No errors have occurred yet in this context. */
1872 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1873 /* If this is not the bottommost context, copy information that we
1874 need from the previous context. */
1875 if (next)
1877 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1878 expression, then we are parsing one in this context, too. */
1879 context->object_type = next->object_type;
1880 /* Thread the stack. */
1881 context->next = next;
1884 return context;
1887 /* Managing the unparsed function queues. */
1889 #define unparsed_funs_with_default_args \
1890 parser->unparsed_queues->last ().funs_with_default_args
1891 #define unparsed_funs_with_definitions \
1892 parser->unparsed_queues->last ().funs_with_definitions
1893 #define unparsed_nsdmis \
1894 parser->unparsed_queues->last ().nsdmis
1895 #define unparsed_classes \
1896 parser->unparsed_queues->last ().classes
1898 static void
1899 push_unparsed_function_queues (cp_parser *parser)
1901 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1902 vec_safe_push (parser->unparsed_queues, e);
1905 static void
1906 pop_unparsed_function_queues (cp_parser *parser)
1908 release_tree_vector (unparsed_funs_with_definitions);
1909 parser->unparsed_queues->pop ();
1912 /* Prototypes. */
1914 /* Constructors and destructors. */
1916 static cp_parser *cp_parser_new
1917 (void);
1919 /* Routines to parse various constructs.
1921 Those that return `tree' will return the error_mark_node (rather
1922 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1923 Sometimes, they will return an ordinary node if error-recovery was
1924 attempted, even though a parse error occurred. So, to check
1925 whether or not a parse error occurred, you should always use
1926 cp_parser_error_occurred. If the construct is optional (indicated
1927 either by an `_opt' in the name of the function that does the
1928 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1929 the construct is not present. */
1931 /* Lexical conventions [gram.lex] */
1933 static tree cp_parser_identifier
1934 (cp_parser *);
1935 static tree cp_parser_string_literal
1936 (cp_parser *, bool, bool, bool);
1937 static tree cp_parser_userdef_char_literal
1938 (cp_parser *);
1939 static tree cp_parser_userdef_string_literal
1940 (tree);
1941 static tree cp_parser_userdef_numeric_literal
1942 (cp_parser *);
1944 /* Basic concepts [gram.basic] */
1946 static bool cp_parser_translation_unit
1947 (cp_parser *);
1949 /* Expressions [gram.expr] */
1951 static tree cp_parser_primary_expression
1952 (cp_parser *, bool, bool, bool, cp_id_kind *);
1953 static tree cp_parser_id_expression
1954 (cp_parser *, bool, bool, bool *, bool, bool);
1955 static tree cp_parser_unqualified_id
1956 (cp_parser *, bool, bool, bool, bool);
1957 static tree cp_parser_nested_name_specifier_opt
1958 (cp_parser *, bool, bool, bool, bool);
1959 static tree cp_parser_nested_name_specifier
1960 (cp_parser *, bool, bool, bool, bool);
1961 static tree cp_parser_qualifying_entity
1962 (cp_parser *, bool, bool, bool, bool, bool);
1963 static tree cp_parser_postfix_expression
1964 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_postfix_open_square_expression
1966 (cp_parser *, tree, bool, bool);
1967 static tree cp_parser_postfix_dot_deref_expression
1968 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1969 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1970 (cp_parser *, int, bool, bool, bool *, bool = false);
1971 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1972 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1973 static void cp_parser_pseudo_destructor_name
1974 (cp_parser *, tree, tree *, tree *);
1975 static tree cp_parser_unary_expression
1976 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1977 static enum tree_code cp_parser_unary_operator
1978 (cp_token *);
1979 static tree cp_parser_new_expression
1980 (cp_parser *);
1981 static vec<tree, va_gc> *cp_parser_new_placement
1982 (cp_parser *);
1983 static tree cp_parser_new_type_id
1984 (cp_parser *, tree *);
1985 static cp_declarator *cp_parser_new_declarator_opt
1986 (cp_parser *);
1987 static cp_declarator *cp_parser_direct_new_declarator
1988 (cp_parser *);
1989 static vec<tree, va_gc> *cp_parser_new_initializer
1990 (cp_parser *);
1991 static tree cp_parser_delete_expression
1992 (cp_parser *);
1993 static tree cp_parser_cast_expression
1994 (cp_parser *, bool, bool, bool, cp_id_kind *);
1995 static tree cp_parser_binary_expression
1996 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1997 static tree cp_parser_question_colon_clause
1998 (cp_parser *, tree);
1999 static tree cp_parser_assignment_expression
2000 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2001 static enum tree_code cp_parser_assignment_operator_opt
2002 (cp_parser *);
2003 static tree cp_parser_expression
2004 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2005 static tree cp_parser_constant_expression
2006 (cp_parser *, bool = false, bool * = NULL);
2007 static tree cp_parser_builtin_offsetof
2008 (cp_parser *);
2009 static tree cp_parser_lambda_expression
2010 (cp_parser *);
2011 static void cp_parser_lambda_introducer
2012 (cp_parser *, tree);
2013 static bool cp_parser_lambda_declarator_opt
2014 (cp_parser *, tree);
2015 static void cp_parser_lambda_body
2016 (cp_parser *, tree);
2018 /* Statements [gram.stmt.stmt] */
2020 static void cp_parser_statement
2021 (cp_parser *, tree, bool, bool *);
2022 static void cp_parser_label_for_labeled_statement
2023 (cp_parser *, tree);
2024 static tree cp_parser_expression_statement
2025 (cp_parser *, tree);
2026 static tree cp_parser_compound_statement
2027 (cp_parser *, tree, bool, bool);
2028 static void cp_parser_statement_seq_opt
2029 (cp_parser *, tree);
2030 static tree cp_parser_selection_statement
2031 (cp_parser *, bool *);
2032 static tree cp_parser_condition
2033 (cp_parser *);
2034 static tree cp_parser_iteration_statement
2035 (cp_parser *, bool);
2036 static bool cp_parser_for_init_statement
2037 (cp_parser *, tree *decl);
2038 static tree cp_parser_for
2039 (cp_parser *, bool);
2040 static tree cp_parser_c_for
2041 (cp_parser *, tree, tree, bool);
2042 static tree cp_parser_range_for
2043 (cp_parser *, tree, tree, tree, bool);
2044 static void do_range_for_auto_deduction
2045 (tree, tree);
2046 static tree cp_parser_perform_range_for_lookup
2047 (tree, tree *, tree *);
2048 static tree cp_parser_range_for_member_function
2049 (tree, tree);
2050 static tree cp_parser_jump_statement
2051 (cp_parser *);
2052 static void cp_parser_declaration_statement
2053 (cp_parser *);
2055 static tree cp_parser_implicitly_scoped_statement
2056 (cp_parser *, bool *, location_t, const char *);
2057 static void cp_parser_already_scoped_statement
2058 (cp_parser *, location_t, const char *);
2060 /* Declarations [gram.dcl.dcl] */
2062 static void cp_parser_declaration_seq_opt
2063 (cp_parser *);
2064 static void cp_parser_declaration
2065 (cp_parser *);
2066 static void cp_parser_block_declaration
2067 (cp_parser *, bool);
2068 static void cp_parser_simple_declaration
2069 (cp_parser *, bool, tree *);
2070 static void cp_parser_decl_specifier_seq
2071 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2072 static tree cp_parser_storage_class_specifier_opt
2073 (cp_parser *);
2074 static tree cp_parser_function_specifier_opt
2075 (cp_parser *, cp_decl_specifier_seq *);
2076 static tree cp_parser_type_specifier
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2078 int *, bool *);
2079 static tree cp_parser_simple_type_specifier
2080 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2081 static tree cp_parser_type_name
2082 (cp_parser *);
2083 static tree cp_parser_nonclass_name
2084 (cp_parser* parser);
2085 static tree cp_parser_elaborated_type_specifier
2086 (cp_parser *, bool, bool);
2087 static tree cp_parser_enum_specifier
2088 (cp_parser *);
2089 static void cp_parser_enumerator_list
2090 (cp_parser *, tree);
2091 static void cp_parser_enumerator_definition
2092 (cp_parser *, tree);
2093 static tree cp_parser_namespace_name
2094 (cp_parser *);
2095 static void cp_parser_namespace_definition
2096 (cp_parser *);
2097 static void cp_parser_namespace_body
2098 (cp_parser *);
2099 static tree cp_parser_qualified_namespace_specifier
2100 (cp_parser *);
2101 static void cp_parser_namespace_alias_definition
2102 (cp_parser *);
2103 static bool cp_parser_using_declaration
2104 (cp_parser *, bool);
2105 static void cp_parser_using_directive
2106 (cp_parser *);
2107 static tree cp_parser_alias_declaration
2108 (cp_parser *);
2109 static void cp_parser_asm_definition
2110 (cp_parser *);
2111 static void cp_parser_linkage_specification
2112 (cp_parser *);
2113 static void cp_parser_static_assert
2114 (cp_parser *, bool);
2115 static tree cp_parser_decltype
2116 (cp_parser *);
2118 /* Declarators [gram.dcl.decl] */
2120 static tree cp_parser_init_declarator
2121 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2122 bool, bool, int, bool *, tree *, location_t *);
2123 static cp_declarator *cp_parser_declarator
2124 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2125 static cp_declarator *cp_parser_direct_declarator
2126 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2127 static enum tree_code cp_parser_ptr_operator
2128 (cp_parser *, tree *, cp_cv_quals *, tree *);
2129 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2130 (cp_parser *);
2131 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2132 (cp_parser *);
2133 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2134 (cp_parser *);
2135 static tree cp_parser_late_return_type_opt
2136 (cp_parser *, cp_declarator *, cp_cv_quals);
2137 static tree cp_parser_declarator_id
2138 (cp_parser *, bool);
2139 static tree cp_parser_type_id
2140 (cp_parser *);
2141 static tree cp_parser_template_type_arg
2142 (cp_parser *);
2143 static tree cp_parser_trailing_type_id (cp_parser *);
2144 static tree cp_parser_type_id_1
2145 (cp_parser *, bool, bool);
2146 static void cp_parser_type_specifier_seq
2147 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2148 static tree cp_parser_parameter_declaration_clause
2149 (cp_parser *);
2150 static tree cp_parser_parameter_declaration_list
2151 (cp_parser *, bool *);
2152 static cp_parameter_declarator *cp_parser_parameter_declaration
2153 (cp_parser *, bool, bool *);
2154 static tree cp_parser_default_argument
2155 (cp_parser *, bool);
2156 static void cp_parser_function_body
2157 (cp_parser *, bool);
2158 static tree cp_parser_initializer
2159 (cp_parser *, bool *, bool *);
2160 static tree cp_parser_initializer_clause
2161 (cp_parser *, bool *);
2162 static tree cp_parser_braced_list
2163 (cp_parser*, bool*);
2164 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2165 (cp_parser *, bool *);
2167 static bool cp_parser_ctor_initializer_opt_and_function_body
2168 (cp_parser *, bool);
2170 static tree cp_parser_late_parsing_omp_declare_simd
2171 (cp_parser *, tree);
2173 static tree cp_parser_late_parsing_cilk_simd_fn_info
2174 (cp_parser *, tree);
2176 static tree synthesize_implicit_template_parm
2177 (cp_parser *);
2178 static tree finish_fully_implicit_template
2179 (cp_parser *, tree);
2181 /* Classes [gram.class] */
2183 static tree cp_parser_class_name
2184 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2185 static tree cp_parser_class_specifier
2186 (cp_parser *);
2187 static tree cp_parser_class_head
2188 (cp_parser *, bool *);
2189 static enum tag_types cp_parser_class_key
2190 (cp_parser *);
2191 static void cp_parser_type_parameter_key
2192 (cp_parser* parser);
2193 static void cp_parser_member_specification_opt
2194 (cp_parser *);
2195 static void cp_parser_member_declaration
2196 (cp_parser *);
2197 static tree cp_parser_pure_specifier
2198 (cp_parser *);
2199 static tree cp_parser_constant_initializer
2200 (cp_parser *);
2202 /* Derived classes [gram.class.derived] */
2204 static tree cp_parser_base_clause
2205 (cp_parser *);
2206 static tree cp_parser_base_specifier
2207 (cp_parser *);
2209 /* Special member functions [gram.special] */
2211 static tree cp_parser_conversion_function_id
2212 (cp_parser *);
2213 static tree cp_parser_conversion_type_id
2214 (cp_parser *);
2215 static cp_declarator *cp_parser_conversion_declarator_opt
2216 (cp_parser *);
2217 static bool cp_parser_ctor_initializer_opt
2218 (cp_parser *);
2219 static void cp_parser_mem_initializer_list
2220 (cp_parser *);
2221 static tree cp_parser_mem_initializer
2222 (cp_parser *);
2223 static tree cp_parser_mem_initializer_id
2224 (cp_parser *);
2226 /* Overloading [gram.over] */
2228 static tree cp_parser_operator_function_id
2229 (cp_parser *);
2230 static tree cp_parser_operator
2231 (cp_parser *);
2233 /* Templates [gram.temp] */
2235 static void cp_parser_template_declaration
2236 (cp_parser *, bool);
2237 static tree cp_parser_template_parameter_list
2238 (cp_parser *);
2239 static tree cp_parser_template_parameter
2240 (cp_parser *, bool *, bool *);
2241 static tree cp_parser_type_parameter
2242 (cp_parser *, bool *);
2243 static tree cp_parser_template_id
2244 (cp_parser *, bool, bool, enum tag_types, bool);
2245 static tree cp_parser_template_name
2246 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2247 static tree cp_parser_template_argument_list
2248 (cp_parser *);
2249 static tree cp_parser_template_argument
2250 (cp_parser *);
2251 static void cp_parser_explicit_instantiation
2252 (cp_parser *);
2253 static void cp_parser_explicit_specialization
2254 (cp_parser *);
2256 /* Exception handling [gram.exception] */
2258 static tree cp_parser_try_block
2259 (cp_parser *);
2260 static bool cp_parser_function_try_block
2261 (cp_parser *);
2262 static void cp_parser_handler_seq
2263 (cp_parser *);
2264 static void cp_parser_handler
2265 (cp_parser *);
2266 static tree cp_parser_exception_declaration
2267 (cp_parser *);
2268 static tree cp_parser_throw_expression
2269 (cp_parser *);
2270 static tree cp_parser_exception_specification_opt
2271 (cp_parser *);
2272 static tree cp_parser_type_id_list
2273 (cp_parser *);
2275 /* GNU Extensions */
2277 static tree cp_parser_asm_specification_opt
2278 (cp_parser *);
2279 static tree cp_parser_asm_operand_list
2280 (cp_parser *);
2281 static tree cp_parser_asm_clobber_list
2282 (cp_parser *);
2283 static tree cp_parser_asm_label_list
2284 (cp_parser *);
2285 static bool cp_next_tokens_can_be_attribute_p
2286 (cp_parser *);
2287 static bool cp_next_tokens_can_be_gnu_attribute_p
2288 (cp_parser *);
2289 static bool cp_next_tokens_can_be_std_attribute_p
2290 (cp_parser *);
2291 static bool cp_nth_tokens_can_be_std_attribute_p
2292 (cp_parser *, size_t);
2293 static bool cp_nth_tokens_can_be_gnu_attribute_p
2294 (cp_parser *, size_t);
2295 static bool cp_nth_tokens_can_be_attribute_p
2296 (cp_parser *, size_t);
2297 static tree cp_parser_attributes_opt
2298 (cp_parser *);
2299 static tree cp_parser_gnu_attributes_opt
2300 (cp_parser *);
2301 static tree cp_parser_gnu_attribute_list
2302 (cp_parser *);
2303 static tree cp_parser_std_attribute
2304 (cp_parser *);
2305 static tree cp_parser_std_attribute_spec
2306 (cp_parser *);
2307 static tree cp_parser_std_attribute_spec_seq
2308 (cp_parser *);
2309 static bool cp_parser_extension_opt
2310 (cp_parser *, int *);
2311 static void cp_parser_label_declaration
2312 (cp_parser *);
2314 /* Transactional Memory Extensions */
2316 static tree cp_parser_transaction
2317 (cp_parser *, enum rid);
2318 static tree cp_parser_transaction_expression
2319 (cp_parser *, enum rid);
2320 static bool cp_parser_function_transaction
2321 (cp_parser *, enum rid);
2322 static tree cp_parser_transaction_cancel
2323 (cp_parser *);
2325 enum pragma_context {
2326 pragma_external,
2327 pragma_member,
2328 pragma_objc_icode,
2329 pragma_stmt,
2330 pragma_compound
2332 static bool cp_parser_pragma
2333 (cp_parser *, enum pragma_context);
2335 /* Objective-C++ Productions */
2337 static tree cp_parser_objc_message_receiver
2338 (cp_parser *);
2339 static tree cp_parser_objc_message_args
2340 (cp_parser *);
2341 static tree cp_parser_objc_message_expression
2342 (cp_parser *);
2343 static tree cp_parser_objc_encode_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_defs_expression
2346 (cp_parser *);
2347 static tree cp_parser_objc_protocol_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_selector_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_expression
2352 (cp_parser *);
2353 static bool cp_parser_objc_selector_p
2354 (enum cpp_ttype);
2355 static tree cp_parser_objc_selector
2356 (cp_parser *);
2357 static tree cp_parser_objc_protocol_refs_opt
2358 (cp_parser *);
2359 static void cp_parser_objc_declaration
2360 (cp_parser *, tree);
2361 static tree cp_parser_objc_statement
2362 (cp_parser *);
2363 static bool cp_parser_objc_valid_prefix_attributes
2364 (cp_parser *, tree *);
2365 static void cp_parser_objc_at_property_declaration
2366 (cp_parser *) ;
2367 static void cp_parser_objc_at_synthesize_declaration
2368 (cp_parser *) ;
2369 static void cp_parser_objc_at_dynamic_declaration
2370 (cp_parser *) ;
2371 static tree cp_parser_objc_struct_declaration
2372 (cp_parser *) ;
2374 /* Utility Routines */
2376 static tree cp_parser_lookup_name
2377 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2378 static tree cp_parser_lookup_name_simple
2379 (cp_parser *, tree, location_t);
2380 static tree cp_parser_maybe_treat_template_as_class
2381 (tree, bool);
2382 static bool cp_parser_check_declarator_template_parameters
2383 (cp_parser *, cp_declarator *, location_t);
2384 static bool cp_parser_check_template_parameters
2385 (cp_parser *, unsigned, location_t, cp_declarator *);
2386 static tree cp_parser_simple_cast_expression
2387 (cp_parser *);
2388 static tree cp_parser_global_scope_opt
2389 (cp_parser *, bool);
2390 static bool cp_parser_constructor_declarator_p
2391 (cp_parser *, bool);
2392 static tree cp_parser_function_definition_from_specifiers_and_declarator
2393 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2394 static tree cp_parser_function_definition_after_declarator
2395 (cp_parser *, bool);
2396 static void cp_parser_template_declaration_after_export
2397 (cp_parser *, bool);
2398 static void cp_parser_perform_template_parameter_access_checks
2399 (vec<deferred_access_check, va_gc> *);
2400 static tree cp_parser_single_declaration
2401 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2402 static tree cp_parser_functional_cast
2403 (cp_parser *, tree);
2404 static tree cp_parser_save_member_function_body
2405 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2406 static tree cp_parser_save_nsdmi
2407 (cp_parser *);
2408 static tree cp_parser_enclosed_template_argument_list
2409 (cp_parser *);
2410 static void cp_parser_save_default_args
2411 (cp_parser *, tree);
2412 static void cp_parser_late_parsing_for_member
2413 (cp_parser *, tree);
2414 static tree cp_parser_late_parse_one_default_arg
2415 (cp_parser *, tree, tree, tree);
2416 static void cp_parser_late_parsing_nsdmi
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_default_args
2419 (cp_parser *, tree);
2420 static tree cp_parser_sizeof_operand
2421 (cp_parser *, enum rid);
2422 static tree cp_parser_trait_expr
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_declares_only_class_p
2425 (cp_parser *);
2426 static void cp_parser_set_storage_class
2427 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2428 static void cp_parser_set_decl_spec_type
2429 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2430 static void set_and_check_decl_spec_loc
2431 (cp_decl_specifier_seq *decl_specs,
2432 cp_decl_spec ds, cp_token *);
2433 static bool cp_parser_friend_p
2434 (const cp_decl_specifier_seq *);
2435 static void cp_parser_required_error
2436 (cp_parser *, required_token, bool);
2437 static cp_token *cp_parser_require
2438 (cp_parser *, enum cpp_ttype, required_token);
2439 static cp_token *cp_parser_require_keyword
2440 (cp_parser *, enum rid, required_token);
2441 static bool cp_parser_token_starts_function_definition_p
2442 (cp_token *);
2443 static bool cp_parser_next_token_starts_class_definition_p
2444 (cp_parser *);
2445 static bool cp_parser_next_token_ends_template_argument_p
2446 (cp_parser *);
2447 static bool cp_parser_nth_token_starts_template_argument_list_p
2448 (cp_parser *, size_t);
2449 static enum tag_types cp_parser_token_is_class_key
2450 (cp_token *);
2451 static enum tag_types cp_parser_token_is_type_parameter_key
2452 (cp_token *);
2453 static void cp_parser_check_class_key
2454 (enum tag_types, tree type);
2455 static void cp_parser_check_access_in_redeclaration
2456 (tree type, location_t location);
2457 static bool cp_parser_optional_template_keyword
2458 (cp_parser *);
2459 static void cp_parser_pre_parsed_nested_name_specifier
2460 (cp_parser *);
2461 static bool cp_parser_cache_group
2462 (cp_parser *, enum cpp_ttype, unsigned);
2463 static tree cp_parser_cache_defarg
2464 (cp_parser *parser, bool nsdmi);
2465 static void cp_parser_parse_tentatively
2466 (cp_parser *);
2467 static void cp_parser_commit_to_tentative_parse
2468 (cp_parser *);
2469 static void cp_parser_commit_to_topmost_tentative_parse
2470 (cp_parser *);
2471 static void cp_parser_abort_tentative_parse
2472 (cp_parser *);
2473 static bool cp_parser_parse_definitely
2474 (cp_parser *);
2475 static inline bool cp_parser_parsing_tentatively
2476 (cp_parser *);
2477 static bool cp_parser_uncommitted_to_tentative_parse_p
2478 (cp_parser *);
2479 static void cp_parser_error
2480 (cp_parser *, const char *);
2481 static void cp_parser_name_lookup_error
2482 (cp_parser *, tree, tree, name_lookup_error, location_t);
2483 static bool cp_parser_simulate_error
2484 (cp_parser *);
2485 static bool cp_parser_check_type_definition
2486 (cp_parser *);
2487 static void cp_parser_check_for_definition_in_return_type
2488 (cp_declarator *, tree, location_t type_location);
2489 static void cp_parser_check_for_invalid_template_id
2490 (cp_parser *, tree, enum tag_types, location_t location);
2491 static bool cp_parser_non_integral_constant_expression
2492 (cp_parser *, non_integral_constant);
2493 static void cp_parser_diagnose_invalid_type_name
2494 (cp_parser *, tree, location_t);
2495 static bool cp_parser_parse_and_diagnose_invalid_type_name
2496 (cp_parser *);
2497 static int cp_parser_skip_to_closing_parenthesis
2498 (cp_parser *, bool, bool, bool);
2499 static void cp_parser_skip_to_end_of_statement
2500 (cp_parser *);
2501 static void cp_parser_consume_semicolon_at_end_of_statement
2502 (cp_parser *);
2503 static void cp_parser_skip_to_end_of_block_or_statement
2504 (cp_parser *);
2505 static bool cp_parser_skip_to_closing_brace
2506 (cp_parser *);
2507 static void cp_parser_skip_to_end_of_template_parameter_list
2508 (cp_parser *);
2509 static void cp_parser_skip_to_pragma_eol
2510 (cp_parser*, cp_token *);
2511 static bool cp_parser_error_occurred
2512 (cp_parser *);
2513 static bool cp_parser_allow_gnu_extensions_p
2514 (cp_parser *);
2515 static bool cp_parser_is_pure_string_literal
2516 (cp_token *);
2517 static bool cp_parser_is_string_literal
2518 (cp_token *);
2519 static bool cp_parser_is_keyword
2520 (cp_token *, enum rid);
2521 static tree cp_parser_make_typename_type
2522 (cp_parser *, tree, location_t location);
2523 static cp_declarator * cp_parser_make_indirect_declarator
2524 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2525 static bool cp_parser_compound_literal_p
2526 (cp_parser *);
2527 static bool cp_parser_array_designator_p
2528 (cp_parser *);
2529 static bool cp_parser_skip_to_closing_square_bracket
2530 (cp_parser *);
2532 /* Returns nonzero if we are parsing tentatively. */
2534 static inline bool
2535 cp_parser_parsing_tentatively (cp_parser* parser)
2537 return parser->context->next != NULL;
2540 /* Returns nonzero if TOKEN is a string literal. */
2542 static bool
2543 cp_parser_is_pure_string_literal (cp_token* token)
2545 return (token->type == CPP_STRING ||
2546 token->type == CPP_STRING16 ||
2547 token->type == CPP_STRING32 ||
2548 token->type == CPP_WSTRING ||
2549 token->type == CPP_UTF8STRING);
2552 /* Returns nonzero if TOKEN is a string literal
2553 of a user-defined string literal. */
2555 static bool
2556 cp_parser_is_string_literal (cp_token* token)
2558 return (cp_parser_is_pure_string_literal (token) ||
2559 token->type == CPP_STRING_USERDEF ||
2560 token->type == CPP_STRING16_USERDEF ||
2561 token->type == CPP_STRING32_USERDEF ||
2562 token->type == CPP_WSTRING_USERDEF ||
2563 token->type == CPP_UTF8STRING_USERDEF);
2566 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2568 static bool
2569 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2571 return token->keyword == keyword;
2574 /* If not parsing tentatively, issue a diagnostic of the form
2575 FILE:LINE: MESSAGE before TOKEN
2576 where TOKEN is the next token in the input stream. MESSAGE
2577 (specified by the caller) is usually of the form "expected
2578 OTHER-TOKEN". */
2580 static void
2581 cp_parser_error (cp_parser* parser, const char* gmsgid)
2583 if (!cp_parser_simulate_error (parser))
2585 cp_token *token = cp_lexer_peek_token (parser->lexer);
2586 /* This diagnostic makes more sense if it is tagged to the line
2587 of the token we just peeked at. */
2588 cp_lexer_set_source_position_from_token (token);
2590 if (token->type == CPP_PRAGMA)
2592 error_at (token->location,
2593 "%<#pragma%> is not allowed here");
2594 cp_parser_skip_to_pragma_eol (parser, token);
2595 return;
2598 c_parse_error (gmsgid,
2599 /* Because c_parser_error does not understand
2600 CPP_KEYWORD, keywords are treated like
2601 identifiers. */
2602 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2603 token->u.value, token->flags);
2607 /* Issue an error about name-lookup failing. NAME is the
2608 IDENTIFIER_NODE DECL is the result of
2609 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2610 the thing that we hoped to find. */
2612 static void
2613 cp_parser_name_lookup_error (cp_parser* parser,
2614 tree name,
2615 tree decl,
2616 name_lookup_error desired,
2617 location_t location)
2619 /* If name lookup completely failed, tell the user that NAME was not
2620 declared. */
2621 if (decl == error_mark_node)
2623 if (parser->scope && parser->scope != global_namespace)
2624 error_at (location, "%<%E::%E%> has not been declared",
2625 parser->scope, name);
2626 else if (parser->scope == global_namespace)
2627 error_at (location, "%<::%E%> has not been declared", name);
2628 else if (parser->object_scope
2629 && !CLASS_TYPE_P (parser->object_scope))
2630 error_at (location, "request for member %qE in non-class type %qT",
2631 name, parser->object_scope);
2632 else if (parser->object_scope)
2633 error_at (location, "%<%T::%E%> has not been declared",
2634 parser->object_scope, name);
2635 else
2636 error_at (location, "%qE has not been declared", name);
2638 else if (parser->scope && parser->scope != global_namespace)
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%<%E::%E%> is not a type",
2644 parser->scope, name);
2645 break;
2646 case NLE_CXX98:
2647 error_at (location, "%<%E::%E%> is not a class or namespace",
2648 parser->scope, name);
2649 break;
2650 case NLE_NOT_CXX98:
2651 error_at (location,
2652 "%<%E::%E%> is not a class, namespace, or enumeration",
2653 parser->scope, name);
2654 break;
2655 default:
2656 gcc_unreachable ();
2660 else if (parser->scope == global_namespace)
2662 switch (desired)
2664 case NLE_TYPE:
2665 error_at (location, "%<::%E%> is not a type", name);
2666 break;
2667 case NLE_CXX98:
2668 error_at (location, "%<::%E%> is not a class or namespace", name);
2669 break;
2670 case NLE_NOT_CXX98:
2671 error_at (location,
2672 "%<::%E%> is not a class, namespace, or enumeration",
2673 name);
2674 break;
2675 default:
2676 gcc_unreachable ();
2679 else
2681 switch (desired)
2683 case NLE_TYPE:
2684 error_at (location, "%qE is not a type", name);
2685 break;
2686 case NLE_CXX98:
2687 error_at (location, "%qE is not a class or namespace", name);
2688 break;
2689 case NLE_NOT_CXX98:
2690 error_at (location,
2691 "%qE is not a class, namespace, or enumeration", name);
2692 break;
2693 default:
2694 gcc_unreachable ();
2699 /* If we are parsing tentatively, remember that an error has occurred
2700 during this tentative parse. Returns true if the error was
2701 simulated; false if a message should be issued by the caller. */
2703 static bool
2704 cp_parser_simulate_error (cp_parser* parser)
2706 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2708 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2709 return true;
2711 return false;
2714 /* This function is called when a type is defined. If type
2715 definitions are forbidden at this point, an error message is
2716 issued. */
2718 static bool
2719 cp_parser_check_type_definition (cp_parser* parser)
2721 /* If types are forbidden here, issue a message. */
2722 if (parser->type_definition_forbidden_message)
2724 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2725 in the message need to be interpreted. */
2726 error (parser->type_definition_forbidden_message);
2727 return false;
2729 return true;
2732 /* This function is called when the DECLARATOR is processed. The TYPE
2733 was a type defined in the decl-specifiers. If it is invalid to
2734 define a type in the decl-specifiers for DECLARATOR, an error is
2735 issued. TYPE_LOCATION is the location of TYPE and is used
2736 for error reporting. */
2738 static void
2739 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2740 tree type, location_t type_location)
2742 /* [dcl.fct] forbids type definitions in return types.
2743 Unfortunately, it's not easy to know whether or not we are
2744 processing a return type until after the fact. */
2745 while (declarator
2746 && (declarator->kind == cdk_pointer
2747 || declarator->kind == cdk_reference
2748 || declarator->kind == cdk_ptrmem))
2749 declarator = declarator->declarator;
2750 if (declarator
2751 && declarator->kind == cdk_function)
2753 error_at (type_location,
2754 "new types may not be defined in a return type");
2755 inform (type_location,
2756 "(perhaps a semicolon is missing after the definition of %qT)",
2757 type);
2761 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2762 "<" in any valid C++ program. If the next token is indeed "<",
2763 issue a message warning the user about what appears to be an
2764 invalid attempt to form a template-id. LOCATION is the location
2765 of the type-specifier (TYPE) */
2767 static void
2768 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2769 tree type,
2770 enum tag_types tag_type,
2771 location_t location)
2773 cp_token_position start = 0;
2775 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2777 if (TYPE_P (type))
2778 error_at (location, "%qT is not a template", type);
2779 else if (identifier_p (type))
2781 if (tag_type != none_type)
2782 error_at (location, "%qE is not a class template", type);
2783 else
2784 error_at (location, "%qE is not a template", type);
2786 else
2787 error_at (location, "invalid template-id");
2788 /* Remember the location of the invalid "<". */
2789 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2790 start = cp_lexer_token_position (parser->lexer, true);
2791 /* Consume the "<". */
2792 cp_lexer_consume_token (parser->lexer);
2793 /* Parse the template arguments. */
2794 cp_parser_enclosed_template_argument_list (parser);
2795 /* Permanently remove the invalid template arguments so that
2796 this error message is not issued again. */
2797 if (start)
2798 cp_lexer_purge_tokens_after (parser->lexer, start);
2802 /* If parsing an integral constant-expression, issue an error message
2803 about the fact that THING appeared and return true. Otherwise,
2804 return false. In either case, set
2805 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2807 static bool
2808 cp_parser_non_integral_constant_expression (cp_parser *parser,
2809 non_integral_constant thing)
2811 parser->non_integral_constant_expression_p = true;
2812 if (parser->integral_constant_expression_p)
2814 if (!parser->allow_non_integral_constant_expression_p)
2816 const char *msg = NULL;
2817 switch (thing)
2819 case NIC_FLOAT:
2820 error ("floating-point literal "
2821 "cannot appear in a constant-expression");
2822 return true;
2823 case NIC_CAST:
2824 error ("a cast to a type other than an integral or "
2825 "enumeration type cannot appear in a "
2826 "constant-expression");
2827 return true;
2828 case NIC_TYPEID:
2829 error ("%<typeid%> operator "
2830 "cannot appear in a constant-expression");
2831 return true;
2832 case NIC_NCC:
2833 error ("non-constant compound literals "
2834 "cannot appear in a constant-expression");
2835 return true;
2836 case NIC_FUNC_CALL:
2837 error ("a function call "
2838 "cannot appear in a constant-expression");
2839 return true;
2840 case NIC_INC:
2841 error ("an increment "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_DEC:
2845 error ("an decrement "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_ARRAY_REF:
2849 error ("an array reference "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_ADDR_LABEL:
2853 error ("the address of a label "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_OVERLOADED:
2857 error ("calls to overloaded operators "
2858 "cannot appear in a constant-expression");
2859 return true;
2860 case NIC_ASSIGNMENT:
2861 error ("an assignment cannot appear in a constant-expression");
2862 return true;
2863 case NIC_COMMA:
2864 error ("a comma operator "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_CONSTRUCTOR:
2868 error ("a call to a constructor "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_TRANSACTION:
2872 error ("a transaction expression "
2873 "cannot appear in a constant-expression");
2874 return true;
2875 case NIC_THIS:
2876 msg = "this";
2877 break;
2878 case NIC_FUNC_NAME:
2879 msg = "__FUNCTION__";
2880 break;
2881 case NIC_PRETTY_FUNC:
2882 msg = "__PRETTY_FUNCTION__";
2883 break;
2884 case NIC_C99_FUNC:
2885 msg = "__func__";
2886 break;
2887 case NIC_VA_ARG:
2888 msg = "va_arg";
2889 break;
2890 case NIC_ARROW:
2891 msg = "->";
2892 break;
2893 case NIC_POINT:
2894 msg = ".";
2895 break;
2896 case NIC_STAR:
2897 msg = "*";
2898 break;
2899 case NIC_ADDR:
2900 msg = "&";
2901 break;
2902 case NIC_PREINCREMENT:
2903 msg = "++";
2904 break;
2905 case NIC_PREDECREMENT:
2906 msg = "--";
2907 break;
2908 case NIC_NEW:
2909 msg = "new";
2910 break;
2911 case NIC_DEL:
2912 msg = "delete";
2913 break;
2914 default:
2915 gcc_unreachable ();
2917 if (msg)
2918 error ("%qs cannot appear in a constant-expression", msg);
2919 return true;
2922 return false;
2925 /* Emit a diagnostic for an invalid type name. This function commits
2926 to the current active tentative parse, if any. (Otherwise, the
2927 problematic construct might be encountered again later, resulting
2928 in duplicate error messages.) LOCATION is the location of ID. */
2930 static void
2931 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2932 location_t location)
2934 tree decl, ambiguous_decls;
2935 cp_parser_commit_to_tentative_parse (parser);
2936 /* Try to lookup the identifier. */
2937 decl = cp_parser_lookup_name (parser, id, none_type,
2938 /*is_template=*/false,
2939 /*is_namespace=*/false,
2940 /*check_dependency=*/true,
2941 &ambiguous_decls, location);
2942 if (ambiguous_decls)
2943 /* If the lookup was ambiguous, an error will already have
2944 been issued. */
2945 return;
2946 /* If the lookup found a template-name, it means that the user forgot
2947 to specify an argument list. Emit a useful error message. */
2948 if (DECL_TYPE_TEMPLATE_P (decl))
2950 error_at (location,
2951 "invalid use of template-name %qE without an argument list",
2952 decl);
2953 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2955 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2956 error_at (location, "invalid use of destructor %qD as a type", id);
2957 else if (TREE_CODE (decl) == TYPE_DECL)
2958 /* Something like 'unsigned A a;' */
2959 error_at (location, "invalid combination of multiple type-specifiers");
2960 else if (!parser->scope)
2962 /* Issue an error message. */
2963 error_at (location, "%qE does not name a type", id);
2964 /* If we're in a template class, it's possible that the user was
2965 referring to a type from a base class. For example:
2967 template <typename T> struct A { typedef T X; };
2968 template <typename T> struct B : public A<T> { X x; };
2970 The user should have said "typename A<T>::X". */
2971 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2972 inform (location, "C++11 %<constexpr%> only available with "
2973 "-std=c++11 or -std=gnu++11");
2974 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2975 inform (location, "C++11 %<noexcept%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11
2978 && TREE_CODE (id) == IDENTIFIER_NODE
2979 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2980 inform (location, "C++11 %<thread_local%> only available with "
2981 "-std=c++11 or -std=gnu++11");
2982 else if (processing_template_decl && current_class_type
2983 && TYPE_BINFO (current_class_type))
2985 tree b;
2987 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2989 b = TREE_CHAIN (b))
2991 tree base_type = BINFO_TYPE (b);
2992 if (CLASS_TYPE_P (base_type)
2993 && dependent_type_p (base_type))
2995 tree field;
2996 /* Go from a particular instantiation of the
2997 template (which will have an empty TYPE_FIELDs),
2998 to the main version. */
2999 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3000 for (field = TYPE_FIELDS (base_type);
3001 field;
3002 field = DECL_CHAIN (field))
3003 if (TREE_CODE (field) == TYPE_DECL
3004 && DECL_NAME (field) == id)
3006 inform (location,
3007 "(perhaps %<typename %T::%E%> was intended)",
3008 BINFO_TYPE (b), id);
3009 break;
3011 if (field)
3012 break;
3017 /* Here we diagnose qualified-ids where the scope is actually correct,
3018 but the identifier does not resolve to a valid type name. */
3019 else if (parser->scope != error_mark_node)
3021 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3023 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3024 error_at (location_of (id),
3025 "%qE in namespace %qE does not name a template type",
3026 id, parser->scope);
3027 else
3028 error_at (location_of (id),
3029 "%qE in namespace %qE does not name a type",
3030 id, parser->scope);
3031 if (DECL_P (decl))
3032 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3034 else if (CLASS_TYPE_P (parser->scope)
3035 && constructor_name_p (id, parser->scope))
3037 /* A<T>::A<T>() */
3038 error_at (location, "%<%T::%E%> names the constructor, not"
3039 " the type", parser->scope, id);
3040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3041 error_at (location, "and %qT has no template constructors",
3042 parser->scope);
3044 else if (TYPE_P (parser->scope)
3045 && dependent_scope_p (parser->scope))
3046 error_at (location, "need %<typename%> before %<%T::%E%> because "
3047 "%qT is a dependent scope",
3048 parser->scope, id, parser->scope);
3049 else if (TYPE_P (parser->scope))
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3052 error_at (location_of (id),
3053 "%qE in %q#T does not name a template type",
3054 id, parser->scope);
3055 else
3056 error_at (location_of (id),
3057 "%qE in %q#T does not name a type",
3058 id, parser->scope);
3059 if (DECL_P (decl))
3060 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3062 else
3063 gcc_unreachable ();
3067 /* Check for a common situation where a type-name should be present,
3068 but is not, and issue a sensible error message. Returns true if an
3069 invalid type-name was detected.
3071 The situation handled by this function are variable declarations of the
3072 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3073 Usually, `ID' should name a type, but if we got here it means that it
3074 does not. We try to emit the best possible error message depending on
3075 how exactly the id-expression looks like. */
3077 static bool
3078 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3080 tree id;
3081 cp_token *token = cp_lexer_peek_token (parser->lexer);
3083 /* Avoid duplicate error about ambiguous lookup. */
3084 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3086 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3087 if (next->type == CPP_NAME && next->error_reported)
3088 goto out;
3091 cp_parser_parse_tentatively (parser);
3092 id = cp_parser_id_expression (parser,
3093 /*template_keyword_p=*/false,
3094 /*check_dependency_p=*/true,
3095 /*template_p=*/NULL,
3096 /*declarator_p=*/true,
3097 /*optional_p=*/false);
3098 /* If the next token is a (, this is a function with no explicit return
3099 type, i.e. constructor, destructor or conversion op. */
3100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3101 || TREE_CODE (id) == TYPE_DECL)
3103 cp_parser_abort_tentative_parse (parser);
3104 return false;
3106 if (!cp_parser_parse_definitely (parser))
3107 return false;
3109 /* Emit a diagnostic for the invalid type. */
3110 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3111 out:
3112 /* If we aren't in the middle of a declarator (i.e. in a
3113 parameter-declaration-clause), skip to the end of the declaration;
3114 there's no point in trying to process it. */
3115 if (!parser->in_declarator_p)
3116 cp_parser_skip_to_end_of_block_or_statement (parser);
3117 return true;
3120 /* Consume tokens up to, and including, the next non-nested closing `)'.
3121 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3122 are doing error recovery. Returns -1 if OR_COMMA is true and we
3123 found an unnested comma. */
3125 static int
3126 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3127 bool recovering,
3128 bool or_comma,
3129 bool consume_paren)
3131 unsigned paren_depth = 0;
3132 unsigned brace_depth = 0;
3133 unsigned square_depth = 0;
3135 if (recovering && !or_comma
3136 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3137 return 0;
3139 while (true)
3141 cp_token * token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3145 case CPP_EOF:
3146 case CPP_PRAGMA_EOL:
3147 /* If we've run out of tokens, then there is no closing `)'. */
3148 return 0;
3150 /* This is good for lambda expression capture-lists. */
3151 case CPP_OPEN_SQUARE:
3152 ++square_depth;
3153 break;
3154 case CPP_CLOSE_SQUARE:
3155 if (!square_depth--)
3156 return 0;
3157 break;
3159 case CPP_SEMICOLON:
3160 /* This matches the processing in skip_to_end_of_statement. */
3161 if (!brace_depth)
3162 return 0;
3163 break;
3165 case CPP_OPEN_BRACE:
3166 ++brace_depth;
3167 break;
3168 case CPP_CLOSE_BRACE:
3169 if (!brace_depth--)
3170 return 0;
3171 break;
3173 case CPP_COMMA:
3174 if (recovering && or_comma && !brace_depth && !paren_depth
3175 && !square_depth)
3176 return -1;
3177 break;
3179 case CPP_OPEN_PAREN:
3180 if (!brace_depth)
3181 ++paren_depth;
3182 break;
3184 case CPP_CLOSE_PAREN:
3185 if (!brace_depth && !paren_depth--)
3187 if (consume_paren)
3188 cp_lexer_consume_token (parser->lexer);
3189 return 1;
3191 break;
3193 default:
3194 break;
3197 /* Consume the token. */
3198 cp_lexer_consume_token (parser->lexer);
3202 /* Consume tokens until we reach the end of the current statement.
3203 Normally, that will be just before consuming a `;'. However, if a
3204 non-nested `}' comes first, then we stop before consuming that. */
3206 static void
3207 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3209 unsigned nesting_depth = 0;
3211 /* Unwind generic function template scope if necessary. */
3212 if (parser->fully_implicit_function_template_p)
3213 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3215 while (true)
3217 cp_token *token = cp_lexer_peek_token (parser->lexer);
3219 switch (token->type)
3221 case CPP_EOF:
3222 case CPP_PRAGMA_EOL:
3223 /* If we've run out of tokens, stop. */
3224 return;
3226 case CPP_SEMICOLON:
3227 /* If the next token is a `;', we have reached the end of the
3228 statement. */
3229 if (!nesting_depth)
3230 return;
3231 break;
3233 case CPP_CLOSE_BRACE:
3234 /* If this is a non-nested '}', stop before consuming it.
3235 That way, when confronted with something like:
3237 { 3 + }
3239 we stop before consuming the closing '}', even though we
3240 have not yet reached a `;'. */
3241 if (nesting_depth == 0)
3242 return;
3244 /* If it is the closing '}' for a block that we have
3245 scanned, stop -- but only after consuming the token.
3246 That way given:
3248 void f g () { ... }
3249 typedef int I;
3251 we will stop after the body of the erroneously declared
3252 function, but before consuming the following `typedef'
3253 declaration. */
3254 if (--nesting_depth == 0)
3256 cp_lexer_consume_token (parser->lexer);
3257 return;
3260 case CPP_OPEN_BRACE:
3261 ++nesting_depth;
3262 break;
3264 default:
3265 break;
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser->lexer);
3273 /* This function is called at the end of a statement or declaration.
3274 If the next token is a semicolon, it is consumed; otherwise, error
3275 recovery is attempted. */
3277 static void
3278 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3280 /* Look for the trailing `;'. */
3281 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3283 /* If there is additional (erroneous) input, skip to the end of
3284 the statement. */
3285 cp_parser_skip_to_end_of_statement (parser);
3286 /* If the next token is now a `;', consume it. */
3287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3288 cp_lexer_consume_token (parser->lexer);
3292 /* Skip tokens until we have consumed an entire block, or until we
3293 have consumed a non-nested `;'. */
3295 static void
3296 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3298 int nesting_depth = 0;
3300 /* Unwind generic function template scope if necessary. */
3301 if (parser->fully_implicit_function_template_p)
3302 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3304 while (nesting_depth >= 0)
3306 cp_token *token = cp_lexer_peek_token (parser->lexer);
3308 switch (token->type)
3310 case CPP_EOF:
3311 case CPP_PRAGMA_EOL:
3312 /* If we've run out of tokens, stop. */
3313 return;
3315 case CPP_SEMICOLON:
3316 /* Stop if this is an unnested ';'. */
3317 if (!nesting_depth)
3318 nesting_depth = -1;
3319 break;
3321 case CPP_CLOSE_BRACE:
3322 /* Stop if this is an unnested '}', or closes the outermost
3323 nesting level. */
3324 nesting_depth--;
3325 if (nesting_depth < 0)
3326 return;
3327 if (!nesting_depth)
3328 nesting_depth = -1;
3329 break;
3331 case CPP_OPEN_BRACE:
3332 /* Nest. */
3333 nesting_depth++;
3334 break;
3336 default:
3337 break;
3340 /* Consume the token. */
3341 cp_lexer_consume_token (parser->lexer);
3345 /* Skip tokens until a non-nested closing curly brace is the next
3346 token, or there are no more tokens. Return true in the first case,
3347 false otherwise. */
3349 static bool
3350 cp_parser_skip_to_closing_brace (cp_parser *parser)
3352 unsigned nesting_depth = 0;
3354 while (true)
3356 cp_token *token = cp_lexer_peek_token (parser->lexer);
3358 switch (token->type)
3360 case CPP_EOF:
3361 case CPP_PRAGMA_EOL:
3362 /* If we've run out of tokens, stop. */
3363 return false;
3365 case CPP_CLOSE_BRACE:
3366 /* If the next token is a non-nested `}', then we have reached
3367 the end of the current block. */
3368 if (nesting_depth-- == 0)
3369 return true;
3370 break;
3372 case CPP_OPEN_BRACE:
3373 /* If it the next token is a `{', then we are entering a new
3374 block. Consume the entire block. */
3375 ++nesting_depth;
3376 break;
3378 default:
3379 break;
3382 /* Consume the token. */
3383 cp_lexer_consume_token (parser->lexer);
3387 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3388 parameter is the PRAGMA token, allowing us to purge the entire pragma
3389 sequence. */
3391 static void
3392 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3394 cp_token *token;
3396 parser->lexer->in_pragma = false;
3399 token = cp_lexer_consume_token (parser->lexer);
3400 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3402 /* Ensure that the pragma is not parsed again. */
3403 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3406 /* Require pragma end of line, resyncing with it as necessary. The
3407 arguments are as for cp_parser_skip_to_pragma_eol. */
3409 static void
3410 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3412 parser->lexer->in_pragma = false;
3413 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3417 /* This is a simple wrapper around make_typename_type. When the id is
3418 an unresolved identifier node, we can provide a superior diagnostic
3419 using cp_parser_diagnose_invalid_type_name. */
3421 static tree
3422 cp_parser_make_typename_type (cp_parser *parser, tree id,
3423 location_t id_location)
3425 tree result;
3426 if (identifier_p (id))
3428 result = make_typename_type (parser->scope, id, typename_type,
3429 /*complain=*/tf_none);
3430 if (result == error_mark_node)
3431 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3432 return result;
3434 return make_typename_type (parser->scope, id, typename_type, tf_error);
3437 /* This is a wrapper around the
3438 make_{pointer,ptrmem,reference}_declarator functions that decides
3439 which one to call based on the CODE and CLASS_TYPE arguments. The
3440 CODE argument should be one of the values returned by
3441 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3442 appertain to the pointer or reference. */
3444 static cp_declarator *
3445 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3446 cp_cv_quals cv_qualifiers,
3447 cp_declarator *target,
3448 tree attributes)
3450 if (code == ERROR_MARK)
3451 return cp_error_declarator;
3453 if (code == INDIRECT_REF)
3454 if (class_type == NULL_TREE)
3455 return make_pointer_declarator (cv_qualifiers, target, attributes);
3456 else
3457 return make_ptrmem_declarator (cv_qualifiers, class_type,
3458 target, attributes);
3459 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3460 return make_reference_declarator (cv_qualifiers, target,
3461 false, attributes);
3462 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3463 return make_reference_declarator (cv_qualifiers, target,
3464 true, attributes);
3465 gcc_unreachable ();
3468 /* Create a new C++ parser. */
3470 static cp_parser *
3471 cp_parser_new (void)
3473 cp_parser *parser;
3474 cp_lexer *lexer;
3475 unsigned i;
3477 /* cp_lexer_new_main is called before doing GC allocation because
3478 cp_lexer_new_main might load a PCH file. */
3479 lexer = cp_lexer_new_main ();
3481 /* Initialize the binops_by_token so that we can get the tree
3482 directly from the token. */
3483 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3484 binops_by_token[binops[i].token_type] = binops[i];
3486 parser = ggc_cleared_alloc<cp_parser> ();
3487 parser->lexer = lexer;
3488 parser->context = cp_parser_context_new (NULL);
3490 /* For now, we always accept GNU extensions. */
3491 parser->allow_gnu_extensions_p = 1;
3493 /* The `>' token is a greater-than operator, not the end of a
3494 template-id. */
3495 parser->greater_than_is_operator_p = true;
3497 parser->default_arg_ok_p = true;
3499 /* We are not parsing a constant-expression. */
3500 parser->integral_constant_expression_p = false;
3501 parser->allow_non_integral_constant_expression_p = false;
3502 parser->non_integral_constant_expression_p = false;
3504 /* Local variable names are not forbidden. */
3505 parser->local_variables_forbidden_p = false;
3507 /* We are not processing an `extern "C"' declaration. */
3508 parser->in_unbraced_linkage_specification_p = false;
3510 /* We are not processing a declarator. */
3511 parser->in_declarator_p = false;
3513 /* We are not processing a template-argument-list. */
3514 parser->in_template_argument_list_p = false;
3516 /* We are not in an iteration statement. */
3517 parser->in_statement = 0;
3519 /* We are not in a switch statement. */
3520 parser->in_switch_statement_p = false;
3522 /* We are not parsing a type-id inside an expression. */
3523 parser->in_type_id_in_expr_p = false;
3525 /* Declarations aren't implicitly extern "C". */
3526 parser->implicit_extern_c = false;
3528 /* String literals should be translated to the execution character set. */
3529 parser->translate_strings_p = true;
3531 /* We are not parsing a function body. */
3532 parser->in_function_body = false;
3534 /* We can correct until told otherwise. */
3535 parser->colon_corrects_to_scope_p = true;
3537 /* The unparsed function queue is empty. */
3538 push_unparsed_function_queues (parser);
3540 /* There are no classes being defined. */
3541 parser->num_classes_being_defined = 0;
3543 /* No template parameters apply. */
3544 parser->num_template_parameter_lists = 0;
3546 /* Not declaring an implicit function template. */
3547 parser->auto_is_implicit_function_template_parm_p = false;
3548 parser->fully_implicit_function_template_p = false;
3549 parser->implicit_template_parms = 0;
3550 parser->implicit_template_scope = 0;
3552 return parser;
3555 /* Create a cp_lexer structure which will emit the tokens in CACHE
3556 and push it onto the parser's lexer stack. This is used for delayed
3557 parsing of in-class method bodies and default arguments, and should
3558 not be confused with tentative parsing. */
3559 static void
3560 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3562 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3563 lexer->next = parser->lexer;
3564 parser->lexer = lexer;
3566 /* Move the current source position to that of the first token in the
3567 new lexer. */
3568 cp_lexer_set_source_position_from_token (lexer->next_token);
3571 /* Pop the top lexer off the parser stack. This is never used for the
3572 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3573 static void
3574 cp_parser_pop_lexer (cp_parser *parser)
3576 cp_lexer *lexer = parser->lexer;
3577 parser->lexer = lexer->next;
3578 cp_lexer_destroy (lexer);
3580 /* Put the current source position back where it was before this
3581 lexer was pushed. */
3582 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3585 /* Lexical conventions [gram.lex] */
3587 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3588 identifier. */
3590 static tree
3591 cp_parser_identifier (cp_parser* parser)
3593 cp_token *token;
3595 /* Look for the identifier. */
3596 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3597 /* Return the value. */
3598 return token ? token->u.value : error_mark_node;
3601 /* Parse a sequence of adjacent string constants. Returns a
3602 TREE_STRING representing the combined, nul-terminated string
3603 constant. If TRANSLATE is true, translate the string to the
3604 execution character set. If WIDE_OK is true, a wide string is
3605 invalid here.
3607 C++98 [lex.string] says that if a narrow string literal token is
3608 adjacent to a wide string literal token, the behavior is undefined.
3609 However, C99 6.4.5p4 says that this results in a wide string literal.
3610 We follow C99 here, for consistency with the C front end.
3612 This code is largely lifted from lex_string() in c-lex.c.
3614 FUTURE: ObjC++ will need to handle @-strings here. */
3615 static tree
3616 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3617 bool lookup_udlit = true)
3619 tree value;
3620 size_t count;
3621 struct obstack str_ob;
3622 cpp_string str, istr, *strs;
3623 cp_token *tok;
3624 enum cpp_ttype type, curr_type;
3625 int have_suffix_p = 0;
3626 tree string_tree;
3627 tree suffix_id = NULL_TREE;
3628 bool curr_tok_is_userdef_p = false;
3630 tok = cp_lexer_peek_token (parser->lexer);
3631 if (!cp_parser_is_string_literal (tok))
3633 cp_parser_error (parser, "expected string-literal");
3634 return error_mark_node;
3637 if (cpp_userdef_string_p (tok->type))
3639 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3640 curr_type = cpp_userdef_string_remove_type (tok->type);
3641 curr_tok_is_userdef_p = true;
3643 else
3645 string_tree = tok->u.value;
3646 curr_type = tok->type;
3648 type = curr_type;
3650 /* Try to avoid the overhead of creating and destroying an obstack
3651 for the common case of just one string. */
3652 if (!cp_parser_is_string_literal
3653 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3655 cp_lexer_consume_token (parser->lexer);
3657 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3658 str.len = TREE_STRING_LENGTH (string_tree);
3659 count = 1;
3661 if (curr_tok_is_userdef_p)
3663 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3664 have_suffix_p = 1;
3665 curr_type = cpp_userdef_string_remove_type (tok->type);
3667 else
3668 curr_type = tok->type;
3670 strs = &str;
3672 else
3674 gcc_obstack_init (&str_ob);
3675 count = 0;
3679 cp_lexer_consume_token (parser->lexer);
3680 count++;
3681 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3682 str.len = TREE_STRING_LENGTH (string_tree);
3684 if (curr_tok_is_userdef_p)
3686 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3687 if (have_suffix_p == 0)
3689 suffix_id = curr_suffix_id;
3690 have_suffix_p = 1;
3692 else if (have_suffix_p == 1
3693 && curr_suffix_id != suffix_id)
3695 error ("inconsistent user-defined literal suffixes"
3696 " %qD and %qD in string literal",
3697 suffix_id, curr_suffix_id);
3698 have_suffix_p = -1;
3700 curr_type = cpp_userdef_string_remove_type (tok->type);
3702 else
3703 curr_type = tok->type;
3705 if (type != curr_type)
3707 if (type == CPP_STRING)
3708 type = curr_type;
3709 else if (curr_type != CPP_STRING)
3710 error_at (tok->location,
3711 "unsupported non-standard concatenation "
3712 "of string literals");
3715 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3717 tok = cp_lexer_peek_token (parser->lexer);
3718 if (cpp_userdef_string_p (tok->type))
3720 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3721 curr_type = cpp_userdef_string_remove_type (tok->type);
3722 curr_tok_is_userdef_p = true;
3724 else
3726 string_tree = tok->u.value;
3727 curr_type = tok->type;
3728 curr_tok_is_userdef_p = false;
3731 while (cp_parser_is_string_literal (tok));
3733 strs = (cpp_string *) obstack_finish (&str_ob);
3736 if (type != CPP_STRING && !wide_ok)
3738 cp_parser_error (parser, "a wide string is invalid in this context");
3739 type = CPP_STRING;
3742 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3743 (parse_in, strs, count, &istr, type))
3745 value = build_string (istr.len, (const char *)istr.text);
3746 free (CONST_CAST (unsigned char *, istr.text));
3748 switch (type)
3750 default:
3751 case CPP_STRING:
3752 case CPP_UTF8STRING:
3753 TREE_TYPE (value) = char_array_type_node;
3754 break;
3755 case CPP_STRING16:
3756 TREE_TYPE (value) = char16_array_type_node;
3757 break;
3758 case CPP_STRING32:
3759 TREE_TYPE (value) = char32_array_type_node;
3760 break;
3761 case CPP_WSTRING:
3762 TREE_TYPE (value) = wchar_array_type_node;
3763 break;
3766 value = fix_string_type (value);
3768 if (have_suffix_p)
3770 tree literal = build_userdef_literal (suffix_id, value,
3771 OT_NONE, NULL_TREE);
3772 if (lookup_udlit)
3773 value = cp_parser_userdef_string_literal (literal);
3774 else
3775 value = literal;
3778 else
3779 /* cpp_interpret_string has issued an error. */
3780 value = error_mark_node;
3782 if (count > 1)
3783 obstack_free (&str_ob, 0);
3785 return value;
3788 /* Look up a literal operator with the name and the exact arguments. */
3790 static tree
3791 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3793 tree decl, fns;
3794 decl = lookup_name (name);
3795 if (!decl || !is_overloaded_fn (decl))
3796 return error_mark_node;
3798 for (fns = decl; fns; fns = OVL_NEXT (fns))
3800 unsigned int ix;
3801 bool found = true;
3802 tree fn = OVL_CURRENT (fns);
3803 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3804 if (parmtypes != NULL_TREE)
3806 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3807 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3809 tree tparm = TREE_VALUE (parmtypes);
3810 tree targ = TREE_TYPE ((*args)[ix]);
3811 bool ptr = TYPE_PTR_P (tparm);
3812 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3813 if ((ptr || arr || !same_type_p (tparm, targ))
3814 && (!ptr || !arr
3815 || !same_type_p (TREE_TYPE (tparm),
3816 TREE_TYPE (targ))))
3817 found = false;
3819 if (found
3820 && ix == vec_safe_length (args)
3821 /* May be this should be sufficient_parms_p instead,
3822 depending on how exactly should user-defined literals
3823 work in presence of default arguments on the literal
3824 operator parameters. */
3825 && parmtypes == void_list_node)
3826 return decl;
3830 return error_mark_node;
3833 /* Parse a user-defined char constant. Returns a call to a user-defined
3834 literal operator taking the character as an argument. */
3836 static tree
3837 cp_parser_userdef_char_literal (cp_parser *parser)
3839 cp_token *token = cp_lexer_consume_token (parser->lexer);
3840 tree literal = token->u.value;
3841 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3842 tree value = USERDEF_LITERAL_VALUE (literal);
3843 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3844 tree decl, result;
3846 /* Build up a call to the user-defined operator */
3847 /* Lookup the name we got back from the id-expression. */
3848 vec<tree, va_gc> *args = make_tree_vector ();
3849 vec_safe_push (args, value);
3850 decl = lookup_literal_operator (name, args);
3851 if (!decl || decl == error_mark_node)
3853 error ("unable to find character literal operator %qD with %qT argument",
3854 name, TREE_TYPE (value));
3855 release_tree_vector (args);
3856 return error_mark_node;
3858 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3859 release_tree_vector (args);
3860 return result;
3863 /* A subroutine of cp_parser_userdef_numeric_literal to
3864 create a char... template parameter pack from a string node. */
3866 static tree
3867 make_char_string_pack (tree value)
3869 tree charvec;
3870 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3871 const char *str = TREE_STRING_POINTER (value);
3872 int i, len = TREE_STRING_LENGTH (value) - 1;
3873 tree argvec = make_tree_vec (1);
3875 /* Fill in CHARVEC with all of the parameters. */
3876 charvec = make_tree_vec (len);
3877 for (i = 0; i < len; ++i)
3878 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3880 /* Build the argument packs. */
3881 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3882 TREE_TYPE (argpack) = char_type_node;
3884 TREE_VEC_ELT (argvec, 0) = argpack;
3886 return argvec;
3889 /* A subroutine of cp_parser_userdef_numeric_literal to
3890 create a char... template parameter pack from a string node. */
3892 static tree
3893 make_string_pack (tree value)
3895 tree charvec;
3896 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3897 const unsigned char *str
3898 = (const unsigned char *) TREE_STRING_POINTER (value);
3899 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3900 int len = TREE_STRING_LENGTH (value) / sz - 1;
3901 tree argvec = make_tree_vec (2);
3903 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3904 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3906 /* First template parm is character type. */
3907 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3909 /* Fill in CHARVEC with all of the parameters. */
3910 charvec = make_tree_vec (len);
3911 for (int i = 0; i < len; ++i)
3912 TREE_VEC_ELT (charvec, i)
3913 = double_int_to_tree (str_char_type_node,
3914 double_int::from_buffer (str + i * sz, sz));
3916 /* Build the argument packs. */
3917 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3918 TREE_TYPE (argpack) = str_char_type_node;
3920 TREE_VEC_ELT (argvec, 1) = argpack;
3922 return argvec;
3925 /* Parse a user-defined numeric constant. returns a call to a user-defined
3926 literal operator. */
3928 static tree
3929 cp_parser_userdef_numeric_literal (cp_parser *parser)
3931 cp_token *token = cp_lexer_consume_token (parser->lexer);
3932 tree literal = token->u.value;
3933 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3934 tree value = USERDEF_LITERAL_VALUE (literal);
3935 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3936 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3937 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3938 tree decl, result;
3939 vec<tree, va_gc> *args;
3941 /* Look for a literal operator taking the exact type of numeric argument
3942 as the literal value. */
3943 args = make_tree_vector ();
3944 vec_safe_push (args, value);
3945 decl = lookup_literal_operator (name, args);
3946 if (decl && decl != error_mark_node)
3948 result = finish_call_expr (decl, &args, false, true,
3949 tf_warning_or_error);
3951 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3953 warning_at (token->location, OPT_Woverflow,
3954 "integer literal exceeds range of %qT type",
3955 long_long_unsigned_type_node);
3957 else
3959 if (overflow > 0)
3960 warning_at (token->location, OPT_Woverflow,
3961 "floating literal exceeds range of %qT type",
3962 long_double_type_node);
3963 else if (overflow < 0)
3964 warning_at (token->location, OPT_Woverflow,
3965 "floating literal truncated to zero");
3968 release_tree_vector (args);
3969 return result;
3971 release_tree_vector (args);
3973 /* If the numeric argument didn't work, look for a raw literal
3974 operator taking a const char* argument consisting of the number
3975 in string format. */
3976 args = make_tree_vector ();
3977 vec_safe_push (args, num_string);
3978 decl = lookup_literal_operator (name, args);
3979 if (decl && decl != error_mark_node)
3981 result = finish_call_expr (decl, &args, false, true,
3982 tf_warning_or_error);
3983 release_tree_vector (args);
3984 return result;
3986 release_tree_vector (args);
3988 /* If the raw literal didn't work, look for a non-type template
3989 function with parameter pack char.... Call the function with
3990 template parameter characters representing the number. */
3991 args = make_tree_vector ();
3992 decl = lookup_literal_operator (name, args);
3993 if (decl && decl != error_mark_node)
3995 tree tmpl_args = make_char_string_pack (num_string);
3996 decl = lookup_template_function (decl, tmpl_args);
3997 result = finish_call_expr (decl, &args, false, true,
3998 tf_warning_or_error);
3999 release_tree_vector (args);
4000 return result;
4003 release_tree_vector (args);
4005 error ("unable to find numeric literal operator %qD", name);
4006 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4007 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4008 "to enable more built-in suffixes");
4009 return error_mark_node;
4012 /* Parse a user-defined string constant. Returns a call to a user-defined
4013 literal operator taking a character pointer and the length of the string
4014 as arguments. */
4016 static tree
4017 cp_parser_userdef_string_literal (tree literal)
4019 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4020 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4021 tree value = USERDEF_LITERAL_VALUE (literal);
4022 int len = TREE_STRING_LENGTH (value)
4023 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4024 tree decl, result;
4025 vec<tree, va_gc> *args;
4027 /* Build up a call to the user-defined operator. */
4028 /* Lookup the name we got back from the id-expression. */
4029 args = make_tree_vector ();
4030 vec_safe_push (args, value);
4031 vec_safe_push (args, build_int_cst (size_type_node, len));
4032 decl = lookup_literal_operator (name, args);
4034 if (decl && decl != error_mark_node)
4036 result = finish_call_expr (decl, &args, false, true,
4037 tf_warning_or_error);
4038 release_tree_vector (args);
4039 return result;
4041 release_tree_vector (args);
4043 /* Look for a template function with typename parameter CharT
4044 and parameter pack CharT... Call the function with
4045 template parameter characters representing the string. */
4046 args = make_tree_vector ();
4047 decl = lookup_literal_operator (name, args);
4048 if (decl && decl != error_mark_node)
4050 tree tmpl_args = make_string_pack (value);
4051 decl = lookup_template_function (decl, tmpl_args);
4052 result = finish_call_expr (decl, &args, false, true,
4053 tf_warning_or_error);
4054 release_tree_vector (args);
4055 return result;
4057 release_tree_vector (args);
4059 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4060 name, TREE_TYPE (value), size_type_node);
4061 return error_mark_node;
4065 /* Basic concepts [gram.basic] */
4067 /* Parse a translation-unit.
4069 translation-unit:
4070 declaration-seq [opt]
4072 Returns TRUE if all went well. */
4074 static bool
4075 cp_parser_translation_unit (cp_parser* parser)
4077 /* The address of the first non-permanent object on the declarator
4078 obstack. */
4079 static void *declarator_obstack_base;
4081 bool success;
4083 /* Create the declarator obstack, if necessary. */
4084 if (!cp_error_declarator)
4086 gcc_obstack_init (&declarator_obstack);
4087 /* Create the error declarator. */
4088 cp_error_declarator = make_declarator (cdk_error);
4089 /* Create the empty parameter list. */
4090 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4091 /* Remember where the base of the declarator obstack lies. */
4092 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4095 cp_parser_declaration_seq_opt (parser);
4097 /* If there are no tokens left then all went well. */
4098 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4100 /* Get rid of the token array; we don't need it any more. */
4101 cp_lexer_destroy (parser->lexer);
4102 parser->lexer = NULL;
4104 /* This file might have been a context that's implicitly extern
4105 "C". If so, pop the lang context. (Only relevant for PCH.) */
4106 if (parser->implicit_extern_c)
4108 pop_lang_context ();
4109 parser->implicit_extern_c = false;
4112 /* Finish up. */
4113 finish_translation_unit ();
4115 success = true;
4117 else
4119 cp_parser_error (parser, "expected declaration");
4120 success = false;
4123 /* Make sure the declarator obstack was fully cleaned up. */
4124 gcc_assert (obstack_next_free (&declarator_obstack)
4125 == declarator_obstack_base);
4127 /* All went well. */
4128 return success;
4131 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4132 decltype context. */
4134 static inline tsubst_flags_t
4135 complain_flags (bool decltype_p)
4137 tsubst_flags_t complain = tf_warning_or_error;
4138 if (decltype_p)
4139 complain |= tf_decltype;
4140 return complain;
4143 /* We're about to parse a collection of statements. If we're currently
4144 parsing tentatively, set up a firewall so that any nested
4145 cp_parser_commit_to_tentative_parse won't affect the current context. */
4147 static cp_token_position
4148 cp_parser_start_tentative_firewall (cp_parser *parser)
4150 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4151 return 0;
4153 cp_parser_parse_tentatively (parser);
4154 cp_parser_commit_to_topmost_tentative_parse (parser);
4155 return cp_lexer_token_position (parser->lexer, false);
4158 /* We've finished parsing the collection of statements. Wrap up the
4159 firewall and replace the relevant tokens with the parsed form. */
4161 static void
4162 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4163 tree expr)
4165 if (!start)
4166 return;
4168 /* Finish the firewall level. */
4169 cp_parser_parse_definitely (parser);
4170 /* And remember the result of the parse for when we try again. */
4171 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4172 token->type = CPP_PREPARSED_EXPR;
4173 token->u.value = expr;
4174 token->keyword = RID_MAX;
4175 cp_lexer_purge_tokens_after (parser->lexer, start);
4178 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4179 enclosing parentheses. */
4181 static tree
4182 cp_parser_statement_expr (cp_parser *parser)
4184 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4186 /* Consume the '('. */
4187 cp_lexer_consume_token (parser->lexer);
4188 /* Start the statement-expression. */
4189 tree expr = begin_stmt_expr ();
4190 /* Parse the compound-statement. */
4191 cp_parser_compound_statement (parser, expr, false, false);
4192 /* Finish up. */
4193 expr = finish_stmt_expr (expr, false);
4194 /* Consume the ')'. */
4195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4196 cp_parser_skip_to_end_of_statement (parser);
4198 cp_parser_end_tentative_firewall (parser, start, expr);
4199 return expr;
4202 /* Expressions [gram.expr] */
4204 /* Parse a primary-expression.
4206 primary-expression:
4207 literal
4208 this
4209 ( expression )
4210 id-expression
4211 lambda-expression (C++11)
4213 GNU Extensions:
4215 primary-expression:
4216 ( compound-statement )
4217 __builtin_va_arg ( assignment-expression , type-id )
4218 __builtin_offsetof ( type-id , offsetof-expression )
4220 C++ Extensions:
4221 __has_nothrow_assign ( type-id )
4222 __has_nothrow_constructor ( type-id )
4223 __has_nothrow_copy ( type-id )
4224 __has_trivial_assign ( type-id )
4225 __has_trivial_constructor ( type-id )
4226 __has_trivial_copy ( type-id )
4227 __has_trivial_destructor ( type-id )
4228 __has_virtual_destructor ( type-id )
4229 __is_abstract ( type-id )
4230 __is_base_of ( type-id , type-id )
4231 __is_class ( type-id )
4232 __is_empty ( type-id )
4233 __is_enum ( type-id )
4234 __is_final ( type-id )
4235 __is_literal_type ( type-id )
4236 __is_pod ( type-id )
4237 __is_polymorphic ( type-id )
4238 __is_std_layout ( type-id )
4239 __is_trivial ( type-id )
4240 __is_union ( type-id )
4242 Objective-C++ Extension:
4244 primary-expression:
4245 objc-expression
4247 literal:
4248 __null
4250 ADDRESS_P is true iff this expression was immediately preceded by
4251 "&" and therefore might denote a pointer-to-member. CAST_P is true
4252 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4253 true iff this expression is a template argument.
4255 Returns a representation of the expression. Upon return, *IDK
4256 indicates what kind of id-expression (if any) was present. */
4258 static tree
4259 cp_parser_primary_expression (cp_parser *parser,
4260 bool address_p,
4261 bool cast_p,
4262 bool template_arg_p,
4263 bool decltype_p,
4264 cp_id_kind *idk)
4266 cp_token *token = NULL;
4268 /* Assume the primary expression is not an id-expression. */
4269 *idk = CP_ID_KIND_NONE;
4271 /* Peek at the next token. */
4272 token = cp_lexer_peek_token (parser->lexer);
4273 switch ((int) token->type)
4275 /* literal:
4276 integer-literal
4277 character-literal
4278 floating-literal
4279 string-literal
4280 boolean-literal
4281 pointer-literal
4282 user-defined-literal */
4283 case CPP_CHAR:
4284 case CPP_CHAR16:
4285 case CPP_CHAR32:
4286 case CPP_WCHAR:
4287 case CPP_NUMBER:
4288 case CPP_PREPARSED_EXPR:
4289 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4290 return cp_parser_userdef_numeric_literal (parser);
4291 token = cp_lexer_consume_token (parser->lexer);
4292 if (TREE_CODE (token->u.value) == FIXED_CST)
4294 error_at (token->location,
4295 "fixed-point types not supported in C++");
4296 return error_mark_node;
4298 /* Floating-point literals are only allowed in an integral
4299 constant expression if they are cast to an integral or
4300 enumeration type. */
4301 if (TREE_CODE (token->u.value) == REAL_CST
4302 && parser->integral_constant_expression_p
4303 && pedantic)
4305 /* CAST_P will be set even in invalid code like "int(2.7 +
4306 ...)". Therefore, we have to check that the next token
4307 is sure to end the cast. */
4308 if (cast_p)
4310 cp_token *next_token;
4312 next_token = cp_lexer_peek_token (parser->lexer);
4313 if (/* The comma at the end of an
4314 enumerator-definition. */
4315 next_token->type != CPP_COMMA
4316 /* The curly brace at the end of an enum-specifier. */
4317 && next_token->type != CPP_CLOSE_BRACE
4318 /* The end of a statement. */
4319 && next_token->type != CPP_SEMICOLON
4320 /* The end of the cast-expression. */
4321 && next_token->type != CPP_CLOSE_PAREN
4322 /* The end of an array bound. */
4323 && next_token->type != CPP_CLOSE_SQUARE
4324 /* The closing ">" in a template-argument-list. */
4325 && (next_token->type != CPP_GREATER
4326 || parser->greater_than_is_operator_p)
4327 /* C++0x only: A ">>" treated like two ">" tokens,
4328 in a template-argument-list. */
4329 && (next_token->type != CPP_RSHIFT
4330 || (cxx_dialect == cxx98)
4331 || parser->greater_than_is_operator_p))
4332 cast_p = false;
4335 /* If we are within a cast, then the constraint that the
4336 cast is to an integral or enumeration type will be
4337 checked at that point. If we are not within a cast, then
4338 this code is invalid. */
4339 if (!cast_p)
4340 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4342 return token->u.value;
4344 case CPP_CHAR_USERDEF:
4345 case CPP_CHAR16_USERDEF:
4346 case CPP_CHAR32_USERDEF:
4347 case CPP_WCHAR_USERDEF:
4348 return cp_parser_userdef_char_literal (parser);
4350 case CPP_STRING:
4351 case CPP_STRING16:
4352 case CPP_STRING32:
4353 case CPP_WSTRING:
4354 case CPP_UTF8STRING:
4355 case CPP_STRING_USERDEF:
4356 case CPP_STRING16_USERDEF:
4357 case CPP_STRING32_USERDEF:
4358 case CPP_WSTRING_USERDEF:
4359 case CPP_UTF8STRING_USERDEF:
4360 /* ??? Should wide strings be allowed when parser->translate_strings_p
4361 is false (i.e. in attributes)? If not, we can kill the third
4362 argument to cp_parser_string_literal. */
4363 return cp_parser_string_literal (parser,
4364 parser->translate_strings_p,
4365 true);
4367 case CPP_OPEN_PAREN:
4368 /* If we see `( { ' then we are looking at the beginning of
4369 a GNU statement-expression. */
4370 if (cp_parser_allow_gnu_extensions_p (parser)
4371 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4373 /* Statement-expressions are not allowed by the standard. */
4374 pedwarn (token->location, OPT_Wpedantic,
4375 "ISO C++ forbids braced-groups within expressions");
4377 /* And they're not allowed outside of a function-body; you
4378 cannot, for example, write:
4380 int i = ({ int j = 3; j + 1; });
4382 at class or namespace scope. */
4383 if (!parser->in_function_body
4384 || parser->in_template_argument_list_p)
4386 error_at (token->location,
4387 "statement-expressions are not allowed outside "
4388 "functions nor in template-argument lists");
4389 cp_parser_skip_to_end_of_block_or_statement (parser);
4390 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4391 cp_lexer_consume_token (parser->lexer);
4392 return error_mark_node;
4394 else
4395 return cp_parser_statement_expr (parser);
4397 /* Otherwise it's a normal parenthesized expression. */
4399 tree expr;
4400 bool saved_greater_than_is_operator_p;
4402 /* Consume the `('. */
4403 cp_lexer_consume_token (parser->lexer);
4404 /* Within a parenthesized expression, a `>' token is always
4405 the greater-than operator. */
4406 saved_greater_than_is_operator_p
4407 = parser->greater_than_is_operator_p;
4408 parser->greater_than_is_operator_p = true;
4410 /* Parse the parenthesized expression. */
4411 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4412 /* Let the front end know that this expression was
4413 enclosed in parentheses. This matters in case, for
4414 example, the expression is of the form `A::B', since
4415 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4416 not. */
4417 expr = finish_parenthesized_expr (expr);
4418 /* DR 705: Wrapping an unqualified name in parentheses
4419 suppresses arg-dependent lookup. We want to pass back
4420 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4421 (c++/37862), but none of the others. */
4422 if (*idk != CP_ID_KIND_QUALIFIED)
4423 *idk = CP_ID_KIND_NONE;
4425 /* The `>' token might be the end of a template-id or
4426 template-parameter-list now. */
4427 parser->greater_than_is_operator_p
4428 = saved_greater_than_is_operator_p;
4429 /* Consume the `)'. */
4430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4431 cp_parser_skip_to_end_of_statement (parser);
4433 return expr;
4436 case CPP_OPEN_SQUARE:
4438 if (c_dialect_objc ())
4440 /* We might have an Objective-C++ message. */
4441 cp_parser_parse_tentatively (parser);
4442 tree msg = cp_parser_objc_message_expression (parser);
4443 /* If that works out, we're done ... */
4444 if (cp_parser_parse_definitely (parser))
4445 return msg;
4446 /* ... else, fall though to see if it's a lambda. */
4448 tree lam = cp_parser_lambda_expression (parser);
4449 /* Don't warn about a failed tentative parse. */
4450 if (cp_parser_error_occurred (parser))
4451 return error_mark_node;
4452 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4453 return lam;
4456 case CPP_OBJC_STRING:
4457 if (c_dialect_objc ())
4458 /* We have an Objective-C++ string literal. */
4459 return cp_parser_objc_expression (parser);
4460 cp_parser_error (parser, "expected primary-expression");
4461 return error_mark_node;
4463 case CPP_KEYWORD:
4464 switch (token->keyword)
4466 /* These two are the boolean literals. */
4467 case RID_TRUE:
4468 cp_lexer_consume_token (parser->lexer);
4469 return boolean_true_node;
4470 case RID_FALSE:
4471 cp_lexer_consume_token (parser->lexer);
4472 return boolean_false_node;
4474 /* The `__null' literal. */
4475 case RID_NULL:
4476 cp_lexer_consume_token (parser->lexer);
4477 return null_node;
4479 /* The `nullptr' literal. */
4480 case RID_NULLPTR:
4481 cp_lexer_consume_token (parser->lexer);
4482 return nullptr_node;
4484 /* Recognize the `this' keyword. */
4485 case RID_THIS:
4486 cp_lexer_consume_token (parser->lexer);
4487 if (parser->local_variables_forbidden_p)
4489 error_at (token->location,
4490 "%<this%> may not be used in this context");
4491 return error_mark_node;
4493 /* Pointers cannot appear in constant-expressions. */
4494 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4495 return error_mark_node;
4496 return finish_this_expr ();
4498 /* The `operator' keyword can be the beginning of an
4499 id-expression. */
4500 case RID_OPERATOR:
4501 goto id_expression;
4503 case RID_FUNCTION_NAME:
4504 case RID_PRETTY_FUNCTION_NAME:
4505 case RID_C99_FUNCTION_NAME:
4507 non_integral_constant name;
4509 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4510 __func__ are the names of variables -- but they are
4511 treated specially. Therefore, they are handled here,
4512 rather than relying on the generic id-expression logic
4513 below. Grammatically, these names are id-expressions.
4515 Consume the token. */
4516 token = cp_lexer_consume_token (parser->lexer);
4518 switch (token->keyword)
4520 case RID_FUNCTION_NAME:
4521 name = NIC_FUNC_NAME;
4522 break;
4523 case RID_PRETTY_FUNCTION_NAME:
4524 name = NIC_PRETTY_FUNC;
4525 break;
4526 case RID_C99_FUNCTION_NAME:
4527 name = NIC_C99_FUNC;
4528 break;
4529 default:
4530 gcc_unreachable ();
4533 if (cp_parser_non_integral_constant_expression (parser, name))
4534 return error_mark_node;
4536 /* Look up the name. */
4537 return finish_fname (token->u.value);
4540 case RID_VA_ARG:
4542 tree expression;
4543 tree type;
4544 source_location type_location;
4546 /* The `__builtin_va_arg' construct is used to handle
4547 `va_arg'. Consume the `__builtin_va_arg' token. */
4548 cp_lexer_consume_token (parser->lexer);
4549 /* Look for the opening `('. */
4550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4551 /* Now, parse the assignment-expression. */
4552 expression = cp_parser_assignment_expression (parser);
4553 /* Look for the `,'. */
4554 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4555 type_location = cp_lexer_peek_token (parser->lexer)->location;
4556 /* Parse the type-id. */
4557 type = cp_parser_type_id (parser);
4558 /* Look for the closing `)'. */
4559 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4560 /* Using `va_arg' in a constant-expression is not
4561 allowed. */
4562 if (cp_parser_non_integral_constant_expression (parser,
4563 NIC_VA_ARG))
4564 return error_mark_node;
4565 return build_x_va_arg (type_location, expression, type);
4568 case RID_OFFSETOF:
4569 return cp_parser_builtin_offsetof (parser);
4571 case RID_HAS_NOTHROW_ASSIGN:
4572 case RID_HAS_NOTHROW_CONSTRUCTOR:
4573 case RID_HAS_NOTHROW_COPY:
4574 case RID_HAS_TRIVIAL_ASSIGN:
4575 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4576 case RID_HAS_TRIVIAL_COPY:
4577 case RID_HAS_TRIVIAL_DESTRUCTOR:
4578 case RID_HAS_VIRTUAL_DESTRUCTOR:
4579 case RID_IS_ABSTRACT:
4580 case RID_IS_BASE_OF:
4581 case RID_IS_CLASS:
4582 case RID_IS_EMPTY:
4583 case RID_IS_ENUM:
4584 case RID_IS_FINAL:
4585 case RID_IS_LITERAL_TYPE:
4586 case RID_IS_POD:
4587 case RID_IS_POLYMORPHIC:
4588 case RID_IS_STD_LAYOUT:
4589 case RID_IS_TRIVIAL:
4590 case RID_IS_TRIVIALLY_ASSIGNABLE:
4591 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4592 case RID_IS_TRIVIALLY_COPYABLE:
4593 case RID_IS_UNION:
4594 return cp_parser_trait_expr (parser, token->keyword);
4596 /* Objective-C++ expressions. */
4597 case RID_AT_ENCODE:
4598 case RID_AT_PROTOCOL:
4599 case RID_AT_SELECTOR:
4600 return cp_parser_objc_expression (parser);
4602 case RID_TEMPLATE:
4603 if (parser->in_function_body
4604 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4605 == CPP_LESS))
4607 error_at (token->location,
4608 "a template declaration cannot appear at block scope");
4609 cp_parser_skip_to_end_of_block_or_statement (parser);
4610 return error_mark_node;
4612 default:
4613 cp_parser_error (parser, "expected primary-expression");
4614 return error_mark_node;
4617 /* An id-expression can start with either an identifier, a
4618 `::' as the beginning of a qualified-id, or the "operator"
4619 keyword. */
4620 case CPP_NAME:
4621 case CPP_SCOPE:
4622 case CPP_TEMPLATE_ID:
4623 case CPP_NESTED_NAME_SPECIFIER:
4625 tree id_expression;
4626 tree decl;
4627 const char *error_msg;
4628 bool template_p;
4629 bool done;
4630 cp_token *id_expr_token;
4632 id_expression:
4633 /* Parse the id-expression. */
4634 id_expression
4635 = cp_parser_id_expression (parser,
4636 /*template_keyword_p=*/false,
4637 /*check_dependency_p=*/true,
4638 &template_p,
4639 /*declarator_p=*/false,
4640 /*optional_p=*/false);
4641 if (id_expression == error_mark_node)
4642 return error_mark_node;
4643 id_expr_token = token;
4644 token = cp_lexer_peek_token (parser->lexer);
4645 done = (token->type != CPP_OPEN_SQUARE
4646 && token->type != CPP_OPEN_PAREN
4647 && token->type != CPP_DOT
4648 && token->type != CPP_DEREF
4649 && token->type != CPP_PLUS_PLUS
4650 && token->type != CPP_MINUS_MINUS);
4651 /* If we have a template-id, then no further lookup is
4652 required. If the template-id was for a template-class, we
4653 will sometimes have a TYPE_DECL at this point. */
4654 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4655 || TREE_CODE (id_expression) == TYPE_DECL)
4656 decl = id_expression;
4657 /* Look up the name. */
4658 else
4660 tree ambiguous_decls;
4662 /* If we already know that this lookup is ambiguous, then
4663 we've already issued an error message; there's no reason
4664 to check again. */
4665 if (id_expr_token->type == CPP_NAME
4666 && id_expr_token->error_reported)
4668 cp_parser_simulate_error (parser);
4669 return error_mark_node;
4672 decl = cp_parser_lookup_name (parser, id_expression,
4673 none_type,
4674 template_p,
4675 /*is_namespace=*/false,
4676 /*check_dependency=*/true,
4677 &ambiguous_decls,
4678 id_expr_token->location);
4679 /* If the lookup was ambiguous, an error will already have
4680 been issued. */
4681 if (ambiguous_decls)
4682 return error_mark_node;
4684 /* In Objective-C++, we may have an Objective-C 2.0
4685 dot-syntax for classes here. */
4686 if (c_dialect_objc ()
4687 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4688 && TREE_CODE (decl) == TYPE_DECL
4689 && objc_is_class_name (decl))
4691 tree component;
4692 cp_lexer_consume_token (parser->lexer);
4693 component = cp_parser_identifier (parser);
4694 if (component == error_mark_node)
4695 return error_mark_node;
4697 return objc_build_class_component_ref (id_expression, component);
4700 /* In Objective-C++, an instance variable (ivar) may be preferred
4701 to whatever cp_parser_lookup_name() found. */
4702 decl = objc_lookup_ivar (decl, id_expression);
4704 /* If name lookup gives us a SCOPE_REF, then the
4705 qualifying scope was dependent. */
4706 if (TREE_CODE (decl) == SCOPE_REF)
4708 /* At this point, we do not know if DECL is a valid
4709 integral constant expression. We assume that it is
4710 in fact such an expression, so that code like:
4712 template <int N> struct A {
4713 int a[B<N>::i];
4716 is accepted. At template-instantiation time, we
4717 will check that B<N>::i is actually a constant. */
4718 return decl;
4720 /* Check to see if DECL is a local variable in a context
4721 where that is forbidden. */
4722 if (parser->local_variables_forbidden_p
4723 && local_variable_p (decl))
4725 /* It might be that we only found DECL because we are
4726 trying to be generous with pre-ISO scoping rules.
4727 For example, consider:
4729 int i;
4730 void g() {
4731 for (int i = 0; i < 10; ++i) {}
4732 extern void f(int j = i);
4735 Here, name look up will originally find the out
4736 of scope `i'. We need to issue a warning message,
4737 but then use the global `i'. */
4738 decl = check_for_out_of_scope_variable (decl);
4739 if (local_variable_p (decl))
4741 error_at (id_expr_token->location,
4742 "local variable %qD may not appear in this context",
4743 decl);
4744 return error_mark_node;
4749 decl = (finish_id_expression
4750 (id_expression, decl, parser->scope,
4751 idk,
4752 parser->integral_constant_expression_p,
4753 parser->allow_non_integral_constant_expression_p,
4754 &parser->non_integral_constant_expression_p,
4755 template_p, done, address_p,
4756 template_arg_p,
4757 &error_msg,
4758 id_expr_token->location));
4759 if (error_msg)
4760 cp_parser_error (parser, error_msg);
4761 return decl;
4764 /* Anything else is an error. */
4765 default:
4766 cp_parser_error (parser, "expected primary-expression");
4767 return error_mark_node;
4771 static inline tree
4772 cp_parser_primary_expression (cp_parser *parser,
4773 bool address_p,
4774 bool cast_p,
4775 bool template_arg_p,
4776 cp_id_kind *idk)
4778 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4779 /*decltype*/false, idk);
4782 /* Parse an id-expression.
4784 id-expression:
4785 unqualified-id
4786 qualified-id
4788 qualified-id:
4789 :: [opt] nested-name-specifier template [opt] unqualified-id
4790 :: identifier
4791 :: operator-function-id
4792 :: template-id
4794 Return a representation of the unqualified portion of the
4795 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4796 a `::' or nested-name-specifier.
4798 Often, if the id-expression was a qualified-id, the caller will
4799 want to make a SCOPE_REF to represent the qualified-id. This
4800 function does not do this in order to avoid wastefully creating
4801 SCOPE_REFs when they are not required.
4803 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4804 `template' keyword.
4806 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4807 uninstantiated templates.
4809 If *TEMPLATE_P is non-NULL, it is set to true iff the
4810 `template' keyword is used to explicitly indicate that the entity
4811 named is a template.
4813 If DECLARATOR_P is true, the id-expression is appearing as part of
4814 a declarator, rather than as part of an expression. */
4816 static tree
4817 cp_parser_id_expression (cp_parser *parser,
4818 bool template_keyword_p,
4819 bool check_dependency_p,
4820 bool *template_p,
4821 bool declarator_p,
4822 bool optional_p)
4824 bool global_scope_p;
4825 bool nested_name_specifier_p;
4827 /* Assume the `template' keyword was not used. */
4828 if (template_p)
4829 *template_p = template_keyword_p;
4831 /* Look for the optional `::' operator. */
4832 global_scope_p
4833 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4834 != NULL_TREE);
4835 /* Look for the optional nested-name-specifier. */
4836 nested_name_specifier_p
4837 = (cp_parser_nested_name_specifier_opt (parser,
4838 /*typename_keyword_p=*/false,
4839 check_dependency_p,
4840 /*type_p=*/false,
4841 declarator_p)
4842 != NULL_TREE);
4843 /* If there is a nested-name-specifier, then we are looking at
4844 the first qualified-id production. */
4845 if (nested_name_specifier_p)
4847 tree saved_scope;
4848 tree saved_object_scope;
4849 tree saved_qualifying_scope;
4850 tree unqualified_id;
4851 bool is_template;
4853 /* See if the next token is the `template' keyword. */
4854 if (!template_p)
4855 template_p = &is_template;
4856 *template_p = cp_parser_optional_template_keyword (parser);
4857 /* Name lookup we do during the processing of the
4858 unqualified-id might obliterate SCOPE. */
4859 saved_scope = parser->scope;
4860 saved_object_scope = parser->object_scope;
4861 saved_qualifying_scope = parser->qualifying_scope;
4862 /* Process the final unqualified-id. */
4863 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4864 check_dependency_p,
4865 declarator_p,
4866 /*optional_p=*/false);
4867 /* Restore the SAVED_SCOPE for our caller. */
4868 parser->scope = saved_scope;
4869 parser->object_scope = saved_object_scope;
4870 parser->qualifying_scope = saved_qualifying_scope;
4872 return unqualified_id;
4874 /* Otherwise, if we are in global scope, then we are looking at one
4875 of the other qualified-id productions. */
4876 else if (global_scope_p)
4878 cp_token *token;
4879 tree id;
4881 /* Peek at the next token. */
4882 token = cp_lexer_peek_token (parser->lexer);
4884 /* If it's an identifier, and the next token is not a "<", then
4885 we can avoid the template-id case. This is an optimization
4886 for this common case. */
4887 if (token->type == CPP_NAME
4888 && !cp_parser_nth_token_starts_template_argument_list_p
4889 (parser, 2))
4890 return cp_parser_identifier (parser);
4892 cp_parser_parse_tentatively (parser);
4893 /* Try a template-id. */
4894 id = cp_parser_template_id (parser,
4895 /*template_keyword_p=*/false,
4896 /*check_dependency_p=*/true,
4897 none_type,
4898 declarator_p);
4899 /* If that worked, we're done. */
4900 if (cp_parser_parse_definitely (parser))
4901 return id;
4903 /* Peek at the next token. (Changes in the token buffer may
4904 have invalidated the pointer obtained above.) */
4905 token = cp_lexer_peek_token (parser->lexer);
4907 switch (token->type)
4909 case CPP_NAME:
4910 return cp_parser_identifier (parser);
4912 case CPP_KEYWORD:
4913 if (token->keyword == RID_OPERATOR)
4914 return cp_parser_operator_function_id (parser);
4915 /* Fall through. */
4917 default:
4918 cp_parser_error (parser, "expected id-expression");
4919 return error_mark_node;
4922 else
4923 return cp_parser_unqualified_id (parser, template_keyword_p,
4924 /*check_dependency_p=*/true,
4925 declarator_p,
4926 optional_p);
4929 /* Parse an unqualified-id.
4931 unqualified-id:
4932 identifier
4933 operator-function-id
4934 conversion-function-id
4935 ~ class-name
4936 template-id
4938 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4939 keyword, in a construct like `A::template ...'.
4941 Returns a representation of unqualified-id. For the `identifier'
4942 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4943 production a BIT_NOT_EXPR is returned; the operand of the
4944 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4945 other productions, see the documentation accompanying the
4946 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4947 names are looked up in uninstantiated templates. If DECLARATOR_P
4948 is true, the unqualified-id is appearing as part of a declarator,
4949 rather than as part of an expression. */
4951 static tree
4952 cp_parser_unqualified_id (cp_parser* parser,
4953 bool template_keyword_p,
4954 bool check_dependency_p,
4955 bool declarator_p,
4956 bool optional_p)
4958 cp_token *token;
4960 /* Peek at the next token. */
4961 token = cp_lexer_peek_token (parser->lexer);
4963 switch ((int) token->type)
4965 case CPP_NAME:
4967 tree id;
4969 /* We don't know yet whether or not this will be a
4970 template-id. */
4971 cp_parser_parse_tentatively (parser);
4972 /* Try a template-id. */
4973 id = cp_parser_template_id (parser, template_keyword_p,
4974 check_dependency_p,
4975 none_type,
4976 declarator_p);
4977 /* If it worked, we're done. */
4978 if (cp_parser_parse_definitely (parser))
4979 return id;
4980 /* Otherwise, it's an ordinary identifier. */
4981 return cp_parser_identifier (parser);
4984 case CPP_TEMPLATE_ID:
4985 return cp_parser_template_id (parser, template_keyword_p,
4986 check_dependency_p,
4987 none_type,
4988 declarator_p);
4990 case CPP_COMPL:
4992 tree type_decl;
4993 tree qualifying_scope;
4994 tree object_scope;
4995 tree scope;
4996 bool done;
4998 /* Consume the `~' token. */
4999 cp_lexer_consume_token (parser->lexer);
5000 /* Parse the class-name. The standard, as written, seems to
5001 say that:
5003 template <typename T> struct S { ~S (); };
5004 template <typename T> S<T>::~S() {}
5006 is invalid, since `~' must be followed by a class-name, but
5007 `S<T>' is dependent, and so not known to be a class.
5008 That's not right; we need to look in uninstantiated
5009 templates. A further complication arises from:
5011 template <typename T> void f(T t) {
5012 t.T::~T();
5015 Here, it is not possible to look up `T' in the scope of `T'
5016 itself. We must look in both the current scope, and the
5017 scope of the containing complete expression.
5019 Yet another issue is:
5021 struct S {
5022 int S;
5023 ~S();
5026 S::~S() {}
5028 The standard does not seem to say that the `S' in `~S'
5029 should refer to the type `S' and not the data member
5030 `S::S'. */
5032 /* DR 244 says that we look up the name after the "~" in the
5033 same scope as we looked up the qualifying name. That idea
5034 isn't fully worked out; it's more complicated than that. */
5035 scope = parser->scope;
5036 object_scope = parser->object_scope;
5037 qualifying_scope = parser->qualifying_scope;
5039 /* Check for invalid scopes. */
5040 if (scope == error_mark_node)
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5043 cp_lexer_consume_token (parser->lexer);
5044 return error_mark_node;
5046 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5048 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5049 error_at (token->location,
5050 "scope %qT before %<~%> is not a class-name",
5051 scope);
5052 cp_parser_simulate_error (parser);
5053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5054 cp_lexer_consume_token (parser->lexer);
5055 return error_mark_node;
5057 gcc_assert (!scope || TYPE_P (scope));
5059 /* If the name is of the form "X::~X" it's OK even if X is a
5060 typedef. */
5061 token = cp_lexer_peek_token (parser->lexer);
5062 if (scope
5063 && token->type == CPP_NAME
5064 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5065 != CPP_LESS)
5066 && (token->u.value == TYPE_IDENTIFIER (scope)
5067 || (CLASS_TYPE_P (scope)
5068 && constructor_name_p (token->u.value, scope))))
5070 cp_lexer_consume_token (parser->lexer);
5071 return build_nt (BIT_NOT_EXPR, scope);
5074 /* ~auto means the destructor of whatever the object is. */
5075 if (cp_parser_is_keyword (token, RID_AUTO))
5077 if (cxx_dialect < cxx14)
5078 pedwarn (input_location, 0,
5079 "%<~auto%> only available with "
5080 "-std=c++14 or -std=gnu++14");
5081 cp_lexer_consume_token (parser->lexer);
5082 return build_nt (BIT_NOT_EXPR, make_auto ());
5085 /* If there was an explicit qualification (S::~T), first look
5086 in the scope given by the qualification (i.e., S).
5088 Note: in the calls to cp_parser_class_name below we pass
5089 typename_type so that lookup finds the injected-class-name
5090 rather than the constructor. */
5091 done = false;
5092 type_decl = NULL_TREE;
5093 if (scope)
5095 cp_parser_parse_tentatively (parser);
5096 type_decl = cp_parser_class_name (parser,
5097 /*typename_keyword_p=*/false,
5098 /*template_keyword_p=*/false,
5099 typename_type,
5100 /*check_dependency=*/false,
5101 /*class_head_p=*/false,
5102 declarator_p);
5103 if (cp_parser_parse_definitely (parser))
5104 done = true;
5106 /* In "N::S::~S", look in "N" as well. */
5107 if (!done && scope && qualifying_scope)
5109 cp_parser_parse_tentatively (parser);
5110 parser->scope = qualifying_scope;
5111 parser->object_scope = NULL_TREE;
5112 parser->qualifying_scope = NULL_TREE;
5113 type_decl
5114 = cp_parser_class_name (parser,
5115 /*typename_keyword_p=*/false,
5116 /*template_keyword_p=*/false,
5117 typename_type,
5118 /*check_dependency=*/false,
5119 /*class_head_p=*/false,
5120 declarator_p);
5121 if (cp_parser_parse_definitely (parser))
5122 done = true;
5124 /* In "p->S::~T", look in the scope given by "*p" as well. */
5125 else if (!done && object_scope)
5127 cp_parser_parse_tentatively (parser);
5128 parser->scope = object_scope;
5129 parser->object_scope = NULL_TREE;
5130 parser->qualifying_scope = NULL_TREE;
5131 type_decl
5132 = cp_parser_class_name (parser,
5133 /*typename_keyword_p=*/false,
5134 /*template_keyword_p=*/false,
5135 typename_type,
5136 /*check_dependency=*/false,
5137 /*class_head_p=*/false,
5138 declarator_p);
5139 if (cp_parser_parse_definitely (parser))
5140 done = true;
5142 /* Look in the surrounding context. */
5143 if (!done)
5145 parser->scope = NULL_TREE;
5146 parser->object_scope = NULL_TREE;
5147 parser->qualifying_scope = NULL_TREE;
5148 if (processing_template_decl)
5149 cp_parser_parse_tentatively (parser);
5150 type_decl
5151 = cp_parser_class_name (parser,
5152 /*typename_keyword_p=*/false,
5153 /*template_keyword_p=*/false,
5154 typename_type,
5155 /*check_dependency=*/false,
5156 /*class_head_p=*/false,
5157 declarator_p);
5158 if (processing_template_decl
5159 && ! cp_parser_parse_definitely (parser))
5161 /* We couldn't find a type with this name, so just accept
5162 it and check for a match at instantiation time. */
5163 type_decl = cp_parser_identifier (parser);
5164 if (type_decl != error_mark_node)
5165 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5166 return type_decl;
5169 /* If an error occurred, assume that the name of the
5170 destructor is the same as the name of the qualifying
5171 class. That allows us to keep parsing after running
5172 into ill-formed destructor names. */
5173 if (type_decl == error_mark_node && scope)
5174 return build_nt (BIT_NOT_EXPR, scope);
5175 else if (type_decl == error_mark_node)
5176 return error_mark_node;
5178 /* Check that destructor name and scope match. */
5179 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5182 error_at (token->location,
5183 "declaration of %<~%T%> as member of %qT",
5184 type_decl, scope);
5185 cp_parser_simulate_error (parser);
5186 return error_mark_node;
5189 /* [class.dtor]
5191 A typedef-name that names a class shall not be used as the
5192 identifier in the declarator for a destructor declaration. */
5193 if (declarator_p
5194 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5195 && !DECL_SELF_REFERENCE_P (type_decl)
5196 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5197 error_at (token->location,
5198 "typedef-name %qD used as destructor declarator",
5199 type_decl);
5201 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5204 case CPP_KEYWORD:
5205 if (token->keyword == RID_OPERATOR)
5207 tree id;
5209 /* This could be a template-id, so we try that first. */
5210 cp_parser_parse_tentatively (parser);
5211 /* Try a template-id. */
5212 id = cp_parser_template_id (parser, template_keyword_p,
5213 /*check_dependency_p=*/true,
5214 none_type,
5215 declarator_p);
5216 /* If that worked, we're done. */
5217 if (cp_parser_parse_definitely (parser))
5218 return id;
5219 /* We still don't know whether we're looking at an
5220 operator-function-id or a conversion-function-id. */
5221 cp_parser_parse_tentatively (parser);
5222 /* Try an operator-function-id. */
5223 id = cp_parser_operator_function_id (parser);
5224 /* If that didn't work, try a conversion-function-id. */
5225 if (!cp_parser_parse_definitely (parser))
5226 id = cp_parser_conversion_function_id (parser);
5227 else if (UDLIT_OPER_P (id))
5229 /* 17.6.3.3.5 */
5230 const char *name = UDLIT_OP_SUFFIX (id);
5231 if (name[0] != '_' && !in_system_header_at (input_location)
5232 && declarator_p)
5233 warning (0, "literal operator suffixes not preceded by %<_%>"
5234 " are reserved for future standardization");
5237 return id;
5239 /* Fall through. */
5241 default:
5242 if (optional_p)
5243 return NULL_TREE;
5244 cp_parser_error (parser, "expected unqualified-id");
5245 return error_mark_node;
5249 /* Parse an (optional) nested-name-specifier.
5251 nested-name-specifier: [C++98]
5252 class-or-namespace-name :: nested-name-specifier [opt]
5253 class-or-namespace-name :: template nested-name-specifier [opt]
5255 nested-name-specifier: [C++0x]
5256 type-name ::
5257 namespace-name ::
5258 nested-name-specifier identifier ::
5259 nested-name-specifier template [opt] simple-template-id ::
5261 PARSER->SCOPE should be set appropriately before this function is
5262 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5263 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5264 in name lookups.
5266 Sets PARSER->SCOPE to the class (TYPE) or namespace
5267 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5268 it unchanged if there is no nested-name-specifier. Returns the new
5269 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5271 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5272 part of a declaration and/or decl-specifier. */
5274 static tree
5275 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5276 bool typename_keyword_p,
5277 bool check_dependency_p,
5278 bool type_p,
5279 bool is_declaration)
5281 bool success = false;
5282 cp_token_position start = 0;
5283 cp_token *token;
5285 /* Remember where the nested-name-specifier starts. */
5286 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5288 start = cp_lexer_token_position (parser->lexer, false);
5289 push_deferring_access_checks (dk_deferred);
5292 while (true)
5294 tree new_scope;
5295 tree old_scope;
5296 tree saved_qualifying_scope;
5297 bool template_keyword_p;
5299 /* Spot cases that cannot be the beginning of a
5300 nested-name-specifier. */
5301 token = cp_lexer_peek_token (parser->lexer);
5303 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5304 the already parsed nested-name-specifier. */
5305 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5307 /* Grab the nested-name-specifier and continue the loop. */
5308 cp_parser_pre_parsed_nested_name_specifier (parser);
5309 /* If we originally encountered this nested-name-specifier
5310 with IS_DECLARATION set to false, we will not have
5311 resolved TYPENAME_TYPEs, so we must do so here. */
5312 if (is_declaration
5313 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5315 new_scope = resolve_typename_type (parser->scope,
5316 /*only_current_p=*/false);
5317 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5318 parser->scope = new_scope;
5320 success = true;
5321 continue;
5324 /* Spot cases that cannot be the beginning of a
5325 nested-name-specifier. On the second and subsequent times
5326 through the loop, we look for the `template' keyword. */
5327 if (success && token->keyword == RID_TEMPLATE)
5329 /* A template-id can start a nested-name-specifier. */
5330 else if (token->type == CPP_TEMPLATE_ID)
5332 /* DR 743: decltype can be used in a nested-name-specifier. */
5333 else if (token_is_decltype (token))
5335 else
5337 /* If the next token is not an identifier, then it is
5338 definitely not a type-name or namespace-name. */
5339 if (token->type != CPP_NAME)
5340 break;
5341 /* If the following token is neither a `<' (to begin a
5342 template-id), nor a `::', then we are not looking at a
5343 nested-name-specifier. */
5344 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5346 if (token->type == CPP_COLON
5347 && parser->colon_corrects_to_scope_p
5348 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5350 error_at (token->location,
5351 "found %<:%> in nested-name-specifier, expected %<::%>");
5352 token->type = CPP_SCOPE;
5355 if (token->type != CPP_SCOPE
5356 && !cp_parser_nth_token_starts_template_argument_list_p
5357 (parser, 2))
5358 break;
5361 /* The nested-name-specifier is optional, so we parse
5362 tentatively. */
5363 cp_parser_parse_tentatively (parser);
5365 /* Look for the optional `template' keyword, if this isn't the
5366 first time through the loop. */
5367 if (success)
5368 template_keyword_p = cp_parser_optional_template_keyword (parser);
5369 else
5370 template_keyword_p = false;
5372 /* Save the old scope since the name lookup we are about to do
5373 might destroy it. */
5374 old_scope = parser->scope;
5375 saved_qualifying_scope = parser->qualifying_scope;
5376 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5377 look up names in "X<T>::I" in order to determine that "Y" is
5378 a template. So, if we have a typename at this point, we make
5379 an effort to look through it. */
5380 if (is_declaration
5381 && !typename_keyword_p
5382 && parser->scope
5383 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5384 parser->scope = resolve_typename_type (parser->scope,
5385 /*only_current_p=*/false);
5386 /* Parse the qualifying entity. */
5387 new_scope
5388 = cp_parser_qualifying_entity (parser,
5389 typename_keyword_p,
5390 template_keyword_p,
5391 check_dependency_p,
5392 type_p,
5393 is_declaration);
5394 /* Look for the `::' token. */
5395 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5397 /* If we found what we wanted, we keep going; otherwise, we're
5398 done. */
5399 if (!cp_parser_parse_definitely (parser))
5401 bool error_p = false;
5403 /* Restore the OLD_SCOPE since it was valid before the
5404 failed attempt at finding the last
5405 class-or-namespace-name. */
5406 parser->scope = old_scope;
5407 parser->qualifying_scope = saved_qualifying_scope;
5409 /* If the next token is a decltype, and the one after that is a
5410 `::', then the decltype has failed to resolve to a class or
5411 enumeration type. Give this error even when parsing
5412 tentatively since it can't possibly be valid--and we're going
5413 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5414 won't get another chance.*/
5415 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5416 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5417 == CPP_SCOPE))
5419 token = cp_lexer_consume_token (parser->lexer);
5420 error_at (token->location, "decltype evaluates to %qT, "
5421 "which is not a class or enumeration type",
5422 token->u.value);
5423 parser->scope = error_mark_node;
5424 error_p = true;
5425 /* As below. */
5426 success = true;
5427 cp_lexer_consume_token (parser->lexer);
5430 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5431 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5433 /* If we have a non-type template-id followed by ::, it can't
5434 possibly be valid. */
5435 token = cp_lexer_peek_token (parser->lexer);
5436 tree tid = token->u.tree_check_value->value;
5437 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5438 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5440 tree tmpl = NULL_TREE;
5441 if (is_overloaded_fn (tid))
5443 tree fns = get_fns (tid);
5444 if (!OVL_CHAIN (fns))
5445 tmpl = OVL_CURRENT (fns);
5446 error_at (token->location, "function template-id %qD "
5447 "in nested-name-specifier", tid);
5449 else
5451 /* Variable template. */
5452 tmpl = TREE_OPERAND (tid, 0);
5453 gcc_assert (variable_template_p (tmpl));
5454 error_at (token->location, "variable template-id %qD "
5455 "in nested-name-specifier", tid);
5457 if (tmpl)
5458 inform (DECL_SOURCE_LOCATION (tmpl),
5459 "%qD declared here", tmpl);
5461 parser->scope = error_mark_node;
5462 error_p = true;
5463 /* As below. */
5464 success = true;
5465 cp_lexer_consume_token (parser->lexer);
5466 cp_lexer_consume_token (parser->lexer);
5470 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5471 break;
5472 /* If the next token is an identifier, and the one after
5473 that is a `::', then any valid interpretation would have
5474 found a class-or-namespace-name. */
5475 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5476 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5477 == CPP_SCOPE)
5478 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5479 != CPP_COMPL))
5481 token = cp_lexer_consume_token (parser->lexer);
5482 if (!error_p)
5484 if (!token->error_reported)
5486 tree decl;
5487 tree ambiguous_decls;
5489 decl = cp_parser_lookup_name (parser, token->u.value,
5490 none_type,
5491 /*is_template=*/false,
5492 /*is_namespace=*/false,
5493 /*check_dependency=*/true,
5494 &ambiguous_decls,
5495 token->location);
5496 if (TREE_CODE (decl) == TEMPLATE_DECL)
5497 error_at (token->location,
5498 "%qD used without template parameters",
5499 decl);
5500 else if (ambiguous_decls)
5502 // cp_parser_lookup_name has the same diagnostic,
5503 // thus make sure to emit it at most once.
5504 if (cp_parser_uncommitted_to_tentative_parse_p
5505 (parser))
5507 error_at (token->location,
5508 "reference to %qD is ambiguous",
5509 token->u.value);
5510 print_candidates (ambiguous_decls);
5512 decl = error_mark_node;
5514 else
5516 if (cxx_dialect != cxx98)
5517 cp_parser_name_lookup_error
5518 (parser, token->u.value, decl, NLE_NOT_CXX98,
5519 token->location);
5520 else
5521 cp_parser_name_lookup_error
5522 (parser, token->u.value, decl, NLE_CXX98,
5523 token->location);
5526 parser->scope = error_mark_node;
5527 error_p = true;
5528 /* Treat this as a successful nested-name-specifier
5529 due to:
5531 [basic.lookup.qual]
5533 If the name found is not a class-name (clause
5534 _class_) or namespace-name (_namespace.def_), the
5535 program is ill-formed. */
5536 success = true;
5538 cp_lexer_consume_token (parser->lexer);
5540 break;
5542 /* We've found one valid nested-name-specifier. */
5543 success = true;
5544 /* Name lookup always gives us a DECL. */
5545 if (TREE_CODE (new_scope) == TYPE_DECL)
5546 new_scope = TREE_TYPE (new_scope);
5547 /* Uses of "template" must be followed by actual templates. */
5548 if (template_keyword_p
5549 && !(CLASS_TYPE_P (new_scope)
5550 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5551 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5552 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5553 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5554 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5555 == TEMPLATE_ID_EXPR)))
5556 permerror (input_location, TYPE_P (new_scope)
5557 ? G_("%qT is not a template")
5558 : G_("%qD is not a template"),
5559 new_scope);
5560 /* If it is a class scope, try to complete it; we are about to
5561 be looking up names inside the class. */
5562 if (TYPE_P (new_scope)
5563 /* Since checking types for dependency can be expensive,
5564 avoid doing it if the type is already complete. */
5565 && !COMPLETE_TYPE_P (new_scope)
5566 /* Do not try to complete dependent types. */
5567 && !dependent_type_p (new_scope))
5569 new_scope = complete_type (new_scope);
5570 /* If it is a typedef to current class, use the current
5571 class instead, as the typedef won't have any names inside
5572 it yet. */
5573 if (!COMPLETE_TYPE_P (new_scope)
5574 && currently_open_class (new_scope))
5575 new_scope = TYPE_MAIN_VARIANT (new_scope);
5577 /* Make sure we look in the right scope the next time through
5578 the loop. */
5579 parser->scope = new_scope;
5582 /* If parsing tentatively, replace the sequence of tokens that makes
5583 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5584 token. That way, should we re-parse the token stream, we will
5585 not have to repeat the effort required to do the parse, nor will
5586 we issue duplicate error messages. */
5587 if (success && start)
5589 cp_token *token;
5591 token = cp_lexer_token_at (parser->lexer, start);
5592 /* Reset the contents of the START token. */
5593 token->type = CPP_NESTED_NAME_SPECIFIER;
5594 /* Retrieve any deferred checks. Do not pop this access checks yet
5595 so the memory will not be reclaimed during token replacing below. */
5596 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5597 token->u.tree_check_value->value = parser->scope;
5598 token->u.tree_check_value->checks = get_deferred_access_checks ();
5599 token->u.tree_check_value->qualifying_scope =
5600 parser->qualifying_scope;
5601 token->keyword = RID_MAX;
5603 /* Purge all subsequent tokens. */
5604 cp_lexer_purge_tokens_after (parser->lexer, start);
5607 if (start)
5608 pop_to_parent_deferring_access_checks ();
5610 return success ? parser->scope : NULL_TREE;
5613 /* Parse a nested-name-specifier. See
5614 cp_parser_nested_name_specifier_opt for details. This function
5615 behaves identically, except that it will an issue an error if no
5616 nested-name-specifier is present. */
5618 static tree
5619 cp_parser_nested_name_specifier (cp_parser *parser,
5620 bool typename_keyword_p,
5621 bool check_dependency_p,
5622 bool type_p,
5623 bool is_declaration)
5625 tree scope;
5627 /* Look for the nested-name-specifier. */
5628 scope = cp_parser_nested_name_specifier_opt (parser,
5629 typename_keyword_p,
5630 check_dependency_p,
5631 type_p,
5632 is_declaration);
5633 /* If it was not present, issue an error message. */
5634 if (!scope)
5636 cp_parser_error (parser, "expected nested-name-specifier");
5637 parser->scope = NULL_TREE;
5640 return scope;
5643 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5644 this is either a class-name or a namespace-name (which corresponds
5645 to the class-or-namespace-name production in the grammar). For
5646 C++0x, it can also be a type-name that refers to an enumeration
5647 type or a simple-template-id.
5649 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5650 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5651 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5652 TYPE_P is TRUE iff the next name should be taken as a class-name,
5653 even the same name is declared to be another entity in the same
5654 scope.
5656 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5657 specified by the class-or-namespace-name. If neither is found the
5658 ERROR_MARK_NODE is returned. */
5660 static tree
5661 cp_parser_qualifying_entity (cp_parser *parser,
5662 bool typename_keyword_p,
5663 bool template_keyword_p,
5664 bool check_dependency_p,
5665 bool type_p,
5666 bool is_declaration)
5668 tree saved_scope;
5669 tree saved_qualifying_scope;
5670 tree saved_object_scope;
5671 tree scope;
5672 bool only_class_p;
5673 bool successful_parse_p;
5675 /* DR 743: decltype can appear in a nested-name-specifier. */
5676 if (cp_lexer_next_token_is_decltype (parser->lexer))
5678 scope = cp_parser_decltype (parser);
5679 if (TREE_CODE (scope) != ENUMERAL_TYPE
5680 && !MAYBE_CLASS_TYPE_P (scope))
5682 cp_parser_simulate_error (parser);
5683 return error_mark_node;
5685 if (TYPE_NAME (scope))
5686 scope = TYPE_NAME (scope);
5687 return scope;
5690 /* Before we try to parse the class-name, we must save away the
5691 current PARSER->SCOPE since cp_parser_class_name will destroy
5692 it. */
5693 saved_scope = parser->scope;
5694 saved_qualifying_scope = parser->qualifying_scope;
5695 saved_object_scope = parser->object_scope;
5696 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5697 there is no need to look for a namespace-name. */
5698 only_class_p = template_keyword_p
5699 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5700 if (!only_class_p)
5701 cp_parser_parse_tentatively (parser);
5702 scope = cp_parser_class_name (parser,
5703 typename_keyword_p,
5704 template_keyword_p,
5705 type_p ? class_type : none_type,
5706 check_dependency_p,
5707 /*class_head_p=*/false,
5708 is_declaration,
5709 /*enum_ok=*/cxx_dialect > cxx98);
5710 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5711 /* If that didn't work, try for a namespace-name. */
5712 if (!only_class_p && !successful_parse_p)
5714 /* Restore the saved scope. */
5715 parser->scope = saved_scope;
5716 parser->qualifying_scope = saved_qualifying_scope;
5717 parser->object_scope = saved_object_scope;
5718 /* If we are not looking at an identifier followed by the scope
5719 resolution operator, then this is not part of a
5720 nested-name-specifier. (Note that this function is only used
5721 to parse the components of a nested-name-specifier.) */
5722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5723 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5724 return error_mark_node;
5725 scope = cp_parser_namespace_name (parser);
5728 return scope;
5731 /* Return true if we are looking at a compound-literal, false otherwise. */
5733 static bool
5734 cp_parser_compound_literal_p (cp_parser *parser)
5736 /* Consume the `('. */
5737 cp_lexer_consume_token (parser->lexer);
5739 cp_lexer_save_tokens (parser->lexer);
5741 /* Skip tokens until the next token is a closing parenthesis.
5742 If we find the closing `)', and the next token is a `{', then
5743 we are looking at a compound-literal. */
5744 bool compound_literal_p
5745 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5746 /*consume_paren=*/true)
5747 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5749 /* Roll back the tokens we skipped. */
5750 cp_lexer_rollback_tokens (parser->lexer);
5752 return compound_literal_p;
5755 /* Parse a postfix-expression.
5757 postfix-expression:
5758 primary-expression
5759 postfix-expression [ expression ]
5760 postfix-expression ( expression-list [opt] )
5761 simple-type-specifier ( expression-list [opt] )
5762 typename :: [opt] nested-name-specifier identifier
5763 ( expression-list [opt] )
5764 typename :: [opt] nested-name-specifier template [opt] template-id
5765 ( expression-list [opt] )
5766 postfix-expression . template [opt] id-expression
5767 postfix-expression -> template [opt] id-expression
5768 postfix-expression . pseudo-destructor-name
5769 postfix-expression -> pseudo-destructor-name
5770 postfix-expression ++
5771 postfix-expression --
5772 dynamic_cast < type-id > ( expression )
5773 static_cast < type-id > ( expression )
5774 reinterpret_cast < type-id > ( expression )
5775 const_cast < type-id > ( expression )
5776 typeid ( expression )
5777 typeid ( type-id )
5779 GNU Extension:
5781 postfix-expression:
5782 ( type-id ) { initializer-list , [opt] }
5784 This extension is a GNU version of the C99 compound-literal
5785 construct. (The C99 grammar uses `type-name' instead of `type-id',
5786 but they are essentially the same concept.)
5788 If ADDRESS_P is true, the postfix expression is the operand of the
5789 `&' operator. CAST_P is true if this expression is the target of a
5790 cast.
5792 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5793 class member access expressions [expr.ref].
5795 Returns a representation of the expression. */
5797 static tree
5798 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5799 bool member_access_only_p, bool decltype_p,
5800 cp_id_kind * pidk_return)
5802 cp_token *token;
5803 location_t loc;
5804 enum rid keyword;
5805 cp_id_kind idk = CP_ID_KIND_NONE;
5806 tree postfix_expression = NULL_TREE;
5807 bool is_member_access = false;
5808 int saved_in_statement = -1;
5810 /* Peek at the next token. */
5811 token = cp_lexer_peek_token (parser->lexer);
5812 loc = token->location;
5813 /* Some of the productions are determined by keywords. */
5814 keyword = token->keyword;
5815 switch (keyword)
5817 case RID_DYNCAST:
5818 case RID_STATCAST:
5819 case RID_REINTCAST:
5820 case RID_CONSTCAST:
5822 tree type;
5823 tree expression;
5824 const char *saved_message;
5825 bool saved_in_type_id_in_expr_p;
5827 /* All of these can be handled in the same way from the point
5828 of view of parsing. Begin by consuming the token
5829 identifying the cast. */
5830 cp_lexer_consume_token (parser->lexer);
5832 /* New types cannot be defined in the cast. */
5833 saved_message = parser->type_definition_forbidden_message;
5834 parser->type_definition_forbidden_message
5835 = G_("types may not be defined in casts");
5837 /* Look for the opening `<'. */
5838 cp_parser_require (parser, CPP_LESS, RT_LESS);
5839 /* Parse the type to which we are casting. */
5840 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5841 parser->in_type_id_in_expr_p = true;
5842 type = cp_parser_type_id (parser);
5843 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5844 /* Look for the closing `>'. */
5845 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5846 /* Restore the old message. */
5847 parser->type_definition_forbidden_message = saved_message;
5849 bool saved_greater_than_is_operator_p
5850 = parser->greater_than_is_operator_p;
5851 parser->greater_than_is_operator_p = true;
5853 /* And the expression which is being cast. */
5854 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5855 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5858 parser->greater_than_is_operator_p
5859 = saved_greater_than_is_operator_p;
5861 /* Only type conversions to integral or enumeration types
5862 can be used in constant-expressions. */
5863 if (!cast_valid_in_integral_constant_expression_p (type)
5864 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5865 return error_mark_node;
5867 switch (keyword)
5869 case RID_DYNCAST:
5870 postfix_expression
5871 = build_dynamic_cast (type, expression, tf_warning_or_error);
5872 break;
5873 case RID_STATCAST:
5874 postfix_expression
5875 = build_static_cast (type, expression, tf_warning_or_error);
5876 break;
5877 case RID_REINTCAST:
5878 postfix_expression
5879 = build_reinterpret_cast (type, expression,
5880 tf_warning_or_error);
5881 break;
5882 case RID_CONSTCAST:
5883 postfix_expression
5884 = build_const_cast (type, expression, tf_warning_or_error);
5885 break;
5886 default:
5887 gcc_unreachable ();
5890 break;
5892 case RID_TYPEID:
5894 tree type;
5895 const char *saved_message;
5896 bool saved_in_type_id_in_expr_p;
5898 /* Consume the `typeid' token. */
5899 cp_lexer_consume_token (parser->lexer);
5900 /* Look for the `(' token. */
5901 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5902 /* Types cannot be defined in a `typeid' expression. */
5903 saved_message = parser->type_definition_forbidden_message;
5904 parser->type_definition_forbidden_message
5905 = G_("types may not be defined in a %<typeid%> expression");
5906 /* We can't be sure yet whether we're looking at a type-id or an
5907 expression. */
5908 cp_parser_parse_tentatively (parser);
5909 /* Try a type-id first. */
5910 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5911 parser->in_type_id_in_expr_p = true;
5912 type = cp_parser_type_id (parser);
5913 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5914 /* Look for the `)' token. Otherwise, we can't be sure that
5915 we're not looking at an expression: consider `typeid (int
5916 (3))', for example. */
5917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5918 /* If all went well, simply lookup the type-id. */
5919 if (cp_parser_parse_definitely (parser))
5920 postfix_expression = get_typeid (type, tf_warning_or_error);
5921 /* Otherwise, fall back to the expression variant. */
5922 else
5924 tree expression;
5926 /* Look for an expression. */
5927 expression = cp_parser_expression (parser, & idk);
5928 /* Compute its typeid. */
5929 postfix_expression = build_typeid (expression, tf_warning_or_error);
5930 /* Look for the `)' token. */
5931 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5933 /* Restore the saved message. */
5934 parser->type_definition_forbidden_message = saved_message;
5935 /* `typeid' may not appear in an integral constant expression. */
5936 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5937 return error_mark_node;
5939 break;
5941 case RID_TYPENAME:
5943 tree type;
5944 /* The syntax permitted here is the same permitted for an
5945 elaborated-type-specifier. */
5946 type = cp_parser_elaborated_type_specifier (parser,
5947 /*is_friend=*/false,
5948 /*is_declaration=*/false);
5949 postfix_expression = cp_parser_functional_cast (parser, type);
5951 break;
5953 case RID_CILK_SPAWN:
5955 cp_lexer_consume_token (parser->lexer);
5956 token = cp_lexer_peek_token (parser->lexer);
5957 if (token->type == CPP_SEMICOLON)
5959 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5960 "an expression");
5961 postfix_expression = error_mark_node;
5962 break;
5964 else if (!current_function_decl)
5966 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5967 "inside a function");
5968 postfix_expression = error_mark_node;
5969 break;
5971 else
5973 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5974 saved_in_statement = parser->in_statement;
5975 parser->in_statement |= IN_CILK_SPAWN;
5977 cfun->calls_cilk_spawn = 1;
5978 postfix_expression =
5979 cp_parser_postfix_expression (parser, false, false,
5980 false, false, &idk);
5981 if (!flag_cilkplus)
5983 error_at (token->location, "-fcilkplus must be enabled to use"
5984 " %<_Cilk_spawn%>");
5985 cfun->calls_cilk_spawn = 0;
5987 else if (saved_in_statement & IN_CILK_SPAWN)
5989 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5990 "are not permitted");
5991 postfix_expression = error_mark_node;
5992 cfun->calls_cilk_spawn = 0;
5994 else
5996 postfix_expression = build_cilk_spawn (token->location,
5997 postfix_expression);
5998 if (postfix_expression != error_mark_node)
5999 SET_EXPR_LOCATION (postfix_expression, input_location);
6000 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6002 break;
6005 case RID_BUILTIN_SHUFFLE:
6007 vec<tree, va_gc> *vec;
6008 unsigned int i;
6009 tree p;
6011 cp_lexer_consume_token (parser->lexer);
6012 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6013 /*cast_p=*/false, /*allow_expansion_p=*/true,
6014 /*non_constant_p=*/NULL);
6015 if (vec == NULL)
6016 return error_mark_node;
6018 FOR_EACH_VEC_ELT (*vec, i, p)
6019 mark_exp_read (p);
6021 if (vec->length () == 2)
6022 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6023 tf_warning_or_error);
6024 else if (vec->length () == 3)
6025 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6026 tf_warning_or_error);
6027 else
6029 error_at (loc, "wrong number of arguments to "
6030 "%<__builtin_shuffle%>");
6031 return error_mark_node;
6033 break;
6036 default:
6038 tree type;
6040 /* If the next thing is a simple-type-specifier, we may be
6041 looking at a functional cast. We could also be looking at
6042 an id-expression. So, we try the functional cast, and if
6043 that doesn't work we fall back to the primary-expression. */
6044 cp_parser_parse_tentatively (parser);
6045 /* Look for the simple-type-specifier. */
6046 type = cp_parser_simple_type_specifier (parser,
6047 /*decl_specs=*/NULL,
6048 CP_PARSER_FLAGS_NONE);
6049 /* Parse the cast itself. */
6050 if (!cp_parser_error_occurred (parser))
6051 postfix_expression
6052 = cp_parser_functional_cast (parser, type);
6053 /* If that worked, we're done. */
6054 if (cp_parser_parse_definitely (parser))
6055 break;
6057 /* If the functional-cast didn't work out, try a
6058 compound-literal. */
6059 if (cp_parser_allow_gnu_extensions_p (parser)
6060 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6062 tree initializer = NULL_TREE;
6064 cp_parser_parse_tentatively (parser);
6066 /* Avoid calling cp_parser_type_id pointlessly, see comment
6067 in cp_parser_cast_expression about c++/29234. */
6068 if (!cp_parser_compound_literal_p (parser))
6069 cp_parser_simulate_error (parser);
6070 else
6072 /* Parse the type. */
6073 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6074 parser->in_type_id_in_expr_p = true;
6075 type = cp_parser_type_id (parser);
6076 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6077 /* Look for the `)'. */
6078 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6081 /* If things aren't going well, there's no need to
6082 keep going. */
6083 if (!cp_parser_error_occurred (parser))
6085 bool non_constant_p;
6086 /* Parse the brace-enclosed initializer list. */
6087 initializer = cp_parser_braced_list (parser,
6088 &non_constant_p);
6090 /* If that worked, we're definitely looking at a
6091 compound-literal expression. */
6092 if (cp_parser_parse_definitely (parser))
6094 /* Warn the user that a compound literal is not
6095 allowed in standard C++. */
6096 pedwarn (input_location, OPT_Wpedantic,
6097 "ISO C++ forbids compound-literals");
6098 /* For simplicity, we disallow compound literals in
6099 constant-expressions. We could
6100 allow compound literals of integer type, whose
6101 initializer was a constant, in constant
6102 expressions. Permitting that usage, as a further
6103 extension, would not change the meaning of any
6104 currently accepted programs. (Of course, as
6105 compound literals are not part of ISO C++, the
6106 standard has nothing to say.) */
6107 if (cp_parser_non_integral_constant_expression (parser,
6108 NIC_NCC))
6110 postfix_expression = error_mark_node;
6111 break;
6113 /* Form the representation of the compound-literal. */
6114 postfix_expression
6115 = finish_compound_literal (type, initializer,
6116 tf_warning_or_error);
6117 break;
6121 /* It must be a primary-expression. */
6122 postfix_expression
6123 = cp_parser_primary_expression (parser, address_p, cast_p,
6124 /*template_arg_p=*/false,
6125 decltype_p,
6126 &idk);
6128 break;
6131 /* Note that we don't need to worry about calling build_cplus_new on a
6132 class-valued CALL_EXPR in decltype when it isn't the end of the
6133 postfix-expression; unary_complex_lvalue will take care of that for
6134 all these cases. */
6136 /* Keep looping until the postfix-expression is complete. */
6137 while (true)
6139 if (idk == CP_ID_KIND_UNQUALIFIED
6140 && identifier_p (postfix_expression)
6141 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6142 /* It is not a Koenig lookup function call. */
6143 postfix_expression
6144 = unqualified_name_lookup_error (postfix_expression);
6146 /* Peek at the next token. */
6147 token = cp_lexer_peek_token (parser->lexer);
6149 switch (token->type)
6151 case CPP_OPEN_SQUARE:
6152 if (cp_next_tokens_can_be_std_attribute_p (parser))
6154 cp_parser_error (parser,
6155 "two consecutive %<[%> shall "
6156 "only introduce an attribute");
6157 return error_mark_node;
6159 postfix_expression
6160 = cp_parser_postfix_open_square_expression (parser,
6161 postfix_expression,
6162 false,
6163 decltype_p);
6164 idk = CP_ID_KIND_NONE;
6165 is_member_access = false;
6166 break;
6168 case CPP_OPEN_PAREN:
6169 /* postfix-expression ( expression-list [opt] ) */
6171 bool koenig_p;
6172 bool is_builtin_constant_p;
6173 bool saved_integral_constant_expression_p = false;
6174 bool saved_non_integral_constant_expression_p = false;
6175 tsubst_flags_t complain = complain_flags (decltype_p);
6176 vec<tree, va_gc> *args;
6178 is_member_access = false;
6180 is_builtin_constant_p
6181 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6182 if (is_builtin_constant_p)
6184 /* The whole point of __builtin_constant_p is to allow
6185 non-constant expressions to appear as arguments. */
6186 saved_integral_constant_expression_p
6187 = parser->integral_constant_expression_p;
6188 saved_non_integral_constant_expression_p
6189 = parser->non_integral_constant_expression_p;
6190 parser->integral_constant_expression_p = false;
6192 args = (cp_parser_parenthesized_expression_list
6193 (parser, non_attr,
6194 /*cast_p=*/false, /*allow_expansion_p=*/true,
6195 /*non_constant_p=*/NULL,
6196 /*want_literal_zero_p=*/warn_memset_transposed_args));
6197 if (is_builtin_constant_p)
6199 parser->integral_constant_expression_p
6200 = saved_integral_constant_expression_p;
6201 parser->non_integral_constant_expression_p
6202 = saved_non_integral_constant_expression_p;
6205 if (args == NULL)
6207 postfix_expression = error_mark_node;
6208 break;
6211 /* Function calls are not permitted in
6212 constant-expressions. */
6213 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6214 && cp_parser_non_integral_constant_expression (parser,
6215 NIC_FUNC_CALL))
6217 postfix_expression = error_mark_node;
6218 release_tree_vector (args);
6219 break;
6222 koenig_p = false;
6223 if (idk == CP_ID_KIND_UNQUALIFIED
6224 || idk == CP_ID_KIND_TEMPLATE_ID)
6226 if (identifier_p (postfix_expression))
6228 if (!args->is_empty ())
6230 koenig_p = true;
6231 if (!any_type_dependent_arguments_p (args))
6232 postfix_expression
6233 = perform_koenig_lookup (postfix_expression, args,
6234 complain);
6236 else
6237 postfix_expression
6238 = unqualified_fn_lookup_error (postfix_expression);
6240 /* We do not perform argument-dependent lookup if
6241 normal lookup finds a non-function, in accordance
6242 with the expected resolution of DR 218. */
6243 else if (!args->is_empty ()
6244 && is_overloaded_fn (postfix_expression))
6246 tree fn = get_first_fn (postfix_expression);
6247 fn = STRIP_TEMPLATE (fn);
6249 /* Do not do argument dependent lookup if regular
6250 lookup finds a member function or a block-scope
6251 function declaration. [basic.lookup.argdep]/3 */
6252 if (!DECL_FUNCTION_MEMBER_P (fn)
6253 && !DECL_LOCAL_FUNCTION_P (fn))
6255 koenig_p = true;
6256 if (!any_type_dependent_arguments_p (args))
6257 postfix_expression
6258 = perform_koenig_lookup (postfix_expression, args,
6259 complain);
6264 if (warn_memset_transposed_args)
6266 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6267 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6268 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6269 && vec_safe_length (args) == 3
6270 && integer_zerop ((*args)[2])
6271 && LITERAL_ZERO_P ((*args)[2])
6272 && !(integer_zerop ((*args)[1])
6273 && LITERAL_ZERO_P ((*args)[1])))
6274 warning (OPT_Wmemset_transposed_args,
6275 "%<memset%> used with constant zero length "
6276 "parameter; this could be due to transposed "
6277 "parameters");
6279 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6280 to avoid leaking those into folder and middle-end. */
6281 unsigned int i;
6282 tree arg;
6283 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6284 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6285 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6288 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6290 tree instance = TREE_OPERAND (postfix_expression, 0);
6291 tree fn = TREE_OPERAND (postfix_expression, 1);
6293 if (processing_template_decl
6294 && (type_dependent_expression_p (instance)
6295 || (!BASELINK_P (fn)
6296 && TREE_CODE (fn) != FIELD_DECL)
6297 || type_dependent_expression_p (fn)
6298 || any_type_dependent_arguments_p (args)))
6300 postfix_expression
6301 = build_nt_call_vec (postfix_expression, args);
6302 release_tree_vector (args);
6303 break;
6306 if (BASELINK_P (fn))
6308 postfix_expression
6309 = (build_new_method_call
6310 (instance, fn, &args, NULL_TREE,
6311 (idk == CP_ID_KIND_QUALIFIED
6312 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6313 : LOOKUP_NORMAL),
6314 /*fn_p=*/NULL,
6315 complain));
6317 else
6318 postfix_expression
6319 = finish_call_expr (postfix_expression, &args,
6320 /*disallow_virtual=*/false,
6321 /*koenig_p=*/false,
6322 complain);
6324 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6325 || TREE_CODE (postfix_expression) == MEMBER_REF
6326 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6327 postfix_expression = (build_offset_ref_call_from_tree
6328 (postfix_expression, &args,
6329 complain));
6330 else if (idk == CP_ID_KIND_QUALIFIED)
6331 /* A call to a static class member, or a namespace-scope
6332 function. */
6333 postfix_expression
6334 = finish_call_expr (postfix_expression, &args,
6335 /*disallow_virtual=*/true,
6336 koenig_p,
6337 complain);
6338 else
6339 /* All other function calls. */
6340 postfix_expression
6341 = finish_call_expr (postfix_expression, &args,
6342 /*disallow_virtual=*/false,
6343 koenig_p,
6344 complain);
6346 protected_set_expr_location (postfix_expression, token->location);
6348 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6349 idk = CP_ID_KIND_NONE;
6351 release_tree_vector (args);
6353 break;
6355 case CPP_DOT:
6356 case CPP_DEREF:
6357 /* postfix-expression . template [opt] id-expression
6358 postfix-expression . pseudo-destructor-name
6359 postfix-expression -> template [opt] id-expression
6360 postfix-expression -> pseudo-destructor-name */
6362 /* Consume the `.' or `->' operator. */
6363 cp_lexer_consume_token (parser->lexer);
6365 postfix_expression
6366 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6367 postfix_expression,
6368 false, &idk, loc);
6370 is_member_access = true;
6371 break;
6373 case CPP_PLUS_PLUS:
6374 /* postfix-expression ++ */
6375 /* Consume the `++' token. */
6376 cp_lexer_consume_token (parser->lexer);
6377 /* Generate a representation for the complete expression. */
6378 postfix_expression
6379 = finish_increment_expr (postfix_expression,
6380 POSTINCREMENT_EXPR);
6381 /* Increments may not appear in constant-expressions. */
6382 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6383 postfix_expression = error_mark_node;
6384 idk = CP_ID_KIND_NONE;
6385 is_member_access = false;
6386 break;
6388 case CPP_MINUS_MINUS:
6389 /* postfix-expression -- */
6390 /* Consume the `--' token. */
6391 cp_lexer_consume_token (parser->lexer);
6392 /* Generate a representation for the complete expression. */
6393 postfix_expression
6394 = finish_increment_expr (postfix_expression,
6395 POSTDECREMENT_EXPR);
6396 /* Decrements may not appear in constant-expressions. */
6397 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6398 postfix_expression = error_mark_node;
6399 idk = CP_ID_KIND_NONE;
6400 is_member_access = false;
6401 break;
6403 default:
6404 if (pidk_return != NULL)
6405 * pidk_return = idk;
6406 if (member_access_only_p)
6407 return is_member_access? postfix_expression : error_mark_node;
6408 else
6409 return postfix_expression;
6413 /* We should never get here. */
6414 gcc_unreachable ();
6415 return error_mark_node;
6418 /* This function parses Cilk Plus array notations. If a normal array expr. is
6419 parsed then the array index is passed back to the caller through *INIT_INDEX
6420 and the function returns a NULL_TREE. If array notation expr. is parsed,
6421 then *INIT_INDEX is ignored by the caller and the function returns
6422 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6423 error_mark_node. */
6425 static tree
6426 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6427 tree array_value)
6429 cp_token *token = NULL;
6430 tree length_index, stride = NULL_TREE, value_tree, array_type;
6431 if (!array_value || array_value == error_mark_node)
6433 cp_parser_skip_to_end_of_statement (parser);
6434 return error_mark_node;
6437 array_type = TREE_TYPE (array_value);
6439 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6440 parser->colon_corrects_to_scope_p = false;
6441 token = cp_lexer_peek_token (parser->lexer);
6443 if (!token)
6445 cp_parser_error (parser, "expected %<:%> or numeral");
6446 return error_mark_node;
6448 else if (token->type == CPP_COLON)
6450 /* Consume the ':'. */
6451 cp_lexer_consume_token (parser->lexer);
6453 /* If we are here, then we have a case like this A[:]. */
6454 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6456 cp_parser_error (parser, "expected %<]%>");
6457 cp_parser_skip_to_end_of_statement (parser);
6458 return error_mark_node;
6460 *init_index = NULL_TREE;
6461 stride = NULL_TREE;
6462 length_index = NULL_TREE;
6464 else
6466 /* If we are here, then there are three valid possibilities:
6467 1. ARRAY [ EXP ]
6468 2. ARRAY [ EXP : EXP ]
6469 3. ARRAY [ EXP : EXP : EXP ] */
6471 *init_index = cp_parser_expression (parser);
6472 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6474 /* This indicates that we have a normal array expression. */
6475 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6476 return NULL_TREE;
6479 /* Consume the ':'. */
6480 cp_lexer_consume_token (parser->lexer);
6481 length_index = cp_parser_expression (parser);
6482 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6484 cp_lexer_consume_token (parser->lexer);
6485 stride = cp_parser_expression (parser);
6488 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6490 if (*init_index == error_mark_node || length_index == error_mark_node
6491 || stride == error_mark_node || array_type == error_mark_node)
6493 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6494 cp_lexer_consume_token (parser->lexer);
6495 return error_mark_node;
6497 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6499 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6500 length_index, stride, array_type);
6501 return value_tree;
6504 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6505 by cp_parser_builtin_offsetof. We're looking for
6507 postfix-expression [ expression ]
6508 postfix-expression [ braced-init-list ] (C++11)
6510 FOR_OFFSETOF is set if we're being called in that context, which
6511 changes how we deal with integer constant expressions. */
6513 static tree
6514 cp_parser_postfix_open_square_expression (cp_parser *parser,
6515 tree postfix_expression,
6516 bool for_offsetof,
6517 bool decltype_p)
6519 tree index = NULL_TREE;
6520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6521 bool saved_greater_than_is_operator_p;
6523 /* Consume the `[' token. */
6524 cp_lexer_consume_token (parser->lexer);
6526 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6527 parser->greater_than_is_operator_p = true;
6529 /* Parse the index expression. */
6530 /* ??? For offsetof, there is a question of what to allow here. If
6531 offsetof is not being used in an integral constant expression context,
6532 then we *could* get the right answer by computing the value at runtime.
6533 If we are in an integral constant expression context, then we might
6534 could accept any constant expression; hard to say without analysis.
6535 Rather than open the barn door too wide right away, allow only integer
6536 constant expressions here. */
6537 if (for_offsetof)
6538 index = cp_parser_constant_expression (parser);
6539 else
6541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6543 bool expr_nonconst_p;
6544 cp_lexer_set_source_position (parser->lexer);
6545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6546 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6547 if (flag_cilkplus
6548 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6550 error_at (cp_lexer_peek_token (parser->lexer)->location,
6551 "braced list index is not allowed with array "
6552 "notation");
6553 cp_parser_skip_to_end_of_statement (parser);
6554 return error_mark_node;
6557 else if (flag_cilkplus)
6559 /* Here are have these two options:
6560 ARRAY[EXP : EXP] - Array notation expr with default
6561 stride of 1.
6562 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6563 stride. */
6564 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6565 postfix_expression);
6566 if (an_exp)
6567 return an_exp;
6569 else
6570 index = cp_parser_expression (parser);
6573 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6575 /* Look for the closing `]'. */
6576 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6578 /* Build the ARRAY_REF. */
6579 postfix_expression = grok_array_decl (loc, postfix_expression,
6580 index, decltype_p);
6582 /* When not doing offsetof, array references are not permitted in
6583 constant-expressions. */
6584 if (!for_offsetof
6585 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6586 postfix_expression = error_mark_node;
6588 return postfix_expression;
6591 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6592 by cp_parser_builtin_offsetof. We're looking for
6594 postfix-expression . template [opt] id-expression
6595 postfix-expression . pseudo-destructor-name
6596 postfix-expression -> template [opt] id-expression
6597 postfix-expression -> pseudo-destructor-name
6599 FOR_OFFSETOF is set if we're being called in that context. That sorta
6600 limits what of the above we'll actually accept, but nevermind.
6601 TOKEN_TYPE is the "." or "->" token, which will already have been
6602 removed from the stream. */
6604 static tree
6605 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6606 enum cpp_ttype token_type,
6607 tree postfix_expression,
6608 bool for_offsetof, cp_id_kind *idk,
6609 location_t location)
6611 tree name;
6612 bool dependent_p;
6613 bool pseudo_destructor_p;
6614 tree scope = NULL_TREE;
6616 /* If this is a `->' operator, dereference the pointer. */
6617 if (token_type == CPP_DEREF)
6618 postfix_expression = build_x_arrow (location, postfix_expression,
6619 tf_warning_or_error);
6620 /* Check to see whether or not the expression is type-dependent. */
6621 dependent_p = type_dependent_expression_p (postfix_expression);
6622 /* The identifier following the `->' or `.' is not qualified. */
6623 parser->scope = NULL_TREE;
6624 parser->qualifying_scope = NULL_TREE;
6625 parser->object_scope = NULL_TREE;
6626 *idk = CP_ID_KIND_NONE;
6628 /* Enter the scope corresponding to the type of the object
6629 given by the POSTFIX_EXPRESSION. */
6630 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6632 scope = TREE_TYPE (postfix_expression);
6633 /* According to the standard, no expression should ever have
6634 reference type. Unfortunately, we do not currently match
6635 the standard in this respect in that our internal representation
6636 of an expression may have reference type even when the standard
6637 says it does not. Therefore, we have to manually obtain the
6638 underlying type here. */
6639 scope = non_reference (scope);
6640 /* The type of the POSTFIX_EXPRESSION must be complete. */
6641 if (scope == unknown_type_node)
6643 error_at (location, "%qE does not have class type",
6644 postfix_expression);
6645 scope = NULL_TREE;
6647 /* Unlike the object expression in other contexts, *this is not
6648 required to be of complete type for purposes of class member
6649 access (5.2.5) outside the member function body. */
6650 else if (postfix_expression != current_class_ref
6651 && !(processing_template_decl && scope == current_class_type))
6652 scope = complete_type_or_else (scope, NULL_TREE);
6653 /* Let the name lookup machinery know that we are processing a
6654 class member access expression. */
6655 parser->context->object_type = scope;
6656 /* If something went wrong, we want to be able to discern that case,
6657 as opposed to the case where there was no SCOPE due to the type
6658 of expression being dependent. */
6659 if (!scope)
6660 scope = error_mark_node;
6661 /* If the SCOPE was erroneous, make the various semantic analysis
6662 functions exit quickly -- and without issuing additional error
6663 messages. */
6664 if (scope == error_mark_node)
6665 postfix_expression = error_mark_node;
6668 /* Assume this expression is not a pseudo-destructor access. */
6669 pseudo_destructor_p = false;
6671 /* If the SCOPE is a scalar type, then, if this is a valid program,
6672 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6673 is type dependent, it can be pseudo-destructor-name or something else.
6674 Try to parse it as pseudo-destructor-name first. */
6675 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6677 tree s;
6678 tree type;
6680 cp_parser_parse_tentatively (parser);
6681 /* Parse the pseudo-destructor-name. */
6682 s = NULL_TREE;
6683 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6684 &s, &type);
6685 if (dependent_p
6686 && (cp_parser_error_occurred (parser)
6687 || !SCALAR_TYPE_P (type)))
6688 cp_parser_abort_tentative_parse (parser);
6689 else if (cp_parser_parse_definitely (parser))
6691 pseudo_destructor_p = true;
6692 postfix_expression
6693 = finish_pseudo_destructor_expr (postfix_expression,
6694 s, type, location);
6698 if (!pseudo_destructor_p)
6700 /* If the SCOPE is not a scalar type, we are looking at an
6701 ordinary class member access expression, rather than a
6702 pseudo-destructor-name. */
6703 bool template_p;
6704 cp_token *token = cp_lexer_peek_token (parser->lexer);
6705 /* Parse the id-expression. */
6706 name = (cp_parser_id_expression
6707 (parser,
6708 cp_parser_optional_template_keyword (parser),
6709 /*check_dependency_p=*/true,
6710 &template_p,
6711 /*declarator_p=*/false,
6712 /*optional_p=*/false));
6713 /* In general, build a SCOPE_REF if the member name is qualified.
6714 However, if the name was not dependent and has already been
6715 resolved; there is no need to build the SCOPE_REF. For example;
6717 struct X { void f(); };
6718 template <typename T> void f(T* t) { t->X::f(); }
6720 Even though "t" is dependent, "X::f" is not and has been resolved
6721 to a BASELINK; there is no need to include scope information. */
6723 /* But we do need to remember that there was an explicit scope for
6724 virtual function calls. */
6725 if (parser->scope)
6726 *idk = CP_ID_KIND_QUALIFIED;
6728 /* If the name is a template-id that names a type, we will get a
6729 TYPE_DECL here. That is invalid code. */
6730 if (TREE_CODE (name) == TYPE_DECL)
6732 error_at (token->location, "invalid use of %qD", name);
6733 postfix_expression = error_mark_node;
6735 else
6737 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6739 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6741 error_at (token->location, "%<%D::%D%> is not a class member",
6742 parser->scope, name);
6743 postfix_expression = error_mark_node;
6745 else
6746 name = build_qualified_name (/*type=*/NULL_TREE,
6747 parser->scope,
6748 name,
6749 template_p);
6750 parser->scope = NULL_TREE;
6751 parser->qualifying_scope = NULL_TREE;
6752 parser->object_scope = NULL_TREE;
6754 if (parser->scope && name && BASELINK_P (name))
6755 adjust_result_of_qualified_name_lookup
6756 (name, parser->scope, scope);
6757 postfix_expression
6758 = finish_class_member_access_expr (postfix_expression, name,
6759 template_p,
6760 tf_warning_or_error);
6764 /* We no longer need to look up names in the scope of the object on
6765 the left-hand side of the `.' or `->' operator. */
6766 parser->context->object_type = NULL_TREE;
6768 /* Outside of offsetof, these operators may not appear in
6769 constant-expressions. */
6770 if (!for_offsetof
6771 && (cp_parser_non_integral_constant_expression
6772 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6773 postfix_expression = error_mark_node;
6775 return postfix_expression;
6778 /* Cache of LITERAL_ZERO_P constants. */
6780 static GTY(()) tree literal_zeros[itk_none];
6782 /* Parse a parenthesized expression-list.
6784 expression-list:
6785 assignment-expression
6786 expression-list, assignment-expression
6788 attribute-list:
6789 expression-list
6790 identifier
6791 identifier, expression-list
6793 CAST_P is true if this expression is the target of a cast.
6795 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6796 argument pack.
6798 Returns a vector of trees. Each element is a representation of an
6799 assignment-expression. NULL is returned if the ( and or ) are
6800 missing. An empty, but allocated, vector is returned on no
6801 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6802 if we are parsing an attribute list for an attribute that wants a
6803 plain identifier argument, normal_attr for an attribute that wants
6804 an expression, or non_attr if we aren't parsing an attribute list. If
6805 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6806 not all of the expressions in the list were constant.
6807 WANT_LITERAL_ZERO_P is true if the caller is interested in
6808 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6809 immediately, this can be removed. */
6811 static vec<tree, va_gc> *
6812 cp_parser_parenthesized_expression_list (cp_parser* parser,
6813 int is_attribute_list,
6814 bool cast_p,
6815 bool allow_expansion_p,
6816 bool *non_constant_p,
6817 bool want_literal_zero_p)
6819 vec<tree, va_gc> *expression_list;
6820 bool fold_expr_p = is_attribute_list != non_attr;
6821 tree identifier = NULL_TREE;
6822 bool saved_greater_than_is_operator_p;
6824 /* Assume all the expressions will be constant. */
6825 if (non_constant_p)
6826 *non_constant_p = false;
6828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6829 return NULL;
6831 expression_list = make_tree_vector ();
6833 /* Within a parenthesized expression, a `>' token is always
6834 the greater-than operator. */
6835 saved_greater_than_is_operator_p
6836 = parser->greater_than_is_operator_p;
6837 parser->greater_than_is_operator_p = true;
6839 /* Consume expressions until there are no more. */
6840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6841 while (true)
6843 tree expr;
6845 /* At the beginning of attribute lists, check to see if the
6846 next token is an identifier. */
6847 if (is_attribute_list == id_attr
6848 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6850 cp_token *token;
6852 /* Consume the identifier. */
6853 token = cp_lexer_consume_token (parser->lexer);
6854 /* Save the identifier. */
6855 identifier = token->u.value;
6857 else
6859 bool expr_non_constant_p;
6861 /* Parse the next assignment-expression. */
6862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6864 /* A braced-init-list. */
6865 cp_lexer_set_source_position (parser->lexer);
6866 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6867 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6868 if (non_constant_p && expr_non_constant_p)
6869 *non_constant_p = true;
6871 else if (non_constant_p)
6873 expr = (cp_parser_constant_expression
6874 (parser, /*allow_non_constant_p=*/true,
6875 &expr_non_constant_p));
6876 if (expr_non_constant_p)
6877 *non_constant_p = true;
6879 else
6881 expr = NULL_TREE;
6882 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6883 switch (tok->type)
6885 case CPP_NUMBER:
6886 case CPP_CHAR:
6887 case CPP_WCHAR:
6888 case CPP_CHAR16:
6889 case CPP_CHAR32:
6890 /* If a parameter is literal zero alone, remember it
6891 for -Wmemset-transposed-args warning. */
6892 if (integer_zerop (tok->u.value)
6893 && !TREE_OVERFLOW (tok->u.value)
6894 && want_literal_zero_p
6895 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6896 == CPP_COMMA
6897 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6898 == CPP_CLOSE_PAREN))
6900 unsigned int i;
6901 for (i = 0; i < itk_none; ++i)
6902 if (TREE_TYPE (tok->u.value) == integer_types[i])
6903 break;
6904 if (i < itk_none && literal_zeros[i])
6905 expr = literal_zeros[i];
6906 else
6908 expr = copy_node (tok->u.value);
6909 LITERAL_ZERO_P (expr) = 1;
6910 if (i < itk_none)
6911 literal_zeros[i] = expr;
6913 /* Consume the 0 token (or '\0', 0LL etc.). */
6914 cp_lexer_consume_token (parser->lexer);
6916 break;
6917 default:
6918 break;
6920 if (expr == NULL_TREE)
6921 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6922 cast_p);
6925 if (fold_expr_p)
6926 expr = instantiate_non_dependent_expr (expr);
6928 /* If we have an ellipsis, then this is an expression
6929 expansion. */
6930 if (allow_expansion_p
6931 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6933 /* Consume the `...'. */
6934 cp_lexer_consume_token (parser->lexer);
6936 /* Build the argument pack. */
6937 expr = make_pack_expansion (expr);
6940 /* Add it to the list. We add error_mark_node
6941 expressions to the list, so that we can still tell if
6942 the correct form for a parenthesized expression-list
6943 is found. That gives better errors. */
6944 vec_safe_push (expression_list, expr);
6946 if (expr == error_mark_node)
6947 goto skip_comma;
6950 /* After the first item, attribute lists look the same as
6951 expression lists. */
6952 is_attribute_list = non_attr;
6954 get_comma:;
6955 /* If the next token isn't a `,', then we are done. */
6956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6957 break;
6959 /* Otherwise, consume the `,' and keep going. */
6960 cp_lexer_consume_token (parser->lexer);
6963 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6965 int ending;
6967 skip_comma:;
6968 /* We try and resync to an unnested comma, as that will give the
6969 user better diagnostics. */
6970 ending = cp_parser_skip_to_closing_parenthesis (parser,
6971 /*recovering=*/true,
6972 /*or_comma=*/true,
6973 /*consume_paren=*/true);
6974 if (ending < 0)
6975 goto get_comma;
6976 if (!ending)
6978 parser->greater_than_is_operator_p
6979 = saved_greater_than_is_operator_p;
6980 return NULL;
6984 parser->greater_than_is_operator_p
6985 = saved_greater_than_is_operator_p;
6987 if (identifier)
6988 vec_safe_insert (expression_list, 0, identifier);
6990 return expression_list;
6993 /* Parse a pseudo-destructor-name.
6995 pseudo-destructor-name:
6996 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6997 :: [opt] nested-name-specifier template template-id :: ~ type-name
6998 :: [opt] nested-name-specifier [opt] ~ type-name
7000 If either of the first two productions is used, sets *SCOPE to the
7001 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7002 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7003 or ERROR_MARK_NODE if the parse fails. */
7005 static void
7006 cp_parser_pseudo_destructor_name (cp_parser* parser,
7007 tree object,
7008 tree* scope,
7009 tree* type)
7011 bool nested_name_specifier_p;
7013 /* Handle ~auto. */
7014 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7015 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7016 && !type_dependent_expression_p (object))
7018 if (cxx_dialect < cxx14)
7019 pedwarn (input_location, 0,
7020 "%<~auto%> only available with "
7021 "-std=c++14 or -std=gnu++14");
7022 cp_lexer_consume_token (parser->lexer);
7023 cp_lexer_consume_token (parser->lexer);
7024 *scope = NULL_TREE;
7025 *type = TREE_TYPE (object);
7026 return;
7029 /* Assume that things will not work out. */
7030 *type = error_mark_node;
7032 /* Look for the optional `::' operator. */
7033 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7034 /* Look for the optional nested-name-specifier. */
7035 nested_name_specifier_p
7036 = (cp_parser_nested_name_specifier_opt (parser,
7037 /*typename_keyword_p=*/false,
7038 /*check_dependency_p=*/true,
7039 /*type_p=*/false,
7040 /*is_declaration=*/false)
7041 != NULL_TREE);
7042 /* Now, if we saw a nested-name-specifier, we might be doing the
7043 second production. */
7044 if (nested_name_specifier_p
7045 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7047 /* Consume the `template' keyword. */
7048 cp_lexer_consume_token (parser->lexer);
7049 /* Parse the template-id. */
7050 cp_parser_template_id (parser,
7051 /*template_keyword_p=*/true,
7052 /*check_dependency_p=*/false,
7053 class_type,
7054 /*is_declaration=*/true);
7055 /* Look for the `::' token. */
7056 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7058 /* If the next token is not a `~', then there might be some
7059 additional qualification. */
7060 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7062 /* At this point, we're looking for "type-name :: ~". The type-name
7063 must not be a class-name, since this is a pseudo-destructor. So,
7064 it must be either an enum-name, or a typedef-name -- both of which
7065 are just identifiers. So, we peek ahead to check that the "::"
7066 and "~" tokens are present; if they are not, then we can avoid
7067 calling type_name. */
7068 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7069 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7070 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7072 cp_parser_error (parser, "non-scalar type");
7073 return;
7076 /* Look for the type-name. */
7077 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7078 if (*scope == error_mark_node)
7079 return;
7081 /* Look for the `::' token. */
7082 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7084 else
7085 *scope = NULL_TREE;
7087 /* Look for the `~'. */
7088 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7090 /* Once we see the ~, this has to be a pseudo-destructor. */
7091 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7092 cp_parser_commit_to_topmost_tentative_parse (parser);
7094 /* Look for the type-name again. We are not responsible for
7095 checking that it matches the first type-name. */
7096 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7099 /* Parse a unary-expression.
7101 unary-expression:
7102 postfix-expression
7103 ++ cast-expression
7104 -- cast-expression
7105 unary-operator cast-expression
7106 sizeof unary-expression
7107 sizeof ( type-id )
7108 alignof ( type-id ) [C++0x]
7109 new-expression
7110 delete-expression
7112 GNU Extensions:
7114 unary-expression:
7115 __extension__ cast-expression
7116 __alignof__ unary-expression
7117 __alignof__ ( type-id )
7118 alignof unary-expression [C++0x]
7119 __real__ cast-expression
7120 __imag__ cast-expression
7121 && identifier
7122 sizeof ( type-id ) { initializer-list , [opt] }
7123 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7124 __alignof__ ( type-id ) { initializer-list , [opt] }
7126 ADDRESS_P is true iff the unary-expression is appearing as the
7127 operand of the `&' operator. CAST_P is true if this expression is
7128 the target of a cast.
7130 Returns a representation of the expression. */
7132 static tree
7133 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7134 bool address_p, bool cast_p, bool decltype_p)
7136 cp_token *token;
7137 enum tree_code unary_operator;
7139 /* Peek at the next token. */
7140 token = cp_lexer_peek_token (parser->lexer);
7141 /* Some keywords give away the kind of expression. */
7142 if (token->type == CPP_KEYWORD)
7144 enum rid keyword = token->keyword;
7146 switch (keyword)
7148 case RID_ALIGNOF:
7149 case RID_SIZEOF:
7151 tree operand, ret;
7152 enum tree_code op;
7153 location_t first_loc;
7155 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7156 /* Consume the token. */
7157 cp_lexer_consume_token (parser->lexer);
7158 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7159 /* Parse the operand. */
7160 operand = cp_parser_sizeof_operand (parser, keyword);
7162 if (TYPE_P (operand))
7163 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7164 else
7166 /* ISO C++ defines alignof only with types, not with
7167 expressions. So pedwarn if alignof is used with a non-
7168 type expression. However, __alignof__ is ok. */
7169 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7170 pedwarn (token->location, OPT_Wpedantic,
7171 "ISO C++ does not allow %<alignof%> "
7172 "with a non-type");
7174 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7176 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7177 SIZEOF_EXPR with the original operand. */
7178 if (op == SIZEOF_EXPR && ret != error_mark_node)
7180 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7182 if (!processing_template_decl && TYPE_P (operand))
7184 ret = build_min (SIZEOF_EXPR, size_type_node,
7185 build1 (NOP_EXPR, operand,
7186 error_mark_node));
7187 SIZEOF_EXPR_TYPE_P (ret) = 1;
7189 else
7190 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7191 TREE_SIDE_EFFECTS (ret) = 0;
7192 TREE_READONLY (ret) = 1;
7194 SET_EXPR_LOCATION (ret, first_loc);
7196 return ret;
7199 case RID_NEW:
7200 return cp_parser_new_expression (parser);
7202 case RID_DELETE:
7203 return cp_parser_delete_expression (parser);
7205 case RID_EXTENSION:
7207 /* The saved value of the PEDANTIC flag. */
7208 int saved_pedantic;
7209 tree expr;
7211 /* Save away the PEDANTIC flag. */
7212 cp_parser_extension_opt (parser, &saved_pedantic);
7213 /* Parse the cast-expression. */
7214 expr = cp_parser_simple_cast_expression (parser);
7215 /* Restore the PEDANTIC flag. */
7216 pedantic = saved_pedantic;
7218 return expr;
7221 case RID_REALPART:
7222 case RID_IMAGPART:
7224 tree expression;
7226 /* Consume the `__real__' or `__imag__' token. */
7227 cp_lexer_consume_token (parser->lexer);
7228 /* Parse the cast-expression. */
7229 expression = cp_parser_simple_cast_expression (parser);
7230 /* Create the complete representation. */
7231 return build_x_unary_op (token->location,
7232 (keyword == RID_REALPART
7233 ? REALPART_EXPR : IMAGPART_EXPR),
7234 expression,
7235 tf_warning_or_error);
7237 break;
7239 case RID_TRANSACTION_ATOMIC:
7240 case RID_TRANSACTION_RELAXED:
7241 return cp_parser_transaction_expression (parser, keyword);
7243 case RID_NOEXCEPT:
7245 tree expr;
7246 const char *saved_message;
7247 bool saved_integral_constant_expression_p;
7248 bool saved_non_integral_constant_expression_p;
7249 bool saved_greater_than_is_operator_p;
7251 cp_lexer_consume_token (parser->lexer);
7252 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7254 saved_message = parser->type_definition_forbidden_message;
7255 parser->type_definition_forbidden_message
7256 = G_("types may not be defined in %<noexcept%> expressions");
7258 saved_integral_constant_expression_p
7259 = parser->integral_constant_expression_p;
7260 saved_non_integral_constant_expression_p
7261 = parser->non_integral_constant_expression_p;
7262 parser->integral_constant_expression_p = false;
7264 saved_greater_than_is_operator_p
7265 = parser->greater_than_is_operator_p;
7266 parser->greater_than_is_operator_p = true;
7268 ++cp_unevaluated_operand;
7269 ++c_inhibit_evaluation_warnings;
7270 ++cp_noexcept_operand;
7271 expr = cp_parser_expression (parser);
7272 --cp_noexcept_operand;
7273 --c_inhibit_evaluation_warnings;
7274 --cp_unevaluated_operand;
7276 parser->greater_than_is_operator_p
7277 = saved_greater_than_is_operator_p;
7279 parser->integral_constant_expression_p
7280 = saved_integral_constant_expression_p;
7281 parser->non_integral_constant_expression_p
7282 = saved_non_integral_constant_expression_p;
7284 parser->type_definition_forbidden_message = saved_message;
7286 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7287 return finish_noexcept_expr (expr, tf_warning_or_error);
7290 default:
7291 break;
7295 /* Look for the `:: new' and `:: delete', which also signal the
7296 beginning of a new-expression, or delete-expression,
7297 respectively. If the next token is `::', then it might be one of
7298 these. */
7299 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7301 enum rid keyword;
7303 /* See if the token after the `::' is one of the keywords in
7304 which we're interested. */
7305 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7306 /* If it's `new', we have a new-expression. */
7307 if (keyword == RID_NEW)
7308 return cp_parser_new_expression (parser);
7309 /* Similarly, for `delete'. */
7310 else if (keyword == RID_DELETE)
7311 return cp_parser_delete_expression (parser);
7314 /* Look for a unary operator. */
7315 unary_operator = cp_parser_unary_operator (token);
7316 /* The `++' and `--' operators can be handled similarly, even though
7317 they are not technically unary-operators in the grammar. */
7318 if (unary_operator == ERROR_MARK)
7320 if (token->type == CPP_PLUS_PLUS)
7321 unary_operator = PREINCREMENT_EXPR;
7322 else if (token->type == CPP_MINUS_MINUS)
7323 unary_operator = PREDECREMENT_EXPR;
7324 /* Handle the GNU address-of-label extension. */
7325 else if (cp_parser_allow_gnu_extensions_p (parser)
7326 && token->type == CPP_AND_AND)
7328 tree identifier;
7329 tree expression;
7330 location_t loc = token->location;
7332 /* Consume the '&&' token. */
7333 cp_lexer_consume_token (parser->lexer);
7334 /* Look for the identifier. */
7335 identifier = cp_parser_identifier (parser);
7336 /* Create an expression representing the address. */
7337 expression = finish_label_address_expr (identifier, loc);
7338 if (cp_parser_non_integral_constant_expression (parser,
7339 NIC_ADDR_LABEL))
7340 expression = error_mark_node;
7341 return expression;
7344 if (unary_operator != ERROR_MARK)
7346 tree cast_expression;
7347 tree expression = error_mark_node;
7348 non_integral_constant non_constant_p = NIC_NONE;
7349 location_t loc = token->location;
7350 tsubst_flags_t complain = complain_flags (decltype_p);
7352 /* Consume the operator token. */
7353 token = cp_lexer_consume_token (parser->lexer);
7354 /* Parse the cast-expression. */
7355 cast_expression
7356 = cp_parser_cast_expression (parser,
7357 unary_operator == ADDR_EXPR,
7358 /*cast_p=*/false,
7359 /*decltype*/false,
7360 pidk);
7361 /* Now, build an appropriate representation. */
7362 switch (unary_operator)
7364 case INDIRECT_REF:
7365 non_constant_p = NIC_STAR;
7366 expression = build_x_indirect_ref (loc, cast_expression,
7367 RO_UNARY_STAR,
7368 complain);
7369 break;
7371 case ADDR_EXPR:
7372 non_constant_p = NIC_ADDR;
7373 /* Fall through. */
7374 case BIT_NOT_EXPR:
7375 expression = build_x_unary_op (loc, unary_operator,
7376 cast_expression,
7377 complain);
7378 break;
7380 case PREINCREMENT_EXPR:
7381 case PREDECREMENT_EXPR:
7382 non_constant_p = unary_operator == PREINCREMENT_EXPR
7383 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7384 /* Fall through. */
7385 case UNARY_PLUS_EXPR:
7386 case NEGATE_EXPR:
7387 case TRUTH_NOT_EXPR:
7388 expression = finish_unary_op_expr (loc, unary_operator,
7389 cast_expression, complain);
7390 break;
7392 default:
7393 gcc_unreachable ();
7396 if (non_constant_p != NIC_NONE
7397 && cp_parser_non_integral_constant_expression (parser,
7398 non_constant_p))
7399 expression = error_mark_node;
7401 return expression;
7404 return cp_parser_postfix_expression (parser, address_p, cast_p,
7405 /*member_access_only_p=*/false,
7406 decltype_p,
7407 pidk);
7410 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7411 unary-operator, the corresponding tree code is returned. */
7413 static enum tree_code
7414 cp_parser_unary_operator (cp_token* token)
7416 switch (token->type)
7418 case CPP_MULT:
7419 return INDIRECT_REF;
7421 case CPP_AND:
7422 return ADDR_EXPR;
7424 case CPP_PLUS:
7425 return UNARY_PLUS_EXPR;
7427 case CPP_MINUS:
7428 return NEGATE_EXPR;
7430 case CPP_NOT:
7431 return TRUTH_NOT_EXPR;
7433 case CPP_COMPL:
7434 return BIT_NOT_EXPR;
7436 default:
7437 return ERROR_MARK;
7441 /* Parse a new-expression.
7443 new-expression:
7444 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7445 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7447 Returns a representation of the expression. */
7449 static tree
7450 cp_parser_new_expression (cp_parser* parser)
7452 bool global_scope_p;
7453 vec<tree, va_gc> *placement;
7454 tree type;
7455 vec<tree, va_gc> *initializer;
7456 tree nelts = NULL_TREE;
7457 tree ret;
7459 /* Look for the optional `::' operator. */
7460 global_scope_p
7461 = (cp_parser_global_scope_opt (parser,
7462 /*current_scope_valid_p=*/false)
7463 != NULL_TREE);
7464 /* Look for the `new' operator. */
7465 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7466 /* There's no easy way to tell a new-placement from the
7467 `( type-id )' construct. */
7468 cp_parser_parse_tentatively (parser);
7469 /* Look for a new-placement. */
7470 placement = cp_parser_new_placement (parser);
7471 /* If that didn't work out, there's no new-placement. */
7472 if (!cp_parser_parse_definitely (parser))
7474 if (placement != NULL)
7475 release_tree_vector (placement);
7476 placement = NULL;
7479 /* If the next token is a `(', then we have a parenthesized
7480 type-id. */
7481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7483 cp_token *token;
7484 const char *saved_message = parser->type_definition_forbidden_message;
7486 /* Consume the `('. */
7487 cp_lexer_consume_token (parser->lexer);
7489 /* Parse the type-id. */
7490 parser->type_definition_forbidden_message
7491 = G_("types may not be defined in a new-expression");
7492 type = cp_parser_type_id (parser);
7493 parser->type_definition_forbidden_message = saved_message;
7495 /* Look for the closing `)'. */
7496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7497 token = cp_lexer_peek_token (parser->lexer);
7498 /* There should not be a direct-new-declarator in this production,
7499 but GCC used to allowed this, so we check and emit a sensible error
7500 message for this case. */
7501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7503 error_at (token->location,
7504 "array bound forbidden after parenthesized type-id");
7505 inform (token->location,
7506 "try removing the parentheses around the type-id");
7507 cp_parser_direct_new_declarator (parser);
7510 /* Otherwise, there must be a new-type-id. */
7511 else
7512 type = cp_parser_new_type_id (parser, &nelts);
7514 /* If the next token is a `(' or '{', then we have a new-initializer. */
7515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7516 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7517 initializer = cp_parser_new_initializer (parser);
7518 else
7519 initializer = NULL;
7521 /* A new-expression may not appear in an integral constant
7522 expression. */
7523 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7524 ret = error_mark_node;
7525 else
7527 /* Create a representation of the new-expression. */
7528 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7529 tf_warning_or_error);
7532 if (placement != NULL)
7533 release_tree_vector (placement);
7534 if (initializer != NULL)
7535 release_tree_vector (initializer);
7537 return ret;
7540 /* Parse a new-placement.
7542 new-placement:
7543 ( expression-list )
7545 Returns the same representation as for an expression-list. */
7547 static vec<tree, va_gc> *
7548 cp_parser_new_placement (cp_parser* parser)
7550 vec<tree, va_gc> *expression_list;
7552 /* Parse the expression-list. */
7553 expression_list = (cp_parser_parenthesized_expression_list
7554 (parser, non_attr, /*cast_p=*/false,
7555 /*allow_expansion_p=*/true,
7556 /*non_constant_p=*/NULL));
7558 if (expression_list && expression_list->is_empty ())
7559 error ("expected expression-list or type-id");
7561 return expression_list;
7564 /* Parse a new-type-id.
7566 new-type-id:
7567 type-specifier-seq new-declarator [opt]
7569 Returns the TYPE allocated. If the new-type-id indicates an array
7570 type, *NELTS is set to the number of elements in the last array
7571 bound; the TYPE will not include the last array bound. */
7573 static tree
7574 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7576 cp_decl_specifier_seq type_specifier_seq;
7577 cp_declarator *new_declarator;
7578 cp_declarator *declarator;
7579 cp_declarator *outer_declarator;
7580 const char *saved_message;
7582 /* The type-specifier sequence must not contain type definitions.
7583 (It cannot contain declarations of new types either, but if they
7584 are not definitions we will catch that because they are not
7585 complete.) */
7586 saved_message = parser->type_definition_forbidden_message;
7587 parser->type_definition_forbidden_message
7588 = G_("types may not be defined in a new-type-id");
7589 /* Parse the type-specifier-seq. */
7590 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7591 /*is_trailing_return=*/false,
7592 &type_specifier_seq);
7593 /* Restore the old message. */
7594 parser->type_definition_forbidden_message = saved_message;
7596 if (type_specifier_seq.type == error_mark_node)
7597 return error_mark_node;
7599 /* Parse the new-declarator. */
7600 new_declarator = cp_parser_new_declarator_opt (parser);
7602 /* Determine the number of elements in the last array dimension, if
7603 any. */
7604 *nelts = NULL_TREE;
7605 /* Skip down to the last array dimension. */
7606 declarator = new_declarator;
7607 outer_declarator = NULL;
7608 while (declarator && (declarator->kind == cdk_pointer
7609 || declarator->kind == cdk_ptrmem))
7611 outer_declarator = declarator;
7612 declarator = declarator->declarator;
7614 while (declarator
7615 && declarator->kind == cdk_array
7616 && declarator->declarator
7617 && declarator->declarator->kind == cdk_array)
7619 outer_declarator = declarator;
7620 declarator = declarator->declarator;
7623 if (declarator && declarator->kind == cdk_array)
7625 *nelts = declarator->u.array.bounds;
7626 if (*nelts == error_mark_node)
7627 *nelts = integer_one_node;
7629 if (outer_declarator)
7630 outer_declarator->declarator = declarator->declarator;
7631 else
7632 new_declarator = NULL;
7635 return groktypename (&type_specifier_seq, new_declarator, false);
7638 /* Parse an (optional) new-declarator.
7640 new-declarator:
7641 ptr-operator new-declarator [opt]
7642 direct-new-declarator
7644 Returns the declarator. */
7646 static cp_declarator *
7647 cp_parser_new_declarator_opt (cp_parser* parser)
7649 enum tree_code code;
7650 tree type, std_attributes = NULL_TREE;
7651 cp_cv_quals cv_quals;
7653 /* We don't know if there's a ptr-operator next, or not. */
7654 cp_parser_parse_tentatively (parser);
7655 /* Look for a ptr-operator. */
7656 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7657 /* If that worked, look for more new-declarators. */
7658 if (cp_parser_parse_definitely (parser))
7660 cp_declarator *declarator;
7662 /* Parse another optional declarator. */
7663 declarator = cp_parser_new_declarator_opt (parser);
7665 declarator = cp_parser_make_indirect_declarator
7666 (code, type, cv_quals, declarator, std_attributes);
7668 return declarator;
7671 /* If the next token is a `[', there is a direct-new-declarator. */
7672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7673 return cp_parser_direct_new_declarator (parser);
7675 return NULL;
7678 /* Parse a direct-new-declarator.
7680 direct-new-declarator:
7681 [ expression ]
7682 direct-new-declarator [constant-expression]
7686 static cp_declarator *
7687 cp_parser_direct_new_declarator (cp_parser* parser)
7689 cp_declarator *declarator = NULL;
7691 while (true)
7693 tree expression;
7694 cp_token *token;
7696 /* Look for the opening `['. */
7697 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7699 token = cp_lexer_peek_token (parser->lexer);
7700 expression = cp_parser_expression (parser);
7701 /* The standard requires that the expression have integral
7702 type. DR 74 adds enumeration types. We believe that the
7703 real intent is that these expressions be handled like the
7704 expression in a `switch' condition, which also allows
7705 classes with a single conversion to integral or
7706 enumeration type. */
7707 if (!processing_template_decl)
7709 expression
7710 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7711 expression,
7712 /*complain=*/true);
7713 if (!expression)
7715 error_at (token->location,
7716 "expression in new-declarator must have integral "
7717 "or enumeration type");
7718 expression = error_mark_node;
7722 /* Look for the closing `]'. */
7723 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7725 /* Add this bound to the declarator. */
7726 declarator = make_array_declarator (declarator, expression);
7728 /* If the next token is not a `[', then there are no more
7729 bounds. */
7730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7731 break;
7734 return declarator;
7737 /* Parse a new-initializer.
7739 new-initializer:
7740 ( expression-list [opt] )
7741 braced-init-list
7743 Returns a representation of the expression-list. */
7745 static vec<tree, va_gc> *
7746 cp_parser_new_initializer (cp_parser* parser)
7748 vec<tree, va_gc> *expression_list;
7750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7752 tree t;
7753 bool expr_non_constant_p;
7754 cp_lexer_set_source_position (parser->lexer);
7755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7756 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7757 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7758 expression_list = make_tree_vector_single (t);
7760 else
7761 expression_list = (cp_parser_parenthesized_expression_list
7762 (parser, non_attr, /*cast_p=*/false,
7763 /*allow_expansion_p=*/true,
7764 /*non_constant_p=*/NULL));
7766 return expression_list;
7769 /* Parse a delete-expression.
7771 delete-expression:
7772 :: [opt] delete cast-expression
7773 :: [opt] delete [ ] cast-expression
7775 Returns a representation of the expression. */
7777 static tree
7778 cp_parser_delete_expression (cp_parser* parser)
7780 bool global_scope_p;
7781 bool array_p;
7782 tree expression;
7784 /* Look for the optional `::' operator. */
7785 global_scope_p
7786 = (cp_parser_global_scope_opt (parser,
7787 /*current_scope_valid_p=*/false)
7788 != NULL_TREE);
7789 /* Look for the `delete' keyword. */
7790 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7791 /* See if the array syntax is in use. */
7792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7794 /* Consume the `[' token. */
7795 cp_lexer_consume_token (parser->lexer);
7796 /* Look for the `]' token. */
7797 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7798 /* Remember that this is the `[]' construct. */
7799 array_p = true;
7801 else
7802 array_p = false;
7804 /* Parse the cast-expression. */
7805 expression = cp_parser_simple_cast_expression (parser);
7807 /* A delete-expression may not appear in an integral constant
7808 expression. */
7809 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7810 return error_mark_node;
7812 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7813 tf_warning_or_error);
7816 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7817 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7818 0 otherwise. */
7820 static int
7821 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7823 cp_token *token = cp_lexer_peek_token (parser->lexer);
7824 switch (token->type)
7826 case CPP_COMMA:
7827 case CPP_SEMICOLON:
7828 case CPP_QUERY:
7829 case CPP_COLON:
7830 case CPP_CLOSE_SQUARE:
7831 case CPP_CLOSE_PAREN:
7832 case CPP_CLOSE_BRACE:
7833 case CPP_OPEN_BRACE:
7834 case CPP_DOT:
7835 case CPP_DOT_STAR:
7836 case CPP_DEREF:
7837 case CPP_DEREF_STAR:
7838 case CPP_DIV:
7839 case CPP_MOD:
7840 case CPP_LSHIFT:
7841 case CPP_RSHIFT:
7842 case CPP_LESS:
7843 case CPP_GREATER:
7844 case CPP_LESS_EQ:
7845 case CPP_GREATER_EQ:
7846 case CPP_EQ_EQ:
7847 case CPP_NOT_EQ:
7848 case CPP_EQ:
7849 case CPP_MULT_EQ:
7850 case CPP_DIV_EQ:
7851 case CPP_MOD_EQ:
7852 case CPP_PLUS_EQ:
7853 case CPP_MINUS_EQ:
7854 case CPP_RSHIFT_EQ:
7855 case CPP_LSHIFT_EQ:
7856 case CPP_AND_EQ:
7857 case CPP_XOR_EQ:
7858 case CPP_OR_EQ:
7859 case CPP_XOR:
7860 case CPP_OR:
7861 case CPP_OR_OR:
7862 case CPP_EOF:
7863 case CPP_ELLIPSIS:
7864 return 0;
7866 case CPP_OPEN_PAREN:
7867 /* In ((type ()) () the last () isn't a valid cast-expression,
7868 so the whole must be parsed as postfix-expression. */
7869 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7870 != CPP_CLOSE_PAREN;
7872 case CPP_OPEN_SQUARE:
7873 /* '[' may start a primary-expression in obj-c++ and in C++11,
7874 as a lambda-expression, eg, '(void)[]{}'. */
7875 if (cxx_dialect >= cxx11)
7876 return -1;
7877 return c_dialect_objc ();
7879 case CPP_PLUS_PLUS:
7880 case CPP_MINUS_MINUS:
7881 /* '++' and '--' may or may not start a cast-expression:
7883 struct T { void operator++(int); };
7884 void f() { (T())++; }
7888 int a;
7889 (int)++a; */
7890 return -1;
7892 default:
7893 return 1;
7897 /* Parse a cast-expression.
7899 cast-expression:
7900 unary-expression
7901 ( type-id ) cast-expression
7903 ADDRESS_P is true iff the unary-expression is appearing as the
7904 operand of the `&' operator. CAST_P is true if this expression is
7905 the target of a cast.
7907 Returns a representation of the expression. */
7909 static tree
7910 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7911 bool decltype_p, cp_id_kind * pidk)
7913 /* If it's a `(', then we might be looking at a cast. */
7914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7916 tree type = NULL_TREE;
7917 tree expr = NULL_TREE;
7918 int cast_expression = 0;
7919 const char *saved_message;
7921 /* There's no way to know yet whether or not this is a cast.
7922 For example, `(int (3))' is a unary-expression, while `(int)
7923 3' is a cast. So, we resort to parsing tentatively. */
7924 cp_parser_parse_tentatively (parser);
7925 /* Types may not be defined in a cast. */
7926 saved_message = parser->type_definition_forbidden_message;
7927 parser->type_definition_forbidden_message
7928 = G_("types may not be defined in casts");
7929 /* Consume the `('. */
7930 cp_lexer_consume_token (parser->lexer);
7931 /* A very tricky bit is that `(struct S) { 3 }' is a
7932 compound-literal (which we permit in C++ as an extension).
7933 But, that construct is not a cast-expression -- it is a
7934 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7935 is legal; if the compound-literal were a cast-expression,
7936 you'd need an extra set of parentheses.) But, if we parse
7937 the type-id, and it happens to be a class-specifier, then we
7938 will commit to the parse at that point, because we cannot
7939 undo the action that is done when creating a new class. So,
7940 then we cannot back up and do a postfix-expression.
7942 Another tricky case is the following (c++/29234):
7944 struct S { void operator () (); };
7946 void foo ()
7948 ( S()() );
7951 As a type-id we parse the parenthesized S()() as a function
7952 returning a function, groktypename complains and we cannot
7953 back up in this case either.
7955 Therefore, we scan ahead to the closing `)', and check to see
7956 if the tokens after the `)' can start a cast-expression. Otherwise
7957 we are dealing with an unary-expression, a postfix-expression
7958 or something else.
7960 Yet another tricky case, in C++11, is the following (c++/54891):
7962 (void)[]{};
7964 The issue is that usually, besides the case of lambda-expressions,
7965 the parenthesized type-id cannot be followed by '[', and, eg, we
7966 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7967 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7968 we don't commit, we try a cast-expression, then an unary-expression.
7970 Save tokens so that we can put them back. */
7971 cp_lexer_save_tokens (parser->lexer);
7973 /* We may be looking at a cast-expression. */
7974 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7975 /*consume_paren=*/true))
7976 cast_expression
7977 = cp_parser_tokens_start_cast_expression (parser);
7979 /* Roll back the tokens we skipped. */
7980 cp_lexer_rollback_tokens (parser->lexer);
7981 /* If we aren't looking at a cast-expression, simulate an error so
7982 that the call to cp_parser_error_occurred below returns true. */
7983 if (!cast_expression)
7984 cp_parser_simulate_error (parser);
7985 else
7987 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7988 parser->in_type_id_in_expr_p = true;
7989 /* Look for the type-id. */
7990 type = cp_parser_type_id (parser);
7991 /* Look for the closing `)'. */
7992 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7993 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7996 /* Restore the saved message. */
7997 parser->type_definition_forbidden_message = saved_message;
7999 /* At this point this can only be either a cast or a
8000 parenthesized ctor such as `(T ())' that looks like a cast to
8001 function returning T. */
8002 if (!cp_parser_error_occurred (parser))
8004 /* Only commit if the cast-expression doesn't start with
8005 '++', '--', or '[' in C++11. */
8006 if (cast_expression > 0)
8007 cp_parser_commit_to_topmost_tentative_parse (parser);
8009 expr = cp_parser_cast_expression (parser,
8010 /*address_p=*/false,
8011 /*cast_p=*/true,
8012 /*decltype_p=*/false,
8013 pidk);
8015 if (cp_parser_parse_definitely (parser))
8017 /* Warn about old-style casts, if so requested. */
8018 if (warn_old_style_cast
8019 && !in_system_header_at (input_location)
8020 && !VOID_TYPE_P (type)
8021 && current_lang_name != lang_name_c)
8022 warning (OPT_Wold_style_cast, "use of old-style cast");
8024 /* Only type conversions to integral or enumeration types
8025 can be used in constant-expressions. */
8026 if (!cast_valid_in_integral_constant_expression_p (type)
8027 && cp_parser_non_integral_constant_expression (parser,
8028 NIC_CAST))
8029 return error_mark_node;
8031 /* Perform the cast. */
8032 expr = build_c_cast (input_location, type, expr);
8033 return expr;
8036 else
8037 cp_parser_abort_tentative_parse (parser);
8040 /* If we get here, then it's not a cast, so it must be a
8041 unary-expression. */
8042 return cp_parser_unary_expression (parser, pidk, address_p,
8043 cast_p, decltype_p);
8046 /* Parse a binary expression of the general form:
8048 pm-expression:
8049 cast-expression
8050 pm-expression .* cast-expression
8051 pm-expression ->* cast-expression
8053 multiplicative-expression:
8054 pm-expression
8055 multiplicative-expression * pm-expression
8056 multiplicative-expression / pm-expression
8057 multiplicative-expression % pm-expression
8059 additive-expression:
8060 multiplicative-expression
8061 additive-expression + multiplicative-expression
8062 additive-expression - multiplicative-expression
8064 shift-expression:
8065 additive-expression
8066 shift-expression << additive-expression
8067 shift-expression >> additive-expression
8069 relational-expression:
8070 shift-expression
8071 relational-expression < shift-expression
8072 relational-expression > shift-expression
8073 relational-expression <= shift-expression
8074 relational-expression >= shift-expression
8076 GNU Extension:
8078 relational-expression:
8079 relational-expression <? shift-expression
8080 relational-expression >? shift-expression
8082 equality-expression:
8083 relational-expression
8084 equality-expression == relational-expression
8085 equality-expression != relational-expression
8087 and-expression:
8088 equality-expression
8089 and-expression & equality-expression
8091 exclusive-or-expression:
8092 and-expression
8093 exclusive-or-expression ^ and-expression
8095 inclusive-or-expression:
8096 exclusive-or-expression
8097 inclusive-or-expression | exclusive-or-expression
8099 logical-and-expression:
8100 inclusive-or-expression
8101 logical-and-expression && inclusive-or-expression
8103 logical-or-expression:
8104 logical-and-expression
8105 logical-or-expression || logical-and-expression
8107 All these are implemented with a single function like:
8109 binary-expression:
8110 simple-cast-expression
8111 binary-expression <token> binary-expression
8113 CAST_P is true if this expression is the target of a cast.
8115 The binops_by_token map is used to get the tree codes for each <token> type.
8116 binary-expressions are associated according to a precedence table. */
8118 #define TOKEN_PRECEDENCE(token) \
8119 (((token->type == CPP_GREATER \
8120 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8121 && !parser->greater_than_is_operator_p) \
8122 ? PREC_NOT_OPERATOR \
8123 : binops_by_token[token->type].prec)
8125 static tree
8126 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8127 bool no_toplevel_fold_p,
8128 bool decltype_p,
8129 enum cp_parser_prec prec,
8130 cp_id_kind * pidk)
8132 cp_parser_expression_stack stack;
8133 cp_parser_expression_stack_entry *sp = &stack[0];
8134 cp_parser_expression_stack_entry current;
8135 tree rhs;
8136 cp_token *token;
8137 enum tree_code rhs_type;
8138 enum cp_parser_prec new_prec, lookahead_prec;
8139 tree overload;
8141 /* Parse the first expression. */
8142 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8143 ? TRUTH_NOT_EXPR : ERROR_MARK);
8144 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8145 cast_p, decltype_p, pidk);
8146 current.prec = prec;
8148 if (cp_parser_error_occurred (parser))
8149 return error_mark_node;
8151 for (;;)
8153 /* Get an operator token. */
8154 token = cp_lexer_peek_token (parser->lexer);
8156 if (warn_cxx11_compat
8157 && token->type == CPP_RSHIFT
8158 && !parser->greater_than_is_operator_p)
8160 if (warning_at (token->location, OPT_Wc__11_compat,
8161 "%<>>%> operator is treated"
8162 " as two right angle brackets in C++11"))
8163 inform (token->location,
8164 "suggest parentheses around %<>>%> expression");
8167 new_prec = TOKEN_PRECEDENCE (token);
8169 /* Popping an entry off the stack means we completed a subexpression:
8170 - either we found a token which is not an operator (`>' where it is not
8171 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8172 will happen repeatedly;
8173 - or, we found an operator which has lower priority. This is the case
8174 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8175 parsing `3 * 4'. */
8176 if (new_prec <= current.prec)
8178 if (sp == stack)
8179 break;
8180 else
8181 goto pop;
8184 get_rhs:
8185 current.tree_type = binops_by_token[token->type].tree_type;
8186 current.loc = token->location;
8188 /* We used the operator token. */
8189 cp_lexer_consume_token (parser->lexer);
8191 /* For "false && x" or "true || x", x will never be executed;
8192 disable warnings while evaluating it. */
8193 if (current.tree_type == TRUTH_ANDIF_EXPR)
8194 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8195 else if (current.tree_type == TRUTH_ORIF_EXPR)
8196 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8198 /* Extract another operand. It may be the RHS of this expression
8199 or the LHS of a new, higher priority expression. */
8200 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8201 ? TRUTH_NOT_EXPR : ERROR_MARK);
8202 rhs = cp_parser_simple_cast_expression (parser);
8204 /* Get another operator token. Look up its precedence to avoid
8205 building a useless (immediately popped) stack entry for common
8206 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8207 token = cp_lexer_peek_token (parser->lexer);
8208 lookahead_prec = TOKEN_PRECEDENCE (token);
8209 if (lookahead_prec > new_prec)
8211 /* ... and prepare to parse the RHS of the new, higher priority
8212 expression. Since precedence levels on the stack are
8213 monotonically increasing, we do not have to care about
8214 stack overflows. */
8215 *sp = current;
8216 ++sp;
8217 current.lhs = rhs;
8218 current.lhs_type = rhs_type;
8219 current.prec = new_prec;
8220 new_prec = lookahead_prec;
8221 goto get_rhs;
8223 pop:
8224 lookahead_prec = new_prec;
8225 /* If the stack is not empty, we have parsed into LHS the right side
8226 (`4' in the example above) of an expression we had suspended.
8227 We can use the information on the stack to recover the LHS (`3')
8228 from the stack together with the tree code (`MULT_EXPR'), and
8229 the precedence of the higher level subexpression
8230 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8231 which will be used to actually build the additive expression. */
8232 rhs = current.lhs;
8233 rhs_type = current.lhs_type;
8234 --sp;
8235 current = *sp;
8238 /* Undo the disabling of warnings done above. */
8239 if (current.tree_type == TRUTH_ANDIF_EXPR)
8240 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8241 else if (current.tree_type == TRUTH_ORIF_EXPR)
8242 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8244 if (warn_logical_not_paren
8245 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8246 && current.lhs_type == TRUTH_NOT_EXPR
8247 /* Avoid warning for !!x == y. */
8248 && (TREE_CODE (current.lhs) != NE_EXPR
8249 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8250 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8251 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8252 /* Avoid warning for !b == y where b is boolean. */
8253 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8254 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8255 != BOOLEAN_TYPE))))
8256 /* Avoid warning for !!b == y where b is boolean. */
8257 && (!DECL_P (current.lhs)
8258 || TREE_TYPE (current.lhs) == NULL_TREE
8259 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8260 warn_logical_not_parentheses (current.loc, current.tree_type,
8261 maybe_constant_value (rhs));
8263 overload = NULL;
8264 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8265 ERROR_MARK for everything that is not a binary expression.
8266 This makes warn_about_parentheses miss some warnings that
8267 involve unary operators. For unary expressions we should
8268 pass the correct tree_code unless the unary expression was
8269 surrounded by parentheses.
8271 if (no_toplevel_fold_p
8272 && lookahead_prec <= current.prec
8273 && sp == stack)
8274 current.lhs = build2 (current.tree_type,
8275 TREE_CODE_CLASS (current.tree_type)
8276 == tcc_comparison
8277 ? boolean_type_node : TREE_TYPE (current.lhs),
8278 current.lhs, rhs);
8279 else
8280 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8281 current.lhs, current.lhs_type,
8282 rhs, rhs_type, &overload,
8283 complain_flags (decltype_p));
8284 current.lhs_type = current.tree_type;
8285 if (EXPR_P (current.lhs))
8286 SET_EXPR_LOCATION (current.lhs, current.loc);
8288 /* If the binary operator required the use of an overloaded operator,
8289 then this expression cannot be an integral constant-expression.
8290 An overloaded operator can be used even if both operands are
8291 otherwise permissible in an integral constant-expression if at
8292 least one of the operands is of enumeration type. */
8294 if (overload
8295 && cp_parser_non_integral_constant_expression (parser,
8296 NIC_OVERLOADED))
8297 return error_mark_node;
8300 return current.lhs;
8303 static tree
8304 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8305 bool no_toplevel_fold_p,
8306 enum cp_parser_prec prec,
8307 cp_id_kind * pidk)
8309 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8310 /*decltype*/false, prec, pidk);
8313 /* Parse the `? expression : assignment-expression' part of a
8314 conditional-expression. The LOGICAL_OR_EXPR is the
8315 logical-or-expression that started the conditional-expression.
8316 Returns a representation of the entire conditional-expression.
8318 This routine is used by cp_parser_assignment_expression.
8320 ? expression : assignment-expression
8322 GNU Extensions:
8324 ? : assignment-expression */
8326 static tree
8327 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8329 tree expr;
8330 tree assignment_expr;
8331 struct cp_token *token;
8332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8334 /* Consume the `?' token. */
8335 cp_lexer_consume_token (parser->lexer);
8336 token = cp_lexer_peek_token (parser->lexer);
8337 if (cp_parser_allow_gnu_extensions_p (parser)
8338 && token->type == CPP_COLON)
8340 pedwarn (token->location, OPT_Wpedantic,
8341 "ISO C++ does not allow ?: with omitted middle operand");
8342 /* Implicit true clause. */
8343 expr = NULL_TREE;
8344 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8345 warn_for_omitted_condop (token->location, logical_or_expr);
8347 else
8349 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8350 parser->colon_corrects_to_scope_p = false;
8351 /* Parse the expression. */
8352 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8353 expr = cp_parser_expression (parser);
8354 c_inhibit_evaluation_warnings +=
8355 ((logical_or_expr == truthvalue_true_node)
8356 - (logical_or_expr == truthvalue_false_node));
8357 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8360 /* The next token should be a `:'. */
8361 cp_parser_require (parser, CPP_COLON, RT_COLON);
8362 /* Parse the assignment-expression. */
8363 assignment_expr = cp_parser_assignment_expression (parser);
8364 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8366 /* Build the conditional-expression. */
8367 return build_x_conditional_expr (loc, logical_or_expr,
8368 expr,
8369 assignment_expr,
8370 tf_warning_or_error);
8373 /* Parse an assignment-expression.
8375 assignment-expression:
8376 conditional-expression
8377 logical-or-expression assignment-operator assignment_expression
8378 throw-expression
8380 CAST_P is true if this expression is the target of a cast.
8381 DECLTYPE_P is true if this expression is the operand of decltype.
8383 Returns a representation for the expression. */
8385 static tree
8386 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8387 bool cast_p, bool decltype_p)
8389 tree expr;
8391 /* If the next token is the `throw' keyword, then we're looking at
8392 a throw-expression. */
8393 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8394 expr = cp_parser_throw_expression (parser);
8395 /* Otherwise, it must be that we are looking at a
8396 logical-or-expression. */
8397 else
8399 /* Parse the binary expressions (logical-or-expression). */
8400 expr = cp_parser_binary_expression (parser, cast_p, false,
8401 decltype_p,
8402 PREC_NOT_OPERATOR, pidk);
8403 /* If the next token is a `?' then we're actually looking at a
8404 conditional-expression. */
8405 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8406 return cp_parser_question_colon_clause (parser, expr);
8407 else
8409 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8411 /* If it's an assignment-operator, we're using the second
8412 production. */
8413 enum tree_code assignment_operator
8414 = cp_parser_assignment_operator_opt (parser);
8415 if (assignment_operator != ERROR_MARK)
8417 bool non_constant_p;
8418 location_t saved_input_location;
8420 /* Parse the right-hand side of the assignment. */
8421 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8423 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8424 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8426 /* An assignment may not appear in a
8427 constant-expression. */
8428 if (cp_parser_non_integral_constant_expression (parser,
8429 NIC_ASSIGNMENT))
8430 return error_mark_node;
8431 /* Build the assignment expression. Its default
8432 location is the location of the '=' token. */
8433 saved_input_location = input_location;
8434 input_location = loc;
8435 expr = build_x_modify_expr (loc, expr,
8436 assignment_operator,
8437 rhs,
8438 complain_flags (decltype_p));
8439 input_location = saved_input_location;
8444 return expr;
8447 /* Parse an (optional) assignment-operator.
8449 assignment-operator: one of
8450 = *= /= %= += -= >>= <<= &= ^= |=
8452 GNU Extension:
8454 assignment-operator: one of
8455 <?= >?=
8457 If the next token is an assignment operator, the corresponding tree
8458 code is returned, and the token is consumed. For example, for
8459 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8460 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8461 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8462 operator, ERROR_MARK is returned. */
8464 static enum tree_code
8465 cp_parser_assignment_operator_opt (cp_parser* parser)
8467 enum tree_code op;
8468 cp_token *token;
8470 /* Peek at the next token. */
8471 token = cp_lexer_peek_token (parser->lexer);
8473 switch (token->type)
8475 case CPP_EQ:
8476 op = NOP_EXPR;
8477 break;
8479 case CPP_MULT_EQ:
8480 op = MULT_EXPR;
8481 break;
8483 case CPP_DIV_EQ:
8484 op = TRUNC_DIV_EXPR;
8485 break;
8487 case CPP_MOD_EQ:
8488 op = TRUNC_MOD_EXPR;
8489 break;
8491 case CPP_PLUS_EQ:
8492 op = PLUS_EXPR;
8493 break;
8495 case CPP_MINUS_EQ:
8496 op = MINUS_EXPR;
8497 break;
8499 case CPP_RSHIFT_EQ:
8500 op = RSHIFT_EXPR;
8501 break;
8503 case CPP_LSHIFT_EQ:
8504 op = LSHIFT_EXPR;
8505 break;
8507 case CPP_AND_EQ:
8508 op = BIT_AND_EXPR;
8509 break;
8511 case CPP_XOR_EQ:
8512 op = BIT_XOR_EXPR;
8513 break;
8515 case CPP_OR_EQ:
8516 op = BIT_IOR_EXPR;
8517 break;
8519 default:
8520 /* Nothing else is an assignment operator. */
8521 op = ERROR_MARK;
8524 /* If it was an assignment operator, consume it. */
8525 if (op != ERROR_MARK)
8526 cp_lexer_consume_token (parser->lexer);
8528 return op;
8531 /* Parse an expression.
8533 expression:
8534 assignment-expression
8535 expression , assignment-expression
8537 CAST_P is true if this expression is the target of a cast.
8538 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8539 except possibly parenthesized or on the RHS of a comma (N3276).
8541 Returns a representation of the expression. */
8543 static tree
8544 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8545 bool cast_p, bool decltype_p)
8547 tree expression = NULL_TREE;
8548 location_t loc = UNKNOWN_LOCATION;
8550 while (true)
8552 tree assignment_expression;
8554 /* Parse the next assignment-expression. */
8555 assignment_expression
8556 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8558 /* We don't create a temporary for a call that is the immediate operand
8559 of decltype or on the RHS of a comma. But when we see a comma, we
8560 need to create a temporary for a call on the LHS. */
8561 if (decltype_p && !processing_template_decl
8562 && TREE_CODE (assignment_expression) == CALL_EXPR
8563 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8564 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8565 assignment_expression
8566 = build_cplus_new (TREE_TYPE (assignment_expression),
8567 assignment_expression, tf_warning_or_error);
8569 /* If this is the first assignment-expression, we can just
8570 save it away. */
8571 if (!expression)
8572 expression = assignment_expression;
8573 else
8574 expression = build_x_compound_expr (loc, expression,
8575 assignment_expression,
8576 complain_flags (decltype_p));
8577 /* If the next token is not a comma, then we are done with the
8578 expression. */
8579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8580 break;
8581 /* Consume the `,'. */
8582 loc = cp_lexer_peek_token (parser->lexer)->location;
8583 cp_lexer_consume_token (parser->lexer);
8584 /* A comma operator cannot appear in a constant-expression. */
8585 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8586 expression = error_mark_node;
8589 return expression;
8592 /* Parse a constant-expression.
8594 constant-expression:
8595 conditional-expression
8597 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8598 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8599 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8600 is false, NON_CONSTANT_P should be NULL. */
8602 static tree
8603 cp_parser_constant_expression (cp_parser* parser,
8604 bool allow_non_constant_p,
8605 bool *non_constant_p)
8607 bool saved_integral_constant_expression_p;
8608 bool saved_allow_non_integral_constant_expression_p;
8609 bool saved_non_integral_constant_expression_p;
8610 tree expression;
8612 /* It might seem that we could simply parse the
8613 conditional-expression, and then check to see if it were
8614 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8615 one that the compiler can figure out is constant, possibly after
8616 doing some simplifications or optimizations. The standard has a
8617 precise definition of constant-expression, and we must honor
8618 that, even though it is somewhat more restrictive.
8620 For example:
8622 int i[(2, 3)];
8624 is not a legal declaration, because `(2, 3)' is not a
8625 constant-expression. The `,' operator is forbidden in a
8626 constant-expression. However, GCC's constant-folding machinery
8627 will fold this operation to an INTEGER_CST for `3'. */
8629 /* Save the old settings. */
8630 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8631 saved_allow_non_integral_constant_expression_p
8632 = parser->allow_non_integral_constant_expression_p;
8633 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8634 /* We are now parsing a constant-expression. */
8635 parser->integral_constant_expression_p = true;
8636 parser->allow_non_integral_constant_expression_p
8637 = (allow_non_constant_p || cxx_dialect >= cxx11);
8638 parser->non_integral_constant_expression_p = false;
8639 /* Although the grammar says "conditional-expression", we parse an
8640 "assignment-expression", which also permits "throw-expression"
8641 and the use of assignment operators. In the case that
8642 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8643 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8644 actually essential that we look for an assignment-expression.
8645 For example, cp_parser_initializer_clauses uses this function to
8646 determine whether a particular assignment-expression is in fact
8647 constant. */
8648 expression = cp_parser_assignment_expression (parser);
8649 /* Restore the old settings. */
8650 parser->integral_constant_expression_p
8651 = saved_integral_constant_expression_p;
8652 parser->allow_non_integral_constant_expression_p
8653 = saved_allow_non_integral_constant_expression_p;
8654 if (cxx_dialect >= cxx11)
8656 /* Require an rvalue constant expression here; that's what our
8657 callers expect. Reference constant expressions are handled
8658 separately in e.g. cp_parser_template_argument. */
8659 bool is_const = potential_rvalue_constant_expression (expression);
8660 parser->non_integral_constant_expression_p = !is_const;
8661 if (!is_const && !allow_non_constant_p)
8662 require_potential_rvalue_constant_expression (expression);
8664 if (allow_non_constant_p)
8665 *non_constant_p = parser->non_integral_constant_expression_p;
8666 parser->non_integral_constant_expression_p
8667 = saved_non_integral_constant_expression_p;
8669 return expression;
8672 /* Parse __builtin_offsetof.
8674 offsetof-expression:
8675 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8677 offsetof-member-designator:
8678 id-expression
8679 | offsetof-member-designator "." id-expression
8680 | offsetof-member-designator "[" expression "]"
8681 | offsetof-member-designator "->" id-expression */
8683 static tree
8684 cp_parser_builtin_offsetof (cp_parser *parser)
8686 int save_ice_p, save_non_ice_p;
8687 tree type, expr;
8688 cp_id_kind dummy;
8689 cp_token *token;
8691 /* We're about to accept non-integral-constant things, but will
8692 definitely yield an integral constant expression. Save and
8693 restore these values around our local parsing. */
8694 save_ice_p = parser->integral_constant_expression_p;
8695 save_non_ice_p = parser->non_integral_constant_expression_p;
8697 /* Consume the "__builtin_offsetof" token. */
8698 cp_lexer_consume_token (parser->lexer);
8699 /* Consume the opening `('. */
8700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8701 /* Parse the type-id. */
8702 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8703 type = cp_parser_type_id (parser);
8704 /* Look for the `,'. */
8705 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8706 token = cp_lexer_peek_token (parser->lexer);
8708 /* Build the (type *)null that begins the traditional offsetof macro. */
8709 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8710 tf_warning_or_error);
8712 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8713 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8714 true, &dummy, token->location);
8715 while (true)
8717 token = cp_lexer_peek_token (parser->lexer);
8718 switch (token->type)
8720 case CPP_OPEN_SQUARE:
8721 /* offsetof-member-designator "[" expression "]" */
8722 expr = cp_parser_postfix_open_square_expression (parser, expr,
8723 true, false);
8724 break;
8726 case CPP_DEREF:
8727 /* offsetof-member-designator "->" identifier */
8728 expr = grok_array_decl (token->location, expr,
8729 integer_zero_node, false);
8730 /* FALLTHRU */
8732 case CPP_DOT:
8733 /* offsetof-member-designator "." identifier */
8734 cp_lexer_consume_token (parser->lexer);
8735 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8736 expr, true, &dummy,
8737 token->location);
8738 break;
8740 case CPP_CLOSE_PAREN:
8741 /* Consume the ")" token. */
8742 cp_lexer_consume_token (parser->lexer);
8743 goto success;
8745 default:
8746 /* Error. We know the following require will fail, but
8747 that gives the proper error message. */
8748 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8749 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8750 expr = error_mark_node;
8751 goto failure;
8755 success:
8756 expr = finish_offsetof (expr, loc);
8758 failure:
8759 parser->integral_constant_expression_p = save_ice_p;
8760 parser->non_integral_constant_expression_p = save_non_ice_p;
8762 return expr;
8765 /* Parse a trait expression.
8767 Returns a representation of the expression, the underlying type
8768 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8770 static tree
8771 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8773 cp_trait_kind kind;
8774 tree type1, type2 = NULL_TREE;
8775 bool binary = false;
8776 bool variadic = false;
8778 switch (keyword)
8780 case RID_HAS_NOTHROW_ASSIGN:
8781 kind = CPTK_HAS_NOTHROW_ASSIGN;
8782 break;
8783 case RID_HAS_NOTHROW_CONSTRUCTOR:
8784 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8785 break;
8786 case RID_HAS_NOTHROW_COPY:
8787 kind = CPTK_HAS_NOTHROW_COPY;
8788 break;
8789 case RID_HAS_TRIVIAL_ASSIGN:
8790 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8791 break;
8792 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8793 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8794 break;
8795 case RID_HAS_TRIVIAL_COPY:
8796 kind = CPTK_HAS_TRIVIAL_COPY;
8797 break;
8798 case RID_HAS_TRIVIAL_DESTRUCTOR:
8799 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8800 break;
8801 case RID_HAS_VIRTUAL_DESTRUCTOR:
8802 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8803 break;
8804 case RID_IS_ABSTRACT:
8805 kind = CPTK_IS_ABSTRACT;
8806 break;
8807 case RID_IS_BASE_OF:
8808 kind = CPTK_IS_BASE_OF;
8809 binary = true;
8810 break;
8811 case RID_IS_CLASS:
8812 kind = CPTK_IS_CLASS;
8813 break;
8814 case RID_IS_EMPTY:
8815 kind = CPTK_IS_EMPTY;
8816 break;
8817 case RID_IS_ENUM:
8818 kind = CPTK_IS_ENUM;
8819 break;
8820 case RID_IS_FINAL:
8821 kind = CPTK_IS_FINAL;
8822 break;
8823 case RID_IS_LITERAL_TYPE:
8824 kind = CPTK_IS_LITERAL_TYPE;
8825 break;
8826 case RID_IS_POD:
8827 kind = CPTK_IS_POD;
8828 break;
8829 case RID_IS_POLYMORPHIC:
8830 kind = CPTK_IS_POLYMORPHIC;
8831 break;
8832 case RID_IS_STD_LAYOUT:
8833 kind = CPTK_IS_STD_LAYOUT;
8834 break;
8835 case RID_IS_TRIVIAL:
8836 kind = CPTK_IS_TRIVIAL;
8837 break;
8838 case RID_IS_TRIVIALLY_ASSIGNABLE:
8839 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8840 binary = true;
8841 break;
8842 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8843 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8844 variadic = true;
8845 break;
8846 case RID_IS_TRIVIALLY_COPYABLE:
8847 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8848 break;
8849 case RID_IS_UNION:
8850 kind = CPTK_IS_UNION;
8851 break;
8852 case RID_UNDERLYING_TYPE:
8853 kind = CPTK_UNDERLYING_TYPE;
8854 break;
8855 case RID_BASES:
8856 kind = CPTK_BASES;
8857 break;
8858 case RID_DIRECT_BASES:
8859 kind = CPTK_DIRECT_BASES;
8860 break;
8861 default:
8862 gcc_unreachable ();
8865 /* Consume the token. */
8866 cp_lexer_consume_token (parser->lexer);
8868 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8870 type1 = cp_parser_type_id (parser);
8872 if (type1 == error_mark_node)
8873 return error_mark_node;
8875 if (binary)
8877 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8879 type2 = cp_parser_type_id (parser);
8881 if (type2 == error_mark_node)
8882 return error_mark_node;
8884 else if (variadic)
8886 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8888 cp_lexer_consume_token (parser->lexer);
8889 tree elt = cp_parser_type_id (parser);
8890 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8892 cp_lexer_consume_token (parser->lexer);
8893 elt = make_pack_expansion (elt);
8895 if (elt == error_mark_node)
8896 return error_mark_node;
8897 type2 = tree_cons (NULL_TREE, elt, type2);
8901 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8903 /* Complete the trait expression, which may mean either processing
8904 the trait expr now or saving it for template instantiation. */
8905 switch(kind)
8907 case CPTK_UNDERLYING_TYPE:
8908 return finish_underlying_type (type1);
8909 case CPTK_BASES:
8910 return finish_bases (type1, false);
8911 case CPTK_DIRECT_BASES:
8912 return finish_bases (type1, true);
8913 default:
8914 return finish_trait_expr (kind, type1, type2);
8918 /* Lambdas that appear in variable initializer or default argument scope
8919 get that in their mangling, so we need to record it. We might as well
8920 use the count for function and namespace scopes as well. */
8921 static GTY(()) tree lambda_scope;
8922 static GTY(()) int lambda_count;
8923 typedef struct GTY(()) tree_int
8925 tree t;
8926 int i;
8927 } tree_int;
8928 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8930 static void
8931 start_lambda_scope (tree decl)
8933 tree_int ti;
8934 gcc_assert (decl);
8935 /* Once we're inside a function, we ignore other scopes and just push
8936 the function again so that popping works properly. */
8937 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8938 decl = current_function_decl;
8939 ti.t = lambda_scope;
8940 ti.i = lambda_count;
8941 vec_safe_push (lambda_scope_stack, ti);
8942 if (lambda_scope != decl)
8944 /* Don't reset the count if we're still in the same function. */
8945 lambda_scope = decl;
8946 lambda_count = 0;
8950 static void
8951 record_lambda_scope (tree lambda)
8953 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8954 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8957 static void
8958 finish_lambda_scope (void)
8960 tree_int *p = &lambda_scope_stack->last ();
8961 if (lambda_scope != p->t)
8963 lambda_scope = p->t;
8964 lambda_count = p->i;
8966 lambda_scope_stack->pop ();
8969 /* Parse a lambda expression.
8971 lambda-expression:
8972 lambda-introducer lambda-declarator [opt] compound-statement
8974 Returns a representation of the expression. */
8976 static tree
8977 cp_parser_lambda_expression (cp_parser* parser)
8979 tree lambda_expr = build_lambda_expr ();
8980 tree type;
8981 bool ok = true;
8982 cp_token *token = cp_lexer_peek_token (parser->lexer);
8983 cp_token_position start = 0;
8985 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8987 if (cp_unevaluated_operand)
8989 if (!token->error_reported)
8991 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8992 "lambda-expression in unevaluated context");
8993 token->error_reported = true;
8995 ok = false;
8997 else if (parser->in_template_argument_list_p)
8999 if (!token->error_reported)
9001 error_at (token->location, "lambda-expression in template-argument");
9002 token->error_reported = true;
9004 ok = false;
9007 /* We may be in the middle of deferred access check. Disable
9008 it now. */
9009 push_deferring_access_checks (dk_no_deferred);
9011 cp_parser_lambda_introducer (parser, lambda_expr);
9013 type = begin_lambda_type (lambda_expr);
9014 if (type == error_mark_node)
9015 return error_mark_node;
9017 record_lambda_scope (lambda_expr);
9019 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9020 determine_visibility (TYPE_NAME (type));
9022 /* Now that we've started the type, add the capture fields for any
9023 explicit captures. */
9024 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9027 /* Inside the class, surrounding template-parameter-lists do not apply. */
9028 unsigned int saved_num_template_parameter_lists
9029 = parser->num_template_parameter_lists;
9030 unsigned char in_statement = parser->in_statement;
9031 bool in_switch_statement_p = parser->in_switch_statement_p;
9032 bool fully_implicit_function_template_p
9033 = parser->fully_implicit_function_template_p;
9034 tree implicit_template_parms = parser->implicit_template_parms;
9035 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9036 bool auto_is_implicit_function_template_parm_p
9037 = parser->auto_is_implicit_function_template_parm_p;
9039 parser->num_template_parameter_lists = 0;
9040 parser->in_statement = 0;
9041 parser->in_switch_statement_p = false;
9042 parser->fully_implicit_function_template_p = false;
9043 parser->implicit_template_parms = 0;
9044 parser->implicit_template_scope = 0;
9045 parser->auto_is_implicit_function_template_parm_p = false;
9047 /* By virtue of defining a local class, a lambda expression has access to
9048 the private variables of enclosing classes. */
9050 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9052 if (ok)
9054 if (!cp_parser_error_occurred (parser)
9055 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9056 && cp_parser_start_tentative_firewall (parser))
9057 start = token;
9058 cp_parser_lambda_body (parser, lambda_expr);
9060 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9062 if (cp_parser_skip_to_closing_brace (parser))
9063 cp_lexer_consume_token (parser->lexer);
9066 /* The capture list was built up in reverse order; fix that now. */
9067 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9068 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9070 if (ok)
9071 maybe_add_lambda_conv_op (type);
9073 type = finish_struct (type, /*attributes=*/NULL_TREE);
9075 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9076 parser->in_statement = in_statement;
9077 parser->in_switch_statement_p = in_switch_statement_p;
9078 parser->fully_implicit_function_template_p
9079 = fully_implicit_function_template_p;
9080 parser->implicit_template_parms = implicit_template_parms;
9081 parser->implicit_template_scope = implicit_template_scope;
9082 parser->auto_is_implicit_function_template_parm_p
9083 = auto_is_implicit_function_template_parm_p;
9086 pop_deferring_access_checks ();
9088 /* This field is only used during parsing of the lambda. */
9089 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9091 /* This lambda shouldn't have any proxies left at this point. */
9092 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9093 /* And now that we're done, push proxies for an enclosing lambda. */
9094 insert_pending_capture_proxies ();
9096 if (ok)
9097 lambda_expr = build_lambda_object (lambda_expr);
9098 else
9099 lambda_expr = error_mark_node;
9101 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9103 return lambda_expr;
9106 /* Parse the beginning of a lambda expression.
9108 lambda-introducer:
9109 [ lambda-capture [opt] ]
9111 LAMBDA_EXPR is the current representation of the lambda expression. */
9113 static void
9114 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9116 /* Need commas after the first capture. */
9117 bool first = true;
9119 /* Eat the leading `['. */
9120 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9122 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9123 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9124 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9125 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9126 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9127 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9129 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9131 cp_lexer_consume_token (parser->lexer);
9132 first = false;
9135 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9137 cp_token* capture_token;
9138 tree capture_id;
9139 tree capture_init_expr;
9140 cp_id_kind idk = CP_ID_KIND_NONE;
9141 bool explicit_init_p = false;
9143 enum capture_kind_type
9145 BY_COPY,
9146 BY_REFERENCE
9148 enum capture_kind_type capture_kind = BY_COPY;
9150 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9152 error ("expected end of capture-list");
9153 return;
9156 if (first)
9157 first = false;
9158 else
9159 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9161 /* Possibly capture `this'. */
9162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9164 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9165 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9166 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9167 "with by-copy capture default");
9168 cp_lexer_consume_token (parser->lexer);
9169 add_capture (lambda_expr,
9170 /*id=*/this_identifier,
9171 /*initializer=*/finish_this_expr(),
9172 /*by_reference_p=*/false,
9173 explicit_init_p);
9174 continue;
9177 /* Remember whether we want to capture as a reference or not. */
9178 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9180 capture_kind = BY_REFERENCE;
9181 cp_lexer_consume_token (parser->lexer);
9184 /* Get the identifier. */
9185 capture_token = cp_lexer_peek_token (parser->lexer);
9186 capture_id = cp_parser_identifier (parser);
9188 if (capture_id == error_mark_node)
9189 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9190 delimiters, but I modified this to stop on unnested ']' as well. It
9191 was already changed to stop on unnested '}', so the
9192 "closing_parenthesis" name is no more misleading with my change. */
9194 cp_parser_skip_to_closing_parenthesis (parser,
9195 /*recovering=*/true,
9196 /*or_comma=*/true,
9197 /*consume_paren=*/true);
9198 break;
9201 /* Find the initializer for this capture. */
9202 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9203 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9204 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9206 bool direct, non_constant;
9207 /* An explicit initializer exists. */
9208 if (cxx_dialect < cxx14)
9209 pedwarn (input_location, 0,
9210 "lambda capture initializers "
9211 "only available with -std=c++14 or -std=gnu++14");
9212 capture_init_expr = cp_parser_initializer (parser, &direct,
9213 &non_constant);
9214 explicit_init_p = true;
9215 if (capture_init_expr == NULL_TREE)
9217 error ("empty initializer for lambda init-capture");
9218 capture_init_expr = error_mark_node;
9221 else
9223 const char* error_msg;
9225 /* Turn the identifier into an id-expression. */
9226 capture_init_expr
9227 = cp_parser_lookup_name_simple (parser, capture_id,
9228 capture_token->location);
9230 if (capture_init_expr == error_mark_node)
9232 unqualified_name_lookup_error (capture_id);
9233 continue;
9235 else if (DECL_P (capture_init_expr)
9236 && (!VAR_P (capture_init_expr)
9237 && TREE_CODE (capture_init_expr) != PARM_DECL))
9239 error_at (capture_token->location,
9240 "capture of non-variable %qD ",
9241 capture_init_expr);
9242 inform (0, "%q+#D declared here", capture_init_expr);
9243 continue;
9245 if (VAR_P (capture_init_expr)
9246 && decl_storage_duration (capture_init_expr) != dk_auto)
9248 if (pedwarn (capture_token->location, 0, "capture of variable "
9249 "%qD with non-automatic storage duration",
9250 capture_init_expr))
9251 inform (0, "%q+#D declared here", capture_init_expr);
9252 continue;
9255 capture_init_expr
9256 = finish_id_expression
9257 (capture_id,
9258 capture_init_expr,
9259 parser->scope,
9260 &idk,
9261 /*integral_constant_expression_p=*/false,
9262 /*allow_non_integral_constant_expression_p=*/false,
9263 /*non_integral_constant_expression_p=*/NULL,
9264 /*template_p=*/false,
9265 /*done=*/true,
9266 /*address_p=*/false,
9267 /*template_arg_p=*/false,
9268 &error_msg,
9269 capture_token->location);
9271 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9273 cp_lexer_consume_token (parser->lexer);
9274 capture_init_expr = make_pack_expansion (capture_init_expr);
9276 else
9277 check_for_bare_parameter_packs (capture_init_expr);
9280 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9281 && !explicit_init_p)
9283 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9284 && capture_kind == BY_COPY)
9285 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9286 "of %qD redundant with by-copy capture default",
9287 capture_id);
9288 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9289 && capture_kind == BY_REFERENCE)
9290 pedwarn (capture_token->location, 0, "explicit by-reference "
9291 "capture of %qD redundant with by-reference capture "
9292 "default", capture_id);
9295 add_capture (lambda_expr,
9296 capture_id,
9297 capture_init_expr,
9298 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9299 explicit_init_p);
9302 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9305 /* Parse the (optional) middle of a lambda expression.
9307 lambda-declarator:
9308 < template-parameter-list [opt] >
9309 ( parameter-declaration-clause [opt] )
9310 attribute-specifier [opt]
9311 mutable [opt]
9312 exception-specification [opt]
9313 lambda-return-type-clause [opt]
9315 LAMBDA_EXPR is the current representation of the lambda expression. */
9317 static bool
9318 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9320 /* 5.1.1.4 of the standard says:
9321 If a lambda-expression does not include a lambda-declarator, it is as if
9322 the lambda-declarator were ().
9323 This means an empty parameter list, no attributes, and no exception
9324 specification. */
9325 tree param_list = void_list_node;
9326 tree attributes = NULL_TREE;
9327 tree exception_spec = NULL_TREE;
9328 tree template_param_list = NULL_TREE;
9330 /* The template-parameter-list is optional, but must begin with
9331 an opening angle if present. */
9332 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9334 if (cxx_dialect < cxx14)
9335 pedwarn (parser->lexer->next_token->location, 0,
9336 "lambda templates are only available with "
9337 "-std=c++14 or -std=gnu++14");
9339 cp_lexer_consume_token (parser->lexer);
9341 template_param_list = cp_parser_template_parameter_list (parser);
9343 cp_parser_skip_to_end_of_template_parameter_list (parser);
9345 /* We just processed one more parameter list. */
9346 ++parser->num_template_parameter_lists;
9349 /* The parameter-declaration-clause is optional (unless
9350 template-parameter-list was given), but must begin with an
9351 opening parenthesis if present. */
9352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9354 cp_lexer_consume_token (parser->lexer);
9356 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9358 /* Parse parameters. */
9359 param_list = cp_parser_parameter_declaration_clause (parser);
9361 /* Default arguments shall not be specified in the
9362 parameter-declaration-clause of a lambda-declarator. */
9363 for (tree t = param_list; t; t = TREE_CHAIN (t))
9364 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9365 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9366 "default argument specified for lambda parameter");
9368 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9370 attributes = cp_parser_attributes_opt (parser);
9372 /* Parse optional `mutable' keyword. */
9373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9375 cp_lexer_consume_token (parser->lexer);
9376 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9379 /* Parse optional exception specification. */
9380 exception_spec = cp_parser_exception_specification_opt (parser);
9382 /* Parse optional trailing return type. */
9383 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9385 cp_lexer_consume_token (parser->lexer);
9386 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9387 = cp_parser_trailing_type_id (parser);
9390 /* The function parameters must be in scope all the way until after the
9391 trailing-return-type in case of decltype. */
9392 pop_bindings_and_leave_scope ();
9394 else if (template_param_list != NULL_TREE) // generate diagnostic
9395 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9397 /* Create the function call operator.
9399 Messing with declarators like this is no uglier than building up the
9400 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9401 other code. */
9403 cp_decl_specifier_seq return_type_specs;
9404 cp_declarator* declarator;
9405 tree fco;
9406 int quals;
9407 void *p;
9409 clear_decl_specs (&return_type_specs);
9410 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9411 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9412 else
9413 /* Maybe we will deduce the return type later. */
9414 return_type_specs.type = make_auto ();
9416 p = obstack_alloc (&declarator_obstack, 0);
9418 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9419 sfk_none);
9421 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9422 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9423 declarator = make_call_declarator (declarator, param_list, quals,
9424 VIRT_SPEC_UNSPECIFIED,
9425 REF_QUAL_NONE,
9426 exception_spec,
9427 /*late_return_type=*/NULL_TREE);
9428 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9430 fco = grokmethod (&return_type_specs,
9431 declarator,
9432 attributes);
9433 if (fco != error_mark_node)
9435 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9436 DECL_ARTIFICIAL (fco) = 1;
9437 /* Give the object parameter a different name. */
9438 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9439 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9440 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9442 if (template_param_list)
9444 fco = finish_member_template_decl (fco);
9445 finish_template_decl (template_param_list);
9446 --parser->num_template_parameter_lists;
9448 else if (parser->fully_implicit_function_template_p)
9449 fco = finish_fully_implicit_template (parser, fco);
9451 finish_member_declaration (fco);
9453 obstack_free (&declarator_obstack, p);
9455 return (fco != error_mark_node);
9459 /* Parse the body of a lambda expression, which is simply
9461 compound-statement
9463 but which requires special handling.
9464 LAMBDA_EXPR is the current representation of the lambda expression. */
9466 static void
9467 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9469 bool nested = (current_function_decl != NULL_TREE);
9470 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9471 if (nested)
9472 push_function_context ();
9473 else
9474 /* Still increment function_depth so that we don't GC in the
9475 middle of an expression. */
9476 ++function_depth;
9477 vec<tree> omp_privatization_save;
9478 save_omp_privatization_clauses (omp_privatization_save);
9479 /* Clear this in case we're in the middle of a default argument. */
9480 parser->local_variables_forbidden_p = false;
9482 /* Finish the function call operator
9483 - class_specifier
9484 + late_parsing_for_member
9485 + function_definition_after_declarator
9486 + ctor_initializer_opt_and_function_body */
9488 tree fco = lambda_function (lambda_expr);
9489 tree body;
9490 bool done = false;
9491 tree compound_stmt;
9492 tree cap;
9494 /* Let the front end know that we are going to be defining this
9495 function. */
9496 start_preparsed_function (fco,
9497 NULL_TREE,
9498 SF_PRE_PARSED | SF_INCLASS_INLINE);
9500 start_lambda_scope (fco);
9501 body = begin_function_body ();
9503 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9504 goto out;
9506 /* Push the proxies for any explicit captures. */
9507 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9508 cap = TREE_CHAIN (cap))
9509 build_capture_proxy (TREE_PURPOSE (cap));
9511 compound_stmt = begin_compound_stmt (0);
9513 /* 5.1.1.4 of the standard says:
9514 If a lambda-expression does not include a trailing-return-type, it
9515 is as if the trailing-return-type denotes the following type:
9516 * if the compound-statement is of the form
9517 { return attribute-specifier [opt] expression ; }
9518 the type of the returned expression after lvalue-to-rvalue
9519 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9520 (_conv.array_ 4.2), and function-to-pointer conversion
9521 (_conv.func_ 4.3);
9522 * otherwise, void. */
9524 /* In a lambda that has neither a lambda-return-type-clause
9525 nor a deducible form, errors should be reported for return statements
9526 in the body. Since we used void as the placeholder return type, parsing
9527 the body as usual will give such desired behavior. */
9528 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9529 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9530 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9532 tree expr = NULL_TREE;
9533 cp_id_kind idk = CP_ID_KIND_NONE;
9535 /* Parse tentatively in case there's more after the initial return
9536 statement. */
9537 cp_parser_parse_tentatively (parser);
9539 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9541 expr = cp_parser_expression (parser, &idk);
9543 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9544 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9546 if (cp_parser_parse_definitely (parser))
9548 if (!processing_template_decl)
9549 apply_deduced_return_type (fco, lambda_return_type (expr));
9551 /* Will get error here if type not deduced yet. */
9552 finish_return_stmt (expr);
9554 done = true;
9558 if (!done)
9560 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9561 cp_parser_label_declaration (parser);
9562 cp_parser_statement_seq_opt (parser, NULL_TREE);
9563 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9566 finish_compound_stmt (compound_stmt);
9568 out:
9569 finish_function_body (body);
9570 finish_lambda_scope ();
9572 /* Finish the function and generate code for it if necessary. */
9573 tree fn = finish_function (/*inline*/2);
9575 /* Only expand if the call op is not a template. */
9576 if (!DECL_TEMPLATE_INFO (fco))
9577 expand_or_defer_fn (fn);
9580 restore_omp_privatization_clauses (omp_privatization_save);
9581 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9582 if (nested)
9583 pop_function_context();
9584 else
9585 --function_depth;
9588 /* Statements [gram.stmt.stmt] */
9590 /* Parse a statement.
9592 statement:
9593 labeled-statement
9594 expression-statement
9595 compound-statement
9596 selection-statement
9597 iteration-statement
9598 jump-statement
9599 declaration-statement
9600 try-block
9602 C++11:
9604 statement:
9605 labeled-statement
9606 attribute-specifier-seq (opt) expression-statement
9607 attribute-specifier-seq (opt) compound-statement
9608 attribute-specifier-seq (opt) selection-statement
9609 attribute-specifier-seq (opt) iteration-statement
9610 attribute-specifier-seq (opt) jump-statement
9611 declaration-statement
9612 attribute-specifier-seq (opt) try-block
9614 TM Extension:
9616 statement:
9617 atomic-statement
9619 IN_COMPOUND is true when the statement is nested inside a
9620 cp_parser_compound_statement; this matters for certain pragmas.
9622 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9623 is a (possibly labeled) if statement which is not enclosed in braces
9624 and has an else clause. This is used to implement -Wparentheses. */
9626 static void
9627 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9628 bool in_compound, bool *if_p)
9630 tree statement, std_attrs = NULL_TREE;
9631 cp_token *token;
9632 location_t statement_location, attrs_location;
9634 restart:
9635 if (if_p != NULL)
9636 *if_p = false;
9637 /* There is no statement yet. */
9638 statement = NULL_TREE;
9640 saved_token_sentinel saved_tokens (parser->lexer);
9641 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9642 if (c_dialect_objc ())
9643 /* In obj-c++, seeing '[[' might be the either the beginning of
9644 c++11 attributes, or a nested objc-message-expression. So
9645 let's parse the c++11 attributes tentatively. */
9646 cp_parser_parse_tentatively (parser);
9647 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9648 if (c_dialect_objc ())
9650 if (!cp_parser_parse_definitely (parser))
9651 std_attrs = NULL_TREE;
9654 /* Peek at the next token. */
9655 token = cp_lexer_peek_token (parser->lexer);
9656 /* Remember the location of the first token in the statement. */
9657 statement_location = token->location;
9658 /* If this is a keyword, then that will often determine what kind of
9659 statement we have. */
9660 if (token->type == CPP_KEYWORD)
9662 enum rid keyword = token->keyword;
9664 switch (keyword)
9666 case RID_CASE:
9667 case RID_DEFAULT:
9668 /* Looks like a labeled-statement with a case label.
9669 Parse the label, and then use tail recursion to parse
9670 the statement. */
9671 cp_parser_label_for_labeled_statement (parser, std_attrs);
9672 goto restart;
9674 case RID_IF:
9675 case RID_SWITCH:
9676 statement = cp_parser_selection_statement (parser, if_p);
9677 break;
9679 case RID_WHILE:
9680 case RID_DO:
9681 case RID_FOR:
9682 statement = cp_parser_iteration_statement (parser, false);
9683 break;
9685 case RID_CILK_FOR:
9686 if (!flag_cilkplus)
9688 error_at (cp_lexer_peek_token (parser->lexer)->location,
9689 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9690 cp_lexer_consume_token (parser->lexer);
9691 statement = error_mark_node;
9693 else
9694 statement = cp_parser_cilk_for (parser, integer_zero_node);
9695 break;
9697 case RID_BREAK:
9698 case RID_CONTINUE:
9699 case RID_RETURN:
9700 case RID_GOTO:
9701 statement = cp_parser_jump_statement (parser);
9702 break;
9704 case RID_CILK_SYNC:
9705 cp_lexer_consume_token (parser->lexer);
9706 if (flag_cilkplus)
9708 tree sync_expr = build_cilk_sync ();
9709 SET_EXPR_LOCATION (sync_expr,
9710 token->location);
9711 statement = finish_expr_stmt (sync_expr);
9713 else
9715 error_at (token->location, "-fcilkplus must be enabled to use"
9716 " %<_Cilk_sync%>");
9717 statement = error_mark_node;
9719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9720 break;
9722 /* Objective-C++ exception-handling constructs. */
9723 case RID_AT_TRY:
9724 case RID_AT_CATCH:
9725 case RID_AT_FINALLY:
9726 case RID_AT_SYNCHRONIZED:
9727 case RID_AT_THROW:
9728 statement = cp_parser_objc_statement (parser);
9729 break;
9731 case RID_TRY:
9732 statement = cp_parser_try_block (parser);
9733 break;
9735 case RID_NAMESPACE:
9736 /* This must be a namespace alias definition. */
9737 cp_parser_declaration_statement (parser);
9738 return;
9740 case RID_TRANSACTION_ATOMIC:
9741 case RID_TRANSACTION_RELAXED:
9742 statement = cp_parser_transaction (parser, keyword);
9743 break;
9744 case RID_TRANSACTION_CANCEL:
9745 statement = cp_parser_transaction_cancel (parser);
9746 break;
9748 default:
9749 /* It might be a keyword like `int' that can start a
9750 declaration-statement. */
9751 break;
9754 else if (token->type == CPP_NAME)
9756 /* If the next token is a `:', then we are looking at a
9757 labeled-statement. */
9758 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9759 if (token->type == CPP_COLON)
9761 /* Looks like a labeled-statement with an ordinary label.
9762 Parse the label, and then use tail recursion to parse
9763 the statement. */
9765 cp_parser_label_for_labeled_statement (parser, std_attrs);
9766 goto restart;
9769 /* Anything that starts with a `{' must be a compound-statement. */
9770 else if (token->type == CPP_OPEN_BRACE)
9771 statement = cp_parser_compound_statement (parser, NULL, false, false);
9772 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9773 a statement all its own. */
9774 else if (token->type == CPP_PRAGMA)
9776 /* Only certain OpenMP pragmas are attached to statements, and thus
9777 are considered statements themselves. All others are not. In
9778 the context of a compound, accept the pragma as a "statement" and
9779 return so that we can check for a close brace. Otherwise we
9780 require a real statement and must go back and read one. */
9781 if (in_compound)
9782 cp_parser_pragma (parser, pragma_compound);
9783 else if (!cp_parser_pragma (parser, pragma_stmt))
9784 goto restart;
9785 return;
9787 else if (token->type == CPP_EOF)
9789 cp_parser_error (parser, "expected statement");
9790 return;
9793 /* Everything else must be a declaration-statement or an
9794 expression-statement. Try for the declaration-statement
9795 first, unless we are looking at a `;', in which case we know that
9796 we have an expression-statement. */
9797 if (!statement)
9799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9801 if (std_attrs != NULL_TREE)
9803 /* Attributes should be parsed as part of the the
9804 declaration, so let's un-parse them. */
9805 saved_tokens.rollback();
9806 std_attrs = NULL_TREE;
9809 cp_parser_parse_tentatively (parser);
9810 /* Try to parse the declaration-statement. */
9811 cp_parser_declaration_statement (parser);
9812 /* If that worked, we're done. */
9813 if (cp_parser_parse_definitely (parser))
9814 return;
9816 /* Look for an expression-statement instead. */
9817 statement = cp_parser_expression_statement (parser, in_statement_expr);
9820 /* Set the line number for the statement. */
9821 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9822 SET_EXPR_LOCATION (statement, statement_location);
9824 /* Note that for now, we don't do anything with c++11 statements
9825 parsed at this level. */
9826 if (std_attrs != NULL_TREE)
9827 warning_at (attrs_location,
9828 OPT_Wattributes,
9829 "attributes at the beginning of statement are ignored");
9832 /* Parse the label for a labeled-statement, i.e.
9834 identifier :
9835 case constant-expression :
9836 default :
9838 GNU Extension:
9839 case constant-expression ... constant-expression : statement
9841 When a label is parsed without errors, the label is added to the
9842 parse tree by the finish_* functions, so this function doesn't
9843 have to return the label. */
9845 static void
9846 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9848 cp_token *token;
9849 tree label = NULL_TREE;
9850 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9852 /* The next token should be an identifier. */
9853 token = cp_lexer_peek_token (parser->lexer);
9854 if (token->type != CPP_NAME
9855 && token->type != CPP_KEYWORD)
9857 cp_parser_error (parser, "expected labeled-statement");
9858 return;
9861 parser->colon_corrects_to_scope_p = false;
9862 switch (token->keyword)
9864 case RID_CASE:
9866 tree expr, expr_hi;
9867 cp_token *ellipsis;
9869 /* Consume the `case' token. */
9870 cp_lexer_consume_token (parser->lexer);
9871 /* Parse the constant-expression. */
9872 expr = cp_parser_constant_expression (parser);
9873 if (check_for_bare_parameter_packs (expr))
9874 expr = error_mark_node;
9876 ellipsis = cp_lexer_peek_token (parser->lexer);
9877 if (ellipsis->type == CPP_ELLIPSIS)
9879 /* Consume the `...' token. */
9880 cp_lexer_consume_token (parser->lexer);
9881 expr_hi = cp_parser_constant_expression (parser);
9882 if (check_for_bare_parameter_packs (expr_hi))
9883 expr_hi = error_mark_node;
9885 /* We don't need to emit warnings here, as the common code
9886 will do this for us. */
9888 else
9889 expr_hi = NULL_TREE;
9891 if (parser->in_switch_statement_p)
9892 finish_case_label (token->location, expr, expr_hi);
9893 else
9894 error_at (token->location,
9895 "case label %qE not within a switch statement",
9896 expr);
9898 break;
9900 case RID_DEFAULT:
9901 /* Consume the `default' token. */
9902 cp_lexer_consume_token (parser->lexer);
9904 if (parser->in_switch_statement_p)
9905 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9906 else
9907 error_at (token->location, "case label not within a switch statement");
9908 break;
9910 default:
9911 /* Anything else must be an ordinary label. */
9912 label = finish_label_stmt (cp_parser_identifier (parser));
9913 break;
9916 /* Require the `:' token. */
9917 cp_parser_require (parser, CPP_COLON, RT_COLON);
9919 /* An ordinary label may optionally be followed by attributes.
9920 However, this is only permitted if the attributes are then
9921 followed by a semicolon. This is because, for backward
9922 compatibility, when parsing
9923 lab: __attribute__ ((unused)) int i;
9924 we want the attribute to attach to "i", not "lab". */
9925 if (label != NULL_TREE
9926 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9928 tree attrs;
9929 cp_parser_parse_tentatively (parser);
9930 attrs = cp_parser_gnu_attributes_opt (parser);
9931 if (attrs == NULL_TREE
9932 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9933 cp_parser_abort_tentative_parse (parser);
9934 else if (!cp_parser_parse_definitely (parser))
9936 else
9937 attributes = chainon (attributes, attrs);
9940 if (attributes != NULL_TREE)
9941 cplus_decl_attributes (&label, attributes, 0);
9943 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9946 /* Parse an expression-statement.
9948 expression-statement:
9949 expression [opt] ;
9951 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9952 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9953 indicates whether this expression-statement is part of an
9954 expression statement. */
9956 static tree
9957 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9959 tree statement = NULL_TREE;
9960 cp_token *token = cp_lexer_peek_token (parser->lexer);
9962 /* If the next token is a ';', then there is no expression
9963 statement. */
9964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9966 statement = cp_parser_expression (parser);
9967 if (statement == error_mark_node
9968 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9970 cp_parser_skip_to_end_of_block_or_statement (parser);
9971 return error_mark_node;
9975 /* Give a helpful message for "A<T>::type t;" and the like. */
9976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9977 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9979 if (TREE_CODE (statement) == SCOPE_REF)
9980 error_at (token->location, "need %<typename%> before %qE because "
9981 "%qT is a dependent scope",
9982 statement, TREE_OPERAND (statement, 0));
9983 else if (is_overloaded_fn (statement)
9984 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9986 /* A::A a; */
9987 tree fn = get_first_fn (statement);
9988 error_at (token->location,
9989 "%<%T::%D%> names the constructor, not the type",
9990 DECL_CONTEXT (fn), DECL_NAME (fn));
9994 /* Consume the final `;'. */
9995 cp_parser_consume_semicolon_at_end_of_statement (parser);
9997 if (in_statement_expr
9998 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9999 /* This is the final expression statement of a statement
10000 expression. */
10001 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10002 else if (statement)
10003 statement = finish_expr_stmt (statement);
10005 return statement;
10008 /* Parse a compound-statement.
10010 compound-statement:
10011 { statement-seq [opt] }
10013 GNU extension:
10015 compound-statement:
10016 { label-declaration-seq [opt] statement-seq [opt] }
10018 label-declaration-seq:
10019 label-declaration
10020 label-declaration-seq label-declaration
10022 Returns a tree representing the statement. */
10024 static tree
10025 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10026 bool in_try, bool function_body)
10028 tree compound_stmt;
10030 /* Consume the `{'. */
10031 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10032 return error_mark_node;
10033 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10034 && !function_body && cxx_dialect < cxx14)
10035 pedwarn (input_location, OPT_Wpedantic,
10036 "compound-statement in constexpr function");
10037 /* Begin the compound-statement. */
10038 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10039 /* If the next keyword is `__label__' we have a label declaration. */
10040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10041 cp_parser_label_declaration (parser);
10042 /* Parse an (optional) statement-seq. */
10043 cp_parser_statement_seq_opt (parser, in_statement_expr);
10044 /* Finish the compound-statement. */
10045 finish_compound_stmt (compound_stmt);
10046 /* Consume the `}'. */
10047 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10049 return compound_stmt;
10052 /* Parse an (optional) statement-seq.
10054 statement-seq:
10055 statement
10056 statement-seq [opt] statement */
10058 static void
10059 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10061 /* Scan statements until there aren't any more. */
10062 while (true)
10064 cp_token *token = cp_lexer_peek_token (parser->lexer);
10066 /* If we are looking at a `}', then we have run out of
10067 statements; the same is true if we have reached the end
10068 of file, or have stumbled upon a stray '@end'. */
10069 if (token->type == CPP_CLOSE_BRACE
10070 || token->type == CPP_EOF
10071 || token->type == CPP_PRAGMA_EOL
10072 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10073 break;
10075 /* If we are in a compound statement and find 'else' then
10076 something went wrong. */
10077 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10079 if (parser->in_statement & IN_IF_STMT)
10080 break;
10081 else
10083 token = cp_lexer_consume_token (parser->lexer);
10084 error_at (token->location, "%<else%> without a previous %<if%>");
10088 /* Parse the statement. */
10089 cp_parser_statement (parser, in_statement_expr, true, NULL);
10093 /* Parse a selection-statement.
10095 selection-statement:
10096 if ( condition ) statement
10097 if ( condition ) statement else statement
10098 switch ( condition ) statement
10100 Returns the new IF_STMT or SWITCH_STMT.
10102 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10103 is a (possibly labeled) if statement which is not enclosed in
10104 braces and has an else clause. This is used to implement
10105 -Wparentheses. */
10107 static tree
10108 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10110 cp_token *token;
10111 enum rid keyword;
10113 if (if_p != NULL)
10114 *if_p = false;
10116 /* Peek at the next token. */
10117 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10119 /* See what kind of keyword it is. */
10120 keyword = token->keyword;
10121 switch (keyword)
10123 case RID_IF:
10124 case RID_SWITCH:
10126 tree statement;
10127 tree condition;
10129 /* Look for the `('. */
10130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10132 cp_parser_skip_to_end_of_statement (parser);
10133 return error_mark_node;
10136 /* Begin the selection-statement. */
10137 if (keyword == RID_IF)
10138 statement = begin_if_stmt ();
10139 else
10140 statement = begin_switch_stmt ();
10142 /* Parse the condition. */
10143 condition = cp_parser_condition (parser);
10144 /* Look for the `)'. */
10145 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10146 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10147 /*consume_paren=*/true);
10149 if (keyword == RID_IF)
10151 bool nested_if;
10152 unsigned char in_statement;
10154 /* Add the condition. */
10155 finish_if_stmt_cond (condition, statement);
10157 /* Parse the then-clause. */
10158 in_statement = parser->in_statement;
10159 parser->in_statement |= IN_IF_STMT;
10160 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10162 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10163 add_stmt (build_empty_stmt (loc));
10164 cp_lexer_consume_token (parser->lexer);
10165 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10166 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10167 "empty body in an %<if%> statement");
10168 nested_if = false;
10170 else
10171 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10172 token->location, "if");
10173 parser->in_statement = in_statement;
10175 finish_then_clause (statement);
10177 /* If the next token is `else', parse the else-clause. */
10178 if (cp_lexer_next_token_is_keyword (parser->lexer,
10179 RID_ELSE))
10181 /* Consume the `else' keyword. */
10182 location_t else_tok_loc
10183 = cp_lexer_consume_token (parser->lexer)->location;
10184 begin_else_clause (statement);
10185 /* Parse the else-clause. */
10186 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10188 location_t loc;
10189 loc = cp_lexer_peek_token (parser->lexer)->location;
10190 warning_at (loc,
10191 OPT_Wempty_body, "suggest braces around "
10192 "empty body in an %<else%> statement");
10193 add_stmt (build_empty_stmt (loc));
10194 cp_lexer_consume_token (parser->lexer);
10196 else
10197 cp_parser_implicitly_scoped_statement (parser, NULL,
10198 else_tok_loc, "else");
10200 finish_else_clause (statement);
10202 /* If we are currently parsing a then-clause, then
10203 IF_P will not be NULL. We set it to true to
10204 indicate that this if statement has an else clause.
10205 This may trigger the Wparentheses warning below
10206 when we get back up to the parent if statement. */
10207 if (if_p != NULL)
10208 *if_p = true;
10210 else
10212 /* This if statement does not have an else clause. If
10213 NESTED_IF is true, then the then-clause is an if
10214 statement which does have an else clause. We warn
10215 about the potential ambiguity. */
10216 if (nested_if)
10217 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10218 "suggest explicit braces to avoid ambiguous"
10219 " %<else%>");
10222 /* Now we're all done with the if-statement. */
10223 finish_if_stmt (statement);
10225 else
10227 bool in_switch_statement_p;
10228 unsigned char in_statement;
10230 /* Add the condition. */
10231 finish_switch_cond (condition, statement);
10233 /* Parse the body of the switch-statement. */
10234 in_switch_statement_p = parser->in_switch_statement_p;
10235 in_statement = parser->in_statement;
10236 parser->in_switch_statement_p = true;
10237 parser->in_statement |= IN_SWITCH_STMT;
10238 cp_parser_implicitly_scoped_statement (parser, NULL,
10239 0, "switch");
10240 parser->in_switch_statement_p = in_switch_statement_p;
10241 parser->in_statement = in_statement;
10243 /* Now we're all done with the switch-statement. */
10244 finish_switch_stmt (statement);
10247 return statement;
10249 break;
10251 default:
10252 cp_parser_error (parser, "expected selection-statement");
10253 return error_mark_node;
10257 /* Parse a condition.
10259 condition:
10260 expression
10261 type-specifier-seq declarator = initializer-clause
10262 type-specifier-seq declarator braced-init-list
10264 GNU Extension:
10266 condition:
10267 type-specifier-seq declarator asm-specification [opt]
10268 attributes [opt] = assignment-expression
10270 Returns the expression that should be tested. */
10272 static tree
10273 cp_parser_condition (cp_parser* parser)
10275 cp_decl_specifier_seq type_specifiers;
10276 const char *saved_message;
10277 int declares_class_or_enum;
10279 /* Try the declaration first. */
10280 cp_parser_parse_tentatively (parser);
10281 /* New types are not allowed in the type-specifier-seq for a
10282 condition. */
10283 saved_message = parser->type_definition_forbidden_message;
10284 parser->type_definition_forbidden_message
10285 = G_("types may not be defined in conditions");
10286 /* Parse the type-specifier-seq. */
10287 cp_parser_decl_specifier_seq (parser,
10288 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10289 &type_specifiers,
10290 &declares_class_or_enum);
10291 /* Restore the saved message. */
10292 parser->type_definition_forbidden_message = saved_message;
10293 /* If all is well, we might be looking at a declaration. */
10294 if (!cp_parser_error_occurred (parser))
10296 tree decl;
10297 tree asm_specification;
10298 tree attributes;
10299 cp_declarator *declarator;
10300 tree initializer = NULL_TREE;
10302 /* Parse the declarator. */
10303 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10304 /*ctor_dtor_or_conv_p=*/NULL,
10305 /*parenthesized_p=*/NULL,
10306 /*member_p=*/false,
10307 /*friend_p=*/false);
10308 /* Parse the attributes. */
10309 attributes = cp_parser_attributes_opt (parser);
10310 /* Parse the asm-specification. */
10311 asm_specification = cp_parser_asm_specification_opt (parser);
10312 /* If the next token is not an `=' or '{', then we might still be
10313 looking at an expression. For example:
10315 if (A(a).x)
10317 looks like a decl-specifier-seq and a declarator -- but then
10318 there is no `=', so this is an expression. */
10319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10320 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10321 cp_parser_simulate_error (parser);
10323 /* If we did see an `=' or '{', then we are looking at a declaration
10324 for sure. */
10325 if (cp_parser_parse_definitely (parser))
10327 tree pushed_scope;
10328 bool non_constant_p;
10329 bool flags = LOOKUP_ONLYCONVERTING;
10331 /* Create the declaration. */
10332 decl = start_decl (declarator, &type_specifiers,
10333 /*initialized_p=*/true,
10334 attributes, /*prefix_attributes=*/NULL_TREE,
10335 &pushed_scope);
10337 /* Parse the initializer. */
10338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10340 initializer = cp_parser_braced_list (parser, &non_constant_p);
10341 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10342 flags = 0;
10344 else
10346 /* Consume the `='. */
10347 cp_parser_require (parser, CPP_EQ, RT_EQ);
10348 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10350 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10351 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10353 /* Process the initializer. */
10354 cp_finish_decl (decl,
10355 initializer, !non_constant_p,
10356 asm_specification,
10357 flags);
10359 if (pushed_scope)
10360 pop_scope (pushed_scope);
10362 return convert_from_reference (decl);
10365 /* If we didn't even get past the declarator successfully, we are
10366 definitely not looking at a declaration. */
10367 else
10368 cp_parser_abort_tentative_parse (parser);
10370 /* Otherwise, we are looking at an expression. */
10371 return cp_parser_expression (parser);
10374 /* Parses a for-statement or range-for-statement until the closing ')',
10375 not included. */
10377 static tree
10378 cp_parser_for (cp_parser *parser, bool ivdep)
10380 tree init, scope, decl;
10381 bool is_range_for;
10383 /* Begin the for-statement. */
10384 scope = begin_for_scope (&init);
10386 /* Parse the initialization. */
10387 is_range_for = cp_parser_for_init_statement (parser, &decl);
10389 if (is_range_for)
10390 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10391 else
10392 return cp_parser_c_for (parser, scope, init, ivdep);
10395 static tree
10396 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10398 /* Normal for loop */
10399 tree condition = NULL_TREE;
10400 tree expression = NULL_TREE;
10401 tree stmt;
10403 stmt = begin_for_stmt (scope, init);
10404 /* The for-init-statement has already been parsed in
10405 cp_parser_for_init_statement, so no work is needed here. */
10406 finish_for_init_stmt (stmt);
10408 /* If there's a condition, process it. */
10409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10410 condition = cp_parser_condition (parser);
10411 else if (ivdep)
10413 cp_parser_error (parser, "missing loop condition in loop with "
10414 "%<GCC ivdep%> pragma");
10415 condition = error_mark_node;
10417 finish_for_cond (condition, stmt, ivdep);
10418 /* Look for the `;'. */
10419 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10421 /* If there's an expression, process it. */
10422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10423 expression = cp_parser_expression (parser);
10424 finish_for_expr (expression, stmt);
10426 return stmt;
10429 /* Tries to parse a range-based for-statement:
10431 range-based-for:
10432 decl-specifier-seq declarator : expression
10434 The decl-specifier-seq declarator and the `:' are already parsed by
10435 cp_parser_for_init_statement. If processing_template_decl it returns a
10436 newly created RANGE_FOR_STMT; if not, it is converted to a
10437 regular FOR_STMT. */
10439 static tree
10440 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10441 bool ivdep)
10443 tree stmt, range_expr;
10445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10447 bool expr_non_constant_p;
10448 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10450 else
10451 range_expr = cp_parser_expression (parser);
10453 /* If in template, STMT is converted to a normal for-statement
10454 at instantiation. If not, it is done just ahead. */
10455 if (processing_template_decl)
10457 if (check_for_bare_parameter_packs (range_expr))
10458 range_expr = error_mark_node;
10459 stmt = begin_range_for_stmt (scope, init);
10460 if (ivdep)
10461 RANGE_FOR_IVDEP (stmt) = 1;
10462 finish_range_for_decl (stmt, range_decl, range_expr);
10463 if (!type_dependent_expression_p (range_expr)
10464 /* do_auto_deduction doesn't mess with template init-lists. */
10465 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10466 do_range_for_auto_deduction (range_decl, range_expr);
10468 else
10470 stmt = begin_for_stmt (scope, init);
10471 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10473 return stmt;
10476 /* Subroutine of cp_convert_range_for: given the initializer expression,
10477 builds up the range temporary. */
10479 static tree
10480 build_range_temp (tree range_expr)
10482 tree range_type, range_temp;
10484 /* Find out the type deduced by the declaration
10485 `auto &&__range = range_expr'. */
10486 range_type = cp_build_reference_type (make_auto (), true);
10487 range_type = do_auto_deduction (range_type, range_expr,
10488 type_uses_auto (range_type));
10490 /* Create the __range variable. */
10491 range_temp = build_decl (input_location, VAR_DECL,
10492 get_identifier ("__for_range"), range_type);
10493 TREE_USED (range_temp) = 1;
10494 DECL_ARTIFICIAL (range_temp) = 1;
10496 return range_temp;
10499 /* Used by cp_parser_range_for in template context: we aren't going to
10500 do a full conversion yet, but we still need to resolve auto in the
10501 type of the for-range-declaration if present. This is basically
10502 a shortcut version of cp_convert_range_for. */
10504 static void
10505 do_range_for_auto_deduction (tree decl, tree range_expr)
10507 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10508 if (auto_node)
10510 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10511 range_temp = convert_from_reference (build_range_temp (range_expr));
10512 iter_type = (cp_parser_perform_range_for_lookup
10513 (range_temp, &begin_dummy, &end_dummy));
10514 if (iter_type)
10516 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10517 iter_type);
10518 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10519 tf_warning_or_error);
10520 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10521 iter_decl, auto_node);
10526 /* Converts a range-based for-statement into a normal
10527 for-statement, as per the definition.
10529 for (RANGE_DECL : RANGE_EXPR)
10530 BLOCK
10532 should be equivalent to:
10535 auto &&__range = RANGE_EXPR;
10536 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10537 __begin != __end;
10538 ++__begin)
10540 RANGE_DECL = *__begin;
10541 BLOCK
10545 If RANGE_EXPR is an array:
10546 BEGIN_EXPR = __range
10547 END_EXPR = __range + ARRAY_SIZE(__range)
10548 Else if RANGE_EXPR has a member 'begin' or 'end':
10549 BEGIN_EXPR = __range.begin()
10550 END_EXPR = __range.end()
10551 Else:
10552 BEGIN_EXPR = begin(__range)
10553 END_EXPR = end(__range);
10555 If __range has a member 'begin' but not 'end', or vice versa, we must
10556 still use the second alternative (it will surely fail, however).
10557 When calling begin()/end() in the third alternative we must use
10558 argument dependent lookup, but always considering 'std' as an associated
10559 namespace. */
10561 tree
10562 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10563 bool ivdep)
10565 tree begin, end;
10566 tree iter_type, begin_expr, end_expr;
10567 tree condition, expression;
10569 if (range_decl == error_mark_node || range_expr == error_mark_node)
10570 /* If an error happened previously do nothing or else a lot of
10571 unhelpful errors would be issued. */
10572 begin_expr = end_expr = iter_type = error_mark_node;
10573 else
10575 tree range_temp;
10577 if (VAR_P (range_expr)
10578 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10579 /* Can't bind a reference to an array of runtime bound. */
10580 range_temp = range_expr;
10581 else
10583 range_temp = build_range_temp (range_expr);
10584 pushdecl (range_temp);
10585 cp_finish_decl (range_temp, range_expr,
10586 /*is_constant_init*/false, NULL_TREE,
10587 LOOKUP_ONLYCONVERTING);
10588 range_temp = convert_from_reference (range_temp);
10590 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10591 &begin_expr, &end_expr);
10594 /* The new for initialization statement. */
10595 begin = build_decl (input_location, VAR_DECL,
10596 get_identifier ("__for_begin"), iter_type);
10597 TREE_USED (begin) = 1;
10598 DECL_ARTIFICIAL (begin) = 1;
10599 pushdecl (begin);
10600 cp_finish_decl (begin, begin_expr,
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10604 end = build_decl (input_location, VAR_DECL,
10605 get_identifier ("__for_end"), iter_type);
10606 TREE_USED (end) = 1;
10607 DECL_ARTIFICIAL (end) = 1;
10608 pushdecl (end);
10609 cp_finish_decl (end, end_expr,
10610 /*is_constant_init*/false, NULL_TREE,
10611 LOOKUP_ONLYCONVERTING);
10613 finish_for_init_stmt (statement);
10615 /* The new for condition. */
10616 condition = build_x_binary_op (input_location, NE_EXPR,
10617 begin, ERROR_MARK,
10618 end, ERROR_MARK,
10619 NULL, tf_warning_or_error);
10620 finish_for_cond (condition, statement, ivdep);
10622 /* The new increment expression. */
10623 expression = finish_unary_op_expr (input_location,
10624 PREINCREMENT_EXPR, begin,
10625 tf_warning_or_error);
10626 finish_for_expr (expression, statement);
10628 /* The declaration is initialized with *__begin inside the loop body. */
10629 cp_finish_decl (range_decl,
10630 build_x_indirect_ref (input_location, begin, RO_NULL,
10631 tf_warning_or_error),
10632 /*is_constant_init*/false, NULL_TREE,
10633 LOOKUP_ONLYCONVERTING);
10635 return statement;
10638 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10639 We need to solve both at the same time because the method used
10640 depends on the existence of members begin or end.
10641 Returns the type deduced for the iterator expression. */
10643 static tree
10644 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10646 if (error_operand_p (range))
10648 *begin = *end = error_mark_node;
10649 return error_mark_node;
10652 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10654 error ("range-based %<for%> expression of type %qT "
10655 "has incomplete type", TREE_TYPE (range));
10656 *begin = *end = error_mark_node;
10657 return error_mark_node;
10659 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10661 /* If RANGE is an array, we will use pointer arithmetic. */
10662 *begin = range;
10663 *end = build_binary_op (input_location, PLUS_EXPR,
10664 range,
10665 array_type_nelts_top (TREE_TYPE (range)),
10667 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10669 else
10671 /* If it is not an array, we must do a bit of magic. */
10672 tree id_begin, id_end;
10673 tree member_begin, member_end;
10675 *begin = *end = error_mark_node;
10677 id_begin = get_identifier ("begin");
10678 id_end = get_identifier ("end");
10679 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10680 /*protect=*/2, /*want_type=*/false,
10681 tf_warning_or_error);
10682 member_end = lookup_member (TREE_TYPE (range), id_end,
10683 /*protect=*/2, /*want_type=*/false,
10684 tf_warning_or_error);
10686 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10688 /* Use the member functions. */
10689 if (member_begin != NULL_TREE)
10690 *begin = cp_parser_range_for_member_function (range, id_begin);
10691 else
10692 error ("range-based %<for%> expression of type %qT has an "
10693 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10695 if (member_end != NULL_TREE)
10696 *end = cp_parser_range_for_member_function (range, id_end);
10697 else
10698 error ("range-based %<for%> expression of type %qT has a "
10699 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10701 else
10703 /* Use global functions with ADL. */
10704 vec<tree, va_gc> *vec;
10705 vec = make_tree_vector ();
10707 vec_safe_push (vec, range);
10709 member_begin = perform_koenig_lookup (id_begin, vec,
10710 tf_warning_or_error);
10711 *begin = finish_call_expr (member_begin, &vec, false, true,
10712 tf_warning_or_error);
10713 member_end = perform_koenig_lookup (id_end, vec,
10714 tf_warning_or_error);
10715 *end = finish_call_expr (member_end, &vec, false, true,
10716 tf_warning_or_error);
10718 release_tree_vector (vec);
10721 /* Last common checks. */
10722 if (*begin == error_mark_node || *end == error_mark_node)
10724 /* If one of the expressions is an error do no more checks. */
10725 *begin = *end = error_mark_node;
10726 return error_mark_node;
10728 else if (type_dependent_expression_p (*begin)
10729 || type_dependent_expression_p (*end))
10730 /* Can happen, when, eg, in a template context, Koenig lookup
10731 can't resolve begin/end (c++/58503). */
10732 return NULL_TREE;
10733 else
10735 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10736 /* The unqualified type of the __begin and __end temporaries should
10737 be the same, as required by the multiple auto declaration. */
10738 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10739 error ("inconsistent begin/end types in range-based %<for%> "
10740 "statement: %qT and %qT",
10741 TREE_TYPE (*begin), TREE_TYPE (*end));
10742 return iter_type;
10747 /* Helper function for cp_parser_perform_range_for_lookup.
10748 Builds a tree for RANGE.IDENTIFIER(). */
10750 static tree
10751 cp_parser_range_for_member_function (tree range, tree identifier)
10753 tree member, res;
10754 vec<tree, va_gc> *vec;
10756 member = finish_class_member_access_expr (range, identifier,
10757 false, tf_warning_or_error);
10758 if (member == error_mark_node)
10759 return error_mark_node;
10761 vec = make_tree_vector ();
10762 res = finish_call_expr (member, &vec,
10763 /*disallow_virtual=*/false,
10764 /*koenig_p=*/false,
10765 tf_warning_or_error);
10766 release_tree_vector (vec);
10767 return res;
10770 /* Parse an iteration-statement.
10772 iteration-statement:
10773 while ( condition ) statement
10774 do statement while ( expression ) ;
10775 for ( for-init-statement condition [opt] ; expression [opt] )
10776 statement
10778 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10780 static tree
10781 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10783 cp_token *token;
10784 location_t tok_loc;
10785 enum rid keyword;
10786 tree statement;
10787 unsigned char in_statement;
10789 /* Peek at the next token. */
10790 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10791 if (!token)
10792 return error_mark_node;
10794 tok_loc = token->location;
10796 /* Remember whether or not we are already within an iteration
10797 statement. */
10798 in_statement = parser->in_statement;
10800 /* See what kind of keyword it is. */
10801 keyword = token->keyword;
10802 switch (keyword)
10804 case RID_WHILE:
10806 tree condition;
10808 /* Begin the while-statement. */
10809 statement = begin_while_stmt ();
10810 /* Look for the `('. */
10811 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10812 /* Parse the condition. */
10813 condition = cp_parser_condition (parser);
10814 finish_while_stmt_cond (condition, statement, ivdep);
10815 /* Look for the `)'. */
10816 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10817 /* Parse the dependent statement. */
10818 parser->in_statement = IN_ITERATION_STMT;
10819 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10820 parser->in_statement = in_statement;
10821 /* We're done with the while-statement. */
10822 finish_while_stmt (statement);
10824 break;
10826 case RID_DO:
10828 tree expression;
10830 /* Begin the do-statement. */
10831 statement = begin_do_stmt ();
10832 /* Parse the body of the do-statement. */
10833 parser->in_statement = IN_ITERATION_STMT;
10834 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10835 parser->in_statement = in_statement;
10836 finish_do_body (statement);
10837 /* Look for the `while' keyword. */
10838 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10839 /* Look for the `('. */
10840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10841 /* Parse the expression. */
10842 expression = cp_parser_expression (parser);
10843 /* We're done with the do-statement. */
10844 finish_do_stmt (expression, statement, ivdep);
10845 /* Look for the `)'. */
10846 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10847 /* Look for the `;'. */
10848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10850 break;
10852 case RID_FOR:
10854 /* Look for the `('. */
10855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10857 statement = cp_parser_for (parser, ivdep);
10859 /* Look for the `)'. */
10860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10862 /* Parse the body of the for-statement. */
10863 parser->in_statement = IN_ITERATION_STMT;
10864 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10865 parser->in_statement = in_statement;
10867 /* We're done with the for-statement. */
10868 finish_for_stmt (statement);
10870 break;
10872 default:
10873 cp_parser_error (parser, "expected iteration-statement");
10874 statement = error_mark_node;
10875 break;
10878 return statement;
10881 /* Parse a for-init-statement or the declarator of a range-based-for.
10882 Returns true if a range-based-for declaration is seen.
10884 for-init-statement:
10885 expression-statement
10886 simple-declaration */
10888 static bool
10889 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10891 /* If the next token is a `;', then we have an empty
10892 expression-statement. Grammatically, this is also a
10893 simple-declaration, but an invalid one, because it does not
10894 declare anything. Therefore, if we did not handle this case
10895 specially, we would issue an error message about an invalid
10896 declaration. */
10897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10899 bool is_range_for = false;
10900 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10902 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10903 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10905 /* N3994 -- for (id : init) ... */
10906 if (cxx_dialect < cxx1z)
10907 pedwarn (input_location, 0, "range-based for loop without a "
10908 "type-specifier only available with "
10909 "-std=c++1z or -std=gnu++1z");
10910 tree name = cp_parser_identifier (parser);
10911 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10912 *decl = build_decl (input_location, VAR_DECL, name, type);
10913 pushdecl (*decl);
10914 cp_lexer_consume_token (parser->lexer);
10915 return true;
10918 /* A colon is used in range-based for. */
10919 parser->colon_corrects_to_scope_p = false;
10921 /* We're going to speculatively look for a declaration, falling back
10922 to an expression, if necessary. */
10923 cp_parser_parse_tentatively (parser);
10924 /* Parse the declaration. */
10925 cp_parser_simple_declaration (parser,
10926 /*function_definition_allowed_p=*/false,
10927 decl);
10928 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10931 /* It is a range-for, consume the ':' */
10932 cp_lexer_consume_token (parser->lexer);
10933 is_range_for = true;
10934 if (cxx_dialect < cxx11)
10936 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10937 "range-based %<for%> loops only available with "
10938 "-std=c++11 or -std=gnu++11");
10939 *decl = error_mark_node;
10942 else
10943 /* The ';' is not consumed yet because we told
10944 cp_parser_simple_declaration not to. */
10945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10947 if (cp_parser_parse_definitely (parser))
10948 return is_range_for;
10949 /* If the tentative parse failed, then we shall need to look for an
10950 expression-statement. */
10952 /* If we are here, it is an expression-statement. */
10953 cp_parser_expression_statement (parser, NULL_TREE);
10954 return false;
10957 /* Parse a jump-statement.
10959 jump-statement:
10960 break ;
10961 continue ;
10962 return expression [opt] ;
10963 return braced-init-list ;
10964 goto identifier ;
10966 GNU extension:
10968 jump-statement:
10969 goto * expression ;
10971 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10973 static tree
10974 cp_parser_jump_statement (cp_parser* parser)
10976 tree statement = error_mark_node;
10977 cp_token *token;
10978 enum rid keyword;
10979 unsigned char in_statement;
10981 /* Peek at the next token. */
10982 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10983 if (!token)
10984 return error_mark_node;
10986 /* See what kind of keyword it is. */
10987 keyword = token->keyword;
10988 switch (keyword)
10990 case RID_BREAK:
10991 in_statement = parser->in_statement & ~IN_IF_STMT;
10992 switch (in_statement)
10994 case 0:
10995 error_at (token->location, "break statement not within loop or switch");
10996 break;
10997 default:
10998 gcc_assert ((in_statement & IN_SWITCH_STMT)
10999 || in_statement == IN_ITERATION_STMT);
11000 statement = finish_break_stmt ();
11001 if (in_statement == IN_ITERATION_STMT)
11002 break_maybe_infinite_loop ();
11003 break;
11004 case IN_OMP_BLOCK:
11005 error_at (token->location, "invalid exit from OpenMP structured block");
11006 break;
11007 case IN_OMP_FOR:
11008 error_at (token->location, "break statement used with OpenMP for loop");
11009 break;
11010 case IN_CILK_SIMD_FOR:
11011 error_at (token->location, "break statement used with Cilk Plus for loop");
11012 break;
11014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11015 break;
11017 case RID_CONTINUE:
11018 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11020 case 0:
11021 error_at (token->location, "continue statement not within a loop");
11022 break;
11023 case IN_CILK_SIMD_FOR:
11024 error_at (token->location,
11025 "continue statement within %<#pragma simd%> loop body");
11026 /* Fall through. */
11027 case IN_ITERATION_STMT:
11028 case IN_OMP_FOR:
11029 statement = finish_continue_stmt ();
11030 break;
11031 case IN_OMP_BLOCK:
11032 error_at (token->location, "invalid exit from OpenMP structured block");
11033 break;
11034 default:
11035 gcc_unreachable ();
11037 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11038 break;
11040 case RID_RETURN:
11042 tree expr;
11043 bool expr_non_constant_p;
11045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11047 cp_lexer_set_source_position (parser->lexer);
11048 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11049 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11051 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11052 expr = cp_parser_expression (parser);
11053 else
11054 /* If the next token is a `;', then there is no
11055 expression. */
11056 expr = NULL_TREE;
11057 /* Build the return-statement. */
11058 statement = finish_return_stmt (expr);
11059 /* Look for the final `;'. */
11060 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11062 break;
11064 case RID_GOTO:
11065 if (parser->in_function_body
11066 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11068 error ("%<goto%> in %<constexpr%> function");
11069 cp_function_chain->invalid_constexpr = true;
11072 /* Create the goto-statement. */
11073 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11075 /* Issue a warning about this use of a GNU extension. */
11076 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11077 /* Consume the '*' token. */
11078 cp_lexer_consume_token (parser->lexer);
11079 /* Parse the dependent expression. */
11080 finish_goto_stmt (cp_parser_expression (parser));
11082 else
11083 finish_goto_stmt (cp_parser_identifier (parser));
11084 /* Look for the final `;'. */
11085 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11086 break;
11088 default:
11089 cp_parser_error (parser, "expected jump-statement");
11090 break;
11093 return statement;
11096 /* Parse a declaration-statement.
11098 declaration-statement:
11099 block-declaration */
11101 static void
11102 cp_parser_declaration_statement (cp_parser* parser)
11104 void *p;
11106 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11107 p = obstack_alloc (&declarator_obstack, 0);
11109 /* Parse the block-declaration. */
11110 cp_parser_block_declaration (parser, /*statement_p=*/true);
11112 /* Free any declarators allocated. */
11113 obstack_free (&declarator_obstack, p);
11116 /* Some dependent statements (like `if (cond) statement'), are
11117 implicitly in their own scope. In other words, if the statement is
11118 a single statement (as opposed to a compound-statement), it is
11119 none-the-less treated as if it were enclosed in braces. Any
11120 declarations appearing in the dependent statement are out of scope
11121 after control passes that point. This function parses a statement,
11122 but ensures that is in its own scope, even if it is not a
11123 compound-statement.
11125 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11126 is a (possibly labeled) if statement which is not enclosed in
11127 braces and has an else clause. This is used to implement
11128 -Wparentheses.
11130 Returns the new statement. */
11132 static tree
11133 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11134 location_t guard_loc,
11135 const char *guard_kind)
11137 tree statement;
11139 if (if_p != NULL)
11140 *if_p = false;
11142 /* Mark if () ; with a special NOP_EXPR. */
11143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11145 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11146 cp_lexer_consume_token (parser->lexer);
11147 statement = add_stmt (build_empty_stmt (loc));
11149 /* if a compound is opened, we simply parse the statement directly. */
11150 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11151 statement = cp_parser_compound_statement (parser, NULL, false, false);
11152 /* If the token is not a `{', then we must take special action. */
11153 else
11155 /* Create a compound-statement. */
11156 statement = begin_compound_stmt (0);
11157 /* Parse the dependent-statement. */
11158 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11159 cp_parser_statement (parser, NULL_TREE, false, if_p);
11160 /* Finish the dummy compound-statement. */
11161 finish_compound_stmt (statement);
11162 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11163 if (next_tok->keyword != RID_ELSE)
11165 location_t next_stmt_loc = next_tok->location;
11166 warn_for_misleading_indentation (guard_loc, body_loc,
11167 next_stmt_loc, next_tok->type,
11168 guard_kind);
11172 /* Return the statement. */
11173 return statement;
11176 /* For some dependent statements (like `while (cond) statement'), we
11177 have already created a scope. Therefore, even if the dependent
11178 statement is a compound-statement, we do not want to create another
11179 scope. */
11181 static void
11182 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11183 const char *guard_kind)
11185 /* If the token is a `{', then we must take special action. */
11186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11188 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11189 cp_parser_statement (parser, NULL_TREE, false, NULL);
11190 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11191 location_t next_stmt_loc = next_tok->location;
11192 warn_for_misleading_indentation (guard_loc, body_loc,
11193 next_stmt_loc, next_tok->type,
11194 guard_kind);
11196 else
11198 /* Avoid calling cp_parser_compound_statement, so that we
11199 don't create a new scope. Do everything else by hand. */
11200 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11201 /* If the next keyword is `__label__' we have a label declaration. */
11202 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11203 cp_parser_label_declaration (parser);
11204 /* Parse an (optional) statement-seq. */
11205 cp_parser_statement_seq_opt (parser, NULL_TREE);
11206 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11210 /* Declarations [gram.dcl.dcl] */
11212 /* Parse an optional declaration-sequence.
11214 declaration-seq:
11215 declaration
11216 declaration-seq declaration */
11218 static void
11219 cp_parser_declaration_seq_opt (cp_parser* parser)
11221 while (true)
11223 cp_token *token;
11225 token = cp_lexer_peek_token (parser->lexer);
11227 if (token->type == CPP_CLOSE_BRACE
11228 || token->type == CPP_EOF
11229 || token->type == CPP_PRAGMA_EOL)
11230 break;
11232 if (token->type == CPP_SEMICOLON)
11234 /* A declaration consisting of a single semicolon is
11235 invalid. Allow it unless we're being pedantic. */
11236 cp_lexer_consume_token (parser->lexer);
11237 if (!in_system_header_at (input_location))
11238 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11239 continue;
11242 /* If we're entering or exiting a region that's implicitly
11243 extern "C", modify the lang context appropriately. */
11244 if (!parser->implicit_extern_c && token->implicit_extern_c)
11246 push_lang_context (lang_name_c);
11247 parser->implicit_extern_c = true;
11249 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11251 pop_lang_context ();
11252 parser->implicit_extern_c = false;
11255 if (token->type == CPP_PRAGMA)
11257 /* A top-level declaration can consist solely of a #pragma.
11258 A nested declaration cannot, so this is done here and not
11259 in cp_parser_declaration. (A #pragma at block scope is
11260 handled in cp_parser_statement.) */
11261 cp_parser_pragma (parser, pragma_external);
11262 continue;
11265 /* Parse the declaration itself. */
11266 cp_parser_declaration (parser);
11270 /* Parse a declaration.
11272 declaration:
11273 block-declaration
11274 function-definition
11275 template-declaration
11276 explicit-instantiation
11277 explicit-specialization
11278 linkage-specification
11279 namespace-definition
11281 GNU extension:
11283 declaration:
11284 __extension__ declaration */
11286 static void
11287 cp_parser_declaration (cp_parser* parser)
11289 cp_token token1;
11290 cp_token token2;
11291 int saved_pedantic;
11292 void *p;
11293 tree attributes = NULL_TREE;
11295 /* Check for the `__extension__' keyword. */
11296 if (cp_parser_extension_opt (parser, &saved_pedantic))
11298 /* Parse the qualified declaration. */
11299 cp_parser_declaration (parser);
11300 /* Restore the PEDANTIC flag. */
11301 pedantic = saved_pedantic;
11303 return;
11306 /* Try to figure out what kind of declaration is present. */
11307 token1 = *cp_lexer_peek_token (parser->lexer);
11309 if (token1.type != CPP_EOF)
11310 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11311 else
11313 token2.type = CPP_EOF;
11314 token2.keyword = RID_MAX;
11317 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11318 p = obstack_alloc (&declarator_obstack, 0);
11320 /* If the next token is `extern' and the following token is a string
11321 literal, then we have a linkage specification. */
11322 if (token1.keyword == RID_EXTERN
11323 && cp_parser_is_pure_string_literal (&token2))
11324 cp_parser_linkage_specification (parser);
11325 /* If the next token is `template', then we have either a template
11326 declaration, an explicit instantiation, or an explicit
11327 specialization. */
11328 else if (token1.keyword == RID_TEMPLATE)
11330 /* `template <>' indicates a template specialization. */
11331 if (token2.type == CPP_LESS
11332 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11333 cp_parser_explicit_specialization (parser);
11334 /* `template <' indicates a template declaration. */
11335 else if (token2.type == CPP_LESS)
11336 cp_parser_template_declaration (parser, /*member_p=*/false);
11337 /* Anything else must be an explicit instantiation. */
11338 else
11339 cp_parser_explicit_instantiation (parser);
11341 /* If the next token is `export', then we have a template
11342 declaration. */
11343 else if (token1.keyword == RID_EXPORT)
11344 cp_parser_template_declaration (parser, /*member_p=*/false);
11345 /* If the next token is `extern', 'static' or 'inline' and the one
11346 after that is `template', we have a GNU extended explicit
11347 instantiation directive. */
11348 else if (cp_parser_allow_gnu_extensions_p (parser)
11349 && (token1.keyword == RID_EXTERN
11350 || token1.keyword == RID_STATIC
11351 || token1.keyword == RID_INLINE)
11352 && token2.keyword == RID_TEMPLATE)
11353 cp_parser_explicit_instantiation (parser);
11354 /* If the next token is `namespace', check for a named or unnamed
11355 namespace definition. */
11356 else if (token1.keyword == RID_NAMESPACE
11357 && (/* A named namespace definition. */
11358 (token2.type == CPP_NAME
11359 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11360 != CPP_EQ))
11361 /* An unnamed namespace definition. */
11362 || token2.type == CPP_OPEN_BRACE
11363 || token2.keyword == RID_ATTRIBUTE))
11364 cp_parser_namespace_definition (parser);
11365 /* An inline (associated) namespace definition. */
11366 else if (token1.keyword == RID_INLINE
11367 && token2.keyword == RID_NAMESPACE)
11368 cp_parser_namespace_definition (parser);
11369 /* Objective-C++ declaration/definition. */
11370 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11371 cp_parser_objc_declaration (parser, NULL_TREE);
11372 else if (c_dialect_objc ()
11373 && token1.keyword == RID_ATTRIBUTE
11374 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11375 cp_parser_objc_declaration (parser, attributes);
11376 /* We must have either a block declaration or a function
11377 definition. */
11378 else
11379 /* Try to parse a block-declaration, or a function-definition. */
11380 cp_parser_block_declaration (parser, /*statement_p=*/false);
11382 /* Free any declarators allocated. */
11383 obstack_free (&declarator_obstack, p);
11386 /* Parse a block-declaration.
11388 block-declaration:
11389 simple-declaration
11390 asm-definition
11391 namespace-alias-definition
11392 using-declaration
11393 using-directive
11395 GNU Extension:
11397 block-declaration:
11398 __extension__ block-declaration
11400 C++0x Extension:
11402 block-declaration:
11403 static_assert-declaration
11405 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11406 part of a declaration-statement. */
11408 static void
11409 cp_parser_block_declaration (cp_parser *parser,
11410 bool statement_p)
11412 cp_token *token1;
11413 int saved_pedantic;
11415 /* Check for the `__extension__' keyword. */
11416 if (cp_parser_extension_opt (parser, &saved_pedantic))
11418 /* Parse the qualified declaration. */
11419 cp_parser_block_declaration (parser, statement_p);
11420 /* Restore the PEDANTIC flag. */
11421 pedantic = saved_pedantic;
11423 return;
11426 /* Peek at the next token to figure out which kind of declaration is
11427 present. */
11428 token1 = cp_lexer_peek_token (parser->lexer);
11430 /* If the next keyword is `asm', we have an asm-definition. */
11431 if (token1->keyword == RID_ASM)
11433 if (statement_p)
11434 cp_parser_commit_to_tentative_parse (parser);
11435 cp_parser_asm_definition (parser);
11437 /* If the next keyword is `namespace', we have a
11438 namespace-alias-definition. */
11439 else if (token1->keyword == RID_NAMESPACE)
11440 cp_parser_namespace_alias_definition (parser);
11441 /* If the next keyword is `using', we have a
11442 using-declaration, a using-directive, or an alias-declaration. */
11443 else if (token1->keyword == RID_USING)
11445 cp_token *token2;
11447 if (statement_p)
11448 cp_parser_commit_to_tentative_parse (parser);
11449 /* If the token after `using' is `namespace', then we have a
11450 using-directive. */
11451 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11452 if (token2->keyword == RID_NAMESPACE)
11453 cp_parser_using_directive (parser);
11454 /* If the second token after 'using' is '=', then we have an
11455 alias-declaration. */
11456 else if (cxx_dialect >= cxx11
11457 && token2->type == CPP_NAME
11458 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11459 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11460 cp_parser_alias_declaration (parser);
11461 /* Otherwise, it's a using-declaration. */
11462 else
11463 cp_parser_using_declaration (parser,
11464 /*access_declaration_p=*/false);
11466 /* If the next keyword is `__label__' we have a misplaced label
11467 declaration. */
11468 else if (token1->keyword == RID_LABEL)
11470 cp_lexer_consume_token (parser->lexer);
11471 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11472 cp_parser_skip_to_end_of_statement (parser);
11473 /* If the next token is now a `;', consume it. */
11474 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11475 cp_lexer_consume_token (parser->lexer);
11477 /* If the next token is `static_assert' we have a static assertion. */
11478 else if (token1->keyword == RID_STATIC_ASSERT)
11479 cp_parser_static_assert (parser, /*member_p=*/false);
11480 /* Anything else must be a simple-declaration. */
11481 else
11482 cp_parser_simple_declaration (parser, !statement_p,
11483 /*maybe_range_for_decl*/NULL);
11486 /* Parse a simple-declaration.
11488 simple-declaration:
11489 decl-specifier-seq [opt] init-declarator-list [opt] ;
11491 init-declarator-list:
11492 init-declarator
11493 init-declarator-list , init-declarator
11495 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11496 function-definition as a simple-declaration.
11498 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11499 parsed declaration if it is an uninitialized single declarator not followed
11500 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11501 if present, will not be consumed. */
11503 static void
11504 cp_parser_simple_declaration (cp_parser* parser,
11505 bool function_definition_allowed_p,
11506 tree *maybe_range_for_decl)
11508 cp_decl_specifier_seq decl_specifiers;
11509 int declares_class_or_enum;
11510 bool saw_declarator;
11511 location_t comma_loc = UNKNOWN_LOCATION;
11512 location_t init_loc = UNKNOWN_LOCATION;
11514 if (maybe_range_for_decl)
11515 *maybe_range_for_decl = NULL_TREE;
11517 /* Defer access checks until we know what is being declared; the
11518 checks for names appearing in the decl-specifier-seq should be
11519 done as if we were in the scope of the thing being declared. */
11520 push_deferring_access_checks (dk_deferred);
11522 /* Parse the decl-specifier-seq. We have to keep track of whether
11523 or not the decl-specifier-seq declares a named class or
11524 enumeration type, since that is the only case in which the
11525 init-declarator-list is allowed to be empty.
11527 [dcl.dcl]
11529 In a simple-declaration, the optional init-declarator-list can be
11530 omitted only when declaring a class or enumeration, that is when
11531 the decl-specifier-seq contains either a class-specifier, an
11532 elaborated-type-specifier, or an enum-specifier. */
11533 cp_parser_decl_specifier_seq (parser,
11534 CP_PARSER_FLAGS_OPTIONAL,
11535 &decl_specifiers,
11536 &declares_class_or_enum);
11537 /* We no longer need to defer access checks. */
11538 stop_deferring_access_checks ();
11540 /* In a block scope, a valid declaration must always have a
11541 decl-specifier-seq. By not trying to parse declarators, we can
11542 resolve the declaration/expression ambiguity more quickly. */
11543 if (!function_definition_allowed_p
11544 && !decl_specifiers.any_specifiers_p)
11546 cp_parser_error (parser, "expected declaration");
11547 goto done;
11550 /* If the next two tokens are both identifiers, the code is
11551 erroneous. The usual cause of this situation is code like:
11553 T t;
11555 where "T" should name a type -- but does not. */
11556 if (!decl_specifiers.any_type_specifiers_p
11557 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11559 /* If parsing tentatively, we should commit; we really are
11560 looking at a declaration. */
11561 cp_parser_commit_to_tentative_parse (parser);
11562 /* Give up. */
11563 goto done;
11566 /* If we have seen at least one decl-specifier, and the next token
11567 is not a parenthesis, then we must be looking at a declaration.
11568 (After "int (" we might be looking at a functional cast.) */
11569 if (decl_specifiers.any_specifiers_p
11570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11571 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11572 && !cp_parser_error_occurred (parser))
11573 cp_parser_commit_to_tentative_parse (parser);
11575 /* Keep going until we hit the `;' at the end of the simple
11576 declaration. */
11577 saw_declarator = false;
11578 while (cp_lexer_next_token_is_not (parser->lexer,
11579 CPP_SEMICOLON))
11581 cp_token *token;
11582 bool function_definition_p;
11583 tree decl;
11585 if (saw_declarator)
11587 /* If we are processing next declarator, comma is expected */
11588 token = cp_lexer_peek_token (parser->lexer);
11589 gcc_assert (token->type == CPP_COMMA);
11590 cp_lexer_consume_token (parser->lexer);
11591 if (maybe_range_for_decl)
11593 *maybe_range_for_decl = error_mark_node;
11594 if (comma_loc == UNKNOWN_LOCATION)
11595 comma_loc = token->location;
11598 else
11599 saw_declarator = true;
11601 /* Parse the init-declarator. */
11602 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11603 /*checks=*/NULL,
11604 function_definition_allowed_p,
11605 /*member_p=*/false,
11606 declares_class_or_enum,
11607 &function_definition_p,
11608 maybe_range_for_decl,
11609 &init_loc);
11610 /* If an error occurred while parsing tentatively, exit quickly.
11611 (That usually happens when in the body of a function; each
11612 statement is treated as a declaration-statement until proven
11613 otherwise.) */
11614 if (cp_parser_error_occurred (parser))
11615 goto done;
11616 /* Handle function definitions specially. */
11617 if (function_definition_p)
11619 /* If the next token is a `,', then we are probably
11620 processing something like:
11622 void f() {}, *p;
11624 which is erroneous. */
11625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11627 cp_token *token = cp_lexer_peek_token (parser->lexer);
11628 error_at (token->location,
11629 "mixing"
11630 " declarations and function-definitions is forbidden");
11632 /* Otherwise, we're done with the list of declarators. */
11633 else
11635 pop_deferring_access_checks ();
11636 return;
11639 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11640 *maybe_range_for_decl = decl;
11641 /* The next token should be either a `,' or a `;'. */
11642 token = cp_lexer_peek_token (parser->lexer);
11643 /* If it's a `,', there are more declarators to come. */
11644 if (token->type == CPP_COMMA)
11645 /* will be consumed next time around */;
11646 /* If it's a `;', we are done. */
11647 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11648 break;
11649 /* Anything else is an error. */
11650 else
11652 /* If we have already issued an error message we don't need
11653 to issue another one. */
11654 if (decl != error_mark_node
11655 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11656 cp_parser_error (parser, "expected %<,%> or %<;%>");
11657 /* Skip tokens until we reach the end of the statement. */
11658 cp_parser_skip_to_end_of_statement (parser);
11659 /* If the next token is now a `;', consume it. */
11660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11661 cp_lexer_consume_token (parser->lexer);
11662 goto done;
11664 /* After the first time around, a function-definition is not
11665 allowed -- even if it was OK at first. For example:
11667 int i, f() {}
11669 is not valid. */
11670 function_definition_allowed_p = false;
11673 /* Issue an error message if no declarators are present, and the
11674 decl-specifier-seq does not itself declare a class or
11675 enumeration: [dcl.dcl]/3. */
11676 if (!saw_declarator)
11678 if (cp_parser_declares_only_class_p (parser))
11680 if (!declares_class_or_enum
11681 && decl_specifiers.type
11682 && OVERLOAD_TYPE_P (decl_specifiers.type))
11683 /* Ensure an error is issued anyway when finish_decltype_type,
11684 called via cp_parser_decl_specifier_seq, returns a class or
11685 an enumeration (c++/51786). */
11686 decl_specifiers.type = NULL_TREE;
11687 shadow_tag (&decl_specifiers);
11689 /* Perform any deferred access checks. */
11690 perform_deferred_access_checks (tf_warning_or_error);
11693 /* Consume the `;'. */
11694 if (!maybe_range_for_decl)
11695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11696 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11698 if (init_loc != UNKNOWN_LOCATION)
11699 error_at (init_loc, "initializer in range-based %<for%> loop");
11700 if (comma_loc != UNKNOWN_LOCATION)
11701 error_at (comma_loc,
11702 "multiple declarations in range-based %<for%> loop");
11705 done:
11706 pop_deferring_access_checks ();
11709 /* Parse a decl-specifier-seq.
11711 decl-specifier-seq:
11712 decl-specifier-seq [opt] decl-specifier
11713 decl-specifier attribute-specifier-seq [opt] (C++11)
11715 decl-specifier:
11716 storage-class-specifier
11717 type-specifier
11718 function-specifier
11719 friend
11720 typedef
11722 GNU Extension:
11724 decl-specifier:
11725 attributes
11727 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11729 The parser flags FLAGS is used to control type-specifier parsing.
11731 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11732 flags:
11734 1: one of the decl-specifiers is an elaborated-type-specifier
11735 (i.e., a type declaration)
11736 2: one of the decl-specifiers is an enum-specifier or a
11737 class-specifier (i.e., a type definition)
11741 static void
11742 cp_parser_decl_specifier_seq (cp_parser* parser,
11743 cp_parser_flags flags,
11744 cp_decl_specifier_seq *decl_specs,
11745 int* declares_class_or_enum)
11747 bool constructor_possible_p = !parser->in_declarator_p;
11748 bool found_decl_spec = false;
11749 cp_token *start_token = NULL;
11750 cp_decl_spec ds;
11752 /* Clear DECL_SPECS. */
11753 clear_decl_specs (decl_specs);
11755 /* Assume no class or enumeration type is declared. */
11756 *declares_class_or_enum = 0;
11758 /* Keep reading specifiers until there are no more to read. */
11759 while (true)
11761 bool constructor_p;
11762 cp_token *token;
11763 ds = ds_last;
11765 /* Peek at the next token. */
11766 token = cp_lexer_peek_token (parser->lexer);
11768 /* Save the first token of the decl spec list for error
11769 reporting. */
11770 if (!start_token)
11771 start_token = token;
11772 /* Handle attributes. */
11773 if (cp_next_tokens_can_be_attribute_p (parser))
11775 /* Parse the attributes. */
11776 tree attrs = cp_parser_attributes_opt (parser);
11778 /* In a sequence of declaration specifiers, c++11 attributes
11779 appertain to the type that precede them. In that case
11780 [dcl.spec]/1 says:
11782 The attribute-specifier-seq affects the type only for
11783 the declaration it appears in, not other declarations
11784 involving the same type.
11786 But for now let's force the user to position the
11787 attribute either at the beginning of the declaration or
11788 after the declarator-id, which would clearly mean that it
11789 applies to the declarator. */
11790 if (cxx11_attribute_p (attrs))
11792 if (!found_decl_spec)
11793 /* The c++11 attribute is at the beginning of the
11794 declaration. It appertains to the entity being
11795 declared. */;
11796 else
11798 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11800 /* This is an attribute following a
11801 class-specifier. */
11802 if (decl_specs->type_definition_p)
11803 warn_misplaced_attr_for_class_type (token->location,
11804 decl_specs->type);
11805 attrs = NULL_TREE;
11807 else
11809 decl_specs->std_attributes
11810 = chainon (decl_specs->std_attributes,
11811 attrs);
11812 if (decl_specs->locations[ds_std_attribute] == 0)
11813 decl_specs->locations[ds_std_attribute] = token->location;
11815 continue;
11819 decl_specs->attributes
11820 = chainon (decl_specs->attributes,
11821 attrs);
11822 if (decl_specs->locations[ds_attribute] == 0)
11823 decl_specs->locations[ds_attribute] = token->location;
11824 continue;
11826 /* Assume we will find a decl-specifier keyword. */
11827 found_decl_spec = true;
11828 /* If the next token is an appropriate keyword, we can simply
11829 add it to the list. */
11830 switch (token->keyword)
11832 /* decl-specifier:
11833 friend
11834 constexpr */
11835 case RID_FRIEND:
11836 if (!at_class_scope_p ())
11838 error_at (token->location, "%<friend%> used outside of class");
11839 cp_lexer_purge_token (parser->lexer);
11841 else
11843 ds = ds_friend;
11844 /* Consume the token. */
11845 cp_lexer_consume_token (parser->lexer);
11847 break;
11849 case RID_CONSTEXPR:
11850 ds = ds_constexpr;
11851 cp_lexer_consume_token (parser->lexer);
11852 break;
11854 /* function-specifier:
11855 inline
11856 virtual
11857 explicit */
11858 case RID_INLINE:
11859 case RID_VIRTUAL:
11860 case RID_EXPLICIT:
11861 cp_parser_function_specifier_opt (parser, decl_specs);
11862 break;
11864 /* decl-specifier:
11865 typedef */
11866 case RID_TYPEDEF:
11867 ds = ds_typedef;
11868 /* Consume the token. */
11869 cp_lexer_consume_token (parser->lexer);
11870 /* A constructor declarator cannot appear in a typedef. */
11871 constructor_possible_p = false;
11872 /* The "typedef" keyword can only occur in a declaration; we
11873 may as well commit at this point. */
11874 cp_parser_commit_to_tentative_parse (parser);
11876 if (decl_specs->storage_class != sc_none)
11877 decl_specs->conflicting_specifiers_p = true;
11878 break;
11880 /* storage-class-specifier:
11881 auto
11882 register
11883 static
11884 extern
11885 mutable
11887 GNU Extension:
11888 thread */
11889 case RID_AUTO:
11890 if (cxx_dialect == cxx98)
11892 /* Consume the token. */
11893 cp_lexer_consume_token (parser->lexer);
11895 /* Complain about `auto' as a storage specifier, if
11896 we're complaining about C++0x compatibility. */
11897 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11898 " changes meaning in C++11; please remove it");
11900 /* Set the storage class anyway. */
11901 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11902 token);
11904 else
11905 /* C++0x auto type-specifier. */
11906 found_decl_spec = false;
11907 break;
11909 case RID_REGISTER:
11910 case RID_STATIC:
11911 case RID_EXTERN:
11912 case RID_MUTABLE:
11913 /* Consume the token. */
11914 cp_lexer_consume_token (parser->lexer);
11915 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11916 token);
11917 break;
11918 case RID_THREAD:
11919 /* Consume the token. */
11920 ds = ds_thread;
11921 cp_lexer_consume_token (parser->lexer);
11922 break;
11924 default:
11925 /* We did not yet find a decl-specifier yet. */
11926 found_decl_spec = false;
11927 break;
11930 if (found_decl_spec
11931 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11932 && token->keyword != RID_CONSTEXPR)
11933 error ("decl-specifier invalid in condition");
11935 if (ds != ds_last)
11936 set_and_check_decl_spec_loc (decl_specs, ds, token);
11938 /* Constructors are a special case. The `S' in `S()' is not a
11939 decl-specifier; it is the beginning of the declarator. */
11940 constructor_p
11941 = (!found_decl_spec
11942 && constructor_possible_p
11943 && (cp_parser_constructor_declarator_p
11944 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11946 /* If we don't have a DECL_SPEC yet, then we must be looking at
11947 a type-specifier. */
11948 if (!found_decl_spec && !constructor_p)
11950 int decl_spec_declares_class_or_enum;
11951 bool is_cv_qualifier;
11952 tree type_spec;
11954 type_spec
11955 = cp_parser_type_specifier (parser, flags,
11956 decl_specs,
11957 /*is_declaration=*/true,
11958 &decl_spec_declares_class_or_enum,
11959 &is_cv_qualifier);
11960 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11962 /* If this type-specifier referenced a user-defined type
11963 (a typedef, class-name, etc.), then we can't allow any
11964 more such type-specifiers henceforth.
11966 [dcl.spec]
11968 The longest sequence of decl-specifiers that could
11969 possibly be a type name is taken as the
11970 decl-specifier-seq of a declaration. The sequence shall
11971 be self-consistent as described below.
11973 [dcl.type]
11975 As a general rule, at most one type-specifier is allowed
11976 in the complete decl-specifier-seq of a declaration. The
11977 only exceptions are the following:
11979 -- const or volatile can be combined with any other
11980 type-specifier.
11982 -- signed or unsigned can be combined with char, long,
11983 short, or int.
11985 -- ..
11987 Example:
11989 typedef char* Pc;
11990 void g (const int Pc);
11992 Here, Pc is *not* part of the decl-specifier seq; it's
11993 the declarator. Therefore, once we see a type-specifier
11994 (other than a cv-qualifier), we forbid any additional
11995 user-defined types. We *do* still allow things like `int
11996 int' to be considered a decl-specifier-seq, and issue the
11997 error message later. */
11998 if (type_spec && !is_cv_qualifier)
11999 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12000 /* A constructor declarator cannot follow a type-specifier. */
12001 if (type_spec)
12003 constructor_possible_p = false;
12004 found_decl_spec = true;
12005 if (!is_cv_qualifier)
12006 decl_specs->any_type_specifiers_p = true;
12010 /* If we still do not have a DECL_SPEC, then there are no more
12011 decl-specifiers. */
12012 if (!found_decl_spec)
12013 break;
12015 decl_specs->any_specifiers_p = true;
12016 /* After we see one decl-specifier, further decl-specifiers are
12017 always optional. */
12018 flags |= CP_PARSER_FLAGS_OPTIONAL;
12021 /* Don't allow a friend specifier with a class definition. */
12022 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12023 && (*declares_class_or_enum & 2))
12024 error_at (decl_specs->locations[ds_friend],
12025 "class definition may not be declared a friend");
12028 /* Parse an (optional) storage-class-specifier.
12030 storage-class-specifier:
12031 auto
12032 register
12033 static
12034 extern
12035 mutable
12037 GNU Extension:
12039 storage-class-specifier:
12040 thread
12042 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12044 static tree
12045 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12047 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12049 case RID_AUTO:
12050 if (cxx_dialect != cxx98)
12051 return NULL_TREE;
12052 /* Fall through for C++98. */
12054 case RID_REGISTER:
12055 case RID_STATIC:
12056 case RID_EXTERN:
12057 case RID_MUTABLE:
12058 case RID_THREAD:
12059 /* Consume the token. */
12060 return cp_lexer_consume_token (parser->lexer)->u.value;
12062 default:
12063 return NULL_TREE;
12067 /* Parse an (optional) function-specifier.
12069 function-specifier:
12070 inline
12071 virtual
12072 explicit
12074 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12075 Updates DECL_SPECS, if it is non-NULL. */
12077 static tree
12078 cp_parser_function_specifier_opt (cp_parser* parser,
12079 cp_decl_specifier_seq *decl_specs)
12081 cp_token *token = cp_lexer_peek_token (parser->lexer);
12082 switch (token->keyword)
12084 case RID_INLINE:
12085 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12086 break;
12088 case RID_VIRTUAL:
12089 /* 14.5.2.3 [temp.mem]
12091 A member function template shall not be virtual. */
12092 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12093 error_at (token->location, "templates may not be %<virtual%>");
12094 else
12095 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12096 break;
12098 case RID_EXPLICIT:
12099 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12100 break;
12102 default:
12103 return NULL_TREE;
12106 /* Consume the token. */
12107 return cp_lexer_consume_token (parser->lexer)->u.value;
12110 /* Parse a linkage-specification.
12112 linkage-specification:
12113 extern string-literal { declaration-seq [opt] }
12114 extern string-literal declaration */
12116 static void
12117 cp_parser_linkage_specification (cp_parser* parser)
12119 tree linkage;
12121 /* Look for the `extern' keyword. */
12122 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12124 /* Look for the string-literal. */
12125 linkage = cp_parser_string_literal (parser, false, false);
12127 /* Transform the literal into an identifier. If the literal is a
12128 wide-character string, or contains embedded NULs, then we can't
12129 handle it as the user wants. */
12130 if (strlen (TREE_STRING_POINTER (linkage))
12131 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12133 cp_parser_error (parser, "invalid linkage-specification");
12134 /* Assume C++ linkage. */
12135 linkage = lang_name_cplusplus;
12137 else
12138 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12140 /* We're now using the new linkage. */
12141 push_lang_context (linkage);
12143 /* If the next token is a `{', then we're using the first
12144 production. */
12145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12147 cp_ensure_no_omp_declare_simd (parser);
12149 /* Consume the `{' token. */
12150 cp_lexer_consume_token (parser->lexer);
12151 /* Parse the declarations. */
12152 cp_parser_declaration_seq_opt (parser);
12153 /* Look for the closing `}'. */
12154 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12156 /* Otherwise, there's just one declaration. */
12157 else
12159 bool saved_in_unbraced_linkage_specification_p;
12161 saved_in_unbraced_linkage_specification_p
12162 = parser->in_unbraced_linkage_specification_p;
12163 parser->in_unbraced_linkage_specification_p = true;
12164 cp_parser_declaration (parser);
12165 parser->in_unbraced_linkage_specification_p
12166 = saved_in_unbraced_linkage_specification_p;
12169 /* We're done with the linkage-specification. */
12170 pop_lang_context ();
12173 /* Parse a static_assert-declaration.
12175 static_assert-declaration:
12176 static_assert ( constant-expression , string-literal ) ;
12177 static_assert ( constant-expression ) ; (C++1Z)
12179 If MEMBER_P, this static_assert is a class member. */
12181 static void
12182 cp_parser_static_assert(cp_parser *parser, bool member_p)
12184 tree condition;
12185 tree message;
12186 cp_token *token;
12187 location_t saved_loc;
12188 bool dummy;
12190 /* Peek at the `static_assert' token so we can keep track of exactly
12191 where the static assertion started. */
12192 token = cp_lexer_peek_token (parser->lexer);
12193 saved_loc = token->location;
12195 /* Look for the `static_assert' keyword. */
12196 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12197 RT_STATIC_ASSERT))
12198 return;
12200 /* We know we are in a static assertion; commit to any tentative
12201 parse. */
12202 if (cp_parser_parsing_tentatively (parser))
12203 cp_parser_commit_to_tentative_parse (parser);
12205 /* Parse the `(' starting the static assertion condition. */
12206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12208 /* Parse the constant-expression. Allow a non-constant expression
12209 here in order to give better diagnostics in finish_static_assert. */
12210 condition =
12211 cp_parser_constant_expression (parser,
12212 /*allow_non_constant_p=*/true,
12213 /*non_constant_p=*/&dummy);
12215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12217 if (cxx_dialect < cxx1z)
12218 pedwarn (input_location, OPT_Wpedantic,
12219 "static_assert without a message "
12220 "only available with -std=c++1z or -std=gnu++1z");
12221 /* Eat the ')' */
12222 cp_lexer_consume_token (parser->lexer);
12223 message = build_string (1, "");
12224 TREE_TYPE (message) = char_array_type_node;
12225 fix_string_type (message);
12227 else
12229 /* Parse the separating `,'. */
12230 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12232 /* Parse the string-literal message. */
12233 message = cp_parser_string_literal (parser,
12234 /*translate=*/false,
12235 /*wide_ok=*/true);
12237 /* A `)' completes the static assertion. */
12238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12239 cp_parser_skip_to_closing_parenthesis (parser,
12240 /*recovering=*/true,
12241 /*or_comma=*/false,
12242 /*consume_paren=*/true);
12245 /* A semicolon terminates the declaration. */
12246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12248 /* Complete the static assertion, which may mean either processing
12249 the static assert now or saving it for template instantiation. */
12250 finish_static_assert (condition, message, saved_loc, member_p);
12253 /* Parse the expression in decltype ( expression ). */
12255 static tree
12256 cp_parser_decltype_expr (cp_parser *parser,
12257 bool &id_expression_or_member_access_p)
12259 cp_token *id_expr_start_token;
12260 tree expr;
12262 /* First, try parsing an id-expression. */
12263 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12264 cp_parser_parse_tentatively (parser);
12265 expr = cp_parser_id_expression (parser,
12266 /*template_keyword_p=*/false,
12267 /*check_dependency_p=*/true,
12268 /*template_p=*/NULL,
12269 /*declarator_p=*/false,
12270 /*optional_p=*/false);
12272 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12274 bool non_integral_constant_expression_p = false;
12275 tree id_expression = expr;
12276 cp_id_kind idk;
12277 const char *error_msg;
12279 if (identifier_p (expr))
12280 /* Lookup the name we got back from the id-expression. */
12281 expr = cp_parser_lookup_name_simple (parser, expr,
12282 id_expr_start_token->location);
12284 if (expr
12285 && expr != error_mark_node
12286 && TREE_CODE (expr) != TYPE_DECL
12287 && (TREE_CODE (expr) != BIT_NOT_EXPR
12288 || !TYPE_P (TREE_OPERAND (expr, 0)))
12289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12291 /* Complete lookup of the id-expression. */
12292 expr = (finish_id_expression
12293 (id_expression, expr, parser->scope, &idk,
12294 /*integral_constant_expression_p=*/false,
12295 /*allow_non_integral_constant_expression_p=*/true,
12296 &non_integral_constant_expression_p,
12297 /*template_p=*/false,
12298 /*done=*/true,
12299 /*address_p=*/false,
12300 /*template_arg_p=*/false,
12301 &error_msg,
12302 id_expr_start_token->location));
12304 if (expr == error_mark_node)
12305 /* We found an id-expression, but it was something that we
12306 should not have found. This is an error, not something
12307 we can recover from, so note that we found an
12308 id-expression and we'll recover as gracefully as
12309 possible. */
12310 id_expression_or_member_access_p = true;
12313 if (expr
12314 && expr != error_mark_node
12315 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12316 /* We have an id-expression. */
12317 id_expression_or_member_access_p = true;
12320 if (!id_expression_or_member_access_p)
12322 /* Abort the id-expression parse. */
12323 cp_parser_abort_tentative_parse (parser);
12325 /* Parsing tentatively, again. */
12326 cp_parser_parse_tentatively (parser);
12328 /* Parse a class member access. */
12329 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12330 /*cast_p=*/false, /*decltype*/true,
12331 /*member_access_only_p=*/true, NULL);
12333 if (expr
12334 && expr != error_mark_node
12335 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12336 /* We have an id-expression. */
12337 id_expression_or_member_access_p = true;
12340 if (id_expression_or_member_access_p)
12341 /* We have parsed the complete id-expression or member access. */
12342 cp_parser_parse_definitely (parser);
12343 else
12345 /* Abort our attempt to parse an id-expression or member access
12346 expression. */
12347 cp_parser_abort_tentative_parse (parser);
12349 /* Parse a full expression. */
12350 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12351 /*decltype_p=*/true);
12354 return expr;
12357 /* Parse a `decltype' type. Returns the type.
12359 simple-type-specifier:
12360 decltype ( expression )
12361 C++14 proposal:
12362 decltype ( auto ) */
12364 static tree
12365 cp_parser_decltype (cp_parser *parser)
12367 tree expr;
12368 bool id_expression_or_member_access_p = false;
12369 const char *saved_message;
12370 bool saved_integral_constant_expression_p;
12371 bool saved_non_integral_constant_expression_p;
12372 bool saved_greater_than_is_operator_p;
12373 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12375 if (start_token->type == CPP_DECLTYPE)
12377 /* Already parsed. */
12378 cp_lexer_consume_token (parser->lexer);
12379 return start_token->u.value;
12382 /* Look for the `decltype' token. */
12383 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12384 return error_mark_node;
12386 /* Parse the opening `('. */
12387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12388 return error_mark_node;
12390 /* decltype (auto) */
12391 if (cxx_dialect >= cxx14
12392 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12394 cp_lexer_consume_token (parser->lexer);
12395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12396 return error_mark_node;
12397 expr = make_decltype_auto ();
12398 AUTO_IS_DECLTYPE (expr) = true;
12399 goto rewrite;
12402 /* Types cannot be defined in a `decltype' expression. Save away the
12403 old message. */
12404 saved_message = parser->type_definition_forbidden_message;
12406 /* And create the new one. */
12407 parser->type_definition_forbidden_message
12408 = G_("types may not be defined in %<decltype%> expressions");
12410 /* The restrictions on constant-expressions do not apply inside
12411 decltype expressions. */
12412 saved_integral_constant_expression_p
12413 = parser->integral_constant_expression_p;
12414 saved_non_integral_constant_expression_p
12415 = parser->non_integral_constant_expression_p;
12416 parser->integral_constant_expression_p = false;
12418 /* Within a parenthesized expression, a `>' token is always
12419 the greater-than operator. */
12420 saved_greater_than_is_operator_p
12421 = parser->greater_than_is_operator_p;
12422 parser->greater_than_is_operator_p = true;
12424 /* Do not actually evaluate the expression. */
12425 ++cp_unevaluated_operand;
12427 /* Do not warn about problems with the expression. */
12428 ++c_inhibit_evaluation_warnings;
12430 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12432 /* Go back to evaluating expressions. */
12433 --cp_unevaluated_operand;
12434 --c_inhibit_evaluation_warnings;
12436 /* The `>' token might be the end of a template-id or
12437 template-parameter-list now. */
12438 parser->greater_than_is_operator_p
12439 = saved_greater_than_is_operator_p;
12441 /* Restore the old message and the integral constant expression
12442 flags. */
12443 parser->type_definition_forbidden_message = saved_message;
12444 parser->integral_constant_expression_p
12445 = saved_integral_constant_expression_p;
12446 parser->non_integral_constant_expression_p
12447 = saved_non_integral_constant_expression_p;
12449 /* Parse to the closing `)'. */
12450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12452 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12453 /*consume_paren=*/true);
12454 return error_mark_node;
12457 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12458 tf_warning_or_error);
12460 rewrite:
12461 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12462 it again. */
12463 start_token->type = CPP_DECLTYPE;
12464 start_token->u.value = expr;
12465 start_token->keyword = RID_MAX;
12466 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12468 return expr;
12471 /* Special member functions [gram.special] */
12473 /* Parse a conversion-function-id.
12475 conversion-function-id:
12476 operator conversion-type-id
12478 Returns an IDENTIFIER_NODE representing the operator. */
12480 static tree
12481 cp_parser_conversion_function_id (cp_parser* parser)
12483 tree type;
12484 tree saved_scope;
12485 tree saved_qualifying_scope;
12486 tree saved_object_scope;
12487 tree pushed_scope = NULL_TREE;
12489 /* Look for the `operator' token. */
12490 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12491 return error_mark_node;
12492 /* When we parse the conversion-type-id, the current scope will be
12493 reset. However, we need that information in able to look up the
12494 conversion function later, so we save it here. */
12495 saved_scope = parser->scope;
12496 saved_qualifying_scope = parser->qualifying_scope;
12497 saved_object_scope = parser->object_scope;
12498 /* We must enter the scope of the class so that the names of
12499 entities declared within the class are available in the
12500 conversion-type-id. For example, consider:
12502 struct S {
12503 typedef int I;
12504 operator I();
12507 S::operator I() { ... }
12509 In order to see that `I' is a type-name in the definition, we
12510 must be in the scope of `S'. */
12511 if (saved_scope)
12512 pushed_scope = push_scope (saved_scope);
12513 /* Parse the conversion-type-id. */
12514 type = cp_parser_conversion_type_id (parser);
12515 /* Leave the scope of the class, if any. */
12516 if (pushed_scope)
12517 pop_scope (pushed_scope);
12518 /* Restore the saved scope. */
12519 parser->scope = saved_scope;
12520 parser->qualifying_scope = saved_qualifying_scope;
12521 parser->object_scope = saved_object_scope;
12522 /* If the TYPE is invalid, indicate failure. */
12523 if (type == error_mark_node)
12524 return error_mark_node;
12525 return mangle_conv_op_name_for_type (type);
12528 /* Parse a conversion-type-id:
12530 conversion-type-id:
12531 type-specifier-seq conversion-declarator [opt]
12533 Returns the TYPE specified. */
12535 static tree
12536 cp_parser_conversion_type_id (cp_parser* parser)
12538 tree attributes;
12539 cp_decl_specifier_seq type_specifiers;
12540 cp_declarator *declarator;
12541 tree type_specified;
12542 const char *saved_message;
12544 /* Parse the attributes. */
12545 attributes = cp_parser_attributes_opt (parser);
12547 saved_message = parser->type_definition_forbidden_message;
12548 parser->type_definition_forbidden_message
12549 = G_("types may not be defined in a conversion-type-id");
12551 /* Parse the type-specifiers. */
12552 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12553 /*is_trailing_return=*/false,
12554 &type_specifiers);
12556 parser->type_definition_forbidden_message = saved_message;
12558 /* If that didn't work, stop. */
12559 if (type_specifiers.type == error_mark_node)
12560 return error_mark_node;
12561 /* Parse the conversion-declarator. */
12562 declarator = cp_parser_conversion_declarator_opt (parser);
12564 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12565 /*initialized=*/0, &attributes);
12566 if (attributes)
12567 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12569 /* Don't give this error when parsing tentatively. This happens to
12570 work because we always parse this definitively once. */
12571 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12572 && type_uses_auto (type_specified))
12574 if (cxx_dialect < cxx14)
12576 error ("invalid use of %<auto%> in conversion operator");
12577 return error_mark_node;
12579 else if (template_parm_scope_p ())
12580 warning (0, "use of %<auto%> in member template "
12581 "conversion operator can never be deduced");
12584 return type_specified;
12587 /* Parse an (optional) conversion-declarator.
12589 conversion-declarator:
12590 ptr-operator conversion-declarator [opt]
12594 static cp_declarator *
12595 cp_parser_conversion_declarator_opt (cp_parser* parser)
12597 enum tree_code code;
12598 tree class_type, std_attributes = NULL_TREE;
12599 cp_cv_quals cv_quals;
12601 /* We don't know if there's a ptr-operator next, or not. */
12602 cp_parser_parse_tentatively (parser);
12603 /* Try the ptr-operator. */
12604 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12605 &std_attributes);
12606 /* If it worked, look for more conversion-declarators. */
12607 if (cp_parser_parse_definitely (parser))
12609 cp_declarator *declarator;
12611 /* Parse another optional declarator. */
12612 declarator = cp_parser_conversion_declarator_opt (parser);
12614 declarator = cp_parser_make_indirect_declarator
12615 (code, class_type, cv_quals, declarator, std_attributes);
12617 return declarator;
12620 return NULL;
12623 /* Parse an (optional) ctor-initializer.
12625 ctor-initializer:
12626 : mem-initializer-list
12628 Returns TRUE iff the ctor-initializer was actually present. */
12630 static bool
12631 cp_parser_ctor_initializer_opt (cp_parser* parser)
12633 /* If the next token is not a `:', then there is no
12634 ctor-initializer. */
12635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12637 /* Do default initialization of any bases and members. */
12638 if (DECL_CONSTRUCTOR_P (current_function_decl))
12639 finish_mem_initializers (NULL_TREE);
12641 return false;
12644 /* Consume the `:' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* And the mem-initializer-list. */
12647 cp_parser_mem_initializer_list (parser);
12649 return true;
12652 /* Parse a mem-initializer-list.
12654 mem-initializer-list:
12655 mem-initializer ... [opt]
12656 mem-initializer ... [opt] , mem-initializer-list */
12658 static void
12659 cp_parser_mem_initializer_list (cp_parser* parser)
12661 tree mem_initializer_list = NULL_TREE;
12662 tree target_ctor = error_mark_node;
12663 cp_token *token = cp_lexer_peek_token (parser->lexer);
12665 /* Let the semantic analysis code know that we are starting the
12666 mem-initializer-list. */
12667 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12668 error_at (token->location,
12669 "only constructors take member initializers");
12671 /* Loop through the list. */
12672 while (true)
12674 tree mem_initializer;
12676 token = cp_lexer_peek_token (parser->lexer);
12677 /* Parse the mem-initializer. */
12678 mem_initializer = cp_parser_mem_initializer (parser);
12679 /* If the next token is a `...', we're expanding member initializers. */
12680 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12682 /* Consume the `...'. */
12683 cp_lexer_consume_token (parser->lexer);
12685 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12686 can be expanded but members cannot. */
12687 if (mem_initializer != error_mark_node
12688 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12690 error_at (token->location,
12691 "cannot expand initializer for member %<%D%>",
12692 TREE_PURPOSE (mem_initializer));
12693 mem_initializer = error_mark_node;
12696 /* Construct the pack expansion type. */
12697 if (mem_initializer != error_mark_node)
12698 mem_initializer = make_pack_expansion (mem_initializer);
12700 if (target_ctor != error_mark_node
12701 && mem_initializer != error_mark_node)
12703 error ("mem-initializer for %qD follows constructor delegation",
12704 TREE_PURPOSE (mem_initializer));
12705 mem_initializer = error_mark_node;
12707 /* Look for a target constructor. */
12708 if (mem_initializer != error_mark_node
12709 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12710 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12712 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12713 if (mem_initializer_list)
12715 error ("constructor delegation follows mem-initializer for %qD",
12716 TREE_PURPOSE (mem_initializer_list));
12717 mem_initializer = error_mark_node;
12719 target_ctor = mem_initializer;
12721 /* Add it to the list, unless it was erroneous. */
12722 if (mem_initializer != error_mark_node)
12724 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12725 mem_initializer_list = mem_initializer;
12727 /* If the next token is not a `,', we're done. */
12728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12729 break;
12730 /* Consume the `,' token. */
12731 cp_lexer_consume_token (parser->lexer);
12734 /* Perform semantic analysis. */
12735 if (DECL_CONSTRUCTOR_P (current_function_decl))
12736 finish_mem_initializers (mem_initializer_list);
12739 /* Parse a mem-initializer.
12741 mem-initializer:
12742 mem-initializer-id ( expression-list [opt] )
12743 mem-initializer-id braced-init-list
12745 GNU extension:
12747 mem-initializer:
12748 ( expression-list [opt] )
12750 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12751 class) or FIELD_DECL (for a non-static data member) to initialize;
12752 the TREE_VALUE is the expression-list. An empty initialization
12753 list is represented by void_list_node. */
12755 static tree
12756 cp_parser_mem_initializer (cp_parser* parser)
12758 tree mem_initializer_id;
12759 tree expression_list;
12760 tree member;
12761 cp_token *token = cp_lexer_peek_token (parser->lexer);
12763 /* Find out what is being initialized. */
12764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12766 permerror (token->location,
12767 "anachronistic old-style base class initializer");
12768 mem_initializer_id = NULL_TREE;
12770 else
12772 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12773 if (mem_initializer_id == error_mark_node)
12774 return mem_initializer_id;
12776 member = expand_member_init (mem_initializer_id);
12777 if (member && !DECL_P (member))
12778 in_base_initializer = 1;
12780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12782 bool expr_non_constant_p;
12783 cp_lexer_set_source_position (parser->lexer);
12784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12785 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12786 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12787 expression_list = build_tree_list (NULL_TREE, expression_list);
12789 else
12791 vec<tree, va_gc> *vec;
12792 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12793 /*cast_p=*/false,
12794 /*allow_expansion_p=*/true,
12795 /*non_constant_p=*/NULL);
12796 if (vec == NULL)
12797 return error_mark_node;
12798 expression_list = build_tree_list_vec (vec);
12799 release_tree_vector (vec);
12802 if (expression_list == error_mark_node)
12803 return error_mark_node;
12804 if (!expression_list)
12805 expression_list = void_type_node;
12807 in_base_initializer = 0;
12809 return member ? build_tree_list (member, expression_list) : error_mark_node;
12812 /* Parse a mem-initializer-id.
12814 mem-initializer-id:
12815 :: [opt] nested-name-specifier [opt] class-name
12816 decltype-specifier (C++11)
12817 identifier
12819 Returns a TYPE indicating the class to be initialized for the first
12820 production (and the second in C++11). Returns an IDENTIFIER_NODE
12821 indicating the data member to be initialized for the last production. */
12823 static tree
12824 cp_parser_mem_initializer_id (cp_parser* parser)
12826 bool global_scope_p;
12827 bool nested_name_specifier_p;
12828 bool template_p = false;
12829 tree id;
12831 cp_token *token = cp_lexer_peek_token (parser->lexer);
12833 /* `typename' is not allowed in this context ([temp.res]). */
12834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12836 error_at (token->location,
12837 "keyword %<typename%> not allowed in this context (a qualified "
12838 "member initializer is implicitly a type)");
12839 cp_lexer_consume_token (parser->lexer);
12841 /* Look for the optional `::' operator. */
12842 global_scope_p
12843 = (cp_parser_global_scope_opt (parser,
12844 /*current_scope_valid_p=*/false)
12845 != NULL_TREE);
12846 /* Look for the optional nested-name-specifier. The simplest way to
12847 implement:
12849 [temp.res]
12851 The keyword `typename' is not permitted in a base-specifier or
12852 mem-initializer; in these contexts a qualified name that
12853 depends on a template-parameter is implicitly assumed to be a
12854 type name.
12856 is to assume that we have seen the `typename' keyword at this
12857 point. */
12858 nested_name_specifier_p
12859 = (cp_parser_nested_name_specifier_opt (parser,
12860 /*typename_keyword_p=*/true,
12861 /*check_dependency_p=*/true,
12862 /*type_p=*/true,
12863 /*is_declaration=*/true)
12864 != NULL_TREE);
12865 if (nested_name_specifier_p)
12866 template_p = cp_parser_optional_template_keyword (parser);
12867 /* If there is a `::' operator or a nested-name-specifier, then we
12868 are definitely looking for a class-name. */
12869 if (global_scope_p || nested_name_specifier_p)
12870 return cp_parser_class_name (parser,
12871 /*typename_keyword_p=*/true,
12872 /*template_keyword_p=*/template_p,
12873 typename_type,
12874 /*check_dependency_p=*/true,
12875 /*class_head_p=*/false,
12876 /*is_declaration=*/true);
12877 /* Otherwise, we could also be looking for an ordinary identifier. */
12878 cp_parser_parse_tentatively (parser);
12879 if (cp_lexer_next_token_is_decltype (parser->lexer))
12880 /* Try a decltype-specifier. */
12881 id = cp_parser_decltype (parser);
12882 else
12883 /* Otherwise, try a class-name. */
12884 id = cp_parser_class_name (parser,
12885 /*typename_keyword_p=*/true,
12886 /*template_keyword_p=*/false,
12887 none_type,
12888 /*check_dependency_p=*/true,
12889 /*class_head_p=*/false,
12890 /*is_declaration=*/true);
12891 /* If we found one, we're done. */
12892 if (cp_parser_parse_definitely (parser))
12893 return id;
12894 /* Otherwise, look for an ordinary identifier. */
12895 return cp_parser_identifier (parser);
12898 /* Overloading [gram.over] */
12900 /* Parse an operator-function-id.
12902 operator-function-id:
12903 operator operator
12905 Returns an IDENTIFIER_NODE for the operator which is a
12906 human-readable spelling of the identifier, e.g., `operator +'. */
12908 static tree
12909 cp_parser_operator_function_id (cp_parser* parser)
12911 /* Look for the `operator' keyword. */
12912 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12913 return error_mark_node;
12914 /* And then the name of the operator itself. */
12915 return cp_parser_operator (parser);
12918 /* Return an identifier node for a user-defined literal operator.
12919 The suffix identifier is chained to the operator name identifier. */
12921 static tree
12922 cp_literal_operator_id (const char* name)
12924 tree identifier;
12925 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12926 + strlen (name) + 10);
12927 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12928 identifier = get_identifier (buffer);
12930 return identifier;
12933 /* Parse an operator.
12935 operator:
12936 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12937 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12938 || ++ -- , ->* -> () []
12940 GNU Extensions:
12942 operator:
12943 <? >? <?= >?=
12945 Returns an IDENTIFIER_NODE for the operator which is a
12946 human-readable spelling of the identifier, e.g., `operator +'. */
12948 static tree
12949 cp_parser_operator (cp_parser* parser)
12951 tree id = NULL_TREE;
12952 cp_token *token;
12953 bool utf8 = false;
12955 /* Peek at the next token. */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 /* Figure out which operator we have. */
12958 switch (token->type)
12960 case CPP_KEYWORD:
12962 enum tree_code op;
12964 /* The keyword should be either `new' or `delete'. */
12965 if (token->keyword == RID_NEW)
12966 op = NEW_EXPR;
12967 else if (token->keyword == RID_DELETE)
12968 op = DELETE_EXPR;
12969 else
12970 break;
12972 /* Consume the `new' or `delete' token. */
12973 cp_lexer_consume_token (parser->lexer);
12975 /* Peek at the next token. */
12976 token = cp_lexer_peek_token (parser->lexer);
12977 /* If it's a `[' token then this is the array variant of the
12978 operator. */
12979 if (token->type == CPP_OPEN_SQUARE)
12981 /* Consume the `[' token. */
12982 cp_lexer_consume_token (parser->lexer);
12983 /* Look for the `]' token. */
12984 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12985 id = ansi_opname (op == NEW_EXPR
12986 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12988 /* Otherwise, we have the non-array variant. */
12989 else
12990 id = ansi_opname (op);
12992 return id;
12995 case CPP_PLUS:
12996 id = ansi_opname (PLUS_EXPR);
12997 break;
12999 case CPP_MINUS:
13000 id = ansi_opname (MINUS_EXPR);
13001 break;
13003 case CPP_MULT:
13004 id = ansi_opname (MULT_EXPR);
13005 break;
13007 case CPP_DIV:
13008 id = ansi_opname (TRUNC_DIV_EXPR);
13009 break;
13011 case CPP_MOD:
13012 id = ansi_opname (TRUNC_MOD_EXPR);
13013 break;
13015 case CPP_XOR:
13016 id = ansi_opname (BIT_XOR_EXPR);
13017 break;
13019 case CPP_AND:
13020 id = ansi_opname (BIT_AND_EXPR);
13021 break;
13023 case CPP_OR:
13024 id = ansi_opname (BIT_IOR_EXPR);
13025 break;
13027 case CPP_COMPL:
13028 id = ansi_opname (BIT_NOT_EXPR);
13029 break;
13031 case CPP_NOT:
13032 id = ansi_opname (TRUTH_NOT_EXPR);
13033 break;
13035 case CPP_EQ:
13036 id = ansi_assopname (NOP_EXPR);
13037 break;
13039 case CPP_LESS:
13040 id = ansi_opname (LT_EXPR);
13041 break;
13043 case CPP_GREATER:
13044 id = ansi_opname (GT_EXPR);
13045 break;
13047 case CPP_PLUS_EQ:
13048 id = ansi_assopname (PLUS_EXPR);
13049 break;
13051 case CPP_MINUS_EQ:
13052 id = ansi_assopname (MINUS_EXPR);
13053 break;
13055 case CPP_MULT_EQ:
13056 id = ansi_assopname (MULT_EXPR);
13057 break;
13059 case CPP_DIV_EQ:
13060 id = ansi_assopname (TRUNC_DIV_EXPR);
13061 break;
13063 case CPP_MOD_EQ:
13064 id = ansi_assopname (TRUNC_MOD_EXPR);
13065 break;
13067 case CPP_XOR_EQ:
13068 id = ansi_assopname (BIT_XOR_EXPR);
13069 break;
13071 case CPP_AND_EQ:
13072 id = ansi_assopname (BIT_AND_EXPR);
13073 break;
13075 case CPP_OR_EQ:
13076 id = ansi_assopname (BIT_IOR_EXPR);
13077 break;
13079 case CPP_LSHIFT:
13080 id = ansi_opname (LSHIFT_EXPR);
13081 break;
13083 case CPP_RSHIFT:
13084 id = ansi_opname (RSHIFT_EXPR);
13085 break;
13087 case CPP_LSHIFT_EQ:
13088 id = ansi_assopname (LSHIFT_EXPR);
13089 break;
13091 case CPP_RSHIFT_EQ:
13092 id = ansi_assopname (RSHIFT_EXPR);
13093 break;
13095 case CPP_EQ_EQ:
13096 id = ansi_opname (EQ_EXPR);
13097 break;
13099 case CPP_NOT_EQ:
13100 id = ansi_opname (NE_EXPR);
13101 break;
13103 case CPP_LESS_EQ:
13104 id = ansi_opname (LE_EXPR);
13105 break;
13107 case CPP_GREATER_EQ:
13108 id = ansi_opname (GE_EXPR);
13109 break;
13111 case CPP_AND_AND:
13112 id = ansi_opname (TRUTH_ANDIF_EXPR);
13113 break;
13115 case CPP_OR_OR:
13116 id = ansi_opname (TRUTH_ORIF_EXPR);
13117 break;
13119 case CPP_PLUS_PLUS:
13120 id = ansi_opname (POSTINCREMENT_EXPR);
13121 break;
13123 case CPP_MINUS_MINUS:
13124 id = ansi_opname (PREDECREMENT_EXPR);
13125 break;
13127 case CPP_COMMA:
13128 id = ansi_opname (COMPOUND_EXPR);
13129 break;
13131 case CPP_DEREF_STAR:
13132 id = ansi_opname (MEMBER_REF);
13133 break;
13135 case CPP_DEREF:
13136 id = ansi_opname (COMPONENT_REF);
13137 break;
13139 case CPP_OPEN_PAREN:
13140 /* Consume the `('. */
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Look for the matching `)'. */
13143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13144 return ansi_opname (CALL_EXPR);
13146 case CPP_OPEN_SQUARE:
13147 /* Consume the `['. */
13148 cp_lexer_consume_token (parser->lexer);
13149 /* Look for the matching `]'. */
13150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13151 return ansi_opname (ARRAY_REF);
13153 case CPP_UTF8STRING:
13154 case CPP_UTF8STRING_USERDEF:
13155 utf8 = true;
13156 case CPP_STRING:
13157 case CPP_WSTRING:
13158 case CPP_STRING16:
13159 case CPP_STRING32:
13160 case CPP_STRING_USERDEF:
13161 case CPP_WSTRING_USERDEF:
13162 case CPP_STRING16_USERDEF:
13163 case CPP_STRING32_USERDEF:
13165 tree str, string_tree;
13166 int sz, len;
13168 if (cxx_dialect == cxx98)
13169 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13171 /* Consume the string. */
13172 str = cp_parser_string_literal (parser, /*translate=*/true,
13173 /*wide_ok=*/true, /*lookup_udlit=*/false);
13174 if (str == error_mark_node)
13175 return error_mark_node;
13176 else if (TREE_CODE (str) == USERDEF_LITERAL)
13178 string_tree = USERDEF_LITERAL_VALUE (str);
13179 id = USERDEF_LITERAL_SUFFIX_ID (str);
13181 else
13183 string_tree = str;
13184 /* Look for the suffix identifier. */
13185 token = cp_lexer_peek_token (parser->lexer);
13186 if (token->type == CPP_NAME)
13187 id = cp_parser_identifier (parser);
13188 else if (token->type == CPP_KEYWORD)
13190 error ("unexpected keyword;"
13191 " remove space between quotes and suffix identifier");
13192 return error_mark_node;
13194 else
13196 error ("expected suffix identifier");
13197 return error_mark_node;
13200 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13201 (TREE_TYPE (TREE_TYPE (string_tree))));
13202 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13203 if (len != 0)
13205 error ("expected empty string after %<operator%> keyword");
13206 return error_mark_node;
13208 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13209 != char_type_node)
13211 error ("invalid encoding prefix in literal operator");
13212 return error_mark_node;
13214 if (id != error_mark_node)
13216 const char *name = IDENTIFIER_POINTER (id);
13217 id = cp_literal_operator_id (name);
13219 return id;
13222 default:
13223 /* Anything else is an error. */
13224 break;
13227 /* If we have selected an identifier, we need to consume the
13228 operator token. */
13229 if (id)
13230 cp_lexer_consume_token (parser->lexer);
13231 /* Otherwise, no valid operator name was present. */
13232 else
13234 cp_parser_error (parser, "expected operator");
13235 id = error_mark_node;
13238 return id;
13241 /* Parse a template-declaration.
13243 template-declaration:
13244 export [opt] template < template-parameter-list > declaration
13246 If MEMBER_P is TRUE, this template-declaration occurs within a
13247 class-specifier.
13249 The grammar rule given by the standard isn't correct. What
13250 is really meant is:
13252 template-declaration:
13253 export [opt] template-parameter-list-seq
13254 decl-specifier-seq [opt] init-declarator [opt] ;
13255 export [opt] template-parameter-list-seq
13256 function-definition
13258 template-parameter-list-seq:
13259 template-parameter-list-seq [opt]
13260 template < template-parameter-list > */
13262 static void
13263 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13265 /* Check for `export'. */
13266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13268 /* Consume the `export' token. */
13269 cp_lexer_consume_token (parser->lexer);
13270 /* Warn that we do not support `export'. */
13271 warning (0, "keyword %<export%> not implemented, and will be ignored");
13274 cp_parser_template_declaration_after_export (parser, member_p);
13277 /* Parse a template-parameter-list.
13279 template-parameter-list:
13280 template-parameter
13281 template-parameter-list , template-parameter
13283 Returns a TREE_LIST. Each node represents a template parameter.
13284 The nodes are connected via their TREE_CHAINs. */
13286 static tree
13287 cp_parser_template_parameter_list (cp_parser* parser)
13289 tree parameter_list = NULL_TREE;
13291 begin_template_parm_list ();
13293 /* The loop below parses the template parms. We first need to know
13294 the total number of template parms to be able to compute proper
13295 canonical types of each dependent type. So after the loop, when
13296 we know the total number of template parms,
13297 end_template_parm_list computes the proper canonical types and
13298 fixes up the dependent types accordingly. */
13299 while (true)
13301 tree parameter;
13302 bool is_non_type;
13303 bool is_parameter_pack;
13304 location_t parm_loc;
13306 /* Parse the template-parameter. */
13307 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13308 parameter = cp_parser_template_parameter (parser,
13309 &is_non_type,
13310 &is_parameter_pack);
13311 /* Add it to the list. */
13312 if (parameter != error_mark_node)
13313 parameter_list = process_template_parm (parameter_list,
13314 parm_loc,
13315 parameter,
13316 is_non_type,
13317 is_parameter_pack);
13318 else
13320 tree err_parm = build_tree_list (parameter, parameter);
13321 parameter_list = chainon (parameter_list, err_parm);
13324 /* If the next token is not a `,', we're done. */
13325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13326 break;
13327 /* Otherwise, consume the `,' token. */
13328 cp_lexer_consume_token (parser->lexer);
13331 return end_template_parm_list (parameter_list);
13334 /* Parse a template-parameter.
13336 template-parameter:
13337 type-parameter
13338 parameter-declaration
13340 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13341 the parameter. The TREE_PURPOSE is the default value, if any.
13342 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13343 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13344 set to true iff this parameter is a parameter pack. */
13346 static tree
13347 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13348 bool *is_parameter_pack)
13350 cp_token *token;
13351 cp_parameter_declarator *parameter_declarator;
13352 cp_declarator *id_declarator;
13353 tree parm;
13355 /* Assume it is a type parameter or a template parameter. */
13356 *is_non_type = false;
13357 /* Assume it not a parameter pack. */
13358 *is_parameter_pack = false;
13359 /* Peek at the next token. */
13360 token = cp_lexer_peek_token (parser->lexer);
13361 /* If it is `class' or `template', we have a type-parameter. */
13362 if (token->keyword == RID_TEMPLATE)
13363 return cp_parser_type_parameter (parser, is_parameter_pack);
13364 /* If it is `class' or `typename' we do not know yet whether it is a
13365 type parameter or a non-type parameter. Consider:
13367 template <typename T, typename T::X X> ...
13371 template <class C, class D*> ...
13373 Here, the first parameter is a type parameter, and the second is
13374 a non-type parameter. We can tell by looking at the token after
13375 the identifier -- if it is a `,', `=', or `>' then we have a type
13376 parameter. */
13377 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13379 /* Peek at the token after `class' or `typename'. */
13380 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13381 /* If it's an ellipsis, we have a template type parameter
13382 pack. */
13383 if (token->type == CPP_ELLIPSIS)
13384 return cp_parser_type_parameter (parser, is_parameter_pack);
13385 /* If it's an identifier, skip it. */
13386 if (token->type == CPP_NAME)
13387 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13388 /* Now, see if the token looks like the end of a template
13389 parameter. */
13390 if (token->type == CPP_COMMA
13391 || token->type == CPP_EQ
13392 || token->type == CPP_GREATER)
13393 return cp_parser_type_parameter (parser, is_parameter_pack);
13396 /* Otherwise, it is a non-type parameter.
13398 [temp.param]
13400 When parsing a default template-argument for a non-type
13401 template-parameter, the first non-nested `>' is taken as the end
13402 of the template parameter-list rather than a greater-than
13403 operator. */
13404 *is_non_type = true;
13405 parameter_declarator
13406 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13407 /*parenthesized_p=*/NULL);
13409 if (!parameter_declarator)
13410 return error_mark_node;
13412 /* If the parameter declaration is marked as a parameter pack, set
13413 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13414 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13415 grokdeclarator. */
13416 if (parameter_declarator->declarator
13417 && parameter_declarator->declarator->parameter_pack_p)
13419 *is_parameter_pack = true;
13420 parameter_declarator->declarator->parameter_pack_p = false;
13423 if (parameter_declarator->default_argument)
13425 /* Can happen in some cases of erroneous input (c++/34892). */
13426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13427 /* Consume the `...' for better error recovery. */
13428 cp_lexer_consume_token (parser->lexer);
13430 /* If the next token is an ellipsis, and we don't already have it
13431 marked as a parameter pack, then we have a parameter pack (that
13432 has no declarator). */
13433 else if (!*is_parameter_pack
13434 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13435 && (declarator_can_be_parameter_pack
13436 (parameter_declarator->declarator)))
13438 /* Consume the `...'. */
13439 cp_lexer_consume_token (parser->lexer);
13440 maybe_warn_variadic_templates ();
13442 *is_parameter_pack = true;
13444 /* We might end up with a pack expansion as the type of the non-type
13445 template parameter, in which case this is a non-type template
13446 parameter pack. */
13447 else if (parameter_declarator->decl_specifiers.type
13448 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13450 *is_parameter_pack = true;
13451 parameter_declarator->decl_specifiers.type =
13452 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13455 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13457 /* Parameter packs cannot have default arguments. However, a
13458 user may try to do so, so we'll parse them and give an
13459 appropriate diagnostic here. */
13461 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13463 /* Find the name of the parameter pack. */
13464 id_declarator = parameter_declarator->declarator;
13465 while (id_declarator && id_declarator->kind != cdk_id)
13466 id_declarator = id_declarator->declarator;
13468 if (id_declarator && id_declarator->kind == cdk_id)
13469 error_at (start_token->location,
13470 "template parameter pack %qD cannot have a default argument",
13471 id_declarator->u.id.unqualified_name);
13472 else
13473 error_at (start_token->location,
13474 "template parameter pack cannot have a default argument");
13476 /* Parse the default argument, but throw away the result. */
13477 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13480 parm = grokdeclarator (parameter_declarator->declarator,
13481 &parameter_declarator->decl_specifiers,
13482 TPARM, /*initialized=*/0,
13483 /*attrlist=*/NULL);
13484 if (parm == error_mark_node)
13485 return error_mark_node;
13487 return build_tree_list (parameter_declarator->default_argument, parm);
13490 /* Parse a type-parameter.
13492 type-parameter:
13493 class identifier [opt]
13494 class identifier [opt] = type-id
13495 typename identifier [opt]
13496 typename identifier [opt] = type-id
13497 template < template-parameter-list > class identifier [opt]
13498 template < template-parameter-list > class identifier [opt]
13499 = id-expression
13501 GNU Extension (variadic templates):
13503 type-parameter:
13504 class ... identifier [opt]
13505 typename ... identifier [opt]
13507 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13508 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13509 the declaration of the parameter.
13511 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13513 static tree
13514 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13516 cp_token *token;
13517 tree parameter;
13519 /* Look for a keyword to tell us what kind of parameter this is. */
13520 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13521 if (!token)
13522 return error_mark_node;
13524 switch (token->keyword)
13526 case RID_CLASS:
13527 case RID_TYPENAME:
13529 tree identifier;
13530 tree default_argument;
13532 /* If the next token is an ellipsis, we have a template
13533 argument pack. */
13534 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13536 /* Consume the `...' token. */
13537 cp_lexer_consume_token (parser->lexer);
13538 maybe_warn_variadic_templates ();
13540 *is_parameter_pack = true;
13543 /* If the next token is an identifier, then it names the
13544 parameter. */
13545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13546 identifier = cp_parser_identifier (parser);
13547 else
13548 identifier = NULL_TREE;
13550 /* Create the parameter. */
13551 parameter = finish_template_type_parm (class_type_node, identifier);
13553 /* If the next token is an `=', we have a default argument. */
13554 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13556 /* Consume the `=' token. */
13557 cp_lexer_consume_token (parser->lexer);
13558 /* Parse the default-argument. */
13559 push_deferring_access_checks (dk_no_deferred);
13560 default_argument = cp_parser_type_id (parser);
13562 /* Template parameter packs cannot have default
13563 arguments. */
13564 if (*is_parameter_pack)
13566 if (identifier)
13567 error_at (token->location,
13568 "template parameter pack %qD cannot have a "
13569 "default argument", identifier);
13570 else
13571 error_at (token->location,
13572 "template parameter packs cannot have "
13573 "default arguments");
13574 default_argument = NULL_TREE;
13576 else if (check_for_bare_parameter_packs (default_argument))
13577 default_argument = error_mark_node;
13578 pop_deferring_access_checks ();
13580 else
13581 default_argument = NULL_TREE;
13583 /* Create the combined representation of the parameter and the
13584 default argument. */
13585 parameter = build_tree_list (default_argument, parameter);
13587 break;
13589 case RID_TEMPLATE:
13591 tree identifier;
13592 tree default_argument;
13594 /* Look for the `<'. */
13595 cp_parser_require (parser, CPP_LESS, RT_LESS);
13596 /* Parse the template-parameter-list. */
13597 cp_parser_template_parameter_list (parser);
13598 /* Look for the `>'. */
13599 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13600 /* Look for the `class' or 'typename' keywords. */
13601 cp_parser_type_parameter_key (parser);
13602 /* If the next token is an ellipsis, we have a template
13603 argument pack. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13606 /* Consume the `...' token. */
13607 cp_lexer_consume_token (parser->lexer);
13608 maybe_warn_variadic_templates ();
13610 *is_parameter_pack = true;
13612 /* If the next token is an `=', then there is a
13613 default-argument. If the next token is a `>', we are at
13614 the end of the parameter-list. If the next token is a `,',
13615 then we are at the end of this parameter. */
13616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13617 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13618 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13620 identifier = cp_parser_identifier (parser);
13621 /* Treat invalid names as if the parameter were nameless. */
13622 if (identifier == error_mark_node)
13623 identifier = NULL_TREE;
13625 else
13626 identifier = NULL_TREE;
13628 /* Create the template parameter. */
13629 parameter = finish_template_template_parm (class_type_node,
13630 identifier);
13632 /* If the next token is an `=', then there is a
13633 default-argument. */
13634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13636 bool is_template;
13638 /* Consume the `='. */
13639 cp_lexer_consume_token (parser->lexer);
13640 /* Parse the id-expression. */
13641 push_deferring_access_checks (dk_no_deferred);
13642 /* save token before parsing the id-expression, for error
13643 reporting */
13644 token = cp_lexer_peek_token (parser->lexer);
13645 default_argument
13646 = cp_parser_id_expression (parser,
13647 /*template_keyword_p=*/false,
13648 /*check_dependency_p=*/true,
13649 /*template_p=*/&is_template,
13650 /*declarator_p=*/false,
13651 /*optional_p=*/false);
13652 if (TREE_CODE (default_argument) == TYPE_DECL)
13653 /* If the id-expression was a template-id that refers to
13654 a template-class, we already have the declaration here,
13655 so no further lookup is needed. */
13657 else
13658 /* Look up the name. */
13659 default_argument
13660 = cp_parser_lookup_name (parser, default_argument,
13661 none_type,
13662 /*is_template=*/is_template,
13663 /*is_namespace=*/false,
13664 /*check_dependency=*/true,
13665 /*ambiguous_decls=*/NULL,
13666 token->location);
13667 /* See if the default argument is valid. */
13668 default_argument
13669 = check_template_template_default_arg (default_argument);
13671 /* Template parameter packs cannot have default
13672 arguments. */
13673 if (*is_parameter_pack)
13675 if (identifier)
13676 error_at (token->location,
13677 "template parameter pack %qD cannot "
13678 "have a default argument",
13679 identifier);
13680 else
13681 error_at (token->location, "template parameter packs cannot "
13682 "have default arguments");
13683 default_argument = NULL_TREE;
13685 pop_deferring_access_checks ();
13687 else
13688 default_argument = NULL_TREE;
13690 /* Create the combined representation of the parameter and the
13691 default argument. */
13692 parameter = build_tree_list (default_argument, parameter);
13694 break;
13696 default:
13697 gcc_unreachable ();
13698 break;
13701 return parameter;
13704 /* Parse a template-id.
13706 template-id:
13707 template-name < template-argument-list [opt] >
13709 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13710 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13711 returned. Otherwise, if the template-name names a function, or set
13712 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13713 names a class, returns a TYPE_DECL for the specialization.
13715 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13716 uninstantiated templates. */
13718 static tree
13719 cp_parser_template_id (cp_parser *parser,
13720 bool template_keyword_p,
13721 bool check_dependency_p,
13722 enum tag_types tag_type,
13723 bool is_declaration)
13725 int i;
13726 tree templ;
13727 tree arguments;
13728 tree template_id;
13729 cp_token_position start_of_id = 0;
13730 deferred_access_check *chk;
13731 vec<deferred_access_check, va_gc> *access_check;
13732 cp_token *next_token = NULL, *next_token_2 = NULL;
13733 bool is_identifier;
13735 /* If the next token corresponds to a template-id, there is no need
13736 to reparse it. */
13737 next_token = cp_lexer_peek_token (parser->lexer);
13738 if (next_token->type == CPP_TEMPLATE_ID)
13740 struct tree_check *check_value;
13742 /* Get the stored value. */
13743 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13744 /* Perform any access checks that were deferred. */
13745 access_check = check_value->checks;
13746 if (access_check)
13748 FOR_EACH_VEC_ELT (*access_check, i, chk)
13749 perform_or_defer_access_check (chk->binfo,
13750 chk->decl,
13751 chk->diag_decl,
13752 tf_warning_or_error);
13754 /* Return the stored value. */
13755 return check_value->value;
13758 /* Avoid performing name lookup if there is no possibility of
13759 finding a template-id. */
13760 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13761 || (next_token->type == CPP_NAME
13762 && !cp_parser_nth_token_starts_template_argument_list_p
13763 (parser, 2)))
13765 cp_parser_error (parser, "expected template-id");
13766 return error_mark_node;
13769 /* Remember where the template-id starts. */
13770 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13771 start_of_id = cp_lexer_token_position (parser->lexer, false);
13773 push_deferring_access_checks (dk_deferred);
13775 /* Parse the template-name. */
13776 is_identifier = false;
13777 templ = cp_parser_template_name (parser, template_keyword_p,
13778 check_dependency_p,
13779 is_declaration,
13780 tag_type,
13781 &is_identifier);
13782 if (templ == error_mark_node || is_identifier)
13784 pop_deferring_access_checks ();
13785 return templ;
13788 /* If we find the sequence `[:' after a template-name, it's probably
13789 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13790 parse correctly the argument list. */
13791 next_token = cp_lexer_peek_token (parser->lexer);
13792 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13793 if (next_token->type == CPP_OPEN_SQUARE
13794 && next_token->flags & DIGRAPH
13795 && next_token_2->type == CPP_COLON
13796 && !(next_token_2->flags & PREV_WHITE))
13798 cp_parser_parse_tentatively (parser);
13799 /* Change `:' into `::'. */
13800 next_token_2->type = CPP_SCOPE;
13801 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13802 CPP_LESS. */
13803 cp_lexer_consume_token (parser->lexer);
13805 /* Parse the arguments. */
13806 arguments = cp_parser_enclosed_template_argument_list (parser);
13807 if (!cp_parser_parse_definitely (parser))
13809 /* If we couldn't parse an argument list, then we revert our changes
13810 and return simply an error. Maybe this is not a template-id
13811 after all. */
13812 next_token_2->type = CPP_COLON;
13813 cp_parser_error (parser, "expected %<<%>");
13814 pop_deferring_access_checks ();
13815 return error_mark_node;
13817 /* Otherwise, emit an error about the invalid digraph, but continue
13818 parsing because we got our argument list. */
13819 if (permerror (next_token->location,
13820 "%<<::%> cannot begin a template-argument list"))
13822 static bool hint = false;
13823 inform (next_token->location,
13824 "%<<:%> is an alternate spelling for %<[%>."
13825 " Insert whitespace between %<<%> and %<::%>");
13826 if (!hint && !flag_permissive)
13828 inform (next_token->location, "(if you use %<-fpermissive%> "
13829 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13830 "accept your code)");
13831 hint = true;
13835 else
13837 /* Look for the `<' that starts the template-argument-list. */
13838 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13840 pop_deferring_access_checks ();
13841 return error_mark_node;
13843 /* Parse the arguments. */
13844 arguments = cp_parser_enclosed_template_argument_list (parser);
13847 /* Build a representation of the specialization. */
13848 if (identifier_p (templ))
13849 template_id = build_min_nt_loc (next_token->location,
13850 TEMPLATE_ID_EXPR,
13851 templ, arguments);
13852 else if (DECL_TYPE_TEMPLATE_P (templ)
13853 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13855 bool entering_scope;
13856 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13857 template (rather than some instantiation thereof) only if
13858 is not nested within some other construct. For example, in
13859 "template <typename T> void f(T) { A<T>::", A<T> is just an
13860 instantiation of A. */
13861 entering_scope = (template_parm_scope_p ()
13862 && cp_lexer_next_token_is (parser->lexer,
13863 CPP_SCOPE));
13864 template_id
13865 = finish_template_type (templ, arguments, entering_scope);
13867 else if (variable_template_p (templ))
13869 template_id = lookup_template_variable (templ, arguments);
13871 else
13873 /* If it's not a class-template or a template-template, it should be
13874 a function-template. */
13875 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13876 || TREE_CODE (templ) == OVERLOAD
13877 || BASELINK_P (templ)));
13879 template_id = lookup_template_function (templ, arguments);
13882 /* If parsing tentatively, replace the sequence of tokens that makes
13883 up the template-id with a CPP_TEMPLATE_ID token. That way,
13884 should we re-parse the token stream, we will not have to repeat
13885 the effort required to do the parse, nor will we issue duplicate
13886 error messages about problems during instantiation of the
13887 template. */
13888 if (start_of_id
13889 /* Don't do this if we had a parse error in a declarator; re-parsing
13890 might succeed if a name changes meaning (60361). */
13891 && !(cp_parser_error_occurred (parser)
13892 && cp_parser_parsing_tentatively (parser)
13893 && parser->in_declarator_p))
13895 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13897 /* Reset the contents of the START_OF_ID token. */
13898 token->type = CPP_TEMPLATE_ID;
13899 /* Retrieve any deferred checks. Do not pop this access checks yet
13900 so the memory will not be reclaimed during token replacing below. */
13901 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13902 token->u.tree_check_value->value = template_id;
13903 token->u.tree_check_value->checks = get_deferred_access_checks ();
13904 token->keyword = RID_MAX;
13906 /* Purge all subsequent tokens. */
13907 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13909 /* ??? Can we actually assume that, if template_id ==
13910 error_mark_node, we will have issued a diagnostic to the
13911 user, as opposed to simply marking the tentative parse as
13912 failed? */
13913 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13914 error_at (token->location, "parse error in template argument list");
13917 pop_to_parent_deferring_access_checks ();
13918 return template_id;
13921 /* Parse a template-name.
13923 template-name:
13924 identifier
13926 The standard should actually say:
13928 template-name:
13929 identifier
13930 operator-function-id
13932 A defect report has been filed about this issue.
13934 A conversion-function-id cannot be a template name because they cannot
13935 be part of a template-id. In fact, looking at this code:
13937 a.operator K<int>()
13939 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13940 It is impossible to call a templated conversion-function-id with an
13941 explicit argument list, since the only allowed template parameter is
13942 the type to which it is converting.
13944 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13945 `template' keyword, in a construction like:
13947 T::template f<3>()
13949 In that case `f' is taken to be a template-name, even though there
13950 is no way of knowing for sure.
13952 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13953 name refers to a set of overloaded functions, at least one of which
13954 is a template, or an IDENTIFIER_NODE with the name of the template,
13955 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13956 names are looked up inside uninstantiated templates. */
13958 static tree
13959 cp_parser_template_name (cp_parser* parser,
13960 bool template_keyword_p,
13961 bool check_dependency_p,
13962 bool is_declaration,
13963 enum tag_types tag_type,
13964 bool *is_identifier)
13966 tree identifier;
13967 tree decl;
13968 tree fns;
13969 cp_token *token = cp_lexer_peek_token (parser->lexer);
13971 /* If the next token is `operator', then we have either an
13972 operator-function-id or a conversion-function-id. */
13973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13975 /* We don't know whether we're looking at an
13976 operator-function-id or a conversion-function-id. */
13977 cp_parser_parse_tentatively (parser);
13978 /* Try an operator-function-id. */
13979 identifier = cp_parser_operator_function_id (parser);
13980 /* If that didn't work, try a conversion-function-id. */
13981 if (!cp_parser_parse_definitely (parser))
13983 cp_parser_error (parser, "expected template-name");
13984 return error_mark_node;
13987 /* Look for the identifier. */
13988 else
13989 identifier = cp_parser_identifier (parser);
13991 /* If we didn't find an identifier, we don't have a template-id. */
13992 if (identifier == error_mark_node)
13993 return error_mark_node;
13995 /* If the name immediately followed the `template' keyword, then it
13996 is a template-name. However, if the next token is not `<', then
13997 we do not treat it as a template-name, since it is not being used
13998 as part of a template-id. This enables us to handle constructs
13999 like:
14001 template <typename T> struct S { S(); };
14002 template <typename T> S<T>::S();
14004 correctly. We would treat `S' as a template -- if it were `S<T>'
14005 -- but we do not if there is no `<'. */
14007 if (processing_template_decl
14008 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14010 /* In a declaration, in a dependent context, we pretend that the
14011 "template" keyword was present in order to improve error
14012 recovery. For example, given:
14014 template <typename T> void f(T::X<int>);
14016 we want to treat "X<int>" as a template-id. */
14017 if (is_declaration
14018 && !template_keyword_p
14019 && parser->scope && TYPE_P (parser->scope)
14020 && check_dependency_p
14021 && dependent_scope_p (parser->scope)
14022 /* Do not do this for dtors (or ctors), since they never
14023 need the template keyword before their name. */
14024 && !constructor_name_p (identifier, parser->scope))
14026 cp_token_position start = 0;
14028 /* Explain what went wrong. */
14029 error_at (token->location, "non-template %qD used as template",
14030 identifier);
14031 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14032 parser->scope, identifier);
14033 /* If parsing tentatively, find the location of the "<" token. */
14034 if (cp_parser_simulate_error (parser))
14035 start = cp_lexer_token_position (parser->lexer, true);
14036 /* Parse the template arguments so that we can issue error
14037 messages about them. */
14038 cp_lexer_consume_token (parser->lexer);
14039 cp_parser_enclosed_template_argument_list (parser);
14040 /* Skip tokens until we find a good place from which to
14041 continue parsing. */
14042 cp_parser_skip_to_closing_parenthesis (parser,
14043 /*recovering=*/true,
14044 /*or_comma=*/true,
14045 /*consume_paren=*/false);
14046 /* If parsing tentatively, permanently remove the
14047 template argument list. That will prevent duplicate
14048 error messages from being issued about the missing
14049 "template" keyword. */
14050 if (start)
14051 cp_lexer_purge_tokens_after (parser->lexer, start);
14052 if (is_identifier)
14053 *is_identifier = true;
14054 return identifier;
14057 /* If the "template" keyword is present, then there is generally
14058 no point in doing name-lookup, so we just return IDENTIFIER.
14059 But, if the qualifying scope is non-dependent then we can
14060 (and must) do name-lookup normally. */
14061 if (template_keyword_p
14062 && (!parser->scope
14063 || (TYPE_P (parser->scope)
14064 && dependent_type_p (parser->scope))))
14065 return identifier;
14068 /* Look up the name. */
14069 decl = cp_parser_lookup_name (parser, identifier,
14070 tag_type,
14071 /*is_template=*/true,
14072 /*is_namespace=*/false,
14073 check_dependency_p,
14074 /*ambiguous_decls=*/NULL,
14075 token->location);
14077 decl = strip_using_decl (decl);
14079 /* If DECL is a template, then the name was a template-name. */
14080 if (TREE_CODE (decl) == TEMPLATE_DECL)
14082 if (TREE_DEPRECATED (decl)
14083 && deprecated_state != DEPRECATED_SUPPRESS)
14084 warn_deprecated_use (decl, NULL_TREE);
14086 else
14088 tree fn = NULL_TREE;
14090 /* The standard does not explicitly indicate whether a name that
14091 names a set of overloaded declarations, some of which are
14092 templates, is a template-name. However, such a name should
14093 be a template-name; otherwise, there is no way to form a
14094 template-id for the overloaded templates. */
14095 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14096 if (TREE_CODE (fns) == OVERLOAD)
14097 for (fn = fns; fn; fn = OVL_NEXT (fn))
14098 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14099 break;
14101 if (!fn)
14103 /* The name does not name a template. */
14104 cp_parser_error (parser, "expected template-name");
14105 return error_mark_node;
14109 /* If DECL is dependent, and refers to a function, then just return
14110 its name; we will look it up again during template instantiation. */
14111 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14113 tree scope = ovl_scope (decl);
14114 if (TYPE_P (scope) && dependent_type_p (scope))
14115 return identifier;
14118 return decl;
14121 /* Parse a template-argument-list.
14123 template-argument-list:
14124 template-argument ... [opt]
14125 template-argument-list , template-argument ... [opt]
14127 Returns a TREE_VEC containing the arguments. */
14129 static tree
14130 cp_parser_template_argument_list (cp_parser* parser)
14132 tree fixed_args[10];
14133 unsigned n_args = 0;
14134 unsigned alloced = 10;
14135 tree *arg_ary = fixed_args;
14136 tree vec;
14137 bool saved_in_template_argument_list_p;
14138 bool saved_ice_p;
14139 bool saved_non_ice_p;
14141 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14142 parser->in_template_argument_list_p = true;
14143 /* Even if the template-id appears in an integral
14144 constant-expression, the contents of the argument list do
14145 not. */
14146 saved_ice_p = parser->integral_constant_expression_p;
14147 parser->integral_constant_expression_p = false;
14148 saved_non_ice_p = parser->non_integral_constant_expression_p;
14149 parser->non_integral_constant_expression_p = false;
14151 /* Parse the arguments. */
14154 tree argument;
14156 if (n_args)
14157 /* Consume the comma. */
14158 cp_lexer_consume_token (parser->lexer);
14160 /* Parse the template-argument. */
14161 argument = cp_parser_template_argument (parser);
14163 /* If the next token is an ellipsis, we're expanding a template
14164 argument pack. */
14165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14167 if (argument == error_mark_node)
14169 cp_token *token = cp_lexer_peek_token (parser->lexer);
14170 error_at (token->location,
14171 "expected parameter pack before %<...%>");
14173 /* Consume the `...' token. */
14174 cp_lexer_consume_token (parser->lexer);
14176 /* Make the argument into a TYPE_PACK_EXPANSION or
14177 EXPR_PACK_EXPANSION. */
14178 argument = make_pack_expansion (argument);
14181 if (n_args == alloced)
14183 alloced *= 2;
14185 if (arg_ary == fixed_args)
14187 arg_ary = XNEWVEC (tree, alloced);
14188 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14190 else
14191 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14193 arg_ary[n_args++] = argument;
14195 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14197 vec = make_tree_vec (n_args);
14199 while (n_args--)
14200 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14202 if (arg_ary != fixed_args)
14203 free (arg_ary);
14204 parser->non_integral_constant_expression_p = saved_non_ice_p;
14205 parser->integral_constant_expression_p = saved_ice_p;
14206 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14207 #ifdef ENABLE_CHECKING
14208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14209 #endif
14210 return vec;
14213 /* Parse a template-argument.
14215 template-argument:
14216 assignment-expression
14217 type-id
14218 id-expression
14220 The representation is that of an assignment-expression, type-id, or
14221 id-expression -- except that the qualified id-expression is
14222 evaluated, so that the value returned is either a DECL or an
14223 OVERLOAD.
14225 Although the standard says "assignment-expression", it forbids
14226 throw-expressions or assignments in the template argument.
14227 Therefore, we use "conditional-expression" instead. */
14229 static tree
14230 cp_parser_template_argument (cp_parser* parser)
14232 tree argument;
14233 bool template_p;
14234 bool address_p;
14235 bool maybe_type_id = false;
14236 cp_token *token = NULL, *argument_start_token = NULL;
14237 location_t loc = 0;
14238 cp_id_kind idk;
14240 /* There's really no way to know what we're looking at, so we just
14241 try each alternative in order.
14243 [temp.arg]
14245 In a template-argument, an ambiguity between a type-id and an
14246 expression is resolved to a type-id, regardless of the form of
14247 the corresponding template-parameter.
14249 Therefore, we try a type-id first. */
14250 cp_parser_parse_tentatively (parser);
14251 argument = cp_parser_template_type_arg (parser);
14252 /* If there was no error parsing the type-id but the next token is a
14253 '>>', our behavior depends on which dialect of C++ we're
14254 parsing. In C++98, we probably found a typo for '> >'. But there
14255 are type-id which are also valid expressions. For instance:
14257 struct X { int operator >> (int); };
14258 template <int V> struct Foo {};
14259 Foo<X () >> 5> r;
14261 Here 'X()' is a valid type-id of a function type, but the user just
14262 wanted to write the expression "X() >> 5". Thus, we remember that we
14263 found a valid type-id, but we still try to parse the argument as an
14264 expression to see what happens.
14266 In C++0x, the '>>' will be considered two separate '>'
14267 tokens. */
14268 if (!cp_parser_error_occurred (parser)
14269 && cxx_dialect == cxx98
14270 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14272 maybe_type_id = true;
14273 cp_parser_abort_tentative_parse (parser);
14275 else
14277 /* If the next token isn't a `,' or a `>', then this argument wasn't
14278 really finished. This means that the argument is not a valid
14279 type-id. */
14280 if (!cp_parser_next_token_ends_template_argument_p (parser))
14281 cp_parser_error (parser, "expected template-argument");
14282 /* If that worked, we're done. */
14283 if (cp_parser_parse_definitely (parser))
14284 return argument;
14286 /* We're still not sure what the argument will be. */
14287 cp_parser_parse_tentatively (parser);
14288 /* Try a template. */
14289 argument_start_token = cp_lexer_peek_token (parser->lexer);
14290 argument = cp_parser_id_expression (parser,
14291 /*template_keyword_p=*/false,
14292 /*check_dependency_p=*/true,
14293 &template_p,
14294 /*declarator_p=*/false,
14295 /*optional_p=*/false);
14296 /* If the next token isn't a `,' or a `>', then this argument wasn't
14297 really finished. */
14298 if (!cp_parser_next_token_ends_template_argument_p (parser))
14299 cp_parser_error (parser, "expected template-argument");
14300 if (!cp_parser_error_occurred (parser))
14302 /* Figure out what is being referred to. If the id-expression
14303 was for a class template specialization, then we will have a
14304 TYPE_DECL at this point. There is no need to do name lookup
14305 at this point in that case. */
14306 if (TREE_CODE (argument) != TYPE_DECL)
14307 argument = cp_parser_lookup_name (parser, argument,
14308 none_type,
14309 /*is_template=*/template_p,
14310 /*is_namespace=*/false,
14311 /*check_dependency=*/true,
14312 /*ambiguous_decls=*/NULL,
14313 argument_start_token->location);
14314 if (TREE_CODE (argument) != TEMPLATE_DECL
14315 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14316 cp_parser_error (parser, "expected template-name");
14318 if (cp_parser_parse_definitely (parser))
14320 if (TREE_DEPRECATED (argument))
14321 warn_deprecated_use (argument, NULL_TREE);
14322 return argument;
14324 /* It must be a non-type argument. There permitted cases are given
14325 in [temp.arg.nontype]:
14327 -- an integral constant-expression of integral or enumeration
14328 type; or
14330 -- the name of a non-type template-parameter; or
14332 -- the name of an object or function with external linkage...
14334 -- the address of an object or function with external linkage...
14336 -- a pointer to member... */
14337 /* Look for a non-type template parameter. */
14338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14340 cp_parser_parse_tentatively (parser);
14341 argument = cp_parser_primary_expression (parser,
14342 /*address_p=*/false,
14343 /*cast_p=*/false,
14344 /*template_arg_p=*/true,
14345 &idk);
14346 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14347 || !cp_parser_next_token_ends_template_argument_p (parser))
14348 cp_parser_simulate_error (parser);
14349 if (cp_parser_parse_definitely (parser))
14350 return argument;
14353 /* If the next token is "&", the argument must be the address of an
14354 object or function with external linkage. */
14355 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14356 if (address_p)
14358 loc = cp_lexer_peek_token (parser->lexer)->location;
14359 cp_lexer_consume_token (parser->lexer);
14361 /* See if we might have an id-expression. */
14362 token = cp_lexer_peek_token (parser->lexer);
14363 if (token->type == CPP_NAME
14364 || token->keyword == RID_OPERATOR
14365 || token->type == CPP_SCOPE
14366 || token->type == CPP_TEMPLATE_ID
14367 || token->type == CPP_NESTED_NAME_SPECIFIER)
14369 cp_parser_parse_tentatively (parser);
14370 argument = cp_parser_primary_expression (parser,
14371 address_p,
14372 /*cast_p=*/false,
14373 /*template_arg_p=*/true,
14374 &idk);
14375 if (cp_parser_error_occurred (parser)
14376 || !cp_parser_next_token_ends_template_argument_p (parser))
14377 cp_parser_abort_tentative_parse (parser);
14378 else
14380 tree probe;
14382 if (INDIRECT_REF_P (argument))
14384 /* Strip the dereference temporarily. */
14385 gcc_assert (REFERENCE_REF_P (argument));
14386 argument = TREE_OPERAND (argument, 0);
14389 /* If we're in a template, we represent a qualified-id referring
14390 to a static data member as a SCOPE_REF even if the scope isn't
14391 dependent so that we can check access control later. */
14392 probe = argument;
14393 if (TREE_CODE (probe) == SCOPE_REF)
14394 probe = TREE_OPERAND (probe, 1);
14395 if (VAR_P (probe))
14397 /* A variable without external linkage might still be a
14398 valid constant-expression, so no error is issued here
14399 if the external-linkage check fails. */
14400 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14401 cp_parser_simulate_error (parser);
14403 else if (is_overloaded_fn (argument))
14404 /* All overloaded functions are allowed; if the external
14405 linkage test does not pass, an error will be issued
14406 later. */
14408 else if (address_p
14409 && (TREE_CODE (argument) == OFFSET_REF
14410 || TREE_CODE (argument) == SCOPE_REF))
14411 /* A pointer-to-member. */
14413 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14415 else
14416 cp_parser_simulate_error (parser);
14418 if (cp_parser_parse_definitely (parser))
14420 if (address_p)
14421 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14422 tf_warning_or_error);
14423 else
14424 argument = convert_from_reference (argument);
14425 return argument;
14429 /* If the argument started with "&", there are no other valid
14430 alternatives at this point. */
14431 if (address_p)
14433 cp_parser_error (parser, "invalid non-type template argument");
14434 return error_mark_node;
14437 /* If the argument wasn't successfully parsed as a type-id followed
14438 by '>>', the argument can only be a constant expression now.
14439 Otherwise, we try parsing the constant-expression tentatively,
14440 because the argument could really be a type-id. */
14441 if (maybe_type_id)
14442 cp_parser_parse_tentatively (parser);
14443 argument = cp_parser_constant_expression (parser);
14445 if (!maybe_type_id)
14446 return argument;
14447 if (!cp_parser_next_token_ends_template_argument_p (parser))
14448 cp_parser_error (parser, "expected template-argument");
14449 if (cp_parser_parse_definitely (parser))
14450 return argument;
14451 /* We did our best to parse the argument as a non type-id, but that
14452 was the only alternative that matched (albeit with a '>' after
14453 it). We can assume it's just a typo from the user, and a
14454 diagnostic will then be issued. */
14455 return cp_parser_template_type_arg (parser);
14458 /* Parse an explicit-instantiation.
14460 explicit-instantiation:
14461 template declaration
14463 Although the standard says `declaration', what it really means is:
14465 explicit-instantiation:
14466 template decl-specifier-seq [opt] declarator [opt] ;
14468 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14469 supposed to be allowed. A defect report has been filed about this
14470 issue.
14472 GNU Extension:
14474 explicit-instantiation:
14475 storage-class-specifier template
14476 decl-specifier-seq [opt] declarator [opt] ;
14477 function-specifier template
14478 decl-specifier-seq [opt] declarator [opt] ; */
14480 static void
14481 cp_parser_explicit_instantiation (cp_parser* parser)
14483 int declares_class_or_enum;
14484 cp_decl_specifier_seq decl_specifiers;
14485 tree extension_specifier = NULL_TREE;
14487 timevar_push (TV_TEMPLATE_INST);
14489 /* Look for an (optional) storage-class-specifier or
14490 function-specifier. */
14491 if (cp_parser_allow_gnu_extensions_p (parser))
14493 extension_specifier
14494 = cp_parser_storage_class_specifier_opt (parser);
14495 if (!extension_specifier)
14496 extension_specifier
14497 = cp_parser_function_specifier_opt (parser,
14498 /*decl_specs=*/NULL);
14501 /* Look for the `template' keyword. */
14502 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14503 /* Let the front end know that we are processing an explicit
14504 instantiation. */
14505 begin_explicit_instantiation ();
14506 /* [temp.explicit] says that we are supposed to ignore access
14507 control while processing explicit instantiation directives. */
14508 push_deferring_access_checks (dk_no_check);
14509 /* Parse a decl-specifier-seq. */
14510 cp_parser_decl_specifier_seq (parser,
14511 CP_PARSER_FLAGS_OPTIONAL,
14512 &decl_specifiers,
14513 &declares_class_or_enum);
14514 /* If there was exactly one decl-specifier, and it declared a class,
14515 and there's no declarator, then we have an explicit type
14516 instantiation. */
14517 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14519 tree type;
14521 type = check_tag_decl (&decl_specifiers,
14522 /*explicit_type_instantiation_p=*/true);
14523 /* Turn access control back on for names used during
14524 template instantiation. */
14525 pop_deferring_access_checks ();
14526 if (type)
14527 do_type_instantiation (type, extension_specifier,
14528 /*complain=*/tf_error);
14530 else
14532 cp_declarator *declarator;
14533 tree decl;
14535 /* Parse the declarator. */
14536 declarator
14537 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14538 /*ctor_dtor_or_conv_p=*/NULL,
14539 /*parenthesized_p=*/NULL,
14540 /*member_p=*/false,
14541 /*friend_p=*/false);
14542 if (declares_class_or_enum & 2)
14543 cp_parser_check_for_definition_in_return_type (declarator,
14544 decl_specifiers.type,
14545 decl_specifiers.locations[ds_type_spec]);
14546 if (declarator != cp_error_declarator)
14548 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14549 permerror (decl_specifiers.locations[ds_inline],
14550 "explicit instantiation shall not use"
14551 " %<inline%> specifier");
14552 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14553 permerror (decl_specifiers.locations[ds_constexpr],
14554 "explicit instantiation shall not use"
14555 " %<constexpr%> specifier");
14557 decl = grokdeclarator (declarator, &decl_specifiers,
14558 NORMAL, 0, &decl_specifiers.attributes);
14559 /* Turn access control back on for names used during
14560 template instantiation. */
14561 pop_deferring_access_checks ();
14562 /* Do the explicit instantiation. */
14563 do_decl_instantiation (decl, extension_specifier);
14565 else
14567 pop_deferring_access_checks ();
14568 /* Skip the body of the explicit instantiation. */
14569 cp_parser_skip_to_end_of_statement (parser);
14572 /* We're done with the instantiation. */
14573 end_explicit_instantiation ();
14575 cp_parser_consume_semicolon_at_end_of_statement (parser);
14577 timevar_pop (TV_TEMPLATE_INST);
14580 /* Parse an explicit-specialization.
14582 explicit-specialization:
14583 template < > declaration
14585 Although the standard says `declaration', what it really means is:
14587 explicit-specialization:
14588 template <> decl-specifier [opt] init-declarator [opt] ;
14589 template <> function-definition
14590 template <> explicit-specialization
14591 template <> template-declaration */
14593 static void
14594 cp_parser_explicit_specialization (cp_parser* parser)
14596 bool need_lang_pop;
14597 cp_token *token = cp_lexer_peek_token (parser->lexer);
14599 /* Look for the `template' keyword. */
14600 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14601 /* Look for the `<'. */
14602 cp_parser_require (parser, CPP_LESS, RT_LESS);
14603 /* Look for the `>'. */
14604 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14605 /* We have processed another parameter list. */
14606 ++parser->num_template_parameter_lists;
14607 /* [temp]
14609 A template ... explicit specialization ... shall not have C
14610 linkage. */
14611 if (current_lang_name == lang_name_c)
14613 error_at (token->location, "template specialization with C linkage");
14614 /* Give it C++ linkage to avoid confusing other parts of the
14615 front end. */
14616 push_lang_context (lang_name_cplusplus);
14617 need_lang_pop = true;
14619 else
14620 need_lang_pop = false;
14621 /* Let the front end know that we are beginning a specialization. */
14622 if (!begin_specialization ())
14624 end_specialization ();
14625 return;
14628 /* If the next keyword is `template', we need to figure out whether
14629 or not we're looking a template-declaration. */
14630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14632 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14633 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14634 cp_parser_template_declaration_after_export (parser,
14635 /*member_p=*/false);
14636 else
14637 cp_parser_explicit_specialization (parser);
14639 else
14640 /* Parse the dependent declaration. */
14641 cp_parser_single_declaration (parser,
14642 /*checks=*/NULL,
14643 /*member_p=*/false,
14644 /*explicit_specialization_p=*/true,
14645 /*friend_p=*/NULL);
14646 /* We're done with the specialization. */
14647 end_specialization ();
14648 /* For the erroneous case of a template with C linkage, we pushed an
14649 implicit C++ linkage scope; exit that scope now. */
14650 if (need_lang_pop)
14651 pop_lang_context ();
14652 /* We're done with this parameter list. */
14653 --parser->num_template_parameter_lists;
14656 /* Parse a type-specifier.
14658 type-specifier:
14659 simple-type-specifier
14660 class-specifier
14661 enum-specifier
14662 elaborated-type-specifier
14663 cv-qualifier
14665 GNU Extension:
14667 type-specifier:
14668 __complex__
14670 Returns a representation of the type-specifier. For a
14671 class-specifier, enum-specifier, or elaborated-type-specifier, a
14672 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14674 The parser flags FLAGS is used to control type-specifier parsing.
14676 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14677 in a decl-specifier-seq.
14679 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14680 class-specifier, enum-specifier, or elaborated-type-specifier, then
14681 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14682 if a type is declared; 2 if it is defined. Otherwise, it is set to
14683 zero.
14685 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14686 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14687 is set to FALSE. */
14689 static tree
14690 cp_parser_type_specifier (cp_parser* parser,
14691 cp_parser_flags flags,
14692 cp_decl_specifier_seq *decl_specs,
14693 bool is_declaration,
14694 int* declares_class_or_enum,
14695 bool* is_cv_qualifier)
14697 tree type_spec = NULL_TREE;
14698 cp_token *token;
14699 enum rid keyword;
14700 cp_decl_spec ds = ds_last;
14702 /* Assume this type-specifier does not declare a new type. */
14703 if (declares_class_or_enum)
14704 *declares_class_or_enum = 0;
14705 /* And that it does not specify a cv-qualifier. */
14706 if (is_cv_qualifier)
14707 *is_cv_qualifier = false;
14708 /* Peek at the next token. */
14709 token = cp_lexer_peek_token (parser->lexer);
14711 /* If we're looking at a keyword, we can use that to guide the
14712 production we choose. */
14713 keyword = token->keyword;
14714 switch (keyword)
14716 case RID_ENUM:
14717 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14718 goto elaborated_type_specifier;
14720 /* Look for the enum-specifier. */
14721 type_spec = cp_parser_enum_specifier (parser);
14722 /* If that worked, we're done. */
14723 if (type_spec)
14725 if (declares_class_or_enum)
14726 *declares_class_or_enum = 2;
14727 if (decl_specs)
14728 cp_parser_set_decl_spec_type (decl_specs,
14729 type_spec,
14730 token,
14731 /*type_definition_p=*/true);
14732 return type_spec;
14734 else
14735 goto elaborated_type_specifier;
14737 /* Any of these indicate either a class-specifier, or an
14738 elaborated-type-specifier. */
14739 case RID_CLASS:
14740 case RID_STRUCT:
14741 case RID_UNION:
14742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14743 goto elaborated_type_specifier;
14745 /* Parse tentatively so that we can back up if we don't find a
14746 class-specifier. */
14747 cp_parser_parse_tentatively (parser);
14748 /* Look for the class-specifier. */
14749 type_spec = cp_parser_class_specifier (parser);
14750 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14751 /* If that worked, we're done. */
14752 if (cp_parser_parse_definitely (parser))
14754 if (declares_class_or_enum)
14755 *declares_class_or_enum = 2;
14756 if (decl_specs)
14757 cp_parser_set_decl_spec_type (decl_specs,
14758 type_spec,
14759 token,
14760 /*type_definition_p=*/true);
14761 return type_spec;
14764 /* Fall through. */
14765 elaborated_type_specifier:
14766 /* We're declaring (not defining) a class or enum. */
14767 if (declares_class_or_enum)
14768 *declares_class_or_enum = 1;
14770 /* Fall through. */
14771 case RID_TYPENAME:
14772 /* Look for an elaborated-type-specifier. */
14773 type_spec
14774 = (cp_parser_elaborated_type_specifier
14775 (parser,
14776 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14777 is_declaration));
14778 if (decl_specs)
14779 cp_parser_set_decl_spec_type (decl_specs,
14780 type_spec,
14781 token,
14782 /*type_definition_p=*/false);
14783 return type_spec;
14785 case RID_CONST:
14786 ds = ds_const;
14787 if (is_cv_qualifier)
14788 *is_cv_qualifier = true;
14789 break;
14791 case RID_VOLATILE:
14792 ds = ds_volatile;
14793 if (is_cv_qualifier)
14794 *is_cv_qualifier = true;
14795 break;
14797 case RID_RESTRICT:
14798 ds = ds_restrict;
14799 if (is_cv_qualifier)
14800 *is_cv_qualifier = true;
14801 break;
14803 case RID_COMPLEX:
14804 /* The `__complex__' keyword is a GNU extension. */
14805 ds = ds_complex;
14806 break;
14808 default:
14809 break;
14812 /* Handle simple keywords. */
14813 if (ds != ds_last)
14815 if (decl_specs)
14817 set_and_check_decl_spec_loc (decl_specs, ds, token);
14818 decl_specs->any_specifiers_p = true;
14820 return cp_lexer_consume_token (parser->lexer)->u.value;
14823 /* If we do not already have a type-specifier, assume we are looking
14824 at a simple-type-specifier. */
14825 type_spec = cp_parser_simple_type_specifier (parser,
14826 decl_specs,
14827 flags);
14829 /* If we didn't find a type-specifier, and a type-specifier was not
14830 optional in this context, issue an error message. */
14831 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14833 cp_parser_error (parser, "expected type specifier");
14834 return error_mark_node;
14837 return type_spec;
14840 /* Parse a simple-type-specifier.
14842 simple-type-specifier:
14843 :: [opt] nested-name-specifier [opt] type-name
14844 :: [opt] nested-name-specifier template template-id
14845 char
14846 wchar_t
14847 bool
14848 short
14850 long
14851 signed
14852 unsigned
14853 float
14854 double
14855 void
14857 C++0x Extension:
14859 simple-type-specifier:
14860 auto
14861 decltype ( expression )
14862 char16_t
14863 char32_t
14864 __underlying_type ( type-id )
14866 GNU Extension:
14868 simple-type-specifier:
14869 __int128
14870 __typeof__ unary-expression
14871 __typeof__ ( type-id )
14872 __typeof__ ( type-id ) { initializer-list , [opt] }
14874 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14875 appropriately updated. */
14877 static tree
14878 cp_parser_simple_type_specifier (cp_parser* parser,
14879 cp_decl_specifier_seq *decl_specs,
14880 cp_parser_flags flags)
14882 tree type = NULL_TREE;
14883 cp_token *token;
14884 int idx;
14886 /* Peek at the next token. */
14887 token = cp_lexer_peek_token (parser->lexer);
14889 /* If we're looking at a keyword, things are easy. */
14890 switch (token->keyword)
14892 case RID_CHAR:
14893 if (decl_specs)
14894 decl_specs->explicit_char_p = true;
14895 type = char_type_node;
14896 break;
14897 case RID_CHAR16:
14898 type = char16_type_node;
14899 break;
14900 case RID_CHAR32:
14901 type = char32_type_node;
14902 break;
14903 case RID_WCHAR:
14904 type = wchar_type_node;
14905 break;
14906 case RID_BOOL:
14907 type = boolean_type_node;
14908 break;
14909 case RID_SHORT:
14910 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14911 type = short_integer_type_node;
14912 break;
14913 case RID_INT:
14914 if (decl_specs)
14915 decl_specs->explicit_int_p = true;
14916 type = integer_type_node;
14917 break;
14918 case RID_INT_N_0:
14919 case RID_INT_N_1:
14920 case RID_INT_N_2:
14921 case RID_INT_N_3:
14922 idx = token->keyword - RID_INT_N_0;
14923 if (! int_n_enabled_p [idx])
14924 break;
14925 if (decl_specs)
14927 decl_specs->explicit_intN_p = true;
14928 decl_specs->int_n_idx = idx;
14930 type = int_n_trees [idx].signed_type;
14931 break;
14932 case RID_LONG:
14933 if (decl_specs)
14934 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14935 type = long_integer_type_node;
14936 break;
14937 case RID_SIGNED:
14938 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14939 type = integer_type_node;
14940 break;
14941 case RID_UNSIGNED:
14942 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14943 type = unsigned_type_node;
14944 break;
14945 case RID_FLOAT:
14946 type = float_type_node;
14947 break;
14948 case RID_DOUBLE:
14949 type = double_type_node;
14950 break;
14951 case RID_VOID:
14952 type = void_type_node;
14953 break;
14955 case RID_AUTO:
14956 maybe_warn_cpp0x (CPP0X_AUTO);
14957 if (parser->auto_is_implicit_function_template_parm_p)
14959 /* The 'auto' might be the placeholder return type for a function decl
14960 with trailing return type. */
14961 bool have_trailing_return_fn_decl = false;
14962 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14963 == CPP_OPEN_PAREN)
14965 cp_parser_parse_tentatively (parser);
14966 cp_lexer_consume_token (parser->lexer);
14967 cp_lexer_consume_token (parser->lexer);
14968 if (cp_parser_skip_to_closing_parenthesis (parser,
14969 /*recovering*/false,
14970 /*or_comma*/false,
14971 /*consume_paren*/true))
14972 have_trailing_return_fn_decl
14973 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14974 cp_parser_abort_tentative_parse (parser);
14977 if (have_trailing_return_fn_decl)
14979 type = make_auto ();
14980 break;
14983 if (cxx_dialect >= cxx14)
14984 type = synthesize_implicit_template_parm (parser);
14985 else
14986 type = error_mark_node;
14988 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14990 if (cxx_dialect < cxx14)
14991 error_at (token->location,
14992 "use of %<auto%> in lambda parameter declaration "
14993 "only available with "
14994 "-std=c++14 or -std=gnu++14");
14996 else if (cxx_dialect < cxx14)
14997 error_at (token->location,
14998 "use of %<auto%> in parameter declaration "
14999 "only available with "
15000 "-std=c++14 or -std=gnu++14");
15001 else
15002 pedwarn (token->location, OPT_Wpedantic,
15003 "ISO C++ forbids use of %<auto%> in parameter "
15004 "declaration");
15006 else
15007 type = make_auto ();
15008 break;
15010 case RID_DECLTYPE:
15011 /* Since DR 743, decltype can either be a simple-type-specifier by
15012 itself or begin a nested-name-specifier. Parsing it will replace
15013 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15014 handling below decide what to do. */
15015 cp_parser_decltype (parser);
15016 cp_lexer_set_token_position (parser->lexer, token);
15017 break;
15019 case RID_TYPEOF:
15020 /* Consume the `typeof' token. */
15021 cp_lexer_consume_token (parser->lexer);
15022 /* Parse the operand to `typeof'. */
15023 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15024 /* If it is not already a TYPE, take its type. */
15025 if (!TYPE_P (type))
15026 type = finish_typeof (type);
15028 if (decl_specs)
15029 cp_parser_set_decl_spec_type (decl_specs, type,
15030 token,
15031 /*type_definition_p=*/false);
15033 return type;
15035 case RID_UNDERLYING_TYPE:
15036 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15037 if (decl_specs)
15038 cp_parser_set_decl_spec_type (decl_specs, type,
15039 token,
15040 /*type_definition_p=*/false);
15042 return type;
15044 case RID_BASES:
15045 case RID_DIRECT_BASES:
15046 type = cp_parser_trait_expr (parser, token->keyword);
15047 if (decl_specs)
15048 cp_parser_set_decl_spec_type (decl_specs, type,
15049 token,
15050 /*type_definition_p=*/false);
15051 return type;
15052 default:
15053 break;
15056 /* If token is an already-parsed decltype not followed by ::,
15057 it's a simple-type-specifier. */
15058 if (token->type == CPP_DECLTYPE
15059 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15061 type = token->u.value;
15062 if (decl_specs)
15064 cp_parser_set_decl_spec_type (decl_specs, type,
15065 token,
15066 /*type_definition_p=*/false);
15067 /* Remember that we are handling a decltype in order to
15068 implement the resolution of DR 1510 when the argument
15069 isn't instantiation dependent. */
15070 decl_specs->decltype_p = true;
15072 cp_lexer_consume_token (parser->lexer);
15073 return type;
15076 /* If the type-specifier was for a built-in type, we're done. */
15077 if (type)
15079 /* Record the type. */
15080 if (decl_specs
15081 && (token->keyword != RID_SIGNED
15082 && token->keyword != RID_UNSIGNED
15083 && token->keyword != RID_SHORT
15084 && token->keyword != RID_LONG))
15085 cp_parser_set_decl_spec_type (decl_specs,
15086 type,
15087 token,
15088 /*type_definition_p=*/false);
15089 if (decl_specs)
15090 decl_specs->any_specifiers_p = true;
15092 /* Consume the token. */
15093 cp_lexer_consume_token (parser->lexer);
15095 if (type == error_mark_node)
15096 return error_mark_node;
15098 /* There is no valid C++ program where a non-template type is
15099 followed by a "<". That usually indicates that the user thought
15100 that the type was a template. */
15101 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15102 token->location);
15104 return TYPE_NAME (type);
15107 /* The type-specifier must be a user-defined type. */
15108 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15110 bool qualified_p;
15111 bool global_p;
15113 /* Don't gobble tokens or issue error messages if this is an
15114 optional type-specifier. */
15115 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15116 cp_parser_parse_tentatively (parser);
15118 /* Look for the optional `::' operator. */
15119 global_p
15120 = (cp_parser_global_scope_opt (parser,
15121 /*current_scope_valid_p=*/false)
15122 != NULL_TREE);
15123 /* Look for the nested-name specifier. */
15124 qualified_p
15125 = (cp_parser_nested_name_specifier_opt (parser,
15126 /*typename_keyword_p=*/false,
15127 /*check_dependency_p=*/true,
15128 /*type_p=*/false,
15129 /*is_declaration=*/false)
15130 != NULL_TREE);
15131 token = cp_lexer_peek_token (parser->lexer);
15132 /* If we have seen a nested-name-specifier, and the next token
15133 is `template', then we are using the template-id production. */
15134 if (parser->scope
15135 && cp_parser_optional_template_keyword (parser))
15137 /* Look for the template-id. */
15138 type = cp_parser_template_id (parser,
15139 /*template_keyword_p=*/true,
15140 /*check_dependency_p=*/true,
15141 none_type,
15142 /*is_declaration=*/false);
15143 /* If the template-id did not name a type, we are out of
15144 luck. */
15145 if (TREE_CODE (type) != TYPE_DECL)
15147 cp_parser_error (parser, "expected template-id for type");
15148 type = NULL_TREE;
15151 /* Otherwise, look for a type-name. */
15152 else
15153 type = cp_parser_type_name (parser);
15154 /* Keep track of all name-lookups performed in class scopes. */
15155 if (type
15156 && !global_p
15157 && !qualified_p
15158 && TREE_CODE (type) == TYPE_DECL
15159 && identifier_p (DECL_NAME (type)))
15160 maybe_note_name_used_in_class (DECL_NAME (type), type);
15161 /* If it didn't work out, we don't have a TYPE. */
15162 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15163 && !cp_parser_parse_definitely (parser))
15164 type = NULL_TREE;
15165 if (type && decl_specs)
15166 cp_parser_set_decl_spec_type (decl_specs, type,
15167 token,
15168 /*type_definition_p=*/false);
15171 /* If we didn't get a type-name, issue an error message. */
15172 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15174 cp_parser_error (parser, "expected type-name");
15175 return error_mark_node;
15178 if (type && type != error_mark_node)
15180 /* See if TYPE is an Objective-C type, and if so, parse and
15181 accept any protocol references following it. Do this before
15182 the cp_parser_check_for_invalid_template_id() call, because
15183 Objective-C types can be followed by '<...>' which would
15184 enclose protocol names rather than template arguments, and so
15185 everything is fine. */
15186 if (c_dialect_objc () && !parser->scope
15187 && (objc_is_id (type) || objc_is_class_name (type)))
15189 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15190 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15192 /* Clobber the "unqualified" type previously entered into
15193 DECL_SPECS with the new, improved protocol-qualified version. */
15194 if (decl_specs)
15195 decl_specs->type = qual_type;
15197 return qual_type;
15200 /* There is no valid C++ program where a non-template type is
15201 followed by a "<". That usually indicates that the user
15202 thought that the type was a template. */
15203 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15204 none_type,
15205 token->location);
15208 return type;
15211 /* Parse a type-name.
15213 type-name:
15214 class-name
15215 enum-name
15216 typedef-name
15217 simple-template-id [in c++0x]
15219 enum-name:
15220 identifier
15222 typedef-name:
15223 identifier
15225 Returns a TYPE_DECL for the type. */
15227 static tree
15228 cp_parser_type_name (cp_parser* parser)
15230 tree type_decl;
15232 /* We can't know yet whether it is a class-name or not. */
15233 cp_parser_parse_tentatively (parser);
15234 /* Try a class-name. */
15235 type_decl = cp_parser_class_name (parser,
15236 /*typename_keyword_p=*/false,
15237 /*template_keyword_p=*/false,
15238 none_type,
15239 /*check_dependency_p=*/true,
15240 /*class_head_p=*/false,
15241 /*is_declaration=*/false);
15242 /* If it's not a class-name, keep looking. */
15243 if (!cp_parser_parse_definitely (parser))
15245 if (cxx_dialect < cxx11)
15246 /* It must be a typedef-name or an enum-name. */
15247 return cp_parser_nonclass_name (parser);
15249 cp_parser_parse_tentatively (parser);
15250 /* It is either a simple-template-id representing an
15251 instantiation of an alias template... */
15252 type_decl = cp_parser_template_id (parser,
15253 /*template_keyword_p=*/false,
15254 /*check_dependency_p=*/true,
15255 none_type,
15256 /*is_declaration=*/false);
15257 /* Note that this must be an instantiation of an alias template
15258 because [temp.names]/6 says:
15260 A template-id that names an alias template specialization
15261 is a type-name.
15263 Whereas [temp.names]/7 says:
15265 A simple-template-id that names a class template
15266 specialization is a class-name. */
15267 if (type_decl != NULL_TREE
15268 && TREE_CODE (type_decl) == TYPE_DECL
15269 && TYPE_DECL_ALIAS_P (type_decl))
15270 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15271 else
15272 cp_parser_simulate_error (parser);
15274 if (!cp_parser_parse_definitely (parser))
15275 /* ... Or a typedef-name or an enum-name. */
15276 return cp_parser_nonclass_name (parser);
15279 return type_decl;
15282 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15284 enum-name:
15285 identifier
15287 typedef-name:
15288 identifier
15290 Returns a TYPE_DECL for the type. */
15292 static tree
15293 cp_parser_nonclass_name (cp_parser* parser)
15295 tree type_decl;
15296 tree identifier;
15298 cp_token *token = cp_lexer_peek_token (parser->lexer);
15299 identifier = cp_parser_identifier (parser);
15300 if (identifier == error_mark_node)
15301 return error_mark_node;
15303 /* Look up the type-name. */
15304 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15306 type_decl = strip_using_decl (type_decl);
15308 if (TREE_CODE (type_decl) != TYPE_DECL
15309 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15311 /* See if this is an Objective-C type. */
15312 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15313 tree type = objc_get_protocol_qualified_type (identifier, protos);
15314 if (type)
15315 type_decl = TYPE_NAME (type);
15318 /* Issue an error if we did not find a type-name. */
15319 if (TREE_CODE (type_decl) != TYPE_DECL
15320 /* In Objective-C, we have the complication that class names are
15321 normally type names and start declarations (eg, the
15322 "NSObject" in "NSObject *object;"), but can be used in an
15323 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15324 is an expression. So, a classname followed by a dot is not a
15325 valid type-name. */
15326 || (objc_is_class_name (TREE_TYPE (type_decl))
15327 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15329 if (!cp_parser_simulate_error (parser))
15330 cp_parser_name_lookup_error (parser, identifier, type_decl,
15331 NLE_TYPE, token->location);
15332 return error_mark_node;
15334 /* Remember that the name was used in the definition of the
15335 current class so that we can check later to see if the
15336 meaning would have been different after the class was
15337 entirely defined. */
15338 else if (type_decl != error_mark_node
15339 && !parser->scope)
15340 maybe_note_name_used_in_class (identifier, type_decl);
15342 return type_decl;
15345 /* Parse an elaborated-type-specifier. Note that the grammar given
15346 here incorporates the resolution to DR68.
15348 elaborated-type-specifier:
15349 class-key :: [opt] nested-name-specifier [opt] identifier
15350 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15351 enum-key :: [opt] nested-name-specifier [opt] identifier
15352 typename :: [opt] nested-name-specifier identifier
15353 typename :: [opt] nested-name-specifier template [opt]
15354 template-id
15356 GNU extension:
15358 elaborated-type-specifier:
15359 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15360 class-key attributes :: [opt] nested-name-specifier [opt]
15361 template [opt] template-id
15362 enum attributes :: [opt] nested-name-specifier [opt] identifier
15364 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15365 declared `friend'. If IS_DECLARATION is TRUE, then this
15366 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15367 something is being declared.
15369 Returns the TYPE specified. */
15371 static tree
15372 cp_parser_elaborated_type_specifier (cp_parser* parser,
15373 bool is_friend,
15374 bool is_declaration)
15376 enum tag_types tag_type;
15377 tree identifier;
15378 tree type = NULL_TREE;
15379 tree attributes = NULL_TREE;
15380 tree globalscope;
15381 cp_token *token = NULL;
15383 /* See if we're looking at the `enum' keyword. */
15384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15386 /* Consume the `enum' token. */
15387 cp_lexer_consume_token (parser->lexer);
15388 /* Remember that it's an enumeration type. */
15389 tag_type = enum_type;
15390 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15391 enums) is used here. */
15392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15393 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15395 pedwarn (input_location, 0, "elaborated-type-specifier "
15396 "for a scoped enum must not use the %<%D%> keyword",
15397 cp_lexer_peek_token (parser->lexer)->u.value);
15398 /* Consume the `struct' or `class' and parse it anyway. */
15399 cp_lexer_consume_token (parser->lexer);
15401 /* Parse the attributes. */
15402 attributes = cp_parser_attributes_opt (parser);
15404 /* Or, it might be `typename'. */
15405 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15406 RID_TYPENAME))
15408 /* Consume the `typename' token. */
15409 cp_lexer_consume_token (parser->lexer);
15410 /* Remember that it's a `typename' type. */
15411 tag_type = typename_type;
15413 /* Otherwise it must be a class-key. */
15414 else
15416 tag_type = cp_parser_class_key (parser);
15417 if (tag_type == none_type)
15418 return error_mark_node;
15419 /* Parse the attributes. */
15420 attributes = cp_parser_attributes_opt (parser);
15423 /* Look for the `::' operator. */
15424 globalscope = cp_parser_global_scope_opt (parser,
15425 /*current_scope_valid_p=*/false);
15426 /* Look for the nested-name-specifier. */
15427 if (tag_type == typename_type && !globalscope)
15429 if (!cp_parser_nested_name_specifier (parser,
15430 /*typename_keyword_p=*/true,
15431 /*check_dependency_p=*/true,
15432 /*type_p=*/true,
15433 is_declaration))
15434 return error_mark_node;
15436 else
15437 /* Even though `typename' is not present, the proposed resolution
15438 to Core Issue 180 says that in `class A<T>::B', `B' should be
15439 considered a type-name, even if `A<T>' is dependent. */
15440 cp_parser_nested_name_specifier_opt (parser,
15441 /*typename_keyword_p=*/true,
15442 /*check_dependency_p=*/true,
15443 /*type_p=*/true,
15444 is_declaration);
15445 /* For everything but enumeration types, consider a template-id.
15446 For an enumeration type, consider only a plain identifier. */
15447 if (tag_type != enum_type)
15449 bool template_p = false;
15450 tree decl;
15452 /* Allow the `template' keyword. */
15453 template_p = cp_parser_optional_template_keyword (parser);
15454 /* If we didn't see `template', we don't know if there's a
15455 template-id or not. */
15456 if (!template_p)
15457 cp_parser_parse_tentatively (parser);
15458 /* Parse the template-id. */
15459 token = cp_lexer_peek_token (parser->lexer);
15460 decl = cp_parser_template_id (parser, template_p,
15461 /*check_dependency_p=*/true,
15462 tag_type,
15463 is_declaration);
15464 /* If we didn't find a template-id, look for an ordinary
15465 identifier. */
15466 if (!template_p && !cp_parser_parse_definitely (parser))
15468 /* We can get here when cp_parser_template_id, called by
15469 cp_parser_class_name with tag_type == none_type, succeeds
15470 and caches a BASELINK. Then, when called again here,
15471 instead of failing and returning an error_mark_node
15472 returns it (see template/typename17.C in C++11).
15473 ??? Could we diagnose this earlier? */
15474 else if (tag_type == typename_type && BASELINK_P (decl))
15476 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15477 type = error_mark_node;
15479 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15480 in effect, then we must assume that, upon instantiation, the
15481 template will correspond to a class. */
15482 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15483 && tag_type == typename_type)
15484 type = make_typename_type (parser->scope, decl,
15485 typename_type,
15486 /*complain=*/tf_error);
15487 /* If the `typename' keyword is in effect and DECL is not a type
15488 decl, then type is non existent. */
15489 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15491 else if (TREE_CODE (decl) == TYPE_DECL)
15492 type = check_elaborated_type_specifier (tag_type, decl,
15493 /*allow_template_p=*/true);
15494 else if (decl == error_mark_node)
15495 type = error_mark_node;
15498 if (!type)
15500 token = cp_lexer_peek_token (parser->lexer);
15501 identifier = cp_parser_identifier (parser);
15503 if (identifier == error_mark_node)
15505 parser->scope = NULL_TREE;
15506 return error_mark_node;
15509 /* For a `typename', we needn't call xref_tag. */
15510 if (tag_type == typename_type
15511 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15512 return cp_parser_make_typename_type (parser, identifier,
15513 token->location);
15515 /* Template parameter lists apply only if we are not within a
15516 function parameter list. */
15517 bool template_parm_lists_apply
15518 = parser->num_template_parameter_lists;
15519 if (template_parm_lists_apply)
15520 for (cp_binding_level *s = current_binding_level;
15521 s && s->kind != sk_template_parms;
15522 s = s->level_chain)
15523 if (s->kind == sk_function_parms)
15524 template_parm_lists_apply = false;
15526 /* Look up a qualified name in the usual way. */
15527 if (parser->scope)
15529 tree decl;
15530 tree ambiguous_decls;
15532 decl = cp_parser_lookup_name (parser, identifier,
15533 tag_type,
15534 /*is_template=*/false,
15535 /*is_namespace=*/false,
15536 /*check_dependency=*/true,
15537 &ambiguous_decls,
15538 token->location);
15540 /* If the lookup was ambiguous, an error will already have been
15541 issued. */
15542 if (ambiguous_decls)
15543 return error_mark_node;
15545 /* If we are parsing friend declaration, DECL may be a
15546 TEMPLATE_DECL tree node here. However, we need to check
15547 whether this TEMPLATE_DECL results in valid code. Consider
15548 the following example:
15550 namespace N {
15551 template <class T> class C {};
15553 class X {
15554 template <class T> friend class N::C; // #1, valid code
15556 template <class T> class Y {
15557 friend class N::C; // #2, invalid code
15560 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15561 name lookup of `N::C'. We see that friend declaration must
15562 be template for the code to be valid. Note that
15563 processing_template_decl does not work here since it is
15564 always 1 for the above two cases. */
15566 decl = (cp_parser_maybe_treat_template_as_class
15567 (decl, /*tag_name_p=*/is_friend
15568 && template_parm_lists_apply));
15570 if (TREE_CODE (decl) != TYPE_DECL)
15572 cp_parser_diagnose_invalid_type_name (parser,
15573 identifier,
15574 token->location);
15575 return error_mark_node;
15578 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15580 bool allow_template = (template_parm_lists_apply
15581 || DECL_SELF_REFERENCE_P (decl));
15582 type = check_elaborated_type_specifier (tag_type, decl,
15583 allow_template);
15585 if (type == error_mark_node)
15586 return error_mark_node;
15589 /* Forward declarations of nested types, such as
15591 class C1::C2;
15592 class C1::C2::C3;
15594 are invalid unless all components preceding the final '::'
15595 are complete. If all enclosing types are complete, these
15596 declarations become merely pointless.
15598 Invalid forward declarations of nested types are errors
15599 caught elsewhere in parsing. Those that are pointless arrive
15600 here. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15603 && !is_friend && !processing_explicit_instantiation)
15604 warning (0, "declaration %qD does not declare anything", decl);
15606 type = TREE_TYPE (decl);
15608 else
15610 /* An elaborated-type-specifier sometimes introduces a new type and
15611 sometimes names an existing type. Normally, the rule is that it
15612 introduces a new type only if there is not an existing type of
15613 the same name already in scope. For example, given:
15615 struct S {};
15616 void f() { struct S s; }
15618 the `struct S' in the body of `f' is the same `struct S' as in
15619 the global scope; the existing definition is used. However, if
15620 there were no global declaration, this would introduce a new
15621 local class named `S'.
15623 An exception to this rule applies to the following code:
15625 namespace N { struct S; }
15627 Here, the elaborated-type-specifier names a new type
15628 unconditionally; even if there is already an `S' in the
15629 containing scope this declaration names a new type.
15630 This exception only applies if the elaborated-type-specifier
15631 forms the complete declaration:
15633 [class.name]
15635 A declaration consisting solely of `class-key identifier ;' is
15636 either a redeclaration of the name in the current scope or a
15637 forward declaration of the identifier as a class name. It
15638 introduces the name into the current scope.
15640 We are in this situation precisely when the next token is a `;'.
15642 An exception to the exception is that a `friend' declaration does
15643 *not* name a new type; i.e., given:
15645 struct S { friend struct T; };
15647 `T' is not a new type in the scope of `S'.
15649 Also, `new struct S' or `sizeof (struct S)' never results in the
15650 definition of a new type; a new type can only be declared in a
15651 declaration context. */
15653 tag_scope ts;
15654 bool template_p;
15656 if (is_friend)
15657 /* Friends have special name lookup rules. */
15658 ts = ts_within_enclosing_non_class;
15659 else if (is_declaration
15660 && cp_lexer_next_token_is (parser->lexer,
15661 CPP_SEMICOLON))
15662 /* This is a `class-key identifier ;' */
15663 ts = ts_current;
15664 else
15665 ts = ts_global;
15667 template_p =
15668 (template_parm_lists_apply
15669 && (cp_parser_next_token_starts_class_definition_p (parser)
15670 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15671 /* An unqualified name was used to reference this type, so
15672 there were no qualifying templates. */
15673 if (template_parm_lists_apply
15674 && !cp_parser_check_template_parameters (parser,
15675 /*num_templates=*/0,
15676 token->location,
15677 /*declarator=*/NULL))
15678 return error_mark_node;
15679 type = xref_tag (tag_type, identifier, ts, template_p);
15683 if (type == error_mark_node)
15684 return error_mark_node;
15686 /* Allow attributes on forward declarations of classes. */
15687 if (attributes)
15689 if (TREE_CODE (type) == TYPENAME_TYPE)
15690 warning (OPT_Wattributes,
15691 "attributes ignored on uninstantiated type");
15692 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15693 && ! processing_explicit_instantiation)
15694 warning (OPT_Wattributes,
15695 "attributes ignored on template instantiation");
15696 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15697 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15698 else
15699 warning (OPT_Wattributes,
15700 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15703 if (tag_type != enum_type)
15705 /* Indicate whether this class was declared as a `class' or as a
15706 `struct'. */
15707 if (TREE_CODE (type) == RECORD_TYPE)
15708 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15709 cp_parser_check_class_key (tag_type, type);
15712 /* A "<" cannot follow an elaborated type specifier. If that
15713 happens, the user was probably trying to form a template-id. */
15714 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15715 token->location);
15717 return type;
15720 /* Parse an enum-specifier.
15722 enum-specifier:
15723 enum-head { enumerator-list [opt] }
15724 enum-head { enumerator-list , } [C++0x]
15726 enum-head:
15727 enum-key identifier [opt] enum-base [opt]
15728 enum-key nested-name-specifier identifier enum-base [opt]
15730 enum-key:
15731 enum
15732 enum class [C++0x]
15733 enum struct [C++0x]
15735 enum-base: [C++0x]
15736 : type-specifier-seq
15738 opaque-enum-specifier:
15739 enum-key identifier enum-base [opt] ;
15741 GNU Extensions:
15742 enum-key attributes[opt] identifier [opt] enum-base [opt]
15743 { enumerator-list [opt] }attributes[opt]
15744 enum-key attributes[opt] identifier [opt] enum-base [opt]
15745 { enumerator-list, }attributes[opt] [C++0x]
15747 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15748 if the token stream isn't an enum-specifier after all. */
15750 static tree
15751 cp_parser_enum_specifier (cp_parser* parser)
15753 tree identifier;
15754 tree type = NULL_TREE;
15755 tree prev_scope;
15756 tree nested_name_specifier = NULL_TREE;
15757 tree attributes;
15758 bool scoped_enum_p = false;
15759 bool has_underlying_type = false;
15760 bool nested_being_defined = false;
15761 bool new_value_list = false;
15762 bool is_new_type = false;
15763 bool is_anonymous = false;
15764 tree underlying_type = NULL_TREE;
15765 cp_token *type_start_token = NULL;
15766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15768 parser->colon_corrects_to_scope_p = false;
15770 /* Parse tentatively so that we can back up if we don't find a
15771 enum-specifier. */
15772 cp_parser_parse_tentatively (parser);
15774 /* Caller guarantees that the current token is 'enum', an identifier
15775 possibly follows, and the token after that is an opening brace.
15776 If we don't have an identifier, fabricate an anonymous name for
15777 the enumeration being defined. */
15778 cp_lexer_consume_token (parser->lexer);
15780 /* Parse the "class" or "struct", which indicates a scoped
15781 enumeration type in C++0x. */
15782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15783 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15785 if (cxx_dialect < cxx11)
15786 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15788 /* Consume the `struct' or `class' token. */
15789 cp_lexer_consume_token (parser->lexer);
15791 scoped_enum_p = true;
15794 attributes = cp_parser_attributes_opt (parser);
15796 /* Clear the qualification. */
15797 parser->scope = NULL_TREE;
15798 parser->qualifying_scope = NULL_TREE;
15799 parser->object_scope = NULL_TREE;
15801 /* Figure out in what scope the declaration is being placed. */
15802 prev_scope = current_scope ();
15804 type_start_token = cp_lexer_peek_token (parser->lexer);
15806 push_deferring_access_checks (dk_no_check);
15807 nested_name_specifier
15808 = cp_parser_nested_name_specifier_opt (parser,
15809 /*typename_keyword_p=*/true,
15810 /*check_dependency_p=*/false,
15811 /*type_p=*/false,
15812 /*is_declaration=*/false);
15814 if (nested_name_specifier)
15816 tree name;
15818 identifier = cp_parser_identifier (parser);
15819 name = cp_parser_lookup_name (parser, identifier,
15820 enum_type,
15821 /*is_template=*/false,
15822 /*is_namespace=*/false,
15823 /*check_dependency=*/true,
15824 /*ambiguous_decls=*/NULL,
15825 input_location);
15826 if (name && name != error_mark_node)
15828 type = TREE_TYPE (name);
15829 if (TREE_CODE (type) == TYPENAME_TYPE)
15831 /* Are template enums allowed in ISO? */
15832 if (template_parm_scope_p ())
15833 pedwarn (type_start_token->location, OPT_Wpedantic,
15834 "%qD is an enumeration template", name);
15835 /* ignore a typename reference, for it will be solved by name
15836 in start_enum. */
15837 type = NULL_TREE;
15840 else if (nested_name_specifier == error_mark_node)
15841 /* We already issued an error. */;
15842 else
15843 error_at (type_start_token->location,
15844 "%qD is not an enumerator-name", identifier);
15846 else
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15849 identifier = cp_parser_identifier (parser);
15850 else
15852 identifier = make_anon_name ();
15853 is_anonymous = true;
15854 if (scoped_enum_p)
15855 error_at (type_start_token->location,
15856 "anonymous scoped enum is not allowed");
15859 pop_deferring_access_checks ();
15861 /* Check for the `:' that denotes a specified underlying type in C++0x.
15862 Note that a ':' could also indicate a bitfield width, however. */
15863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15865 cp_decl_specifier_seq type_specifiers;
15867 /* Consume the `:'. */
15868 cp_lexer_consume_token (parser->lexer);
15870 /* Parse the type-specifier-seq. */
15871 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15872 /*is_trailing_return=*/false,
15873 &type_specifiers);
15875 /* At this point this is surely not elaborated type specifier. */
15876 if (!cp_parser_parse_definitely (parser))
15877 return NULL_TREE;
15879 if (cxx_dialect < cxx11)
15880 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15882 has_underlying_type = true;
15884 /* If that didn't work, stop. */
15885 if (type_specifiers.type != error_mark_node)
15887 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15888 /*initialized=*/0, NULL);
15889 if (underlying_type == error_mark_node
15890 || check_for_bare_parameter_packs (underlying_type))
15891 underlying_type = NULL_TREE;
15895 /* Look for the `{' but don't consume it yet. */
15896 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15898 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15900 cp_parser_error (parser, "expected %<{%>");
15901 if (has_underlying_type)
15903 type = NULL_TREE;
15904 goto out;
15907 /* An opaque-enum-specifier must have a ';' here. */
15908 if ((scoped_enum_p || underlying_type)
15909 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15911 cp_parser_error (parser, "expected %<;%> or %<{%>");
15912 if (has_underlying_type)
15914 type = NULL_TREE;
15915 goto out;
15920 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15921 return NULL_TREE;
15923 if (nested_name_specifier)
15925 if (CLASS_TYPE_P (nested_name_specifier))
15927 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15928 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15929 push_scope (nested_name_specifier);
15931 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15933 push_nested_namespace (nested_name_specifier);
15937 /* Issue an error message if type-definitions are forbidden here. */
15938 if (!cp_parser_check_type_definition (parser))
15939 type = error_mark_node;
15940 else
15941 /* Create the new type. We do this before consuming the opening
15942 brace so the enum will be recorded as being on the line of its
15943 tag (or the 'enum' keyword, if there is no tag). */
15944 type = start_enum (identifier, type, underlying_type,
15945 scoped_enum_p, &is_new_type);
15947 /* If the next token is not '{' it is an opaque-enum-specifier or an
15948 elaborated-type-specifier. */
15949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15951 timevar_push (TV_PARSE_ENUM);
15952 if (nested_name_specifier
15953 && nested_name_specifier != error_mark_node)
15955 /* The following catches invalid code such as:
15956 enum class S<int>::E { A, B, C }; */
15957 if (!processing_specialization
15958 && CLASS_TYPE_P (nested_name_specifier)
15959 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15960 error_at (type_start_token->location, "cannot add an enumerator "
15961 "list to a template instantiation");
15963 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15965 error_at (type_start_token->location,
15966 "%<%T::%E%> has not been declared",
15967 TYPE_CONTEXT (nested_name_specifier),
15968 nested_name_specifier);
15969 type = error_mark_node;
15971 /* If that scope does not contain the scope in which the
15972 class was originally declared, the program is invalid. */
15973 else if (prev_scope && !is_ancestor (prev_scope,
15974 nested_name_specifier))
15976 if (at_namespace_scope_p ())
15977 error_at (type_start_token->location,
15978 "declaration of %qD in namespace %qD which does not "
15979 "enclose %qD",
15980 type, prev_scope, nested_name_specifier);
15981 else
15982 error_at (type_start_token->location,
15983 "declaration of %qD in %qD which does not "
15984 "enclose %qD",
15985 type, prev_scope, nested_name_specifier);
15986 type = error_mark_node;
15990 if (scoped_enum_p)
15991 begin_scope (sk_scoped_enum, type);
15993 /* Consume the opening brace. */
15994 cp_lexer_consume_token (parser->lexer);
15996 if (type == error_mark_node)
15997 ; /* Nothing to add */
15998 else if (OPAQUE_ENUM_P (type)
15999 || (cxx_dialect > cxx98 && processing_specialization))
16001 new_value_list = true;
16002 SET_OPAQUE_ENUM_P (type, false);
16003 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16005 else
16007 error_at (type_start_token->location,
16008 "multiple definition of %q#T", type);
16009 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16010 "previous definition here");
16011 type = error_mark_node;
16014 if (type == error_mark_node)
16015 cp_parser_skip_to_end_of_block_or_statement (parser);
16016 /* If the next token is not '}', then there are some enumerators. */
16017 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16019 if (is_anonymous && !scoped_enum_p)
16020 pedwarn (type_start_token->location, OPT_Wpedantic,
16021 "ISO C++ forbids empty anonymous enum");
16023 else
16024 cp_parser_enumerator_list (parser, type);
16026 /* Consume the final '}'. */
16027 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16029 if (scoped_enum_p)
16030 finish_scope ();
16031 timevar_pop (TV_PARSE_ENUM);
16033 else
16035 /* If a ';' follows, then it is an opaque-enum-specifier
16036 and additional restrictions apply. */
16037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16039 if (is_anonymous)
16040 error_at (type_start_token->location,
16041 "opaque-enum-specifier without name");
16042 else if (nested_name_specifier)
16043 error_at (type_start_token->location,
16044 "opaque-enum-specifier must use a simple identifier");
16048 /* Look for trailing attributes to apply to this enumeration, and
16049 apply them if appropriate. */
16050 if (cp_parser_allow_gnu_extensions_p (parser))
16052 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16053 trailing_attr = chainon (trailing_attr, attributes);
16054 cplus_decl_attributes (&type,
16055 trailing_attr,
16056 (int) ATTR_FLAG_TYPE_IN_PLACE);
16059 /* Finish up the enumeration. */
16060 if (type != error_mark_node)
16062 if (new_value_list)
16063 finish_enum_value_list (type);
16064 if (is_new_type)
16065 finish_enum (type);
16068 if (nested_name_specifier)
16070 if (CLASS_TYPE_P (nested_name_specifier))
16072 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16073 pop_scope (nested_name_specifier);
16075 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16077 pop_nested_namespace (nested_name_specifier);
16080 out:
16081 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16082 return type;
16085 /* Parse an enumerator-list. The enumerators all have the indicated
16086 TYPE.
16088 enumerator-list:
16089 enumerator-definition
16090 enumerator-list , enumerator-definition */
16092 static void
16093 cp_parser_enumerator_list (cp_parser* parser, tree type)
16095 while (true)
16097 /* Parse an enumerator-definition. */
16098 cp_parser_enumerator_definition (parser, type);
16100 /* If the next token is not a ',', we've reached the end of
16101 the list. */
16102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16103 break;
16104 /* Otherwise, consume the `,' and keep going. */
16105 cp_lexer_consume_token (parser->lexer);
16106 /* If the next token is a `}', there is a trailing comma. */
16107 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16109 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16110 pedwarn (input_location, OPT_Wpedantic,
16111 "comma at end of enumerator list");
16112 break;
16117 /* Parse an enumerator-definition. The enumerator has the indicated
16118 TYPE.
16120 enumerator-definition:
16121 enumerator
16122 enumerator = constant-expression
16124 enumerator:
16125 identifier
16127 GNU Extensions:
16129 enumerator-definition:
16130 enumerator attributes [opt]
16131 enumerator attributes [opt] = constant-expression */
16133 static void
16134 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16136 tree identifier;
16137 tree value;
16138 location_t loc;
16140 /* Save the input location because we are interested in the location
16141 of the identifier and not the location of the explicit value. */
16142 loc = cp_lexer_peek_token (parser->lexer)->location;
16144 /* Look for the identifier. */
16145 identifier = cp_parser_identifier (parser);
16146 if (identifier == error_mark_node)
16147 return;
16149 /* Parse any specified attributes. */
16150 tree attrs = cp_parser_attributes_opt (parser);
16152 /* If the next token is an '=', then there is an explicit value. */
16153 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16155 /* Consume the `=' token. */
16156 cp_lexer_consume_token (parser->lexer);
16157 /* Parse the value. */
16158 value = cp_parser_constant_expression (parser);
16160 else
16161 value = NULL_TREE;
16163 /* If we are processing a template, make sure the initializer of the
16164 enumerator doesn't contain any bare template parameter pack. */
16165 if (check_for_bare_parameter_packs (value))
16166 value = error_mark_node;
16168 /* Create the enumerator. */
16169 build_enumerator (identifier, value, type, attrs, loc);
16172 /* Parse a namespace-name.
16174 namespace-name:
16175 original-namespace-name
16176 namespace-alias
16178 Returns the NAMESPACE_DECL for the namespace. */
16180 static tree
16181 cp_parser_namespace_name (cp_parser* parser)
16183 tree identifier;
16184 tree namespace_decl;
16186 cp_token *token = cp_lexer_peek_token (parser->lexer);
16188 /* Get the name of the namespace. */
16189 identifier = cp_parser_identifier (parser);
16190 if (identifier == error_mark_node)
16191 return error_mark_node;
16193 /* Look up the identifier in the currently active scope. Look only
16194 for namespaces, due to:
16196 [basic.lookup.udir]
16198 When looking up a namespace-name in a using-directive or alias
16199 definition, only namespace names are considered.
16201 And:
16203 [basic.lookup.qual]
16205 During the lookup of a name preceding the :: scope resolution
16206 operator, object, function, and enumerator names are ignored.
16208 (Note that cp_parser_qualifying_entity only calls this
16209 function if the token after the name is the scope resolution
16210 operator.) */
16211 namespace_decl = cp_parser_lookup_name (parser, identifier,
16212 none_type,
16213 /*is_template=*/false,
16214 /*is_namespace=*/true,
16215 /*check_dependency=*/true,
16216 /*ambiguous_decls=*/NULL,
16217 token->location);
16218 /* If it's not a namespace, issue an error. */
16219 if (namespace_decl == error_mark_node
16220 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16222 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16223 error_at (token->location, "%qD is not a namespace-name", identifier);
16224 cp_parser_error (parser, "expected namespace-name");
16225 namespace_decl = error_mark_node;
16228 return namespace_decl;
16231 /* Parse a namespace-definition.
16233 namespace-definition:
16234 named-namespace-definition
16235 unnamed-namespace-definition
16237 named-namespace-definition:
16238 original-namespace-definition
16239 extension-namespace-definition
16241 original-namespace-definition:
16242 namespace identifier { namespace-body }
16244 extension-namespace-definition:
16245 namespace original-namespace-name { namespace-body }
16247 unnamed-namespace-definition:
16248 namespace { namespace-body } */
16250 static void
16251 cp_parser_namespace_definition (cp_parser* parser)
16253 tree identifier, attribs;
16254 bool has_visibility;
16255 bool is_inline;
16257 cp_ensure_no_omp_declare_simd (parser);
16258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16260 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16261 is_inline = true;
16262 cp_lexer_consume_token (parser->lexer);
16264 else
16265 is_inline = false;
16267 /* Look for the `namespace' keyword. */
16268 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16270 /* Get the name of the namespace. We do not attempt to distinguish
16271 between an original-namespace-definition and an
16272 extension-namespace-definition at this point. The semantic
16273 analysis routines are responsible for that. */
16274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16275 identifier = cp_parser_identifier (parser);
16276 else
16277 identifier = NULL_TREE;
16279 /* Parse any specified attributes. */
16280 attribs = cp_parser_attributes_opt (parser);
16282 /* Look for the `{' to start the namespace. */
16283 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16284 /* Start the namespace. */
16285 push_namespace (identifier);
16287 /* "inline namespace" is equivalent to a stub namespace definition
16288 followed by a strong using directive. */
16289 if (is_inline)
16291 tree name_space = current_namespace;
16292 /* Set up namespace association. */
16293 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16294 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16295 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16296 /* Import the contents of the inline namespace. */
16297 pop_namespace ();
16298 do_using_directive (name_space);
16299 push_namespace (identifier);
16302 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16304 /* Parse the body of the namespace. */
16305 cp_parser_namespace_body (parser);
16307 if (has_visibility)
16308 pop_visibility (1);
16310 /* Finish the namespace. */
16311 pop_namespace ();
16312 /* Look for the final `}'. */
16313 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16316 /* Parse a namespace-body.
16318 namespace-body:
16319 declaration-seq [opt] */
16321 static void
16322 cp_parser_namespace_body (cp_parser* parser)
16324 cp_parser_declaration_seq_opt (parser);
16327 /* Parse a namespace-alias-definition.
16329 namespace-alias-definition:
16330 namespace identifier = qualified-namespace-specifier ; */
16332 static void
16333 cp_parser_namespace_alias_definition (cp_parser* parser)
16335 tree identifier;
16336 tree namespace_specifier;
16338 cp_token *token = cp_lexer_peek_token (parser->lexer);
16340 /* Look for the `namespace' keyword. */
16341 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16342 /* Look for the identifier. */
16343 identifier = cp_parser_identifier (parser);
16344 if (identifier == error_mark_node)
16345 return;
16346 /* Look for the `=' token. */
16347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16348 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16350 error_at (token->location, "%<namespace%> definition is not allowed here");
16351 /* Skip the definition. */
16352 cp_lexer_consume_token (parser->lexer);
16353 if (cp_parser_skip_to_closing_brace (parser))
16354 cp_lexer_consume_token (parser->lexer);
16355 return;
16357 cp_parser_require (parser, CPP_EQ, RT_EQ);
16358 /* Look for the qualified-namespace-specifier. */
16359 namespace_specifier
16360 = cp_parser_qualified_namespace_specifier (parser);
16361 /* Look for the `;' token. */
16362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16364 /* Register the alias in the symbol table. */
16365 do_namespace_alias (identifier, namespace_specifier);
16368 /* Parse a qualified-namespace-specifier.
16370 qualified-namespace-specifier:
16371 :: [opt] nested-name-specifier [opt] namespace-name
16373 Returns a NAMESPACE_DECL corresponding to the specified
16374 namespace. */
16376 static tree
16377 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16379 /* Look for the optional `::'. */
16380 cp_parser_global_scope_opt (parser,
16381 /*current_scope_valid_p=*/false);
16383 /* Look for the optional nested-name-specifier. */
16384 cp_parser_nested_name_specifier_opt (parser,
16385 /*typename_keyword_p=*/false,
16386 /*check_dependency_p=*/true,
16387 /*type_p=*/false,
16388 /*is_declaration=*/true);
16390 return cp_parser_namespace_name (parser);
16393 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16394 access declaration.
16396 using-declaration:
16397 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16398 using :: unqualified-id ;
16400 access-declaration:
16401 qualified-id ;
16405 static bool
16406 cp_parser_using_declaration (cp_parser* parser,
16407 bool access_declaration_p)
16409 cp_token *token;
16410 bool typename_p = false;
16411 bool global_scope_p;
16412 tree decl;
16413 tree identifier;
16414 tree qscope;
16415 int oldcount = errorcount;
16416 cp_token *diag_token = NULL;
16418 if (access_declaration_p)
16420 diag_token = cp_lexer_peek_token (parser->lexer);
16421 cp_parser_parse_tentatively (parser);
16423 else
16425 /* Look for the `using' keyword. */
16426 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16428 /* Peek at the next token. */
16429 token = cp_lexer_peek_token (parser->lexer);
16430 /* See if it's `typename'. */
16431 if (token->keyword == RID_TYPENAME)
16433 /* Remember that we've seen it. */
16434 typename_p = true;
16435 /* Consume the `typename' token. */
16436 cp_lexer_consume_token (parser->lexer);
16440 /* Look for the optional global scope qualification. */
16441 global_scope_p
16442 = (cp_parser_global_scope_opt (parser,
16443 /*current_scope_valid_p=*/false)
16444 != NULL_TREE);
16446 /* If we saw `typename', or didn't see `::', then there must be a
16447 nested-name-specifier present. */
16448 if (typename_p || !global_scope_p)
16450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16451 /*check_dependency_p=*/true,
16452 /*type_p=*/false,
16453 /*is_declaration=*/true);
16454 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16456 cp_parser_skip_to_end_of_block_or_statement (parser);
16457 return false;
16460 /* Otherwise, we could be in either of the two productions. In that
16461 case, treat the nested-name-specifier as optional. */
16462 else
16463 qscope = cp_parser_nested_name_specifier_opt (parser,
16464 /*typename_keyword_p=*/false,
16465 /*check_dependency_p=*/true,
16466 /*type_p=*/false,
16467 /*is_declaration=*/true);
16468 if (!qscope)
16469 qscope = global_namespace;
16470 else if (UNSCOPED_ENUM_P (qscope))
16471 qscope = CP_TYPE_CONTEXT (qscope);
16473 if (access_declaration_p && cp_parser_error_occurred (parser))
16474 /* Something has already gone wrong; there's no need to parse
16475 further. Since an error has occurred, the return value of
16476 cp_parser_parse_definitely will be false, as required. */
16477 return cp_parser_parse_definitely (parser);
16479 token = cp_lexer_peek_token (parser->lexer);
16480 /* Parse the unqualified-id. */
16481 identifier = cp_parser_unqualified_id (parser,
16482 /*template_keyword_p=*/false,
16483 /*check_dependency_p=*/true,
16484 /*declarator_p=*/true,
16485 /*optional_p=*/false);
16487 if (access_declaration_p)
16489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16490 cp_parser_simulate_error (parser);
16491 if (!cp_parser_parse_definitely (parser))
16492 return false;
16495 /* The function we call to handle a using-declaration is different
16496 depending on what scope we are in. */
16497 if (qscope == error_mark_node || identifier == error_mark_node)
16499 else if (!identifier_p (identifier)
16500 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16501 /* [namespace.udecl]
16503 A using declaration shall not name a template-id. */
16504 error_at (token->location,
16505 "a template-id may not appear in a using-declaration");
16506 else
16508 if (at_class_scope_p ())
16510 /* Create the USING_DECL. */
16511 decl = do_class_using_decl (parser->scope, identifier);
16513 if (decl && typename_p)
16514 USING_DECL_TYPENAME_P (decl) = 1;
16516 if (check_for_bare_parameter_packs (decl))
16518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16519 return false;
16521 else
16522 /* Add it to the list of members in this class. */
16523 finish_member_declaration (decl);
16525 else
16527 decl = cp_parser_lookup_name_simple (parser,
16528 identifier,
16529 token->location);
16530 if (decl == error_mark_node)
16531 cp_parser_name_lookup_error (parser, identifier,
16532 decl, NLE_NULL,
16533 token->location);
16534 else if (check_for_bare_parameter_packs (decl))
16536 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16537 return false;
16539 else if (!at_namespace_scope_p ())
16540 do_local_using_decl (decl, qscope, identifier);
16541 else
16542 do_toplevel_using_decl (decl, qscope, identifier);
16546 /* Look for the final `;'. */
16547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16549 if (access_declaration_p && errorcount == oldcount)
16550 warning_at (diag_token->location, OPT_Wdeprecated,
16551 "access declarations are deprecated "
16552 "in favour of using-declarations; "
16553 "suggestion: add the %<using%> keyword");
16555 return true;
16558 /* Parse an alias-declaration.
16560 alias-declaration:
16561 using identifier attribute-specifier-seq [opt] = type-id */
16563 static tree
16564 cp_parser_alias_declaration (cp_parser* parser)
16566 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16567 location_t id_location;
16568 cp_declarator *declarator;
16569 cp_decl_specifier_seq decl_specs;
16570 bool member_p;
16571 const char *saved_message = NULL;
16573 /* Look for the `using' keyword. */
16574 cp_token *using_token
16575 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16576 if (using_token == NULL)
16577 return error_mark_node;
16579 id_location = cp_lexer_peek_token (parser->lexer)->location;
16580 id = cp_parser_identifier (parser);
16581 if (id == error_mark_node)
16582 return error_mark_node;
16584 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16585 attributes = cp_parser_attributes_opt (parser);
16586 if (attributes == error_mark_node)
16587 return error_mark_node;
16589 cp_parser_require (parser, CPP_EQ, RT_EQ);
16591 if (cp_parser_error_occurred (parser))
16592 return error_mark_node;
16594 cp_parser_commit_to_tentative_parse (parser);
16596 /* Now we are going to parse the type-id of the declaration. */
16599 [dcl.type]/3 says:
16601 "A type-specifier-seq shall not define a class or enumeration
16602 unless it appears in the type-id of an alias-declaration (7.1.3) that
16603 is not the declaration of a template-declaration."
16605 In other words, if we currently are in an alias template, the
16606 type-id should not define a type.
16608 So let's set parser->type_definition_forbidden_message in that
16609 case; cp_parser_check_type_definition (called by
16610 cp_parser_class_specifier) will then emit an error if a type is
16611 defined in the type-id. */
16612 if (parser->num_template_parameter_lists)
16614 saved_message = parser->type_definition_forbidden_message;
16615 parser->type_definition_forbidden_message =
16616 G_("types may not be defined in alias template declarations");
16619 type = cp_parser_type_id (parser);
16621 /* Restore the error message if need be. */
16622 if (parser->num_template_parameter_lists)
16623 parser->type_definition_forbidden_message = saved_message;
16625 if (type == error_mark_node
16626 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16628 cp_parser_skip_to_end_of_block_or_statement (parser);
16629 return error_mark_node;
16632 /* A typedef-name can also be introduced by an alias-declaration. The
16633 identifier following the using keyword becomes a typedef-name. It has
16634 the same semantics as if it were introduced by the typedef
16635 specifier. In particular, it does not define a new type and it shall
16636 not appear in the type-id. */
16638 clear_decl_specs (&decl_specs);
16639 decl_specs.type = type;
16640 if (attributes != NULL_TREE)
16642 decl_specs.attributes = attributes;
16643 set_and_check_decl_spec_loc (&decl_specs,
16644 ds_attribute,
16645 attrs_token);
16647 set_and_check_decl_spec_loc (&decl_specs,
16648 ds_typedef,
16649 using_token);
16650 set_and_check_decl_spec_loc (&decl_specs,
16651 ds_alias,
16652 using_token);
16654 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16655 declarator->id_loc = id_location;
16657 member_p = at_class_scope_p ();
16658 if (member_p)
16659 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16660 NULL_TREE, attributes);
16661 else
16662 decl = start_decl (declarator, &decl_specs, 0,
16663 attributes, NULL_TREE, &pushed_scope);
16664 if (decl == error_mark_node)
16665 return decl;
16667 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16669 if (pushed_scope)
16670 pop_scope (pushed_scope);
16672 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16673 added into the symbol table; otherwise, return the TYPE_DECL. */
16674 if (DECL_LANG_SPECIFIC (decl)
16675 && DECL_TEMPLATE_INFO (decl)
16676 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16678 decl = DECL_TI_TEMPLATE (decl);
16679 if (member_p)
16680 check_member_template (decl);
16683 return decl;
16686 /* Parse a using-directive.
16688 using-directive:
16689 using namespace :: [opt] nested-name-specifier [opt]
16690 namespace-name ; */
16692 static void
16693 cp_parser_using_directive (cp_parser* parser)
16695 tree namespace_decl;
16696 tree attribs;
16698 /* Look for the `using' keyword. */
16699 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16700 /* And the `namespace' keyword. */
16701 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16702 /* Look for the optional `::' operator. */
16703 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16704 /* And the optional nested-name-specifier. */
16705 cp_parser_nested_name_specifier_opt (parser,
16706 /*typename_keyword_p=*/false,
16707 /*check_dependency_p=*/true,
16708 /*type_p=*/false,
16709 /*is_declaration=*/true);
16710 /* Get the namespace being used. */
16711 namespace_decl = cp_parser_namespace_name (parser);
16712 /* And any specified attributes. */
16713 attribs = cp_parser_attributes_opt (parser);
16714 /* Update the symbol table. */
16715 parse_using_directive (namespace_decl, attribs);
16716 /* Look for the final `;'. */
16717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16720 /* Parse an asm-definition.
16722 asm-definition:
16723 asm ( string-literal ) ;
16725 GNU Extension:
16727 asm-definition:
16728 asm volatile [opt] ( string-literal ) ;
16729 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16730 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16731 : asm-operand-list [opt] ) ;
16732 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16733 : asm-operand-list [opt]
16734 : asm-clobber-list [opt] ) ;
16735 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16736 : asm-clobber-list [opt]
16737 : asm-goto-list ) ; */
16739 static void
16740 cp_parser_asm_definition (cp_parser* parser)
16742 tree string;
16743 tree outputs = NULL_TREE;
16744 tree inputs = NULL_TREE;
16745 tree clobbers = NULL_TREE;
16746 tree labels = NULL_TREE;
16747 tree asm_stmt;
16748 bool volatile_p = false;
16749 bool extended_p = false;
16750 bool invalid_inputs_p = false;
16751 bool invalid_outputs_p = false;
16752 bool goto_p = false;
16753 required_token missing = RT_NONE;
16755 /* Look for the `asm' keyword. */
16756 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16758 if (parser->in_function_body
16759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16761 error ("%<asm%> in %<constexpr%> function");
16762 cp_function_chain->invalid_constexpr = true;
16765 /* See if the next token is `volatile'. */
16766 if (cp_parser_allow_gnu_extensions_p (parser)
16767 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16769 /* Remember that we saw the `volatile' keyword. */
16770 volatile_p = true;
16771 /* Consume the token. */
16772 cp_lexer_consume_token (parser->lexer);
16774 if (cp_parser_allow_gnu_extensions_p (parser)
16775 && parser->in_function_body
16776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16778 /* Remember that we saw the `goto' keyword. */
16779 goto_p = true;
16780 /* Consume the token. */
16781 cp_lexer_consume_token (parser->lexer);
16783 /* Look for the opening `('. */
16784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16785 return;
16786 /* Look for the string. */
16787 string = cp_parser_string_literal (parser, false, false);
16788 if (string == error_mark_node)
16790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16791 /*consume_paren=*/true);
16792 return;
16795 /* If we're allowing GNU extensions, check for the extended assembly
16796 syntax. Unfortunately, the `:' tokens need not be separated by
16797 a space in C, and so, for compatibility, we tolerate that here
16798 too. Doing that means that we have to treat the `::' operator as
16799 two `:' tokens. */
16800 if (cp_parser_allow_gnu_extensions_p (parser)
16801 && parser->in_function_body
16802 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16803 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16805 bool inputs_p = false;
16806 bool clobbers_p = false;
16807 bool labels_p = false;
16809 /* The extended syntax was used. */
16810 extended_p = true;
16812 /* Look for outputs. */
16813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16815 /* Consume the `:'. */
16816 cp_lexer_consume_token (parser->lexer);
16817 /* Parse the output-operands. */
16818 if (cp_lexer_next_token_is_not (parser->lexer,
16819 CPP_COLON)
16820 && cp_lexer_next_token_is_not (parser->lexer,
16821 CPP_SCOPE)
16822 && cp_lexer_next_token_is_not (parser->lexer,
16823 CPP_CLOSE_PAREN)
16824 && !goto_p)
16826 outputs = cp_parser_asm_operand_list (parser);
16827 if (outputs == error_mark_node)
16828 invalid_outputs_p = true;
16831 /* If the next token is `::', there are no outputs, and the
16832 next token is the beginning of the inputs. */
16833 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16834 /* The inputs are coming next. */
16835 inputs_p = true;
16837 /* Look for inputs. */
16838 if (inputs_p
16839 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16841 /* Consume the `:' or `::'. */
16842 cp_lexer_consume_token (parser->lexer);
16843 /* Parse the output-operands. */
16844 if (cp_lexer_next_token_is_not (parser->lexer,
16845 CPP_COLON)
16846 && cp_lexer_next_token_is_not (parser->lexer,
16847 CPP_SCOPE)
16848 && cp_lexer_next_token_is_not (parser->lexer,
16849 CPP_CLOSE_PAREN))
16851 inputs = cp_parser_asm_operand_list (parser);
16852 if (inputs == error_mark_node)
16853 invalid_inputs_p = true;
16856 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16857 /* The clobbers are coming next. */
16858 clobbers_p = true;
16860 /* Look for clobbers. */
16861 if (clobbers_p
16862 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16864 clobbers_p = true;
16865 /* Consume the `:' or `::'. */
16866 cp_lexer_consume_token (parser->lexer);
16867 /* Parse the clobbers. */
16868 if (cp_lexer_next_token_is_not (parser->lexer,
16869 CPP_COLON)
16870 && cp_lexer_next_token_is_not (parser->lexer,
16871 CPP_CLOSE_PAREN))
16872 clobbers = cp_parser_asm_clobber_list (parser);
16874 else if (goto_p
16875 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16876 /* The labels are coming next. */
16877 labels_p = true;
16879 /* Look for labels. */
16880 if (labels_p
16881 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16883 labels_p = true;
16884 /* Consume the `:' or `::'. */
16885 cp_lexer_consume_token (parser->lexer);
16886 /* Parse the labels. */
16887 labels = cp_parser_asm_label_list (parser);
16890 if (goto_p && !labels_p)
16891 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16893 else if (goto_p)
16894 missing = RT_COLON_SCOPE;
16896 /* Look for the closing `)'. */
16897 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16898 missing ? missing : RT_CLOSE_PAREN))
16899 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16900 /*consume_paren=*/true);
16901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16903 if (!invalid_inputs_p && !invalid_outputs_p)
16905 /* Create the ASM_EXPR. */
16906 if (parser->in_function_body)
16908 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16909 inputs, clobbers, labels);
16910 /* If the extended syntax was not used, mark the ASM_EXPR. */
16911 if (!extended_p)
16913 tree temp = asm_stmt;
16914 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16915 temp = TREE_OPERAND (temp, 0);
16917 ASM_INPUT_P (temp) = 1;
16920 else
16921 symtab->finalize_toplevel_asm (string);
16925 /* Declarators [gram.dcl.decl] */
16927 /* Parse an init-declarator.
16929 init-declarator:
16930 declarator initializer [opt]
16932 GNU Extension:
16934 init-declarator:
16935 declarator asm-specification [opt] attributes [opt] initializer [opt]
16937 function-definition:
16938 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16939 function-body
16940 decl-specifier-seq [opt] declarator function-try-block
16942 GNU Extension:
16944 function-definition:
16945 __extension__ function-definition
16947 TM Extension:
16949 function-definition:
16950 decl-specifier-seq [opt] declarator function-transaction-block
16952 The DECL_SPECIFIERS apply to this declarator. Returns a
16953 representation of the entity declared. If MEMBER_P is TRUE, then
16954 this declarator appears in a class scope. The new DECL created by
16955 this declarator is returned.
16957 The CHECKS are access checks that should be performed once we know
16958 what entity is being declared (and, therefore, what classes have
16959 befriended it).
16961 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16962 for a function-definition here as well. If the declarator is a
16963 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16964 be TRUE upon return. By that point, the function-definition will
16965 have been completely parsed.
16967 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16968 is FALSE.
16970 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16971 parsed declaration if it is an uninitialized single declarator not followed
16972 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16973 if present, will not be consumed. If returned, this declarator will be
16974 created with SD_INITIALIZED but will not call cp_finish_decl.
16976 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16977 and there is an initializer, the pointed location_t is set to the
16978 location of the '=' or `(', or '{' in C++11 token introducing the
16979 initializer. */
16981 static tree
16982 cp_parser_init_declarator (cp_parser* parser,
16983 cp_decl_specifier_seq *decl_specifiers,
16984 vec<deferred_access_check, va_gc> *checks,
16985 bool function_definition_allowed_p,
16986 bool member_p,
16987 int declares_class_or_enum,
16988 bool* function_definition_p,
16989 tree* maybe_range_for_decl,
16990 location_t* init_loc)
16992 cp_token *token = NULL, *asm_spec_start_token = NULL,
16993 *attributes_start_token = NULL;
16994 cp_declarator *declarator;
16995 tree prefix_attributes;
16996 tree attributes = NULL;
16997 tree asm_specification;
16998 tree initializer;
16999 tree decl = NULL_TREE;
17000 tree scope;
17001 int is_initialized;
17002 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17003 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17004 "(...)". */
17005 enum cpp_ttype initialization_kind;
17006 bool is_direct_init = false;
17007 bool is_non_constant_init;
17008 int ctor_dtor_or_conv_p;
17009 bool friend_p = cp_parser_friend_p (decl_specifiers);
17010 tree pushed_scope = NULL_TREE;
17011 bool range_for_decl_p = false;
17012 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17013 location_t tmp_init_loc = UNKNOWN_LOCATION;
17015 /* Gather the attributes that were provided with the
17016 decl-specifiers. */
17017 prefix_attributes = decl_specifiers->attributes;
17019 /* Assume that this is not the declarator for a function
17020 definition. */
17021 if (function_definition_p)
17022 *function_definition_p = false;
17024 /* Default arguments are only permitted for function parameters. */
17025 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17026 parser->default_arg_ok_p = false;
17028 /* Defer access checks while parsing the declarator; we cannot know
17029 what names are accessible until we know what is being
17030 declared. */
17031 resume_deferring_access_checks ();
17033 /* Parse the declarator. */
17034 token = cp_lexer_peek_token (parser->lexer);
17035 declarator
17036 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17037 &ctor_dtor_or_conv_p,
17038 /*parenthesized_p=*/NULL,
17039 member_p, friend_p);
17040 /* Gather up the deferred checks. */
17041 stop_deferring_access_checks ();
17043 parser->default_arg_ok_p = saved_default_arg_ok_p;
17045 /* If the DECLARATOR was erroneous, there's no need to go
17046 further. */
17047 if (declarator == cp_error_declarator)
17048 return error_mark_node;
17050 /* Check that the number of template-parameter-lists is OK. */
17051 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17052 token->location))
17053 return error_mark_node;
17055 if (declares_class_or_enum & 2)
17056 cp_parser_check_for_definition_in_return_type (declarator,
17057 decl_specifiers->type,
17058 decl_specifiers->locations[ds_type_spec]);
17060 /* Figure out what scope the entity declared by the DECLARATOR is
17061 located in. `grokdeclarator' sometimes changes the scope, so
17062 we compute it now. */
17063 scope = get_scope_of_declarator (declarator);
17065 /* Perform any lookups in the declared type which were thought to be
17066 dependent, but are not in the scope of the declarator. */
17067 decl_specifiers->type
17068 = maybe_update_decl_type (decl_specifiers->type, scope);
17070 /* If we're allowing GNU extensions, look for an
17071 asm-specification. */
17072 if (cp_parser_allow_gnu_extensions_p (parser))
17074 /* Look for an asm-specification. */
17075 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17076 asm_specification = cp_parser_asm_specification_opt (parser);
17078 else
17079 asm_specification = NULL_TREE;
17081 /* Look for attributes. */
17082 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17083 attributes = cp_parser_attributes_opt (parser);
17085 /* Peek at the next token. */
17086 token = cp_lexer_peek_token (parser->lexer);
17088 bool bogus_implicit_tmpl = false;
17090 if (function_declarator_p (declarator))
17092 /* Check to see if the token indicates the start of a
17093 function-definition. */
17094 if (cp_parser_token_starts_function_definition_p (token))
17096 if (!function_definition_allowed_p)
17098 /* If a function-definition should not appear here, issue an
17099 error message. */
17100 cp_parser_error (parser,
17101 "a function-definition is not allowed here");
17102 return error_mark_node;
17105 location_t func_brace_location
17106 = cp_lexer_peek_token (parser->lexer)->location;
17108 /* Neither attributes nor an asm-specification are allowed
17109 on a function-definition. */
17110 if (asm_specification)
17111 error_at (asm_spec_start_token->location,
17112 "an asm-specification is not allowed "
17113 "on a function-definition");
17114 if (attributes)
17115 error_at (attributes_start_token->location,
17116 "attributes are not allowed "
17117 "on a function-definition");
17118 /* This is a function-definition. */
17119 *function_definition_p = true;
17121 /* Parse the function definition. */
17122 if (member_p)
17123 decl = cp_parser_save_member_function_body (parser,
17124 decl_specifiers,
17125 declarator,
17126 prefix_attributes);
17127 else
17128 decl =
17129 (cp_parser_function_definition_from_specifiers_and_declarator
17130 (parser, decl_specifiers, prefix_attributes, declarator));
17132 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17134 /* This is where the prologue starts... */
17135 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17136 = func_brace_location;
17139 return decl;
17142 else if (parser->fully_implicit_function_template_p)
17144 /* A non-template declaration involving a function parameter list
17145 containing an implicit template parameter will be made into a
17146 template. If the resulting declaration is not going to be an
17147 actual function then finish the template scope here to prevent it.
17148 An error message will be issued once we have a decl to talk about.
17150 FIXME probably we should do type deduction rather than create an
17151 implicit template, but the standard currently doesn't allow it. */
17152 bogus_implicit_tmpl = true;
17153 finish_fully_implicit_template (parser, NULL_TREE);
17156 /* [dcl.dcl]
17158 Only in function declarations for constructors, destructors, and
17159 type conversions can the decl-specifier-seq be omitted.
17161 We explicitly postpone this check past the point where we handle
17162 function-definitions because we tolerate function-definitions
17163 that are missing their return types in some modes. */
17164 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17166 cp_parser_error (parser,
17167 "expected constructor, destructor, or type conversion");
17168 return error_mark_node;
17171 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17172 if (token->type == CPP_EQ
17173 || token->type == CPP_OPEN_PAREN
17174 || token->type == CPP_OPEN_BRACE)
17176 is_initialized = SD_INITIALIZED;
17177 initialization_kind = token->type;
17178 if (maybe_range_for_decl)
17179 *maybe_range_for_decl = error_mark_node;
17180 tmp_init_loc = token->location;
17181 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17182 *init_loc = tmp_init_loc;
17184 if (token->type == CPP_EQ
17185 && function_declarator_p (declarator))
17187 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17188 if (t2->keyword == RID_DEFAULT)
17189 is_initialized = SD_DEFAULTED;
17190 else if (t2->keyword == RID_DELETE)
17191 is_initialized = SD_DELETED;
17194 else
17196 /* If the init-declarator isn't initialized and isn't followed by a
17197 `,' or `;', it's not a valid init-declarator. */
17198 if (token->type != CPP_COMMA
17199 && token->type != CPP_SEMICOLON)
17201 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17202 range_for_decl_p = true;
17203 else
17205 if (!maybe_range_for_decl)
17206 cp_parser_error (parser, "expected initializer");
17207 return error_mark_node;
17210 is_initialized = SD_UNINITIALIZED;
17211 initialization_kind = CPP_EOF;
17214 /* Because start_decl has side-effects, we should only call it if we
17215 know we're going ahead. By this point, we know that we cannot
17216 possibly be looking at any other construct. */
17217 cp_parser_commit_to_tentative_parse (parser);
17219 /* Enter the newly declared entry in the symbol table. If we're
17220 processing a declaration in a class-specifier, we wait until
17221 after processing the initializer. */
17222 if (!member_p)
17224 if (parser->in_unbraced_linkage_specification_p)
17225 decl_specifiers->storage_class = sc_extern;
17226 decl = start_decl (declarator, decl_specifiers,
17227 range_for_decl_p? SD_INITIALIZED : is_initialized,
17228 attributes, prefix_attributes, &pushed_scope);
17229 cp_finalize_omp_declare_simd (parser, decl);
17230 /* Adjust location of decl if declarator->id_loc is more appropriate:
17231 set, and decl wasn't merged with another decl, in which case its
17232 location would be different from input_location, and more accurate. */
17233 if (DECL_P (decl)
17234 && declarator->id_loc != UNKNOWN_LOCATION
17235 && DECL_SOURCE_LOCATION (decl) == input_location)
17236 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17238 else if (scope)
17239 /* Enter the SCOPE. That way unqualified names appearing in the
17240 initializer will be looked up in SCOPE. */
17241 pushed_scope = push_scope (scope);
17243 /* Perform deferred access control checks, now that we know in which
17244 SCOPE the declared entity resides. */
17245 if (!member_p && decl)
17247 tree saved_current_function_decl = NULL_TREE;
17249 /* If the entity being declared is a function, pretend that we
17250 are in its scope. If it is a `friend', it may have access to
17251 things that would not otherwise be accessible. */
17252 if (TREE_CODE (decl) == FUNCTION_DECL)
17254 saved_current_function_decl = current_function_decl;
17255 current_function_decl = decl;
17258 /* Perform access checks for template parameters. */
17259 cp_parser_perform_template_parameter_access_checks (checks);
17261 /* Perform the access control checks for the declarator and the
17262 decl-specifiers. */
17263 perform_deferred_access_checks (tf_warning_or_error);
17265 /* Restore the saved value. */
17266 if (TREE_CODE (decl) == FUNCTION_DECL)
17267 current_function_decl = saved_current_function_decl;
17270 /* Parse the initializer. */
17271 initializer = NULL_TREE;
17272 is_direct_init = false;
17273 is_non_constant_init = true;
17274 if (is_initialized)
17276 if (function_declarator_p (declarator))
17278 if (initialization_kind == CPP_EQ)
17279 initializer = cp_parser_pure_specifier (parser);
17280 else
17282 /* If the declaration was erroneous, we don't really
17283 know what the user intended, so just silently
17284 consume the initializer. */
17285 if (decl != error_mark_node)
17286 error_at (tmp_init_loc, "initializer provided for function");
17287 cp_parser_skip_to_closing_parenthesis (parser,
17288 /*recovering=*/true,
17289 /*or_comma=*/false,
17290 /*consume_paren=*/true);
17293 else
17295 /* We want to record the extra mangling scope for in-class
17296 initializers of class members and initializers of static data
17297 member templates. The former involves deferring
17298 parsing of the initializer until end of class as with default
17299 arguments. So right here we only handle the latter. */
17300 if (!member_p && processing_template_decl)
17301 start_lambda_scope (decl);
17302 initializer = cp_parser_initializer (parser,
17303 &is_direct_init,
17304 &is_non_constant_init);
17305 if (!member_p && processing_template_decl)
17306 finish_lambda_scope ();
17307 if (initializer == error_mark_node)
17308 cp_parser_skip_to_end_of_statement (parser);
17312 /* The old parser allows attributes to appear after a parenthesized
17313 initializer. Mark Mitchell proposed removing this functionality
17314 on the GCC mailing lists on 2002-08-13. This parser accepts the
17315 attributes -- but ignores them. */
17316 if (cp_parser_allow_gnu_extensions_p (parser)
17317 && initialization_kind == CPP_OPEN_PAREN)
17318 if (cp_parser_attributes_opt (parser))
17319 warning (OPT_Wattributes,
17320 "attributes after parenthesized initializer ignored");
17322 /* And now complain about a non-function implicit template. */
17323 if (bogus_implicit_tmpl)
17324 error_at (DECL_SOURCE_LOCATION (decl),
17325 "non-function %qD declared as implicit template", decl);
17327 /* For an in-class declaration, use `grokfield' to create the
17328 declaration. */
17329 if (member_p)
17331 if (pushed_scope)
17333 pop_scope (pushed_scope);
17334 pushed_scope = NULL_TREE;
17336 decl = grokfield (declarator, decl_specifiers,
17337 initializer, !is_non_constant_init,
17338 /*asmspec=*/NULL_TREE,
17339 chainon (attributes, prefix_attributes));
17340 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17341 cp_parser_save_default_args (parser, decl);
17342 cp_finalize_omp_declare_simd (parser, decl);
17345 /* Finish processing the declaration. But, skip member
17346 declarations. */
17347 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17349 cp_finish_decl (decl,
17350 initializer, !is_non_constant_init,
17351 asm_specification,
17352 /* If the initializer is in parentheses, then this is
17353 a direct-initialization, which means that an
17354 `explicit' constructor is OK. Otherwise, an
17355 `explicit' constructor cannot be used. */
17356 ((is_direct_init || !is_initialized)
17357 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17359 else if ((cxx_dialect != cxx98) && friend_p
17360 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17361 /* Core issue #226 (C++0x only): A default template-argument
17362 shall not be specified in a friend class template
17363 declaration. */
17364 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17365 /*is_partial=*/false, /*is_friend_decl=*/1);
17367 if (!friend_p && pushed_scope)
17368 pop_scope (pushed_scope);
17370 if (function_declarator_p (declarator)
17371 && parser->fully_implicit_function_template_p)
17373 if (member_p)
17374 decl = finish_fully_implicit_template (parser, decl);
17375 else
17376 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17379 return decl;
17382 /* Parse a declarator.
17384 declarator:
17385 direct-declarator
17386 ptr-operator declarator
17388 abstract-declarator:
17389 ptr-operator abstract-declarator [opt]
17390 direct-abstract-declarator
17392 GNU Extensions:
17394 declarator:
17395 attributes [opt] direct-declarator
17396 attributes [opt] ptr-operator declarator
17398 abstract-declarator:
17399 attributes [opt] ptr-operator abstract-declarator [opt]
17400 attributes [opt] direct-abstract-declarator
17402 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17403 detect constructor, destructor or conversion operators. It is set
17404 to -1 if the declarator is a name, and +1 if it is a
17405 function. Otherwise it is set to zero. Usually you just want to
17406 test for >0, but internally the negative value is used.
17408 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17409 a decl-specifier-seq unless it declares a constructor, destructor,
17410 or conversion. It might seem that we could check this condition in
17411 semantic analysis, rather than parsing, but that makes it difficult
17412 to handle something like `f()'. We want to notice that there are
17413 no decl-specifiers, and therefore realize that this is an
17414 expression, not a declaration.)
17416 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17417 the declarator is a direct-declarator of the form "(...)".
17419 MEMBER_P is true iff this declarator is a member-declarator.
17421 FRIEND_P is true iff this declarator is a friend. */
17423 static cp_declarator *
17424 cp_parser_declarator (cp_parser* parser,
17425 cp_parser_declarator_kind dcl_kind,
17426 int* ctor_dtor_or_conv_p,
17427 bool* parenthesized_p,
17428 bool member_p, bool friend_p)
17430 cp_declarator *declarator;
17431 enum tree_code code;
17432 cp_cv_quals cv_quals;
17433 tree class_type;
17434 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17436 /* Assume this is not a constructor, destructor, or type-conversion
17437 operator. */
17438 if (ctor_dtor_or_conv_p)
17439 *ctor_dtor_or_conv_p = 0;
17441 if (cp_parser_allow_gnu_extensions_p (parser))
17442 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17444 /* Check for the ptr-operator production. */
17445 cp_parser_parse_tentatively (parser);
17446 /* Parse the ptr-operator. */
17447 code = cp_parser_ptr_operator (parser,
17448 &class_type,
17449 &cv_quals,
17450 &std_attributes);
17452 /* If that worked, then we have a ptr-operator. */
17453 if (cp_parser_parse_definitely (parser))
17455 /* If a ptr-operator was found, then this declarator was not
17456 parenthesized. */
17457 if (parenthesized_p)
17458 *parenthesized_p = true;
17459 /* The dependent declarator is optional if we are parsing an
17460 abstract-declarator. */
17461 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17462 cp_parser_parse_tentatively (parser);
17464 /* Parse the dependent declarator. */
17465 declarator = cp_parser_declarator (parser, dcl_kind,
17466 /*ctor_dtor_or_conv_p=*/NULL,
17467 /*parenthesized_p=*/NULL,
17468 /*member_p=*/false,
17469 friend_p);
17471 /* If we are parsing an abstract-declarator, we must handle the
17472 case where the dependent declarator is absent. */
17473 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17474 && !cp_parser_parse_definitely (parser))
17475 declarator = NULL;
17477 declarator = cp_parser_make_indirect_declarator
17478 (code, class_type, cv_quals, declarator, std_attributes);
17480 /* Everything else is a direct-declarator. */
17481 else
17483 if (parenthesized_p)
17484 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17485 CPP_OPEN_PAREN);
17486 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17487 ctor_dtor_or_conv_p,
17488 member_p, friend_p);
17491 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17492 declarator->attributes = gnu_attributes;
17493 return declarator;
17496 /* Parse a direct-declarator or direct-abstract-declarator.
17498 direct-declarator:
17499 declarator-id
17500 direct-declarator ( parameter-declaration-clause )
17501 cv-qualifier-seq [opt]
17502 ref-qualifier [opt]
17503 exception-specification [opt]
17504 direct-declarator [ constant-expression [opt] ]
17505 ( declarator )
17507 direct-abstract-declarator:
17508 direct-abstract-declarator [opt]
17509 ( parameter-declaration-clause )
17510 cv-qualifier-seq [opt]
17511 ref-qualifier [opt]
17512 exception-specification [opt]
17513 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17514 ( abstract-declarator )
17516 Returns a representation of the declarator. DCL_KIND is
17517 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17518 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17519 we are parsing a direct-declarator. It is
17520 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17521 of ambiguity we prefer an abstract declarator, as per
17522 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17523 as for cp_parser_declarator. */
17525 static cp_declarator *
17526 cp_parser_direct_declarator (cp_parser* parser,
17527 cp_parser_declarator_kind dcl_kind,
17528 int* ctor_dtor_or_conv_p,
17529 bool member_p, bool friend_p)
17531 cp_token *token;
17532 cp_declarator *declarator = NULL;
17533 tree scope = NULL_TREE;
17534 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17535 bool saved_in_declarator_p = parser->in_declarator_p;
17536 bool first = true;
17537 tree pushed_scope = NULL_TREE;
17539 while (true)
17541 /* Peek at the next token. */
17542 token = cp_lexer_peek_token (parser->lexer);
17543 if (token->type == CPP_OPEN_PAREN)
17545 /* This is either a parameter-declaration-clause, or a
17546 parenthesized declarator. When we know we are parsing a
17547 named declarator, it must be a parenthesized declarator
17548 if FIRST is true. For instance, `(int)' is a
17549 parameter-declaration-clause, with an omitted
17550 direct-abstract-declarator. But `((*))', is a
17551 parenthesized abstract declarator. Finally, when T is a
17552 template parameter `(T)' is a
17553 parameter-declaration-clause, and not a parenthesized
17554 named declarator.
17556 We first try and parse a parameter-declaration-clause,
17557 and then try a nested declarator (if FIRST is true).
17559 It is not an error for it not to be a
17560 parameter-declaration-clause, even when FIRST is
17561 false. Consider,
17563 int i (int);
17564 int i (3);
17566 The first is the declaration of a function while the
17567 second is the definition of a variable, including its
17568 initializer.
17570 Having seen only the parenthesis, we cannot know which of
17571 these two alternatives should be selected. Even more
17572 complex are examples like:
17574 int i (int (a));
17575 int i (int (3));
17577 The former is a function-declaration; the latter is a
17578 variable initialization.
17580 Thus again, we try a parameter-declaration-clause, and if
17581 that fails, we back out and return. */
17583 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17585 tree params;
17586 bool is_declarator = false;
17588 /* In a member-declarator, the only valid interpretation
17589 of a parenthesis is the start of a
17590 parameter-declaration-clause. (It is invalid to
17591 initialize a static data member with a parenthesized
17592 initializer; only the "=" form of initialization is
17593 permitted.) */
17594 if (!member_p)
17595 cp_parser_parse_tentatively (parser);
17597 /* Consume the `('. */
17598 cp_lexer_consume_token (parser->lexer);
17599 if (first)
17601 /* If this is going to be an abstract declarator, we're
17602 in a declarator and we can't have default args. */
17603 parser->default_arg_ok_p = false;
17604 parser->in_declarator_p = true;
17607 begin_scope (sk_function_parms, NULL_TREE);
17609 /* Parse the parameter-declaration-clause. */
17610 params = cp_parser_parameter_declaration_clause (parser);
17612 /* Consume the `)'. */
17613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17615 /* If all went well, parse the cv-qualifier-seq,
17616 ref-qualifier and the exception-specification. */
17617 if (member_p || cp_parser_parse_definitely (parser))
17619 cp_cv_quals cv_quals;
17620 cp_virt_specifiers virt_specifiers;
17621 cp_ref_qualifier ref_qual;
17622 tree exception_specification;
17623 tree late_return;
17624 tree attrs;
17625 bool memfn = (member_p || (pushed_scope
17626 && CLASS_TYPE_P (pushed_scope)));
17628 is_declarator = true;
17630 if (ctor_dtor_or_conv_p)
17631 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17632 first = false;
17634 /* Parse the cv-qualifier-seq. */
17635 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17636 /* Parse the ref-qualifier. */
17637 ref_qual = cp_parser_ref_qualifier_opt (parser);
17638 /* And the exception-specification. */
17639 exception_specification
17640 = cp_parser_exception_specification_opt (parser);
17642 attrs = cp_parser_std_attribute_spec_seq (parser);
17644 /* In here, we handle cases where attribute is used after
17645 the function declaration. For example:
17646 void func (int x) __attribute__((vector(..))); */
17647 if (flag_cilkplus
17648 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17650 cp_parser_parse_tentatively (parser);
17651 tree attr = cp_parser_gnu_attributes_opt (parser);
17652 if (cp_lexer_next_token_is_not (parser->lexer,
17653 CPP_SEMICOLON)
17654 && cp_lexer_next_token_is_not (parser->lexer,
17655 CPP_OPEN_BRACE))
17656 cp_parser_abort_tentative_parse (parser);
17657 else if (!cp_parser_parse_definitely (parser))
17659 else
17660 attrs = chainon (attr, attrs);
17662 late_return = (cp_parser_late_return_type_opt
17663 (parser, declarator,
17664 memfn ? cv_quals : -1));
17667 /* Parse the virt-specifier-seq. */
17668 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17670 /* Create the function-declarator. */
17671 declarator = make_call_declarator (declarator,
17672 params,
17673 cv_quals,
17674 virt_specifiers,
17675 ref_qual,
17676 exception_specification,
17677 late_return);
17678 declarator->std_attributes = attrs;
17679 /* Any subsequent parameter lists are to do with
17680 return type, so are not those of the declared
17681 function. */
17682 parser->default_arg_ok_p = false;
17685 /* Remove the function parms from scope. */
17686 pop_bindings_and_leave_scope ();
17688 if (is_declarator)
17689 /* Repeat the main loop. */
17690 continue;
17693 /* If this is the first, we can try a parenthesized
17694 declarator. */
17695 if (first)
17697 bool saved_in_type_id_in_expr_p;
17699 parser->default_arg_ok_p = saved_default_arg_ok_p;
17700 parser->in_declarator_p = saved_in_declarator_p;
17702 /* Consume the `('. */
17703 cp_lexer_consume_token (parser->lexer);
17704 /* Parse the nested declarator. */
17705 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17706 parser->in_type_id_in_expr_p = true;
17707 declarator
17708 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17709 /*parenthesized_p=*/NULL,
17710 member_p, friend_p);
17711 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17712 first = false;
17713 /* Expect a `)'. */
17714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17715 declarator = cp_error_declarator;
17716 if (declarator == cp_error_declarator)
17717 break;
17719 goto handle_declarator;
17721 /* Otherwise, we must be done. */
17722 else
17723 break;
17725 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17726 && token->type == CPP_OPEN_SQUARE
17727 && !cp_next_tokens_can_be_attribute_p (parser))
17729 /* Parse an array-declarator. */
17730 tree bounds, attrs;
17732 if (ctor_dtor_or_conv_p)
17733 *ctor_dtor_or_conv_p = 0;
17735 first = false;
17736 parser->default_arg_ok_p = false;
17737 parser->in_declarator_p = true;
17738 /* Consume the `['. */
17739 cp_lexer_consume_token (parser->lexer);
17740 /* Peek at the next token. */
17741 token = cp_lexer_peek_token (parser->lexer);
17742 /* If the next token is `]', then there is no
17743 constant-expression. */
17744 if (token->type != CPP_CLOSE_SQUARE)
17746 bool non_constant_p;
17747 bounds
17748 = cp_parser_constant_expression (parser,
17749 /*allow_non_constant=*/true,
17750 &non_constant_p);
17751 if (!non_constant_p)
17752 /* OK */;
17753 else if (error_operand_p (bounds))
17754 /* Already gave an error. */;
17755 else if (!parser->in_function_body
17756 || current_binding_level->kind == sk_function_parms)
17758 /* Normally, the array bound must be an integral constant
17759 expression. However, as an extension, we allow VLAs
17760 in function scopes as long as they aren't part of a
17761 parameter declaration. */
17762 cp_parser_error (parser,
17763 "array bound is not an integer constant");
17764 bounds = error_mark_node;
17766 else if (processing_template_decl
17767 && !type_dependent_expression_p (bounds))
17769 /* Remember this wasn't a constant-expression. */
17770 bounds = build_nop (TREE_TYPE (bounds), bounds);
17771 TREE_SIDE_EFFECTS (bounds) = 1;
17774 else
17775 bounds = NULL_TREE;
17776 /* Look for the closing `]'. */
17777 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17779 declarator = cp_error_declarator;
17780 break;
17783 attrs = cp_parser_std_attribute_spec_seq (parser);
17784 declarator = make_array_declarator (declarator, bounds);
17785 declarator->std_attributes = attrs;
17787 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17790 tree qualifying_scope;
17791 tree unqualified_name;
17792 tree attrs;
17793 special_function_kind sfk;
17794 bool abstract_ok;
17795 bool pack_expansion_p = false;
17796 cp_token *declarator_id_start_token;
17798 /* Parse a declarator-id */
17799 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17800 if (abstract_ok)
17802 cp_parser_parse_tentatively (parser);
17804 /* If we see an ellipsis, we should be looking at a
17805 parameter pack. */
17806 if (token->type == CPP_ELLIPSIS)
17808 /* Consume the `...' */
17809 cp_lexer_consume_token (parser->lexer);
17811 pack_expansion_p = true;
17815 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17816 unqualified_name
17817 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17818 qualifying_scope = parser->scope;
17819 if (abstract_ok)
17821 bool okay = false;
17823 if (!unqualified_name && pack_expansion_p)
17825 /* Check whether an error occurred. */
17826 okay = !cp_parser_error_occurred (parser);
17828 /* We already consumed the ellipsis to mark a
17829 parameter pack, but we have no way to report it,
17830 so abort the tentative parse. We will be exiting
17831 immediately anyway. */
17832 cp_parser_abort_tentative_parse (parser);
17834 else
17835 okay = cp_parser_parse_definitely (parser);
17837 if (!okay)
17838 unqualified_name = error_mark_node;
17839 else if (unqualified_name
17840 && (qualifying_scope
17841 || (!identifier_p (unqualified_name))))
17843 cp_parser_error (parser, "expected unqualified-id");
17844 unqualified_name = error_mark_node;
17848 if (!unqualified_name)
17849 return NULL;
17850 if (unqualified_name == error_mark_node)
17852 declarator = cp_error_declarator;
17853 pack_expansion_p = false;
17854 declarator->parameter_pack_p = false;
17855 break;
17858 attrs = cp_parser_std_attribute_spec_seq (parser);
17860 if (qualifying_scope && at_namespace_scope_p ()
17861 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17863 /* In the declaration of a member of a template class
17864 outside of the class itself, the SCOPE will sometimes
17865 be a TYPENAME_TYPE. For example, given:
17867 template <typename T>
17868 int S<T>::R::i = 3;
17870 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17871 this context, we must resolve S<T>::R to an ordinary
17872 type, rather than a typename type.
17874 The reason we normally avoid resolving TYPENAME_TYPEs
17875 is that a specialization of `S' might render
17876 `S<T>::R' not a type. However, if `S' is
17877 specialized, then this `i' will not be used, so there
17878 is no harm in resolving the types here. */
17879 tree type;
17881 /* Resolve the TYPENAME_TYPE. */
17882 type = resolve_typename_type (qualifying_scope,
17883 /*only_current_p=*/false);
17884 /* If that failed, the declarator is invalid. */
17885 if (TREE_CODE (type) == TYPENAME_TYPE)
17887 if (typedef_variant_p (type))
17888 error_at (declarator_id_start_token->location,
17889 "cannot define member of dependent typedef "
17890 "%qT", type);
17891 else
17892 error_at (declarator_id_start_token->location,
17893 "%<%T::%E%> is not a type",
17894 TYPE_CONTEXT (qualifying_scope),
17895 TYPE_IDENTIFIER (qualifying_scope));
17897 qualifying_scope = type;
17900 sfk = sfk_none;
17902 if (unqualified_name)
17904 tree class_type;
17906 if (qualifying_scope
17907 && CLASS_TYPE_P (qualifying_scope))
17908 class_type = qualifying_scope;
17909 else
17910 class_type = current_class_type;
17912 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17914 tree name_type = TREE_TYPE (unqualified_name);
17915 if (class_type && same_type_p (name_type, class_type))
17917 if (qualifying_scope
17918 && CLASSTYPE_USE_TEMPLATE (name_type))
17920 error_at (declarator_id_start_token->location,
17921 "invalid use of constructor as a template");
17922 inform (declarator_id_start_token->location,
17923 "use %<%T::%D%> instead of %<%T::%D%> to "
17924 "name the constructor in a qualified name",
17925 class_type,
17926 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17927 class_type, name_type);
17928 declarator = cp_error_declarator;
17929 break;
17931 else
17932 unqualified_name = constructor_name (class_type);
17934 else
17936 /* We do not attempt to print the declarator
17937 here because we do not have enough
17938 information about its original syntactic
17939 form. */
17940 cp_parser_error (parser, "invalid declarator");
17941 declarator = cp_error_declarator;
17942 break;
17946 if (class_type)
17948 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17949 sfk = sfk_destructor;
17950 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17951 sfk = sfk_conversion;
17952 else if (/* There's no way to declare a constructor
17953 for an anonymous type, even if the type
17954 got a name for linkage purposes. */
17955 !TYPE_WAS_ANONYMOUS (class_type)
17956 /* Handle correctly (c++/19200):
17958 struct S {
17959 struct T{};
17960 friend void S(T);
17963 and also:
17965 namespace N {
17966 void S();
17969 struct S {
17970 friend void N::S();
17971 }; */
17972 && !(friend_p
17973 && class_type != qualifying_scope)
17974 && constructor_name_p (unqualified_name,
17975 class_type))
17977 unqualified_name = constructor_name (class_type);
17978 sfk = sfk_constructor;
17980 else if (is_overloaded_fn (unqualified_name)
17981 && DECL_CONSTRUCTOR_P (get_first_fn
17982 (unqualified_name)))
17983 sfk = sfk_constructor;
17985 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17986 *ctor_dtor_or_conv_p = -1;
17989 declarator = make_id_declarator (qualifying_scope,
17990 unqualified_name,
17991 sfk);
17992 declarator->std_attributes = attrs;
17993 declarator->id_loc = token->location;
17994 declarator->parameter_pack_p = pack_expansion_p;
17996 if (pack_expansion_p)
17997 maybe_warn_variadic_templates ();
18000 handle_declarator:;
18001 scope = get_scope_of_declarator (declarator);
18002 if (scope)
18004 /* Any names that appear after the declarator-id for a
18005 member are looked up in the containing scope. */
18006 if (at_function_scope_p ())
18008 /* But declarations with qualified-ids can't appear in a
18009 function. */
18010 cp_parser_error (parser, "qualified-id in declaration");
18011 declarator = cp_error_declarator;
18012 break;
18014 pushed_scope = push_scope (scope);
18016 parser->in_declarator_p = true;
18017 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18018 || (declarator && declarator->kind == cdk_id))
18019 /* Default args are only allowed on function
18020 declarations. */
18021 parser->default_arg_ok_p = saved_default_arg_ok_p;
18022 else
18023 parser->default_arg_ok_p = false;
18025 first = false;
18027 /* We're done. */
18028 else
18029 break;
18032 /* For an abstract declarator, we might wind up with nothing at this
18033 point. That's an error; the declarator is not optional. */
18034 if (!declarator)
18035 cp_parser_error (parser, "expected declarator");
18037 /* If we entered a scope, we must exit it now. */
18038 if (pushed_scope)
18039 pop_scope (pushed_scope);
18041 parser->default_arg_ok_p = saved_default_arg_ok_p;
18042 parser->in_declarator_p = saved_in_declarator_p;
18044 return declarator;
18047 /* Parse a ptr-operator.
18049 ptr-operator:
18050 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18051 * cv-qualifier-seq [opt]
18053 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18054 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18056 GNU Extension:
18058 ptr-operator:
18059 & cv-qualifier-seq [opt]
18061 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18062 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18063 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18064 filled in with the TYPE containing the member. *CV_QUALS is
18065 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18066 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18067 Note that the tree codes returned by this function have nothing
18068 to do with the types of trees that will be eventually be created
18069 to represent the pointer or reference type being parsed. They are
18070 just constants with suggestive names. */
18071 static enum tree_code
18072 cp_parser_ptr_operator (cp_parser* parser,
18073 tree* type,
18074 cp_cv_quals *cv_quals,
18075 tree *attributes)
18077 enum tree_code code = ERROR_MARK;
18078 cp_token *token;
18079 tree attrs = NULL_TREE;
18081 /* Assume that it's not a pointer-to-member. */
18082 *type = NULL_TREE;
18083 /* And that there are no cv-qualifiers. */
18084 *cv_quals = TYPE_UNQUALIFIED;
18086 /* Peek at the next token. */
18087 token = cp_lexer_peek_token (parser->lexer);
18089 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18090 if (token->type == CPP_MULT)
18091 code = INDIRECT_REF;
18092 else if (token->type == CPP_AND)
18093 code = ADDR_EXPR;
18094 else if ((cxx_dialect != cxx98) &&
18095 token->type == CPP_AND_AND) /* C++0x only */
18096 code = NON_LVALUE_EXPR;
18098 if (code != ERROR_MARK)
18100 /* Consume the `*', `&' or `&&'. */
18101 cp_lexer_consume_token (parser->lexer);
18103 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18104 `&', if we are allowing GNU extensions. (The only qualifier
18105 that can legally appear after `&' is `restrict', but that is
18106 enforced during semantic analysis. */
18107 if (code == INDIRECT_REF
18108 || cp_parser_allow_gnu_extensions_p (parser))
18109 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18111 attrs = cp_parser_std_attribute_spec_seq (parser);
18112 if (attributes != NULL)
18113 *attributes = attrs;
18115 else
18117 /* Try the pointer-to-member case. */
18118 cp_parser_parse_tentatively (parser);
18119 /* Look for the optional `::' operator. */
18120 cp_parser_global_scope_opt (parser,
18121 /*current_scope_valid_p=*/false);
18122 /* Look for the nested-name specifier. */
18123 token = cp_lexer_peek_token (parser->lexer);
18124 cp_parser_nested_name_specifier (parser,
18125 /*typename_keyword_p=*/false,
18126 /*check_dependency_p=*/true,
18127 /*type_p=*/false,
18128 /*is_declaration=*/false);
18129 /* If we found it, and the next token is a `*', then we are
18130 indeed looking at a pointer-to-member operator. */
18131 if (!cp_parser_error_occurred (parser)
18132 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18134 /* Indicate that the `*' operator was used. */
18135 code = INDIRECT_REF;
18137 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18138 error_at (token->location, "%qD is a namespace", parser->scope);
18139 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18140 error_at (token->location, "cannot form pointer to member of "
18141 "non-class %q#T", parser->scope);
18142 else
18144 /* The type of which the member is a member is given by the
18145 current SCOPE. */
18146 *type = parser->scope;
18147 /* The next name will not be qualified. */
18148 parser->scope = NULL_TREE;
18149 parser->qualifying_scope = NULL_TREE;
18150 parser->object_scope = NULL_TREE;
18151 /* Look for optional c++11 attributes. */
18152 attrs = cp_parser_std_attribute_spec_seq (parser);
18153 if (attributes != NULL)
18154 *attributes = attrs;
18155 /* Look for the optional cv-qualifier-seq. */
18156 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18159 /* If that didn't work we don't have a ptr-operator. */
18160 if (!cp_parser_parse_definitely (parser))
18161 cp_parser_error (parser, "expected ptr-operator");
18164 return code;
18167 /* Parse an (optional) cv-qualifier-seq.
18169 cv-qualifier-seq:
18170 cv-qualifier cv-qualifier-seq [opt]
18172 cv-qualifier:
18173 const
18174 volatile
18176 GNU Extension:
18178 cv-qualifier:
18179 __restrict__
18181 Returns a bitmask representing the cv-qualifiers. */
18183 static cp_cv_quals
18184 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18186 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18188 while (true)
18190 cp_token *token;
18191 cp_cv_quals cv_qualifier;
18193 /* Peek at the next token. */
18194 token = cp_lexer_peek_token (parser->lexer);
18195 /* See if it's a cv-qualifier. */
18196 switch (token->keyword)
18198 case RID_CONST:
18199 cv_qualifier = TYPE_QUAL_CONST;
18200 break;
18202 case RID_VOLATILE:
18203 cv_qualifier = TYPE_QUAL_VOLATILE;
18204 break;
18206 case RID_RESTRICT:
18207 cv_qualifier = TYPE_QUAL_RESTRICT;
18208 break;
18210 default:
18211 cv_qualifier = TYPE_UNQUALIFIED;
18212 break;
18215 if (!cv_qualifier)
18216 break;
18218 if (cv_quals & cv_qualifier)
18220 error_at (token->location, "duplicate cv-qualifier");
18221 cp_lexer_purge_token (parser->lexer);
18223 else
18225 cp_lexer_consume_token (parser->lexer);
18226 cv_quals |= cv_qualifier;
18230 return cv_quals;
18233 /* Parse an (optional) ref-qualifier
18235 ref-qualifier:
18239 Returns cp_ref_qualifier representing ref-qualifier. */
18241 static cp_ref_qualifier
18242 cp_parser_ref_qualifier_opt (cp_parser* parser)
18244 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18246 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18247 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18248 return ref_qual;
18250 while (true)
18252 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18253 cp_token *token = cp_lexer_peek_token (parser->lexer);
18255 switch (token->type)
18257 case CPP_AND:
18258 curr_ref_qual = REF_QUAL_LVALUE;
18259 break;
18261 case CPP_AND_AND:
18262 curr_ref_qual = REF_QUAL_RVALUE;
18263 break;
18265 default:
18266 curr_ref_qual = REF_QUAL_NONE;
18267 break;
18270 if (!curr_ref_qual)
18271 break;
18272 else if (ref_qual)
18274 error_at (token->location, "multiple ref-qualifiers");
18275 cp_lexer_purge_token (parser->lexer);
18277 else
18279 ref_qual = curr_ref_qual;
18280 cp_lexer_consume_token (parser->lexer);
18284 return ref_qual;
18287 /* Parse an (optional) virt-specifier-seq.
18289 virt-specifier-seq:
18290 virt-specifier virt-specifier-seq [opt]
18292 virt-specifier:
18293 override
18294 final
18296 Returns a bitmask representing the virt-specifiers. */
18298 static cp_virt_specifiers
18299 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18301 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18303 while (true)
18305 cp_token *token;
18306 cp_virt_specifiers virt_specifier;
18308 /* Peek at the next token. */
18309 token = cp_lexer_peek_token (parser->lexer);
18310 /* See if it's a virt-specifier-qualifier. */
18311 if (token->type != CPP_NAME)
18312 break;
18313 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18315 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18316 virt_specifier = VIRT_SPEC_OVERRIDE;
18318 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18320 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18321 virt_specifier = VIRT_SPEC_FINAL;
18323 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18325 virt_specifier = VIRT_SPEC_FINAL;
18327 else
18328 break;
18330 if (virt_specifiers & virt_specifier)
18332 error_at (token->location, "duplicate virt-specifier");
18333 cp_lexer_purge_token (parser->lexer);
18335 else
18337 cp_lexer_consume_token (parser->lexer);
18338 virt_specifiers |= virt_specifier;
18341 return virt_specifiers;
18344 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18345 is in scope even though it isn't real. */
18347 void
18348 inject_this_parameter (tree ctype, cp_cv_quals quals)
18350 tree this_parm;
18352 if (current_class_ptr)
18354 /* We don't clear this between NSDMIs. Is it already what we want? */
18355 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18356 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18357 && cp_type_quals (type) == quals)
18358 return;
18361 this_parm = build_this_parm (ctype, quals);
18362 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18363 current_class_ptr = NULL_TREE;
18364 current_class_ref
18365 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18366 current_class_ptr = this_parm;
18369 /* Return true iff our current scope is a non-static data member
18370 initializer. */
18372 bool
18373 parsing_nsdmi (void)
18375 /* We recognize NSDMI context by the context-less 'this' pointer set up
18376 by the function above. */
18377 if (current_class_ptr
18378 && TREE_CODE (current_class_ptr) == PARM_DECL
18379 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18380 return true;
18381 return false;
18384 /* Parse a late-specified return type, if any. This is not a separate
18385 non-terminal, but part of a function declarator, which looks like
18387 -> trailing-type-specifier-seq abstract-declarator(opt)
18389 Returns the type indicated by the type-id.
18391 In addition to this this parses any queued up omp declare simd
18392 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18394 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18395 function. */
18397 static tree
18398 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18399 cp_cv_quals quals)
18401 cp_token *token;
18402 tree type = NULL_TREE;
18403 bool declare_simd_p = (parser->omp_declare_simd
18404 && declarator
18405 && declarator->kind == cdk_id);
18407 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18408 && declarator && declarator->kind == cdk_id);
18410 /* Peek at the next token. */
18411 token = cp_lexer_peek_token (parser->lexer);
18412 /* A late-specified return type is indicated by an initial '->'. */
18413 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18414 return NULL_TREE;
18416 tree save_ccp = current_class_ptr;
18417 tree save_ccr = current_class_ref;
18418 if (quals >= 0)
18420 /* DR 1207: 'this' is in scope in the trailing return type. */
18421 inject_this_parameter (current_class_type, quals);
18424 if (token->type == CPP_DEREF)
18426 /* Consume the ->. */
18427 cp_lexer_consume_token (parser->lexer);
18429 type = cp_parser_trailing_type_id (parser);
18432 if (cilk_simd_fn_vector_p)
18433 declarator->std_attributes
18434 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18435 declarator->std_attributes);
18436 if (declare_simd_p)
18437 declarator->std_attributes
18438 = cp_parser_late_parsing_omp_declare_simd (parser,
18439 declarator->std_attributes);
18441 if (quals >= 0)
18443 current_class_ptr = save_ccp;
18444 current_class_ref = save_ccr;
18447 return type;
18450 /* Parse a declarator-id.
18452 declarator-id:
18453 id-expression
18454 :: [opt] nested-name-specifier [opt] type-name
18456 In the `id-expression' case, the value returned is as for
18457 cp_parser_id_expression if the id-expression was an unqualified-id.
18458 If the id-expression was a qualified-id, then a SCOPE_REF is
18459 returned. The first operand is the scope (either a NAMESPACE_DECL
18460 or TREE_TYPE), but the second is still just a representation of an
18461 unqualified-id. */
18463 static tree
18464 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18466 tree id;
18467 /* The expression must be an id-expression. Assume that qualified
18468 names are the names of types so that:
18470 template <class T>
18471 int S<T>::R::i = 3;
18473 will work; we must treat `S<T>::R' as the name of a type.
18474 Similarly, assume that qualified names are templates, where
18475 required, so that:
18477 template <class T>
18478 int S<T>::R<T>::i = 3;
18480 will work, too. */
18481 id = cp_parser_id_expression (parser,
18482 /*template_keyword_p=*/false,
18483 /*check_dependency_p=*/false,
18484 /*template_p=*/NULL,
18485 /*declarator_p=*/true,
18486 optional_p);
18487 if (id && BASELINK_P (id))
18488 id = BASELINK_FUNCTIONS (id);
18489 return id;
18492 /* Parse a type-id.
18494 type-id:
18495 type-specifier-seq abstract-declarator [opt]
18497 Returns the TYPE specified. */
18499 static tree
18500 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18501 bool is_trailing_return)
18503 cp_decl_specifier_seq type_specifier_seq;
18504 cp_declarator *abstract_declarator;
18506 /* Parse the type-specifier-seq. */
18507 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18508 is_trailing_return,
18509 &type_specifier_seq);
18510 if (type_specifier_seq.type == error_mark_node)
18511 return error_mark_node;
18513 /* There might or might not be an abstract declarator. */
18514 cp_parser_parse_tentatively (parser);
18515 /* Look for the declarator. */
18516 abstract_declarator
18517 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18518 /*parenthesized_p=*/NULL,
18519 /*member_p=*/false,
18520 /*friend_p=*/false);
18521 /* Check to see if there really was a declarator. */
18522 if (!cp_parser_parse_definitely (parser))
18523 abstract_declarator = NULL;
18525 if (type_specifier_seq.type
18526 /* None of the valid uses of 'auto' in C++14 involve the type-id
18527 nonterminal, but it is valid in a trailing-return-type. */
18528 && !(cxx_dialect >= cxx14 && is_trailing_return)
18529 && type_uses_auto (type_specifier_seq.type))
18531 /* A type-id with type 'auto' is only ok if the abstract declarator
18532 is a function declarator with a late-specified return type. */
18533 if (abstract_declarator
18534 && abstract_declarator->kind == cdk_function
18535 && abstract_declarator->u.function.late_return_type)
18536 /* OK */;
18537 else
18539 error ("invalid use of %<auto%>");
18540 return error_mark_node;
18544 return groktypename (&type_specifier_seq, abstract_declarator,
18545 is_template_arg);
18548 static tree cp_parser_type_id (cp_parser *parser)
18550 return cp_parser_type_id_1 (parser, false, false);
18553 static tree cp_parser_template_type_arg (cp_parser *parser)
18555 tree r;
18556 const char *saved_message = parser->type_definition_forbidden_message;
18557 parser->type_definition_forbidden_message
18558 = G_("types may not be defined in template arguments");
18559 r = cp_parser_type_id_1 (parser, true, false);
18560 parser->type_definition_forbidden_message = saved_message;
18561 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18563 error ("invalid use of %<auto%> in template argument");
18564 r = error_mark_node;
18566 return r;
18569 static tree cp_parser_trailing_type_id (cp_parser *parser)
18571 return cp_parser_type_id_1 (parser, false, true);
18574 /* Parse a type-specifier-seq.
18576 type-specifier-seq:
18577 type-specifier type-specifier-seq [opt]
18579 GNU extension:
18581 type-specifier-seq:
18582 attributes type-specifier-seq [opt]
18584 If IS_DECLARATION is true, we are at the start of a "condition" or
18585 exception-declaration, so we might be followed by a declarator-id.
18587 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18588 i.e. we've just seen "->".
18590 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18592 static void
18593 cp_parser_type_specifier_seq (cp_parser* parser,
18594 bool is_declaration,
18595 bool is_trailing_return,
18596 cp_decl_specifier_seq *type_specifier_seq)
18598 bool seen_type_specifier = false;
18599 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18600 cp_token *start_token = NULL;
18602 /* Clear the TYPE_SPECIFIER_SEQ. */
18603 clear_decl_specs (type_specifier_seq);
18605 /* In the context of a trailing return type, enum E { } is an
18606 elaborated-type-specifier followed by a function-body, not an
18607 enum-specifier. */
18608 if (is_trailing_return)
18609 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18611 /* Parse the type-specifiers and attributes. */
18612 while (true)
18614 tree type_specifier;
18615 bool is_cv_qualifier;
18617 /* Check for attributes first. */
18618 if (cp_next_tokens_can_be_attribute_p (parser))
18620 type_specifier_seq->attributes =
18621 chainon (type_specifier_seq->attributes,
18622 cp_parser_attributes_opt (parser));
18623 continue;
18626 /* record the token of the beginning of the type specifier seq,
18627 for error reporting purposes*/
18628 if (!start_token)
18629 start_token = cp_lexer_peek_token (parser->lexer);
18631 /* Look for the type-specifier. */
18632 type_specifier = cp_parser_type_specifier (parser,
18633 flags,
18634 type_specifier_seq,
18635 /*is_declaration=*/false,
18636 NULL,
18637 &is_cv_qualifier);
18638 if (!type_specifier)
18640 /* If the first type-specifier could not be found, this is not a
18641 type-specifier-seq at all. */
18642 if (!seen_type_specifier)
18644 /* Set in_declarator_p to avoid skipping to the semicolon. */
18645 int in_decl = parser->in_declarator_p;
18646 parser->in_declarator_p = true;
18648 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18649 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18650 cp_parser_error (parser, "expected type-specifier");
18652 parser->in_declarator_p = in_decl;
18654 type_specifier_seq->type = error_mark_node;
18655 return;
18657 /* If subsequent type-specifiers could not be found, the
18658 type-specifier-seq is complete. */
18659 break;
18662 seen_type_specifier = true;
18663 /* The standard says that a condition can be:
18665 type-specifier-seq declarator = assignment-expression
18667 However, given:
18669 struct S {};
18670 if (int S = ...)
18672 we should treat the "S" as a declarator, not as a
18673 type-specifier. The standard doesn't say that explicitly for
18674 type-specifier-seq, but it does say that for
18675 decl-specifier-seq in an ordinary declaration. Perhaps it
18676 would be clearer just to allow a decl-specifier-seq here, and
18677 then add a semantic restriction that if any decl-specifiers
18678 that are not type-specifiers appear, the program is invalid. */
18679 if (is_declaration && !is_cv_qualifier)
18680 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18684 /* Return whether the function currently being declared has an associated
18685 template parameter list. */
18687 static bool
18688 function_being_declared_is_template_p (cp_parser* parser)
18690 if (!current_template_parms || processing_template_parmlist)
18691 return false;
18693 if (parser->implicit_template_scope)
18694 return true;
18696 if (at_class_scope_p ()
18697 && TYPE_BEING_DEFINED (current_class_type))
18698 return parser->num_template_parameter_lists != 0;
18700 return ((int) parser->num_template_parameter_lists > template_class_depth
18701 (current_class_type));
18704 /* Parse a parameter-declaration-clause.
18706 parameter-declaration-clause:
18707 parameter-declaration-list [opt] ... [opt]
18708 parameter-declaration-list , ...
18710 Returns a representation for the parameter declarations. A return
18711 value of NULL indicates a parameter-declaration-clause consisting
18712 only of an ellipsis. */
18714 static tree
18715 cp_parser_parameter_declaration_clause (cp_parser* parser)
18717 tree parameters;
18718 cp_token *token;
18719 bool ellipsis_p;
18720 bool is_error;
18722 struct cleanup {
18723 cp_parser* parser;
18724 int auto_is_implicit_function_template_parm_p;
18725 ~cleanup() {
18726 parser->auto_is_implicit_function_template_parm_p
18727 = auto_is_implicit_function_template_parm_p;
18729 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18731 (void) cleanup;
18733 if (!processing_specialization
18734 && !processing_template_parmlist
18735 && !processing_explicit_instantiation)
18736 if (!current_function_decl
18737 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18738 parser->auto_is_implicit_function_template_parm_p = true;
18740 /* Peek at the next token. */
18741 token = cp_lexer_peek_token (parser->lexer);
18742 /* Check for trivial parameter-declaration-clauses. */
18743 if (token->type == CPP_ELLIPSIS)
18745 /* Consume the `...' token. */
18746 cp_lexer_consume_token (parser->lexer);
18747 return NULL_TREE;
18749 else if (token->type == CPP_CLOSE_PAREN)
18750 /* There are no parameters. */
18752 #ifndef NO_IMPLICIT_EXTERN_C
18753 if (in_system_header_at (input_location)
18754 && current_class_type == NULL
18755 && current_lang_name == lang_name_c)
18756 return NULL_TREE;
18757 else
18758 #endif
18759 return void_list_node;
18761 /* Check for `(void)', too, which is a special case. */
18762 else if (token->keyword == RID_VOID
18763 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18764 == CPP_CLOSE_PAREN))
18766 /* Consume the `void' token. */
18767 cp_lexer_consume_token (parser->lexer);
18768 /* There are no parameters. */
18769 return void_list_node;
18772 /* Parse the parameter-declaration-list. */
18773 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18774 /* If a parse error occurred while parsing the
18775 parameter-declaration-list, then the entire
18776 parameter-declaration-clause is erroneous. */
18777 if (is_error)
18778 return NULL;
18780 /* Peek at the next token. */
18781 token = cp_lexer_peek_token (parser->lexer);
18782 /* If it's a `,', the clause should terminate with an ellipsis. */
18783 if (token->type == CPP_COMMA)
18785 /* Consume the `,'. */
18786 cp_lexer_consume_token (parser->lexer);
18787 /* Expect an ellipsis. */
18788 ellipsis_p
18789 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18791 /* It might also be `...' if the optional trailing `,' was
18792 omitted. */
18793 else if (token->type == CPP_ELLIPSIS)
18795 /* Consume the `...' token. */
18796 cp_lexer_consume_token (parser->lexer);
18797 /* And remember that we saw it. */
18798 ellipsis_p = true;
18800 else
18801 ellipsis_p = false;
18803 /* Finish the parameter list. */
18804 if (!ellipsis_p)
18805 parameters = chainon (parameters, void_list_node);
18807 return parameters;
18810 /* Parse a parameter-declaration-list.
18812 parameter-declaration-list:
18813 parameter-declaration
18814 parameter-declaration-list , parameter-declaration
18816 Returns a representation of the parameter-declaration-list, as for
18817 cp_parser_parameter_declaration_clause. However, the
18818 `void_list_node' is never appended to the list. Upon return,
18819 *IS_ERROR will be true iff an error occurred. */
18821 static tree
18822 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18824 tree parameters = NULL_TREE;
18825 tree *tail = &parameters;
18826 bool saved_in_unbraced_linkage_specification_p;
18827 int index = 0;
18829 /* Assume all will go well. */
18830 *is_error = false;
18831 /* The special considerations that apply to a function within an
18832 unbraced linkage specifications do not apply to the parameters
18833 to the function. */
18834 saved_in_unbraced_linkage_specification_p
18835 = parser->in_unbraced_linkage_specification_p;
18836 parser->in_unbraced_linkage_specification_p = false;
18838 /* Look for more parameters. */
18839 while (true)
18841 cp_parameter_declarator *parameter;
18842 tree decl = error_mark_node;
18843 bool parenthesized_p = false;
18844 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18845 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18846 (current_template_parms)) : 0);
18848 /* Parse the parameter. */
18849 parameter
18850 = cp_parser_parameter_declaration (parser,
18851 /*template_parm_p=*/false,
18852 &parenthesized_p);
18854 /* We don't know yet if the enclosing context is deprecated, so wait
18855 and warn in grokparms if appropriate. */
18856 deprecated_state = DEPRECATED_SUPPRESS;
18858 if (parameter)
18860 /* If a function parameter pack was specified and an implicit template
18861 parameter was introduced during cp_parser_parameter_declaration,
18862 change any implicit parameters introduced into packs. */
18863 if (parser->implicit_template_parms
18864 && parameter->declarator
18865 && parameter->declarator->parameter_pack_p)
18867 int latest_template_parm_idx = TREE_VEC_LENGTH
18868 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18870 if (latest_template_parm_idx != template_parm_idx)
18871 parameter->decl_specifiers.type = convert_generic_types_to_packs
18872 (parameter->decl_specifiers.type,
18873 template_parm_idx, latest_template_parm_idx);
18876 decl = grokdeclarator (parameter->declarator,
18877 &parameter->decl_specifiers,
18878 PARM,
18879 parameter->default_argument != NULL_TREE,
18880 &parameter->decl_specifiers.attributes);
18883 deprecated_state = DEPRECATED_NORMAL;
18885 /* If a parse error occurred parsing the parameter declaration,
18886 then the entire parameter-declaration-list is erroneous. */
18887 if (decl == error_mark_node)
18889 *is_error = true;
18890 parameters = error_mark_node;
18891 break;
18894 if (parameter->decl_specifiers.attributes)
18895 cplus_decl_attributes (&decl,
18896 parameter->decl_specifiers.attributes,
18898 if (DECL_NAME (decl))
18899 decl = pushdecl (decl);
18901 if (decl != error_mark_node)
18903 retrofit_lang_decl (decl);
18904 DECL_PARM_INDEX (decl) = ++index;
18905 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18908 /* Add the new parameter to the list. */
18909 *tail = build_tree_list (parameter->default_argument, decl);
18910 tail = &TREE_CHAIN (*tail);
18912 /* Peek at the next token. */
18913 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18914 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18915 /* These are for Objective-C++ */
18916 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18917 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18918 /* The parameter-declaration-list is complete. */
18919 break;
18920 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18922 cp_token *token;
18924 /* Peek at the next token. */
18925 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18926 /* If it's an ellipsis, then the list is complete. */
18927 if (token->type == CPP_ELLIPSIS)
18928 break;
18929 /* Otherwise, there must be more parameters. Consume the
18930 `,'. */
18931 cp_lexer_consume_token (parser->lexer);
18932 /* When parsing something like:
18934 int i(float f, double d)
18936 we can tell after seeing the declaration for "f" that we
18937 are not looking at an initialization of a variable "i",
18938 but rather at the declaration of a function "i".
18940 Due to the fact that the parsing of template arguments
18941 (as specified to a template-id) requires backtracking we
18942 cannot use this technique when inside a template argument
18943 list. */
18944 if (!parser->in_template_argument_list_p
18945 && !parser->in_type_id_in_expr_p
18946 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18947 /* However, a parameter-declaration of the form
18948 "float(f)" (which is a valid declaration of a
18949 parameter "f") can also be interpreted as an
18950 expression (the conversion of "f" to "float"). */
18951 && !parenthesized_p)
18952 cp_parser_commit_to_tentative_parse (parser);
18954 else
18956 cp_parser_error (parser, "expected %<,%> or %<...%>");
18957 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18958 cp_parser_skip_to_closing_parenthesis (parser,
18959 /*recovering=*/true,
18960 /*or_comma=*/false,
18961 /*consume_paren=*/false);
18962 break;
18966 parser->in_unbraced_linkage_specification_p
18967 = saved_in_unbraced_linkage_specification_p;
18969 /* Reset implicit_template_scope if we are about to leave the function
18970 parameter list that introduced it. Note that for out-of-line member
18971 definitions, there will be one or more class scopes before we get to
18972 the template parameter scope. */
18974 if (cp_binding_level *its = parser->implicit_template_scope)
18975 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18977 while (maybe_its->kind == sk_class)
18978 maybe_its = maybe_its->level_chain;
18979 if (maybe_its == its)
18981 parser->implicit_template_parms = 0;
18982 parser->implicit_template_scope = 0;
18986 return parameters;
18989 /* Parse a parameter declaration.
18991 parameter-declaration:
18992 decl-specifier-seq ... [opt] declarator
18993 decl-specifier-seq declarator = assignment-expression
18994 decl-specifier-seq ... [opt] abstract-declarator [opt]
18995 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18997 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18998 declares a template parameter. (In that case, a non-nested `>'
18999 token encountered during the parsing of the assignment-expression
19000 is not interpreted as a greater-than operator.)
19002 Returns a representation of the parameter, or NULL if an error
19003 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19004 true iff the declarator is of the form "(p)". */
19006 static cp_parameter_declarator *
19007 cp_parser_parameter_declaration (cp_parser *parser,
19008 bool template_parm_p,
19009 bool *parenthesized_p)
19011 int declares_class_or_enum;
19012 cp_decl_specifier_seq decl_specifiers;
19013 cp_declarator *declarator;
19014 tree default_argument;
19015 cp_token *token = NULL, *declarator_token_start = NULL;
19016 const char *saved_message;
19018 /* In a template parameter, `>' is not an operator.
19020 [temp.param]
19022 When parsing a default template-argument for a non-type
19023 template-parameter, the first non-nested `>' is taken as the end
19024 of the template parameter-list rather than a greater-than
19025 operator. */
19027 /* Type definitions may not appear in parameter types. */
19028 saved_message = parser->type_definition_forbidden_message;
19029 parser->type_definition_forbidden_message
19030 = G_("types may not be defined in parameter types");
19032 /* Parse the declaration-specifiers. */
19033 cp_parser_decl_specifier_seq (parser,
19034 CP_PARSER_FLAGS_NONE,
19035 &decl_specifiers,
19036 &declares_class_or_enum);
19038 /* Complain about missing 'typename' or other invalid type names. */
19039 if (!decl_specifiers.any_type_specifiers_p
19040 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19041 decl_specifiers.type = error_mark_node;
19043 /* If an error occurred, there's no reason to attempt to parse the
19044 rest of the declaration. */
19045 if (cp_parser_error_occurred (parser))
19047 parser->type_definition_forbidden_message = saved_message;
19048 return NULL;
19051 /* Peek at the next token. */
19052 token = cp_lexer_peek_token (parser->lexer);
19054 /* If the next token is a `)', `,', `=', `>', or `...', then there
19055 is no declarator. However, when variadic templates are enabled,
19056 there may be a declarator following `...'. */
19057 if (token->type == CPP_CLOSE_PAREN
19058 || token->type == CPP_COMMA
19059 || token->type == CPP_EQ
19060 || token->type == CPP_GREATER)
19062 declarator = NULL;
19063 if (parenthesized_p)
19064 *parenthesized_p = false;
19066 /* Otherwise, there should be a declarator. */
19067 else
19069 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19070 parser->default_arg_ok_p = false;
19072 /* After seeing a decl-specifier-seq, if the next token is not a
19073 "(", there is no possibility that the code is a valid
19074 expression. Therefore, if parsing tentatively, we commit at
19075 this point. */
19076 if (!parser->in_template_argument_list_p
19077 /* In an expression context, having seen:
19079 (int((char ...
19081 we cannot be sure whether we are looking at a
19082 function-type (taking a "char" as a parameter) or a cast
19083 of some object of type "char" to "int". */
19084 && !parser->in_type_id_in_expr_p
19085 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19086 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19087 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19088 cp_parser_commit_to_tentative_parse (parser);
19089 /* Parse the declarator. */
19090 declarator_token_start = token;
19091 declarator = cp_parser_declarator (parser,
19092 CP_PARSER_DECLARATOR_EITHER,
19093 /*ctor_dtor_or_conv_p=*/NULL,
19094 parenthesized_p,
19095 /*member_p=*/false,
19096 /*friend_p=*/false);
19097 parser->default_arg_ok_p = saved_default_arg_ok_p;
19098 /* After the declarator, allow more attributes. */
19099 decl_specifiers.attributes
19100 = chainon (decl_specifiers.attributes,
19101 cp_parser_attributes_opt (parser));
19104 /* If the next token is an ellipsis, and we have not seen a
19105 declarator name, and the type of the declarator contains parameter
19106 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19107 a parameter pack expansion expression. Otherwise, leave the
19108 ellipsis for a C-style variadic function. */
19109 token = cp_lexer_peek_token (parser->lexer);
19110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19112 tree type = decl_specifiers.type;
19114 if (type && DECL_P (type))
19115 type = TREE_TYPE (type);
19117 if (type
19118 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19119 && declarator_can_be_parameter_pack (declarator)
19120 && (!declarator || !declarator->parameter_pack_p)
19121 && uses_parameter_packs (type))
19123 /* Consume the `...'. */
19124 cp_lexer_consume_token (parser->lexer);
19125 maybe_warn_variadic_templates ();
19127 /* Build a pack expansion type */
19128 if (declarator)
19129 declarator->parameter_pack_p = true;
19130 else
19131 decl_specifiers.type = make_pack_expansion (type);
19135 /* The restriction on defining new types applies only to the type
19136 of the parameter, not to the default argument. */
19137 parser->type_definition_forbidden_message = saved_message;
19139 /* If the next token is `=', then process a default argument. */
19140 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19142 token = cp_lexer_peek_token (parser->lexer);
19143 /* If we are defining a class, then the tokens that make up the
19144 default argument must be saved and processed later. */
19145 if (!template_parm_p && at_class_scope_p ()
19146 && TYPE_BEING_DEFINED (current_class_type)
19147 && !LAMBDA_TYPE_P (current_class_type))
19148 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19149 /* Outside of a class definition, we can just parse the
19150 assignment-expression. */
19151 else
19152 default_argument
19153 = cp_parser_default_argument (parser, template_parm_p);
19155 if (!parser->default_arg_ok_p)
19157 if (flag_permissive)
19158 warning (0, "deprecated use of default argument for parameter of non-function");
19159 else
19161 error_at (token->location,
19162 "default arguments are only "
19163 "permitted for function parameters");
19164 default_argument = NULL_TREE;
19167 else if ((declarator && declarator->parameter_pack_p)
19168 || (decl_specifiers.type
19169 && PACK_EXPANSION_P (decl_specifiers.type)))
19171 /* Find the name of the parameter pack. */
19172 cp_declarator *id_declarator = declarator;
19173 while (id_declarator && id_declarator->kind != cdk_id)
19174 id_declarator = id_declarator->declarator;
19176 if (id_declarator && id_declarator->kind == cdk_id)
19177 error_at (declarator_token_start->location,
19178 template_parm_p
19179 ? G_("template parameter pack %qD "
19180 "cannot have a default argument")
19181 : G_("parameter pack %qD cannot have "
19182 "a default argument"),
19183 id_declarator->u.id.unqualified_name);
19184 else
19185 error_at (declarator_token_start->location,
19186 template_parm_p
19187 ? G_("template parameter pack cannot have "
19188 "a default argument")
19189 : G_("parameter pack cannot have a "
19190 "default argument"));
19192 default_argument = NULL_TREE;
19195 else
19196 default_argument = NULL_TREE;
19198 return make_parameter_declarator (&decl_specifiers,
19199 declarator,
19200 default_argument);
19203 /* Parse a default argument and return it.
19205 TEMPLATE_PARM_P is true if this is a default argument for a
19206 non-type template parameter. */
19207 static tree
19208 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19210 tree default_argument = NULL_TREE;
19211 bool saved_greater_than_is_operator_p;
19212 bool saved_local_variables_forbidden_p;
19213 bool non_constant_p, is_direct_init;
19215 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19216 set correctly. */
19217 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19218 parser->greater_than_is_operator_p = !template_parm_p;
19219 /* Local variable names (and the `this' keyword) may not
19220 appear in a default argument. */
19221 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19222 parser->local_variables_forbidden_p = true;
19223 /* Parse the assignment-expression. */
19224 if (template_parm_p)
19225 push_deferring_access_checks (dk_no_deferred);
19226 tree saved_class_ptr = NULL_TREE;
19227 tree saved_class_ref = NULL_TREE;
19228 /* The "this" pointer is not valid in a default argument. */
19229 if (cfun)
19231 saved_class_ptr = current_class_ptr;
19232 cp_function_chain->x_current_class_ptr = NULL_TREE;
19233 saved_class_ref = current_class_ref;
19234 cp_function_chain->x_current_class_ref = NULL_TREE;
19236 default_argument
19237 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19238 /* Restore the "this" pointer. */
19239 if (cfun)
19241 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19242 cp_function_chain->x_current_class_ref = saved_class_ref;
19244 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19246 if (template_parm_p)
19247 pop_deferring_access_checks ();
19248 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19249 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19251 return default_argument;
19254 /* Parse a function-body.
19256 function-body:
19257 compound_statement */
19259 static void
19260 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19262 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19265 /* Parse a ctor-initializer-opt followed by a function-body. Return
19266 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19267 is true we are parsing a function-try-block. */
19269 static bool
19270 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19271 bool in_function_try_block)
19273 tree body, list;
19274 bool ctor_initializer_p;
19275 const bool check_body_p =
19276 DECL_CONSTRUCTOR_P (current_function_decl)
19277 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19278 tree last = NULL;
19280 /* Begin the function body. */
19281 body = begin_function_body ();
19282 /* Parse the optional ctor-initializer. */
19283 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19285 /* If we're parsing a constexpr constructor definition, we need
19286 to check that the constructor body is indeed empty. However,
19287 before we get to cp_parser_function_body lot of junk has been
19288 generated, so we can't just check that we have an empty block.
19289 Rather we take a snapshot of the outermost block, and check whether
19290 cp_parser_function_body changed its state. */
19291 if (check_body_p)
19293 list = cur_stmt_list;
19294 if (STATEMENT_LIST_TAIL (list))
19295 last = STATEMENT_LIST_TAIL (list)->stmt;
19297 /* Parse the function-body. */
19298 cp_parser_function_body (parser, in_function_try_block);
19299 if (check_body_p)
19300 check_constexpr_ctor_body (last, list, /*complain=*/true);
19301 /* Finish the function body. */
19302 finish_function_body (body);
19304 return ctor_initializer_p;
19307 /* Parse an initializer.
19309 initializer:
19310 = initializer-clause
19311 ( expression-list )
19313 Returns an expression representing the initializer. If no
19314 initializer is present, NULL_TREE is returned.
19316 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19317 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19318 set to TRUE if there is no initializer present. If there is an
19319 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19320 is set to true; otherwise it is set to false. */
19322 static tree
19323 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19324 bool* non_constant_p)
19326 cp_token *token;
19327 tree init;
19329 /* Peek at the next token. */
19330 token = cp_lexer_peek_token (parser->lexer);
19332 /* Let our caller know whether or not this initializer was
19333 parenthesized. */
19334 *is_direct_init = (token->type != CPP_EQ);
19335 /* Assume that the initializer is constant. */
19336 *non_constant_p = false;
19338 if (token->type == CPP_EQ)
19340 /* Consume the `='. */
19341 cp_lexer_consume_token (parser->lexer);
19342 /* Parse the initializer-clause. */
19343 init = cp_parser_initializer_clause (parser, non_constant_p);
19345 else if (token->type == CPP_OPEN_PAREN)
19347 vec<tree, va_gc> *vec;
19348 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19349 /*cast_p=*/false,
19350 /*allow_expansion_p=*/true,
19351 non_constant_p);
19352 if (vec == NULL)
19353 return error_mark_node;
19354 init = build_tree_list_vec (vec);
19355 release_tree_vector (vec);
19357 else if (token->type == CPP_OPEN_BRACE)
19359 cp_lexer_set_source_position (parser->lexer);
19360 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19361 init = cp_parser_braced_list (parser, non_constant_p);
19362 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19364 else
19366 /* Anything else is an error. */
19367 cp_parser_error (parser, "expected initializer");
19368 init = error_mark_node;
19371 return init;
19374 /* Parse an initializer-clause.
19376 initializer-clause:
19377 assignment-expression
19378 braced-init-list
19380 Returns an expression representing the initializer.
19382 If the `assignment-expression' production is used the value
19383 returned is simply a representation for the expression.
19385 Otherwise, calls cp_parser_braced_list. */
19387 static tree
19388 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19390 tree initializer;
19392 /* Assume the expression is constant. */
19393 *non_constant_p = false;
19395 /* If it is not a `{', then we are looking at an
19396 assignment-expression. */
19397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19399 initializer
19400 = cp_parser_constant_expression (parser,
19401 /*allow_non_constant_p=*/true,
19402 non_constant_p);
19404 else
19405 initializer = cp_parser_braced_list (parser, non_constant_p);
19407 return initializer;
19410 /* Parse a brace-enclosed initializer list.
19412 braced-init-list:
19413 { initializer-list , [opt] }
19416 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19417 the elements of the initializer-list (or NULL, if the last
19418 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19419 NULL_TREE. There is no way to detect whether or not the optional
19420 trailing `,' was provided. NON_CONSTANT_P is as for
19421 cp_parser_initializer. */
19423 static tree
19424 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19426 tree initializer;
19428 /* Consume the `{' token. */
19429 cp_lexer_consume_token (parser->lexer);
19430 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19431 initializer = make_node (CONSTRUCTOR);
19432 /* If it's not a `}', then there is a non-trivial initializer. */
19433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19435 /* Parse the initializer list. */
19436 CONSTRUCTOR_ELTS (initializer)
19437 = cp_parser_initializer_list (parser, non_constant_p);
19438 /* A trailing `,' token is allowed. */
19439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19440 cp_lexer_consume_token (parser->lexer);
19442 else
19443 *non_constant_p = false;
19444 /* Now, there should be a trailing `}'. */
19445 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19446 TREE_TYPE (initializer) = init_list_type_node;
19447 return initializer;
19450 /* Consume tokens up to, and including, the next non-nested closing `]'.
19451 Returns true iff we found a closing `]'. */
19453 static bool
19454 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19456 unsigned square_depth = 0;
19458 while (true)
19460 cp_token * token = cp_lexer_peek_token (parser->lexer);
19462 switch (token->type)
19464 case CPP_EOF:
19465 case CPP_PRAGMA_EOL:
19466 /* If we've run out of tokens, then there is no closing `]'. */
19467 return false;
19469 case CPP_OPEN_SQUARE:
19470 ++square_depth;
19471 break;
19473 case CPP_CLOSE_SQUARE:
19474 if (!square_depth--)
19476 cp_lexer_consume_token (parser->lexer);
19477 return true;
19479 break;
19481 default:
19482 break;
19485 /* Consume the token. */
19486 cp_lexer_consume_token (parser->lexer);
19490 /* Return true if we are looking at an array-designator, false otherwise. */
19492 static bool
19493 cp_parser_array_designator_p (cp_parser *parser)
19495 /* Consume the `['. */
19496 cp_lexer_consume_token (parser->lexer);
19498 cp_lexer_save_tokens (parser->lexer);
19500 /* Skip tokens until the next token is a closing square bracket.
19501 If we find the closing `]', and the next token is a `=', then
19502 we are looking at an array designator. */
19503 bool array_designator_p
19504 = (cp_parser_skip_to_closing_square_bracket (parser)
19505 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19507 /* Roll back the tokens we skipped. */
19508 cp_lexer_rollback_tokens (parser->lexer);
19510 return array_designator_p;
19513 /* Parse an initializer-list.
19515 initializer-list:
19516 initializer-clause ... [opt]
19517 initializer-list , initializer-clause ... [opt]
19519 GNU Extension:
19521 initializer-list:
19522 designation initializer-clause ...[opt]
19523 initializer-list , designation initializer-clause ...[opt]
19525 designation:
19526 . identifier =
19527 identifier :
19528 [ constant-expression ] =
19530 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19531 for the initializer. If the INDEX of the elt is non-NULL, it is the
19532 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19533 as for cp_parser_initializer. */
19535 static vec<constructor_elt, va_gc> *
19536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19538 vec<constructor_elt, va_gc> *v = NULL;
19540 /* Assume all of the expressions are constant. */
19541 *non_constant_p = false;
19543 /* Parse the rest of the list. */
19544 while (true)
19546 cp_token *token;
19547 tree designator;
19548 tree initializer;
19549 bool clause_non_constant_p;
19551 /* If the next token is an identifier and the following one is a
19552 colon, we are looking at the GNU designated-initializer
19553 syntax. */
19554 if (cp_parser_allow_gnu_extensions_p (parser)
19555 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19558 /* Warn the user that they are using an extension. */
19559 pedwarn (input_location, OPT_Wpedantic,
19560 "ISO C++ does not allow designated initializers");
19561 /* Consume the identifier. */
19562 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19563 /* Consume the `:'. */
19564 cp_lexer_consume_token (parser->lexer);
19566 /* Also handle the C99 syntax, '. id ='. */
19567 else if (cp_parser_allow_gnu_extensions_p (parser)
19568 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19569 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19570 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19572 /* Warn the user that they are using an extension. */
19573 pedwarn (input_location, OPT_Wpedantic,
19574 "ISO C++ does not allow C99 designated initializers");
19575 /* Consume the `.'. */
19576 cp_lexer_consume_token (parser->lexer);
19577 /* Consume the identifier. */
19578 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19579 /* Consume the `='. */
19580 cp_lexer_consume_token (parser->lexer);
19582 /* Also handle C99 array designators, '[ const ] ='. */
19583 else if (cp_parser_allow_gnu_extensions_p (parser)
19584 && !c_dialect_objc ()
19585 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19587 /* In C++11, [ could start a lambda-introducer. */
19588 bool non_const = false;
19590 cp_parser_parse_tentatively (parser);
19592 if (!cp_parser_array_designator_p (parser))
19594 cp_parser_simulate_error (parser);
19595 designator = NULL_TREE;
19597 else
19599 designator = cp_parser_constant_expression (parser, true,
19600 &non_const);
19601 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19602 cp_parser_require (parser, CPP_EQ, RT_EQ);
19605 if (!cp_parser_parse_definitely (parser))
19606 designator = NULL_TREE;
19607 else if (non_const)
19608 require_potential_rvalue_constant_expression (designator);
19610 else
19611 designator = NULL_TREE;
19613 /* Parse the initializer. */
19614 initializer = cp_parser_initializer_clause (parser,
19615 &clause_non_constant_p);
19616 /* If any clause is non-constant, so is the entire initializer. */
19617 if (clause_non_constant_p)
19618 *non_constant_p = true;
19620 /* If we have an ellipsis, this is an initializer pack
19621 expansion. */
19622 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19624 /* Consume the `...'. */
19625 cp_lexer_consume_token (parser->lexer);
19627 /* Turn the initializer into an initializer expansion. */
19628 initializer = make_pack_expansion (initializer);
19631 /* Add it to the vector. */
19632 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19634 /* If the next token is not a comma, we have reached the end of
19635 the list. */
19636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19637 break;
19639 /* Peek at the next token. */
19640 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19641 /* If the next token is a `}', then we're still done. An
19642 initializer-clause can have a trailing `,' after the
19643 initializer-list and before the closing `}'. */
19644 if (token->type == CPP_CLOSE_BRACE)
19645 break;
19647 /* Consume the `,' token. */
19648 cp_lexer_consume_token (parser->lexer);
19651 return v;
19654 /* Classes [gram.class] */
19656 /* Parse a class-name.
19658 class-name:
19659 identifier
19660 template-id
19662 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19663 to indicate that names looked up in dependent types should be
19664 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19665 keyword has been used to indicate that the name that appears next
19666 is a template. TAG_TYPE indicates the explicit tag given before
19667 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19668 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19669 is the class being defined in a class-head. If ENUM_OK is TRUE,
19670 enum-names are also accepted.
19672 Returns the TYPE_DECL representing the class. */
19674 static tree
19675 cp_parser_class_name (cp_parser *parser,
19676 bool typename_keyword_p,
19677 bool template_keyword_p,
19678 enum tag_types tag_type,
19679 bool check_dependency_p,
19680 bool class_head_p,
19681 bool is_declaration,
19682 bool enum_ok)
19684 tree decl;
19685 tree scope;
19686 bool typename_p;
19687 cp_token *token;
19688 tree identifier = NULL_TREE;
19690 /* All class-names start with an identifier. */
19691 token = cp_lexer_peek_token (parser->lexer);
19692 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19694 cp_parser_error (parser, "expected class-name");
19695 return error_mark_node;
19698 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19699 to a template-id, so we save it here. */
19700 scope = parser->scope;
19701 if (scope == error_mark_node)
19702 return error_mark_node;
19704 /* Any name names a type if we're following the `typename' keyword
19705 in a qualified name where the enclosing scope is type-dependent. */
19706 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19707 && dependent_type_p (scope));
19708 /* Handle the common case (an identifier, but not a template-id)
19709 efficiently. */
19710 if (token->type == CPP_NAME
19711 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19713 cp_token *identifier_token;
19714 bool ambiguous_p;
19716 /* Look for the identifier. */
19717 identifier_token = cp_lexer_peek_token (parser->lexer);
19718 ambiguous_p = identifier_token->error_reported;
19719 identifier = cp_parser_identifier (parser);
19720 /* If the next token isn't an identifier, we are certainly not
19721 looking at a class-name. */
19722 if (identifier == error_mark_node)
19723 decl = error_mark_node;
19724 /* If we know this is a type-name, there's no need to look it
19725 up. */
19726 else if (typename_p)
19727 decl = identifier;
19728 else
19730 tree ambiguous_decls;
19731 /* If we already know that this lookup is ambiguous, then
19732 we've already issued an error message; there's no reason
19733 to check again. */
19734 if (ambiguous_p)
19736 cp_parser_simulate_error (parser);
19737 return error_mark_node;
19739 /* If the next token is a `::', then the name must be a type
19740 name.
19742 [basic.lookup.qual]
19744 During the lookup for a name preceding the :: scope
19745 resolution operator, object, function, and enumerator
19746 names are ignored. */
19747 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19748 tag_type = typename_type;
19749 /* Look up the name. */
19750 decl = cp_parser_lookup_name (parser, identifier,
19751 tag_type,
19752 /*is_template=*/false,
19753 /*is_namespace=*/false,
19754 check_dependency_p,
19755 &ambiguous_decls,
19756 identifier_token->location);
19757 if (ambiguous_decls)
19759 if (cp_parser_parsing_tentatively (parser))
19760 cp_parser_simulate_error (parser);
19761 return error_mark_node;
19765 else
19767 /* Try a template-id. */
19768 decl = cp_parser_template_id (parser, template_keyword_p,
19769 check_dependency_p,
19770 tag_type,
19771 is_declaration);
19772 if (decl == error_mark_node)
19773 return error_mark_node;
19776 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19778 /* If this is a typename, create a TYPENAME_TYPE. */
19779 if (typename_p && decl != error_mark_node)
19781 decl = make_typename_type (scope, decl, typename_type,
19782 /*complain=*/tf_error);
19783 if (decl != error_mark_node)
19784 decl = TYPE_NAME (decl);
19787 decl = strip_using_decl (decl);
19789 /* Check to see that it is really the name of a class. */
19790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19791 && identifier_p (TREE_OPERAND (decl, 0))
19792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19793 /* Situations like this:
19795 template <typename T> struct A {
19796 typename T::template X<int>::I i;
19799 are problematic. Is `T::template X<int>' a class-name? The
19800 standard does not seem to be definitive, but there is no other
19801 valid interpretation of the following `::'. Therefore, those
19802 names are considered class-names. */
19804 decl = make_typename_type (scope, decl, tag_type, tf_error);
19805 if (decl != error_mark_node)
19806 decl = TYPE_NAME (decl);
19808 else if (TREE_CODE (decl) != TYPE_DECL
19809 || TREE_TYPE (decl) == error_mark_node
19810 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19811 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19812 /* In Objective-C 2.0, a classname followed by '.' starts a
19813 dot-syntax expression, and it's not a type-name. */
19814 || (c_dialect_objc ()
19815 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19816 && objc_is_class_name (decl)))
19817 decl = error_mark_node;
19819 if (decl == error_mark_node)
19820 cp_parser_error (parser, "expected class-name");
19821 else if (identifier && !parser->scope)
19822 maybe_note_name_used_in_class (identifier, decl);
19824 return decl;
19827 /* Parse a class-specifier.
19829 class-specifier:
19830 class-head { member-specification [opt] }
19832 Returns the TREE_TYPE representing the class. */
19834 static tree
19835 cp_parser_class_specifier_1 (cp_parser* parser)
19837 tree type;
19838 tree attributes = NULL_TREE;
19839 bool nested_name_specifier_p;
19840 unsigned saved_num_template_parameter_lists;
19841 bool saved_in_function_body;
19842 unsigned char in_statement;
19843 bool in_switch_statement_p;
19844 bool saved_in_unbraced_linkage_specification_p;
19845 tree old_scope = NULL_TREE;
19846 tree scope = NULL_TREE;
19847 cp_token *closing_brace;
19849 push_deferring_access_checks (dk_no_deferred);
19851 /* Parse the class-head. */
19852 type = cp_parser_class_head (parser,
19853 &nested_name_specifier_p);
19854 /* If the class-head was a semantic disaster, skip the entire body
19855 of the class. */
19856 if (!type)
19858 cp_parser_skip_to_end_of_block_or_statement (parser);
19859 pop_deferring_access_checks ();
19860 return error_mark_node;
19863 /* Look for the `{'. */
19864 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19866 pop_deferring_access_checks ();
19867 return error_mark_node;
19870 cp_ensure_no_omp_declare_simd (parser);
19872 /* Issue an error message if type-definitions are forbidden here. */
19873 cp_parser_check_type_definition (parser);
19874 /* Remember that we are defining one more class. */
19875 ++parser->num_classes_being_defined;
19876 /* Inside the class, surrounding template-parameter-lists do not
19877 apply. */
19878 saved_num_template_parameter_lists
19879 = parser->num_template_parameter_lists;
19880 parser->num_template_parameter_lists = 0;
19881 /* We are not in a function body. */
19882 saved_in_function_body = parser->in_function_body;
19883 parser->in_function_body = false;
19884 /* Or in a loop. */
19885 in_statement = parser->in_statement;
19886 parser->in_statement = 0;
19887 /* Or in a switch. */
19888 in_switch_statement_p = parser->in_switch_statement_p;
19889 parser->in_switch_statement_p = false;
19890 /* We are not immediately inside an extern "lang" block. */
19891 saved_in_unbraced_linkage_specification_p
19892 = parser->in_unbraced_linkage_specification_p;
19893 parser->in_unbraced_linkage_specification_p = false;
19895 /* Start the class. */
19896 if (nested_name_specifier_p)
19898 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19899 old_scope = push_inner_scope (scope);
19901 type = begin_class_definition (type);
19903 if (type == error_mark_node)
19904 /* If the type is erroneous, skip the entire body of the class. */
19905 cp_parser_skip_to_closing_brace (parser);
19906 else
19907 /* Parse the member-specification. */
19908 cp_parser_member_specification_opt (parser);
19910 /* Look for the trailing `}'. */
19911 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19912 /* Look for trailing attributes to apply to this class. */
19913 if (cp_parser_allow_gnu_extensions_p (parser))
19914 attributes = cp_parser_gnu_attributes_opt (parser);
19915 if (type != error_mark_node)
19916 type = finish_struct (type, attributes);
19917 if (nested_name_specifier_p)
19918 pop_inner_scope (old_scope, scope);
19920 /* We've finished a type definition. Check for the common syntax
19921 error of forgetting a semicolon after the definition. We need to
19922 be careful, as we can't just check for not-a-semicolon and be done
19923 with it; the user might have typed:
19925 class X { } c = ...;
19926 class X { } *p = ...;
19928 and so forth. Instead, enumerate all the possible tokens that
19929 might follow this production; if we don't see one of them, then
19930 complain and silently insert the semicolon. */
19932 cp_token *token = cp_lexer_peek_token (parser->lexer);
19933 bool want_semicolon = true;
19935 if (cp_next_tokens_can_be_std_attribute_p (parser))
19936 /* Don't try to parse c++11 attributes here. As per the
19937 grammar, that should be a task for
19938 cp_parser_decl_specifier_seq. */
19939 want_semicolon = false;
19941 switch (token->type)
19943 case CPP_NAME:
19944 case CPP_SEMICOLON:
19945 case CPP_MULT:
19946 case CPP_AND:
19947 case CPP_OPEN_PAREN:
19948 case CPP_CLOSE_PAREN:
19949 case CPP_COMMA:
19950 want_semicolon = false;
19951 break;
19953 /* While it's legal for type qualifiers and storage class
19954 specifiers to follow type definitions in the grammar, only
19955 compiler testsuites contain code like that. Assume that if
19956 we see such code, then what we're really seeing is a case
19957 like:
19959 class X { }
19960 const <type> var = ...;
19964 class Y { }
19965 static <type> func (...) ...
19967 i.e. the qualifier or specifier applies to the next
19968 declaration. To do so, however, we need to look ahead one
19969 more token to see if *that* token is a type specifier.
19971 This code could be improved to handle:
19973 class Z { }
19974 static const <type> var = ...; */
19975 case CPP_KEYWORD:
19976 if (keyword_is_decl_specifier (token->keyword))
19978 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19980 /* Handling user-defined types here would be nice, but very
19981 tricky. */
19982 want_semicolon
19983 = (lookahead->type == CPP_KEYWORD
19984 && keyword_begins_type_specifier (lookahead->keyword));
19986 break;
19987 default:
19988 break;
19991 /* If we don't have a type, then something is very wrong and we
19992 shouldn't try to do anything clever. Likewise for not seeing the
19993 closing brace. */
19994 if (closing_brace && TYPE_P (type) && want_semicolon)
19996 cp_token_position prev
19997 = cp_lexer_previous_token_position (parser->lexer);
19998 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19999 location_t loc = prev_token->location;
20001 if (CLASSTYPE_DECLARED_CLASS (type))
20002 error_at (loc, "expected %<;%> after class definition");
20003 else if (TREE_CODE (type) == RECORD_TYPE)
20004 error_at (loc, "expected %<;%> after struct definition");
20005 else if (TREE_CODE (type) == UNION_TYPE)
20006 error_at (loc, "expected %<;%> after union definition");
20007 else
20008 gcc_unreachable ();
20010 /* Unget one token and smash it to look as though we encountered
20011 a semicolon in the input stream. */
20012 cp_lexer_set_token_position (parser->lexer, prev);
20013 token = cp_lexer_peek_token (parser->lexer);
20014 token->type = CPP_SEMICOLON;
20015 token->keyword = RID_MAX;
20019 /* If this class is not itself within the scope of another class,
20020 then we need to parse the bodies of all of the queued function
20021 definitions. Note that the queued functions defined in a class
20022 are not always processed immediately following the
20023 class-specifier for that class. Consider:
20025 struct A {
20026 struct B { void f() { sizeof (A); } };
20029 If `f' were processed before the processing of `A' were
20030 completed, there would be no way to compute the size of `A'.
20031 Note that the nesting we are interested in here is lexical --
20032 not the semantic nesting given by TYPE_CONTEXT. In particular,
20033 for:
20035 struct A { struct B; };
20036 struct A::B { void f() { } };
20038 there is no need to delay the parsing of `A::B::f'. */
20039 if (--parser->num_classes_being_defined == 0)
20041 tree decl;
20042 tree class_type = NULL_TREE;
20043 tree pushed_scope = NULL_TREE;
20044 unsigned ix;
20045 cp_default_arg_entry *e;
20046 tree save_ccp, save_ccr;
20048 /* In a first pass, parse default arguments to the functions.
20049 Then, in a second pass, parse the bodies of the functions.
20050 This two-phased approach handles cases like:
20052 struct S {
20053 void f() { g(); }
20054 void g(int i = 3);
20058 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20060 decl = e->decl;
20061 /* If there are default arguments that have not yet been processed,
20062 take care of them now. */
20063 if (class_type != e->class_type)
20065 if (pushed_scope)
20066 pop_scope (pushed_scope);
20067 class_type = e->class_type;
20068 pushed_scope = push_scope (class_type);
20070 /* Make sure that any template parameters are in scope. */
20071 maybe_begin_member_template_processing (decl);
20072 /* Parse the default argument expressions. */
20073 cp_parser_late_parsing_default_args (parser, decl);
20074 /* Remove any template parameters from the symbol table. */
20075 maybe_end_member_template_processing ();
20077 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20078 /* Now parse any NSDMIs. */
20079 save_ccp = current_class_ptr;
20080 save_ccr = current_class_ref;
20081 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20083 if (class_type != DECL_CONTEXT (decl))
20085 if (pushed_scope)
20086 pop_scope (pushed_scope);
20087 class_type = DECL_CONTEXT (decl);
20088 pushed_scope = push_scope (class_type);
20090 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20091 cp_parser_late_parsing_nsdmi (parser, decl);
20093 vec_safe_truncate (unparsed_nsdmis, 0);
20094 current_class_ptr = save_ccp;
20095 current_class_ref = save_ccr;
20096 if (pushed_scope)
20097 pop_scope (pushed_scope);
20099 /* Now do some post-NSDMI bookkeeping. */
20100 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20101 after_nsdmi_defaulted_late_checks (class_type);
20102 vec_safe_truncate (unparsed_classes, 0);
20103 after_nsdmi_defaulted_late_checks (type);
20105 /* Now parse the body of the functions. */
20106 if (flag_openmp)
20108 /* OpenMP UDRs need to be parsed before all other functions. */
20109 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20110 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20111 cp_parser_late_parsing_for_member (parser, decl);
20112 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20113 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20114 cp_parser_late_parsing_for_member (parser, decl);
20116 else
20117 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20118 cp_parser_late_parsing_for_member (parser, decl);
20119 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20121 else
20122 vec_safe_push (unparsed_classes, type);
20124 /* Put back any saved access checks. */
20125 pop_deferring_access_checks ();
20127 /* Restore saved state. */
20128 parser->in_switch_statement_p = in_switch_statement_p;
20129 parser->in_statement = in_statement;
20130 parser->in_function_body = saved_in_function_body;
20131 parser->num_template_parameter_lists
20132 = saved_num_template_parameter_lists;
20133 parser->in_unbraced_linkage_specification_p
20134 = saved_in_unbraced_linkage_specification_p;
20136 return type;
20139 static tree
20140 cp_parser_class_specifier (cp_parser* parser)
20142 tree ret;
20143 timevar_push (TV_PARSE_STRUCT);
20144 ret = cp_parser_class_specifier_1 (parser);
20145 timevar_pop (TV_PARSE_STRUCT);
20146 return ret;
20149 /* Parse a class-head.
20151 class-head:
20152 class-key identifier [opt] base-clause [opt]
20153 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20154 class-key nested-name-specifier [opt] template-id
20155 base-clause [opt]
20157 class-virt-specifier:
20158 final
20160 GNU Extensions:
20161 class-key attributes identifier [opt] base-clause [opt]
20162 class-key attributes nested-name-specifier identifier base-clause [opt]
20163 class-key attributes nested-name-specifier [opt] template-id
20164 base-clause [opt]
20166 Upon return BASES is initialized to the list of base classes (or
20167 NULL, if there are none) in the same form returned by
20168 cp_parser_base_clause.
20170 Returns the TYPE of the indicated class. Sets
20171 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20172 involving a nested-name-specifier was used, and FALSE otherwise.
20174 Returns error_mark_node if this is not a class-head.
20176 Returns NULL_TREE if the class-head is syntactically valid, but
20177 semantically invalid in a way that means we should skip the entire
20178 body of the class. */
20180 static tree
20181 cp_parser_class_head (cp_parser* parser,
20182 bool* nested_name_specifier_p)
20184 tree nested_name_specifier;
20185 enum tag_types class_key;
20186 tree id = NULL_TREE;
20187 tree type = NULL_TREE;
20188 tree attributes;
20189 tree bases;
20190 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20191 bool template_id_p = false;
20192 bool qualified_p = false;
20193 bool invalid_nested_name_p = false;
20194 bool invalid_explicit_specialization_p = false;
20195 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20196 tree pushed_scope = NULL_TREE;
20197 unsigned num_templates;
20198 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20199 /* Assume no nested-name-specifier will be present. */
20200 *nested_name_specifier_p = false;
20201 /* Assume no template parameter lists will be used in defining the
20202 type. */
20203 num_templates = 0;
20204 parser->colon_corrects_to_scope_p = false;
20206 /* Look for the class-key. */
20207 class_key = cp_parser_class_key (parser);
20208 if (class_key == none_type)
20209 return error_mark_node;
20211 /* Parse the attributes. */
20212 attributes = cp_parser_attributes_opt (parser);
20214 /* If the next token is `::', that is invalid -- but sometimes
20215 people do try to write:
20217 struct ::S {};
20219 Handle this gracefully by accepting the extra qualifier, and then
20220 issuing an error about it later if this really is a
20221 class-head. If it turns out just to be an elaborated type
20222 specifier, remain silent. */
20223 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20224 qualified_p = true;
20226 push_deferring_access_checks (dk_no_check);
20228 /* Determine the name of the class. Begin by looking for an
20229 optional nested-name-specifier. */
20230 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20231 nested_name_specifier
20232 = cp_parser_nested_name_specifier_opt (parser,
20233 /*typename_keyword_p=*/false,
20234 /*check_dependency_p=*/false,
20235 /*type_p=*/true,
20236 /*is_declaration=*/false);
20237 /* If there was a nested-name-specifier, then there *must* be an
20238 identifier. */
20239 if (nested_name_specifier)
20241 type_start_token = cp_lexer_peek_token (parser->lexer);
20242 /* Although the grammar says `identifier', it really means
20243 `class-name' or `template-name'. You are only allowed to
20244 define a class that has already been declared with this
20245 syntax.
20247 The proposed resolution for Core Issue 180 says that wherever
20248 you see `class T::X' you should treat `X' as a type-name.
20250 It is OK to define an inaccessible class; for example:
20252 class A { class B; };
20253 class A::B {};
20255 We do not know if we will see a class-name, or a
20256 template-name. We look for a class-name first, in case the
20257 class-name is a template-id; if we looked for the
20258 template-name first we would stop after the template-name. */
20259 cp_parser_parse_tentatively (parser);
20260 type = cp_parser_class_name (parser,
20261 /*typename_keyword_p=*/false,
20262 /*template_keyword_p=*/false,
20263 class_type,
20264 /*check_dependency_p=*/false,
20265 /*class_head_p=*/true,
20266 /*is_declaration=*/false);
20267 /* If that didn't work, ignore the nested-name-specifier. */
20268 if (!cp_parser_parse_definitely (parser))
20270 invalid_nested_name_p = true;
20271 type_start_token = cp_lexer_peek_token (parser->lexer);
20272 id = cp_parser_identifier (parser);
20273 if (id == error_mark_node)
20274 id = NULL_TREE;
20276 /* If we could not find a corresponding TYPE, treat this
20277 declaration like an unqualified declaration. */
20278 if (type == error_mark_node)
20279 nested_name_specifier = NULL_TREE;
20280 /* Otherwise, count the number of templates used in TYPE and its
20281 containing scopes. */
20282 else
20284 tree scope;
20286 for (scope = TREE_TYPE (type);
20287 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20288 scope = get_containing_scope (scope))
20289 if (TYPE_P (scope)
20290 && CLASS_TYPE_P (scope)
20291 && CLASSTYPE_TEMPLATE_INFO (scope)
20292 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20293 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20294 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20295 ++num_templates;
20298 /* Otherwise, the identifier is optional. */
20299 else
20301 /* We don't know whether what comes next is a template-id,
20302 an identifier, or nothing at all. */
20303 cp_parser_parse_tentatively (parser);
20304 /* Check for a template-id. */
20305 type_start_token = cp_lexer_peek_token (parser->lexer);
20306 id = cp_parser_template_id (parser,
20307 /*template_keyword_p=*/false,
20308 /*check_dependency_p=*/true,
20309 class_key,
20310 /*is_declaration=*/true);
20311 /* If that didn't work, it could still be an identifier. */
20312 if (!cp_parser_parse_definitely (parser))
20314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20316 type_start_token = cp_lexer_peek_token (parser->lexer);
20317 id = cp_parser_identifier (parser);
20319 else
20320 id = NULL_TREE;
20322 else
20324 template_id_p = true;
20325 ++num_templates;
20329 pop_deferring_access_checks ();
20331 if (id)
20333 cp_parser_check_for_invalid_template_id (parser, id,
20334 class_key,
20335 type_start_token->location);
20337 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20339 /* If it's not a `:' or a `{' then we can't really be looking at a
20340 class-head, since a class-head only appears as part of a
20341 class-specifier. We have to detect this situation before calling
20342 xref_tag, since that has irreversible side-effects. */
20343 if (!cp_parser_next_token_starts_class_definition_p (parser))
20345 cp_parser_error (parser, "expected %<{%> or %<:%>");
20346 type = error_mark_node;
20347 goto out;
20350 /* At this point, we're going ahead with the class-specifier, even
20351 if some other problem occurs. */
20352 cp_parser_commit_to_tentative_parse (parser);
20353 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20355 cp_parser_error (parser,
20356 "cannot specify %<override%> for a class");
20357 type = error_mark_node;
20358 goto out;
20360 /* Issue the error about the overly-qualified name now. */
20361 if (qualified_p)
20363 cp_parser_error (parser,
20364 "global qualification of class name is invalid");
20365 type = error_mark_node;
20366 goto out;
20368 else if (invalid_nested_name_p)
20370 cp_parser_error (parser,
20371 "qualified name does not name a class");
20372 type = error_mark_node;
20373 goto out;
20375 else if (nested_name_specifier)
20377 tree scope;
20379 /* Reject typedef-names in class heads. */
20380 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20382 error_at (type_start_token->location,
20383 "invalid class name in declaration of %qD",
20384 type);
20385 type = NULL_TREE;
20386 goto done;
20389 /* Figure out in what scope the declaration is being placed. */
20390 scope = current_scope ();
20391 /* If that scope does not contain the scope in which the
20392 class was originally declared, the program is invalid. */
20393 if (scope && !is_ancestor (scope, nested_name_specifier))
20395 if (at_namespace_scope_p ())
20396 error_at (type_start_token->location,
20397 "declaration of %qD in namespace %qD which does not "
20398 "enclose %qD",
20399 type, scope, nested_name_specifier);
20400 else
20401 error_at (type_start_token->location,
20402 "declaration of %qD in %qD which does not enclose %qD",
20403 type, scope, nested_name_specifier);
20404 type = NULL_TREE;
20405 goto done;
20407 /* [dcl.meaning]
20409 A declarator-id shall not be qualified except for the
20410 definition of a ... nested class outside of its class
20411 ... [or] the definition or explicit instantiation of a
20412 class member of a namespace outside of its namespace. */
20413 if (scope == nested_name_specifier)
20415 permerror (nested_name_specifier_token_start->location,
20416 "extra qualification not allowed");
20417 nested_name_specifier = NULL_TREE;
20418 num_templates = 0;
20421 /* An explicit-specialization must be preceded by "template <>". If
20422 it is not, try to recover gracefully. */
20423 if (at_namespace_scope_p ()
20424 && parser->num_template_parameter_lists == 0
20425 && template_id_p)
20427 error_at (type_start_token->location,
20428 "an explicit specialization must be preceded by %<template <>%>");
20429 invalid_explicit_specialization_p = true;
20430 /* Take the same action that would have been taken by
20431 cp_parser_explicit_specialization. */
20432 ++parser->num_template_parameter_lists;
20433 begin_specialization ();
20435 /* There must be no "return" statements between this point and the
20436 end of this function; set "type "to the correct return value and
20437 use "goto done;" to return. */
20438 /* Make sure that the right number of template parameters were
20439 present. */
20440 if (!cp_parser_check_template_parameters (parser, num_templates,
20441 type_start_token->location,
20442 /*declarator=*/NULL))
20444 /* If something went wrong, there is no point in even trying to
20445 process the class-definition. */
20446 type = NULL_TREE;
20447 goto done;
20450 /* Look up the type. */
20451 if (template_id_p)
20453 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20454 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20455 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20457 error_at (type_start_token->location,
20458 "function template %qD redeclared as a class template", id);
20459 type = error_mark_node;
20461 else
20463 type = TREE_TYPE (id);
20464 type = maybe_process_partial_specialization (type);
20466 if (nested_name_specifier)
20467 pushed_scope = push_scope (nested_name_specifier);
20469 else if (nested_name_specifier)
20471 tree class_type;
20473 /* Given:
20475 template <typename T> struct S { struct T };
20476 template <typename T> struct S<T>::T { };
20478 we will get a TYPENAME_TYPE when processing the definition of
20479 `S::T'. We need to resolve it to the actual type before we
20480 try to define it. */
20481 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20483 class_type = resolve_typename_type (TREE_TYPE (type),
20484 /*only_current_p=*/false);
20485 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20486 type = TYPE_NAME (class_type);
20487 else
20489 cp_parser_error (parser, "could not resolve typename type");
20490 type = error_mark_node;
20494 if (maybe_process_partial_specialization (TREE_TYPE (type))
20495 == error_mark_node)
20497 type = NULL_TREE;
20498 goto done;
20501 class_type = current_class_type;
20502 /* Enter the scope indicated by the nested-name-specifier. */
20503 pushed_scope = push_scope (nested_name_specifier);
20504 /* Get the canonical version of this type. */
20505 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20506 /* Call push_template_decl if it seems like we should be defining a
20507 template either from the template headers or the type we're
20508 defining, so that we diagnose both extra and missing headers. */
20509 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20510 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20511 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20513 type = push_template_decl (type);
20514 if (type == error_mark_node)
20516 type = NULL_TREE;
20517 goto done;
20521 type = TREE_TYPE (type);
20522 *nested_name_specifier_p = true;
20524 else /* The name is not a nested name. */
20526 /* If the class was unnamed, create a dummy name. */
20527 if (!id)
20528 id = make_anon_name ();
20529 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20530 parser->num_template_parameter_lists);
20533 /* Indicate whether this class was declared as a `class' or as a
20534 `struct'. */
20535 if (TREE_CODE (type) == RECORD_TYPE)
20536 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20537 cp_parser_check_class_key (class_key, type);
20539 /* If this type was already complete, and we see another definition,
20540 that's an error. */
20541 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20543 error_at (type_start_token->location, "redefinition of %q#T",
20544 type);
20545 error_at (type_start_token->location, "previous definition of %q+#T",
20546 type);
20547 type = NULL_TREE;
20548 goto done;
20550 else if (type == error_mark_node)
20551 type = NULL_TREE;
20553 if (type)
20555 /* Apply attributes now, before any use of the class as a template
20556 argument in its base list. */
20557 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20558 fixup_attribute_variants (type);
20561 /* We will have entered the scope containing the class; the names of
20562 base classes should be looked up in that context. For example:
20564 struct A { struct B {}; struct C; };
20565 struct A::C : B {};
20567 is valid. */
20569 /* Get the list of base-classes, if there is one. */
20570 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20572 /* PR59482: enter the class scope so that base-specifiers are looked
20573 up correctly. */
20574 if (type)
20575 pushclass (type);
20576 bases = cp_parser_base_clause (parser);
20577 /* PR59482: get out of the previously pushed class scope so that the
20578 subsequent pops pop the right thing. */
20579 if (type)
20580 popclass ();
20582 else
20583 bases = NULL_TREE;
20585 /* If we're really defining a class, process the base classes.
20586 If they're invalid, fail. */
20587 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20588 && !xref_basetypes (type, bases))
20589 type = NULL_TREE;
20591 done:
20592 /* Leave the scope given by the nested-name-specifier. We will
20593 enter the class scope itself while processing the members. */
20594 if (pushed_scope)
20595 pop_scope (pushed_scope);
20597 if (invalid_explicit_specialization_p)
20599 end_specialization ();
20600 --parser->num_template_parameter_lists;
20603 if (type)
20604 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20605 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20606 CLASSTYPE_FINAL (type) = 1;
20607 out:
20608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20609 return type;
20612 /* Parse a class-key.
20614 class-key:
20615 class
20616 struct
20617 union
20619 Returns the kind of class-key specified, or none_type to indicate
20620 error. */
20622 static enum tag_types
20623 cp_parser_class_key (cp_parser* parser)
20625 cp_token *token;
20626 enum tag_types tag_type;
20628 /* Look for the class-key. */
20629 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20630 if (!token)
20631 return none_type;
20633 /* Check to see if the TOKEN is a class-key. */
20634 tag_type = cp_parser_token_is_class_key (token);
20635 if (!tag_type)
20636 cp_parser_error (parser, "expected class-key");
20637 return tag_type;
20640 /* Parse a type-parameter-key.
20642 type-parameter-key:
20643 class
20644 typename
20647 static void
20648 cp_parser_type_parameter_key (cp_parser* parser)
20650 /* Look for the type-parameter-key. */
20651 enum tag_types tag_type = none_type;
20652 cp_token *token = cp_lexer_peek_token (parser->lexer);
20653 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20655 cp_lexer_consume_token (parser->lexer);
20656 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20657 /* typename is not allowed in a template template parameter
20658 by the standard until C++1Z. */
20659 pedwarn (token->location, OPT_Wpedantic,
20660 "ISO C++ forbids typename key in template template parameter;"
20661 " use -std=c++1z or -std=gnu++1z");
20663 else
20664 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20666 return;
20669 /* Parse an (optional) member-specification.
20671 member-specification:
20672 member-declaration member-specification [opt]
20673 access-specifier : member-specification [opt] */
20675 static void
20676 cp_parser_member_specification_opt (cp_parser* parser)
20678 while (true)
20680 cp_token *token;
20681 enum rid keyword;
20683 /* Peek at the next token. */
20684 token = cp_lexer_peek_token (parser->lexer);
20685 /* If it's a `}', or EOF then we've seen all the members. */
20686 if (token->type == CPP_CLOSE_BRACE
20687 || token->type == CPP_EOF
20688 || token->type == CPP_PRAGMA_EOL)
20689 break;
20691 /* See if this token is a keyword. */
20692 keyword = token->keyword;
20693 switch (keyword)
20695 case RID_PUBLIC:
20696 case RID_PROTECTED:
20697 case RID_PRIVATE:
20698 /* Consume the access-specifier. */
20699 cp_lexer_consume_token (parser->lexer);
20700 /* Remember which access-specifier is active. */
20701 current_access_specifier = token->u.value;
20702 /* Look for the `:'. */
20703 cp_parser_require (parser, CPP_COLON, RT_COLON);
20704 break;
20706 default:
20707 /* Accept #pragmas at class scope. */
20708 if (token->type == CPP_PRAGMA)
20710 cp_parser_pragma (parser, pragma_member);
20711 break;
20714 /* Otherwise, the next construction must be a
20715 member-declaration. */
20716 cp_parser_member_declaration (parser);
20721 /* Parse a member-declaration.
20723 member-declaration:
20724 decl-specifier-seq [opt] member-declarator-list [opt] ;
20725 function-definition ; [opt]
20726 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20727 using-declaration
20728 template-declaration
20729 alias-declaration
20731 member-declarator-list:
20732 member-declarator
20733 member-declarator-list , member-declarator
20735 member-declarator:
20736 declarator pure-specifier [opt]
20737 declarator constant-initializer [opt]
20738 identifier [opt] : constant-expression
20740 GNU Extensions:
20742 member-declaration:
20743 __extension__ member-declaration
20745 member-declarator:
20746 declarator attributes [opt] pure-specifier [opt]
20747 declarator attributes [opt] constant-initializer [opt]
20748 identifier [opt] attributes [opt] : constant-expression
20750 C++0x Extensions:
20752 member-declaration:
20753 static_assert-declaration */
20755 static void
20756 cp_parser_member_declaration (cp_parser* parser)
20758 cp_decl_specifier_seq decl_specifiers;
20759 tree prefix_attributes;
20760 tree decl;
20761 int declares_class_or_enum;
20762 bool friend_p;
20763 cp_token *token = NULL;
20764 cp_token *decl_spec_token_start = NULL;
20765 cp_token *initializer_token_start = NULL;
20766 int saved_pedantic;
20767 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20769 /* Check for the `__extension__' keyword. */
20770 if (cp_parser_extension_opt (parser, &saved_pedantic))
20772 /* Recurse. */
20773 cp_parser_member_declaration (parser);
20774 /* Restore the old value of the PEDANTIC flag. */
20775 pedantic = saved_pedantic;
20777 return;
20780 /* Check for a template-declaration. */
20781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20783 /* An explicit specialization here is an error condition, and we
20784 expect the specialization handler to detect and report this. */
20785 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20786 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20787 cp_parser_explicit_specialization (parser);
20788 else
20789 cp_parser_template_declaration (parser, /*member_p=*/true);
20791 return;
20794 /* Check for a using-declaration. */
20795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20797 if (cxx_dialect < cxx11)
20799 /* Parse the using-declaration. */
20800 cp_parser_using_declaration (parser,
20801 /*access_declaration_p=*/false);
20802 return;
20804 else
20806 tree decl;
20807 bool alias_decl_expected;
20808 cp_parser_parse_tentatively (parser);
20809 decl = cp_parser_alias_declaration (parser);
20810 /* Note that if we actually see the '=' token after the
20811 identifier, cp_parser_alias_declaration commits the
20812 tentative parse. In that case, we really expects an
20813 alias-declaration. Otherwise, we expect a using
20814 declaration. */
20815 alias_decl_expected =
20816 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20817 cp_parser_parse_definitely (parser);
20819 if (alias_decl_expected)
20820 finish_member_declaration (decl);
20821 else
20822 cp_parser_using_declaration (parser,
20823 /*access_declaration_p=*/false);
20824 return;
20828 /* Check for @defs. */
20829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20831 tree ivar, member;
20832 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20833 ivar = ivar_chains;
20834 while (ivar)
20836 member = ivar;
20837 ivar = TREE_CHAIN (member);
20838 TREE_CHAIN (member) = NULL_TREE;
20839 finish_member_declaration (member);
20841 return;
20844 /* If the next token is `static_assert' we have a static assertion. */
20845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20847 cp_parser_static_assert (parser, /*member_p=*/true);
20848 return;
20851 parser->colon_corrects_to_scope_p = false;
20853 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20854 goto out;
20856 /* Parse the decl-specifier-seq. */
20857 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20858 cp_parser_decl_specifier_seq (parser,
20859 CP_PARSER_FLAGS_OPTIONAL,
20860 &decl_specifiers,
20861 &declares_class_or_enum);
20862 /* Check for an invalid type-name. */
20863 if (!decl_specifiers.any_type_specifiers_p
20864 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20865 goto out;
20866 /* If there is no declarator, then the decl-specifier-seq should
20867 specify a type. */
20868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20870 /* If there was no decl-specifier-seq, and the next token is a
20871 `;', then we have something like:
20873 struct S { ; };
20875 [class.mem]
20877 Each member-declaration shall declare at least one member
20878 name of the class. */
20879 if (!decl_specifiers.any_specifiers_p)
20881 cp_token *token = cp_lexer_peek_token (parser->lexer);
20882 if (!in_system_header_at (token->location))
20883 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20885 else
20887 tree type;
20889 /* See if this declaration is a friend. */
20890 friend_p = cp_parser_friend_p (&decl_specifiers);
20891 /* If there were decl-specifiers, check to see if there was
20892 a class-declaration. */
20893 type = check_tag_decl (&decl_specifiers,
20894 /*explicit_type_instantiation_p=*/false);
20895 /* Nested classes have already been added to the class, but
20896 a `friend' needs to be explicitly registered. */
20897 if (friend_p)
20899 /* If the `friend' keyword was present, the friend must
20900 be introduced with a class-key. */
20901 if (!declares_class_or_enum && cxx_dialect < cxx11)
20902 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20903 "in C++03 a class-key must be used "
20904 "when declaring a friend");
20905 /* In this case:
20907 template <typename T> struct A {
20908 friend struct A<T>::B;
20911 A<T>::B will be represented by a TYPENAME_TYPE, and
20912 therefore not recognized by check_tag_decl. */
20913 if (!type)
20915 type = decl_specifiers.type;
20916 if (type && TREE_CODE (type) == TYPE_DECL)
20917 type = TREE_TYPE (type);
20919 if (!type || !TYPE_P (type))
20920 error_at (decl_spec_token_start->location,
20921 "friend declaration does not name a class or "
20922 "function");
20923 else
20924 make_friend_class (current_class_type, type,
20925 /*complain=*/true);
20927 /* If there is no TYPE, an error message will already have
20928 been issued. */
20929 else if (!type || type == error_mark_node)
20931 /* An anonymous aggregate has to be handled specially; such
20932 a declaration really declares a data member (with a
20933 particular type), as opposed to a nested class. */
20934 else if (ANON_AGGR_TYPE_P (type))
20936 /* C++11 9.5/6. */
20937 if (decl_specifiers.storage_class != sc_none)
20938 error_at (decl_spec_token_start->location,
20939 "a storage class on an anonymous aggregate "
20940 "in class scope is not allowed");
20942 /* Remove constructors and such from TYPE, now that we
20943 know it is an anonymous aggregate. */
20944 fixup_anonymous_aggr (type);
20945 /* And make the corresponding data member. */
20946 decl = build_decl (decl_spec_token_start->location,
20947 FIELD_DECL, NULL_TREE, type);
20948 /* Add it to the class. */
20949 finish_member_declaration (decl);
20951 else
20952 cp_parser_check_access_in_redeclaration
20953 (TYPE_NAME (type),
20954 decl_spec_token_start->location);
20957 else
20959 bool assume_semicolon = false;
20961 /* Clear attributes from the decl_specifiers but keep them
20962 around as prefix attributes that apply them to the entity
20963 being declared. */
20964 prefix_attributes = decl_specifiers.attributes;
20965 decl_specifiers.attributes = NULL_TREE;
20967 /* See if these declarations will be friends. */
20968 friend_p = cp_parser_friend_p (&decl_specifiers);
20970 /* Keep going until we hit the `;' at the end of the
20971 declaration. */
20972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20974 tree attributes = NULL_TREE;
20975 tree first_attribute;
20977 /* Peek at the next token. */
20978 token = cp_lexer_peek_token (parser->lexer);
20980 /* Check for a bitfield declaration. */
20981 if (token->type == CPP_COLON
20982 || (token->type == CPP_NAME
20983 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20984 == CPP_COLON))
20986 tree identifier;
20987 tree width;
20989 /* Get the name of the bitfield. Note that we cannot just
20990 check TOKEN here because it may have been invalidated by
20991 the call to cp_lexer_peek_nth_token above. */
20992 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20993 identifier = cp_parser_identifier (parser);
20994 else
20995 identifier = NULL_TREE;
20997 /* Consume the `:' token. */
20998 cp_lexer_consume_token (parser->lexer);
20999 /* Get the width of the bitfield. */
21000 width
21001 = cp_parser_constant_expression (parser);
21003 /* Look for attributes that apply to the bitfield. */
21004 attributes = cp_parser_attributes_opt (parser);
21005 /* Remember which attributes are prefix attributes and
21006 which are not. */
21007 first_attribute = attributes;
21008 /* Combine the attributes. */
21009 attributes = chainon (prefix_attributes, attributes);
21011 /* Create the bitfield declaration. */
21012 decl = grokbitfield (identifier
21013 ? make_id_declarator (NULL_TREE,
21014 identifier,
21015 sfk_none)
21016 : NULL,
21017 &decl_specifiers,
21018 width,
21019 attributes);
21021 else
21023 cp_declarator *declarator;
21024 tree initializer;
21025 tree asm_specification;
21026 int ctor_dtor_or_conv_p;
21028 /* Parse the declarator. */
21029 declarator
21030 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21031 &ctor_dtor_or_conv_p,
21032 /*parenthesized_p=*/NULL,
21033 /*member_p=*/true,
21034 friend_p);
21036 /* If something went wrong parsing the declarator, make sure
21037 that we at least consume some tokens. */
21038 if (declarator == cp_error_declarator)
21040 /* Skip to the end of the statement. */
21041 cp_parser_skip_to_end_of_statement (parser);
21042 /* If the next token is not a semicolon, that is
21043 probably because we just skipped over the body of
21044 a function. So, we consume a semicolon if
21045 present, but do not issue an error message if it
21046 is not present. */
21047 if (cp_lexer_next_token_is (parser->lexer,
21048 CPP_SEMICOLON))
21049 cp_lexer_consume_token (parser->lexer);
21050 goto out;
21053 if (declares_class_or_enum & 2)
21054 cp_parser_check_for_definition_in_return_type
21055 (declarator, decl_specifiers.type,
21056 decl_specifiers.locations[ds_type_spec]);
21058 /* Look for an asm-specification. */
21059 asm_specification = cp_parser_asm_specification_opt (parser);
21060 /* Look for attributes that apply to the declaration. */
21061 attributes = cp_parser_attributes_opt (parser);
21062 /* Remember which attributes are prefix attributes and
21063 which are not. */
21064 first_attribute = attributes;
21065 /* Combine the attributes. */
21066 attributes = chainon (prefix_attributes, attributes);
21068 /* If it's an `=', then we have a constant-initializer or a
21069 pure-specifier. It is not correct to parse the
21070 initializer before registering the member declaration
21071 since the member declaration should be in scope while
21072 its initializer is processed. However, the rest of the
21073 front end does not yet provide an interface that allows
21074 us to handle this correctly. */
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21077 /* In [class.mem]:
21079 A pure-specifier shall be used only in the declaration of
21080 a virtual function.
21082 A member-declarator can contain a constant-initializer
21083 only if it declares a static member of integral or
21084 enumeration type.
21086 Therefore, if the DECLARATOR is for a function, we look
21087 for a pure-specifier; otherwise, we look for a
21088 constant-initializer. When we call `grokfield', it will
21089 perform more stringent semantics checks. */
21090 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21091 if (function_declarator_p (declarator)
21092 || (decl_specifiers.type
21093 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21094 && declarator->kind == cdk_id
21095 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21096 == FUNCTION_TYPE)))
21097 initializer = cp_parser_pure_specifier (parser);
21098 else if (decl_specifiers.storage_class != sc_static)
21099 initializer = cp_parser_save_nsdmi (parser);
21100 else if (cxx_dialect >= cxx11)
21102 bool nonconst;
21103 /* Don't require a constant rvalue in C++11, since we
21104 might want a reference constant. We'll enforce
21105 constancy later. */
21106 cp_lexer_consume_token (parser->lexer);
21107 /* Parse the initializer. */
21108 initializer = cp_parser_initializer_clause (parser,
21109 &nonconst);
21111 else
21112 /* Parse the initializer. */
21113 initializer = cp_parser_constant_initializer (parser);
21115 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21116 && !function_declarator_p (declarator))
21118 bool x;
21119 if (decl_specifiers.storage_class != sc_static)
21120 initializer = cp_parser_save_nsdmi (parser);
21121 else
21122 initializer = cp_parser_initializer (parser, &x, &x);
21124 /* Otherwise, there is no initializer. */
21125 else
21126 initializer = NULL_TREE;
21128 /* See if we are probably looking at a function
21129 definition. We are certainly not looking at a
21130 member-declarator. Calling `grokfield' has
21131 side-effects, so we must not do it unless we are sure
21132 that we are looking at a member-declarator. */
21133 if (cp_parser_token_starts_function_definition_p
21134 (cp_lexer_peek_token (parser->lexer)))
21136 /* The grammar does not allow a pure-specifier to be
21137 used when a member function is defined. (It is
21138 possible that this fact is an oversight in the
21139 standard, since a pure function may be defined
21140 outside of the class-specifier. */
21141 if (initializer && initializer_token_start)
21142 error_at (initializer_token_start->location,
21143 "pure-specifier on function-definition");
21144 decl = cp_parser_save_member_function_body (parser,
21145 &decl_specifiers,
21146 declarator,
21147 attributes);
21148 if (parser->fully_implicit_function_template_p)
21149 decl = finish_fully_implicit_template (parser, decl);
21150 /* If the member was not a friend, declare it here. */
21151 if (!friend_p)
21152 finish_member_declaration (decl);
21153 /* Peek at the next token. */
21154 token = cp_lexer_peek_token (parser->lexer);
21155 /* If the next token is a semicolon, consume it. */
21156 if (token->type == CPP_SEMICOLON)
21157 cp_lexer_consume_token (parser->lexer);
21158 goto out;
21160 else
21161 if (declarator->kind == cdk_function)
21162 declarator->id_loc = token->location;
21163 /* Create the declaration. */
21164 decl = grokfield (declarator, &decl_specifiers,
21165 initializer, /*init_const_expr_p=*/true,
21166 asm_specification, attributes);
21167 if (parser->fully_implicit_function_template_p)
21169 if (friend_p)
21170 finish_fully_implicit_template (parser, 0);
21171 else
21172 decl = finish_fully_implicit_template (parser, decl);
21176 cp_finalize_omp_declare_simd (parser, decl);
21178 /* Reset PREFIX_ATTRIBUTES. */
21179 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21180 attributes = TREE_CHAIN (attributes);
21181 if (attributes)
21182 TREE_CHAIN (attributes) = NULL_TREE;
21184 /* If there is any qualification still in effect, clear it
21185 now; we will be starting fresh with the next declarator. */
21186 parser->scope = NULL_TREE;
21187 parser->qualifying_scope = NULL_TREE;
21188 parser->object_scope = NULL_TREE;
21189 /* If it's a `,', then there are more declarators. */
21190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21192 cp_lexer_consume_token (parser->lexer);
21193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21195 cp_token *token = cp_lexer_previous_token (parser->lexer);
21196 error_at (token->location,
21197 "stray %<,%> at end of member declaration");
21200 /* If the next token isn't a `;', then we have a parse error. */
21201 else if (cp_lexer_next_token_is_not (parser->lexer,
21202 CPP_SEMICOLON))
21204 /* The next token might be a ways away from where the
21205 actual semicolon is missing. Find the previous token
21206 and use that for our error position. */
21207 cp_token *token = cp_lexer_previous_token (parser->lexer);
21208 error_at (token->location,
21209 "expected %<;%> at end of member declaration");
21211 /* Assume that the user meant to provide a semicolon. If
21212 we were to cp_parser_skip_to_end_of_statement, we might
21213 skip to a semicolon inside a member function definition
21214 and issue nonsensical error messages. */
21215 assume_semicolon = true;
21218 if (decl)
21220 /* Add DECL to the list of members. */
21221 if (!friend_p
21222 /* Explicitly include, eg, NSDMIs, for better error
21223 recovery (c++/58650). */
21224 || !DECL_DECLARES_FUNCTION_P (decl))
21225 finish_member_declaration (decl);
21227 if (TREE_CODE (decl) == FUNCTION_DECL)
21228 cp_parser_save_default_args (parser, decl);
21229 else if (TREE_CODE (decl) == FIELD_DECL
21230 && !DECL_C_BIT_FIELD (decl)
21231 && DECL_INITIAL (decl))
21232 /* Add DECL to the queue of NSDMI to be parsed later. */
21233 vec_safe_push (unparsed_nsdmis, decl);
21236 if (assume_semicolon)
21237 goto out;
21241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21242 out:
21243 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21246 /* Parse a pure-specifier.
21248 pure-specifier:
21251 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21252 Otherwise, ERROR_MARK_NODE is returned. */
21254 static tree
21255 cp_parser_pure_specifier (cp_parser* parser)
21257 cp_token *token;
21259 /* Look for the `=' token. */
21260 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21261 return error_mark_node;
21262 /* Look for the `0' token. */
21263 token = cp_lexer_peek_token (parser->lexer);
21265 if (token->type == CPP_EOF
21266 || token->type == CPP_PRAGMA_EOL)
21267 return error_mark_node;
21269 cp_lexer_consume_token (parser->lexer);
21271 /* Accept = default or = delete in c++0x mode. */
21272 if (token->keyword == RID_DEFAULT
21273 || token->keyword == RID_DELETE)
21275 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21276 return token->u.value;
21279 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21280 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21282 cp_parser_error (parser,
21283 "invalid pure specifier (only %<= 0%> is allowed)");
21284 cp_parser_skip_to_end_of_statement (parser);
21285 return error_mark_node;
21287 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21289 error_at (token->location, "templates may not be %<virtual%>");
21290 return error_mark_node;
21293 return integer_zero_node;
21296 /* Parse a constant-initializer.
21298 constant-initializer:
21299 = constant-expression
21301 Returns a representation of the constant-expression. */
21303 static tree
21304 cp_parser_constant_initializer (cp_parser* parser)
21306 /* Look for the `=' token. */
21307 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21308 return error_mark_node;
21310 /* It is invalid to write:
21312 struct S { static const int i = { 7 }; };
21315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21317 cp_parser_error (parser,
21318 "a brace-enclosed initializer is not allowed here");
21319 /* Consume the opening brace. */
21320 cp_lexer_consume_token (parser->lexer);
21321 /* Skip the initializer. */
21322 cp_parser_skip_to_closing_brace (parser);
21323 /* Look for the trailing `}'. */
21324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21326 return error_mark_node;
21329 return cp_parser_constant_expression (parser);
21332 /* Derived classes [gram.class.derived] */
21334 /* Parse a base-clause.
21336 base-clause:
21337 : base-specifier-list
21339 base-specifier-list:
21340 base-specifier ... [opt]
21341 base-specifier-list , base-specifier ... [opt]
21343 Returns a TREE_LIST representing the base-classes, in the order in
21344 which they were declared. The representation of each node is as
21345 described by cp_parser_base_specifier.
21347 In the case that no bases are specified, this function will return
21348 NULL_TREE, not ERROR_MARK_NODE. */
21350 static tree
21351 cp_parser_base_clause (cp_parser* parser)
21353 tree bases = NULL_TREE;
21355 /* Look for the `:' that begins the list. */
21356 cp_parser_require (parser, CPP_COLON, RT_COLON);
21358 /* Scan the base-specifier-list. */
21359 while (true)
21361 cp_token *token;
21362 tree base;
21363 bool pack_expansion_p = false;
21365 /* Look for the base-specifier. */
21366 base = cp_parser_base_specifier (parser);
21367 /* Look for the (optional) ellipsis. */
21368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21370 /* Consume the `...'. */
21371 cp_lexer_consume_token (parser->lexer);
21373 pack_expansion_p = true;
21376 /* Add BASE to the front of the list. */
21377 if (base && base != error_mark_node)
21379 if (pack_expansion_p)
21380 /* Make this a pack expansion type. */
21381 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21383 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21385 TREE_CHAIN (base) = bases;
21386 bases = base;
21389 /* Peek at the next token. */
21390 token = cp_lexer_peek_token (parser->lexer);
21391 /* If it's not a comma, then the list is complete. */
21392 if (token->type != CPP_COMMA)
21393 break;
21394 /* Consume the `,'. */
21395 cp_lexer_consume_token (parser->lexer);
21398 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21399 base class had a qualified name. However, the next name that
21400 appears is certainly not qualified. */
21401 parser->scope = NULL_TREE;
21402 parser->qualifying_scope = NULL_TREE;
21403 parser->object_scope = NULL_TREE;
21405 return nreverse (bases);
21408 /* Parse a base-specifier.
21410 base-specifier:
21411 :: [opt] nested-name-specifier [opt] class-name
21412 virtual access-specifier [opt] :: [opt] nested-name-specifier
21413 [opt] class-name
21414 access-specifier virtual [opt] :: [opt] nested-name-specifier
21415 [opt] class-name
21417 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21418 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21419 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21420 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21422 static tree
21423 cp_parser_base_specifier (cp_parser* parser)
21425 cp_token *token;
21426 bool done = false;
21427 bool virtual_p = false;
21428 bool duplicate_virtual_error_issued_p = false;
21429 bool duplicate_access_error_issued_p = false;
21430 bool class_scope_p, template_p;
21431 tree access = access_default_node;
21432 tree type;
21434 /* Process the optional `virtual' and `access-specifier'. */
21435 while (!done)
21437 /* Peek at the next token. */
21438 token = cp_lexer_peek_token (parser->lexer);
21439 /* Process `virtual'. */
21440 switch (token->keyword)
21442 case RID_VIRTUAL:
21443 /* If `virtual' appears more than once, issue an error. */
21444 if (virtual_p && !duplicate_virtual_error_issued_p)
21446 cp_parser_error (parser,
21447 "%<virtual%> specified more than once in base-specified");
21448 duplicate_virtual_error_issued_p = true;
21451 virtual_p = true;
21453 /* Consume the `virtual' token. */
21454 cp_lexer_consume_token (parser->lexer);
21456 break;
21458 case RID_PUBLIC:
21459 case RID_PROTECTED:
21460 case RID_PRIVATE:
21461 /* If more than one access specifier appears, issue an
21462 error. */
21463 if (access != access_default_node
21464 && !duplicate_access_error_issued_p)
21466 cp_parser_error (parser,
21467 "more than one access specifier in base-specified");
21468 duplicate_access_error_issued_p = true;
21471 access = ridpointers[(int) token->keyword];
21473 /* Consume the access-specifier. */
21474 cp_lexer_consume_token (parser->lexer);
21476 break;
21478 default:
21479 done = true;
21480 break;
21483 /* It is not uncommon to see programs mechanically, erroneously, use
21484 the 'typename' keyword to denote (dependent) qualified types
21485 as base classes. */
21486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21488 token = cp_lexer_peek_token (parser->lexer);
21489 if (!processing_template_decl)
21490 error_at (token->location,
21491 "keyword %<typename%> not allowed outside of templates");
21492 else
21493 error_at (token->location,
21494 "keyword %<typename%> not allowed in this context "
21495 "(the base class is implicitly a type)");
21496 cp_lexer_consume_token (parser->lexer);
21499 /* Look for the optional `::' operator. */
21500 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21501 /* Look for the nested-name-specifier. The simplest way to
21502 implement:
21504 [temp.res]
21506 The keyword `typename' is not permitted in a base-specifier or
21507 mem-initializer; in these contexts a qualified name that
21508 depends on a template-parameter is implicitly assumed to be a
21509 type name.
21511 is to pretend that we have seen the `typename' keyword at this
21512 point. */
21513 cp_parser_nested_name_specifier_opt (parser,
21514 /*typename_keyword_p=*/true,
21515 /*check_dependency_p=*/true,
21516 typename_type,
21517 /*is_declaration=*/true);
21518 /* If the base class is given by a qualified name, assume that names
21519 we see are type names or templates, as appropriate. */
21520 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21521 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21523 if (!parser->scope
21524 && cp_lexer_next_token_is_decltype (parser->lexer))
21525 /* DR 950 allows decltype as a base-specifier. */
21526 type = cp_parser_decltype (parser);
21527 else
21529 /* Otherwise, look for the class-name. */
21530 type = cp_parser_class_name (parser,
21531 class_scope_p,
21532 template_p,
21533 typename_type,
21534 /*check_dependency_p=*/true,
21535 /*class_head_p=*/false,
21536 /*is_declaration=*/true);
21537 type = TREE_TYPE (type);
21540 if (type == error_mark_node)
21541 return error_mark_node;
21543 return finish_base_specifier (type, access, virtual_p);
21546 /* Exception handling [gram.exception] */
21548 /* Parse an (optional) noexcept-specification.
21550 noexcept-specification:
21551 noexcept ( constant-expression ) [opt]
21553 If no noexcept-specification is present, returns NULL_TREE.
21554 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21555 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21556 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21557 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21558 in which case a boolean condition is returned instead. */
21560 static tree
21561 cp_parser_noexcept_specification_opt (cp_parser* parser,
21562 bool require_constexpr,
21563 bool* consumed_expr,
21564 bool return_cond)
21566 cp_token *token;
21567 const char *saved_message;
21569 /* Peek at the next token. */
21570 token = cp_lexer_peek_token (parser->lexer);
21572 /* Is it a noexcept-specification? */
21573 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21575 tree expr;
21576 cp_lexer_consume_token (parser->lexer);
21578 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21580 cp_lexer_consume_token (parser->lexer);
21582 if (require_constexpr)
21584 /* Types may not be defined in an exception-specification. */
21585 saved_message = parser->type_definition_forbidden_message;
21586 parser->type_definition_forbidden_message
21587 = G_("types may not be defined in an exception-specification");
21589 expr = cp_parser_constant_expression (parser);
21591 /* Restore the saved message. */
21592 parser->type_definition_forbidden_message = saved_message;
21594 else
21596 expr = cp_parser_expression (parser);
21597 *consumed_expr = true;
21600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21602 else
21604 expr = boolean_true_node;
21605 if (!require_constexpr)
21606 *consumed_expr = false;
21609 /* We cannot build a noexcept-spec right away because this will check
21610 that expr is a constexpr. */
21611 if (!return_cond)
21612 return build_noexcept_spec (expr, tf_warning_or_error);
21613 else
21614 return expr;
21616 else
21617 return NULL_TREE;
21620 /* Parse an (optional) exception-specification.
21622 exception-specification:
21623 throw ( type-id-list [opt] )
21625 Returns a TREE_LIST representing the exception-specification. The
21626 TREE_VALUE of each node is a type. */
21628 static tree
21629 cp_parser_exception_specification_opt (cp_parser* parser)
21631 cp_token *token;
21632 tree type_id_list;
21633 const char *saved_message;
21635 /* Peek at the next token. */
21636 token = cp_lexer_peek_token (parser->lexer);
21638 /* Is it a noexcept-specification? */
21639 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21640 false);
21641 if (type_id_list != NULL_TREE)
21642 return type_id_list;
21644 /* If it's not `throw', then there's no exception-specification. */
21645 if (!cp_parser_is_keyword (token, RID_THROW))
21646 return NULL_TREE;
21648 #if 0
21649 /* Enable this once a lot of code has transitioned to noexcept? */
21650 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21651 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21652 "deprecated in C++0x; use %<noexcept%> instead");
21653 #endif
21655 /* Consume the `throw'. */
21656 cp_lexer_consume_token (parser->lexer);
21658 /* Look for the `('. */
21659 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21661 /* Peek at the next token. */
21662 token = cp_lexer_peek_token (parser->lexer);
21663 /* If it's not a `)', then there is a type-id-list. */
21664 if (token->type != CPP_CLOSE_PAREN)
21666 /* Types may not be defined in an exception-specification. */
21667 saved_message = parser->type_definition_forbidden_message;
21668 parser->type_definition_forbidden_message
21669 = G_("types may not be defined in an exception-specification");
21670 /* Parse the type-id-list. */
21671 type_id_list = cp_parser_type_id_list (parser);
21672 /* Restore the saved message. */
21673 parser->type_definition_forbidden_message = saved_message;
21675 else
21676 type_id_list = empty_except_spec;
21678 /* Look for the `)'. */
21679 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21681 return type_id_list;
21684 /* Parse an (optional) type-id-list.
21686 type-id-list:
21687 type-id ... [opt]
21688 type-id-list , type-id ... [opt]
21690 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21691 in the order that the types were presented. */
21693 static tree
21694 cp_parser_type_id_list (cp_parser* parser)
21696 tree types = NULL_TREE;
21698 while (true)
21700 cp_token *token;
21701 tree type;
21703 /* Get the next type-id. */
21704 type = cp_parser_type_id (parser);
21705 /* Parse the optional ellipsis. */
21706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21708 /* Consume the `...'. */
21709 cp_lexer_consume_token (parser->lexer);
21711 /* Turn the type into a pack expansion expression. */
21712 type = make_pack_expansion (type);
21714 /* Add it to the list. */
21715 types = add_exception_specifier (types, type, /*complain=*/1);
21716 /* Peek at the next token. */
21717 token = cp_lexer_peek_token (parser->lexer);
21718 /* If it is not a `,', we are done. */
21719 if (token->type != CPP_COMMA)
21720 break;
21721 /* Consume the `,'. */
21722 cp_lexer_consume_token (parser->lexer);
21725 return nreverse (types);
21728 /* Parse a try-block.
21730 try-block:
21731 try compound-statement handler-seq */
21733 static tree
21734 cp_parser_try_block (cp_parser* parser)
21736 tree try_block;
21738 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21739 if (parser->in_function_body
21740 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21741 error ("%<try%> in %<constexpr%> function");
21743 try_block = begin_try_block ();
21744 cp_parser_compound_statement (parser, NULL, true, false);
21745 finish_try_block (try_block);
21746 cp_parser_handler_seq (parser);
21747 finish_handler_sequence (try_block);
21749 return try_block;
21752 /* Parse a function-try-block.
21754 function-try-block:
21755 try ctor-initializer [opt] function-body handler-seq */
21757 static bool
21758 cp_parser_function_try_block (cp_parser* parser)
21760 tree compound_stmt;
21761 tree try_block;
21762 bool ctor_initializer_p;
21764 /* Look for the `try' keyword. */
21765 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21766 return false;
21767 /* Let the rest of the front end know where we are. */
21768 try_block = begin_function_try_block (&compound_stmt);
21769 /* Parse the function-body. */
21770 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21771 (parser, /*in_function_try_block=*/true);
21772 /* We're done with the `try' part. */
21773 finish_function_try_block (try_block);
21774 /* Parse the handlers. */
21775 cp_parser_handler_seq (parser);
21776 /* We're done with the handlers. */
21777 finish_function_handler_sequence (try_block, compound_stmt);
21779 return ctor_initializer_p;
21782 /* Parse a handler-seq.
21784 handler-seq:
21785 handler handler-seq [opt] */
21787 static void
21788 cp_parser_handler_seq (cp_parser* parser)
21790 while (true)
21792 cp_token *token;
21794 /* Parse the handler. */
21795 cp_parser_handler (parser);
21796 /* Peek at the next token. */
21797 token = cp_lexer_peek_token (parser->lexer);
21798 /* If it's not `catch' then there are no more handlers. */
21799 if (!cp_parser_is_keyword (token, RID_CATCH))
21800 break;
21804 /* Parse a handler.
21806 handler:
21807 catch ( exception-declaration ) compound-statement */
21809 static void
21810 cp_parser_handler (cp_parser* parser)
21812 tree handler;
21813 tree declaration;
21815 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21816 handler = begin_handler ();
21817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21818 declaration = cp_parser_exception_declaration (parser);
21819 finish_handler_parms (declaration, handler);
21820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21821 cp_parser_compound_statement (parser, NULL, false, false);
21822 finish_handler (handler);
21825 /* Parse an exception-declaration.
21827 exception-declaration:
21828 type-specifier-seq declarator
21829 type-specifier-seq abstract-declarator
21830 type-specifier-seq
21833 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21834 ellipsis variant is used. */
21836 static tree
21837 cp_parser_exception_declaration (cp_parser* parser)
21839 cp_decl_specifier_seq type_specifiers;
21840 cp_declarator *declarator;
21841 const char *saved_message;
21843 /* If it's an ellipsis, it's easy to handle. */
21844 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21846 /* Consume the `...' token. */
21847 cp_lexer_consume_token (parser->lexer);
21848 return NULL_TREE;
21851 /* Types may not be defined in exception-declarations. */
21852 saved_message = parser->type_definition_forbidden_message;
21853 parser->type_definition_forbidden_message
21854 = G_("types may not be defined in exception-declarations");
21856 /* Parse the type-specifier-seq. */
21857 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21858 /*is_trailing_return=*/false,
21859 &type_specifiers);
21860 /* If it's a `)', then there is no declarator. */
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21862 declarator = NULL;
21863 else
21864 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21865 /*ctor_dtor_or_conv_p=*/NULL,
21866 /*parenthesized_p=*/NULL,
21867 /*member_p=*/false,
21868 /*friend_p=*/false);
21870 /* Restore the saved message. */
21871 parser->type_definition_forbidden_message = saved_message;
21873 if (!type_specifiers.any_specifiers_p)
21874 return error_mark_node;
21876 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21879 /* Parse a throw-expression.
21881 throw-expression:
21882 throw assignment-expression [opt]
21884 Returns a THROW_EXPR representing the throw-expression. */
21886 static tree
21887 cp_parser_throw_expression (cp_parser* parser)
21889 tree expression;
21890 cp_token* token;
21892 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21893 token = cp_lexer_peek_token (parser->lexer);
21894 /* Figure out whether or not there is an assignment-expression
21895 following the "throw" keyword. */
21896 if (token->type == CPP_COMMA
21897 || token->type == CPP_SEMICOLON
21898 || token->type == CPP_CLOSE_PAREN
21899 || token->type == CPP_CLOSE_SQUARE
21900 || token->type == CPP_CLOSE_BRACE
21901 || token->type == CPP_COLON)
21902 expression = NULL_TREE;
21903 else
21904 expression = cp_parser_assignment_expression (parser);
21906 return build_throw (expression);
21909 /* GNU Extensions */
21911 /* Parse an (optional) asm-specification.
21913 asm-specification:
21914 asm ( string-literal )
21916 If the asm-specification is present, returns a STRING_CST
21917 corresponding to the string-literal. Otherwise, returns
21918 NULL_TREE. */
21920 static tree
21921 cp_parser_asm_specification_opt (cp_parser* parser)
21923 cp_token *token;
21924 tree asm_specification;
21926 /* Peek at the next token. */
21927 token = cp_lexer_peek_token (parser->lexer);
21928 /* If the next token isn't the `asm' keyword, then there's no
21929 asm-specification. */
21930 if (!cp_parser_is_keyword (token, RID_ASM))
21931 return NULL_TREE;
21933 /* Consume the `asm' token. */
21934 cp_lexer_consume_token (parser->lexer);
21935 /* Look for the `('. */
21936 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21938 /* Look for the string-literal. */
21939 asm_specification = cp_parser_string_literal (parser, false, false);
21941 /* Look for the `)'. */
21942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21944 return asm_specification;
21947 /* Parse an asm-operand-list.
21949 asm-operand-list:
21950 asm-operand
21951 asm-operand-list , asm-operand
21953 asm-operand:
21954 string-literal ( expression )
21955 [ string-literal ] string-literal ( expression )
21957 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21958 each node is the expression. The TREE_PURPOSE is itself a
21959 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21960 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21961 is a STRING_CST for the string literal before the parenthesis. Returns
21962 ERROR_MARK_NODE if any of the operands are invalid. */
21964 static tree
21965 cp_parser_asm_operand_list (cp_parser* parser)
21967 tree asm_operands = NULL_TREE;
21968 bool invalid_operands = false;
21970 while (true)
21972 tree string_literal;
21973 tree expression;
21974 tree name;
21976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21978 /* Consume the `[' token. */
21979 cp_lexer_consume_token (parser->lexer);
21980 /* Read the operand name. */
21981 name = cp_parser_identifier (parser);
21982 if (name != error_mark_node)
21983 name = build_string (IDENTIFIER_LENGTH (name),
21984 IDENTIFIER_POINTER (name));
21985 /* Look for the closing `]'. */
21986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21988 else
21989 name = NULL_TREE;
21990 /* Look for the string-literal. */
21991 string_literal = cp_parser_string_literal (parser, false, false);
21993 /* Look for the `('. */
21994 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21995 /* Parse the expression. */
21996 expression = cp_parser_expression (parser);
21997 /* Look for the `)'. */
21998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22000 if (name == error_mark_node
22001 || string_literal == error_mark_node
22002 || expression == error_mark_node)
22003 invalid_operands = true;
22005 /* Add this operand to the list. */
22006 asm_operands = tree_cons (build_tree_list (name, string_literal),
22007 expression,
22008 asm_operands);
22009 /* If the next token is not a `,', there are no more
22010 operands. */
22011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22012 break;
22013 /* Consume the `,'. */
22014 cp_lexer_consume_token (parser->lexer);
22017 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22020 /* Parse an asm-clobber-list.
22022 asm-clobber-list:
22023 string-literal
22024 asm-clobber-list , string-literal
22026 Returns a TREE_LIST, indicating the clobbers in the order that they
22027 appeared. The TREE_VALUE of each node is a STRING_CST. */
22029 static tree
22030 cp_parser_asm_clobber_list (cp_parser* parser)
22032 tree clobbers = NULL_TREE;
22034 while (true)
22036 tree string_literal;
22038 /* Look for the string literal. */
22039 string_literal = cp_parser_string_literal (parser, false, false);
22040 /* Add it to the list. */
22041 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22042 /* If the next token is not a `,', then the list is
22043 complete. */
22044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22045 break;
22046 /* Consume the `,' token. */
22047 cp_lexer_consume_token (parser->lexer);
22050 return clobbers;
22053 /* Parse an asm-label-list.
22055 asm-label-list:
22056 identifier
22057 asm-label-list , identifier
22059 Returns a TREE_LIST, indicating the labels in the order that they
22060 appeared. The TREE_VALUE of each node is a label. */
22062 static tree
22063 cp_parser_asm_label_list (cp_parser* parser)
22065 tree labels = NULL_TREE;
22067 while (true)
22069 tree identifier, label, name;
22071 /* Look for the identifier. */
22072 identifier = cp_parser_identifier (parser);
22073 if (!error_operand_p (identifier))
22075 label = lookup_label (identifier);
22076 if (TREE_CODE (label) == LABEL_DECL)
22078 TREE_USED (label) = 1;
22079 check_goto (label);
22080 name = build_string (IDENTIFIER_LENGTH (identifier),
22081 IDENTIFIER_POINTER (identifier));
22082 labels = tree_cons (name, label, labels);
22085 /* If the next token is not a `,', then the list is
22086 complete. */
22087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22088 break;
22089 /* Consume the `,' token. */
22090 cp_lexer_consume_token (parser->lexer);
22093 return nreverse (labels);
22096 /* Return TRUE iff the next tokens in the stream are possibly the
22097 beginning of a GNU extension attribute. */
22099 static bool
22100 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22102 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22105 /* Return TRUE iff the next tokens in the stream are possibly the
22106 beginning of a standard C++-11 attribute specifier. */
22108 static bool
22109 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22111 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22114 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22115 beginning of a standard C++-11 attribute specifier. */
22117 static bool
22118 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22120 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22122 return (cxx_dialect >= cxx11
22123 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22124 || (token->type == CPP_OPEN_SQUARE
22125 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22126 && token->type == CPP_OPEN_SQUARE)));
22129 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22130 beginning of a GNU extension attribute. */
22132 static bool
22133 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22135 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22137 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22140 /* Return true iff the next tokens can be the beginning of either a
22141 GNU attribute list, or a standard C++11 attribute sequence. */
22143 static bool
22144 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22146 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22147 || cp_next_tokens_can_be_std_attribute_p (parser));
22150 /* Return true iff the next Nth tokens can be the beginning of either
22151 a GNU attribute list, or a standard C++11 attribute sequence. */
22153 static bool
22154 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22156 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22157 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22160 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22161 of GNU attributes, or return NULL. */
22163 static tree
22164 cp_parser_attributes_opt (cp_parser *parser)
22166 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22167 return cp_parser_gnu_attributes_opt (parser);
22168 return cp_parser_std_attribute_spec_seq (parser);
22171 #define CILK_SIMD_FN_CLAUSE_MASK \
22172 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22173 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22174 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22175 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22176 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22178 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22179 vector [(<clauses>)] */
22181 static void
22182 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22184 bool first_p = parser->cilk_simd_fn_info == NULL;
22185 cp_token *token = v_token;
22186 if (first_p)
22188 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22189 parser->cilk_simd_fn_info->error_seen = false;
22190 parser->cilk_simd_fn_info->fndecl_seen = false;
22191 parser->cilk_simd_fn_info->tokens = vNULL;
22193 int paren_scope = 0;
22194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22196 cp_lexer_consume_token (parser->lexer);
22197 v_token = cp_lexer_peek_token (parser->lexer);
22198 paren_scope++;
22200 while (paren_scope > 0)
22202 token = cp_lexer_peek_token (parser->lexer);
22203 if (token->type == CPP_OPEN_PAREN)
22204 paren_scope++;
22205 else if (token->type == CPP_CLOSE_PAREN)
22206 paren_scope--;
22207 /* Do not push the last ')' */
22208 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22209 cp_lexer_consume_token (parser->lexer);
22212 token->type = CPP_PRAGMA_EOL;
22213 parser->lexer->next_token = token;
22214 cp_lexer_consume_token (parser->lexer);
22216 struct cp_token_cache *cp
22217 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22218 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22221 /* Parse an (optional) series of attributes.
22223 attributes:
22224 attributes attribute
22226 attribute:
22227 __attribute__ (( attribute-list [opt] ))
22229 The return value is as for cp_parser_gnu_attribute_list. */
22231 static tree
22232 cp_parser_gnu_attributes_opt (cp_parser* parser)
22234 tree attributes = NULL_TREE;
22236 while (true)
22238 cp_token *token;
22239 tree attribute_list;
22240 bool ok = true;
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_token (parser->lexer);
22244 /* If it's not `__attribute__', then we're done. */
22245 if (token->keyword != RID_ATTRIBUTE)
22246 break;
22248 /* Consume the `__attribute__' keyword. */
22249 cp_lexer_consume_token (parser->lexer);
22250 /* Look for the two `(' tokens. */
22251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22252 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22254 /* Peek at the next token. */
22255 token = cp_lexer_peek_token (parser->lexer);
22256 if (token->type != CPP_CLOSE_PAREN)
22257 /* Parse the attribute-list. */
22258 attribute_list = cp_parser_gnu_attribute_list (parser);
22259 else
22260 /* If the next token is a `)', then there is no attribute
22261 list. */
22262 attribute_list = NULL;
22264 /* Look for the two `)' tokens. */
22265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22266 ok = false;
22267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22268 ok = false;
22269 if (!ok)
22270 cp_parser_skip_to_end_of_statement (parser);
22272 /* Add these new attributes to the list. */
22273 attributes = chainon (attributes, attribute_list);
22276 return attributes;
22279 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22280 "__vector" or "__vector__." */
22282 static inline bool
22283 is_cilkplus_vector_p (tree name)
22285 if (flag_cilkplus && is_attribute_p ("vector", name))
22286 return true;
22287 return false;
22290 /* Parse a GNU attribute-list.
22292 attribute-list:
22293 attribute
22294 attribute-list , attribute
22296 attribute:
22297 identifier
22298 identifier ( identifier )
22299 identifier ( identifier , expression-list )
22300 identifier ( expression-list )
22302 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22303 to an attribute. The TREE_PURPOSE of each node is the identifier
22304 indicating which attribute is in use. The TREE_VALUE represents
22305 the arguments, if any. */
22307 static tree
22308 cp_parser_gnu_attribute_list (cp_parser* parser)
22310 tree attribute_list = NULL_TREE;
22311 bool save_translate_strings_p = parser->translate_strings_p;
22313 parser->translate_strings_p = false;
22314 while (true)
22316 cp_token *token;
22317 tree identifier;
22318 tree attribute;
22320 /* Look for the identifier. We also allow keywords here; for
22321 example `__attribute__ ((const))' is legal. */
22322 token = cp_lexer_peek_token (parser->lexer);
22323 if (token->type == CPP_NAME
22324 || token->type == CPP_KEYWORD)
22326 tree arguments = NULL_TREE;
22328 /* Consume the token, but save it since we need it for the
22329 SIMD enabled function parsing. */
22330 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22332 /* Save away the identifier that indicates which attribute
22333 this is. */
22334 identifier = (token->type == CPP_KEYWORD)
22335 /* For keywords, use the canonical spelling, not the
22336 parsed identifier. */
22337 ? ridpointers[(int) token->keyword]
22338 : id_token->u.value;
22340 attribute = build_tree_list (identifier, NULL_TREE);
22342 /* Peek at the next token. */
22343 token = cp_lexer_peek_token (parser->lexer);
22344 /* If it's an `(', then parse the attribute arguments. */
22345 if (token->type == CPP_OPEN_PAREN)
22347 vec<tree, va_gc> *vec;
22348 int attr_flag = (attribute_takes_identifier_p (identifier)
22349 ? id_attr : normal_attr);
22350 if (is_cilkplus_vector_p (identifier))
22352 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22353 continue;
22355 else
22356 vec = cp_parser_parenthesized_expression_list
22357 (parser, attr_flag, /*cast_p=*/false,
22358 /*allow_expansion_p=*/false,
22359 /*non_constant_p=*/NULL);
22360 if (vec == NULL)
22361 arguments = error_mark_node;
22362 else
22364 arguments = build_tree_list_vec (vec);
22365 release_tree_vector (vec);
22367 /* Save the arguments away. */
22368 TREE_VALUE (attribute) = arguments;
22370 else if (is_cilkplus_vector_p (identifier))
22372 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22373 continue;
22376 if (arguments != error_mark_node)
22378 /* Add this attribute to the list. */
22379 TREE_CHAIN (attribute) = attribute_list;
22380 attribute_list = attribute;
22383 token = cp_lexer_peek_token (parser->lexer);
22385 /* Now, look for more attributes. If the next token isn't a
22386 `,', we're done. */
22387 if (token->type != CPP_COMMA)
22388 break;
22390 /* Consume the comma and keep going. */
22391 cp_lexer_consume_token (parser->lexer);
22393 parser->translate_strings_p = save_translate_strings_p;
22395 /* We built up the list in reverse order. */
22396 return nreverse (attribute_list);
22399 /* Parse a standard C++11 attribute.
22401 The returned representation is a TREE_LIST which TREE_PURPOSE is
22402 the scoped name of the attribute, and the TREE_VALUE is its
22403 arguments list.
22405 Note that the scoped name of the attribute is itself a TREE_LIST
22406 which TREE_PURPOSE is the namespace of the attribute, and
22407 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22408 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22409 and which TREE_PURPOSE is directly the attribute name.
22411 Clients of the attribute code should use get_attribute_namespace
22412 and get_attribute_name to get the actual namespace and name of
22413 attributes, regardless of their being GNU or C++11 attributes.
22415 attribute:
22416 attribute-token attribute-argument-clause [opt]
22418 attribute-token:
22419 identifier
22420 attribute-scoped-token
22422 attribute-scoped-token:
22423 attribute-namespace :: identifier
22425 attribute-namespace:
22426 identifier
22428 attribute-argument-clause:
22429 ( balanced-token-seq )
22431 balanced-token-seq:
22432 balanced-token [opt]
22433 balanced-token-seq balanced-token
22435 balanced-token:
22436 ( balanced-token-seq )
22437 [ balanced-token-seq ]
22438 { balanced-token-seq }. */
22440 static tree
22441 cp_parser_std_attribute (cp_parser *parser)
22443 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22444 cp_token *token;
22446 /* First, parse name of the the attribute, a.k.a
22447 attribute-token. */
22449 token = cp_lexer_peek_token (parser->lexer);
22450 if (token->type == CPP_NAME)
22451 attr_id = token->u.value;
22452 else if (token->type == CPP_KEYWORD)
22453 attr_id = ridpointers[(int) token->keyword];
22454 else if (token->flags & NAMED_OP)
22455 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22457 if (attr_id == NULL_TREE)
22458 return NULL_TREE;
22460 cp_lexer_consume_token (parser->lexer);
22462 token = cp_lexer_peek_token (parser->lexer);
22463 if (token->type == CPP_SCOPE)
22465 /* We are seeing a scoped attribute token. */
22467 cp_lexer_consume_token (parser->lexer);
22468 attr_ns = attr_id;
22470 token = cp_lexer_consume_token (parser->lexer);
22471 if (token->type == CPP_NAME)
22472 attr_id = token->u.value;
22473 else if (token->type == CPP_KEYWORD)
22474 attr_id = ridpointers[(int) token->keyword];
22475 else
22477 error_at (token->location,
22478 "expected an identifier for the attribute name");
22479 return error_mark_node;
22481 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22482 NULL_TREE);
22483 token = cp_lexer_peek_token (parser->lexer);
22485 else
22487 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22488 NULL_TREE);
22489 /* C++11 noreturn attribute is equivalent to GNU's. */
22490 if (is_attribute_p ("noreturn", attr_id))
22491 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22492 /* C++14 deprecated attribute is equivalent to GNU's. */
22493 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22495 if (cxx_dialect == cxx11)
22496 pedwarn (token->location, OPT_Wpedantic,
22497 "%<deprecated%> is a C++14 feature;"
22498 " use %<gnu::deprecated%>");
22499 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22503 /* Now parse the optional argument clause of the attribute. */
22505 if (token->type != CPP_OPEN_PAREN)
22506 return attribute;
22509 vec<tree, va_gc> *vec;
22510 int attr_flag = normal_attr;
22512 if (attr_ns == get_identifier ("gnu")
22513 && attribute_takes_identifier_p (attr_id))
22514 /* A GNU attribute that takes an identifier in parameter. */
22515 attr_flag = id_attr;
22517 vec = cp_parser_parenthesized_expression_list
22518 (parser, attr_flag, /*cast_p=*/false,
22519 /*allow_expansion_p=*/true,
22520 /*non_constant_p=*/NULL);
22521 if (vec == NULL)
22522 arguments = error_mark_node;
22523 else
22525 arguments = build_tree_list_vec (vec);
22526 release_tree_vector (vec);
22529 if (arguments == error_mark_node)
22530 attribute = error_mark_node;
22531 else
22532 TREE_VALUE (attribute) = arguments;
22535 return attribute;
22538 /* Parse a list of standard C++-11 attributes.
22540 attribute-list:
22541 attribute [opt]
22542 attribute-list , attribute[opt]
22543 attribute ...
22544 attribute-list , attribute ...
22547 static tree
22548 cp_parser_std_attribute_list (cp_parser *parser)
22550 tree attributes = NULL_TREE, attribute = NULL_TREE;
22551 cp_token *token = NULL;
22553 while (true)
22555 attribute = cp_parser_std_attribute (parser);
22556 if (attribute == error_mark_node)
22557 break;
22558 if (attribute != NULL_TREE)
22560 TREE_CHAIN (attribute) = attributes;
22561 attributes = attribute;
22563 token = cp_lexer_peek_token (parser->lexer);
22564 if (token->type == CPP_ELLIPSIS)
22566 cp_lexer_consume_token (parser->lexer);
22567 TREE_VALUE (attribute)
22568 = make_pack_expansion (TREE_VALUE (attribute));
22569 token = cp_lexer_peek_token (parser->lexer);
22571 if (token->type != CPP_COMMA)
22572 break;
22573 cp_lexer_consume_token (parser->lexer);
22575 attributes = nreverse (attributes);
22576 return attributes;
22579 /* Parse a standard C++-11 attribute specifier.
22581 attribute-specifier:
22582 [ [ attribute-list ] ]
22583 alignment-specifier
22585 alignment-specifier:
22586 alignas ( type-id ... [opt] )
22587 alignas ( alignment-expression ... [opt] ). */
22589 static tree
22590 cp_parser_std_attribute_spec (cp_parser *parser)
22592 tree attributes = NULL_TREE;
22593 cp_token *token = cp_lexer_peek_token (parser->lexer);
22595 if (token->type == CPP_OPEN_SQUARE
22596 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22598 cp_lexer_consume_token (parser->lexer);
22599 cp_lexer_consume_token (parser->lexer);
22601 attributes = cp_parser_std_attribute_list (parser);
22603 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22604 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22605 cp_parser_skip_to_end_of_statement (parser);
22606 else
22607 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22608 when we are sure that we have actually parsed them. */
22609 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22611 else
22613 tree alignas_expr;
22615 /* Look for an alignment-specifier. */
22617 token = cp_lexer_peek_token (parser->lexer);
22619 if (token->type != CPP_KEYWORD
22620 || token->keyword != RID_ALIGNAS)
22621 return NULL_TREE;
22623 cp_lexer_consume_token (parser->lexer);
22624 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22626 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22628 cp_parser_error (parser, "expected %<(%>");
22629 return error_mark_node;
22632 cp_parser_parse_tentatively (parser);
22633 alignas_expr = cp_parser_type_id (parser);
22635 if (!cp_parser_parse_definitely (parser))
22637 gcc_assert (alignas_expr == error_mark_node
22638 || alignas_expr == NULL_TREE);
22640 alignas_expr =
22641 cp_parser_assignment_expression (parser);
22642 if (alignas_expr == error_mark_node)
22643 cp_parser_skip_to_end_of_statement (parser);
22644 if (alignas_expr == NULL_TREE
22645 || alignas_expr == error_mark_node)
22646 return alignas_expr;
22649 alignas_expr = cxx_alignas_expr (alignas_expr);
22650 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22652 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22654 cp_lexer_consume_token (parser->lexer);
22655 alignas_expr = make_pack_expansion (alignas_expr);
22658 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22660 cp_parser_error (parser, "expected %<)%>");
22661 return error_mark_node;
22664 /* Build the C++-11 representation of an 'aligned'
22665 attribute. */
22666 attributes =
22667 build_tree_list (build_tree_list (get_identifier ("gnu"),
22668 get_identifier ("aligned")),
22669 alignas_expr);
22672 return attributes;
22675 /* Parse a standard C++-11 attribute-specifier-seq.
22677 attribute-specifier-seq:
22678 attribute-specifier-seq [opt] attribute-specifier
22681 static tree
22682 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22684 tree attr_specs = NULL;
22686 while (true)
22688 tree attr_spec = cp_parser_std_attribute_spec (parser);
22689 if (attr_spec == NULL_TREE)
22690 break;
22691 if (attr_spec == error_mark_node)
22692 return error_mark_node;
22694 TREE_CHAIN (attr_spec) = attr_specs;
22695 attr_specs = attr_spec;
22698 attr_specs = nreverse (attr_specs);
22699 return attr_specs;
22702 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22703 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22704 current value of the PEDANTIC flag, regardless of whether or not
22705 the `__extension__' keyword is present. The caller is responsible
22706 for restoring the value of the PEDANTIC flag. */
22708 static bool
22709 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22711 /* Save the old value of the PEDANTIC flag. */
22712 *saved_pedantic = pedantic;
22714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22716 /* Consume the `__extension__' token. */
22717 cp_lexer_consume_token (parser->lexer);
22718 /* We're not being pedantic while the `__extension__' keyword is
22719 in effect. */
22720 pedantic = 0;
22722 return true;
22725 return false;
22728 /* Parse a label declaration.
22730 label-declaration:
22731 __label__ label-declarator-seq ;
22733 label-declarator-seq:
22734 identifier , label-declarator-seq
22735 identifier */
22737 static void
22738 cp_parser_label_declaration (cp_parser* parser)
22740 /* Look for the `__label__' keyword. */
22741 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22743 while (true)
22745 tree identifier;
22747 /* Look for an identifier. */
22748 identifier = cp_parser_identifier (parser);
22749 /* If we failed, stop. */
22750 if (identifier == error_mark_node)
22751 break;
22752 /* Declare it as a label. */
22753 finish_label_decl (identifier);
22754 /* If the next token is a `;', stop. */
22755 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22756 break;
22757 /* Look for the `,' separating the label declarations. */
22758 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22761 /* Look for the final `;'. */
22762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22765 /* Support Functions */
22767 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22768 NAME should have one of the representations used for an
22769 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22770 is returned. If PARSER->SCOPE is a dependent type, then a
22771 SCOPE_REF is returned.
22773 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22774 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22775 was formed. Abstractly, such entities should not be passed to this
22776 function, because they do not need to be looked up, but it is
22777 simpler to check for this special case here, rather than at the
22778 call-sites.
22780 In cases not explicitly covered above, this function returns a
22781 DECL, OVERLOAD, or baselink representing the result of the lookup.
22782 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22783 is returned.
22785 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22786 (e.g., "struct") that was used. In that case bindings that do not
22787 refer to types are ignored.
22789 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22790 ignored.
22792 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22793 are ignored.
22795 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22796 types.
22798 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22799 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22800 NULL_TREE otherwise. */
22802 static tree
22803 cp_parser_lookup_name (cp_parser *parser, tree name,
22804 enum tag_types tag_type,
22805 bool is_template,
22806 bool is_namespace,
22807 bool check_dependency,
22808 tree *ambiguous_decls,
22809 location_t name_location)
22811 tree decl;
22812 tree object_type = parser->context->object_type;
22814 /* Assume that the lookup will be unambiguous. */
22815 if (ambiguous_decls)
22816 *ambiguous_decls = NULL_TREE;
22818 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22819 no longer valid. Note that if we are parsing tentatively, and
22820 the parse fails, OBJECT_TYPE will be automatically restored. */
22821 parser->context->object_type = NULL_TREE;
22823 if (name == error_mark_node)
22824 return error_mark_node;
22826 /* A template-id has already been resolved; there is no lookup to
22827 do. */
22828 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22829 return name;
22830 if (BASELINK_P (name))
22832 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22833 == TEMPLATE_ID_EXPR);
22834 return name;
22837 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22838 it should already have been checked to make sure that the name
22839 used matches the type being destroyed. */
22840 if (TREE_CODE (name) == BIT_NOT_EXPR)
22842 tree type;
22844 /* Figure out to which type this destructor applies. */
22845 if (parser->scope)
22846 type = parser->scope;
22847 else if (object_type)
22848 type = object_type;
22849 else
22850 type = current_class_type;
22851 /* If that's not a class type, there is no destructor. */
22852 if (!type || !CLASS_TYPE_P (type))
22853 return error_mark_node;
22854 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22855 lazily_declare_fn (sfk_destructor, type);
22856 if (!CLASSTYPE_DESTRUCTORS (type))
22857 return error_mark_node;
22858 /* If it was a class type, return the destructor. */
22859 return CLASSTYPE_DESTRUCTORS (type);
22862 /* By this point, the NAME should be an ordinary identifier. If
22863 the id-expression was a qualified name, the qualifying scope is
22864 stored in PARSER->SCOPE at this point. */
22865 gcc_assert (identifier_p (name));
22867 /* Perform the lookup. */
22868 if (parser->scope)
22870 bool dependent_p;
22872 if (parser->scope == error_mark_node)
22873 return error_mark_node;
22875 /* If the SCOPE is dependent, the lookup must be deferred until
22876 the template is instantiated -- unless we are explicitly
22877 looking up names in uninstantiated templates. Even then, we
22878 cannot look up the name if the scope is not a class type; it
22879 might, for example, be a template type parameter. */
22880 dependent_p = (TYPE_P (parser->scope)
22881 && dependent_scope_p (parser->scope));
22882 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22883 && dependent_p)
22884 /* Defer lookup. */
22885 decl = error_mark_node;
22886 else
22888 tree pushed_scope = NULL_TREE;
22890 /* If PARSER->SCOPE is a dependent type, then it must be a
22891 class type, and we must not be checking dependencies;
22892 otherwise, we would have processed this lookup above. So
22893 that PARSER->SCOPE is not considered a dependent base by
22894 lookup_member, we must enter the scope here. */
22895 if (dependent_p)
22896 pushed_scope = push_scope (parser->scope);
22898 /* If the PARSER->SCOPE is a template specialization, it
22899 may be instantiated during name lookup. In that case,
22900 errors may be issued. Even if we rollback the current
22901 tentative parse, those errors are valid. */
22902 decl = lookup_qualified_name (parser->scope, name,
22903 tag_type != none_type,
22904 /*complain=*/true);
22906 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22907 lookup result and the nested-name-specifier nominates a class C:
22908 * if the name specified after the nested-name-specifier, when
22909 looked up in C, is the injected-class-name of C (Clause 9), or
22910 * if the name specified after the nested-name-specifier is the
22911 same as the identifier or the simple-template-id's template-
22912 name in the last component of the nested-name-specifier,
22913 the name is instead considered to name the constructor of
22914 class C. [ Note: for example, the constructor is not an
22915 acceptable lookup result in an elaborated-type-specifier so
22916 the constructor would not be used in place of the
22917 injected-class-name. --end note ] Such a constructor name
22918 shall be used only in the declarator-id of a declaration that
22919 names a constructor or in a using-declaration. */
22920 if (tag_type == none_type
22921 && DECL_SELF_REFERENCE_P (decl)
22922 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22923 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22924 tag_type != none_type,
22925 /*complain=*/true);
22927 /* If we have a single function from a using decl, pull it out. */
22928 if (TREE_CODE (decl) == OVERLOAD
22929 && !really_overloaded_fn (decl))
22930 decl = OVL_FUNCTION (decl);
22932 if (pushed_scope)
22933 pop_scope (pushed_scope);
22936 /* If the scope is a dependent type and either we deferred lookup or
22937 we did lookup but didn't find the name, rememeber the name. */
22938 if (decl == error_mark_node && TYPE_P (parser->scope)
22939 && dependent_type_p (parser->scope))
22941 if (tag_type)
22943 tree type;
22945 /* The resolution to Core Issue 180 says that `struct
22946 A::B' should be considered a type-name, even if `A'
22947 is dependent. */
22948 type = make_typename_type (parser->scope, name, tag_type,
22949 /*complain=*/tf_error);
22950 if (type != error_mark_node)
22951 decl = TYPE_NAME (type);
22953 else if (is_template
22954 && (cp_parser_next_token_ends_template_argument_p (parser)
22955 || cp_lexer_next_token_is (parser->lexer,
22956 CPP_CLOSE_PAREN)))
22957 decl = make_unbound_class_template (parser->scope,
22958 name, NULL_TREE,
22959 /*complain=*/tf_error);
22960 else
22961 decl = build_qualified_name (/*type=*/NULL_TREE,
22962 parser->scope, name,
22963 is_template);
22965 parser->qualifying_scope = parser->scope;
22966 parser->object_scope = NULL_TREE;
22968 else if (object_type)
22970 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22971 OBJECT_TYPE is not a class. */
22972 if (CLASS_TYPE_P (object_type))
22973 /* If the OBJECT_TYPE is a template specialization, it may
22974 be instantiated during name lookup. In that case, errors
22975 may be issued. Even if we rollback the current tentative
22976 parse, those errors are valid. */
22977 decl = lookup_member (object_type,
22978 name,
22979 /*protect=*/0,
22980 tag_type != none_type,
22981 tf_warning_or_error);
22982 else
22983 decl = NULL_TREE;
22985 if (!decl)
22986 /* Look it up in the enclosing context. */
22987 decl = lookup_name_real (name, tag_type != none_type,
22988 /*nonclass=*/0,
22989 /*block_p=*/true, is_namespace, 0);
22990 parser->object_scope = object_type;
22991 parser->qualifying_scope = NULL_TREE;
22993 else
22995 decl = lookup_name_real (name, tag_type != none_type,
22996 /*nonclass=*/0,
22997 /*block_p=*/true, is_namespace, 0);
22998 parser->qualifying_scope = NULL_TREE;
22999 parser->object_scope = NULL_TREE;
23002 /* If the lookup failed, let our caller know. */
23003 if (!decl || decl == error_mark_node)
23004 return error_mark_node;
23006 /* Pull out the template from an injected-class-name (or multiple). */
23007 if (is_template)
23008 decl = maybe_get_template_decl_from_type_decl (decl);
23010 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23011 if (TREE_CODE (decl) == TREE_LIST)
23013 if (ambiguous_decls)
23014 *ambiguous_decls = decl;
23015 /* The error message we have to print is too complicated for
23016 cp_parser_error, so we incorporate its actions directly. */
23017 if (!cp_parser_simulate_error (parser))
23019 error_at (name_location, "reference to %qD is ambiguous",
23020 name);
23021 print_candidates (decl);
23023 return error_mark_node;
23026 gcc_assert (DECL_P (decl)
23027 || TREE_CODE (decl) == OVERLOAD
23028 || TREE_CODE (decl) == SCOPE_REF
23029 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23030 || BASELINK_P (decl));
23032 /* If we have resolved the name of a member declaration, check to
23033 see if the declaration is accessible. When the name resolves to
23034 set of overloaded functions, accessibility is checked when
23035 overload resolution is done.
23037 During an explicit instantiation, access is not checked at all,
23038 as per [temp.explicit]. */
23039 if (DECL_P (decl))
23040 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23042 maybe_record_typedef_use (decl);
23044 return decl;
23047 /* Like cp_parser_lookup_name, but for use in the typical case where
23048 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23049 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23051 static tree
23052 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23054 return cp_parser_lookup_name (parser, name,
23055 none_type,
23056 /*is_template=*/false,
23057 /*is_namespace=*/false,
23058 /*check_dependency=*/true,
23059 /*ambiguous_decls=*/NULL,
23060 location);
23063 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23064 the current context, return the TYPE_DECL. If TAG_NAME_P is
23065 true, the DECL indicates the class being defined in a class-head,
23066 or declared in an elaborated-type-specifier.
23068 Otherwise, return DECL. */
23070 static tree
23071 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23073 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23074 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23076 struct A {
23077 template <typename T> struct B;
23080 template <typename T> struct A::B {};
23082 Similarly, in an elaborated-type-specifier:
23084 namespace N { struct X{}; }
23086 struct A {
23087 template <typename T> friend struct N::X;
23090 However, if the DECL refers to a class type, and we are in
23091 the scope of the class, then the name lookup automatically
23092 finds the TYPE_DECL created by build_self_reference rather
23093 than a TEMPLATE_DECL. For example, in:
23095 template <class T> struct S {
23096 S s;
23099 there is no need to handle such case. */
23101 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23102 return DECL_TEMPLATE_RESULT (decl);
23104 return decl;
23107 /* If too many, or too few, template-parameter lists apply to the
23108 declarator, issue an error message. Returns TRUE if all went well,
23109 and FALSE otherwise. */
23111 static bool
23112 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23113 cp_declarator *declarator,
23114 location_t declarator_location)
23116 switch (declarator->kind)
23118 case cdk_id:
23120 unsigned num_templates = 0;
23121 tree scope = declarator->u.id.qualifying_scope;
23123 if (scope)
23124 num_templates = num_template_headers_for_class (scope);
23125 else if (TREE_CODE (declarator->u.id.unqualified_name)
23126 == TEMPLATE_ID_EXPR)
23127 /* If the DECLARATOR has the form `X<y>' then it uses one
23128 additional level of template parameters. */
23129 ++num_templates;
23131 return cp_parser_check_template_parameters
23132 (parser, num_templates, declarator_location, declarator);
23135 case cdk_function:
23136 case cdk_array:
23137 case cdk_pointer:
23138 case cdk_reference:
23139 case cdk_ptrmem:
23140 return (cp_parser_check_declarator_template_parameters
23141 (parser, declarator->declarator, declarator_location));
23143 case cdk_error:
23144 return true;
23146 default:
23147 gcc_unreachable ();
23149 return false;
23152 /* NUM_TEMPLATES were used in the current declaration. If that is
23153 invalid, return FALSE and issue an error messages. Otherwise,
23154 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23155 declarator and we can print more accurate diagnostics. */
23157 static bool
23158 cp_parser_check_template_parameters (cp_parser* parser,
23159 unsigned num_templates,
23160 location_t location,
23161 cp_declarator *declarator)
23163 /* If there are the same number of template classes and parameter
23164 lists, that's OK. */
23165 if (parser->num_template_parameter_lists == num_templates)
23166 return true;
23167 /* If there are more, but only one more, then we are referring to a
23168 member template. That's OK too. */
23169 if (parser->num_template_parameter_lists == num_templates + 1)
23170 return true;
23171 /* If there are more template classes than parameter lists, we have
23172 something like:
23174 template <class T> void S<T>::R<T>::f (); */
23175 if (parser->num_template_parameter_lists < num_templates)
23177 if (declarator && !current_function_decl)
23178 error_at (location, "specializing member %<%T::%E%> "
23179 "requires %<template<>%> syntax",
23180 declarator->u.id.qualifying_scope,
23181 declarator->u.id.unqualified_name);
23182 else if (declarator)
23183 error_at (location, "invalid declaration of %<%T::%E%>",
23184 declarator->u.id.qualifying_scope,
23185 declarator->u.id.unqualified_name);
23186 else
23187 error_at (location, "too few template-parameter-lists");
23188 return false;
23190 /* Otherwise, there are too many template parameter lists. We have
23191 something like:
23193 template <class T> template <class U> void S::f(); */
23194 error_at (location, "too many template-parameter-lists");
23195 return false;
23198 /* Parse an optional `::' token indicating that the following name is
23199 from the global namespace. If so, PARSER->SCOPE is set to the
23200 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23201 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23202 Returns the new value of PARSER->SCOPE, if the `::' token is
23203 present, and NULL_TREE otherwise. */
23205 static tree
23206 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23208 cp_token *token;
23210 /* Peek at the next token. */
23211 token = cp_lexer_peek_token (parser->lexer);
23212 /* If we're looking at a `::' token then we're starting from the
23213 global namespace, not our current location. */
23214 if (token->type == CPP_SCOPE)
23216 /* Consume the `::' token. */
23217 cp_lexer_consume_token (parser->lexer);
23218 /* Set the SCOPE so that we know where to start the lookup. */
23219 parser->scope = global_namespace;
23220 parser->qualifying_scope = global_namespace;
23221 parser->object_scope = NULL_TREE;
23223 return parser->scope;
23225 else if (!current_scope_valid_p)
23227 parser->scope = NULL_TREE;
23228 parser->qualifying_scope = NULL_TREE;
23229 parser->object_scope = NULL_TREE;
23232 return NULL_TREE;
23235 /* Returns TRUE if the upcoming token sequence is the start of a
23236 constructor declarator. If FRIEND_P is true, the declarator is
23237 preceded by the `friend' specifier. */
23239 static bool
23240 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23242 bool constructor_p;
23243 bool outside_class_specifier_p;
23244 tree nested_name_specifier;
23245 cp_token *next_token;
23247 /* The common case is that this is not a constructor declarator, so
23248 try to avoid doing lots of work if at all possible. It's not
23249 valid declare a constructor at function scope. */
23250 if (parser->in_function_body)
23251 return false;
23252 /* And only certain tokens can begin a constructor declarator. */
23253 next_token = cp_lexer_peek_token (parser->lexer);
23254 if (next_token->type != CPP_NAME
23255 && next_token->type != CPP_SCOPE
23256 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23257 && next_token->type != CPP_TEMPLATE_ID)
23258 return false;
23260 /* Parse tentatively; we are going to roll back all of the tokens
23261 consumed here. */
23262 cp_parser_parse_tentatively (parser);
23263 /* Assume that we are looking at a constructor declarator. */
23264 constructor_p = true;
23266 /* Look for the optional `::' operator. */
23267 cp_parser_global_scope_opt (parser,
23268 /*current_scope_valid_p=*/false);
23269 /* Look for the nested-name-specifier. */
23270 nested_name_specifier
23271 = (cp_parser_nested_name_specifier_opt (parser,
23272 /*typename_keyword_p=*/false,
23273 /*check_dependency_p=*/false,
23274 /*type_p=*/false,
23275 /*is_declaration=*/false));
23277 outside_class_specifier_p = (!at_class_scope_p ()
23278 || !TYPE_BEING_DEFINED (current_class_type)
23279 || friend_p);
23281 /* Outside of a class-specifier, there must be a
23282 nested-name-specifier. */
23283 if (!nested_name_specifier && outside_class_specifier_p)
23284 constructor_p = false;
23285 else if (nested_name_specifier == error_mark_node)
23286 constructor_p = false;
23288 /* If we have a class scope, this is easy; DR 147 says that S::S always
23289 names the constructor, and no other qualified name could. */
23290 if (constructor_p && nested_name_specifier
23291 && CLASS_TYPE_P (nested_name_specifier))
23293 tree id = cp_parser_unqualified_id (parser,
23294 /*template_keyword_p=*/false,
23295 /*check_dependency_p=*/false,
23296 /*declarator_p=*/true,
23297 /*optional_p=*/false);
23298 if (is_overloaded_fn (id))
23299 id = DECL_NAME (get_first_fn (id));
23300 if (!constructor_name_p (id, nested_name_specifier))
23301 constructor_p = false;
23303 /* If we still think that this might be a constructor-declarator,
23304 look for a class-name. */
23305 else if (constructor_p)
23307 /* If we have:
23309 template <typename T> struct S {
23310 S();
23313 we must recognize that the nested `S' names a class. */
23314 tree type_decl;
23315 type_decl = cp_parser_class_name (parser,
23316 /*typename_keyword_p=*/false,
23317 /*template_keyword_p=*/false,
23318 none_type,
23319 /*check_dependency_p=*/false,
23320 /*class_head_p=*/false,
23321 /*is_declaration=*/false);
23322 /* If there was no class-name, then this is not a constructor.
23323 Otherwise, if we are in a class-specifier and we aren't
23324 handling a friend declaration, check that its type matches
23325 current_class_type (c++/38313). Note: error_mark_node
23326 is left alone for error recovery purposes. */
23327 constructor_p = (!cp_parser_error_occurred (parser)
23328 && (outside_class_specifier_p
23329 || type_decl == error_mark_node
23330 || same_type_p (current_class_type,
23331 TREE_TYPE (type_decl))));
23333 /* If we're still considering a constructor, we have to see a `(',
23334 to begin the parameter-declaration-clause, followed by either a
23335 `)', an `...', or a decl-specifier. We need to check for a
23336 type-specifier to avoid being fooled into thinking that:
23338 S (f) (int);
23340 is a constructor. (It is actually a function named `f' that
23341 takes one parameter (of type `int') and returns a value of type
23342 `S'. */
23343 if (constructor_p
23344 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23345 constructor_p = false;
23347 if (constructor_p
23348 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23349 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23350 /* A parameter declaration begins with a decl-specifier,
23351 which is either the "attribute" keyword, a storage class
23352 specifier, or (usually) a type-specifier. */
23353 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23355 tree type;
23356 tree pushed_scope = NULL_TREE;
23357 unsigned saved_num_template_parameter_lists;
23359 /* Names appearing in the type-specifier should be looked up
23360 in the scope of the class. */
23361 if (current_class_type)
23362 type = NULL_TREE;
23363 else
23365 type = TREE_TYPE (type_decl);
23366 if (TREE_CODE (type) == TYPENAME_TYPE)
23368 type = resolve_typename_type (type,
23369 /*only_current_p=*/false);
23370 if (TREE_CODE (type) == TYPENAME_TYPE)
23372 cp_parser_abort_tentative_parse (parser);
23373 return false;
23376 pushed_scope = push_scope (type);
23379 /* Inside the constructor parameter list, surrounding
23380 template-parameter-lists do not apply. */
23381 saved_num_template_parameter_lists
23382 = parser->num_template_parameter_lists;
23383 parser->num_template_parameter_lists = 0;
23385 /* Look for the type-specifier. */
23386 cp_parser_type_specifier (parser,
23387 CP_PARSER_FLAGS_NONE,
23388 /*decl_specs=*/NULL,
23389 /*is_declarator=*/true,
23390 /*declares_class_or_enum=*/NULL,
23391 /*is_cv_qualifier=*/NULL);
23393 parser->num_template_parameter_lists
23394 = saved_num_template_parameter_lists;
23396 /* Leave the scope of the class. */
23397 if (pushed_scope)
23398 pop_scope (pushed_scope);
23400 constructor_p = !cp_parser_error_occurred (parser);
23404 /* We did not really want to consume any tokens. */
23405 cp_parser_abort_tentative_parse (parser);
23407 return constructor_p;
23410 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23411 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23412 they must be performed once we are in the scope of the function.
23414 Returns the function defined. */
23416 static tree
23417 cp_parser_function_definition_from_specifiers_and_declarator
23418 (cp_parser* parser,
23419 cp_decl_specifier_seq *decl_specifiers,
23420 tree attributes,
23421 const cp_declarator *declarator)
23423 tree fn;
23424 bool success_p;
23426 /* Begin the function-definition. */
23427 success_p = start_function (decl_specifiers, declarator, attributes);
23429 /* The things we're about to see are not directly qualified by any
23430 template headers we've seen thus far. */
23431 reset_specialization ();
23433 /* If there were names looked up in the decl-specifier-seq that we
23434 did not check, check them now. We must wait until we are in the
23435 scope of the function to perform the checks, since the function
23436 might be a friend. */
23437 perform_deferred_access_checks (tf_warning_or_error);
23439 if (success_p)
23441 cp_finalize_omp_declare_simd (parser, current_function_decl);
23442 parser->omp_declare_simd = NULL;
23445 if (!success_p)
23447 /* Skip the entire function. */
23448 cp_parser_skip_to_end_of_block_or_statement (parser);
23449 fn = error_mark_node;
23451 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23453 /* Seen already, skip it. An error message has already been output. */
23454 cp_parser_skip_to_end_of_block_or_statement (parser);
23455 fn = current_function_decl;
23456 current_function_decl = NULL_TREE;
23457 /* If this is a function from a class, pop the nested class. */
23458 if (current_class_name)
23459 pop_nested_class ();
23461 else
23463 timevar_id_t tv;
23464 if (DECL_DECLARED_INLINE_P (current_function_decl))
23465 tv = TV_PARSE_INLINE;
23466 else
23467 tv = TV_PARSE_FUNC;
23468 timevar_push (tv);
23469 fn = cp_parser_function_definition_after_declarator (parser,
23470 /*inline_p=*/false);
23471 timevar_pop (tv);
23474 return fn;
23477 /* Parse the part of a function-definition that follows the
23478 declarator. INLINE_P is TRUE iff this function is an inline
23479 function defined within a class-specifier.
23481 Returns the function defined. */
23483 static tree
23484 cp_parser_function_definition_after_declarator (cp_parser* parser,
23485 bool inline_p)
23487 tree fn;
23488 bool ctor_initializer_p = false;
23489 bool saved_in_unbraced_linkage_specification_p;
23490 bool saved_in_function_body;
23491 unsigned saved_num_template_parameter_lists;
23492 cp_token *token;
23493 bool fully_implicit_function_template_p
23494 = parser->fully_implicit_function_template_p;
23495 parser->fully_implicit_function_template_p = false;
23496 tree implicit_template_parms
23497 = parser->implicit_template_parms;
23498 parser->implicit_template_parms = 0;
23499 cp_binding_level* implicit_template_scope
23500 = parser->implicit_template_scope;
23501 parser->implicit_template_scope = 0;
23503 saved_in_function_body = parser->in_function_body;
23504 parser->in_function_body = true;
23505 /* If the next token is `return', then the code may be trying to
23506 make use of the "named return value" extension that G++ used to
23507 support. */
23508 token = cp_lexer_peek_token (parser->lexer);
23509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23511 /* Consume the `return' keyword. */
23512 cp_lexer_consume_token (parser->lexer);
23513 /* Look for the identifier that indicates what value is to be
23514 returned. */
23515 cp_parser_identifier (parser);
23516 /* Issue an error message. */
23517 error_at (token->location,
23518 "named return values are no longer supported");
23519 /* Skip tokens until we reach the start of the function body. */
23520 while (true)
23522 cp_token *token = cp_lexer_peek_token (parser->lexer);
23523 if (token->type == CPP_OPEN_BRACE
23524 || token->type == CPP_EOF
23525 || token->type == CPP_PRAGMA_EOL)
23526 break;
23527 cp_lexer_consume_token (parser->lexer);
23530 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23531 anything declared inside `f'. */
23532 saved_in_unbraced_linkage_specification_p
23533 = parser->in_unbraced_linkage_specification_p;
23534 parser->in_unbraced_linkage_specification_p = false;
23535 /* Inside the function, surrounding template-parameter-lists do not
23536 apply. */
23537 saved_num_template_parameter_lists
23538 = parser->num_template_parameter_lists;
23539 parser->num_template_parameter_lists = 0;
23541 start_lambda_scope (current_function_decl);
23543 /* If the next token is `try', `__transaction_atomic', or
23544 `__transaction_relaxed`, then we are looking at either function-try-block
23545 or function-transaction-block. Note that all of these include the
23546 function-body. */
23547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23548 ctor_initializer_p = cp_parser_function_transaction (parser,
23549 RID_TRANSACTION_ATOMIC);
23550 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23551 RID_TRANSACTION_RELAXED))
23552 ctor_initializer_p = cp_parser_function_transaction (parser,
23553 RID_TRANSACTION_RELAXED);
23554 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23555 ctor_initializer_p = cp_parser_function_try_block (parser);
23556 else
23557 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23558 (parser, /*in_function_try_block=*/false);
23560 finish_lambda_scope ();
23562 /* Finish the function. */
23563 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23564 (inline_p ? 2 : 0));
23565 /* Generate code for it, if necessary. */
23566 expand_or_defer_fn (fn);
23567 /* Restore the saved values. */
23568 parser->in_unbraced_linkage_specification_p
23569 = saved_in_unbraced_linkage_specification_p;
23570 parser->num_template_parameter_lists
23571 = saved_num_template_parameter_lists;
23572 parser->in_function_body = saved_in_function_body;
23574 parser->fully_implicit_function_template_p
23575 = fully_implicit_function_template_p;
23576 parser->implicit_template_parms
23577 = implicit_template_parms;
23578 parser->implicit_template_scope
23579 = implicit_template_scope;
23581 if (parser->fully_implicit_function_template_p)
23582 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23584 return fn;
23587 /* Parse a template-declaration, assuming that the `export' (and
23588 `extern') keywords, if present, has already been scanned. MEMBER_P
23589 is as for cp_parser_template_declaration. */
23591 static void
23592 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23594 tree decl = NULL_TREE;
23595 vec<deferred_access_check, va_gc> *checks;
23596 tree parameter_list;
23597 bool friend_p = false;
23598 bool need_lang_pop;
23599 cp_token *token;
23601 /* Look for the `template' keyword. */
23602 token = cp_lexer_peek_token (parser->lexer);
23603 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23604 return;
23606 /* And the `<'. */
23607 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23608 return;
23609 if (at_class_scope_p () && current_function_decl)
23611 /* 14.5.2.2 [temp.mem]
23613 A local class shall not have member templates. */
23614 error_at (token->location,
23615 "invalid declaration of member template in local class");
23616 cp_parser_skip_to_end_of_block_or_statement (parser);
23617 return;
23619 /* [temp]
23621 A template ... shall not have C linkage. */
23622 if (current_lang_name == lang_name_c)
23624 error_at (token->location, "template with C linkage");
23625 /* Give it C++ linkage to avoid confusing other parts of the
23626 front end. */
23627 push_lang_context (lang_name_cplusplus);
23628 need_lang_pop = true;
23630 else
23631 need_lang_pop = false;
23633 /* We cannot perform access checks on the template parameter
23634 declarations until we know what is being declared, just as we
23635 cannot check the decl-specifier list. */
23636 push_deferring_access_checks (dk_deferred);
23638 /* If the next token is `>', then we have an invalid
23639 specialization. Rather than complain about an invalid template
23640 parameter, issue an error message here. */
23641 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23643 cp_parser_error (parser, "invalid explicit specialization");
23644 begin_specialization ();
23645 parameter_list = NULL_TREE;
23647 else
23649 /* Parse the template parameters. */
23650 parameter_list = cp_parser_template_parameter_list (parser);
23653 /* Get the deferred access checks from the parameter list. These
23654 will be checked once we know what is being declared, as for a
23655 member template the checks must be performed in the scope of the
23656 class containing the member. */
23657 checks = get_deferred_access_checks ();
23659 /* Look for the `>'. */
23660 cp_parser_skip_to_end_of_template_parameter_list (parser);
23661 /* We just processed one more parameter list. */
23662 ++parser->num_template_parameter_lists;
23663 /* If the next token is `template', there are more template
23664 parameters. */
23665 if (cp_lexer_next_token_is_keyword (parser->lexer,
23666 RID_TEMPLATE))
23667 cp_parser_template_declaration_after_export (parser, member_p);
23668 else if (cxx_dialect >= cxx11
23669 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23670 decl = cp_parser_alias_declaration (parser);
23671 else
23673 /* There are no access checks when parsing a template, as we do not
23674 know if a specialization will be a friend. */
23675 push_deferring_access_checks (dk_no_check);
23676 token = cp_lexer_peek_token (parser->lexer);
23677 decl = cp_parser_single_declaration (parser,
23678 checks,
23679 member_p,
23680 /*explicit_specialization_p=*/false,
23681 &friend_p);
23682 pop_deferring_access_checks ();
23684 /* If this is a member template declaration, let the front
23685 end know. */
23686 if (member_p && !friend_p && decl)
23688 if (TREE_CODE (decl) == TYPE_DECL)
23689 cp_parser_check_access_in_redeclaration (decl, token->location);
23691 decl = finish_member_template_decl (decl);
23693 else if (friend_p && decl
23694 && DECL_DECLARES_TYPE_P (decl))
23695 make_friend_class (current_class_type, TREE_TYPE (decl),
23696 /*complain=*/true);
23698 /* We are done with the current parameter list. */
23699 --parser->num_template_parameter_lists;
23701 pop_deferring_access_checks ();
23703 /* Finish up. */
23704 finish_template_decl (parameter_list);
23706 /* Check the template arguments for a literal operator template. */
23707 if (decl
23708 && DECL_DECLARES_FUNCTION_P (decl)
23709 && UDLIT_OPER_P (DECL_NAME (decl)))
23711 bool ok = true;
23712 if (parameter_list == NULL_TREE)
23713 ok = false;
23714 else
23716 int num_parms = TREE_VEC_LENGTH (parameter_list);
23717 if (num_parms == 1)
23719 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23720 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23721 if (TREE_TYPE (parm) != char_type_node
23722 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23723 ok = false;
23725 else if (num_parms == 2 && cxx_dialect >= cxx14)
23727 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23728 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23729 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23730 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23731 if (TREE_TYPE (parm) != TREE_TYPE (type)
23732 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23733 ok = false;
23735 else
23736 ok = false;
23738 if (!ok)
23740 if (cxx_dialect >= cxx14)
23741 error ("literal operator template %qD has invalid parameter list."
23742 " Expected non-type template argument pack <char...>"
23743 " or <typename CharT, CharT...>",
23744 decl);
23745 else
23746 error ("literal operator template %qD has invalid parameter list."
23747 " Expected non-type template argument pack <char...>",
23748 decl);
23751 /* Register member declarations. */
23752 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23753 finish_member_declaration (decl);
23754 /* For the erroneous case of a template with C linkage, we pushed an
23755 implicit C++ linkage scope; exit that scope now. */
23756 if (need_lang_pop)
23757 pop_lang_context ();
23758 /* If DECL is a function template, we must return to parse it later.
23759 (Even though there is no definition, there might be default
23760 arguments that need handling.) */
23761 if (member_p && decl
23762 && DECL_DECLARES_FUNCTION_P (decl))
23763 vec_safe_push (unparsed_funs_with_definitions, decl);
23766 /* Perform the deferred access checks from a template-parameter-list.
23767 CHECKS is a TREE_LIST of access checks, as returned by
23768 get_deferred_access_checks. */
23770 static void
23771 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23773 ++processing_template_parmlist;
23774 perform_access_checks (checks, tf_warning_or_error);
23775 --processing_template_parmlist;
23778 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23779 `function-definition' sequence that follows a template header.
23780 If MEMBER_P is true, this declaration appears in a class scope.
23782 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23783 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23785 static tree
23786 cp_parser_single_declaration (cp_parser* parser,
23787 vec<deferred_access_check, va_gc> *checks,
23788 bool member_p,
23789 bool explicit_specialization_p,
23790 bool* friend_p)
23792 int declares_class_or_enum;
23793 tree decl = NULL_TREE;
23794 cp_decl_specifier_seq decl_specifiers;
23795 bool function_definition_p = false;
23796 cp_token *decl_spec_token_start;
23798 /* This function is only used when processing a template
23799 declaration. */
23800 gcc_assert (innermost_scope_kind () == sk_template_parms
23801 || innermost_scope_kind () == sk_template_spec);
23803 /* Defer access checks until we know what is being declared. */
23804 push_deferring_access_checks (dk_deferred);
23806 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23807 alternative. */
23808 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23809 cp_parser_decl_specifier_seq (parser,
23810 CP_PARSER_FLAGS_OPTIONAL,
23811 &decl_specifiers,
23812 &declares_class_or_enum);
23813 if (friend_p)
23814 *friend_p = cp_parser_friend_p (&decl_specifiers);
23816 /* There are no template typedefs. */
23817 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23819 error_at (decl_spec_token_start->location,
23820 "template declaration of %<typedef%>");
23821 decl = error_mark_node;
23824 /* Gather up the access checks that occurred the
23825 decl-specifier-seq. */
23826 stop_deferring_access_checks ();
23828 /* Check for the declaration of a template class. */
23829 if (declares_class_or_enum)
23831 if (cp_parser_declares_only_class_p (parser))
23833 decl = shadow_tag (&decl_specifiers);
23835 /* In this case:
23837 struct C {
23838 friend template <typename T> struct A<T>::B;
23841 A<T>::B will be represented by a TYPENAME_TYPE, and
23842 therefore not recognized by shadow_tag. */
23843 if (friend_p && *friend_p
23844 && !decl
23845 && decl_specifiers.type
23846 && TYPE_P (decl_specifiers.type))
23847 decl = decl_specifiers.type;
23849 if (decl && decl != error_mark_node)
23850 decl = TYPE_NAME (decl);
23851 else
23852 decl = error_mark_node;
23854 /* Perform access checks for template parameters. */
23855 cp_parser_perform_template_parameter_access_checks (checks);
23859 /* Complain about missing 'typename' or other invalid type names. */
23860 if (!decl_specifiers.any_type_specifiers_p
23861 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23863 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23864 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23865 the rest of this declaration. */
23866 decl = error_mark_node;
23867 goto out;
23870 /* If it's not a template class, try for a template function. If
23871 the next token is a `;', then this declaration does not declare
23872 anything. But, if there were errors in the decl-specifiers, then
23873 the error might well have come from an attempted class-specifier.
23874 In that case, there's no need to warn about a missing declarator. */
23875 if (!decl
23876 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23877 || decl_specifiers.type != error_mark_node))
23879 decl = cp_parser_init_declarator (parser,
23880 &decl_specifiers,
23881 checks,
23882 /*function_definition_allowed_p=*/true,
23883 member_p,
23884 declares_class_or_enum,
23885 &function_definition_p,
23886 NULL, NULL);
23888 /* 7.1.1-1 [dcl.stc]
23890 A storage-class-specifier shall not be specified in an explicit
23891 specialization... */
23892 if (decl
23893 && explicit_specialization_p
23894 && decl_specifiers.storage_class != sc_none)
23896 error_at (decl_spec_token_start->location,
23897 "explicit template specialization cannot have a storage class");
23898 decl = error_mark_node;
23901 if (decl && VAR_P (decl))
23902 check_template_variable (decl);
23905 /* Look for a trailing `;' after the declaration. */
23906 if (!function_definition_p
23907 && (decl == error_mark_node
23908 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23909 cp_parser_skip_to_end_of_block_or_statement (parser);
23911 out:
23912 pop_deferring_access_checks ();
23914 /* Clear any current qualification; whatever comes next is the start
23915 of something new. */
23916 parser->scope = NULL_TREE;
23917 parser->qualifying_scope = NULL_TREE;
23918 parser->object_scope = NULL_TREE;
23920 return decl;
23923 /* Parse a cast-expression that is not the operand of a unary "&". */
23925 static tree
23926 cp_parser_simple_cast_expression (cp_parser *parser)
23928 return cp_parser_cast_expression (parser, /*address_p=*/false,
23929 /*cast_p=*/false, /*decltype*/false, NULL);
23932 /* Parse a functional cast to TYPE. Returns an expression
23933 representing the cast. */
23935 static tree
23936 cp_parser_functional_cast (cp_parser* parser, tree type)
23938 vec<tree, va_gc> *vec;
23939 tree expression_list;
23940 tree cast;
23941 bool nonconst_p;
23943 if (!type)
23944 type = error_mark_node;
23946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23948 cp_lexer_set_source_position (parser->lexer);
23949 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23950 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23951 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23952 if (TREE_CODE (type) == TYPE_DECL)
23953 type = TREE_TYPE (type);
23954 return finish_compound_literal (type, expression_list,
23955 tf_warning_or_error);
23959 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23960 /*cast_p=*/true,
23961 /*allow_expansion_p=*/true,
23962 /*non_constant_p=*/NULL);
23963 if (vec == NULL)
23964 expression_list = error_mark_node;
23965 else
23967 expression_list = build_tree_list_vec (vec);
23968 release_tree_vector (vec);
23971 cast = build_functional_cast (type, expression_list,
23972 tf_warning_or_error);
23973 /* [expr.const]/1: In an integral constant expression "only type
23974 conversions to integral or enumeration type can be used". */
23975 if (TREE_CODE (type) == TYPE_DECL)
23976 type = TREE_TYPE (type);
23977 if (cast != error_mark_node
23978 && !cast_valid_in_integral_constant_expression_p (type)
23979 && cp_parser_non_integral_constant_expression (parser,
23980 NIC_CONSTRUCTOR))
23981 return error_mark_node;
23982 return cast;
23985 /* Save the tokens that make up the body of a member function defined
23986 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23987 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23988 specifiers applied to the declaration. Returns the FUNCTION_DECL
23989 for the member function. */
23991 static tree
23992 cp_parser_save_member_function_body (cp_parser* parser,
23993 cp_decl_specifier_seq *decl_specifiers,
23994 cp_declarator *declarator,
23995 tree attributes)
23997 cp_token *first;
23998 cp_token *last;
23999 tree fn;
24001 /* Create the FUNCTION_DECL. */
24002 fn = grokmethod (decl_specifiers, declarator, attributes);
24003 cp_finalize_omp_declare_simd (parser, fn);
24004 /* If something went badly wrong, bail out now. */
24005 if (fn == error_mark_node)
24007 /* If there's a function-body, skip it. */
24008 if (cp_parser_token_starts_function_definition_p
24009 (cp_lexer_peek_token (parser->lexer)))
24010 cp_parser_skip_to_end_of_block_or_statement (parser);
24011 return error_mark_node;
24014 /* Remember it, if there default args to post process. */
24015 cp_parser_save_default_args (parser, fn);
24017 /* Save away the tokens that make up the body of the
24018 function. */
24019 first = parser->lexer->next_token;
24020 /* Handle function try blocks. */
24021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24022 cp_lexer_consume_token (parser->lexer);
24023 /* We can have braced-init-list mem-initializers before the fn body. */
24024 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24026 cp_lexer_consume_token (parser->lexer);
24027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24029 /* cache_group will stop after an un-nested { } pair, too. */
24030 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24031 break;
24033 /* variadic mem-inits have ... after the ')'. */
24034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24035 cp_lexer_consume_token (parser->lexer);
24038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24039 /* Handle function try blocks. */
24040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24042 last = parser->lexer->next_token;
24044 /* Save away the inline definition; we will process it when the
24045 class is complete. */
24046 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24047 DECL_PENDING_INLINE_P (fn) = 1;
24049 /* We need to know that this was defined in the class, so that
24050 friend templates are handled correctly. */
24051 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24053 /* Add FN to the queue of functions to be parsed later. */
24054 vec_safe_push (unparsed_funs_with_definitions, fn);
24056 return fn;
24059 /* Save the tokens that make up the in-class initializer for a non-static
24060 data member. Returns a DEFAULT_ARG. */
24062 static tree
24063 cp_parser_save_nsdmi (cp_parser* parser)
24065 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24068 /* Parse a template-argument-list, as well as the trailing ">" (but
24069 not the opening "<"). See cp_parser_template_argument_list for the
24070 return value. */
24072 static tree
24073 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24075 tree arguments;
24076 tree saved_scope;
24077 tree saved_qualifying_scope;
24078 tree saved_object_scope;
24079 bool saved_greater_than_is_operator_p;
24080 int saved_unevaluated_operand;
24081 int saved_inhibit_evaluation_warnings;
24083 /* [temp.names]
24085 When parsing a template-id, the first non-nested `>' is taken as
24086 the end of the template-argument-list rather than a greater-than
24087 operator. */
24088 saved_greater_than_is_operator_p
24089 = parser->greater_than_is_operator_p;
24090 parser->greater_than_is_operator_p = false;
24091 /* Parsing the argument list may modify SCOPE, so we save it
24092 here. */
24093 saved_scope = parser->scope;
24094 saved_qualifying_scope = parser->qualifying_scope;
24095 saved_object_scope = parser->object_scope;
24096 /* We need to evaluate the template arguments, even though this
24097 template-id may be nested within a "sizeof". */
24098 saved_unevaluated_operand = cp_unevaluated_operand;
24099 cp_unevaluated_operand = 0;
24100 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24101 c_inhibit_evaluation_warnings = 0;
24102 /* Parse the template-argument-list itself. */
24103 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24104 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24105 arguments = NULL_TREE;
24106 else
24107 arguments = cp_parser_template_argument_list (parser);
24108 /* Look for the `>' that ends the template-argument-list. If we find
24109 a '>>' instead, it's probably just a typo. */
24110 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24112 if (cxx_dialect != cxx98)
24114 /* In C++0x, a `>>' in a template argument list or cast
24115 expression is considered to be two separate `>'
24116 tokens. So, change the current token to a `>', but don't
24117 consume it: it will be consumed later when the outer
24118 template argument list (or cast expression) is parsed.
24119 Note that this replacement of `>' for `>>' is necessary
24120 even if we are parsing tentatively: in the tentative
24121 case, after calling
24122 cp_parser_enclosed_template_argument_list we will always
24123 throw away all of the template arguments and the first
24124 closing `>', either because the template argument list
24125 was erroneous or because we are replacing those tokens
24126 with a CPP_TEMPLATE_ID token. The second `>' (which will
24127 not have been thrown away) is needed either to close an
24128 outer template argument list or to complete a new-style
24129 cast. */
24130 cp_token *token = cp_lexer_peek_token (parser->lexer);
24131 token->type = CPP_GREATER;
24133 else if (!saved_greater_than_is_operator_p)
24135 /* If we're in a nested template argument list, the '>>' has
24136 to be a typo for '> >'. We emit the error message, but we
24137 continue parsing and we push a '>' as next token, so that
24138 the argument list will be parsed correctly. Note that the
24139 global source location is still on the token before the
24140 '>>', so we need to say explicitly where we want it. */
24141 cp_token *token = cp_lexer_peek_token (parser->lexer);
24142 error_at (token->location, "%<>>%> should be %<> >%> "
24143 "within a nested template argument list");
24145 token->type = CPP_GREATER;
24147 else
24149 /* If this is not a nested template argument list, the '>>'
24150 is a typo for '>'. Emit an error message and continue.
24151 Same deal about the token location, but here we can get it
24152 right by consuming the '>>' before issuing the diagnostic. */
24153 cp_token *token = cp_lexer_consume_token (parser->lexer);
24154 error_at (token->location,
24155 "spurious %<>>%>, use %<>%> to terminate "
24156 "a template argument list");
24159 else
24160 cp_parser_skip_to_end_of_template_parameter_list (parser);
24161 /* The `>' token might be a greater-than operator again now. */
24162 parser->greater_than_is_operator_p
24163 = saved_greater_than_is_operator_p;
24164 /* Restore the SAVED_SCOPE. */
24165 parser->scope = saved_scope;
24166 parser->qualifying_scope = saved_qualifying_scope;
24167 parser->object_scope = saved_object_scope;
24168 cp_unevaluated_operand = saved_unevaluated_operand;
24169 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24171 return arguments;
24174 /* MEMBER_FUNCTION is a member function, or a friend. If default
24175 arguments, or the body of the function have not yet been parsed,
24176 parse them now. */
24178 static void
24179 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24181 timevar_push (TV_PARSE_INMETH);
24182 /* If this member is a template, get the underlying
24183 FUNCTION_DECL. */
24184 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24185 member_function = DECL_TEMPLATE_RESULT (member_function);
24187 /* There should not be any class definitions in progress at this
24188 point; the bodies of members are only parsed outside of all class
24189 definitions. */
24190 gcc_assert (parser->num_classes_being_defined == 0);
24191 /* While we're parsing the member functions we might encounter more
24192 classes. We want to handle them right away, but we don't want
24193 them getting mixed up with functions that are currently in the
24194 queue. */
24195 push_unparsed_function_queues (parser);
24197 /* Make sure that any template parameters are in scope. */
24198 maybe_begin_member_template_processing (member_function);
24200 /* If the body of the function has not yet been parsed, parse it
24201 now. */
24202 if (DECL_PENDING_INLINE_P (member_function))
24204 tree function_scope;
24205 cp_token_cache *tokens;
24207 /* The function is no longer pending; we are processing it. */
24208 tokens = DECL_PENDING_INLINE_INFO (member_function);
24209 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24210 DECL_PENDING_INLINE_P (member_function) = 0;
24212 /* If this is a local class, enter the scope of the containing
24213 function. */
24214 function_scope = current_function_decl;
24215 if (function_scope)
24216 push_function_context ();
24218 /* Push the body of the function onto the lexer stack. */
24219 cp_parser_push_lexer_for_tokens (parser, tokens);
24221 /* Let the front end know that we going to be defining this
24222 function. */
24223 start_preparsed_function (member_function, NULL_TREE,
24224 SF_PRE_PARSED | SF_INCLASS_INLINE);
24226 /* Don't do access checking if it is a templated function. */
24227 if (processing_template_decl)
24228 push_deferring_access_checks (dk_no_check);
24230 /* #pragma omp declare reduction needs special parsing. */
24231 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24233 parser->lexer->in_pragma = true;
24234 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24235 finish_function (/*inline*/2);
24236 cp_check_omp_declare_reduction (member_function);
24238 else
24239 /* Now, parse the body of the function. */
24240 cp_parser_function_definition_after_declarator (parser,
24241 /*inline_p=*/true);
24243 if (processing_template_decl)
24244 pop_deferring_access_checks ();
24246 /* Leave the scope of the containing function. */
24247 if (function_scope)
24248 pop_function_context ();
24249 cp_parser_pop_lexer (parser);
24252 /* Remove any template parameters from the symbol table. */
24253 maybe_end_member_template_processing ();
24255 /* Restore the queue. */
24256 pop_unparsed_function_queues (parser);
24257 timevar_pop (TV_PARSE_INMETH);
24260 /* If DECL contains any default args, remember it on the unparsed
24261 functions queue. */
24263 static void
24264 cp_parser_save_default_args (cp_parser* parser, tree decl)
24266 tree probe;
24268 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24269 probe;
24270 probe = TREE_CHAIN (probe))
24271 if (TREE_PURPOSE (probe))
24273 cp_default_arg_entry entry = {current_class_type, decl};
24274 vec_safe_push (unparsed_funs_with_default_args, entry);
24275 break;
24279 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24280 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24281 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24282 from the parameter-type-list. */
24284 static tree
24285 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24286 tree default_arg, tree parmtype)
24288 cp_token_cache *tokens;
24289 tree parsed_arg;
24290 bool dummy;
24292 if (default_arg == error_mark_node)
24293 return error_mark_node;
24295 /* Push the saved tokens for the default argument onto the parser's
24296 lexer stack. */
24297 tokens = DEFARG_TOKENS (default_arg);
24298 cp_parser_push_lexer_for_tokens (parser, tokens);
24300 start_lambda_scope (decl);
24302 /* Parse the default argument. */
24303 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24304 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24307 finish_lambda_scope ();
24309 if (parsed_arg == error_mark_node)
24310 cp_parser_skip_to_end_of_statement (parser);
24312 if (!processing_template_decl)
24314 /* In a non-template class, check conversions now. In a template,
24315 we'll wait and instantiate these as needed. */
24316 if (TREE_CODE (decl) == PARM_DECL)
24317 parsed_arg = check_default_argument (parmtype, parsed_arg,
24318 tf_warning_or_error);
24319 else
24320 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24323 /* If the token stream has not been completely used up, then
24324 there was extra junk after the end of the default
24325 argument. */
24326 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24328 if (TREE_CODE (decl) == PARM_DECL)
24329 cp_parser_error (parser, "expected %<,%>");
24330 else
24331 cp_parser_error (parser, "expected %<;%>");
24334 /* Revert to the main lexer. */
24335 cp_parser_pop_lexer (parser);
24337 return parsed_arg;
24340 /* FIELD is a non-static data member with an initializer which we saved for
24341 later; parse it now. */
24343 static void
24344 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24346 tree def;
24348 maybe_begin_member_template_processing (field);
24350 push_unparsed_function_queues (parser);
24351 def = cp_parser_late_parse_one_default_arg (parser, field,
24352 DECL_INITIAL (field),
24353 NULL_TREE);
24354 pop_unparsed_function_queues (parser);
24356 maybe_end_member_template_processing ();
24358 DECL_INITIAL (field) = def;
24361 /* FN is a FUNCTION_DECL which may contains a parameter with an
24362 unparsed DEFAULT_ARG. Parse the default args now. This function
24363 assumes that the current scope is the scope in which the default
24364 argument should be processed. */
24366 static void
24367 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24369 bool saved_local_variables_forbidden_p;
24370 tree parm, parmdecl;
24372 /* While we're parsing the default args, we might (due to the
24373 statement expression extension) encounter more classes. We want
24374 to handle them right away, but we don't want them getting mixed
24375 up with default args that are currently in the queue. */
24376 push_unparsed_function_queues (parser);
24378 /* Local variable names (and the `this' keyword) may not appear
24379 in a default argument. */
24380 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24381 parser->local_variables_forbidden_p = true;
24383 push_defarg_context (fn);
24385 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24386 parmdecl = DECL_ARGUMENTS (fn);
24387 parm && parm != void_list_node;
24388 parm = TREE_CHAIN (parm),
24389 parmdecl = DECL_CHAIN (parmdecl))
24391 tree default_arg = TREE_PURPOSE (parm);
24392 tree parsed_arg;
24393 vec<tree, va_gc> *insts;
24394 tree copy;
24395 unsigned ix;
24397 if (!default_arg)
24398 continue;
24400 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24401 /* This can happen for a friend declaration for a function
24402 already declared with default arguments. */
24403 continue;
24405 parsed_arg
24406 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24407 default_arg,
24408 TREE_VALUE (parm));
24409 if (parsed_arg == error_mark_node)
24411 continue;
24414 TREE_PURPOSE (parm) = parsed_arg;
24416 /* Update any instantiations we've already created. */
24417 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24418 vec_safe_iterate (insts, ix, &copy); ix++)
24419 TREE_PURPOSE (copy) = parsed_arg;
24422 pop_defarg_context ();
24424 /* Make sure no default arg is missing. */
24425 check_default_args (fn);
24427 /* Restore the state of local_variables_forbidden_p. */
24428 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24430 /* Restore the queue. */
24431 pop_unparsed_function_queues (parser);
24434 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24436 sizeof ... ( identifier )
24438 where the 'sizeof' token has already been consumed. */
24440 static tree
24441 cp_parser_sizeof_pack (cp_parser *parser)
24443 /* Consume the `...'. */
24444 cp_lexer_consume_token (parser->lexer);
24445 maybe_warn_variadic_templates ();
24447 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24448 if (paren)
24449 cp_lexer_consume_token (parser->lexer);
24450 else
24451 permerror (cp_lexer_peek_token (parser->lexer)->location,
24452 "%<sizeof...%> argument must be surrounded by parentheses");
24454 cp_token *token = cp_lexer_peek_token (parser->lexer);
24455 tree name = cp_parser_identifier (parser);
24456 if (name == error_mark_node)
24457 return error_mark_node;
24458 /* The name is not qualified. */
24459 parser->scope = NULL_TREE;
24460 parser->qualifying_scope = NULL_TREE;
24461 parser->object_scope = NULL_TREE;
24462 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24463 if (expr == error_mark_node)
24464 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24465 token->location);
24466 if (TREE_CODE (expr) == TYPE_DECL)
24467 expr = TREE_TYPE (expr);
24468 else if (TREE_CODE (expr) == CONST_DECL)
24469 expr = DECL_INITIAL (expr);
24470 expr = make_pack_expansion (expr);
24472 if (paren)
24473 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24475 return expr;
24478 /* Parse the operand of `sizeof' (or a similar operator). Returns
24479 either a TYPE or an expression, depending on the form of the
24480 input. The KEYWORD indicates which kind of expression we have
24481 encountered. */
24483 static tree
24484 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24486 tree expr = NULL_TREE;
24487 const char *saved_message;
24488 char *tmp;
24489 bool saved_integral_constant_expression_p;
24490 bool saved_non_integral_constant_expression_p;
24492 /* If it's a `...', then we are computing the length of a parameter
24493 pack. */
24494 if (keyword == RID_SIZEOF
24495 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24496 return cp_parser_sizeof_pack (parser);
24498 /* Types cannot be defined in a `sizeof' expression. Save away the
24499 old message. */
24500 saved_message = parser->type_definition_forbidden_message;
24501 /* And create the new one. */
24502 tmp = concat ("types may not be defined in %<",
24503 IDENTIFIER_POINTER (ridpointers[keyword]),
24504 "%> expressions", NULL);
24505 parser->type_definition_forbidden_message = tmp;
24507 /* The restrictions on constant-expressions do not apply inside
24508 sizeof expressions. */
24509 saved_integral_constant_expression_p
24510 = parser->integral_constant_expression_p;
24511 saved_non_integral_constant_expression_p
24512 = parser->non_integral_constant_expression_p;
24513 parser->integral_constant_expression_p = false;
24515 /* Do not actually evaluate the expression. */
24516 ++cp_unevaluated_operand;
24517 ++c_inhibit_evaluation_warnings;
24518 /* If it's a `(', then we might be looking at the type-id
24519 construction. */
24520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24522 tree type = NULL_TREE;
24524 /* We can't be sure yet whether we're looking at a type-id or an
24525 expression. */
24526 cp_parser_parse_tentatively (parser);
24527 /* Note: as a GNU Extension, compound literals are considered
24528 postfix-expressions as they are in C99, so they are valid
24529 arguments to sizeof. See comment in cp_parser_cast_expression
24530 for details. */
24531 if (cp_parser_compound_literal_p (parser))
24532 cp_parser_simulate_error (parser);
24533 else
24535 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24536 parser->in_type_id_in_expr_p = true;
24537 /* Look for the type-id. */
24538 type = cp_parser_type_id (parser);
24539 /* Look for the closing `)'. */
24540 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24541 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24544 /* If all went well, then we're done. */
24545 if (cp_parser_parse_definitely (parser))
24547 cp_decl_specifier_seq decl_specs;
24549 /* Build a trivial decl-specifier-seq. */
24550 clear_decl_specs (&decl_specs);
24551 decl_specs.type = type;
24553 /* Call grokdeclarator to figure out what type this is. */
24554 expr = grokdeclarator (NULL,
24555 &decl_specs,
24556 TYPENAME,
24557 /*initialized=*/0,
24558 /*attrlist=*/NULL);
24562 /* If the type-id production did not work out, then we must be
24563 looking at the unary-expression production. */
24564 if (!expr)
24565 expr = cp_parser_unary_expression (parser);
24567 /* Go back to evaluating expressions. */
24568 --cp_unevaluated_operand;
24569 --c_inhibit_evaluation_warnings;
24571 /* Free the message we created. */
24572 free (tmp);
24573 /* And restore the old one. */
24574 parser->type_definition_forbidden_message = saved_message;
24575 parser->integral_constant_expression_p
24576 = saved_integral_constant_expression_p;
24577 parser->non_integral_constant_expression_p
24578 = saved_non_integral_constant_expression_p;
24580 return expr;
24583 /* If the current declaration has no declarator, return true. */
24585 static bool
24586 cp_parser_declares_only_class_p (cp_parser *parser)
24588 /* If the next token is a `;' or a `,' then there is no
24589 declarator. */
24590 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24591 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24594 /* Update the DECL_SPECS to reflect the storage class indicated by
24595 KEYWORD. */
24597 static void
24598 cp_parser_set_storage_class (cp_parser *parser,
24599 cp_decl_specifier_seq *decl_specs,
24600 enum rid keyword,
24601 cp_token *token)
24603 cp_storage_class storage_class;
24605 if (parser->in_unbraced_linkage_specification_p)
24607 error_at (token->location, "invalid use of %qD in linkage specification",
24608 ridpointers[keyword]);
24609 return;
24611 else if (decl_specs->storage_class != sc_none)
24613 decl_specs->conflicting_specifiers_p = true;
24614 return;
24617 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24618 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24619 && decl_specs->gnu_thread_keyword_p)
24621 pedwarn (decl_specs->locations[ds_thread], 0,
24622 "%<__thread%> before %qD", ridpointers[keyword]);
24625 switch (keyword)
24627 case RID_AUTO:
24628 storage_class = sc_auto;
24629 break;
24630 case RID_REGISTER:
24631 storage_class = sc_register;
24632 break;
24633 case RID_STATIC:
24634 storage_class = sc_static;
24635 break;
24636 case RID_EXTERN:
24637 storage_class = sc_extern;
24638 break;
24639 case RID_MUTABLE:
24640 storage_class = sc_mutable;
24641 break;
24642 default:
24643 gcc_unreachable ();
24645 decl_specs->storage_class = storage_class;
24646 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24648 /* A storage class specifier cannot be applied alongside a typedef
24649 specifier. If there is a typedef specifier present then set
24650 conflicting_specifiers_p which will trigger an error later
24651 on in grokdeclarator. */
24652 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24653 decl_specs->conflicting_specifiers_p = true;
24656 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24657 is true, the type is a class or enum definition. */
24659 static void
24660 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24661 tree type_spec,
24662 cp_token *token,
24663 bool type_definition_p)
24665 decl_specs->any_specifiers_p = true;
24667 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24668 (with, for example, in "typedef int wchar_t;") we remember that
24669 this is what happened. In system headers, we ignore these
24670 declarations so that G++ can work with system headers that are not
24671 C++-safe. */
24672 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24673 && !type_definition_p
24674 && (type_spec == boolean_type_node
24675 || type_spec == char16_type_node
24676 || type_spec == char32_type_node
24677 || type_spec == wchar_type_node)
24678 && (decl_specs->type
24679 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24680 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24681 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24682 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24684 decl_specs->redefined_builtin_type = type_spec;
24685 set_and_check_decl_spec_loc (decl_specs,
24686 ds_redefined_builtin_type_spec,
24687 token);
24688 if (!decl_specs->type)
24690 decl_specs->type = type_spec;
24691 decl_specs->type_definition_p = false;
24692 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24695 else if (decl_specs->type)
24696 decl_specs->multiple_types_p = true;
24697 else
24699 decl_specs->type = type_spec;
24700 decl_specs->type_definition_p = type_definition_p;
24701 decl_specs->redefined_builtin_type = NULL_TREE;
24702 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24706 /* True iff TOKEN is the GNU keyword __thread. */
24708 static bool
24709 token_is__thread (cp_token *token)
24711 gcc_assert (token->keyword == RID_THREAD);
24712 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24715 /* Set the location for a declarator specifier and check if it is
24716 duplicated.
24718 DECL_SPECS is the sequence of declarator specifiers onto which to
24719 set the location.
24721 DS is the single declarator specifier to set which location is to
24722 be set onto the existing sequence of declarators.
24724 LOCATION is the location for the declarator specifier to
24725 consider. */
24727 static void
24728 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24729 cp_decl_spec ds, cp_token *token)
24731 gcc_assert (ds < ds_last);
24733 if (decl_specs == NULL)
24734 return;
24736 source_location location = token->location;
24738 if (decl_specs->locations[ds] == 0)
24740 decl_specs->locations[ds] = location;
24741 if (ds == ds_thread)
24742 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24744 else
24746 if (ds == ds_long)
24748 if (decl_specs->locations[ds_long_long] != 0)
24749 error_at (location,
24750 "%<long long long%> is too long for GCC");
24751 else
24753 decl_specs->locations[ds_long_long] = location;
24754 pedwarn_cxx98 (location,
24755 OPT_Wlong_long,
24756 "ISO C++ 1998 does not support %<long long%>");
24759 else if (ds == ds_thread)
24761 bool gnu = token_is__thread (token);
24762 if (gnu != decl_specs->gnu_thread_keyword_p)
24763 error_at (location,
24764 "both %<__thread%> and %<thread_local%> specified");
24765 else
24766 error_at (location, "duplicate %qD", token->u.value);
24768 else
24770 static const char *const decl_spec_names[] = {
24771 "signed",
24772 "unsigned",
24773 "short",
24774 "long",
24775 "const",
24776 "volatile",
24777 "restrict",
24778 "inline",
24779 "virtual",
24780 "explicit",
24781 "friend",
24782 "typedef",
24783 "using",
24784 "constexpr",
24785 "__complex"
24787 error_at (location,
24788 "duplicate %qs", decl_spec_names[ds]);
24793 /* Return true iff the declarator specifier DS is present in the
24794 sequence of declarator specifiers DECL_SPECS. */
24796 bool
24797 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24798 cp_decl_spec ds)
24800 gcc_assert (ds < ds_last);
24802 if (decl_specs == NULL)
24803 return false;
24805 return decl_specs->locations[ds] != 0;
24808 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24809 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24811 static bool
24812 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24814 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24817 /* Issue an error message indicating that TOKEN_DESC was expected.
24818 If KEYWORD is true, it indicated this function is called by
24819 cp_parser_require_keword and the required token can only be
24820 a indicated keyword. */
24822 static void
24823 cp_parser_required_error (cp_parser *parser,
24824 required_token token_desc,
24825 bool keyword)
24827 switch (token_desc)
24829 case RT_NEW:
24830 cp_parser_error (parser, "expected %<new%>");
24831 return;
24832 case RT_DELETE:
24833 cp_parser_error (parser, "expected %<delete%>");
24834 return;
24835 case RT_RETURN:
24836 cp_parser_error (parser, "expected %<return%>");
24837 return;
24838 case RT_WHILE:
24839 cp_parser_error (parser, "expected %<while%>");
24840 return;
24841 case RT_EXTERN:
24842 cp_parser_error (parser, "expected %<extern%>");
24843 return;
24844 case RT_STATIC_ASSERT:
24845 cp_parser_error (parser, "expected %<static_assert%>");
24846 return;
24847 case RT_DECLTYPE:
24848 cp_parser_error (parser, "expected %<decltype%>");
24849 return;
24850 case RT_OPERATOR:
24851 cp_parser_error (parser, "expected %<operator%>");
24852 return;
24853 case RT_CLASS:
24854 cp_parser_error (parser, "expected %<class%>");
24855 return;
24856 case RT_TEMPLATE:
24857 cp_parser_error (parser, "expected %<template%>");
24858 return;
24859 case RT_NAMESPACE:
24860 cp_parser_error (parser, "expected %<namespace%>");
24861 return;
24862 case RT_USING:
24863 cp_parser_error (parser, "expected %<using%>");
24864 return;
24865 case RT_ASM:
24866 cp_parser_error (parser, "expected %<asm%>");
24867 return;
24868 case RT_TRY:
24869 cp_parser_error (parser, "expected %<try%>");
24870 return;
24871 case RT_CATCH:
24872 cp_parser_error (parser, "expected %<catch%>");
24873 return;
24874 case RT_THROW:
24875 cp_parser_error (parser, "expected %<throw%>");
24876 return;
24877 case RT_LABEL:
24878 cp_parser_error (parser, "expected %<__label__%>");
24879 return;
24880 case RT_AT_TRY:
24881 cp_parser_error (parser, "expected %<@try%>");
24882 return;
24883 case RT_AT_SYNCHRONIZED:
24884 cp_parser_error (parser, "expected %<@synchronized%>");
24885 return;
24886 case RT_AT_THROW:
24887 cp_parser_error (parser, "expected %<@throw%>");
24888 return;
24889 case RT_TRANSACTION_ATOMIC:
24890 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24891 return;
24892 case RT_TRANSACTION_RELAXED:
24893 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24894 return;
24895 default:
24896 break;
24898 if (!keyword)
24900 switch (token_desc)
24902 case RT_SEMICOLON:
24903 cp_parser_error (parser, "expected %<;%>");
24904 return;
24905 case RT_OPEN_PAREN:
24906 cp_parser_error (parser, "expected %<(%>");
24907 return;
24908 case RT_CLOSE_BRACE:
24909 cp_parser_error (parser, "expected %<}%>");
24910 return;
24911 case RT_OPEN_BRACE:
24912 cp_parser_error (parser, "expected %<{%>");
24913 return;
24914 case RT_CLOSE_SQUARE:
24915 cp_parser_error (parser, "expected %<]%>");
24916 return;
24917 case RT_OPEN_SQUARE:
24918 cp_parser_error (parser, "expected %<[%>");
24919 return;
24920 case RT_COMMA:
24921 cp_parser_error (parser, "expected %<,%>");
24922 return;
24923 case RT_SCOPE:
24924 cp_parser_error (parser, "expected %<::%>");
24925 return;
24926 case RT_LESS:
24927 cp_parser_error (parser, "expected %<<%>");
24928 return;
24929 case RT_GREATER:
24930 cp_parser_error (parser, "expected %<>%>");
24931 return;
24932 case RT_EQ:
24933 cp_parser_error (parser, "expected %<=%>");
24934 return;
24935 case RT_ELLIPSIS:
24936 cp_parser_error (parser, "expected %<...%>");
24937 return;
24938 case RT_MULT:
24939 cp_parser_error (parser, "expected %<*%>");
24940 return;
24941 case RT_COMPL:
24942 cp_parser_error (parser, "expected %<~%>");
24943 return;
24944 case RT_COLON:
24945 cp_parser_error (parser, "expected %<:%>");
24946 return;
24947 case RT_COLON_SCOPE:
24948 cp_parser_error (parser, "expected %<:%> or %<::%>");
24949 return;
24950 case RT_CLOSE_PAREN:
24951 cp_parser_error (parser, "expected %<)%>");
24952 return;
24953 case RT_COMMA_CLOSE_PAREN:
24954 cp_parser_error (parser, "expected %<,%> or %<)%>");
24955 return;
24956 case RT_PRAGMA_EOL:
24957 cp_parser_error (parser, "expected end of line");
24958 return;
24959 case RT_NAME:
24960 cp_parser_error (parser, "expected identifier");
24961 return;
24962 case RT_SELECT:
24963 cp_parser_error (parser, "expected selection-statement");
24964 return;
24965 case RT_INTERATION:
24966 cp_parser_error (parser, "expected iteration-statement");
24967 return;
24968 case RT_JUMP:
24969 cp_parser_error (parser, "expected jump-statement");
24970 return;
24971 case RT_CLASS_KEY:
24972 cp_parser_error (parser, "expected class-key");
24973 return;
24974 case RT_CLASS_TYPENAME_TEMPLATE:
24975 cp_parser_error (parser,
24976 "expected %<class%>, %<typename%>, or %<template%>");
24977 return;
24978 default:
24979 gcc_unreachable ();
24982 else
24983 gcc_unreachable ();
24988 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24989 issue an error message indicating that TOKEN_DESC was expected.
24991 Returns the token consumed, if the token had the appropriate type.
24992 Otherwise, returns NULL. */
24994 static cp_token *
24995 cp_parser_require (cp_parser* parser,
24996 enum cpp_ttype type,
24997 required_token token_desc)
24999 if (cp_lexer_next_token_is (parser->lexer, type))
25000 return cp_lexer_consume_token (parser->lexer);
25001 else
25003 /* Output the MESSAGE -- unless we're parsing tentatively. */
25004 if (!cp_parser_simulate_error (parser))
25005 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25006 return NULL;
25010 /* An error message is produced if the next token is not '>'.
25011 All further tokens are skipped until the desired token is
25012 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25014 static void
25015 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25017 /* Current level of '< ... >'. */
25018 unsigned level = 0;
25019 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25020 unsigned nesting_depth = 0;
25022 /* Are we ready, yet? If not, issue error message. */
25023 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25024 return;
25026 /* Skip tokens until the desired token is found. */
25027 while (true)
25029 /* Peek at the next token. */
25030 switch (cp_lexer_peek_token (parser->lexer)->type)
25032 case CPP_LESS:
25033 if (!nesting_depth)
25034 ++level;
25035 break;
25037 case CPP_RSHIFT:
25038 if (cxx_dialect == cxx98)
25039 /* C++0x views the `>>' operator as two `>' tokens, but
25040 C++98 does not. */
25041 break;
25042 else if (!nesting_depth && level-- == 0)
25044 /* We've hit a `>>' where the first `>' closes the
25045 template argument list, and the second `>' is
25046 spurious. Just consume the `>>' and stop; we've
25047 already produced at least one error. */
25048 cp_lexer_consume_token (parser->lexer);
25049 return;
25051 /* Fall through for C++0x, so we handle the second `>' in
25052 the `>>'. */
25054 case CPP_GREATER:
25055 if (!nesting_depth && level-- == 0)
25057 /* We've reached the token we want, consume it and stop. */
25058 cp_lexer_consume_token (parser->lexer);
25059 return;
25061 break;
25063 case CPP_OPEN_PAREN:
25064 case CPP_OPEN_SQUARE:
25065 ++nesting_depth;
25066 break;
25068 case CPP_CLOSE_PAREN:
25069 case CPP_CLOSE_SQUARE:
25070 if (nesting_depth-- == 0)
25071 return;
25072 break;
25074 case CPP_EOF:
25075 case CPP_PRAGMA_EOL:
25076 case CPP_SEMICOLON:
25077 case CPP_OPEN_BRACE:
25078 case CPP_CLOSE_BRACE:
25079 /* The '>' was probably forgotten, don't look further. */
25080 return;
25082 default:
25083 break;
25086 /* Consume this token. */
25087 cp_lexer_consume_token (parser->lexer);
25091 /* If the next token is the indicated keyword, consume it. Otherwise,
25092 issue an error message indicating that TOKEN_DESC was expected.
25094 Returns the token consumed, if the token had the appropriate type.
25095 Otherwise, returns NULL. */
25097 static cp_token *
25098 cp_parser_require_keyword (cp_parser* parser,
25099 enum rid keyword,
25100 required_token token_desc)
25102 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25104 if (token && token->keyword != keyword)
25106 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25107 return NULL;
25110 return token;
25113 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25114 function-definition. */
25116 static bool
25117 cp_parser_token_starts_function_definition_p (cp_token* token)
25119 return (/* An ordinary function-body begins with an `{'. */
25120 token->type == CPP_OPEN_BRACE
25121 /* A ctor-initializer begins with a `:'. */
25122 || token->type == CPP_COLON
25123 /* A function-try-block begins with `try'. */
25124 || token->keyword == RID_TRY
25125 /* A function-transaction-block begins with `__transaction_atomic'
25126 or `__transaction_relaxed'. */
25127 || token->keyword == RID_TRANSACTION_ATOMIC
25128 || token->keyword == RID_TRANSACTION_RELAXED
25129 /* The named return value extension begins with `return'. */
25130 || token->keyword == RID_RETURN);
25133 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25134 definition. */
25136 static bool
25137 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25139 cp_token *token;
25141 token = cp_lexer_peek_token (parser->lexer);
25142 return (token->type == CPP_OPEN_BRACE
25143 || (token->type == CPP_COLON
25144 && !parser->colon_doesnt_start_class_def_p));
25147 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25148 C++0x) ending a template-argument. */
25150 static bool
25151 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25153 cp_token *token;
25155 token = cp_lexer_peek_token (parser->lexer);
25156 return (token->type == CPP_COMMA
25157 || token->type == CPP_GREATER
25158 || token->type == CPP_ELLIPSIS
25159 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25162 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25163 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25165 static bool
25166 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25167 size_t n)
25169 cp_token *token;
25171 token = cp_lexer_peek_nth_token (parser->lexer, n);
25172 if (token->type == CPP_LESS)
25173 return true;
25174 /* Check for the sequence `<::' in the original code. It would be lexed as
25175 `[:', where `[' is a digraph, and there is no whitespace before
25176 `:'. */
25177 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25179 cp_token *token2;
25180 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25181 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25182 return true;
25184 return false;
25187 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25188 or none_type otherwise. */
25190 static enum tag_types
25191 cp_parser_token_is_class_key (cp_token* token)
25193 switch (token->keyword)
25195 case RID_CLASS:
25196 return class_type;
25197 case RID_STRUCT:
25198 return record_type;
25199 case RID_UNION:
25200 return union_type;
25202 default:
25203 return none_type;
25207 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25208 or none_type otherwise or if the token is null. */
25210 static enum tag_types
25211 cp_parser_token_is_type_parameter_key (cp_token* token)
25213 if (!token)
25214 return none_type;
25216 switch (token->keyword)
25218 case RID_CLASS:
25219 return class_type;
25220 case RID_TYPENAME:
25221 return typename_type;
25223 default:
25224 return none_type;
25228 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25230 static void
25231 cp_parser_check_class_key (enum tag_types class_key, tree type)
25233 if (type == error_mark_node)
25234 return;
25235 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25237 if (permerror (input_location, "%qs tag used in naming %q#T",
25238 class_key == union_type ? "union"
25239 : class_key == record_type ? "struct" : "class",
25240 type))
25241 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25242 "%q#T was previously declared here", type);
25246 /* Issue an error message if DECL is redeclared with different
25247 access than its original declaration [class.access.spec/3].
25248 This applies to nested classes and nested class templates.
25249 [class.mem/1]. */
25251 static void
25252 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25254 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25255 return;
25257 if ((TREE_PRIVATE (decl)
25258 != (current_access_specifier == access_private_node))
25259 || (TREE_PROTECTED (decl)
25260 != (current_access_specifier == access_protected_node)))
25261 error_at (location, "%qD redeclared with different access", decl);
25264 /* Look for the `template' keyword, as a syntactic disambiguator.
25265 Return TRUE iff it is present, in which case it will be
25266 consumed. */
25268 static bool
25269 cp_parser_optional_template_keyword (cp_parser *parser)
25271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25273 /* In C++98 the `template' keyword can only be used within templates;
25274 outside templates the parser can always figure out what is a
25275 template and what is not. In C++11, per the resolution of DR 468,
25276 `template' is allowed in cases where it is not strictly necessary. */
25277 if (!processing_template_decl
25278 && pedantic && cxx_dialect == cxx98)
25280 cp_token *token = cp_lexer_peek_token (parser->lexer);
25281 pedwarn (token->location, OPT_Wpedantic,
25282 "in C++98 %<template%> (as a disambiguator) is only "
25283 "allowed within templates");
25284 /* If this part of the token stream is rescanned, the same
25285 error message would be generated. So, we purge the token
25286 from the stream. */
25287 cp_lexer_purge_token (parser->lexer);
25288 return false;
25290 else
25292 /* Consume the `template' keyword. */
25293 cp_lexer_consume_token (parser->lexer);
25294 return true;
25297 return false;
25300 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25301 set PARSER->SCOPE, and perform other related actions. */
25303 static void
25304 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25306 int i;
25307 struct tree_check *check_value;
25308 deferred_access_check *chk;
25309 vec<deferred_access_check, va_gc> *checks;
25311 /* Get the stored value. */
25312 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25313 /* Perform any access checks that were deferred. */
25314 checks = check_value->checks;
25315 if (checks)
25317 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25318 perform_or_defer_access_check (chk->binfo,
25319 chk->decl,
25320 chk->diag_decl, tf_warning_or_error);
25322 /* Set the scope from the stored value. */
25323 parser->scope = check_value->value;
25324 parser->qualifying_scope = check_value->qualifying_scope;
25325 parser->object_scope = NULL_TREE;
25328 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25329 encounter the end of a block before what we were looking for. */
25331 static bool
25332 cp_parser_cache_group (cp_parser *parser,
25333 enum cpp_ttype end,
25334 unsigned depth)
25336 while (true)
25338 cp_token *token = cp_lexer_peek_token (parser->lexer);
25340 /* Abort a parenthesized expression if we encounter a semicolon. */
25341 if ((end == CPP_CLOSE_PAREN || depth == 0)
25342 && token->type == CPP_SEMICOLON)
25343 return true;
25344 /* If we've reached the end of the file, stop. */
25345 if (token->type == CPP_EOF
25346 || (end != CPP_PRAGMA_EOL
25347 && token->type == CPP_PRAGMA_EOL))
25348 return true;
25349 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25350 /* We've hit the end of an enclosing block, so there's been some
25351 kind of syntax error. */
25352 return true;
25354 /* Consume the token. */
25355 cp_lexer_consume_token (parser->lexer);
25356 /* See if it starts a new group. */
25357 if (token->type == CPP_OPEN_BRACE)
25359 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25360 /* In theory this should probably check end == '}', but
25361 cp_parser_save_member_function_body needs it to exit
25362 after either '}' or ')' when called with ')'. */
25363 if (depth == 0)
25364 return false;
25366 else if (token->type == CPP_OPEN_PAREN)
25368 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25369 if (depth == 0 && end == CPP_CLOSE_PAREN)
25370 return false;
25372 else if (token->type == CPP_PRAGMA)
25373 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25374 else if (token->type == end)
25375 return false;
25379 /* Like above, for caching a default argument or NSDMI. Both of these are
25380 terminated by a non-nested comma, but it can be unclear whether or not a
25381 comma is nested in a template argument list unless we do more parsing.
25382 In order to handle this ambiguity, when we encounter a ',' after a '<'
25383 we try to parse what follows as a parameter-declaration-list (in the
25384 case of a default argument) or a member-declarator (in the case of an
25385 NSDMI). If that succeeds, then we stop caching. */
25387 static tree
25388 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25390 unsigned depth = 0;
25391 int maybe_template_id = 0;
25392 cp_token *first_token;
25393 cp_token *token;
25394 tree default_argument;
25396 /* Add tokens until we have processed the entire default
25397 argument. We add the range [first_token, token). */
25398 first_token = cp_lexer_peek_token (parser->lexer);
25399 if (first_token->type == CPP_OPEN_BRACE)
25401 /* For list-initialization, this is straightforward. */
25402 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25403 token = cp_lexer_peek_token (parser->lexer);
25405 else while (true)
25407 bool done = false;
25409 /* Peek at the next token. */
25410 token = cp_lexer_peek_token (parser->lexer);
25411 /* What we do depends on what token we have. */
25412 switch (token->type)
25414 /* In valid code, a default argument must be
25415 immediately followed by a `,' `)', or `...'. */
25416 case CPP_COMMA:
25417 if (depth == 0 && maybe_template_id)
25419 /* If we've seen a '<', we might be in a
25420 template-argument-list. Until Core issue 325 is
25421 resolved, we don't know how this situation ought
25422 to be handled, so try to DTRT. We check whether
25423 what comes after the comma is a valid parameter
25424 declaration list. If it is, then the comma ends
25425 the default argument; otherwise the default
25426 argument continues. */
25427 bool error = false;
25428 cp_token *peek;
25430 /* Set ITALP so cp_parser_parameter_declaration_list
25431 doesn't decide to commit to this parse. */
25432 bool saved_italp = parser->in_template_argument_list_p;
25433 parser->in_template_argument_list_p = true;
25435 cp_parser_parse_tentatively (parser);
25437 if (nsdmi)
25439 /* Parse declarators until we reach a non-comma or
25440 somthing that cannot be an initializer.
25441 Just checking whether we're looking at a single
25442 declarator is insufficient. Consider:
25443 int var = tuple<T,U>::x;
25444 The template parameter 'U' looks exactly like a
25445 declarator. */
25448 int ctor_dtor_or_conv_p;
25449 cp_lexer_consume_token (parser->lexer);
25450 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25451 &ctor_dtor_or_conv_p,
25452 /*parenthesized_p=*/NULL,
25453 /*member_p=*/true,
25454 /*friend_p=*/false);
25455 peek = cp_lexer_peek_token (parser->lexer);
25456 if (cp_parser_error_occurred (parser))
25457 break;
25459 while (peek->type == CPP_COMMA);
25460 /* If we met an '=' or ';' then the original comma
25461 was the end of the NSDMI. Otherwise assume
25462 we're still in the NSDMI. */
25463 error = (peek->type != CPP_EQ
25464 && peek->type != CPP_SEMICOLON);
25466 else
25468 cp_lexer_consume_token (parser->lexer);
25469 begin_scope (sk_function_parms, NULL_TREE);
25470 cp_parser_parameter_declaration_list (parser, &error);
25471 pop_bindings_and_leave_scope ();
25473 if (!cp_parser_error_occurred (parser) && !error)
25474 done = true;
25475 cp_parser_abort_tentative_parse (parser);
25477 parser->in_template_argument_list_p = saved_italp;
25478 break;
25480 case CPP_CLOSE_PAREN:
25481 case CPP_ELLIPSIS:
25482 /* If we run into a non-nested `;', `}', or `]',
25483 then the code is invalid -- but the default
25484 argument is certainly over. */
25485 case CPP_SEMICOLON:
25486 case CPP_CLOSE_BRACE:
25487 case CPP_CLOSE_SQUARE:
25488 if (depth == 0
25489 /* Handle correctly int n = sizeof ... ( p ); */
25490 && token->type != CPP_ELLIPSIS)
25491 done = true;
25492 /* Update DEPTH, if necessary. */
25493 else if (token->type == CPP_CLOSE_PAREN
25494 || token->type == CPP_CLOSE_BRACE
25495 || token->type == CPP_CLOSE_SQUARE)
25496 --depth;
25497 break;
25499 case CPP_OPEN_PAREN:
25500 case CPP_OPEN_SQUARE:
25501 case CPP_OPEN_BRACE:
25502 ++depth;
25503 break;
25505 case CPP_LESS:
25506 if (depth == 0)
25507 /* This might be the comparison operator, or it might
25508 start a template argument list. */
25509 ++maybe_template_id;
25510 break;
25512 case CPP_RSHIFT:
25513 if (cxx_dialect == cxx98)
25514 break;
25515 /* Fall through for C++0x, which treats the `>>'
25516 operator like two `>' tokens in certain
25517 cases. */
25519 case CPP_GREATER:
25520 if (depth == 0)
25522 /* This might be an operator, or it might close a
25523 template argument list. But if a previous '<'
25524 started a template argument list, this will have
25525 closed it, so we can't be in one anymore. */
25526 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25527 if (maybe_template_id < 0)
25528 maybe_template_id = 0;
25530 break;
25532 /* If we run out of tokens, issue an error message. */
25533 case CPP_EOF:
25534 case CPP_PRAGMA_EOL:
25535 error_at (token->location, "file ends in default argument");
25536 done = true;
25537 break;
25539 case CPP_NAME:
25540 case CPP_SCOPE:
25541 /* In these cases, we should look for template-ids.
25542 For example, if the default argument is
25543 `X<int, double>()', we need to do name lookup to
25544 figure out whether or not `X' is a template; if
25545 so, the `,' does not end the default argument.
25547 That is not yet done. */
25548 break;
25550 default:
25551 break;
25554 /* If we've reached the end, stop. */
25555 if (done)
25556 break;
25558 /* Add the token to the token block. */
25559 token = cp_lexer_consume_token (parser->lexer);
25562 /* Create a DEFAULT_ARG to represent the unparsed default
25563 argument. */
25564 default_argument = make_node (DEFAULT_ARG);
25565 DEFARG_TOKENS (default_argument)
25566 = cp_token_cache_new (first_token, token);
25567 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25569 return default_argument;
25572 /* Begin parsing tentatively. We always save tokens while parsing
25573 tentatively so that if the tentative parsing fails we can restore the
25574 tokens. */
25576 static void
25577 cp_parser_parse_tentatively (cp_parser* parser)
25579 /* Enter a new parsing context. */
25580 parser->context = cp_parser_context_new (parser->context);
25581 /* Begin saving tokens. */
25582 cp_lexer_save_tokens (parser->lexer);
25583 /* In order to avoid repetitive access control error messages,
25584 access checks are queued up until we are no longer parsing
25585 tentatively. */
25586 push_deferring_access_checks (dk_deferred);
25589 /* Commit to the currently active tentative parse. */
25591 static void
25592 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25594 cp_parser_context *context;
25595 cp_lexer *lexer;
25597 /* Mark all of the levels as committed. */
25598 lexer = parser->lexer;
25599 for (context = parser->context; context->next; context = context->next)
25601 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25602 break;
25603 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25604 while (!cp_lexer_saving_tokens (lexer))
25605 lexer = lexer->next;
25606 cp_lexer_commit_tokens (lexer);
25610 /* Commit to the topmost currently active tentative parse.
25612 Note that this function shouldn't be called when there are
25613 irreversible side-effects while in a tentative state. For
25614 example, we shouldn't create a permanent entry in the symbol
25615 table, or issue an error message that might not apply if the
25616 tentative parse is aborted. */
25618 static void
25619 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25621 cp_parser_context *context = parser->context;
25622 cp_lexer *lexer = parser->lexer;
25624 if (context)
25626 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25627 return;
25628 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25630 while (!cp_lexer_saving_tokens (lexer))
25631 lexer = lexer->next;
25632 cp_lexer_commit_tokens (lexer);
25636 /* Abort the currently active tentative parse. All consumed tokens
25637 will be rolled back, and no diagnostics will be issued. */
25639 static void
25640 cp_parser_abort_tentative_parse (cp_parser* parser)
25642 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25643 || errorcount > 0);
25644 cp_parser_simulate_error (parser);
25645 /* Now, pretend that we want to see if the construct was
25646 successfully parsed. */
25647 cp_parser_parse_definitely (parser);
25650 /* Stop parsing tentatively. If a parse error has occurred, restore the
25651 token stream. Otherwise, commit to the tokens we have consumed.
25652 Returns true if no error occurred; false otherwise. */
25654 static bool
25655 cp_parser_parse_definitely (cp_parser* parser)
25657 bool error_occurred;
25658 cp_parser_context *context;
25660 /* Remember whether or not an error occurred, since we are about to
25661 destroy that information. */
25662 error_occurred = cp_parser_error_occurred (parser);
25663 /* Remove the topmost context from the stack. */
25664 context = parser->context;
25665 parser->context = context->next;
25666 /* If no parse errors occurred, commit to the tentative parse. */
25667 if (!error_occurred)
25669 /* Commit to the tokens read tentatively, unless that was
25670 already done. */
25671 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25672 cp_lexer_commit_tokens (parser->lexer);
25674 pop_to_parent_deferring_access_checks ();
25676 /* Otherwise, if errors occurred, roll back our state so that things
25677 are just as they were before we began the tentative parse. */
25678 else
25680 cp_lexer_rollback_tokens (parser->lexer);
25681 pop_deferring_access_checks ();
25683 /* Add the context to the front of the free list. */
25684 context->next = cp_parser_context_free_list;
25685 cp_parser_context_free_list = context;
25687 return !error_occurred;
25690 /* Returns true if we are parsing tentatively and are not committed to
25691 this tentative parse. */
25693 static bool
25694 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25696 return (cp_parser_parsing_tentatively (parser)
25697 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25700 /* Returns nonzero iff an error has occurred during the most recent
25701 tentative parse. */
25703 static bool
25704 cp_parser_error_occurred (cp_parser* parser)
25706 return (cp_parser_parsing_tentatively (parser)
25707 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25710 /* Returns nonzero if GNU extensions are allowed. */
25712 static bool
25713 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25715 return parser->allow_gnu_extensions_p;
25718 /* Objective-C++ Productions */
25721 /* Parse an Objective-C expression, which feeds into a primary-expression
25722 above.
25724 objc-expression:
25725 objc-message-expression
25726 objc-string-literal
25727 objc-encode-expression
25728 objc-protocol-expression
25729 objc-selector-expression
25731 Returns a tree representation of the expression. */
25733 static tree
25734 cp_parser_objc_expression (cp_parser* parser)
25736 /* Try to figure out what kind of declaration is present. */
25737 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25739 switch (kwd->type)
25741 case CPP_OPEN_SQUARE:
25742 return cp_parser_objc_message_expression (parser);
25744 case CPP_OBJC_STRING:
25745 kwd = cp_lexer_consume_token (parser->lexer);
25746 return objc_build_string_object (kwd->u.value);
25748 case CPP_KEYWORD:
25749 switch (kwd->keyword)
25751 case RID_AT_ENCODE:
25752 return cp_parser_objc_encode_expression (parser);
25754 case RID_AT_PROTOCOL:
25755 return cp_parser_objc_protocol_expression (parser);
25757 case RID_AT_SELECTOR:
25758 return cp_parser_objc_selector_expression (parser);
25760 default:
25761 break;
25763 default:
25764 error_at (kwd->location,
25765 "misplaced %<@%D%> Objective-C++ construct",
25766 kwd->u.value);
25767 cp_parser_skip_to_end_of_block_or_statement (parser);
25770 return error_mark_node;
25773 /* Parse an Objective-C message expression.
25775 objc-message-expression:
25776 [ objc-message-receiver objc-message-args ]
25778 Returns a representation of an Objective-C message. */
25780 static tree
25781 cp_parser_objc_message_expression (cp_parser* parser)
25783 tree receiver, messageargs;
25785 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25786 receiver = cp_parser_objc_message_receiver (parser);
25787 messageargs = cp_parser_objc_message_args (parser);
25788 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25790 return objc_build_message_expr (receiver, messageargs);
25793 /* Parse an objc-message-receiver.
25795 objc-message-receiver:
25796 expression
25797 simple-type-specifier
25799 Returns a representation of the type or expression. */
25801 static tree
25802 cp_parser_objc_message_receiver (cp_parser* parser)
25804 tree rcv;
25806 /* An Objective-C message receiver may be either (1) a type
25807 or (2) an expression. */
25808 cp_parser_parse_tentatively (parser);
25809 rcv = cp_parser_expression (parser);
25811 /* If that worked out, fine. */
25812 if (cp_parser_parse_definitely (parser))
25813 return rcv;
25815 cp_parser_parse_tentatively (parser);
25816 rcv = cp_parser_simple_type_specifier (parser,
25817 /*decl_specs=*/NULL,
25818 CP_PARSER_FLAGS_NONE);
25820 if (cp_parser_parse_definitely (parser))
25821 return objc_get_class_reference (rcv);
25823 cp_parser_error (parser, "objective-c++ message receiver expected");
25824 return error_mark_node;
25827 /* Parse the arguments and selectors comprising an Objective-C message.
25829 objc-message-args:
25830 objc-selector
25831 objc-selector-args
25832 objc-selector-args , objc-comma-args
25834 objc-selector-args:
25835 objc-selector [opt] : assignment-expression
25836 objc-selector-args objc-selector [opt] : assignment-expression
25838 objc-comma-args:
25839 assignment-expression
25840 objc-comma-args , assignment-expression
25842 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25843 selector arguments and TREE_VALUE containing a list of comma
25844 arguments. */
25846 static tree
25847 cp_parser_objc_message_args (cp_parser* parser)
25849 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25850 bool maybe_unary_selector_p = true;
25851 cp_token *token = cp_lexer_peek_token (parser->lexer);
25853 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25855 tree selector = NULL_TREE, arg;
25857 if (token->type != CPP_COLON)
25858 selector = cp_parser_objc_selector (parser);
25860 /* Detect if we have a unary selector. */
25861 if (maybe_unary_selector_p
25862 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25863 return build_tree_list (selector, NULL_TREE);
25865 maybe_unary_selector_p = false;
25866 cp_parser_require (parser, CPP_COLON, RT_COLON);
25867 arg = cp_parser_assignment_expression (parser);
25869 sel_args
25870 = chainon (sel_args,
25871 build_tree_list (selector, arg));
25873 token = cp_lexer_peek_token (parser->lexer);
25876 /* Handle non-selector arguments, if any. */
25877 while (token->type == CPP_COMMA)
25879 tree arg;
25881 cp_lexer_consume_token (parser->lexer);
25882 arg = cp_parser_assignment_expression (parser);
25884 addl_args
25885 = chainon (addl_args,
25886 build_tree_list (NULL_TREE, arg));
25888 token = cp_lexer_peek_token (parser->lexer);
25891 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25893 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25894 return build_tree_list (error_mark_node, error_mark_node);
25897 return build_tree_list (sel_args, addl_args);
25900 /* Parse an Objective-C encode expression.
25902 objc-encode-expression:
25903 @encode objc-typename
25905 Returns an encoded representation of the type argument. */
25907 static tree
25908 cp_parser_objc_encode_expression (cp_parser* parser)
25910 tree type;
25911 cp_token *token;
25913 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25914 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25915 token = cp_lexer_peek_token (parser->lexer);
25916 type = complete_type (cp_parser_type_id (parser));
25917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25919 if (!type)
25921 error_at (token->location,
25922 "%<@encode%> must specify a type as an argument");
25923 return error_mark_node;
25926 /* This happens if we find @encode(T) (where T is a template
25927 typename or something dependent on a template typename) when
25928 parsing a template. In that case, we can't compile it
25929 immediately, but we rather create an AT_ENCODE_EXPR which will
25930 need to be instantiated when the template is used.
25932 if (dependent_type_p (type))
25934 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25935 TREE_READONLY (value) = 1;
25936 return value;
25939 return objc_build_encode_expr (type);
25942 /* Parse an Objective-C @defs expression. */
25944 static tree
25945 cp_parser_objc_defs_expression (cp_parser *parser)
25947 tree name;
25949 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25950 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25951 name = cp_parser_identifier (parser);
25952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25954 return objc_get_class_ivars (name);
25957 /* Parse an Objective-C protocol expression.
25959 objc-protocol-expression:
25960 @protocol ( identifier )
25962 Returns a representation of the protocol expression. */
25964 static tree
25965 cp_parser_objc_protocol_expression (cp_parser* parser)
25967 tree proto;
25969 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25970 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25971 proto = cp_parser_identifier (parser);
25972 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25974 return objc_build_protocol_expr (proto);
25977 /* Parse an Objective-C selector expression.
25979 objc-selector-expression:
25980 @selector ( objc-method-signature )
25982 objc-method-signature:
25983 objc-selector
25984 objc-selector-seq
25986 objc-selector-seq:
25987 objc-selector :
25988 objc-selector-seq objc-selector :
25990 Returns a representation of the method selector. */
25992 static tree
25993 cp_parser_objc_selector_expression (cp_parser* parser)
25995 tree sel_seq = NULL_TREE;
25996 bool maybe_unary_selector_p = true;
25997 cp_token *token;
25998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26000 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26001 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26002 token = cp_lexer_peek_token (parser->lexer);
26004 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26005 || token->type == CPP_SCOPE)
26007 tree selector = NULL_TREE;
26009 if (token->type != CPP_COLON
26010 || token->type == CPP_SCOPE)
26011 selector = cp_parser_objc_selector (parser);
26013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26014 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26016 /* Detect if we have a unary selector. */
26017 if (maybe_unary_selector_p)
26019 sel_seq = selector;
26020 goto finish_selector;
26022 else
26024 cp_parser_error (parser, "expected %<:%>");
26027 maybe_unary_selector_p = false;
26028 token = cp_lexer_consume_token (parser->lexer);
26030 if (token->type == CPP_SCOPE)
26032 sel_seq
26033 = chainon (sel_seq,
26034 build_tree_list (selector, NULL_TREE));
26035 sel_seq
26036 = chainon (sel_seq,
26037 build_tree_list (NULL_TREE, NULL_TREE));
26039 else
26040 sel_seq
26041 = chainon (sel_seq,
26042 build_tree_list (selector, NULL_TREE));
26044 token = cp_lexer_peek_token (parser->lexer);
26047 finish_selector:
26048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26050 return objc_build_selector_expr (loc, sel_seq);
26053 /* Parse a list of identifiers.
26055 objc-identifier-list:
26056 identifier
26057 objc-identifier-list , identifier
26059 Returns a TREE_LIST of identifier nodes. */
26061 static tree
26062 cp_parser_objc_identifier_list (cp_parser* parser)
26064 tree identifier;
26065 tree list;
26066 cp_token *sep;
26068 identifier = cp_parser_identifier (parser);
26069 if (identifier == error_mark_node)
26070 return error_mark_node;
26072 list = build_tree_list (NULL_TREE, identifier);
26073 sep = cp_lexer_peek_token (parser->lexer);
26075 while (sep->type == CPP_COMMA)
26077 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26078 identifier = cp_parser_identifier (parser);
26079 if (identifier == error_mark_node)
26080 return list;
26082 list = chainon (list, build_tree_list (NULL_TREE,
26083 identifier));
26084 sep = cp_lexer_peek_token (parser->lexer);
26087 return list;
26090 /* Parse an Objective-C alias declaration.
26092 objc-alias-declaration:
26093 @compatibility_alias identifier identifier ;
26095 This function registers the alias mapping with the Objective-C front end.
26096 It returns nothing. */
26098 static void
26099 cp_parser_objc_alias_declaration (cp_parser* parser)
26101 tree alias, orig;
26103 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26104 alias = cp_parser_identifier (parser);
26105 orig = cp_parser_identifier (parser);
26106 objc_declare_alias (alias, orig);
26107 cp_parser_consume_semicolon_at_end_of_statement (parser);
26110 /* Parse an Objective-C class forward-declaration.
26112 objc-class-declaration:
26113 @class objc-identifier-list ;
26115 The function registers the forward declarations with the Objective-C
26116 front end. It returns nothing. */
26118 static void
26119 cp_parser_objc_class_declaration (cp_parser* parser)
26121 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26122 while (true)
26124 tree id;
26126 id = cp_parser_identifier (parser);
26127 if (id == error_mark_node)
26128 break;
26130 objc_declare_class (id);
26132 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26133 cp_lexer_consume_token (parser->lexer);
26134 else
26135 break;
26137 cp_parser_consume_semicolon_at_end_of_statement (parser);
26140 /* Parse a list of Objective-C protocol references.
26142 objc-protocol-refs-opt:
26143 objc-protocol-refs [opt]
26145 objc-protocol-refs:
26146 < objc-identifier-list >
26148 Returns a TREE_LIST of identifiers, if any. */
26150 static tree
26151 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26153 tree protorefs = NULL_TREE;
26155 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26157 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26158 protorefs = cp_parser_objc_identifier_list (parser);
26159 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26162 return protorefs;
26165 /* Parse a Objective-C visibility specification. */
26167 static void
26168 cp_parser_objc_visibility_spec (cp_parser* parser)
26170 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26172 switch (vis->keyword)
26174 case RID_AT_PRIVATE:
26175 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26176 break;
26177 case RID_AT_PROTECTED:
26178 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26179 break;
26180 case RID_AT_PUBLIC:
26181 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26182 break;
26183 case RID_AT_PACKAGE:
26184 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26185 break;
26186 default:
26187 return;
26190 /* Eat '@private'/'@protected'/'@public'. */
26191 cp_lexer_consume_token (parser->lexer);
26194 /* Parse an Objective-C method type. Return 'true' if it is a class
26195 (+) method, and 'false' if it is an instance (-) method. */
26197 static inline bool
26198 cp_parser_objc_method_type (cp_parser* parser)
26200 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26201 return true;
26202 else
26203 return false;
26206 /* Parse an Objective-C protocol qualifier. */
26208 static tree
26209 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26211 tree quals = NULL_TREE, node;
26212 cp_token *token = cp_lexer_peek_token (parser->lexer);
26214 node = token->u.value;
26216 while (node && identifier_p (node)
26217 && (node == ridpointers [(int) RID_IN]
26218 || node == ridpointers [(int) RID_OUT]
26219 || node == ridpointers [(int) RID_INOUT]
26220 || node == ridpointers [(int) RID_BYCOPY]
26221 || node == ridpointers [(int) RID_BYREF]
26222 || node == ridpointers [(int) RID_ONEWAY]))
26224 quals = tree_cons (NULL_TREE, node, quals);
26225 cp_lexer_consume_token (parser->lexer);
26226 token = cp_lexer_peek_token (parser->lexer);
26227 node = token->u.value;
26230 return quals;
26233 /* Parse an Objective-C typename. */
26235 static tree
26236 cp_parser_objc_typename (cp_parser* parser)
26238 tree type_name = NULL_TREE;
26240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26242 tree proto_quals, cp_type = NULL_TREE;
26244 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26245 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26247 /* An ObjC type name may consist of just protocol qualifiers, in which
26248 case the type shall default to 'id'. */
26249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26251 cp_type = cp_parser_type_id (parser);
26253 /* If the type could not be parsed, an error has already
26254 been produced. For error recovery, behave as if it had
26255 not been specified, which will use the default type
26256 'id'. */
26257 if (cp_type == error_mark_node)
26259 cp_type = NULL_TREE;
26260 /* We need to skip to the closing parenthesis as
26261 cp_parser_type_id() does not seem to do it for
26262 us. */
26263 cp_parser_skip_to_closing_parenthesis (parser,
26264 /*recovering=*/true,
26265 /*or_comma=*/false,
26266 /*consume_paren=*/false);
26270 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26271 type_name = build_tree_list (proto_quals, cp_type);
26274 return type_name;
26277 /* Check to see if TYPE refers to an Objective-C selector name. */
26279 static bool
26280 cp_parser_objc_selector_p (enum cpp_ttype type)
26282 return (type == CPP_NAME || type == CPP_KEYWORD
26283 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26284 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26285 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26286 || type == CPP_XOR || type == CPP_XOR_EQ);
26289 /* Parse an Objective-C selector. */
26291 static tree
26292 cp_parser_objc_selector (cp_parser* parser)
26294 cp_token *token = cp_lexer_consume_token (parser->lexer);
26296 if (!cp_parser_objc_selector_p (token->type))
26298 error_at (token->location, "invalid Objective-C++ selector name");
26299 return error_mark_node;
26302 /* C++ operator names are allowed to appear in ObjC selectors. */
26303 switch (token->type)
26305 case CPP_AND_AND: return get_identifier ("and");
26306 case CPP_AND_EQ: return get_identifier ("and_eq");
26307 case CPP_AND: return get_identifier ("bitand");
26308 case CPP_OR: return get_identifier ("bitor");
26309 case CPP_COMPL: return get_identifier ("compl");
26310 case CPP_NOT: return get_identifier ("not");
26311 case CPP_NOT_EQ: return get_identifier ("not_eq");
26312 case CPP_OR_OR: return get_identifier ("or");
26313 case CPP_OR_EQ: return get_identifier ("or_eq");
26314 case CPP_XOR: return get_identifier ("xor");
26315 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26316 default: return token->u.value;
26320 /* Parse an Objective-C params list. */
26322 static tree
26323 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26325 tree params = NULL_TREE;
26326 bool maybe_unary_selector_p = true;
26327 cp_token *token = cp_lexer_peek_token (parser->lexer);
26329 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26331 tree selector = NULL_TREE, type_name, identifier;
26332 tree parm_attr = NULL_TREE;
26334 if (token->keyword == RID_ATTRIBUTE)
26335 break;
26337 if (token->type != CPP_COLON)
26338 selector = cp_parser_objc_selector (parser);
26340 /* Detect if we have a unary selector. */
26341 if (maybe_unary_selector_p
26342 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26344 params = selector; /* Might be followed by attributes. */
26345 break;
26348 maybe_unary_selector_p = false;
26349 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26351 /* Something went quite wrong. There should be a colon
26352 here, but there is not. Stop parsing parameters. */
26353 break;
26355 type_name = cp_parser_objc_typename (parser);
26356 /* New ObjC allows attributes on parameters too. */
26357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26358 parm_attr = cp_parser_attributes_opt (parser);
26359 identifier = cp_parser_identifier (parser);
26361 params
26362 = chainon (params,
26363 objc_build_keyword_decl (selector,
26364 type_name,
26365 identifier,
26366 parm_attr));
26368 token = cp_lexer_peek_token (parser->lexer);
26371 if (params == NULL_TREE)
26373 cp_parser_error (parser, "objective-c++ method declaration is expected");
26374 return error_mark_node;
26377 /* We allow tail attributes for the method. */
26378 if (token->keyword == RID_ATTRIBUTE)
26380 *attributes = cp_parser_attributes_opt (parser);
26381 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26382 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26383 return params;
26384 cp_parser_error (parser,
26385 "method attributes must be specified at the end");
26386 return error_mark_node;
26389 if (params == NULL_TREE)
26391 cp_parser_error (parser, "objective-c++ method declaration is expected");
26392 return error_mark_node;
26394 return params;
26397 /* Parse the non-keyword Objective-C params. */
26399 static tree
26400 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26401 tree* attributes)
26403 tree params = make_node (TREE_LIST);
26404 cp_token *token = cp_lexer_peek_token (parser->lexer);
26405 *ellipsisp = false; /* Initially, assume no ellipsis. */
26407 while (token->type == CPP_COMMA)
26409 cp_parameter_declarator *parmdecl;
26410 tree parm;
26412 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26413 token = cp_lexer_peek_token (parser->lexer);
26415 if (token->type == CPP_ELLIPSIS)
26417 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26418 *ellipsisp = true;
26419 token = cp_lexer_peek_token (parser->lexer);
26420 break;
26423 /* TODO: parse attributes for tail parameters. */
26424 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26425 parm = grokdeclarator (parmdecl->declarator,
26426 &parmdecl->decl_specifiers,
26427 PARM, /*initialized=*/0,
26428 /*attrlist=*/NULL);
26430 chainon (params, build_tree_list (NULL_TREE, parm));
26431 token = cp_lexer_peek_token (parser->lexer);
26434 /* We allow tail attributes for the method. */
26435 if (token->keyword == RID_ATTRIBUTE)
26437 if (*attributes == NULL_TREE)
26439 *attributes = cp_parser_attributes_opt (parser);
26440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26441 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26442 return params;
26444 else
26445 /* We have an error, but parse the attributes, so that we can
26446 carry on. */
26447 *attributes = cp_parser_attributes_opt (parser);
26449 cp_parser_error (parser,
26450 "method attributes must be specified at the end");
26451 return error_mark_node;
26454 return params;
26457 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26459 static void
26460 cp_parser_objc_interstitial_code (cp_parser* parser)
26462 cp_token *token = cp_lexer_peek_token (parser->lexer);
26464 /* If the next token is `extern' and the following token is a string
26465 literal, then we have a linkage specification. */
26466 if (token->keyword == RID_EXTERN
26467 && cp_parser_is_pure_string_literal
26468 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26469 cp_parser_linkage_specification (parser);
26470 /* Handle #pragma, if any. */
26471 else if (token->type == CPP_PRAGMA)
26472 cp_parser_pragma (parser, pragma_objc_icode);
26473 /* Allow stray semicolons. */
26474 else if (token->type == CPP_SEMICOLON)
26475 cp_lexer_consume_token (parser->lexer);
26476 /* Mark methods as optional or required, when building protocols. */
26477 else if (token->keyword == RID_AT_OPTIONAL)
26479 cp_lexer_consume_token (parser->lexer);
26480 objc_set_method_opt (true);
26482 else if (token->keyword == RID_AT_REQUIRED)
26484 cp_lexer_consume_token (parser->lexer);
26485 objc_set_method_opt (false);
26487 else if (token->keyword == RID_NAMESPACE)
26488 cp_parser_namespace_definition (parser);
26489 /* Other stray characters must generate errors. */
26490 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26492 cp_lexer_consume_token (parser->lexer);
26493 error ("stray %qs between Objective-C++ methods",
26494 token->type == CPP_OPEN_BRACE ? "{" : "}");
26496 /* Finally, try to parse a block-declaration, or a function-definition. */
26497 else
26498 cp_parser_block_declaration (parser, /*statement_p=*/false);
26501 /* Parse a method signature. */
26503 static tree
26504 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26506 tree rettype, kwdparms, optparms;
26507 bool ellipsis = false;
26508 bool is_class_method;
26510 is_class_method = cp_parser_objc_method_type (parser);
26511 rettype = cp_parser_objc_typename (parser);
26512 *attributes = NULL_TREE;
26513 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26514 if (kwdparms == error_mark_node)
26515 return error_mark_node;
26516 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26517 if (optparms == error_mark_node)
26518 return error_mark_node;
26520 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26523 static bool
26524 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26526 tree tattr;
26527 cp_lexer_save_tokens (parser->lexer);
26528 tattr = cp_parser_attributes_opt (parser);
26529 gcc_assert (tattr) ;
26531 /* If the attributes are followed by a method introducer, this is not allowed.
26532 Dump the attributes and flag the situation. */
26533 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26534 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26535 return true;
26537 /* Otherwise, the attributes introduce some interstitial code, possibly so
26538 rewind to allow that check. */
26539 cp_lexer_rollback_tokens (parser->lexer);
26540 return false;
26543 /* Parse an Objective-C method prototype list. */
26545 static void
26546 cp_parser_objc_method_prototype_list (cp_parser* parser)
26548 cp_token *token = cp_lexer_peek_token (parser->lexer);
26550 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26552 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26554 tree attributes, sig;
26555 bool is_class_method;
26556 if (token->type == CPP_PLUS)
26557 is_class_method = true;
26558 else
26559 is_class_method = false;
26560 sig = cp_parser_objc_method_signature (parser, &attributes);
26561 if (sig == error_mark_node)
26563 cp_parser_skip_to_end_of_block_or_statement (parser);
26564 token = cp_lexer_peek_token (parser->lexer);
26565 continue;
26567 objc_add_method_declaration (is_class_method, sig, attributes);
26568 cp_parser_consume_semicolon_at_end_of_statement (parser);
26570 else if (token->keyword == RID_AT_PROPERTY)
26571 cp_parser_objc_at_property_declaration (parser);
26572 else if (token->keyword == RID_ATTRIBUTE
26573 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26574 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26575 OPT_Wattributes,
26576 "prefix attributes are ignored for methods");
26577 else
26578 /* Allow for interspersed non-ObjC++ code. */
26579 cp_parser_objc_interstitial_code (parser);
26581 token = cp_lexer_peek_token (parser->lexer);
26584 if (token->type != CPP_EOF)
26585 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26586 else
26587 cp_parser_error (parser, "expected %<@end%>");
26589 objc_finish_interface ();
26592 /* Parse an Objective-C method definition list. */
26594 static void
26595 cp_parser_objc_method_definition_list (cp_parser* parser)
26597 cp_token *token = cp_lexer_peek_token (parser->lexer);
26599 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26601 tree meth;
26603 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26605 cp_token *ptk;
26606 tree sig, attribute;
26607 bool is_class_method;
26608 if (token->type == CPP_PLUS)
26609 is_class_method = true;
26610 else
26611 is_class_method = false;
26612 push_deferring_access_checks (dk_deferred);
26613 sig = cp_parser_objc_method_signature (parser, &attribute);
26614 if (sig == error_mark_node)
26616 cp_parser_skip_to_end_of_block_or_statement (parser);
26617 token = cp_lexer_peek_token (parser->lexer);
26618 continue;
26620 objc_start_method_definition (is_class_method, sig, attribute,
26621 NULL_TREE);
26623 /* For historical reasons, we accept an optional semicolon. */
26624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26625 cp_lexer_consume_token (parser->lexer);
26627 ptk = cp_lexer_peek_token (parser->lexer);
26628 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26629 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26631 perform_deferred_access_checks (tf_warning_or_error);
26632 stop_deferring_access_checks ();
26633 meth = cp_parser_function_definition_after_declarator (parser,
26634 false);
26635 pop_deferring_access_checks ();
26636 objc_finish_method_definition (meth);
26639 /* The following case will be removed once @synthesize is
26640 completely implemented. */
26641 else if (token->keyword == RID_AT_PROPERTY)
26642 cp_parser_objc_at_property_declaration (parser);
26643 else if (token->keyword == RID_AT_SYNTHESIZE)
26644 cp_parser_objc_at_synthesize_declaration (parser);
26645 else if (token->keyword == RID_AT_DYNAMIC)
26646 cp_parser_objc_at_dynamic_declaration (parser);
26647 else if (token->keyword == RID_ATTRIBUTE
26648 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26649 warning_at (token->location, OPT_Wattributes,
26650 "prefix attributes are ignored for methods");
26651 else
26652 /* Allow for interspersed non-ObjC++ code. */
26653 cp_parser_objc_interstitial_code (parser);
26655 token = cp_lexer_peek_token (parser->lexer);
26658 if (token->type != CPP_EOF)
26659 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26660 else
26661 cp_parser_error (parser, "expected %<@end%>");
26663 objc_finish_implementation ();
26666 /* Parse Objective-C ivars. */
26668 static void
26669 cp_parser_objc_class_ivars (cp_parser* parser)
26671 cp_token *token = cp_lexer_peek_token (parser->lexer);
26673 if (token->type != CPP_OPEN_BRACE)
26674 return; /* No ivars specified. */
26676 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26677 token = cp_lexer_peek_token (parser->lexer);
26679 while (token->type != CPP_CLOSE_BRACE
26680 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26682 cp_decl_specifier_seq declspecs;
26683 int decl_class_or_enum_p;
26684 tree prefix_attributes;
26686 cp_parser_objc_visibility_spec (parser);
26688 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26689 break;
26691 cp_parser_decl_specifier_seq (parser,
26692 CP_PARSER_FLAGS_OPTIONAL,
26693 &declspecs,
26694 &decl_class_or_enum_p);
26696 /* auto, register, static, extern, mutable. */
26697 if (declspecs.storage_class != sc_none)
26699 cp_parser_error (parser, "invalid type for instance variable");
26700 declspecs.storage_class = sc_none;
26703 /* thread_local. */
26704 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26706 cp_parser_error (parser, "invalid type for instance variable");
26707 declspecs.locations[ds_thread] = 0;
26710 /* typedef. */
26711 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26713 cp_parser_error (parser, "invalid type for instance variable");
26714 declspecs.locations[ds_typedef] = 0;
26717 prefix_attributes = declspecs.attributes;
26718 declspecs.attributes = NULL_TREE;
26720 /* Keep going until we hit the `;' at the end of the
26721 declaration. */
26722 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26724 tree width = NULL_TREE, attributes, first_attribute, decl;
26725 cp_declarator *declarator = NULL;
26726 int ctor_dtor_or_conv_p;
26728 /* Check for a (possibly unnamed) bitfield declaration. */
26729 token = cp_lexer_peek_token (parser->lexer);
26730 if (token->type == CPP_COLON)
26731 goto eat_colon;
26733 if (token->type == CPP_NAME
26734 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26735 == CPP_COLON))
26737 /* Get the name of the bitfield. */
26738 declarator = make_id_declarator (NULL_TREE,
26739 cp_parser_identifier (parser),
26740 sfk_none);
26742 eat_colon:
26743 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26744 /* Get the width of the bitfield. */
26745 width
26746 = cp_parser_constant_expression (parser);
26748 else
26750 /* Parse the declarator. */
26751 declarator
26752 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26753 &ctor_dtor_or_conv_p,
26754 /*parenthesized_p=*/NULL,
26755 /*member_p=*/false,
26756 /*friend_p=*/false);
26759 /* Look for attributes that apply to the ivar. */
26760 attributes = cp_parser_attributes_opt (parser);
26761 /* Remember which attributes are prefix attributes and
26762 which are not. */
26763 first_attribute = attributes;
26764 /* Combine the attributes. */
26765 attributes = chainon (prefix_attributes, attributes);
26767 if (width)
26768 /* Create the bitfield declaration. */
26769 decl = grokbitfield (declarator, &declspecs,
26770 width,
26771 attributes);
26772 else
26773 decl = grokfield (declarator, &declspecs,
26774 NULL_TREE, /*init_const_expr_p=*/false,
26775 NULL_TREE, attributes);
26777 /* Add the instance variable. */
26778 if (decl != error_mark_node && decl != NULL_TREE)
26779 objc_add_instance_variable (decl);
26781 /* Reset PREFIX_ATTRIBUTES. */
26782 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26783 attributes = TREE_CHAIN (attributes);
26784 if (attributes)
26785 TREE_CHAIN (attributes) = NULL_TREE;
26787 token = cp_lexer_peek_token (parser->lexer);
26789 if (token->type == CPP_COMMA)
26791 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26792 continue;
26794 break;
26797 cp_parser_consume_semicolon_at_end_of_statement (parser);
26798 token = cp_lexer_peek_token (parser->lexer);
26801 if (token->keyword == RID_AT_END)
26802 cp_parser_error (parser, "expected %<}%>");
26804 /* Do not consume the RID_AT_END, so it will be read again as terminating
26805 the @interface of @implementation. */
26806 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26807 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26809 /* For historical reasons, we accept an optional semicolon. */
26810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26811 cp_lexer_consume_token (parser->lexer);
26814 /* Parse an Objective-C protocol declaration. */
26816 static void
26817 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26819 tree proto, protorefs;
26820 cp_token *tok;
26822 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26825 tok = cp_lexer_peek_token (parser->lexer);
26826 error_at (tok->location, "identifier expected after %<@protocol%>");
26827 cp_parser_consume_semicolon_at_end_of_statement (parser);
26828 return;
26831 /* See if we have a forward declaration or a definition. */
26832 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26834 /* Try a forward declaration first. */
26835 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26837 while (true)
26839 tree id;
26841 id = cp_parser_identifier (parser);
26842 if (id == error_mark_node)
26843 break;
26845 objc_declare_protocol (id, attributes);
26847 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26848 cp_lexer_consume_token (parser->lexer);
26849 else
26850 break;
26852 cp_parser_consume_semicolon_at_end_of_statement (parser);
26855 /* Ok, we got a full-fledged definition (or at least should). */
26856 else
26858 proto = cp_parser_identifier (parser);
26859 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26860 objc_start_protocol (proto, protorefs, attributes);
26861 cp_parser_objc_method_prototype_list (parser);
26865 /* Parse an Objective-C superclass or category. */
26867 static void
26868 cp_parser_objc_superclass_or_category (cp_parser *parser,
26869 bool iface_p,
26870 tree *super,
26871 tree *categ, bool *is_class_extension)
26873 cp_token *next = cp_lexer_peek_token (parser->lexer);
26875 *super = *categ = NULL_TREE;
26876 *is_class_extension = false;
26877 if (next->type == CPP_COLON)
26879 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26880 *super = cp_parser_identifier (parser);
26882 else if (next->type == CPP_OPEN_PAREN)
26884 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26886 /* If there is no category name, and this is an @interface, we
26887 have a class extension. */
26888 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26890 *categ = NULL_TREE;
26891 *is_class_extension = true;
26893 else
26894 *categ = cp_parser_identifier (parser);
26896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26900 /* Parse an Objective-C class interface. */
26902 static void
26903 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26905 tree name, super, categ, protos;
26906 bool is_class_extension;
26908 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26909 name = cp_parser_identifier (parser);
26910 if (name == error_mark_node)
26912 /* It's hard to recover because even if valid @interface stuff
26913 is to follow, we can't compile it (or validate it) if we
26914 don't even know which class it refers to. Let's assume this
26915 was a stray '@interface' token in the stream and skip it.
26917 return;
26919 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26920 &is_class_extension);
26921 protos = cp_parser_objc_protocol_refs_opt (parser);
26923 /* We have either a class or a category on our hands. */
26924 if (categ || is_class_extension)
26925 objc_start_category_interface (name, categ, protos, attributes);
26926 else
26928 objc_start_class_interface (name, super, protos, attributes);
26929 /* Handle instance variable declarations, if any. */
26930 cp_parser_objc_class_ivars (parser);
26931 objc_continue_interface ();
26934 cp_parser_objc_method_prototype_list (parser);
26937 /* Parse an Objective-C class implementation. */
26939 static void
26940 cp_parser_objc_class_implementation (cp_parser* parser)
26942 tree name, super, categ;
26943 bool is_class_extension;
26945 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26946 name = cp_parser_identifier (parser);
26947 if (name == error_mark_node)
26949 /* It's hard to recover because even if valid @implementation
26950 stuff is to follow, we can't compile it (or validate it) if
26951 we don't even know which class it refers to. Let's assume
26952 this was a stray '@implementation' token in the stream and
26953 skip it.
26955 return;
26957 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26958 &is_class_extension);
26960 /* We have either a class or a category on our hands. */
26961 if (categ)
26962 objc_start_category_implementation (name, categ);
26963 else
26965 objc_start_class_implementation (name, super);
26966 /* Handle instance variable declarations, if any. */
26967 cp_parser_objc_class_ivars (parser);
26968 objc_continue_implementation ();
26971 cp_parser_objc_method_definition_list (parser);
26974 /* Consume the @end token and finish off the implementation. */
26976 static void
26977 cp_parser_objc_end_implementation (cp_parser* parser)
26979 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26980 objc_finish_implementation ();
26983 /* Parse an Objective-C declaration. */
26985 static void
26986 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26988 /* Try to figure out what kind of declaration is present. */
26989 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26991 if (attributes)
26992 switch (kwd->keyword)
26994 case RID_AT_ALIAS:
26995 case RID_AT_CLASS:
26996 case RID_AT_END:
26997 error_at (kwd->location, "attributes may not be specified before"
26998 " the %<@%D%> Objective-C++ keyword",
26999 kwd->u.value);
27000 attributes = NULL;
27001 break;
27002 case RID_AT_IMPLEMENTATION:
27003 warning_at (kwd->location, OPT_Wattributes,
27004 "prefix attributes are ignored before %<@%D%>",
27005 kwd->u.value);
27006 attributes = NULL;
27007 default:
27008 break;
27011 switch (kwd->keyword)
27013 case RID_AT_ALIAS:
27014 cp_parser_objc_alias_declaration (parser);
27015 break;
27016 case RID_AT_CLASS:
27017 cp_parser_objc_class_declaration (parser);
27018 break;
27019 case RID_AT_PROTOCOL:
27020 cp_parser_objc_protocol_declaration (parser, attributes);
27021 break;
27022 case RID_AT_INTERFACE:
27023 cp_parser_objc_class_interface (parser, attributes);
27024 break;
27025 case RID_AT_IMPLEMENTATION:
27026 cp_parser_objc_class_implementation (parser);
27027 break;
27028 case RID_AT_END:
27029 cp_parser_objc_end_implementation (parser);
27030 break;
27031 default:
27032 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27033 kwd->u.value);
27034 cp_parser_skip_to_end_of_block_or_statement (parser);
27038 /* Parse an Objective-C try-catch-finally statement.
27040 objc-try-catch-finally-stmt:
27041 @try compound-statement objc-catch-clause-seq [opt]
27042 objc-finally-clause [opt]
27044 objc-catch-clause-seq:
27045 objc-catch-clause objc-catch-clause-seq [opt]
27047 objc-catch-clause:
27048 @catch ( objc-exception-declaration ) compound-statement
27050 objc-finally-clause:
27051 @finally compound-statement
27053 objc-exception-declaration:
27054 parameter-declaration
27055 '...'
27057 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27059 Returns NULL_TREE.
27061 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27062 for C. Keep them in sync. */
27064 static tree
27065 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27067 location_t location;
27068 tree stmt;
27070 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27071 location = cp_lexer_peek_token (parser->lexer)->location;
27072 objc_maybe_warn_exceptions (location);
27073 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27074 node, lest it get absorbed into the surrounding block. */
27075 stmt = push_stmt_list ();
27076 cp_parser_compound_statement (parser, NULL, false, false);
27077 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27079 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27081 cp_parameter_declarator *parm;
27082 tree parameter_declaration = error_mark_node;
27083 bool seen_open_paren = false;
27085 cp_lexer_consume_token (parser->lexer);
27086 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27087 seen_open_paren = true;
27088 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27090 /* We have "@catch (...)" (where the '...' are literally
27091 what is in the code). Skip the '...'.
27092 parameter_declaration is set to NULL_TREE, and
27093 objc_being_catch_clauses() knows that that means
27094 '...'. */
27095 cp_lexer_consume_token (parser->lexer);
27096 parameter_declaration = NULL_TREE;
27098 else
27100 /* We have "@catch (NSException *exception)" or something
27101 like that. Parse the parameter declaration. */
27102 parm = cp_parser_parameter_declaration (parser, false, NULL);
27103 if (parm == NULL)
27104 parameter_declaration = error_mark_node;
27105 else
27106 parameter_declaration = grokdeclarator (parm->declarator,
27107 &parm->decl_specifiers,
27108 PARM, /*initialized=*/0,
27109 /*attrlist=*/NULL);
27111 if (seen_open_paren)
27112 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27113 else
27115 /* If there was no open parenthesis, we are recovering from
27116 an error, and we are trying to figure out what mistake
27117 the user has made. */
27119 /* If there is an immediate closing parenthesis, the user
27120 probably forgot the opening one (ie, they typed "@catch
27121 NSException *e)". Parse the closing parenthesis and keep
27122 going. */
27123 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27124 cp_lexer_consume_token (parser->lexer);
27126 /* If these is no immediate closing parenthesis, the user
27127 probably doesn't know that parenthesis are required at
27128 all (ie, they typed "@catch NSException *e"). So, just
27129 forget about the closing parenthesis and keep going. */
27131 objc_begin_catch_clause (parameter_declaration);
27132 cp_parser_compound_statement (parser, NULL, false, false);
27133 objc_finish_catch_clause ();
27135 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27137 cp_lexer_consume_token (parser->lexer);
27138 location = cp_lexer_peek_token (parser->lexer)->location;
27139 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27140 node, lest it get absorbed into the surrounding block. */
27141 stmt = push_stmt_list ();
27142 cp_parser_compound_statement (parser, NULL, false, false);
27143 objc_build_finally_clause (location, pop_stmt_list (stmt));
27146 return objc_finish_try_stmt ();
27149 /* Parse an Objective-C synchronized statement.
27151 objc-synchronized-stmt:
27152 @synchronized ( expression ) compound-statement
27154 Returns NULL_TREE. */
27156 static tree
27157 cp_parser_objc_synchronized_statement (cp_parser *parser)
27159 location_t location;
27160 tree lock, stmt;
27162 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27164 location = cp_lexer_peek_token (parser->lexer)->location;
27165 objc_maybe_warn_exceptions (location);
27166 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27167 lock = cp_parser_expression (parser);
27168 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27170 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27171 node, lest it get absorbed into the surrounding block. */
27172 stmt = push_stmt_list ();
27173 cp_parser_compound_statement (parser, NULL, false, false);
27175 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27178 /* Parse an Objective-C throw statement.
27180 objc-throw-stmt:
27181 @throw assignment-expression [opt] ;
27183 Returns a constructed '@throw' statement. */
27185 static tree
27186 cp_parser_objc_throw_statement (cp_parser *parser)
27188 tree expr = NULL_TREE;
27189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27191 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27194 expr = cp_parser_expression (parser);
27196 cp_parser_consume_semicolon_at_end_of_statement (parser);
27198 return objc_build_throw_stmt (loc, expr);
27201 /* Parse an Objective-C statement. */
27203 static tree
27204 cp_parser_objc_statement (cp_parser * parser)
27206 /* Try to figure out what kind of declaration is present. */
27207 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27209 switch (kwd->keyword)
27211 case RID_AT_TRY:
27212 return cp_parser_objc_try_catch_finally_statement (parser);
27213 case RID_AT_SYNCHRONIZED:
27214 return cp_parser_objc_synchronized_statement (parser);
27215 case RID_AT_THROW:
27216 return cp_parser_objc_throw_statement (parser);
27217 default:
27218 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27219 kwd->u.value);
27220 cp_parser_skip_to_end_of_block_or_statement (parser);
27223 return error_mark_node;
27226 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27227 look ahead to see if an objc keyword follows the attributes. This
27228 is to detect the use of prefix attributes on ObjC @interface and
27229 @protocol. */
27231 static bool
27232 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27234 cp_lexer_save_tokens (parser->lexer);
27235 *attrib = cp_parser_attributes_opt (parser);
27236 gcc_assert (*attrib);
27237 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27239 cp_lexer_commit_tokens (parser->lexer);
27240 return true;
27242 cp_lexer_rollback_tokens (parser->lexer);
27243 return false;
27246 /* This routine is a minimal replacement for
27247 c_parser_struct_declaration () used when parsing the list of
27248 types/names or ObjC++ properties. For example, when parsing the
27249 code
27251 @property (readonly) int a, b, c;
27253 this function is responsible for parsing "int a, int b, int c" and
27254 returning the declarations as CHAIN of DECLs.
27256 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27257 similar parsing. */
27258 static tree
27259 cp_parser_objc_struct_declaration (cp_parser *parser)
27261 tree decls = NULL_TREE;
27262 cp_decl_specifier_seq declspecs;
27263 int decl_class_or_enum_p;
27264 tree prefix_attributes;
27266 cp_parser_decl_specifier_seq (parser,
27267 CP_PARSER_FLAGS_NONE,
27268 &declspecs,
27269 &decl_class_or_enum_p);
27271 if (declspecs.type == error_mark_node)
27272 return error_mark_node;
27274 /* auto, register, static, extern, mutable. */
27275 if (declspecs.storage_class != sc_none)
27277 cp_parser_error (parser, "invalid type for property");
27278 declspecs.storage_class = sc_none;
27281 /* thread_local. */
27282 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27284 cp_parser_error (parser, "invalid type for property");
27285 declspecs.locations[ds_thread] = 0;
27288 /* typedef. */
27289 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27291 cp_parser_error (parser, "invalid type for property");
27292 declspecs.locations[ds_typedef] = 0;
27295 prefix_attributes = declspecs.attributes;
27296 declspecs.attributes = NULL_TREE;
27298 /* Keep going until we hit the `;' at the end of the declaration. */
27299 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27301 tree attributes, first_attribute, decl;
27302 cp_declarator *declarator;
27303 cp_token *token;
27305 /* Parse the declarator. */
27306 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27307 NULL, NULL, false, false);
27309 /* Look for attributes that apply to the ivar. */
27310 attributes = cp_parser_attributes_opt (parser);
27311 /* Remember which attributes are prefix attributes and
27312 which are not. */
27313 first_attribute = attributes;
27314 /* Combine the attributes. */
27315 attributes = chainon (prefix_attributes, attributes);
27317 decl = grokfield (declarator, &declspecs,
27318 NULL_TREE, /*init_const_expr_p=*/false,
27319 NULL_TREE, attributes);
27321 if (decl == error_mark_node || decl == NULL_TREE)
27322 return error_mark_node;
27324 /* Reset PREFIX_ATTRIBUTES. */
27325 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27326 attributes = TREE_CHAIN (attributes);
27327 if (attributes)
27328 TREE_CHAIN (attributes) = NULL_TREE;
27330 DECL_CHAIN (decl) = decls;
27331 decls = decl;
27333 token = cp_lexer_peek_token (parser->lexer);
27334 if (token->type == CPP_COMMA)
27336 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27337 continue;
27339 else
27340 break;
27342 return decls;
27345 /* Parse an Objective-C @property declaration. The syntax is:
27347 objc-property-declaration:
27348 '@property' objc-property-attributes[opt] struct-declaration ;
27350 objc-property-attributes:
27351 '(' objc-property-attribute-list ')'
27353 objc-property-attribute-list:
27354 objc-property-attribute
27355 objc-property-attribute-list, objc-property-attribute
27357 objc-property-attribute
27358 'getter' = identifier
27359 'setter' = identifier
27360 'readonly'
27361 'readwrite'
27362 'assign'
27363 'retain'
27364 'copy'
27365 'nonatomic'
27367 For example:
27368 @property NSString *name;
27369 @property (readonly) id object;
27370 @property (retain, nonatomic, getter=getTheName) id name;
27371 @property int a, b, c;
27373 PS: This function is identical to
27374 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27375 static void
27376 cp_parser_objc_at_property_declaration (cp_parser *parser)
27378 /* The following variables hold the attributes of the properties as
27379 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27380 seen. When we see an attribute, we set them to 'true' (if they
27381 are boolean properties) or to the identifier (if they have an
27382 argument, ie, for getter and setter). Note that here we only
27383 parse the list of attributes, check the syntax and accumulate the
27384 attributes that we find. objc_add_property_declaration() will
27385 then process the information. */
27386 bool property_assign = false;
27387 bool property_copy = false;
27388 tree property_getter_ident = NULL_TREE;
27389 bool property_nonatomic = false;
27390 bool property_readonly = false;
27391 bool property_readwrite = false;
27392 bool property_retain = false;
27393 tree property_setter_ident = NULL_TREE;
27395 /* 'properties' is the list of properties that we read. Usually a
27396 single one, but maybe more (eg, in "@property int a, b, c;" there
27397 are three). */
27398 tree properties;
27399 location_t loc;
27401 loc = cp_lexer_peek_token (parser->lexer)->location;
27403 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27405 /* Parse the optional attribute list... */
27406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27408 /* Eat the '('. */
27409 cp_lexer_consume_token (parser->lexer);
27411 while (true)
27413 bool syntax_error = false;
27414 cp_token *token = cp_lexer_peek_token (parser->lexer);
27415 enum rid keyword;
27417 if (token->type != CPP_NAME)
27419 cp_parser_error (parser, "expected identifier");
27420 break;
27422 keyword = C_RID_CODE (token->u.value);
27423 cp_lexer_consume_token (parser->lexer);
27424 switch (keyword)
27426 case RID_ASSIGN: property_assign = true; break;
27427 case RID_COPY: property_copy = true; break;
27428 case RID_NONATOMIC: property_nonatomic = true; break;
27429 case RID_READONLY: property_readonly = true; break;
27430 case RID_READWRITE: property_readwrite = true; break;
27431 case RID_RETAIN: property_retain = true; break;
27433 case RID_GETTER:
27434 case RID_SETTER:
27435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27437 if (keyword == RID_GETTER)
27438 cp_parser_error (parser,
27439 "missing %<=%> (after %<getter%> attribute)");
27440 else
27441 cp_parser_error (parser,
27442 "missing %<=%> (after %<setter%> attribute)");
27443 syntax_error = true;
27444 break;
27446 cp_lexer_consume_token (parser->lexer); /* eat the = */
27447 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27449 cp_parser_error (parser, "expected identifier");
27450 syntax_error = true;
27451 break;
27453 if (keyword == RID_SETTER)
27455 if (property_setter_ident != NULL_TREE)
27457 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27458 cp_lexer_consume_token (parser->lexer);
27460 else
27461 property_setter_ident = cp_parser_objc_selector (parser);
27462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27463 cp_parser_error (parser, "setter name must terminate with %<:%>");
27464 else
27465 cp_lexer_consume_token (parser->lexer);
27467 else
27469 if (property_getter_ident != NULL_TREE)
27471 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27472 cp_lexer_consume_token (parser->lexer);
27474 else
27475 property_getter_ident = cp_parser_objc_selector (parser);
27477 break;
27478 default:
27479 cp_parser_error (parser, "unknown property attribute");
27480 syntax_error = true;
27481 break;
27484 if (syntax_error)
27485 break;
27487 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27488 cp_lexer_consume_token (parser->lexer);
27489 else
27490 break;
27493 /* FIXME: "@property (setter, assign);" will generate a spurious
27494 "error: expected ‘)’ before ‘,’ token". This is because
27495 cp_parser_require, unlike the C counterpart, will produce an
27496 error even if we are in error recovery. */
27497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27499 cp_parser_skip_to_closing_parenthesis (parser,
27500 /*recovering=*/true,
27501 /*or_comma=*/false,
27502 /*consume_paren=*/true);
27506 /* ... and the property declaration(s). */
27507 properties = cp_parser_objc_struct_declaration (parser);
27509 if (properties == error_mark_node)
27511 cp_parser_skip_to_end_of_statement (parser);
27512 /* If the next token is now a `;', consume it. */
27513 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27514 cp_lexer_consume_token (parser->lexer);
27515 return;
27518 if (properties == NULL_TREE)
27519 cp_parser_error (parser, "expected identifier");
27520 else
27522 /* Comma-separated properties are chained together in
27523 reverse order; add them one by one. */
27524 properties = nreverse (properties);
27526 for (; properties; properties = TREE_CHAIN (properties))
27527 objc_add_property_declaration (loc, copy_node (properties),
27528 property_readonly, property_readwrite,
27529 property_assign, property_retain,
27530 property_copy, property_nonatomic,
27531 property_getter_ident, property_setter_ident);
27534 cp_parser_consume_semicolon_at_end_of_statement (parser);
27537 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27539 objc-synthesize-declaration:
27540 @synthesize objc-synthesize-identifier-list ;
27542 objc-synthesize-identifier-list:
27543 objc-synthesize-identifier
27544 objc-synthesize-identifier-list, objc-synthesize-identifier
27546 objc-synthesize-identifier
27547 identifier
27548 identifier = identifier
27550 For example:
27551 @synthesize MyProperty;
27552 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27554 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27555 for C. Keep them in sync.
27557 static void
27558 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27560 tree list = NULL_TREE;
27561 location_t loc;
27562 loc = cp_lexer_peek_token (parser->lexer)->location;
27564 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27565 while (true)
27567 tree property, ivar;
27568 property = cp_parser_identifier (parser);
27569 if (property == error_mark_node)
27571 cp_parser_consume_semicolon_at_end_of_statement (parser);
27572 return;
27574 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27576 cp_lexer_consume_token (parser->lexer);
27577 ivar = cp_parser_identifier (parser);
27578 if (ivar == error_mark_node)
27580 cp_parser_consume_semicolon_at_end_of_statement (parser);
27581 return;
27584 else
27585 ivar = NULL_TREE;
27586 list = chainon (list, build_tree_list (ivar, property));
27587 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27588 cp_lexer_consume_token (parser->lexer);
27589 else
27590 break;
27592 cp_parser_consume_semicolon_at_end_of_statement (parser);
27593 objc_add_synthesize_declaration (loc, list);
27596 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27598 objc-dynamic-declaration:
27599 @dynamic identifier-list ;
27601 For example:
27602 @dynamic MyProperty;
27603 @dynamic MyProperty, AnotherProperty;
27605 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27606 for C. Keep them in sync.
27608 static void
27609 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27611 tree list = NULL_TREE;
27612 location_t loc;
27613 loc = cp_lexer_peek_token (parser->lexer)->location;
27615 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27616 while (true)
27618 tree property;
27619 property = cp_parser_identifier (parser);
27620 if (property == error_mark_node)
27622 cp_parser_consume_semicolon_at_end_of_statement (parser);
27623 return;
27625 list = chainon (list, build_tree_list (NULL, property));
27626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27627 cp_lexer_consume_token (parser->lexer);
27628 else
27629 break;
27631 cp_parser_consume_semicolon_at_end_of_statement (parser);
27632 objc_add_dynamic_declaration (loc, list);
27636 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27638 /* Returns name of the next clause.
27639 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27640 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27641 returned and the token is consumed. */
27643 static pragma_omp_clause
27644 cp_parser_omp_clause_name (cp_parser *parser)
27646 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27649 result = PRAGMA_OMP_CLAUSE_IF;
27650 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27651 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27652 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27653 result = PRAGMA_OACC_CLAUSE_DELETE;
27654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27655 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27656 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27657 result = PRAGMA_OMP_CLAUSE_FOR;
27658 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27661 const char *p = IDENTIFIER_POINTER (id);
27663 switch (p[0])
27665 case 'a':
27666 if (!strcmp ("aligned", p))
27667 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27668 else if (!strcmp ("async", p))
27669 result = PRAGMA_OACC_CLAUSE_ASYNC;
27670 break;
27671 case 'c':
27672 if (!strcmp ("collapse", p))
27673 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27674 else if (!strcmp ("copy", p))
27675 result = PRAGMA_OACC_CLAUSE_COPY;
27676 else if (!strcmp ("copyin", p))
27677 result = PRAGMA_OMP_CLAUSE_COPYIN;
27678 else if (!strcmp ("copyout", p))
27679 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27680 else if (!strcmp ("copyprivate", p))
27681 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27682 else if (!strcmp ("create", p))
27683 result = PRAGMA_OACC_CLAUSE_CREATE;
27684 break;
27685 case 'd':
27686 if (!strcmp ("defaultmap", p))
27687 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
27688 else if (!strcmp ("depend", p))
27689 result = PRAGMA_OMP_CLAUSE_DEPEND;
27690 else if (!strcmp ("device", p))
27691 result = PRAGMA_OMP_CLAUSE_DEVICE;
27692 else if (!strcmp ("deviceptr", p))
27693 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27694 else if (!strcmp ("dist_schedule", p))
27695 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27696 break;
27697 case 'f':
27698 if (!strcmp ("final", p))
27699 result = PRAGMA_OMP_CLAUSE_FINAL;
27700 else if (!strcmp ("firstprivate", p))
27701 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27702 else if (!strcmp ("from", p))
27703 result = PRAGMA_OMP_CLAUSE_FROM;
27704 break;
27705 case 'g':
27706 if (!strcmp ("grainsize", p))
27707 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
27708 break;
27709 case 'h':
27710 if (!strcmp ("hint", p))
27711 result = PRAGMA_OMP_CLAUSE_HINT;
27712 else if (!strcmp ("host", p))
27713 result = PRAGMA_OACC_CLAUSE_HOST;
27714 break;
27715 case 'i':
27716 if (!strcmp ("inbranch", p))
27717 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27718 break;
27719 case 'l':
27720 if (!strcmp ("lastprivate", p))
27721 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27722 else if (!strcmp ("linear", p))
27723 result = PRAGMA_OMP_CLAUSE_LINEAR;
27724 break;
27725 case 'm':
27726 if (!strcmp ("map", p))
27727 result = PRAGMA_OMP_CLAUSE_MAP;
27728 else if (!strcmp ("mergeable", p))
27729 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27730 else if (flag_cilkplus && !strcmp ("mask", p))
27731 result = PRAGMA_CILK_CLAUSE_MASK;
27732 break;
27733 case 'n':
27734 if (!strcmp ("nogroup", p))
27735 result = PRAGMA_OMP_CLAUSE_NOGROUP;
27736 else if (!strcmp ("notinbranch", p))
27737 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27738 else if (!strcmp ("nowait", p))
27739 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27740 else if (flag_cilkplus && !strcmp ("nomask", p))
27741 result = PRAGMA_CILK_CLAUSE_NOMASK;
27742 else if (!strcmp ("num_gangs", p))
27743 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27744 else if (!strcmp ("num_tasks", p))
27745 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
27746 else if (!strcmp ("num_teams", p))
27747 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27748 else if (!strcmp ("num_threads", p))
27749 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27750 else if (!strcmp ("num_workers", p))
27751 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27752 break;
27753 case 'o':
27754 if (!strcmp ("ordered", p))
27755 result = PRAGMA_OMP_CLAUSE_ORDERED;
27756 break;
27757 case 'p':
27758 if (!strcmp ("parallel", p))
27759 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27760 else if (!strcmp ("present", p))
27761 result = PRAGMA_OACC_CLAUSE_PRESENT;
27762 else if (!strcmp ("present_or_copy", p)
27763 || !strcmp ("pcopy", p))
27764 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27765 else if (!strcmp ("present_or_copyin", p)
27766 || !strcmp ("pcopyin", p))
27767 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27768 else if (!strcmp ("present_or_copyout", p)
27769 || !strcmp ("pcopyout", p))
27770 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27771 else if (!strcmp ("present_or_create", p)
27772 || !strcmp ("pcreate", p))
27773 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27774 else if (!strcmp ("priority", p))
27775 result = PRAGMA_OMP_CLAUSE_PRIORITY;
27776 else if (!strcmp ("proc_bind", p))
27777 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27778 break;
27779 case 'r':
27780 if (!strcmp ("reduction", p))
27781 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27782 break;
27783 case 's':
27784 if (!strcmp ("safelen", p))
27785 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27786 else if (!strcmp ("schedule", p))
27787 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27788 else if (!strcmp ("sections", p))
27789 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27790 else if (!strcmp ("self", p))
27791 result = PRAGMA_OACC_CLAUSE_SELF;
27792 else if (!strcmp ("shared", p))
27793 result = PRAGMA_OMP_CLAUSE_SHARED;
27794 else if (!strcmp ("simd", p))
27795 result = PRAGMA_OMP_CLAUSE_SIMD;
27796 else if (!strcmp ("simdlen", p))
27797 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27798 break;
27799 case 't':
27800 if (!strcmp ("taskgroup", p))
27801 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27802 else if (!strcmp ("thread_limit", p))
27803 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27804 else if (!strcmp ("threads", p))
27805 result = PRAGMA_OMP_CLAUSE_THREADS;
27806 else if (!strcmp ("to", p))
27807 result = PRAGMA_OMP_CLAUSE_TO;
27808 break;
27809 case 'u':
27810 if (!strcmp ("uniform", p))
27811 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27812 else if (!strcmp ("untied", p))
27813 result = PRAGMA_OMP_CLAUSE_UNTIED;
27814 break;
27815 case 'v':
27816 if (!strcmp ("vector_length", p))
27817 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27818 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27819 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27820 break;
27821 case 'w':
27822 if (!strcmp ("wait", p))
27823 result = PRAGMA_OACC_CLAUSE_WAIT;
27824 break;
27828 if (result != PRAGMA_OMP_CLAUSE_NONE)
27829 cp_lexer_consume_token (parser->lexer);
27831 return result;
27834 /* Validate that a clause of the given type does not already exist. */
27836 static void
27837 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27838 const char *name, location_t location)
27840 tree c;
27842 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27843 if (OMP_CLAUSE_CODE (c) == code)
27845 error_at (location, "too many %qs clauses", name);
27846 break;
27850 /* OpenMP 2.5:
27851 variable-list:
27852 identifier
27853 variable-list , identifier
27855 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27856 colon). An opening parenthesis will have been consumed by the caller.
27858 If KIND is nonzero, create the appropriate node and install the decl
27859 in OMP_CLAUSE_DECL and add the node to the head of the list.
27861 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27862 return the list created.
27864 COLON can be NULL if only closing parenthesis should end the list,
27865 or pointer to bool which will receive false if the list is terminated
27866 by closing parenthesis or true if the list is terminated by colon. */
27868 static tree
27869 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27870 tree list, bool *colon)
27872 cp_token *token;
27873 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27874 if (colon)
27876 parser->colon_corrects_to_scope_p = false;
27877 *colon = false;
27879 while (1)
27881 tree name, decl;
27883 token = cp_lexer_peek_token (parser->lexer);
27884 name = cp_parser_id_expression (parser, /*template_p=*/false,
27885 /*check_dependency_p=*/true,
27886 /*template_p=*/NULL,
27887 /*declarator_p=*/false,
27888 /*optional_p=*/false);
27889 if (name == error_mark_node)
27890 goto skip_comma;
27892 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27893 if (decl == error_mark_node)
27894 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27895 token->location);
27896 else if (kind != 0)
27898 switch (kind)
27900 case OMP_CLAUSE__CACHE_:
27901 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27903 error_at (token->location, "expected %<[%>");
27904 decl = error_mark_node;
27905 break;
27907 /* FALL THROUGH. */
27908 case OMP_CLAUSE_MAP:
27909 case OMP_CLAUSE_FROM:
27910 case OMP_CLAUSE_TO:
27911 case OMP_CLAUSE_DEPEND:
27912 case OMP_CLAUSE_REDUCTION:
27913 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27915 tree low_bound = NULL_TREE, length = NULL_TREE;
27917 parser->colon_corrects_to_scope_p = false;
27918 cp_lexer_consume_token (parser->lexer);
27919 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27920 low_bound = cp_parser_expression (parser);
27921 if (!colon)
27922 parser->colon_corrects_to_scope_p
27923 = saved_colon_corrects_to_scope_p;
27924 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27925 length = integer_one_node;
27926 else
27928 /* Look for `:'. */
27929 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27930 goto skip_comma;
27931 if (!cp_lexer_next_token_is (parser->lexer,
27932 CPP_CLOSE_SQUARE))
27933 length = cp_parser_expression (parser);
27935 /* Look for the closing `]'. */
27936 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27937 RT_CLOSE_SQUARE))
27938 goto skip_comma;
27940 if (kind == OMP_CLAUSE__CACHE_)
27942 if (TREE_CODE (low_bound) != INTEGER_CST
27943 && !TREE_READONLY (low_bound))
27945 error_at (token->location,
27946 "%qD is not a constant", low_bound);
27947 decl = error_mark_node;
27950 if (TREE_CODE (length) != INTEGER_CST
27951 && !TREE_READONLY (length))
27953 error_at (token->location,
27954 "%qD is not a constant", length);
27955 decl = error_mark_node;
27959 decl = tree_cons (low_bound, length, decl);
27961 break;
27962 default:
27963 break;
27966 tree u = build_omp_clause (token->location, kind);
27967 OMP_CLAUSE_DECL (u) = decl;
27968 OMP_CLAUSE_CHAIN (u) = list;
27969 list = u;
27971 else
27972 list = tree_cons (decl, NULL_TREE, list);
27974 get_comma:
27975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27976 break;
27977 cp_lexer_consume_token (parser->lexer);
27980 if (colon)
27981 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27983 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27985 *colon = true;
27986 cp_parser_require (parser, CPP_COLON, RT_COLON);
27987 return list;
27990 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27992 int ending;
27994 /* Try to resync to an unnested comma. Copied from
27995 cp_parser_parenthesized_expression_list. */
27996 skip_comma:
27997 if (colon)
27998 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27999 ending = cp_parser_skip_to_closing_parenthesis (parser,
28000 /*recovering=*/true,
28001 /*or_comma=*/true,
28002 /*consume_paren=*/true);
28003 if (ending < 0)
28004 goto get_comma;
28007 return list;
28010 /* Similarly, but expect leading and trailing parenthesis. This is a very
28011 common case for omp clauses. */
28013 static tree
28014 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28016 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28017 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28018 return list;
28021 /* OpenACC 2.0:
28022 copy ( variable-list )
28023 copyin ( variable-list )
28024 copyout ( variable-list )
28025 create ( variable-list )
28026 delete ( variable-list )
28027 present ( variable-list )
28028 present_or_copy ( variable-list )
28029 pcopy ( variable-list )
28030 present_or_copyin ( variable-list )
28031 pcopyin ( variable-list )
28032 present_or_copyout ( variable-list )
28033 pcopyout ( variable-list )
28034 present_or_create ( variable-list )
28035 pcreate ( variable-list ) */
28037 static tree
28038 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28039 tree list)
28041 enum gomp_map_kind kind;
28042 switch (c_kind)
28044 case PRAGMA_OACC_CLAUSE_COPY:
28045 kind = GOMP_MAP_FORCE_TOFROM;
28046 break;
28047 case PRAGMA_OACC_CLAUSE_COPYIN:
28048 kind = GOMP_MAP_FORCE_TO;
28049 break;
28050 case PRAGMA_OACC_CLAUSE_COPYOUT:
28051 kind = GOMP_MAP_FORCE_FROM;
28052 break;
28053 case PRAGMA_OACC_CLAUSE_CREATE:
28054 kind = GOMP_MAP_FORCE_ALLOC;
28055 break;
28056 case PRAGMA_OACC_CLAUSE_DELETE:
28057 kind = GOMP_MAP_FORCE_DEALLOC;
28058 break;
28059 case PRAGMA_OACC_CLAUSE_DEVICE:
28060 kind = GOMP_MAP_FORCE_TO;
28061 break;
28062 case PRAGMA_OACC_CLAUSE_HOST:
28063 case PRAGMA_OACC_CLAUSE_SELF:
28064 kind = GOMP_MAP_FORCE_FROM;
28065 break;
28066 case PRAGMA_OACC_CLAUSE_PRESENT:
28067 kind = GOMP_MAP_FORCE_PRESENT;
28068 break;
28069 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28070 kind = GOMP_MAP_TOFROM;
28071 break;
28072 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28073 kind = GOMP_MAP_TO;
28074 break;
28075 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28076 kind = GOMP_MAP_FROM;
28077 break;
28078 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28079 kind = GOMP_MAP_ALLOC;
28080 break;
28081 default:
28082 gcc_unreachable ();
28084 tree nl, c;
28085 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28087 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28088 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28090 return nl;
28093 /* OpenACC 2.0:
28094 deviceptr ( variable-list ) */
28096 static tree
28097 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28099 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28100 tree vars, t;
28102 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28103 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28104 variable-list must only allow for pointer variables. */
28105 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28106 for (t = vars; t; t = TREE_CHAIN (t))
28108 tree v = TREE_PURPOSE (t);
28110 /* FIXME diagnostics: Ideally we should keep individual
28111 locations for all the variables in the var list to make the
28112 following errors more precise. Perhaps
28113 c_parser_omp_var_list_parens should construct a list of
28114 locations to go along with the var list. */
28116 if (!VAR_P (v))
28117 error_at (loc, "%qD is not a variable", v);
28118 else if (TREE_TYPE (v) == error_mark_node)
28120 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28121 error_at (loc, "%qD is not a pointer variable", v);
28123 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28124 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28125 OMP_CLAUSE_DECL (u) = v;
28126 OMP_CLAUSE_CHAIN (u) = list;
28127 list = u;
28130 return list;
28133 /* OpenACC:
28134 vector_length ( expression ) */
28136 static tree
28137 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28139 tree t, c;
28140 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28141 bool error = false;
28143 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28144 return list;
28146 t = cp_parser_condition (parser);
28147 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28149 error_at (location, "expected positive integer expression");
28150 error = true;
28153 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28156 /*or_comma=*/false,
28157 /*consume_paren=*/true);
28158 return list;
28161 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28162 location);
28164 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28165 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28166 OMP_CLAUSE_CHAIN (c) = list;
28167 list = c;
28169 return list;
28172 /* OpenACC 2.0
28173 Parse wait clause or directive parameters. */
28175 static tree
28176 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28178 vec<tree, va_gc> *args;
28179 tree t, args_tree;
28181 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28182 /*cast_p=*/false,
28183 /*allow_expansion_p=*/true,
28184 /*non_constant_p=*/NULL);
28186 if (args == NULL || args->length () == 0)
28188 cp_parser_error (parser, "expected integer expression before ')'");
28189 if (args != NULL)
28190 release_tree_vector (args);
28191 return list;
28194 args_tree = build_tree_list_vec (args);
28196 release_tree_vector (args);
28198 for (t = args_tree; t; t = TREE_CHAIN (t))
28200 tree targ = TREE_VALUE (t);
28202 if (targ != error_mark_node)
28204 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28205 error ("%<wait%> expression must be integral");
28206 else
28208 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28210 mark_rvalue_use (targ);
28211 OMP_CLAUSE_DECL (c) = targ;
28212 OMP_CLAUSE_CHAIN (c) = list;
28213 list = c;
28218 return list;
28221 /* OpenACC:
28222 wait ( int-expr-list ) */
28224 static tree
28225 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28227 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28229 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28230 return list;
28232 list = cp_parser_oacc_wait_list (parser, location, list);
28234 return list;
28237 /* OpenMP 3.0:
28238 collapse ( constant-expression ) */
28240 static tree
28241 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28243 tree c, num;
28244 location_t loc;
28245 HOST_WIDE_INT n;
28247 loc = cp_lexer_peek_token (parser->lexer)->location;
28248 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28249 return list;
28251 num = cp_parser_constant_expression (parser);
28253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28255 /*or_comma=*/false,
28256 /*consume_paren=*/true);
28258 if (num == error_mark_node)
28259 return list;
28260 num = fold_non_dependent_expr (num);
28261 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28262 || !tree_fits_shwi_p (num)
28263 || (n = tree_to_shwi (num)) <= 0
28264 || (int) n != n)
28266 error_at (loc, "collapse argument needs positive constant integer expression");
28267 return list;
28270 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28271 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28272 OMP_CLAUSE_CHAIN (c) = list;
28273 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28275 return c;
28278 /* OpenMP 2.5:
28279 default ( shared | none ) */
28281 static tree
28282 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28284 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28285 tree c;
28287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28288 return list;
28289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28292 const char *p = IDENTIFIER_POINTER (id);
28294 switch (p[0])
28296 case 'n':
28297 if (strcmp ("none", p) != 0)
28298 goto invalid_kind;
28299 kind = OMP_CLAUSE_DEFAULT_NONE;
28300 break;
28302 case 's':
28303 if (strcmp ("shared", p) != 0)
28304 goto invalid_kind;
28305 kind = OMP_CLAUSE_DEFAULT_SHARED;
28306 break;
28308 default:
28309 goto invalid_kind;
28312 cp_lexer_consume_token (parser->lexer);
28314 else
28316 invalid_kind:
28317 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28320 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28322 /*or_comma=*/false,
28323 /*consume_paren=*/true);
28325 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28326 return list;
28328 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28329 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28330 OMP_CLAUSE_CHAIN (c) = list;
28331 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28333 return c;
28336 /* OpenMP 3.1:
28337 final ( expression ) */
28339 static tree
28340 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28342 tree t, c;
28344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28345 return list;
28347 t = cp_parser_condition (parser);
28349 if (t == error_mark_node
28350 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28352 /*or_comma=*/false,
28353 /*consume_paren=*/true);
28355 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28357 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28358 OMP_CLAUSE_FINAL_EXPR (c) = t;
28359 OMP_CLAUSE_CHAIN (c) = list;
28361 return c;
28364 /* OpenMP 2.5:
28365 if ( expression ) */
28367 static tree
28368 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28370 tree t, c;
28372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28373 return list;
28375 t = cp_parser_condition (parser);
28377 if (t == error_mark_node
28378 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28379 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28380 /*or_comma=*/false,
28381 /*consume_paren=*/true);
28383 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28385 c = build_omp_clause (location, OMP_CLAUSE_IF);
28386 OMP_CLAUSE_IF_EXPR (c) = t;
28387 OMP_CLAUSE_CHAIN (c) = list;
28389 return c;
28392 /* OpenMP 3.1:
28393 mergeable */
28395 static tree
28396 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28397 tree list, location_t location)
28399 tree c;
28401 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28402 location);
28404 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28405 OMP_CLAUSE_CHAIN (c) = list;
28406 return c;
28409 /* OpenMP 2.5:
28410 nowait */
28412 static tree
28413 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28414 tree list, location_t location)
28416 tree c;
28418 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28420 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28421 OMP_CLAUSE_CHAIN (c) = list;
28422 return c;
28425 /* OpenACC:
28426 num_gangs ( expression ) */
28428 static tree
28429 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28431 tree t, c;
28432 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28434 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28435 return list;
28437 t = cp_parser_condition (parser);
28439 if (t == error_mark_node
28440 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28441 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28442 /*or_comma=*/false,
28443 /*consume_paren=*/true);
28445 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28447 error_at (location, "expected positive integer expression");
28448 return list;
28451 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28453 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28454 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28455 OMP_CLAUSE_CHAIN (c) = list;
28456 list = c;
28458 return list;
28461 /* OpenMP 2.5:
28462 num_threads ( expression ) */
28464 static tree
28465 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28466 location_t location)
28468 tree t, c;
28470 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28471 return list;
28473 t = cp_parser_expression (parser);
28475 if (t == error_mark_node
28476 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28478 /*or_comma=*/false,
28479 /*consume_paren=*/true);
28481 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28482 "num_threads", location);
28484 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28485 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28486 OMP_CLAUSE_CHAIN (c) = list;
28488 return c;
28491 /* OpenMP 4.1:
28492 num_tasks ( expression ) */
28494 static tree
28495 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
28496 location_t location)
28498 tree t, c;
28500 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28501 return list;
28503 t = cp_parser_expression (parser);
28505 if (t == error_mark_node
28506 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28508 /*or_comma=*/false,
28509 /*consume_paren=*/true);
28511 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
28512 "num_tasks", location);
28514 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
28515 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
28516 OMP_CLAUSE_CHAIN (c) = list;
28518 return c;
28521 /* OpenMP 4.1:
28522 grainsize ( expression ) */
28524 static tree
28525 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
28526 location_t location)
28528 tree t, c;
28530 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28531 return list;
28533 t = cp_parser_expression (parser);
28535 if (t == error_mark_node
28536 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28537 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28538 /*or_comma=*/false,
28539 /*consume_paren=*/true);
28541 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
28542 "grainsize", location);
28544 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
28545 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
28546 OMP_CLAUSE_CHAIN (c) = list;
28548 return c;
28551 /* OpenMP 4.1:
28552 priority ( expression ) */
28554 static tree
28555 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
28556 location_t location)
28558 tree t, c;
28560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28561 return list;
28563 t = cp_parser_expression (parser);
28565 if (t == error_mark_node
28566 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28568 /*or_comma=*/false,
28569 /*consume_paren=*/true);
28571 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
28572 "priority", location);
28574 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
28575 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
28576 OMP_CLAUSE_CHAIN (c) = list;
28578 return c;
28581 /* OpenMP 4.1:
28582 hint ( expression ) */
28584 static tree
28585 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
28586 location_t location)
28588 tree t, c;
28590 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28591 return list;
28593 t = cp_parser_expression (parser);
28595 if (t == error_mark_node
28596 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28597 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28598 /*or_comma=*/false,
28599 /*consume_paren=*/true);
28601 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
28603 c = build_omp_clause (location, OMP_CLAUSE_HINT);
28604 OMP_CLAUSE_HINT_EXPR (c) = t;
28605 OMP_CLAUSE_CHAIN (c) = list;
28607 return c;
28610 /* OpenMP 4.1:
28611 defaultmap ( tofrom : scalar ) */
28613 static tree
28614 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
28615 location_t location)
28617 tree c, id;
28618 const char *p;
28620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28621 return list;
28623 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28625 cp_parser_error (parser, "expected %<tofrom%>");
28626 goto out_err;
28628 id = cp_lexer_peek_token (parser->lexer)->u.value;
28629 p = IDENTIFIER_POINTER (id);
28630 if (strcmp (p, "tofrom") != 0)
28632 cp_parser_error (parser, "expected %<tofrom%>");
28633 goto out_err;
28635 cp_lexer_consume_token (parser->lexer);
28636 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28637 goto out_err;
28639 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28641 cp_parser_error (parser, "expected %<scalar%>");
28642 goto out_err;
28644 id = cp_lexer_peek_token (parser->lexer)->u.value;
28645 p = IDENTIFIER_POINTER (id);
28646 if (strcmp (p, "scalar") != 0)
28648 cp_parser_error (parser, "expected %<scalar%>");
28649 goto out_err;
28651 cp_lexer_consume_token (parser->lexer);
28652 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28653 goto out_err;
28655 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
28656 location);
28658 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
28659 OMP_CLAUSE_CHAIN (c) = list;
28660 return c;
28662 out_err:
28663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28664 /*or_comma=*/false,
28665 /*consume_paren=*/true);
28666 return list;
28669 /* OpenACC:
28670 num_workers ( expression ) */
28672 static tree
28673 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28675 tree t, c;
28676 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28679 return list;
28681 t = cp_parser_condition (parser);
28683 if (t == error_mark_node
28684 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28686 /*or_comma=*/false,
28687 /*consume_paren=*/true);
28689 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28691 error_at (location, "expected positive integer expression");
28692 return list;
28695 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28696 location);
28698 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28699 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28700 OMP_CLAUSE_CHAIN (c) = list;
28701 list = c;
28703 return list;
28706 /* OpenMP 2.5:
28707 ordered
28709 OpenMP 4.1:
28710 ordered ( constant-expression ) */
28712 static tree
28713 cp_parser_omp_clause_ordered (cp_parser *parser,
28714 tree list, location_t location)
28716 tree c, num = NULL_TREE;
28717 HOST_WIDE_INT n;
28719 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28720 "ordered", location);
28722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28724 cp_lexer_consume_token (parser->lexer);
28726 num = cp_parser_constant_expression (parser);
28728 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28730 /*or_comma=*/false,
28731 /*consume_paren=*/true);
28733 if (num == error_mark_node)
28734 return list;
28735 num = fold_non_dependent_expr (num);
28736 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28737 || !tree_fits_shwi_p (num)
28738 || (n = tree_to_shwi (num)) <= 0
28739 || (int) n != n)
28741 error_at (location,
28742 "ordered argument needs positive constant integer "
28743 "expression");
28744 return list;
28748 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28749 OMP_CLAUSE_ORDERED_EXPR (c) = num;
28750 OMP_CLAUSE_CHAIN (c) = list;
28751 return c;
28754 /* OpenMP 2.5:
28755 reduction ( reduction-operator : variable-list )
28757 reduction-operator:
28758 One of: + * - & ^ | && ||
28760 OpenMP 3.1:
28762 reduction-operator:
28763 One of: + * - & ^ | && || min max
28765 OpenMP 4.0:
28767 reduction-operator:
28768 One of: + * - & ^ | && ||
28769 id-expression */
28771 static tree
28772 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28774 enum tree_code code = ERROR_MARK;
28775 tree nlist, c, id = NULL_TREE;
28777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28778 return list;
28780 switch (cp_lexer_peek_token (parser->lexer)->type)
28782 case CPP_PLUS: code = PLUS_EXPR; break;
28783 case CPP_MULT: code = MULT_EXPR; break;
28784 case CPP_MINUS: code = MINUS_EXPR; break;
28785 case CPP_AND: code = BIT_AND_EXPR; break;
28786 case CPP_XOR: code = BIT_XOR_EXPR; break;
28787 case CPP_OR: code = BIT_IOR_EXPR; break;
28788 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28789 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28790 default: break;
28793 if (code != ERROR_MARK)
28794 cp_lexer_consume_token (parser->lexer);
28795 else
28797 bool saved_colon_corrects_to_scope_p;
28798 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28799 parser->colon_corrects_to_scope_p = false;
28800 id = cp_parser_id_expression (parser, /*template_p=*/false,
28801 /*check_dependency_p=*/true,
28802 /*template_p=*/NULL,
28803 /*declarator_p=*/false,
28804 /*optional_p=*/false);
28805 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28806 if (identifier_p (id))
28808 const char *p = IDENTIFIER_POINTER (id);
28810 if (strcmp (p, "min") == 0)
28811 code = MIN_EXPR;
28812 else if (strcmp (p, "max") == 0)
28813 code = MAX_EXPR;
28814 else if (id == ansi_opname (PLUS_EXPR))
28815 code = PLUS_EXPR;
28816 else if (id == ansi_opname (MULT_EXPR))
28817 code = MULT_EXPR;
28818 else if (id == ansi_opname (MINUS_EXPR))
28819 code = MINUS_EXPR;
28820 else if (id == ansi_opname (BIT_AND_EXPR))
28821 code = BIT_AND_EXPR;
28822 else if (id == ansi_opname (BIT_IOR_EXPR))
28823 code = BIT_IOR_EXPR;
28824 else if (id == ansi_opname (BIT_XOR_EXPR))
28825 code = BIT_XOR_EXPR;
28826 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28827 code = TRUTH_ANDIF_EXPR;
28828 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28829 code = TRUTH_ORIF_EXPR;
28830 id = omp_reduction_id (code, id, NULL_TREE);
28831 tree scope = parser->scope;
28832 if (scope)
28833 id = build_qualified_name (NULL_TREE, scope, id, false);
28834 parser->scope = NULL_TREE;
28835 parser->qualifying_scope = NULL_TREE;
28836 parser->object_scope = NULL_TREE;
28838 else
28840 error ("invalid reduction-identifier");
28841 resync_fail:
28842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28843 /*or_comma=*/false,
28844 /*consume_paren=*/true);
28845 return list;
28849 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28850 goto resync_fail;
28852 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28853 NULL);
28854 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28856 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28857 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28860 return nlist;
28863 /* OpenMP 2.5:
28864 schedule ( schedule-kind )
28865 schedule ( schedule-kind , expression )
28867 schedule-kind:
28868 static | dynamic | guided | runtime | auto
28870 OpenMP 4.1:
28871 schedule ( schedule-modifier : schedule-kind )
28872 schedule ( schedule-modifier : schedule-kind , expression )
28874 schedule-modifier:
28875 simd */
28877 static tree
28878 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28880 tree c, t;
28882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28883 return list;
28885 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28889 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28890 const char *p = IDENTIFIER_POINTER (id);
28891 if (strcmp ("simd", p) == 0
28892 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
28894 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
28895 cp_lexer_consume_token (parser->lexer);
28896 cp_lexer_consume_token (parser->lexer);
28900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28903 const char *p = IDENTIFIER_POINTER (id);
28905 switch (p[0])
28907 case 'd':
28908 if (strcmp ("dynamic", p) != 0)
28909 goto invalid_kind;
28910 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28911 break;
28913 case 'g':
28914 if (strcmp ("guided", p) != 0)
28915 goto invalid_kind;
28916 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28917 break;
28919 case 'r':
28920 if (strcmp ("runtime", p) != 0)
28921 goto invalid_kind;
28922 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28923 break;
28925 default:
28926 goto invalid_kind;
28929 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28930 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28931 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28932 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28933 else
28934 goto invalid_kind;
28935 cp_lexer_consume_token (parser->lexer);
28937 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28939 cp_token *token;
28940 cp_lexer_consume_token (parser->lexer);
28942 token = cp_lexer_peek_token (parser->lexer);
28943 t = cp_parser_assignment_expression (parser);
28945 if (t == error_mark_node)
28946 goto resync_fail;
28947 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28948 error_at (token->location, "schedule %<runtime%> does not take "
28949 "a %<chunk_size%> parameter");
28950 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28951 error_at (token->location, "schedule %<auto%> does not take "
28952 "a %<chunk_size%> parameter");
28953 else
28954 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28956 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28957 goto resync_fail;
28959 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28960 goto resync_fail;
28962 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28963 OMP_CLAUSE_CHAIN (c) = list;
28964 return c;
28966 invalid_kind:
28967 cp_parser_error (parser, "invalid schedule kind");
28968 resync_fail:
28969 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28970 /*or_comma=*/false,
28971 /*consume_paren=*/true);
28972 return list;
28975 /* OpenMP 3.0:
28976 untied */
28978 static tree
28979 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28980 tree list, location_t location)
28982 tree c;
28984 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28986 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28987 OMP_CLAUSE_CHAIN (c) = list;
28988 return c;
28991 /* OpenMP 4.0:
28992 inbranch
28993 notinbranch */
28995 static tree
28996 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28997 tree list, location_t location)
28999 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29000 tree c = build_omp_clause (location, code);
29001 OMP_CLAUSE_CHAIN (c) = list;
29002 return c;
29005 /* OpenMP 4.0:
29006 parallel
29008 sections
29009 taskgroup */
29011 static tree
29012 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
29013 enum omp_clause_code code,
29014 tree list, location_t location)
29016 tree c = build_omp_clause (location, code);
29017 OMP_CLAUSE_CHAIN (c) = list;
29018 return c;
29021 /* OpenMP 4.1:
29022 nogroup */
29024 static tree
29025 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
29026 tree list, location_t location)
29028 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
29029 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
29030 OMP_CLAUSE_CHAIN (c) = list;
29031 return c;
29034 /* OpenMP 4.1:
29035 simd
29036 threads */
29038 static tree
29039 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
29040 enum omp_clause_code code,
29041 tree list, location_t location)
29043 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29044 tree c = build_omp_clause (location, code);
29045 OMP_CLAUSE_CHAIN (c) = list;
29046 return c;
29049 /* OpenMP 4.0:
29050 num_teams ( expression ) */
29052 static tree
29053 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
29054 location_t location)
29056 tree t, c;
29058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29059 return list;
29061 t = cp_parser_expression (parser);
29063 if (t == error_mark_node
29064 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29065 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29066 /*or_comma=*/false,
29067 /*consume_paren=*/true);
29069 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
29070 "num_teams", location);
29072 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
29073 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
29074 OMP_CLAUSE_CHAIN (c) = list;
29076 return c;
29079 /* OpenMP 4.0:
29080 thread_limit ( expression ) */
29082 static tree
29083 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
29084 location_t location)
29086 tree t, c;
29088 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29089 return list;
29091 t = cp_parser_expression (parser);
29093 if (t == error_mark_node
29094 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29095 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29096 /*or_comma=*/false,
29097 /*consume_paren=*/true);
29099 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
29100 "thread_limit", location);
29102 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
29103 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
29104 OMP_CLAUSE_CHAIN (c) = list;
29106 return c;
29109 /* OpenMP 4.0:
29110 aligned ( variable-list )
29111 aligned ( variable-list : constant-expression ) */
29113 static tree
29114 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
29116 tree nlist, c, alignment = NULL_TREE;
29117 bool colon;
29119 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29120 return list;
29122 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
29123 &colon);
29125 if (colon)
29127 alignment = cp_parser_constant_expression (parser);
29129 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29131 /*or_comma=*/false,
29132 /*consume_paren=*/true);
29134 if (alignment == error_mark_node)
29135 alignment = NULL_TREE;
29138 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29139 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
29141 return nlist;
29144 /* OpenMP 4.0:
29145 linear ( variable-list )
29146 linear ( variable-list : expression )
29148 OpenMP 4.1:
29149 linear ( modifier ( variable-list ) )
29150 linear ( modifier ( variable-list ) : expression ) */
29152 static tree
29153 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
29154 bool is_cilk_simd_fn)
29156 tree nlist, c, step = integer_one_node;
29157 bool colon;
29158 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
29160 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29161 return list;
29163 if (!is_cilk_simd_fn
29164 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29166 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29167 const char *p = IDENTIFIER_POINTER (id);
29169 if (strcmp ("ref", p) == 0)
29170 kind = OMP_CLAUSE_LINEAR_REF;
29171 else if (strcmp ("val", p) == 0)
29172 kind = OMP_CLAUSE_LINEAR_VAL;
29173 else if (strcmp ("uval", p) == 0)
29174 kind = OMP_CLAUSE_LINEAR_UVAL;
29175 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29176 cp_lexer_consume_token (parser->lexer);
29177 else
29178 kind = OMP_CLAUSE_LINEAR_DEFAULT;
29181 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
29182 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
29183 &colon);
29184 else
29186 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
29187 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
29188 if (colon)
29189 cp_parser_require (parser, CPP_COLON, RT_COLON);
29190 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29191 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29192 /*or_comma=*/false,
29193 /*consume_paren=*/true);
29196 if (colon)
29198 step = cp_parser_expression (parser);
29200 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
29202 sorry ("using parameters for %<linear%> step is not supported yet");
29203 step = integer_one_node;
29205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29207 /*or_comma=*/false,
29208 /*consume_paren=*/true);
29210 if (step == error_mark_node)
29211 return list;
29214 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29216 OMP_CLAUSE_LINEAR_STEP (c) = step;
29217 OMP_CLAUSE_LINEAR_KIND (c) = kind;
29220 return nlist;
29223 /* OpenMP 4.0:
29224 safelen ( constant-expression ) */
29226 static tree
29227 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
29228 location_t location)
29230 tree t, c;
29232 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29233 return list;
29235 t = cp_parser_constant_expression (parser);
29237 if (t == error_mark_node
29238 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29239 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29240 /*or_comma=*/false,
29241 /*consume_paren=*/true);
29243 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
29245 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
29246 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
29247 OMP_CLAUSE_CHAIN (c) = list;
29249 return c;
29252 /* OpenMP 4.0:
29253 simdlen ( constant-expression ) */
29255 static tree
29256 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
29257 location_t location)
29259 tree t, c;
29261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29262 return list;
29264 t = cp_parser_constant_expression (parser);
29266 if (t == error_mark_node
29267 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29269 /*or_comma=*/false,
29270 /*consume_paren=*/true);
29272 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
29274 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
29275 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
29276 OMP_CLAUSE_CHAIN (c) = list;
29278 return c;
29281 /* OpenMP 4.0:
29282 depend ( depend-kind : variable-list )
29284 depend-kind:
29285 in | out | inout
29287 OpenMP 4.1:
29288 depend ( depend-loop-kind [ : vec ] )
29290 depend-loop-kind:
29291 source | sink */
29293 static tree
29294 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
29296 tree nlist, c;
29297 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29300 return list;
29302 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29304 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29305 const char *p = IDENTIFIER_POINTER (id);
29307 if (strcmp ("in", p) == 0)
29308 kind = OMP_CLAUSE_DEPEND_IN;
29309 else if (strcmp ("inout", p) == 0)
29310 kind = OMP_CLAUSE_DEPEND_INOUT;
29311 else if (strcmp ("out", p) == 0)
29312 kind = OMP_CLAUSE_DEPEND_OUT;
29313 else if (strcmp ("source", p) == 0)
29314 kind = OMP_CLAUSE_DEPEND_SOURCE;
29315 else if (strcmp ("sink", p) == 0)
29316 kind = OMP_CLAUSE_DEPEND_SINK;
29317 else
29318 goto invalid_kind;
29320 else
29321 goto invalid_kind;
29323 cp_lexer_consume_token (parser->lexer);
29325 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
29327 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
29328 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29329 OMP_CLAUSE_DECL (c) = NULL_TREE;
29330 OMP_CLAUSE_CHAIN (c) = list;
29331 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29332 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29333 /*or_comma=*/false,
29334 /*consume_paren=*/true);
29335 return c;
29338 /* FIXME: Handle OMP_CLAUSE_DEPEND_SINK. */
29340 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29341 goto resync_fail;
29343 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29344 NULL);
29346 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29347 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29349 return nlist;
29351 invalid_kind:
29352 cp_parser_error (parser, "invalid depend kind");
29353 resync_fail:
29354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29355 /*or_comma=*/false,
29356 /*consume_paren=*/true);
29357 return list;
29360 /* OpenMP 4.0:
29361 map ( map-kind : variable-list )
29362 map ( variable-list )
29364 map-kind:
29365 alloc | to | from | tofrom
29367 OpenMP 4.1:
29368 map-kind:
29369 alloc | to | from | tofrom | release | delete
29371 map ( always [,] map-kind: variable-list ) */
29373 static tree
29374 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29376 tree nlist, c;
29377 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29378 bool always = false;
29380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29381 return list;
29383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29386 const char *p = IDENTIFIER_POINTER (id);
29388 if (strcmp ("always", p) == 0)
29390 int nth = 2;
29391 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
29392 nth++;
29393 if (cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
29394 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
29395 == CPP_COLON))
29397 always = true;
29398 cp_lexer_consume_token (parser->lexer);
29399 if (nth == 3)
29400 cp_lexer_consume_token (parser->lexer);
29405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29406 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29408 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29409 const char *p = IDENTIFIER_POINTER (id);
29411 if (strcmp ("alloc", p) == 0)
29412 kind = GOMP_MAP_ALLOC;
29413 else if (strcmp ("to", p) == 0)
29414 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
29415 else if (strcmp ("from", p) == 0)
29416 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
29417 else if (strcmp ("tofrom", p) == 0)
29418 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
29419 else if (strcmp ("release", p) == 0)
29420 kind = GOMP_MAP_RELEASE;
29421 else if (strcmp ("delete", p) == 0)
29422 kind = GOMP_MAP_DELETE;
29423 else
29425 cp_parser_error (parser, "invalid map kind");
29426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29427 /*or_comma=*/false,
29428 /*consume_paren=*/true);
29429 return list;
29431 cp_lexer_consume_token (parser->lexer);
29432 cp_lexer_consume_token (parser->lexer);
29435 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29436 NULL);
29438 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29439 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29441 return nlist;
29444 /* OpenMP 4.0:
29445 device ( expression ) */
29447 static tree
29448 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29449 location_t location)
29451 tree t, c;
29453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29454 return list;
29456 t = cp_parser_expression (parser);
29458 if (t == error_mark_node
29459 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29460 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29461 /*or_comma=*/false,
29462 /*consume_paren=*/true);
29464 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29465 "device", location);
29467 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29468 OMP_CLAUSE_DEVICE_ID (c) = t;
29469 OMP_CLAUSE_CHAIN (c) = list;
29471 return c;
29474 /* OpenMP 4.0:
29475 dist_schedule ( static )
29476 dist_schedule ( static , expression ) */
29478 static tree
29479 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29480 location_t location)
29482 tree c, t;
29484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29485 return list;
29487 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29489 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29490 goto invalid_kind;
29491 cp_lexer_consume_token (parser->lexer);
29493 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29495 cp_lexer_consume_token (parser->lexer);
29497 t = cp_parser_assignment_expression (parser);
29499 if (t == error_mark_node)
29500 goto resync_fail;
29501 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29504 goto resync_fail;
29506 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29507 goto resync_fail;
29509 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29510 location);
29511 OMP_CLAUSE_CHAIN (c) = list;
29512 return c;
29514 invalid_kind:
29515 cp_parser_error (parser, "invalid dist_schedule kind");
29516 resync_fail:
29517 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29518 /*or_comma=*/false,
29519 /*consume_paren=*/true);
29520 return list;
29523 /* OpenMP 4.0:
29524 proc_bind ( proc-bind-kind )
29526 proc-bind-kind:
29527 master | close | spread */
29529 static tree
29530 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29531 location_t location)
29533 tree c;
29534 enum omp_clause_proc_bind_kind kind;
29536 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29537 return list;
29539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29541 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29542 const char *p = IDENTIFIER_POINTER (id);
29544 if (strcmp ("master", p) == 0)
29545 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29546 else if (strcmp ("close", p) == 0)
29547 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29548 else if (strcmp ("spread", p) == 0)
29549 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29550 else
29551 goto invalid_kind;
29553 else
29554 goto invalid_kind;
29556 cp_lexer_consume_token (parser->lexer);
29557 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29558 goto resync_fail;
29560 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29561 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29562 location);
29563 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29564 OMP_CLAUSE_CHAIN (c) = list;
29565 return c;
29567 invalid_kind:
29568 cp_parser_error (parser, "invalid depend kind");
29569 resync_fail:
29570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29571 /*or_comma=*/false,
29572 /*consume_paren=*/true);
29573 return list;
29576 /* OpenACC:
29577 async [( int-expr )] */
29579 static tree
29580 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29582 tree c, t;
29583 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29585 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29587 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29589 cp_lexer_consume_token (parser->lexer);
29591 t = cp_parser_expression (parser);
29592 if (t == error_mark_node
29593 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29594 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29595 /*or_comma=*/false,
29596 /*consume_paren=*/true);
29599 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29601 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29602 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29603 OMP_CLAUSE_CHAIN (c) = list;
29604 list = c;
29606 return list;
29609 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29610 is a bitmask in MASK. Return the list of clauses found. */
29612 static tree
29613 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29614 const char *where, cp_token *pragma_tok,
29615 bool finish_p = true)
29617 tree clauses = NULL;
29618 bool first = true;
29620 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29622 location_t here;
29623 pragma_omp_clause c_kind;
29624 const char *c_name;
29625 tree prev = clauses;
29627 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29628 cp_lexer_consume_token (parser->lexer);
29630 here = cp_lexer_peek_token (parser->lexer)->location;
29631 c_kind = cp_parser_omp_clause_name (parser);
29633 switch (c_kind)
29635 case PRAGMA_OACC_CLAUSE_ASYNC:
29636 clauses = cp_parser_oacc_clause_async (parser, clauses);
29637 c_name = "async";
29638 break;
29639 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29640 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29641 c_name = "collapse";
29642 break;
29643 case PRAGMA_OACC_CLAUSE_COPY:
29644 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29645 c_name = "copy";
29646 break;
29647 case PRAGMA_OACC_CLAUSE_COPYIN:
29648 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29649 c_name = "copyin";
29650 break;
29651 case PRAGMA_OACC_CLAUSE_COPYOUT:
29652 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29653 c_name = "copyout";
29654 break;
29655 case PRAGMA_OACC_CLAUSE_CREATE:
29656 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29657 c_name = "create";
29658 break;
29659 case PRAGMA_OACC_CLAUSE_DELETE:
29660 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29661 c_name = "delete";
29662 break;
29663 case PRAGMA_OACC_CLAUSE_DEVICE:
29664 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29665 c_name = "device";
29666 break;
29667 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29668 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29669 c_name = "deviceptr";
29670 break;
29671 case PRAGMA_OACC_CLAUSE_HOST:
29672 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29673 c_name = "host";
29674 break;
29675 case PRAGMA_OACC_CLAUSE_IF:
29676 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29677 c_name = "if";
29678 break;
29679 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29680 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29681 c_name = "num_gangs";
29682 break;
29683 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29684 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29685 c_name = "num_workers";
29686 break;
29687 case PRAGMA_OACC_CLAUSE_PRESENT:
29688 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29689 c_name = "present";
29690 break;
29691 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29692 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29693 c_name = "present_or_copy";
29694 break;
29695 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29696 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29697 c_name = "present_or_copyin";
29698 break;
29699 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29700 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29701 c_name = "present_or_copyout";
29702 break;
29703 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29704 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29705 c_name = "present_or_create";
29706 break;
29707 case PRAGMA_OACC_CLAUSE_REDUCTION:
29708 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29709 c_name = "reduction";
29710 break;
29711 case PRAGMA_OACC_CLAUSE_SELF:
29712 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29713 c_name = "self";
29714 break;
29715 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29716 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29717 c_name = "vector_length";
29718 break;
29719 case PRAGMA_OACC_CLAUSE_WAIT:
29720 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29721 c_name = "wait";
29722 break;
29723 default:
29724 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29725 goto saw_error;
29728 first = false;
29730 if (((mask >> c_kind) & 1) == 0)
29732 /* Remove the invalid clause(s) from the list to avoid
29733 confusing the rest of the compiler. */
29734 clauses = prev;
29735 error_at (here, "%qs is not valid for %qs", c_name, where);
29739 saw_error:
29740 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29742 if (finish_p)
29743 return finish_omp_clauses (clauses, false);
29745 return clauses;
29748 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29749 is a bitmask in MASK. Return the list of clauses found; the result
29750 of clause default goes in *pdefault. */
29752 static tree
29753 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29754 const char *where, cp_token *pragma_tok,
29755 bool finish_p = true)
29757 tree clauses = NULL;
29758 bool first = true;
29759 cp_token *token = NULL;
29760 bool cilk_simd_fn = false;
29762 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29764 pragma_omp_clause c_kind;
29765 const char *c_name;
29766 tree prev = clauses;
29768 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29769 cp_lexer_consume_token (parser->lexer);
29771 token = cp_lexer_peek_token (parser->lexer);
29772 c_kind = cp_parser_omp_clause_name (parser);
29774 switch (c_kind)
29776 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29777 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29778 token->location);
29779 c_name = "collapse";
29780 break;
29781 case PRAGMA_OMP_CLAUSE_COPYIN:
29782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29783 c_name = "copyin";
29784 break;
29785 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29786 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29787 clauses);
29788 c_name = "copyprivate";
29789 break;
29790 case PRAGMA_OMP_CLAUSE_DEFAULT:
29791 clauses = cp_parser_omp_clause_default (parser, clauses,
29792 token->location);
29793 c_name = "default";
29794 break;
29795 case PRAGMA_OMP_CLAUSE_FINAL:
29796 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29797 c_name = "final";
29798 break;
29799 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29800 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29801 clauses);
29802 c_name = "firstprivate";
29803 break;
29804 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
29805 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
29806 token->location);
29807 c_name = "grainsize";
29808 break;
29809 case PRAGMA_OMP_CLAUSE_HINT:
29810 clauses = cp_parser_omp_clause_hint (parser, clauses,
29811 token->location);
29812 c_name = "hint";
29813 break;
29814 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
29815 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
29816 token->location);
29817 c_name = "defaultmap";
29818 break;
29819 case PRAGMA_OMP_CLAUSE_IF:
29820 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29821 c_name = "if";
29822 break;
29823 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29824 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29825 clauses);
29826 c_name = "lastprivate";
29827 break;
29828 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29829 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29830 token->location);
29831 c_name = "mergeable";
29832 break;
29833 case PRAGMA_OMP_CLAUSE_NOWAIT:
29834 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29835 c_name = "nowait";
29836 break;
29837 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
29838 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
29839 token->location);
29840 c_name = "num_tasks";
29841 break;
29842 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29843 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29844 token->location);
29845 c_name = "num_threads";
29846 break;
29847 case PRAGMA_OMP_CLAUSE_ORDERED:
29848 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29849 token->location);
29850 c_name = "ordered";
29851 break;
29852 case PRAGMA_OMP_CLAUSE_PRIORITY:
29853 clauses = cp_parser_omp_clause_priority (parser, clauses,
29854 token->location);
29855 c_name = "priority";
29856 break;
29857 case PRAGMA_OMP_CLAUSE_PRIVATE:
29858 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29859 clauses);
29860 c_name = "private";
29861 break;
29862 case PRAGMA_OMP_CLAUSE_REDUCTION:
29863 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29864 c_name = "reduction";
29865 break;
29866 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29867 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29868 token->location);
29869 c_name = "schedule";
29870 break;
29871 case PRAGMA_OMP_CLAUSE_SHARED:
29872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29873 clauses);
29874 c_name = "shared";
29875 break;
29876 case PRAGMA_OMP_CLAUSE_UNTIED:
29877 clauses = cp_parser_omp_clause_untied (parser, clauses,
29878 token->location);
29879 c_name = "untied";
29880 break;
29881 case PRAGMA_OMP_CLAUSE_INBRANCH:
29882 case PRAGMA_CILK_CLAUSE_MASK:
29883 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29884 clauses, token->location);
29885 c_name = "inbranch";
29886 break;
29887 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29888 case PRAGMA_CILK_CLAUSE_NOMASK:
29889 clauses = cp_parser_omp_clause_branch (parser,
29890 OMP_CLAUSE_NOTINBRANCH,
29891 clauses, token->location);
29892 c_name = "notinbranch";
29893 break;
29894 case PRAGMA_OMP_CLAUSE_PARALLEL:
29895 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29896 clauses, token->location);
29897 c_name = "parallel";
29898 if (!first)
29900 clause_not_first:
29901 error_at (token->location, "%qs must be the first clause of %qs",
29902 c_name, where);
29903 clauses = prev;
29905 break;
29906 case PRAGMA_OMP_CLAUSE_FOR:
29907 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29908 clauses, token->location);
29909 c_name = "for";
29910 if (!first)
29911 goto clause_not_first;
29912 break;
29913 case PRAGMA_OMP_CLAUSE_SECTIONS:
29914 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29915 clauses, token->location);
29916 c_name = "sections";
29917 if (!first)
29918 goto clause_not_first;
29919 break;
29920 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29921 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29922 clauses, token->location);
29923 c_name = "taskgroup";
29924 if (!first)
29925 goto clause_not_first;
29926 break;
29927 case PRAGMA_OMP_CLAUSE_TO:
29928 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29929 clauses);
29930 c_name = "to";
29931 break;
29932 case PRAGMA_OMP_CLAUSE_FROM:
29933 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29934 clauses);
29935 c_name = "from";
29936 break;
29937 case PRAGMA_OMP_CLAUSE_UNIFORM:
29938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29939 clauses);
29940 c_name = "uniform";
29941 break;
29942 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29943 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29944 token->location);
29945 c_name = "num_teams";
29946 break;
29947 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29948 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29949 token->location);
29950 c_name = "thread_limit";
29951 break;
29952 case PRAGMA_OMP_CLAUSE_ALIGNED:
29953 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29954 c_name = "aligned";
29955 break;
29956 case PRAGMA_OMP_CLAUSE_LINEAR:
29957 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29958 cilk_simd_fn = true;
29959 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29960 c_name = "linear";
29961 break;
29962 case PRAGMA_OMP_CLAUSE_DEPEND:
29963 clauses = cp_parser_omp_clause_depend (parser, clauses,
29964 token->location);
29965 c_name = "depend";
29966 break;
29967 case PRAGMA_OMP_CLAUSE_MAP:
29968 clauses = cp_parser_omp_clause_map (parser, clauses);
29969 c_name = "map";
29970 break;
29971 case PRAGMA_OMP_CLAUSE_DEVICE:
29972 clauses = cp_parser_omp_clause_device (parser, clauses,
29973 token->location);
29974 c_name = "device";
29975 break;
29976 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29977 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29978 token->location);
29979 c_name = "dist_schedule";
29980 break;
29981 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29982 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29983 token->location);
29984 c_name = "proc_bind";
29985 break;
29986 case PRAGMA_OMP_CLAUSE_SAFELEN:
29987 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29988 token->location);
29989 c_name = "safelen";
29990 break;
29991 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29992 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29993 token->location);
29994 c_name = "simdlen";
29995 break;
29996 case PRAGMA_OMP_CLAUSE_NOGROUP:
29997 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
29998 token->location);
29999 c_name = "nogroup";
30000 break;
30001 case PRAGMA_OMP_CLAUSE_THREADS:
30002 clauses
30003 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
30004 clauses, token->location);
30005 c_name = "threads";
30006 break;
30007 case PRAGMA_OMP_CLAUSE_SIMD:
30008 clauses
30009 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
30010 clauses, token->location);
30011 c_name = "simd";
30012 break;
30013 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
30014 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
30015 c_name = "simdlen";
30016 break;
30017 default:
30018 cp_parser_error (parser, "expected %<#pragma omp%> clause");
30019 goto saw_error;
30022 first = false;
30024 if (((mask >> c_kind) & 1) == 0)
30026 /* Remove the invalid clause(s) from the list to avoid
30027 confusing the rest of the compiler. */
30028 clauses = prev;
30029 error_at (token->location, "%qs is not valid for %qs", c_name, where);
30032 saw_error:
30033 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
30034 no reason to skip to the end. */
30035 if (!(flag_cilkplus && pragma_tok == NULL))
30036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30037 if (finish_p)
30039 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
30040 return finish_omp_clauses (clauses, false, true);
30041 else
30042 return finish_omp_clauses (clauses, true);
30044 return clauses;
30047 /* OpenMP 2.5:
30048 structured-block:
30049 statement
30051 In practice, we're also interested in adding the statement to an
30052 outer node. So it is convenient if we work around the fact that
30053 cp_parser_statement calls add_stmt. */
30055 static unsigned
30056 cp_parser_begin_omp_structured_block (cp_parser *parser)
30058 unsigned save = parser->in_statement;
30060 /* Only move the values to IN_OMP_BLOCK if they weren't false.
30061 This preserves the "not within loop or switch" style error messages
30062 for nonsense cases like
30063 void foo() {
30064 #pragma omp single
30065 break;
30068 if (parser->in_statement)
30069 parser->in_statement = IN_OMP_BLOCK;
30071 return save;
30074 static void
30075 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
30077 parser->in_statement = save;
30080 static tree
30081 cp_parser_omp_structured_block (cp_parser *parser)
30083 tree stmt = begin_omp_structured_block ();
30084 unsigned int save = cp_parser_begin_omp_structured_block (parser);
30086 cp_parser_statement (parser, NULL_TREE, false, NULL);
30088 cp_parser_end_omp_structured_block (parser, save);
30089 return finish_omp_structured_block (stmt);
30092 /* OpenMP 2.5:
30093 # pragma omp atomic new-line
30094 expression-stmt
30096 expression-stmt:
30097 x binop= expr | x++ | ++x | x-- | --x
30098 binop:
30099 +, *, -, /, &, ^, |, <<, >>
30101 where x is an lvalue expression with scalar type.
30103 OpenMP 3.1:
30104 # pragma omp atomic new-line
30105 update-stmt
30107 # pragma omp atomic read new-line
30108 read-stmt
30110 # pragma omp atomic write new-line
30111 write-stmt
30113 # pragma omp atomic update new-line
30114 update-stmt
30116 # pragma omp atomic capture new-line
30117 capture-stmt
30119 # pragma omp atomic capture new-line
30120 capture-block
30122 read-stmt:
30123 v = x
30124 write-stmt:
30125 x = expr
30126 update-stmt:
30127 expression-stmt | x = x binop expr
30128 capture-stmt:
30129 v = expression-stmt
30130 capture-block:
30131 { v = x; update-stmt; } | { update-stmt; v = x; }
30133 OpenMP 4.0:
30134 update-stmt:
30135 expression-stmt | x = x binop expr | x = expr binop x
30136 capture-stmt:
30137 v = update-stmt
30138 capture-block:
30139 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
30141 where x and v are lvalue expressions with scalar type. */
30143 static void
30144 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
30146 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
30147 tree rhs1 = NULL_TREE, orig_lhs;
30148 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
30149 bool structured_block = false;
30150 bool seq_cst = false;
30152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30155 const char *p = IDENTIFIER_POINTER (id);
30157 if (!strcmp (p, "seq_cst"))
30159 seq_cst = true;
30160 cp_lexer_consume_token (parser->lexer);
30161 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30162 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30163 cp_lexer_consume_token (parser->lexer);
30166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30169 const char *p = IDENTIFIER_POINTER (id);
30171 if (!strcmp (p, "read"))
30172 code = OMP_ATOMIC_READ;
30173 else if (!strcmp (p, "write"))
30174 code = NOP_EXPR;
30175 else if (!strcmp (p, "update"))
30176 code = OMP_ATOMIC;
30177 else if (!strcmp (p, "capture"))
30178 code = OMP_ATOMIC_CAPTURE_NEW;
30179 else
30180 p = NULL;
30181 if (p)
30182 cp_lexer_consume_token (parser->lexer);
30184 if (!seq_cst)
30186 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30187 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30188 cp_lexer_consume_token (parser->lexer);
30190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30193 const char *p = IDENTIFIER_POINTER (id);
30195 if (!strcmp (p, "seq_cst"))
30197 seq_cst = true;
30198 cp_lexer_consume_token (parser->lexer);
30202 cp_parser_require_pragma_eol (parser, pragma_tok);
30204 switch (code)
30206 case OMP_ATOMIC_READ:
30207 case NOP_EXPR: /* atomic write */
30208 v = cp_parser_unary_expression (parser);
30209 if (v == error_mark_node)
30210 goto saw_error;
30211 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30212 goto saw_error;
30213 if (code == NOP_EXPR)
30214 lhs = cp_parser_expression (parser);
30215 else
30216 lhs = cp_parser_unary_expression (parser);
30217 if (lhs == error_mark_node)
30218 goto saw_error;
30219 if (code == NOP_EXPR)
30221 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
30222 opcode. */
30223 code = OMP_ATOMIC;
30224 rhs = lhs;
30225 lhs = v;
30226 v = NULL_TREE;
30228 goto done;
30229 case OMP_ATOMIC_CAPTURE_NEW:
30230 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30232 cp_lexer_consume_token (parser->lexer);
30233 structured_block = true;
30235 else
30237 v = cp_parser_unary_expression (parser);
30238 if (v == error_mark_node)
30239 goto saw_error;
30240 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30241 goto saw_error;
30243 default:
30244 break;
30247 restart:
30248 lhs = cp_parser_unary_expression (parser);
30249 orig_lhs = lhs;
30250 switch (TREE_CODE (lhs))
30252 case ERROR_MARK:
30253 goto saw_error;
30255 case POSTINCREMENT_EXPR:
30256 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30257 code = OMP_ATOMIC_CAPTURE_OLD;
30258 /* FALLTHROUGH */
30259 case PREINCREMENT_EXPR:
30260 lhs = TREE_OPERAND (lhs, 0);
30261 opcode = PLUS_EXPR;
30262 rhs = integer_one_node;
30263 break;
30265 case POSTDECREMENT_EXPR:
30266 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30267 code = OMP_ATOMIC_CAPTURE_OLD;
30268 /* FALLTHROUGH */
30269 case PREDECREMENT_EXPR:
30270 lhs = TREE_OPERAND (lhs, 0);
30271 opcode = MINUS_EXPR;
30272 rhs = integer_one_node;
30273 break;
30275 case COMPOUND_EXPR:
30276 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
30277 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
30278 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
30279 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
30280 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
30281 (TREE_OPERAND (lhs, 1), 0), 0)))
30282 == BOOLEAN_TYPE)
30283 /* Undo effects of boolean_increment for post {in,de}crement. */
30284 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
30285 /* FALLTHRU */
30286 case MODIFY_EXPR:
30287 if (TREE_CODE (lhs) == MODIFY_EXPR
30288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
30290 /* Undo effects of boolean_increment. */
30291 if (integer_onep (TREE_OPERAND (lhs, 1)))
30293 /* This is pre or post increment. */
30294 rhs = TREE_OPERAND (lhs, 1);
30295 lhs = TREE_OPERAND (lhs, 0);
30296 opcode = NOP_EXPR;
30297 if (code == OMP_ATOMIC_CAPTURE_NEW
30298 && !structured_block
30299 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
30300 code = OMP_ATOMIC_CAPTURE_OLD;
30301 break;
30304 /* FALLTHRU */
30305 default:
30306 switch (cp_lexer_peek_token (parser->lexer)->type)
30308 case CPP_MULT_EQ:
30309 opcode = MULT_EXPR;
30310 break;
30311 case CPP_DIV_EQ:
30312 opcode = TRUNC_DIV_EXPR;
30313 break;
30314 case CPP_PLUS_EQ:
30315 opcode = PLUS_EXPR;
30316 break;
30317 case CPP_MINUS_EQ:
30318 opcode = MINUS_EXPR;
30319 break;
30320 case CPP_LSHIFT_EQ:
30321 opcode = LSHIFT_EXPR;
30322 break;
30323 case CPP_RSHIFT_EQ:
30324 opcode = RSHIFT_EXPR;
30325 break;
30326 case CPP_AND_EQ:
30327 opcode = BIT_AND_EXPR;
30328 break;
30329 case CPP_OR_EQ:
30330 opcode = BIT_IOR_EXPR;
30331 break;
30332 case CPP_XOR_EQ:
30333 opcode = BIT_XOR_EXPR;
30334 break;
30335 case CPP_EQ:
30336 enum cp_parser_prec oprec;
30337 cp_token *token;
30338 cp_lexer_consume_token (parser->lexer);
30339 cp_parser_parse_tentatively (parser);
30340 rhs1 = cp_parser_simple_cast_expression (parser);
30341 if (rhs1 == error_mark_node)
30343 cp_parser_abort_tentative_parse (parser);
30344 cp_parser_simple_cast_expression (parser);
30345 goto saw_error;
30347 token = cp_lexer_peek_token (parser->lexer);
30348 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
30350 cp_parser_abort_tentative_parse (parser);
30351 cp_parser_parse_tentatively (parser);
30352 rhs = cp_parser_binary_expression (parser, false, true,
30353 PREC_NOT_OPERATOR, NULL);
30354 if (rhs == error_mark_node)
30356 cp_parser_abort_tentative_parse (parser);
30357 cp_parser_binary_expression (parser, false, true,
30358 PREC_NOT_OPERATOR, NULL);
30359 goto saw_error;
30361 switch (TREE_CODE (rhs))
30363 case MULT_EXPR:
30364 case TRUNC_DIV_EXPR:
30365 case RDIV_EXPR:
30366 case PLUS_EXPR:
30367 case MINUS_EXPR:
30368 case LSHIFT_EXPR:
30369 case RSHIFT_EXPR:
30370 case BIT_AND_EXPR:
30371 case BIT_IOR_EXPR:
30372 case BIT_XOR_EXPR:
30373 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
30375 if (cp_parser_parse_definitely (parser))
30377 opcode = TREE_CODE (rhs);
30378 rhs1 = TREE_OPERAND (rhs, 0);
30379 rhs = TREE_OPERAND (rhs, 1);
30380 goto stmt_done;
30382 else
30383 goto saw_error;
30385 break;
30386 default:
30387 break;
30389 cp_parser_abort_tentative_parse (parser);
30390 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
30392 rhs = cp_parser_expression (parser);
30393 if (rhs == error_mark_node)
30394 goto saw_error;
30395 opcode = NOP_EXPR;
30396 rhs1 = NULL_TREE;
30397 goto stmt_done;
30399 cp_parser_error (parser,
30400 "invalid form of %<#pragma omp atomic%>");
30401 goto saw_error;
30403 if (!cp_parser_parse_definitely (parser))
30404 goto saw_error;
30405 switch (token->type)
30407 case CPP_SEMICOLON:
30408 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30410 code = OMP_ATOMIC_CAPTURE_OLD;
30411 v = lhs;
30412 lhs = NULL_TREE;
30413 lhs1 = rhs1;
30414 rhs1 = NULL_TREE;
30415 cp_lexer_consume_token (parser->lexer);
30416 goto restart;
30418 else if (structured_block)
30420 opcode = NOP_EXPR;
30421 rhs = rhs1;
30422 rhs1 = NULL_TREE;
30423 goto stmt_done;
30425 cp_parser_error (parser,
30426 "invalid form of %<#pragma omp atomic%>");
30427 goto saw_error;
30428 case CPP_MULT:
30429 opcode = MULT_EXPR;
30430 break;
30431 case CPP_DIV:
30432 opcode = TRUNC_DIV_EXPR;
30433 break;
30434 case CPP_PLUS:
30435 opcode = PLUS_EXPR;
30436 break;
30437 case CPP_MINUS:
30438 opcode = MINUS_EXPR;
30439 break;
30440 case CPP_LSHIFT:
30441 opcode = LSHIFT_EXPR;
30442 break;
30443 case CPP_RSHIFT:
30444 opcode = RSHIFT_EXPR;
30445 break;
30446 case CPP_AND:
30447 opcode = BIT_AND_EXPR;
30448 break;
30449 case CPP_OR:
30450 opcode = BIT_IOR_EXPR;
30451 break;
30452 case CPP_XOR:
30453 opcode = BIT_XOR_EXPR;
30454 break;
30455 default:
30456 cp_parser_error (parser,
30457 "invalid operator for %<#pragma omp atomic%>");
30458 goto saw_error;
30460 oprec = TOKEN_PRECEDENCE (token);
30461 gcc_assert (oprec != PREC_NOT_OPERATOR);
30462 if (commutative_tree_code (opcode))
30463 oprec = (enum cp_parser_prec) (oprec - 1);
30464 cp_lexer_consume_token (parser->lexer);
30465 rhs = cp_parser_binary_expression (parser, false, false,
30466 oprec, NULL);
30467 if (rhs == error_mark_node)
30468 goto saw_error;
30469 goto stmt_done;
30470 /* FALLTHROUGH */
30471 default:
30472 cp_parser_error (parser,
30473 "invalid operator for %<#pragma omp atomic%>");
30474 goto saw_error;
30476 cp_lexer_consume_token (parser->lexer);
30478 rhs = cp_parser_expression (parser);
30479 if (rhs == error_mark_node)
30480 goto saw_error;
30481 break;
30483 stmt_done:
30484 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30486 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30487 goto saw_error;
30488 v = cp_parser_unary_expression (parser);
30489 if (v == error_mark_node)
30490 goto saw_error;
30491 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30492 goto saw_error;
30493 lhs1 = cp_parser_unary_expression (parser);
30494 if (lhs1 == error_mark_node)
30495 goto saw_error;
30497 if (structured_block)
30499 cp_parser_consume_semicolon_at_end_of_statement (parser);
30500 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30502 done:
30503 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30504 if (!structured_block)
30505 cp_parser_consume_semicolon_at_end_of_statement (parser);
30506 return;
30508 saw_error:
30509 cp_parser_skip_to_end_of_block_or_statement (parser);
30510 if (structured_block)
30512 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30513 cp_lexer_consume_token (parser->lexer);
30514 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30516 cp_parser_skip_to_end_of_block_or_statement (parser);
30517 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30518 cp_lexer_consume_token (parser->lexer);
30524 /* OpenMP 2.5:
30525 # pragma omp barrier new-line */
30527 static void
30528 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30530 cp_parser_require_pragma_eol (parser, pragma_tok);
30531 finish_omp_barrier ();
30534 /* OpenMP 2.5:
30535 # pragma omp critical [(name)] new-line
30536 structured-block
30538 OpenMP 4.1:
30539 # pragma omp critical [(name) [hint(expression)]] new-line
30540 structured-block */
30542 #define OMP_CRITICAL_CLAUSE_MASK \
30543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
30545 static tree
30546 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30548 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
30550 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30552 cp_lexer_consume_token (parser->lexer);
30554 name = cp_parser_identifier (parser);
30556 if (name == error_mark_node
30557 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30559 /*or_comma=*/false,
30560 /*consume_paren=*/true);
30561 if (name == error_mark_node)
30562 name = NULL;
30564 clauses = cp_parser_omp_all_clauses (parser,
30565 OMP_CRITICAL_CLAUSE_MASK,
30566 "#pragma omp critical", pragma_tok);
30568 else
30569 cp_parser_require_pragma_eol (parser, pragma_tok);
30571 stmt = cp_parser_omp_structured_block (parser);
30572 return c_finish_omp_critical (input_location, stmt, name, clauses);
30575 /* OpenMP 2.5:
30576 # pragma omp flush flush-vars[opt] new-line
30578 flush-vars:
30579 ( variable-list ) */
30581 static void
30582 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30585 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30586 cp_parser_require_pragma_eol (parser, pragma_tok);
30588 finish_omp_flush ();
30591 /* Helper function, to parse omp for increment expression. */
30593 static tree
30594 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30596 tree cond = cp_parser_binary_expression (parser, false, true,
30597 PREC_NOT_OPERATOR, NULL);
30598 if (cond == error_mark_node
30599 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30601 cp_parser_skip_to_end_of_statement (parser);
30602 return error_mark_node;
30605 switch (TREE_CODE (cond))
30607 case GT_EXPR:
30608 case GE_EXPR:
30609 case LT_EXPR:
30610 case LE_EXPR:
30611 break;
30612 case NE_EXPR:
30613 if (code == CILK_SIMD || code == CILK_FOR)
30614 break;
30615 /* Fall through: OpenMP disallows NE_EXPR. */
30616 default:
30617 return error_mark_node;
30620 /* If decl is an iterator, preserve LHS and RHS of the relational
30621 expr until finish_omp_for. */
30622 if (decl
30623 && (type_dependent_expression_p (decl)
30624 || CLASS_TYPE_P (TREE_TYPE (decl))))
30625 return cond;
30627 return build_x_binary_op (input_location, TREE_CODE (cond),
30628 TREE_OPERAND (cond, 0), ERROR_MARK,
30629 TREE_OPERAND (cond, 1), ERROR_MARK,
30630 /*overload=*/NULL, tf_warning_or_error);
30633 /* Helper function, to parse omp for increment expression. */
30635 static tree
30636 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30638 cp_token *token = cp_lexer_peek_token (parser->lexer);
30639 enum tree_code op;
30640 tree lhs, rhs;
30641 cp_id_kind idk;
30642 bool decl_first;
30644 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30646 op = (token->type == CPP_PLUS_PLUS
30647 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30648 cp_lexer_consume_token (parser->lexer);
30649 lhs = cp_parser_simple_cast_expression (parser);
30650 if (lhs != decl
30651 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
30652 return error_mark_node;
30653 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30656 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30657 if (lhs != decl
30658 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
30659 return error_mark_node;
30661 token = cp_lexer_peek_token (parser->lexer);
30662 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30664 op = (token->type == CPP_PLUS_PLUS
30665 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30666 cp_lexer_consume_token (parser->lexer);
30667 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30670 op = cp_parser_assignment_operator_opt (parser);
30671 if (op == ERROR_MARK)
30672 return error_mark_node;
30674 if (op != NOP_EXPR)
30676 rhs = cp_parser_assignment_expression (parser);
30677 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30678 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30681 lhs = cp_parser_binary_expression (parser, false, false,
30682 PREC_ADDITIVE_EXPRESSION, NULL);
30683 token = cp_lexer_peek_token (parser->lexer);
30684 decl_first = (lhs == decl
30685 || (processing_template_decl && cp_tree_equal (lhs, decl)));
30686 if (decl_first)
30687 lhs = NULL_TREE;
30688 if (token->type != CPP_PLUS
30689 && token->type != CPP_MINUS)
30690 return error_mark_node;
30694 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30695 cp_lexer_consume_token (parser->lexer);
30696 rhs = cp_parser_binary_expression (parser, false, false,
30697 PREC_ADDITIVE_EXPRESSION, NULL);
30698 token = cp_lexer_peek_token (parser->lexer);
30699 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30701 if (lhs == NULL_TREE)
30703 if (op == PLUS_EXPR)
30704 lhs = rhs;
30705 else
30706 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30707 tf_warning_or_error);
30709 else
30710 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30711 ERROR_MARK, NULL, tf_warning_or_error);
30714 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30716 if (!decl_first)
30718 if ((rhs != decl
30719 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
30720 || op == MINUS_EXPR)
30721 return error_mark_node;
30722 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30724 else
30725 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30727 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30730 /* Parse the initialization statement of either an OpenMP for loop or
30731 a Cilk Plus for loop.
30733 Return true if the resulting construct should have an
30734 OMP_CLAUSE_PRIVATE added to it. */
30736 static tree
30737 cp_parser_omp_for_loop_init (cp_parser *parser,
30738 enum tree_code code,
30739 tree &this_pre_body,
30740 vec<tree, va_gc> *for_block,
30741 tree &init,
30742 tree &decl,
30743 tree &real_decl)
30745 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30746 return NULL_TREE;
30748 tree add_private_clause = NULL_TREE;
30750 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30752 init-expr:
30753 var = lb
30754 integer-type var = lb
30755 random-access-iterator-type var = lb
30756 pointer-type var = lb
30758 cp_decl_specifier_seq type_specifiers;
30760 /* First, try to parse as an initialized declaration. See
30761 cp_parser_condition, from whence the bulk of this is copied. */
30763 cp_parser_parse_tentatively (parser);
30764 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30765 /*is_trailing_return=*/false,
30766 &type_specifiers);
30767 if (cp_parser_parse_definitely (parser))
30769 /* If parsing a type specifier seq succeeded, then this
30770 MUST be a initialized declaration. */
30771 tree asm_specification, attributes;
30772 cp_declarator *declarator;
30774 declarator = cp_parser_declarator (parser,
30775 CP_PARSER_DECLARATOR_NAMED,
30776 /*ctor_dtor_or_conv_p=*/NULL,
30777 /*parenthesized_p=*/NULL,
30778 /*member_p=*/false,
30779 /*friend_p=*/false);
30780 attributes = cp_parser_attributes_opt (parser);
30781 asm_specification = cp_parser_asm_specification_opt (parser);
30783 if (declarator == cp_error_declarator)
30784 cp_parser_skip_to_end_of_statement (parser);
30786 else
30788 tree pushed_scope, auto_node;
30790 decl = start_decl (declarator, &type_specifiers,
30791 SD_INITIALIZED, attributes,
30792 /*prefix_attributes=*/NULL_TREE,
30793 &pushed_scope);
30795 auto_node = type_uses_auto (TREE_TYPE (decl));
30796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30798 if (cp_lexer_next_token_is (parser->lexer,
30799 CPP_OPEN_PAREN))
30801 if (code != CILK_SIMD && code != CILK_FOR)
30802 error ("parenthesized initialization is not allowed in "
30803 "OpenMP %<for%> loop");
30804 else
30805 error ("parenthesized initialization is "
30806 "not allowed in for-loop");
30808 else
30809 /* Trigger an error. */
30810 cp_parser_require (parser, CPP_EQ, RT_EQ);
30812 init = error_mark_node;
30813 cp_parser_skip_to_end_of_statement (parser);
30815 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30816 || type_dependent_expression_p (decl)
30817 || auto_node)
30819 bool is_direct_init, is_non_constant_init;
30821 init = cp_parser_initializer (parser,
30822 &is_direct_init,
30823 &is_non_constant_init);
30825 if (auto_node)
30827 TREE_TYPE (decl)
30828 = do_auto_deduction (TREE_TYPE (decl), init,
30829 auto_node);
30831 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30832 && !type_dependent_expression_p (decl))
30833 goto non_class;
30836 cp_finish_decl (decl, init, !is_non_constant_init,
30837 asm_specification,
30838 LOOKUP_ONLYCONVERTING);
30839 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30841 vec_safe_push (for_block, this_pre_body);
30842 init = NULL_TREE;
30844 else
30845 init = pop_stmt_list (this_pre_body);
30846 this_pre_body = NULL_TREE;
30848 else
30850 /* Consume '='. */
30851 cp_lexer_consume_token (parser->lexer);
30852 init = cp_parser_assignment_expression (parser);
30854 non_class:
30855 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30856 init = error_mark_node;
30857 else
30858 cp_finish_decl (decl, NULL_TREE,
30859 /*init_const_expr_p=*/false,
30860 asm_specification,
30861 LOOKUP_ONLYCONVERTING);
30864 if (pushed_scope)
30865 pop_scope (pushed_scope);
30868 else
30870 cp_id_kind idk;
30871 /* If parsing a type specifier sequence failed, then
30872 this MUST be a simple expression. */
30873 if (code == CILK_FOR)
30874 error ("%<_Cilk_for%> allows expression instead of declaration only "
30875 "in C, not in C++");
30876 cp_parser_parse_tentatively (parser);
30877 decl = cp_parser_primary_expression (parser, false, false,
30878 false, &idk);
30879 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
30880 if (!cp_parser_error_occurred (parser)
30881 && decl
30882 && (TREE_CODE (decl) == COMPONENT_REF
30883 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
30885 cp_parser_abort_tentative_parse (parser);
30886 cp_parser_parse_tentatively (parser);
30887 cp_token *token = cp_lexer_peek_token (parser->lexer);
30888 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
30889 /*check_dependency_p=*/true,
30890 /*template_p=*/NULL,
30891 /*declarator_p=*/false,
30892 /*optional_p=*/false);
30893 if (name != error_mark_node
30894 && last_tok == cp_lexer_peek_token (parser->lexer))
30896 decl = cp_parser_lookup_name_simple (parser, name,
30897 token->location);
30898 if (TREE_CODE (decl) == FIELD_DECL)
30899 add_private_clause = omp_privatize_field (decl);
30901 cp_parser_abort_tentative_parse (parser);
30902 cp_parser_parse_tentatively (parser);
30903 decl = cp_parser_primary_expression (parser, false, false,
30904 false, &idk);
30906 if (!cp_parser_error_occurred (parser)
30907 && decl
30908 && DECL_P (decl)
30909 && CLASS_TYPE_P (TREE_TYPE (decl)))
30911 tree rhs;
30913 cp_parser_parse_definitely (parser);
30914 cp_parser_require (parser, CPP_EQ, RT_EQ);
30915 rhs = cp_parser_assignment_expression (parser);
30916 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30917 decl, NOP_EXPR,
30918 rhs,
30919 tf_warning_or_error));
30920 if (!add_private_clause)
30921 add_private_clause = decl;
30923 else
30925 decl = NULL;
30926 cp_parser_abort_tentative_parse (parser);
30927 init = cp_parser_expression (parser);
30928 if (init)
30930 if (TREE_CODE (init) == MODIFY_EXPR
30931 || TREE_CODE (init) == MODOP_EXPR)
30932 real_decl = TREE_OPERAND (init, 0);
30936 return add_private_clause;
30939 /* Parse the restricted form of the for statement allowed by OpenMP. */
30941 static tree
30942 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30943 tree *cclauses)
30945 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30946 tree real_decl, initv, condv, incrv, declv;
30947 tree this_pre_body, cl;
30948 location_t loc_first;
30949 bool collapse_err = false;
30950 int i, collapse = 1, ordered = 0, count, nbraces = 0;
30951 vec<tree, va_gc> *for_block = make_tree_vector ();
30953 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30954 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30955 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30956 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
30957 && OMP_CLAUSE_ORDERED_EXPR (cl))
30958 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
30960 gcc_assert (collapse >= 1 && ordered >= 0);
30961 count = collapse + (ordered > 0 ? ordered - 1 : 0);
30963 declv = make_tree_vec (count);
30964 initv = make_tree_vec (count);
30965 condv = make_tree_vec (count);
30966 incrv = make_tree_vec (count);
30968 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30970 for (i = 0; i < count; i++)
30972 int bracecount = 0;
30973 tree add_private_clause = NULL_TREE;
30974 location_t loc;
30976 if (code != CILK_FOR
30977 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30979 cp_parser_error (parser, "for statement expected");
30980 return NULL;
30982 if (code == CILK_FOR
30983 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30985 cp_parser_error (parser, "_Cilk_for statement expected");
30986 return NULL;
30988 loc = cp_lexer_consume_token (parser->lexer)->location;
30990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30991 return NULL;
30993 init = decl = real_decl = NULL;
30994 this_pre_body = push_stmt_list ();
30996 add_private_clause
30997 = cp_parser_omp_for_loop_init (parser, code,
30998 this_pre_body, for_block,
30999 init, decl, real_decl);
31001 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31002 if (this_pre_body)
31004 this_pre_body = pop_stmt_list (this_pre_body);
31005 if (pre_body)
31007 tree t = pre_body;
31008 pre_body = push_stmt_list ();
31009 add_stmt (t);
31010 add_stmt (this_pre_body);
31011 pre_body = pop_stmt_list (pre_body);
31013 else
31014 pre_body = this_pre_body;
31017 if (decl)
31018 real_decl = decl;
31019 if (cclauses != NULL
31020 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
31021 && real_decl != NULL_TREE)
31023 tree *c;
31024 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
31025 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
31026 && OMP_CLAUSE_DECL (*c) == real_decl)
31028 error_at (loc, "iteration variable %qD"
31029 " should not be firstprivate", real_decl);
31030 *c = OMP_CLAUSE_CHAIN (*c);
31032 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
31033 && OMP_CLAUSE_DECL (*c) == real_decl)
31035 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
31036 tree l = *c;
31037 *c = OMP_CLAUSE_CHAIN (*c);
31038 if (code == OMP_SIMD)
31040 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31041 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
31043 else
31045 OMP_CLAUSE_CHAIN (l) = clauses;
31046 clauses = l;
31048 add_private_clause = NULL_TREE;
31050 else
31052 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
31053 && OMP_CLAUSE_DECL (*c) == real_decl)
31054 add_private_clause = NULL_TREE;
31055 c = &OMP_CLAUSE_CHAIN (*c);
31059 if (add_private_clause)
31061 tree c;
31062 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31064 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
31065 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
31066 && OMP_CLAUSE_DECL (c) == decl)
31067 break;
31068 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
31069 && OMP_CLAUSE_DECL (c) == decl)
31070 error_at (loc, "iteration variable %qD "
31071 "should not be firstprivate",
31072 decl);
31073 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
31074 && OMP_CLAUSE_DECL (c) == decl)
31075 error_at (loc, "iteration variable %qD should not be reduction",
31076 decl);
31078 if (c == NULL)
31080 if (code != OMP_SIMD)
31081 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
31082 else if (collapse == 1)
31083 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31084 else
31085 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
31086 OMP_CLAUSE_DECL (c) = add_private_clause;
31087 c = finish_omp_clauses (c, true);
31088 if (c)
31090 OMP_CLAUSE_CHAIN (c) = clauses;
31091 clauses = c;
31092 /* For linear, signal that we need to fill up
31093 the so far unknown linear step. */
31094 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
31095 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
31100 cond = NULL;
31101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31102 cond = cp_parser_omp_for_cond (parser, decl, code);
31103 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31105 incr = NULL;
31106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31108 /* If decl is an iterator, preserve the operator on decl
31109 until finish_omp_for. */
31110 if (real_decl
31111 && ((processing_template_decl
31112 && (TREE_TYPE (real_decl) == NULL_TREE
31113 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
31114 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
31115 incr = cp_parser_omp_for_incr (parser, real_decl);
31116 else
31117 incr = cp_parser_expression (parser);
31118 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
31119 SET_EXPR_LOCATION (incr, input_location);
31122 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31124 /*or_comma=*/false,
31125 /*consume_paren=*/true);
31127 TREE_VEC_ELT (declv, i) = decl;
31128 TREE_VEC_ELT (initv, i) = init;
31129 TREE_VEC_ELT (condv, i) = cond;
31130 TREE_VEC_ELT (incrv, i) = incr;
31132 if (i == count - 1)
31133 break;
31135 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
31136 in between the collapsed for loops to be still considered perfectly
31137 nested. Hopefully the final version clarifies this.
31138 For now handle (multiple) {'s and empty statements. */
31139 cp_parser_parse_tentatively (parser);
31142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31143 break;
31144 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31146 cp_lexer_consume_token (parser->lexer);
31147 bracecount++;
31149 else if (bracecount
31150 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31151 cp_lexer_consume_token (parser->lexer);
31152 else
31154 loc = cp_lexer_peek_token (parser->lexer)->location;
31155 error_at (loc, "not enough collapsed for loops");
31156 collapse_err = true;
31157 cp_parser_abort_tentative_parse (parser);
31158 declv = NULL_TREE;
31159 break;
31162 while (1);
31164 if (declv)
31166 cp_parser_parse_definitely (parser);
31167 nbraces += bracecount;
31171 /* Note that we saved the original contents of this flag when we entered
31172 the structured block, and so we don't need to re-save it here. */
31173 if (code == CILK_SIMD || code == CILK_FOR)
31174 parser->in_statement = IN_CILK_SIMD_FOR;
31175 else
31176 parser->in_statement = IN_OMP_FOR;
31178 /* Note that the grammar doesn't call for a structured block here,
31179 though the loop as a whole is a structured block. */
31180 body = push_stmt_list ();
31181 cp_parser_statement (parser, NULL_TREE, false, NULL);
31182 body = pop_stmt_list (body);
31184 if (declv == NULL_TREE)
31185 ret = NULL_TREE;
31186 else
31187 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
31188 pre_body, clauses);
31190 while (nbraces)
31192 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31194 cp_lexer_consume_token (parser->lexer);
31195 nbraces--;
31197 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31198 cp_lexer_consume_token (parser->lexer);
31199 else
31201 if (!collapse_err)
31203 error_at (cp_lexer_peek_token (parser->lexer)->location,
31204 "collapsed loops not perfectly nested");
31206 collapse_err = true;
31207 cp_parser_statement_seq_opt (parser, NULL);
31208 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
31209 break;
31213 while (!for_block->is_empty ())
31214 add_stmt (pop_stmt_list (for_block->pop ()));
31215 release_tree_vector (for_block);
31217 return ret;
31220 /* Helper function for OpenMP parsing, split clauses and call
31221 finish_omp_clauses on each of the set of clauses afterwards. */
31223 static void
31224 cp_omp_split_clauses (location_t loc, enum tree_code code,
31225 omp_clause_mask mask, tree clauses, tree *cclauses)
31227 int i;
31228 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
31229 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
31230 if (cclauses[i])
31231 cclauses[i] = finish_omp_clauses (cclauses[i], true);
31234 /* OpenMP 4.0:
31235 #pragma omp simd simd-clause[optseq] new-line
31236 for-loop */
31238 #define OMP_SIMD_CLAUSE_MASK \
31239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
31240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31248 static tree
31249 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
31250 char *p_name, omp_clause_mask mask, tree *cclauses)
31252 tree clauses, sb, ret;
31253 unsigned int save;
31254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31256 strcat (p_name, " simd");
31257 mask |= OMP_SIMD_CLAUSE_MASK;
31258 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
31260 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31261 cclauses == NULL);
31262 if (cclauses)
31264 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
31265 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
31268 sb = begin_omp_structured_block ();
31269 save = cp_parser_begin_omp_structured_block (parser);
31271 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
31273 cp_parser_end_omp_structured_block (parser, save);
31274 add_stmt (finish_omp_structured_block (sb));
31276 return ret;
31279 /* OpenMP 2.5:
31280 #pragma omp for for-clause[optseq] new-line
31281 for-loop
31283 OpenMP 4.0:
31284 #pragma omp for simd for-simd-clause[optseq] new-line
31285 for-loop */
31287 #define OMP_FOR_CLAUSE_MASK \
31288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
31294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
31295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
31296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31298 static tree
31299 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
31300 char *p_name, omp_clause_mask mask, tree *cclauses)
31302 tree clauses, sb, ret;
31303 unsigned int save;
31304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31306 strcat (p_name, " for");
31307 mask |= OMP_FOR_CLAUSE_MASK;
31308 if (cclauses)
31309 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31311 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31314 const char *p = IDENTIFIER_POINTER (id);
31316 if (strcmp (p, "simd") == 0)
31318 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31319 if (cclauses == NULL)
31320 cclauses = cclauses_buf;
31322 cp_lexer_consume_token (parser->lexer);
31323 if (!flag_openmp) /* flag_openmp_simd */
31324 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31325 cclauses);
31326 sb = begin_omp_structured_block ();
31327 save = cp_parser_begin_omp_structured_block (parser);
31328 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31329 cclauses);
31330 cp_parser_end_omp_structured_block (parser, save);
31331 tree body = finish_omp_structured_block (sb);
31332 if (ret == NULL)
31333 return ret;
31334 ret = make_node (OMP_FOR);
31335 TREE_TYPE (ret) = void_type_node;
31336 OMP_FOR_BODY (ret) = body;
31337 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31338 SET_EXPR_LOCATION (ret, loc);
31339 add_stmt (ret);
31340 return ret;
31343 if (!flag_openmp) /* flag_openmp_simd */
31345 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31346 return NULL_TREE;
31349 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31350 cclauses == NULL);
31351 if (cclauses)
31353 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
31354 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31357 sb = begin_omp_structured_block ();
31358 save = cp_parser_begin_omp_structured_block (parser);
31360 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
31362 cp_parser_end_omp_structured_block (parser, save);
31363 add_stmt (finish_omp_structured_block (sb));
31365 return ret;
31368 /* OpenMP 2.5:
31369 # pragma omp master new-line
31370 structured-block */
31372 static tree
31373 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
31375 cp_parser_require_pragma_eol (parser, pragma_tok);
31376 return c_finish_omp_master (input_location,
31377 cp_parser_omp_structured_block (parser));
31380 /* OpenMP 2.5:
31381 # pragma omp ordered new-line
31382 structured-block
31384 OpenMP 4.1:
31385 # pragma omp ordered ordered-clauses new-line
31386 structured-block */
31388 #define OMP_ORDERED_CLAUSE_MASK \
31389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
31390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
31392 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
31393 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
31395 static bool
31396 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
31397 enum pragma_context context)
31399 location_t loc = pragma_tok->location;
31401 if (context != pragma_stmt && context != pragma_compound)
31403 cp_parser_error (parser, "expected declaration specifiers");
31404 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31405 return false;
31408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31411 const char *p = IDENTIFIER_POINTER (id);
31413 if (strcmp (p, "depend") == 0)
31415 if (context == pragma_stmt)
31417 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
31418 "%<depend%> clause may only be used in compound "
31419 "statements");
31420 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31421 return false;
31423 tree clauses
31424 = cp_parser_omp_all_clauses (parser,
31425 OMP_ORDERED_DEPEND_CLAUSE_MASK,
31426 "#pragma omp ordered", pragma_tok);
31427 c_finish_omp_ordered (loc, clauses, NULL_TREE);
31428 return false;
31432 tree clauses
31433 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
31434 "#pragma omp ordered", pragma_tok);
31435 c_finish_omp_ordered (loc, clauses,
31436 cp_parser_omp_structured_block (parser));
31437 return true;
31440 /* OpenMP 2.5:
31442 section-scope:
31443 { section-sequence }
31445 section-sequence:
31446 section-directive[opt] structured-block
31447 section-sequence section-directive structured-block */
31449 static tree
31450 cp_parser_omp_sections_scope (cp_parser *parser)
31452 tree stmt, substmt;
31453 bool error_suppress = false;
31454 cp_token *tok;
31456 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
31457 return NULL_TREE;
31459 stmt = push_stmt_list ();
31461 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
31463 substmt = cp_parser_omp_structured_block (parser);
31464 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31465 add_stmt (substmt);
31468 while (1)
31470 tok = cp_lexer_peek_token (parser->lexer);
31471 if (tok->type == CPP_CLOSE_BRACE)
31472 break;
31473 if (tok->type == CPP_EOF)
31474 break;
31476 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
31478 cp_lexer_consume_token (parser->lexer);
31479 cp_parser_require_pragma_eol (parser, tok);
31480 error_suppress = false;
31482 else if (!error_suppress)
31484 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
31485 error_suppress = true;
31488 substmt = cp_parser_omp_structured_block (parser);
31489 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31490 add_stmt (substmt);
31492 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31494 substmt = pop_stmt_list (stmt);
31496 stmt = make_node (OMP_SECTIONS);
31497 TREE_TYPE (stmt) = void_type_node;
31498 OMP_SECTIONS_BODY (stmt) = substmt;
31500 add_stmt (stmt);
31501 return stmt;
31504 /* OpenMP 2.5:
31505 # pragma omp sections sections-clause[optseq] newline
31506 sections-scope */
31508 #define OMP_SECTIONS_CLAUSE_MASK \
31509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31515 static tree
31516 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
31517 char *p_name, omp_clause_mask mask, tree *cclauses)
31519 tree clauses, ret;
31520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31522 strcat (p_name, " sections");
31523 mask |= OMP_SECTIONS_CLAUSE_MASK;
31524 if (cclauses)
31525 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31527 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31528 cclauses == NULL);
31529 if (cclauses)
31531 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31532 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31535 ret = cp_parser_omp_sections_scope (parser);
31536 if (ret)
31537 OMP_SECTIONS_CLAUSES (ret) = clauses;
31539 return ret;
31542 /* OpenMP 2.5:
31543 # pragma omp parallel parallel-clause[optseq] new-line
31544 structured-block
31545 # pragma omp parallel for parallel-for-clause[optseq] new-line
31546 structured-block
31547 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31548 structured-block
31550 OpenMP 4.0:
31551 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31552 structured-block */
31554 #define OMP_PARALLEL_CLAUSE_MASK \
31555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31565 static tree
31566 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31567 char *p_name, omp_clause_mask mask, tree *cclauses)
31569 tree stmt, clauses, block;
31570 unsigned int save;
31571 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31573 strcat (p_name, " parallel");
31574 mask |= OMP_PARALLEL_CLAUSE_MASK;
31576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31578 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31579 if (cclauses == NULL)
31580 cclauses = cclauses_buf;
31582 cp_lexer_consume_token (parser->lexer);
31583 if (!flag_openmp) /* flag_openmp_simd */
31584 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31585 block = begin_omp_parallel ();
31586 save = cp_parser_begin_omp_structured_block (parser);
31587 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31588 cp_parser_end_omp_structured_block (parser, save);
31589 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31590 block);
31591 if (ret == NULL_TREE)
31592 return ret;
31593 OMP_PARALLEL_COMBINED (stmt) = 1;
31594 return stmt;
31596 else if (cclauses)
31598 error_at (loc, "expected %<for%> after %qs", p_name);
31599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31600 return NULL_TREE;
31602 else if (!flag_openmp) /* flag_openmp_simd */
31604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31605 return NULL_TREE;
31607 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31610 const char *p = IDENTIFIER_POINTER (id);
31611 if (strcmp (p, "sections") == 0)
31613 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31614 cclauses = cclauses_buf;
31616 cp_lexer_consume_token (parser->lexer);
31617 block = begin_omp_parallel ();
31618 save = cp_parser_begin_omp_structured_block (parser);
31619 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31620 cp_parser_end_omp_structured_block (parser, save);
31621 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31622 block);
31623 OMP_PARALLEL_COMBINED (stmt) = 1;
31624 return stmt;
31628 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31630 block = begin_omp_parallel ();
31631 save = cp_parser_begin_omp_structured_block (parser);
31632 cp_parser_statement (parser, NULL_TREE, false, NULL);
31633 cp_parser_end_omp_structured_block (parser, save);
31634 stmt = finish_omp_parallel (clauses, block);
31635 return stmt;
31638 /* OpenMP 2.5:
31639 # pragma omp single single-clause[optseq] new-line
31640 structured-block */
31642 #define OMP_SINGLE_CLAUSE_MASK \
31643 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31648 static tree
31649 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31651 tree stmt = make_node (OMP_SINGLE);
31652 TREE_TYPE (stmt) = void_type_node;
31654 OMP_SINGLE_CLAUSES (stmt)
31655 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31656 "#pragma omp single", pragma_tok);
31657 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31659 return add_stmt (stmt);
31662 /* OpenMP 3.0:
31663 # pragma omp task task-clause[optseq] new-line
31664 structured-block */
31666 #define OMP_TASK_CLAUSE_MASK \
31667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
31678 static tree
31679 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31681 tree clauses, block;
31682 unsigned int save;
31684 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31685 "#pragma omp task", pragma_tok);
31686 block = begin_omp_task ();
31687 save = cp_parser_begin_omp_structured_block (parser);
31688 cp_parser_statement (parser, NULL_TREE, false, NULL);
31689 cp_parser_end_omp_structured_block (parser, save);
31690 return finish_omp_task (clauses, block);
31693 /* OpenMP 3.0:
31694 # pragma omp taskwait new-line */
31696 static void
31697 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31699 cp_parser_require_pragma_eol (parser, pragma_tok);
31700 finish_omp_taskwait ();
31703 /* OpenMP 3.1:
31704 # pragma omp taskyield new-line */
31706 static void
31707 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31709 cp_parser_require_pragma_eol (parser, pragma_tok);
31710 finish_omp_taskyield ();
31713 /* OpenMP 4.0:
31714 # pragma omp taskgroup new-line
31715 structured-block */
31717 static tree
31718 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31720 cp_parser_require_pragma_eol (parser, pragma_tok);
31721 return c_finish_omp_taskgroup (input_location,
31722 cp_parser_omp_structured_block (parser));
31726 /* OpenMP 2.5:
31727 # pragma omp threadprivate (variable-list) */
31729 static void
31730 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31732 tree vars;
31734 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31735 cp_parser_require_pragma_eol (parser, pragma_tok);
31737 finish_omp_threadprivate (vars);
31740 /* OpenMP 4.0:
31741 # pragma omp cancel cancel-clause[optseq] new-line */
31743 #define OMP_CANCEL_CLAUSE_MASK \
31744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31750 static void
31751 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31753 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31754 "#pragma omp cancel", pragma_tok);
31755 finish_omp_cancel (clauses);
31758 /* OpenMP 4.0:
31759 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31761 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31767 static void
31768 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31770 tree clauses;
31771 bool point_seen = false;
31773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31775 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31776 const char *p = IDENTIFIER_POINTER (id);
31778 if (strcmp (p, "point") == 0)
31780 cp_lexer_consume_token (parser->lexer);
31781 point_seen = true;
31784 if (!point_seen)
31786 cp_parser_error (parser, "expected %<point%>");
31787 cp_parser_require_pragma_eol (parser, pragma_tok);
31788 return;
31791 clauses = cp_parser_omp_all_clauses (parser,
31792 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31793 "#pragma omp cancellation point",
31794 pragma_tok);
31795 finish_omp_cancellation_point (clauses);
31798 /* OpenMP 4.0:
31799 #pragma omp distribute distribute-clause[optseq] new-line
31800 for-loop */
31802 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31808 static tree
31809 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31810 char *p_name, omp_clause_mask mask, tree *cclauses)
31812 tree clauses, sb, ret;
31813 unsigned int save;
31814 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31816 strcat (p_name, " distribute");
31817 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31821 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31822 const char *p = IDENTIFIER_POINTER (id);
31823 bool simd = false;
31824 bool parallel = false;
31826 if (strcmp (p, "simd") == 0)
31827 simd = true;
31828 else
31829 parallel = strcmp (p, "parallel") == 0;
31830 if (parallel || simd)
31832 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31833 if (cclauses == NULL)
31834 cclauses = cclauses_buf;
31835 cp_lexer_consume_token (parser->lexer);
31836 if (!flag_openmp) /* flag_openmp_simd */
31838 if (simd)
31839 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31840 cclauses);
31841 else
31842 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31843 cclauses);
31845 sb = begin_omp_structured_block ();
31846 save = cp_parser_begin_omp_structured_block (parser);
31847 if (simd)
31848 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31849 cclauses);
31850 else
31851 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31852 cclauses);
31853 cp_parser_end_omp_structured_block (parser, save);
31854 tree body = finish_omp_structured_block (sb);
31855 if (ret == NULL)
31856 return ret;
31857 ret = make_node (OMP_DISTRIBUTE);
31858 TREE_TYPE (ret) = void_type_node;
31859 OMP_FOR_BODY (ret) = body;
31860 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31861 SET_EXPR_LOCATION (ret, loc);
31862 add_stmt (ret);
31863 return ret;
31866 if (!flag_openmp) /* flag_openmp_simd */
31868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31869 return NULL_TREE;
31872 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31873 cclauses == NULL);
31874 if (cclauses)
31876 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31877 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31880 sb = begin_omp_structured_block ();
31881 save = cp_parser_begin_omp_structured_block (parser);
31883 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31885 cp_parser_end_omp_structured_block (parser, save);
31886 add_stmt (finish_omp_structured_block (sb));
31888 return ret;
31891 /* OpenMP 4.0:
31892 # pragma omp teams teams-clause[optseq] new-line
31893 structured-block */
31895 #define OMP_TEAMS_CLAUSE_MASK \
31896 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31904 static tree
31905 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31906 char *p_name, omp_clause_mask mask, tree *cclauses)
31908 tree clauses, sb, ret;
31909 unsigned int save;
31910 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31912 strcat (p_name, " teams");
31913 mask |= OMP_TEAMS_CLAUSE_MASK;
31915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31917 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31918 const char *p = IDENTIFIER_POINTER (id);
31919 if (strcmp (p, "distribute") == 0)
31921 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31922 if (cclauses == NULL)
31923 cclauses = cclauses_buf;
31925 cp_lexer_consume_token (parser->lexer);
31926 if (!flag_openmp) /* flag_openmp_simd */
31927 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31928 cclauses);
31929 sb = begin_omp_structured_block ();
31930 save = cp_parser_begin_omp_structured_block (parser);
31931 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31932 cclauses);
31933 cp_parser_end_omp_structured_block (parser, save);
31934 tree body = finish_omp_structured_block (sb);
31935 if (ret == NULL)
31936 return ret;
31937 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31938 ret = make_node (OMP_TEAMS);
31939 TREE_TYPE (ret) = void_type_node;
31940 OMP_TEAMS_CLAUSES (ret) = clauses;
31941 OMP_TEAMS_BODY (ret) = body;
31942 OMP_TEAMS_COMBINED (ret) = 1;
31943 return add_stmt (ret);
31946 if (!flag_openmp) /* flag_openmp_simd */
31948 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31949 return NULL_TREE;
31952 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31953 cclauses == NULL);
31954 if (cclauses)
31956 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31957 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31960 tree stmt = make_node (OMP_TEAMS);
31961 TREE_TYPE (stmt) = void_type_node;
31962 OMP_TEAMS_CLAUSES (stmt) = clauses;
31963 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31965 return add_stmt (stmt);
31968 /* OpenMP 4.0:
31969 # pragma omp target data target-data-clause[optseq] new-line
31970 structured-block */
31972 #define OMP_TARGET_DATA_CLAUSE_MASK \
31973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31977 static tree
31978 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31980 tree clauses
31981 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31982 "#pragma omp target data", pragma_tok);
31983 int map_seen = 0;
31984 for (tree *pc = &clauses; *pc;)
31986 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
31987 switch (OMP_CLAUSE_MAP_KIND (*pc))
31989 case GOMP_MAP_TO:
31990 case GOMP_MAP_ALWAYS_TO:
31991 case GOMP_MAP_FROM:
31992 case GOMP_MAP_ALWAYS_FROM:
31993 case GOMP_MAP_TOFROM:
31994 case GOMP_MAP_ALWAYS_TOFROM:
31995 case GOMP_MAP_ALLOC:
31996 case GOMP_MAP_POINTER:
31997 map_seen = 3;
31998 break;
31999 default:
32000 map_seen |= 1;
32001 error_at (OMP_CLAUSE_LOCATION (*pc),
32002 "%<#pragma omp target data%> with map-type other "
32003 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
32004 "on %<map%> clause");
32005 *pc = OMP_CLAUSE_CHAIN (*pc);
32006 continue;
32008 pc = &OMP_CLAUSE_CHAIN (*pc);
32011 if (map_seen != 3)
32013 if (map_seen == 0)
32014 error_at (pragma_tok->location,
32015 "%<#pragma omp target data%> must contain at least "
32016 "one %<map%> clause");
32017 return NULL_TREE;
32020 tree stmt = make_node (OMP_TARGET_DATA);
32021 TREE_TYPE (stmt) = void_type_node;
32022 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
32024 keep_next_level (true);
32025 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
32027 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32028 return add_stmt (stmt);
32031 /* OpenMP 4.1:
32032 # pragma omp target enter data target-enter-data-clause[optseq] new-line
32033 structured-block */
32035 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
32036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32042 static tree
32043 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
32044 enum pragma_context context)
32046 bool data_seen = false;
32047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32049 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32050 const char *p = IDENTIFIER_POINTER (id);
32052 if (strcmp (p, "data") == 0)
32054 cp_lexer_consume_token (parser->lexer);
32055 data_seen = true;
32058 if (!data_seen)
32060 cp_parser_error (parser, "expected %<data%>");
32061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32062 return NULL_TREE;
32065 if (context == pragma_stmt)
32067 error_at (pragma_tok->location,
32068 "%<#pragma omp target enter data%> may only be "
32069 "used in compound statements");
32070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32071 return NULL_TREE;
32074 tree clauses
32075 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
32076 "#pragma omp target enter data", pragma_tok);
32077 int map_seen = 0;
32078 for (tree *pc = &clauses; *pc;)
32080 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32081 switch (OMP_CLAUSE_MAP_KIND (*pc))
32083 case GOMP_MAP_TO:
32084 case GOMP_MAP_ALWAYS_TO:
32085 case GOMP_MAP_ALLOC:
32086 case GOMP_MAP_POINTER:
32087 map_seen = 3;
32088 break;
32089 default:
32090 map_seen |= 1;
32091 error_at (OMP_CLAUSE_LOCATION (*pc),
32092 "%<#pragma omp target enter data%> with map-type other "
32093 "than %<to%> or %<alloc%> on %<map%> clause");
32094 *pc = OMP_CLAUSE_CHAIN (*pc);
32095 continue;
32097 pc = &OMP_CLAUSE_CHAIN (*pc);
32100 if (map_seen != 3)
32102 if (map_seen == 0)
32103 error_at (pragma_tok->location,
32104 "%<#pragma omp target enter data%> must contain at least "
32105 "one %<map%> clause");
32106 return NULL_TREE;
32109 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
32110 TREE_TYPE (stmt) = void_type_node;
32111 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
32112 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32113 return add_stmt (stmt);
32116 /* OpenMP 4.1:
32117 # pragma omp target exit data target-enter-data-clause[optseq] new-line
32118 structured-block */
32120 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
32121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32127 static tree
32128 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
32129 enum pragma_context context)
32131 bool data_seen = false;
32132 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32134 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32135 const char *p = IDENTIFIER_POINTER (id);
32137 if (strcmp (p, "data") == 0)
32139 cp_lexer_consume_token (parser->lexer);
32140 data_seen = true;
32143 if (!data_seen)
32145 cp_parser_error (parser, "expected %<data%>");
32146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32147 return NULL_TREE;
32150 if (context == pragma_stmt)
32152 error_at (pragma_tok->location,
32153 "%<#pragma omp target exit data%> may only be "
32154 "used in compound statements");
32155 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32156 return NULL_TREE;
32159 tree clauses
32160 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
32161 "#pragma omp target exit data", pragma_tok);
32162 int map_seen = 0;
32163 for (tree *pc = &clauses; *pc;)
32165 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32166 switch (OMP_CLAUSE_MAP_KIND (*pc))
32168 case GOMP_MAP_FROM:
32169 case GOMP_MAP_ALWAYS_FROM:
32170 case GOMP_MAP_RELEASE:
32171 case GOMP_MAP_DELETE:
32172 case GOMP_MAP_POINTER:
32173 map_seen = 3;
32174 break;
32175 default:
32176 map_seen |= 1;
32177 error_at (OMP_CLAUSE_LOCATION (*pc),
32178 "%<#pragma omp target exit data%> with map-type other "
32179 "than %<from%>, %<release%> or %<delete%> on %<map%>"
32180 " clause");
32181 *pc = OMP_CLAUSE_CHAIN (*pc);
32182 continue;
32184 pc = &OMP_CLAUSE_CHAIN (*pc);
32187 if (map_seen != 3)
32189 if (map_seen == 0)
32190 error_at (pragma_tok->location,
32191 "%<#pragma omp target exit data%> must contain at least "
32192 "one %<map%> clause");
32193 return NULL_TREE;
32196 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
32197 TREE_TYPE (stmt) = void_type_node;
32198 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
32199 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32200 return add_stmt (stmt);
32203 /* OpenMP 4.0:
32204 # pragma omp target update target-update-clause[optseq] new-line */
32206 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
32207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
32208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
32209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32214 static bool
32215 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
32216 enum pragma_context context)
32218 if (context == pragma_stmt)
32220 error_at (pragma_tok->location,
32221 "%<#pragma omp target update%> may only be "
32222 "used in compound statements");
32223 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32224 return false;
32227 tree clauses
32228 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
32229 "#pragma omp target update", pragma_tok);
32230 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
32231 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
32233 error_at (pragma_tok->location,
32234 "%<#pragma omp target update%> must contain at least one "
32235 "%<from%> or %<to%> clauses");
32236 return false;
32239 tree stmt = make_node (OMP_TARGET_UPDATE);
32240 TREE_TYPE (stmt) = void_type_node;
32241 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
32242 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32243 add_stmt (stmt);
32244 return false;
32247 /* OpenMP 4.0:
32248 # pragma omp target target-clause[optseq] new-line
32249 structured-block */
32251 #define OMP_TARGET_CLAUSE_MASK \
32252 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
32257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP))
32261 static bool
32262 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
32263 enum pragma_context context)
32265 tree *pc = NULL, stmt;
32267 if (context != pragma_stmt && context != pragma_compound)
32269 cp_parser_error (parser, "expected declaration specifiers");
32270 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32271 return false;
32274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32276 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32277 const char *p = IDENTIFIER_POINTER (id);
32279 if (strcmp (p, "teams") == 0)
32281 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
32282 char p_name[sizeof ("#pragma omp target teams distribute "
32283 "parallel for simd")];
32285 cp_lexer_consume_token (parser->lexer);
32286 strcpy (p_name, "#pragma omp target");
32287 if (!flag_openmp) /* flag_openmp_simd */
32289 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
32290 OMP_TARGET_CLAUSE_MASK,
32291 cclauses);
32292 return stmt != NULL_TREE;
32294 keep_next_level (true);
32295 tree sb = begin_omp_structured_block ();
32296 unsigned save = cp_parser_begin_omp_structured_block (parser);
32297 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
32298 OMP_TARGET_CLAUSE_MASK, cclauses);
32299 cp_parser_end_omp_structured_block (parser, save);
32300 tree body = finish_omp_structured_block (sb);
32301 if (ret == NULL_TREE)
32302 return false;
32303 tree stmt = make_node (OMP_TARGET);
32304 TREE_TYPE (stmt) = void_type_node;
32305 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
32306 OMP_TARGET_BODY (stmt) = body;
32307 add_stmt (stmt);
32308 pc = &OMP_TARGET_CLAUSES (stmt);
32309 goto check_clauses;
32311 else if (!flag_openmp) /* flag_openmp_simd */
32313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32314 return false;
32316 else if (strcmp (p, "data") == 0)
32318 cp_lexer_consume_token (parser->lexer);
32319 cp_parser_omp_target_data (parser, pragma_tok);
32320 return true;
32322 else if (strcmp (p, "enter") == 0)
32324 cp_lexer_consume_token (parser->lexer);
32325 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
32326 return false;
32328 else if (strcmp (p, "exit") == 0)
32330 cp_lexer_consume_token (parser->lexer);
32331 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
32332 return false;
32334 else if (strcmp (p, "update") == 0)
32336 cp_lexer_consume_token (parser->lexer);
32337 return cp_parser_omp_target_update (parser, pragma_tok, context);
32341 stmt = make_node (OMP_TARGET);
32342 TREE_TYPE (stmt) = void_type_node;
32344 OMP_TARGET_CLAUSES (stmt)
32345 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
32346 "#pragma omp target", pragma_tok);
32347 pc = &OMP_TARGET_CLAUSES (stmt);
32348 keep_next_level (true);
32349 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
32351 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32352 add_stmt (stmt);
32354 check_clauses:
32355 while (*pc)
32357 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32358 switch (OMP_CLAUSE_MAP_KIND (*pc))
32360 case GOMP_MAP_TO:
32361 case GOMP_MAP_ALWAYS_TO:
32362 case GOMP_MAP_FROM:
32363 case GOMP_MAP_ALWAYS_FROM:
32364 case GOMP_MAP_TOFROM:
32365 case GOMP_MAP_ALWAYS_TOFROM:
32366 case GOMP_MAP_ALLOC:
32367 case GOMP_MAP_POINTER:
32368 break;
32369 default:
32370 error_at (OMP_CLAUSE_LOCATION (*pc),
32371 "%<#pragma omp target%> with map-type other "
32372 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
32373 "on %<map%> clause");
32374 *pc = OMP_CLAUSE_CHAIN (*pc);
32375 continue;
32377 pc = &OMP_CLAUSE_CHAIN (*pc);
32379 return true;
32382 /* OpenACC 2.0:
32383 # pragma acc cache (variable-list) new-line
32386 static tree
32387 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
32389 tree stmt, clauses;
32391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
32392 clauses = finish_omp_clauses (clauses, false);
32394 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
32396 stmt = make_node (OACC_CACHE);
32397 TREE_TYPE (stmt) = void_type_node;
32398 OACC_CACHE_CLAUSES (stmt) = clauses;
32399 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32400 add_stmt (stmt);
32402 return stmt;
32405 /* OpenACC 2.0:
32406 # pragma acc data oacc-data-clause[optseq] new-line
32407 structured-block */
32409 #define OACC_DATA_CLAUSE_MASK \
32410 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
32422 static tree
32423 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
32425 tree stmt, clauses, block;
32426 unsigned int save;
32428 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
32429 "#pragma acc data", pragma_tok);
32431 block = begin_omp_parallel ();
32432 save = cp_parser_begin_omp_structured_block (parser);
32433 cp_parser_statement (parser, NULL_TREE, false, NULL);
32434 cp_parser_end_omp_structured_block (parser, save);
32435 stmt = finish_oacc_data (clauses, block);
32436 return stmt;
32439 /* OpenACC 2.0:
32440 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
32444 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
32446 LOC is the location of the #pragma token.
32449 #define OACC_ENTER_DATA_CLAUSE_MASK \
32450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32458 #define OACC_EXIT_DATA_CLAUSE_MASK \
32459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
32463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32465 static tree
32466 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
32467 bool enter)
32469 tree stmt, clauses;
32471 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
32472 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32474 cp_parser_error (parser, enter
32475 ? "expected %<data%> in %<#pragma acc enter data%>"
32476 : "expected %<data%> in %<#pragma acc exit data%>");
32477 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32478 return NULL_TREE;
32481 const char *p =
32482 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
32483 if (strcmp (p, "data") != 0)
32485 cp_parser_error (parser, "invalid pragma");
32486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32487 return NULL_TREE;
32490 cp_lexer_consume_token (parser->lexer);
32492 if (enter)
32493 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
32494 "#pragma acc enter data", pragma_tok);
32495 else
32496 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
32497 "#pragma acc exit data", pragma_tok);
32499 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32501 error_at (pragma_tok->location,
32502 "%<#pragma acc enter data%> has no data movement clause");
32503 return NULL_TREE;
32506 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
32507 TREE_TYPE (stmt) = void_type_node;
32508 OMP_STANDALONE_CLAUSES (stmt) = clauses;
32509 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32510 add_stmt (stmt);
32511 return stmt;
32514 /* OpenACC 2.0:
32515 # pragma acc kernels oacc-kernels-clause[optseq] new-line
32516 structured-block */
32518 #define OACC_KERNELS_CLAUSE_MASK \
32519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32533 static tree
32534 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
32536 tree stmt, clauses, block;
32537 unsigned int save;
32539 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
32540 "#pragma acc kernels", pragma_tok);
32542 block = begin_omp_parallel ();
32543 save = cp_parser_begin_omp_structured_block (parser);
32544 cp_parser_statement (parser, NULL_TREE, false, NULL);
32545 cp_parser_end_omp_structured_block (parser, save);
32546 stmt = finish_oacc_kernels (clauses, block);
32547 return stmt;
32550 /* OpenACC 2.0:
32551 # pragma acc loop oacc-loop-clause[optseq] new-line
32552 structured-block */
32554 #define OACC_LOOP_CLAUSE_MASK \
32555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
32556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
32558 static tree
32559 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
32561 tree stmt, clauses, block;
32562 int save;
32564 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
32565 "#pragma acc loop", pragma_tok);
32567 block = begin_omp_structured_block ();
32568 save = cp_parser_begin_omp_structured_block (parser);
32569 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
32570 cp_parser_end_omp_structured_block (parser, save);
32571 add_stmt (finish_omp_structured_block (block));
32572 return stmt;
32575 /* OpenACC 2.0:
32576 # pragma acc parallel oacc-parallel-clause[optseq] new-line
32577 structured-block */
32579 #define OACC_PARALLEL_CLAUSE_MASK \
32580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
32588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
32589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
32595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
32596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32598 static tree
32599 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
32601 tree stmt, clauses, block;
32602 unsigned int save;
32604 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
32605 "#pragma acc parallel", pragma_tok);
32607 block = begin_omp_parallel ();
32608 save = cp_parser_begin_omp_structured_block (parser);
32609 cp_parser_statement (parser, NULL_TREE, false, NULL);
32610 cp_parser_end_omp_structured_block (parser, save);
32611 stmt = finish_oacc_parallel (clauses, block);
32612 return stmt;
32615 /* OpenACC 2.0:
32616 # pragma acc update oacc-update-clause[optseq] new-line
32619 #define OACC_UPDATE_CLAUSE_MASK \
32620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
32622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
32623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
32625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32627 static tree
32628 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
32630 tree stmt, clauses;
32632 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
32633 "#pragma acc update", pragma_tok);
32635 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32637 error_at (pragma_tok->location,
32638 "%<#pragma acc update%> must contain at least one "
32639 "%<device%> or %<host/self%> clause");
32640 return NULL_TREE;
32643 stmt = make_node (OACC_UPDATE);
32644 TREE_TYPE (stmt) = void_type_node;
32645 OACC_UPDATE_CLAUSES (stmt) = clauses;
32646 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32647 add_stmt (stmt);
32648 return stmt;
32651 /* OpenACC 2.0:
32652 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
32654 LOC is the location of the #pragma token.
32657 #define OACC_WAIT_CLAUSE_MASK \
32658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
32660 static tree
32661 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
32663 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
32664 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32666 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32667 list = cp_parser_oacc_wait_list (parser, loc, list);
32669 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
32670 "#pragma acc wait", pragma_tok);
32672 stmt = c_finish_oacc_wait (loc, list, clauses);
32674 return stmt;
32677 /* OpenMP 4.0:
32678 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
32680 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
32681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
32682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
32683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
32684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
32685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
32686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
32688 static void
32689 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
32690 enum pragma_context context)
32692 bool first_p = parser->omp_declare_simd == NULL;
32693 cp_omp_declare_simd_data data;
32694 if (first_p)
32696 data.error_seen = false;
32697 data.fndecl_seen = false;
32698 data.tokens = vNULL;
32699 parser->omp_declare_simd = &data;
32701 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32702 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32703 cp_lexer_consume_token (parser->lexer);
32704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32705 parser->omp_declare_simd->error_seen = true;
32706 cp_parser_require_pragma_eol (parser, pragma_tok);
32707 struct cp_token_cache *cp
32708 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
32709 parser->omp_declare_simd->tokens.safe_push (cp);
32710 if (first_p)
32712 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
32713 cp_parser_pragma (parser, context);
32714 switch (context)
32716 case pragma_external:
32717 cp_parser_declaration (parser);
32718 break;
32719 case pragma_member:
32720 cp_parser_member_declaration (parser);
32721 break;
32722 case pragma_objc_icode:
32723 cp_parser_block_declaration (parser, /*statement_p=*/false);
32724 break;
32725 default:
32726 cp_parser_declaration_statement (parser);
32727 break;
32729 if (parser->omp_declare_simd
32730 && !parser->omp_declare_simd->error_seen
32731 && !parser->omp_declare_simd->fndecl_seen)
32732 error_at (pragma_tok->location,
32733 "%<#pragma omp declare simd%> not immediately followed by "
32734 "function declaration or definition");
32735 data.tokens.release ();
32736 parser->omp_declare_simd = NULL;
32740 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
32741 This function is modelled similar to the late parsing of omp declare
32742 simd. */
32744 static tree
32745 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
32747 struct cp_token_cache *ce;
32748 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
32749 int ii = 0;
32751 if (parser->omp_declare_simd != NULL)
32753 error ("%<#pragma omp declare simd%> cannot be used in the same function"
32754 " marked as a Cilk Plus SIMD-enabled function");
32755 XDELETE (parser->cilk_simd_fn_info);
32756 parser->cilk_simd_fn_info = NULL;
32757 return attrs;
32759 if (!info->error_seen && info->fndecl_seen)
32761 error ("vector attribute not immediately followed by a single function"
32762 " declaration or definition");
32763 info->error_seen = true;
32765 if (info->error_seen)
32766 return attrs;
32768 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
32770 tree c, cl;
32772 cp_parser_push_lexer_for_tokens (parser, ce);
32773 parser->lexer->in_pragma = true;
32774 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
32775 "SIMD-enabled functions attribute",
32776 NULL);
32777 cp_parser_pop_lexer (parser);
32778 if (cl)
32779 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32781 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
32782 TREE_CHAIN (c) = attrs;
32783 attrs = c;
32785 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32786 TREE_CHAIN (c) = attrs;
32787 if (processing_template_decl)
32788 ATTR_IS_DEPENDENT (c) = 1;
32789 attrs = c;
32791 info->fndecl_seen = true;
32792 XDELETE (parser->cilk_simd_fn_info);
32793 parser->cilk_simd_fn_info = NULL;
32794 return attrs;
32797 /* Finalize #pragma omp declare simd clauses after direct declarator has
32798 been parsed, and put that into "omp declare simd" attribute. */
32800 static tree
32801 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
32803 struct cp_token_cache *ce;
32804 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32805 int i;
32807 if (!data->error_seen && data->fndecl_seen)
32809 error ("%<#pragma omp declare simd%> not immediately followed by "
32810 "a single function declaration or definition");
32811 data->error_seen = true;
32812 return attrs;
32814 if (data->error_seen)
32815 return attrs;
32817 FOR_EACH_VEC_ELT (data->tokens, i, ce)
32819 tree c, cl;
32821 cp_parser_push_lexer_for_tokens (parser, ce);
32822 parser->lexer->in_pragma = true;
32823 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32824 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32825 cp_lexer_consume_token (parser->lexer);
32826 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32827 "#pragma omp declare simd", pragma_tok);
32828 cp_parser_pop_lexer (parser);
32829 if (cl)
32830 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32831 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32832 TREE_CHAIN (c) = attrs;
32833 if (processing_template_decl)
32834 ATTR_IS_DEPENDENT (c) = 1;
32835 attrs = c;
32838 data->fndecl_seen = true;
32839 return attrs;
32843 /* OpenMP 4.0:
32844 # pragma omp declare target new-line
32845 declarations and definitions
32846 # pragma omp end declare target new-line */
32848 static void
32849 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32851 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32852 scope_chain->omp_declare_target_attribute++;
32855 static void
32856 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32858 const char *p = "";
32859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32862 p = IDENTIFIER_POINTER (id);
32864 if (strcmp (p, "declare") == 0)
32866 cp_lexer_consume_token (parser->lexer);
32867 p = "";
32868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32870 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32871 p = IDENTIFIER_POINTER (id);
32873 if (strcmp (p, "target") == 0)
32874 cp_lexer_consume_token (parser->lexer);
32875 else
32877 cp_parser_error (parser, "expected %<target%>");
32878 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32879 return;
32882 else
32884 cp_parser_error (parser, "expected %<declare%>");
32885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32886 return;
32888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32889 if (!scope_chain->omp_declare_target_attribute)
32890 error_at (pragma_tok->location,
32891 "%<#pragma omp end declare target%> without corresponding "
32892 "%<#pragma omp declare target%>");
32893 else
32894 scope_chain->omp_declare_target_attribute--;
32897 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32898 expression and optional initializer clause of
32899 #pragma omp declare reduction. We store the expression(s) as
32900 either 3, 6 or 7 special statements inside of the artificial function's
32901 body. The first two statements are DECL_EXPRs for the artificial
32902 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32903 expression that uses those variables.
32904 If there was any INITIALIZER clause, this is followed by further statements,
32905 the fourth and fifth statements are DECL_EXPRs for the artificial
32906 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32907 constructor variant (first token after open paren is not omp_priv),
32908 then the sixth statement is a statement with the function call expression
32909 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32910 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32911 to initialize the OMP_PRIV artificial variable and there is seventh
32912 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32914 static bool
32915 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32917 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32918 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32919 type = TREE_TYPE (type);
32920 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32921 DECL_ARTIFICIAL (omp_out) = 1;
32922 pushdecl (omp_out);
32923 add_decl_expr (omp_out);
32924 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32925 DECL_ARTIFICIAL (omp_in) = 1;
32926 pushdecl (omp_in);
32927 add_decl_expr (omp_in);
32928 tree combiner;
32929 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32931 keep_next_level (true);
32932 tree block = begin_omp_structured_block ();
32933 combiner = cp_parser_expression (parser);
32934 finish_expr_stmt (combiner);
32935 block = finish_omp_structured_block (block);
32936 add_stmt (block);
32938 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32939 return false;
32941 const char *p = "";
32942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32944 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32945 p = IDENTIFIER_POINTER (id);
32948 if (strcmp (p, "initializer") == 0)
32950 cp_lexer_consume_token (parser->lexer);
32951 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32952 return false;
32954 p = "";
32955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32957 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32958 p = IDENTIFIER_POINTER (id);
32961 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32962 DECL_ARTIFICIAL (omp_priv) = 1;
32963 pushdecl (omp_priv);
32964 add_decl_expr (omp_priv);
32965 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32966 DECL_ARTIFICIAL (omp_orig) = 1;
32967 pushdecl (omp_orig);
32968 add_decl_expr (omp_orig);
32970 keep_next_level (true);
32971 block = begin_omp_structured_block ();
32973 bool ctor = false;
32974 if (strcmp (p, "omp_priv") == 0)
32976 bool is_direct_init, is_non_constant_init;
32977 ctor = true;
32978 cp_lexer_consume_token (parser->lexer);
32979 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32981 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32982 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32983 == CPP_CLOSE_PAREN
32984 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32985 == CPP_CLOSE_PAREN))
32987 finish_omp_structured_block (block);
32988 error ("invalid initializer clause");
32989 return false;
32991 initializer = cp_parser_initializer (parser, &is_direct_init,
32992 &is_non_constant_init);
32993 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32994 NULL_TREE, LOOKUP_ONLYCONVERTING);
32996 else
32998 cp_parser_parse_tentatively (parser);
32999 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
33000 /*check_dependency_p=*/true,
33001 /*template_p=*/NULL,
33002 /*declarator_p=*/false,
33003 /*optional_p=*/false);
33004 vec<tree, va_gc> *args;
33005 if (fn_name == error_mark_node
33006 || cp_parser_error_occurred (parser)
33007 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33008 || ((args = cp_parser_parenthesized_expression_list
33009 (parser, non_attr, /*cast_p=*/false,
33010 /*allow_expansion_p=*/true,
33011 /*non_constant_p=*/NULL)),
33012 cp_parser_error_occurred (parser)))
33014 finish_omp_structured_block (block);
33015 cp_parser_abort_tentative_parse (parser);
33016 cp_parser_error (parser, "expected id-expression (arguments)");
33017 return false;
33019 unsigned int i;
33020 tree arg;
33021 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
33022 if (arg == omp_priv
33023 || (TREE_CODE (arg) == ADDR_EXPR
33024 && TREE_OPERAND (arg, 0) == omp_priv))
33025 break;
33026 cp_parser_abort_tentative_parse (parser);
33027 if (arg == NULL_TREE)
33028 error ("one of the initializer call arguments should be %<omp_priv%>"
33029 " or %<&omp_priv%>");
33030 initializer = cp_parser_postfix_expression (parser, false, false, false,
33031 false, NULL);
33032 finish_expr_stmt (initializer);
33035 block = finish_omp_structured_block (block);
33036 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
33037 add_stmt (block);
33039 if (ctor)
33040 add_decl_expr (omp_orig);
33042 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33043 return false;
33046 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
33047 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
33049 return true;
33052 /* OpenMP 4.0
33053 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33054 initializer-clause[opt] new-line
33056 initializer-clause:
33057 initializer (omp_priv initializer)
33058 initializer (function-name (argument-list)) */
33060 static void
33061 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
33062 enum pragma_context)
33064 auto_vec<tree> types;
33065 enum tree_code reduc_code = ERROR_MARK;
33066 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
33067 unsigned int i;
33068 cp_token *first_token;
33069 cp_token_cache *cp;
33070 int errs;
33071 void *p;
33073 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
33074 p = obstack_alloc (&declarator_obstack, 0);
33076 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33077 goto fail;
33079 switch (cp_lexer_peek_token (parser->lexer)->type)
33081 case CPP_PLUS:
33082 reduc_code = PLUS_EXPR;
33083 break;
33084 case CPP_MULT:
33085 reduc_code = MULT_EXPR;
33086 break;
33087 case CPP_MINUS:
33088 reduc_code = MINUS_EXPR;
33089 break;
33090 case CPP_AND:
33091 reduc_code = BIT_AND_EXPR;
33092 break;
33093 case CPP_XOR:
33094 reduc_code = BIT_XOR_EXPR;
33095 break;
33096 case CPP_OR:
33097 reduc_code = BIT_IOR_EXPR;
33098 break;
33099 case CPP_AND_AND:
33100 reduc_code = TRUTH_ANDIF_EXPR;
33101 break;
33102 case CPP_OR_OR:
33103 reduc_code = TRUTH_ORIF_EXPR;
33104 break;
33105 case CPP_NAME:
33106 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
33107 break;
33108 default:
33109 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
33110 "%<|%>, %<&&%>, %<||%> or identifier");
33111 goto fail;
33114 if (reduc_code != ERROR_MARK)
33115 cp_lexer_consume_token (parser->lexer);
33117 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
33118 if (reduc_id == error_mark_node)
33119 goto fail;
33121 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33122 goto fail;
33124 /* Types may not be defined in declare reduction type list. */
33125 const char *saved_message;
33126 saved_message = parser->type_definition_forbidden_message;
33127 parser->type_definition_forbidden_message
33128 = G_("types may not be defined in declare reduction type list");
33129 bool saved_colon_corrects_to_scope_p;
33130 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33131 parser->colon_corrects_to_scope_p = false;
33132 bool saved_colon_doesnt_start_class_def_p;
33133 saved_colon_doesnt_start_class_def_p
33134 = parser->colon_doesnt_start_class_def_p;
33135 parser->colon_doesnt_start_class_def_p = true;
33137 while (true)
33139 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33140 type = cp_parser_type_id (parser);
33141 if (type == error_mark_node)
33143 else if (ARITHMETIC_TYPE_P (type)
33144 && (orig_reduc_id == NULL_TREE
33145 || (TREE_CODE (type) != COMPLEX_TYPE
33146 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33147 "min") == 0
33148 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33149 "max") == 0))))
33150 error_at (loc, "predeclared arithmetic type %qT in "
33151 "%<#pragma omp declare reduction%>", type);
33152 else if (TREE_CODE (type) == FUNCTION_TYPE
33153 || TREE_CODE (type) == METHOD_TYPE
33154 || TREE_CODE (type) == ARRAY_TYPE)
33155 error_at (loc, "function or array type %qT in "
33156 "%<#pragma omp declare reduction%>", type);
33157 else if (TREE_CODE (type) == REFERENCE_TYPE)
33158 error_at (loc, "reference type %qT in "
33159 "%<#pragma omp declare reduction%>", type);
33160 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
33161 error_at (loc, "const, volatile or __restrict qualified type %qT in "
33162 "%<#pragma omp declare reduction%>", type);
33163 else
33164 types.safe_push (type);
33166 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33167 cp_lexer_consume_token (parser->lexer);
33168 else
33169 break;
33172 /* Restore the saved message. */
33173 parser->type_definition_forbidden_message = saved_message;
33174 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33175 parser->colon_doesnt_start_class_def_p
33176 = saved_colon_doesnt_start_class_def_p;
33178 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
33179 || types.is_empty ())
33181 fail:
33182 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33183 goto done;
33186 first_token = cp_lexer_peek_token (parser->lexer);
33187 cp = NULL;
33188 errs = errorcount;
33189 FOR_EACH_VEC_ELT (types, i, type)
33191 tree fntype
33192 = build_function_type_list (void_type_node,
33193 cp_build_reference_type (type, false),
33194 NULL_TREE);
33195 tree this_reduc_id = reduc_id;
33196 if (!dependent_type_p (type))
33197 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
33198 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
33199 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
33200 DECL_ARTIFICIAL (fndecl) = 1;
33201 DECL_EXTERNAL (fndecl) = 1;
33202 DECL_DECLARED_INLINE_P (fndecl) = 1;
33203 DECL_IGNORED_P (fndecl) = 1;
33204 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
33205 DECL_ATTRIBUTES (fndecl)
33206 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
33207 DECL_ATTRIBUTES (fndecl));
33208 if (processing_template_decl)
33209 fndecl = push_template_decl (fndecl);
33210 bool block_scope = false;
33211 tree block = NULL_TREE;
33212 if (current_function_decl)
33214 block_scope = true;
33215 DECL_CONTEXT (fndecl) = global_namespace;
33216 if (!processing_template_decl)
33217 pushdecl (fndecl);
33219 else if (current_class_type)
33221 if (cp == NULL)
33223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33224 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33225 cp_lexer_consume_token (parser->lexer);
33226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33227 goto fail;
33228 cp = cp_token_cache_new (first_token,
33229 cp_lexer_peek_nth_token (parser->lexer,
33230 2));
33232 DECL_STATIC_FUNCTION_P (fndecl) = 1;
33233 finish_member_declaration (fndecl);
33234 DECL_PENDING_INLINE_INFO (fndecl) = cp;
33235 DECL_PENDING_INLINE_P (fndecl) = 1;
33236 vec_safe_push (unparsed_funs_with_definitions, fndecl);
33237 continue;
33239 else
33241 DECL_CONTEXT (fndecl) = current_namespace;
33242 pushdecl (fndecl);
33244 if (!block_scope)
33245 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
33246 else
33247 block = begin_omp_structured_block ();
33248 if (cp)
33250 cp_parser_push_lexer_for_tokens (parser, cp);
33251 parser->lexer->in_pragma = true;
33253 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
33255 if (!block_scope)
33256 finish_function (0);
33257 else
33258 DECL_CONTEXT (fndecl) = current_function_decl;
33259 if (cp)
33260 cp_parser_pop_lexer (parser);
33261 goto fail;
33263 if (cp)
33264 cp_parser_pop_lexer (parser);
33265 if (!block_scope)
33266 finish_function (0);
33267 else
33269 DECL_CONTEXT (fndecl) = current_function_decl;
33270 block = finish_omp_structured_block (block);
33271 if (TREE_CODE (block) == BIND_EXPR)
33272 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
33273 else if (TREE_CODE (block) == STATEMENT_LIST)
33274 DECL_SAVED_TREE (fndecl) = block;
33275 if (processing_template_decl)
33276 add_decl_expr (fndecl);
33278 cp_check_omp_declare_reduction (fndecl);
33279 if (cp == NULL && types.length () > 1)
33280 cp = cp_token_cache_new (first_token,
33281 cp_lexer_peek_nth_token (parser->lexer, 2));
33282 if (errs != errorcount)
33283 break;
33286 cp_parser_require_pragma_eol (parser, pragma_tok);
33288 done:
33289 /* Free any declarators allocated. */
33290 obstack_free (&declarator_obstack, p);
33293 /* OpenMP 4.0
33294 #pragma omp declare simd declare-simd-clauses[optseq] new-line
33295 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33296 initializer-clause[opt] new-line
33297 #pragma omp declare target new-line */
33299 static void
33300 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
33301 enum pragma_context context)
33303 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33305 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33306 const char *p = IDENTIFIER_POINTER (id);
33308 if (strcmp (p, "simd") == 0)
33310 cp_lexer_consume_token (parser->lexer);
33311 cp_parser_omp_declare_simd (parser, pragma_tok,
33312 context);
33313 return;
33315 cp_ensure_no_omp_declare_simd (parser);
33316 if (strcmp (p, "reduction") == 0)
33318 cp_lexer_consume_token (parser->lexer);
33319 cp_parser_omp_declare_reduction (parser, pragma_tok,
33320 context);
33321 return;
33323 if (!flag_openmp) /* flag_openmp_simd */
33325 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33326 return;
33328 if (strcmp (p, "target") == 0)
33330 cp_lexer_consume_token (parser->lexer);
33331 cp_parser_omp_declare_target (parser, pragma_tok);
33332 return;
33335 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
33336 "or %<target%>");
33337 cp_parser_require_pragma_eol (parser, pragma_tok);
33340 /* OpenMP 4.1:
33341 7 #pragma omp taskloop taskloop-clause[optseq] new-line
33342 for-loop
33344 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
33345 for-loop */
33347 #define OMP_TASKLOOP_CLAUSE_MASK \
33348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
33354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
33355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
33356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
33361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33363 static tree
33364 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
33365 char *p_name, omp_clause_mask mask, tree *cclauses)
33367 tree clauses, sb, ret;
33368 unsigned int save;
33369 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33371 strcat (p_name, " taskloop");
33372 mask |= OMP_TASKLOOP_CLAUSE_MASK;
33374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33377 const char *p = IDENTIFIER_POINTER (id);
33379 if (strcmp (p, "simd") == 0)
33381 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33382 if (cclauses == NULL)
33383 cclauses = cclauses_buf;
33385 cp_lexer_consume_token (parser->lexer);
33386 if (!flag_openmp) /* flag_openmp_simd */
33387 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33388 cclauses);
33389 sb = begin_omp_structured_block ();
33390 save = cp_parser_begin_omp_structured_block (parser);
33391 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33392 cclauses);
33393 cp_parser_end_omp_structured_block (parser, save);
33394 tree body = finish_omp_structured_block (sb);
33395 if (ret == NULL)
33396 return ret;
33397 ret = make_node (OMP_TASKLOOP);
33398 TREE_TYPE (ret) = void_type_node;
33399 OMP_FOR_BODY (ret) = body;
33400 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
33401 SET_EXPR_LOCATION (ret, loc);
33402 add_stmt (ret);
33403 return ret;
33406 if (!flag_openmp) /* flag_openmp_simd */
33408 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33409 return NULL_TREE;
33412 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33413 cclauses == NULL);
33414 if (cclauses)
33416 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
33417 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
33420 sb = begin_omp_structured_block ();
33421 save = cp_parser_begin_omp_structured_block (parser);
33423 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
33425 cp_parser_end_omp_structured_block (parser, save);
33426 add_stmt (finish_omp_structured_block (sb));
33428 return ret;
33431 /* Main entry point to OpenMP statement pragmas. */
33433 static void
33434 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
33436 tree stmt;
33437 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
33438 omp_clause_mask mask (0);
33440 switch (pragma_tok->pragma_kind)
33442 case PRAGMA_OACC_CACHE:
33443 stmt = cp_parser_oacc_cache (parser, pragma_tok);
33444 break;
33445 case PRAGMA_OACC_DATA:
33446 stmt = cp_parser_oacc_data (parser, pragma_tok);
33447 break;
33448 case PRAGMA_OACC_ENTER_DATA:
33449 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
33450 break;
33451 case PRAGMA_OACC_EXIT_DATA:
33452 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
33453 break;
33454 case PRAGMA_OACC_KERNELS:
33455 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
33456 break;
33457 case PRAGMA_OACC_LOOP:
33458 stmt = cp_parser_oacc_loop (parser, pragma_tok);
33459 break;
33460 case PRAGMA_OACC_PARALLEL:
33461 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
33462 break;
33463 case PRAGMA_OACC_UPDATE:
33464 stmt = cp_parser_oacc_update (parser, pragma_tok);
33465 break;
33466 case PRAGMA_OACC_WAIT:
33467 stmt = cp_parser_oacc_wait (parser, pragma_tok);
33468 break;
33469 case PRAGMA_OMP_ATOMIC:
33470 cp_parser_omp_atomic (parser, pragma_tok);
33471 return;
33472 case PRAGMA_OMP_CRITICAL:
33473 stmt = cp_parser_omp_critical (parser, pragma_tok);
33474 break;
33475 case PRAGMA_OMP_DISTRIBUTE:
33476 strcpy (p_name, "#pragma omp");
33477 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
33478 break;
33479 case PRAGMA_OMP_FOR:
33480 strcpy (p_name, "#pragma omp");
33481 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
33482 break;
33483 case PRAGMA_OMP_MASTER:
33484 stmt = cp_parser_omp_master (parser, pragma_tok);
33485 break;
33486 case PRAGMA_OMP_PARALLEL:
33487 strcpy (p_name, "#pragma omp");
33488 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
33489 break;
33490 case PRAGMA_OMP_SECTIONS:
33491 strcpy (p_name, "#pragma omp");
33492 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
33493 break;
33494 case PRAGMA_OMP_SIMD:
33495 strcpy (p_name, "#pragma omp");
33496 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
33497 break;
33498 case PRAGMA_OMP_SINGLE:
33499 stmt = cp_parser_omp_single (parser, pragma_tok);
33500 break;
33501 case PRAGMA_OMP_TASK:
33502 stmt = cp_parser_omp_task (parser, pragma_tok);
33503 break;
33504 case PRAGMA_OMP_TASKGROUP:
33505 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
33506 break;
33507 case PRAGMA_OMP_TASKLOOP:
33508 strcpy (p_name, "#pragma omp");
33509 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
33510 break;
33511 case PRAGMA_OMP_TEAMS:
33512 strcpy (p_name, "#pragma omp");
33513 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
33514 break;
33515 default:
33516 gcc_unreachable ();
33519 if (stmt)
33520 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33523 /* Transactional Memory parsing routines. */
33525 /* Parse a transaction attribute.
33527 txn-attribute:
33528 attribute
33529 [ [ identifier ] ]
33531 ??? Simplify this when C++0x bracket attributes are
33532 implemented properly. */
33534 static tree
33535 cp_parser_txn_attribute_opt (cp_parser *parser)
33537 cp_token *token;
33538 tree attr_name, attr = NULL;
33540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33541 return cp_parser_attributes_opt (parser);
33543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
33544 return NULL_TREE;
33545 cp_lexer_consume_token (parser->lexer);
33546 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
33547 goto error1;
33549 token = cp_lexer_peek_token (parser->lexer);
33550 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
33552 token = cp_lexer_consume_token (parser->lexer);
33554 attr_name = (token->type == CPP_KEYWORD
33555 /* For keywords, use the canonical spelling,
33556 not the parsed identifier. */
33557 ? ridpointers[(int) token->keyword]
33558 : token->u.value);
33559 attr = build_tree_list (attr_name, NULL_TREE);
33561 else
33562 cp_parser_error (parser, "expected identifier");
33564 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33565 error1:
33566 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33567 return attr;
33570 /* Parse a __transaction_atomic or __transaction_relaxed statement.
33572 transaction-statement:
33573 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
33574 compound-statement
33575 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
33578 static tree
33579 cp_parser_transaction (cp_parser *parser, enum rid keyword)
33581 unsigned char old_in = parser->in_transaction;
33582 unsigned char this_in = 1, new_in;
33583 cp_token *token;
33584 tree stmt, attrs, noex;
33586 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33587 || keyword == RID_TRANSACTION_RELAXED);
33588 token = cp_parser_require_keyword (parser, keyword,
33589 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33590 : RT_TRANSACTION_RELAXED));
33591 gcc_assert (token != NULL);
33593 if (keyword == RID_TRANSACTION_RELAXED)
33594 this_in |= TM_STMT_ATTR_RELAXED;
33595 else
33597 attrs = cp_parser_txn_attribute_opt (parser);
33598 if (attrs)
33599 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33602 /* Parse a noexcept specification. */
33603 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
33605 /* Keep track if we're in the lexical scope of an outer transaction. */
33606 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
33608 stmt = begin_transaction_stmt (token->location, NULL, this_in);
33610 parser->in_transaction = new_in;
33611 cp_parser_compound_statement (parser, NULL, false, false);
33612 parser->in_transaction = old_in;
33614 finish_transaction_stmt (stmt, NULL, this_in, noex);
33616 return stmt;
33619 /* Parse a __transaction_atomic or __transaction_relaxed expression.
33621 transaction-expression:
33622 __transaction_atomic txn-noexcept-spec[opt] ( expression )
33623 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
33626 static tree
33627 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
33629 unsigned char old_in = parser->in_transaction;
33630 unsigned char this_in = 1;
33631 cp_token *token;
33632 tree expr, noex;
33633 bool noex_expr;
33635 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33636 || keyword == RID_TRANSACTION_RELAXED);
33638 if (!flag_tm)
33639 error (keyword == RID_TRANSACTION_RELAXED
33640 ? G_("%<__transaction_relaxed%> without transactional memory "
33641 "support enabled")
33642 : G_("%<__transaction_atomic%> without transactional memory "
33643 "support enabled"));
33645 token = cp_parser_require_keyword (parser, keyword,
33646 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33647 : RT_TRANSACTION_RELAXED));
33648 gcc_assert (token != NULL);
33650 if (keyword == RID_TRANSACTION_RELAXED)
33651 this_in |= TM_STMT_ATTR_RELAXED;
33653 /* Set this early. This might mean that we allow transaction_cancel in
33654 an expression that we find out later actually has to be a constexpr.
33655 However, we expect that cxx_constant_value will be able to deal with
33656 this; also, if the noexcept has no constexpr, then what we parse next
33657 really is a transaction's body. */
33658 parser->in_transaction = this_in;
33660 /* Parse a noexcept specification. */
33661 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
33662 true);
33664 if (!noex || !noex_expr
33665 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33667 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
33669 expr = cp_parser_expression (parser);
33670 expr = finish_parenthesized_expr (expr);
33672 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
33674 else
33676 /* The only expression that is available got parsed for the noexcept
33677 already. noexcept is true then. */
33678 expr = noex;
33679 noex = boolean_true_node;
33682 expr = build_transaction_expr (token->location, expr, this_in, noex);
33683 parser->in_transaction = old_in;
33685 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
33686 return error_mark_node;
33688 return (flag_tm ? expr : error_mark_node);
33691 /* Parse a function-transaction-block.
33693 function-transaction-block:
33694 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
33695 function-body
33696 __transaction_atomic txn-attribute[opt] function-try-block
33697 __transaction_relaxed ctor-initializer[opt] function-body
33698 __transaction_relaxed function-try-block
33701 static bool
33702 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
33704 unsigned char old_in = parser->in_transaction;
33705 unsigned char new_in = 1;
33706 tree compound_stmt, stmt, attrs;
33707 bool ctor_initializer_p;
33708 cp_token *token;
33710 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33711 || keyword == RID_TRANSACTION_RELAXED);
33712 token = cp_parser_require_keyword (parser, keyword,
33713 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33714 : RT_TRANSACTION_RELAXED));
33715 gcc_assert (token != NULL);
33717 if (keyword == RID_TRANSACTION_RELAXED)
33718 new_in |= TM_STMT_ATTR_RELAXED;
33719 else
33721 attrs = cp_parser_txn_attribute_opt (parser);
33722 if (attrs)
33723 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33726 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
33728 parser->in_transaction = new_in;
33730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33731 ctor_initializer_p = cp_parser_function_try_block (parser);
33732 else
33733 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
33734 (parser, /*in_function_try_block=*/false);
33736 parser->in_transaction = old_in;
33738 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
33740 return ctor_initializer_p;
33743 /* Parse a __transaction_cancel statement.
33745 cancel-statement:
33746 __transaction_cancel txn-attribute[opt] ;
33747 __transaction_cancel txn-attribute[opt] throw-expression ;
33749 ??? Cancel and throw is not yet implemented. */
33751 static tree
33752 cp_parser_transaction_cancel (cp_parser *parser)
33754 cp_token *token;
33755 bool is_outer = false;
33756 tree stmt, attrs;
33758 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
33759 RT_TRANSACTION_CANCEL);
33760 gcc_assert (token != NULL);
33762 attrs = cp_parser_txn_attribute_opt (parser);
33763 if (attrs)
33764 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
33766 /* ??? Parse cancel-and-throw here. */
33768 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33770 if (!flag_tm)
33772 error_at (token->location, "%<__transaction_cancel%> without "
33773 "transactional memory support enabled");
33774 return error_mark_node;
33776 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
33778 error_at (token->location, "%<__transaction_cancel%> within a "
33779 "%<__transaction_relaxed%>");
33780 return error_mark_node;
33782 else if (is_outer)
33784 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
33785 && !is_tm_may_cancel_outer (current_function_decl))
33787 error_at (token->location, "outer %<__transaction_cancel%> not "
33788 "within outer %<__transaction_atomic%>");
33789 error_at (token->location,
33790 " or a %<transaction_may_cancel_outer%> function");
33791 return error_mark_node;
33794 else if (parser->in_transaction == 0)
33796 error_at (token->location, "%<__transaction_cancel%> not within "
33797 "%<__transaction_atomic%>");
33798 return error_mark_node;
33801 stmt = build_tm_abort_call (token->location, is_outer);
33802 add_stmt (stmt);
33804 return stmt;
33807 /* The parser. */
33809 static GTY (()) cp_parser *the_parser;
33812 /* Special handling for the first token or line in the file. The first
33813 thing in the file might be #pragma GCC pch_preprocess, which loads a
33814 PCH file, which is a GC collection point. So we need to handle this
33815 first pragma without benefit of an existing lexer structure.
33817 Always returns one token to the caller in *FIRST_TOKEN. This is
33818 either the true first token of the file, or the first token after
33819 the initial pragma. */
33821 static void
33822 cp_parser_initial_pragma (cp_token *first_token)
33824 tree name = NULL;
33826 cp_lexer_get_preprocessor_token (NULL, first_token);
33827 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
33828 return;
33830 cp_lexer_get_preprocessor_token (NULL, first_token);
33831 if (first_token->type == CPP_STRING)
33833 name = first_token->u.value;
33835 cp_lexer_get_preprocessor_token (NULL, first_token);
33836 if (first_token->type != CPP_PRAGMA_EOL)
33837 error_at (first_token->location,
33838 "junk at end of %<#pragma GCC pch_preprocess%>");
33840 else
33841 error_at (first_token->location, "expected string literal");
33843 /* Skip to the end of the pragma. */
33844 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
33845 cp_lexer_get_preprocessor_token (NULL, first_token);
33847 /* Now actually load the PCH file. */
33848 if (name)
33849 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
33851 /* Read one more token to return to our caller. We have to do this
33852 after reading the PCH file in, since its pointers have to be
33853 live. */
33854 cp_lexer_get_preprocessor_token (NULL, first_token);
33857 /* Parses the grainsize pragma for the _Cilk_for statement.
33858 Syntax:
33859 #pragma cilk grainsize = <VALUE>. */
33861 static void
33862 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
33864 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
33866 tree exp = cp_parser_binary_expression (parser, false, false,
33867 PREC_NOT_OPERATOR, NULL);
33868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33869 if (!exp || exp == error_mark_node)
33871 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
33872 return;
33875 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
33876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33877 cp_parser_cilk_for (parser, exp);
33878 else
33879 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
33880 "%<#pragma cilk grainsize%> is not followed by "
33881 "%<_Cilk_for%>");
33882 return;
33884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33887 /* Normal parsing of a pragma token. Here we can (and must) use the
33888 regular lexer. */
33890 static bool
33891 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33893 cp_token *pragma_tok;
33894 unsigned int id;
33895 tree stmt;
33896 bool ret;
33898 pragma_tok = cp_lexer_consume_token (parser->lexer);
33899 gcc_assert (pragma_tok->type == CPP_PRAGMA);
33900 parser->lexer->in_pragma = true;
33902 id = pragma_tok->pragma_kind;
33903 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33904 cp_ensure_no_omp_declare_simd (parser);
33905 switch (id)
33907 case PRAGMA_GCC_PCH_PREPROCESS:
33908 error_at (pragma_tok->location,
33909 "%<#pragma GCC pch_preprocess%> must be first");
33910 break;
33912 case PRAGMA_OMP_BARRIER:
33913 switch (context)
33915 case pragma_compound:
33916 cp_parser_omp_barrier (parser, pragma_tok);
33917 return false;
33918 case pragma_stmt:
33919 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33920 "used in compound statements");
33921 break;
33922 default:
33923 goto bad_stmt;
33925 break;
33927 case PRAGMA_OMP_FLUSH:
33928 switch (context)
33930 case pragma_compound:
33931 cp_parser_omp_flush (parser, pragma_tok);
33932 return false;
33933 case pragma_stmt:
33934 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33935 "used in compound statements");
33936 break;
33937 default:
33938 goto bad_stmt;
33940 break;
33942 case PRAGMA_OMP_TASKWAIT:
33943 switch (context)
33945 case pragma_compound:
33946 cp_parser_omp_taskwait (parser, pragma_tok);
33947 return false;
33948 case pragma_stmt:
33949 error_at (pragma_tok->location,
33950 "%<#pragma omp taskwait%> may only be "
33951 "used in compound statements");
33952 break;
33953 default:
33954 goto bad_stmt;
33956 break;
33958 case PRAGMA_OMP_TASKYIELD:
33959 switch (context)
33961 case pragma_compound:
33962 cp_parser_omp_taskyield (parser, pragma_tok);
33963 return false;
33964 case pragma_stmt:
33965 error_at (pragma_tok->location,
33966 "%<#pragma omp taskyield%> may only be "
33967 "used in compound statements");
33968 break;
33969 default:
33970 goto bad_stmt;
33972 break;
33974 case PRAGMA_OMP_CANCEL:
33975 switch (context)
33977 case pragma_compound:
33978 cp_parser_omp_cancel (parser, pragma_tok);
33979 return false;
33980 case pragma_stmt:
33981 error_at (pragma_tok->location,
33982 "%<#pragma omp cancel%> may only be "
33983 "used in compound statements");
33984 break;
33985 default:
33986 goto bad_stmt;
33988 break;
33990 case PRAGMA_OMP_CANCELLATION_POINT:
33991 switch (context)
33993 case pragma_compound:
33994 cp_parser_omp_cancellation_point (parser, pragma_tok);
33995 return false;
33996 case pragma_stmt:
33997 error_at (pragma_tok->location,
33998 "%<#pragma omp cancellation point%> may only be "
33999 "used in compound statements");
34000 break;
34001 default:
34002 goto bad_stmt;
34004 break;
34006 case PRAGMA_OMP_THREADPRIVATE:
34007 cp_parser_omp_threadprivate (parser, pragma_tok);
34008 return false;
34010 case PRAGMA_OMP_DECLARE_REDUCTION:
34011 cp_parser_omp_declare (parser, pragma_tok, context);
34012 return false;
34014 case PRAGMA_OACC_CACHE:
34015 case PRAGMA_OACC_DATA:
34016 case PRAGMA_OACC_ENTER_DATA:
34017 case PRAGMA_OACC_EXIT_DATA:
34018 case PRAGMA_OACC_KERNELS:
34019 case PRAGMA_OACC_PARALLEL:
34020 case PRAGMA_OACC_LOOP:
34021 case PRAGMA_OACC_UPDATE:
34022 case PRAGMA_OACC_WAIT:
34023 case PRAGMA_OMP_ATOMIC:
34024 case PRAGMA_OMP_CRITICAL:
34025 case PRAGMA_OMP_DISTRIBUTE:
34026 case PRAGMA_OMP_FOR:
34027 case PRAGMA_OMP_MASTER:
34028 case PRAGMA_OMP_PARALLEL:
34029 case PRAGMA_OMP_SECTIONS:
34030 case PRAGMA_OMP_SIMD:
34031 case PRAGMA_OMP_SINGLE:
34032 case PRAGMA_OMP_TASK:
34033 case PRAGMA_OMP_TASKGROUP:
34034 case PRAGMA_OMP_TASKLOOP:
34035 case PRAGMA_OMP_TEAMS:
34036 if (context != pragma_stmt && context != pragma_compound)
34037 goto bad_stmt;
34038 stmt = push_omp_privatization_clauses (false);
34039 cp_parser_omp_construct (parser, pragma_tok);
34040 pop_omp_privatization_clauses (stmt);
34041 return true;
34043 case PRAGMA_OMP_ORDERED:
34044 stmt = push_omp_privatization_clauses (false);
34045 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
34046 pop_omp_privatization_clauses (stmt);
34047 return ret;
34049 case PRAGMA_OMP_TARGET:
34050 stmt = push_omp_privatization_clauses (false);
34051 ret = cp_parser_omp_target (parser, pragma_tok, context);
34052 pop_omp_privatization_clauses (stmt);
34053 return ret;
34055 case PRAGMA_OMP_END_DECLARE_TARGET:
34056 cp_parser_omp_end_declare_target (parser, pragma_tok);
34057 return false;
34059 case PRAGMA_OMP_SECTION:
34060 error_at (pragma_tok->location,
34061 "%<#pragma omp section%> may only be used in "
34062 "%<#pragma omp sections%> construct");
34063 break;
34065 case PRAGMA_IVDEP:
34067 if (context == pragma_external)
34069 error_at (pragma_tok->location,
34070 "%<#pragma GCC ivdep%> must be inside a function");
34071 break;
34073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34074 cp_token *tok;
34075 tok = cp_lexer_peek_token (the_parser->lexer);
34076 if (tok->type != CPP_KEYWORD
34077 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
34078 && tok->keyword != RID_DO))
34080 cp_parser_error (parser, "for, while or do statement expected");
34081 return false;
34083 cp_parser_iteration_statement (parser, true);
34084 return true;
34087 case PRAGMA_CILK_SIMD:
34088 if (context == pragma_external)
34090 error_at (pragma_tok->location,
34091 "%<#pragma simd%> must be inside a function");
34092 break;
34094 stmt = push_omp_privatization_clauses (false);
34095 cp_parser_cilk_simd (parser, pragma_tok);
34096 pop_omp_privatization_clauses (stmt);
34097 return true;
34099 case PRAGMA_CILK_GRAINSIZE:
34100 if (context == pragma_external)
34102 error_at (pragma_tok->location,
34103 "%<#pragma cilk grainsize%> must be inside a function");
34104 break;
34107 /* Ignore the pragma if Cilk Plus is not enabled. */
34108 if (flag_cilkplus)
34110 cp_parser_cilk_grainsize (parser, pragma_tok);
34111 return true;
34113 else
34115 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
34116 "%<#pragma cilk grainsize%>");
34117 break;
34120 default:
34121 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
34122 c_invoke_pragma_handler (id);
34123 break;
34125 bad_stmt:
34126 cp_parser_error (parser, "expected declaration specifiers");
34127 break;
34130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34131 return false;
34134 /* The interface the pragma parsers have to the lexer. */
34136 enum cpp_ttype
34137 pragma_lex (tree *value)
34139 cp_token *tok;
34140 enum cpp_ttype ret;
34142 tok = cp_lexer_peek_token (the_parser->lexer);
34144 ret = tok->type;
34145 *value = tok->u.value;
34147 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
34148 ret = CPP_EOF;
34149 else if (ret == CPP_STRING)
34150 *value = cp_parser_string_literal (the_parser, false, false);
34151 else
34153 cp_lexer_consume_token (the_parser->lexer);
34154 if (ret == CPP_KEYWORD)
34155 ret = CPP_NAME;
34158 return ret;
34162 /* External interface. */
34164 /* Parse one entire translation unit. */
34166 void
34167 c_parse_file (void)
34169 static bool already_called = false;
34171 if (already_called)
34172 fatal_error (input_location,
34173 "inter-module optimizations not implemented for C++");
34174 already_called = true;
34176 the_parser = cp_parser_new ();
34177 push_deferring_access_checks (flag_access_control
34178 ? dk_no_deferred : dk_no_check);
34179 cp_parser_translation_unit (the_parser);
34180 the_parser = NULL;
34183 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
34184 vectorlength clause:
34185 Syntax:
34186 vectorlength ( constant-expression ) */
34188 static tree
34189 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
34190 bool is_simd_fn)
34192 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34193 tree expr;
34194 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
34195 safelen clause. Thus, vectorlength is represented as OMP 4.0
34196 safelen. For SIMD-enabled function it is represented by OMP 4.0
34197 simdlen. */
34198 if (!is_simd_fn)
34199 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
34200 loc);
34201 else
34202 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
34203 loc);
34205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34206 return error_mark_node;
34208 expr = cp_parser_constant_expression (parser);
34209 expr = maybe_constant_value (expr);
34211 /* If expr == error_mark_node, then don't emit any errors nor
34212 create a clause. if any of the above functions returns
34213 error mark node then they would have emitted an error message. */
34214 if (expr == error_mark_node)
34216 else if (!TREE_TYPE (expr)
34217 || !TREE_CONSTANT (expr)
34218 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
34219 error_at (loc, "vectorlength must be an integer constant");
34220 else if (TREE_CONSTANT (expr)
34221 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
34222 error_at (loc, "vectorlength must be a power of 2");
34223 else
34225 tree c;
34226 if (!is_simd_fn)
34228 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
34229 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
34230 OMP_CLAUSE_CHAIN (c) = clauses;
34231 clauses = c;
34233 else
34235 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
34236 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
34237 OMP_CLAUSE_CHAIN (c) = clauses;
34238 clauses = c;
34242 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34243 return error_mark_node;
34244 return clauses;
34247 /* Handles the Cilk Plus #pragma simd linear clause.
34248 Syntax:
34249 linear ( simd-linear-variable-list )
34251 simd-linear-variable-list:
34252 simd-linear-variable
34253 simd-linear-variable-list , simd-linear-variable
34255 simd-linear-variable:
34256 id-expression
34257 id-expression : simd-linear-step
34259 simd-linear-step:
34260 conditional-expression */
34262 static tree
34263 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
34265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34267 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34268 return clauses;
34269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34271 cp_parser_error (parser, "expected identifier");
34272 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34273 return error_mark_node;
34276 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34277 parser->colon_corrects_to_scope_p = false;
34278 while (1)
34280 cp_token *token = cp_lexer_peek_token (parser->lexer);
34281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34283 cp_parser_error (parser, "expected variable-name");
34284 clauses = error_mark_node;
34285 break;
34288 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
34289 false, false);
34290 tree decl = cp_parser_lookup_name_simple (parser, var_name,
34291 token->location);
34292 if (decl == error_mark_node)
34294 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
34295 token->location);
34296 clauses = error_mark_node;
34298 else
34300 tree e = NULL_TREE;
34301 tree step_size = integer_one_node;
34303 /* If present, parse the linear step. Otherwise, assume the default
34304 value of 1. */
34305 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
34307 cp_lexer_consume_token (parser->lexer);
34309 e = cp_parser_assignment_expression (parser);
34310 e = maybe_constant_value (e);
34312 if (e == error_mark_node)
34314 /* If an error has occurred, then the whole pragma is
34315 considered ill-formed. Thus, no reason to keep
34316 parsing. */
34317 clauses = error_mark_node;
34318 break;
34320 else if (type_dependent_expression_p (e)
34321 || value_dependent_expression_p (e)
34322 || (TREE_TYPE (e)
34323 && INTEGRAL_TYPE_P (TREE_TYPE (e))
34324 && (TREE_CONSTANT (e)
34325 || DECL_P (e))))
34326 step_size = e;
34327 else
34328 cp_parser_error (parser,
34329 "step size must be an integer constant "
34330 "expression or an integer variable");
34333 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
34334 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34335 OMP_CLAUSE_DECL (l) = decl;
34336 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
34337 OMP_CLAUSE_CHAIN (l) = clauses;
34338 clauses = l;
34340 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34341 cp_lexer_consume_token (parser->lexer);
34342 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34343 break;
34344 else
34346 error_at (cp_lexer_peek_token (parser->lexer)->location,
34347 "expected %<,%> or %<)%> after %qE", decl);
34348 clauses = error_mark_node;
34349 break;
34352 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34353 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34354 return clauses;
34357 /* Returns the name of the next clause. If the clause is not
34358 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
34359 token is not consumed. Otherwise, the appropriate enum from the
34360 pragma_simd_clause is returned and the token is consumed. */
34362 static pragma_omp_clause
34363 cp_parser_cilk_simd_clause_name (cp_parser *parser)
34365 pragma_omp_clause clause_type;
34366 cp_token *token = cp_lexer_peek_token (parser->lexer);
34368 if (token->keyword == RID_PRIVATE)
34369 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
34370 else if (!token->u.value || token->type != CPP_NAME)
34371 return PRAGMA_CILK_CLAUSE_NONE;
34372 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
34373 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
34374 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
34375 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
34376 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
34377 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
34378 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
34379 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
34380 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
34381 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
34382 else
34383 return PRAGMA_CILK_CLAUSE_NONE;
34385 cp_lexer_consume_token (parser->lexer);
34386 return clause_type;
34389 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
34391 static tree
34392 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
34394 tree clauses = NULL_TREE;
34396 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34397 && clauses != error_mark_node)
34399 pragma_omp_clause c_kind;
34400 c_kind = cp_parser_cilk_simd_clause_name (parser);
34401 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
34402 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
34403 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
34404 clauses = cp_parser_cilk_simd_linear (parser, clauses);
34405 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
34406 /* Use the OpenMP 4.0 equivalent function. */
34407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
34408 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
34409 /* Use the OpenMP 4.0 equivalent function. */
34410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34411 clauses);
34412 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
34413 /* Use the OMP 4.0 equivalent function. */
34414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34415 clauses);
34416 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
34417 /* Use the OMP 4.0 equivalent function. */
34418 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34419 else
34421 clauses = error_mark_node;
34422 cp_parser_error (parser, "expected %<#pragma simd%> clause");
34423 break;
34427 cp_parser_skip_to_pragma_eol (parser, pragma_token);
34429 if (clauses == error_mark_node)
34430 return error_mark_node;
34431 else
34432 return c_finish_cilk_clauses (clauses);
34435 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
34437 static void
34438 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
34440 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
34442 if (clauses == error_mark_node)
34443 return;
34445 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
34447 error_at (cp_lexer_peek_token (parser->lexer)->location,
34448 "for statement expected");
34449 return;
34452 tree sb = begin_omp_structured_block ();
34453 int save = cp_parser_begin_omp_structured_block (parser);
34454 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
34455 if (ret)
34456 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
34457 cp_parser_end_omp_structured_block (parser, save);
34458 add_stmt (finish_omp_structured_block (sb));
34461 /* Main entry-point for parsing Cilk Plus _Cilk_for
34462 loops. The return value is error_mark_node
34463 when errors happen and CILK_FOR tree on success. */
34465 static tree
34466 cp_parser_cilk_for (cp_parser *parser, tree grain)
34468 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
34469 gcc_unreachable ();
34471 tree sb = begin_omp_structured_block ();
34472 int save = cp_parser_begin_omp_structured_block (parser);
34474 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
34475 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
34476 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
34477 clauses = finish_omp_clauses (clauses, false);
34479 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
34480 if (ret)
34481 cpp_validate_cilk_plus_loop (ret);
34482 else
34483 ret = error_mark_node;
34485 cp_parser_end_omp_structured_block (parser, save);
34486 add_stmt (finish_omp_structured_block (sb));
34487 return ret;
34490 /* Create an identifier for a generic parameter type (a synthesized
34491 template parameter implied by `auto' or a concept identifier). */
34493 static GTY(()) int generic_parm_count;
34494 static tree
34495 make_generic_type_name ()
34497 char buf[32];
34498 sprintf (buf, "auto:%d", ++generic_parm_count);
34499 return get_identifier (buf);
34502 /* Predicate that behaves as is_auto_or_concept but matches the parent
34503 node of the generic type rather than the generic type itself. This
34504 allows for type transformation in add_implicit_template_parms. */
34506 static inline bool
34507 tree_type_is_auto_or_concept (const_tree t)
34509 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
34512 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
34513 (creating a new template parameter list if necessary). Returns the newly
34514 created template type parm. */
34516 tree
34517 synthesize_implicit_template_parm (cp_parser *parser)
34519 gcc_assert (current_binding_level->kind == sk_function_parms);
34521 /* We are either continuing a function template that already contains implicit
34522 template parameters, creating a new fully-implicit function template, or
34523 extending an existing explicit function template with implicit template
34524 parameters. */
34526 cp_binding_level *const entry_scope = current_binding_level;
34528 bool become_template = false;
34529 cp_binding_level *parent_scope = 0;
34531 if (parser->implicit_template_scope)
34533 gcc_assert (parser->implicit_template_parms);
34535 current_binding_level = parser->implicit_template_scope;
34537 else
34539 /* Roll back to the existing template parameter scope (in the case of
34540 extending an explicit function template) or introduce a new template
34541 parameter scope ahead of the function parameter scope (or class scope
34542 in the case of out-of-line member definitions). The function scope is
34543 added back after template parameter synthesis below. */
34545 cp_binding_level *scope = entry_scope;
34547 while (scope->kind == sk_function_parms)
34549 parent_scope = scope;
34550 scope = scope->level_chain;
34552 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
34554 /* If not defining a class, then any class scope is a scope level in
34555 an out-of-line member definition. In this case simply wind back
34556 beyond the first such scope to inject the template parameter list.
34557 Otherwise wind back to the class being defined. The latter can
34558 occur in class member friend declarations such as:
34560 class A {
34561 void foo (auto);
34563 class B {
34564 friend void A::foo (auto);
34567 The template parameter list synthesized for the friend declaration
34568 must be injected in the scope of 'B'. This can also occur in
34569 erroneous cases such as:
34571 struct A {
34572 struct B {
34573 void foo (auto);
34575 void B::foo (auto) {}
34578 Here the attempted definition of 'B::foo' within 'A' is ill-formed
34579 but, nevertheless, the template parameter list synthesized for the
34580 declarator should be injected into the scope of 'A' as if the
34581 ill-formed template was specified explicitly. */
34583 while (scope->kind == sk_class && !scope->defining_class_p)
34585 parent_scope = scope;
34586 scope = scope->level_chain;
34590 current_binding_level = scope;
34592 if (scope->kind != sk_template_parms
34593 || !function_being_declared_is_template_p (parser))
34595 /* Introduce a new template parameter list for implicit template
34596 parameters. */
34598 become_template = true;
34600 parser->implicit_template_scope
34601 = begin_scope (sk_template_parms, NULL);
34603 ++processing_template_decl;
34605 parser->fully_implicit_function_template_p = true;
34606 ++parser->num_template_parameter_lists;
34608 else
34610 /* Synthesize implicit template parameters at the end of the explicit
34611 template parameter list. */
34613 gcc_assert (current_template_parms);
34615 parser->implicit_template_scope = scope;
34617 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34618 parser->implicit_template_parms
34619 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
34623 /* Synthesize a new template parameter and track the current template
34624 parameter chain with implicit_template_parms. */
34626 tree synth_id = make_generic_type_name ();
34627 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
34628 synth_id);
34629 tree new_parm
34630 = process_template_parm (parser->implicit_template_parms,
34631 input_location,
34632 build_tree_list (NULL_TREE, synth_tmpl_parm),
34633 /*non_type=*/false,
34634 /*param_pack=*/false);
34637 if (parser->implicit_template_parms)
34638 parser->implicit_template_parms
34639 = TREE_CHAIN (parser->implicit_template_parms);
34640 else
34641 parser->implicit_template_parms = new_parm;
34643 tree new_type = TREE_TYPE (getdecls ());
34645 /* If creating a fully implicit function template, start the new implicit
34646 template parameter list with this synthesized type, otherwise grow the
34647 current template parameter list. */
34649 if (become_template)
34651 parent_scope->level_chain = current_binding_level;
34653 tree new_parms = make_tree_vec (1);
34654 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
34655 current_template_parms = tree_cons (size_int (processing_template_decl),
34656 new_parms, current_template_parms);
34658 else
34660 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34661 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
34662 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
34663 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
34666 current_binding_level = entry_scope;
34668 return new_type;
34671 /* Finish the declaration of a fully implicit function template. Such a
34672 template has no explicit template parameter list so has not been through the
34673 normal template head and tail processing. synthesize_implicit_template_parm
34674 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
34675 provided if the declaration is a class member such that its template
34676 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
34677 form is returned. Otherwise NULL_TREE is returned. */
34679 tree
34680 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
34682 gcc_assert (parser->fully_implicit_function_template_p);
34684 if (member_decl_opt && member_decl_opt != error_mark_node
34685 && DECL_VIRTUAL_P (member_decl_opt))
34687 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
34688 "implicit templates may not be %<virtual%>");
34689 DECL_VIRTUAL_P (member_decl_opt) = false;
34692 if (member_decl_opt)
34693 member_decl_opt = finish_member_template_decl (member_decl_opt);
34694 end_template_decl ();
34696 parser->fully_implicit_function_template_p = false;
34697 --parser->num_template_parameter_lists;
34699 return member_decl_opt;
34702 #include "gt-cp-parser.h"