libcpp:
[official-gcc.git] / gcc / cp / parser.c
blobbb3d636eee1327d052d30dd75a510de77455ad35
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "print-tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "trans-mem.h"
34 #include "cp-tree.h"
35 #include "intl.h"
36 #include "c-family/c-pragma.h"
37 #include "decl.h"
38 #include "flags.h"
39 #include "diagnostic-core.h"
40 #include "target.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "cgraph.h"
44 #include "c-family/c-common.h"
45 #include "c-family/c-objc.h"
46 #include "plugin.h"
47 #include "tree-pretty-print.h"
48 #include "parser.h"
49 #include "type-utils.h"
50 #include "omp-low.h"
51 #include "gomp-constants.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.c) and the C++ parser. */
59 static cp_token eof_token =
61 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
64 /* The various kinds of non integral constant we encounter. */
65 typedef enum non_integral_constant {
66 NIC_NONE,
67 /* floating-point literal */
68 NIC_FLOAT,
69 /* %<this%> */
70 NIC_THIS,
71 /* %<__FUNCTION__%> */
72 NIC_FUNC_NAME,
73 /* %<__PRETTY_FUNCTION__%> */
74 NIC_PRETTY_FUNC,
75 /* %<__func__%> */
76 NIC_C99_FUNC,
77 /* "%<va_arg%> */
78 NIC_VA_ARG,
79 /* a cast */
80 NIC_CAST,
81 /* %<typeid%> operator */
82 NIC_TYPEID,
83 /* non-constant compound literals */
84 NIC_NCC,
85 /* a function call */
86 NIC_FUNC_CALL,
87 /* an increment */
88 NIC_INC,
89 /* an decrement */
90 NIC_DEC,
91 /* an array reference */
92 NIC_ARRAY_REF,
93 /* %<->%> */
94 NIC_ARROW,
95 /* %<.%> */
96 NIC_POINT,
97 /* the address of a label */
98 NIC_ADDR_LABEL,
99 /* %<*%> */
100 NIC_STAR,
101 /* %<&%> */
102 NIC_ADDR,
103 /* %<++%> */
104 NIC_PREINCREMENT,
105 /* %<--%> */
106 NIC_PREDECREMENT,
107 /* %<new%> */
108 NIC_NEW,
109 /* %<delete%> */
110 NIC_DEL,
111 /* calls to overloaded operators */
112 NIC_OVERLOADED,
113 /* an assignment */
114 NIC_ASSIGNMENT,
115 /* a comma operator */
116 NIC_COMMA,
117 /* a call to a constructor */
118 NIC_CONSTRUCTOR,
119 /* a transaction expression */
120 NIC_TRANSACTION
121 } non_integral_constant;
123 /* The various kinds of errors about name-lookup failing. */
124 typedef enum name_lookup_error {
125 /* NULL */
126 NLE_NULL,
127 /* is not a type */
128 NLE_TYPE,
129 /* is not a class or namespace */
130 NLE_CXX98,
131 /* is not a class, namespace, or enumeration */
132 NLE_NOT_CXX98
133 } name_lookup_error;
135 /* The various kinds of required token */
136 typedef enum required_token {
137 RT_NONE,
138 RT_SEMICOLON, /* ';' */
139 RT_OPEN_PAREN, /* '(' */
140 RT_CLOSE_BRACE, /* '}' */
141 RT_OPEN_BRACE, /* '{' */
142 RT_CLOSE_SQUARE, /* ']' */
143 RT_OPEN_SQUARE, /* '[' */
144 RT_COMMA, /* ',' */
145 RT_SCOPE, /* '::' */
146 RT_LESS, /* '<' */
147 RT_GREATER, /* '>' */
148 RT_EQ, /* '=' */
149 RT_ELLIPSIS, /* '...' */
150 RT_MULT, /* '*' */
151 RT_COMPL, /* '~' */
152 RT_COLON, /* ':' */
153 RT_COLON_SCOPE, /* ':' or '::' */
154 RT_CLOSE_PAREN, /* ')' */
155 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
156 RT_PRAGMA_EOL, /* end of line */
157 RT_NAME, /* identifier */
159 /* The type is CPP_KEYWORD */
160 RT_NEW, /* new */
161 RT_DELETE, /* delete */
162 RT_RETURN, /* return */
163 RT_WHILE, /* while */
164 RT_EXTERN, /* extern */
165 RT_STATIC_ASSERT, /* static_assert */
166 RT_DECLTYPE, /* decltype */
167 RT_OPERATOR, /* operator */
168 RT_CLASS, /* class */
169 RT_TEMPLATE, /* template */
170 RT_NAMESPACE, /* namespace */
171 RT_USING, /* using */
172 RT_ASM, /* asm */
173 RT_TRY, /* try */
174 RT_CATCH, /* catch */
175 RT_THROW, /* throw */
176 RT_LABEL, /* __label__ */
177 RT_AT_TRY, /* @try */
178 RT_AT_SYNCHRONIZED, /* @synchronized */
179 RT_AT_THROW, /* @throw */
181 RT_SELECT, /* selection-statement */
182 RT_INTERATION, /* iteration-statement */
183 RT_JUMP, /* jump-statement */
184 RT_CLASS_KEY, /* class-key */
185 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
186 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
187 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
188 RT_TRANSACTION_CANCEL /* __transaction_cancel */
189 } required_token;
191 /* Prototypes. */
193 static cp_lexer *cp_lexer_new_main
194 (void);
195 static cp_lexer *cp_lexer_new_from_tokens
196 (cp_token_cache *tokens);
197 static void cp_lexer_destroy
198 (cp_lexer *);
199 static int cp_lexer_saving_tokens
200 (const cp_lexer *);
201 static cp_token *cp_lexer_token_at
202 (cp_lexer *, cp_token_position);
203 static void cp_lexer_get_preprocessor_token
204 (cp_lexer *, cp_token *);
205 static inline cp_token *cp_lexer_peek_token
206 (cp_lexer *);
207 static cp_token *cp_lexer_peek_nth_token
208 (cp_lexer *, size_t);
209 static inline bool cp_lexer_next_token_is
210 (cp_lexer *, enum cpp_ttype);
211 static bool cp_lexer_next_token_is_not
212 (cp_lexer *, enum cpp_ttype);
213 static bool cp_lexer_next_token_is_keyword
214 (cp_lexer *, enum rid);
215 static cp_token *cp_lexer_consume_token
216 (cp_lexer *);
217 static void cp_lexer_purge_token
218 (cp_lexer *);
219 static void cp_lexer_purge_tokens_after
220 (cp_lexer *, cp_token_position);
221 static void cp_lexer_save_tokens
222 (cp_lexer *);
223 static void cp_lexer_commit_tokens
224 (cp_lexer *);
225 static void cp_lexer_rollback_tokens
226 (cp_lexer *);
227 static void cp_lexer_print_token
228 (FILE *, cp_token *);
229 static inline bool cp_lexer_debugging_p
230 (cp_lexer *);
231 static void cp_lexer_start_debugging
232 (cp_lexer *) ATTRIBUTE_UNUSED;
233 static void cp_lexer_stop_debugging
234 (cp_lexer *) ATTRIBUTE_UNUSED;
236 static cp_token_cache *cp_token_cache_new
237 (cp_token *, cp_token *);
239 static void cp_parser_initial_pragma
240 (cp_token *);
242 static tree cp_literal_operator_id
243 (const char *);
245 static void cp_parser_cilk_simd
246 (cp_parser *, cp_token *);
247 static tree cp_parser_cilk_for
248 (cp_parser *, tree);
249 static bool cp_parser_omp_declare_reduction_exprs
250 (tree, cp_parser *);
251 static tree cp_parser_cilk_simd_vectorlength
252 (cp_parser *, tree, bool);
254 /* Manifest constants. */
255 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
256 #define CP_SAVED_TOKEN_STACK 5
258 /* Variables. */
260 /* The stream to which debugging output should be written. */
261 static FILE *cp_lexer_debug_stream;
263 /* Nonzero if we are parsing an unevaluated operand: an operand to
264 sizeof, typeof, or alignof. */
265 int cp_unevaluated_operand;
267 /* Dump up to NUM tokens in BUFFER to FILE starting with token
268 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
269 first token in BUFFER. If NUM is 0, dump all the tokens. If
270 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
271 highlighted by surrounding it in [[ ]]. */
273 static void
274 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
275 cp_token *start_token, unsigned num,
276 cp_token *curr_token)
278 unsigned i, nprinted;
279 cp_token *token;
280 bool do_print;
282 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
284 if (buffer == NULL)
285 return;
287 if (num == 0)
288 num = buffer->length ();
290 if (start_token == NULL)
291 start_token = buffer->address ();
293 if (start_token > buffer->address ())
295 cp_lexer_print_token (file, &(*buffer)[0]);
296 fprintf (file, " ... ");
299 do_print = false;
300 nprinted = 0;
301 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
303 if (token == start_token)
304 do_print = true;
306 if (!do_print)
307 continue;
309 nprinted++;
310 if (token == curr_token)
311 fprintf (file, "[[");
313 cp_lexer_print_token (file, token);
315 if (token == curr_token)
316 fprintf (file, "]]");
318 switch (token->type)
320 case CPP_SEMICOLON:
321 case CPP_OPEN_BRACE:
322 case CPP_CLOSE_BRACE:
323 case CPP_EOF:
324 fputc ('\n', file);
325 break;
327 default:
328 fputc (' ', file);
332 if (i == num && i < buffer->length ())
334 fprintf (file, " ... ");
335 cp_lexer_print_token (file, &buffer->last ());
338 fprintf (file, "\n");
342 /* Dump all tokens in BUFFER to stderr. */
344 void
345 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
347 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
350 DEBUG_FUNCTION void
351 debug (vec<cp_token, va_gc> &ref)
353 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> *ptr)
359 if (ptr)
360 debug (*ptr);
361 else
362 fprintf (stderr, "<nil>\n");
366 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
367 description for T. */
369 static void
370 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
372 if (t)
374 fprintf (file, "%s: ", desc);
375 print_node_brief (file, "", t, 0);
380 /* Dump parser context C to FILE. */
382 static void
383 cp_debug_print_context (FILE *file, cp_parser_context *c)
385 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
386 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
387 print_node_brief (file, "", c->object_type, 0);
388 fprintf (file, "}\n");
392 /* Print the stack of parsing contexts to FILE starting with FIRST. */
394 static void
395 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
397 unsigned i;
398 cp_parser_context *c;
400 fprintf (file, "Parsing context stack:\n");
401 for (i = 0, c = first; c; c = c->next, i++)
403 fprintf (file, "\t#%u: ", i);
404 cp_debug_print_context (file, c);
409 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
411 static void
412 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
414 if (flag)
415 fprintf (file, "%s: true\n", desc);
419 /* Print an unparsed function entry UF to FILE. */
421 static void
422 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
424 unsigned i;
425 cp_default_arg_entry *default_arg_fn;
426 tree fn;
428 fprintf (file, "\tFunctions with default args:\n");
429 for (i = 0;
430 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
431 i++)
433 fprintf (file, "\t\tClass type: ");
434 print_node_brief (file, "", default_arg_fn->class_type, 0);
435 fprintf (file, "\t\tDeclaration: ");
436 print_node_brief (file, "", default_arg_fn->decl, 0);
437 fprintf (file, "\n");
440 fprintf (file, "\n\tFunctions with definitions that require "
441 "post-processing\n\t\t");
442 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
444 print_node_brief (file, "", fn, 0);
445 fprintf (file, " ");
447 fprintf (file, "\n");
449 fprintf (file, "\n\tNon-static data members with initializers that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
460 /* Print the stack of unparsed member functions S to FILE. */
462 static void
463 cp_debug_print_unparsed_queues (FILE *file,
464 vec<cp_unparsed_functions_entry, va_gc> *s)
466 unsigned i;
467 cp_unparsed_functions_entry *uf;
469 fprintf (file, "Unparsed functions\n");
470 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
472 fprintf (file, "#%u:\n", i);
473 cp_debug_print_unparsed_function (file, uf);
478 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
479 the given PARSER. If FILE is NULL, the output is printed on stderr. */
481 static void
482 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
484 cp_token *next_token, *first_token, *start_token;
486 if (file == NULL)
487 file = stderr;
489 next_token = parser->lexer->next_token;
490 first_token = parser->lexer->buffer->address ();
491 start_token = (next_token > first_token + window_size / 2)
492 ? next_token - window_size / 2
493 : first_token;
494 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
495 next_token);
499 /* Dump debugging information for the given PARSER. If FILE is NULL,
500 the output is printed on stderr. */
502 void
503 cp_debug_parser (FILE *file, cp_parser *parser)
505 const size_t window_size = 20;
506 cp_token *token;
507 expanded_location eloc;
509 if (file == NULL)
510 file = stderr;
512 fprintf (file, "Parser state\n\n");
513 fprintf (file, "Number of tokens: %u\n",
514 vec_safe_length (parser->lexer->buffer));
515 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
516 cp_debug_print_tree_if_set (file, "Object scope",
517 parser->object_scope);
518 cp_debug_print_tree_if_set (file, "Qualifying scope",
519 parser->qualifying_scope);
520 cp_debug_print_context_stack (file, parser->context);
521 cp_debug_print_flag (file, "Allow GNU extensions",
522 parser->allow_gnu_extensions_p);
523 cp_debug_print_flag (file, "'>' token is greater-than",
524 parser->greater_than_is_operator_p);
525 cp_debug_print_flag (file, "Default args allowed in current "
526 "parameter list", parser->default_arg_ok_p);
527 cp_debug_print_flag (file, "Parsing integral constant-expression",
528 parser->integral_constant_expression_p);
529 cp_debug_print_flag (file, "Allow non-constant expression in current "
530 "constant-expression",
531 parser->allow_non_integral_constant_expression_p);
532 cp_debug_print_flag (file, "Seen non-constant expression",
533 parser->non_integral_constant_expression_p);
534 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
535 "current context",
536 parser->local_variables_forbidden_p);
537 cp_debug_print_flag (file, "In unbraced linkage specification",
538 parser->in_unbraced_linkage_specification_p);
539 cp_debug_print_flag (file, "Parsing a declarator",
540 parser->in_declarator_p);
541 cp_debug_print_flag (file, "In template argument list",
542 parser->in_template_argument_list_p);
543 cp_debug_print_flag (file, "Parsing an iteration statement",
544 parser->in_statement & IN_ITERATION_STMT);
545 cp_debug_print_flag (file, "Parsing a switch statement",
546 parser->in_statement & IN_SWITCH_STMT);
547 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
548 parser->in_statement & IN_OMP_BLOCK);
549 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
550 parser->in_statement & IN_CILK_SIMD_FOR);
551 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
552 parser->in_statement & IN_OMP_FOR);
553 cp_debug_print_flag (file, "Parsing an if statement",
554 parser->in_statement & IN_IF_STMT);
555 cp_debug_print_flag (file, "Parsing a type-id in an expression "
556 "context", parser->in_type_id_in_expr_p);
557 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
558 parser->implicit_extern_c);
559 cp_debug_print_flag (file, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
590 cp_debug_parser (stderr, &ref);
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
604 static cp_lexer *
605 cp_lexer_alloc (void)
607 cp_lexer *lexer;
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622 return lexer;
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
629 static cp_lexer *
630 cp_lexer_new_main (void)
632 cp_lexer *lexer;
633 cp_token token;
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
640 lexer = cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
691 /* Frees all resources associated with LEXER. */
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
701 /* Returns nonzero if debugging information should be output. */
703 static inline bool
704 cp_lexer_debugging_p (cp_lexer *lexer)
706 return lexer->debugging_p;
710 static inline cp_token_position
711 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
713 gcc_assert (!previous_p || lexer->next_token != &eof_token);
715 return lexer->next_token - previous_p;
718 static inline cp_token *
719 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
721 return pos;
724 static inline void
725 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
727 lexer->next_token = cp_lexer_token_at (lexer, pos);
730 static inline cp_token_position
731 cp_lexer_previous_token_position (cp_lexer *lexer)
733 if (lexer->next_token == &eof_token)
734 return lexer->last_token - 1;
735 else
736 return cp_lexer_token_position (lexer, true);
739 static inline cp_token *
740 cp_lexer_previous_token (cp_lexer *lexer)
742 cp_token_position tp = cp_lexer_previous_token_position (lexer);
744 return cp_lexer_token_at (lexer, tp);
747 /* nonzero if we are presently saving tokens. */
749 static inline int
750 cp_lexer_saving_tokens (const cp_lexer* lexer)
752 return lexer->saved_tokens.length () != 0;
755 /* Store the next token from the preprocessor in *TOKEN. Return true
756 if we reach EOF. If LEXER is NULL, assume we are handling an
757 initial #pragma pch_preprocess, and thus want the lexer to return
758 processed strings. */
760 static void
761 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
763 static int is_extern_c = 0;
765 /* Get a new token from the preprocessor. */
766 token->type
767 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
768 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
769 token->keyword = RID_MAX;
770 token->pragma_kind = PRAGMA_NONE;
771 token->purged_p = false;
772 token->error_reported = false;
774 /* On some systems, some header files are surrounded by an
775 implicit extern "C" block. Set a flag in the token if it
776 comes from such a header. */
777 is_extern_c += pending_lang_change;
778 pending_lang_change = 0;
779 token->implicit_extern_c = is_extern_c > 0;
781 /* Check to see if this token is a keyword. */
782 if (token->type == CPP_NAME)
784 if (C_IS_RESERVED_WORD (token->u.value))
786 /* Mark this token as a keyword. */
787 token->type = CPP_KEYWORD;
788 /* Record which keyword. */
789 token->keyword = C_RID_CODE (token->u.value);
791 else
793 if (warn_cxx11_compat
794 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
797 /* Warn about the C++0x keyword (but still treat it as
798 an identifier). */
799 warning (OPT_Wc__11_compat,
800 "identifier %qE is a keyword in C++11",
801 token->u.value);
803 /* Clear out the C_RID_CODE so we don't warn about this
804 particular identifier-turned-keyword again. */
805 C_SET_RID_CODE (token->u.value, RID_MAX);
808 token->keyword = RID_MAX;
811 else if (token->type == CPP_AT_NAME)
813 /* This only happens in Objective-C++; it must be a keyword. */
814 token->type = CPP_KEYWORD;
815 switch (C_RID_CODE (token->u.value))
817 /* Replace 'class' with '@class', 'private' with '@private',
818 etc. This prevents confusion with the C++ keyword
819 'class', and makes the tokens consistent with other
820 Objective-C 'AT' keywords. For example '@class' is
821 reported as RID_AT_CLASS which is consistent with
822 '@synchronized', which is reported as
823 RID_AT_SYNCHRONIZED.
825 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
826 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
827 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
828 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
829 case RID_THROW: token->keyword = RID_AT_THROW; break;
830 case RID_TRY: token->keyword = RID_AT_TRY; break;
831 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
832 default: token->keyword = C_RID_CODE (token->u.value);
835 else if (token->type == CPP_PRAGMA)
837 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
838 token->pragma_kind = ((enum pragma_kind)
839 TREE_INT_CST_LOW (token->u.value));
840 token->u.value = NULL_TREE;
844 /* Update the globals input_location and the input file stack from TOKEN. */
845 static inline void
846 cp_lexer_set_source_position_from_token (cp_token *token)
848 if (token->type != CPP_EOF)
850 input_location = token->location;
854 /* Update the globals input_location and the input file stack from LEXER. */
855 static inline void
856 cp_lexer_set_source_position (cp_lexer *lexer)
858 cp_token *token = cp_lexer_peek_token (lexer);
859 cp_lexer_set_source_position_from_token (token);
862 /* Return a pointer to the next token in the token stream, but do not
863 consume it. */
865 static inline cp_token *
866 cp_lexer_peek_token (cp_lexer *lexer)
868 if (cp_lexer_debugging_p (lexer))
870 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
871 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
872 putc ('\n', cp_lexer_debug_stream);
874 return lexer->next_token;
877 /* Return true if the next token has the indicated TYPE. */
879 static inline bool
880 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
882 return cp_lexer_peek_token (lexer)->type == type;
885 /* Return true if the next token does not have the indicated TYPE. */
887 static inline bool
888 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
890 return !cp_lexer_next_token_is (lexer, type);
893 /* Return true if the next token is the indicated KEYWORD. */
895 static inline bool
896 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
898 return cp_lexer_peek_token (lexer)->keyword == keyword;
901 static inline bool
902 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
904 return cp_lexer_peek_nth_token (lexer, n)->type == type;
907 static inline bool
908 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
910 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
913 /* Return true if the next token is not the indicated KEYWORD. */
915 static inline bool
916 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
918 return cp_lexer_peek_token (lexer)->keyword != keyword;
921 /* Return true if the next token is a keyword for a decl-specifier. */
923 static bool
924 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
926 cp_token *token;
928 token = cp_lexer_peek_token (lexer);
929 switch (token->keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 return true;
968 default:
969 if (token->keyword >= RID_FIRST_INT_N
970 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
977 /* Returns TRUE iff the token T begins a decltype type. */
979 static bool
980 token_is_decltype (cp_token *t)
982 return (t->keyword == RID_DECLTYPE
983 || t->type == CPP_DECLTYPE);
986 /* Returns TRUE iff the next token begins a decltype type. */
988 static bool
989 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
991 cp_token *t = cp_lexer_peek_token (lexer);
992 return token_is_decltype (t);
995 /* Return a pointer to the Nth token in the token stream. If N is 1,
996 then this is precisely equivalent to cp_lexer_peek_token (except
997 that it is not inline). One would like to disallow that case, but
998 there is one case (cp_parser_nth_token_starts_template_id) where
999 the caller passes a variable for N and it might be 1. */
1001 static cp_token *
1002 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1004 cp_token *token;
1006 /* N is 1-based, not zero-based. */
1007 gcc_assert (n > 0);
1009 if (cp_lexer_debugging_p (lexer))
1010 fprintf (cp_lexer_debug_stream,
1011 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1013 --n;
1014 token = lexer->next_token;
1015 gcc_assert (!n || token != &eof_token);
1016 while (n != 0)
1018 ++token;
1019 if (token == lexer->last_token)
1021 token = &eof_token;
1022 break;
1025 if (!token->purged_p)
1026 --n;
1029 if (cp_lexer_debugging_p (lexer))
1031 cp_lexer_print_token (cp_lexer_debug_stream, token);
1032 putc ('\n', cp_lexer_debug_stream);
1035 return token;
1038 /* Return the next token, and advance the lexer's next_token pointer
1039 to point to the next non-purged token. */
1041 static cp_token *
1042 cp_lexer_consume_token (cp_lexer* lexer)
1044 cp_token *token = lexer->next_token;
1046 gcc_assert (token != &eof_token);
1047 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1051 lexer->next_token++;
1052 if (lexer->next_token == lexer->last_token)
1054 lexer->next_token = &eof_token;
1055 break;
1059 while (lexer->next_token->purged_p);
1061 cp_lexer_set_source_position_from_token (token);
1063 /* Provide debugging output. */
1064 if (cp_lexer_debugging_p (lexer))
1066 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1067 cp_lexer_print_token (cp_lexer_debug_stream, token);
1068 putc ('\n', cp_lexer_debug_stream);
1071 return token;
1074 /* Permanently remove the next token from the token stream, and
1075 advance the next_token pointer to refer to the next non-purged
1076 token. */
1078 static void
1079 cp_lexer_purge_token (cp_lexer *lexer)
1081 cp_token *tok = lexer->next_token;
1083 gcc_assert (tok != &eof_token);
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1091 tok++;
1092 if (tok == lexer->last_token)
1094 tok = &eof_token;
1095 break;
1098 while (tok->purged_p);
1099 lexer->next_token = tok;
1102 /* Permanently remove all tokens after TOK, up to, but not
1103 including, the token that will be returned next by
1104 cp_lexer_peek_token. */
1106 static void
1107 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1109 cp_token *peek = lexer->next_token;
1111 if (peek == &eof_token)
1112 peek = lexer->last_token;
1114 gcc_assert (tok < peek);
1116 for ( tok += 1; tok != peek; tok += 1)
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 /* Begin saving tokens. All tokens consumed after this point will be
1126 preserved. */
1128 static void
1129 cp_lexer_save_tokens (cp_lexer* lexer)
1131 /* Provide debugging output. */
1132 if (cp_lexer_debugging_p (lexer))
1133 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1135 lexer->saved_tokens.safe_push (lexer->next_token);
1138 /* Commit to the portion of the token stream most recently saved. */
1140 static void
1141 cp_lexer_commit_tokens (cp_lexer* lexer)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1147 lexer->saved_tokens.pop ();
1150 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1151 to the token stream. Stop saving tokens. */
1153 static void
1154 cp_lexer_rollback_tokens (cp_lexer* lexer)
1156 /* Provide debugging output. */
1157 if (cp_lexer_debugging_p (lexer))
1158 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1160 lexer->next_token = lexer->saved_tokens.pop ();
1163 /* RAII wrapper around the above functions, with sanity checking. Creating
1164 a variable saves tokens, which are committed when the variable is
1165 destroyed unless they are explicitly rolled back by calling the rollback
1166 member function. */
1168 struct saved_token_sentinel
1170 cp_lexer *lexer;
1171 unsigned len;
1172 bool commit;
1173 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1175 len = lexer->saved_tokens.length ();
1176 cp_lexer_save_tokens (lexer);
1178 void rollback ()
1180 cp_lexer_rollback_tokens (lexer);
1181 commit = false;
1183 ~saved_token_sentinel()
1185 if (commit)
1186 cp_lexer_commit_tokens (lexer);
1187 gcc_assert (lexer->saved_tokens.length () == len);
1191 /* Print a representation of the TOKEN on the STREAM. */
1193 static void
1194 cp_lexer_print_token (FILE * stream, cp_token *token)
1196 /* We don't use cpp_type2name here because the parser defines
1197 a few tokens of its own. */
1198 static const char *const token_names[] = {
1199 /* cpplib-defined token types */
1200 #define OP(e, s) #e,
1201 #define TK(e, s) #e,
1202 TTYPE_TABLE
1203 #undef OP
1204 #undef TK
1205 /* C++ parser token types - see "Manifest constants", above. */
1206 "KEYWORD",
1207 "TEMPLATE_ID",
1208 "NESTED_NAME_SPECIFIER",
1211 /* For some tokens, print the associated data. */
1212 switch (token->type)
1214 case CPP_KEYWORD:
1215 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1216 For example, `struct' is mapped to an INTEGER_CST. */
1217 if (!identifier_p (token->u.value))
1218 break;
1219 /* else fall through */
1220 case CPP_NAME:
1221 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1222 break;
1224 case CPP_STRING:
1225 case CPP_STRING16:
1226 case CPP_STRING32:
1227 case CPP_WSTRING:
1228 case CPP_UTF8STRING:
1229 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1230 break;
1232 case CPP_NUMBER:
1233 print_generic_expr (stream, token->u.value, 0);
1234 break;
1236 default:
1237 /* If we have a name for the token, print it out. Otherwise, we
1238 simply give the numeric code. */
1239 if (token->type < ARRAY_SIZE(token_names))
1240 fputs (token_names[token->type], stream);
1241 else
1242 fprintf (stream, "[%d]", token->type);
1243 break;
1247 DEBUG_FUNCTION void
1248 debug (cp_token &ref)
1250 cp_lexer_print_token (stderr, &ref);
1251 fprintf (stderr, "\n");
1254 DEBUG_FUNCTION void
1255 debug (cp_token *ptr)
1257 if (ptr)
1258 debug (*ptr);
1259 else
1260 fprintf (stderr, "<nil>\n");
1264 /* Start emitting debugging information. */
1266 static void
1267 cp_lexer_start_debugging (cp_lexer* lexer)
1269 lexer->debugging_p = true;
1270 cp_lexer_debug_stream = stderr;
1273 /* Stop emitting debugging information. */
1275 static void
1276 cp_lexer_stop_debugging (cp_lexer* lexer)
1278 lexer->debugging_p = false;
1279 cp_lexer_debug_stream = NULL;
1282 /* Create a new cp_token_cache, representing a range of tokens. */
1284 static cp_token_cache *
1285 cp_token_cache_new (cp_token *first, cp_token *last)
1287 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1288 cache->first = first;
1289 cache->last = last;
1290 return cache;
1293 /* Diagnose if #pragma omp declare simd isn't followed immediately
1294 by function declaration or definition. */
1296 static inline void
1297 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1299 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1301 error ("%<#pragma omp declare simd%> not immediately followed by "
1302 "function declaration or definition");
1303 parser->omp_declare_simd = NULL;
1307 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1308 and put that into "omp declare simd" attribute. */
1310 static inline void
1311 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1313 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1315 if (fndecl == error_mark_node)
1317 parser->omp_declare_simd = NULL;
1318 return;
1320 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1322 cp_ensure_no_omp_declare_simd (parser);
1323 return;
1328 /* Decl-specifiers. */
1330 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1332 static void
1333 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1335 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1338 /* Declarators. */
1340 /* Nothing other than the parser should be creating declarators;
1341 declarators are a semi-syntactic representation of C++ entities.
1342 Other parts of the front end that need to create entities (like
1343 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1345 static cp_declarator *make_call_declarator
1346 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1347 static cp_declarator *make_array_declarator
1348 (cp_declarator *, tree);
1349 static cp_declarator *make_pointer_declarator
1350 (cp_cv_quals, cp_declarator *, tree);
1351 static cp_declarator *make_reference_declarator
1352 (cp_cv_quals, cp_declarator *, bool, tree);
1353 static cp_parameter_declarator *make_parameter_declarator
1354 (cp_decl_specifier_seq *, cp_declarator *, tree);
1355 static cp_declarator *make_ptrmem_declarator
1356 (cp_cv_quals, tree, cp_declarator *, tree);
1358 /* An erroneous declarator. */
1359 static cp_declarator *cp_error_declarator;
1361 /* The obstack on which declarators and related data structures are
1362 allocated. */
1363 static struct obstack declarator_obstack;
1365 /* Alloc BYTES from the declarator memory pool. */
1367 static inline void *
1368 alloc_declarator (size_t bytes)
1370 return obstack_alloc (&declarator_obstack, bytes);
1373 /* Allocate a declarator of the indicated KIND. Clear fields that are
1374 common to all declarators. */
1376 static cp_declarator *
1377 make_declarator (cp_declarator_kind kind)
1379 cp_declarator *declarator;
1381 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1382 declarator->kind = kind;
1383 declarator->attributes = NULL_TREE;
1384 declarator->std_attributes = NULL_TREE;
1385 declarator->declarator = NULL;
1386 declarator->parameter_pack_p = false;
1387 declarator->id_loc = UNKNOWN_LOCATION;
1389 return declarator;
1392 /* Make a declarator for a generalized identifier. If
1393 QUALIFYING_SCOPE is non-NULL, the identifier is
1394 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1395 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1396 is, if any. */
1398 static cp_declarator *
1399 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1400 special_function_kind sfk)
1402 cp_declarator *declarator;
1404 /* It is valid to write:
1406 class C { void f(); };
1407 typedef C D;
1408 void D::f();
1410 The standard is not clear about whether `typedef const C D' is
1411 legal; as of 2002-09-15 the committee is considering that
1412 question. EDG 3.0 allows that syntax. Therefore, we do as
1413 well. */
1414 if (qualifying_scope && TYPE_P (qualifying_scope))
1415 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1417 gcc_assert (identifier_p (unqualified_name)
1418 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1419 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1421 declarator = make_declarator (cdk_id);
1422 declarator->u.id.qualifying_scope = qualifying_scope;
1423 declarator->u.id.unqualified_name = unqualified_name;
1424 declarator->u.id.sfk = sfk;
1426 return declarator;
1429 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1430 of modifiers such as const or volatile to apply to the pointer
1431 type, represented as identifiers. ATTRIBUTES represent the attributes that
1432 appertain to the pointer or reference. */
1434 cp_declarator *
1435 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1436 tree attributes)
1438 cp_declarator *declarator;
1440 declarator = make_declarator (cdk_pointer);
1441 declarator->declarator = target;
1442 declarator->u.pointer.qualifiers = cv_qualifiers;
1443 declarator->u.pointer.class_type = NULL_TREE;
1444 if (target)
1446 declarator->id_loc = target->id_loc;
1447 declarator->parameter_pack_p = target->parameter_pack_p;
1448 target->parameter_pack_p = false;
1450 else
1451 declarator->parameter_pack_p = false;
1453 declarator->std_attributes = attributes;
1455 return declarator;
1458 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1459 represent the attributes that appertain to the pointer or
1460 reference. */
1462 cp_declarator *
1463 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1464 bool rvalue_ref, tree attributes)
1466 cp_declarator *declarator;
1468 declarator = make_declarator (cdk_reference);
1469 declarator->declarator = target;
1470 declarator->u.reference.qualifiers = cv_qualifiers;
1471 declarator->u.reference.rvalue_ref = rvalue_ref;
1472 if (target)
1474 declarator->id_loc = target->id_loc;
1475 declarator->parameter_pack_p = target->parameter_pack_p;
1476 target->parameter_pack_p = false;
1478 else
1479 declarator->parameter_pack_p = false;
1481 declarator->std_attributes = attributes;
1483 return declarator;
1486 /* Like make_pointer_declarator -- but for a pointer to a non-static
1487 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1490 cp_declarator *
1491 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1492 cp_declarator *pointee,
1493 tree attributes)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_ptrmem);
1498 declarator->declarator = pointee;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = class_type;
1502 if (pointee)
1504 declarator->parameter_pack_p = pointee->parameter_pack_p;
1505 pointee->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 declarator->std_attributes = attributes;
1512 return declarator;
1515 /* Make a declarator for the function given by TARGET, with the
1516 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1517 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1518 indicates what exceptions can be thrown. */
1520 cp_declarator *
1521 make_call_declarator (cp_declarator *target,
1522 tree parms,
1523 cp_cv_quals cv_qualifiers,
1524 cp_virt_specifiers virt_specifiers,
1525 cp_ref_qualifier ref_qualifier,
1526 tree exception_specification,
1527 tree late_return_type)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_function);
1532 declarator->declarator = target;
1533 declarator->u.function.parameters = parms;
1534 declarator->u.function.qualifiers = cv_qualifiers;
1535 declarator->u.function.virt_specifiers = virt_specifiers;
1536 declarator->u.function.ref_qualifier = ref_qualifier;
1537 declarator->u.function.exception_specification = exception_specification;
1538 declarator->u.function.late_return_type = late_return_type;
1539 if (target)
1541 declarator->id_loc = target->id_loc;
1542 declarator->parameter_pack_p = target->parameter_pack_p;
1543 target->parameter_pack_p = false;
1545 else
1546 declarator->parameter_pack_p = false;
1548 return declarator;
1551 /* Make a declarator for an array of BOUNDS elements, each of which is
1552 defined by ELEMENT. */
1554 cp_declarator *
1555 make_array_declarator (cp_declarator *element, tree bounds)
1557 cp_declarator *declarator;
1559 declarator = make_declarator (cdk_array);
1560 declarator->declarator = element;
1561 declarator->u.array.bounds = bounds;
1562 if (element)
1564 declarator->id_loc = element->id_loc;
1565 declarator->parameter_pack_p = element->parameter_pack_p;
1566 element->parameter_pack_p = false;
1568 else
1569 declarator->parameter_pack_p = false;
1571 return declarator;
1574 /* Determine whether the declarator we've seen so far can be a
1575 parameter pack, when followed by an ellipsis. */
1576 static bool
1577 declarator_can_be_parameter_pack (cp_declarator *declarator)
1579 /* Search for a declarator name, or any other declarator that goes
1580 after the point where the ellipsis could appear in a parameter
1581 pack. If we find any of these, then this declarator can not be
1582 made into a parameter pack. */
1583 bool found = false;
1584 while (declarator && !found)
1586 switch ((int)declarator->kind)
1588 case cdk_id:
1589 case cdk_array:
1590 found = true;
1591 break;
1593 case cdk_error:
1594 return true;
1596 default:
1597 declarator = declarator->declarator;
1598 break;
1602 return !found;
1605 cp_parameter_declarator *no_parameters;
1607 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1608 DECLARATOR and DEFAULT_ARGUMENT. */
1610 cp_parameter_declarator *
1611 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1612 cp_declarator *declarator,
1613 tree default_argument)
1615 cp_parameter_declarator *parameter;
1617 parameter = ((cp_parameter_declarator *)
1618 alloc_declarator (sizeof (cp_parameter_declarator)));
1619 parameter->next = NULL;
1620 if (decl_specifiers)
1621 parameter->decl_specifiers = *decl_specifiers;
1622 else
1623 clear_decl_specs (&parameter->decl_specifiers);
1624 parameter->declarator = declarator;
1625 parameter->default_argument = default_argument;
1626 parameter->ellipsis_p = false;
1628 return parameter;
1631 /* Returns true iff DECLARATOR is a declaration for a function. */
1633 static bool
1634 function_declarator_p (const cp_declarator *declarator)
1636 while (declarator)
1638 if (declarator->kind == cdk_function
1639 && declarator->declarator->kind == cdk_id)
1640 return true;
1641 if (declarator->kind == cdk_id
1642 || declarator->kind == cdk_error)
1643 return false;
1644 declarator = declarator->declarator;
1646 return false;
1649 /* The parser. */
1651 /* Overview
1652 --------
1654 A cp_parser parses the token stream as specified by the C++
1655 grammar. Its job is purely parsing, not semantic analysis. For
1656 example, the parser breaks the token stream into declarators,
1657 expressions, statements, and other similar syntactic constructs.
1658 It does not check that the types of the expressions on either side
1659 of an assignment-statement are compatible, or that a function is
1660 not declared with a parameter of type `void'.
1662 The parser invokes routines elsewhere in the compiler to perform
1663 semantic analysis and to build up the abstract syntax tree for the
1664 code processed.
1666 The parser (and the template instantiation code, which is, in a
1667 way, a close relative of parsing) are the only parts of the
1668 compiler that should be calling push_scope and pop_scope, or
1669 related functions. The parser (and template instantiation code)
1670 keeps track of what scope is presently active; everything else
1671 should simply honor that. (The code that generates static
1672 initializers may also need to set the scope, in order to check
1673 access control correctly when emitting the initializers.)
1675 Methodology
1676 -----------
1678 The parser is of the standard recursive-descent variety. Upcoming
1679 tokens in the token stream are examined in order to determine which
1680 production to use when parsing a non-terminal. Some C++ constructs
1681 require arbitrary look ahead to disambiguate. For example, it is
1682 impossible, in the general case, to tell whether a statement is an
1683 expression or declaration without scanning the entire statement.
1684 Therefore, the parser is capable of "parsing tentatively." When the
1685 parser is not sure what construct comes next, it enters this mode.
1686 Then, while we attempt to parse the construct, the parser queues up
1687 error messages, rather than issuing them immediately, and saves the
1688 tokens it consumes. If the construct is parsed successfully, the
1689 parser "commits", i.e., it issues any queued error messages and
1690 the tokens that were being preserved are permanently discarded.
1691 If, however, the construct is not parsed successfully, the parser
1692 rolls back its state completely so that it can resume parsing using
1693 a different alternative.
1695 Future Improvements
1696 -------------------
1698 The performance of the parser could probably be improved substantially.
1699 We could often eliminate the need to parse tentatively by looking ahead
1700 a little bit. In some places, this approach might not entirely eliminate
1701 the need to parse tentatively, but it might still speed up the average
1702 case. */
1704 /* Flags that are passed to some parsing functions. These values can
1705 be bitwise-ored together. */
1707 enum
1709 /* No flags. */
1710 CP_PARSER_FLAGS_NONE = 0x0,
1711 /* The construct is optional. If it is not present, then no error
1712 should be issued. */
1713 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1714 /* When parsing a type-specifier, treat user-defined type-names
1715 as non-type identifiers. */
1716 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1717 /* When parsing a type-specifier, do not try to parse a class-specifier
1718 or enum-specifier. */
1719 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1720 /* When parsing a decl-specifier-seq, only allow type-specifier or
1721 constexpr. */
1722 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1725 /* This type is used for parameters and variables which hold
1726 combinations of the above flags. */
1727 typedef int cp_parser_flags;
1729 /* The different kinds of declarators we want to parse. */
1731 typedef enum cp_parser_declarator_kind
1733 /* We want an abstract declarator. */
1734 CP_PARSER_DECLARATOR_ABSTRACT,
1735 /* We want a named declarator. */
1736 CP_PARSER_DECLARATOR_NAMED,
1737 /* We don't mind, but the name must be an unqualified-id. */
1738 CP_PARSER_DECLARATOR_EITHER
1739 } cp_parser_declarator_kind;
1741 /* The precedence values used to parse binary expressions. The minimum value
1742 of PREC must be 1, because zero is reserved to quickly discriminate
1743 binary operators from other tokens. */
1745 enum cp_parser_prec
1747 PREC_NOT_OPERATOR,
1748 PREC_LOGICAL_OR_EXPRESSION,
1749 PREC_LOGICAL_AND_EXPRESSION,
1750 PREC_INCLUSIVE_OR_EXPRESSION,
1751 PREC_EXCLUSIVE_OR_EXPRESSION,
1752 PREC_AND_EXPRESSION,
1753 PREC_EQUALITY_EXPRESSION,
1754 PREC_RELATIONAL_EXPRESSION,
1755 PREC_SHIFT_EXPRESSION,
1756 PREC_ADDITIVE_EXPRESSION,
1757 PREC_MULTIPLICATIVE_EXPRESSION,
1758 PREC_PM_EXPRESSION,
1759 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1762 /* A mapping from a token type to a corresponding tree node type, with a
1763 precedence value. */
1765 typedef struct cp_parser_binary_operations_map_node
1767 /* The token type. */
1768 enum cpp_ttype token_type;
1769 /* The corresponding tree code. */
1770 enum tree_code tree_type;
1771 /* The precedence of this operator. */
1772 enum cp_parser_prec prec;
1773 } cp_parser_binary_operations_map_node;
1775 typedef struct cp_parser_expression_stack_entry
1777 /* Left hand side of the binary operation we are currently
1778 parsing. */
1779 tree lhs;
1780 /* Original tree code for left hand side, if it was a binary
1781 expression itself (used for -Wparentheses). */
1782 enum tree_code lhs_type;
1783 /* Tree code for the binary operation we are parsing. */
1784 enum tree_code tree_type;
1785 /* Precedence of the binary operation we are parsing. */
1786 enum cp_parser_prec prec;
1787 /* Location of the binary operation we are parsing. */
1788 location_t loc;
1789 } cp_parser_expression_stack_entry;
1791 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1792 entries because precedence levels on the stack are monotonically
1793 increasing. */
1794 typedef struct cp_parser_expression_stack_entry
1795 cp_parser_expression_stack[NUM_PREC_VALUES];
1797 /* Prototypes. */
1799 /* Constructors and destructors. */
1801 static cp_parser_context *cp_parser_context_new
1802 (cp_parser_context *);
1804 /* Class variables. */
1806 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1808 /* The operator-precedence table used by cp_parser_binary_expression.
1809 Transformed into an associative array (binops_by_token) by
1810 cp_parser_new. */
1812 static const cp_parser_binary_operations_map_node binops[] = {
1813 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1814 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1816 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1817 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1818 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1820 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1821 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1823 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1824 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1826 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1827 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1828 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1829 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1831 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1832 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1834 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1836 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1838 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1840 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1842 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1845 /* The same as binops, but initialized by cp_parser_new so that
1846 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1847 for speed. */
1848 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1850 /* Constructors and destructors. */
1852 /* Construct a new context. The context below this one on the stack
1853 is given by NEXT. */
1855 static cp_parser_context *
1856 cp_parser_context_new (cp_parser_context* next)
1858 cp_parser_context *context;
1860 /* Allocate the storage. */
1861 if (cp_parser_context_free_list != NULL)
1863 /* Pull the first entry from the free list. */
1864 context = cp_parser_context_free_list;
1865 cp_parser_context_free_list = context->next;
1866 memset (context, 0, sizeof (*context));
1868 else
1869 context = ggc_cleared_alloc<cp_parser_context> ();
1871 /* No errors have occurred yet in this context. */
1872 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1873 /* If this is not the bottommost context, copy information that we
1874 need from the previous context. */
1875 if (next)
1877 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1878 expression, then we are parsing one in this context, too. */
1879 context->object_type = next->object_type;
1880 /* Thread the stack. */
1881 context->next = next;
1884 return context;
1887 /* Managing the unparsed function queues. */
1889 #define unparsed_funs_with_default_args \
1890 parser->unparsed_queues->last ().funs_with_default_args
1891 #define unparsed_funs_with_definitions \
1892 parser->unparsed_queues->last ().funs_with_definitions
1893 #define unparsed_nsdmis \
1894 parser->unparsed_queues->last ().nsdmis
1895 #define unparsed_classes \
1896 parser->unparsed_queues->last ().classes
1898 static void
1899 push_unparsed_function_queues (cp_parser *parser)
1901 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1902 vec_safe_push (parser->unparsed_queues, e);
1905 static void
1906 pop_unparsed_function_queues (cp_parser *parser)
1908 release_tree_vector (unparsed_funs_with_definitions);
1909 parser->unparsed_queues->pop ();
1912 /* Prototypes. */
1914 /* Constructors and destructors. */
1916 static cp_parser *cp_parser_new
1917 (void);
1919 /* Routines to parse various constructs.
1921 Those that return `tree' will return the error_mark_node (rather
1922 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1923 Sometimes, they will return an ordinary node if error-recovery was
1924 attempted, even though a parse error occurred. So, to check
1925 whether or not a parse error occurred, you should always use
1926 cp_parser_error_occurred. If the construct is optional (indicated
1927 either by an `_opt' in the name of the function that does the
1928 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1929 the construct is not present. */
1931 /* Lexical conventions [gram.lex] */
1933 static tree cp_parser_identifier
1934 (cp_parser *);
1935 static tree cp_parser_string_literal
1936 (cp_parser *, bool, bool, bool);
1937 static tree cp_parser_userdef_char_literal
1938 (cp_parser *);
1939 static tree cp_parser_userdef_string_literal
1940 (tree);
1941 static tree cp_parser_userdef_numeric_literal
1942 (cp_parser *);
1944 /* Basic concepts [gram.basic] */
1946 static bool cp_parser_translation_unit
1947 (cp_parser *);
1949 /* Expressions [gram.expr] */
1951 static tree cp_parser_primary_expression
1952 (cp_parser *, bool, bool, bool, cp_id_kind *);
1953 static tree cp_parser_id_expression
1954 (cp_parser *, bool, bool, bool *, bool, bool);
1955 static tree cp_parser_unqualified_id
1956 (cp_parser *, bool, bool, bool, bool);
1957 static tree cp_parser_nested_name_specifier_opt
1958 (cp_parser *, bool, bool, bool, bool);
1959 static tree cp_parser_nested_name_specifier
1960 (cp_parser *, bool, bool, bool, bool);
1961 static tree cp_parser_qualifying_entity
1962 (cp_parser *, bool, bool, bool, bool, bool);
1963 static tree cp_parser_postfix_expression
1964 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_postfix_open_square_expression
1966 (cp_parser *, tree, bool, bool);
1967 static tree cp_parser_postfix_dot_deref_expression
1968 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1969 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1970 (cp_parser *, int, bool, bool, bool *, bool = false);
1971 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1972 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1973 static void cp_parser_pseudo_destructor_name
1974 (cp_parser *, tree, tree *, tree *);
1975 static tree cp_parser_unary_expression
1976 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1977 static enum tree_code cp_parser_unary_operator
1978 (cp_token *);
1979 static tree cp_parser_new_expression
1980 (cp_parser *);
1981 static vec<tree, va_gc> *cp_parser_new_placement
1982 (cp_parser *);
1983 static tree cp_parser_new_type_id
1984 (cp_parser *, tree *);
1985 static cp_declarator *cp_parser_new_declarator_opt
1986 (cp_parser *);
1987 static cp_declarator *cp_parser_direct_new_declarator
1988 (cp_parser *);
1989 static vec<tree, va_gc> *cp_parser_new_initializer
1990 (cp_parser *);
1991 static tree cp_parser_delete_expression
1992 (cp_parser *);
1993 static tree cp_parser_cast_expression
1994 (cp_parser *, bool, bool, bool, cp_id_kind *);
1995 static tree cp_parser_binary_expression
1996 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1997 static tree cp_parser_question_colon_clause
1998 (cp_parser *, tree);
1999 static tree cp_parser_assignment_expression
2000 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2001 static enum tree_code cp_parser_assignment_operator_opt
2002 (cp_parser *);
2003 static tree cp_parser_expression
2004 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2005 static tree cp_parser_constant_expression
2006 (cp_parser *, bool = false, bool * = NULL);
2007 static tree cp_parser_builtin_offsetof
2008 (cp_parser *);
2009 static tree cp_parser_lambda_expression
2010 (cp_parser *);
2011 static void cp_parser_lambda_introducer
2012 (cp_parser *, tree);
2013 static bool cp_parser_lambda_declarator_opt
2014 (cp_parser *, tree);
2015 static void cp_parser_lambda_body
2016 (cp_parser *, tree);
2018 /* Statements [gram.stmt.stmt] */
2020 static void cp_parser_statement
2021 (cp_parser *, tree, bool, bool *);
2022 static void cp_parser_label_for_labeled_statement
2023 (cp_parser *, tree);
2024 static tree cp_parser_expression_statement
2025 (cp_parser *, tree);
2026 static tree cp_parser_compound_statement
2027 (cp_parser *, tree, bool, bool);
2028 static void cp_parser_statement_seq_opt
2029 (cp_parser *, tree);
2030 static tree cp_parser_selection_statement
2031 (cp_parser *, bool *);
2032 static tree cp_parser_condition
2033 (cp_parser *);
2034 static tree cp_parser_iteration_statement
2035 (cp_parser *, bool);
2036 static bool cp_parser_for_init_statement
2037 (cp_parser *, tree *decl);
2038 static tree cp_parser_for
2039 (cp_parser *, bool);
2040 static tree cp_parser_c_for
2041 (cp_parser *, tree, tree, bool);
2042 static tree cp_parser_range_for
2043 (cp_parser *, tree, tree, tree, bool);
2044 static void do_range_for_auto_deduction
2045 (tree, tree);
2046 static tree cp_parser_perform_range_for_lookup
2047 (tree, tree *, tree *);
2048 static tree cp_parser_range_for_member_function
2049 (tree, tree);
2050 static tree cp_parser_jump_statement
2051 (cp_parser *);
2052 static void cp_parser_declaration_statement
2053 (cp_parser *);
2055 static tree cp_parser_implicitly_scoped_statement
2056 (cp_parser *, bool *, location_t, const char *);
2057 static void cp_parser_already_scoped_statement
2058 (cp_parser *, location_t, const char *);
2060 /* Declarations [gram.dcl.dcl] */
2062 static void cp_parser_declaration_seq_opt
2063 (cp_parser *);
2064 static void cp_parser_declaration
2065 (cp_parser *);
2066 static void cp_parser_block_declaration
2067 (cp_parser *, bool);
2068 static void cp_parser_simple_declaration
2069 (cp_parser *, bool, tree *);
2070 static void cp_parser_decl_specifier_seq
2071 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2072 static tree cp_parser_storage_class_specifier_opt
2073 (cp_parser *);
2074 static tree cp_parser_function_specifier_opt
2075 (cp_parser *, cp_decl_specifier_seq *);
2076 static tree cp_parser_type_specifier
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2078 int *, bool *);
2079 static tree cp_parser_simple_type_specifier
2080 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2081 static tree cp_parser_type_name
2082 (cp_parser *);
2083 static tree cp_parser_nonclass_name
2084 (cp_parser* parser);
2085 static tree cp_parser_elaborated_type_specifier
2086 (cp_parser *, bool, bool);
2087 static tree cp_parser_enum_specifier
2088 (cp_parser *);
2089 static void cp_parser_enumerator_list
2090 (cp_parser *, tree);
2091 static void cp_parser_enumerator_definition
2092 (cp_parser *, tree);
2093 static tree cp_parser_namespace_name
2094 (cp_parser *);
2095 static void cp_parser_namespace_definition
2096 (cp_parser *);
2097 static void cp_parser_namespace_body
2098 (cp_parser *);
2099 static tree cp_parser_qualified_namespace_specifier
2100 (cp_parser *);
2101 static void cp_parser_namespace_alias_definition
2102 (cp_parser *);
2103 static bool cp_parser_using_declaration
2104 (cp_parser *, bool);
2105 static void cp_parser_using_directive
2106 (cp_parser *);
2107 static tree cp_parser_alias_declaration
2108 (cp_parser *);
2109 static void cp_parser_asm_definition
2110 (cp_parser *);
2111 static void cp_parser_linkage_specification
2112 (cp_parser *);
2113 static void cp_parser_static_assert
2114 (cp_parser *, bool);
2115 static tree cp_parser_decltype
2116 (cp_parser *);
2118 /* Declarators [gram.dcl.decl] */
2120 static tree cp_parser_init_declarator
2121 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2122 bool, bool, int, bool *, tree *, location_t *);
2123 static cp_declarator *cp_parser_declarator
2124 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2125 static cp_declarator *cp_parser_direct_declarator
2126 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2127 static enum tree_code cp_parser_ptr_operator
2128 (cp_parser *, tree *, cp_cv_quals *, tree *);
2129 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2130 (cp_parser *);
2131 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2132 (cp_parser *);
2133 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2134 (cp_parser *);
2135 static tree cp_parser_late_return_type_opt
2136 (cp_parser *, cp_declarator *, cp_cv_quals);
2137 static tree cp_parser_declarator_id
2138 (cp_parser *, bool);
2139 static tree cp_parser_type_id
2140 (cp_parser *);
2141 static tree cp_parser_template_type_arg
2142 (cp_parser *);
2143 static tree cp_parser_trailing_type_id (cp_parser *);
2144 static tree cp_parser_type_id_1
2145 (cp_parser *, bool, bool);
2146 static void cp_parser_type_specifier_seq
2147 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2148 static tree cp_parser_parameter_declaration_clause
2149 (cp_parser *);
2150 static tree cp_parser_parameter_declaration_list
2151 (cp_parser *, bool *);
2152 static cp_parameter_declarator *cp_parser_parameter_declaration
2153 (cp_parser *, bool, bool *);
2154 static tree cp_parser_default_argument
2155 (cp_parser *, bool);
2156 static void cp_parser_function_body
2157 (cp_parser *, bool);
2158 static tree cp_parser_initializer
2159 (cp_parser *, bool *, bool *);
2160 static tree cp_parser_initializer_clause
2161 (cp_parser *, bool *);
2162 static tree cp_parser_braced_list
2163 (cp_parser*, bool*);
2164 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2165 (cp_parser *, bool *);
2167 static bool cp_parser_ctor_initializer_opt_and_function_body
2168 (cp_parser *, bool);
2170 static tree cp_parser_late_parsing_omp_declare_simd
2171 (cp_parser *, tree);
2173 static tree cp_parser_late_parsing_cilk_simd_fn_info
2174 (cp_parser *, tree);
2176 static tree synthesize_implicit_template_parm
2177 (cp_parser *);
2178 static tree finish_fully_implicit_template
2179 (cp_parser *, tree);
2181 /* Classes [gram.class] */
2183 static tree cp_parser_class_name
2184 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2185 static tree cp_parser_class_specifier
2186 (cp_parser *);
2187 static tree cp_parser_class_head
2188 (cp_parser *, bool *);
2189 static enum tag_types cp_parser_class_key
2190 (cp_parser *);
2191 static void cp_parser_type_parameter_key
2192 (cp_parser* parser);
2193 static void cp_parser_member_specification_opt
2194 (cp_parser *);
2195 static void cp_parser_member_declaration
2196 (cp_parser *);
2197 static tree cp_parser_pure_specifier
2198 (cp_parser *);
2199 static tree cp_parser_constant_initializer
2200 (cp_parser *);
2202 /* Derived classes [gram.class.derived] */
2204 static tree cp_parser_base_clause
2205 (cp_parser *);
2206 static tree cp_parser_base_specifier
2207 (cp_parser *);
2209 /* Special member functions [gram.special] */
2211 static tree cp_parser_conversion_function_id
2212 (cp_parser *);
2213 static tree cp_parser_conversion_type_id
2214 (cp_parser *);
2215 static cp_declarator *cp_parser_conversion_declarator_opt
2216 (cp_parser *);
2217 static bool cp_parser_ctor_initializer_opt
2218 (cp_parser *);
2219 static void cp_parser_mem_initializer_list
2220 (cp_parser *);
2221 static tree cp_parser_mem_initializer
2222 (cp_parser *);
2223 static tree cp_parser_mem_initializer_id
2224 (cp_parser *);
2226 /* Overloading [gram.over] */
2228 static tree cp_parser_operator_function_id
2229 (cp_parser *);
2230 static tree cp_parser_operator
2231 (cp_parser *);
2233 /* Templates [gram.temp] */
2235 static void cp_parser_template_declaration
2236 (cp_parser *, bool);
2237 static tree cp_parser_template_parameter_list
2238 (cp_parser *);
2239 static tree cp_parser_template_parameter
2240 (cp_parser *, bool *, bool *);
2241 static tree cp_parser_type_parameter
2242 (cp_parser *, bool *);
2243 static tree cp_parser_template_id
2244 (cp_parser *, bool, bool, enum tag_types, bool);
2245 static tree cp_parser_template_name
2246 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2247 static tree cp_parser_template_argument_list
2248 (cp_parser *);
2249 static tree cp_parser_template_argument
2250 (cp_parser *);
2251 static void cp_parser_explicit_instantiation
2252 (cp_parser *);
2253 static void cp_parser_explicit_specialization
2254 (cp_parser *);
2256 /* Exception handling [gram.exception] */
2258 static tree cp_parser_try_block
2259 (cp_parser *);
2260 static bool cp_parser_function_try_block
2261 (cp_parser *);
2262 static void cp_parser_handler_seq
2263 (cp_parser *);
2264 static void cp_parser_handler
2265 (cp_parser *);
2266 static tree cp_parser_exception_declaration
2267 (cp_parser *);
2268 static tree cp_parser_throw_expression
2269 (cp_parser *);
2270 static tree cp_parser_exception_specification_opt
2271 (cp_parser *);
2272 static tree cp_parser_type_id_list
2273 (cp_parser *);
2275 /* GNU Extensions */
2277 static tree cp_parser_asm_specification_opt
2278 (cp_parser *);
2279 static tree cp_parser_asm_operand_list
2280 (cp_parser *);
2281 static tree cp_parser_asm_clobber_list
2282 (cp_parser *);
2283 static tree cp_parser_asm_label_list
2284 (cp_parser *);
2285 static bool cp_next_tokens_can_be_attribute_p
2286 (cp_parser *);
2287 static bool cp_next_tokens_can_be_gnu_attribute_p
2288 (cp_parser *);
2289 static bool cp_next_tokens_can_be_std_attribute_p
2290 (cp_parser *);
2291 static bool cp_nth_tokens_can_be_std_attribute_p
2292 (cp_parser *, size_t);
2293 static bool cp_nth_tokens_can_be_gnu_attribute_p
2294 (cp_parser *, size_t);
2295 static bool cp_nth_tokens_can_be_attribute_p
2296 (cp_parser *, size_t);
2297 static tree cp_parser_attributes_opt
2298 (cp_parser *);
2299 static tree cp_parser_gnu_attributes_opt
2300 (cp_parser *);
2301 static tree cp_parser_gnu_attribute_list
2302 (cp_parser *);
2303 static tree cp_parser_std_attribute
2304 (cp_parser *);
2305 static tree cp_parser_std_attribute_spec
2306 (cp_parser *);
2307 static tree cp_parser_std_attribute_spec_seq
2308 (cp_parser *);
2309 static bool cp_parser_extension_opt
2310 (cp_parser *, int *);
2311 static void cp_parser_label_declaration
2312 (cp_parser *);
2314 /* Transactional Memory Extensions */
2316 static tree cp_parser_transaction
2317 (cp_parser *, enum rid);
2318 static tree cp_parser_transaction_expression
2319 (cp_parser *, enum rid);
2320 static bool cp_parser_function_transaction
2321 (cp_parser *, enum rid);
2322 static tree cp_parser_transaction_cancel
2323 (cp_parser *);
2325 enum pragma_context {
2326 pragma_external,
2327 pragma_member,
2328 pragma_objc_icode,
2329 pragma_stmt,
2330 pragma_compound
2332 static bool cp_parser_pragma
2333 (cp_parser *, enum pragma_context);
2335 /* Objective-C++ Productions */
2337 static tree cp_parser_objc_message_receiver
2338 (cp_parser *);
2339 static tree cp_parser_objc_message_args
2340 (cp_parser *);
2341 static tree cp_parser_objc_message_expression
2342 (cp_parser *);
2343 static tree cp_parser_objc_encode_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_defs_expression
2346 (cp_parser *);
2347 static tree cp_parser_objc_protocol_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_selector_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_expression
2352 (cp_parser *);
2353 static bool cp_parser_objc_selector_p
2354 (enum cpp_ttype);
2355 static tree cp_parser_objc_selector
2356 (cp_parser *);
2357 static tree cp_parser_objc_protocol_refs_opt
2358 (cp_parser *);
2359 static void cp_parser_objc_declaration
2360 (cp_parser *, tree);
2361 static tree cp_parser_objc_statement
2362 (cp_parser *);
2363 static bool cp_parser_objc_valid_prefix_attributes
2364 (cp_parser *, tree *);
2365 static void cp_parser_objc_at_property_declaration
2366 (cp_parser *) ;
2367 static void cp_parser_objc_at_synthesize_declaration
2368 (cp_parser *) ;
2369 static void cp_parser_objc_at_dynamic_declaration
2370 (cp_parser *) ;
2371 static tree cp_parser_objc_struct_declaration
2372 (cp_parser *) ;
2374 /* Utility Routines */
2376 static tree cp_parser_lookup_name
2377 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2378 static tree cp_parser_lookup_name_simple
2379 (cp_parser *, tree, location_t);
2380 static tree cp_parser_maybe_treat_template_as_class
2381 (tree, bool);
2382 static bool cp_parser_check_declarator_template_parameters
2383 (cp_parser *, cp_declarator *, location_t);
2384 static bool cp_parser_check_template_parameters
2385 (cp_parser *, unsigned, location_t, cp_declarator *);
2386 static tree cp_parser_simple_cast_expression
2387 (cp_parser *);
2388 static tree cp_parser_global_scope_opt
2389 (cp_parser *, bool);
2390 static bool cp_parser_constructor_declarator_p
2391 (cp_parser *, bool);
2392 static tree cp_parser_function_definition_from_specifiers_and_declarator
2393 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2394 static tree cp_parser_function_definition_after_declarator
2395 (cp_parser *, bool);
2396 static void cp_parser_template_declaration_after_export
2397 (cp_parser *, bool);
2398 static void cp_parser_perform_template_parameter_access_checks
2399 (vec<deferred_access_check, va_gc> *);
2400 static tree cp_parser_single_declaration
2401 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2402 static tree cp_parser_functional_cast
2403 (cp_parser *, tree);
2404 static tree cp_parser_save_member_function_body
2405 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2406 static tree cp_parser_save_nsdmi
2407 (cp_parser *);
2408 static tree cp_parser_enclosed_template_argument_list
2409 (cp_parser *);
2410 static void cp_parser_save_default_args
2411 (cp_parser *, tree);
2412 static void cp_parser_late_parsing_for_member
2413 (cp_parser *, tree);
2414 static tree cp_parser_late_parse_one_default_arg
2415 (cp_parser *, tree, tree, tree);
2416 static void cp_parser_late_parsing_nsdmi
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_default_args
2419 (cp_parser *, tree);
2420 static tree cp_parser_sizeof_operand
2421 (cp_parser *, enum rid);
2422 static tree cp_parser_trait_expr
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_declares_only_class_p
2425 (cp_parser *);
2426 static void cp_parser_set_storage_class
2427 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2428 static void cp_parser_set_decl_spec_type
2429 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2430 static void set_and_check_decl_spec_loc
2431 (cp_decl_specifier_seq *decl_specs,
2432 cp_decl_spec ds, cp_token *);
2433 static bool cp_parser_friend_p
2434 (const cp_decl_specifier_seq *);
2435 static void cp_parser_required_error
2436 (cp_parser *, required_token, bool);
2437 static cp_token *cp_parser_require
2438 (cp_parser *, enum cpp_ttype, required_token);
2439 static cp_token *cp_parser_require_keyword
2440 (cp_parser *, enum rid, required_token);
2441 static bool cp_parser_token_starts_function_definition_p
2442 (cp_token *);
2443 static bool cp_parser_next_token_starts_class_definition_p
2444 (cp_parser *);
2445 static bool cp_parser_next_token_ends_template_argument_p
2446 (cp_parser *);
2447 static bool cp_parser_nth_token_starts_template_argument_list_p
2448 (cp_parser *, size_t);
2449 static enum tag_types cp_parser_token_is_class_key
2450 (cp_token *);
2451 static enum tag_types cp_parser_token_is_type_parameter_key
2452 (cp_token *);
2453 static void cp_parser_check_class_key
2454 (enum tag_types, tree type);
2455 static void cp_parser_check_access_in_redeclaration
2456 (tree type, location_t location);
2457 static bool cp_parser_optional_template_keyword
2458 (cp_parser *);
2459 static void cp_parser_pre_parsed_nested_name_specifier
2460 (cp_parser *);
2461 static bool cp_parser_cache_group
2462 (cp_parser *, enum cpp_ttype, unsigned);
2463 static tree cp_parser_cache_defarg
2464 (cp_parser *parser, bool nsdmi);
2465 static void cp_parser_parse_tentatively
2466 (cp_parser *);
2467 static void cp_parser_commit_to_tentative_parse
2468 (cp_parser *);
2469 static void cp_parser_commit_to_topmost_tentative_parse
2470 (cp_parser *);
2471 static void cp_parser_abort_tentative_parse
2472 (cp_parser *);
2473 static bool cp_parser_parse_definitely
2474 (cp_parser *);
2475 static inline bool cp_parser_parsing_tentatively
2476 (cp_parser *);
2477 static bool cp_parser_uncommitted_to_tentative_parse_p
2478 (cp_parser *);
2479 static void cp_parser_error
2480 (cp_parser *, const char *);
2481 static void cp_parser_name_lookup_error
2482 (cp_parser *, tree, tree, name_lookup_error, location_t);
2483 static bool cp_parser_simulate_error
2484 (cp_parser *);
2485 static bool cp_parser_check_type_definition
2486 (cp_parser *);
2487 static void cp_parser_check_for_definition_in_return_type
2488 (cp_declarator *, tree, location_t type_location);
2489 static void cp_parser_check_for_invalid_template_id
2490 (cp_parser *, tree, enum tag_types, location_t location);
2491 static bool cp_parser_non_integral_constant_expression
2492 (cp_parser *, non_integral_constant);
2493 static void cp_parser_diagnose_invalid_type_name
2494 (cp_parser *, tree, location_t);
2495 static bool cp_parser_parse_and_diagnose_invalid_type_name
2496 (cp_parser *);
2497 static int cp_parser_skip_to_closing_parenthesis
2498 (cp_parser *, bool, bool, bool);
2499 static void cp_parser_skip_to_end_of_statement
2500 (cp_parser *);
2501 static void cp_parser_consume_semicolon_at_end_of_statement
2502 (cp_parser *);
2503 static void cp_parser_skip_to_end_of_block_or_statement
2504 (cp_parser *);
2505 static bool cp_parser_skip_to_closing_brace
2506 (cp_parser *);
2507 static void cp_parser_skip_to_end_of_template_parameter_list
2508 (cp_parser *);
2509 static void cp_parser_skip_to_pragma_eol
2510 (cp_parser*, cp_token *);
2511 static bool cp_parser_error_occurred
2512 (cp_parser *);
2513 static bool cp_parser_allow_gnu_extensions_p
2514 (cp_parser *);
2515 static bool cp_parser_is_pure_string_literal
2516 (cp_token *);
2517 static bool cp_parser_is_string_literal
2518 (cp_token *);
2519 static bool cp_parser_is_keyword
2520 (cp_token *, enum rid);
2521 static tree cp_parser_make_typename_type
2522 (cp_parser *, tree, location_t location);
2523 static cp_declarator * cp_parser_make_indirect_declarator
2524 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2525 static bool cp_parser_compound_literal_p
2526 (cp_parser *);
2527 static bool cp_parser_array_designator_p
2528 (cp_parser *);
2529 static bool cp_parser_skip_to_closing_square_bracket
2530 (cp_parser *);
2532 /* Returns nonzero if we are parsing tentatively. */
2534 static inline bool
2535 cp_parser_parsing_tentatively (cp_parser* parser)
2537 return parser->context->next != NULL;
2540 /* Returns nonzero if TOKEN is a string literal. */
2542 static bool
2543 cp_parser_is_pure_string_literal (cp_token* token)
2545 return (token->type == CPP_STRING ||
2546 token->type == CPP_STRING16 ||
2547 token->type == CPP_STRING32 ||
2548 token->type == CPP_WSTRING ||
2549 token->type == CPP_UTF8STRING);
2552 /* Returns nonzero if TOKEN is a string literal
2553 of a user-defined string literal. */
2555 static bool
2556 cp_parser_is_string_literal (cp_token* token)
2558 return (cp_parser_is_pure_string_literal (token) ||
2559 token->type == CPP_STRING_USERDEF ||
2560 token->type == CPP_STRING16_USERDEF ||
2561 token->type == CPP_STRING32_USERDEF ||
2562 token->type == CPP_WSTRING_USERDEF ||
2563 token->type == CPP_UTF8STRING_USERDEF);
2566 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2568 static bool
2569 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2571 return token->keyword == keyword;
2574 /* If not parsing tentatively, issue a diagnostic of the form
2575 FILE:LINE: MESSAGE before TOKEN
2576 where TOKEN is the next token in the input stream. MESSAGE
2577 (specified by the caller) is usually of the form "expected
2578 OTHER-TOKEN". */
2580 static void
2581 cp_parser_error (cp_parser* parser, const char* gmsgid)
2583 if (!cp_parser_simulate_error (parser))
2585 cp_token *token = cp_lexer_peek_token (parser->lexer);
2586 /* This diagnostic makes more sense if it is tagged to the line
2587 of the token we just peeked at. */
2588 cp_lexer_set_source_position_from_token (token);
2590 if (token->type == CPP_PRAGMA)
2592 error_at (token->location,
2593 "%<#pragma%> is not allowed here");
2594 cp_parser_skip_to_pragma_eol (parser, token);
2595 return;
2598 c_parse_error (gmsgid,
2599 /* Because c_parser_error does not understand
2600 CPP_KEYWORD, keywords are treated like
2601 identifiers. */
2602 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2603 token->u.value, token->flags);
2607 /* Issue an error about name-lookup failing. NAME is the
2608 IDENTIFIER_NODE DECL is the result of
2609 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2610 the thing that we hoped to find. */
2612 static void
2613 cp_parser_name_lookup_error (cp_parser* parser,
2614 tree name,
2615 tree decl,
2616 name_lookup_error desired,
2617 location_t location)
2619 /* If name lookup completely failed, tell the user that NAME was not
2620 declared. */
2621 if (decl == error_mark_node)
2623 if (parser->scope && parser->scope != global_namespace)
2624 error_at (location, "%<%E::%E%> has not been declared",
2625 parser->scope, name);
2626 else if (parser->scope == global_namespace)
2627 error_at (location, "%<::%E%> has not been declared", name);
2628 else if (parser->object_scope
2629 && !CLASS_TYPE_P (parser->object_scope))
2630 error_at (location, "request for member %qE in non-class type %qT",
2631 name, parser->object_scope);
2632 else if (parser->object_scope)
2633 error_at (location, "%<%T::%E%> has not been declared",
2634 parser->object_scope, name);
2635 else
2636 error_at (location, "%qE has not been declared", name);
2638 else if (parser->scope && parser->scope != global_namespace)
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%<%E::%E%> is not a type",
2644 parser->scope, name);
2645 break;
2646 case NLE_CXX98:
2647 error_at (location, "%<%E::%E%> is not a class or namespace",
2648 parser->scope, name);
2649 break;
2650 case NLE_NOT_CXX98:
2651 error_at (location,
2652 "%<%E::%E%> is not a class, namespace, or enumeration",
2653 parser->scope, name);
2654 break;
2655 default:
2656 gcc_unreachable ();
2660 else if (parser->scope == global_namespace)
2662 switch (desired)
2664 case NLE_TYPE:
2665 error_at (location, "%<::%E%> is not a type", name);
2666 break;
2667 case NLE_CXX98:
2668 error_at (location, "%<::%E%> is not a class or namespace", name);
2669 break;
2670 case NLE_NOT_CXX98:
2671 error_at (location,
2672 "%<::%E%> is not a class, namespace, or enumeration",
2673 name);
2674 break;
2675 default:
2676 gcc_unreachable ();
2679 else
2681 switch (desired)
2683 case NLE_TYPE:
2684 error_at (location, "%qE is not a type", name);
2685 break;
2686 case NLE_CXX98:
2687 error_at (location, "%qE is not a class or namespace", name);
2688 break;
2689 case NLE_NOT_CXX98:
2690 error_at (location,
2691 "%qE is not a class, namespace, or enumeration", name);
2692 break;
2693 default:
2694 gcc_unreachable ();
2699 /* If we are parsing tentatively, remember that an error has occurred
2700 during this tentative parse. Returns true if the error was
2701 simulated; false if a message should be issued by the caller. */
2703 static bool
2704 cp_parser_simulate_error (cp_parser* parser)
2706 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2708 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2709 return true;
2711 return false;
2714 /* This function is called when a type is defined. If type
2715 definitions are forbidden at this point, an error message is
2716 issued. */
2718 static bool
2719 cp_parser_check_type_definition (cp_parser* parser)
2721 /* If types are forbidden here, issue a message. */
2722 if (parser->type_definition_forbidden_message)
2724 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2725 in the message need to be interpreted. */
2726 error (parser->type_definition_forbidden_message);
2727 return false;
2729 return true;
2732 /* This function is called when the DECLARATOR is processed. The TYPE
2733 was a type defined in the decl-specifiers. If it is invalid to
2734 define a type in the decl-specifiers for DECLARATOR, an error is
2735 issued. TYPE_LOCATION is the location of TYPE and is used
2736 for error reporting. */
2738 static void
2739 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2740 tree type, location_t type_location)
2742 /* [dcl.fct] forbids type definitions in return types.
2743 Unfortunately, it's not easy to know whether or not we are
2744 processing a return type until after the fact. */
2745 while (declarator
2746 && (declarator->kind == cdk_pointer
2747 || declarator->kind == cdk_reference
2748 || declarator->kind == cdk_ptrmem))
2749 declarator = declarator->declarator;
2750 if (declarator
2751 && declarator->kind == cdk_function)
2753 error_at (type_location,
2754 "new types may not be defined in a return type");
2755 inform (type_location,
2756 "(perhaps a semicolon is missing after the definition of %qT)",
2757 type);
2761 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2762 "<" in any valid C++ program. If the next token is indeed "<",
2763 issue a message warning the user about what appears to be an
2764 invalid attempt to form a template-id. LOCATION is the location
2765 of the type-specifier (TYPE) */
2767 static void
2768 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2769 tree type,
2770 enum tag_types tag_type,
2771 location_t location)
2773 cp_token_position start = 0;
2775 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2777 if (TYPE_P (type))
2778 error_at (location, "%qT is not a template", type);
2779 else if (identifier_p (type))
2781 if (tag_type != none_type)
2782 error_at (location, "%qE is not a class template", type);
2783 else
2784 error_at (location, "%qE is not a template", type);
2786 else
2787 error_at (location, "invalid template-id");
2788 /* Remember the location of the invalid "<". */
2789 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2790 start = cp_lexer_token_position (parser->lexer, true);
2791 /* Consume the "<". */
2792 cp_lexer_consume_token (parser->lexer);
2793 /* Parse the template arguments. */
2794 cp_parser_enclosed_template_argument_list (parser);
2795 /* Permanently remove the invalid template arguments so that
2796 this error message is not issued again. */
2797 if (start)
2798 cp_lexer_purge_tokens_after (parser->lexer, start);
2802 /* If parsing an integral constant-expression, issue an error message
2803 about the fact that THING appeared and return true. Otherwise,
2804 return false. In either case, set
2805 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2807 static bool
2808 cp_parser_non_integral_constant_expression (cp_parser *parser,
2809 non_integral_constant thing)
2811 parser->non_integral_constant_expression_p = true;
2812 if (parser->integral_constant_expression_p)
2814 if (!parser->allow_non_integral_constant_expression_p)
2816 const char *msg = NULL;
2817 switch (thing)
2819 case NIC_FLOAT:
2820 error ("floating-point literal "
2821 "cannot appear in a constant-expression");
2822 return true;
2823 case NIC_CAST:
2824 error ("a cast to a type other than an integral or "
2825 "enumeration type cannot appear in a "
2826 "constant-expression");
2827 return true;
2828 case NIC_TYPEID:
2829 error ("%<typeid%> operator "
2830 "cannot appear in a constant-expression");
2831 return true;
2832 case NIC_NCC:
2833 error ("non-constant compound literals "
2834 "cannot appear in a constant-expression");
2835 return true;
2836 case NIC_FUNC_CALL:
2837 error ("a function call "
2838 "cannot appear in a constant-expression");
2839 return true;
2840 case NIC_INC:
2841 error ("an increment "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_DEC:
2845 error ("an decrement "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_ARRAY_REF:
2849 error ("an array reference "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_ADDR_LABEL:
2853 error ("the address of a label "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_OVERLOADED:
2857 error ("calls to overloaded operators "
2858 "cannot appear in a constant-expression");
2859 return true;
2860 case NIC_ASSIGNMENT:
2861 error ("an assignment cannot appear in a constant-expression");
2862 return true;
2863 case NIC_COMMA:
2864 error ("a comma operator "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_CONSTRUCTOR:
2868 error ("a call to a constructor "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_TRANSACTION:
2872 error ("a transaction expression "
2873 "cannot appear in a constant-expression");
2874 return true;
2875 case NIC_THIS:
2876 msg = "this";
2877 break;
2878 case NIC_FUNC_NAME:
2879 msg = "__FUNCTION__";
2880 break;
2881 case NIC_PRETTY_FUNC:
2882 msg = "__PRETTY_FUNCTION__";
2883 break;
2884 case NIC_C99_FUNC:
2885 msg = "__func__";
2886 break;
2887 case NIC_VA_ARG:
2888 msg = "va_arg";
2889 break;
2890 case NIC_ARROW:
2891 msg = "->";
2892 break;
2893 case NIC_POINT:
2894 msg = ".";
2895 break;
2896 case NIC_STAR:
2897 msg = "*";
2898 break;
2899 case NIC_ADDR:
2900 msg = "&";
2901 break;
2902 case NIC_PREINCREMENT:
2903 msg = "++";
2904 break;
2905 case NIC_PREDECREMENT:
2906 msg = "--";
2907 break;
2908 case NIC_NEW:
2909 msg = "new";
2910 break;
2911 case NIC_DEL:
2912 msg = "delete";
2913 break;
2914 default:
2915 gcc_unreachable ();
2917 if (msg)
2918 error ("%qs cannot appear in a constant-expression", msg);
2919 return true;
2922 return false;
2925 /* Emit a diagnostic for an invalid type name. This function commits
2926 to the current active tentative parse, if any. (Otherwise, the
2927 problematic construct might be encountered again later, resulting
2928 in duplicate error messages.) LOCATION is the location of ID. */
2930 static void
2931 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2932 location_t location)
2934 tree decl, ambiguous_decls;
2935 cp_parser_commit_to_tentative_parse (parser);
2936 /* Try to lookup the identifier. */
2937 decl = cp_parser_lookup_name (parser, id, none_type,
2938 /*is_template=*/false,
2939 /*is_namespace=*/false,
2940 /*check_dependency=*/true,
2941 &ambiguous_decls, location);
2942 if (ambiguous_decls)
2943 /* If the lookup was ambiguous, an error will already have
2944 been issued. */
2945 return;
2946 /* If the lookup found a template-name, it means that the user forgot
2947 to specify an argument list. Emit a useful error message. */
2948 if (DECL_TYPE_TEMPLATE_P (decl))
2950 error_at (location,
2951 "invalid use of template-name %qE without an argument list",
2952 decl);
2953 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2955 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2956 error_at (location, "invalid use of destructor %qD as a type", id);
2957 else if (TREE_CODE (decl) == TYPE_DECL)
2958 /* Something like 'unsigned A a;' */
2959 error_at (location, "invalid combination of multiple type-specifiers");
2960 else if (!parser->scope)
2962 /* Issue an error message. */
2963 error_at (location, "%qE does not name a type", id);
2964 /* If we're in a template class, it's possible that the user was
2965 referring to a type from a base class. For example:
2967 template <typename T> struct A { typedef T X; };
2968 template <typename T> struct B : public A<T> { X x; };
2970 The user should have said "typename A<T>::X". */
2971 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2972 inform (location, "C++11 %<constexpr%> only available with "
2973 "-std=c++11 or -std=gnu++11");
2974 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2975 inform (location, "C++11 %<noexcept%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11
2978 && TREE_CODE (id) == IDENTIFIER_NODE
2979 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2980 inform (location, "C++11 %<thread_local%> only available with "
2981 "-std=c++11 or -std=gnu++11");
2982 else if (processing_template_decl && current_class_type
2983 && TYPE_BINFO (current_class_type))
2985 tree b;
2987 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2989 b = TREE_CHAIN (b))
2991 tree base_type = BINFO_TYPE (b);
2992 if (CLASS_TYPE_P (base_type)
2993 && dependent_type_p (base_type))
2995 tree field;
2996 /* Go from a particular instantiation of the
2997 template (which will have an empty TYPE_FIELDs),
2998 to the main version. */
2999 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3000 for (field = TYPE_FIELDS (base_type);
3001 field;
3002 field = DECL_CHAIN (field))
3003 if (TREE_CODE (field) == TYPE_DECL
3004 && DECL_NAME (field) == id)
3006 inform (location,
3007 "(perhaps %<typename %T::%E%> was intended)",
3008 BINFO_TYPE (b), id);
3009 break;
3011 if (field)
3012 break;
3017 /* Here we diagnose qualified-ids where the scope is actually correct,
3018 but the identifier does not resolve to a valid type name. */
3019 else if (parser->scope != error_mark_node)
3021 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3023 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3024 error_at (location_of (id),
3025 "%qE in namespace %qE does not name a template type",
3026 id, parser->scope);
3027 else
3028 error_at (location_of (id),
3029 "%qE in namespace %qE does not name a type",
3030 id, parser->scope);
3031 if (DECL_P (decl))
3032 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3034 else if (CLASS_TYPE_P (parser->scope)
3035 && constructor_name_p (id, parser->scope))
3037 /* A<T>::A<T>() */
3038 error_at (location, "%<%T::%E%> names the constructor, not"
3039 " the type", parser->scope, id);
3040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3041 error_at (location, "and %qT has no template constructors",
3042 parser->scope);
3044 else if (TYPE_P (parser->scope)
3045 && dependent_scope_p (parser->scope))
3046 error_at (location, "need %<typename%> before %<%T::%E%> because "
3047 "%qT is a dependent scope",
3048 parser->scope, id, parser->scope);
3049 else if (TYPE_P (parser->scope))
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3052 error_at (location_of (id),
3053 "%qE in %q#T does not name a template type",
3054 id, parser->scope);
3055 else
3056 error_at (location_of (id),
3057 "%qE in %q#T does not name a type",
3058 id, parser->scope);
3059 if (DECL_P (decl))
3060 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3062 else
3063 gcc_unreachable ();
3067 /* Check for a common situation where a type-name should be present,
3068 but is not, and issue a sensible error message. Returns true if an
3069 invalid type-name was detected.
3071 The situation handled by this function are variable declarations of the
3072 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3073 Usually, `ID' should name a type, but if we got here it means that it
3074 does not. We try to emit the best possible error message depending on
3075 how exactly the id-expression looks like. */
3077 static bool
3078 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3080 tree id;
3081 cp_token *token = cp_lexer_peek_token (parser->lexer);
3083 /* Avoid duplicate error about ambiguous lookup. */
3084 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3086 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3087 if (next->type == CPP_NAME && next->error_reported)
3088 goto out;
3091 cp_parser_parse_tentatively (parser);
3092 id = cp_parser_id_expression (parser,
3093 /*template_keyword_p=*/false,
3094 /*check_dependency_p=*/true,
3095 /*template_p=*/NULL,
3096 /*declarator_p=*/true,
3097 /*optional_p=*/false);
3098 /* If the next token is a (, this is a function with no explicit return
3099 type, i.e. constructor, destructor or conversion op. */
3100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3101 || TREE_CODE (id) == TYPE_DECL)
3103 cp_parser_abort_tentative_parse (parser);
3104 return false;
3106 if (!cp_parser_parse_definitely (parser))
3107 return false;
3109 /* Emit a diagnostic for the invalid type. */
3110 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3111 out:
3112 /* If we aren't in the middle of a declarator (i.e. in a
3113 parameter-declaration-clause), skip to the end of the declaration;
3114 there's no point in trying to process it. */
3115 if (!parser->in_declarator_p)
3116 cp_parser_skip_to_end_of_block_or_statement (parser);
3117 return true;
3120 /* Consume tokens up to, and including, the next non-nested closing `)'.
3121 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3122 are doing error recovery. Returns -1 if OR_COMMA is true and we
3123 found an unnested comma. */
3125 static int
3126 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3127 bool recovering,
3128 bool or_comma,
3129 bool consume_paren)
3131 unsigned paren_depth = 0;
3132 unsigned brace_depth = 0;
3133 unsigned square_depth = 0;
3135 if (recovering && !or_comma
3136 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3137 return 0;
3139 while (true)
3141 cp_token * token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3145 case CPP_EOF:
3146 case CPP_PRAGMA_EOL:
3147 /* If we've run out of tokens, then there is no closing `)'. */
3148 return 0;
3150 /* This is good for lambda expression capture-lists. */
3151 case CPP_OPEN_SQUARE:
3152 ++square_depth;
3153 break;
3154 case CPP_CLOSE_SQUARE:
3155 if (!square_depth--)
3156 return 0;
3157 break;
3159 case CPP_SEMICOLON:
3160 /* This matches the processing in skip_to_end_of_statement. */
3161 if (!brace_depth)
3162 return 0;
3163 break;
3165 case CPP_OPEN_BRACE:
3166 ++brace_depth;
3167 break;
3168 case CPP_CLOSE_BRACE:
3169 if (!brace_depth--)
3170 return 0;
3171 break;
3173 case CPP_COMMA:
3174 if (recovering && or_comma && !brace_depth && !paren_depth
3175 && !square_depth)
3176 return -1;
3177 break;
3179 case CPP_OPEN_PAREN:
3180 if (!brace_depth)
3181 ++paren_depth;
3182 break;
3184 case CPP_CLOSE_PAREN:
3185 if (!brace_depth && !paren_depth--)
3187 if (consume_paren)
3188 cp_lexer_consume_token (parser->lexer);
3189 return 1;
3191 break;
3193 default:
3194 break;
3197 /* Consume the token. */
3198 cp_lexer_consume_token (parser->lexer);
3202 /* Consume tokens until we reach the end of the current statement.
3203 Normally, that will be just before consuming a `;'. However, if a
3204 non-nested `}' comes first, then we stop before consuming that. */
3206 static void
3207 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3209 unsigned nesting_depth = 0;
3211 /* Unwind generic function template scope if necessary. */
3212 if (parser->fully_implicit_function_template_p)
3213 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3215 while (true)
3217 cp_token *token = cp_lexer_peek_token (parser->lexer);
3219 switch (token->type)
3221 case CPP_EOF:
3222 case CPP_PRAGMA_EOL:
3223 /* If we've run out of tokens, stop. */
3224 return;
3226 case CPP_SEMICOLON:
3227 /* If the next token is a `;', we have reached the end of the
3228 statement. */
3229 if (!nesting_depth)
3230 return;
3231 break;
3233 case CPP_CLOSE_BRACE:
3234 /* If this is a non-nested '}', stop before consuming it.
3235 That way, when confronted with something like:
3237 { 3 + }
3239 we stop before consuming the closing '}', even though we
3240 have not yet reached a `;'. */
3241 if (nesting_depth == 0)
3242 return;
3244 /* If it is the closing '}' for a block that we have
3245 scanned, stop -- but only after consuming the token.
3246 That way given:
3248 void f g () { ... }
3249 typedef int I;
3251 we will stop after the body of the erroneously declared
3252 function, but before consuming the following `typedef'
3253 declaration. */
3254 if (--nesting_depth == 0)
3256 cp_lexer_consume_token (parser->lexer);
3257 return;
3260 case CPP_OPEN_BRACE:
3261 ++nesting_depth;
3262 break;
3264 default:
3265 break;
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser->lexer);
3273 /* This function is called at the end of a statement or declaration.
3274 If the next token is a semicolon, it is consumed; otherwise, error
3275 recovery is attempted. */
3277 static void
3278 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3280 /* Look for the trailing `;'. */
3281 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3283 /* If there is additional (erroneous) input, skip to the end of
3284 the statement. */
3285 cp_parser_skip_to_end_of_statement (parser);
3286 /* If the next token is now a `;', consume it. */
3287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3288 cp_lexer_consume_token (parser->lexer);
3292 /* Skip tokens until we have consumed an entire block, or until we
3293 have consumed a non-nested `;'. */
3295 static void
3296 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3298 int nesting_depth = 0;
3300 /* Unwind generic function template scope if necessary. */
3301 if (parser->fully_implicit_function_template_p)
3302 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3304 while (nesting_depth >= 0)
3306 cp_token *token = cp_lexer_peek_token (parser->lexer);
3308 switch (token->type)
3310 case CPP_EOF:
3311 case CPP_PRAGMA_EOL:
3312 /* If we've run out of tokens, stop. */
3313 return;
3315 case CPP_SEMICOLON:
3316 /* Stop if this is an unnested ';'. */
3317 if (!nesting_depth)
3318 nesting_depth = -1;
3319 break;
3321 case CPP_CLOSE_BRACE:
3322 /* Stop if this is an unnested '}', or closes the outermost
3323 nesting level. */
3324 nesting_depth--;
3325 if (nesting_depth < 0)
3326 return;
3327 if (!nesting_depth)
3328 nesting_depth = -1;
3329 break;
3331 case CPP_OPEN_BRACE:
3332 /* Nest. */
3333 nesting_depth++;
3334 break;
3336 default:
3337 break;
3340 /* Consume the token. */
3341 cp_lexer_consume_token (parser->lexer);
3345 /* Skip tokens until a non-nested closing curly brace is the next
3346 token, or there are no more tokens. Return true in the first case,
3347 false otherwise. */
3349 static bool
3350 cp_parser_skip_to_closing_brace (cp_parser *parser)
3352 unsigned nesting_depth = 0;
3354 while (true)
3356 cp_token *token = cp_lexer_peek_token (parser->lexer);
3358 switch (token->type)
3360 case CPP_EOF:
3361 case CPP_PRAGMA_EOL:
3362 /* If we've run out of tokens, stop. */
3363 return false;
3365 case CPP_CLOSE_BRACE:
3366 /* If the next token is a non-nested `}', then we have reached
3367 the end of the current block. */
3368 if (nesting_depth-- == 0)
3369 return true;
3370 break;
3372 case CPP_OPEN_BRACE:
3373 /* If it the next token is a `{', then we are entering a new
3374 block. Consume the entire block. */
3375 ++nesting_depth;
3376 break;
3378 default:
3379 break;
3382 /* Consume the token. */
3383 cp_lexer_consume_token (parser->lexer);
3387 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3388 parameter is the PRAGMA token, allowing us to purge the entire pragma
3389 sequence. */
3391 static void
3392 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3394 cp_token *token;
3396 parser->lexer->in_pragma = false;
3399 token = cp_lexer_consume_token (parser->lexer);
3400 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3402 /* Ensure that the pragma is not parsed again. */
3403 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3406 /* Require pragma end of line, resyncing with it as necessary. The
3407 arguments are as for cp_parser_skip_to_pragma_eol. */
3409 static void
3410 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3412 parser->lexer->in_pragma = false;
3413 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3417 /* This is a simple wrapper around make_typename_type. When the id is
3418 an unresolved identifier node, we can provide a superior diagnostic
3419 using cp_parser_diagnose_invalid_type_name. */
3421 static tree
3422 cp_parser_make_typename_type (cp_parser *parser, tree id,
3423 location_t id_location)
3425 tree result;
3426 if (identifier_p (id))
3428 result = make_typename_type (parser->scope, id, typename_type,
3429 /*complain=*/tf_none);
3430 if (result == error_mark_node)
3431 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3432 return result;
3434 return make_typename_type (parser->scope, id, typename_type, tf_error);
3437 /* This is a wrapper around the
3438 make_{pointer,ptrmem,reference}_declarator functions that decides
3439 which one to call based on the CODE and CLASS_TYPE arguments. The
3440 CODE argument should be one of the values returned by
3441 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3442 appertain to the pointer or reference. */
3444 static cp_declarator *
3445 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3446 cp_cv_quals cv_qualifiers,
3447 cp_declarator *target,
3448 tree attributes)
3450 if (code == ERROR_MARK)
3451 return cp_error_declarator;
3453 if (code == INDIRECT_REF)
3454 if (class_type == NULL_TREE)
3455 return make_pointer_declarator (cv_qualifiers, target, attributes);
3456 else
3457 return make_ptrmem_declarator (cv_qualifiers, class_type,
3458 target, attributes);
3459 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3460 return make_reference_declarator (cv_qualifiers, target,
3461 false, attributes);
3462 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3463 return make_reference_declarator (cv_qualifiers, target,
3464 true, attributes);
3465 gcc_unreachable ();
3468 /* Create a new C++ parser. */
3470 static cp_parser *
3471 cp_parser_new (void)
3473 cp_parser *parser;
3474 cp_lexer *lexer;
3475 unsigned i;
3477 /* cp_lexer_new_main is called before doing GC allocation because
3478 cp_lexer_new_main might load a PCH file. */
3479 lexer = cp_lexer_new_main ();
3481 /* Initialize the binops_by_token so that we can get the tree
3482 directly from the token. */
3483 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3484 binops_by_token[binops[i].token_type] = binops[i];
3486 parser = ggc_cleared_alloc<cp_parser> ();
3487 parser->lexer = lexer;
3488 parser->context = cp_parser_context_new (NULL);
3490 /* For now, we always accept GNU extensions. */
3491 parser->allow_gnu_extensions_p = 1;
3493 /* The `>' token is a greater-than operator, not the end of a
3494 template-id. */
3495 parser->greater_than_is_operator_p = true;
3497 parser->default_arg_ok_p = true;
3499 /* We are not parsing a constant-expression. */
3500 parser->integral_constant_expression_p = false;
3501 parser->allow_non_integral_constant_expression_p = false;
3502 parser->non_integral_constant_expression_p = false;
3504 /* Local variable names are not forbidden. */
3505 parser->local_variables_forbidden_p = false;
3507 /* We are not processing an `extern "C"' declaration. */
3508 parser->in_unbraced_linkage_specification_p = false;
3510 /* We are not processing a declarator. */
3511 parser->in_declarator_p = false;
3513 /* We are not processing a template-argument-list. */
3514 parser->in_template_argument_list_p = false;
3516 /* We are not in an iteration statement. */
3517 parser->in_statement = 0;
3519 /* We are not in a switch statement. */
3520 parser->in_switch_statement_p = false;
3522 /* We are not parsing a type-id inside an expression. */
3523 parser->in_type_id_in_expr_p = false;
3525 /* Declarations aren't implicitly extern "C". */
3526 parser->implicit_extern_c = false;
3528 /* String literals should be translated to the execution character set. */
3529 parser->translate_strings_p = true;
3531 /* We are not parsing a function body. */
3532 parser->in_function_body = false;
3534 /* We can correct until told otherwise. */
3535 parser->colon_corrects_to_scope_p = true;
3537 /* The unparsed function queue is empty. */
3538 push_unparsed_function_queues (parser);
3540 /* There are no classes being defined. */
3541 parser->num_classes_being_defined = 0;
3543 /* No template parameters apply. */
3544 parser->num_template_parameter_lists = 0;
3546 /* Not declaring an implicit function template. */
3547 parser->auto_is_implicit_function_template_parm_p = false;
3548 parser->fully_implicit_function_template_p = false;
3549 parser->implicit_template_parms = 0;
3550 parser->implicit_template_scope = 0;
3552 return parser;
3555 /* Create a cp_lexer structure which will emit the tokens in CACHE
3556 and push it onto the parser's lexer stack. This is used for delayed
3557 parsing of in-class method bodies and default arguments, and should
3558 not be confused with tentative parsing. */
3559 static void
3560 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3562 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3563 lexer->next = parser->lexer;
3564 parser->lexer = lexer;
3566 /* Move the current source position to that of the first token in the
3567 new lexer. */
3568 cp_lexer_set_source_position_from_token (lexer->next_token);
3571 /* Pop the top lexer off the parser stack. This is never used for the
3572 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3573 static void
3574 cp_parser_pop_lexer (cp_parser *parser)
3576 cp_lexer *lexer = parser->lexer;
3577 parser->lexer = lexer->next;
3578 cp_lexer_destroy (lexer);
3580 /* Put the current source position back where it was before this
3581 lexer was pushed. */
3582 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3585 /* Lexical conventions [gram.lex] */
3587 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3588 identifier. */
3590 static tree
3591 cp_parser_identifier (cp_parser* parser)
3593 cp_token *token;
3595 /* Look for the identifier. */
3596 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3597 /* Return the value. */
3598 return token ? token->u.value : error_mark_node;
3601 /* Parse a sequence of adjacent string constants. Returns a
3602 TREE_STRING representing the combined, nul-terminated string
3603 constant. If TRANSLATE is true, translate the string to the
3604 execution character set. If WIDE_OK is true, a wide string is
3605 invalid here.
3607 C++98 [lex.string] says that if a narrow string literal token is
3608 adjacent to a wide string literal token, the behavior is undefined.
3609 However, C99 6.4.5p4 says that this results in a wide string literal.
3610 We follow C99 here, for consistency with the C front end.
3612 This code is largely lifted from lex_string() in c-lex.c.
3614 FUTURE: ObjC++ will need to handle @-strings here. */
3615 static tree
3616 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3617 bool lookup_udlit = true)
3619 tree value;
3620 size_t count;
3621 struct obstack str_ob;
3622 cpp_string str, istr, *strs;
3623 cp_token *tok;
3624 enum cpp_ttype type, curr_type;
3625 int have_suffix_p = 0;
3626 tree string_tree;
3627 tree suffix_id = NULL_TREE;
3628 bool curr_tok_is_userdef_p = false;
3630 tok = cp_lexer_peek_token (parser->lexer);
3631 if (!cp_parser_is_string_literal (tok))
3633 cp_parser_error (parser, "expected string-literal");
3634 return error_mark_node;
3637 if (cpp_userdef_string_p (tok->type))
3639 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3640 curr_type = cpp_userdef_string_remove_type (tok->type);
3641 curr_tok_is_userdef_p = true;
3643 else
3645 string_tree = tok->u.value;
3646 curr_type = tok->type;
3648 type = curr_type;
3650 /* Try to avoid the overhead of creating and destroying an obstack
3651 for the common case of just one string. */
3652 if (!cp_parser_is_string_literal
3653 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3655 cp_lexer_consume_token (parser->lexer);
3657 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3658 str.len = TREE_STRING_LENGTH (string_tree);
3659 count = 1;
3661 if (curr_tok_is_userdef_p)
3663 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3664 have_suffix_p = 1;
3665 curr_type = cpp_userdef_string_remove_type (tok->type);
3667 else
3668 curr_type = tok->type;
3670 strs = &str;
3672 else
3674 gcc_obstack_init (&str_ob);
3675 count = 0;
3679 cp_lexer_consume_token (parser->lexer);
3680 count++;
3681 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3682 str.len = TREE_STRING_LENGTH (string_tree);
3684 if (curr_tok_is_userdef_p)
3686 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3687 if (have_suffix_p == 0)
3689 suffix_id = curr_suffix_id;
3690 have_suffix_p = 1;
3692 else if (have_suffix_p == 1
3693 && curr_suffix_id != suffix_id)
3695 error ("inconsistent user-defined literal suffixes"
3696 " %qD and %qD in string literal",
3697 suffix_id, curr_suffix_id);
3698 have_suffix_p = -1;
3700 curr_type = cpp_userdef_string_remove_type (tok->type);
3702 else
3703 curr_type = tok->type;
3705 if (type != curr_type)
3707 if (type == CPP_STRING)
3708 type = curr_type;
3709 else if (curr_type != CPP_STRING)
3710 error_at (tok->location,
3711 "unsupported non-standard concatenation "
3712 "of string literals");
3715 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3717 tok = cp_lexer_peek_token (parser->lexer);
3718 if (cpp_userdef_string_p (tok->type))
3720 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3721 curr_type = cpp_userdef_string_remove_type (tok->type);
3722 curr_tok_is_userdef_p = true;
3724 else
3726 string_tree = tok->u.value;
3727 curr_type = tok->type;
3728 curr_tok_is_userdef_p = false;
3731 while (cp_parser_is_string_literal (tok));
3733 strs = (cpp_string *) obstack_finish (&str_ob);
3736 if (type != CPP_STRING && !wide_ok)
3738 cp_parser_error (parser, "a wide string is invalid in this context");
3739 type = CPP_STRING;
3742 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3743 (parse_in, strs, count, &istr, type))
3745 value = build_string (istr.len, (const char *)istr.text);
3746 free (CONST_CAST (unsigned char *, istr.text));
3748 switch (type)
3750 default:
3751 case CPP_STRING:
3752 case CPP_UTF8STRING:
3753 TREE_TYPE (value) = char_array_type_node;
3754 break;
3755 case CPP_STRING16:
3756 TREE_TYPE (value) = char16_array_type_node;
3757 break;
3758 case CPP_STRING32:
3759 TREE_TYPE (value) = char32_array_type_node;
3760 break;
3761 case CPP_WSTRING:
3762 TREE_TYPE (value) = wchar_array_type_node;
3763 break;
3766 value = fix_string_type (value);
3768 if (have_suffix_p)
3770 tree literal = build_userdef_literal (suffix_id, value,
3771 OT_NONE, NULL_TREE);
3772 if (lookup_udlit)
3773 value = cp_parser_userdef_string_literal (literal);
3774 else
3775 value = literal;
3778 else
3779 /* cpp_interpret_string has issued an error. */
3780 value = error_mark_node;
3782 if (count > 1)
3783 obstack_free (&str_ob, 0);
3785 return value;
3788 /* Look up a literal operator with the name and the exact arguments. */
3790 static tree
3791 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3793 tree decl, fns;
3794 decl = lookup_name (name);
3795 if (!decl || !is_overloaded_fn (decl))
3796 return error_mark_node;
3798 for (fns = decl; fns; fns = OVL_NEXT (fns))
3800 unsigned int ix;
3801 bool found = true;
3802 tree fn = OVL_CURRENT (fns);
3803 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3804 if (parmtypes != NULL_TREE)
3806 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3807 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3809 tree tparm = TREE_VALUE (parmtypes);
3810 tree targ = TREE_TYPE ((*args)[ix]);
3811 bool ptr = TYPE_PTR_P (tparm);
3812 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3813 if ((ptr || arr || !same_type_p (tparm, targ))
3814 && (!ptr || !arr
3815 || !same_type_p (TREE_TYPE (tparm),
3816 TREE_TYPE (targ))))
3817 found = false;
3819 if (found
3820 && ix == vec_safe_length (args)
3821 /* May be this should be sufficient_parms_p instead,
3822 depending on how exactly should user-defined literals
3823 work in presence of default arguments on the literal
3824 operator parameters. */
3825 && parmtypes == void_list_node)
3826 return decl;
3830 return error_mark_node;
3833 /* Parse a user-defined char constant. Returns a call to a user-defined
3834 literal operator taking the character as an argument. */
3836 static tree
3837 cp_parser_userdef_char_literal (cp_parser *parser)
3839 cp_token *token = cp_lexer_consume_token (parser->lexer);
3840 tree literal = token->u.value;
3841 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3842 tree value = USERDEF_LITERAL_VALUE (literal);
3843 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3844 tree decl, result;
3846 /* Build up a call to the user-defined operator */
3847 /* Lookup the name we got back from the id-expression. */
3848 vec<tree, va_gc> *args = make_tree_vector ();
3849 vec_safe_push (args, value);
3850 decl = lookup_literal_operator (name, args);
3851 if (!decl || decl == error_mark_node)
3853 error ("unable to find character literal operator %qD with %qT argument",
3854 name, TREE_TYPE (value));
3855 release_tree_vector (args);
3856 return error_mark_node;
3858 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3859 release_tree_vector (args);
3860 return result;
3863 /* A subroutine of cp_parser_userdef_numeric_literal to
3864 create a char... template parameter pack from a string node. */
3866 static tree
3867 make_char_string_pack (tree value)
3869 tree charvec;
3870 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3871 const char *str = TREE_STRING_POINTER (value);
3872 int i, len = TREE_STRING_LENGTH (value) - 1;
3873 tree argvec = make_tree_vec (1);
3875 /* Fill in CHARVEC with all of the parameters. */
3876 charvec = make_tree_vec (len);
3877 for (i = 0; i < len; ++i)
3878 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3880 /* Build the argument packs. */
3881 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3882 TREE_TYPE (argpack) = char_type_node;
3884 TREE_VEC_ELT (argvec, 0) = argpack;
3886 return argvec;
3889 /* A subroutine of cp_parser_userdef_numeric_literal to
3890 create a char... template parameter pack from a string node. */
3892 static tree
3893 make_string_pack (tree value)
3895 tree charvec;
3896 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3897 const unsigned char *str
3898 = (const unsigned char *) TREE_STRING_POINTER (value);
3899 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3900 int len = TREE_STRING_LENGTH (value) / sz - 1;
3901 tree argvec = make_tree_vec (2);
3903 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3904 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3906 /* First template parm is character type. */
3907 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3909 /* Fill in CHARVEC with all of the parameters. */
3910 charvec = make_tree_vec (len);
3911 for (int i = 0; i < len; ++i)
3912 TREE_VEC_ELT (charvec, i)
3913 = double_int_to_tree (str_char_type_node,
3914 double_int::from_buffer (str + i * sz, sz));
3916 /* Build the argument packs. */
3917 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3918 TREE_TYPE (argpack) = str_char_type_node;
3920 TREE_VEC_ELT (argvec, 1) = argpack;
3922 return argvec;
3925 /* Parse a user-defined numeric constant. returns a call to a user-defined
3926 literal operator. */
3928 static tree
3929 cp_parser_userdef_numeric_literal (cp_parser *parser)
3931 cp_token *token = cp_lexer_consume_token (parser->lexer);
3932 tree literal = token->u.value;
3933 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3934 tree value = USERDEF_LITERAL_VALUE (literal);
3935 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3936 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3937 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3938 tree decl, result;
3939 vec<tree, va_gc> *args;
3941 /* Look for a literal operator taking the exact type of numeric argument
3942 as the literal value. */
3943 args = make_tree_vector ();
3944 vec_safe_push (args, value);
3945 decl = lookup_literal_operator (name, args);
3946 if (decl && decl != error_mark_node)
3948 result = finish_call_expr (decl, &args, false, true,
3949 tf_warning_or_error);
3951 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3953 warning_at (token->location, OPT_Woverflow,
3954 "integer literal exceeds range of %qT type",
3955 long_long_unsigned_type_node);
3957 else
3959 if (overflow > 0)
3960 warning_at (token->location, OPT_Woverflow,
3961 "floating literal exceeds range of %qT type",
3962 long_double_type_node);
3963 else if (overflow < 0)
3964 warning_at (token->location, OPT_Woverflow,
3965 "floating literal truncated to zero");
3968 release_tree_vector (args);
3969 return result;
3971 release_tree_vector (args);
3973 /* If the numeric argument didn't work, look for a raw literal
3974 operator taking a const char* argument consisting of the number
3975 in string format. */
3976 args = make_tree_vector ();
3977 vec_safe_push (args, num_string);
3978 decl = lookup_literal_operator (name, args);
3979 if (decl && decl != error_mark_node)
3981 result = finish_call_expr (decl, &args, false, true,
3982 tf_warning_or_error);
3983 release_tree_vector (args);
3984 return result;
3986 release_tree_vector (args);
3988 /* If the raw literal didn't work, look for a non-type template
3989 function with parameter pack char.... Call the function with
3990 template parameter characters representing the number. */
3991 args = make_tree_vector ();
3992 decl = lookup_literal_operator (name, args);
3993 if (decl && decl != error_mark_node)
3995 tree tmpl_args = make_char_string_pack (num_string);
3996 decl = lookup_template_function (decl, tmpl_args);
3997 result = finish_call_expr (decl, &args, false, true,
3998 tf_warning_or_error);
3999 release_tree_vector (args);
4000 return result;
4003 release_tree_vector (args);
4005 error ("unable to find numeric literal operator %qD", name);
4006 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4007 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4008 "to enable more built-in suffixes");
4009 return error_mark_node;
4012 /* Parse a user-defined string constant. Returns a call to a user-defined
4013 literal operator taking a character pointer and the length of the string
4014 as arguments. */
4016 static tree
4017 cp_parser_userdef_string_literal (tree literal)
4019 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4020 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4021 tree value = USERDEF_LITERAL_VALUE (literal);
4022 int len = TREE_STRING_LENGTH (value)
4023 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4024 tree decl, result;
4025 vec<tree, va_gc> *args;
4027 /* Build up a call to the user-defined operator. */
4028 /* Lookup the name we got back from the id-expression. */
4029 args = make_tree_vector ();
4030 vec_safe_push (args, value);
4031 vec_safe_push (args, build_int_cst (size_type_node, len));
4032 decl = lookup_literal_operator (name, args);
4034 if (decl && decl != error_mark_node)
4036 result = finish_call_expr (decl, &args, false, true,
4037 tf_warning_or_error);
4038 release_tree_vector (args);
4039 return result;
4041 release_tree_vector (args);
4043 /* Look for a template function with typename parameter CharT
4044 and parameter pack CharT... Call the function with
4045 template parameter characters representing the string. */
4046 args = make_tree_vector ();
4047 decl = lookup_literal_operator (name, args);
4048 if (decl && decl != error_mark_node)
4050 tree tmpl_args = make_string_pack (value);
4051 decl = lookup_template_function (decl, tmpl_args);
4052 result = finish_call_expr (decl, &args, false, true,
4053 tf_warning_or_error);
4054 release_tree_vector (args);
4055 return result;
4057 release_tree_vector (args);
4059 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4060 name, TREE_TYPE (value), size_type_node);
4061 return error_mark_node;
4065 /* Basic concepts [gram.basic] */
4067 /* Parse a translation-unit.
4069 translation-unit:
4070 declaration-seq [opt]
4072 Returns TRUE if all went well. */
4074 static bool
4075 cp_parser_translation_unit (cp_parser* parser)
4077 /* The address of the first non-permanent object on the declarator
4078 obstack. */
4079 static void *declarator_obstack_base;
4081 bool success;
4083 /* Create the declarator obstack, if necessary. */
4084 if (!cp_error_declarator)
4086 gcc_obstack_init (&declarator_obstack);
4087 /* Create the error declarator. */
4088 cp_error_declarator = make_declarator (cdk_error);
4089 /* Create the empty parameter list. */
4090 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4091 /* Remember where the base of the declarator obstack lies. */
4092 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4095 cp_parser_declaration_seq_opt (parser);
4097 /* If there are no tokens left then all went well. */
4098 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4100 /* Get rid of the token array; we don't need it any more. */
4101 cp_lexer_destroy (parser->lexer);
4102 parser->lexer = NULL;
4104 /* This file might have been a context that's implicitly extern
4105 "C". If so, pop the lang context. (Only relevant for PCH.) */
4106 if (parser->implicit_extern_c)
4108 pop_lang_context ();
4109 parser->implicit_extern_c = false;
4112 /* Finish up. */
4113 finish_translation_unit ();
4115 success = true;
4117 else
4119 cp_parser_error (parser, "expected declaration");
4120 success = false;
4123 /* Make sure the declarator obstack was fully cleaned up. */
4124 gcc_assert (obstack_next_free (&declarator_obstack)
4125 == declarator_obstack_base);
4127 /* All went well. */
4128 return success;
4131 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4132 decltype context. */
4134 static inline tsubst_flags_t
4135 complain_flags (bool decltype_p)
4137 tsubst_flags_t complain = tf_warning_or_error;
4138 if (decltype_p)
4139 complain |= tf_decltype;
4140 return complain;
4143 /* We're about to parse a collection of statements. If we're currently
4144 parsing tentatively, set up a firewall so that any nested
4145 cp_parser_commit_to_tentative_parse won't affect the current context. */
4147 static cp_token_position
4148 cp_parser_start_tentative_firewall (cp_parser *parser)
4150 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4151 return 0;
4153 cp_parser_parse_tentatively (parser);
4154 cp_parser_commit_to_topmost_tentative_parse (parser);
4155 return cp_lexer_token_position (parser->lexer, false);
4158 /* We've finished parsing the collection of statements. Wrap up the
4159 firewall and replace the relevant tokens with the parsed form. */
4161 static void
4162 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4163 tree expr)
4165 if (!start)
4166 return;
4168 /* Finish the firewall level. */
4169 cp_parser_parse_definitely (parser);
4170 /* And remember the result of the parse for when we try again. */
4171 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4172 token->type = CPP_PREPARSED_EXPR;
4173 token->u.value = expr;
4174 token->keyword = RID_MAX;
4175 cp_lexer_purge_tokens_after (parser->lexer, start);
4178 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4179 enclosing parentheses. */
4181 static tree
4182 cp_parser_statement_expr (cp_parser *parser)
4184 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4186 /* Consume the '('. */
4187 cp_lexer_consume_token (parser->lexer);
4188 /* Start the statement-expression. */
4189 tree expr = begin_stmt_expr ();
4190 /* Parse the compound-statement. */
4191 cp_parser_compound_statement (parser, expr, false, false);
4192 /* Finish up. */
4193 expr = finish_stmt_expr (expr, false);
4194 /* Consume the ')'. */
4195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4196 cp_parser_skip_to_end_of_statement (parser);
4198 cp_parser_end_tentative_firewall (parser, start, expr);
4199 return expr;
4202 /* Expressions [gram.expr] */
4204 /* Parse a primary-expression.
4206 primary-expression:
4207 literal
4208 this
4209 ( expression )
4210 id-expression
4211 lambda-expression (C++11)
4213 GNU Extensions:
4215 primary-expression:
4216 ( compound-statement )
4217 __builtin_va_arg ( assignment-expression , type-id )
4218 __builtin_offsetof ( type-id , offsetof-expression )
4220 C++ Extensions:
4221 __has_nothrow_assign ( type-id )
4222 __has_nothrow_constructor ( type-id )
4223 __has_nothrow_copy ( type-id )
4224 __has_trivial_assign ( type-id )
4225 __has_trivial_constructor ( type-id )
4226 __has_trivial_copy ( type-id )
4227 __has_trivial_destructor ( type-id )
4228 __has_virtual_destructor ( type-id )
4229 __is_abstract ( type-id )
4230 __is_base_of ( type-id , type-id )
4231 __is_class ( type-id )
4232 __is_empty ( type-id )
4233 __is_enum ( type-id )
4234 __is_final ( type-id )
4235 __is_literal_type ( type-id )
4236 __is_pod ( type-id )
4237 __is_polymorphic ( type-id )
4238 __is_std_layout ( type-id )
4239 __is_trivial ( type-id )
4240 __is_union ( type-id )
4242 Objective-C++ Extension:
4244 primary-expression:
4245 objc-expression
4247 literal:
4248 __null
4250 ADDRESS_P is true iff this expression was immediately preceded by
4251 "&" and therefore might denote a pointer-to-member. CAST_P is true
4252 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4253 true iff this expression is a template argument.
4255 Returns a representation of the expression. Upon return, *IDK
4256 indicates what kind of id-expression (if any) was present. */
4258 static tree
4259 cp_parser_primary_expression (cp_parser *parser,
4260 bool address_p,
4261 bool cast_p,
4262 bool template_arg_p,
4263 bool decltype_p,
4264 cp_id_kind *idk)
4266 cp_token *token = NULL;
4268 /* Assume the primary expression is not an id-expression. */
4269 *idk = CP_ID_KIND_NONE;
4271 /* Peek at the next token. */
4272 token = cp_lexer_peek_token (parser->lexer);
4273 switch ((int) token->type)
4275 /* literal:
4276 integer-literal
4277 character-literal
4278 floating-literal
4279 string-literal
4280 boolean-literal
4281 pointer-literal
4282 user-defined-literal */
4283 case CPP_CHAR:
4284 case CPP_CHAR16:
4285 case CPP_CHAR32:
4286 case CPP_WCHAR:
4287 case CPP_UTF8CHAR:
4288 case CPP_NUMBER:
4289 case CPP_PREPARSED_EXPR:
4290 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4291 return cp_parser_userdef_numeric_literal (parser);
4292 token = cp_lexer_consume_token (parser->lexer);
4293 if (TREE_CODE (token->u.value) == FIXED_CST)
4295 error_at (token->location,
4296 "fixed-point types not supported in C++");
4297 return error_mark_node;
4299 /* Floating-point literals are only allowed in an integral
4300 constant expression if they are cast to an integral or
4301 enumeration type. */
4302 if (TREE_CODE (token->u.value) == REAL_CST
4303 && parser->integral_constant_expression_p
4304 && pedantic)
4306 /* CAST_P will be set even in invalid code like "int(2.7 +
4307 ...)". Therefore, we have to check that the next token
4308 is sure to end the cast. */
4309 if (cast_p)
4311 cp_token *next_token;
4313 next_token = cp_lexer_peek_token (parser->lexer);
4314 if (/* The comma at the end of an
4315 enumerator-definition. */
4316 next_token->type != CPP_COMMA
4317 /* The curly brace at the end of an enum-specifier. */
4318 && next_token->type != CPP_CLOSE_BRACE
4319 /* The end of a statement. */
4320 && next_token->type != CPP_SEMICOLON
4321 /* The end of the cast-expression. */
4322 && next_token->type != CPP_CLOSE_PAREN
4323 /* The end of an array bound. */
4324 && next_token->type != CPP_CLOSE_SQUARE
4325 /* The closing ">" in a template-argument-list. */
4326 && (next_token->type != CPP_GREATER
4327 || parser->greater_than_is_operator_p)
4328 /* C++0x only: A ">>" treated like two ">" tokens,
4329 in a template-argument-list. */
4330 && (next_token->type != CPP_RSHIFT
4331 || (cxx_dialect == cxx98)
4332 || parser->greater_than_is_operator_p))
4333 cast_p = false;
4336 /* If we are within a cast, then the constraint that the
4337 cast is to an integral or enumeration type will be
4338 checked at that point. If we are not within a cast, then
4339 this code is invalid. */
4340 if (!cast_p)
4341 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4343 return token->u.value;
4345 case CPP_CHAR_USERDEF:
4346 case CPP_CHAR16_USERDEF:
4347 case CPP_CHAR32_USERDEF:
4348 case CPP_WCHAR_USERDEF:
4349 case CPP_UTF8CHAR_USERDEF:
4350 return cp_parser_userdef_char_literal (parser);
4352 case CPP_STRING:
4353 case CPP_STRING16:
4354 case CPP_STRING32:
4355 case CPP_WSTRING:
4356 case CPP_UTF8STRING:
4357 case CPP_STRING_USERDEF:
4358 case CPP_STRING16_USERDEF:
4359 case CPP_STRING32_USERDEF:
4360 case CPP_WSTRING_USERDEF:
4361 case CPP_UTF8STRING_USERDEF:
4362 /* ??? Should wide strings be allowed when parser->translate_strings_p
4363 is false (i.e. in attributes)? If not, we can kill the third
4364 argument to cp_parser_string_literal. */
4365 return cp_parser_string_literal (parser,
4366 parser->translate_strings_p,
4367 true);
4369 case CPP_OPEN_PAREN:
4370 /* If we see `( { ' then we are looking at the beginning of
4371 a GNU statement-expression. */
4372 if (cp_parser_allow_gnu_extensions_p (parser)
4373 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4375 /* Statement-expressions are not allowed by the standard. */
4376 pedwarn (token->location, OPT_Wpedantic,
4377 "ISO C++ forbids braced-groups within expressions");
4379 /* And they're not allowed outside of a function-body; you
4380 cannot, for example, write:
4382 int i = ({ int j = 3; j + 1; });
4384 at class or namespace scope. */
4385 if (!parser->in_function_body
4386 || parser->in_template_argument_list_p)
4388 error_at (token->location,
4389 "statement-expressions are not allowed outside "
4390 "functions nor in template-argument lists");
4391 cp_parser_skip_to_end_of_block_or_statement (parser);
4392 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4393 cp_lexer_consume_token (parser->lexer);
4394 return error_mark_node;
4396 else
4397 return cp_parser_statement_expr (parser);
4399 /* Otherwise it's a normal parenthesized expression. */
4401 tree expr;
4402 bool saved_greater_than_is_operator_p;
4404 /* Consume the `('. */
4405 cp_lexer_consume_token (parser->lexer);
4406 /* Within a parenthesized expression, a `>' token is always
4407 the greater-than operator. */
4408 saved_greater_than_is_operator_p
4409 = parser->greater_than_is_operator_p;
4410 parser->greater_than_is_operator_p = true;
4412 /* Parse the parenthesized expression. */
4413 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4414 /* Let the front end know that this expression was
4415 enclosed in parentheses. This matters in case, for
4416 example, the expression is of the form `A::B', since
4417 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4418 not. */
4419 expr = finish_parenthesized_expr (expr);
4420 /* DR 705: Wrapping an unqualified name in parentheses
4421 suppresses arg-dependent lookup. We want to pass back
4422 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4423 (c++/37862), but none of the others. */
4424 if (*idk != CP_ID_KIND_QUALIFIED)
4425 *idk = CP_ID_KIND_NONE;
4427 /* The `>' token might be the end of a template-id or
4428 template-parameter-list now. */
4429 parser->greater_than_is_operator_p
4430 = saved_greater_than_is_operator_p;
4431 /* Consume the `)'. */
4432 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4433 cp_parser_skip_to_end_of_statement (parser);
4435 return expr;
4438 case CPP_OPEN_SQUARE:
4440 if (c_dialect_objc ())
4442 /* We might have an Objective-C++ message. */
4443 cp_parser_parse_tentatively (parser);
4444 tree msg = cp_parser_objc_message_expression (parser);
4445 /* If that works out, we're done ... */
4446 if (cp_parser_parse_definitely (parser))
4447 return msg;
4448 /* ... else, fall though to see if it's a lambda. */
4450 tree lam = cp_parser_lambda_expression (parser);
4451 /* Don't warn about a failed tentative parse. */
4452 if (cp_parser_error_occurred (parser))
4453 return error_mark_node;
4454 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4455 return lam;
4458 case CPP_OBJC_STRING:
4459 if (c_dialect_objc ())
4460 /* We have an Objective-C++ string literal. */
4461 return cp_parser_objc_expression (parser);
4462 cp_parser_error (parser, "expected primary-expression");
4463 return error_mark_node;
4465 case CPP_KEYWORD:
4466 switch (token->keyword)
4468 /* These two are the boolean literals. */
4469 case RID_TRUE:
4470 cp_lexer_consume_token (parser->lexer);
4471 return boolean_true_node;
4472 case RID_FALSE:
4473 cp_lexer_consume_token (parser->lexer);
4474 return boolean_false_node;
4476 /* The `__null' literal. */
4477 case RID_NULL:
4478 cp_lexer_consume_token (parser->lexer);
4479 return null_node;
4481 /* The `nullptr' literal. */
4482 case RID_NULLPTR:
4483 cp_lexer_consume_token (parser->lexer);
4484 return nullptr_node;
4486 /* Recognize the `this' keyword. */
4487 case RID_THIS:
4488 cp_lexer_consume_token (parser->lexer);
4489 if (parser->local_variables_forbidden_p)
4491 error_at (token->location,
4492 "%<this%> may not be used in this context");
4493 return error_mark_node;
4495 /* Pointers cannot appear in constant-expressions. */
4496 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4497 return error_mark_node;
4498 return finish_this_expr ();
4500 /* The `operator' keyword can be the beginning of an
4501 id-expression. */
4502 case RID_OPERATOR:
4503 goto id_expression;
4505 case RID_FUNCTION_NAME:
4506 case RID_PRETTY_FUNCTION_NAME:
4507 case RID_C99_FUNCTION_NAME:
4509 non_integral_constant name;
4511 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4512 __func__ are the names of variables -- but they are
4513 treated specially. Therefore, they are handled here,
4514 rather than relying on the generic id-expression logic
4515 below. Grammatically, these names are id-expressions.
4517 Consume the token. */
4518 token = cp_lexer_consume_token (parser->lexer);
4520 switch (token->keyword)
4522 case RID_FUNCTION_NAME:
4523 name = NIC_FUNC_NAME;
4524 break;
4525 case RID_PRETTY_FUNCTION_NAME:
4526 name = NIC_PRETTY_FUNC;
4527 break;
4528 case RID_C99_FUNCTION_NAME:
4529 name = NIC_C99_FUNC;
4530 break;
4531 default:
4532 gcc_unreachable ();
4535 if (cp_parser_non_integral_constant_expression (parser, name))
4536 return error_mark_node;
4538 /* Look up the name. */
4539 return finish_fname (token->u.value);
4542 case RID_VA_ARG:
4544 tree expression;
4545 tree type;
4546 source_location type_location;
4548 /* The `__builtin_va_arg' construct is used to handle
4549 `va_arg'. Consume the `__builtin_va_arg' token. */
4550 cp_lexer_consume_token (parser->lexer);
4551 /* Look for the opening `('. */
4552 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4553 /* Now, parse the assignment-expression. */
4554 expression = cp_parser_assignment_expression (parser);
4555 /* Look for the `,'. */
4556 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4557 type_location = cp_lexer_peek_token (parser->lexer)->location;
4558 /* Parse the type-id. */
4559 type = cp_parser_type_id (parser);
4560 /* Look for the closing `)'. */
4561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4562 /* Using `va_arg' in a constant-expression is not
4563 allowed. */
4564 if (cp_parser_non_integral_constant_expression (parser,
4565 NIC_VA_ARG))
4566 return error_mark_node;
4567 return build_x_va_arg (type_location, expression, type);
4570 case RID_OFFSETOF:
4571 return cp_parser_builtin_offsetof (parser);
4573 case RID_HAS_NOTHROW_ASSIGN:
4574 case RID_HAS_NOTHROW_CONSTRUCTOR:
4575 case RID_HAS_NOTHROW_COPY:
4576 case RID_HAS_TRIVIAL_ASSIGN:
4577 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4578 case RID_HAS_TRIVIAL_COPY:
4579 case RID_HAS_TRIVIAL_DESTRUCTOR:
4580 case RID_HAS_VIRTUAL_DESTRUCTOR:
4581 case RID_IS_ABSTRACT:
4582 case RID_IS_BASE_OF:
4583 case RID_IS_CLASS:
4584 case RID_IS_EMPTY:
4585 case RID_IS_ENUM:
4586 case RID_IS_FINAL:
4587 case RID_IS_LITERAL_TYPE:
4588 case RID_IS_POD:
4589 case RID_IS_POLYMORPHIC:
4590 case RID_IS_STD_LAYOUT:
4591 case RID_IS_TRIVIAL:
4592 case RID_IS_TRIVIALLY_ASSIGNABLE:
4593 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4594 case RID_IS_TRIVIALLY_COPYABLE:
4595 case RID_IS_UNION:
4596 return cp_parser_trait_expr (parser, token->keyword);
4598 /* Objective-C++ expressions. */
4599 case RID_AT_ENCODE:
4600 case RID_AT_PROTOCOL:
4601 case RID_AT_SELECTOR:
4602 return cp_parser_objc_expression (parser);
4604 case RID_TEMPLATE:
4605 if (parser->in_function_body
4606 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4607 == CPP_LESS))
4609 error_at (token->location,
4610 "a template declaration cannot appear at block scope");
4611 cp_parser_skip_to_end_of_block_or_statement (parser);
4612 return error_mark_node;
4614 default:
4615 cp_parser_error (parser, "expected primary-expression");
4616 return error_mark_node;
4619 /* An id-expression can start with either an identifier, a
4620 `::' as the beginning of a qualified-id, or the "operator"
4621 keyword. */
4622 case CPP_NAME:
4623 case CPP_SCOPE:
4624 case CPP_TEMPLATE_ID:
4625 case CPP_NESTED_NAME_SPECIFIER:
4627 tree id_expression;
4628 tree decl;
4629 const char *error_msg;
4630 bool template_p;
4631 bool done;
4632 cp_token *id_expr_token;
4634 id_expression:
4635 /* Parse the id-expression. */
4636 id_expression
4637 = cp_parser_id_expression (parser,
4638 /*template_keyword_p=*/false,
4639 /*check_dependency_p=*/true,
4640 &template_p,
4641 /*declarator_p=*/false,
4642 /*optional_p=*/false);
4643 if (id_expression == error_mark_node)
4644 return error_mark_node;
4645 id_expr_token = token;
4646 token = cp_lexer_peek_token (parser->lexer);
4647 done = (token->type != CPP_OPEN_SQUARE
4648 && token->type != CPP_OPEN_PAREN
4649 && token->type != CPP_DOT
4650 && token->type != CPP_DEREF
4651 && token->type != CPP_PLUS_PLUS
4652 && token->type != CPP_MINUS_MINUS);
4653 /* If we have a template-id, then no further lookup is
4654 required. If the template-id was for a template-class, we
4655 will sometimes have a TYPE_DECL at this point. */
4656 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4657 || TREE_CODE (id_expression) == TYPE_DECL)
4658 decl = id_expression;
4659 /* Look up the name. */
4660 else
4662 tree ambiguous_decls;
4664 /* If we already know that this lookup is ambiguous, then
4665 we've already issued an error message; there's no reason
4666 to check again. */
4667 if (id_expr_token->type == CPP_NAME
4668 && id_expr_token->error_reported)
4670 cp_parser_simulate_error (parser);
4671 return error_mark_node;
4674 decl = cp_parser_lookup_name (parser, id_expression,
4675 none_type,
4676 template_p,
4677 /*is_namespace=*/false,
4678 /*check_dependency=*/true,
4679 &ambiguous_decls,
4680 id_expr_token->location);
4681 /* If the lookup was ambiguous, an error will already have
4682 been issued. */
4683 if (ambiguous_decls)
4684 return error_mark_node;
4686 /* In Objective-C++, we may have an Objective-C 2.0
4687 dot-syntax for classes here. */
4688 if (c_dialect_objc ()
4689 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4690 && TREE_CODE (decl) == TYPE_DECL
4691 && objc_is_class_name (decl))
4693 tree component;
4694 cp_lexer_consume_token (parser->lexer);
4695 component = cp_parser_identifier (parser);
4696 if (component == error_mark_node)
4697 return error_mark_node;
4699 return objc_build_class_component_ref (id_expression, component);
4702 /* In Objective-C++, an instance variable (ivar) may be preferred
4703 to whatever cp_parser_lookup_name() found. */
4704 decl = objc_lookup_ivar (decl, id_expression);
4706 /* If name lookup gives us a SCOPE_REF, then the
4707 qualifying scope was dependent. */
4708 if (TREE_CODE (decl) == SCOPE_REF)
4710 /* At this point, we do not know if DECL is a valid
4711 integral constant expression. We assume that it is
4712 in fact such an expression, so that code like:
4714 template <int N> struct A {
4715 int a[B<N>::i];
4718 is accepted. At template-instantiation time, we
4719 will check that B<N>::i is actually a constant. */
4720 return decl;
4722 /* Check to see if DECL is a local variable in a context
4723 where that is forbidden. */
4724 if (parser->local_variables_forbidden_p
4725 && local_variable_p (decl))
4727 /* It might be that we only found DECL because we are
4728 trying to be generous with pre-ISO scoping rules.
4729 For example, consider:
4731 int i;
4732 void g() {
4733 for (int i = 0; i < 10; ++i) {}
4734 extern void f(int j = i);
4737 Here, name look up will originally find the out
4738 of scope `i'. We need to issue a warning message,
4739 but then use the global `i'. */
4740 decl = check_for_out_of_scope_variable (decl);
4741 if (local_variable_p (decl))
4743 error_at (id_expr_token->location,
4744 "local variable %qD may not appear in this context",
4745 decl);
4746 return error_mark_node;
4751 decl = (finish_id_expression
4752 (id_expression, decl, parser->scope,
4753 idk,
4754 parser->integral_constant_expression_p,
4755 parser->allow_non_integral_constant_expression_p,
4756 &parser->non_integral_constant_expression_p,
4757 template_p, done, address_p,
4758 template_arg_p,
4759 &error_msg,
4760 id_expr_token->location));
4761 if (error_msg)
4762 cp_parser_error (parser, error_msg);
4763 return decl;
4766 /* Anything else is an error. */
4767 default:
4768 cp_parser_error (parser, "expected primary-expression");
4769 return error_mark_node;
4773 static inline tree
4774 cp_parser_primary_expression (cp_parser *parser,
4775 bool address_p,
4776 bool cast_p,
4777 bool template_arg_p,
4778 cp_id_kind *idk)
4780 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4781 /*decltype*/false, idk);
4784 /* Parse an id-expression.
4786 id-expression:
4787 unqualified-id
4788 qualified-id
4790 qualified-id:
4791 :: [opt] nested-name-specifier template [opt] unqualified-id
4792 :: identifier
4793 :: operator-function-id
4794 :: template-id
4796 Return a representation of the unqualified portion of the
4797 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4798 a `::' or nested-name-specifier.
4800 Often, if the id-expression was a qualified-id, the caller will
4801 want to make a SCOPE_REF to represent the qualified-id. This
4802 function does not do this in order to avoid wastefully creating
4803 SCOPE_REFs when they are not required.
4805 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4806 `template' keyword.
4808 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4809 uninstantiated templates.
4811 If *TEMPLATE_P is non-NULL, it is set to true iff the
4812 `template' keyword is used to explicitly indicate that the entity
4813 named is a template.
4815 If DECLARATOR_P is true, the id-expression is appearing as part of
4816 a declarator, rather than as part of an expression. */
4818 static tree
4819 cp_parser_id_expression (cp_parser *parser,
4820 bool template_keyword_p,
4821 bool check_dependency_p,
4822 bool *template_p,
4823 bool declarator_p,
4824 bool optional_p)
4826 bool global_scope_p;
4827 bool nested_name_specifier_p;
4829 /* Assume the `template' keyword was not used. */
4830 if (template_p)
4831 *template_p = template_keyword_p;
4833 /* Look for the optional `::' operator. */
4834 global_scope_p
4835 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4836 != NULL_TREE);
4837 /* Look for the optional nested-name-specifier. */
4838 nested_name_specifier_p
4839 = (cp_parser_nested_name_specifier_opt (parser,
4840 /*typename_keyword_p=*/false,
4841 check_dependency_p,
4842 /*type_p=*/false,
4843 declarator_p)
4844 != NULL_TREE);
4845 /* If there is a nested-name-specifier, then we are looking at
4846 the first qualified-id production. */
4847 if (nested_name_specifier_p)
4849 tree saved_scope;
4850 tree saved_object_scope;
4851 tree saved_qualifying_scope;
4852 tree unqualified_id;
4853 bool is_template;
4855 /* See if the next token is the `template' keyword. */
4856 if (!template_p)
4857 template_p = &is_template;
4858 *template_p = cp_parser_optional_template_keyword (parser);
4859 /* Name lookup we do during the processing of the
4860 unqualified-id might obliterate SCOPE. */
4861 saved_scope = parser->scope;
4862 saved_object_scope = parser->object_scope;
4863 saved_qualifying_scope = parser->qualifying_scope;
4864 /* Process the final unqualified-id. */
4865 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4866 check_dependency_p,
4867 declarator_p,
4868 /*optional_p=*/false);
4869 /* Restore the SAVED_SCOPE for our caller. */
4870 parser->scope = saved_scope;
4871 parser->object_scope = saved_object_scope;
4872 parser->qualifying_scope = saved_qualifying_scope;
4874 return unqualified_id;
4876 /* Otherwise, if we are in global scope, then we are looking at one
4877 of the other qualified-id productions. */
4878 else if (global_scope_p)
4880 cp_token *token;
4881 tree id;
4883 /* Peek at the next token. */
4884 token = cp_lexer_peek_token (parser->lexer);
4886 /* If it's an identifier, and the next token is not a "<", then
4887 we can avoid the template-id case. This is an optimization
4888 for this common case. */
4889 if (token->type == CPP_NAME
4890 && !cp_parser_nth_token_starts_template_argument_list_p
4891 (parser, 2))
4892 return cp_parser_identifier (parser);
4894 cp_parser_parse_tentatively (parser);
4895 /* Try a template-id. */
4896 id = cp_parser_template_id (parser,
4897 /*template_keyword_p=*/false,
4898 /*check_dependency_p=*/true,
4899 none_type,
4900 declarator_p);
4901 /* If that worked, we're done. */
4902 if (cp_parser_parse_definitely (parser))
4903 return id;
4905 /* Peek at the next token. (Changes in the token buffer may
4906 have invalidated the pointer obtained above.) */
4907 token = cp_lexer_peek_token (parser->lexer);
4909 switch (token->type)
4911 case CPP_NAME:
4912 return cp_parser_identifier (parser);
4914 case CPP_KEYWORD:
4915 if (token->keyword == RID_OPERATOR)
4916 return cp_parser_operator_function_id (parser);
4917 /* Fall through. */
4919 default:
4920 cp_parser_error (parser, "expected id-expression");
4921 return error_mark_node;
4924 else
4925 return cp_parser_unqualified_id (parser, template_keyword_p,
4926 /*check_dependency_p=*/true,
4927 declarator_p,
4928 optional_p);
4931 /* Parse an unqualified-id.
4933 unqualified-id:
4934 identifier
4935 operator-function-id
4936 conversion-function-id
4937 ~ class-name
4938 template-id
4940 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4941 keyword, in a construct like `A::template ...'.
4943 Returns a representation of unqualified-id. For the `identifier'
4944 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4945 production a BIT_NOT_EXPR is returned; the operand of the
4946 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4947 other productions, see the documentation accompanying the
4948 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4949 names are looked up in uninstantiated templates. If DECLARATOR_P
4950 is true, the unqualified-id is appearing as part of a declarator,
4951 rather than as part of an expression. */
4953 static tree
4954 cp_parser_unqualified_id (cp_parser* parser,
4955 bool template_keyword_p,
4956 bool check_dependency_p,
4957 bool declarator_p,
4958 bool optional_p)
4960 cp_token *token;
4962 /* Peek at the next token. */
4963 token = cp_lexer_peek_token (parser->lexer);
4965 switch ((int) token->type)
4967 case CPP_NAME:
4969 tree id;
4971 /* We don't know yet whether or not this will be a
4972 template-id. */
4973 cp_parser_parse_tentatively (parser);
4974 /* Try a template-id. */
4975 id = cp_parser_template_id (parser, template_keyword_p,
4976 check_dependency_p,
4977 none_type,
4978 declarator_p);
4979 /* If it worked, we're done. */
4980 if (cp_parser_parse_definitely (parser))
4981 return id;
4982 /* Otherwise, it's an ordinary identifier. */
4983 return cp_parser_identifier (parser);
4986 case CPP_TEMPLATE_ID:
4987 return cp_parser_template_id (parser, template_keyword_p,
4988 check_dependency_p,
4989 none_type,
4990 declarator_p);
4992 case CPP_COMPL:
4994 tree type_decl;
4995 tree qualifying_scope;
4996 tree object_scope;
4997 tree scope;
4998 bool done;
5000 /* Consume the `~' token. */
5001 cp_lexer_consume_token (parser->lexer);
5002 /* Parse the class-name. The standard, as written, seems to
5003 say that:
5005 template <typename T> struct S { ~S (); };
5006 template <typename T> S<T>::~S() {}
5008 is invalid, since `~' must be followed by a class-name, but
5009 `S<T>' is dependent, and so not known to be a class.
5010 That's not right; we need to look in uninstantiated
5011 templates. A further complication arises from:
5013 template <typename T> void f(T t) {
5014 t.T::~T();
5017 Here, it is not possible to look up `T' in the scope of `T'
5018 itself. We must look in both the current scope, and the
5019 scope of the containing complete expression.
5021 Yet another issue is:
5023 struct S {
5024 int S;
5025 ~S();
5028 S::~S() {}
5030 The standard does not seem to say that the `S' in `~S'
5031 should refer to the type `S' and not the data member
5032 `S::S'. */
5034 /* DR 244 says that we look up the name after the "~" in the
5035 same scope as we looked up the qualifying name. That idea
5036 isn't fully worked out; it's more complicated than that. */
5037 scope = parser->scope;
5038 object_scope = parser->object_scope;
5039 qualifying_scope = parser->qualifying_scope;
5041 /* Check for invalid scopes. */
5042 if (scope == error_mark_node)
5044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5045 cp_lexer_consume_token (parser->lexer);
5046 return error_mark_node;
5048 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5051 error_at (token->location,
5052 "scope %qT before %<~%> is not a class-name",
5053 scope);
5054 cp_parser_simulate_error (parser);
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5056 cp_lexer_consume_token (parser->lexer);
5057 return error_mark_node;
5059 gcc_assert (!scope || TYPE_P (scope));
5061 /* If the name is of the form "X::~X" it's OK even if X is a
5062 typedef. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 if (scope
5065 && token->type == CPP_NAME
5066 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5067 != CPP_LESS)
5068 && (token->u.value == TYPE_IDENTIFIER (scope)
5069 || (CLASS_TYPE_P (scope)
5070 && constructor_name_p (token->u.value, scope))))
5072 cp_lexer_consume_token (parser->lexer);
5073 return build_nt (BIT_NOT_EXPR, scope);
5076 /* ~auto means the destructor of whatever the object is. */
5077 if (cp_parser_is_keyword (token, RID_AUTO))
5079 if (cxx_dialect < cxx14)
5080 pedwarn (input_location, 0,
5081 "%<~auto%> only available with "
5082 "-std=c++14 or -std=gnu++14");
5083 cp_lexer_consume_token (parser->lexer);
5084 return build_nt (BIT_NOT_EXPR, make_auto ());
5087 /* If there was an explicit qualification (S::~T), first look
5088 in the scope given by the qualification (i.e., S).
5090 Note: in the calls to cp_parser_class_name below we pass
5091 typename_type so that lookup finds the injected-class-name
5092 rather than the constructor. */
5093 done = false;
5094 type_decl = NULL_TREE;
5095 if (scope)
5097 cp_parser_parse_tentatively (parser);
5098 type_decl = cp_parser_class_name (parser,
5099 /*typename_keyword_p=*/false,
5100 /*template_keyword_p=*/false,
5101 typename_type,
5102 /*check_dependency=*/false,
5103 /*class_head_p=*/false,
5104 declarator_p);
5105 if (cp_parser_parse_definitely (parser))
5106 done = true;
5108 /* In "N::S::~S", look in "N" as well. */
5109 if (!done && scope && qualifying_scope)
5111 cp_parser_parse_tentatively (parser);
5112 parser->scope = qualifying_scope;
5113 parser->object_scope = NULL_TREE;
5114 parser->qualifying_scope = NULL_TREE;
5115 type_decl
5116 = cp_parser_class_name (parser,
5117 /*typename_keyword_p=*/false,
5118 /*template_keyword_p=*/false,
5119 typename_type,
5120 /*check_dependency=*/false,
5121 /*class_head_p=*/false,
5122 declarator_p);
5123 if (cp_parser_parse_definitely (parser))
5124 done = true;
5126 /* In "p->S::~T", look in the scope given by "*p" as well. */
5127 else if (!done && object_scope)
5129 cp_parser_parse_tentatively (parser);
5130 parser->scope = object_scope;
5131 parser->object_scope = NULL_TREE;
5132 parser->qualifying_scope = NULL_TREE;
5133 type_decl
5134 = cp_parser_class_name (parser,
5135 /*typename_keyword_p=*/false,
5136 /*template_keyword_p=*/false,
5137 typename_type,
5138 /*check_dependency=*/false,
5139 /*class_head_p=*/false,
5140 declarator_p);
5141 if (cp_parser_parse_definitely (parser))
5142 done = true;
5144 /* Look in the surrounding context. */
5145 if (!done)
5147 parser->scope = NULL_TREE;
5148 parser->object_scope = NULL_TREE;
5149 parser->qualifying_scope = NULL_TREE;
5150 if (processing_template_decl)
5151 cp_parser_parse_tentatively (parser);
5152 type_decl
5153 = cp_parser_class_name (parser,
5154 /*typename_keyword_p=*/false,
5155 /*template_keyword_p=*/false,
5156 typename_type,
5157 /*check_dependency=*/false,
5158 /*class_head_p=*/false,
5159 declarator_p);
5160 if (processing_template_decl
5161 && ! cp_parser_parse_definitely (parser))
5163 /* We couldn't find a type with this name, so just accept
5164 it and check for a match at instantiation time. */
5165 type_decl = cp_parser_identifier (parser);
5166 if (type_decl != error_mark_node)
5167 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5168 return type_decl;
5171 /* If an error occurred, assume that the name of the
5172 destructor is the same as the name of the qualifying
5173 class. That allows us to keep parsing after running
5174 into ill-formed destructor names. */
5175 if (type_decl == error_mark_node && scope)
5176 return build_nt (BIT_NOT_EXPR, scope);
5177 else if (type_decl == error_mark_node)
5178 return error_mark_node;
5180 /* Check that destructor name and scope match. */
5181 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5184 error_at (token->location,
5185 "declaration of %<~%T%> as member of %qT",
5186 type_decl, scope);
5187 cp_parser_simulate_error (parser);
5188 return error_mark_node;
5191 /* [class.dtor]
5193 A typedef-name that names a class shall not be used as the
5194 identifier in the declarator for a destructor declaration. */
5195 if (declarator_p
5196 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5197 && !DECL_SELF_REFERENCE_P (type_decl)
5198 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5199 error_at (token->location,
5200 "typedef-name %qD used as destructor declarator",
5201 type_decl);
5203 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5206 case CPP_KEYWORD:
5207 if (token->keyword == RID_OPERATOR)
5209 tree id;
5211 /* This could be a template-id, so we try that first. */
5212 cp_parser_parse_tentatively (parser);
5213 /* Try a template-id. */
5214 id = cp_parser_template_id (parser, template_keyword_p,
5215 /*check_dependency_p=*/true,
5216 none_type,
5217 declarator_p);
5218 /* If that worked, we're done. */
5219 if (cp_parser_parse_definitely (parser))
5220 return id;
5221 /* We still don't know whether we're looking at an
5222 operator-function-id or a conversion-function-id. */
5223 cp_parser_parse_tentatively (parser);
5224 /* Try an operator-function-id. */
5225 id = cp_parser_operator_function_id (parser);
5226 /* If that didn't work, try a conversion-function-id. */
5227 if (!cp_parser_parse_definitely (parser))
5228 id = cp_parser_conversion_function_id (parser);
5229 else if (UDLIT_OPER_P (id))
5231 /* 17.6.3.3.5 */
5232 const char *name = UDLIT_OP_SUFFIX (id);
5233 if (name[0] != '_' && !in_system_header_at (input_location)
5234 && declarator_p)
5235 warning (0, "literal operator suffixes not preceded by %<_%>"
5236 " are reserved for future standardization");
5239 return id;
5241 /* Fall through. */
5243 default:
5244 if (optional_p)
5245 return NULL_TREE;
5246 cp_parser_error (parser, "expected unqualified-id");
5247 return error_mark_node;
5251 /* Parse an (optional) nested-name-specifier.
5253 nested-name-specifier: [C++98]
5254 class-or-namespace-name :: nested-name-specifier [opt]
5255 class-or-namespace-name :: template nested-name-specifier [opt]
5257 nested-name-specifier: [C++0x]
5258 type-name ::
5259 namespace-name ::
5260 nested-name-specifier identifier ::
5261 nested-name-specifier template [opt] simple-template-id ::
5263 PARSER->SCOPE should be set appropriately before this function is
5264 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5265 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5266 in name lookups.
5268 Sets PARSER->SCOPE to the class (TYPE) or namespace
5269 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5270 it unchanged if there is no nested-name-specifier. Returns the new
5271 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5273 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5274 part of a declaration and/or decl-specifier. */
5276 static tree
5277 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5278 bool typename_keyword_p,
5279 bool check_dependency_p,
5280 bool type_p,
5281 bool is_declaration)
5283 bool success = false;
5284 cp_token_position start = 0;
5285 cp_token *token;
5287 /* Remember where the nested-name-specifier starts. */
5288 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5290 start = cp_lexer_token_position (parser->lexer, false);
5291 push_deferring_access_checks (dk_deferred);
5294 while (true)
5296 tree new_scope;
5297 tree old_scope;
5298 tree saved_qualifying_scope;
5299 bool template_keyword_p;
5301 /* Spot cases that cannot be the beginning of a
5302 nested-name-specifier. */
5303 token = cp_lexer_peek_token (parser->lexer);
5305 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5306 the already parsed nested-name-specifier. */
5307 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5309 /* Grab the nested-name-specifier and continue the loop. */
5310 cp_parser_pre_parsed_nested_name_specifier (parser);
5311 /* If we originally encountered this nested-name-specifier
5312 with IS_DECLARATION set to false, we will not have
5313 resolved TYPENAME_TYPEs, so we must do so here. */
5314 if (is_declaration
5315 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5317 new_scope = resolve_typename_type (parser->scope,
5318 /*only_current_p=*/false);
5319 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5320 parser->scope = new_scope;
5322 success = true;
5323 continue;
5326 /* Spot cases that cannot be the beginning of a
5327 nested-name-specifier. On the second and subsequent times
5328 through the loop, we look for the `template' keyword. */
5329 if (success && token->keyword == RID_TEMPLATE)
5331 /* A template-id can start a nested-name-specifier. */
5332 else if (token->type == CPP_TEMPLATE_ID)
5334 /* DR 743: decltype can be used in a nested-name-specifier. */
5335 else if (token_is_decltype (token))
5337 else
5339 /* If the next token is not an identifier, then it is
5340 definitely not a type-name or namespace-name. */
5341 if (token->type != CPP_NAME)
5342 break;
5343 /* If the following token is neither a `<' (to begin a
5344 template-id), nor a `::', then we are not looking at a
5345 nested-name-specifier. */
5346 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5348 if (token->type == CPP_COLON
5349 && parser->colon_corrects_to_scope_p
5350 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5352 error_at (token->location,
5353 "found %<:%> in nested-name-specifier, expected %<::%>");
5354 token->type = CPP_SCOPE;
5357 if (token->type != CPP_SCOPE
5358 && !cp_parser_nth_token_starts_template_argument_list_p
5359 (parser, 2))
5360 break;
5363 /* The nested-name-specifier is optional, so we parse
5364 tentatively. */
5365 cp_parser_parse_tentatively (parser);
5367 /* Look for the optional `template' keyword, if this isn't the
5368 first time through the loop. */
5369 if (success)
5370 template_keyword_p = cp_parser_optional_template_keyword (parser);
5371 else
5372 template_keyword_p = false;
5374 /* Save the old scope since the name lookup we are about to do
5375 might destroy it. */
5376 old_scope = parser->scope;
5377 saved_qualifying_scope = parser->qualifying_scope;
5378 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5379 look up names in "X<T>::I" in order to determine that "Y" is
5380 a template. So, if we have a typename at this point, we make
5381 an effort to look through it. */
5382 if (is_declaration
5383 && !typename_keyword_p
5384 && parser->scope
5385 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5386 parser->scope = resolve_typename_type (parser->scope,
5387 /*only_current_p=*/false);
5388 /* Parse the qualifying entity. */
5389 new_scope
5390 = cp_parser_qualifying_entity (parser,
5391 typename_keyword_p,
5392 template_keyword_p,
5393 check_dependency_p,
5394 type_p,
5395 is_declaration);
5396 /* Look for the `::' token. */
5397 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5399 /* If we found what we wanted, we keep going; otherwise, we're
5400 done. */
5401 if (!cp_parser_parse_definitely (parser))
5403 bool error_p = false;
5405 /* Restore the OLD_SCOPE since it was valid before the
5406 failed attempt at finding the last
5407 class-or-namespace-name. */
5408 parser->scope = old_scope;
5409 parser->qualifying_scope = saved_qualifying_scope;
5411 /* If the next token is a decltype, and the one after that is a
5412 `::', then the decltype has failed to resolve to a class or
5413 enumeration type. Give this error even when parsing
5414 tentatively since it can't possibly be valid--and we're going
5415 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5416 won't get another chance.*/
5417 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5418 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5419 == CPP_SCOPE))
5421 token = cp_lexer_consume_token (parser->lexer);
5422 error_at (token->location, "decltype evaluates to %qT, "
5423 "which is not a class or enumeration type",
5424 token->u.value);
5425 parser->scope = error_mark_node;
5426 error_p = true;
5427 /* As below. */
5428 success = true;
5429 cp_lexer_consume_token (parser->lexer);
5432 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5433 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5435 /* If we have a non-type template-id followed by ::, it can't
5436 possibly be valid. */
5437 token = cp_lexer_peek_token (parser->lexer);
5438 tree tid = token->u.tree_check_value->value;
5439 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5440 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5442 tree tmpl = NULL_TREE;
5443 if (is_overloaded_fn (tid))
5445 tree fns = get_fns (tid);
5446 if (!OVL_CHAIN (fns))
5447 tmpl = OVL_CURRENT (fns);
5448 error_at (token->location, "function template-id %qD "
5449 "in nested-name-specifier", tid);
5451 else
5453 /* Variable template. */
5454 tmpl = TREE_OPERAND (tid, 0);
5455 gcc_assert (variable_template_p (tmpl));
5456 error_at (token->location, "variable template-id %qD "
5457 "in nested-name-specifier", tid);
5459 if (tmpl)
5460 inform (DECL_SOURCE_LOCATION (tmpl),
5461 "%qD declared here", tmpl);
5463 parser->scope = error_mark_node;
5464 error_p = true;
5465 /* As below. */
5466 success = true;
5467 cp_lexer_consume_token (parser->lexer);
5468 cp_lexer_consume_token (parser->lexer);
5472 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5473 break;
5474 /* If the next token is an identifier, and the one after
5475 that is a `::', then any valid interpretation would have
5476 found a class-or-namespace-name. */
5477 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5478 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5479 == CPP_SCOPE)
5480 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5481 != CPP_COMPL))
5483 token = cp_lexer_consume_token (parser->lexer);
5484 if (!error_p)
5486 if (!token->error_reported)
5488 tree decl;
5489 tree ambiguous_decls;
5491 decl = cp_parser_lookup_name (parser, token->u.value,
5492 none_type,
5493 /*is_template=*/false,
5494 /*is_namespace=*/false,
5495 /*check_dependency=*/true,
5496 &ambiguous_decls,
5497 token->location);
5498 if (TREE_CODE (decl) == TEMPLATE_DECL)
5499 error_at (token->location,
5500 "%qD used without template parameters",
5501 decl);
5502 else if (ambiguous_decls)
5504 // cp_parser_lookup_name has the same diagnostic,
5505 // thus make sure to emit it at most once.
5506 if (cp_parser_uncommitted_to_tentative_parse_p
5507 (parser))
5509 error_at (token->location,
5510 "reference to %qD is ambiguous",
5511 token->u.value);
5512 print_candidates (ambiguous_decls);
5514 decl = error_mark_node;
5516 else
5518 if (cxx_dialect != cxx98)
5519 cp_parser_name_lookup_error
5520 (parser, token->u.value, decl, NLE_NOT_CXX98,
5521 token->location);
5522 else
5523 cp_parser_name_lookup_error
5524 (parser, token->u.value, decl, NLE_CXX98,
5525 token->location);
5528 parser->scope = error_mark_node;
5529 error_p = true;
5530 /* Treat this as a successful nested-name-specifier
5531 due to:
5533 [basic.lookup.qual]
5535 If the name found is not a class-name (clause
5536 _class_) or namespace-name (_namespace.def_), the
5537 program is ill-formed. */
5538 success = true;
5540 cp_lexer_consume_token (parser->lexer);
5542 break;
5544 /* We've found one valid nested-name-specifier. */
5545 success = true;
5546 /* Name lookup always gives us a DECL. */
5547 if (TREE_CODE (new_scope) == TYPE_DECL)
5548 new_scope = TREE_TYPE (new_scope);
5549 /* Uses of "template" must be followed by actual templates. */
5550 if (template_keyword_p
5551 && !(CLASS_TYPE_P (new_scope)
5552 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5553 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5554 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5555 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5556 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5557 == TEMPLATE_ID_EXPR)))
5558 permerror (input_location, TYPE_P (new_scope)
5559 ? G_("%qT is not a template")
5560 : G_("%qD is not a template"),
5561 new_scope);
5562 /* If it is a class scope, try to complete it; we are about to
5563 be looking up names inside the class. */
5564 if (TYPE_P (new_scope)
5565 /* Since checking types for dependency can be expensive,
5566 avoid doing it if the type is already complete. */
5567 && !COMPLETE_TYPE_P (new_scope)
5568 /* Do not try to complete dependent types. */
5569 && !dependent_type_p (new_scope))
5571 new_scope = complete_type (new_scope);
5572 /* If it is a typedef to current class, use the current
5573 class instead, as the typedef won't have any names inside
5574 it yet. */
5575 if (!COMPLETE_TYPE_P (new_scope)
5576 && currently_open_class (new_scope))
5577 new_scope = TYPE_MAIN_VARIANT (new_scope);
5579 /* Make sure we look in the right scope the next time through
5580 the loop. */
5581 parser->scope = new_scope;
5584 /* If parsing tentatively, replace the sequence of tokens that makes
5585 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5586 token. That way, should we re-parse the token stream, we will
5587 not have to repeat the effort required to do the parse, nor will
5588 we issue duplicate error messages. */
5589 if (success && start)
5591 cp_token *token;
5593 token = cp_lexer_token_at (parser->lexer, start);
5594 /* Reset the contents of the START token. */
5595 token->type = CPP_NESTED_NAME_SPECIFIER;
5596 /* Retrieve any deferred checks. Do not pop this access checks yet
5597 so the memory will not be reclaimed during token replacing below. */
5598 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5599 token->u.tree_check_value->value = parser->scope;
5600 token->u.tree_check_value->checks = get_deferred_access_checks ();
5601 token->u.tree_check_value->qualifying_scope =
5602 parser->qualifying_scope;
5603 token->keyword = RID_MAX;
5605 /* Purge all subsequent tokens. */
5606 cp_lexer_purge_tokens_after (parser->lexer, start);
5609 if (start)
5610 pop_to_parent_deferring_access_checks ();
5612 return success ? parser->scope : NULL_TREE;
5615 /* Parse a nested-name-specifier. See
5616 cp_parser_nested_name_specifier_opt for details. This function
5617 behaves identically, except that it will an issue an error if no
5618 nested-name-specifier is present. */
5620 static tree
5621 cp_parser_nested_name_specifier (cp_parser *parser,
5622 bool typename_keyword_p,
5623 bool check_dependency_p,
5624 bool type_p,
5625 bool is_declaration)
5627 tree scope;
5629 /* Look for the nested-name-specifier. */
5630 scope = cp_parser_nested_name_specifier_opt (parser,
5631 typename_keyword_p,
5632 check_dependency_p,
5633 type_p,
5634 is_declaration);
5635 /* If it was not present, issue an error message. */
5636 if (!scope)
5638 cp_parser_error (parser, "expected nested-name-specifier");
5639 parser->scope = NULL_TREE;
5642 return scope;
5645 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5646 this is either a class-name or a namespace-name (which corresponds
5647 to the class-or-namespace-name production in the grammar). For
5648 C++0x, it can also be a type-name that refers to an enumeration
5649 type or a simple-template-id.
5651 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5652 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5653 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5654 TYPE_P is TRUE iff the next name should be taken as a class-name,
5655 even the same name is declared to be another entity in the same
5656 scope.
5658 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5659 specified by the class-or-namespace-name. If neither is found the
5660 ERROR_MARK_NODE is returned. */
5662 static tree
5663 cp_parser_qualifying_entity (cp_parser *parser,
5664 bool typename_keyword_p,
5665 bool template_keyword_p,
5666 bool check_dependency_p,
5667 bool type_p,
5668 bool is_declaration)
5670 tree saved_scope;
5671 tree saved_qualifying_scope;
5672 tree saved_object_scope;
5673 tree scope;
5674 bool only_class_p;
5675 bool successful_parse_p;
5677 /* DR 743: decltype can appear in a nested-name-specifier. */
5678 if (cp_lexer_next_token_is_decltype (parser->lexer))
5680 scope = cp_parser_decltype (parser);
5681 if (TREE_CODE (scope) != ENUMERAL_TYPE
5682 && !MAYBE_CLASS_TYPE_P (scope))
5684 cp_parser_simulate_error (parser);
5685 return error_mark_node;
5687 if (TYPE_NAME (scope))
5688 scope = TYPE_NAME (scope);
5689 return scope;
5692 /* Before we try to parse the class-name, we must save away the
5693 current PARSER->SCOPE since cp_parser_class_name will destroy
5694 it. */
5695 saved_scope = parser->scope;
5696 saved_qualifying_scope = parser->qualifying_scope;
5697 saved_object_scope = parser->object_scope;
5698 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5699 there is no need to look for a namespace-name. */
5700 only_class_p = template_keyword_p
5701 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5702 if (!only_class_p)
5703 cp_parser_parse_tentatively (parser);
5704 scope = cp_parser_class_name (parser,
5705 typename_keyword_p,
5706 template_keyword_p,
5707 type_p ? class_type : none_type,
5708 check_dependency_p,
5709 /*class_head_p=*/false,
5710 is_declaration,
5711 /*enum_ok=*/cxx_dialect > cxx98);
5712 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5713 /* If that didn't work, try for a namespace-name. */
5714 if (!only_class_p && !successful_parse_p)
5716 /* Restore the saved scope. */
5717 parser->scope = saved_scope;
5718 parser->qualifying_scope = saved_qualifying_scope;
5719 parser->object_scope = saved_object_scope;
5720 /* If we are not looking at an identifier followed by the scope
5721 resolution operator, then this is not part of a
5722 nested-name-specifier. (Note that this function is only used
5723 to parse the components of a nested-name-specifier.) */
5724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5725 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5726 return error_mark_node;
5727 scope = cp_parser_namespace_name (parser);
5730 return scope;
5733 /* Return true if we are looking at a compound-literal, false otherwise. */
5735 static bool
5736 cp_parser_compound_literal_p (cp_parser *parser)
5738 /* Consume the `('. */
5739 cp_lexer_consume_token (parser->lexer);
5741 cp_lexer_save_tokens (parser->lexer);
5743 /* Skip tokens until the next token is a closing parenthesis.
5744 If we find the closing `)', and the next token is a `{', then
5745 we are looking at a compound-literal. */
5746 bool compound_literal_p
5747 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5748 /*consume_paren=*/true)
5749 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5751 /* Roll back the tokens we skipped. */
5752 cp_lexer_rollback_tokens (parser->lexer);
5754 return compound_literal_p;
5757 /* Parse a postfix-expression.
5759 postfix-expression:
5760 primary-expression
5761 postfix-expression [ expression ]
5762 postfix-expression ( expression-list [opt] )
5763 simple-type-specifier ( expression-list [opt] )
5764 typename :: [opt] nested-name-specifier identifier
5765 ( expression-list [opt] )
5766 typename :: [opt] nested-name-specifier template [opt] template-id
5767 ( expression-list [opt] )
5768 postfix-expression . template [opt] id-expression
5769 postfix-expression -> template [opt] id-expression
5770 postfix-expression . pseudo-destructor-name
5771 postfix-expression -> pseudo-destructor-name
5772 postfix-expression ++
5773 postfix-expression --
5774 dynamic_cast < type-id > ( expression )
5775 static_cast < type-id > ( expression )
5776 reinterpret_cast < type-id > ( expression )
5777 const_cast < type-id > ( expression )
5778 typeid ( expression )
5779 typeid ( type-id )
5781 GNU Extension:
5783 postfix-expression:
5784 ( type-id ) { initializer-list , [opt] }
5786 This extension is a GNU version of the C99 compound-literal
5787 construct. (The C99 grammar uses `type-name' instead of `type-id',
5788 but they are essentially the same concept.)
5790 If ADDRESS_P is true, the postfix expression is the operand of the
5791 `&' operator. CAST_P is true if this expression is the target of a
5792 cast.
5794 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5795 class member access expressions [expr.ref].
5797 Returns a representation of the expression. */
5799 static tree
5800 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5801 bool member_access_only_p, bool decltype_p,
5802 cp_id_kind * pidk_return)
5804 cp_token *token;
5805 location_t loc;
5806 enum rid keyword;
5807 cp_id_kind idk = CP_ID_KIND_NONE;
5808 tree postfix_expression = NULL_TREE;
5809 bool is_member_access = false;
5810 int saved_in_statement = -1;
5812 /* Peek at the next token. */
5813 token = cp_lexer_peek_token (parser->lexer);
5814 loc = token->location;
5815 /* Some of the productions are determined by keywords. */
5816 keyword = token->keyword;
5817 switch (keyword)
5819 case RID_DYNCAST:
5820 case RID_STATCAST:
5821 case RID_REINTCAST:
5822 case RID_CONSTCAST:
5824 tree type;
5825 tree expression;
5826 const char *saved_message;
5827 bool saved_in_type_id_in_expr_p;
5829 /* All of these can be handled in the same way from the point
5830 of view of parsing. Begin by consuming the token
5831 identifying the cast. */
5832 cp_lexer_consume_token (parser->lexer);
5834 /* New types cannot be defined in the cast. */
5835 saved_message = parser->type_definition_forbidden_message;
5836 parser->type_definition_forbidden_message
5837 = G_("types may not be defined in casts");
5839 /* Look for the opening `<'. */
5840 cp_parser_require (parser, CPP_LESS, RT_LESS);
5841 /* Parse the type to which we are casting. */
5842 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5843 parser->in_type_id_in_expr_p = true;
5844 type = cp_parser_type_id (parser);
5845 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5846 /* Look for the closing `>'. */
5847 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5848 /* Restore the old message. */
5849 parser->type_definition_forbidden_message = saved_message;
5851 bool saved_greater_than_is_operator_p
5852 = parser->greater_than_is_operator_p;
5853 parser->greater_than_is_operator_p = true;
5855 /* And the expression which is being cast. */
5856 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5857 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5860 parser->greater_than_is_operator_p
5861 = saved_greater_than_is_operator_p;
5863 /* Only type conversions to integral or enumeration types
5864 can be used in constant-expressions. */
5865 if (!cast_valid_in_integral_constant_expression_p (type)
5866 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5867 return error_mark_node;
5869 switch (keyword)
5871 case RID_DYNCAST:
5872 postfix_expression
5873 = build_dynamic_cast (type, expression, tf_warning_or_error);
5874 break;
5875 case RID_STATCAST:
5876 postfix_expression
5877 = build_static_cast (type, expression, tf_warning_or_error);
5878 break;
5879 case RID_REINTCAST:
5880 postfix_expression
5881 = build_reinterpret_cast (type, expression,
5882 tf_warning_or_error);
5883 break;
5884 case RID_CONSTCAST:
5885 postfix_expression
5886 = build_const_cast (type, expression, tf_warning_or_error);
5887 break;
5888 default:
5889 gcc_unreachable ();
5892 break;
5894 case RID_TYPEID:
5896 tree type;
5897 const char *saved_message;
5898 bool saved_in_type_id_in_expr_p;
5900 /* Consume the `typeid' token. */
5901 cp_lexer_consume_token (parser->lexer);
5902 /* Look for the `(' token. */
5903 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5904 /* Types cannot be defined in a `typeid' expression. */
5905 saved_message = parser->type_definition_forbidden_message;
5906 parser->type_definition_forbidden_message
5907 = G_("types may not be defined in a %<typeid%> expression");
5908 /* We can't be sure yet whether we're looking at a type-id or an
5909 expression. */
5910 cp_parser_parse_tentatively (parser);
5911 /* Try a type-id first. */
5912 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5913 parser->in_type_id_in_expr_p = true;
5914 type = cp_parser_type_id (parser);
5915 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5916 /* Look for the `)' token. Otherwise, we can't be sure that
5917 we're not looking at an expression: consider `typeid (int
5918 (3))', for example. */
5919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5920 /* If all went well, simply lookup the type-id. */
5921 if (cp_parser_parse_definitely (parser))
5922 postfix_expression = get_typeid (type, tf_warning_or_error);
5923 /* Otherwise, fall back to the expression variant. */
5924 else
5926 tree expression;
5928 /* Look for an expression. */
5929 expression = cp_parser_expression (parser, & idk);
5930 /* Compute its typeid. */
5931 postfix_expression = build_typeid (expression, tf_warning_or_error);
5932 /* Look for the `)' token. */
5933 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5935 /* Restore the saved message. */
5936 parser->type_definition_forbidden_message = saved_message;
5937 /* `typeid' may not appear in an integral constant expression. */
5938 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5939 return error_mark_node;
5941 break;
5943 case RID_TYPENAME:
5945 tree type;
5946 /* The syntax permitted here is the same permitted for an
5947 elaborated-type-specifier. */
5948 type = cp_parser_elaborated_type_specifier (parser,
5949 /*is_friend=*/false,
5950 /*is_declaration=*/false);
5951 postfix_expression = cp_parser_functional_cast (parser, type);
5953 break;
5955 case RID_CILK_SPAWN:
5957 cp_lexer_consume_token (parser->lexer);
5958 token = cp_lexer_peek_token (parser->lexer);
5959 if (token->type == CPP_SEMICOLON)
5961 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5962 "an expression");
5963 postfix_expression = error_mark_node;
5964 break;
5966 else if (!current_function_decl)
5968 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5969 "inside a function");
5970 postfix_expression = error_mark_node;
5971 break;
5973 else
5975 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5976 saved_in_statement = parser->in_statement;
5977 parser->in_statement |= IN_CILK_SPAWN;
5979 cfun->calls_cilk_spawn = 1;
5980 postfix_expression =
5981 cp_parser_postfix_expression (parser, false, false,
5982 false, false, &idk);
5983 if (!flag_cilkplus)
5985 error_at (token->location, "-fcilkplus must be enabled to use"
5986 " %<_Cilk_spawn%>");
5987 cfun->calls_cilk_spawn = 0;
5989 else if (saved_in_statement & IN_CILK_SPAWN)
5991 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5992 "are not permitted");
5993 postfix_expression = error_mark_node;
5994 cfun->calls_cilk_spawn = 0;
5996 else
5998 postfix_expression = build_cilk_spawn (token->location,
5999 postfix_expression);
6000 if (postfix_expression != error_mark_node)
6001 SET_EXPR_LOCATION (postfix_expression, input_location);
6002 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6004 break;
6007 case RID_BUILTIN_SHUFFLE:
6009 vec<tree, va_gc> *vec;
6010 unsigned int i;
6011 tree p;
6013 cp_lexer_consume_token (parser->lexer);
6014 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6015 /*cast_p=*/false, /*allow_expansion_p=*/true,
6016 /*non_constant_p=*/NULL);
6017 if (vec == NULL)
6018 return error_mark_node;
6020 FOR_EACH_VEC_ELT (*vec, i, p)
6021 mark_exp_read (p);
6023 if (vec->length () == 2)
6024 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6025 tf_warning_or_error);
6026 else if (vec->length () == 3)
6027 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6028 tf_warning_or_error);
6029 else
6031 error_at (loc, "wrong number of arguments to "
6032 "%<__builtin_shuffle%>");
6033 return error_mark_node;
6035 break;
6038 default:
6040 tree type;
6042 /* If the next thing is a simple-type-specifier, we may be
6043 looking at a functional cast. We could also be looking at
6044 an id-expression. So, we try the functional cast, and if
6045 that doesn't work we fall back to the primary-expression. */
6046 cp_parser_parse_tentatively (parser);
6047 /* Look for the simple-type-specifier. */
6048 type = cp_parser_simple_type_specifier (parser,
6049 /*decl_specs=*/NULL,
6050 CP_PARSER_FLAGS_NONE);
6051 /* Parse the cast itself. */
6052 if (!cp_parser_error_occurred (parser))
6053 postfix_expression
6054 = cp_parser_functional_cast (parser, type);
6055 /* If that worked, we're done. */
6056 if (cp_parser_parse_definitely (parser))
6057 break;
6059 /* If the functional-cast didn't work out, try a
6060 compound-literal. */
6061 if (cp_parser_allow_gnu_extensions_p (parser)
6062 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6064 tree initializer = NULL_TREE;
6066 cp_parser_parse_tentatively (parser);
6068 /* Avoid calling cp_parser_type_id pointlessly, see comment
6069 in cp_parser_cast_expression about c++/29234. */
6070 if (!cp_parser_compound_literal_p (parser))
6071 cp_parser_simulate_error (parser);
6072 else
6074 /* Parse the type. */
6075 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6076 parser->in_type_id_in_expr_p = true;
6077 type = cp_parser_type_id (parser);
6078 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6079 /* Look for the `)'. */
6080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6083 /* If things aren't going well, there's no need to
6084 keep going. */
6085 if (!cp_parser_error_occurred (parser))
6087 bool non_constant_p;
6088 /* Parse the brace-enclosed initializer list. */
6089 initializer = cp_parser_braced_list (parser,
6090 &non_constant_p);
6092 /* If that worked, we're definitely looking at a
6093 compound-literal expression. */
6094 if (cp_parser_parse_definitely (parser))
6096 /* Warn the user that a compound literal is not
6097 allowed in standard C++. */
6098 pedwarn (input_location, OPT_Wpedantic,
6099 "ISO C++ forbids compound-literals");
6100 /* For simplicity, we disallow compound literals in
6101 constant-expressions. We could
6102 allow compound literals of integer type, whose
6103 initializer was a constant, in constant
6104 expressions. Permitting that usage, as a further
6105 extension, would not change the meaning of any
6106 currently accepted programs. (Of course, as
6107 compound literals are not part of ISO C++, the
6108 standard has nothing to say.) */
6109 if (cp_parser_non_integral_constant_expression (parser,
6110 NIC_NCC))
6112 postfix_expression = error_mark_node;
6113 break;
6115 /* Form the representation of the compound-literal. */
6116 postfix_expression
6117 = finish_compound_literal (type, initializer,
6118 tf_warning_or_error);
6119 break;
6123 /* It must be a primary-expression. */
6124 postfix_expression
6125 = cp_parser_primary_expression (parser, address_p, cast_p,
6126 /*template_arg_p=*/false,
6127 decltype_p,
6128 &idk);
6130 break;
6133 /* Note that we don't need to worry about calling build_cplus_new on a
6134 class-valued CALL_EXPR in decltype when it isn't the end of the
6135 postfix-expression; unary_complex_lvalue will take care of that for
6136 all these cases. */
6138 /* Keep looping until the postfix-expression is complete. */
6139 while (true)
6141 if (idk == CP_ID_KIND_UNQUALIFIED
6142 && identifier_p (postfix_expression)
6143 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6144 /* It is not a Koenig lookup function call. */
6145 postfix_expression
6146 = unqualified_name_lookup_error (postfix_expression);
6148 /* Peek at the next token. */
6149 token = cp_lexer_peek_token (parser->lexer);
6151 switch (token->type)
6153 case CPP_OPEN_SQUARE:
6154 if (cp_next_tokens_can_be_std_attribute_p (parser))
6156 cp_parser_error (parser,
6157 "two consecutive %<[%> shall "
6158 "only introduce an attribute");
6159 return error_mark_node;
6161 postfix_expression
6162 = cp_parser_postfix_open_square_expression (parser,
6163 postfix_expression,
6164 false,
6165 decltype_p);
6166 idk = CP_ID_KIND_NONE;
6167 is_member_access = false;
6168 break;
6170 case CPP_OPEN_PAREN:
6171 /* postfix-expression ( expression-list [opt] ) */
6173 bool koenig_p;
6174 bool is_builtin_constant_p;
6175 bool saved_integral_constant_expression_p = false;
6176 bool saved_non_integral_constant_expression_p = false;
6177 tsubst_flags_t complain = complain_flags (decltype_p);
6178 vec<tree, va_gc> *args;
6180 is_member_access = false;
6182 is_builtin_constant_p
6183 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6184 if (is_builtin_constant_p)
6186 /* The whole point of __builtin_constant_p is to allow
6187 non-constant expressions to appear as arguments. */
6188 saved_integral_constant_expression_p
6189 = parser->integral_constant_expression_p;
6190 saved_non_integral_constant_expression_p
6191 = parser->non_integral_constant_expression_p;
6192 parser->integral_constant_expression_p = false;
6194 args = (cp_parser_parenthesized_expression_list
6195 (parser, non_attr,
6196 /*cast_p=*/false, /*allow_expansion_p=*/true,
6197 /*non_constant_p=*/NULL,
6198 /*want_literal_zero_p=*/warn_memset_transposed_args));
6199 if (is_builtin_constant_p)
6201 parser->integral_constant_expression_p
6202 = saved_integral_constant_expression_p;
6203 parser->non_integral_constant_expression_p
6204 = saved_non_integral_constant_expression_p;
6207 if (args == NULL)
6209 postfix_expression = error_mark_node;
6210 break;
6213 /* Function calls are not permitted in
6214 constant-expressions. */
6215 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6216 && cp_parser_non_integral_constant_expression (parser,
6217 NIC_FUNC_CALL))
6219 postfix_expression = error_mark_node;
6220 release_tree_vector (args);
6221 break;
6224 koenig_p = false;
6225 if (idk == CP_ID_KIND_UNQUALIFIED
6226 || idk == CP_ID_KIND_TEMPLATE_ID)
6228 if (identifier_p (postfix_expression))
6230 if (!args->is_empty ())
6232 koenig_p = true;
6233 if (!any_type_dependent_arguments_p (args))
6234 postfix_expression
6235 = perform_koenig_lookup (postfix_expression, args,
6236 complain);
6238 else
6239 postfix_expression
6240 = unqualified_fn_lookup_error (postfix_expression);
6242 /* We do not perform argument-dependent lookup if
6243 normal lookup finds a non-function, in accordance
6244 with the expected resolution of DR 218. */
6245 else if (!args->is_empty ()
6246 && is_overloaded_fn (postfix_expression))
6248 tree fn = get_first_fn (postfix_expression);
6249 fn = STRIP_TEMPLATE (fn);
6251 /* Do not do argument dependent lookup if regular
6252 lookup finds a member function or a block-scope
6253 function declaration. [basic.lookup.argdep]/3 */
6254 if (!DECL_FUNCTION_MEMBER_P (fn)
6255 && !DECL_LOCAL_FUNCTION_P (fn))
6257 koenig_p = true;
6258 if (!any_type_dependent_arguments_p (args))
6259 postfix_expression
6260 = perform_koenig_lookup (postfix_expression, args,
6261 complain);
6266 if (warn_memset_transposed_args)
6268 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6269 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6270 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6271 && vec_safe_length (args) == 3
6272 && integer_zerop ((*args)[2])
6273 && LITERAL_ZERO_P ((*args)[2])
6274 && !(integer_zerop ((*args)[1])
6275 && LITERAL_ZERO_P ((*args)[1])))
6276 warning (OPT_Wmemset_transposed_args,
6277 "%<memset%> used with constant zero length "
6278 "parameter; this could be due to transposed "
6279 "parameters");
6281 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6282 to avoid leaking those into folder and middle-end. */
6283 unsigned int i;
6284 tree arg;
6285 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6286 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6287 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6290 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6292 tree instance = TREE_OPERAND (postfix_expression, 0);
6293 tree fn = TREE_OPERAND (postfix_expression, 1);
6295 if (processing_template_decl
6296 && (type_dependent_expression_p (instance)
6297 || (!BASELINK_P (fn)
6298 && TREE_CODE (fn) != FIELD_DECL)
6299 || type_dependent_expression_p (fn)
6300 || any_type_dependent_arguments_p (args)))
6302 postfix_expression
6303 = build_nt_call_vec (postfix_expression, args);
6304 release_tree_vector (args);
6305 break;
6308 if (BASELINK_P (fn))
6310 postfix_expression
6311 = (build_new_method_call
6312 (instance, fn, &args, NULL_TREE,
6313 (idk == CP_ID_KIND_QUALIFIED
6314 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6315 : LOOKUP_NORMAL),
6316 /*fn_p=*/NULL,
6317 complain));
6319 else
6320 postfix_expression
6321 = finish_call_expr (postfix_expression, &args,
6322 /*disallow_virtual=*/false,
6323 /*koenig_p=*/false,
6324 complain);
6326 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6327 || TREE_CODE (postfix_expression) == MEMBER_REF
6328 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6329 postfix_expression = (build_offset_ref_call_from_tree
6330 (postfix_expression, &args,
6331 complain));
6332 else if (idk == CP_ID_KIND_QUALIFIED)
6333 /* A call to a static class member, or a namespace-scope
6334 function. */
6335 postfix_expression
6336 = finish_call_expr (postfix_expression, &args,
6337 /*disallow_virtual=*/true,
6338 koenig_p,
6339 complain);
6340 else
6341 /* All other function calls. */
6342 postfix_expression
6343 = finish_call_expr (postfix_expression, &args,
6344 /*disallow_virtual=*/false,
6345 koenig_p,
6346 complain);
6348 protected_set_expr_location (postfix_expression, token->location);
6350 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6351 idk = CP_ID_KIND_NONE;
6353 release_tree_vector (args);
6355 break;
6357 case CPP_DOT:
6358 case CPP_DEREF:
6359 /* postfix-expression . template [opt] id-expression
6360 postfix-expression . pseudo-destructor-name
6361 postfix-expression -> template [opt] id-expression
6362 postfix-expression -> pseudo-destructor-name */
6364 /* Consume the `.' or `->' operator. */
6365 cp_lexer_consume_token (parser->lexer);
6367 postfix_expression
6368 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6369 postfix_expression,
6370 false, &idk, loc);
6372 is_member_access = true;
6373 break;
6375 case CPP_PLUS_PLUS:
6376 /* postfix-expression ++ */
6377 /* Consume the `++' token. */
6378 cp_lexer_consume_token (parser->lexer);
6379 /* Generate a representation for the complete expression. */
6380 postfix_expression
6381 = finish_increment_expr (postfix_expression,
6382 POSTINCREMENT_EXPR);
6383 /* Increments may not appear in constant-expressions. */
6384 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6385 postfix_expression = error_mark_node;
6386 idk = CP_ID_KIND_NONE;
6387 is_member_access = false;
6388 break;
6390 case CPP_MINUS_MINUS:
6391 /* postfix-expression -- */
6392 /* Consume the `--' token. */
6393 cp_lexer_consume_token (parser->lexer);
6394 /* Generate a representation for the complete expression. */
6395 postfix_expression
6396 = finish_increment_expr (postfix_expression,
6397 POSTDECREMENT_EXPR);
6398 /* Decrements may not appear in constant-expressions. */
6399 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6400 postfix_expression = error_mark_node;
6401 idk = CP_ID_KIND_NONE;
6402 is_member_access = false;
6403 break;
6405 default:
6406 if (pidk_return != NULL)
6407 * pidk_return = idk;
6408 if (member_access_only_p)
6409 return is_member_access? postfix_expression : error_mark_node;
6410 else
6411 return postfix_expression;
6415 /* We should never get here. */
6416 gcc_unreachable ();
6417 return error_mark_node;
6420 /* This function parses Cilk Plus array notations. If a normal array expr. is
6421 parsed then the array index is passed back to the caller through *INIT_INDEX
6422 and the function returns a NULL_TREE. If array notation expr. is parsed,
6423 then *INIT_INDEX is ignored by the caller and the function returns
6424 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6425 error_mark_node. */
6427 static tree
6428 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6429 tree array_value)
6431 cp_token *token = NULL;
6432 tree length_index, stride = NULL_TREE, value_tree, array_type;
6433 if (!array_value || array_value == error_mark_node)
6435 cp_parser_skip_to_end_of_statement (parser);
6436 return error_mark_node;
6439 array_type = TREE_TYPE (array_value);
6441 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6442 parser->colon_corrects_to_scope_p = false;
6443 token = cp_lexer_peek_token (parser->lexer);
6445 if (!token)
6447 cp_parser_error (parser, "expected %<:%> or numeral");
6448 return error_mark_node;
6450 else if (token->type == CPP_COLON)
6452 /* Consume the ':'. */
6453 cp_lexer_consume_token (parser->lexer);
6455 /* If we are here, then we have a case like this A[:]. */
6456 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6458 cp_parser_error (parser, "expected %<]%>");
6459 cp_parser_skip_to_end_of_statement (parser);
6460 return error_mark_node;
6462 *init_index = NULL_TREE;
6463 stride = NULL_TREE;
6464 length_index = NULL_TREE;
6466 else
6468 /* If we are here, then there are three valid possibilities:
6469 1. ARRAY [ EXP ]
6470 2. ARRAY [ EXP : EXP ]
6471 3. ARRAY [ EXP : EXP : EXP ] */
6473 *init_index = cp_parser_expression (parser);
6474 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6476 /* This indicates that we have a normal array expression. */
6477 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6478 return NULL_TREE;
6481 /* Consume the ':'. */
6482 cp_lexer_consume_token (parser->lexer);
6483 length_index = cp_parser_expression (parser);
6484 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6486 cp_lexer_consume_token (parser->lexer);
6487 stride = cp_parser_expression (parser);
6490 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6492 if (*init_index == error_mark_node || length_index == error_mark_node
6493 || stride == error_mark_node || array_type == error_mark_node)
6495 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6496 cp_lexer_consume_token (parser->lexer);
6497 return error_mark_node;
6499 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6501 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6502 length_index, stride, array_type);
6503 return value_tree;
6506 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6507 by cp_parser_builtin_offsetof. We're looking for
6509 postfix-expression [ expression ]
6510 postfix-expression [ braced-init-list ] (C++11)
6512 FOR_OFFSETOF is set if we're being called in that context, which
6513 changes how we deal with integer constant expressions. */
6515 static tree
6516 cp_parser_postfix_open_square_expression (cp_parser *parser,
6517 tree postfix_expression,
6518 bool for_offsetof,
6519 bool decltype_p)
6521 tree index = NULL_TREE;
6522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6523 bool saved_greater_than_is_operator_p;
6525 /* Consume the `[' token. */
6526 cp_lexer_consume_token (parser->lexer);
6528 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6529 parser->greater_than_is_operator_p = true;
6531 /* Parse the index expression. */
6532 /* ??? For offsetof, there is a question of what to allow here. If
6533 offsetof is not being used in an integral constant expression context,
6534 then we *could* get the right answer by computing the value at runtime.
6535 If we are in an integral constant expression context, then we might
6536 could accept any constant expression; hard to say without analysis.
6537 Rather than open the barn door too wide right away, allow only integer
6538 constant expressions here. */
6539 if (for_offsetof)
6540 index = cp_parser_constant_expression (parser);
6541 else
6543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6545 bool expr_nonconst_p;
6546 cp_lexer_set_source_position (parser->lexer);
6547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6548 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6549 if (flag_cilkplus
6550 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6552 error_at (cp_lexer_peek_token (parser->lexer)->location,
6553 "braced list index is not allowed with array "
6554 "notation");
6555 cp_parser_skip_to_end_of_statement (parser);
6556 return error_mark_node;
6559 else if (flag_cilkplus)
6561 /* Here are have these two options:
6562 ARRAY[EXP : EXP] - Array notation expr with default
6563 stride of 1.
6564 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6565 stride. */
6566 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6567 postfix_expression);
6568 if (an_exp)
6569 return an_exp;
6571 else
6572 index = cp_parser_expression (parser);
6575 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6577 /* Look for the closing `]'. */
6578 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6580 /* Build the ARRAY_REF. */
6581 postfix_expression = grok_array_decl (loc, postfix_expression,
6582 index, decltype_p);
6584 /* When not doing offsetof, array references are not permitted in
6585 constant-expressions. */
6586 if (!for_offsetof
6587 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6588 postfix_expression = error_mark_node;
6590 return postfix_expression;
6593 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6594 by cp_parser_builtin_offsetof. We're looking for
6596 postfix-expression . template [opt] id-expression
6597 postfix-expression . pseudo-destructor-name
6598 postfix-expression -> template [opt] id-expression
6599 postfix-expression -> pseudo-destructor-name
6601 FOR_OFFSETOF is set if we're being called in that context. That sorta
6602 limits what of the above we'll actually accept, but nevermind.
6603 TOKEN_TYPE is the "." or "->" token, which will already have been
6604 removed from the stream. */
6606 static tree
6607 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6608 enum cpp_ttype token_type,
6609 tree postfix_expression,
6610 bool for_offsetof, cp_id_kind *idk,
6611 location_t location)
6613 tree name;
6614 bool dependent_p;
6615 bool pseudo_destructor_p;
6616 tree scope = NULL_TREE;
6618 /* If this is a `->' operator, dereference the pointer. */
6619 if (token_type == CPP_DEREF)
6620 postfix_expression = build_x_arrow (location, postfix_expression,
6621 tf_warning_or_error);
6622 /* Check to see whether or not the expression is type-dependent. */
6623 dependent_p = type_dependent_expression_p (postfix_expression);
6624 /* The identifier following the `->' or `.' is not qualified. */
6625 parser->scope = NULL_TREE;
6626 parser->qualifying_scope = NULL_TREE;
6627 parser->object_scope = NULL_TREE;
6628 *idk = CP_ID_KIND_NONE;
6630 /* Enter the scope corresponding to the type of the object
6631 given by the POSTFIX_EXPRESSION. */
6632 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6634 scope = TREE_TYPE (postfix_expression);
6635 /* According to the standard, no expression should ever have
6636 reference type. Unfortunately, we do not currently match
6637 the standard in this respect in that our internal representation
6638 of an expression may have reference type even when the standard
6639 says it does not. Therefore, we have to manually obtain the
6640 underlying type here. */
6641 scope = non_reference (scope);
6642 /* The type of the POSTFIX_EXPRESSION must be complete. */
6643 if (scope == unknown_type_node)
6645 error_at (location, "%qE does not have class type",
6646 postfix_expression);
6647 scope = NULL_TREE;
6649 /* Unlike the object expression in other contexts, *this is not
6650 required to be of complete type for purposes of class member
6651 access (5.2.5) outside the member function body. */
6652 else if (postfix_expression != current_class_ref
6653 && !(processing_template_decl && scope == current_class_type))
6654 scope = complete_type_or_else (scope, NULL_TREE);
6655 /* Let the name lookup machinery know that we are processing a
6656 class member access expression. */
6657 parser->context->object_type = scope;
6658 /* If something went wrong, we want to be able to discern that case,
6659 as opposed to the case where there was no SCOPE due to the type
6660 of expression being dependent. */
6661 if (!scope)
6662 scope = error_mark_node;
6663 /* If the SCOPE was erroneous, make the various semantic analysis
6664 functions exit quickly -- and without issuing additional error
6665 messages. */
6666 if (scope == error_mark_node)
6667 postfix_expression = error_mark_node;
6670 /* Assume this expression is not a pseudo-destructor access. */
6671 pseudo_destructor_p = false;
6673 /* If the SCOPE is a scalar type, then, if this is a valid program,
6674 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6675 is type dependent, it can be pseudo-destructor-name or something else.
6676 Try to parse it as pseudo-destructor-name first. */
6677 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6679 tree s;
6680 tree type;
6682 cp_parser_parse_tentatively (parser);
6683 /* Parse the pseudo-destructor-name. */
6684 s = NULL_TREE;
6685 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6686 &s, &type);
6687 if (dependent_p
6688 && (cp_parser_error_occurred (parser)
6689 || !SCALAR_TYPE_P (type)))
6690 cp_parser_abort_tentative_parse (parser);
6691 else if (cp_parser_parse_definitely (parser))
6693 pseudo_destructor_p = true;
6694 postfix_expression
6695 = finish_pseudo_destructor_expr (postfix_expression,
6696 s, type, location);
6700 if (!pseudo_destructor_p)
6702 /* If the SCOPE is not a scalar type, we are looking at an
6703 ordinary class member access expression, rather than a
6704 pseudo-destructor-name. */
6705 bool template_p;
6706 cp_token *token = cp_lexer_peek_token (parser->lexer);
6707 /* Parse the id-expression. */
6708 name = (cp_parser_id_expression
6709 (parser,
6710 cp_parser_optional_template_keyword (parser),
6711 /*check_dependency_p=*/true,
6712 &template_p,
6713 /*declarator_p=*/false,
6714 /*optional_p=*/false));
6715 /* In general, build a SCOPE_REF if the member name is qualified.
6716 However, if the name was not dependent and has already been
6717 resolved; there is no need to build the SCOPE_REF. For example;
6719 struct X { void f(); };
6720 template <typename T> void f(T* t) { t->X::f(); }
6722 Even though "t" is dependent, "X::f" is not and has been resolved
6723 to a BASELINK; there is no need to include scope information. */
6725 /* But we do need to remember that there was an explicit scope for
6726 virtual function calls. */
6727 if (parser->scope)
6728 *idk = CP_ID_KIND_QUALIFIED;
6730 /* If the name is a template-id that names a type, we will get a
6731 TYPE_DECL here. That is invalid code. */
6732 if (TREE_CODE (name) == TYPE_DECL)
6734 error_at (token->location, "invalid use of %qD", name);
6735 postfix_expression = error_mark_node;
6737 else
6739 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6741 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6743 error_at (token->location, "%<%D::%D%> is not a class member",
6744 parser->scope, name);
6745 postfix_expression = error_mark_node;
6747 else
6748 name = build_qualified_name (/*type=*/NULL_TREE,
6749 parser->scope,
6750 name,
6751 template_p);
6752 parser->scope = NULL_TREE;
6753 parser->qualifying_scope = NULL_TREE;
6754 parser->object_scope = NULL_TREE;
6756 if (parser->scope && name && BASELINK_P (name))
6757 adjust_result_of_qualified_name_lookup
6758 (name, parser->scope, scope);
6759 postfix_expression
6760 = finish_class_member_access_expr (postfix_expression, name,
6761 template_p,
6762 tf_warning_or_error);
6766 /* We no longer need to look up names in the scope of the object on
6767 the left-hand side of the `.' or `->' operator. */
6768 parser->context->object_type = NULL_TREE;
6770 /* Outside of offsetof, these operators may not appear in
6771 constant-expressions. */
6772 if (!for_offsetof
6773 && (cp_parser_non_integral_constant_expression
6774 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6775 postfix_expression = error_mark_node;
6777 return postfix_expression;
6780 /* Cache of LITERAL_ZERO_P constants. */
6782 static GTY(()) tree literal_zeros[itk_none];
6784 /* Parse a parenthesized expression-list.
6786 expression-list:
6787 assignment-expression
6788 expression-list, assignment-expression
6790 attribute-list:
6791 expression-list
6792 identifier
6793 identifier, expression-list
6795 CAST_P is true if this expression is the target of a cast.
6797 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6798 argument pack.
6800 Returns a vector of trees. Each element is a representation of an
6801 assignment-expression. NULL is returned if the ( and or ) are
6802 missing. An empty, but allocated, vector is returned on no
6803 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6804 if we are parsing an attribute list for an attribute that wants a
6805 plain identifier argument, normal_attr for an attribute that wants
6806 an expression, or non_attr if we aren't parsing an attribute list. If
6807 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6808 not all of the expressions in the list were constant.
6809 WANT_LITERAL_ZERO_P is true if the caller is interested in
6810 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6811 immediately, this can be removed. */
6813 static vec<tree, va_gc> *
6814 cp_parser_parenthesized_expression_list (cp_parser* parser,
6815 int is_attribute_list,
6816 bool cast_p,
6817 bool allow_expansion_p,
6818 bool *non_constant_p,
6819 bool want_literal_zero_p)
6821 vec<tree, va_gc> *expression_list;
6822 bool fold_expr_p = is_attribute_list != non_attr;
6823 tree identifier = NULL_TREE;
6824 bool saved_greater_than_is_operator_p;
6826 /* Assume all the expressions will be constant. */
6827 if (non_constant_p)
6828 *non_constant_p = false;
6830 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6831 return NULL;
6833 expression_list = make_tree_vector ();
6835 /* Within a parenthesized expression, a `>' token is always
6836 the greater-than operator. */
6837 saved_greater_than_is_operator_p
6838 = parser->greater_than_is_operator_p;
6839 parser->greater_than_is_operator_p = true;
6841 /* Consume expressions until there are no more. */
6842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6843 while (true)
6845 tree expr;
6847 /* At the beginning of attribute lists, check to see if the
6848 next token is an identifier. */
6849 if (is_attribute_list == id_attr
6850 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6852 cp_token *token;
6854 /* Consume the identifier. */
6855 token = cp_lexer_consume_token (parser->lexer);
6856 /* Save the identifier. */
6857 identifier = token->u.value;
6859 else
6861 bool expr_non_constant_p;
6863 /* Parse the next assignment-expression. */
6864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6866 /* A braced-init-list. */
6867 cp_lexer_set_source_position (parser->lexer);
6868 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6869 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6870 if (non_constant_p && expr_non_constant_p)
6871 *non_constant_p = true;
6873 else if (non_constant_p)
6875 expr = (cp_parser_constant_expression
6876 (parser, /*allow_non_constant_p=*/true,
6877 &expr_non_constant_p));
6878 if (expr_non_constant_p)
6879 *non_constant_p = true;
6881 else
6883 expr = NULL_TREE;
6884 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6885 switch (tok->type)
6887 case CPP_NUMBER:
6888 case CPP_CHAR:
6889 case CPP_WCHAR:
6890 case CPP_CHAR16:
6891 case CPP_CHAR32:
6892 case CPP_UTF8CHAR:
6893 /* If a parameter is literal zero alone, remember it
6894 for -Wmemset-transposed-args warning. */
6895 if (integer_zerop (tok->u.value)
6896 && !TREE_OVERFLOW (tok->u.value)
6897 && want_literal_zero_p
6898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6899 == CPP_COMMA
6900 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6901 == CPP_CLOSE_PAREN))
6903 unsigned int i;
6904 for (i = 0; i < itk_none; ++i)
6905 if (TREE_TYPE (tok->u.value) == integer_types[i])
6906 break;
6907 if (i < itk_none && literal_zeros[i])
6908 expr = literal_zeros[i];
6909 else
6911 expr = copy_node (tok->u.value);
6912 LITERAL_ZERO_P (expr) = 1;
6913 if (i < itk_none)
6914 literal_zeros[i] = expr;
6916 /* Consume the 0 token (or '\0', 0LL etc.). */
6917 cp_lexer_consume_token (parser->lexer);
6919 break;
6920 default:
6921 break;
6923 if (expr == NULL_TREE)
6924 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6925 cast_p);
6928 if (fold_expr_p)
6929 expr = instantiate_non_dependent_expr (expr);
6931 /* If we have an ellipsis, then this is an expression
6932 expansion. */
6933 if (allow_expansion_p
6934 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6936 /* Consume the `...'. */
6937 cp_lexer_consume_token (parser->lexer);
6939 /* Build the argument pack. */
6940 expr = make_pack_expansion (expr);
6943 /* Add it to the list. We add error_mark_node
6944 expressions to the list, so that we can still tell if
6945 the correct form for a parenthesized expression-list
6946 is found. That gives better errors. */
6947 vec_safe_push (expression_list, expr);
6949 if (expr == error_mark_node)
6950 goto skip_comma;
6953 /* After the first item, attribute lists look the same as
6954 expression lists. */
6955 is_attribute_list = non_attr;
6957 get_comma:;
6958 /* If the next token isn't a `,', then we are done. */
6959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6960 break;
6962 /* Otherwise, consume the `,' and keep going. */
6963 cp_lexer_consume_token (parser->lexer);
6966 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6968 int ending;
6970 skip_comma:;
6971 /* We try and resync to an unnested comma, as that will give the
6972 user better diagnostics. */
6973 ending = cp_parser_skip_to_closing_parenthesis (parser,
6974 /*recovering=*/true,
6975 /*or_comma=*/true,
6976 /*consume_paren=*/true);
6977 if (ending < 0)
6978 goto get_comma;
6979 if (!ending)
6981 parser->greater_than_is_operator_p
6982 = saved_greater_than_is_operator_p;
6983 return NULL;
6987 parser->greater_than_is_operator_p
6988 = saved_greater_than_is_operator_p;
6990 if (identifier)
6991 vec_safe_insert (expression_list, 0, identifier);
6993 return expression_list;
6996 /* Parse a pseudo-destructor-name.
6998 pseudo-destructor-name:
6999 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7000 :: [opt] nested-name-specifier template template-id :: ~ type-name
7001 :: [opt] nested-name-specifier [opt] ~ type-name
7003 If either of the first two productions is used, sets *SCOPE to the
7004 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7005 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7006 or ERROR_MARK_NODE if the parse fails. */
7008 static void
7009 cp_parser_pseudo_destructor_name (cp_parser* parser,
7010 tree object,
7011 tree* scope,
7012 tree* type)
7014 bool nested_name_specifier_p;
7016 /* Handle ~auto. */
7017 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7018 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7019 && !type_dependent_expression_p (object))
7021 if (cxx_dialect < cxx14)
7022 pedwarn (input_location, 0,
7023 "%<~auto%> only available with "
7024 "-std=c++14 or -std=gnu++14");
7025 cp_lexer_consume_token (parser->lexer);
7026 cp_lexer_consume_token (parser->lexer);
7027 *scope = NULL_TREE;
7028 *type = TREE_TYPE (object);
7029 return;
7032 /* Assume that things will not work out. */
7033 *type = error_mark_node;
7035 /* Look for the optional `::' operator. */
7036 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7037 /* Look for the optional nested-name-specifier. */
7038 nested_name_specifier_p
7039 = (cp_parser_nested_name_specifier_opt (parser,
7040 /*typename_keyword_p=*/false,
7041 /*check_dependency_p=*/true,
7042 /*type_p=*/false,
7043 /*is_declaration=*/false)
7044 != NULL_TREE);
7045 /* Now, if we saw a nested-name-specifier, we might be doing the
7046 second production. */
7047 if (nested_name_specifier_p
7048 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7050 /* Consume the `template' keyword. */
7051 cp_lexer_consume_token (parser->lexer);
7052 /* Parse the template-id. */
7053 cp_parser_template_id (parser,
7054 /*template_keyword_p=*/true,
7055 /*check_dependency_p=*/false,
7056 class_type,
7057 /*is_declaration=*/true);
7058 /* Look for the `::' token. */
7059 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7061 /* If the next token is not a `~', then there might be some
7062 additional qualification. */
7063 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7065 /* At this point, we're looking for "type-name :: ~". The type-name
7066 must not be a class-name, since this is a pseudo-destructor. So,
7067 it must be either an enum-name, or a typedef-name -- both of which
7068 are just identifiers. So, we peek ahead to check that the "::"
7069 and "~" tokens are present; if they are not, then we can avoid
7070 calling type_name. */
7071 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7072 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7073 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7075 cp_parser_error (parser, "non-scalar type");
7076 return;
7079 /* Look for the type-name. */
7080 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7081 if (*scope == error_mark_node)
7082 return;
7084 /* Look for the `::' token. */
7085 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7087 else
7088 *scope = NULL_TREE;
7090 /* Look for the `~'. */
7091 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7093 /* Once we see the ~, this has to be a pseudo-destructor. */
7094 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7095 cp_parser_commit_to_topmost_tentative_parse (parser);
7097 /* Look for the type-name again. We are not responsible for
7098 checking that it matches the first type-name. */
7099 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7102 /* Parse a unary-expression.
7104 unary-expression:
7105 postfix-expression
7106 ++ cast-expression
7107 -- cast-expression
7108 unary-operator cast-expression
7109 sizeof unary-expression
7110 sizeof ( type-id )
7111 alignof ( type-id ) [C++0x]
7112 new-expression
7113 delete-expression
7115 GNU Extensions:
7117 unary-expression:
7118 __extension__ cast-expression
7119 __alignof__ unary-expression
7120 __alignof__ ( type-id )
7121 alignof unary-expression [C++0x]
7122 __real__ cast-expression
7123 __imag__ cast-expression
7124 && identifier
7125 sizeof ( type-id ) { initializer-list , [opt] }
7126 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7127 __alignof__ ( type-id ) { initializer-list , [opt] }
7129 ADDRESS_P is true iff the unary-expression is appearing as the
7130 operand of the `&' operator. CAST_P is true if this expression is
7131 the target of a cast.
7133 Returns a representation of the expression. */
7135 static tree
7136 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7137 bool address_p, bool cast_p, bool decltype_p)
7139 cp_token *token;
7140 enum tree_code unary_operator;
7142 /* Peek at the next token. */
7143 token = cp_lexer_peek_token (parser->lexer);
7144 /* Some keywords give away the kind of expression. */
7145 if (token->type == CPP_KEYWORD)
7147 enum rid keyword = token->keyword;
7149 switch (keyword)
7151 case RID_ALIGNOF:
7152 case RID_SIZEOF:
7154 tree operand, ret;
7155 enum tree_code op;
7156 location_t first_loc;
7158 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7159 /* Consume the token. */
7160 cp_lexer_consume_token (parser->lexer);
7161 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7162 /* Parse the operand. */
7163 operand = cp_parser_sizeof_operand (parser, keyword);
7165 if (TYPE_P (operand))
7166 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7167 else
7169 /* ISO C++ defines alignof only with types, not with
7170 expressions. So pedwarn if alignof is used with a non-
7171 type expression. However, __alignof__ is ok. */
7172 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7173 pedwarn (token->location, OPT_Wpedantic,
7174 "ISO C++ does not allow %<alignof%> "
7175 "with a non-type");
7177 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7179 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7180 SIZEOF_EXPR with the original operand. */
7181 if (op == SIZEOF_EXPR && ret != error_mark_node)
7183 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7185 if (!processing_template_decl && TYPE_P (operand))
7187 ret = build_min (SIZEOF_EXPR, size_type_node,
7188 build1 (NOP_EXPR, operand,
7189 error_mark_node));
7190 SIZEOF_EXPR_TYPE_P (ret) = 1;
7192 else
7193 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7194 TREE_SIDE_EFFECTS (ret) = 0;
7195 TREE_READONLY (ret) = 1;
7197 SET_EXPR_LOCATION (ret, first_loc);
7199 return ret;
7202 case RID_NEW:
7203 return cp_parser_new_expression (parser);
7205 case RID_DELETE:
7206 return cp_parser_delete_expression (parser);
7208 case RID_EXTENSION:
7210 /* The saved value of the PEDANTIC flag. */
7211 int saved_pedantic;
7212 tree expr;
7214 /* Save away the PEDANTIC flag. */
7215 cp_parser_extension_opt (parser, &saved_pedantic);
7216 /* Parse the cast-expression. */
7217 expr = cp_parser_simple_cast_expression (parser);
7218 /* Restore the PEDANTIC flag. */
7219 pedantic = saved_pedantic;
7221 return expr;
7224 case RID_REALPART:
7225 case RID_IMAGPART:
7227 tree expression;
7229 /* Consume the `__real__' or `__imag__' token. */
7230 cp_lexer_consume_token (parser->lexer);
7231 /* Parse the cast-expression. */
7232 expression = cp_parser_simple_cast_expression (parser);
7233 /* Create the complete representation. */
7234 return build_x_unary_op (token->location,
7235 (keyword == RID_REALPART
7236 ? REALPART_EXPR : IMAGPART_EXPR),
7237 expression,
7238 tf_warning_or_error);
7240 break;
7242 case RID_TRANSACTION_ATOMIC:
7243 case RID_TRANSACTION_RELAXED:
7244 return cp_parser_transaction_expression (parser, keyword);
7246 case RID_NOEXCEPT:
7248 tree expr;
7249 const char *saved_message;
7250 bool saved_integral_constant_expression_p;
7251 bool saved_non_integral_constant_expression_p;
7252 bool saved_greater_than_is_operator_p;
7254 cp_lexer_consume_token (parser->lexer);
7255 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7257 saved_message = parser->type_definition_forbidden_message;
7258 parser->type_definition_forbidden_message
7259 = G_("types may not be defined in %<noexcept%> expressions");
7261 saved_integral_constant_expression_p
7262 = parser->integral_constant_expression_p;
7263 saved_non_integral_constant_expression_p
7264 = parser->non_integral_constant_expression_p;
7265 parser->integral_constant_expression_p = false;
7267 saved_greater_than_is_operator_p
7268 = parser->greater_than_is_operator_p;
7269 parser->greater_than_is_operator_p = true;
7271 ++cp_unevaluated_operand;
7272 ++c_inhibit_evaluation_warnings;
7273 ++cp_noexcept_operand;
7274 expr = cp_parser_expression (parser);
7275 --cp_noexcept_operand;
7276 --c_inhibit_evaluation_warnings;
7277 --cp_unevaluated_operand;
7279 parser->greater_than_is_operator_p
7280 = saved_greater_than_is_operator_p;
7282 parser->integral_constant_expression_p
7283 = saved_integral_constant_expression_p;
7284 parser->non_integral_constant_expression_p
7285 = saved_non_integral_constant_expression_p;
7287 parser->type_definition_forbidden_message = saved_message;
7289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7290 return finish_noexcept_expr (expr, tf_warning_or_error);
7293 default:
7294 break;
7298 /* Look for the `:: new' and `:: delete', which also signal the
7299 beginning of a new-expression, or delete-expression,
7300 respectively. If the next token is `::', then it might be one of
7301 these. */
7302 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7304 enum rid keyword;
7306 /* See if the token after the `::' is one of the keywords in
7307 which we're interested. */
7308 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7309 /* If it's `new', we have a new-expression. */
7310 if (keyword == RID_NEW)
7311 return cp_parser_new_expression (parser);
7312 /* Similarly, for `delete'. */
7313 else if (keyword == RID_DELETE)
7314 return cp_parser_delete_expression (parser);
7317 /* Look for a unary operator. */
7318 unary_operator = cp_parser_unary_operator (token);
7319 /* The `++' and `--' operators can be handled similarly, even though
7320 they are not technically unary-operators in the grammar. */
7321 if (unary_operator == ERROR_MARK)
7323 if (token->type == CPP_PLUS_PLUS)
7324 unary_operator = PREINCREMENT_EXPR;
7325 else if (token->type == CPP_MINUS_MINUS)
7326 unary_operator = PREDECREMENT_EXPR;
7327 /* Handle the GNU address-of-label extension. */
7328 else if (cp_parser_allow_gnu_extensions_p (parser)
7329 && token->type == CPP_AND_AND)
7331 tree identifier;
7332 tree expression;
7333 location_t loc = token->location;
7335 /* Consume the '&&' token. */
7336 cp_lexer_consume_token (parser->lexer);
7337 /* Look for the identifier. */
7338 identifier = cp_parser_identifier (parser);
7339 /* Create an expression representing the address. */
7340 expression = finish_label_address_expr (identifier, loc);
7341 if (cp_parser_non_integral_constant_expression (parser,
7342 NIC_ADDR_LABEL))
7343 expression = error_mark_node;
7344 return expression;
7347 if (unary_operator != ERROR_MARK)
7349 tree cast_expression;
7350 tree expression = error_mark_node;
7351 non_integral_constant non_constant_p = NIC_NONE;
7352 location_t loc = token->location;
7353 tsubst_flags_t complain = complain_flags (decltype_p);
7355 /* Consume the operator token. */
7356 token = cp_lexer_consume_token (parser->lexer);
7357 /* Parse the cast-expression. */
7358 cast_expression
7359 = cp_parser_cast_expression (parser,
7360 unary_operator == ADDR_EXPR,
7361 /*cast_p=*/false,
7362 /*decltype*/false,
7363 pidk);
7364 /* Now, build an appropriate representation. */
7365 switch (unary_operator)
7367 case INDIRECT_REF:
7368 non_constant_p = NIC_STAR;
7369 expression = build_x_indirect_ref (loc, cast_expression,
7370 RO_UNARY_STAR,
7371 complain);
7372 break;
7374 case ADDR_EXPR:
7375 non_constant_p = NIC_ADDR;
7376 /* Fall through. */
7377 case BIT_NOT_EXPR:
7378 expression = build_x_unary_op (loc, unary_operator,
7379 cast_expression,
7380 complain);
7381 break;
7383 case PREINCREMENT_EXPR:
7384 case PREDECREMENT_EXPR:
7385 non_constant_p = unary_operator == PREINCREMENT_EXPR
7386 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7387 /* Fall through. */
7388 case UNARY_PLUS_EXPR:
7389 case NEGATE_EXPR:
7390 case TRUTH_NOT_EXPR:
7391 expression = finish_unary_op_expr (loc, unary_operator,
7392 cast_expression, complain);
7393 break;
7395 default:
7396 gcc_unreachable ();
7399 if (non_constant_p != NIC_NONE
7400 && cp_parser_non_integral_constant_expression (parser,
7401 non_constant_p))
7402 expression = error_mark_node;
7404 return expression;
7407 return cp_parser_postfix_expression (parser, address_p, cast_p,
7408 /*member_access_only_p=*/false,
7409 decltype_p,
7410 pidk);
7413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7414 unary-operator, the corresponding tree code is returned. */
7416 static enum tree_code
7417 cp_parser_unary_operator (cp_token* token)
7419 switch (token->type)
7421 case CPP_MULT:
7422 return INDIRECT_REF;
7424 case CPP_AND:
7425 return ADDR_EXPR;
7427 case CPP_PLUS:
7428 return UNARY_PLUS_EXPR;
7430 case CPP_MINUS:
7431 return NEGATE_EXPR;
7433 case CPP_NOT:
7434 return TRUTH_NOT_EXPR;
7436 case CPP_COMPL:
7437 return BIT_NOT_EXPR;
7439 default:
7440 return ERROR_MARK;
7444 /* Parse a new-expression.
7446 new-expression:
7447 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7448 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7450 Returns a representation of the expression. */
7452 static tree
7453 cp_parser_new_expression (cp_parser* parser)
7455 bool global_scope_p;
7456 vec<tree, va_gc> *placement;
7457 tree type;
7458 vec<tree, va_gc> *initializer;
7459 tree nelts = NULL_TREE;
7460 tree ret;
7462 /* Look for the optional `::' operator. */
7463 global_scope_p
7464 = (cp_parser_global_scope_opt (parser,
7465 /*current_scope_valid_p=*/false)
7466 != NULL_TREE);
7467 /* Look for the `new' operator. */
7468 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7469 /* There's no easy way to tell a new-placement from the
7470 `( type-id )' construct. */
7471 cp_parser_parse_tentatively (parser);
7472 /* Look for a new-placement. */
7473 placement = cp_parser_new_placement (parser);
7474 /* If that didn't work out, there's no new-placement. */
7475 if (!cp_parser_parse_definitely (parser))
7477 if (placement != NULL)
7478 release_tree_vector (placement);
7479 placement = NULL;
7482 /* If the next token is a `(', then we have a parenthesized
7483 type-id. */
7484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7486 cp_token *token;
7487 const char *saved_message = parser->type_definition_forbidden_message;
7489 /* Consume the `('. */
7490 cp_lexer_consume_token (parser->lexer);
7492 /* Parse the type-id. */
7493 parser->type_definition_forbidden_message
7494 = G_("types may not be defined in a new-expression");
7495 type = cp_parser_type_id (parser);
7496 parser->type_definition_forbidden_message = saved_message;
7498 /* Look for the closing `)'. */
7499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7500 token = cp_lexer_peek_token (parser->lexer);
7501 /* There should not be a direct-new-declarator in this production,
7502 but GCC used to allowed this, so we check and emit a sensible error
7503 message for this case. */
7504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7506 error_at (token->location,
7507 "array bound forbidden after parenthesized type-id");
7508 inform (token->location,
7509 "try removing the parentheses around the type-id");
7510 cp_parser_direct_new_declarator (parser);
7513 /* Otherwise, there must be a new-type-id. */
7514 else
7515 type = cp_parser_new_type_id (parser, &nelts);
7517 /* If the next token is a `(' or '{', then we have a new-initializer. */
7518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7519 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7520 initializer = cp_parser_new_initializer (parser);
7521 else
7522 initializer = NULL;
7524 /* A new-expression may not appear in an integral constant
7525 expression. */
7526 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7527 ret = error_mark_node;
7528 else
7530 /* Create a representation of the new-expression. */
7531 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7532 tf_warning_or_error);
7535 if (placement != NULL)
7536 release_tree_vector (placement);
7537 if (initializer != NULL)
7538 release_tree_vector (initializer);
7540 return ret;
7543 /* Parse a new-placement.
7545 new-placement:
7546 ( expression-list )
7548 Returns the same representation as for an expression-list. */
7550 static vec<tree, va_gc> *
7551 cp_parser_new_placement (cp_parser* parser)
7553 vec<tree, va_gc> *expression_list;
7555 /* Parse the expression-list. */
7556 expression_list = (cp_parser_parenthesized_expression_list
7557 (parser, non_attr, /*cast_p=*/false,
7558 /*allow_expansion_p=*/true,
7559 /*non_constant_p=*/NULL));
7561 if (expression_list && expression_list->is_empty ())
7562 error ("expected expression-list or type-id");
7564 return expression_list;
7567 /* Parse a new-type-id.
7569 new-type-id:
7570 type-specifier-seq new-declarator [opt]
7572 Returns the TYPE allocated. If the new-type-id indicates an array
7573 type, *NELTS is set to the number of elements in the last array
7574 bound; the TYPE will not include the last array bound. */
7576 static tree
7577 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7579 cp_decl_specifier_seq type_specifier_seq;
7580 cp_declarator *new_declarator;
7581 cp_declarator *declarator;
7582 cp_declarator *outer_declarator;
7583 const char *saved_message;
7585 /* The type-specifier sequence must not contain type definitions.
7586 (It cannot contain declarations of new types either, but if they
7587 are not definitions we will catch that because they are not
7588 complete.) */
7589 saved_message = parser->type_definition_forbidden_message;
7590 parser->type_definition_forbidden_message
7591 = G_("types may not be defined in a new-type-id");
7592 /* Parse the type-specifier-seq. */
7593 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7594 /*is_trailing_return=*/false,
7595 &type_specifier_seq);
7596 /* Restore the old message. */
7597 parser->type_definition_forbidden_message = saved_message;
7599 if (type_specifier_seq.type == error_mark_node)
7600 return error_mark_node;
7602 /* Parse the new-declarator. */
7603 new_declarator = cp_parser_new_declarator_opt (parser);
7605 /* Determine the number of elements in the last array dimension, if
7606 any. */
7607 *nelts = NULL_TREE;
7608 /* Skip down to the last array dimension. */
7609 declarator = new_declarator;
7610 outer_declarator = NULL;
7611 while (declarator && (declarator->kind == cdk_pointer
7612 || declarator->kind == cdk_ptrmem))
7614 outer_declarator = declarator;
7615 declarator = declarator->declarator;
7617 while (declarator
7618 && declarator->kind == cdk_array
7619 && declarator->declarator
7620 && declarator->declarator->kind == cdk_array)
7622 outer_declarator = declarator;
7623 declarator = declarator->declarator;
7626 if (declarator && declarator->kind == cdk_array)
7628 *nelts = declarator->u.array.bounds;
7629 if (*nelts == error_mark_node)
7630 *nelts = integer_one_node;
7632 if (outer_declarator)
7633 outer_declarator->declarator = declarator->declarator;
7634 else
7635 new_declarator = NULL;
7638 return groktypename (&type_specifier_seq, new_declarator, false);
7641 /* Parse an (optional) new-declarator.
7643 new-declarator:
7644 ptr-operator new-declarator [opt]
7645 direct-new-declarator
7647 Returns the declarator. */
7649 static cp_declarator *
7650 cp_parser_new_declarator_opt (cp_parser* parser)
7652 enum tree_code code;
7653 tree type, std_attributes = NULL_TREE;
7654 cp_cv_quals cv_quals;
7656 /* We don't know if there's a ptr-operator next, or not. */
7657 cp_parser_parse_tentatively (parser);
7658 /* Look for a ptr-operator. */
7659 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7660 /* If that worked, look for more new-declarators. */
7661 if (cp_parser_parse_definitely (parser))
7663 cp_declarator *declarator;
7665 /* Parse another optional declarator. */
7666 declarator = cp_parser_new_declarator_opt (parser);
7668 declarator = cp_parser_make_indirect_declarator
7669 (code, type, cv_quals, declarator, std_attributes);
7671 return declarator;
7674 /* If the next token is a `[', there is a direct-new-declarator. */
7675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7676 return cp_parser_direct_new_declarator (parser);
7678 return NULL;
7681 /* Parse a direct-new-declarator.
7683 direct-new-declarator:
7684 [ expression ]
7685 direct-new-declarator [constant-expression]
7689 static cp_declarator *
7690 cp_parser_direct_new_declarator (cp_parser* parser)
7692 cp_declarator *declarator = NULL;
7694 while (true)
7696 tree expression;
7697 cp_token *token;
7699 /* Look for the opening `['. */
7700 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7702 token = cp_lexer_peek_token (parser->lexer);
7703 expression = cp_parser_expression (parser);
7704 /* The standard requires that the expression have integral
7705 type. DR 74 adds enumeration types. We believe that the
7706 real intent is that these expressions be handled like the
7707 expression in a `switch' condition, which also allows
7708 classes with a single conversion to integral or
7709 enumeration type. */
7710 if (!processing_template_decl)
7712 expression
7713 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7714 expression,
7715 /*complain=*/true);
7716 if (!expression)
7718 error_at (token->location,
7719 "expression in new-declarator must have integral "
7720 "or enumeration type");
7721 expression = error_mark_node;
7725 /* Look for the closing `]'. */
7726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7728 /* Add this bound to the declarator. */
7729 declarator = make_array_declarator (declarator, expression);
7731 /* If the next token is not a `[', then there are no more
7732 bounds. */
7733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7734 break;
7737 return declarator;
7740 /* Parse a new-initializer.
7742 new-initializer:
7743 ( expression-list [opt] )
7744 braced-init-list
7746 Returns a representation of the expression-list. */
7748 static vec<tree, va_gc> *
7749 cp_parser_new_initializer (cp_parser* parser)
7751 vec<tree, va_gc> *expression_list;
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7755 tree t;
7756 bool expr_non_constant_p;
7757 cp_lexer_set_source_position (parser->lexer);
7758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7759 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7760 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7761 expression_list = make_tree_vector_single (t);
7763 else
7764 expression_list = (cp_parser_parenthesized_expression_list
7765 (parser, non_attr, /*cast_p=*/false,
7766 /*allow_expansion_p=*/true,
7767 /*non_constant_p=*/NULL));
7769 return expression_list;
7772 /* Parse a delete-expression.
7774 delete-expression:
7775 :: [opt] delete cast-expression
7776 :: [opt] delete [ ] cast-expression
7778 Returns a representation of the expression. */
7780 static tree
7781 cp_parser_delete_expression (cp_parser* parser)
7783 bool global_scope_p;
7784 bool array_p;
7785 tree expression;
7787 /* Look for the optional `::' operator. */
7788 global_scope_p
7789 = (cp_parser_global_scope_opt (parser,
7790 /*current_scope_valid_p=*/false)
7791 != NULL_TREE);
7792 /* Look for the `delete' keyword. */
7793 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7794 /* See if the array syntax is in use. */
7795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7797 /* Consume the `[' token. */
7798 cp_lexer_consume_token (parser->lexer);
7799 /* Look for the `]' token. */
7800 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7801 /* Remember that this is the `[]' construct. */
7802 array_p = true;
7804 else
7805 array_p = false;
7807 /* Parse the cast-expression. */
7808 expression = cp_parser_simple_cast_expression (parser);
7810 /* A delete-expression may not appear in an integral constant
7811 expression. */
7812 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7813 return error_mark_node;
7815 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7816 tf_warning_or_error);
7819 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7820 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7821 0 otherwise. */
7823 static int
7824 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7826 cp_token *token = cp_lexer_peek_token (parser->lexer);
7827 switch (token->type)
7829 case CPP_COMMA:
7830 case CPP_SEMICOLON:
7831 case CPP_QUERY:
7832 case CPP_COLON:
7833 case CPP_CLOSE_SQUARE:
7834 case CPP_CLOSE_PAREN:
7835 case CPP_CLOSE_BRACE:
7836 case CPP_OPEN_BRACE:
7837 case CPP_DOT:
7838 case CPP_DOT_STAR:
7839 case CPP_DEREF:
7840 case CPP_DEREF_STAR:
7841 case CPP_DIV:
7842 case CPP_MOD:
7843 case CPP_LSHIFT:
7844 case CPP_RSHIFT:
7845 case CPP_LESS:
7846 case CPP_GREATER:
7847 case CPP_LESS_EQ:
7848 case CPP_GREATER_EQ:
7849 case CPP_EQ_EQ:
7850 case CPP_NOT_EQ:
7851 case CPP_EQ:
7852 case CPP_MULT_EQ:
7853 case CPP_DIV_EQ:
7854 case CPP_MOD_EQ:
7855 case CPP_PLUS_EQ:
7856 case CPP_MINUS_EQ:
7857 case CPP_RSHIFT_EQ:
7858 case CPP_LSHIFT_EQ:
7859 case CPP_AND_EQ:
7860 case CPP_XOR_EQ:
7861 case CPP_OR_EQ:
7862 case CPP_XOR:
7863 case CPP_OR:
7864 case CPP_OR_OR:
7865 case CPP_EOF:
7866 case CPP_ELLIPSIS:
7867 return 0;
7869 case CPP_OPEN_PAREN:
7870 /* In ((type ()) () the last () isn't a valid cast-expression,
7871 so the whole must be parsed as postfix-expression. */
7872 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7873 != CPP_CLOSE_PAREN;
7875 case CPP_OPEN_SQUARE:
7876 /* '[' may start a primary-expression in obj-c++ and in C++11,
7877 as a lambda-expression, eg, '(void)[]{}'. */
7878 if (cxx_dialect >= cxx11)
7879 return -1;
7880 return c_dialect_objc ();
7882 case CPP_PLUS_PLUS:
7883 case CPP_MINUS_MINUS:
7884 /* '++' and '--' may or may not start a cast-expression:
7886 struct T { void operator++(int); };
7887 void f() { (T())++; }
7891 int a;
7892 (int)++a; */
7893 return -1;
7895 default:
7896 return 1;
7900 /* Parse a cast-expression.
7902 cast-expression:
7903 unary-expression
7904 ( type-id ) cast-expression
7906 ADDRESS_P is true iff the unary-expression is appearing as the
7907 operand of the `&' operator. CAST_P is true if this expression is
7908 the target of a cast.
7910 Returns a representation of the expression. */
7912 static tree
7913 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7914 bool decltype_p, cp_id_kind * pidk)
7916 /* If it's a `(', then we might be looking at a cast. */
7917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7919 tree type = NULL_TREE;
7920 tree expr = NULL_TREE;
7921 int cast_expression = 0;
7922 const char *saved_message;
7924 /* There's no way to know yet whether or not this is a cast.
7925 For example, `(int (3))' is a unary-expression, while `(int)
7926 3' is a cast. So, we resort to parsing tentatively. */
7927 cp_parser_parse_tentatively (parser);
7928 /* Types may not be defined in a cast. */
7929 saved_message = parser->type_definition_forbidden_message;
7930 parser->type_definition_forbidden_message
7931 = G_("types may not be defined in casts");
7932 /* Consume the `('. */
7933 cp_lexer_consume_token (parser->lexer);
7934 /* A very tricky bit is that `(struct S) { 3 }' is a
7935 compound-literal (which we permit in C++ as an extension).
7936 But, that construct is not a cast-expression -- it is a
7937 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7938 is legal; if the compound-literal were a cast-expression,
7939 you'd need an extra set of parentheses.) But, if we parse
7940 the type-id, and it happens to be a class-specifier, then we
7941 will commit to the parse at that point, because we cannot
7942 undo the action that is done when creating a new class. So,
7943 then we cannot back up and do a postfix-expression.
7945 Another tricky case is the following (c++/29234):
7947 struct S { void operator () (); };
7949 void foo ()
7951 ( S()() );
7954 As a type-id we parse the parenthesized S()() as a function
7955 returning a function, groktypename complains and we cannot
7956 back up in this case either.
7958 Therefore, we scan ahead to the closing `)', and check to see
7959 if the tokens after the `)' can start a cast-expression. Otherwise
7960 we are dealing with an unary-expression, a postfix-expression
7961 or something else.
7963 Yet another tricky case, in C++11, is the following (c++/54891):
7965 (void)[]{};
7967 The issue is that usually, besides the case of lambda-expressions,
7968 the parenthesized type-id cannot be followed by '[', and, eg, we
7969 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7970 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7971 we don't commit, we try a cast-expression, then an unary-expression.
7973 Save tokens so that we can put them back. */
7974 cp_lexer_save_tokens (parser->lexer);
7976 /* We may be looking at a cast-expression. */
7977 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7978 /*consume_paren=*/true))
7979 cast_expression
7980 = cp_parser_tokens_start_cast_expression (parser);
7982 /* Roll back the tokens we skipped. */
7983 cp_lexer_rollback_tokens (parser->lexer);
7984 /* If we aren't looking at a cast-expression, simulate an error so
7985 that the call to cp_parser_error_occurred below returns true. */
7986 if (!cast_expression)
7987 cp_parser_simulate_error (parser);
7988 else
7990 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7991 parser->in_type_id_in_expr_p = true;
7992 /* Look for the type-id. */
7993 type = cp_parser_type_id (parser);
7994 /* Look for the closing `)'. */
7995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7996 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7999 /* Restore the saved message. */
8000 parser->type_definition_forbidden_message = saved_message;
8002 /* At this point this can only be either a cast or a
8003 parenthesized ctor such as `(T ())' that looks like a cast to
8004 function returning T. */
8005 if (!cp_parser_error_occurred (parser))
8007 /* Only commit if the cast-expression doesn't start with
8008 '++', '--', or '[' in C++11. */
8009 if (cast_expression > 0)
8010 cp_parser_commit_to_topmost_tentative_parse (parser);
8012 expr = cp_parser_cast_expression (parser,
8013 /*address_p=*/false,
8014 /*cast_p=*/true,
8015 /*decltype_p=*/false,
8016 pidk);
8018 if (cp_parser_parse_definitely (parser))
8020 /* Warn about old-style casts, if so requested. */
8021 if (warn_old_style_cast
8022 && !in_system_header_at (input_location)
8023 && !VOID_TYPE_P (type)
8024 && current_lang_name != lang_name_c)
8025 warning (OPT_Wold_style_cast, "use of old-style cast");
8027 /* Only type conversions to integral or enumeration types
8028 can be used in constant-expressions. */
8029 if (!cast_valid_in_integral_constant_expression_p (type)
8030 && cp_parser_non_integral_constant_expression (parser,
8031 NIC_CAST))
8032 return error_mark_node;
8034 /* Perform the cast. */
8035 expr = build_c_cast (input_location, type, expr);
8036 return expr;
8039 else
8040 cp_parser_abort_tentative_parse (parser);
8043 /* If we get here, then it's not a cast, so it must be a
8044 unary-expression. */
8045 return cp_parser_unary_expression (parser, pidk, address_p,
8046 cast_p, decltype_p);
8049 /* Parse a binary expression of the general form:
8051 pm-expression:
8052 cast-expression
8053 pm-expression .* cast-expression
8054 pm-expression ->* cast-expression
8056 multiplicative-expression:
8057 pm-expression
8058 multiplicative-expression * pm-expression
8059 multiplicative-expression / pm-expression
8060 multiplicative-expression % pm-expression
8062 additive-expression:
8063 multiplicative-expression
8064 additive-expression + multiplicative-expression
8065 additive-expression - multiplicative-expression
8067 shift-expression:
8068 additive-expression
8069 shift-expression << additive-expression
8070 shift-expression >> additive-expression
8072 relational-expression:
8073 shift-expression
8074 relational-expression < shift-expression
8075 relational-expression > shift-expression
8076 relational-expression <= shift-expression
8077 relational-expression >= shift-expression
8079 GNU Extension:
8081 relational-expression:
8082 relational-expression <? shift-expression
8083 relational-expression >? shift-expression
8085 equality-expression:
8086 relational-expression
8087 equality-expression == relational-expression
8088 equality-expression != relational-expression
8090 and-expression:
8091 equality-expression
8092 and-expression & equality-expression
8094 exclusive-or-expression:
8095 and-expression
8096 exclusive-or-expression ^ and-expression
8098 inclusive-or-expression:
8099 exclusive-or-expression
8100 inclusive-or-expression | exclusive-or-expression
8102 logical-and-expression:
8103 inclusive-or-expression
8104 logical-and-expression && inclusive-or-expression
8106 logical-or-expression:
8107 logical-and-expression
8108 logical-or-expression || logical-and-expression
8110 All these are implemented with a single function like:
8112 binary-expression:
8113 simple-cast-expression
8114 binary-expression <token> binary-expression
8116 CAST_P is true if this expression is the target of a cast.
8118 The binops_by_token map is used to get the tree codes for each <token> type.
8119 binary-expressions are associated according to a precedence table. */
8121 #define TOKEN_PRECEDENCE(token) \
8122 (((token->type == CPP_GREATER \
8123 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8124 && !parser->greater_than_is_operator_p) \
8125 ? PREC_NOT_OPERATOR \
8126 : binops_by_token[token->type].prec)
8128 static tree
8129 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8130 bool no_toplevel_fold_p,
8131 bool decltype_p,
8132 enum cp_parser_prec prec,
8133 cp_id_kind * pidk)
8135 cp_parser_expression_stack stack;
8136 cp_parser_expression_stack_entry *sp = &stack[0];
8137 cp_parser_expression_stack_entry current;
8138 tree rhs;
8139 cp_token *token;
8140 enum tree_code rhs_type;
8141 enum cp_parser_prec new_prec, lookahead_prec;
8142 tree overload;
8144 /* Parse the first expression. */
8145 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8146 ? TRUTH_NOT_EXPR : ERROR_MARK);
8147 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8148 cast_p, decltype_p, pidk);
8149 current.prec = prec;
8151 if (cp_parser_error_occurred (parser))
8152 return error_mark_node;
8154 for (;;)
8156 /* Get an operator token. */
8157 token = cp_lexer_peek_token (parser->lexer);
8159 if (warn_cxx11_compat
8160 && token->type == CPP_RSHIFT
8161 && !parser->greater_than_is_operator_p)
8163 if (warning_at (token->location, OPT_Wc__11_compat,
8164 "%<>>%> operator is treated"
8165 " as two right angle brackets in C++11"))
8166 inform (token->location,
8167 "suggest parentheses around %<>>%> expression");
8170 new_prec = TOKEN_PRECEDENCE (token);
8172 /* Popping an entry off the stack means we completed a subexpression:
8173 - either we found a token which is not an operator (`>' where it is not
8174 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8175 will happen repeatedly;
8176 - or, we found an operator which has lower priority. This is the case
8177 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8178 parsing `3 * 4'. */
8179 if (new_prec <= current.prec)
8181 if (sp == stack)
8182 break;
8183 else
8184 goto pop;
8187 get_rhs:
8188 current.tree_type = binops_by_token[token->type].tree_type;
8189 current.loc = token->location;
8191 /* We used the operator token. */
8192 cp_lexer_consume_token (parser->lexer);
8194 /* For "false && x" or "true || x", x will never be executed;
8195 disable warnings while evaluating it. */
8196 if (current.tree_type == TRUTH_ANDIF_EXPR)
8197 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8198 else if (current.tree_type == TRUTH_ORIF_EXPR)
8199 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8201 /* Extract another operand. It may be the RHS of this expression
8202 or the LHS of a new, higher priority expression. */
8203 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8204 ? TRUTH_NOT_EXPR : ERROR_MARK);
8205 rhs = cp_parser_simple_cast_expression (parser);
8207 /* Get another operator token. Look up its precedence to avoid
8208 building a useless (immediately popped) stack entry for common
8209 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8210 token = cp_lexer_peek_token (parser->lexer);
8211 lookahead_prec = TOKEN_PRECEDENCE (token);
8212 if (lookahead_prec > new_prec)
8214 /* ... and prepare to parse the RHS of the new, higher priority
8215 expression. Since precedence levels on the stack are
8216 monotonically increasing, we do not have to care about
8217 stack overflows. */
8218 *sp = current;
8219 ++sp;
8220 current.lhs = rhs;
8221 current.lhs_type = rhs_type;
8222 current.prec = new_prec;
8223 new_prec = lookahead_prec;
8224 goto get_rhs;
8226 pop:
8227 lookahead_prec = new_prec;
8228 /* If the stack is not empty, we have parsed into LHS the right side
8229 (`4' in the example above) of an expression we had suspended.
8230 We can use the information on the stack to recover the LHS (`3')
8231 from the stack together with the tree code (`MULT_EXPR'), and
8232 the precedence of the higher level subexpression
8233 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8234 which will be used to actually build the additive expression. */
8235 rhs = current.lhs;
8236 rhs_type = current.lhs_type;
8237 --sp;
8238 current = *sp;
8241 /* Undo the disabling of warnings done above. */
8242 if (current.tree_type == TRUTH_ANDIF_EXPR)
8243 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8244 else if (current.tree_type == TRUTH_ORIF_EXPR)
8245 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8247 if (warn_logical_not_paren
8248 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8249 && current.lhs_type == TRUTH_NOT_EXPR
8250 /* Avoid warning for !!x == y. */
8251 && (TREE_CODE (current.lhs) != NE_EXPR
8252 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8253 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8254 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8255 /* Avoid warning for !b == y where b is boolean. */
8256 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8257 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8258 != BOOLEAN_TYPE))))
8259 /* Avoid warning for !!b == y where b is boolean. */
8260 && (!DECL_P (current.lhs)
8261 || TREE_TYPE (current.lhs) == NULL_TREE
8262 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8263 warn_logical_not_parentheses (current.loc, current.tree_type,
8264 maybe_constant_value (rhs));
8266 overload = NULL;
8267 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8268 ERROR_MARK for everything that is not a binary expression.
8269 This makes warn_about_parentheses miss some warnings that
8270 involve unary operators. For unary expressions we should
8271 pass the correct tree_code unless the unary expression was
8272 surrounded by parentheses.
8274 if (no_toplevel_fold_p
8275 && lookahead_prec <= current.prec
8276 && sp == stack)
8277 current.lhs = build2 (current.tree_type,
8278 TREE_CODE_CLASS (current.tree_type)
8279 == tcc_comparison
8280 ? boolean_type_node : TREE_TYPE (current.lhs),
8281 current.lhs, rhs);
8282 else
8283 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8284 current.lhs, current.lhs_type,
8285 rhs, rhs_type, &overload,
8286 complain_flags (decltype_p));
8287 current.lhs_type = current.tree_type;
8288 if (EXPR_P (current.lhs))
8289 SET_EXPR_LOCATION (current.lhs, current.loc);
8291 /* If the binary operator required the use of an overloaded operator,
8292 then this expression cannot be an integral constant-expression.
8293 An overloaded operator can be used even if both operands are
8294 otherwise permissible in an integral constant-expression if at
8295 least one of the operands is of enumeration type. */
8297 if (overload
8298 && cp_parser_non_integral_constant_expression (parser,
8299 NIC_OVERLOADED))
8300 return error_mark_node;
8303 return current.lhs;
8306 static tree
8307 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8308 bool no_toplevel_fold_p,
8309 enum cp_parser_prec prec,
8310 cp_id_kind * pidk)
8312 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8313 /*decltype*/false, prec, pidk);
8316 /* Parse the `? expression : assignment-expression' part of a
8317 conditional-expression. The LOGICAL_OR_EXPR is the
8318 logical-or-expression that started the conditional-expression.
8319 Returns a representation of the entire conditional-expression.
8321 This routine is used by cp_parser_assignment_expression.
8323 ? expression : assignment-expression
8325 GNU Extensions:
8327 ? : assignment-expression */
8329 static tree
8330 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8332 tree expr;
8333 tree assignment_expr;
8334 struct cp_token *token;
8335 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8337 /* Consume the `?' token. */
8338 cp_lexer_consume_token (parser->lexer);
8339 token = cp_lexer_peek_token (parser->lexer);
8340 if (cp_parser_allow_gnu_extensions_p (parser)
8341 && token->type == CPP_COLON)
8343 pedwarn (token->location, OPT_Wpedantic,
8344 "ISO C++ does not allow ?: with omitted middle operand");
8345 /* Implicit true clause. */
8346 expr = NULL_TREE;
8347 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8348 warn_for_omitted_condop (token->location, logical_or_expr);
8350 else
8352 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8353 parser->colon_corrects_to_scope_p = false;
8354 /* Parse the expression. */
8355 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8356 expr = cp_parser_expression (parser);
8357 c_inhibit_evaluation_warnings +=
8358 ((logical_or_expr == truthvalue_true_node)
8359 - (logical_or_expr == truthvalue_false_node));
8360 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8363 /* The next token should be a `:'. */
8364 cp_parser_require (parser, CPP_COLON, RT_COLON);
8365 /* Parse the assignment-expression. */
8366 assignment_expr = cp_parser_assignment_expression (parser);
8367 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8369 /* Build the conditional-expression. */
8370 return build_x_conditional_expr (loc, logical_or_expr,
8371 expr,
8372 assignment_expr,
8373 tf_warning_or_error);
8376 /* Parse an assignment-expression.
8378 assignment-expression:
8379 conditional-expression
8380 logical-or-expression assignment-operator assignment_expression
8381 throw-expression
8383 CAST_P is true if this expression is the target of a cast.
8384 DECLTYPE_P is true if this expression is the operand of decltype.
8386 Returns a representation for the expression. */
8388 static tree
8389 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8390 bool cast_p, bool decltype_p)
8392 tree expr;
8394 /* If the next token is the `throw' keyword, then we're looking at
8395 a throw-expression. */
8396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8397 expr = cp_parser_throw_expression (parser);
8398 /* Otherwise, it must be that we are looking at a
8399 logical-or-expression. */
8400 else
8402 /* Parse the binary expressions (logical-or-expression). */
8403 expr = cp_parser_binary_expression (parser, cast_p, false,
8404 decltype_p,
8405 PREC_NOT_OPERATOR, pidk);
8406 /* If the next token is a `?' then we're actually looking at a
8407 conditional-expression. */
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8409 return cp_parser_question_colon_clause (parser, expr);
8410 else
8412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8414 /* If it's an assignment-operator, we're using the second
8415 production. */
8416 enum tree_code assignment_operator
8417 = cp_parser_assignment_operator_opt (parser);
8418 if (assignment_operator != ERROR_MARK)
8420 bool non_constant_p;
8421 location_t saved_input_location;
8423 /* Parse the right-hand side of the assignment. */
8424 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8426 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8427 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8429 /* An assignment may not appear in a
8430 constant-expression. */
8431 if (cp_parser_non_integral_constant_expression (parser,
8432 NIC_ASSIGNMENT))
8433 return error_mark_node;
8434 /* Build the assignment expression. Its default
8435 location is the location of the '=' token. */
8436 saved_input_location = input_location;
8437 input_location = loc;
8438 expr = build_x_modify_expr (loc, expr,
8439 assignment_operator,
8440 rhs,
8441 complain_flags (decltype_p));
8442 input_location = saved_input_location;
8447 return expr;
8450 /* Parse an (optional) assignment-operator.
8452 assignment-operator: one of
8453 = *= /= %= += -= >>= <<= &= ^= |=
8455 GNU Extension:
8457 assignment-operator: one of
8458 <?= >?=
8460 If the next token is an assignment operator, the corresponding tree
8461 code is returned, and the token is consumed. For example, for
8462 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8463 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8464 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8465 operator, ERROR_MARK is returned. */
8467 static enum tree_code
8468 cp_parser_assignment_operator_opt (cp_parser* parser)
8470 enum tree_code op;
8471 cp_token *token;
8473 /* Peek at the next token. */
8474 token = cp_lexer_peek_token (parser->lexer);
8476 switch (token->type)
8478 case CPP_EQ:
8479 op = NOP_EXPR;
8480 break;
8482 case CPP_MULT_EQ:
8483 op = MULT_EXPR;
8484 break;
8486 case CPP_DIV_EQ:
8487 op = TRUNC_DIV_EXPR;
8488 break;
8490 case CPP_MOD_EQ:
8491 op = TRUNC_MOD_EXPR;
8492 break;
8494 case CPP_PLUS_EQ:
8495 op = PLUS_EXPR;
8496 break;
8498 case CPP_MINUS_EQ:
8499 op = MINUS_EXPR;
8500 break;
8502 case CPP_RSHIFT_EQ:
8503 op = RSHIFT_EXPR;
8504 break;
8506 case CPP_LSHIFT_EQ:
8507 op = LSHIFT_EXPR;
8508 break;
8510 case CPP_AND_EQ:
8511 op = BIT_AND_EXPR;
8512 break;
8514 case CPP_XOR_EQ:
8515 op = BIT_XOR_EXPR;
8516 break;
8518 case CPP_OR_EQ:
8519 op = BIT_IOR_EXPR;
8520 break;
8522 default:
8523 /* Nothing else is an assignment operator. */
8524 op = ERROR_MARK;
8527 /* If it was an assignment operator, consume it. */
8528 if (op != ERROR_MARK)
8529 cp_lexer_consume_token (parser->lexer);
8531 return op;
8534 /* Parse an expression.
8536 expression:
8537 assignment-expression
8538 expression , assignment-expression
8540 CAST_P is true if this expression is the target of a cast.
8541 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8542 except possibly parenthesized or on the RHS of a comma (N3276).
8544 Returns a representation of the expression. */
8546 static tree
8547 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8548 bool cast_p, bool decltype_p)
8550 tree expression = NULL_TREE;
8551 location_t loc = UNKNOWN_LOCATION;
8553 while (true)
8555 tree assignment_expression;
8557 /* Parse the next assignment-expression. */
8558 assignment_expression
8559 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8561 /* We don't create a temporary for a call that is the immediate operand
8562 of decltype or on the RHS of a comma. But when we see a comma, we
8563 need to create a temporary for a call on the LHS. */
8564 if (decltype_p && !processing_template_decl
8565 && TREE_CODE (assignment_expression) == CALL_EXPR
8566 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8567 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8568 assignment_expression
8569 = build_cplus_new (TREE_TYPE (assignment_expression),
8570 assignment_expression, tf_warning_or_error);
8572 /* If this is the first assignment-expression, we can just
8573 save it away. */
8574 if (!expression)
8575 expression = assignment_expression;
8576 else
8577 expression = build_x_compound_expr (loc, expression,
8578 assignment_expression,
8579 complain_flags (decltype_p));
8580 /* If the next token is not a comma, then we are done with the
8581 expression. */
8582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8583 break;
8584 /* Consume the `,'. */
8585 loc = cp_lexer_peek_token (parser->lexer)->location;
8586 cp_lexer_consume_token (parser->lexer);
8587 /* A comma operator cannot appear in a constant-expression. */
8588 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8589 expression = error_mark_node;
8592 return expression;
8595 /* Parse a constant-expression.
8597 constant-expression:
8598 conditional-expression
8600 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8601 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8602 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8603 is false, NON_CONSTANT_P should be NULL. */
8605 static tree
8606 cp_parser_constant_expression (cp_parser* parser,
8607 bool allow_non_constant_p,
8608 bool *non_constant_p)
8610 bool saved_integral_constant_expression_p;
8611 bool saved_allow_non_integral_constant_expression_p;
8612 bool saved_non_integral_constant_expression_p;
8613 tree expression;
8615 /* It might seem that we could simply parse the
8616 conditional-expression, and then check to see if it were
8617 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8618 one that the compiler can figure out is constant, possibly after
8619 doing some simplifications or optimizations. The standard has a
8620 precise definition of constant-expression, and we must honor
8621 that, even though it is somewhat more restrictive.
8623 For example:
8625 int i[(2, 3)];
8627 is not a legal declaration, because `(2, 3)' is not a
8628 constant-expression. The `,' operator is forbidden in a
8629 constant-expression. However, GCC's constant-folding machinery
8630 will fold this operation to an INTEGER_CST for `3'. */
8632 /* Save the old settings. */
8633 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8634 saved_allow_non_integral_constant_expression_p
8635 = parser->allow_non_integral_constant_expression_p;
8636 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8637 /* We are now parsing a constant-expression. */
8638 parser->integral_constant_expression_p = true;
8639 parser->allow_non_integral_constant_expression_p
8640 = (allow_non_constant_p || cxx_dialect >= cxx11);
8641 parser->non_integral_constant_expression_p = false;
8642 /* Although the grammar says "conditional-expression", we parse an
8643 "assignment-expression", which also permits "throw-expression"
8644 and the use of assignment operators. In the case that
8645 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8646 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8647 actually essential that we look for an assignment-expression.
8648 For example, cp_parser_initializer_clauses uses this function to
8649 determine whether a particular assignment-expression is in fact
8650 constant. */
8651 expression = cp_parser_assignment_expression (parser);
8652 /* Restore the old settings. */
8653 parser->integral_constant_expression_p
8654 = saved_integral_constant_expression_p;
8655 parser->allow_non_integral_constant_expression_p
8656 = saved_allow_non_integral_constant_expression_p;
8657 if (cxx_dialect >= cxx11)
8659 /* Require an rvalue constant expression here; that's what our
8660 callers expect. Reference constant expressions are handled
8661 separately in e.g. cp_parser_template_argument. */
8662 bool is_const = potential_rvalue_constant_expression (expression);
8663 parser->non_integral_constant_expression_p = !is_const;
8664 if (!is_const && !allow_non_constant_p)
8665 require_potential_rvalue_constant_expression (expression);
8667 if (allow_non_constant_p)
8668 *non_constant_p = parser->non_integral_constant_expression_p;
8669 parser->non_integral_constant_expression_p
8670 = saved_non_integral_constant_expression_p;
8672 return expression;
8675 /* Parse __builtin_offsetof.
8677 offsetof-expression:
8678 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8680 offsetof-member-designator:
8681 id-expression
8682 | offsetof-member-designator "." id-expression
8683 | offsetof-member-designator "[" expression "]"
8684 | offsetof-member-designator "->" id-expression */
8686 static tree
8687 cp_parser_builtin_offsetof (cp_parser *parser)
8689 int save_ice_p, save_non_ice_p;
8690 tree type, expr;
8691 cp_id_kind dummy;
8692 cp_token *token;
8694 /* We're about to accept non-integral-constant things, but will
8695 definitely yield an integral constant expression. Save and
8696 restore these values around our local parsing. */
8697 save_ice_p = parser->integral_constant_expression_p;
8698 save_non_ice_p = parser->non_integral_constant_expression_p;
8700 /* Consume the "__builtin_offsetof" token. */
8701 cp_lexer_consume_token (parser->lexer);
8702 /* Consume the opening `('. */
8703 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8704 /* Parse the type-id. */
8705 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8706 type = cp_parser_type_id (parser);
8707 /* Look for the `,'. */
8708 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8709 token = cp_lexer_peek_token (parser->lexer);
8711 /* Build the (type *)null that begins the traditional offsetof macro. */
8712 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8713 tf_warning_or_error);
8715 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8716 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8717 true, &dummy, token->location);
8718 while (true)
8720 token = cp_lexer_peek_token (parser->lexer);
8721 switch (token->type)
8723 case CPP_OPEN_SQUARE:
8724 /* offsetof-member-designator "[" expression "]" */
8725 expr = cp_parser_postfix_open_square_expression (parser, expr,
8726 true, false);
8727 break;
8729 case CPP_DEREF:
8730 /* offsetof-member-designator "->" identifier */
8731 expr = grok_array_decl (token->location, expr,
8732 integer_zero_node, false);
8733 /* FALLTHRU */
8735 case CPP_DOT:
8736 /* offsetof-member-designator "." identifier */
8737 cp_lexer_consume_token (parser->lexer);
8738 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8739 expr, true, &dummy,
8740 token->location);
8741 break;
8743 case CPP_CLOSE_PAREN:
8744 /* Consume the ")" token. */
8745 cp_lexer_consume_token (parser->lexer);
8746 goto success;
8748 default:
8749 /* Error. We know the following require will fail, but
8750 that gives the proper error message. */
8751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8752 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8753 expr = error_mark_node;
8754 goto failure;
8758 success:
8759 expr = finish_offsetof (expr, loc);
8761 failure:
8762 parser->integral_constant_expression_p = save_ice_p;
8763 parser->non_integral_constant_expression_p = save_non_ice_p;
8765 return expr;
8768 /* Parse a trait expression.
8770 Returns a representation of the expression, the underlying type
8771 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8773 static tree
8774 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8776 cp_trait_kind kind;
8777 tree type1, type2 = NULL_TREE;
8778 bool binary = false;
8779 bool variadic = false;
8781 switch (keyword)
8783 case RID_HAS_NOTHROW_ASSIGN:
8784 kind = CPTK_HAS_NOTHROW_ASSIGN;
8785 break;
8786 case RID_HAS_NOTHROW_CONSTRUCTOR:
8787 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8788 break;
8789 case RID_HAS_NOTHROW_COPY:
8790 kind = CPTK_HAS_NOTHROW_COPY;
8791 break;
8792 case RID_HAS_TRIVIAL_ASSIGN:
8793 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8794 break;
8795 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8796 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8797 break;
8798 case RID_HAS_TRIVIAL_COPY:
8799 kind = CPTK_HAS_TRIVIAL_COPY;
8800 break;
8801 case RID_HAS_TRIVIAL_DESTRUCTOR:
8802 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8803 break;
8804 case RID_HAS_VIRTUAL_DESTRUCTOR:
8805 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8806 break;
8807 case RID_IS_ABSTRACT:
8808 kind = CPTK_IS_ABSTRACT;
8809 break;
8810 case RID_IS_BASE_OF:
8811 kind = CPTK_IS_BASE_OF;
8812 binary = true;
8813 break;
8814 case RID_IS_CLASS:
8815 kind = CPTK_IS_CLASS;
8816 break;
8817 case RID_IS_EMPTY:
8818 kind = CPTK_IS_EMPTY;
8819 break;
8820 case RID_IS_ENUM:
8821 kind = CPTK_IS_ENUM;
8822 break;
8823 case RID_IS_FINAL:
8824 kind = CPTK_IS_FINAL;
8825 break;
8826 case RID_IS_LITERAL_TYPE:
8827 kind = CPTK_IS_LITERAL_TYPE;
8828 break;
8829 case RID_IS_POD:
8830 kind = CPTK_IS_POD;
8831 break;
8832 case RID_IS_POLYMORPHIC:
8833 kind = CPTK_IS_POLYMORPHIC;
8834 break;
8835 case RID_IS_STD_LAYOUT:
8836 kind = CPTK_IS_STD_LAYOUT;
8837 break;
8838 case RID_IS_TRIVIAL:
8839 kind = CPTK_IS_TRIVIAL;
8840 break;
8841 case RID_IS_TRIVIALLY_ASSIGNABLE:
8842 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8843 binary = true;
8844 break;
8845 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8846 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8847 variadic = true;
8848 break;
8849 case RID_IS_TRIVIALLY_COPYABLE:
8850 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8851 break;
8852 case RID_IS_UNION:
8853 kind = CPTK_IS_UNION;
8854 break;
8855 case RID_UNDERLYING_TYPE:
8856 kind = CPTK_UNDERLYING_TYPE;
8857 break;
8858 case RID_BASES:
8859 kind = CPTK_BASES;
8860 break;
8861 case RID_DIRECT_BASES:
8862 kind = CPTK_DIRECT_BASES;
8863 break;
8864 default:
8865 gcc_unreachable ();
8868 /* Consume the token. */
8869 cp_lexer_consume_token (parser->lexer);
8871 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8873 type1 = cp_parser_type_id (parser);
8875 if (type1 == error_mark_node)
8876 return error_mark_node;
8878 if (binary)
8880 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8882 type2 = cp_parser_type_id (parser);
8884 if (type2 == error_mark_node)
8885 return error_mark_node;
8887 else if (variadic)
8889 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8891 cp_lexer_consume_token (parser->lexer);
8892 tree elt = cp_parser_type_id (parser);
8893 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8895 cp_lexer_consume_token (parser->lexer);
8896 elt = make_pack_expansion (elt);
8898 if (elt == error_mark_node)
8899 return error_mark_node;
8900 type2 = tree_cons (NULL_TREE, elt, type2);
8904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8906 /* Complete the trait expression, which may mean either processing
8907 the trait expr now or saving it for template instantiation. */
8908 switch(kind)
8910 case CPTK_UNDERLYING_TYPE:
8911 return finish_underlying_type (type1);
8912 case CPTK_BASES:
8913 return finish_bases (type1, false);
8914 case CPTK_DIRECT_BASES:
8915 return finish_bases (type1, true);
8916 default:
8917 return finish_trait_expr (kind, type1, type2);
8921 /* Lambdas that appear in variable initializer or default argument scope
8922 get that in their mangling, so we need to record it. We might as well
8923 use the count for function and namespace scopes as well. */
8924 static GTY(()) tree lambda_scope;
8925 static GTY(()) int lambda_count;
8926 typedef struct GTY(()) tree_int
8928 tree t;
8929 int i;
8930 } tree_int;
8931 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8933 static void
8934 start_lambda_scope (tree decl)
8936 tree_int ti;
8937 gcc_assert (decl);
8938 /* Once we're inside a function, we ignore other scopes and just push
8939 the function again so that popping works properly. */
8940 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8941 decl = current_function_decl;
8942 ti.t = lambda_scope;
8943 ti.i = lambda_count;
8944 vec_safe_push (lambda_scope_stack, ti);
8945 if (lambda_scope != decl)
8947 /* Don't reset the count if we're still in the same function. */
8948 lambda_scope = decl;
8949 lambda_count = 0;
8953 static void
8954 record_lambda_scope (tree lambda)
8956 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8957 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8960 static void
8961 finish_lambda_scope (void)
8963 tree_int *p = &lambda_scope_stack->last ();
8964 if (lambda_scope != p->t)
8966 lambda_scope = p->t;
8967 lambda_count = p->i;
8969 lambda_scope_stack->pop ();
8972 /* Parse a lambda expression.
8974 lambda-expression:
8975 lambda-introducer lambda-declarator [opt] compound-statement
8977 Returns a representation of the expression. */
8979 static tree
8980 cp_parser_lambda_expression (cp_parser* parser)
8982 tree lambda_expr = build_lambda_expr ();
8983 tree type;
8984 bool ok = true;
8985 cp_token *token = cp_lexer_peek_token (parser->lexer);
8986 cp_token_position start = 0;
8988 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8990 if (cp_unevaluated_operand)
8992 if (!token->error_reported)
8994 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8995 "lambda-expression in unevaluated context");
8996 token->error_reported = true;
8998 ok = false;
9000 else if (parser->in_template_argument_list_p)
9002 if (!token->error_reported)
9004 error_at (token->location, "lambda-expression in template-argument");
9005 token->error_reported = true;
9007 ok = false;
9010 /* We may be in the middle of deferred access check. Disable
9011 it now. */
9012 push_deferring_access_checks (dk_no_deferred);
9014 cp_parser_lambda_introducer (parser, lambda_expr);
9016 type = begin_lambda_type (lambda_expr);
9017 if (type == error_mark_node)
9018 return error_mark_node;
9020 record_lambda_scope (lambda_expr);
9022 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9023 determine_visibility (TYPE_NAME (type));
9025 /* Now that we've started the type, add the capture fields for any
9026 explicit captures. */
9027 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9030 /* Inside the class, surrounding template-parameter-lists do not apply. */
9031 unsigned int saved_num_template_parameter_lists
9032 = parser->num_template_parameter_lists;
9033 unsigned char in_statement = parser->in_statement;
9034 bool in_switch_statement_p = parser->in_switch_statement_p;
9035 bool fully_implicit_function_template_p
9036 = parser->fully_implicit_function_template_p;
9037 tree implicit_template_parms = parser->implicit_template_parms;
9038 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9039 bool auto_is_implicit_function_template_parm_p
9040 = parser->auto_is_implicit_function_template_parm_p;
9042 parser->num_template_parameter_lists = 0;
9043 parser->in_statement = 0;
9044 parser->in_switch_statement_p = false;
9045 parser->fully_implicit_function_template_p = false;
9046 parser->implicit_template_parms = 0;
9047 parser->implicit_template_scope = 0;
9048 parser->auto_is_implicit_function_template_parm_p = false;
9050 /* By virtue of defining a local class, a lambda expression has access to
9051 the private variables of enclosing classes. */
9053 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9055 if (ok)
9057 if (!cp_parser_error_occurred (parser)
9058 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9059 && cp_parser_start_tentative_firewall (parser))
9060 start = token;
9061 cp_parser_lambda_body (parser, lambda_expr);
9063 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9065 if (cp_parser_skip_to_closing_brace (parser))
9066 cp_lexer_consume_token (parser->lexer);
9069 /* The capture list was built up in reverse order; fix that now. */
9070 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9071 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9073 if (ok)
9074 maybe_add_lambda_conv_op (type);
9076 type = finish_struct (type, /*attributes=*/NULL_TREE);
9078 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9079 parser->in_statement = in_statement;
9080 parser->in_switch_statement_p = in_switch_statement_p;
9081 parser->fully_implicit_function_template_p
9082 = fully_implicit_function_template_p;
9083 parser->implicit_template_parms = implicit_template_parms;
9084 parser->implicit_template_scope = implicit_template_scope;
9085 parser->auto_is_implicit_function_template_parm_p
9086 = auto_is_implicit_function_template_parm_p;
9089 pop_deferring_access_checks ();
9091 /* This field is only used during parsing of the lambda. */
9092 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9094 /* This lambda shouldn't have any proxies left at this point. */
9095 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9096 /* And now that we're done, push proxies for an enclosing lambda. */
9097 insert_pending_capture_proxies ();
9099 if (ok)
9100 lambda_expr = build_lambda_object (lambda_expr);
9101 else
9102 lambda_expr = error_mark_node;
9104 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9106 return lambda_expr;
9109 /* Parse the beginning of a lambda expression.
9111 lambda-introducer:
9112 [ lambda-capture [opt] ]
9114 LAMBDA_EXPR is the current representation of the lambda expression. */
9116 static void
9117 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9119 /* Need commas after the first capture. */
9120 bool first = true;
9122 /* Eat the leading `['. */
9123 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9125 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9127 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9128 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9129 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9130 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9132 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9134 cp_lexer_consume_token (parser->lexer);
9135 first = false;
9138 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9140 cp_token* capture_token;
9141 tree capture_id;
9142 tree capture_init_expr;
9143 cp_id_kind idk = CP_ID_KIND_NONE;
9144 bool explicit_init_p = false;
9146 enum capture_kind_type
9148 BY_COPY,
9149 BY_REFERENCE
9151 enum capture_kind_type capture_kind = BY_COPY;
9153 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9155 error ("expected end of capture-list");
9156 return;
9159 if (first)
9160 first = false;
9161 else
9162 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9164 /* Possibly capture `this'. */
9165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9167 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9168 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9169 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9170 "with by-copy capture default");
9171 cp_lexer_consume_token (parser->lexer);
9172 add_capture (lambda_expr,
9173 /*id=*/this_identifier,
9174 /*initializer=*/finish_this_expr(),
9175 /*by_reference_p=*/false,
9176 explicit_init_p);
9177 continue;
9180 /* Remember whether we want to capture as a reference or not. */
9181 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9183 capture_kind = BY_REFERENCE;
9184 cp_lexer_consume_token (parser->lexer);
9187 /* Get the identifier. */
9188 capture_token = cp_lexer_peek_token (parser->lexer);
9189 capture_id = cp_parser_identifier (parser);
9191 if (capture_id == error_mark_node)
9192 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9193 delimiters, but I modified this to stop on unnested ']' as well. It
9194 was already changed to stop on unnested '}', so the
9195 "closing_parenthesis" name is no more misleading with my change. */
9197 cp_parser_skip_to_closing_parenthesis (parser,
9198 /*recovering=*/true,
9199 /*or_comma=*/true,
9200 /*consume_paren=*/true);
9201 break;
9204 /* Find the initializer for this capture. */
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9206 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9207 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9209 bool direct, non_constant;
9210 /* An explicit initializer exists. */
9211 if (cxx_dialect < cxx14)
9212 pedwarn (input_location, 0,
9213 "lambda capture initializers "
9214 "only available with -std=c++14 or -std=gnu++14");
9215 capture_init_expr = cp_parser_initializer (parser, &direct,
9216 &non_constant);
9217 explicit_init_p = true;
9218 if (capture_init_expr == NULL_TREE)
9220 error ("empty initializer for lambda init-capture");
9221 capture_init_expr = error_mark_node;
9224 else
9226 const char* error_msg;
9228 /* Turn the identifier into an id-expression. */
9229 capture_init_expr
9230 = cp_parser_lookup_name_simple (parser, capture_id,
9231 capture_token->location);
9233 if (capture_init_expr == error_mark_node)
9235 unqualified_name_lookup_error (capture_id);
9236 continue;
9238 else if (DECL_P (capture_init_expr)
9239 && (!VAR_P (capture_init_expr)
9240 && TREE_CODE (capture_init_expr) != PARM_DECL))
9242 error_at (capture_token->location,
9243 "capture of non-variable %qD ",
9244 capture_init_expr);
9245 inform (0, "%q+#D declared here", capture_init_expr);
9246 continue;
9248 if (VAR_P (capture_init_expr)
9249 && decl_storage_duration (capture_init_expr) != dk_auto)
9251 if (pedwarn (capture_token->location, 0, "capture of variable "
9252 "%qD with non-automatic storage duration",
9253 capture_init_expr))
9254 inform (0, "%q+#D declared here", capture_init_expr);
9255 continue;
9258 capture_init_expr
9259 = finish_id_expression
9260 (capture_id,
9261 capture_init_expr,
9262 parser->scope,
9263 &idk,
9264 /*integral_constant_expression_p=*/false,
9265 /*allow_non_integral_constant_expression_p=*/false,
9266 /*non_integral_constant_expression_p=*/NULL,
9267 /*template_p=*/false,
9268 /*done=*/true,
9269 /*address_p=*/false,
9270 /*template_arg_p=*/false,
9271 &error_msg,
9272 capture_token->location);
9274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9276 cp_lexer_consume_token (parser->lexer);
9277 capture_init_expr = make_pack_expansion (capture_init_expr);
9279 else
9280 check_for_bare_parameter_packs (capture_init_expr);
9283 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9284 && !explicit_init_p)
9286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9287 && capture_kind == BY_COPY)
9288 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9289 "of %qD redundant with by-copy capture default",
9290 capture_id);
9291 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9292 && capture_kind == BY_REFERENCE)
9293 pedwarn (capture_token->location, 0, "explicit by-reference "
9294 "capture of %qD redundant with by-reference capture "
9295 "default", capture_id);
9298 add_capture (lambda_expr,
9299 capture_id,
9300 capture_init_expr,
9301 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9302 explicit_init_p);
9305 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9308 /* Parse the (optional) middle of a lambda expression.
9310 lambda-declarator:
9311 < template-parameter-list [opt] >
9312 ( parameter-declaration-clause [opt] )
9313 attribute-specifier [opt]
9314 mutable [opt]
9315 exception-specification [opt]
9316 lambda-return-type-clause [opt]
9318 LAMBDA_EXPR is the current representation of the lambda expression. */
9320 static bool
9321 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9323 /* 5.1.1.4 of the standard says:
9324 If a lambda-expression does not include a lambda-declarator, it is as if
9325 the lambda-declarator were ().
9326 This means an empty parameter list, no attributes, and no exception
9327 specification. */
9328 tree param_list = void_list_node;
9329 tree attributes = NULL_TREE;
9330 tree exception_spec = NULL_TREE;
9331 tree template_param_list = NULL_TREE;
9333 /* The template-parameter-list is optional, but must begin with
9334 an opening angle if present. */
9335 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9337 if (cxx_dialect < cxx14)
9338 pedwarn (parser->lexer->next_token->location, 0,
9339 "lambda templates are only available with "
9340 "-std=c++14 or -std=gnu++14");
9342 cp_lexer_consume_token (parser->lexer);
9344 template_param_list = cp_parser_template_parameter_list (parser);
9346 cp_parser_skip_to_end_of_template_parameter_list (parser);
9348 /* We just processed one more parameter list. */
9349 ++parser->num_template_parameter_lists;
9352 /* The parameter-declaration-clause is optional (unless
9353 template-parameter-list was given), but must begin with an
9354 opening parenthesis if present. */
9355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9357 cp_lexer_consume_token (parser->lexer);
9359 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9361 /* Parse parameters. */
9362 param_list = cp_parser_parameter_declaration_clause (parser);
9364 /* Default arguments shall not be specified in the
9365 parameter-declaration-clause of a lambda-declarator. */
9366 for (tree t = param_list; t; t = TREE_CHAIN (t))
9367 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9368 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9369 "default argument specified for lambda parameter");
9371 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9373 attributes = cp_parser_attributes_opt (parser);
9375 /* Parse optional `mutable' keyword. */
9376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9378 cp_lexer_consume_token (parser->lexer);
9379 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9382 /* Parse optional exception specification. */
9383 exception_spec = cp_parser_exception_specification_opt (parser);
9385 /* Parse optional trailing return type. */
9386 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9388 cp_lexer_consume_token (parser->lexer);
9389 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9390 = cp_parser_trailing_type_id (parser);
9393 /* The function parameters must be in scope all the way until after the
9394 trailing-return-type in case of decltype. */
9395 pop_bindings_and_leave_scope ();
9397 else if (template_param_list != NULL_TREE) // generate diagnostic
9398 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9400 /* Create the function call operator.
9402 Messing with declarators like this is no uglier than building up the
9403 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9404 other code. */
9406 cp_decl_specifier_seq return_type_specs;
9407 cp_declarator* declarator;
9408 tree fco;
9409 int quals;
9410 void *p;
9412 clear_decl_specs (&return_type_specs);
9413 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9414 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9415 else
9416 /* Maybe we will deduce the return type later. */
9417 return_type_specs.type = make_auto ();
9419 p = obstack_alloc (&declarator_obstack, 0);
9421 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9422 sfk_none);
9424 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9425 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9426 declarator = make_call_declarator (declarator, param_list, quals,
9427 VIRT_SPEC_UNSPECIFIED,
9428 REF_QUAL_NONE,
9429 exception_spec,
9430 /*late_return_type=*/NULL_TREE);
9431 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9433 fco = grokmethod (&return_type_specs,
9434 declarator,
9435 attributes);
9436 if (fco != error_mark_node)
9438 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9439 DECL_ARTIFICIAL (fco) = 1;
9440 /* Give the object parameter a different name. */
9441 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9442 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9443 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9445 if (template_param_list)
9447 fco = finish_member_template_decl (fco);
9448 finish_template_decl (template_param_list);
9449 --parser->num_template_parameter_lists;
9451 else if (parser->fully_implicit_function_template_p)
9452 fco = finish_fully_implicit_template (parser, fco);
9454 finish_member_declaration (fco);
9456 obstack_free (&declarator_obstack, p);
9458 return (fco != error_mark_node);
9462 /* Parse the body of a lambda expression, which is simply
9464 compound-statement
9466 but which requires special handling.
9467 LAMBDA_EXPR is the current representation of the lambda expression. */
9469 static void
9470 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9472 bool nested = (current_function_decl != NULL_TREE);
9473 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9474 if (nested)
9475 push_function_context ();
9476 else
9477 /* Still increment function_depth so that we don't GC in the
9478 middle of an expression. */
9479 ++function_depth;
9480 /* Clear this in case we're in the middle of a default argument. */
9481 parser->local_variables_forbidden_p = false;
9483 /* Finish the function call operator
9484 - class_specifier
9485 + late_parsing_for_member
9486 + function_definition_after_declarator
9487 + ctor_initializer_opt_and_function_body */
9489 tree fco = lambda_function (lambda_expr);
9490 tree body;
9491 bool done = false;
9492 tree compound_stmt;
9493 tree cap;
9495 /* Let the front end know that we are going to be defining this
9496 function. */
9497 start_preparsed_function (fco,
9498 NULL_TREE,
9499 SF_PRE_PARSED | SF_INCLASS_INLINE);
9501 start_lambda_scope (fco);
9502 body = begin_function_body ();
9504 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9505 goto out;
9507 /* Push the proxies for any explicit captures. */
9508 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9509 cap = TREE_CHAIN (cap))
9510 build_capture_proxy (TREE_PURPOSE (cap));
9512 compound_stmt = begin_compound_stmt (0);
9514 /* 5.1.1.4 of the standard says:
9515 If a lambda-expression does not include a trailing-return-type, it
9516 is as if the trailing-return-type denotes the following type:
9517 * if the compound-statement is of the form
9518 { return attribute-specifier [opt] expression ; }
9519 the type of the returned expression after lvalue-to-rvalue
9520 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9521 (_conv.array_ 4.2), and function-to-pointer conversion
9522 (_conv.func_ 4.3);
9523 * otherwise, void. */
9525 /* In a lambda that has neither a lambda-return-type-clause
9526 nor a deducible form, errors should be reported for return statements
9527 in the body. Since we used void as the placeholder return type, parsing
9528 the body as usual will give such desired behavior. */
9529 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9530 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9533 tree expr = NULL_TREE;
9534 cp_id_kind idk = CP_ID_KIND_NONE;
9536 /* Parse tentatively in case there's more after the initial return
9537 statement. */
9538 cp_parser_parse_tentatively (parser);
9540 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9542 expr = cp_parser_expression (parser, &idk);
9544 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9547 if (cp_parser_parse_definitely (parser))
9549 if (!processing_template_decl)
9550 apply_deduced_return_type (fco, lambda_return_type (expr));
9552 /* Will get error here if type not deduced yet. */
9553 finish_return_stmt (expr);
9555 done = true;
9559 if (!done)
9561 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9562 cp_parser_label_declaration (parser);
9563 cp_parser_statement_seq_opt (parser, NULL_TREE);
9564 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9567 finish_compound_stmt (compound_stmt);
9569 out:
9570 finish_function_body (body);
9571 finish_lambda_scope ();
9573 /* Finish the function and generate code for it if necessary. */
9574 tree fn = finish_function (/*inline*/2);
9576 /* Only expand if the call op is not a template. */
9577 if (!DECL_TEMPLATE_INFO (fco))
9578 expand_or_defer_fn (fn);
9581 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9582 if (nested)
9583 pop_function_context();
9584 else
9585 --function_depth;
9588 /* Statements [gram.stmt.stmt] */
9590 /* Parse a statement.
9592 statement:
9593 labeled-statement
9594 expression-statement
9595 compound-statement
9596 selection-statement
9597 iteration-statement
9598 jump-statement
9599 declaration-statement
9600 try-block
9602 C++11:
9604 statement:
9605 labeled-statement
9606 attribute-specifier-seq (opt) expression-statement
9607 attribute-specifier-seq (opt) compound-statement
9608 attribute-specifier-seq (opt) selection-statement
9609 attribute-specifier-seq (opt) iteration-statement
9610 attribute-specifier-seq (opt) jump-statement
9611 declaration-statement
9612 attribute-specifier-seq (opt) try-block
9614 TM Extension:
9616 statement:
9617 atomic-statement
9619 IN_COMPOUND is true when the statement is nested inside a
9620 cp_parser_compound_statement; this matters for certain pragmas.
9622 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9623 is a (possibly labeled) if statement which is not enclosed in braces
9624 and has an else clause. This is used to implement -Wparentheses. */
9626 static void
9627 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9628 bool in_compound, bool *if_p)
9630 tree statement, std_attrs = NULL_TREE;
9631 cp_token *token;
9632 location_t statement_location, attrs_location;
9634 restart:
9635 if (if_p != NULL)
9636 *if_p = false;
9637 /* There is no statement yet. */
9638 statement = NULL_TREE;
9640 saved_token_sentinel saved_tokens (parser->lexer);
9641 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9642 if (c_dialect_objc ())
9643 /* In obj-c++, seeing '[[' might be the either the beginning of
9644 c++11 attributes, or a nested objc-message-expression. So
9645 let's parse the c++11 attributes tentatively. */
9646 cp_parser_parse_tentatively (parser);
9647 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9648 if (c_dialect_objc ())
9650 if (!cp_parser_parse_definitely (parser))
9651 std_attrs = NULL_TREE;
9654 /* Peek at the next token. */
9655 token = cp_lexer_peek_token (parser->lexer);
9656 /* Remember the location of the first token in the statement. */
9657 statement_location = token->location;
9658 /* If this is a keyword, then that will often determine what kind of
9659 statement we have. */
9660 if (token->type == CPP_KEYWORD)
9662 enum rid keyword = token->keyword;
9664 switch (keyword)
9666 case RID_CASE:
9667 case RID_DEFAULT:
9668 /* Looks like a labeled-statement with a case label.
9669 Parse the label, and then use tail recursion to parse
9670 the statement. */
9671 cp_parser_label_for_labeled_statement (parser, std_attrs);
9672 goto restart;
9674 case RID_IF:
9675 case RID_SWITCH:
9676 statement = cp_parser_selection_statement (parser, if_p);
9677 break;
9679 case RID_WHILE:
9680 case RID_DO:
9681 case RID_FOR:
9682 statement = cp_parser_iteration_statement (parser, false);
9683 break;
9685 case RID_CILK_FOR:
9686 if (!flag_cilkplus)
9688 error_at (cp_lexer_peek_token (parser->lexer)->location,
9689 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9690 cp_lexer_consume_token (parser->lexer);
9691 statement = error_mark_node;
9693 else
9694 statement = cp_parser_cilk_for (parser, integer_zero_node);
9695 break;
9697 case RID_BREAK:
9698 case RID_CONTINUE:
9699 case RID_RETURN:
9700 case RID_GOTO:
9701 statement = cp_parser_jump_statement (parser);
9702 break;
9704 case RID_CILK_SYNC:
9705 cp_lexer_consume_token (parser->lexer);
9706 if (flag_cilkplus)
9708 tree sync_expr = build_cilk_sync ();
9709 SET_EXPR_LOCATION (sync_expr,
9710 token->location);
9711 statement = finish_expr_stmt (sync_expr);
9713 else
9715 error_at (token->location, "-fcilkplus must be enabled to use"
9716 " %<_Cilk_sync%>");
9717 statement = error_mark_node;
9719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9720 break;
9722 /* Objective-C++ exception-handling constructs. */
9723 case RID_AT_TRY:
9724 case RID_AT_CATCH:
9725 case RID_AT_FINALLY:
9726 case RID_AT_SYNCHRONIZED:
9727 case RID_AT_THROW:
9728 statement = cp_parser_objc_statement (parser);
9729 break;
9731 case RID_TRY:
9732 statement = cp_parser_try_block (parser);
9733 break;
9735 case RID_NAMESPACE:
9736 /* This must be a namespace alias definition. */
9737 cp_parser_declaration_statement (parser);
9738 return;
9740 case RID_TRANSACTION_ATOMIC:
9741 case RID_TRANSACTION_RELAXED:
9742 statement = cp_parser_transaction (parser, keyword);
9743 break;
9744 case RID_TRANSACTION_CANCEL:
9745 statement = cp_parser_transaction_cancel (parser);
9746 break;
9748 default:
9749 /* It might be a keyword like `int' that can start a
9750 declaration-statement. */
9751 break;
9754 else if (token->type == CPP_NAME)
9756 /* If the next token is a `:', then we are looking at a
9757 labeled-statement. */
9758 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9759 if (token->type == CPP_COLON)
9761 /* Looks like a labeled-statement with an ordinary label.
9762 Parse the label, and then use tail recursion to parse
9763 the statement. */
9765 cp_parser_label_for_labeled_statement (parser, std_attrs);
9766 goto restart;
9769 /* Anything that starts with a `{' must be a compound-statement. */
9770 else if (token->type == CPP_OPEN_BRACE)
9771 statement = cp_parser_compound_statement (parser, NULL, false, false);
9772 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9773 a statement all its own. */
9774 else if (token->type == CPP_PRAGMA)
9776 /* Only certain OpenMP pragmas are attached to statements, and thus
9777 are considered statements themselves. All others are not. In
9778 the context of a compound, accept the pragma as a "statement" and
9779 return so that we can check for a close brace. Otherwise we
9780 require a real statement and must go back and read one. */
9781 if (in_compound)
9782 cp_parser_pragma (parser, pragma_compound);
9783 else if (!cp_parser_pragma (parser, pragma_stmt))
9784 goto restart;
9785 return;
9787 else if (token->type == CPP_EOF)
9789 cp_parser_error (parser, "expected statement");
9790 return;
9793 /* Everything else must be a declaration-statement or an
9794 expression-statement. Try for the declaration-statement
9795 first, unless we are looking at a `;', in which case we know that
9796 we have an expression-statement. */
9797 if (!statement)
9799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9801 if (std_attrs != NULL_TREE)
9803 /* Attributes should be parsed as part of the the
9804 declaration, so let's un-parse them. */
9805 saved_tokens.rollback();
9806 std_attrs = NULL_TREE;
9809 cp_parser_parse_tentatively (parser);
9810 /* Try to parse the declaration-statement. */
9811 cp_parser_declaration_statement (parser);
9812 /* If that worked, we're done. */
9813 if (cp_parser_parse_definitely (parser))
9814 return;
9816 /* Look for an expression-statement instead. */
9817 statement = cp_parser_expression_statement (parser, in_statement_expr);
9820 /* Set the line number for the statement. */
9821 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9822 SET_EXPR_LOCATION (statement, statement_location);
9824 /* Note that for now, we don't do anything with c++11 statements
9825 parsed at this level. */
9826 if (std_attrs != NULL_TREE)
9827 warning_at (attrs_location,
9828 OPT_Wattributes,
9829 "attributes at the beginning of statement are ignored");
9832 /* Parse the label for a labeled-statement, i.e.
9834 identifier :
9835 case constant-expression :
9836 default :
9838 GNU Extension:
9839 case constant-expression ... constant-expression : statement
9841 When a label is parsed without errors, the label is added to the
9842 parse tree by the finish_* functions, so this function doesn't
9843 have to return the label. */
9845 static void
9846 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9848 cp_token *token;
9849 tree label = NULL_TREE;
9850 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9852 /* The next token should be an identifier. */
9853 token = cp_lexer_peek_token (parser->lexer);
9854 if (token->type != CPP_NAME
9855 && token->type != CPP_KEYWORD)
9857 cp_parser_error (parser, "expected labeled-statement");
9858 return;
9861 parser->colon_corrects_to_scope_p = false;
9862 switch (token->keyword)
9864 case RID_CASE:
9866 tree expr, expr_hi;
9867 cp_token *ellipsis;
9869 /* Consume the `case' token. */
9870 cp_lexer_consume_token (parser->lexer);
9871 /* Parse the constant-expression. */
9872 expr = cp_parser_constant_expression (parser);
9873 if (check_for_bare_parameter_packs (expr))
9874 expr = error_mark_node;
9876 ellipsis = cp_lexer_peek_token (parser->lexer);
9877 if (ellipsis->type == CPP_ELLIPSIS)
9879 /* Consume the `...' token. */
9880 cp_lexer_consume_token (parser->lexer);
9881 expr_hi = cp_parser_constant_expression (parser);
9882 if (check_for_bare_parameter_packs (expr_hi))
9883 expr_hi = error_mark_node;
9885 /* We don't need to emit warnings here, as the common code
9886 will do this for us. */
9888 else
9889 expr_hi = NULL_TREE;
9891 if (parser->in_switch_statement_p)
9892 finish_case_label (token->location, expr, expr_hi);
9893 else
9894 error_at (token->location,
9895 "case label %qE not within a switch statement",
9896 expr);
9898 break;
9900 case RID_DEFAULT:
9901 /* Consume the `default' token. */
9902 cp_lexer_consume_token (parser->lexer);
9904 if (parser->in_switch_statement_p)
9905 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9906 else
9907 error_at (token->location, "case label not within a switch statement");
9908 break;
9910 default:
9911 /* Anything else must be an ordinary label. */
9912 label = finish_label_stmt (cp_parser_identifier (parser));
9913 break;
9916 /* Require the `:' token. */
9917 cp_parser_require (parser, CPP_COLON, RT_COLON);
9919 /* An ordinary label may optionally be followed by attributes.
9920 However, this is only permitted if the attributes are then
9921 followed by a semicolon. This is because, for backward
9922 compatibility, when parsing
9923 lab: __attribute__ ((unused)) int i;
9924 we want the attribute to attach to "i", not "lab". */
9925 if (label != NULL_TREE
9926 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9928 tree attrs;
9929 cp_parser_parse_tentatively (parser);
9930 attrs = cp_parser_gnu_attributes_opt (parser);
9931 if (attrs == NULL_TREE
9932 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9933 cp_parser_abort_tentative_parse (parser);
9934 else if (!cp_parser_parse_definitely (parser))
9936 else
9937 attributes = chainon (attributes, attrs);
9940 if (attributes != NULL_TREE)
9941 cplus_decl_attributes (&label, attributes, 0);
9943 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9946 /* Parse an expression-statement.
9948 expression-statement:
9949 expression [opt] ;
9951 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9952 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9953 indicates whether this expression-statement is part of an
9954 expression statement. */
9956 static tree
9957 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9959 tree statement = NULL_TREE;
9960 cp_token *token = cp_lexer_peek_token (parser->lexer);
9962 /* If the next token is a ';', then there is no expression
9963 statement. */
9964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9966 statement = cp_parser_expression (parser);
9967 if (statement == error_mark_node
9968 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9970 cp_parser_skip_to_end_of_block_or_statement (parser);
9971 return error_mark_node;
9975 /* Give a helpful message for "A<T>::type t;" and the like. */
9976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9977 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9979 if (TREE_CODE (statement) == SCOPE_REF)
9980 error_at (token->location, "need %<typename%> before %qE because "
9981 "%qT is a dependent scope",
9982 statement, TREE_OPERAND (statement, 0));
9983 else if (is_overloaded_fn (statement)
9984 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9986 /* A::A a; */
9987 tree fn = get_first_fn (statement);
9988 error_at (token->location,
9989 "%<%T::%D%> names the constructor, not the type",
9990 DECL_CONTEXT (fn), DECL_NAME (fn));
9994 /* Consume the final `;'. */
9995 cp_parser_consume_semicolon_at_end_of_statement (parser);
9997 if (in_statement_expr
9998 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9999 /* This is the final expression statement of a statement
10000 expression. */
10001 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10002 else if (statement)
10003 statement = finish_expr_stmt (statement);
10005 return statement;
10008 /* Parse a compound-statement.
10010 compound-statement:
10011 { statement-seq [opt] }
10013 GNU extension:
10015 compound-statement:
10016 { label-declaration-seq [opt] statement-seq [opt] }
10018 label-declaration-seq:
10019 label-declaration
10020 label-declaration-seq label-declaration
10022 Returns a tree representing the statement. */
10024 static tree
10025 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10026 bool in_try, bool function_body)
10028 tree compound_stmt;
10030 /* Consume the `{'. */
10031 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10032 return error_mark_node;
10033 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10034 && !function_body && cxx_dialect < cxx14)
10035 pedwarn (input_location, OPT_Wpedantic,
10036 "compound-statement in constexpr function");
10037 /* Begin the compound-statement. */
10038 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10039 /* If the next keyword is `__label__' we have a label declaration. */
10040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10041 cp_parser_label_declaration (parser);
10042 /* Parse an (optional) statement-seq. */
10043 cp_parser_statement_seq_opt (parser, in_statement_expr);
10044 /* Finish the compound-statement. */
10045 finish_compound_stmt (compound_stmt);
10046 /* Consume the `}'. */
10047 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10049 return compound_stmt;
10052 /* Parse an (optional) statement-seq.
10054 statement-seq:
10055 statement
10056 statement-seq [opt] statement */
10058 static void
10059 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10061 /* Scan statements until there aren't any more. */
10062 while (true)
10064 cp_token *token = cp_lexer_peek_token (parser->lexer);
10066 /* If we are looking at a `}', then we have run out of
10067 statements; the same is true if we have reached the end
10068 of file, or have stumbled upon a stray '@end'. */
10069 if (token->type == CPP_CLOSE_BRACE
10070 || token->type == CPP_EOF
10071 || token->type == CPP_PRAGMA_EOL
10072 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10073 break;
10075 /* If we are in a compound statement and find 'else' then
10076 something went wrong. */
10077 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10079 if (parser->in_statement & IN_IF_STMT)
10080 break;
10081 else
10083 token = cp_lexer_consume_token (parser->lexer);
10084 error_at (token->location, "%<else%> without a previous %<if%>");
10088 /* Parse the statement. */
10089 cp_parser_statement (parser, in_statement_expr, true, NULL);
10093 /* Parse a selection-statement.
10095 selection-statement:
10096 if ( condition ) statement
10097 if ( condition ) statement else statement
10098 switch ( condition ) statement
10100 Returns the new IF_STMT or SWITCH_STMT.
10102 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10103 is a (possibly labeled) if statement which is not enclosed in
10104 braces and has an else clause. This is used to implement
10105 -Wparentheses. */
10107 static tree
10108 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10110 cp_token *token;
10111 enum rid keyword;
10113 if (if_p != NULL)
10114 *if_p = false;
10116 /* Peek at the next token. */
10117 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10119 /* See what kind of keyword it is. */
10120 keyword = token->keyword;
10121 switch (keyword)
10123 case RID_IF:
10124 case RID_SWITCH:
10126 tree statement;
10127 tree condition;
10129 /* Look for the `('. */
10130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10132 cp_parser_skip_to_end_of_statement (parser);
10133 return error_mark_node;
10136 /* Begin the selection-statement. */
10137 if (keyword == RID_IF)
10138 statement = begin_if_stmt ();
10139 else
10140 statement = begin_switch_stmt ();
10142 /* Parse the condition. */
10143 condition = cp_parser_condition (parser);
10144 /* Look for the `)'. */
10145 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10146 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10147 /*consume_paren=*/true);
10149 if (keyword == RID_IF)
10151 bool nested_if;
10152 unsigned char in_statement;
10154 /* Add the condition. */
10155 finish_if_stmt_cond (condition, statement);
10157 /* Parse the then-clause. */
10158 in_statement = parser->in_statement;
10159 parser->in_statement |= IN_IF_STMT;
10160 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10162 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10163 add_stmt (build_empty_stmt (loc));
10164 cp_lexer_consume_token (parser->lexer);
10165 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10166 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10167 "empty body in an %<if%> statement");
10168 nested_if = false;
10170 else
10171 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10172 token->location, "if");
10173 parser->in_statement = in_statement;
10175 finish_then_clause (statement);
10177 /* If the next token is `else', parse the else-clause. */
10178 if (cp_lexer_next_token_is_keyword (parser->lexer,
10179 RID_ELSE))
10181 /* Consume the `else' keyword. */
10182 location_t else_tok_loc
10183 = cp_lexer_consume_token (parser->lexer)->location;
10184 begin_else_clause (statement);
10185 /* Parse the else-clause. */
10186 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10188 location_t loc;
10189 loc = cp_lexer_peek_token (parser->lexer)->location;
10190 warning_at (loc,
10191 OPT_Wempty_body, "suggest braces around "
10192 "empty body in an %<else%> statement");
10193 add_stmt (build_empty_stmt (loc));
10194 cp_lexer_consume_token (parser->lexer);
10196 else
10197 cp_parser_implicitly_scoped_statement (parser, NULL,
10198 else_tok_loc, "else");
10200 finish_else_clause (statement);
10202 /* If we are currently parsing a then-clause, then
10203 IF_P will not be NULL. We set it to true to
10204 indicate that this if statement has an else clause.
10205 This may trigger the Wparentheses warning below
10206 when we get back up to the parent if statement. */
10207 if (if_p != NULL)
10208 *if_p = true;
10210 else
10212 /* This if statement does not have an else clause. If
10213 NESTED_IF is true, then the then-clause is an if
10214 statement which does have an else clause. We warn
10215 about the potential ambiguity. */
10216 if (nested_if)
10217 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10218 "suggest explicit braces to avoid ambiguous"
10219 " %<else%>");
10222 /* Now we're all done with the if-statement. */
10223 finish_if_stmt (statement);
10225 else
10227 bool in_switch_statement_p;
10228 unsigned char in_statement;
10230 /* Add the condition. */
10231 finish_switch_cond (condition, statement);
10233 /* Parse the body of the switch-statement. */
10234 in_switch_statement_p = parser->in_switch_statement_p;
10235 in_statement = parser->in_statement;
10236 parser->in_switch_statement_p = true;
10237 parser->in_statement |= IN_SWITCH_STMT;
10238 cp_parser_implicitly_scoped_statement (parser, NULL,
10239 0, "switch");
10240 parser->in_switch_statement_p = in_switch_statement_p;
10241 parser->in_statement = in_statement;
10243 /* Now we're all done with the switch-statement. */
10244 finish_switch_stmt (statement);
10247 return statement;
10249 break;
10251 default:
10252 cp_parser_error (parser, "expected selection-statement");
10253 return error_mark_node;
10257 /* Parse a condition.
10259 condition:
10260 expression
10261 type-specifier-seq declarator = initializer-clause
10262 type-specifier-seq declarator braced-init-list
10264 GNU Extension:
10266 condition:
10267 type-specifier-seq declarator asm-specification [opt]
10268 attributes [opt] = assignment-expression
10270 Returns the expression that should be tested. */
10272 static tree
10273 cp_parser_condition (cp_parser* parser)
10275 cp_decl_specifier_seq type_specifiers;
10276 const char *saved_message;
10277 int declares_class_or_enum;
10279 /* Try the declaration first. */
10280 cp_parser_parse_tentatively (parser);
10281 /* New types are not allowed in the type-specifier-seq for a
10282 condition. */
10283 saved_message = parser->type_definition_forbidden_message;
10284 parser->type_definition_forbidden_message
10285 = G_("types may not be defined in conditions");
10286 /* Parse the type-specifier-seq. */
10287 cp_parser_decl_specifier_seq (parser,
10288 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10289 &type_specifiers,
10290 &declares_class_or_enum);
10291 /* Restore the saved message. */
10292 parser->type_definition_forbidden_message = saved_message;
10293 /* If all is well, we might be looking at a declaration. */
10294 if (!cp_parser_error_occurred (parser))
10296 tree decl;
10297 tree asm_specification;
10298 tree attributes;
10299 cp_declarator *declarator;
10300 tree initializer = NULL_TREE;
10302 /* Parse the declarator. */
10303 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10304 /*ctor_dtor_or_conv_p=*/NULL,
10305 /*parenthesized_p=*/NULL,
10306 /*member_p=*/false,
10307 /*friend_p=*/false);
10308 /* Parse the attributes. */
10309 attributes = cp_parser_attributes_opt (parser);
10310 /* Parse the asm-specification. */
10311 asm_specification = cp_parser_asm_specification_opt (parser);
10312 /* If the next token is not an `=' or '{', then we might still be
10313 looking at an expression. For example:
10315 if (A(a).x)
10317 looks like a decl-specifier-seq and a declarator -- but then
10318 there is no `=', so this is an expression. */
10319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10320 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10321 cp_parser_simulate_error (parser);
10323 /* If we did see an `=' or '{', then we are looking at a declaration
10324 for sure. */
10325 if (cp_parser_parse_definitely (parser))
10327 tree pushed_scope;
10328 bool non_constant_p;
10329 bool flags = LOOKUP_ONLYCONVERTING;
10331 /* Create the declaration. */
10332 decl = start_decl (declarator, &type_specifiers,
10333 /*initialized_p=*/true,
10334 attributes, /*prefix_attributes=*/NULL_TREE,
10335 &pushed_scope);
10337 /* Parse the initializer. */
10338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10340 initializer = cp_parser_braced_list (parser, &non_constant_p);
10341 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10342 flags = 0;
10344 else
10346 /* Consume the `='. */
10347 cp_parser_require (parser, CPP_EQ, RT_EQ);
10348 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10350 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10351 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10353 /* Process the initializer. */
10354 cp_finish_decl (decl,
10355 initializer, !non_constant_p,
10356 asm_specification,
10357 flags);
10359 if (pushed_scope)
10360 pop_scope (pushed_scope);
10362 return convert_from_reference (decl);
10365 /* If we didn't even get past the declarator successfully, we are
10366 definitely not looking at a declaration. */
10367 else
10368 cp_parser_abort_tentative_parse (parser);
10370 /* Otherwise, we are looking at an expression. */
10371 return cp_parser_expression (parser);
10374 /* Parses a for-statement or range-for-statement until the closing ')',
10375 not included. */
10377 static tree
10378 cp_parser_for (cp_parser *parser, bool ivdep)
10380 tree init, scope, decl;
10381 bool is_range_for;
10383 /* Begin the for-statement. */
10384 scope = begin_for_scope (&init);
10386 /* Parse the initialization. */
10387 is_range_for = cp_parser_for_init_statement (parser, &decl);
10389 if (is_range_for)
10390 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10391 else
10392 return cp_parser_c_for (parser, scope, init, ivdep);
10395 static tree
10396 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10398 /* Normal for loop */
10399 tree condition = NULL_TREE;
10400 tree expression = NULL_TREE;
10401 tree stmt;
10403 stmt = begin_for_stmt (scope, init);
10404 /* The for-init-statement has already been parsed in
10405 cp_parser_for_init_statement, so no work is needed here. */
10406 finish_for_init_stmt (stmt);
10408 /* If there's a condition, process it. */
10409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10410 condition = cp_parser_condition (parser);
10411 else if (ivdep)
10413 cp_parser_error (parser, "missing loop condition in loop with "
10414 "%<GCC ivdep%> pragma");
10415 condition = error_mark_node;
10417 finish_for_cond (condition, stmt, ivdep);
10418 /* Look for the `;'. */
10419 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10421 /* If there's an expression, process it. */
10422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10423 expression = cp_parser_expression (parser);
10424 finish_for_expr (expression, stmt);
10426 return stmt;
10429 /* Tries to parse a range-based for-statement:
10431 range-based-for:
10432 decl-specifier-seq declarator : expression
10434 The decl-specifier-seq declarator and the `:' are already parsed by
10435 cp_parser_for_init_statement. If processing_template_decl it returns a
10436 newly created RANGE_FOR_STMT; if not, it is converted to a
10437 regular FOR_STMT. */
10439 static tree
10440 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10441 bool ivdep)
10443 tree stmt, range_expr;
10445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10447 bool expr_non_constant_p;
10448 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10450 else
10451 range_expr = cp_parser_expression (parser);
10453 /* If in template, STMT is converted to a normal for-statement
10454 at instantiation. If not, it is done just ahead. */
10455 if (processing_template_decl)
10457 if (check_for_bare_parameter_packs (range_expr))
10458 range_expr = error_mark_node;
10459 stmt = begin_range_for_stmt (scope, init);
10460 if (ivdep)
10461 RANGE_FOR_IVDEP (stmt) = 1;
10462 finish_range_for_decl (stmt, range_decl, range_expr);
10463 if (!type_dependent_expression_p (range_expr)
10464 /* do_auto_deduction doesn't mess with template init-lists. */
10465 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10466 do_range_for_auto_deduction (range_decl, range_expr);
10468 else
10470 stmt = begin_for_stmt (scope, init);
10471 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10473 return stmt;
10476 /* Subroutine of cp_convert_range_for: given the initializer expression,
10477 builds up the range temporary. */
10479 static tree
10480 build_range_temp (tree range_expr)
10482 tree range_type, range_temp;
10484 /* Find out the type deduced by the declaration
10485 `auto &&__range = range_expr'. */
10486 range_type = cp_build_reference_type (make_auto (), true);
10487 range_type = do_auto_deduction (range_type, range_expr,
10488 type_uses_auto (range_type));
10490 /* Create the __range variable. */
10491 range_temp = build_decl (input_location, VAR_DECL,
10492 get_identifier ("__for_range"), range_type);
10493 TREE_USED (range_temp) = 1;
10494 DECL_ARTIFICIAL (range_temp) = 1;
10496 return range_temp;
10499 /* Used by cp_parser_range_for in template context: we aren't going to
10500 do a full conversion yet, but we still need to resolve auto in the
10501 type of the for-range-declaration if present. This is basically
10502 a shortcut version of cp_convert_range_for. */
10504 static void
10505 do_range_for_auto_deduction (tree decl, tree range_expr)
10507 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10508 if (auto_node)
10510 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10511 range_temp = convert_from_reference (build_range_temp (range_expr));
10512 iter_type = (cp_parser_perform_range_for_lookup
10513 (range_temp, &begin_dummy, &end_dummy));
10514 if (iter_type)
10516 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10517 iter_type);
10518 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10519 tf_warning_or_error);
10520 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10521 iter_decl, auto_node);
10526 /* Converts a range-based for-statement into a normal
10527 for-statement, as per the definition.
10529 for (RANGE_DECL : RANGE_EXPR)
10530 BLOCK
10532 should be equivalent to:
10535 auto &&__range = RANGE_EXPR;
10536 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10537 __begin != __end;
10538 ++__begin)
10540 RANGE_DECL = *__begin;
10541 BLOCK
10545 If RANGE_EXPR is an array:
10546 BEGIN_EXPR = __range
10547 END_EXPR = __range + ARRAY_SIZE(__range)
10548 Else if RANGE_EXPR has a member 'begin' or 'end':
10549 BEGIN_EXPR = __range.begin()
10550 END_EXPR = __range.end()
10551 Else:
10552 BEGIN_EXPR = begin(__range)
10553 END_EXPR = end(__range);
10555 If __range has a member 'begin' but not 'end', or vice versa, we must
10556 still use the second alternative (it will surely fail, however).
10557 When calling begin()/end() in the third alternative we must use
10558 argument dependent lookup, but always considering 'std' as an associated
10559 namespace. */
10561 tree
10562 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10563 bool ivdep)
10565 tree begin, end;
10566 tree iter_type, begin_expr, end_expr;
10567 tree condition, expression;
10569 if (range_decl == error_mark_node || range_expr == error_mark_node)
10570 /* If an error happened previously do nothing or else a lot of
10571 unhelpful errors would be issued. */
10572 begin_expr = end_expr = iter_type = error_mark_node;
10573 else
10575 tree range_temp;
10577 if (VAR_P (range_expr)
10578 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10579 /* Can't bind a reference to an array of runtime bound. */
10580 range_temp = range_expr;
10581 else
10583 range_temp = build_range_temp (range_expr);
10584 pushdecl (range_temp);
10585 cp_finish_decl (range_temp, range_expr,
10586 /*is_constant_init*/false, NULL_TREE,
10587 LOOKUP_ONLYCONVERTING);
10588 range_temp = convert_from_reference (range_temp);
10590 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10591 &begin_expr, &end_expr);
10594 /* The new for initialization statement. */
10595 begin = build_decl (input_location, VAR_DECL,
10596 get_identifier ("__for_begin"), iter_type);
10597 TREE_USED (begin) = 1;
10598 DECL_ARTIFICIAL (begin) = 1;
10599 pushdecl (begin);
10600 cp_finish_decl (begin, begin_expr,
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10604 end = build_decl (input_location, VAR_DECL,
10605 get_identifier ("__for_end"), iter_type);
10606 TREE_USED (end) = 1;
10607 DECL_ARTIFICIAL (end) = 1;
10608 pushdecl (end);
10609 cp_finish_decl (end, end_expr,
10610 /*is_constant_init*/false, NULL_TREE,
10611 LOOKUP_ONLYCONVERTING);
10613 finish_for_init_stmt (statement);
10615 /* The new for condition. */
10616 condition = build_x_binary_op (input_location, NE_EXPR,
10617 begin, ERROR_MARK,
10618 end, ERROR_MARK,
10619 NULL, tf_warning_or_error);
10620 finish_for_cond (condition, statement, ivdep);
10622 /* The new increment expression. */
10623 expression = finish_unary_op_expr (input_location,
10624 PREINCREMENT_EXPR, begin,
10625 tf_warning_or_error);
10626 finish_for_expr (expression, statement);
10628 /* The declaration is initialized with *__begin inside the loop body. */
10629 cp_finish_decl (range_decl,
10630 build_x_indirect_ref (input_location, begin, RO_NULL,
10631 tf_warning_or_error),
10632 /*is_constant_init*/false, NULL_TREE,
10633 LOOKUP_ONLYCONVERTING);
10635 return statement;
10638 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10639 We need to solve both at the same time because the method used
10640 depends on the existence of members begin or end.
10641 Returns the type deduced for the iterator expression. */
10643 static tree
10644 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10646 if (error_operand_p (range))
10648 *begin = *end = error_mark_node;
10649 return error_mark_node;
10652 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10654 error ("range-based %<for%> expression of type %qT "
10655 "has incomplete type", TREE_TYPE (range));
10656 *begin = *end = error_mark_node;
10657 return error_mark_node;
10659 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10661 /* If RANGE is an array, we will use pointer arithmetic. */
10662 *begin = range;
10663 *end = build_binary_op (input_location, PLUS_EXPR,
10664 range,
10665 array_type_nelts_top (TREE_TYPE (range)),
10667 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10669 else
10671 /* If it is not an array, we must do a bit of magic. */
10672 tree id_begin, id_end;
10673 tree member_begin, member_end;
10675 *begin = *end = error_mark_node;
10677 id_begin = get_identifier ("begin");
10678 id_end = get_identifier ("end");
10679 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10680 /*protect=*/2, /*want_type=*/false,
10681 tf_warning_or_error);
10682 member_end = lookup_member (TREE_TYPE (range), id_end,
10683 /*protect=*/2, /*want_type=*/false,
10684 tf_warning_or_error);
10686 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10688 /* Use the member functions. */
10689 if (member_begin != NULL_TREE)
10690 *begin = cp_parser_range_for_member_function (range, id_begin);
10691 else
10692 error ("range-based %<for%> expression of type %qT has an "
10693 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10695 if (member_end != NULL_TREE)
10696 *end = cp_parser_range_for_member_function (range, id_end);
10697 else
10698 error ("range-based %<for%> expression of type %qT has a "
10699 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10701 else
10703 /* Use global functions with ADL. */
10704 vec<tree, va_gc> *vec;
10705 vec = make_tree_vector ();
10707 vec_safe_push (vec, range);
10709 member_begin = perform_koenig_lookup (id_begin, vec,
10710 tf_warning_or_error);
10711 *begin = finish_call_expr (member_begin, &vec, false, true,
10712 tf_warning_or_error);
10713 member_end = perform_koenig_lookup (id_end, vec,
10714 tf_warning_or_error);
10715 *end = finish_call_expr (member_end, &vec, false, true,
10716 tf_warning_or_error);
10718 release_tree_vector (vec);
10721 /* Last common checks. */
10722 if (*begin == error_mark_node || *end == error_mark_node)
10724 /* If one of the expressions is an error do no more checks. */
10725 *begin = *end = error_mark_node;
10726 return error_mark_node;
10728 else if (type_dependent_expression_p (*begin)
10729 || type_dependent_expression_p (*end))
10730 /* Can happen, when, eg, in a template context, Koenig lookup
10731 can't resolve begin/end (c++/58503). */
10732 return NULL_TREE;
10733 else
10735 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10736 /* The unqualified type of the __begin and __end temporaries should
10737 be the same, as required by the multiple auto declaration. */
10738 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10739 error ("inconsistent begin/end types in range-based %<for%> "
10740 "statement: %qT and %qT",
10741 TREE_TYPE (*begin), TREE_TYPE (*end));
10742 return iter_type;
10747 /* Helper function for cp_parser_perform_range_for_lookup.
10748 Builds a tree for RANGE.IDENTIFIER(). */
10750 static tree
10751 cp_parser_range_for_member_function (tree range, tree identifier)
10753 tree member, res;
10754 vec<tree, va_gc> *vec;
10756 member = finish_class_member_access_expr (range, identifier,
10757 false, tf_warning_or_error);
10758 if (member == error_mark_node)
10759 return error_mark_node;
10761 vec = make_tree_vector ();
10762 res = finish_call_expr (member, &vec,
10763 /*disallow_virtual=*/false,
10764 /*koenig_p=*/false,
10765 tf_warning_or_error);
10766 release_tree_vector (vec);
10767 return res;
10770 /* Parse an iteration-statement.
10772 iteration-statement:
10773 while ( condition ) statement
10774 do statement while ( expression ) ;
10775 for ( for-init-statement condition [opt] ; expression [opt] )
10776 statement
10778 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10780 static tree
10781 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10783 cp_token *token;
10784 location_t tok_loc;
10785 enum rid keyword;
10786 tree statement;
10787 unsigned char in_statement;
10789 /* Peek at the next token. */
10790 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10791 if (!token)
10792 return error_mark_node;
10794 tok_loc = token->location;
10796 /* Remember whether or not we are already within an iteration
10797 statement. */
10798 in_statement = parser->in_statement;
10800 /* See what kind of keyword it is. */
10801 keyword = token->keyword;
10802 switch (keyword)
10804 case RID_WHILE:
10806 tree condition;
10808 /* Begin the while-statement. */
10809 statement = begin_while_stmt ();
10810 /* Look for the `('. */
10811 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10812 /* Parse the condition. */
10813 condition = cp_parser_condition (parser);
10814 finish_while_stmt_cond (condition, statement, ivdep);
10815 /* Look for the `)'. */
10816 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10817 /* Parse the dependent statement. */
10818 parser->in_statement = IN_ITERATION_STMT;
10819 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10820 parser->in_statement = in_statement;
10821 /* We're done with the while-statement. */
10822 finish_while_stmt (statement);
10824 break;
10826 case RID_DO:
10828 tree expression;
10830 /* Begin the do-statement. */
10831 statement = begin_do_stmt ();
10832 /* Parse the body of the do-statement. */
10833 parser->in_statement = IN_ITERATION_STMT;
10834 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10835 parser->in_statement = in_statement;
10836 finish_do_body (statement);
10837 /* Look for the `while' keyword. */
10838 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10839 /* Look for the `('. */
10840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10841 /* Parse the expression. */
10842 expression = cp_parser_expression (parser);
10843 /* We're done with the do-statement. */
10844 finish_do_stmt (expression, statement, ivdep);
10845 /* Look for the `)'. */
10846 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10847 /* Look for the `;'. */
10848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10850 break;
10852 case RID_FOR:
10854 /* Look for the `('. */
10855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10857 statement = cp_parser_for (parser, ivdep);
10859 /* Look for the `)'. */
10860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10862 /* Parse the body of the for-statement. */
10863 parser->in_statement = IN_ITERATION_STMT;
10864 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10865 parser->in_statement = in_statement;
10867 /* We're done with the for-statement. */
10868 finish_for_stmt (statement);
10870 break;
10872 default:
10873 cp_parser_error (parser, "expected iteration-statement");
10874 statement = error_mark_node;
10875 break;
10878 return statement;
10881 /* Parse a for-init-statement or the declarator of a range-based-for.
10882 Returns true if a range-based-for declaration is seen.
10884 for-init-statement:
10885 expression-statement
10886 simple-declaration */
10888 static bool
10889 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10891 /* If the next token is a `;', then we have an empty
10892 expression-statement. Grammatically, this is also a
10893 simple-declaration, but an invalid one, because it does not
10894 declare anything. Therefore, if we did not handle this case
10895 specially, we would issue an error message about an invalid
10896 declaration. */
10897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10899 bool is_range_for = false;
10900 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10902 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10903 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10905 /* N3994 -- for (id : init) ... */
10906 if (cxx_dialect < cxx1z)
10907 pedwarn (input_location, 0, "range-based for loop without a "
10908 "type-specifier only available with "
10909 "-std=c++1z or -std=gnu++1z");
10910 tree name = cp_parser_identifier (parser);
10911 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10912 *decl = build_decl (input_location, VAR_DECL, name, type);
10913 pushdecl (*decl);
10914 cp_lexer_consume_token (parser->lexer);
10915 return true;
10918 /* A colon is used in range-based for. */
10919 parser->colon_corrects_to_scope_p = false;
10921 /* We're going to speculatively look for a declaration, falling back
10922 to an expression, if necessary. */
10923 cp_parser_parse_tentatively (parser);
10924 /* Parse the declaration. */
10925 cp_parser_simple_declaration (parser,
10926 /*function_definition_allowed_p=*/false,
10927 decl);
10928 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10931 /* It is a range-for, consume the ':' */
10932 cp_lexer_consume_token (parser->lexer);
10933 is_range_for = true;
10934 if (cxx_dialect < cxx11)
10936 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10937 "range-based %<for%> loops only available with "
10938 "-std=c++11 or -std=gnu++11");
10939 *decl = error_mark_node;
10942 else
10943 /* The ';' is not consumed yet because we told
10944 cp_parser_simple_declaration not to. */
10945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10947 if (cp_parser_parse_definitely (parser))
10948 return is_range_for;
10949 /* If the tentative parse failed, then we shall need to look for an
10950 expression-statement. */
10952 /* If we are here, it is an expression-statement. */
10953 cp_parser_expression_statement (parser, NULL_TREE);
10954 return false;
10957 /* Parse a jump-statement.
10959 jump-statement:
10960 break ;
10961 continue ;
10962 return expression [opt] ;
10963 return braced-init-list ;
10964 goto identifier ;
10966 GNU extension:
10968 jump-statement:
10969 goto * expression ;
10971 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10973 static tree
10974 cp_parser_jump_statement (cp_parser* parser)
10976 tree statement = error_mark_node;
10977 cp_token *token;
10978 enum rid keyword;
10979 unsigned char in_statement;
10981 /* Peek at the next token. */
10982 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10983 if (!token)
10984 return error_mark_node;
10986 /* See what kind of keyword it is. */
10987 keyword = token->keyword;
10988 switch (keyword)
10990 case RID_BREAK:
10991 in_statement = parser->in_statement & ~IN_IF_STMT;
10992 switch (in_statement)
10994 case 0:
10995 error_at (token->location, "break statement not within loop or switch");
10996 break;
10997 default:
10998 gcc_assert ((in_statement & IN_SWITCH_STMT)
10999 || in_statement == IN_ITERATION_STMT);
11000 statement = finish_break_stmt ();
11001 if (in_statement == IN_ITERATION_STMT)
11002 break_maybe_infinite_loop ();
11003 break;
11004 case IN_OMP_BLOCK:
11005 error_at (token->location, "invalid exit from OpenMP structured block");
11006 break;
11007 case IN_OMP_FOR:
11008 error_at (token->location, "break statement used with OpenMP for loop");
11009 break;
11010 case IN_CILK_SIMD_FOR:
11011 error_at (token->location, "break statement used with Cilk Plus for loop");
11012 break;
11014 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11015 break;
11017 case RID_CONTINUE:
11018 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11020 case 0:
11021 error_at (token->location, "continue statement not within a loop");
11022 break;
11023 case IN_CILK_SIMD_FOR:
11024 error_at (token->location,
11025 "continue statement within %<#pragma simd%> loop body");
11026 /* Fall through. */
11027 case IN_ITERATION_STMT:
11028 case IN_OMP_FOR:
11029 statement = finish_continue_stmt ();
11030 break;
11031 case IN_OMP_BLOCK:
11032 error_at (token->location, "invalid exit from OpenMP structured block");
11033 break;
11034 default:
11035 gcc_unreachable ();
11037 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11038 break;
11040 case RID_RETURN:
11042 tree expr;
11043 bool expr_non_constant_p;
11045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11047 cp_lexer_set_source_position (parser->lexer);
11048 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11049 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11051 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11052 expr = cp_parser_expression (parser);
11053 else
11054 /* If the next token is a `;', then there is no
11055 expression. */
11056 expr = NULL_TREE;
11057 /* Build the return-statement. */
11058 statement = finish_return_stmt (expr);
11059 /* Look for the final `;'. */
11060 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11062 break;
11064 case RID_GOTO:
11065 if (parser->in_function_body
11066 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11068 error ("%<goto%> in %<constexpr%> function");
11069 cp_function_chain->invalid_constexpr = true;
11072 /* Create the goto-statement. */
11073 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11075 /* Issue a warning about this use of a GNU extension. */
11076 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11077 /* Consume the '*' token. */
11078 cp_lexer_consume_token (parser->lexer);
11079 /* Parse the dependent expression. */
11080 finish_goto_stmt (cp_parser_expression (parser));
11082 else
11083 finish_goto_stmt (cp_parser_identifier (parser));
11084 /* Look for the final `;'. */
11085 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11086 break;
11088 default:
11089 cp_parser_error (parser, "expected jump-statement");
11090 break;
11093 return statement;
11096 /* Parse a declaration-statement.
11098 declaration-statement:
11099 block-declaration */
11101 static void
11102 cp_parser_declaration_statement (cp_parser* parser)
11104 void *p;
11106 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11107 p = obstack_alloc (&declarator_obstack, 0);
11109 /* Parse the block-declaration. */
11110 cp_parser_block_declaration (parser, /*statement_p=*/true);
11112 /* Free any declarators allocated. */
11113 obstack_free (&declarator_obstack, p);
11116 /* Some dependent statements (like `if (cond) statement'), are
11117 implicitly in their own scope. In other words, if the statement is
11118 a single statement (as opposed to a compound-statement), it is
11119 none-the-less treated as if it were enclosed in braces. Any
11120 declarations appearing in the dependent statement are out of scope
11121 after control passes that point. This function parses a statement,
11122 but ensures that is in its own scope, even if it is not a
11123 compound-statement.
11125 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11126 is a (possibly labeled) if statement which is not enclosed in
11127 braces and has an else clause. This is used to implement
11128 -Wparentheses.
11130 Returns the new statement. */
11132 static tree
11133 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11134 location_t guard_loc,
11135 const char *guard_kind)
11137 tree statement;
11139 if (if_p != NULL)
11140 *if_p = false;
11142 /* Mark if () ; with a special NOP_EXPR. */
11143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11145 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11146 cp_lexer_consume_token (parser->lexer);
11147 statement = add_stmt (build_empty_stmt (loc));
11149 /* if a compound is opened, we simply parse the statement directly. */
11150 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11151 statement = cp_parser_compound_statement (parser, NULL, false, false);
11152 /* If the token is not a `{', then we must take special action. */
11153 else
11155 /* Create a compound-statement. */
11156 statement = begin_compound_stmt (0);
11157 /* Parse the dependent-statement. */
11158 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11159 cp_parser_statement (parser, NULL_TREE, false, if_p);
11160 /* Finish the dummy compound-statement. */
11161 finish_compound_stmt (statement);
11162 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11163 if (next_tok->keyword != RID_ELSE)
11165 location_t next_stmt_loc = next_tok->location;
11166 warn_for_misleading_indentation (guard_loc, body_loc,
11167 next_stmt_loc, next_tok->type,
11168 guard_kind);
11172 /* Return the statement. */
11173 return statement;
11176 /* For some dependent statements (like `while (cond) statement'), we
11177 have already created a scope. Therefore, even if the dependent
11178 statement is a compound-statement, we do not want to create another
11179 scope. */
11181 static void
11182 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11183 const char *guard_kind)
11185 /* If the token is a `{', then we must take special action. */
11186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11188 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11189 cp_parser_statement (parser, NULL_TREE, false, NULL);
11190 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11191 location_t next_stmt_loc = next_tok->location;
11192 warn_for_misleading_indentation (guard_loc, body_loc,
11193 next_stmt_loc, next_tok->type,
11194 guard_kind);
11196 else
11198 /* Avoid calling cp_parser_compound_statement, so that we
11199 don't create a new scope. Do everything else by hand. */
11200 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11201 /* If the next keyword is `__label__' we have a label declaration. */
11202 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11203 cp_parser_label_declaration (parser);
11204 /* Parse an (optional) statement-seq. */
11205 cp_parser_statement_seq_opt (parser, NULL_TREE);
11206 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11210 /* Declarations [gram.dcl.dcl] */
11212 /* Parse an optional declaration-sequence.
11214 declaration-seq:
11215 declaration
11216 declaration-seq declaration */
11218 static void
11219 cp_parser_declaration_seq_opt (cp_parser* parser)
11221 while (true)
11223 cp_token *token;
11225 token = cp_lexer_peek_token (parser->lexer);
11227 if (token->type == CPP_CLOSE_BRACE
11228 || token->type == CPP_EOF
11229 || token->type == CPP_PRAGMA_EOL)
11230 break;
11232 if (token->type == CPP_SEMICOLON)
11234 /* A declaration consisting of a single semicolon is
11235 invalid. Allow it unless we're being pedantic. */
11236 cp_lexer_consume_token (parser->lexer);
11237 if (!in_system_header_at (input_location))
11238 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11239 continue;
11242 /* If we're entering or exiting a region that's implicitly
11243 extern "C", modify the lang context appropriately. */
11244 if (!parser->implicit_extern_c && token->implicit_extern_c)
11246 push_lang_context (lang_name_c);
11247 parser->implicit_extern_c = true;
11249 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11251 pop_lang_context ();
11252 parser->implicit_extern_c = false;
11255 if (token->type == CPP_PRAGMA)
11257 /* A top-level declaration can consist solely of a #pragma.
11258 A nested declaration cannot, so this is done here and not
11259 in cp_parser_declaration. (A #pragma at block scope is
11260 handled in cp_parser_statement.) */
11261 cp_parser_pragma (parser, pragma_external);
11262 continue;
11265 /* Parse the declaration itself. */
11266 cp_parser_declaration (parser);
11270 /* Parse a declaration.
11272 declaration:
11273 block-declaration
11274 function-definition
11275 template-declaration
11276 explicit-instantiation
11277 explicit-specialization
11278 linkage-specification
11279 namespace-definition
11281 GNU extension:
11283 declaration:
11284 __extension__ declaration */
11286 static void
11287 cp_parser_declaration (cp_parser* parser)
11289 cp_token token1;
11290 cp_token token2;
11291 int saved_pedantic;
11292 void *p;
11293 tree attributes = NULL_TREE;
11295 /* Check for the `__extension__' keyword. */
11296 if (cp_parser_extension_opt (parser, &saved_pedantic))
11298 /* Parse the qualified declaration. */
11299 cp_parser_declaration (parser);
11300 /* Restore the PEDANTIC flag. */
11301 pedantic = saved_pedantic;
11303 return;
11306 /* Try to figure out what kind of declaration is present. */
11307 token1 = *cp_lexer_peek_token (parser->lexer);
11309 if (token1.type != CPP_EOF)
11310 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11311 else
11313 token2.type = CPP_EOF;
11314 token2.keyword = RID_MAX;
11317 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11318 p = obstack_alloc (&declarator_obstack, 0);
11320 /* If the next token is `extern' and the following token is a string
11321 literal, then we have a linkage specification. */
11322 if (token1.keyword == RID_EXTERN
11323 && cp_parser_is_pure_string_literal (&token2))
11324 cp_parser_linkage_specification (parser);
11325 /* If the next token is `template', then we have either a template
11326 declaration, an explicit instantiation, or an explicit
11327 specialization. */
11328 else if (token1.keyword == RID_TEMPLATE)
11330 /* `template <>' indicates a template specialization. */
11331 if (token2.type == CPP_LESS
11332 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11333 cp_parser_explicit_specialization (parser);
11334 /* `template <' indicates a template declaration. */
11335 else if (token2.type == CPP_LESS)
11336 cp_parser_template_declaration (parser, /*member_p=*/false);
11337 /* Anything else must be an explicit instantiation. */
11338 else
11339 cp_parser_explicit_instantiation (parser);
11341 /* If the next token is `export', then we have a template
11342 declaration. */
11343 else if (token1.keyword == RID_EXPORT)
11344 cp_parser_template_declaration (parser, /*member_p=*/false);
11345 /* If the next token is `extern', 'static' or 'inline' and the one
11346 after that is `template', we have a GNU extended explicit
11347 instantiation directive. */
11348 else if (cp_parser_allow_gnu_extensions_p (parser)
11349 && (token1.keyword == RID_EXTERN
11350 || token1.keyword == RID_STATIC
11351 || token1.keyword == RID_INLINE)
11352 && token2.keyword == RID_TEMPLATE)
11353 cp_parser_explicit_instantiation (parser);
11354 /* If the next token is `namespace', check for a named or unnamed
11355 namespace definition. */
11356 else if (token1.keyword == RID_NAMESPACE
11357 && (/* A named namespace definition. */
11358 (token2.type == CPP_NAME
11359 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11360 != CPP_EQ))
11361 /* An unnamed namespace definition. */
11362 || token2.type == CPP_OPEN_BRACE
11363 || token2.keyword == RID_ATTRIBUTE))
11364 cp_parser_namespace_definition (parser);
11365 /* An inline (associated) namespace definition. */
11366 else if (token1.keyword == RID_INLINE
11367 && token2.keyword == RID_NAMESPACE)
11368 cp_parser_namespace_definition (parser);
11369 /* Objective-C++ declaration/definition. */
11370 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11371 cp_parser_objc_declaration (parser, NULL_TREE);
11372 else if (c_dialect_objc ()
11373 && token1.keyword == RID_ATTRIBUTE
11374 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11375 cp_parser_objc_declaration (parser, attributes);
11376 /* We must have either a block declaration or a function
11377 definition. */
11378 else
11379 /* Try to parse a block-declaration, or a function-definition. */
11380 cp_parser_block_declaration (parser, /*statement_p=*/false);
11382 /* Free any declarators allocated. */
11383 obstack_free (&declarator_obstack, p);
11386 /* Parse a block-declaration.
11388 block-declaration:
11389 simple-declaration
11390 asm-definition
11391 namespace-alias-definition
11392 using-declaration
11393 using-directive
11395 GNU Extension:
11397 block-declaration:
11398 __extension__ block-declaration
11400 C++0x Extension:
11402 block-declaration:
11403 static_assert-declaration
11405 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11406 part of a declaration-statement. */
11408 static void
11409 cp_parser_block_declaration (cp_parser *parser,
11410 bool statement_p)
11412 cp_token *token1;
11413 int saved_pedantic;
11415 /* Check for the `__extension__' keyword. */
11416 if (cp_parser_extension_opt (parser, &saved_pedantic))
11418 /* Parse the qualified declaration. */
11419 cp_parser_block_declaration (parser, statement_p);
11420 /* Restore the PEDANTIC flag. */
11421 pedantic = saved_pedantic;
11423 return;
11426 /* Peek at the next token to figure out which kind of declaration is
11427 present. */
11428 token1 = cp_lexer_peek_token (parser->lexer);
11430 /* If the next keyword is `asm', we have an asm-definition. */
11431 if (token1->keyword == RID_ASM)
11433 if (statement_p)
11434 cp_parser_commit_to_tentative_parse (parser);
11435 cp_parser_asm_definition (parser);
11437 /* If the next keyword is `namespace', we have a
11438 namespace-alias-definition. */
11439 else if (token1->keyword == RID_NAMESPACE)
11440 cp_parser_namespace_alias_definition (parser);
11441 /* If the next keyword is `using', we have a
11442 using-declaration, a using-directive, or an alias-declaration. */
11443 else if (token1->keyword == RID_USING)
11445 cp_token *token2;
11447 if (statement_p)
11448 cp_parser_commit_to_tentative_parse (parser);
11449 /* If the token after `using' is `namespace', then we have a
11450 using-directive. */
11451 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11452 if (token2->keyword == RID_NAMESPACE)
11453 cp_parser_using_directive (parser);
11454 /* If the second token after 'using' is '=', then we have an
11455 alias-declaration. */
11456 else if (cxx_dialect >= cxx11
11457 && token2->type == CPP_NAME
11458 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11459 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11460 cp_parser_alias_declaration (parser);
11461 /* Otherwise, it's a using-declaration. */
11462 else
11463 cp_parser_using_declaration (parser,
11464 /*access_declaration_p=*/false);
11466 /* If the next keyword is `__label__' we have a misplaced label
11467 declaration. */
11468 else if (token1->keyword == RID_LABEL)
11470 cp_lexer_consume_token (parser->lexer);
11471 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11472 cp_parser_skip_to_end_of_statement (parser);
11473 /* If the next token is now a `;', consume it. */
11474 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11475 cp_lexer_consume_token (parser->lexer);
11477 /* If the next token is `static_assert' we have a static assertion. */
11478 else if (token1->keyword == RID_STATIC_ASSERT)
11479 cp_parser_static_assert (parser, /*member_p=*/false);
11480 /* Anything else must be a simple-declaration. */
11481 else
11482 cp_parser_simple_declaration (parser, !statement_p,
11483 /*maybe_range_for_decl*/NULL);
11486 /* Parse a simple-declaration.
11488 simple-declaration:
11489 decl-specifier-seq [opt] init-declarator-list [opt] ;
11491 init-declarator-list:
11492 init-declarator
11493 init-declarator-list , init-declarator
11495 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11496 function-definition as a simple-declaration.
11498 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11499 parsed declaration if it is an uninitialized single declarator not followed
11500 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11501 if present, will not be consumed. */
11503 static void
11504 cp_parser_simple_declaration (cp_parser* parser,
11505 bool function_definition_allowed_p,
11506 tree *maybe_range_for_decl)
11508 cp_decl_specifier_seq decl_specifiers;
11509 int declares_class_or_enum;
11510 bool saw_declarator;
11511 location_t comma_loc = UNKNOWN_LOCATION;
11512 location_t init_loc = UNKNOWN_LOCATION;
11514 if (maybe_range_for_decl)
11515 *maybe_range_for_decl = NULL_TREE;
11517 /* Defer access checks until we know what is being declared; the
11518 checks for names appearing in the decl-specifier-seq should be
11519 done as if we were in the scope of the thing being declared. */
11520 push_deferring_access_checks (dk_deferred);
11522 /* Parse the decl-specifier-seq. We have to keep track of whether
11523 or not the decl-specifier-seq declares a named class or
11524 enumeration type, since that is the only case in which the
11525 init-declarator-list is allowed to be empty.
11527 [dcl.dcl]
11529 In a simple-declaration, the optional init-declarator-list can be
11530 omitted only when declaring a class or enumeration, that is when
11531 the decl-specifier-seq contains either a class-specifier, an
11532 elaborated-type-specifier, or an enum-specifier. */
11533 cp_parser_decl_specifier_seq (parser,
11534 CP_PARSER_FLAGS_OPTIONAL,
11535 &decl_specifiers,
11536 &declares_class_or_enum);
11537 /* We no longer need to defer access checks. */
11538 stop_deferring_access_checks ();
11540 /* In a block scope, a valid declaration must always have a
11541 decl-specifier-seq. By not trying to parse declarators, we can
11542 resolve the declaration/expression ambiguity more quickly. */
11543 if (!function_definition_allowed_p
11544 && !decl_specifiers.any_specifiers_p)
11546 cp_parser_error (parser, "expected declaration");
11547 goto done;
11550 /* If the next two tokens are both identifiers, the code is
11551 erroneous. The usual cause of this situation is code like:
11553 T t;
11555 where "T" should name a type -- but does not. */
11556 if (!decl_specifiers.any_type_specifiers_p
11557 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11559 /* If parsing tentatively, we should commit; we really are
11560 looking at a declaration. */
11561 cp_parser_commit_to_tentative_parse (parser);
11562 /* Give up. */
11563 goto done;
11566 /* If we have seen at least one decl-specifier, and the next token
11567 is not a parenthesis, then we must be looking at a declaration.
11568 (After "int (" we might be looking at a functional cast.) */
11569 if (decl_specifiers.any_specifiers_p
11570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11571 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11572 && !cp_parser_error_occurred (parser))
11573 cp_parser_commit_to_tentative_parse (parser);
11575 /* Keep going until we hit the `;' at the end of the simple
11576 declaration. */
11577 saw_declarator = false;
11578 while (cp_lexer_next_token_is_not (parser->lexer,
11579 CPP_SEMICOLON))
11581 cp_token *token;
11582 bool function_definition_p;
11583 tree decl;
11585 if (saw_declarator)
11587 /* If we are processing next declarator, comma is expected */
11588 token = cp_lexer_peek_token (parser->lexer);
11589 gcc_assert (token->type == CPP_COMMA);
11590 cp_lexer_consume_token (parser->lexer);
11591 if (maybe_range_for_decl)
11593 *maybe_range_for_decl = error_mark_node;
11594 if (comma_loc == UNKNOWN_LOCATION)
11595 comma_loc = token->location;
11598 else
11599 saw_declarator = true;
11601 /* Parse the init-declarator. */
11602 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11603 /*checks=*/NULL,
11604 function_definition_allowed_p,
11605 /*member_p=*/false,
11606 declares_class_or_enum,
11607 &function_definition_p,
11608 maybe_range_for_decl,
11609 &init_loc);
11610 /* If an error occurred while parsing tentatively, exit quickly.
11611 (That usually happens when in the body of a function; each
11612 statement is treated as a declaration-statement until proven
11613 otherwise.) */
11614 if (cp_parser_error_occurred (parser))
11615 goto done;
11616 /* Handle function definitions specially. */
11617 if (function_definition_p)
11619 /* If the next token is a `,', then we are probably
11620 processing something like:
11622 void f() {}, *p;
11624 which is erroneous. */
11625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11627 cp_token *token = cp_lexer_peek_token (parser->lexer);
11628 error_at (token->location,
11629 "mixing"
11630 " declarations and function-definitions is forbidden");
11632 /* Otherwise, we're done with the list of declarators. */
11633 else
11635 pop_deferring_access_checks ();
11636 return;
11639 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11640 *maybe_range_for_decl = decl;
11641 /* The next token should be either a `,' or a `;'. */
11642 token = cp_lexer_peek_token (parser->lexer);
11643 /* If it's a `,', there are more declarators to come. */
11644 if (token->type == CPP_COMMA)
11645 /* will be consumed next time around */;
11646 /* If it's a `;', we are done. */
11647 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11648 break;
11649 /* Anything else is an error. */
11650 else
11652 /* If we have already issued an error message we don't need
11653 to issue another one. */
11654 if (decl != error_mark_node
11655 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11656 cp_parser_error (parser, "expected %<,%> or %<;%>");
11657 /* Skip tokens until we reach the end of the statement. */
11658 cp_parser_skip_to_end_of_statement (parser);
11659 /* If the next token is now a `;', consume it. */
11660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11661 cp_lexer_consume_token (parser->lexer);
11662 goto done;
11664 /* After the first time around, a function-definition is not
11665 allowed -- even if it was OK at first. For example:
11667 int i, f() {}
11669 is not valid. */
11670 function_definition_allowed_p = false;
11673 /* Issue an error message if no declarators are present, and the
11674 decl-specifier-seq does not itself declare a class or
11675 enumeration: [dcl.dcl]/3. */
11676 if (!saw_declarator)
11678 if (cp_parser_declares_only_class_p (parser))
11680 if (!declares_class_or_enum
11681 && decl_specifiers.type
11682 && OVERLOAD_TYPE_P (decl_specifiers.type))
11683 /* Ensure an error is issued anyway when finish_decltype_type,
11684 called via cp_parser_decl_specifier_seq, returns a class or
11685 an enumeration (c++/51786). */
11686 decl_specifiers.type = NULL_TREE;
11687 shadow_tag (&decl_specifiers);
11689 /* Perform any deferred access checks. */
11690 perform_deferred_access_checks (tf_warning_or_error);
11693 /* Consume the `;'. */
11694 if (!maybe_range_for_decl)
11695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11696 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11698 if (init_loc != UNKNOWN_LOCATION)
11699 error_at (init_loc, "initializer in range-based %<for%> loop");
11700 if (comma_loc != UNKNOWN_LOCATION)
11701 error_at (comma_loc,
11702 "multiple declarations in range-based %<for%> loop");
11705 done:
11706 pop_deferring_access_checks ();
11709 /* Parse a decl-specifier-seq.
11711 decl-specifier-seq:
11712 decl-specifier-seq [opt] decl-specifier
11713 decl-specifier attribute-specifier-seq [opt] (C++11)
11715 decl-specifier:
11716 storage-class-specifier
11717 type-specifier
11718 function-specifier
11719 friend
11720 typedef
11722 GNU Extension:
11724 decl-specifier:
11725 attributes
11727 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11729 The parser flags FLAGS is used to control type-specifier parsing.
11731 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11732 flags:
11734 1: one of the decl-specifiers is an elaborated-type-specifier
11735 (i.e., a type declaration)
11736 2: one of the decl-specifiers is an enum-specifier or a
11737 class-specifier (i.e., a type definition)
11741 static void
11742 cp_parser_decl_specifier_seq (cp_parser* parser,
11743 cp_parser_flags flags,
11744 cp_decl_specifier_seq *decl_specs,
11745 int* declares_class_or_enum)
11747 bool constructor_possible_p = !parser->in_declarator_p;
11748 bool found_decl_spec = false;
11749 cp_token *start_token = NULL;
11750 cp_decl_spec ds;
11752 /* Clear DECL_SPECS. */
11753 clear_decl_specs (decl_specs);
11755 /* Assume no class or enumeration type is declared. */
11756 *declares_class_or_enum = 0;
11758 /* Keep reading specifiers until there are no more to read. */
11759 while (true)
11761 bool constructor_p;
11762 cp_token *token;
11763 ds = ds_last;
11765 /* Peek at the next token. */
11766 token = cp_lexer_peek_token (parser->lexer);
11768 /* Save the first token of the decl spec list for error
11769 reporting. */
11770 if (!start_token)
11771 start_token = token;
11772 /* Handle attributes. */
11773 if (cp_next_tokens_can_be_attribute_p (parser))
11775 /* Parse the attributes. */
11776 tree attrs = cp_parser_attributes_opt (parser);
11778 /* In a sequence of declaration specifiers, c++11 attributes
11779 appertain to the type that precede them. In that case
11780 [dcl.spec]/1 says:
11782 The attribute-specifier-seq affects the type only for
11783 the declaration it appears in, not other declarations
11784 involving the same type.
11786 But for now let's force the user to position the
11787 attribute either at the beginning of the declaration or
11788 after the declarator-id, which would clearly mean that it
11789 applies to the declarator. */
11790 if (cxx11_attribute_p (attrs))
11792 if (!found_decl_spec)
11793 /* The c++11 attribute is at the beginning of the
11794 declaration. It appertains to the entity being
11795 declared. */;
11796 else
11798 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11800 /* This is an attribute following a
11801 class-specifier. */
11802 if (decl_specs->type_definition_p)
11803 warn_misplaced_attr_for_class_type (token->location,
11804 decl_specs->type);
11805 attrs = NULL_TREE;
11807 else
11809 decl_specs->std_attributes
11810 = chainon (decl_specs->std_attributes,
11811 attrs);
11812 if (decl_specs->locations[ds_std_attribute] == 0)
11813 decl_specs->locations[ds_std_attribute] = token->location;
11815 continue;
11819 decl_specs->attributes
11820 = chainon (decl_specs->attributes,
11821 attrs);
11822 if (decl_specs->locations[ds_attribute] == 0)
11823 decl_specs->locations[ds_attribute] = token->location;
11824 continue;
11826 /* Assume we will find a decl-specifier keyword. */
11827 found_decl_spec = true;
11828 /* If the next token is an appropriate keyword, we can simply
11829 add it to the list. */
11830 switch (token->keyword)
11832 /* decl-specifier:
11833 friend
11834 constexpr */
11835 case RID_FRIEND:
11836 if (!at_class_scope_p ())
11838 error_at (token->location, "%<friend%> used outside of class");
11839 cp_lexer_purge_token (parser->lexer);
11841 else
11843 ds = ds_friend;
11844 /* Consume the token. */
11845 cp_lexer_consume_token (parser->lexer);
11847 break;
11849 case RID_CONSTEXPR:
11850 ds = ds_constexpr;
11851 cp_lexer_consume_token (parser->lexer);
11852 break;
11854 /* function-specifier:
11855 inline
11856 virtual
11857 explicit */
11858 case RID_INLINE:
11859 case RID_VIRTUAL:
11860 case RID_EXPLICIT:
11861 cp_parser_function_specifier_opt (parser, decl_specs);
11862 break;
11864 /* decl-specifier:
11865 typedef */
11866 case RID_TYPEDEF:
11867 ds = ds_typedef;
11868 /* Consume the token. */
11869 cp_lexer_consume_token (parser->lexer);
11870 /* A constructor declarator cannot appear in a typedef. */
11871 constructor_possible_p = false;
11872 /* The "typedef" keyword can only occur in a declaration; we
11873 may as well commit at this point. */
11874 cp_parser_commit_to_tentative_parse (parser);
11876 if (decl_specs->storage_class != sc_none)
11877 decl_specs->conflicting_specifiers_p = true;
11878 break;
11880 /* storage-class-specifier:
11881 auto
11882 register
11883 static
11884 extern
11885 mutable
11887 GNU Extension:
11888 thread */
11889 case RID_AUTO:
11890 if (cxx_dialect == cxx98)
11892 /* Consume the token. */
11893 cp_lexer_consume_token (parser->lexer);
11895 /* Complain about `auto' as a storage specifier, if
11896 we're complaining about C++0x compatibility. */
11897 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11898 " changes meaning in C++11; please remove it");
11900 /* Set the storage class anyway. */
11901 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11902 token);
11904 else
11905 /* C++0x auto type-specifier. */
11906 found_decl_spec = false;
11907 break;
11909 case RID_REGISTER:
11910 case RID_STATIC:
11911 case RID_EXTERN:
11912 case RID_MUTABLE:
11913 /* Consume the token. */
11914 cp_lexer_consume_token (parser->lexer);
11915 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11916 token);
11917 break;
11918 case RID_THREAD:
11919 /* Consume the token. */
11920 ds = ds_thread;
11921 cp_lexer_consume_token (parser->lexer);
11922 break;
11924 default:
11925 /* We did not yet find a decl-specifier yet. */
11926 found_decl_spec = false;
11927 break;
11930 if (found_decl_spec
11931 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11932 && token->keyword != RID_CONSTEXPR)
11933 error ("decl-specifier invalid in condition");
11935 if (ds != ds_last)
11936 set_and_check_decl_spec_loc (decl_specs, ds, token);
11938 /* Constructors are a special case. The `S' in `S()' is not a
11939 decl-specifier; it is the beginning of the declarator. */
11940 constructor_p
11941 = (!found_decl_spec
11942 && constructor_possible_p
11943 && (cp_parser_constructor_declarator_p
11944 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11946 /* If we don't have a DECL_SPEC yet, then we must be looking at
11947 a type-specifier. */
11948 if (!found_decl_spec && !constructor_p)
11950 int decl_spec_declares_class_or_enum;
11951 bool is_cv_qualifier;
11952 tree type_spec;
11954 type_spec
11955 = cp_parser_type_specifier (parser, flags,
11956 decl_specs,
11957 /*is_declaration=*/true,
11958 &decl_spec_declares_class_or_enum,
11959 &is_cv_qualifier);
11960 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11962 /* If this type-specifier referenced a user-defined type
11963 (a typedef, class-name, etc.), then we can't allow any
11964 more such type-specifiers henceforth.
11966 [dcl.spec]
11968 The longest sequence of decl-specifiers that could
11969 possibly be a type name is taken as the
11970 decl-specifier-seq of a declaration. The sequence shall
11971 be self-consistent as described below.
11973 [dcl.type]
11975 As a general rule, at most one type-specifier is allowed
11976 in the complete decl-specifier-seq of a declaration. The
11977 only exceptions are the following:
11979 -- const or volatile can be combined with any other
11980 type-specifier.
11982 -- signed or unsigned can be combined with char, long,
11983 short, or int.
11985 -- ..
11987 Example:
11989 typedef char* Pc;
11990 void g (const int Pc);
11992 Here, Pc is *not* part of the decl-specifier seq; it's
11993 the declarator. Therefore, once we see a type-specifier
11994 (other than a cv-qualifier), we forbid any additional
11995 user-defined types. We *do* still allow things like `int
11996 int' to be considered a decl-specifier-seq, and issue the
11997 error message later. */
11998 if (type_spec && !is_cv_qualifier)
11999 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12000 /* A constructor declarator cannot follow a type-specifier. */
12001 if (type_spec)
12003 constructor_possible_p = false;
12004 found_decl_spec = true;
12005 if (!is_cv_qualifier)
12006 decl_specs->any_type_specifiers_p = true;
12010 /* If we still do not have a DECL_SPEC, then there are no more
12011 decl-specifiers. */
12012 if (!found_decl_spec)
12013 break;
12015 decl_specs->any_specifiers_p = true;
12016 /* After we see one decl-specifier, further decl-specifiers are
12017 always optional. */
12018 flags |= CP_PARSER_FLAGS_OPTIONAL;
12021 /* Don't allow a friend specifier with a class definition. */
12022 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12023 && (*declares_class_or_enum & 2))
12024 error_at (decl_specs->locations[ds_friend],
12025 "class definition may not be declared a friend");
12028 /* Parse an (optional) storage-class-specifier.
12030 storage-class-specifier:
12031 auto
12032 register
12033 static
12034 extern
12035 mutable
12037 GNU Extension:
12039 storage-class-specifier:
12040 thread
12042 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12044 static tree
12045 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12047 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12049 case RID_AUTO:
12050 if (cxx_dialect != cxx98)
12051 return NULL_TREE;
12052 /* Fall through for C++98. */
12054 case RID_REGISTER:
12055 case RID_STATIC:
12056 case RID_EXTERN:
12057 case RID_MUTABLE:
12058 case RID_THREAD:
12059 /* Consume the token. */
12060 return cp_lexer_consume_token (parser->lexer)->u.value;
12062 default:
12063 return NULL_TREE;
12067 /* Parse an (optional) function-specifier.
12069 function-specifier:
12070 inline
12071 virtual
12072 explicit
12074 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12075 Updates DECL_SPECS, if it is non-NULL. */
12077 static tree
12078 cp_parser_function_specifier_opt (cp_parser* parser,
12079 cp_decl_specifier_seq *decl_specs)
12081 cp_token *token = cp_lexer_peek_token (parser->lexer);
12082 switch (token->keyword)
12084 case RID_INLINE:
12085 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12086 break;
12088 case RID_VIRTUAL:
12089 /* 14.5.2.3 [temp.mem]
12091 A member function template shall not be virtual. */
12092 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12093 error_at (token->location, "templates may not be %<virtual%>");
12094 else
12095 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12096 break;
12098 case RID_EXPLICIT:
12099 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12100 break;
12102 default:
12103 return NULL_TREE;
12106 /* Consume the token. */
12107 return cp_lexer_consume_token (parser->lexer)->u.value;
12110 /* Parse a linkage-specification.
12112 linkage-specification:
12113 extern string-literal { declaration-seq [opt] }
12114 extern string-literal declaration */
12116 static void
12117 cp_parser_linkage_specification (cp_parser* parser)
12119 tree linkage;
12121 /* Look for the `extern' keyword. */
12122 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12124 /* Look for the string-literal. */
12125 linkage = cp_parser_string_literal (parser, false, false);
12127 /* Transform the literal into an identifier. If the literal is a
12128 wide-character string, or contains embedded NULs, then we can't
12129 handle it as the user wants. */
12130 if (strlen (TREE_STRING_POINTER (linkage))
12131 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12133 cp_parser_error (parser, "invalid linkage-specification");
12134 /* Assume C++ linkage. */
12135 linkage = lang_name_cplusplus;
12137 else
12138 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12140 /* We're now using the new linkage. */
12141 push_lang_context (linkage);
12143 /* If the next token is a `{', then we're using the first
12144 production. */
12145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12147 cp_ensure_no_omp_declare_simd (parser);
12149 /* Consume the `{' token. */
12150 cp_lexer_consume_token (parser->lexer);
12151 /* Parse the declarations. */
12152 cp_parser_declaration_seq_opt (parser);
12153 /* Look for the closing `}'. */
12154 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12156 /* Otherwise, there's just one declaration. */
12157 else
12159 bool saved_in_unbraced_linkage_specification_p;
12161 saved_in_unbraced_linkage_specification_p
12162 = parser->in_unbraced_linkage_specification_p;
12163 parser->in_unbraced_linkage_specification_p = true;
12164 cp_parser_declaration (parser);
12165 parser->in_unbraced_linkage_specification_p
12166 = saved_in_unbraced_linkage_specification_p;
12169 /* We're done with the linkage-specification. */
12170 pop_lang_context ();
12173 /* Parse a static_assert-declaration.
12175 static_assert-declaration:
12176 static_assert ( constant-expression , string-literal ) ;
12177 static_assert ( constant-expression ) ; (C++1Z)
12179 If MEMBER_P, this static_assert is a class member. */
12181 static void
12182 cp_parser_static_assert(cp_parser *parser, bool member_p)
12184 tree condition;
12185 tree message;
12186 cp_token *token;
12187 location_t saved_loc;
12188 bool dummy;
12190 /* Peek at the `static_assert' token so we can keep track of exactly
12191 where the static assertion started. */
12192 token = cp_lexer_peek_token (parser->lexer);
12193 saved_loc = token->location;
12195 /* Look for the `static_assert' keyword. */
12196 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12197 RT_STATIC_ASSERT))
12198 return;
12200 /* We know we are in a static assertion; commit to any tentative
12201 parse. */
12202 if (cp_parser_parsing_tentatively (parser))
12203 cp_parser_commit_to_tentative_parse (parser);
12205 /* Parse the `(' starting the static assertion condition. */
12206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12208 /* Parse the constant-expression. Allow a non-constant expression
12209 here in order to give better diagnostics in finish_static_assert. */
12210 condition =
12211 cp_parser_constant_expression (parser,
12212 /*allow_non_constant_p=*/true,
12213 /*non_constant_p=*/&dummy);
12215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12217 if (cxx_dialect < cxx1z)
12218 pedwarn (input_location, OPT_Wpedantic,
12219 "static_assert without a message "
12220 "only available with -std=c++1z or -std=gnu++1z");
12221 /* Eat the ')' */
12222 cp_lexer_consume_token (parser->lexer);
12223 message = build_string (1, "");
12224 TREE_TYPE (message) = char_array_type_node;
12225 fix_string_type (message);
12227 else
12229 /* Parse the separating `,'. */
12230 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12232 /* Parse the string-literal message. */
12233 message = cp_parser_string_literal (parser,
12234 /*translate=*/false,
12235 /*wide_ok=*/true);
12237 /* A `)' completes the static assertion. */
12238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12239 cp_parser_skip_to_closing_parenthesis (parser,
12240 /*recovering=*/true,
12241 /*or_comma=*/false,
12242 /*consume_paren=*/true);
12245 /* A semicolon terminates the declaration. */
12246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12248 /* Complete the static assertion, which may mean either processing
12249 the static assert now or saving it for template instantiation. */
12250 finish_static_assert (condition, message, saved_loc, member_p);
12253 /* Parse the expression in decltype ( expression ). */
12255 static tree
12256 cp_parser_decltype_expr (cp_parser *parser,
12257 bool &id_expression_or_member_access_p)
12259 cp_token *id_expr_start_token;
12260 tree expr;
12262 /* First, try parsing an id-expression. */
12263 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12264 cp_parser_parse_tentatively (parser);
12265 expr = cp_parser_id_expression (parser,
12266 /*template_keyword_p=*/false,
12267 /*check_dependency_p=*/true,
12268 /*template_p=*/NULL,
12269 /*declarator_p=*/false,
12270 /*optional_p=*/false);
12272 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12274 bool non_integral_constant_expression_p = false;
12275 tree id_expression = expr;
12276 cp_id_kind idk;
12277 const char *error_msg;
12279 if (identifier_p (expr))
12280 /* Lookup the name we got back from the id-expression. */
12281 expr = cp_parser_lookup_name_simple (parser, expr,
12282 id_expr_start_token->location);
12284 if (expr
12285 && expr != error_mark_node
12286 && TREE_CODE (expr) != TYPE_DECL
12287 && (TREE_CODE (expr) != BIT_NOT_EXPR
12288 || !TYPE_P (TREE_OPERAND (expr, 0)))
12289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12291 /* Complete lookup of the id-expression. */
12292 expr = (finish_id_expression
12293 (id_expression, expr, parser->scope, &idk,
12294 /*integral_constant_expression_p=*/false,
12295 /*allow_non_integral_constant_expression_p=*/true,
12296 &non_integral_constant_expression_p,
12297 /*template_p=*/false,
12298 /*done=*/true,
12299 /*address_p=*/false,
12300 /*template_arg_p=*/false,
12301 &error_msg,
12302 id_expr_start_token->location));
12304 if (expr == error_mark_node)
12305 /* We found an id-expression, but it was something that we
12306 should not have found. This is an error, not something
12307 we can recover from, so note that we found an
12308 id-expression and we'll recover as gracefully as
12309 possible. */
12310 id_expression_or_member_access_p = true;
12313 if (expr
12314 && expr != error_mark_node
12315 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12316 /* We have an id-expression. */
12317 id_expression_or_member_access_p = true;
12320 if (!id_expression_or_member_access_p)
12322 /* Abort the id-expression parse. */
12323 cp_parser_abort_tentative_parse (parser);
12325 /* Parsing tentatively, again. */
12326 cp_parser_parse_tentatively (parser);
12328 /* Parse a class member access. */
12329 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12330 /*cast_p=*/false, /*decltype*/true,
12331 /*member_access_only_p=*/true, NULL);
12333 if (expr
12334 && expr != error_mark_node
12335 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12336 /* We have an id-expression. */
12337 id_expression_or_member_access_p = true;
12340 if (id_expression_or_member_access_p)
12341 /* We have parsed the complete id-expression or member access. */
12342 cp_parser_parse_definitely (parser);
12343 else
12345 /* Abort our attempt to parse an id-expression or member access
12346 expression. */
12347 cp_parser_abort_tentative_parse (parser);
12349 /* Parse a full expression. */
12350 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12351 /*decltype_p=*/true);
12354 return expr;
12357 /* Parse a `decltype' type. Returns the type.
12359 simple-type-specifier:
12360 decltype ( expression )
12361 C++14 proposal:
12362 decltype ( auto ) */
12364 static tree
12365 cp_parser_decltype (cp_parser *parser)
12367 tree expr;
12368 bool id_expression_or_member_access_p = false;
12369 const char *saved_message;
12370 bool saved_integral_constant_expression_p;
12371 bool saved_non_integral_constant_expression_p;
12372 bool saved_greater_than_is_operator_p;
12373 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12375 if (start_token->type == CPP_DECLTYPE)
12377 /* Already parsed. */
12378 cp_lexer_consume_token (parser->lexer);
12379 return start_token->u.value;
12382 /* Look for the `decltype' token. */
12383 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12384 return error_mark_node;
12386 /* Parse the opening `('. */
12387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12388 return error_mark_node;
12390 /* decltype (auto) */
12391 if (cxx_dialect >= cxx14
12392 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12394 cp_lexer_consume_token (parser->lexer);
12395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12396 return error_mark_node;
12397 expr = make_decltype_auto ();
12398 AUTO_IS_DECLTYPE (expr) = true;
12399 goto rewrite;
12402 /* Types cannot be defined in a `decltype' expression. Save away the
12403 old message. */
12404 saved_message = parser->type_definition_forbidden_message;
12406 /* And create the new one. */
12407 parser->type_definition_forbidden_message
12408 = G_("types may not be defined in %<decltype%> expressions");
12410 /* The restrictions on constant-expressions do not apply inside
12411 decltype expressions. */
12412 saved_integral_constant_expression_p
12413 = parser->integral_constant_expression_p;
12414 saved_non_integral_constant_expression_p
12415 = parser->non_integral_constant_expression_p;
12416 parser->integral_constant_expression_p = false;
12418 /* Within a parenthesized expression, a `>' token is always
12419 the greater-than operator. */
12420 saved_greater_than_is_operator_p
12421 = parser->greater_than_is_operator_p;
12422 parser->greater_than_is_operator_p = true;
12424 /* Do not actually evaluate the expression. */
12425 ++cp_unevaluated_operand;
12427 /* Do not warn about problems with the expression. */
12428 ++c_inhibit_evaluation_warnings;
12430 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12432 /* Go back to evaluating expressions. */
12433 --cp_unevaluated_operand;
12434 --c_inhibit_evaluation_warnings;
12436 /* The `>' token might be the end of a template-id or
12437 template-parameter-list now. */
12438 parser->greater_than_is_operator_p
12439 = saved_greater_than_is_operator_p;
12441 /* Restore the old message and the integral constant expression
12442 flags. */
12443 parser->type_definition_forbidden_message = saved_message;
12444 parser->integral_constant_expression_p
12445 = saved_integral_constant_expression_p;
12446 parser->non_integral_constant_expression_p
12447 = saved_non_integral_constant_expression_p;
12449 /* Parse to the closing `)'. */
12450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12452 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12453 /*consume_paren=*/true);
12454 return error_mark_node;
12457 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12458 tf_warning_or_error);
12460 rewrite:
12461 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12462 it again. */
12463 start_token->type = CPP_DECLTYPE;
12464 start_token->u.value = expr;
12465 start_token->keyword = RID_MAX;
12466 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12468 return expr;
12471 /* Special member functions [gram.special] */
12473 /* Parse a conversion-function-id.
12475 conversion-function-id:
12476 operator conversion-type-id
12478 Returns an IDENTIFIER_NODE representing the operator. */
12480 static tree
12481 cp_parser_conversion_function_id (cp_parser* parser)
12483 tree type;
12484 tree saved_scope;
12485 tree saved_qualifying_scope;
12486 tree saved_object_scope;
12487 tree pushed_scope = NULL_TREE;
12489 /* Look for the `operator' token. */
12490 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12491 return error_mark_node;
12492 /* When we parse the conversion-type-id, the current scope will be
12493 reset. However, we need that information in able to look up the
12494 conversion function later, so we save it here. */
12495 saved_scope = parser->scope;
12496 saved_qualifying_scope = parser->qualifying_scope;
12497 saved_object_scope = parser->object_scope;
12498 /* We must enter the scope of the class so that the names of
12499 entities declared within the class are available in the
12500 conversion-type-id. For example, consider:
12502 struct S {
12503 typedef int I;
12504 operator I();
12507 S::operator I() { ... }
12509 In order to see that `I' is a type-name in the definition, we
12510 must be in the scope of `S'. */
12511 if (saved_scope)
12512 pushed_scope = push_scope (saved_scope);
12513 /* Parse the conversion-type-id. */
12514 type = cp_parser_conversion_type_id (parser);
12515 /* Leave the scope of the class, if any. */
12516 if (pushed_scope)
12517 pop_scope (pushed_scope);
12518 /* Restore the saved scope. */
12519 parser->scope = saved_scope;
12520 parser->qualifying_scope = saved_qualifying_scope;
12521 parser->object_scope = saved_object_scope;
12522 /* If the TYPE is invalid, indicate failure. */
12523 if (type == error_mark_node)
12524 return error_mark_node;
12525 return mangle_conv_op_name_for_type (type);
12528 /* Parse a conversion-type-id:
12530 conversion-type-id:
12531 type-specifier-seq conversion-declarator [opt]
12533 Returns the TYPE specified. */
12535 static tree
12536 cp_parser_conversion_type_id (cp_parser* parser)
12538 tree attributes;
12539 cp_decl_specifier_seq type_specifiers;
12540 cp_declarator *declarator;
12541 tree type_specified;
12542 const char *saved_message;
12544 /* Parse the attributes. */
12545 attributes = cp_parser_attributes_opt (parser);
12547 saved_message = parser->type_definition_forbidden_message;
12548 parser->type_definition_forbidden_message
12549 = G_("types may not be defined in a conversion-type-id");
12551 /* Parse the type-specifiers. */
12552 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12553 /*is_trailing_return=*/false,
12554 &type_specifiers);
12556 parser->type_definition_forbidden_message = saved_message;
12558 /* If that didn't work, stop. */
12559 if (type_specifiers.type == error_mark_node)
12560 return error_mark_node;
12561 /* Parse the conversion-declarator. */
12562 declarator = cp_parser_conversion_declarator_opt (parser);
12564 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12565 /*initialized=*/0, &attributes);
12566 if (attributes)
12567 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12569 /* Don't give this error when parsing tentatively. This happens to
12570 work because we always parse this definitively once. */
12571 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12572 && type_uses_auto (type_specified))
12574 if (cxx_dialect < cxx14)
12576 error ("invalid use of %<auto%> in conversion operator");
12577 return error_mark_node;
12579 else if (template_parm_scope_p ())
12580 warning (0, "use of %<auto%> in member template "
12581 "conversion operator can never be deduced");
12584 return type_specified;
12587 /* Parse an (optional) conversion-declarator.
12589 conversion-declarator:
12590 ptr-operator conversion-declarator [opt]
12594 static cp_declarator *
12595 cp_parser_conversion_declarator_opt (cp_parser* parser)
12597 enum tree_code code;
12598 tree class_type, std_attributes = NULL_TREE;
12599 cp_cv_quals cv_quals;
12601 /* We don't know if there's a ptr-operator next, or not. */
12602 cp_parser_parse_tentatively (parser);
12603 /* Try the ptr-operator. */
12604 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12605 &std_attributes);
12606 /* If it worked, look for more conversion-declarators. */
12607 if (cp_parser_parse_definitely (parser))
12609 cp_declarator *declarator;
12611 /* Parse another optional declarator. */
12612 declarator = cp_parser_conversion_declarator_opt (parser);
12614 declarator = cp_parser_make_indirect_declarator
12615 (code, class_type, cv_quals, declarator, std_attributes);
12617 return declarator;
12620 return NULL;
12623 /* Parse an (optional) ctor-initializer.
12625 ctor-initializer:
12626 : mem-initializer-list
12628 Returns TRUE iff the ctor-initializer was actually present. */
12630 static bool
12631 cp_parser_ctor_initializer_opt (cp_parser* parser)
12633 /* If the next token is not a `:', then there is no
12634 ctor-initializer. */
12635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12637 /* Do default initialization of any bases and members. */
12638 if (DECL_CONSTRUCTOR_P (current_function_decl))
12639 finish_mem_initializers (NULL_TREE);
12641 return false;
12644 /* Consume the `:' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* And the mem-initializer-list. */
12647 cp_parser_mem_initializer_list (parser);
12649 return true;
12652 /* Parse a mem-initializer-list.
12654 mem-initializer-list:
12655 mem-initializer ... [opt]
12656 mem-initializer ... [opt] , mem-initializer-list */
12658 static void
12659 cp_parser_mem_initializer_list (cp_parser* parser)
12661 tree mem_initializer_list = NULL_TREE;
12662 tree target_ctor = error_mark_node;
12663 cp_token *token = cp_lexer_peek_token (parser->lexer);
12665 /* Let the semantic analysis code know that we are starting the
12666 mem-initializer-list. */
12667 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12668 error_at (token->location,
12669 "only constructors take member initializers");
12671 /* Loop through the list. */
12672 while (true)
12674 tree mem_initializer;
12676 token = cp_lexer_peek_token (parser->lexer);
12677 /* Parse the mem-initializer. */
12678 mem_initializer = cp_parser_mem_initializer (parser);
12679 /* If the next token is a `...', we're expanding member initializers. */
12680 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12682 /* Consume the `...'. */
12683 cp_lexer_consume_token (parser->lexer);
12685 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12686 can be expanded but members cannot. */
12687 if (mem_initializer != error_mark_node
12688 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12690 error_at (token->location,
12691 "cannot expand initializer for member %<%D%>",
12692 TREE_PURPOSE (mem_initializer));
12693 mem_initializer = error_mark_node;
12696 /* Construct the pack expansion type. */
12697 if (mem_initializer != error_mark_node)
12698 mem_initializer = make_pack_expansion (mem_initializer);
12700 if (target_ctor != error_mark_node
12701 && mem_initializer != error_mark_node)
12703 error ("mem-initializer for %qD follows constructor delegation",
12704 TREE_PURPOSE (mem_initializer));
12705 mem_initializer = error_mark_node;
12707 /* Look for a target constructor. */
12708 if (mem_initializer != error_mark_node
12709 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12710 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12712 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12713 if (mem_initializer_list)
12715 error ("constructor delegation follows mem-initializer for %qD",
12716 TREE_PURPOSE (mem_initializer_list));
12717 mem_initializer = error_mark_node;
12719 target_ctor = mem_initializer;
12721 /* Add it to the list, unless it was erroneous. */
12722 if (mem_initializer != error_mark_node)
12724 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12725 mem_initializer_list = mem_initializer;
12727 /* If the next token is not a `,', we're done. */
12728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12729 break;
12730 /* Consume the `,' token. */
12731 cp_lexer_consume_token (parser->lexer);
12734 /* Perform semantic analysis. */
12735 if (DECL_CONSTRUCTOR_P (current_function_decl))
12736 finish_mem_initializers (mem_initializer_list);
12739 /* Parse a mem-initializer.
12741 mem-initializer:
12742 mem-initializer-id ( expression-list [opt] )
12743 mem-initializer-id braced-init-list
12745 GNU extension:
12747 mem-initializer:
12748 ( expression-list [opt] )
12750 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12751 class) or FIELD_DECL (for a non-static data member) to initialize;
12752 the TREE_VALUE is the expression-list. An empty initialization
12753 list is represented by void_list_node. */
12755 static tree
12756 cp_parser_mem_initializer (cp_parser* parser)
12758 tree mem_initializer_id;
12759 tree expression_list;
12760 tree member;
12761 cp_token *token = cp_lexer_peek_token (parser->lexer);
12763 /* Find out what is being initialized. */
12764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12766 permerror (token->location,
12767 "anachronistic old-style base class initializer");
12768 mem_initializer_id = NULL_TREE;
12770 else
12772 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12773 if (mem_initializer_id == error_mark_node)
12774 return mem_initializer_id;
12776 member = expand_member_init (mem_initializer_id);
12777 if (member && !DECL_P (member))
12778 in_base_initializer = 1;
12780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12782 bool expr_non_constant_p;
12783 cp_lexer_set_source_position (parser->lexer);
12784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12785 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12786 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12787 expression_list = build_tree_list (NULL_TREE, expression_list);
12789 else
12791 vec<tree, va_gc> *vec;
12792 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12793 /*cast_p=*/false,
12794 /*allow_expansion_p=*/true,
12795 /*non_constant_p=*/NULL);
12796 if (vec == NULL)
12797 return error_mark_node;
12798 expression_list = build_tree_list_vec (vec);
12799 release_tree_vector (vec);
12802 if (expression_list == error_mark_node)
12803 return error_mark_node;
12804 if (!expression_list)
12805 expression_list = void_type_node;
12807 in_base_initializer = 0;
12809 return member ? build_tree_list (member, expression_list) : error_mark_node;
12812 /* Parse a mem-initializer-id.
12814 mem-initializer-id:
12815 :: [opt] nested-name-specifier [opt] class-name
12816 decltype-specifier (C++11)
12817 identifier
12819 Returns a TYPE indicating the class to be initialized for the first
12820 production (and the second in C++11). Returns an IDENTIFIER_NODE
12821 indicating the data member to be initialized for the last production. */
12823 static tree
12824 cp_parser_mem_initializer_id (cp_parser* parser)
12826 bool global_scope_p;
12827 bool nested_name_specifier_p;
12828 bool template_p = false;
12829 tree id;
12831 cp_token *token = cp_lexer_peek_token (parser->lexer);
12833 /* `typename' is not allowed in this context ([temp.res]). */
12834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12836 error_at (token->location,
12837 "keyword %<typename%> not allowed in this context (a qualified "
12838 "member initializer is implicitly a type)");
12839 cp_lexer_consume_token (parser->lexer);
12841 /* Look for the optional `::' operator. */
12842 global_scope_p
12843 = (cp_parser_global_scope_opt (parser,
12844 /*current_scope_valid_p=*/false)
12845 != NULL_TREE);
12846 /* Look for the optional nested-name-specifier. The simplest way to
12847 implement:
12849 [temp.res]
12851 The keyword `typename' is not permitted in a base-specifier or
12852 mem-initializer; in these contexts a qualified name that
12853 depends on a template-parameter is implicitly assumed to be a
12854 type name.
12856 is to assume that we have seen the `typename' keyword at this
12857 point. */
12858 nested_name_specifier_p
12859 = (cp_parser_nested_name_specifier_opt (parser,
12860 /*typename_keyword_p=*/true,
12861 /*check_dependency_p=*/true,
12862 /*type_p=*/true,
12863 /*is_declaration=*/true)
12864 != NULL_TREE);
12865 if (nested_name_specifier_p)
12866 template_p = cp_parser_optional_template_keyword (parser);
12867 /* If there is a `::' operator or a nested-name-specifier, then we
12868 are definitely looking for a class-name. */
12869 if (global_scope_p || nested_name_specifier_p)
12870 return cp_parser_class_name (parser,
12871 /*typename_keyword_p=*/true,
12872 /*template_keyword_p=*/template_p,
12873 typename_type,
12874 /*check_dependency_p=*/true,
12875 /*class_head_p=*/false,
12876 /*is_declaration=*/true);
12877 /* Otherwise, we could also be looking for an ordinary identifier. */
12878 cp_parser_parse_tentatively (parser);
12879 if (cp_lexer_next_token_is_decltype (parser->lexer))
12880 /* Try a decltype-specifier. */
12881 id = cp_parser_decltype (parser);
12882 else
12883 /* Otherwise, try a class-name. */
12884 id = cp_parser_class_name (parser,
12885 /*typename_keyword_p=*/true,
12886 /*template_keyword_p=*/false,
12887 none_type,
12888 /*check_dependency_p=*/true,
12889 /*class_head_p=*/false,
12890 /*is_declaration=*/true);
12891 /* If we found one, we're done. */
12892 if (cp_parser_parse_definitely (parser))
12893 return id;
12894 /* Otherwise, look for an ordinary identifier. */
12895 return cp_parser_identifier (parser);
12898 /* Overloading [gram.over] */
12900 /* Parse an operator-function-id.
12902 operator-function-id:
12903 operator operator
12905 Returns an IDENTIFIER_NODE for the operator which is a
12906 human-readable spelling of the identifier, e.g., `operator +'. */
12908 static tree
12909 cp_parser_operator_function_id (cp_parser* parser)
12911 /* Look for the `operator' keyword. */
12912 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12913 return error_mark_node;
12914 /* And then the name of the operator itself. */
12915 return cp_parser_operator (parser);
12918 /* Return an identifier node for a user-defined literal operator.
12919 The suffix identifier is chained to the operator name identifier. */
12921 static tree
12922 cp_literal_operator_id (const char* name)
12924 tree identifier;
12925 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12926 + strlen (name) + 10);
12927 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12928 identifier = get_identifier (buffer);
12930 return identifier;
12933 /* Parse an operator.
12935 operator:
12936 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12937 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12938 || ++ -- , ->* -> () []
12940 GNU Extensions:
12942 operator:
12943 <? >? <?= >?=
12945 Returns an IDENTIFIER_NODE for the operator which is a
12946 human-readable spelling of the identifier, e.g., `operator +'. */
12948 static tree
12949 cp_parser_operator (cp_parser* parser)
12951 tree id = NULL_TREE;
12952 cp_token *token;
12953 bool utf8 = false;
12955 /* Peek at the next token. */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 /* Figure out which operator we have. */
12958 switch (token->type)
12960 case CPP_KEYWORD:
12962 enum tree_code op;
12964 /* The keyword should be either `new' or `delete'. */
12965 if (token->keyword == RID_NEW)
12966 op = NEW_EXPR;
12967 else if (token->keyword == RID_DELETE)
12968 op = DELETE_EXPR;
12969 else
12970 break;
12972 /* Consume the `new' or `delete' token. */
12973 cp_lexer_consume_token (parser->lexer);
12975 /* Peek at the next token. */
12976 token = cp_lexer_peek_token (parser->lexer);
12977 /* If it's a `[' token then this is the array variant of the
12978 operator. */
12979 if (token->type == CPP_OPEN_SQUARE)
12981 /* Consume the `[' token. */
12982 cp_lexer_consume_token (parser->lexer);
12983 /* Look for the `]' token. */
12984 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12985 id = ansi_opname (op == NEW_EXPR
12986 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12988 /* Otherwise, we have the non-array variant. */
12989 else
12990 id = ansi_opname (op);
12992 return id;
12995 case CPP_PLUS:
12996 id = ansi_opname (PLUS_EXPR);
12997 break;
12999 case CPP_MINUS:
13000 id = ansi_opname (MINUS_EXPR);
13001 break;
13003 case CPP_MULT:
13004 id = ansi_opname (MULT_EXPR);
13005 break;
13007 case CPP_DIV:
13008 id = ansi_opname (TRUNC_DIV_EXPR);
13009 break;
13011 case CPP_MOD:
13012 id = ansi_opname (TRUNC_MOD_EXPR);
13013 break;
13015 case CPP_XOR:
13016 id = ansi_opname (BIT_XOR_EXPR);
13017 break;
13019 case CPP_AND:
13020 id = ansi_opname (BIT_AND_EXPR);
13021 break;
13023 case CPP_OR:
13024 id = ansi_opname (BIT_IOR_EXPR);
13025 break;
13027 case CPP_COMPL:
13028 id = ansi_opname (BIT_NOT_EXPR);
13029 break;
13031 case CPP_NOT:
13032 id = ansi_opname (TRUTH_NOT_EXPR);
13033 break;
13035 case CPP_EQ:
13036 id = ansi_assopname (NOP_EXPR);
13037 break;
13039 case CPP_LESS:
13040 id = ansi_opname (LT_EXPR);
13041 break;
13043 case CPP_GREATER:
13044 id = ansi_opname (GT_EXPR);
13045 break;
13047 case CPP_PLUS_EQ:
13048 id = ansi_assopname (PLUS_EXPR);
13049 break;
13051 case CPP_MINUS_EQ:
13052 id = ansi_assopname (MINUS_EXPR);
13053 break;
13055 case CPP_MULT_EQ:
13056 id = ansi_assopname (MULT_EXPR);
13057 break;
13059 case CPP_DIV_EQ:
13060 id = ansi_assopname (TRUNC_DIV_EXPR);
13061 break;
13063 case CPP_MOD_EQ:
13064 id = ansi_assopname (TRUNC_MOD_EXPR);
13065 break;
13067 case CPP_XOR_EQ:
13068 id = ansi_assopname (BIT_XOR_EXPR);
13069 break;
13071 case CPP_AND_EQ:
13072 id = ansi_assopname (BIT_AND_EXPR);
13073 break;
13075 case CPP_OR_EQ:
13076 id = ansi_assopname (BIT_IOR_EXPR);
13077 break;
13079 case CPP_LSHIFT:
13080 id = ansi_opname (LSHIFT_EXPR);
13081 break;
13083 case CPP_RSHIFT:
13084 id = ansi_opname (RSHIFT_EXPR);
13085 break;
13087 case CPP_LSHIFT_EQ:
13088 id = ansi_assopname (LSHIFT_EXPR);
13089 break;
13091 case CPP_RSHIFT_EQ:
13092 id = ansi_assopname (RSHIFT_EXPR);
13093 break;
13095 case CPP_EQ_EQ:
13096 id = ansi_opname (EQ_EXPR);
13097 break;
13099 case CPP_NOT_EQ:
13100 id = ansi_opname (NE_EXPR);
13101 break;
13103 case CPP_LESS_EQ:
13104 id = ansi_opname (LE_EXPR);
13105 break;
13107 case CPP_GREATER_EQ:
13108 id = ansi_opname (GE_EXPR);
13109 break;
13111 case CPP_AND_AND:
13112 id = ansi_opname (TRUTH_ANDIF_EXPR);
13113 break;
13115 case CPP_OR_OR:
13116 id = ansi_opname (TRUTH_ORIF_EXPR);
13117 break;
13119 case CPP_PLUS_PLUS:
13120 id = ansi_opname (POSTINCREMENT_EXPR);
13121 break;
13123 case CPP_MINUS_MINUS:
13124 id = ansi_opname (PREDECREMENT_EXPR);
13125 break;
13127 case CPP_COMMA:
13128 id = ansi_opname (COMPOUND_EXPR);
13129 break;
13131 case CPP_DEREF_STAR:
13132 id = ansi_opname (MEMBER_REF);
13133 break;
13135 case CPP_DEREF:
13136 id = ansi_opname (COMPONENT_REF);
13137 break;
13139 case CPP_OPEN_PAREN:
13140 /* Consume the `('. */
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Look for the matching `)'. */
13143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13144 return ansi_opname (CALL_EXPR);
13146 case CPP_OPEN_SQUARE:
13147 /* Consume the `['. */
13148 cp_lexer_consume_token (parser->lexer);
13149 /* Look for the matching `]'. */
13150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13151 return ansi_opname (ARRAY_REF);
13153 case CPP_UTF8STRING:
13154 case CPP_UTF8STRING_USERDEF:
13155 utf8 = true;
13156 case CPP_STRING:
13157 case CPP_WSTRING:
13158 case CPP_STRING16:
13159 case CPP_STRING32:
13160 case CPP_STRING_USERDEF:
13161 case CPP_WSTRING_USERDEF:
13162 case CPP_STRING16_USERDEF:
13163 case CPP_STRING32_USERDEF:
13165 tree str, string_tree;
13166 int sz, len;
13168 if (cxx_dialect == cxx98)
13169 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13171 /* Consume the string. */
13172 str = cp_parser_string_literal (parser, /*translate=*/true,
13173 /*wide_ok=*/true, /*lookup_udlit=*/false);
13174 if (str == error_mark_node)
13175 return error_mark_node;
13176 else if (TREE_CODE (str) == USERDEF_LITERAL)
13178 string_tree = USERDEF_LITERAL_VALUE (str);
13179 id = USERDEF_LITERAL_SUFFIX_ID (str);
13181 else
13183 string_tree = str;
13184 /* Look for the suffix identifier. */
13185 token = cp_lexer_peek_token (parser->lexer);
13186 if (token->type == CPP_NAME)
13187 id = cp_parser_identifier (parser);
13188 else if (token->type == CPP_KEYWORD)
13190 error ("unexpected keyword;"
13191 " remove space between quotes and suffix identifier");
13192 return error_mark_node;
13194 else
13196 error ("expected suffix identifier");
13197 return error_mark_node;
13200 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13201 (TREE_TYPE (TREE_TYPE (string_tree))));
13202 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13203 if (len != 0)
13205 error ("expected empty string after %<operator%> keyword");
13206 return error_mark_node;
13208 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13209 != char_type_node)
13211 error ("invalid encoding prefix in literal operator");
13212 return error_mark_node;
13214 if (id != error_mark_node)
13216 const char *name = IDENTIFIER_POINTER (id);
13217 id = cp_literal_operator_id (name);
13219 return id;
13222 default:
13223 /* Anything else is an error. */
13224 break;
13227 /* If we have selected an identifier, we need to consume the
13228 operator token. */
13229 if (id)
13230 cp_lexer_consume_token (parser->lexer);
13231 /* Otherwise, no valid operator name was present. */
13232 else
13234 cp_parser_error (parser, "expected operator");
13235 id = error_mark_node;
13238 return id;
13241 /* Parse a template-declaration.
13243 template-declaration:
13244 export [opt] template < template-parameter-list > declaration
13246 If MEMBER_P is TRUE, this template-declaration occurs within a
13247 class-specifier.
13249 The grammar rule given by the standard isn't correct. What
13250 is really meant is:
13252 template-declaration:
13253 export [opt] template-parameter-list-seq
13254 decl-specifier-seq [opt] init-declarator [opt] ;
13255 export [opt] template-parameter-list-seq
13256 function-definition
13258 template-parameter-list-seq:
13259 template-parameter-list-seq [opt]
13260 template < template-parameter-list > */
13262 static void
13263 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13265 /* Check for `export'. */
13266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13268 /* Consume the `export' token. */
13269 cp_lexer_consume_token (parser->lexer);
13270 /* Warn that we do not support `export'. */
13271 warning (0, "keyword %<export%> not implemented, and will be ignored");
13274 cp_parser_template_declaration_after_export (parser, member_p);
13277 /* Parse a template-parameter-list.
13279 template-parameter-list:
13280 template-parameter
13281 template-parameter-list , template-parameter
13283 Returns a TREE_LIST. Each node represents a template parameter.
13284 The nodes are connected via their TREE_CHAINs. */
13286 static tree
13287 cp_parser_template_parameter_list (cp_parser* parser)
13289 tree parameter_list = NULL_TREE;
13291 begin_template_parm_list ();
13293 /* The loop below parses the template parms. We first need to know
13294 the total number of template parms to be able to compute proper
13295 canonical types of each dependent type. So after the loop, when
13296 we know the total number of template parms,
13297 end_template_parm_list computes the proper canonical types and
13298 fixes up the dependent types accordingly. */
13299 while (true)
13301 tree parameter;
13302 bool is_non_type;
13303 bool is_parameter_pack;
13304 location_t parm_loc;
13306 /* Parse the template-parameter. */
13307 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13308 parameter = cp_parser_template_parameter (parser,
13309 &is_non_type,
13310 &is_parameter_pack);
13311 /* Add it to the list. */
13312 if (parameter != error_mark_node)
13313 parameter_list = process_template_parm (parameter_list,
13314 parm_loc,
13315 parameter,
13316 is_non_type,
13317 is_parameter_pack);
13318 else
13320 tree err_parm = build_tree_list (parameter, parameter);
13321 parameter_list = chainon (parameter_list, err_parm);
13324 /* If the next token is not a `,', we're done. */
13325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13326 break;
13327 /* Otherwise, consume the `,' token. */
13328 cp_lexer_consume_token (parser->lexer);
13331 return end_template_parm_list (parameter_list);
13334 /* Parse a template-parameter.
13336 template-parameter:
13337 type-parameter
13338 parameter-declaration
13340 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13341 the parameter. The TREE_PURPOSE is the default value, if any.
13342 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13343 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13344 set to true iff this parameter is a parameter pack. */
13346 static tree
13347 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13348 bool *is_parameter_pack)
13350 cp_token *token;
13351 cp_parameter_declarator *parameter_declarator;
13352 cp_declarator *id_declarator;
13353 tree parm;
13355 /* Assume it is a type parameter or a template parameter. */
13356 *is_non_type = false;
13357 /* Assume it not a parameter pack. */
13358 *is_parameter_pack = false;
13359 /* Peek at the next token. */
13360 token = cp_lexer_peek_token (parser->lexer);
13361 /* If it is `class' or `template', we have a type-parameter. */
13362 if (token->keyword == RID_TEMPLATE)
13363 return cp_parser_type_parameter (parser, is_parameter_pack);
13364 /* If it is `class' or `typename' we do not know yet whether it is a
13365 type parameter or a non-type parameter. Consider:
13367 template <typename T, typename T::X X> ...
13371 template <class C, class D*> ...
13373 Here, the first parameter is a type parameter, and the second is
13374 a non-type parameter. We can tell by looking at the token after
13375 the identifier -- if it is a `,', `=', or `>' then we have a type
13376 parameter. */
13377 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13379 /* Peek at the token after `class' or `typename'. */
13380 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13381 /* If it's an ellipsis, we have a template type parameter
13382 pack. */
13383 if (token->type == CPP_ELLIPSIS)
13384 return cp_parser_type_parameter (parser, is_parameter_pack);
13385 /* If it's an identifier, skip it. */
13386 if (token->type == CPP_NAME)
13387 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13388 /* Now, see if the token looks like the end of a template
13389 parameter. */
13390 if (token->type == CPP_COMMA
13391 || token->type == CPP_EQ
13392 || token->type == CPP_GREATER)
13393 return cp_parser_type_parameter (parser, is_parameter_pack);
13396 /* Otherwise, it is a non-type parameter.
13398 [temp.param]
13400 When parsing a default template-argument for a non-type
13401 template-parameter, the first non-nested `>' is taken as the end
13402 of the template parameter-list rather than a greater-than
13403 operator. */
13404 *is_non_type = true;
13405 parameter_declarator
13406 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13407 /*parenthesized_p=*/NULL);
13409 if (!parameter_declarator)
13410 return error_mark_node;
13412 /* If the parameter declaration is marked as a parameter pack, set
13413 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13414 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13415 grokdeclarator. */
13416 if (parameter_declarator->declarator
13417 && parameter_declarator->declarator->parameter_pack_p)
13419 *is_parameter_pack = true;
13420 parameter_declarator->declarator->parameter_pack_p = false;
13423 if (parameter_declarator->default_argument)
13425 /* Can happen in some cases of erroneous input (c++/34892). */
13426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13427 /* Consume the `...' for better error recovery. */
13428 cp_lexer_consume_token (parser->lexer);
13430 /* If the next token is an ellipsis, and we don't already have it
13431 marked as a parameter pack, then we have a parameter pack (that
13432 has no declarator). */
13433 else if (!*is_parameter_pack
13434 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13435 && (declarator_can_be_parameter_pack
13436 (parameter_declarator->declarator)))
13438 /* Consume the `...'. */
13439 cp_lexer_consume_token (parser->lexer);
13440 maybe_warn_variadic_templates ();
13442 *is_parameter_pack = true;
13444 /* We might end up with a pack expansion as the type of the non-type
13445 template parameter, in which case this is a non-type template
13446 parameter pack. */
13447 else if (parameter_declarator->decl_specifiers.type
13448 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13450 *is_parameter_pack = true;
13451 parameter_declarator->decl_specifiers.type =
13452 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13455 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13457 /* Parameter packs cannot have default arguments. However, a
13458 user may try to do so, so we'll parse them and give an
13459 appropriate diagnostic here. */
13461 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13463 /* Find the name of the parameter pack. */
13464 id_declarator = parameter_declarator->declarator;
13465 while (id_declarator && id_declarator->kind != cdk_id)
13466 id_declarator = id_declarator->declarator;
13468 if (id_declarator && id_declarator->kind == cdk_id)
13469 error_at (start_token->location,
13470 "template parameter pack %qD cannot have a default argument",
13471 id_declarator->u.id.unqualified_name);
13472 else
13473 error_at (start_token->location,
13474 "template parameter pack cannot have a default argument");
13476 /* Parse the default argument, but throw away the result. */
13477 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13480 parm = grokdeclarator (parameter_declarator->declarator,
13481 &parameter_declarator->decl_specifiers,
13482 TPARM, /*initialized=*/0,
13483 /*attrlist=*/NULL);
13484 if (parm == error_mark_node)
13485 return error_mark_node;
13487 return build_tree_list (parameter_declarator->default_argument, parm);
13490 /* Parse a type-parameter.
13492 type-parameter:
13493 class identifier [opt]
13494 class identifier [opt] = type-id
13495 typename identifier [opt]
13496 typename identifier [opt] = type-id
13497 template < template-parameter-list > class identifier [opt]
13498 template < template-parameter-list > class identifier [opt]
13499 = id-expression
13501 GNU Extension (variadic templates):
13503 type-parameter:
13504 class ... identifier [opt]
13505 typename ... identifier [opt]
13507 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13508 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13509 the declaration of the parameter.
13511 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13513 static tree
13514 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13516 cp_token *token;
13517 tree parameter;
13519 /* Look for a keyword to tell us what kind of parameter this is. */
13520 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13521 if (!token)
13522 return error_mark_node;
13524 switch (token->keyword)
13526 case RID_CLASS:
13527 case RID_TYPENAME:
13529 tree identifier;
13530 tree default_argument;
13532 /* If the next token is an ellipsis, we have a template
13533 argument pack. */
13534 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13536 /* Consume the `...' token. */
13537 cp_lexer_consume_token (parser->lexer);
13538 maybe_warn_variadic_templates ();
13540 *is_parameter_pack = true;
13543 /* If the next token is an identifier, then it names the
13544 parameter. */
13545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13546 identifier = cp_parser_identifier (parser);
13547 else
13548 identifier = NULL_TREE;
13550 /* Create the parameter. */
13551 parameter = finish_template_type_parm (class_type_node, identifier);
13553 /* If the next token is an `=', we have a default argument. */
13554 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13556 /* Consume the `=' token. */
13557 cp_lexer_consume_token (parser->lexer);
13558 /* Parse the default-argument. */
13559 push_deferring_access_checks (dk_no_deferred);
13560 default_argument = cp_parser_type_id (parser);
13562 /* Template parameter packs cannot have default
13563 arguments. */
13564 if (*is_parameter_pack)
13566 if (identifier)
13567 error_at (token->location,
13568 "template parameter pack %qD cannot have a "
13569 "default argument", identifier);
13570 else
13571 error_at (token->location,
13572 "template parameter packs cannot have "
13573 "default arguments");
13574 default_argument = NULL_TREE;
13576 else if (check_for_bare_parameter_packs (default_argument))
13577 default_argument = error_mark_node;
13578 pop_deferring_access_checks ();
13580 else
13581 default_argument = NULL_TREE;
13583 /* Create the combined representation of the parameter and the
13584 default argument. */
13585 parameter = build_tree_list (default_argument, parameter);
13587 break;
13589 case RID_TEMPLATE:
13591 tree identifier;
13592 tree default_argument;
13594 /* Look for the `<'. */
13595 cp_parser_require (parser, CPP_LESS, RT_LESS);
13596 /* Parse the template-parameter-list. */
13597 cp_parser_template_parameter_list (parser);
13598 /* Look for the `>'. */
13599 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13600 /* Look for the `class' or 'typename' keywords. */
13601 cp_parser_type_parameter_key (parser);
13602 /* If the next token is an ellipsis, we have a template
13603 argument pack. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13606 /* Consume the `...' token. */
13607 cp_lexer_consume_token (parser->lexer);
13608 maybe_warn_variadic_templates ();
13610 *is_parameter_pack = true;
13612 /* If the next token is an `=', then there is a
13613 default-argument. If the next token is a `>', we are at
13614 the end of the parameter-list. If the next token is a `,',
13615 then we are at the end of this parameter. */
13616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13617 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13618 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13620 identifier = cp_parser_identifier (parser);
13621 /* Treat invalid names as if the parameter were nameless. */
13622 if (identifier == error_mark_node)
13623 identifier = NULL_TREE;
13625 else
13626 identifier = NULL_TREE;
13628 /* Create the template parameter. */
13629 parameter = finish_template_template_parm (class_type_node,
13630 identifier);
13632 /* If the next token is an `=', then there is a
13633 default-argument. */
13634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13636 bool is_template;
13638 /* Consume the `='. */
13639 cp_lexer_consume_token (parser->lexer);
13640 /* Parse the id-expression. */
13641 push_deferring_access_checks (dk_no_deferred);
13642 /* save token before parsing the id-expression, for error
13643 reporting */
13644 token = cp_lexer_peek_token (parser->lexer);
13645 default_argument
13646 = cp_parser_id_expression (parser,
13647 /*template_keyword_p=*/false,
13648 /*check_dependency_p=*/true,
13649 /*template_p=*/&is_template,
13650 /*declarator_p=*/false,
13651 /*optional_p=*/false);
13652 if (TREE_CODE (default_argument) == TYPE_DECL)
13653 /* If the id-expression was a template-id that refers to
13654 a template-class, we already have the declaration here,
13655 so no further lookup is needed. */
13657 else
13658 /* Look up the name. */
13659 default_argument
13660 = cp_parser_lookup_name (parser, default_argument,
13661 none_type,
13662 /*is_template=*/is_template,
13663 /*is_namespace=*/false,
13664 /*check_dependency=*/true,
13665 /*ambiguous_decls=*/NULL,
13666 token->location);
13667 /* See if the default argument is valid. */
13668 default_argument
13669 = check_template_template_default_arg (default_argument);
13671 /* Template parameter packs cannot have default
13672 arguments. */
13673 if (*is_parameter_pack)
13675 if (identifier)
13676 error_at (token->location,
13677 "template parameter pack %qD cannot "
13678 "have a default argument",
13679 identifier);
13680 else
13681 error_at (token->location, "template parameter packs cannot "
13682 "have default arguments");
13683 default_argument = NULL_TREE;
13685 pop_deferring_access_checks ();
13687 else
13688 default_argument = NULL_TREE;
13690 /* Create the combined representation of the parameter and the
13691 default argument. */
13692 parameter = build_tree_list (default_argument, parameter);
13694 break;
13696 default:
13697 gcc_unreachable ();
13698 break;
13701 return parameter;
13704 /* Parse a template-id.
13706 template-id:
13707 template-name < template-argument-list [opt] >
13709 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13710 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13711 returned. Otherwise, if the template-name names a function, or set
13712 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13713 names a class, returns a TYPE_DECL for the specialization.
13715 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13716 uninstantiated templates. */
13718 static tree
13719 cp_parser_template_id (cp_parser *parser,
13720 bool template_keyword_p,
13721 bool check_dependency_p,
13722 enum tag_types tag_type,
13723 bool is_declaration)
13725 int i;
13726 tree templ;
13727 tree arguments;
13728 tree template_id;
13729 cp_token_position start_of_id = 0;
13730 deferred_access_check *chk;
13731 vec<deferred_access_check, va_gc> *access_check;
13732 cp_token *next_token = NULL, *next_token_2 = NULL;
13733 bool is_identifier;
13735 /* If the next token corresponds to a template-id, there is no need
13736 to reparse it. */
13737 next_token = cp_lexer_peek_token (parser->lexer);
13738 if (next_token->type == CPP_TEMPLATE_ID)
13740 struct tree_check *check_value;
13742 /* Get the stored value. */
13743 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13744 /* Perform any access checks that were deferred. */
13745 access_check = check_value->checks;
13746 if (access_check)
13748 FOR_EACH_VEC_ELT (*access_check, i, chk)
13749 perform_or_defer_access_check (chk->binfo,
13750 chk->decl,
13751 chk->diag_decl,
13752 tf_warning_or_error);
13754 /* Return the stored value. */
13755 return check_value->value;
13758 /* Avoid performing name lookup if there is no possibility of
13759 finding a template-id. */
13760 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13761 || (next_token->type == CPP_NAME
13762 && !cp_parser_nth_token_starts_template_argument_list_p
13763 (parser, 2)))
13765 cp_parser_error (parser, "expected template-id");
13766 return error_mark_node;
13769 /* Remember where the template-id starts. */
13770 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13771 start_of_id = cp_lexer_token_position (parser->lexer, false);
13773 push_deferring_access_checks (dk_deferred);
13775 /* Parse the template-name. */
13776 is_identifier = false;
13777 templ = cp_parser_template_name (parser, template_keyword_p,
13778 check_dependency_p,
13779 is_declaration,
13780 tag_type,
13781 &is_identifier);
13782 if (templ == error_mark_node || is_identifier)
13784 pop_deferring_access_checks ();
13785 return templ;
13788 /* If we find the sequence `[:' after a template-name, it's probably
13789 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13790 parse correctly the argument list. */
13791 next_token = cp_lexer_peek_token (parser->lexer);
13792 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13793 if (next_token->type == CPP_OPEN_SQUARE
13794 && next_token->flags & DIGRAPH
13795 && next_token_2->type == CPP_COLON
13796 && !(next_token_2->flags & PREV_WHITE))
13798 cp_parser_parse_tentatively (parser);
13799 /* Change `:' into `::'. */
13800 next_token_2->type = CPP_SCOPE;
13801 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13802 CPP_LESS. */
13803 cp_lexer_consume_token (parser->lexer);
13805 /* Parse the arguments. */
13806 arguments = cp_parser_enclosed_template_argument_list (parser);
13807 if (!cp_parser_parse_definitely (parser))
13809 /* If we couldn't parse an argument list, then we revert our changes
13810 and return simply an error. Maybe this is not a template-id
13811 after all. */
13812 next_token_2->type = CPP_COLON;
13813 cp_parser_error (parser, "expected %<<%>");
13814 pop_deferring_access_checks ();
13815 return error_mark_node;
13817 /* Otherwise, emit an error about the invalid digraph, but continue
13818 parsing because we got our argument list. */
13819 if (permerror (next_token->location,
13820 "%<<::%> cannot begin a template-argument list"))
13822 static bool hint = false;
13823 inform (next_token->location,
13824 "%<<:%> is an alternate spelling for %<[%>."
13825 " Insert whitespace between %<<%> and %<::%>");
13826 if (!hint && !flag_permissive)
13828 inform (next_token->location, "(if you use %<-fpermissive%> "
13829 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13830 "accept your code)");
13831 hint = true;
13835 else
13837 /* Look for the `<' that starts the template-argument-list. */
13838 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13840 pop_deferring_access_checks ();
13841 return error_mark_node;
13843 /* Parse the arguments. */
13844 arguments = cp_parser_enclosed_template_argument_list (parser);
13847 /* Build a representation of the specialization. */
13848 if (identifier_p (templ))
13849 template_id = build_min_nt_loc (next_token->location,
13850 TEMPLATE_ID_EXPR,
13851 templ, arguments);
13852 else if (DECL_TYPE_TEMPLATE_P (templ)
13853 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13855 bool entering_scope;
13856 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13857 template (rather than some instantiation thereof) only if
13858 is not nested within some other construct. For example, in
13859 "template <typename T> void f(T) { A<T>::", A<T> is just an
13860 instantiation of A. */
13861 entering_scope = (template_parm_scope_p ()
13862 && cp_lexer_next_token_is (parser->lexer,
13863 CPP_SCOPE));
13864 template_id
13865 = finish_template_type (templ, arguments, entering_scope);
13867 else if (variable_template_p (templ))
13869 template_id = lookup_template_variable (templ, arguments);
13871 else
13873 /* If it's not a class-template or a template-template, it should be
13874 a function-template. */
13875 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13876 || TREE_CODE (templ) == OVERLOAD
13877 || BASELINK_P (templ)));
13879 template_id = lookup_template_function (templ, arguments);
13882 /* If parsing tentatively, replace the sequence of tokens that makes
13883 up the template-id with a CPP_TEMPLATE_ID token. That way,
13884 should we re-parse the token stream, we will not have to repeat
13885 the effort required to do the parse, nor will we issue duplicate
13886 error messages about problems during instantiation of the
13887 template. */
13888 if (start_of_id
13889 /* Don't do this if we had a parse error in a declarator; re-parsing
13890 might succeed if a name changes meaning (60361). */
13891 && !(cp_parser_error_occurred (parser)
13892 && cp_parser_parsing_tentatively (parser)
13893 && parser->in_declarator_p))
13895 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13897 /* Reset the contents of the START_OF_ID token. */
13898 token->type = CPP_TEMPLATE_ID;
13899 /* Retrieve any deferred checks. Do not pop this access checks yet
13900 so the memory will not be reclaimed during token replacing below. */
13901 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13902 token->u.tree_check_value->value = template_id;
13903 token->u.tree_check_value->checks = get_deferred_access_checks ();
13904 token->keyword = RID_MAX;
13906 /* Purge all subsequent tokens. */
13907 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13909 /* ??? Can we actually assume that, if template_id ==
13910 error_mark_node, we will have issued a diagnostic to the
13911 user, as opposed to simply marking the tentative parse as
13912 failed? */
13913 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13914 error_at (token->location, "parse error in template argument list");
13917 pop_to_parent_deferring_access_checks ();
13918 return template_id;
13921 /* Parse a template-name.
13923 template-name:
13924 identifier
13926 The standard should actually say:
13928 template-name:
13929 identifier
13930 operator-function-id
13932 A defect report has been filed about this issue.
13934 A conversion-function-id cannot be a template name because they cannot
13935 be part of a template-id. In fact, looking at this code:
13937 a.operator K<int>()
13939 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13940 It is impossible to call a templated conversion-function-id with an
13941 explicit argument list, since the only allowed template parameter is
13942 the type to which it is converting.
13944 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13945 `template' keyword, in a construction like:
13947 T::template f<3>()
13949 In that case `f' is taken to be a template-name, even though there
13950 is no way of knowing for sure.
13952 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13953 name refers to a set of overloaded functions, at least one of which
13954 is a template, or an IDENTIFIER_NODE with the name of the template,
13955 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13956 names are looked up inside uninstantiated templates. */
13958 static tree
13959 cp_parser_template_name (cp_parser* parser,
13960 bool template_keyword_p,
13961 bool check_dependency_p,
13962 bool is_declaration,
13963 enum tag_types tag_type,
13964 bool *is_identifier)
13966 tree identifier;
13967 tree decl;
13968 tree fns;
13969 cp_token *token = cp_lexer_peek_token (parser->lexer);
13971 /* If the next token is `operator', then we have either an
13972 operator-function-id or a conversion-function-id. */
13973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13975 /* We don't know whether we're looking at an
13976 operator-function-id or a conversion-function-id. */
13977 cp_parser_parse_tentatively (parser);
13978 /* Try an operator-function-id. */
13979 identifier = cp_parser_operator_function_id (parser);
13980 /* If that didn't work, try a conversion-function-id. */
13981 if (!cp_parser_parse_definitely (parser))
13983 cp_parser_error (parser, "expected template-name");
13984 return error_mark_node;
13987 /* Look for the identifier. */
13988 else
13989 identifier = cp_parser_identifier (parser);
13991 /* If we didn't find an identifier, we don't have a template-id. */
13992 if (identifier == error_mark_node)
13993 return error_mark_node;
13995 /* If the name immediately followed the `template' keyword, then it
13996 is a template-name. However, if the next token is not `<', then
13997 we do not treat it as a template-name, since it is not being used
13998 as part of a template-id. This enables us to handle constructs
13999 like:
14001 template <typename T> struct S { S(); };
14002 template <typename T> S<T>::S();
14004 correctly. We would treat `S' as a template -- if it were `S<T>'
14005 -- but we do not if there is no `<'. */
14007 if (processing_template_decl
14008 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14010 /* In a declaration, in a dependent context, we pretend that the
14011 "template" keyword was present in order to improve error
14012 recovery. For example, given:
14014 template <typename T> void f(T::X<int>);
14016 we want to treat "X<int>" as a template-id. */
14017 if (is_declaration
14018 && !template_keyword_p
14019 && parser->scope && TYPE_P (parser->scope)
14020 && check_dependency_p
14021 && dependent_scope_p (parser->scope)
14022 /* Do not do this for dtors (or ctors), since they never
14023 need the template keyword before their name. */
14024 && !constructor_name_p (identifier, parser->scope))
14026 cp_token_position start = 0;
14028 /* Explain what went wrong. */
14029 error_at (token->location, "non-template %qD used as template",
14030 identifier);
14031 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14032 parser->scope, identifier);
14033 /* If parsing tentatively, find the location of the "<" token. */
14034 if (cp_parser_simulate_error (parser))
14035 start = cp_lexer_token_position (parser->lexer, true);
14036 /* Parse the template arguments so that we can issue error
14037 messages about them. */
14038 cp_lexer_consume_token (parser->lexer);
14039 cp_parser_enclosed_template_argument_list (parser);
14040 /* Skip tokens until we find a good place from which to
14041 continue parsing. */
14042 cp_parser_skip_to_closing_parenthesis (parser,
14043 /*recovering=*/true,
14044 /*or_comma=*/true,
14045 /*consume_paren=*/false);
14046 /* If parsing tentatively, permanently remove the
14047 template argument list. That will prevent duplicate
14048 error messages from being issued about the missing
14049 "template" keyword. */
14050 if (start)
14051 cp_lexer_purge_tokens_after (parser->lexer, start);
14052 if (is_identifier)
14053 *is_identifier = true;
14054 return identifier;
14057 /* If the "template" keyword is present, then there is generally
14058 no point in doing name-lookup, so we just return IDENTIFIER.
14059 But, if the qualifying scope is non-dependent then we can
14060 (and must) do name-lookup normally. */
14061 if (template_keyword_p
14062 && (!parser->scope
14063 || (TYPE_P (parser->scope)
14064 && dependent_type_p (parser->scope))))
14065 return identifier;
14068 /* Look up the name. */
14069 decl = cp_parser_lookup_name (parser, identifier,
14070 tag_type,
14071 /*is_template=*/true,
14072 /*is_namespace=*/false,
14073 check_dependency_p,
14074 /*ambiguous_decls=*/NULL,
14075 token->location);
14077 decl = strip_using_decl (decl);
14079 /* If DECL is a template, then the name was a template-name. */
14080 if (TREE_CODE (decl) == TEMPLATE_DECL)
14082 if (TREE_DEPRECATED (decl)
14083 && deprecated_state != DEPRECATED_SUPPRESS)
14084 warn_deprecated_use (decl, NULL_TREE);
14086 else
14088 tree fn = NULL_TREE;
14090 /* The standard does not explicitly indicate whether a name that
14091 names a set of overloaded declarations, some of which are
14092 templates, is a template-name. However, such a name should
14093 be a template-name; otherwise, there is no way to form a
14094 template-id for the overloaded templates. */
14095 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14096 if (TREE_CODE (fns) == OVERLOAD)
14097 for (fn = fns; fn; fn = OVL_NEXT (fn))
14098 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14099 break;
14101 if (!fn)
14103 /* The name does not name a template. */
14104 cp_parser_error (parser, "expected template-name");
14105 return error_mark_node;
14109 /* If DECL is dependent, and refers to a function, then just return
14110 its name; we will look it up again during template instantiation. */
14111 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14113 tree scope = ovl_scope (decl);
14114 if (TYPE_P (scope) && dependent_type_p (scope))
14115 return identifier;
14118 return decl;
14121 /* Parse a template-argument-list.
14123 template-argument-list:
14124 template-argument ... [opt]
14125 template-argument-list , template-argument ... [opt]
14127 Returns a TREE_VEC containing the arguments. */
14129 static tree
14130 cp_parser_template_argument_list (cp_parser* parser)
14132 tree fixed_args[10];
14133 unsigned n_args = 0;
14134 unsigned alloced = 10;
14135 tree *arg_ary = fixed_args;
14136 tree vec;
14137 bool saved_in_template_argument_list_p;
14138 bool saved_ice_p;
14139 bool saved_non_ice_p;
14141 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14142 parser->in_template_argument_list_p = true;
14143 /* Even if the template-id appears in an integral
14144 constant-expression, the contents of the argument list do
14145 not. */
14146 saved_ice_p = parser->integral_constant_expression_p;
14147 parser->integral_constant_expression_p = false;
14148 saved_non_ice_p = parser->non_integral_constant_expression_p;
14149 parser->non_integral_constant_expression_p = false;
14151 /* Parse the arguments. */
14154 tree argument;
14156 if (n_args)
14157 /* Consume the comma. */
14158 cp_lexer_consume_token (parser->lexer);
14160 /* Parse the template-argument. */
14161 argument = cp_parser_template_argument (parser);
14163 /* If the next token is an ellipsis, we're expanding a template
14164 argument pack. */
14165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14167 if (argument == error_mark_node)
14169 cp_token *token = cp_lexer_peek_token (parser->lexer);
14170 error_at (token->location,
14171 "expected parameter pack before %<...%>");
14173 /* Consume the `...' token. */
14174 cp_lexer_consume_token (parser->lexer);
14176 /* Make the argument into a TYPE_PACK_EXPANSION or
14177 EXPR_PACK_EXPANSION. */
14178 argument = make_pack_expansion (argument);
14181 if (n_args == alloced)
14183 alloced *= 2;
14185 if (arg_ary == fixed_args)
14187 arg_ary = XNEWVEC (tree, alloced);
14188 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14190 else
14191 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14193 arg_ary[n_args++] = argument;
14195 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14197 vec = make_tree_vec (n_args);
14199 while (n_args--)
14200 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14202 if (arg_ary != fixed_args)
14203 free (arg_ary);
14204 parser->non_integral_constant_expression_p = saved_non_ice_p;
14205 parser->integral_constant_expression_p = saved_ice_p;
14206 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14207 #ifdef ENABLE_CHECKING
14208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14209 #endif
14210 return vec;
14213 /* Parse a template-argument.
14215 template-argument:
14216 assignment-expression
14217 type-id
14218 id-expression
14220 The representation is that of an assignment-expression, type-id, or
14221 id-expression -- except that the qualified id-expression is
14222 evaluated, so that the value returned is either a DECL or an
14223 OVERLOAD.
14225 Although the standard says "assignment-expression", it forbids
14226 throw-expressions or assignments in the template argument.
14227 Therefore, we use "conditional-expression" instead. */
14229 static tree
14230 cp_parser_template_argument (cp_parser* parser)
14232 tree argument;
14233 bool template_p;
14234 bool address_p;
14235 bool maybe_type_id = false;
14236 cp_token *token = NULL, *argument_start_token = NULL;
14237 location_t loc = 0;
14238 cp_id_kind idk;
14240 /* There's really no way to know what we're looking at, so we just
14241 try each alternative in order.
14243 [temp.arg]
14245 In a template-argument, an ambiguity between a type-id and an
14246 expression is resolved to a type-id, regardless of the form of
14247 the corresponding template-parameter.
14249 Therefore, we try a type-id first. */
14250 cp_parser_parse_tentatively (parser);
14251 argument = cp_parser_template_type_arg (parser);
14252 /* If there was no error parsing the type-id but the next token is a
14253 '>>', our behavior depends on which dialect of C++ we're
14254 parsing. In C++98, we probably found a typo for '> >'. But there
14255 are type-id which are also valid expressions. For instance:
14257 struct X { int operator >> (int); };
14258 template <int V> struct Foo {};
14259 Foo<X () >> 5> r;
14261 Here 'X()' is a valid type-id of a function type, but the user just
14262 wanted to write the expression "X() >> 5". Thus, we remember that we
14263 found a valid type-id, but we still try to parse the argument as an
14264 expression to see what happens.
14266 In C++0x, the '>>' will be considered two separate '>'
14267 tokens. */
14268 if (!cp_parser_error_occurred (parser)
14269 && cxx_dialect == cxx98
14270 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14272 maybe_type_id = true;
14273 cp_parser_abort_tentative_parse (parser);
14275 else
14277 /* If the next token isn't a `,' or a `>', then this argument wasn't
14278 really finished. This means that the argument is not a valid
14279 type-id. */
14280 if (!cp_parser_next_token_ends_template_argument_p (parser))
14281 cp_parser_error (parser, "expected template-argument");
14282 /* If that worked, we're done. */
14283 if (cp_parser_parse_definitely (parser))
14284 return argument;
14286 /* We're still not sure what the argument will be. */
14287 cp_parser_parse_tentatively (parser);
14288 /* Try a template. */
14289 argument_start_token = cp_lexer_peek_token (parser->lexer);
14290 argument = cp_parser_id_expression (parser,
14291 /*template_keyword_p=*/false,
14292 /*check_dependency_p=*/true,
14293 &template_p,
14294 /*declarator_p=*/false,
14295 /*optional_p=*/false);
14296 /* If the next token isn't a `,' or a `>', then this argument wasn't
14297 really finished. */
14298 if (!cp_parser_next_token_ends_template_argument_p (parser))
14299 cp_parser_error (parser, "expected template-argument");
14300 if (!cp_parser_error_occurred (parser))
14302 /* Figure out what is being referred to. If the id-expression
14303 was for a class template specialization, then we will have a
14304 TYPE_DECL at this point. There is no need to do name lookup
14305 at this point in that case. */
14306 if (TREE_CODE (argument) != TYPE_DECL)
14307 argument = cp_parser_lookup_name (parser, argument,
14308 none_type,
14309 /*is_template=*/template_p,
14310 /*is_namespace=*/false,
14311 /*check_dependency=*/true,
14312 /*ambiguous_decls=*/NULL,
14313 argument_start_token->location);
14314 if (TREE_CODE (argument) != TEMPLATE_DECL
14315 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14316 cp_parser_error (parser, "expected template-name");
14318 if (cp_parser_parse_definitely (parser))
14320 if (TREE_DEPRECATED (argument))
14321 warn_deprecated_use (argument, NULL_TREE);
14322 return argument;
14324 /* It must be a non-type argument. There permitted cases are given
14325 in [temp.arg.nontype]:
14327 -- an integral constant-expression of integral or enumeration
14328 type; or
14330 -- the name of a non-type template-parameter; or
14332 -- the name of an object or function with external linkage...
14334 -- the address of an object or function with external linkage...
14336 -- a pointer to member... */
14337 /* Look for a non-type template parameter. */
14338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14340 cp_parser_parse_tentatively (parser);
14341 argument = cp_parser_primary_expression (parser,
14342 /*address_p=*/false,
14343 /*cast_p=*/false,
14344 /*template_arg_p=*/true,
14345 &idk);
14346 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14347 || !cp_parser_next_token_ends_template_argument_p (parser))
14348 cp_parser_simulate_error (parser);
14349 if (cp_parser_parse_definitely (parser))
14350 return argument;
14353 /* If the next token is "&", the argument must be the address of an
14354 object or function with external linkage. */
14355 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14356 if (address_p)
14358 loc = cp_lexer_peek_token (parser->lexer)->location;
14359 cp_lexer_consume_token (parser->lexer);
14361 /* See if we might have an id-expression. */
14362 token = cp_lexer_peek_token (parser->lexer);
14363 if (token->type == CPP_NAME
14364 || token->keyword == RID_OPERATOR
14365 || token->type == CPP_SCOPE
14366 || token->type == CPP_TEMPLATE_ID
14367 || token->type == CPP_NESTED_NAME_SPECIFIER)
14369 cp_parser_parse_tentatively (parser);
14370 argument = cp_parser_primary_expression (parser,
14371 address_p,
14372 /*cast_p=*/false,
14373 /*template_arg_p=*/true,
14374 &idk);
14375 if (cp_parser_error_occurred (parser)
14376 || !cp_parser_next_token_ends_template_argument_p (parser))
14377 cp_parser_abort_tentative_parse (parser);
14378 else
14380 tree probe;
14382 if (INDIRECT_REF_P (argument))
14384 /* Strip the dereference temporarily. */
14385 gcc_assert (REFERENCE_REF_P (argument));
14386 argument = TREE_OPERAND (argument, 0);
14389 /* If we're in a template, we represent a qualified-id referring
14390 to a static data member as a SCOPE_REF even if the scope isn't
14391 dependent so that we can check access control later. */
14392 probe = argument;
14393 if (TREE_CODE (probe) == SCOPE_REF)
14394 probe = TREE_OPERAND (probe, 1);
14395 if (VAR_P (probe))
14397 /* A variable without external linkage might still be a
14398 valid constant-expression, so no error is issued here
14399 if the external-linkage check fails. */
14400 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14401 cp_parser_simulate_error (parser);
14403 else if (is_overloaded_fn (argument))
14404 /* All overloaded functions are allowed; if the external
14405 linkage test does not pass, an error will be issued
14406 later. */
14408 else if (address_p
14409 && (TREE_CODE (argument) == OFFSET_REF
14410 || TREE_CODE (argument) == SCOPE_REF))
14411 /* A pointer-to-member. */
14413 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14415 else
14416 cp_parser_simulate_error (parser);
14418 if (cp_parser_parse_definitely (parser))
14420 if (address_p)
14421 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14422 tf_warning_or_error);
14423 else
14424 argument = convert_from_reference (argument);
14425 return argument;
14429 /* If the argument started with "&", there are no other valid
14430 alternatives at this point. */
14431 if (address_p)
14433 cp_parser_error (parser, "invalid non-type template argument");
14434 return error_mark_node;
14437 /* If the argument wasn't successfully parsed as a type-id followed
14438 by '>>', the argument can only be a constant expression now.
14439 Otherwise, we try parsing the constant-expression tentatively,
14440 because the argument could really be a type-id. */
14441 if (maybe_type_id)
14442 cp_parser_parse_tentatively (parser);
14443 argument = cp_parser_constant_expression (parser);
14445 if (!maybe_type_id)
14446 return argument;
14447 if (!cp_parser_next_token_ends_template_argument_p (parser))
14448 cp_parser_error (parser, "expected template-argument");
14449 if (cp_parser_parse_definitely (parser))
14450 return argument;
14451 /* We did our best to parse the argument as a non type-id, but that
14452 was the only alternative that matched (albeit with a '>' after
14453 it). We can assume it's just a typo from the user, and a
14454 diagnostic will then be issued. */
14455 return cp_parser_template_type_arg (parser);
14458 /* Parse an explicit-instantiation.
14460 explicit-instantiation:
14461 template declaration
14463 Although the standard says `declaration', what it really means is:
14465 explicit-instantiation:
14466 template decl-specifier-seq [opt] declarator [opt] ;
14468 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14469 supposed to be allowed. A defect report has been filed about this
14470 issue.
14472 GNU Extension:
14474 explicit-instantiation:
14475 storage-class-specifier template
14476 decl-specifier-seq [opt] declarator [opt] ;
14477 function-specifier template
14478 decl-specifier-seq [opt] declarator [opt] ; */
14480 static void
14481 cp_parser_explicit_instantiation (cp_parser* parser)
14483 int declares_class_or_enum;
14484 cp_decl_specifier_seq decl_specifiers;
14485 tree extension_specifier = NULL_TREE;
14487 timevar_push (TV_TEMPLATE_INST);
14489 /* Look for an (optional) storage-class-specifier or
14490 function-specifier. */
14491 if (cp_parser_allow_gnu_extensions_p (parser))
14493 extension_specifier
14494 = cp_parser_storage_class_specifier_opt (parser);
14495 if (!extension_specifier)
14496 extension_specifier
14497 = cp_parser_function_specifier_opt (parser,
14498 /*decl_specs=*/NULL);
14501 /* Look for the `template' keyword. */
14502 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14503 /* Let the front end know that we are processing an explicit
14504 instantiation. */
14505 begin_explicit_instantiation ();
14506 /* [temp.explicit] says that we are supposed to ignore access
14507 control while processing explicit instantiation directives. */
14508 push_deferring_access_checks (dk_no_check);
14509 /* Parse a decl-specifier-seq. */
14510 cp_parser_decl_specifier_seq (parser,
14511 CP_PARSER_FLAGS_OPTIONAL,
14512 &decl_specifiers,
14513 &declares_class_or_enum);
14514 /* If there was exactly one decl-specifier, and it declared a class,
14515 and there's no declarator, then we have an explicit type
14516 instantiation. */
14517 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14519 tree type;
14521 type = check_tag_decl (&decl_specifiers,
14522 /*explicit_type_instantiation_p=*/true);
14523 /* Turn access control back on for names used during
14524 template instantiation. */
14525 pop_deferring_access_checks ();
14526 if (type)
14527 do_type_instantiation (type, extension_specifier,
14528 /*complain=*/tf_error);
14530 else
14532 cp_declarator *declarator;
14533 tree decl;
14535 /* Parse the declarator. */
14536 declarator
14537 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14538 /*ctor_dtor_or_conv_p=*/NULL,
14539 /*parenthesized_p=*/NULL,
14540 /*member_p=*/false,
14541 /*friend_p=*/false);
14542 if (declares_class_or_enum & 2)
14543 cp_parser_check_for_definition_in_return_type (declarator,
14544 decl_specifiers.type,
14545 decl_specifiers.locations[ds_type_spec]);
14546 if (declarator != cp_error_declarator)
14548 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14549 permerror (decl_specifiers.locations[ds_inline],
14550 "explicit instantiation shall not use"
14551 " %<inline%> specifier");
14552 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14553 permerror (decl_specifiers.locations[ds_constexpr],
14554 "explicit instantiation shall not use"
14555 " %<constexpr%> specifier");
14557 decl = grokdeclarator (declarator, &decl_specifiers,
14558 NORMAL, 0, &decl_specifiers.attributes);
14559 /* Turn access control back on for names used during
14560 template instantiation. */
14561 pop_deferring_access_checks ();
14562 /* Do the explicit instantiation. */
14563 do_decl_instantiation (decl, extension_specifier);
14565 else
14567 pop_deferring_access_checks ();
14568 /* Skip the body of the explicit instantiation. */
14569 cp_parser_skip_to_end_of_statement (parser);
14572 /* We're done with the instantiation. */
14573 end_explicit_instantiation ();
14575 cp_parser_consume_semicolon_at_end_of_statement (parser);
14577 timevar_pop (TV_TEMPLATE_INST);
14580 /* Parse an explicit-specialization.
14582 explicit-specialization:
14583 template < > declaration
14585 Although the standard says `declaration', what it really means is:
14587 explicit-specialization:
14588 template <> decl-specifier [opt] init-declarator [opt] ;
14589 template <> function-definition
14590 template <> explicit-specialization
14591 template <> template-declaration */
14593 static void
14594 cp_parser_explicit_specialization (cp_parser* parser)
14596 bool need_lang_pop;
14597 cp_token *token = cp_lexer_peek_token (parser->lexer);
14599 /* Look for the `template' keyword. */
14600 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14601 /* Look for the `<'. */
14602 cp_parser_require (parser, CPP_LESS, RT_LESS);
14603 /* Look for the `>'. */
14604 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14605 /* We have processed another parameter list. */
14606 ++parser->num_template_parameter_lists;
14607 /* [temp]
14609 A template ... explicit specialization ... shall not have C
14610 linkage. */
14611 if (current_lang_name == lang_name_c)
14613 error_at (token->location, "template specialization with C linkage");
14614 /* Give it C++ linkage to avoid confusing other parts of the
14615 front end. */
14616 push_lang_context (lang_name_cplusplus);
14617 need_lang_pop = true;
14619 else
14620 need_lang_pop = false;
14621 /* Let the front end know that we are beginning a specialization. */
14622 if (!begin_specialization ())
14624 end_specialization ();
14625 return;
14628 /* If the next keyword is `template', we need to figure out whether
14629 or not we're looking a template-declaration. */
14630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14632 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14633 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14634 cp_parser_template_declaration_after_export (parser,
14635 /*member_p=*/false);
14636 else
14637 cp_parser_explicit_specialization (parser);
14639 else
14640 /* Parse the dependent declaration. */
14641 cp_parser_single_declaration (parser,
14642 /*checks=*/NULL,
14643 /*member_p=*/false,
14644 /*explicit_specialization_p=*/true,
14645 /*friend_p=*/NULL);
14646 /* We're done with the specialization. */
14647 end_specialization ();
14648 /* For the erroneous case of a template with C linkage, we pushed an
14649 implicit C++ linkage scope; exit that scope now. */
14650 if (need_lang_pop)
14651 pop_lang_context ();
14652 /* We're done with this parameter list. */
14653 --parser->num_template_parameter_lists;
14656 /* Parse a type-specifier.
14658 type-specifier:
14659 simple-type-specifier
14660 class-specifier
14661 enum-specifier
14662 elaborated-type-specifier
14663 cv-qualifier
14665 GNU Extension:
14667 type-specifier:
14668 __complex__
14670 Returns a representation of the type-specifier. For a
14671 class-specifier, enum-specifier, or elaborated-type-specifier, a
14672 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14674 The parser flags FLAGS is used to control type-specifier parsing.
14676 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14677 in a decl-specifier-seq.
14679 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14680 class-specifier, enum-specifier, or elaborated-type-specifier, then
14681 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14682 if a type is declared; 2 if it is defined. Otherwise, it is set to
14683 zero.
14685 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14686 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14687 is set to FALSE. */
14689 static tree
14690 cp_parser_type_specifier (cp_parser* parser,
14691 cp_parser_flags flags,
14692 cp_decl_specifier_seq *decl_specs,
14693 bool is_declaration,
14694 int* declares_class_or_enum,
14695 bool* is_cv_qualifier)
14697 tree type_spec = NULL_TREE;
14698 cp_token *token;
14699 enum rid keyword;
14700 cp_decl_spec ds = ds_last;
14702 /* Assume this type-specifier does not declare a new type. */
14703 if (declares_class_or_enum)
14704 *declares_class_or_enum = 0;
14705 /* And that it does not specify a cv-qualifier. */
14706 if (is_cv_qualifier)
14707 *is_cv_qualifier = false;
14708 /* Peek at the next token. */
14709 token = cp_lexer_peek_token (parser->lexer);
14711 /* If we're looking at a keyword, we can use that to guide the
14712 production we choose. */
14713 keyword = token->keyword;
14714 switch (keyword)
14716 case RID_ENUM:
14717 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14718 goto elaborated_type_specifier;
14720 /* Look for the enum-specifier. */
14721 type_spec = cp_parser_enum_specifier (parser);
14722 /* If that worked, we're done. */
14723 if (type_spec)
14725 if (declares_class_or_enum)
14726 *declares_class_or_enum = 2;
14727 if (decl_specs)
14728 cp_parser_set_decl_spec_type (decl_specs,
14729 type_spec,
14730 token,
14731 /*type_definition_p=*/true);
14732 return type_spec;
14734 else
14735 goto elaborated_type_specifier;
14737 /* Any of these indicate either a class-specifier, or an
14738 elaborated-type-specifier. */
14739 case RID_CLASS:
14740 case RID_STRUCT:
14741 case RID_UNION:
14742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14743 goto elaborated_type_specifier;
14745 /* Parse tentatively so that we can back up if we don't find a
14746 class-specifier. */
14747 cp_parser_parse_tentatively (parser);
14748 /* Look for the class-specifier. */
14749 type_spec = cp_parser_class_specifier (parser);
14750 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14751 /* If that worked, we're done. */
14752 if (cp_parser_parse_definitely (parser))
14754 if (declares_class_or_enum)
14755 *declares_class_or_enum = 2;
14756 if (decl_specs)
14757 cp_parser_set_decl_spec_type (decl_specs,
14758 type_spec,
14759 token,
14760 /*type_definition_p=*/true);
14761 return type_spec;
14764 /* Fall through. */
14765 elaborated_type_specifier:
14766 /* We're declaring (not defining) a class or enum. */
14767 if (declares_class_or_enum)
14768 *declares_class_or_enum = 1;
14770 /* Fall through. */
14771 case RID_TYPENAME:
14772 /* Look for an elaborated-type-specifier. */
14773 type_spec
14774 = (cp_parser_elaborated_type_specifier
14775 (parser,
14776 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14777 is_declaration));
14778 if (decl_specs)
14779 cp_parser_set_decl_spec_type (decl_specs,
14780 type_spec,
14781 token,
14782 /*type_definition_p=*/false);
14783 return type_spec;
14785 case RID_CONST:
14786 ds = ds_const;
14787 if (is_cv_qualifier)
14788 *is_cv_qualifier = true;
14789 break;
14791 case RID_VOLATILE:
14792 ds = ds_volatile;
14793 if (is_cv_qualifier)
14794 *is_cv_qualifier = true;
14795 break;
14797 case RID_RESTRICT:
14798 ds = ds_restrict;
14799 if (is_cv_qualifier)
14800 *is_cv_qualifier = true;
14801 break;
14803 case RID_COMPLEX:
14804 /* The `__complex__' keyword is a GNU extension. */
14805 ds = ds_complex;
14806 break;
14808 default:
14809 break;
14812 /* Handle simple keywords. */
14813 if (ds != ds_last)
14815 if (decl_specs)
14817 set_and_check_decl_spec_loc (decl_specs, ds, token);
14818 decl_specs->any_specifiers_p = true;
14820 return cp_lexer_consume_token (parser->lexer)->u.value;
14823 /* If we do not already have a type-specifier, assume we are looking
14824 at a simple-type-specifier. */
14825 type_spec = cp_parser_simple_type_specifier (parser,
14826 decl_specs,
14827 flags);
14829 /* If we didn't find a type-specifier, and a type-specifier was not
14830 optional in this context, issue an error message. */
14831 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14833 cp_parser_error (parser, "expected type specifier");
14834 return error_mark_node;
14837 return type_spec;
14840 /* Parse a simple-type-specifier.
14842 simple-type-specifier:
14843 :: [opt] nested-name-specifier [opt] type-name
14844 :: [opt] nested-name-specifier template template-id
14845 char
14846 wchar_t
14847 bool
14848 short
14850 long
14851 signed
14852 unsigned
14853 float
14854 double
14855 void
14857 C++0x Extension:
14859 simple-type-specifier:
14860 auto
14861 decltype ( expression )
14862 char16_t
14863 char32_t
14864 __underlying_type ( type-id )
14866 GNU Extension:
14868 simple-type-specifier:
14869 __int128
14870 __typeof__ unary-expression
14871 __typeof__ ( type-id )
14872 __typeof__ ( type-id ) { initializer-list , [opt] }
14874 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14875 appropriately updated. */
14877 static tree
14878 cp_parser_simple_type_specifier (cp_parser* parser,
14879 cp_decl_specifier_seq *decl_specs,
14880 cp_parser_flags flags)
14882 tree type = NULL_TREE;
14883 cp_token *token;
14884 int idx;
14886 /* Peek at the next token. */
14887 token = cp_lexer_peek_token (parser->lexer);
14889 /* If we're looking at a keyword, things are easy. */
14890 switch (token->keyword)
14892 case RID_CHAR:
14893 if (decl_specs)
14894 decl_specs->explicit_char_p = true;
14895 type = char_type_node;
14896 break;
14897 case RID_CHAR16:
14898 type = char16_type_node;
14899 break;
14900 case RID_CHAR32:
14901 type = char32_type_node;
14902 break;
14903 case RID_WCHAR:
14904 type = wchar_type_node;
14905 break;
14906 case RID_BOOL:
14907 type = boolean_type_node;
14908 break;
14909 case RID_SHORT:
14910 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14911 type = short_integer_type_node;
14912 break;
14913 case RID_INT:
14914 if (decl_specs)
14915 decl_specs->explicit_int_p = true;
14916 type = integer_type_node;
14917 break;
14918 case RID_INT_N_0:
14919 case RID_INT_N_1:
14920 case RID_INT_N_2:
14921 case RID_INT_N_3:
14922 idx = token->keyword - RID_INT_N_0;
14923 if (! int_n_enabled_p [idx])
14924 break;
14925 if (decl_specs)
14927 decl_specs->explicit_intN_p = true;
14928 decl_specs->int_n_idx = idx;
14930 type = int_n_trees [idx].signed_type;
14931 break;
14932 case RID_LONG:
14933 if (decl_specs)
14934 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14935 type = long_integer_type_node;
14936 break;
14937 case RID_SIGNED:
14938 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14939 type = integer_type_node;
14940 break;
14941 case RID_UNSIGNED:
14942 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14943 type = unsigned_type_node;
14944 break;
14945 case RID_FLOAT:
14946 type = float_type_node;
14947 break;
14948 case RID_DOUBLE:
14949 type = double_type_node;
14950 break;
14951 case RID_VOID:
14952 type = void_type_node;
14953 break;
14955 case RID_AUTO:
14956 maybe_warn_cpp0x (CPP0X_AUTO);
14957 if (parser->auto_is_implicit_function_template_parm_p)
14959 /* The 'auto' might be the placeholder return type for a function decl
14960 with trailing return type. */
14961 bool have_trailing_return_fn_decl = false;
14962 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14963 == CPP_OPEN_PAREN)
14965 cp_parser_parse_tentatively (parser);
14966 cp_lexer_consume_token (parser->lexer);
14967 cp_lexer_consume_token (parser->lexer);
14968 if (cp_parser_skip_to_closing_parenthesis (parser,
14969 /*recovering*/false,
14970 /*or_comma*/false,
14971 /*consume_paren*/true))
14972 have_trailing_return_fn_decl
14973 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14974 cp_parser_abort_tentative_parse (parser);
14977 if (have_trailing_return_fn_decl)
14979 type = make_auto ();
14980 break;
14983 if (cxx_dialect >= cxx14)
14984 type = synthesize_implicit_template_parm (parser);
14985 else
14986 type = error_mark_node;
14988 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14990 if (cxx_dialect < cxx14)
14991 error_at (token->location,
14992 "use of %<auto%> in lambda parameter declaration "
14993 "only available with "
14994 "-std=c++14 or -std=gnu++14");
14996 else if (cxx_dialect < cxx14)
14997 error_at (token->location,
14998 "use of %<auto%> in parameter declaration "
14999 "only available with "
15000 "-std=c++14 or -std=gnu++14");
15001 else
15002 pedwarn (token->location, OPT_Wpedantic,
15003 "ISO C++ forbids use of %<auto%> in parameter "
15004 "declaration");
15006 else
15007 type = make_auto ();
15008 break;
15010 case RID_DECLTYPE:
15011 /* Since DR 743, decltype can either be a simple-type-specifier by
15012 itself or begin a nested-name-specifier. Parsing it will replace
15013 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15014 handling below decide what to do. */
15015 cp_parser_decltype (parser);
15016 cp_lexer_set_token_position (parser->lexer, token);
15017 break;
15019 case RID_TYPEOF:
15020 /* Consume the `typeof' token. */
15021 cp_lexer_consume_token (parser->lexer);
15022 /* Parse the operand to `typeof'. */
15023 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15024 /* If it is not already a TYPE, take its type. */
15025 if (!TYPE_P (type))
15026 type = finish_typeof (type);
15028 if (decl_specs)
15029 cp_parser_set_decl_spec_type (decl_specs, type,
15030 token,
15031 /*type_definition_p=*/false);
15033 return type;
15035 case RID_UNDERLYING_TYPE:
15036 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15037 if (decl_specs)
15038 cp_parser_set_decl_spec_type (decl_specs, type,
15039 token,
15040 /*type_definition_p=*/false);
15042 return type;
15044 case RID_BASES:
15045 case RID_DIRECT_BASES:
15046 type = cp_parser_trait_expr (parser, token->keyword);
15047 if (decl_specs)
15048 cp_parser_set_decl_spec_type (decl_specs, type,
15049 token,
15050 /*type_definition_p=*/false);
15051 return type;
15052 default:
15053 break;
15056 /* If token is an already-parsed decltype not followed by ::,
15057 it's a simple-type-specifier. */
15058 if (token->type == CPP_DECLTYPE
15059 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15061 type = token->u.value;
15062 if (decl_specs)
15064 cp_parser_set_decl_spec_type (decl_specs, type,
15065 token,
15066 /*type_definition_p=*/false);
15067 /* Remember that we are handling a decltype in order to
15068 implement the resolution of DR 1510 when the argument
15069 isn't instantiation dependent. */
15070 decl_specs->decltype_p = true;
15072 cp_lexer_consume_token (parser->lexer);
15073 return type;
15076 /* If the type-specifier was for a built-in type, we're done. */
15077 if (type)
15079 /* Record the type. */
15080 if (decl_specs
15081 && (token->keyword != RID_SIGNED
15082 && token->keyword != RID_UNSIGNED
15083 && token->keyword != RID_SHORT
15084 && token->keyword != RID_LONG))
15085 cp_parser_set_decl_spec_type (decl_specs,
15086 type,
15087 token,
15088 /*type_definition_p=*/false);
15089 if (decl_specs)
15090 decl_specs->any_specifiers_p = true;
15092 /* Consume the token. */
15093 cp_lexer_consume_token (parser->lexer);
15095 if (type == error_mark_node)
15096 return error_mark_node;
15098 /* There is no valid C++ program where a non-template type is
15099 followed by a "<". That usually indicates that the user thought
15100 that the type was a template. */
15101 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15102 token->location);
15104 return TYPE_NAME (type);
15107 /* The type-specifier must be a user-defined type. */
15108 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15110 bool qualified_p;
15111 bool global_p;
15113 /* Don't gobble tokens or issue error messages if this is an
15114 optional type-specifier. */
15115 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15116 cp_parser_parse_tentatively (parser);
15118 /* Look for the optional `::' operator. */
15119 global_p
15120 = (cp_parser_global_scope_opt (parser,
15121 /*current_scope_valid_p=*/false)
15122 != NULL_TREE);
15123 /* Look for the nested-name specifier. */
15124 qualified_p
15125 = (cp_parser_nested_name_specifier_opt (parser,
15126 /*typename_keyword_p=*/false,
15127 /*check_dependency_p=*/true,
15128 /*type_p=*/false,
15129 /*is_declaration=*/false)
15130 != NULL_TREE);
15131 token = cp_lexer_peek_token (parser->lexer);
15132 /* If we have seen a nested-name-specifier, and the next token
15133 is `template', then we are using the template-id production. */
15134 if (parser->scope
15135 && cp_parser_optional_template_keyword (parser))
15137 /* Look for the template-id. */
15138 type = cp_parser_template_id (parser,
15139 /*template_keyword_p=*/true,
15140 /*check_dependency_p=*/true,
15141 none_type,
15142 /*is_declaration=*/false);
15143 /* If the template-id did not name a type, we are out of
15144 luck. */
15145 if (TREE_CODE (type) != TYPE_DECL)
15147 cp_parser_error (parser, "expected template-id for type");
15148 type = NULL_TREE;
15151 /* Otherwise, look for a type-name. */
15152 else
15153 type = cp_parser_type_name (parser);
15154 /* Keep track of all name-lookups performed in class scopes. */
15155 if (type
15156 && !global_p
15157 && !qualified_p
15158 && TREE_CODE (type) == TYPE_DECL
15159 && identifier_p (DECL_NAME (type)))
15160 maybe_note_name_used_in_class (DECL_NAME (type), type);
15161 /* If it didn't work out, we don't have a TYPE. */
15162 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15163 && !cp_parser_parse_definitely (parser))
15164 type = NULL_TREE;
15165 if (type && decl_specs)
15166 cp_parser_set_decl_spec_type (decl_specs, type,
15167 token,
15168 /*type_definition_p=*/false);
15171 /* If we didn't get a type-name, issue an error message. */
15172 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15174 cp_parser_error (parser, "expected type-name");
15175 return error_mark_node;
15178 if (type && type != error_mark_node)
15180 /* See if TYPE is an Objective-C type, and if so, parse and
15181 accept any protocol references following it. Do this before
15182 the cp_parser_check_for_invalid_template_id() call, because
15183 Objective-C types can be followed by '<...>' which would
15184 enclose protocol names rather than template arguments, and so
15185 everything is fine. */
15186 if (c_dialect_objc () && !parser->scope
15187 && (objc_is_id (type) || objc_is_class_name (type)))
15189 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15190 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15192 /* Clobber the "unqualified" type previously entered into
15193 DECL_SPECS with the new, improved protocol-qualified version. */
15194 if (decl_specs)
15195 decl_specs->type = qual_type;
15197 return qual_type;
15200 /* There is no valid C++ program where a non-template type is
15201 followed by a "<". That usually indicates that the user
15202 thought that the type was a template. */
15203 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15204 none_type,
15205 token->location);
15208 return type;
15211 /* Parse a type-name.
15213 type-name:
15214 class-name
15215 enum-name
15216 typedef-name
15217 simple-template-id [in c++0x]
15219 enum-name:
15220 identifier
15222 typedef-name:
15223 identifier
15225 Returns a TYPE_DECL for the type. */
15227 static tree
15228 cp_parser_type_name (cp_parser* parser)
15230 tree type_decl;
15232 /* We can't know yet whether it is a class-name or not. */
15233 cp_parser_parse_tentatively (parser);
15234 /* Try a class-name. */
15235 type_decl = cp_parser_class_name (parser,
15236 /*typename_keyword_p=*/false,
15237 /*template_keyword_p=*/false,
15238 none_type,
15239 /*check_dependency_p=*/true,
15240 /*class_head_p=*/false,
15241 /*is_declaration=*/false);
15242 /* If it's not a class-name, keep looking. */
15243 if (!cp_parser_parse_definitely (parser))
15245 if (cxx_dialect < cxx11)
15246 /* It must be a typedef-name or an enum-name. */
15247 return cp_parser_nonclass_name (parser);
15249 cp_parser_parse_tentatively (parser);
15250 /* It is either a simple-template-id representing an
15251 instantiation of an alias template... */
15252 type_decl = cp_parser_template_id (parser,
15253 /*template_keyword_p=*/false,
15254 /*check_dependency_p=*/true,
15255 none_type,
15256 /*is_declaration=*/false);
15257 /* Note that this must be an instantiation of an alias template
15258 because [temp.names]/6 says:
15260 A template-id that names an alias template specialization
15261 is a type-name.
15263 Whereas [temp.names]/7 says:
15265 A simple-template-id that names a class template
15266 specialization is a class-name. */
15267 if (type_decl != NULL_TREE
15268 && TREE_CODE (type_decl) == TYPE_DECL
15269 && TYPE_DECL_ALIAS_P (type_decl))
15270 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15271 else
15272 cp_parser_simulate_error (parser);
15274 if (!cp_parser_parse_definitely (parser))
15275 /* ... Or a typedef-name or an enum-name. */
15276 return cp_parser_nonclass_name (parser);
15279 return type_decl;
15282 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15284 enum-name:
15285 identifier
15287 typedef-name:
15288 identifier
15290 Returns a TYPE_DECL for the type. */
15292 static tree
15293 cp_parser_nonclass_name (cp_parser* parser)
15295 tree type_decl;
15296 tree identifier;
15298 cp_token *token = cp_lexer_peek_token (parser->lexer);
15299 identifier = cp_parser_identifier (parser);
15300 if (identifier == error_mark_node)
15301 return error_mark_node;
15303 /* Look up the type-name. */
15304 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15306 type_decl = strip_using_decl (type_decl);
15308 if (TREE_CODE (type_decl) != TYPE_DECL
15309 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15311 /* See if this is an Objective-C type. */
15312 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15313 tree type = objc_get_protocol_qualified_type (identifier, protos);
15314 if (type)
15315 type_decl = TYPE_NAME (type);
15318 /* Issue an error if we did not find a type-name. */
15319 if (TREE_CODE (type_decl) != TYPE_DECL
15320 /* In Objective-C, we have the complication that class names are
15321 normally type names and start declarations (eg, the
15322 "NSObject" in "NSObject *object;"), but can be used in an
15323 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15324 is an expression. So, a classname followed by a dot is not a
15325 valid type-name. */
15326 || (objc_is_class_name (TREE_TYPE (type_decl))
15327 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15329 if (!cp_parser_simulate_error (parser))
15330 cp_parser_name_lookup_error (parser, identifier, type_decl,
15331 NLE_TYPE, token->location);
15332 return error_mark_node;
15334 /* Remember that the name was used in the definition of the
15335 current class so that we can check later to see if the
15336 meaning would have been different after the class was
15337 entirely defined. */
15338 else if (type_decl != error_mark_node
15339 && !parser->scope)
15340 maybe_note_name_used_in_class (identifier, type_decl);
15342 return type_decl;
15345 /* Parse an elaborated-type-specifier. Note that the grammar given
15346 here incorporates the resolution to DR68.
15348 elaborated-type-specifier:
15349 class-key :: [opt] nested-name-specifier [opt] identifier
15350 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15351 enum-key :: [opt] nested-name-specifier [opt] identifier
15352 typename :: [opt] nested-name-specifier identifier
15353 typename :: [opt] nested-name-specifier template [opt]
15354 template-id
15356 GNU extension:
15358 elaborated-type-specifier:
15359 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15360 class-key attributes :: [opt] nested-name-specifier [opt]
15361 template [opt] template-id
15362 enum attributes :: [opt] nested-name-specifier [opt] identifier
15364 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15365 declared `friend'. If IS_DECLARATION is TRUE, then this
15366 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15367 something is being declared.
15369 Returns the TYPE specified. */
15371 static tree
15372 cp_parser_elaborated_type_specifier (cp_parser* parser,
15373 bool is_friend,
15374 bool is_declaration)
15376 enum tag_types tag_type;
15377 tree identifier;
15378 tree type = NULL_TREE;
15379 tree attributes = NULL_TREE;
15380 tree globalscope;
15381 cp_token *token = NULL;
15383 /* See if we're looking at the `enum' keyword. */
15384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15386 /* Consume the `enum' token. */
15387 cp_lexer_consume_token (parser->lexer);
15388 /* Remember that it's an enumeration type. */
15389 tag_type = enum_type;
15390 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15391 enums) is used here. */
15392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15393 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15395 pedwarn (input_location, 0, "elaborated-type-specifier "
15396 "for a scoped enum must not use the %<%D%> keyword",
15397 cp_lexer_peek_token (parser->lexer)->u.value);
15398 /* Consume the `struct' or `class' and parse it anyway. */
15399 cp_lexer_consume_token (parser->lexer);
15401 /* Parse the attributes. */
15402 attributes = cp_parser_attributes_opt (parser);
15404 /* Or, it might be `typename'. */
15405 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15406 RID_TYPENAME))
15408 /* Consume the `typename' token. */
15409 cp_lexer_consume_token (parser->lexer);
15410 /* Remember that it's a `typename' type. */
15411 tag_type = typename_type;
15413 /* Otherwise it must be a class-key. */
15414 else
15416 tag_type = cp_parser_class_key (parser);
15417 if (tag_type == none_type)
15418 return error_mark_node;
15419 /* Parse the attributes. */
15420 attributes = cp_parser_attributes_opt (parser);
15423 /* Look for the `::' operator. */
15424 globalscope = cp_parser_global_scope_opt (parser,
15425 /*current_scope_valid_p=*/false);
15426 /* Look for the nested-name-specifier. */
15427 if (tag_type == typename_type && !globalscope)
15429 if (!cp_parser_nested_name_specifier (parser,
15430 /*typename_keyword_p=*/true,
15431 /*check_dependency_p=*/true,
15432 /*type_p=*/true,
15433 is_declaration))
15434 return error_mark_node;
15436 else
15437 /* Even though `typename' is not present, the proposed resolution
15438 to Core Issue 180 says that in `class A<T>::B', `B' should be
15439 considered a type-name, even if `A<T>' is dependent. */
15440 cp_parser_nested_name_specifier_opt (parser,
15441 /*typename_keyword_p=*/true,
15442 /*check_dependency_p=*/true,
15443 /*type_p=*/true,
15444 is_declaration);
15445 /* For everything but enumeration types, consider a template-id.
15446 For an enumeration type, consider only a plain identifier. */
15447 if (tag_type != enum_type)
15449 bool template_p = false;
15450 tree decl;
15452 /* Allow the `template' keyword. */
15453 template_p = cp_parser_optional_template_keyword (parser);
15454 /* If we didn't see `template', we don't know if there's a
15455 template-id or not. */
15456 if (!template_p)
15457 cp_parser_parse_tentatively (parser);
15458 /* Parse the template-id. */
15459 token = cp_lexer_peek_token (parser->lexer);
15460 decl = cp_parser_template_id (parser, template_p,
15461 /*check_dependency_p=*/true,
15462 tag_type,
15463 is_declaration);
15464 /* If we didn't find a template-id, look for an ordinary
15465 identifier. */
15466 if (!template_p && !cp_parser_parse_definitely (parser))
15468 /* We can get here when cp_parser_template_id, called by
15469 cp_parser_class_name with tag_type == none_type, succeeds
15470 and caches a BASELINK. Then, when called again here,
15471 instead of failing and returning an error_mark_node
15472 returns it (see template/typename17.C in C++11).
15473 ??? Could we diagnose this earlier? */
15474 else if (tag_type == typename_type && BASELINK_P (decl))
15476 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15477 type = error_mark_node;
15479 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15480 in effect, then we must assume that, upon instantiation, the
15481 template will correspond to a class. */
15482 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15483 && tag_type == typename_type)
15484 type = make_typename_type (parser->scope, decl,
15485 typename_type,
15486 /*complain=*/tf_error);
15487 /* If the `typename' keyword is in effect and DECL is not a type
15488 decl, then type is non existent. */
15489 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15491 else if (TREE_CODE (decl) == TYPE_DECL)
15492 type = check_elaborated_type_specifier (tag_type, decl,
15493 /*allow_template_p=*/true);
15494 else if (decl == error_mark_node)
15495 type = error_mark_node;
15498 if (!type)
15500 token = cp_lexer_peek_token (parser->lexer);
15501 identifier = cp_parser_identifier (parser);
15503 if (identifier == error_mark_node)
15505 parser->scope = NULL_TREE;
15506 return error_mark_node;
15509 /* For a `typename', we needn't call xref_tag. */
15510 if (tag_type == typename_type
15511 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15512 return cp_parser_make_typename_type (parser, identifier,
15513 token->location);
15515 /* Template parameter lists apply only if we are not within a
15516 function parameter list. */
15517 bool template_parm_lists_apply
15518 = parser->num_template_parameter_lists;
15519 if (template_parm_lists_apply)
15520 for (cp_binding_level *s = current_binding_level;
15521 s && s->kind != sk_template_parms;
15522 s = s->level_chain)
15523 if (s->kind == sk_function_parms)
15524 template_parm_lists_apply = false;
15526 /* Look up a qualified name in the usual way. */
15527 if (parser->scope)
15529 tree decl;
15530 tree ambiguous_decls;
15532 decl = cp_parser_lookup_name (parser, identifier,
15533 tag_type,
15534 /*is_template=*/false,
15535 /*is_namespace=*/false,
15536 /*check_dependency=*/true,
15537 &ambiguous_decls,
15538 token->location);
15540 /* If the lookup was ambiguous, an error will already have been
15541 issued. */
15542 if (ambiguous_decls)
15543 return error_mark_node;
15545 /* If we are parsing friend declaration, DECL may be a
15546 TEMPLATE_DECL tree node here. However, we need to check
15547 whether this TEMPLATE_DECL results in valid code. Consider
15548 the following example:
15550 namespace N {
15551 template <class T> class C {};
15553 class X {
15554 template <class T> friend class N::C; // #1, valid code
15556 template <class T> class Y {
15557 friend class N::C; // #2, invalid code
15560 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15561 name lookup of `N::C'. We see that friend declaration must
15562 be template for the code to be valid. Note that
15563 processing_template_decl does not work here since it is
15564 always 1 for the above two cases. */
15566 decl = (cp_parser_maybe_treat_template_as_class
15567 (decl, /*tag_name_p=*/is_friend
15568 && template_parm_lists_apply));
15570 if (TREE_CODE (decl) != TYPE_DECL)
15572 cp_parser_diagnose_invalid_type_name (parser,
15573 identifier,
15574 token->location);
15575 return error_mark_node;
15578 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15580 bool allow_template = (template_parm_lists_apply
15581 || DECL_SELF_REFERENCE_P (decl));
15582 type = check_elaborated_type_specifier (tag_type, decl,
15583 allow_template);
15585 if (type == error_mark_node)
15586 return error_mark_node;
15589 /* Forward declarations of nested types, such as
15591 class C1::C2;
15592 class C1::C2::C3;
15594 are invalid unless all components preceding the final '::'
15595 are complete. If all enclosing types are complete, these
15596 declarations become merely pointless.
15598 Invalid forward declarations of nested types are errors
15599 caught elsewhere in parsing. Those that are pointless arrive
15600 here. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15603 && !is_friend && !processing_explicit_instantiation)
15604 warning (0, "declaration %qD does not declare anything", decl);
15606 type = TREE_TYPE (decl);
15608 else
15610 /* An elaborated-type-specifier sometimes introduces a new type and
15611 sometimes names an existing type. Normally, the rule is that it
15612 introduces a new type only if there is not an existing type of
15613 the same name already in scope. For example, given:
15615 struct S {};
15616 void f() { struct S s; }
15618 the `struct S' in the body of `f' is the same `struct S' as in
15619 the global scope; the existing definition is used. However, if
15620 there were no global declaration, this would introduce a new
15621 local class named `S'.
15623 An exception to this rule applies to the following code:
15625 namespace N { struct S; }
15627 Here, the elaborated-type-specifier names a new type
15628 unconditionally; even if there is already an `S' in the
15629 containing scope this declaration names a new type.
15630 This exception only applies if the elaborated-type-specifier
15631 forms the complete declaration:
15633 [class.name]
15635 A declaration consisting solely of `class-key identifier ;' is
15636 either a redeclaration of the name in the current scope or a
15637 forward declaration of the identifier as a class name. It
15638 introduces the name into the current scope.
15640 We are in this situation precisely when the next token is a `;'.
15642 An exception to the exception is that a `friend' declaration does
15643 *not* name a new type; i.e., given:
15645 struct S { friend struct T; };
15647 `T' is not a new type in the scope of `S'.
15649 Also, `new struct S' or `sizeof (struct S)' never results in the
15650 definition of a new type; a new type can only be declared in a
15651 declaration context. */
15653 tag_scope ts;
15654 bool template_p;
15656 if (is_friend)
15657 /* Friends have special name lookup rules. */
15658 ts = ts_within_enclosing_non_class;
15659 else if (is_declaration
15660 && cp_lexer_next_token_is (parser->lexer,
15661 CPP_SEMICOLON))
15662 /* This is a `class-key identifier ;' */
15663 ts = ts_current;
15664 else
15665 ts = ts_global;
15667 template_p =
15668 (template_parm_lists_apply
15669 && (cp_parser_next_token_starts_class_definition_p (parser)
15670 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15671 /* An unqualified name was used to reference this type, so
15672 there were no qualifying templates. */
15673 if (template_parm_lists_apply
15674 && !cp_parser_check_template_parameters (parser,
15675 /*num_templates=*/0,
15676 token->location,
15677 /*declarator=*/NULL))
15678 return error_mark_node;
15679 type = xref_tag (tag_type, identifier, ts, template_p);
15683 if (type == error_mark_node)
15684 return error_mark_node;
15686 /* Allow attributes on forward declarations of classes. */
15687 if (attributes)
15689 if (TREE_CODE (type) == TYPENAME_TYPE)
15690 warning (OPT_Wattributes,
15691 "attributes ignored on uninstantiated type");
15692 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15693 && ! processing_explicit_instantiation)
15694 warning (OPT_Wattributes,
15695 "attributes ignored on template instantiation");
15696 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15697 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15698 else
15699 warning (OPT_Wattributes,
15700 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15703 if (tag_type != enum_type)
15705 /* Indicate whether this class was declared as a `class' or as a
15706 `struct'. */
15707 if (TREE_CODE (type) == RECORD_TYPE)
15708 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15709 cp_parser_check_class_key (tag_type, type);
15712 /* A "<" cannot follow an elaborated type specifier. If that
15713 happens, the user was probably trying to form a template-id. */
15714 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15715 token->location);
15717 return type;
15720 /* Parse an enum-specifier.
15722 enum-specifier:
15723 enum-head { enumerator-list [opt] }
15724 enum-head { enumerator-list , } [C++0x]
15726 enum-head:
15727 enum-key identifier [opt] enum-base [opt]
15728 enum-key nested-name-specifier identifier enum-base [opt]
15730 enum-key:
15731 enum
15732 enum class [C++0x]
15733 enum struct [C++0x]
15735 enum-base: [C++0x]
15736 : type-specifier-seq
15738 opaque-enum-specifier:
15739 enum-key identifier enum-base [opt] ;
15741 GNU Extensions:
15742 enum-key attributes[opt] identifier [opt] enum-base [opt]
15743 { enumerator-list [opt] }attributes[opt]
15744 enum-key attributes[opt] identifier [opt] enum-base [opt]
15745 { enumerator-list, }attributes[opt] [C++0x]
15747 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15748 if the token stream isn't an enum-specifier after all. */
15750 static tree
15751 cp_parser_enum_specifier (cp_parser* parser)
15753 tree identifier;
15754 tree type = NULL_TREE;
15755 tree prev_scope;
15756 tree nested_name_specifier = NULL_TREE;
15757 tree attributes;
15758 bool scoped_enum_p = false;
15759 bool has_underlying_type = false;
15760 bool nested_being_defined = false;
15761 bool new_value_list = false;
15762 bool is_new_type = false;
15763 bool is_anonymous = false;
15764 tree underlying_type = NULL_TREE;
15765 cp_token *type_start_token = NULL;
15766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15768 parser->colon_corrects_to_scope_p = false;
15770 /* Parse tentatively so that we can back up if we don't find a
15771 enum-specifier. */
15772 cp_parser_parse_tentatively (parser);
15774 /* Caller guarantees that the current token is 'enum', an identifier
15775 possibly follows, and the token after that is an opening brace.
15776 If we don't have an identifier, fabricate an anonymous name for
15777 the enumeration being defined. */
15778 cp_lexer_consume_token (parser->lexer);
15780 /* Parse the "class" or "struct", which indicates a scoped
15781 enumeration type in C++0x. */
15782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15783 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15785 if (cxx_dialect < cxx11)
15786 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15788 /* Consume the `struct' or `class' token. */
15789 cp_lexer_consume_token (parser->lexer);
15791 scoped_enum_p = true;
15794 attributes = cp_parser_attributes_opt (parser);
15796 /* Clear the qualification. */
15797 parser->scope = NULL_TREE;
15798 parser->qualifying_scope = NULL_TREE;
15799 parser->object_scope = NULL_TREE;
15801 /* Figure out in what scope the declaration is being placed. */
15802 prev_scope = current_scope ();
15804 type_start_token = cp_lexer_peek_token (parser->lexer);
15806 push_deferring_access_checks (dk_no_check);
15807 nested_name_specifier
15808 = cp_parser_nested_name_specifier_opt (parser,
15809 /*typename_keyword_p=*/true,
15810 /*check_dependency_p=*/false,
15811 /*type_p=*/false,
15812 /*is_declaration=*/false);
15814 if (nested_name_specifier)
15816 tree name;
15818 identifier = cp_parser_identifier (parser);
15819 name = cp_parser_lookup_name (parser, identifier,
15820 enum_type,
15821 /*is_template=*/false,
15822 /*is_namespace=*/false,
15823 /*check_dependency=*/true,
15824 /*ambiguous_decls=*/NULL,
15825 input_location);
15826 if (name && name != error_mark_node)
15828 type = TREE_TYPE (name);
15829 if (TREE_CODE (type) == TYPENAME_TYPE)
15831 /* Are template enums allowed in ISO? */
15832 if (template_parm_scope_p ())
15833 pedwarn (type_start_token->location, OPT_Wpedantic,
15834 "%qD is an enumeration template", name);
15835 /* ignore a typename reference, for it will be solved by name
15836 in start_enum. */
15837 type = NULL_TREE;
15840 else if (nested_name_specifier == error_mark_node)
15841 /* We already issued an error. */;
15842 else
15843 error_at (type_start_token->location,
15844 "%qD is not an enumerator-name", identifier);
15846 else
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15849 identifier = cp_parser_identifier (parser);
15850 else
15852 identifier = make_anon_name ();
15853 is_anonymous = true;
15854 if (scoped_enum_p)
15855 error_at (type_start_token->location,
15856 "anonymous scoped enum is not allowed");
15859 pop_deferring_access_checks ();
15861 /* Check for the `:' that denotes a specified underlying type in C++0x.
15862 Note that a ':' could also indicate a bitfield width, however. */
15863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15865 cp_decl_specifier_seq type_specifiers;
15867 /* Consume the `:'. */
15868 cp_lexer_consume_token (parser->lexer);
15870 /* Parse the type-specifier-seq. */
15871 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15872 /*is_trailing_return=*/false,
15873 &type_specifiers);
15875 /* At this point this is surely not elaborated type specifier. */
15876 if (!cp_parser_parse_definitely (parser))
15877 return NULL_TREE;
15879 if (cxx_dialect < cxx11)
15880 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15882 has_underlying_type = true;
15884 /* If that didn't work, stop. */
15885 if (type_specifiers.type != error_mark_node)
15887 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15888 /*initialized=*/0, NULL);
15889 if (underlying_type == error_mark_node
15890 || check_for_bare_parameter_packs (underlying_type))
15891 underlying_type = NULL_TREE;
15895 /* Look for the `{' but don't consume it yet. */
15896 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15898 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15900 cp_parser_error (parser, "expected %<{%>");
15901 if (has_underlying_type)
15903 type = NULL_TREE;
15904 goto out;
15907 /* An opaque-enum-specifier must have a ';' here. */
15908 if ((scoped_enum_p || underlying_type)
15909 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15911 cp_parser_error (parser, "expected %<;%> or %<{%>");
15912 if (has_underlying_type)
15914 type = NULL_TREE;
15915 goto out;
15920 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15921 return NULL_TREE;
15923 if (nested_name_specifier)
15925 if (CLASS_TYPE_P (nested_name_specifier))
15927 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15928 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15929 push_scope (nested_name_specifier);
15931 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15933 push_nested_namespace (nested_name_specifier);
15937 /* Issue an error message if type-definitions are forbidden here. */
15938 if (!cp_parser_check_type_definition (parser))
15939 type = error_mark_node;
15940 else
15941 /* Create the new type. We do this before consuming the opening
15942 brace so the enum will be recorded as being on the line of its
15943 tag (or the 'enum' keyword, if there is no tag). */
15944 type = start_enum (identifier, type, underlying_type,
15945 scoped_enum_p, &is_new_type);
15947 /* If the next token is not '{' it is an opaque-enum-specifier or an
15948 elaborated-type-specifier. */
15949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15951 timevar_push (TV_PARSE_ENUM);
15952 if (nested_name_specifier
15953 && nested_name_specifier != error_mark_node)
15955 /* The following catches invalid code such as:
15956 enum class S<int>::E { A, B, C }; */
15957 if (!processing_specialization
15958 && CLASS_TYPE_P (nested_name_specifier)
15959 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15960 error_at (type_start_token->location, "cannot add an enumerator "
15961 "list to a template instantiation");
15963 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15965 error_at (type_start_token->location,
15966 "%<%T::%E%> has not been declared",
15967 TYPE_CONTEXT (nested_name_specifier),
15968 nested_name_specifier);
15969 type = error_mark_node;
15971 /* If that scope does not contain the scope in which the
15972 class was originally declared, the program is invalid. */
15973 else if (prev_scope && !is_ancestor (prev_scope,
15974 nested_name_specifier))
15976 if (at_namespace_scope_p ())
15977 error_at (type_start_token->location,
15978 "declaration of %qD in namespace %qD which does not "
15979 "enclose %qD",
15980 type, prev_scope, nested_name_specifier);
15981 else
15982 error_at (type_start_token->location,
15983 "declaration of %qD in %qD which does not "
15984 "enclose %qD",
15985 type, prev_scope, nested_name_specifier);
15986 type = error_mark_node;
15990 if (scoped_enum_p)
15991 begin_scope (sk_scoped_enum, type);
15993 /* Consume the opening brace. */
15994 cp_lexer_consume_token (parser->lexer);
15996 if (type == error_mark_node)
15997 ; /* Nothing to add */
15998 else if (OPAQUE_ENUM_P (type)
15999 || (cxx_dialect > cxx98 && processing_specialization))
16001 new_value_list = true;
16002 SET_OPAQUE_ENUM_P (type, false);
16003 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16005 else
16007 error_at (type_start_token->location,
16008 "multiple definition of %q#T", type);
16009 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16010 "previous definition here");
16011 type = error_mark_node;
16014 if (type == error_mark_node)
16015 cp_parser_skip_to_end_of_block_or_statement (parser);
16016 /* If the next token is not '}', then there are some enumerators. */
16017 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16019 if (is_anonymous && !scoped_enum_p)
16020 pedwarn (type_start_token->location, OPT_Wpedantic,
16021 "ISO C++ forbids empty anonymous enum");
16023 else
16024 cp_parser_enumerator_list (parser, type);
16026 /* Consume the final '}'. */
16027 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16029 if (scoped_enum_p)
16030 finish_scope ();
16031 timevar_pop (TV_PARSE_ENUM);
16033 else
16035 /* If a ';' follows, then it is an opaque-enum-specifier
16036 and additional restrictions apply. */
16037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16039 if (is_anonymous)
16040 error_at (type_start_token->location,
16041 "opaque-enum-specifier without name");
16042 else if (nested_name_specifier)
16043 error_at (type_start_token->location,
16044 "opaque-enum-specifier must use a simple identifier");
16048 /* Look for trailing attributes to apply to this enumeration, and
16049 apply them if appropriate. */
16050 if (cp_parser_allow_gnu_extensions_p (parser))
16052 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16053 trailing_attr = chainon (trailing_attr, attributes);
16054 cplus_decl_attributes (&type,
16055 trailing_attr,
16056 (int) ATTR_FLAG_TYPE_IN_PLACE);
16059 /* Finish up the enumeration. */
16060 if (type != error_mark_node)
16062 if (new_value_list)
16063 finish_enum_value_list (type);
16064 if (is_new_type)
16065 finish_enum (type);
16068 if (nested_name_specifier)
16070 if (CLASS_TYPE_P (nested_name_specifier))
16072 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16073 pop_scope (nested_name_specifier);
16075 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16077 pop_nested_namespace (nested_name_specifier);
16080 out:
16081 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16082 return type;
16085 /* Parse an enumerator-list. The enumerators all have the indicated
16086 TYPE.
16088 enumerator-list:
16089 enumerator-definition
16090 enumerator-list , enumerator-definition */
16092 static void
16093 cp_parser_enumerator_list (cp_parser* parser, tree type)
16095 while (true)
16097 /* Parse an enumerator-definition. */
16098 cp_parser_enumerator_definition (parser, type);
16100 /* If the next token is not a ',', we've reached the end of
16101 the list. */
16102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16103 break;
16104 /* Otherwise, consume the `,' and keep going. */
16105 cp_lexer_consume_token (parser->lexer);
16106 /* If the next token is a `}', there is a trailing comma. */
16107 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16109 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16110 pedwarn (input_location, OPT_Wpedantic,
16111 "comma at end of enumerator list");
16112 break;
16117 /* Parse an enumerator-definition. The enumerator has the indicated
16118 TYPE.
16120 enumerator-definition:
16121 enumerator
16122 enumerator = constant-expression
16124 enumerator:
16125 identifier
16127 GNU Extensions:
16129 enumerator-definition:
16130 enumerator attributes [opt]
16131 enumerator attributes [opt] = constant-expression */
16133 static void
16134 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16136 tree identifier;
16137 tree value;
16138 location_t loc;
16140 /* Save the input location because we are interested in the location
16141 of the identifier and not the location of the explicit value. */
16142 loc = cp_lexer_peek_token (parser->lexer)->location;
16144 /* Look for the identifier. */
16145 identifier = cp_parser_identifier (parser);
16146 if (identifier == error_mark_node)
16147 return;
16149 /* Parse any specified attributes. */
16150 tree attrs = cp_parser_attributes_opt (parser);
16152 /* If the next token is an '=', then there is an explicit value. */
16153 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16155 /* Consume the `=' token. */
16156 cp_lexer_consume_token (parser->lexer);
16157 /* Parse the value. */
16158 value = cp_parser_constant_expression (parser);
16160 else
16161 value = NULL_TREE;
16163 /* If we are processing a template, make sure the initializer of the
16164 enumerator doesn't contain any bare template parameter pack. */
16165 if (check_for_bare_parameter_packs (value))
16166 value = error_mark_node;
16168 /* Create the enumerator. */
16169 build_enumerator (identifier, value, type, attrs, loc);
16172 /* Parse a namespace-name.
16174 namespace-name:
16175 original-namespace-name
16176 namespace-alias
16178 Returns the NAMESPACE_DECL for the namespace. */
16180 static tree
16181 cp_parser_namespace_name (cp_parser* parser)
16183 tree identifier;
16184 tree namespace_decl;
16186 cp_token *token = cp_lexer_peek_token (parser->lexer);
16188 /* Get the name of the namespace. */
16189 identifier = cp_parser_identifier (parser);
16190 if (identifier == error_mark_node)
16191 return error_mark_node;
16193 /* Look up the identifier in the currently active scope. Look only
16194 for namespaces, due to:
16196 [basic.lookup.udir]
16198 When looking up a namespace-name in a using-directive or alias
16199 definition, only namespace names are considered.
16201 And:
16203 [basic.lookup.qual]
16205 During the lookup of a name preceding the :: scope resolution
16206 operator, object, function, and enumerator names are ignored.
16208 (Note that cp_parser_qualifying_entity only calls this
16209 function if the token after the name is the scope resolution
16210 operator.) */
16211 namespace_decl = cp_parser_lookup_name (parser, identifier,
16212 none_type,
16213 /*is_template=*/false,
16214 /*is_namespace=*/true,
16215 /*check_dependency=*/true,
16216 /*ambiguous_decls=*/NULL,
16217 token->location);
16218 /* If it's not a namespace, issue an error. */
16219 if (namespace_decl == error_mark_node
16220 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16222 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16223 error_at (token->location, "%qD is not a namespace-name", identifier);
16224 cp_parser_error (parser, "expected namespace-name");
16225 namespace_decl = error_mark_node;
16228 return namespace_decl;
16231 /* Parse a namespace-definition.
16233 namespace-definition:
16234 named-namespace-definition
16235 unnamed-namespace-definition
16237 named-namespace-definition:
16238 original-namespace-definition
16239 extension-namespace-definition
16241 original-namespace-definition:
16242 namespace identifier { namespace-body }
16244 extension-namespace-definition:
16245 namespace original-namespace-name { namespace-body }
16247 unnamed-namespace-definition:
16248 namespace { namespace-body } */
16250 static void
16251 cp_parser_namespace_definition (cp_parser* parser)
16253 tree identifier, attribs;
16254 bool has_visibility;
16255 bool is_inline;
16257 cp_ensure_no_omp_declare_simd (parser);
16258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16260 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16261 is_inline = true;
16262 cp_lexer_consume_token (parser->lexer);
16264 else
16265 is_inline = false;
16267 /* Look for the `namespace' keyword. */
16268 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16270 /* Get the name of the namespace. We do not attempt to distinguish
16271 between an original-namespace-definition and an
16272 extension-namespace-definition at this point. The semantic
16273 analysis routines are responsible for that. */
16274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16275 identifier = cp_parser_identifier (parser);
16276 else
16277 identifier = NULL_TREE;
16279 /* Parse any specified attributes. */
16280 attribs = cp_parser_attributes_opt (parser);
16282 /* Look for the `{' to start the namespace. */
16283 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16284 /* Start the namespace. */
16285 push_namespace (identifier);
16287 /* "inline namespace" is equivalent to a stub namespace definition
16288 followed by a strong using directive. */
16289 if (is_inline)
16291 tree name_space = current_namespace;
16292 /* Set up namespace association. */
16293 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16294 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16295 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16296 /* Import the contents of the inline namespace. */
16297 pop_namespace ();
16298 do_using_directive (name_space);
16299 push_namespace (identifier);
16302 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16304 /* Parse the body of the namespace. */
16305 cp_parser_namespace_body (parser);
16307 if (has_visibility)
16308 pop_visibility (1);
16310 /* Finish the namespace. */
16311 pop_namespace ();
16312 /* Look for the final `}'. */
16313 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16316 /* Parse a namespace-body.
16318 namespace-body:
16319 declaration-seq [opt] */
16321 static void
16322 cp_parser_namespace_body (cp_parser* parser)
16324 cp_parser_declaration_seq_opt (parser);
16327 /* Parse a namespace-alias-definition.
16329 namespace-alias-definition:
16330 namespace identifier = qualified-namespace-specifier ; */
16332 static void
16333 cp_parser_namespace_alias_definition (cp_parser* parser)
16335 tree identifier;
16336 tree namespace_specifier;
16338 cp_token *token = cp_lexer_peek_token (parser->lexer);
16340 /* Look for the `namespace' keyword. */
16341 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16342 /* Look for the identifier. */
16343 identifier = cp_parser_identifier (parser);
16344 if (identifier == error_mark_node)
16345 return;
16346 /* Look for the `=' token. */
16347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16348 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16350 error_at (token->location, "%<namespace%> definition is not allowed here");
16351 /* Skip the definition. */
16352 cp_lexer_consume_token (parser->lexer);
16353 if (cp_parser_skip_to_closing_brace (parser))
16354 cp_lexer_consume_token (parser->lexer);
16355 return;
16357 cp_parser_require (parser, CPP_EQ, RT_EQ);
16358 /* Look for the qualified-namespace-specifier. */
16359 namespace_specifier
16360 = cp_parser_qualified_namespace_specifier (parser);
16361 /* Look for the `;' token. */
16362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16364 /* Register the alias in the symbol table. */
16365 do_namespace_alias (identifier, namespace_specifier);
16368 /* Parse a qualified-namespace-specifier.
16370 qualified-namespace-specifier:
16371 :: [opt] nested-name-specifier [opt] namespace-name
16373 Returns a NAMESPACE_DECL corresponding to the specified
16374 namespace. */
16376 static tree
16377 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16379 /* Look for the optional `::'. */
16380 cp_parser_global_scope_opt (parser,
16381 /*current_scope_valid_p=*/false);
16383 /* Look for the optional nested-name-specifier. */
16384 cp_parser_nested_name_specifier_opt (parser,
16385 /*typename_keyword_p=*/false,
16386 /*check_dependency_p=*/true,
16387 /*type_p=*/false,
16388 /*is_declaration=*/true);
16390 return cp_parser_namespace_name (parser);
16393 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16394 access declaration.
16396 using-declaration:
16397 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16398 using :: unqualified-id ;
16400 access-declaration:
16401 qualified-id ;
16405 static bool
16406 cp_parser_using_declaration (cp_parser* parser,
16407 bool access_declaration_p)
16409 cp_token *token;
16410 bool typename_p = false;
16411 bool global_scope_p;
16412 tree decl;
16413 tree identifier;
16414 tree qscope;
16415 int oldcount = errorcount;
16416 cp_token *diag_token = NULL;
16418 if (access_declaration_p)
16420 diag_token = cp_lexer_peek_token (parser->lexer);
16421 cp_parser_parse_tentatively (parser);
16423 else
16425 /* Look for the `using' keyword. */
16426 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16428 /* Peek at the next token. */
16429 token = cp_lexer_peek_token (parser->lexer);
16430 /* See if it's `typename'. */
16431 if (token->keyword == RID_TYPENAME)
16433 /* Remember that we've seen it. */
16434 typename_p = true;
16435 /* Consume the `typename' token. */
16436 cp_lexer_consume_token (parser->lexer);
16440 /* Look for the optional global scope qualification. */
16441 global_scope_p
16442 = (cp_parser_global_scope_opt (parser,
16443 /*current_scope_valid_p=*/false)
16444 != NULL_TREE);
16446 /* If we saw `typename', or didn't see `::', then there must be a
16447 nested-name-specifier present. */
16448 if (typename_p || !global_scope_p)
16450 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16451 /*check_dependency_p=*/true,
16452 /*type_p=*/false,
16453 /*is_declaration=*/true);
16454 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16456 cp_parser_skip_to_end_of_block_or_statement (parser);
16457 return false;
16460 /* Otherwise, we could be in either of the two productions. In that
16461 case, treat the nested-name-specifier as optional. */
16462 else
16463 qscope = cp_parser_nested_name_specifier_opt (parser,
16464 /*typename_keyword_p=*/false,
16465 /*check_dependency_p=*/true,
16466 /*type_p=*/false,
16467 /*is_declaration=*/true);
16468 if (!qscope)
16469 qscope = global_namespace;
16470 else if (UNSCOPED_ENUM_P (qscope))
16471 qscope = CP_TYPE_CONTEXT (qscope);
16473 if (access_declaration_p && cp_parser_error_occurred (parser))
16474 /* Something has already gone wrong; there's no need to parse
16475 further. Since an error has occurred, the return value of
16476 cp_parser_parse_definitely will be false, as required. */
16477 return cp_parser_parse_definitely (parser);
16479 token = cp_lexer_peek_token (parser->lexer);
16480 /* Parse the unqualified-id. */
16481 identifier = cp_parser_unqualified_id (parser,
16482 /*template_keyword_p=*/false,
16483 /*check_dependency_p=*/true,
16484 /*declarator_p=*/true,
16485 /*optional_p=*/false);
16487 if (access_declaration_p)
16489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16490 cp_parser_simulate_error (parser);
16491 if (!cp_parser_parse_definitely (parser))
16492 return false;
16495 /* The function we call to handle a using-declaration is different
16496 depending on what scope we are in. */
16497 if (qscope == error_mark_node || identifier == error_mark_node)
16499 else if (!identifier_p (identifier)
16500 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16501 /* [namespace.udecl]
16503 A using declaration shall not name a template-id. */
16504 error_at (token->location,
16505 "a template-id may not appear in a using-declaration");
16506 else
16508 if (at_class_scope_p ())
16510 /* Create the USING_DECL. */
16511 decl = do_class_using_decl (parser->scope, identifier);
16513 if (decl && typename_p)
16514 USING_DECL_TYPENAME_P (decl) = 1;
16516 if (check_for_bare_parameter_packs (decl))
16518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16519 return false;
16521 else
16522 /* Add it to the list of members in this class. */
16523 finish_member_declaration (decl);
16525 else
16527 decl = cp_parser_lookup_name_simple (parser,
16528 identifier,
16529 token->location);
16530 if (decl == error_mark_node)
16531 cp_parser_name_lookup_error (parser, identifier,
16532 decl, NLE_NULL,
16533 token->location);
16534 else if (check_for_bare_parameter_packs (decl))
16536 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16537 return false;
16539 else if (!at_namespace_scope_p ())
16540 do_local_using_decl (decl, qscope, identifier);
16541 else
16542 do_toplevel_using_decl (decl, qscope, identifier);
16546 /* Look for the final `;'. */
16547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16549 if (access_declaration_p && errorcount == oldcount)
16550 warning_at (diag_token->location, OPT_Wdeprecated,
16551 "access declarations are deprecated "
16552 "in favour of using-declarations; "
16553 "suggestion: add the %<using%> keyword");
16555 return true;
16558 /* Parse an alias-declaration.
16560 alias-declaration:
16561 using identifier attribute-specifier-seq [opt] = type-id */
16563 static tree
16564 cp_parser_alias_declaration (cp_parser* parser)
16566 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16567 location_t id_location;
16568 cp_declarator *declarator;
16569 cp_decl_specifier_seq decl_specs;
16570 bool member_p;
16571 const char *saved_message = NULL;
16573 /* Look for the `using' keyword. */
16574 cp_token *using_token
16575 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16576 if (using_token == NULL)
16577 return error_mark_node;
16579 id_location = cp_lexer_peek_token (parser->lexer)->location;
16580 id = cp_parser_identifier (parser);
16581 if (id == error_mark_node)
16582 return error_mark_node;
16584 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16585 attributes = cp_parser_attributes_opt (parser);
16586 if (attributes == error_mark_node)
16587 return error_mark_node;
16589 cp_parser_require (parser, CPP_EQ, RT_EQ);
16591 if (cp_parser_error_occurred (parser))
16592 return error_mark_node;
16594 cp_parser_commit_to_tentative_parse (parser);
16596 /* Now we are going to parse the type-id of the declaration. */
16599 [dcl.type]/3 says:
16601 "A type-specifier-seq shall not define a class or enumeration
16602 unless it appears in the type-id of an alias-declaration (7.1.3) that
16603 is not the declaration of a template-declaration."
16605 In other words, if we currently are in an alias template, the
16606 type-id should not define a type.
16608 So let's set parser->type_definition_forbidden_message in that
16609 case; cp_parser_check_type_definition (called by
16610 cp_parser_class_specifier) will then emit an error if a type is
16611 defined in the type-id. */
16612 if (parser->num_template_parameter_lists)
16614 saved_message = parser->type_definition_forbidden_message;
16615 parser->type_definition_forbidden_message =
16616 G_("types may not be defined in alias template declarations");
16619 type = cp_parser_type_id (parser);
16621 /* Restore the error message if need be. */
16622 if (parser->num_template_parameter_lists)
16623 parser->type_definition_forbidden_message = saved_message;
16625 if (type == error_mark_node
16626 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16628 cp_parser_skip_to_end_of_block_or_statement (parser);
16629 return error_mark_node;
16632 /* A typedef-name can also be introduced by an alias-declaration. The
16633 identifier following the using keyword becomes a typedef-name. It has
16634 the same semantics as if it were introduced by the typedef
16635 specifier. In particular, it does not define a new type and it shall
16636 not appear in the type-id. */
16638 clear_decl_specs (&decl_specs);
16639 decl_specs.type = type;
16640 if (attributes != NULL_TREE)
16642 decl_specs.attributes = attributes;
16643 set_and_check_decl_spec_loc (&decl_specs,
16644 ds_attribute,
16645 attrs_token);
16647 set_and_check_decl_spec_loc (&decl_specs,
16648 ds_typedef,
16649 using_token);
16650 set_and_check_decl_spec_loc (&decl_specs,
16651 ds_alias,
16652 using_token);
16654 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16655 declarator->id_loc = id_location;
16657 member_p = at_class_scope_p ();
16658 if (member_p)
16659 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16660 NULL_TREE, attributes);
16661 else
16662 decl = start_decl (declarator, &decl_specs, 0,
16663 attributes, NULL_TREE, &pushed_scope);
16664 if (decl == error_mark_node)
16665 return decl;
16667 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16669 if (pushed_scope)
16670 pop_scope (pushed_scope);
16672 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16673 added into the symbol table; otherwise, return the TYPE_DECL. */
16674 if (DECL_LANG_SPECIFIC (decl)
16675 && DECL_TEMPLATE_INFO (decl)
16676 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16678 decl = DECL_TI_TEMPLATE (decl);
16679 if (member_p)
16680 check_member_template (decl);
16683 return decl;
16686 /* Parse a using-directive.
16688 using-directive:
16689 using namespace :: [opt] nested-name-specifier [opt]
16690 namespace-name ; */
16692 static void
16693 cp_parser_using_directive (cp_parser* parser)
16695 tree namespace_decl;
16696 tree attribs;
16698 /* Look for the `using' keyword. */
16699 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16700 /* And the `namespace' keyword. */
16701 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16702 /* Look for the optional `::' operator. */
16703 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16704 /* And the optional nested-name-specifier. */
16705 cp_parser_nested_name_specifier_opt (parser,
16706 /*typename_keyword_p=*/false,
16707 /*check_dependency_p=*/true,
16708 /*type_p=*/false,
16709 /*is_declaration=*/true);
16710 /* Get the namespace being used. */
16711 namespace_decl = cp_parser_namespace_name (parser);
16712 /* And any specified attributes. */
16713 attribs = cp_parser_attributes_opt (parser);
16714 /* Update the symbol table. */
16715 parse_using_directive (namespace_decl, attribs);
16716 /* Look for the final `;'. */
16717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16720 /* Parse an asm-definition.
16722 asm-definition:
16723 asm ( string-literal ) ;
16725 GNU Extension:
16727 asm-definition:
16728 asm volatile [opt] ( string-literal ) ;
16729 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16730 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16731 : asm-operand-list [opt] ) ;
16732 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16733 : asm-operand-list [opt]
16734 : asm-clobber-list [opt] ) ;
16735 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16736 : asm-clobber-list [opt]
16737 : asm-goto-list ) ; */
16739 static void
16740 cp_parser_asm_definition (cp_parser* parser)
16742 tree string;
16743 tree outputs = NULL_TREE;
16744 tree inputs = NULL_TREE;
16745 tree clobbers = NULL_TREE;
16746 tree labels = NULL_TREE;
16747 tree asm_stmt;
16748 bool volatile_p = false;
16749 bool extended_p = false;
16750 bool invalid_inputs_p = false;
16751 bool invalid_outputs_p = false;
16752 bool goto_p = false;
16753 required_token missing = RT_NONE;
16755 /* Look for the `asm' keyword. */
16756 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16758 if (parser->in_function_body
16759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16761 error ("%<asm%> in %<constexpr%> function");
16762 cp_function_chain->invalid_constexpr = true;
16765 /* See if the next token is `volatile'. */
16766 if (cp_parser_allow_gnu_extensions_p (parser)
16767 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16769 /* Remember that we saw the `volatile' keyword. */
16770 volatile_p = true;
16771 /* Consume the token. */
16772 cp_lexer_consume_token (parser->lexer);
16774 if (cp_parser_allow_gnu_extensions_p (parser)
16775 && parser->in_function_body
16776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16778 /* Remember that we saw the `goto' keyword. */
16779 goto_p = true;
16780 /* Consume the token. */
16781 cp_lexer_consume_token (parser->lexer);
16783 /* Look for the opening `('. */
16784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16785 return;
16786 /* Look for the string. */
16787 string = cp_parser_string_literal (parser, false, false);
16788 if (string == error_mark_node)
16790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16791 /*consume_paren=*/true);
16792 return;
16795 /* If we're allowing GNU extensions, check for the extended assembly
16796 syntax. Unfortunately, the `:' tokens need not be separated by
16797 a space in C, and so, for compatibility, we tolerate that here
16798 too. Doing that means that we have to treat the `::' operator as
16799 two `:' tokens. */
16800 if (cp_parser_allow_gnu_extensions_p (parser)
16801 && parser->in_function_body
16802 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16803 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16805 bool inputs_p = false;
16806 bool clobbers_p = false;
16807 bool labels_p = false;
16809 /* The extended syntax was used. */
16810 extended_p = true;
16812 /* Look for outputs. */
16813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16815 /* Consume the `:'. */
16816 cp_lexer_consume_token (parser->lexer);
16817 /* Parse the output-operands. */
16818 if (cp_lexer_next_token_is_not (parser->lexer,
16819 CPP_COLON)
16820 && cp_lexer_next_token_is_not (parser->lexer,
16821 CPP_SCOPE)
16822 && cp_lexer_next_token_is_not (parser->lexer,
16823 CPP_CLOSE_PAREN)
16824 && !goto_p)
16826 outputs = cp_parser_asm_operand_list (parser);
16827 if (outputs == error_mark_node)
16828 invalid_outputs_p = true;
16831 /* If the next token is `::', there are no outputs, and the
16832 next token is the beginning of the inputs. */
16833 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16834 /* The inputs are coming next. */
16835 inputs_p = true;
16837 /* Look for inputs. */
16838 if (inputs_p
16839 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16841 /* Consume the `:' or `::'. */
16842 cp_lexer_consume_token (parser->lexer);
16843 /* Parse the output-operands. */
16844 if (cp_lexer_next_token_is_not (parser->lexer,
16845 CPP_COLON)
16846 && cp_lexer_next_token_is_not (parser->lexer,
16847 CPP_SCOPE)
16848 && cp_lexer_next_token_is_not (parser->lexer,
16849 CPP_CLOSE_PAREN))
16851 inputs = cp_parser_asm_operand_list (parser);
16852 if (inputs == error_mark_node)
16853 invalid_inputs_p = true;
16856 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16857 /* The clobbers are coming next. */
16858 clobbers_p = true;
16860 /* Look for clobbers. */
16861 if (clobbers_p
16862 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16864 clobbers_p = true;
16865 /* Consume the `:' or `::'. */
16866 cp_lexer_consume_token (parser->lexer);
16867 /* Parse the clobbers. */
16868 if (cp_lexer_next_token_is_not (parser->lexer,
16869 CPP_COLON)
16870 && cp_lexer_next_token_is_not (parser->lexer,
16871 CPP_CLOSE_PAREN))
16872 clobbers = cp_parser_asm_clobber_list (parser);
16874 else if (goto_p
16875 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16876 /* The labels are coming next. */
16877 labels_p = true;
16879 /* Look for labels. */
16880 if (labels_p
16881 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16883 labels_p = true;
16884 /* Consume the `:' or `::'. */
16885 cp_lexer_consume_token (parser->lexer);
16886 /* Parse the labels. */
16887 labels = cp_parser_asm_label_list (parser);
16890 if (goto_p && !labels_p)
16891 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16893 else if (goto_p)
16894 missing = RT_COLON_SCOPE;
16896 /* Look for the closing `)'. */
16897 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16898 missing ? missing : RT_CLOSE_PAREN))
16899 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16900 /*consume_paren=*/true);
16901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16903 if (!invalid_inputs_p && !invalid_outputs_p)
16905 /* Create the ASM_EXPR. */
16906 if (parser->in_function_body)
16908 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16909 inputs, clobbers, labels);
16910 /* If the extended syntax was not used, mark the ASM_EXPR. */
16911 if (!extended_p)
16913 tree temp = asm_stmt;
16914 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16915 temp = TREE_OPERAND (temp, 0);
16917 ASM_INPUT_P (temp) = 1;
16920 else
16921 symtab->finalize_toplevel_asm (string);
16925 /* Declarators [gram.dcl.decl] */
16927 /* Parse an init-declarator.
16929 init-declarator:
16930 declarator initializer [opt]
16932 GNU Extension:
16934 init-declarator:
16935 declarator asm-specification [opt] attributes [opt] initializer [opt]
16937 function-definition:
16938 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16939 function-body
16940 decl-specifier-seq [opt] declarator function-try-block
16942 GNU Extension:
16944 function-definition:
16945 __extension__ function-definition
16947 TM Extension:
16949 function-definition:
16950 decl-specifier-seq [opt] declarator function-transaction-block
16952 The DECL_SPECIFIERS apply to this declarator. Returns a
16953 representation of the entity declared. If MEMBER_P is TRUE, then
16954 this declarator appears in a class scope. The new DECL created by
16955 this declarator is returned.
16957 The CHECKS are access checks that should be performed once we know
16958 what entity is being declared (and, therefore, what classes have
16959 befriended it).
16961 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16962 for a function-definition here as well. If the declarator is a
16963 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16964 be TRUE upon return. By that point, the function-definition will
16965 have been completely parsed.
16967 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16968 is FALSE.
16970 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16971 parsed declaration if it is an uninitialized single declarator not followed
16972 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16973 if present, will not be consumed. If returned, this declarator will be
16974 created with SD_INITIALIZED but will not call cp_finish_decl.
16976 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16977 and there is an initializer, the pointed location_t is set to the
16978 location of the '=' or `(', or '{' in C++11 token introducing the
16979 initializer. */
16981 static tree
16982 cp_parser_init_declarator (cp_parser* parser,
16983 cp_decl_specifier_seq *decl_specifiers,
16984 vec<deferred_access_check, va_gc> *checks,
16985 bool function_definition_allowed_p,
16986 bool member_p,
16987 int declares_class_or_enum,
16988 bool* function_definition_p,
16989 tree* maybe_range_for_decl,
16990 location_t* init_loc)
16992 cp_token *token = NULL, *asm_spec_start_token = NULL,
16993 *attributes_start_token = NULL;
16994 cp_declarator *declarator;
16995 tree prefix_attributes;
16996 tree attributes = NULL;
16997 tree asm_specification;
16998 tree initializer;
16999 tree decl = NULL_TREE;
17000 tree scope;
17001 int is_initialized;
17002 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17003 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17004 "(...)". */
17005 enum cpp_ttype initialization_kind;
17006 bool is_direct_init = false;
17007 bool is_non_constant_init;
17008 int ctor_dtor_or_conv_p;
17009 bool friend_p = cp_parser_friend_p (decl_specifiers);
17010 tree pushed_scope = NULL_TREE;
17011 bool range_for_decl_p = false;
17012 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17013 location_t tmp_init_loc = UNKNOWN_LOCATION;
17015 /* Gather the attributes that were provided with the
17016 decl-specifiers. */
17017 prefix_attributes = decl_specifiers->attributes;
17019 /* Assume that this is not the declarator for a function
17020 definition. */
17021 if (function_definition_p)
17022 *function_definition_p = false;
17024 /* Default arguments are only permitted for function parameters. */
17025 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17026 parser->default_arg_ok_p = false;
17028 /* Defer access checks while parsing the declarator; we cannot know
17029 what names are accessible until we know what is being
17030 declared. */
17031 resume_deferring_access_checks ();
17033 /* Parse the declarator. */
17034 token = cp_lexer_peek_token (parser->lexer);
17035 declarator
17036 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17037 &ctor_dtor_or_conv_p,
17038 /*parenthesized_p=*/NULL,
17039 member_p, friend_p);
17040 /* Gather up the deferred checks. */
17041 stop_deferring_access_checks ();
17043 parser->default_arg_ok_p = saved_default_arg_ok_p;
17045 /* If the DECLARATOR was erroneous, there's no need to go
17046 further. */
17047 if (declarator == cp_error_declarator)
17048 return error_mark_node;
17050 /* Check that the number of template-parameter-lists is OK. */
17051 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17052 token->location))
17053 return error_mark_node;
17055 if (declares_class_or_enum & 2)
17056 cp_parser_check_for_definition_in_return_type (declarator,
17057 decl_specifiers->type,
17058 decl_specifiers->locations[ds_type_spec]);
17060 /* Figure out what scope the entity declared by the DECLARATOR is
17061 located in. `grokdeclarator' sometimes changes the scope, so
17062 we compute it now. */
17063 scope = get_scope_of_declarator (declarator);
17065 /* Perform any lookups in the declared type which were thought to be
17066 dependent, but are not in the scope of the declarator. */
17067 decl_specifiers->type
17068 = maybe_update_decl_type (decl_specifiers->type, scope);
17070 /* If we're allowing GNU extensions, look for an
17071 asm-specification. */
17072 if (cp_parser_allow_gnu_extensions_p (parser))
17074 /* Look for an asm-specification. */
17075 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17076 asm_specification = cp_parser_asm_specification_opt (parser);
17078 else
17079 asm_specification = NULL_TREE;
17081 /* Look for attributes. */
17082 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17083 attributes = cp_parser_attributes_opt (parser);
17085 /* Peek at the next token. */
17086 token = cp_lexer_peek_token (parser->lexer);
17088 bool bogus_implicit_tmpl = false;
17090 if (function_declarator_p (declarator))
17092 /* Check to see if the token indicates the start of a
17093 function-definition. */
17094 if (cp_parser_token_starts_function_definition_p (token))
17096 if (!function_definition_allowed_p)
17098 /* If a function-definition should not appear here, issue an
17099 error message. */
17100 cp_parser_error (parser,
17101 "a function-definition is not allowed here");
17102 return error_mark_node;
17105 location_t func_brace_location
17106 = cp_lexer_peek_token (parser->lexer)->location;
17108 /* Neither attributes nor an asm-specification are allowed
17109 on a function-definition. */
17110 if (asm_specification)
17111 error_at (asm_spec_start_token->location,
17112 "an asm-specification is not allowed "
17113 "on a function-definition");
17114 if (attributes)
17115 error_at (attributes_start_token->location,
17116 "attributes are not allowed "
17117 "on a function-definition");
17118 /* This is a function-definition. */
17119 *function_definition_p = true;
17121 /* Parse the function definition. */
17122 if (member_p)
17123 decl = cp_parser_save_member_function_body (parser,
17124 decl_specifiers,
17125 declarator,
17126 prefix_attributes);
17127 else
17128 decl =
17129 (cp_parser_function_definition_from_specifiers_and_declarator
17130 (parser, decl_specifiers, prefix_attributes, declarator));
17132 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17134 /* This is where the prologue starts... */
17135 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17136 = func_brace_location;
17139 return decl;
17142 else if (parser->fully_implicit_function_template_p)
17144 /* A non-template declaration involving a function parameter list
17145 containing an implicit template parameter will be made into a
17146 template. If the resulting declaration is not going to be an
17147 actual function then finish the template scope here to prevent it.
17148 An error message will be issued once we have a decl to talk about.
17150 FIXME probably we should do type deduction rather than create an
17151 implicit template, but the standard currently doesn't allow it. */
17152 bogus_implicit_tmpl = true;
17153 finish_fully_implicit_template (parser, NULL_TREE);
17156 /* [dcl.dcl]
17158 Only in function declarations for constructors, destructors, and
17159 type conversions can the decl-specifier-seq be omitted.
17161 We explicitly postpone this check past the point where we handle
17162 function-definitions because we tolerate function-definitions
17163 that are missing their return types in some modes. */
17164 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17166 cp_parser_error (parser,
17167 "expected constructor, destructor, or type conversion");
17168 return error_mark_node;
17171 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17172 if (token->type == CPP_EQ
17173 || token->type == CPP_OPEN_PAREN
17174 || token->type == CPP_OPEN_BRACE)
17176 is_initialized = SD_INITIALIZED;
17177 initialization_kind = token->type;
17178 if (maybe_range_for_decl)
17179 *maybe_range_for_decl = error_mark_node;
17180 tmp_init_loc = token->location;
17181 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17182 *init_loc = tmp_init_loc;
17184 if (token->type == CPP_EQ
17185 && function_declarator_p (declarator))
17187 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17188 if (t2->keyword == RID_DEFAULT)
17189 is_initialized = SD_DEFAULTED;
17190 else if (t2->keyword == RID_DELETE)
17191 is_initialized = SD_DELETED;
17194 else
17196 /* If the init-declarator isn't initialized and isn't followed by a
17197 `,' or `;', it's not a valid init-declarator. */
17198 if (token->type != CPP_COMMA
17199 && token->type != CPP_SEMICOLON)
17201 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17202 range_for_decl_p = true;
17203 else
17205 if (!maybe_range_for_decl)
17206 cp_parser_error (parser, "expected initializer");
17207 return error_mark_node;
17210 is_initialized = SD_UNINITIALIZED;
17211 initialization_kind = CPP_EOF;
17214 /* Because start_decl has side-effects, we should only call it if we
17215 know we're going ahead. By this point, we know that we cannot
17216 possibly be looking at any other construct. */
17217 cp_parser_commit_to_tentative_parse (parser);
17219 /* Enter the newly declared entry in the symbol table. If we're
17220 processing a declaration in a class-specifier, we wait until
17221 after processing the initializer. */
17222 if (!member_p)
17224 if (parser->in_unbraced_linkage_specification_p)
17225 decl_specifiers->storage_class = sc_extern;
17226 decl = start_decl (declarator, decl_specifiers,
17227 range_for_decl_p? SD_INITIALIZED : is_initialized,
17228 attributes, prefix_attributes, &pushed_scope);
17229 cp_finalize_omp_declare_simd (parser, decl);
17230 /* Adjust location of decl if declarator->id_loc is more appropriate:
17231 set, and decl wasn't merged with another decl, in which case its
17232 location would be different from input_location, and more accurate. */
17233 if (DECL_P (decl)
17234 && declarator->id_loc != UNKNOWN_LOCATION
17235 && DECL_SOURCE_LOCATION (decl) == input_location)
17236 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17238 else if (scope)
17239 /* Enter the SCOPE. That way unqualified names appearing in the
17240 initializer will be looked up in SCOPE. */
17241 pushed_scope = push_scope (scope);
17243 /* Perform deferred access control checks, now that we know in which
17244 SCOPE the declared entity resides. */
17245 if (!member_p && decl)
17247 tree saved_current_function_decl = NULL_TREE;
17249 /* If the entity being declared is a function, pretend that we
17250 are in its scope. If it is a `friend', it may have access to
17251 things that would not otherwise be accessible. */
17252 if (TREE_CODE (decl) == FUNCTION_DECL)
17254 saved_current_function_decl = current_function_decl;
17255 current_function_decl = decl;
17258 /* Perform access checks for template parameters. */
17259 cp_parser_perform_template_parameter_access_checks (checks);
17261 /* Perform the access control checks for the declarator and the
17262 decl-specifiers. */
17263 perform_deferred_access_checks (tf_warning_or_error);
17265 /* Restore the saved value. */
17266 if (TREE_CODE (decl) == FUNCTION_DECL)
17267 current_function_decl = saved_current_function_decl;
17270 /* Parse the initializer. */
17271 initializer = NULL_TREE;
17272 is_direct_init = false;
17273 is_non_constant_init = true;
17274 if (is_initialized)
17276 if (function_declarator_p (declarator))
17278 if (initialization_kind == CPP_EQ)
17279 initializer = cp_parser_pure_specifier (parser);
17280 else
17282 /* If the declaration was erroneous, we don't really
17283 know what the user intended, so just silently
17284 consume the initializer. */
17285 if (decl != error_mark_node)
17286 error_at (tmp_init_loc, "initializer provided for function");
17287 cp_parser_skip_to_closing_parenthesis (parser,
17288 /*recovering=*/true,
17289 /*or_comma=*/false,
17290 /*consume_paren=*/true);
17293 else
17295 /* We want to record the extra mangling scope for in-class
17296 initializers of class members and initializers of static data
17297 member templates. The former involves deferring
17298 parsing of the initializer until end of class as with default
17299 arguments. So right here we only handle the latter. */
17300 if (!member_p && processing_template_decl)
17301 start_lambda_scope (decl);
17302 initializer = cp_parser_initializer (parser,
17303 &is_direct_init,
17304 &is_non_constant_init);
17305 if (!member_p && processing_template_decl)
17306 finish_lambda_scope ();
17307 if (initializer == error_mark_node)
17308 cp_parser_skip_to_end_of_statement (parser);
17312 /* The old parser allows attributes to appear after a parenthesized
17313 initializer. Mark Mitchell proposed removing this functionality
17314 on the GCC mailing lists on 2002-08-13. This parser accepts the
17315 attributes -- but ignores them. */
17316 if (cp_parser_allow_gnu_extensions_p (parser)
17317 && initialization_kind == CPP_OPEN_PAREN)
17318 if (cp_parser_attributes_opt (parser))
17319 warning (OPT_Wattributes,
17320 "attributes after parenthesized initializer ignored");
17322 /* And now complain about a non-function implicit template. */
17323 if (bogus_implicit_tmpl)
17324 error_at (DECL_SOURCE_LOCATION (decl),
17325 "non-function %qD declared as implicit template", decl);
17327 /* For an in-class declaration, use `grokfield' to create the
17328 declaration. */
17329 if (member_p)
17331 if (pushed_scope)
17333 pop_scope (pushed_scope);
17334 pushed_scope = NULL_TREE;
17336 decl = grokfield (declarator, decl_specifiers,
17337 initializer, !is_non_constant_init,
17338 /*asmspec=*/NULL_TREE,
17339 chainon (attributes, prefix_attributes));
17340 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17341 cp_parser_save_default_args (parser, decl);
17342 cp_finalize_omp_declare_simd (parser, decl);
17345 /* Finish processing the declaration. But, skip member
17346 declarations. */
17347 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17349 cp_finish_decl (decl,
17350 initializer, !is_non_constant_init,
17351 asm_specification,
17352 /* If the initializer is in parentheses, then this is
17353 a direct-initialization, which means that an
17354 `explicit' constructor is OK. Otherwise, an
17355 `explicit' constructor cannot be used. */
17356 ((is_direct_init || !is_initialized)
17357 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17359 else if ((cxx_dialect != cxx98) && friend_p
17360 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17361 /* Core issue #226 (C++0x only): A default template-argument
17362 shall not be specified in a friend class template
17363 declaration. */
17364 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17365 /*is_partial=*/false, /*is_friend_decl=*/1);
17367 if (!friend_p && pushed_scope)
17368 pop_scope (pushed_scope);
17370 if (function_declarator_p (declarator)
17371 && parser->fully_implicit_function_template_p)
17373 if (member_p)
17374 decl = finish_fully_implicit_template (parser, decl);
17375 else
17376 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17379 return decl;
17382 /* Parse a declarator.
17384 declarator:
17385 direct-declarator
17386 ptr-operator declarator
17388 abstract-declarator:
17389 ptr-operator abstract-declarator [opt]
17390 direct-abstract-declarator
17392 GNU Extensions:
17394 declarator:
17395 attributes [opt] direct-declarator
17396 attributes [opt] ptr-operator declarator
17398 abstract-declarator:
17399 attributes [opt] ptr-operator abstract-declarator [opt]
17400 attributes [opt] direct-abstract-declarator
17402 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17403 detect constructor, destructor or conversion operators. It is set
17404 to -1 if the declarator is a name, and +1 if it is a
17405 function. Otherwise it is set to zero. Usually you just want to
17406 test for >0, but internally the negative value is used.
17408 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17409 a decl-specifier-seq unless it declares a constructor, destructor,
17410 or conversion. It might seem that we could check this condition in
17411 semantic analysis, rather than parsing, but that makes it difficult
17412 to handle something like `f()'. We want to notice that there are
17413 no decl-specifiers, and therefore realize that this is an
17414 expression, not a declaration.)
17416 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17417 the declarator is a direct-declarator of the form "(...)".
17419 MEMBER_P is true iff this declarator is a member-declarator.
17421 FRIEND_P is true iff this declarator is a friend. */
17423 static cp_declarator *
17424 cp_parser_declarator (cp_parser* parser,
17425 cp_parser_declarator_kind dcl_kind,
17426 int* ctor_dtor_or_conv_p,
17427 bool* parenthesized_p,
17428 bool member_p, bool friend_p)
17430 cp_declarator *declarator;
17431 enum tree_code code;
17432 cp_cv_quals cv_quals;
17433 tree class_type;
17434 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17436 /* Assume this is not a constructor, destructor, or type-conversion
17437 operator. */
17438 if (ctor_dtor_or_conv_p)
17439 *ctor_dtor_or_conv_p = 0;
17441 if (cp_parser_allow_gnu_extensions_p (parser))
17442 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17444 /* Check for the ptr-operator production. */
17445 cp_parser_parse_tentatively (parser);
17446 /* Parse the ptr-operator. */
17447 code = cp_parser_ptr_operator (parser,
17448 &class_type,
17449 &cv_quals,
17450 &std_attributes);
17452 /* If that worked, then we have a ptr-operator. */
17453 if (cp_parser_parse_definitely (parser))
17455 /* If a ptr-operator was found, then this declarator was not
17456 parenthesized. */
17457 if (parenthesized_p)
17458 *parenthesized_p = true;
17459 /* The dependent declarator is optional if we are parsing an
17460 abstract-declarator. */
17461 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17462 cp_parser_parse_tentatively (parser);
17464 /* Parse the dependent declarator. */
17465 declarator = cp_parser_declarator (parser, dcl_kind,
17466 /*ctor_dtor_or_conv_p=*/NULL,
17467 /*parenthesized_p=*/NULL,
17468 /*member_p=*/false,
17469 friend_p);
17471 /* If we are parsing an abstract-declarator, we must handle the
17472 case where the dependent declarator is absent. */
17473 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17474 && !cp_parser_parse_definitely (parser))
17475 declarator = NULL;
17477 declarator = cp_parser_make_indirect_declarator
17478 (code, class_type, cv_quals, declarator, std_attributes);
17480 /* Everything else is a direct-declarator. */
17481 else
17483 if (parenthesized_p)
17484 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17485 CPP_OPEN_PAREN);
17486 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17487 ctor_dtor_or_conv_p,
17488 member_p, friend_p);
17491 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17492 declarator->attributes = gnu_attributes;
17493 return declarator;
17496 /* Parse a direct-declarator or direct-abstract-declarator.
17498 direct-declarator:
17499 declarator-id
17500 direct-declarator ( parameter-declaration-clause )
17501 cv-qualifier-seq [opt]
17502 ref-qualifier [opt]
17503 exception-specification [opt]
17504 direct-declarator [ constant-expression [opt] ]
17505 ( declarator )
17507 direct-abstract-declarator:
17508 direct-abstract-declarator [opt]
17509 ( parameter-declaration-clause )
17510 cv-qualifier-seq [opt]
17511 ref-qualifier [opt]
17512 exception-specification [opt]
17513 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17514 ( abstract-declarator )
17516 Returns a representation of the declarator. DCL_KIND is
17517 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17518 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17519 we are parsing a direct-declarator. It is
17520 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17521 of ambiguity we prefer an abstract declarator, as per
17522 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17523 as for cp_parser_declarator. */
17525 static cp_declarator *
17526 cp_parser_direct_declarator (cp_parser* parser,
17527 cp_parser_declarator_kind dcl_kind,
17528 int* ctor_dtor_or_conv_p,
17529 bool member_p, bool friend_p)
17531 cp_token *token;
17532 cp_declarator *declarator = NULL;
17533 tree scope = NULL_TREE;
17534 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17535 bool saved_in_declarator_p = parser->in_declarator_p;
17536 bool first = true;
17537 tree pushed_scope = NULL_TREE;
17539 while (true)
17541 /* Peek at the next token. */
17542 token = cp_lexer_peek_token (parser->lexer);
17543 if (token->type == CPP_OPEN_PAREN)
17545 /* This is either a parameter-declaration-clause, or a
17546 parenthesized declarator. When we know we are parsing a
17547 named declarator, it must be a parenthesized declarator
17548 if FIRST is true. For instance, `(int)' is a
17549 parameter-declaration-clause, with an omitted
17550 direct-abstract-declarator. But `((*))', is a
17551 parenthesized abstract declarator. Finally, when T is a
17552 template parameter `(T)' is a
17553 parameter-declaration-clause, and not a parenthesized
17554 named declarator.
17556 We first try and parse a parameter-declaration-clause,
17557 and then try a nested declarator (if FIRST is true).
17559 It is not an error for it not to be a
17560 parameter-declaration-clause, even when FIRST is
17561 false. Consider,
17563 int i (int);
17564 int i (3);
17566 The first is the declaration of a function while the
17567 second is the definition of a variable, including its
17568 initializer.
17570 Having seen only the parenthesis, we cannot know which of
17571 these two alternatives should be selected. Even more
17572 complex are examples like:
17574 int i (int (a));
17575 int i (int (3));
17577 The former is a function-declaration; the latter is a
17578 variable initialization.
17580 Thus again, we try a parameter-declaration-clause, and if
17581 that fails, we back out and return. */
17583 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17585 tree params;
17586 bool is_declarator = false;
17588 /* In a member-declarator, the only valid interpretation
17589 of a parenthesis is the start of a
17590 parameter-declaration-clause. (It is invalid to
17591 initialize a static data member with a parenthesized
17592 initializer; only the "=" form of initialization is
17593 permitted.) */
17594 if (!member_p)
17595 cp_parser_parse_tentatively (parser);
17597 /* Consume the `('. */
17598 cp_lexer_consume_token (parser->lexer);
17599 if (first)
17601 /* If this is going to be an abstract declarator, we're
17602 in a declarator and we can't have default args. */
17603 parser->default_arg_ok_p = false;
17604 parser->in_declarator_p = true;
17607 begin_scope (sk_function_parms, NULL_TREE);
17609 /* Parse the parameter-declaration-clause. */
17610 params = cp_parser_parameter_declaration_clause (parser);
17612 /* Consume the `)'. */
17613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17615 /* If all went well, parse the cv-qualifier-seq,
17616 ref-qualifier and the exception-specification. */
17617 if (member_p || cp_parser_parse_definitely (parser))
17619 cp_cv_quals cv_quals;
17620 cp_virt_specifiers virt_specifiers;
17621 cp_ref_qualifier ref_qual;
17622 tree exception_specification;
17623 tree late_return;
17624 tree attrs;
17625 bool memfn = (member_p || (pushed_scope
17626 && CLASS_TYPE_P (pushed_scope)));
17628 is_declarator = true;
17630 if (ctor_dtor_or_conv_p)
17631 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17632 first = false;
17634 /* Parse the cv-qualifier-seq. */
17635 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17636 /* Parse the ref-qualifier. */
17637 ref_qual = cp_parser_ref_qualifier_opt (parser);
17638 /* And the exception-specification. */
17639 exception_specification
17640 = cp_parser_exception_specification_opt (parser);
17642 attrs = cp_parser_std_attribute_spec_seq (parser);
17644 /* In here, we handle cases where attribute is used after
17645 the function declaration. For example:
17646 void func (int x) __attribute__((vector(..))); */
17647 if (flag_cilkplus
17648 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17650 cp_parser_parse_tentatively (parser);
17651 tree attr = cp_parser_gnu_attributes_opt (parser);
17652 if (cp_lexer_next_token_is_not (parser->lexer,
17653 CPP_SEMICOLON)
17654 && cp_lexer_next_token_is_not (parser->lexer,
17655 CPP_OPEN_BRACE))
17656 cp_parser_abort_tentative_parse (parser);
17657 else if (!cp_parser_parse_definitely (parser))
17659 else
17660 attrs = chainon (attr, attrs);
17662 late_return = (cp_parser_late_return_type_opt
17663 (parser, declarator,
17664 memfn ? cv_quals : -1));
17667 /* Parse the virt-specifier-seq. */
17668 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17670 /* Create the function-declarator. */
17671 declarator = make_call_declarator (declarator,
17672 params,
17673 cv_quals,
17674 virt_specifiers,
17675 ref_qual,
17676 exception_specification,
17677 late_return);
17678 declarator->std_attributes = attrs;
17679 /* Any subsequent parameter lists are to do with
17680 return type, so are not those of the declared
17681 function. */
17682 parser->default_arg_ok_p = false;
17685 /* Remove the function parms from scope. */
17686 pop_bindings_and_leave_scope ();
17688 if (is_declarator)
17689 /* Repeat the main loop. */
17690 continue;
17693 /* If this is the first, we can try a parenthesized
17694 declarator. */
17695 if (first)
17697 bool saved_in_type_id_in_expr_p;
17699 parser->default_arg_ok_p = saved_default_arg_ok_p;
17700 parser->in_declarator_p = saved_in_declarator_p;
17702 /* Consume the `('. */
17703 cp_lexer_consume_token (parser->lexer);
17704 /* Parse the nested declarator. */
17705 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17706 parser->in_type_id_in_expr_p = true;
17707 declarator
17708 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17709 /*parenthesized_p=*/NULL,
17710 member_p, friend_p);
17711 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17712 first = false;
17713 /* Expect a `)'. */
17714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17715 declarator = cp_error_declarator;
17716 if (declarator == cp_error_declarator)
17717 break;
17719 goto handle_declarator;
17721 /* Otherwise, we must be done. */
17722 else
17723 break;
17725 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17726 && token->type == CPP_OPEN_SQUARE
17727 && !cp_next_tokens_can_be_attribute_p (parser))
17729 /* Parse an array-declarator. */
17730 tree bounds, attrs;
17732 if (ctor_dtor_or_conv_p)
17733 *ctor_dtor_or_conv_p = 0;
17735 first = false;
17736 parser->default_arg_ok_p = false;
17737 parser->in_declarator_p = true;
17738 /* Consume the `['. */
17739 cp_lexer_consume_token (parser->lexer);
17740 /* Peek at the next token. */
17741 token = cp_lexer_peek_token (parser->lexer);
17742 /* If the next token is `]', then there is no
17743 constant-expression. */
17744 if (token->type != CPP_CLOSE_SQUARE)
17746 bool non_constant_p;
17747 bounds
17748 = cp_parser_constant_expression (parser,
17749 /*allow_non_constant=*/true,
17750 &non_constant_p);
17751 if (!non_constant_p)
17752 /* OK */;
17753 else if (error_operand_p (bounds))
17754 /* Already gave an error. */;
17755 else if (!parser->in_function_body
17756 || current_binding_level->kind == sk_function_parms)
17758 /* Normally, the array bound must be an integral constant
17759 expression. However, as an extension, we allow VLAs
17760 in function scopes as long as they aren't part of a
17761 parameter declaration. */
17762 cp_parser_error (parser,
17763 "array bound is not an integer constant");
17764 bounds = error_mark_node;
17766 else if (processing_template_decl
17767 && !type_dependent_expression_p (bounds))
17769 /* Remember this wasn't a constant-expression. */
17770 bounds = build_nop (TREE_TYPE (bounds), bounds);
17771 TREE_SIDE_EFFECTS (bounds) = 1;
17774 else
17775 bounds = NULL_TREE;
17776 /* Look for the closing `]'. */
17777 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17779 declarator = cp_error_declarator;
17780 break;
17783 attrs = cp_parser_std_attribute_spec_seq (parser);
17784 declarator = make_array_declarator (declarator, bounds);
17785 declarator->std_attributes = attrs;
17787 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17790 tree qualifying_scope;
17791 tree unqualified_name;
17792 tree attrs;
17793 special_function_kind sfk;
17794 bool abstract_ok;
17795 bool pack_expansion_p = false;
17796 cp_token *declarator_id_start_token;
17798 /* Parse a declarator-id */
17799 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17800 if (abstract_ok)
17802 cp_parser_parse_tentatively (parser);
17804 /* If we see an ellipsis, we should be looking at a
17805 parameter pack. */
17806 if (token->type == CPP_ELLIPSIS)
17808 /* Consume the `...' */
17809 cp_lexer_consume_token (parser->lexer);
17811 pack_expansion_p = true;
17815 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17816 unqualified_name
17817 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17818 qualifying_scope = parser->scope;
17819 if (abstract_ok)
17821 bool okay = false;
17823 if (!unqualified_name && pack_expansion_p)
17825 /* Check whether an error occurred. */
17826 okay = !cp_parser_error_occurred (parser);
17828 /* We already consumed the ellipsis to mark a
17829 parameter pack, but we have no way to report it,
17830 so abort the tentative parse. We will be exiting
17831 immediately anyway. */
17832 cp_parser_abort_tentative_parse (parser);
17834 else
17835 okay = cp_parser_parse_definitely (parser);
17837 if (!okay)
17838 unqualified_name = error_mark_node;
17839 else if (unqualified_name
17840 && (qualifying_scope
17841 || (!identifier_p (unqualified_name))))
17843 cp_parser_error (parser, "expected unqualified-id");
17844 unqualified_name = error_mark_node;
17848 if (!unqualified_name)
17849 return NULL;
17850 if (unqualified_name == error_mark_node)
17852 declarator = cp_error_declarator;
17853 pack_expansion_p = false;
17854 declarator->parameter_pack_p = false;
17855 break;
17858 attrs = cp_parser_std_attribute_spec_seq (parser);
17860 if (qualifying_scope && at_namespace_scope_p ()
17861 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17863 /* In the declaration of a member of a template class
17864 outside of the class itself, the SCOPE will sometimes
17865 be a TYPENAME_TYPE. For example, given:
17867 template <typename T>
17868 int S<T>::R::i = 3;
17870 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17871 this context, we must resolve S<T>::R to an ordinary
17872 type, rather than a typename type.
17874 The reason we normally avoid resolving TYPENAME_TYPEs
17875 is that a specialization of `S' might render
17876 `S<T>::R' not a type. However, if `S' is
17877 specialized, then this `i' will not be used, so there
17878 is no harm in resolving the types here. */
17879 tree type;
17881 /* Resolve the TYPENAME_TYPE. */
17882 type = resolve_typename_type (qualifying_scope,
17883 /*only_current_p=*/false);
17884 /* If that failed, the declarator is invalid. */
17885 if (TREE_CODE (type) == TYPENAME_TYPE)
17887 if (typedef_variant_p (type))
17888 error_at (declarator_id_start_token->location,
17889 "cannot define member of dependent typedef "
17890 "%qT", type);
17891 else
17892 error_at (declarator_id_start_token->location,
17893 "%<%T::%E%> is not a type",
17894 TYPE_CONTEXT (qualifying_scope),
17895 TYPE_IDENTIFIER (qualifying_scope));
17897 qualifying_scope = type;
17900 sfk = sfk_none;
17902 if (unqualified_name)
17904 tree class_type;
17906 if (qualifying_scope
17907 && CLASS_TYPE_P (qualifying_scope))
17908 class_type = qualifying_scope;
17909 else
17910 class_type = current_class_type;
17912 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17914 tree name_type = TREE_TYPE (unqualified_name);
17915 if (class_type && same_type_p (name_type, class_type))
17917 if (qualifying_scope
17918 && CLASSTYPE_USE_TEMPLATE (name_type))
17920 error_at (declarator_id_start_token->location,
17921 "invalid use of constructor as a template");
17922 inform (declarator_id_start_token->location,
17923 "use %<%T::%D%> instead of %<%T::%D%> to "
17924 "name the constructor in a qualified name",
17925 class_type,
17926 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17927 class_type, name_type);
17928 declarator = cp_error_declarator;
17929 break;
17931 else
17932 unqualified_name = constructor_name (class_type);
17934 else
17936 /* We do not attempt to print the declarator
17937 here because we do not have enough
17938 information about its original syntactic
17939 form. */
17940 cp_parser_error (parser, "invalid declarator");
17941 declarator = cp_error_declarator;
17942 break;
17946 if (class_type)
17948 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17949 sfk = sfk_destructor;
17950 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17951 sfk = sfk_conversion;
17952 else if (/* There's no way to declare a constructor
17953 for an anonymous type, even if the type
17954 got a name for linkage purposes. */
17955 !TYPE_WAS_ANONYMOUS (class_type)
17956 /* Handle correctly (c++/19200):
17958 struct S {
17959 struct T{};
17960 friend void S(T);
17963 and also:
17965 namespace N {
17966 void S();
17969 struct S {
17970 friend void N::S();
17971 }; */
17972 && !(friend_p
17973 && class_type != qualifying_scope)
17974 && constructor_name_p (unqualified_name,
17975 class_type))
17977 unqualified_name = constructor_name (class_type);
17978 sfk = sfk_constructor;
17980 else if (is_overloaded_fn (unqualified_name)
17981 && DECL_CONSTRUCTOR_P (get_first_fn
17982 (unqualified_name)))
17983 sfk = sfk_constructor;
17985 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17986 *ctor_dtor_or_conv_p = -1;
17989 declarator = make_id_declarator (qualifying_scope,
17990 unqualified_name,
17991 sfk);
17992 declarator->std_attributes = attrs;
17993 declarator->id_loc = token->location;
17994 declarator->parameter_pack_p = pack_expansion_p;
17996 if (pack_expansion_p)
17997 maybe_warn_variadic_templates ();
18000 handle_declarator:;
18001 scope = get_scope_of_declarator (declarator);
18002 if (scope)
18004 /* Any names that appear after the declarator-id for a
18005 member are looked up in the containing scope. */
18006 if (at_function_scope_p ())
18008 /* But declarations with qualified-ids can't appear in a
18009 function. */
18010 cp_parser_error (parser, "qualified-id in declaration");
18011 declarator = cp_error_declarator;
18012 break;
18014 pushed_scope = push_scope (scope);
18016 parser->in_declarator_p = true;
18017 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18018 || (declarator && declarator->kind == cdk_id))
18019 /* Default args are only allowed on function
18020 declarations. */
18021 parser->default_arg_ok_p = saved_default_arg_ok_p;
18022 else
18023 parser->default_arg_ok_p = false;
18025 first = false;
18027 /* We're done. */
18028 else
18029 break;
18032 /* For an abstract declarator, we might wind up with nothing at this
18033 point. That's an error; the declarator is not optional. */
18034 if (!declarator)
18035 cp_parser_error (parser, "expected declarator");
18037 /* If we entered a scope, we must exit it now. */
18038 if (pushed_scope)
18039 pop_scope (pushed_scope);
18041 parser->default_arg_ok_p = saved_default_arg_ok_p;
18042 parser->in_declarator_p = saved_in_declarator_p;
18044 return declarator;
18047 /* Parse a ptr-operator.
18049 ptr-operator:
18050 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18051 * cv-qualifier-seq [opt]
18053 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18054 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18056 GNU Extension:
18058 ptr-operator:
18059 & cv-qualifier-seq [opt]
18061 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18062 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18063 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18064 filled in with the TYPE containing the member. *CV_QUALS is
18065 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18066 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18067 Note that the tree codes returned by this function have nothing
18068 to do with the types of trees that will be eventually be created
18069 to represent the pointer or reference type being parsed. They are
18070 just constants with suggestive names. */
18071 static enum tree_code
18072 cp_parser_ptr_operator (cp_parser* parser,
18073 tree* type,
18074 cp_cv_quals *cv_quals,
18075 tree *attributes)
18077 enum tree_code code = ERROR_MARK;
18078 cp_token *token;
18079 tree attrs = NULL_TREE;
18081 /* Assume that it's not a pointer-to-member. */
18082 *type = NULL_TREE;
18083 /* And that there are no cv-qualifiers. */
18084 *cv_quals = TYPE_UNQUALIFIED;
18086 /* Peek at the next token. */
18087 token = cp_lexer_peek_token (parser->lexer);
18089 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18090 if (token->type == CPP_MULT)
18091 code = INDIRECT_REF;
18092 else if (token->type == CPP_AND)
18093 code = ADDR_EXPR;
18094 else if ((cxx_dialect != cxx98) &&
18095 token->type == CPP_AND_AND) /* C++0x only */
18096 code = NON_LVALUE_EXPR;
18098 if (code != ERROR_MARK)
18100 /* Consume the `*', `&' or `&&'. */
18101 cp_lexer_consume_token (parser->lexer);
18103 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18104 `&', if we are allowing GNU extensions. (The only qualifier
18105 that can legally appear after `&' is `restrict', but that is
18106 enforced during semantic analysis. */
18107 if (code == INDIRECT_REF
18108 || cp_parser_allow_gnu_extensions_p (parser))
18109 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18111 attrs = cp_parser_std_attribute_spec_seq (parser);
18112 if (attributes != NULL)
18113 *attributes = attrs;
18115 else
18117 /* Try the pointer-to-member case. */
18118 cp_parser_parse_tentatively (parser);
18119 /* Look for the optional `::' operator. */
18120 cp_parser_global_scope_opt (parser,
18121 /*current_scope_valid_p=*/false);
18122 /* Look for the nested-name specifier. */
18123 token = cp_lexer_peek_token (parser->lexer);
18124 cp_parser_nested_name_specifier (parser,
18125 /*typename_keyword_p=*/false,
18126 /*check_dependency_p=*/true,
18127 /*type_p=*/false,
18128 /*is_declaration=*/false);
18129 /* If we found it, and the next token is a `*', then we are
18130 indeed looking at a pointer-to-member operator. */
18131 if (!cp_parser_error_occurred (parser)
18132 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18134 /* Indicate that the `*' operator was used. */
18135 code = INDIRECT_REF;
18137 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18138 error_at (token->location, "%qD is a namespace", parser->scope);
18139 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18140 error_at (token->location, "cannot form pointer to member of "
18141 "non-class %q#T", parser->scope);
18142 else
18144 /* The type of which the member is a member is given by the
18145 current SCOPE. */
18146 *type = parser->scope;
18147 /* The next name will not be qualified. */
18148 parser->scope = NULL_TREE;
18149 parser->qualifying_scope = NULL_TREE;
18150 parser->object_scope = NULL_TREE;
18151 /* Look for optional c++11 attributes. */
18152 attrs = cp_parser_std_attribute_spec_seq (parser);
18153 if (attributes != NULL)
18154 *attributes = attrs;
18155 /* Look for the optional cv-qualifier-seq. */
18156 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18159 /* If that didn't work we don't have a ptr-operator. */
18160 if (!cp_parser_parse_definitely (parser))
18161 cp_parser_error (parser, "expected ptr-operator");
18164 return code;
18167 /* Parse an (optional) cv-qualifier-seq.
18169 cv-qualifier-seq:
18170 cv-qualifier cv-qualifier-seq [opt]
18172 cv-qualifier:
18173 const
18174 volatile
18176 GNU Extension:
18178 cv-qualifier:
18179 __restrict__
18181 Returns a bitmask representing the cv-qualifiers. */
18183 static cp_cv_quals
18184 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18186 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18188 while (true)
18190 cp_token *token;
18191 cp_cv_quals cv_qualifier;
18193 /* Peek at the next token. */
18194 token = cp_lexer_peek_token (parser->lexer);
18195 /* See if it's a cv-qualifier. */
18196 switch (token->keyword)
18198 case RID_CONST:
18199 cv_qualifier = TYPE_QUAL_CONST;
18200 break;
18202 case RID_VOLATILE:
18203 cv_qualifier = TYPE_QUAL_VOLATILE;
18204 break;
18206 case RID_RESTRICT:
18207 cv_qualifier = TYPE_QUAL_RESTRICT;
18208 break;
18210 default:
18211 cv_qualifier = TYPE_UNQUALIFIED;
18212 break;
18215 if (!cv_qualifier)
18216 break;
18218 if (cv_quals & cv_qualifier)
18220 error_at (token->location, "duplicate cv-qualifier");
18221 cp_lexer_purge_token (parser->lexer);
18223 else
18225 cp_lexer_consume_token (parser->lexer);
18226 cv_quals |= cv_qualifier;
18230 return cv_quals;
18233 /* Parse an (optional) ref-qualifier
18235 ref-qualifier:
18239 Returns cp_ref_qualifier representing ref-qualifier. */
18241 static cp_ref_qualifier
18242 cp_parser_ref_qualifier_opt (cp_parser* parser)
18244 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18246 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18247 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18248 return ref_qual;
18250 while (true)
18252 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18253 cp_token *token = cp_lexer_peek_token (parser->lexer);
18255 switch (token->type)
18257 case CPP_AND:
18258 curr_ref_qual = REF_QUAL_LVALUE;
18259 break;
18261 case CPP_AND_AND:
18262 curr_ref_qual = REF_QUAL_RVALUE;
18263 break;
18265 default:
18266 curr_ref_qual = REF_QUAL_NONE;
18267 break;
18270 if (!curr_ref_qual)
18271 break;
18272 else if (ref_qual)
18274 error_at (token->location, "multiple ref-qualifiers");
18275 cp_lexer_purge_token (parser->lexer);
18277 else
18279 ref_qual = curr_ref_qual;
18280 cp_lexer_consume_token (parser->lexer);
18284 return ref_qual;
18287 /* Parse an (optional) virt-specifier-seq.
18289 virt-specifier-seq:
18290 virt-specifier virt-specifier-seq [opt]
18292 virt-specifier:
18293 override
18294 final
18296 Returns a bitmask representing the virt-specifiers. */
18298 static cp_virt_specifiers
18299 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18301 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18303 while (true)
18305 cp_token *token;
18306 cp_virt_specifiers virt_specifier;
18308 /* Peek at the next token. */
18309 token = cp_lexer_peek_token (parser->lexer);
18310 /* See if it's a virt-specifier-qualifier. */
18311 if (token->type != CPP_NAME)
18312 break;
18313 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18315 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18316 virt_specifier = VIRT_SPEC_OVERRIDE;
18318 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18320 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18321 virt_specifier = VIRT_SPEC_FINAL;
18323 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18325 virt_specifier = VIRT_SPEC_FINAL;
18327 else
18328 break;
18330 if (virt_specifiers & virt_specifier)
18332 error_at (token->location, "duplicate virt-specifier");
18333 cp_lexer_purge_token (parser->lexer);
18335 else
18337 cp_lexer_consume_token (parser->lexer);
18338 virt_specifiers |= virt_specifier;
18341 return virt_specifiers;
18344 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18345 is in scope even though it isn't real. */
18347 void
18348 inject_this_parameter (tree ctype, cp_cv_quals quals)
18350 tree this_parm;
18352 if (current_class_ptr)
18354 /* We don't clear this between NSDMIs. Is it already what we want? */
18355 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18356 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18357 && cp_type_quals (type) == quals)
18358 return;
18361 this_parm = build_this_parm (ctype, quals);
18362 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18363 current_class_ptr = NULL_TREE;
18364 current_class_ref
18365 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18366 current_class_ptr = this_parm;
18369 /* Return true iff our current scope is a non-static data member
18370 initializer. */
18372 bool
18373 parsing_nsdmi (void)
18375 /* We recognize NSDMI context by the context-less 'this' pointer set up
18376 by the function above. */
18377 if (current_class_ptr
18378 && TREE_CODE (current_class_ptr) == PARM_DECL
18379 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18380 return true;
18381 return false;
18384 /* Parse a late-specified return type, if any. This is not a separate
18385 non-terminal, but part of a function declarator, which looks like
18387 -> trailing-type-specifier-seq abstract-declarator(opt)
18389 Returns the type indicated by the type-id.
18391 In addition to this this parses any queued up omp declare simd
18392 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18394 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18395 function. */
18397 static tree
18398 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18399 cp_cv_quals quals)
18401 cp_token *token;
18402 tree type = NULL_TREE;
18403 bool declare_simd_p = (parser->omp_declare_simd
18404 && declarator
18405 && declarator->kind == cdk_id);
18407 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18408 && declarator && declarator->kind == cdk_id);
18410 /* Peek at the next token. */
18411 token = cp_lexer_peek_token (parser->lexer);
18412 /* A late-specified return type is indicated by an initial '->'. */
18413 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18414 return NULL_TREE;
18416 tree save_ccp = current_class_ptr;
18417 tree save_ccr = current_class_ref;
18418 if (quals >= 0)
18420 /* DR 1207: 'this' is in scope in the trailing return type. */
18421 inject_this_parameter (current_class_type, quals);
18424 if (token->type == CPP_DEREF)
18426 /* Consume the ->. */
18427 cp_lexer_consume_token (parser->lexer);
18429 type = cp_parser_trailing_type_id (parser);
18432 if (cilk_simd_fn_vector_p)
18433 declarator->std_attributes
18434 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18435 declarator->std_attributes);
18436 if (declare_simd_p)
18437 declarator->std_attributes
18438 = cp_parser_late_parsing_omp_declare_simd (parser,
18439 declarator->std_attributes);
18441 if (quals >= 0)
18443 current_class_ptr = save_ccp;
18444 current_class_ref = save_ccr;
18447 return type;
18450 /* Parse a declarator-id.
18452 declarator-id:
18453 id-expression
18454 :: [opt] nested-name-specifier [opt] type-name
18456 In the `id-expression' case, the value returned is as for
18457 cp_parser_id_expression if the id-expression was an unqualified-id.
18458 If the id-expression was a qualified-id, then a SCOPE_REF is
18459 returned. The first operand is the scope (either a NAMESPACE_DECL
18460 or TREE_TYPE), but the second is still just a representation of an
18461 unqualified-id. */
18463 static tree
18464 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18466 tree id;
18467 /* The expression must be an id-expression. Assume that qualified
18468 names are the names of types so that:
18470 template <class T>
18471 int S<T>::R::i = 3;
18473 will work; we must treat `S<T>::R' as the name of a type.
18474 Similarly, assume that qualified names are templates, where
18475 required, so that:
18477 template <class T>
18478 int S<T>::R<T>::i = 3;
18480 will work, too. */
18481 id = cp_parser_id_expression (parser,
18482 /*template_keyword_p=*/false,
18483 /*check_dependency_p=*/false,
18484 /*template_p=*/NULL,
18485 /*declarator_p=*/true,
18486 optional_p);
18487 if (id && BASELINK_P (id))
18488 id = BASELINK_FUNCTIONS (id);
18489 return id;
18492 /* Parse a type-id.
18494 type-id:
18495 type-specifier-seq abstract-declarator [opt]
18497 Returns the TYPE specified. */
18499 static tree
18500 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18501 bool is_trailing_return)
18503 cp_decl_specifier_seq type_specifier_seq;
18504 cp_declarator *abstract_declarator;
18506 /* Parse the type-specifier-seq. */
18507 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18508 is_trailing_return,
18509 &type_specifier_seq);
18510 if (type_specifier_seq.type == error_mark_node)
18511 return error_mark_node;
18513 /* There might or might not be an abstract declarator. */
18514 cp_parser_parse_tentatively (parser);
18515 /* Look for the declarator. */
18516 abstract_declarator
18517 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18518 /*parenthesized_p=*/NULL,
18519 /*member_p=*/false,
18520 /*friend_p=*/false);
18521 /* Check to see if there really was a declarator. */
18522 if (!cp_parser_parse_definitely (parser))
18523 abstract_declarator = NULL;
18525 if (type_specifier_seq.type
18526 /* None of the valid uses of 'auto' in C++14 involve the type-id
18527 nonterminal, but it is valid in a trailing-return-type. */
18528 && !(cxx_dialect >= cxx14 && is_trailing_return)
18529 && type_uses_auto (type_specifier_seq.type))
18531 /* A type-id with type 'auto' is only ok if the abstract declarator
18532 is a function declarator with a late-specified return type. */
18533 if (abstract_declarator
18534 && abstract_declarator->kind == cdk_function
18535 && abstract_declarator->u.function.late_return_type)
18536 /* OK */;
18537 else
18539 error ("invalid use of %<auto%>");
18540 return error_mark_node;
18544 return groktypename (&type_specifier_seq, abstract_declarator,
18545 is_template_arg);
18548 static tree cp_parser_type_id (cp_parser *parser)
18550 return cp_parser_type_id_1 (parser, false, false);
18553 static tree cp_parser_template_type_arg (cp_parser *parser)
18555 tree r;
18556 const char *saved_message = parser->type_definition_forbidden_message;
18557 parser->type_definition_forbidden_message
18558 = G_("types may not be defined in template arguments");
18559 r = cp_parser_type_id_1 (parser, true, false);
18560 parser->type_definition_forbidden_message = saved_message;
18561 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18563 error ("invalid use of %<auto%> in template argument");
18564 r = error_mark_node;
18566 return r;
18569 static tree cp_parser_trailing_type_id (cp_parser *parser)
18571 return cp_parser_type_id_1 (parser, false, true);
18574 /* Parse a type-specifier-seq.
18576 type-specifier-seq:
18577 type-specifier type-specifier-seq [opt]
18579 GNU extension:
18581 type-specifier-seq:
18582 attributes type-specifier-seq [opt]
18584 If IS_DECLARATION is true, we are at the start of a "condition" or
18585 exception-declaration, so we might be followed by a declarator-id.
18587 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18588 i.e. we've just seen "->".
18590 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18592 static void
18593 cp_parser_type_specifier_seq (cp_parser* parser,
18594 bool is_declaration,
18595 bool is_trailing_return,
18596 cp_decl_specifier_seq *type_specifier_seq)
18598 bool seen_type_specifier = false;
18599 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18600 cp_token *start_token = NULL;
18602 /* Clear the TYPE_SPECIFIER_SEQ. */
18603 clear_decl_specs (type_specifier_seq);
18605 /* In the context of a trailing return type, enum E { } is an
18606 elaborated-type-specifier followed by a function-body, not an
18607 enum-specifier. */
18608 if (is_trailing_return)
18609 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18611 /* Parse the type-specifiers and attributes. */
18612 while (true)
18614 tree type_specifier;
18615 bool is_cv_qualifier;
18617 /* Check for attributes first. */
18618 if (cp_next_tokens_can_be_attribute_p (parser))
18620 type_specifier_seq->attributes =
18621 chainon (type_specifier_seq->attributes,
18622 cp_parser_attributes_opt (parser));
18623 continue;
18626 /* record the token of the beginning of the type specifier seq,
18627 for error reporting purposes*/
18628 if (!start_token)
18629 start_token = cp_lexer_peek_token (parser->lexer);
18631 /* Look for the type-specifier. */
18632 type_specifier = cp_parser_type_specifier (parser,
18633 flags,
18634 type_specifier_seq,
18635 /*is_declaration=*/false,
18636 NULL,
18637 &is_cv_qualifier);
18638 if (!type_specifier)
18640 /* If the first type-specifier could not be found, this is not a
18641 type-specifier-seq at all. */
18642 if (!seen_type_specifier)
18644 /* Set in_declarator_p to avoid skipping to the semicolon. */
18645 int in_decl = parser->in_declarator_p;
18646 parser->in_declarator_p = true;
18648 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18649 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18650 cp_parser_error (parser, "expected type-specifier");
18652 parser->in_declarator_p = in_decl;
18654 type_specifier_seq->type = error_mark_node;
18655 return;
18657 /* If subsequent type-specifiers could not be found, the
18658 type-specifier-seq is complete. */
18659 break;
18662 seen_type_specifier = true;
18663 /* The standard says that a condition can be:
18665 type-specifier-seq declarator = assignment-expression
18667 However, given:
18669 struct S {};
18670 if (int S = ...)
18672 we should treat the "S" as a declarator, not as a
18673 type-specifier. The standard doesn't say that explicitly for
18674 type-specifier-seq, but it does say that for
18675 decl-specifier-seq in an ordinary declaration. Perhaps it
18676 would be clearer just to allow a decl-specifier-seq here, and
18677 then add a semantic restriction that if any decl-specifiers
18678 that are not type-specifiers appear, the program is invalid. */
18679 if (is_declaration && !is_cv_qualifier)
18680 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18684 /* Return whether the function currently being declared has an associated
18685 template parameter list. */
18687 static bool
18688 function_being_declared_is_template_p (cp_parser* parser)
18690 if (!current_template_parms || processing_template_parmlist)
18691 return false;
18693 if (parser->implicit_template_scope)
18694 return true;
18696 if (at_class_scope_p ()
18697 && TYPE_BEING_DEFINED (current_class_type))
18698 return parser->num_template_parameter_lists != 0;
18700 return ((int) parser->num_template_parameter_lists > template_class_depth
18701 (current_class_type));
18704 /* Parse a parameter-declaration-clause.
18706 parameter-declaration-clause:
18707 parameter-declaration-list [opt] ... [opt]
18708 parameter-declaration-list , ...
18710 Returns a representation for the parameter declarations. A return
18711 value of NULL indicates a parameter-declaration-clause consisting
18712 only of an ellipsis. */
18714 static tree
18715 cp_parser_parameter_declaration_clause (cp_parser* parser)
18717 tree parameters;
18718 cp_token *token;
18719 bool ellipsis_p;
18720 bool is_error;
18722 struct cleanup {
18723 cp_parser* parser;
18724 int auto_is_implicit_function_template_parm_p;
18725 ~cleanup() {
18726 parser->auto_is_implicit_function_template_parm_p
18727 = auto_is_implicit_function_template_parm_p;
18729 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18731 (void) cleanup;
18733 if (!processing_specialization
18734 && !processing_template_parmlist
18735 && !processing_explicit_instantiation)
18736 if (!current_function_decl
18737 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18738 parser->auto_is_implicit_function_template_parm_p = true;
18740 /* Peek at the next token. */
18741 token = cp_lexer_peek_token (parser->lexer);
18742 /* Check for trivial parameter-declaration-clauses. */
18743 if (token->type == CPP_ELLIPSIS)
18745 /* Consume the `...' token. */
18746 cp_lexer_consume_token (parser->lexer);
18747 return NULL_TREE;
18749 else if (token->type == CPP_CLOSE_PAREN)
18750 /* There are no parameters. */
18752 #ifndef NO_IMPLICIT_EXTERN_C
18753 if (in_system_header_at (input_location)
18754 && current_class_type == NULL
18755 && current_lang_name == lang_name_c)
18756 return NULL_TREE;
18757 else
18758 #endif
18759 return void_list_node;
18761 /* Check for `(void)', too, which is a special case. */
18762 else if (token->keyword == RID_VOID
18763 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18764 == CPP_CLOSE_PAREN))
18766 /* Consume the `void' token. */
18767 cp_lexer_consume_token (parser->lexer);
18768 /* There are no parameters. */
18769 return void_list_node;
18772 /* Parse the parameter-declaration-list. */
18773 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18774 /* If a parse error occurred while parsing the
18775 parameter-declaration-list, then the entire
18776 parameter-declaration-clause is erroneous. */
18777 if (is_error)
18778 return NULL;
18780 /* Peek at the next token. */
18781 token = cp_lexer_peek_token (parser->lexer);
18782 /* If it's a `,', the clause should terminate with an ellipsis. */
18783 if (token->type == CPP_COMMA)
18785 /* Consume the `,'. */
18786 cp_lexer_consume_token (parser->lexer);
18787 /* Expect an ellipsis. */
18788 ellipsis_p
18789 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18791 /* It might also be `...' if the optional trailing `,' was
18792 omitted. */
18793 else if (token->type == CPP_ELLIPSIS)
18795 /* Consume the `...' token. */
18796 cp_lexer_consume_token (parser->lexer);
18797 /* And remember that we saw it. */
18798 ellipsis_p = true;
18800 else
18801 ellipsis_p = false;
18803 /* Finish the parameter list. */
18804 if (!ellipsis_p)
18805 parameters = chainon (parameters, void_list_node);
18807 return parameters;
18810 /* Parse a parameter-declaration-list.
18812 parameter-declaration-list:
18813 parameter-declaration
18814 parameter-declaration-list , parameter-declaration
18816 Returns a representation of the parameter-declaration-list, as for
18817 cp_parser_parameter_declaration_clause. However, the
18818 `void_list_node' is never appended to the list. Upon return,
18819 *IS_ERROR will be true iff an error occurred. */
18821 static tree
18822 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18824 tree parameters = NULL_TREE;
18825 tree *tail = &parameters;
18826 bool saved_in_unbraced_linkage_specification_p;
18827 int index = 0;
18829 /* Assume all will go well. */
18830 *is_error = false;
18831 /* The special considerations that apply to a function within an
18832 unbraced linkage specifications do not apply to the parameters
18833 to the function. */
18834 saved_in_unbraced_linkage_specification_p
18835 = parser->in_unbraced_linkage_specification_p;
18836 parser->in_unbraced_linkage_specification_p = false;
18838 /* Look for more parameters. */
18839 while (true)
18841 cp_parameter_declarator *parameter;
18842 tree decl = error_mark_node;
18843 bool parenthesized_p = false;
18844 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18845 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18846 (current_template_parms)) : 0);
18848 /* Parse the parameter. */
18849 parameter
18850 = cp_parser_parameter_declaration (parser,
18851 /*template_parm_p=*/false,
18852 &parenthesized_p);
18854 /* We don't know yet if the enclosing context is deprecated, so wait
18855 and warn in grokparms if appropriate. */
18856 deprecated_state = DEPRECATED_SUPPRESS;
18858 if (parameter)
18860 /* If a function parameter pack was specified and an implicit template
18861 parameter was introduced during cp_parser_parameter_declaration,
18862 change any implicit parameters introduced into packs. */
18863 if (parser->implicit_template_parms
18864 && parameter->declarator
18865 && parameter->declarator->parameter_pack_p)
18867 int latest_template_parm_idx = TREE_VEC_LENGTH
18868 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18870 if (latest_template_parm_idx != template_parm_idx)
18871 parameter->decl_specifiers.type = convert_generic_types_to_packs
18872 (parameter->decl_specifiers.type,
18873 template_parm_idx, latest_template_parm_idx);
18876 decl = grokdeclarator (parameter->declarator,
18877 &parameter->decl_specifiers,
18878 PARM,
18879 parameter->default_argument != NULL_TREE,
18880 &parameter->decl_specifiers.attributes);
18883 deprecated_state = DEPRECATED_NORMAL;
18885 /* If a parse error occurred parsing the parameter declaration,
18886 then the entire parameter-declaration-list is erroneous. */
18887 if (decl == error_mark_node)
18889 *is_error = true;
18890 parameters = error_mark_node;
18891 break;
18894 if (parameter->decl_specifiers.attributes)
18895 cplus_decl_attributes (&decl,
18896 parameter->decl_specifiers.attributes,
18898 if (DECL_NAME (decl))
18899 decl = pushdecl (decl);
18901 if (decl != error_mark_node)
18903 retrofit_lang_decl (decl);
18904 DECL_PARM_INDEX (decl) = ++index;
18905 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18908 /* Add the new parameter to the list. */
18909 *tail = build_tree_list (parameter->default_argument, decl);
18910 tail = &TREE_CHAIN (*tail);
18912 /* Peek at the next token. */
18913 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18914 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18915 /* These are for Objective-C++ */
18916 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18917 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18918 /* The parameter-declaration-list is complete. */
18919 break;
18920 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18922 cp_token *token;
18924 /* Peek at the next token. */
18925 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18926 /* If it's an ellipsis, then the list is complete. */
18927 if (token->type == CPP_ELLIPSIS)
18928 break;
18929 /* Otherwise, there must be more parameters. Consume the
18930 `,'. */
18931 cp_lexer_consume_token (parser->lexer);
18932 /* When parsing something like:
18934 int i(float f, double d)
18936 we can tell after seeing the declaration for "f" that we
18937 are not looking at an initialization of a variable "i",
18938 but rather at the declaration of a function "i".
18940 Due to the fact that the parsing of template arguments
18941 (as specified to a template-id) requires backtracking we
18942 cannot use this technique when inside a template argument
18943 list. */
18944 if (!parser->in_template_argument_list_p
18945 && !parser->in_type_id_in_expr_p
18946 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18947 /* However, a parameter-declaration of the form
18948 "float(f)" (which is a valid declaration of a
18949 parameter "f") can also be interpreted as an
18950 expression (the conversion of "f" to "float"). */
18951 && !parenthesized_p)
18952 cp_parser_commit_to_tentative_parse (parser);
18954 else
18956 cp_parser_error (parser, "expected %<,%> or %<...%>");
18957 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18958 cp_parser_skip_to_closing_parenthesis (parser,
18959 /*recovering=*/true,
18960 /*or_comma=*/false,
18961 /*consume_paren=*/false);
18962 break;
18966 parser->in_unbraced_linkage_specification_p
18967 = saved_in_unbraced_linkage_specification_p;
18969 /* Reset implicit_template_scope if we are about to leave the function
18970 parameter list that introduced it. Note that for out-of-line member
18971 definitions, there will be one or more class scopes before we get to
18972 the template parameter scope. */
18974 if (cp_binding_level *its = parser->implicit_template_scope)
18975 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18977 while (maybe_its->kind == sk_class)
18978 maybe_its = maybe_its->level_chain;
18979 if (maybe_its == its)
18981 parser->implicit_template_parms = 0;
18982 parser->implicit_template_scope = 0;
18986 return parameters;
18989 /* Parse a parameter declaration.
18991 parameter-declaration:
18992 decl-specifier-seq ... [opt] declarator
18993 decl-specifier-seq declarator = assignment-expression
18994 decl-specifier-seq ... [opt] abstract-declarator [opt]
18995 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18997 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18998 declares a template parameter. (In that case, a non-nested `>'
18999 token encountered during the parsing of the assignment-expression
19000 is not interpreted as a greater-than operator.)
19002 Returns a representation of the parameter, or NULL if an error
19003 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19004 true iff the declarator is of the form "(p)". */
19006 static cp_parameter_declarator *
19007 cp_parser_parameter_declaration (cp_parser *parser,
19008 bool template_parm_p,
19009 bool *parenthesized_p)
19011 int declares_class_or_enum;
19012 cp_decl_specifier_seq decl_specifiers;
19013 cp_declarator *declarator;
19014 tree default_argument;
19015 cp_token *token = NULL, *declarator_token_start = NULL;
19016 const char *saved_message;
19018 /* In a template parameter, `>' is not an operator.
19020 [temp.param]
19022 When parsing a default template-argument for a non-type
19023 template-parameter, the first non-nested `>' is taken as the end
19024 of the template parameter-list rather than a greater-than
19025 operator. */
19027 /* Type definitions may not appear in parameter types. */
19028 saved_message = parser->type_definition_forbidden_message;
19029 parser->type_definition_forbidden_message
19030 = G_("types may not be defined in parameter types");
19032 /* Parse the declaration-specifiers. */
19033 cp_parser_decl_specifier_seq (parser,
19034 CP_PARSER_FLAGS_NONE,
19035 &decl_specifiers,
19036 &declares_class_or_enum);
19038 /* Complain about missing 'typename' or other invalid type names. */
19039 if (!decl_specifiers.any_type_specifiers_p
19040 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19041 decl_specifiers.type = error_mark_node;
19043 /* If an error occurred, there's no reason to attempt to parse the
19044 rest of the declaration. */
19045 if (cp_parser_error_occurred (parser))
19047 parser->type_definition_forbidden_message = saved_message;
19048 return NULL;
19051 /* Peek at the next token. */
19052 token = cp_lexer_peek_token (parser->lexer);
19054 /* If the next token is a `)', `,', `=', `>', or `...', then there
19055 is no declarator. However, when variadic templates are enabled,
19056 there may be a declarator following `...'. */
19057 if (token->type == CPP_CLOSE_PAREN
19058 || token->type == CPP_COMMA
19059 || token->type == CPP_EQ
19060 || token->type == CPP_GREATER)
19062 declarator = NULL;
19063 if (parenthesized_p)
19064 *parenthesized_p = false;
19066 /* Otherwise, there should be a declarator. */
19067 else
19069 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19070 parser->default_arg_ok_p = false;
19072 /* After seeing a decl-specifier-seq, if the next token is not a
19073 "(", there is no possibility that the code is a valid
19074 expression. Therefore, if parsing tentatively, we commit at
19075 this point. */
19076 if (!parser->in_template_argument_list_p
19077 /* In an expression context, having seen:
19079 (int((char ...
19081 we cannot be sure whether we are looking at a
19082 function-type (taking a "char" as a parameter) or a cast
19083 of some object of type "char" to "int". */
19084 && !parser->in_type_id_in_expr_p
19085 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19086 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19087 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19088 cp_parser_commit_to_tentative_parse (parser);
19089 /* Parse the declarator. */
19090 declarator_token_start = token;
19091 declarator = cp_parser_declarator (parser,
19092 CP_PARSER_DECLARATOR_EITHER,
19093 /*ctor_dtor_or_conv_p=*/NULL,
19094 parenthesized_p,
19095 /*member_p=*/false,
19096 /*friend_p=*/false);
19097 parser->default_arg_ok_p = saved_default_arg_ok_p;
19098 /* After the declarator, allow more attributes. */
19099 decl_specifiers.attributes
19100 = chainon (decl_specifiers.attributes,
19101 cp_parser_attributes_opt (parser));
19104 /* If the next token is an ellipsis, and we have not seen a
19105 declarator name, and the type of the declarator contains parameter
19106 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19107 a parameter pack expansion expression. Otherwise, leave the
19108 ellipsis for a C-style variadic function. */
19109 token = cp_lexer_peek_token (parser->lexer);
19110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19112 tree type = decl_specifiers.type;
19114 if (type && DECL_P (type))
19115 type = TREE_TYPE (type);
19117 if (type
19118 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19119 && declarator_can_be_parameter_pack (declarator)
19120 && (!declarator || !declarator->parameter_pack_p)
19121 && uses_parameter_packs (type))
19123 /* Consume the `...'. */
19124 cp_lexer_consume_token (parser->lexer);
19125 maybe_warn_variadic_templates ();
19127 /* Build a pack expansion type */
19128 if (declarator)
19129 declarator->parameter_pack_p = true;
19130 else
19131 decl_specifiers.type = make_pack_expansion (type);
19135 /* The restriction on defining new types applies only to the type
19136 of the parameter, not to the default argument. */
19137 parser->type_definition_forbidden_message = saved_message;
19139 /* If the next token is `=', then process a default argument. */
19140 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19142 token = cp_lexer_peek_token (parser->lexer);
19143 /* If we are defining a class, then the tokens that make up the
19144 default argument must be saved and processed later. */
19145 if (!template_parm_p && at_class_scope_p ()
19146 && TYPE_BEING_DEFINED (current_class_type)
19147 && !LAMBDA_TYPE_P (current_class_type))
19148 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19149 /* Outside of a class definition, we can just parse the
19150 assignment-expression. */
19151 else
19152 default_argument
19153 = cp_parser_default_argument (parser, template_parm_p);
19155 if (!parser->default_arg_ok_p)
19157 if (flag_permissive)
19158 warning (0, "deprecated use of default argument for parameter of non-function");
19159 else
19161 error_at (token->location,
19162 "default arguments are only "
19163 "permitted for function parameters");
19164 default_argument = NULL_TREE;
19167 else if ((declarator && declarator->parameter_pack_p)
19168 || (decl_specifiers.type
19169 && PACK_EXPANSION_P (decl_specifiers.type)))
19171 /* Find the name of the parameter pack. */
19172 cp_declarator *id_declarator = declarator;
19173 while (id_declarator && id_declarator->kind != cdk_id)
19174 id_declarator = id_declarator->declarator;
19176 if (id_declarator && id_declarator->kind == cdk_id)
19177 error_at (declarator_token_start->location,
19178 template_parm_p
19179 ? G_("template parameter pack %qD "
19180 "cannot have a default argument")
19181 : G_("parameter pack %qD cannot have "
19182 "a default argument"),
19183 id_declarator->u.id.unqualified_name);
19184 else
19185 error_at (declarator_token_start->location,
19186 template_parm_p
19187 ? G_("template parameter pack cannot have "
19188 "a default argument")
19189 : G_("parameter pack cannot have a "
19190 "default argument"));
19192 default_argument = NULL_TREE;
19195 else
19196 default_argument = NULL_TREE;
19198 return make_parameter_declarator (&decl_specifiers,
19199 declarator,
19200 default_argument);
19203 /* Parse a default argument and return it.
19205 TEMPLATE_PARM_P is true if this is a default argument for a
19206 non-type template parameter. */
19207 static tree
19208 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19210 tree default_argument = NULL_TREE;
19211 bool saved_greater_than_is_operator_p;
19212 bool saved_local_variables_forbidden_p;
19213 bool non_constant_p, is_direct_init;
19215 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19216 set correctly. */
19217 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19218 parser->greater_than_is_operator_p = !template_parm_p;
19219 /* Local variable names (and the `this' keyword) may not
19220 appear in a default argument. */
19221 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19222 parser->local_variables_forbidden_p = true;
19223 /* Parse the assignment-expression. */
19224 if (template_parm_p)
19225 push_deferring_access_checks (dk_no_deferred);
19226 tree saved_class_ptr = NULL_TREE;
19227 tree saved_class_ref = NULL_TREE;
19228 /* The "this" pointer is not valid in a default argument. */
19229 if (cfun)
19231 saved_class_ptr = current_class_ptr;
19232 cp_function_chain->x_current_class_ptr = NULL_TREE;
19233 saved_class_ref = current_class_ref;
19234 cp_function_chain->x_current_class_ref = NULL_TREE;
19236 default_argument
19237 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19238 /* Restore the "this" pointer. */
19239 if (cfun)
19241 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19242 cp_function_chain->x_current_class_ref = saved_class_ref;
19244 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19246 if (template_parm_p)
19247 pop_deferring_access_checks ();
19248 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19249 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19251 return default_argument;
19254 /* Parse a function-body.
19256 function-body:
19257 compound_statement */
19259 static void
19260 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19262 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19265 /* Parse a ctor-initializer-opt followed by a function-body. Return
19266 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19267 is true we are parsing a function-try-block. */
19269 static bool
19270 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19271 bool in_function_try_block)
19273 tree body, list;
19274 bool ctor_initializer_p;
19275 const bool check_body_p =
19276 DECL_CONSTRUCTOR_P (current_function_decl)
19277 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19278 tree last = NULL;
19280 /* Begin the function body. */
19281 body = begin_function_body ();
19282 /* Parse the optional ctor-initializer. */
19283 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19285 /* If we're parsing a constexpr constructor definition, we need
19286 to check that the constructor body is indeed empty. However,
19287 before we get to cp_parser_function_body lot of junk has been
19288 generated, so we can't just check that we have an empty block.
19289 Rather we take a snapshot of the outermost block, and check whether
19290 cp_parser_function_body changed its state. */
19291 if (check_body_p)
19293 list = cur_stmt_list;
19294 if (STATEMENT_LIST_TAIL (list))
19295 last = STATEMENT_LIST_TAIL (list)->stmt;
19297 /* Parse the function-body. */
19298 cp_parser_function_body (parser, in_function_try_block);
19299 if (check_body_p)
19300 check_constexpr_ctor_body (last, list, /*complain=*/true);
19301 /* Finish the function body. */
19302 finish_function_body (body);
19304 return ctor_initializer_p;
19307 /* Parse an initializer.
19309 initializer:
19310 = initializer-clause
19311 ( expression-list )
19313 Returns an expression representing the initializer. If no
19314 initializer is present, NULL_TREE is returned.
19316 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19317 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19318 set to TRUE if there is no initializer present. If there is an
19319 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19320 is set to true; otherwise it is set to false. */
19322 static tree
19323 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19324 bool* non_constant_p)
19326 cp_token *token;
19327 tree init;
19329 /* Peek at the next token. */
19330 token = cp_lexer_peek_token (parser->lexer);
19332 /* Let our caller know whether or not this initializer was
19333 parenthesized. */
19334 *is_direct_init = (token->type != CPP_EQ);
19335 /* Assume that the initializer is constant. */
19336 *non_constant_p = false;
19338 if (token->type == CPP_EQ)
19340 /* Consume the `='. */
19341 cp_lexer_consume_token (parser->lexer);
19342 /* Parse the initializer-clause. */
19343 init = cp_parser_initializer_clause (parser, non_constant_p);
19345 else if (token->type == CPP_OPEN_PAREN)
19347 vec<tree, va_gc> *vec;
19348 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19349 /*cast_p=*/false,
19350 /*allow_expansion_p=*/true,
19351 non_constant_p);
19352 if (vec == NULL)
19353 return error_mark_node;
19354 init = build_tree_list_vec (vec);
19355 release_tree_vector (vec);
19357 else if (token->type == CPP_OPEN_BRACE)
19359 cp_lexer_set_source_position (parser->lexer);
19360 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19361 init = cp_parser_braced_list (parser, non_constant_p);
19362 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19364 else
19366 /* Anything else is an error. */
19367 cp_parser_error (parser, "expected initializer");
19368 init = error_mark_node;
19371 return init;
19374 /* Parse an initializer-clause.
19376 initializer-clause:
19377 assignment-expression
19378 braced-init-list
19380 Returns an expression representing the initializer.
19382 If the `assignment-expression' production is used the value
19383 returned is simply a representation for the expression.
19385 Otherwise, calls cp_parser_braced_list. */
19387 static tree
19388 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19390 tree initializer;
19392 /* Assume the expression is constant. */
19393 *non_constant_p = false;
19395 /* If it is not a `{', then we are looking at an
19396 assignment-expression. */
19397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19399 initializer
19400 = cp_parser_constant_expression (parser,
19401 /*allow_non_constant_p=*/true,
19402 non_constant_p);
19404 else
19405 initializer = cp_parser_braced_list (parser, non_constant_p);
19407 return initializer;
19410 /* Parse a brace-enclosed initializer list.
19412 braced-init-list:
19413 { initializer-list , [opt] }
19416 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19417 the elements of the initializer-list (or NULL, if the last
19418 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19419 NULL_TREE. There is no way to detect whether or not the optional
19420 trailing `,' was provided. NON_CONSTANT_P is as for
19421 cp_parser_initializer. */
19423 static tree
19424 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19426 tree initializer;
19428 /* Consume the `{' token. */
19429 cp_lexer_consume_token (parser->lexer);
19430 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19431 initializer = make_node (CONSTRUCTOR);
19432 /* If it's not a `}', then there is a non-trivial initializer. */
19433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19435 /* Parse the initializer list. */
19436 CONSTRUCTOR_ELTS (initializer)
19437 = cp_parser_initializer_list (parser, non_constant_p);
19438 /* A trailing `,' token is allowed. */
19439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19440 cp_lexer_consume_token (parser->lexer);
19442 else
19443 *non_constant_p = false;
19444 /* Now, there should be a trailing `}'. */
19445 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19446 TREE_TYPE (initializer) = init_list_type_node;
19447 return initializer;
19450 /* Consume tokens up to, and including, the next non-nested closing `]'.
19451 Returns true iff we found a closing `]'. */
19453 static bool
19454 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19456 unsigned square_depth = 0;
19458 while (true)
19460 cp_token * token = cp_lexer_peek_token (parser->lexer);
19462 switch (token->type)
19464 case CPP_EOF:
19465 case CPP_PRAGMA_EOL:
19466 /* If we've run out of tokens, then there is no closing `]'. */
19467 return false;
19469 case CPP_OPEN_SQUARE:
19470 ++square_depth;
19471 break;
19473 case CPP_CLOSE_SQUARE:
19474 if (!square_depth--)
19476 cp_lexer_consume_token (parser->lexer);
19477 return true;
19479 break;
19481 default:
19482 break;
19485 /* Consume the token. */
19486 cp_lexer_consume_token (parser->lexer);
19490 /* Return true if we are looking at an array-designator, false otherwise. */
19492 static bool
19493 cp_parser_array_designator_p (cp_parser *parser)
19495 /* Consume the `['. */
19496 cp_lexer_consume_token (parser->lexer);
19498 cp_lexer_save_tokens (parser->lexer);
19500 /* Skip tokens until the next token is a closing square bracket.
19501 If we find the closing `]', and the next token is a `=', then
19502 we are looking at an array designator. */
19503 bool array_designator_p
19504 = (cp_parser_skip_to_closing_square_bracket (parser)
19505 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19507 /* Roll back the tokens we skipped. */
19508 cp_lexer_rollback_tokens (parser->lexer);
19510 return array_designator_p;
19513 /* Parse an initializer-list.
19515 initializer-list:
19516 initializer-clause ... [opt]
19517 initializer-list , initializer-clause ... [opt]
19519 GNU Extension:
19521 initializer-list:
19522 designation initializer-clause ...[opt]
19523 initializer-list , designation initializer-clause ...[opt]
19525 designation:
19526 . identifier =
19527 identifier :
19528 [ constant-expression ] =
19530 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19531 for the initializer. If the INDEX of the elt is non-NULL, it is the
19532 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19533 as for cp_parser_initializer. */
19535 static vec<constructor_elt, va_gc> *
19536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19538 vec<constructor_elt, va_gc> *v = NULL;
19540 /* Assume all of the expressions are constant. */
19541 *non_constant_p = false;
19543 /* Parse the rest of the list. */
19544 while (true)
19546 cp_token *token;
19547 tree designator;
19548 tree initializer;
19549 bool clause_non_constant_p;
19551 /* If the next token is an identifier and the following one is a
19552 colon, we are looking at the GNU designated-initializer
19553 syntax. */
19554 if (cp_parser_allow_gnu_extensions_p (parser)
19555 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19558 /* Warn the user that they are using an extension. */
19559 pedwarn (input_location, OPT_Wpedantic,
19560 "ISO C++ does not allow designated initializers");
19561 /* Consume the identifier. */
19562 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19563 /* Consume the `:'. */
19564 cp_lexer_consume_token (parser->lexer);
19566 /* Also handle the C99 syntax, '. id ='. */
19567 else if (cp_parser_allow_gnu_extensions_p (parser)
19568 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19569 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19570 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19572 /* Warn the user that they are using an extension. */
19573 pedwarn (input_location, OPT_Wpedantic,
19574 "ISO C++ does not allow C99 designated initializers");
19575 /* Consume the `.'. */
19576 cp_lexer_consume_token (parser->lexer);
19577 /* Consume the identifier. */
19578 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19579 /* Consume the `='. */
19580 cp_lexer_consume_token (parser->lexer);
19582 /* Also handle C99 array designators, '[ const ] ='. */
19583 else if (cp_parser_allow_gnu_extensions_p (parser)
19584 && !c_dialect_objc ()
19585 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19587 /* In C++11, [ could start a lambda-introducer. */
19588 bool non_const = false;
19590 cp_parser_parse_tentatively (parser);
19592 if (!cp_parser_array_designator_p (parser))
19594 cp_parser_simulate_error (parser);
19595 designator = NULL_TREE;
19597 else
19599 designator = cp_parser_constant_expression (parser, true,
19600 &non_const);
19601 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19602 cp_parser_require (parser, CPP_EQ, RT_EQ);
19605 if (!cp_parser_parse_definitely (parser))
19606 designator = NULL_TREE;
19607 else if (non_const)
19608 require_potential_rvalue_constant_expression (designator);
19610 else
19611 designator = NULL_TREE;
19613 /* Parse the initializer. */
19614 initializer = cp_parser_initializer_clause (parser,
19615 &clause_non_constant_p);
19616 /* If any clause is non-constant, so is the entire initializer. */
19617 if (clause_non_constant_p)
19618 *non_constant_p = true;
19620 /* If we have an ellipsis, this is an initializer pack
19621 expansion. */
19622 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19624 /* Consume the `...'. */
19625 cp_lexer_consume_token (parser->lexer);
19627 /* Turn the initializer into an initializer expansion. */
19628 initializer = make_pack_expansion (initializer);
19631 /* Add it to the vector. */
19632 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19634 /* If the next token is not a comma, we have reached the end of
19635 the list. */
19636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19637 break;
19639 /* Peek at the next token. */
19640 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19641 /* If the next token is a `}', then we're still done. An
19642 initializer-clause can have a trailing `,' after the
19643 initializer-list and before the closing `}'. */
19644 if (token->type == CPP_CLOSE_BRACE)
19645 break;
19647 /* Consume the `,' token. */
19648 cp_lexer_consume_token (parser->lexer);
19651 return v;
19654 /* Classes [gram.class] */
19656 /* Parse a class-name.
19658 class-name:
19659 identifier
19660 template-id
19662 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19663 to indicate that names looked up in dependent types should be
19664 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19665 keyword has been used to indicate that the name that appears next
19666 is a template. TAG_TYPE indicates the explicit tag given before
19667 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19668 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19669 is the class being defined in a class-head. If ENUM_OK is TRUE,
19670 enum-names are also accepted.
19672 Returns the TYPE_DECL representing the class. */
19674 static tree
19675 cp_parser_class_name (cp_parser *parser,
19676 bool typename_keyword_p,
19677 bool template_keyword_p,
19678 enum tag_types tag_type,
19679 bool check_dependency_p,
19680 bool class_head_p,
19681 bool is_declaration,
19682 bool enum_ok)
19684 tree decl;
19685 tree scope;
19686 bool typename_p;
19687 cp_token *token;
19688 tree identifier = NULL_TREE;
19690 /* All class-names start with an identifier. */
19691 token = cp_lexer_peek_token (parser->lexer);
19692 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19694 cp_parser_error (parser, "expected class-name");
19695 return error_mark_node;
19698 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19699 to a template-id, so we save it here. */
19700 scope = parser->scope;
19701 if (scope == error_mark_node)
19702 return error_mark_node;
19704 /* Any name names a type if we're following the `typename' keyword
19705 in a qualified name where the enclosing scope is type-dependent. */
19706 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19707 && dependent_type_p (scope));
19708 /* Handle the common case (an identifier, but not a template-id)
19709 efficiently. */
19710 if (token->type == CPP_NAME
19711 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19713 cp_token *identifier_token;
19714 bool ambiguous_p;
19716 /* Look for the identifier. */
19717 identifier_token = cp_lexer_peek_token (parser->lexer);
19718 ambiguous_p = identifier_token->error_reported;
19719 identifier = cp_parser_identifier (parser);
19720 /* If the next token isn't an identifier, we are certainly not
19721 looking at a class-name. */
19722 if (identifier == error_mark_node)
19723 decl = error_mark_node;
19724 /* If we know this is a type-name, there's no need to look it
19725 up. */
19726 else if (typename_p)
19727 decl = identifier;
19728 else
19730 tree ambiguous_decls;
19731 /* If we already know that this lookup is ambiguous, then
19732 we've already issued an error message; there's no reason
19733 to check again. */
19734 if (ambiguous_p)
19736 cp_parser_simulate_error (parser);
19737 return error_mark_node;
19739 /* If the next token is a `::', then the name must be a type
19740 name.
19742 [basic.lookup.qual]
19744 During the lookup for a name preceding the :: scope
19745 resolution operator, object, function, and enumerator
19746 names are ignored. */
19747 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19748 tag_type = typename_type;
19749 /* Look up the name. */
19750 decl = cp_parser_lookup_name (parser, identifier,
19751 tag_type,
19752 /*is_template=*/false,
19753 /*is_namespace=*/false,
19754 check_dependency_p,
19755 &ambiguous_decls,
19756 identifier_token->location);
19757 if (ambiguous_decls)
19759 if (cp_parser_parsing_tentatively (parser))
19760 cp_parser_simulate_error (parser);
19761 return error_mark_node;
19765 else
19767 /* Try a template-id. */
19768 decl = cp_parser_template_id (parser, template_keyword_p,
19769 check_dependency_p,
19770 tag_type,
19771 is_declaration);
19772 if (decl == error_mark_node)
19773 return error_mark_node;
19776 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19778 /* If this is a typename, create a TYPENAME_TYPE. */
19779 if (typename_p && decl != error_mark_node)
19781 decl = make_typename_type (scope, decl, typename_type,
19782 /*complain=*/tf_error);
19783 if (decl != error_mark_node)
19784 decl = TYPE_NAME (decl);
19787 decl = strip_using_decl (decl);
19789 /* Check to see that it is really the name of a class. */
19790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19791 && identifier_p (TREE_OPERAND (decl, 0))
19792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19793 /* Situations like this:
19795 template <typename T> struct A {
19796 typename T::template X<int>::I i;
19799 are problematic. Is `T::template X<int>' a class-name? The
19800 standard does not seem to be definitive, but there is no other
19801 valid interpretation of the following `::'. Therefore, those
19802 names are considered class-names. */
19804 decl = make_typename_type (scope, decl, tag_type, tf_error);
19805 if (decl != error_mark_node)
19806 decl = TYPE_NAME (decl);
19808 else if (TREE_CODE (decl) != TYPE_DECL
19809 || TREE_TYPE (decl) == error_mark_node
19810 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19811 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19812 /* In Objective-C 2.0, a classname followed by '.' starts a
19813 dot-syntax expression, and it's not a type-name. */
19814 || (c_dialect_objc ()
19815 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19816 && objc_is_class_name (decl)))
19817 decl = error_mark_node;
19819 if (decl == error_mark_node)
19820 cp_parser_error (parser, "expected class-name");
19821 else if (identifier && !parser->scope)
19822 maybe_note_name_used_in_class (identifier, decl);
19824 return decl;
19827 /* Parse a class-specifier.
19829 class-specifier:
19830 class-head { member-specification [opt] }
19832 Returns the TREE_TYPE representing the class. */
19834 static tree
19835 cp_parser_class_specifier_1 (cp_parser* parser)
19837 tree type;
19838 tree attributes = NULL_TREE;
19839 bool nested_name_specifier_p;
19840 unsigned saved_num_template_parameter_lists;
19841 bool saved_in_function_body;
19842 unsigned char in_statement;
19843 bool in_switch_statement_p;
19844 bool saved_in_unbraced_linkage_specification_p;
19845 tree old_scope = NULL_TREE;
19846 tree scope = NULL_TREE;
19847 cp_token *closing_brace;
19849 push_deferring_access_checks (dk_no_deferred);
19851 /* Parse the class-head. */
19852 type = cp_parser_class_head (parser,
19853 &nested_name_specifier_p);
19854 /* If the class-head was a semantic disaster, skip the entire body
19855 of the class. */
19856 if (!type)
19858 cp_parser_skip_to_end_of_block_or_statement (parser);
19859 pop_deferring_access_checks ();
19860 return error_mark_node;
19863 /* Look for the `{'. */
19864 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19866 pop_deferring_access_checks ();
19867 return error_mark_node;
19870 cp_ensure_no_omp_declare_simd (parser);
19872 /* Issue an error message if type-definitions are forbidden here. */
19873 cp_parser_check_type_definition (parser);
19874 /* Remember that we are defining one more class. */
19875 ++parser->num_classes_being_defined;
19876 /* Inside the class, surrounding template-parameter-lists do not
19877 apply. */
19878 saved_num_template_parameter_lists
19879 = parser->num_template_parameter_lists;
19880 parser->num_template_parameter_lists = 0;
19881 /* We are not in a function body. */
19882 saved_in_function_body = parser->in_function_body;
19883 parser->in_function_body = false;
19884 /* Or in a loop. */
19885 in_statement = parser->in_statement;
19886 parser->in_statement = 0;
19887 /* Or in a switch. */
19888 in_switch_statement_p = parser->in_switch_statement_p;
19889 parser->in_switch_statement_p = false;
19890 /* We are not immediately inside an extern "lang" block. */
19891 saved_in_unbraced_linkage_specification_p
19892 = parser->in_unbraced_linkage_specification_p;
19893 parser->in_unbraced_linkage_specification_p = false;
19895 /* Start the class. */
19896 if (nested_name_specifier_p)
19898 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19899 old_scope = push_inner_scope (scope);
19901 type = begin_class_definition (type);
19903 if (type == error_mark_node)
19904 /* If the type is erroneous, skip the entire body of the class. */
19905 cp_parser_skip_to_closing_brace (parser);
19906 else
19907 /* Parse the member-specification. */
19908 cp_parser_member_specification_opt (parser);
19910 /* Look for the trailing `}'. */
19911 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19912 /* Look for trailing attributes to apply to this class. */
19913 if (cp_parser_allow_gnu_extensions_p (parser))
19914 attributes = cp_parser_gnu_attributes_opt (parser);
19915 if (type != error_mark_node)
19916 type = finish_struct (type, attributes);
19917 if (nested_name_specifier_p)
19918 pop_inner_scope (old_scope, scope);
19920 /* We've finished a type definition. Check for the common syntax
19921 error of forgetting a semicolon after the definition. We need to
19922 be careful, as we can't just check for not-a-semicolon and be done
19923 with it; the user might have typed:
19925 class X { } c = ...;
19926 class X { } *p = ...;
19928 and so forth. Instead, enumerate all the possible tokens that
19929 might follow this production; if we don't see one of them, then
19930 complain and silently insert the semicolon. */
19932 cp_token *token = cp_lexer_peek_token (parser->lexer);
19933 bool want_semicolon = true;
19935 if (cp_next_tokens_can_be_std_attribute_p (parser))
19936 /* Don't try to parse c++11 attributes here. As per the
19937 grammar, that should be a task for
19938 cp_parser_decl_specifier_seq. */
19939 want_semicolon = false;
19941 switch (token->type)
19943 case CPP_NAME:
19944 case CPP_SEMICOLON:
19945 case CPP_MULT:
19946 case CPP_AND:
19947 case CPP_OPEN_PAREN:
19948 case CPP_CLOSE_PAREN:
19949 case CPP_COMMA:
19950 want_semicolon = false;
19951 break;
19953 /* While it's legal for type qualifiers and storage class
19954 specifiers to follow type definitions in the grammar, only
19955 compiler testsuites contain code like that. Assume that if
19956 we see such code, then what we're really seeing is a case
19957 like:
19959 class X { }
19960 const <type> var = ...;
19964 class Y { }
19965 static <type> func (...) ...
19967 i.e. the qualifier or specifier applies to the next
19968 declaration. To do so, however, we need to look ahead one
19969 more token to see if *that* token is a type specifier.
19971 This code could be improved to handle:
19973 class Z { }
19974 static const <type> var = ...; */
19975 case CPP_KEYWORD:
19976 if (keyword_is_decl_specifier (token->keyword))
19978 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19980 /* Handling user-defined types here would be nice, but very
19981 tricky. */
19982 want_semicolon
19983 = (lookahead->type == CPP_KEYWORD
19984 && keyword_begins_type_specifier (lookahead->keyword));
19986 break;
19987 default:
19988 break;
19991 /* If we don't have a type, then something is very wrong and we
19992 shouldn't try to do anything clever. Likewise for not seeing the
19993 closing brace. */
19994 if (closing_brace && TYPE_P (type) && want_semicolon)
19996 cp_token_position prev
19997 = cp_lexer_previous_token_position (parser->lexer);
19998 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19999 location_t loc = prev_token->location;
20001 if (CLASSTYPE_DECLARED_CLASS (type))
20002 error_at (loc, "expected %<;%> after class definition");
20003 else if (TREE_CODE (type) == RECORD_TYPE)
20004 error_at (loc, "expected %<;%> after struct definition");
20005 else if (TREE_CODE (type) == UNION_TYPE)
20006 error_at (loc, "expected %<;%> after union definition");
20007 else
20008 gcc_unreachable ();
20010 /* Unget one token and smash it to look as though we encountered
20011 a semicolon in the input stream. */
20012 cp_lexer_set_token_position (parser->lexer, prev);
20013 token = cp_lexer_peek_token (parser->lexer);
20014 token->type = CPP_SEMICOLON;
20015 token->keyword = RID_MAX;
20019 /* If this class is not itself within the scope of another class,
20020 then we need to parse the bodies of all of the queued function
20021 definitions. Note that the queued functions defined in a class
20022 are not always processed immediately following the
20023 class-specifier for that class. Consider:
20025 struct A {
20026 struct B { void f() { sizeof (A); } };
20029 If `f' were processed before the processing of `A' were
20030 completed, there would be no way to compute the size of `A'.
20031 Note that the nesting we are interested in here is lexical --
20032 not the semantic nesting given by TYPE_CONTEXT. In particular,
20033 for:
20035 struct A { struct B; };
20036 struct A::B { void f() { } };
20038 there is no need to delay the parsing of `A::B::f'. */
20039 if (--parser->num_classes_being_defined == 0)
20041 tree decl;
20042 tree class_type = NULL_TREE;
20043 tree pushed_scope = NULL_TREE;
20044 unsigned ix;
20045 cp_default_arg_entry *e;
20046 tree save_ccp, save_ccr;
20048 /* In a first pass, parse default arguments to the functions.
20049 Then, in a second pass, parse the bodies of the functions.
20050 This two-phased approach handles cases like:
20052 struct S {
20053 void f() { g(); }
20054 void g(int i = 3);
20058 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20060 decl = e->decl;
20061 /* If there are default arguments that have not yet been processed,
20062 take care of them now. */
20063 if (class_type != e->class_type)
20065 if (pushed_scope)
20066 pop_scope (pushed_scope);
20067 class_type = e->class_type;
20068 pushed_scope = push_scope (class_type);
20070 /* Make sure that any template parameters are in scope. */
20071 maybe_begin_member_template_processing (decl);
20072 /* Parse the default argument expressions. */
20073 cp_parser_late_parsing_default_args (parser, decl);
20074 /* Remove any template parameters from the symbol table. */
20075 maybe_end_member_template_processing ();
20077 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20078 /* Now parse any NSDMIs. */
20079 save_ccp = current_class_ptr;
20080 save_ccr = current_class_ref;
20081 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20083 if (class_type != DECL_CONTEXT (decl))
20085 if (pushed_scope)
20086 pop_scope (pushed_scope);
20087 class_type = DECL_CONTEXT (decl);
20088 pushed_scope = push_scope (class_type);
20090 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20091 cp_parser_late_parsing_nsdmi (parser, decl);
20093 vec_safe_truncate (unparsed_nsdmis, 0);
20094 current_class_ptr = save_ccp;
20095 current_class_ref = save_ccr;
20096 if (pushed_scope)
20097 pop_scope (pushed_scope);
20099 /* Now do some post-NSDMI bookkeeping. */
20100 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20101 after_nsdmi_defaulted_late_checks (class_type);
20102 vec_safe_truncate (unparsed_classes, 0);
20103 after_nsdmi_defaulted_late_checks (type);
20105 /* Now parse the body of the functions. */
20106 if (flag_openmp)
20108 /* OpenMP UDRs need to be parsed before all other functions. */
20109 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20110 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20111 cp_parser_late_parsing_for_member (parser, decl);
20112 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20113 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20114 cp_parser_late_parsing_for_member (parser, decl);
20116 else
20117 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20118 cp_parser_late_parsing_for_member (parser, decl);
20119 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20121 else
20122 vec_safe_push (unparsed_classes, type);
20124 /* Put back any saved access checks. */
20125 pop_deferring_access_checks ();
20127 /* Restore saved state. */
20128 parser->in_switch_statement_p = in_switch_statement_p;
20129 parser->in_statement = in_statement;
20130 parser->in_function_body = saved_in_function_body;
20131 parser->num_template_parameter_lists
20132 = saved_num_template_parameter_lists;
20133 parser->in_unbraced_linkage_specification_p
20134 = saved_in_unbraced_linkage_specification_p;
20136 return type;
20139 static tree
20140 cp_parser_class_specifier (cp_parser* parser)
20142 tree ret;
20143 timevar_push (TV_PARSE_STRUCT);
20144 ret = cp_parser_class_specifier_1 (parser);
20145 timevar_pop (TV_PARSE_STRUCT);
20146 return ret;
20149 /* Parse a class-head.
20151 class-head:
20152 class-key identifier [opt] base-clause [opt]
20153 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20154 class-key nested-name-specifier [opt] template-id
20155 base-clause [opt]
20157 class-virt-specifier:
20158 final
20160 GNU Extensions:
20161 class-key attributes identifier [opt] base-clause [opt]
20162 class-key attributes nested-name-specifier identifier base-clause [opt]
20163 class-key attributes nested-name-specifier [opt] template-id
20164 base-clause [opt]
20166 Upon return BASES is initialized to the list of base classes (or
20167 NULL, if there are none) in the same form returned by
20168 cp_parser_base_clause.
20170 Returns the TYPE of the indicated class. Sets
20171 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20172 involving a nested-name-specifier was used, and FALSE otherwise.
20174 Returns error_mark_node if this is not a class-head.
20176 Returns NULL_TREE if the class-head is syntactically valid, but
20177 semantically invalid in a way that means we should skip the entire
20178 body of the class. */
20180 static tree
20181 cp_parser_class_head (cp_parser* parser,
20182 bool* nested_name_specifier_p)
20184 tree nested_name_specifier;
20185 enum tag_types class_key;
20186 tree id = NULL_TREE;
20187 tree type = NULL_TREE;
20188 tree attributes;
20189 tree bases;
20190 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20191 bool template_id_p = false;
20192 bool qualified_p = false;
20193 bool invalid_nested_name_p = false;
20194 bool invalid_explicit_specialization_p = false;
20195 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20196 tree pushed_scope = NULL_TREE;
20197 unsigned num_templates;
20198 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20199 /* Assume no nested-name-specifier will be present. */
20200 *nested_name_specifier_p = false;
20201 /* Assume no template parameter lists will be used in defining the
20202 type. */
20203 num_templates = 0;
20204 parser->colon_corrects_to_scope_p = false;
20206 /* Look for the class-key. */
20207 class_key = cp_parser_class_key (parser);
20208 if (class_key == none_type)
20209 return error_mark_node;
20211 /* Parse the attributes. */
20212 attributes = cp_parser_attributes_opt (parser);
20214 /* If the next token is `::', that is invalid -- but sometimes
20215 people do try to write:
20217 struct ::S {};
20219 Handle this gracefully by accepting the extra qualifier, and then
20220 issuing an error about it later if this really is a
20221 class-head. If it turns out just to be an elaborated type
20222 specifier, remain silent. */
20223 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20224 qualified_p = true;
20226 push_deferring_access_checks (dk_no_check);
20228 /* Determine the name of the class. Begin by looking for an
20229 optional nested-name-specifier. */
20230 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20231 nested_name_specifier
20232 = cp_parser_nested_name_specifier_opt (parser,
20233 /*typename_keyword_p=*/false,
20234 /*check_dependency_p=*/false,
20235 /*type_p=*/true,
20236 /*is_declaration=*/false);
20237 /* If there was a nested-name-specifier, then there *must* be an
20238 identifier. */
20239 if (nested_name_specifier)
20241 type_start_token = cp_lexer_peek_token (parser->lexer);
20242 /* Although the grammar says `identifier', it really means
20243 `class-name' or `template-name'. You are only allowed to
20244 define a class that has already been declared with this
20245 syntax.
20247 The proposed resolution for Core Issue 180 says that wherever
20248 you see `class T::X' you should treat `X' as a type-name.
20250 It is OK to define an inaccessible class; for example:
20252 class A { class B; };
20253 class A::B {};
20255 We do not know if we will see a class-name, or a
20256 template-name. We look for a class-name first, in case the
20257 class-name is a template-id; if we looked for the
20258 template-name first we would stop after the template-name. */
20259 cp_parser_parse_tentatively (parser);
20260 type = cp_parser_class_name (parser,
20261 /*typename_keyword_p=*/false,
20262 /*template_keyword_p=*/false,
20263 class_type,
20264 /*check_dependency_p=*/false,
20265 /*class_head_p=*/true,
20266 /*is_declaration=*/false);
20267 /* If that didn't work, ignore the nested-name-specifier. */
20268 if (!cp_parser_parse_definitely (parser))
20270 invalid_nested_name_p = true;
20271 type_start_token = cp_lexer_peek_token (parser->lexer);
20272 id = cp_parser_identifier (parser);
20273 if (id == error_mark_node)
20274 id = NULL_TREE;
20276 /* If we could not find a corresponding TYPE, treat this
20277 declaration like an unqualified declaration. */
20278 if (type == error_mark_node)
20279 nested_name_specifier = NULL_TREE;
20280 /* Otherwise, count the number of templates used in TYPE and its
20281 containing scopes. */
20282 else
20284 tree scope;
20286 for (scope = TREE_TYPE (type);
20287 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20288 scope = get_containing_scope (scope))
20289 if (TYPE_P (scope)
20290 && CLASS_TYPE_P (scope)
20291 && CLASSTYPE_TEMPLATE_INFO (scope)
20292 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20293 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20294 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20295 ++num_templates;
20298 /* Otherwise, the identifier is optional. */
20299 else
20301 /* We don't know whether what comes next is a template-id,
20302 an identifier, or nothing at all. */
20303 cp_parser_parse_tentatively (parser);
20304 /* Check for a template-id. */
20305 type_start_token = cp_lexer_peek_token (parser->lexer);
20306 id = cp_parser_template_id (parser,
20307 /*template_keyword_p=*/false,
20308 /*check_dependency_p=*/true,
20309 class_key,
20310 /*is_declaration=*/true);
20311 /* If that didn't work, it could still be an identifier. */
20312 if (!cp_parser_parse_definitely (parser))
20314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20316 type_start_token = cp_lexer_peek_token (parser->lexer);
20317 id = cp_parser_identifier (parser);
20319 else
20320 id = NULL_TREE;
20322 else
20324 template_id_p = true;
20325 ++num_templates;
20329 pop_deferring_access_checks ();
20331 if (id)
20333 cp_parser_check_for_invalid_template_id (parser, id,
20334 class_key,
20335 type_start_token->location);
20337 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20339 /* If it's not a `:' or a `{' then we can't really be looking at a
20340 class-head, since a class-head only appears as part of a
20341 class-specifier. We have to detect this situation before calling
20342 xref_tag, since that has irreversible side-effects. */
20343 if (!cp_parser_next_token_starts_class_definition_p (parser))
20345 cp_parser_error (parser, "expected %<{%> or %<:%>");
20346 type = error_mark_node;
20347 goto out;
20350 /* At this point, we're going ahead with the class-specifier, even
20351 if some other problem occurs. */
20352 cp_parser_commit_to_tentative_parse (parser);
20353 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20355 cp_parser_error (parser,
20356 "cannot specify %<override%> for a class");
20357 type = error_mark_node;
20358 goto out;
20360 /* Issue the error about the overly-qualified name now. */
20361 if (qualified_p)
20363 cp_parser_error (parser,
20364 "global qualification of class name is invalid");
20365 type = error_mark_node;
20366 goto out;
20368 else if (invalid_nested_name_p)
20370 cp_parser_error (parser,
20371 "qualified name does not name a class");
20372 type = error_mark_node;
20373 goto out;
20375 else if (nested_name_specifier)
20377 tree scope;
20379 /* Reject typedef-names in class heads. */
20380 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20382 error_at (type_start_token->location,
20383 "invalid class name in declaration of %qD",
20384 type);
20385 type = NULL_TREE;
20386 goto done;
20389 /* Figure out in what scope the declaration is being placed. */
20390 scope = current_scope ();
20391 /* If that scope does not contain the scope in which the
20392 class was originally declared, the program is invalid. */
20393 if (scope && !is_ancestor (scope, nested_name_specifier))
20395 if (at_namespace_scope_p ())
20396 error_at (type_start_token->location,
20397 "declaration of %qD in namespace %qD which does not "
20398 "enclose %qD",
20399 type, scope, nested_name_specifier);
20400 else
20401 error_at (type_start_token->location,
20402 "declaration of %qD in %qD which does not enclose %qD",
20403 type, scope, nested_name_specifier);
20404 type = NULL_TREE;
20405 goto done;
20407 /* [dcl.meaning]
20409 A declarator-id shall not be qualified except for the
20410 definition of a ... nested class outside of its class
20411 ... [or] the definition or explicit instantiation of a
20412 class member of a namespace outside of its namespace. */
20413 if (scope == nested_name_specifier)
20415 permerror (nested_name_specifier_token_start->location,
20416 "extra qualification not allowed");
20417 nested_name_specifier = NULL_TREE;
20418 num_templates = 0;
20421 /* An explicit-specialization must be preceded by "template <>". If
20422 it is not, try to recover gracefully. */
20423 if (at_namespace_scope_p ()
20424 && parser->num_template_parameter_lists == 0
20425 && template_id_p)
20427 error_at (type_start_token->location,
20428 "an explicit specialization must be preceded by %<template <>%>");
20429 invalid_explicit_specialization_p = true;
20430 /* Take the same action that would have been taken by
20431 cp_parser_explicit_specialization. */
20432 ++parser->num_template_parameter_lists;
20433 begin_specialization ();
20435 /* There must be no "return" statements between this point and the
20436 end of this function; set "type "to the correct return value and
20437 use "goto done;" to return. */
20438 /* Make sure that the right number of template parameters were
20439 present. */
20440 if (!cp_parser_check_template_parameters (parser, num_templates,
20441 type_start_token->location,
20442 /*declarator=*/NULL))
20444 /* If something went wrong, there is no point in even trying to
20445 process the class-definition. */
20446 type = NULL_TREE;
20447 goto done;
20450 /* Look up the type. */
20451 if (template_id_p)
20453 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20454 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20455 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20457 error_at (type_start_token->location,
20458 "function template %qD redeclared as a class template", id);
20459 type = error_mark_node;
20461 else
20463 type = TREE_TYPE (id);
20464 type = maybe_process_partial_specialization (type);
20466 if (nested_name_specifier)
20467 pushed_scope = push_scope (nested_name_specifier);
20469 else if (nested_name_specifier)
20471 tree class_type;
20473 /* Given:
20475 template <typename T> struct S { struct T };
20476 template <typename T> struct S<T>::T { };
20478 we will get a TYPENAME_TYPE when processing the definition of
20479 `S::T'. We need to resolve it to the actual type before we
20480 try to define it. */
20481 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20483 class_type = resolve_typename_type (TREE_TYPE (type),
20484 /*only_current_p=*/false);
20485 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20486 type = TYPE_NAME (class_type);
20487 else
20489 cp_parser_error (parser, "could not resolve typename type");
20490 type = error_mark_node;
20494 if (maybe_process_partial_specialization (TREE_TYPE (type))
20495 == error_mark_node)
20497 type = NULL_TREE;
20498 goto done;
20501 class_type = current_class_type;
20502 /* Enter the scope indicated by the nested-name-specifier. */
20503 pushed_scope = push_scope (nested_name_specifier);
20504 /* Get the canonical version of this type. */
20505 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20506 /* Call push_template_decl if it seems like we should be defining a
20507 template either from the template headers or the type we're
20508 defining, so that we diagnose both extra and missing headers. */
20509 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20510 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20511 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20513 type = push_template_decl (type);
20514 if (type == error_mark_node)
20516 type = NULL_TREE;
20517 goto done;
20521 type = TREE_TYPE (type);
20522 *nested_name_specifier_p = true;
20524 else /* The name is not a nested name. */
20526 /* If the class was unnamed, create a dummy name. */
20527 if (!id)
20528 id = make_anon_name ();
20529 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20530 parser->num_template_parameter_lists);
20533 /* Indicate whether this class was declared as a `class' or as a
20534 `struct'. */
20535 if (TREE_CODE (type) == RECORD_TYPE)
20536 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20537 cp_parser_check_class_key (class_key, type);
20539 /* If this type was already complete, and we see another definition,
20540 that's an error. */
20541 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20543 error_at (type_start_token->location, "redefinition of %q#T",
20544 type);
20545 error_at (type_start_token->location, "previous definition of %q+#T",
20546 type);
20547 type = NULL_TREE;
20548 goto done;
20550 else if (type == error_mark_node)
20551 type = NULL_TREE;
20553 if (type)
20555 /* Apply attributes now, before any use of the class as a template
20556 argument in its base list. */
20557 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20558 fixup_attribute_variants (type);
20561 /* We will have entered the scope containing the class; the names of
20562 base classes should be looked up in that context. For example:
20564 struct A { struct B {}; struct C; };
20565 struct A::C : B {};
20567 is valid. */
20569 /* Get the list of base-classes, if there is one. */
20570 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20572 /* PR59482: enter the class scope so that base-specifiers are looked
20573 up correctly. */
20574 if (type)
20575 pushclass (type);
20576 bases = cp_parser_base_clause (parser);
20577 /* PR59482: get out of the previously pushed class scope so that the
20578 subsequent pops pop the right thing. */
20579 if (type)
20580 popclass ();
20582 else
20583 bases = NULL_TREE;
20585 /* If we're really defining a class, process the base classes.
20586 If they're invalid, fail. */
20587 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20588 && !xref_basetypes (type, bases))
20589 type = NULL_TREE;
20591 done:
20592 /* Leave the scope given by the nested-name-specifier. We will
20593 enter the class scope itself while processing the members. */
20594 if (pushed_scope)
20595 pop_scope (pushed_scope);
20597 if (invalid_explicit_specialization_p)
20599 end_specialization ();
20600 --parser->num_template_parameter_lists;
20603 if (type)
20604 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20605 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20606 CLASSTYPE_FINAL (type) = 1;
20607 out:
20608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20609 return type;
20612 /* Parse a class-key.
20614 class-key:
20615 class
20616 struct
20617 union
20619 Returns the kind of class-key specified, or none_type to indicate
20620 error. */
20622 static enum tag_types
20623 cp_parser_class_key (cp_parser* parser)
20625 cp_token *token;
20626 enum tag_types tag_type;
20628 /* Look for the class-key. */
20629 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20630 if (!token)
20631 return none_type;
20633 /* Check to see if the TOKEN is a class-key. */
20634 tag_type = cp_parser_token_is_class_key (token);
20635 if (!tag_type)
20636 cp_parser_error (parser, "expected class-key");
20637 return tag_type;
20640 /* Parse a type-parameter-key.
20642 type-parameter-key:
20643 class
20644 typename
20647 static void
20648 cp_parser_type_parameter_key (cp_parser* parser)
20650 /* Look for the type-parameter-key. */
20651 enum tag_types tag_type = none_type;
20652 cp_token *token = cp_lexer_peek_token (parser->lexer);
20653 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20655 cp_lexer_consume_token (parser->lexer);
20656 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20657 /* typename is not allowed in a template template parameter
20658 by the standard until C++1Z. */
20659 pedwarn (token->location, OPT_Wpedantic,
20660 "ISO C++ forbids typename key in template template parameter;"
20661 " use -std=c++1z or -std=gnu++1z");
20663 else
20664 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20666 return;
20669 /* Parse an (optional) member-specification.
20671 member-specification:
20672 member-declaration member-specification [opt]
20673 access-specifier : member-specification [opt] */
20675 static void
20676 cp_parser_member_specification_opt (cp_parser* parser)
20678 while (true)
20680 cp_token *token;
20681 enum rid keyword;
20683 /* Peek at the next token. */
20684 token = cp_lexer_peek_token (parser->lexer);
20685 /* If it's a `}', or EOF then we've seen all the members. */
20686 if (token->type == CPP_CLOSE_BRACE
20687 || token->type == CPP_EOF
20688 || token->type == CPP_PRAGMA_EOL)
20689 break;
20691 /* See if this token is a keyword. */
20692 keyword = token->keyword;
20693 switch (keyword)
20695 case RID_PUBLIC:
20696 case RID_PROTECTED:
20697 case RID_PRIVATE:
20698 /* Consume the access-specifier. */
20699 cp_lexer_consume_token (parser->lexer);
20700 /* Remember which access-specifier is active. */
20701 current_access_specifier = token->u.value;
20702 /* Look for the `:'. */
20703 cp_parser_require (parser, CPP_COLON, RT_COLON);
20704 break;
20706 default:
20707 /* Accept #pragmas at class scope. */
20708 if (token->type == CPP_PRAGMA)
20710 cp_parser_pragma (parser, pragma_member);
20711 break;
20714 /* Otherwise, the next construction must be a
20715 member-declaration. */
20716 cp_parser_member_declaration (parser);
20721 /* Parse a member-declaration.
20723 member-declaration:
20724 decl-specifier-seq [opt] member-declarator-list [opt] ;
20725 function-definition ; [opt]
20726 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20727 using-declaration
20728 template-declaration
20729 alias-declaration
20731 member-declarator-list:
20732 member-declarator
20733 member-declarator-list , member-declarator
20735 member-declarator:
20736 declarator pure-specifier [opt]
20737 declarator constant-initializer [opt]
20738 identifier [opt] : constant-expression
20740 GNU Extensions:
20742 member-declaration:
20743 __extension__ member-declaration
20745 member-declarator:
20746 declarator attributes [opt] pure-specifier [opt]
20747 declarator attributes [opt] constant-initializer [opt]
20748 identifier [opt] attributes [opt] : constant-expression
20750 C++0x Extensions:
20752 member-declaration:
20753 static_assert-declaration */
20755 static void
20756 cp_parser_member_declaration (cp_parser* parser)
20758 cp_decl_specifier_seq decl_specifiers;
20759 tree prefix_attributes;
20760 tree decl;
20761 int declares_class_or_enum;
20762 bool friend_p;
20763 cp_token *token = NULL;
20764 cp_token *decl_spec_token_start = NULL;
20765 cp_token *initializer_token_start = NULL;
20766 int saved_pedantic;
20767 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20769 /* Check for the `__extension__' keyword. */
20770 if (cp_parser_extension_opt (parser, &saved_pedantic))
20772 /* Recurse. */
20773 cp_parser_member_declaration (parser);
20774 /* Restore the old value of the PEDANTIC flag. */
20775 pedantic = saved_pedantic;
20777 return;
20780 /* Check for a template-declaration. */
20781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20783 /* An explicit specialization here is an error condition, and we
20784 expect the specialization handler to detect and report this. */
20785 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20786 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20787 cp_parser_explicit_specialization (parser);
20788 else
20789 cp_parser_template_declaration (parser, /*member_p=*/true);
20791 return;
20794 /* Check for a using-declaration. */
20795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20797 if (cxx_dialect < cxx11)
20799 /* Parse the using-declaration. */
20800 cp_parser_using_declaration (parser,
20801 /*access_declaration_p=*/false);
20802 return;
20804 else
20806 tree decl;
20807 bool alias_decl_expected;
20808 cp_parser_parse_tentatively (parser);
20809 decl = cp_parser_alias_declaration (parser);
20810 /* Note that if we actually see the '=' token after the
20811 identifier, cp_parser_alias_declaration commits the
20812 tentative parse. In that case, we really expects an
20813 alias-declaration. Otherwise, we expect a using
20814 declaration. */
20815 alias_decl_expected =
20816 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20817 cp_parser_parse_definitely (parser);
20819 if (alias_decl_expected)
20820 finish_member_declaration (decl);
20821 else
20822 cp_parser_using_declaration (parser,
20823 /*access_declaration_p=*/false);
20824 return;
20828 /* Check for @defs. */
20829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20831 tree ivar, member;
20832 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20833 ivar = ivar_chains;
20834 while (ivar)
20836 member = ivar;
20837 ivar = TREE_CHAIN (member);
20838 TREE_CHAIN (member) = NULL_TREE;
20839 finish_member_declaration (member);
20841 return;
20844 /* If the next token is `static_assert' we have a static assertion. */
20845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20847 cp_parser_static_assert (parser, /*member_p=*/true);
20848 return;
20851 parser->colon_corrects_to_scope_p = false;
20853 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20854 goto out;
20856 /* Parse the decl-specifier-seq. */
20857 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20858 cp_parser_decl_specifier_seq (parser,
20859 CP_PARSER_FLAGS_OPTIONAL,
20860 &decl_specifiers,
20861 &declares_class_or_enum);
20862 /* Check for an invalid type-name. */
20863 if (!decl_specifiers.any_type_specifiers_p
20864 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20865 goto out;
20866 /* If there is no declarator, then the decl-specifier-seq should
20867 specify a type. */
20868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20870 /* If there was no decl-specifier-seq, and the next token is a
20871 `;', then we have something like:
20873 struct S { ; };
20875 [class.mem]
20877 Each member-declaration shall declare at least one member
20878 name of the class. */
20879 if (!decl_specifiers.any_specifiers_p)
20881 cp_token *token = cp_lexer_peek_token (parser->lexer);
20882 if (!in_system_header_at (token->location))
20883 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20885 else
20887 tree type;
20889 /* See if this declaration is a friend. */
20890 friend_p = cp_parser_friend_p (&decl_specifiers);
20891 /* If there were decl-specifiers, check to see if there was
20892 a class-declaration. */
20893 type = check_tag_decl (&decl_specifiers,
20894 /*explicit_type_instantiation_p=*/false);
20895 /* Nested classes have already been added to the class, but
20896 a `friend' needs to be explicitly registered. */
20897 if (friend_p)
20899 /* If the `friend' keyword was present, the friend must
20900 be introduced with a class-key. */
20901 if (!declares_class_or_enum && cxx_dialect < cxx11)
20902 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20903 "in C++03 a class-key must be used "
20904 "when declaring a friend");
20905 /* In this case:
20907 template <typename T> struct A {
20908 friend struct A<T>::B;
20911 A<T>::B will be represented by a TYPENAME_TYPE, and
20912 therefore not recognized by check_tag_decl. */
20913 if (!type)
20915 type = decl_specifiers.type;
20916 if (type && TREE_CODE (type) == TYPE_DECL)
20917 type = TREE_TYPE (type);
20919 if (!type || !TYPE_P (type))
20920 error_at (decl_spec_token_start->location,
20921 "friend declaration does not name a class or "
20922 "function");
20923 else
20924 make_friend_class (current_class_type, type,
20925 /*complain=*/true);
20927 /* If there is no TYPE, an error message will already have
20928 been issued. */
20929 else if (!type || type == error_mark_node)
20931 /* An anonymous aggregate has to be handled specially; such
20932 a declaration really declares a data member (with a
20933 particular type), as opposed to a nested class. */
20934 else if (ANON_AGGR_TYPE_P (type))
20936 /* C++11 9.5/6. */
20937 if (decl_specifiers.storage_class != sc_none)
20938 error_at (decl_spec_token_start->location,
20939 "a storage class on an anonymous aggregate "
20940 "in class scope is not allowed");
20942 /* Remove constructors and such from TYPE, now that we
20943 know it is an anonymous aggregate. */
20944 fixup_anonymous_aggr (type);
20945 /* And make the corresponding data member. */
20946 decl = build_decl (decl_spec_token_start->location,
20947 FIELD_DECL, NULL_TREE, type);
20948 /* Add it to the class. */
20949 finish_member_declaration (decl);
20951 else
20952 cp_parser_check_access_in_redeclaration
20953 (TYPE_NAME (type),
20954 decl_spec_token_start->location);
20957 else
20959 bool assume_semicolon = false;
20961 /* Clear attributes from the decl_specifiers but keep them
20962 around as prefix attributes that apply them to the entity
20963 being declared. */
20964 prefix_attributes = decl_specifiers.attributes;
20965 decl_specifiers.attributes = NULL_TREE;
20967 /* See if these declarations will be friends. */
20968 friend_p = cp_parser_friend_p (&decl_specifiers);
20970 /* Keep going until we hit the `;' at the end of the
20971 declaration. */
20972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20974 tree attributes = NULL_TREE;
20975 tree first_attribute;
20977 /* Peek at the next token. */
20978 token = cp_lexer_peek_token (parser->lexer);
20980 /* Check for a bitfield declaration. */
20981 if (token->type == CPP_COLON
20982 || (token->type == CPP_NAME
20983 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20984 == CPP_COLON))
20986 tree identifier;
20987 tree width;
20989 /* Get the name of the bitfield. Note that we cannot just
20990 check TOKEN here because it may have been invalidated by
20991 the call to cp_lexer_peek_nth_token above. */
20992 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20993 identifier = cp_parser_identifier (parser);
20994 else
20995 identifier = NULL_TREE;
20997 /* Consume the `:' token. */
20998 cp_lexer_consume_token (parser->lexer);
20999 /* Get the width of the bitfield. */
21000 width
21001 = cp_parser_constant_expression (parser);
21003 /* Look for attributes that apply to the bitfield. */
21004 attributes = cp_parser_attributes_opt (parser);
21005 /* Remember which attributes are prefix attributes and
21006 which are not. */
21007 first_attribute = attributes;
21008 /* Combine the attributes. */
21009 attributes = chainon (prefix_attributes, attributes);
21011 /* Create the bitfield declaration. */
21012 decl = grokbitfield (identifier
21013 ? make_id_declarator (NULL_TREE,
21014 identifier,
21015 sfk_none)
21016 : NULL,
21017 &decl_specifiers,
21018 width,
21019 attributes);
21021 else
21023 cp_declarator *declarator;
21024 tree initializer;
21025 tree asm_specification;
21026 int ctor_dtor_or_conv_p;
21028 /* Parse the declarator. */
21029 declarator
21030 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21031 &ctor_dtor_or_conv_p,
21032 /*parenthesized_p=*/NULL,
21033 /*member_p=*/true,
21034 friend_p);
21036 /* If something went wrong parsing the declarator, make sure
21037 that we at least consume some tokens. */
21038 if (declarator == cp_error_declarator)
21040 /* Skip to the end of the statement. */
21041 cp_parser_skip_to_end_of_statement (parser);
21042 /* If the next token is not a semicolon, that is
21043 probably because we just skipped over the body of
21044 a function. So, we consume a semicolon if
21045 present, but do not issue an error message if it
21046 is not present. */
21047 if (cp_lexer_next_token_is (parser->lexer,
21048 CPP_SEMICOLON))
21049 cp_lexer_consume_token (parser->lexer);
21050 goto out;
21053 if (declares_class_or_enum & 2)
21054 cp_parser_check_for_definition_in_return_type
21055 (declarator, decl_specifiers.type,
21056 decl_specifiers.locations[ds_type_spec]);
21058 /* Look for an asm-specification. */
21059 asm_specification = cp_parser_asm_specification_opt (parser);
21060 /* Look for attributes that apply to the declaration. */
21061 attributes = cp_parser_attributes_opt (parser);
21062 /* Remember which attributes are prefix attributes and
21063 which are not. */
21064 first_attribute = attributes;
21065 /* Combine the attributes. */
21066 attributes = chainon (prefix_attributes, attributes);
21068 /* If it's an `=', then we have a constant-initializer or a
21069 pure-specifier. It is not correct to parse the
21070 initializer before registering the member declaration
21071 since the member declaration should be in scope while
21072 its initializer is processed. However, the rest of the
21073 front end does not yet provide an interface that allows
21074 us to handle this correctly. */
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21077 /* In [class.mem]:
21079 A pure-specifier shall be used only in the declaration of
21080 a virtual function.
21082 A member-declarator can contain a constant-initializer
21083 only if it declares a static member of integral or
21084 enumeration type.
21086 Therefore, if the DECLARATOR is for a function, we look
21087 for a pure-specifier; otherwise, we look for a
21088 constant-initializer. When we call `grokfield', it will
21089 perform more stringent semantics checks. */
21090 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21091 if (function_declarator_p (declarator)
21092 || (decl_specifiers.type
21093 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21094 && declarator->kind == cdk_id
21095 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21096 == FUNCTION_TYPE)))
21097 initializer = cp_parser_pure_specifier (parser);
21098 else if (decl_specifiers.storage_class != sc_static)
21099 initializer = cp_parser_save_nsdmi (parser);
21100 else if (cxx_dialect >= cxx11)
21102 bool nonconst;
21103 /* Don't require a constant rvalue in C++11, since we
21104 might want a reference constant. We'll enforce
21105 constancy later. */
21106 cp_lexer_consume_token (parser->lexer);
21107 /* Parse the initializer. */
21108 initializer = cp_parser_initializer_clause (parser,
21109 &nonconst);
21111 else
21112 /* Parse the initializer. */
21113 initializer = cp_parser_constant_initializer (parser);
21115 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21116 && !function_declarator_p (declarator))
21118 bool x;
21119 if (decl_specifiers.storage_class != sc_static)
21120 initializer = cp_parser_save_nsdmi (parser);
21121 else
21122 initializer = cp_parser_initializer (parser, &x, &x);
21124 /* Otherwise, there is no initializer. */
21125 else
21126 initializer = NULL_TREE;
21128 /* See if we are probably looking at a function
21129 definition. We are certainly not looking at a
21130 member-declarator. Calling `grokfield' has
21131 side-effects, so we must not do it unless we are sure
21132 that we are looking at a member-declarator. */
21133 if (cp_parser_token_starts_function_definition_p
21134 (cp_lexer_peek_token (parser->lexer)))
21136 /* The grammar does not allow a pure-specifier to be
21137 used when a member function is defined. (It is
21138 possible that this fact is an oversight in the
21139 standard, since a pure function may be defined
21140 outside of the class-specifier. */
21141 if (initializer && initializer_token_start)
21142 error_at (initializer_token_start->location,
21143 "pure-specifier on function-definition");
21144 decl = cp_parser_save_member_function_body (parser,
21145 &decl_specifiers,
21146 declarator,
21147 attributes);
21148 if (parser->fully_implicit_function_template_p)
21149 decl = finish_fully_implicit_template (parser, decl);
21150 /* If the member was not a friend, declare it here. */
21151 if (!friend_p)
21152 finish_member_declaration (decl);
21153 /* Peek at the next token. */
21154 token = cp_lexer_peek_token (parser->lexer);
21155 /* If the next token is a semicolon, consume it. */
21156 if (token->type == CPP_SEMICOLON)
21157 cp_lexer_consume_token (parser->lexer);
21158 goto out;
21160 else
21161 if (declarator->kind == cdk_function)
21162 declarator->id_loc = token->location;
21163 /* Create the declaration. */
21164 decl = grokfield (declarator, &decl_specifiers,
21165 initializer, /*init_const_expr_p=*/true,
21166 asm_specification, attributes);
21167 if (parser->fully_implicit_function_template_p)
21169 if (friend_p)
21170 finish_fully_implicit_template (parser, 0);
21171 else
21172 decl = finish_fully_implicit_template (parser, decl);
21176 cp_finalize_omp_declare_simd (parser, decl);
21178 /* Reset PREFIX_ATTRIBUTES. */
21179 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21180 attributes = TREE_CHAIN (attributes);
21181 if (attributes)
21182 TREE_CHAIN (attributes) = NULL_TREE;
21184 /* If there is any qualification still in effect, clear it
21185 now; we will be starting fresh with the next declarator. */
21186 parser->scope = NULL_TREE;
21187 parser->qualifying_scope = NULL_TREE;
21188 parser->object_scope = NULL_TREE;
21189 /* If it's a `,', then there are more declarators. */
21190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21192 cp_lexer_consume_token (parser->lexer);
21193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21195 cp_token *token = cp_lexer_previous_token (parser->lexer);
21196 error_at (token->location,
21197 "stray %<,%> at end of member declaration");
21200 /* If the next token isn't a `;', then we have a parse error. */
21201 else if (cp_lexer_next_token_is_not (parser->lexer,
21202 CPP_SEMICOLON))
21204 /* The next token might be a ways away from where the
21205 actual semicolon is missing. Find the previous token
21206 and use that for our error position. */
21207 cp_token *token = cp_lexer_previous_token (parser->lexer);
21208 error_at (token->location,
21209 "expected %<;%> at end of member declaration");
21211 /* Assume that the user meant to provide a semicolon. If
21212 we were to cp_parser_skip_to_end_of_statement, we might
21213 skip to a semicolon inside a member function definition
21214 and issue nonsensical error messages. */
21215 assume_semicolon = true;
21218 if (decl)
21220 /* Add DECL to the list of members. */
21221 if (!friend_p
21222 /* Explicitly include, eg, NSDMIs, for better error
21223 recovery (c++/58650). */
21224 || !DECL_DECLARES_FUNCTION_P (decl))
21225 finish_member_declaration (decl);
21227 if (TREE_CODE (decl) == FUNCTION_DECL)
21228 cp_parser_save_default_args (parser, decl);
21229 else if (TREE_CODE (decl) == FIELD_DECL
21230 && !DECL_C_BIT_FIELD (decl)
21231 && DECL_INITIAL (decl))
21232 /* Add DECL to the queue of NSDMI to be parsed later. */
21233 vec_safe_push (unparsed_nsdmis, decl);
21236 if (assume_semicolon)
21237 goto out;
21241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21242 out:
21243 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21246 /* Parse a pure-specifier.
21248 pure-specifier:
21251 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21252 Otherwise, ERROR_MARK_NODE is returned. */
21254 static tree
21255 cp_parser_pure_specifier (cp_parser* parser)
21257 cp_token *token;
21259 /* Look for the `=' token. */
21260 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21261 return error_mark_node;
21262 /* Look for the `0' token. */
21263 token = cp_lexer_peek_token (parser->lexer);
21265 if (token->type == CPP_EOF
21266 || token->type == CPP_PRAGMA_EOL)
21267 return error_mark_node;
21269 cp_lexer_consume_token (parser->lexer);
21271 /* Accept = default or = delete in c++0x mode. */
21272 if (token->keyword == RID_DEFAULT
21273 || token->keyword == RID_DELETE)
21275 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21276 return token->u.value;
21279 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21280 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21282 cp_parser_error (parser,
21283 "invalid pure specifier (only %<= 0%> is allowed)");
21284 cp_parser_skip_to_end_of_statement (parser);
21285 return error_mark_node;
21287 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21289 error_at (token->location, "templates may not be %<virtual%>");
21290 return error_mark_node;
21293 return integer_zero_node;
21296 /* Parse a constant-initializer.
21298 constant-initializer:
21299 = constant-expression
21301 Returns a representation of the constant-expression. */
21303 static tree
21304 cp_parser_constant_initializer (cp_parser* parser)
21306 /* Look for the `=' token. */
21307 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21308 return error_mark_node;
21310 /* It is invalid to write:
21312 struct S { static const int i = { 7 }; };
21315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21317 cp_parser_error (parser,
21318 "a brace-enclosed initializer is not allowed here");
21319 /* Consume the opening brace. */
21320 cp_lexer_consume_token (parser->lexer);
21321 /* Skip the initializer. */
21322 cp_parser_skip_to_closing_brace (parser);
21323 /* Look for the trailing `}'. */
21324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21326 return error_mark_node;
21329 return cp_parser_constant_expression (parser);
21332 /* Derived classes [gram.class.derived] */
21334 /* Parse a base-clause.
21336 base-clause:
21337 : base-specifier-list
21339 base-specifier-list:
21340 base-specifier ... [opt]
21341 base-specifier-list , base-specifier ... [opt]
21343 Returns a TREE_LIST representing the base-classes, in the order in
21344 which they were declared. The representation of each node is as
21345 described by cp_parser_base_specifier.
21347 In the case that no bases are specified, this function will return
21348 NULL_TREE, not ERROR_MARK_NODE. */
21350 static tree
21351 cp_parser_base_clause (cp_parser* parser)
21353 tree bases = NULL_TREE;
21355 /* Look for the `:' that begins the list. */
21356 cp_parser_require (parser, CPP_COLON, RT_COLON);
21358 /* Scan the base-specifier-list. */
21359 while (true)
21361 cp_token *token;
21362 tree base;
21363 bool pack_expansion_p = false;
21365 /* Look for the base-specifier. */
21366 base = cp_parser_base_specifier (parser);
21367 /* Look for the (optional) ellipsis. */
21368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21370 /* Consume the `...'. */
21371 cp_lexer_consume_token (parser->lexer);
21373 pack_expansion_p = true;
21376 /* Add BASE to the front of the list. */
21377 if (base && base != error_mark_node)
21379 if (pack_expansion_p)
21380 /* Make this a pack expansion type. */
21381 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21383 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21385 TREE_CHAIN (base) = bases;
21386 bases = base;
21389 /* Peek at the next token. */
21390 token = cp_lexer_peek_token (parser->lexer);
21391 /* If it's not a comma, then the list is complete. */
21392 if (token->type != CPP_COMMA)
21393 break;
21394 /* Consume the `,'. */
21395 cp_lexer_consume_token (parser->lexer);
21398 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21399 base class had a qualified name. However, the next name that
21400 appears is certainly not qualified. */
21401 parser->scope = NULL_TREE;
21402 parser->qualifying_scope = NULL_TREE;
21403 parser->object_scope = NULL_TREE;
21405 return nreverse (bases);
21408 /* Parse a base-specifier.
21410 base-specifier:
21411 :: [opt] nested-name-specifier [opt] class-name
21412 virtual access-specifier [opt] :: [opt] nested-name-specifier
21413 [opt] class-name
21414 access-specifier virtual [opt] :: [opt] nested-name-specifier
21415 [opt] class-name
21417 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21418 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21419 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21420 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21422 static tree
21423 cp_parser_base_specifier (cp_parser* parser)
21425 cp_token *token;
21426 bool done = false;
21427 bool virtual_p = false;
21428 bool duplicate_virtual_error_issued_p = false;
21429 bool duplicate_access_error_issued_p = false;
21430 bool class_scope_p, template_p;
21431 tree access = access_default_node;
21432 tree type;
21434 /* Process the optional `virtual' and `access-specifier'. */
21435 while (!done)
21437 /* Peek at the next token. */
21438 token = cp_lexer_peek_token (parser->lexer);
21439 /* Process `virtual'. */
21440 switch (token->keyword)
21442 case RID_VIRTUAL:
21443 /* If `virtual' appears more than once, issue an error. */
21444 if (virtual_p && !duplicate_virtual_error_issued_p)
21446 cp_parser_error (parser,
21447 "%<virtual%> specified more than once in base-specified");
21448 duplicate_virtual_error_issued_p = true;
21451 virtual_p = true;
21453 /* Consume the `virtual' token. */
21454 cp_lexer_consume_token (parser->lexer);
21456 break;
21458 case RID_PUBLIC:
21459 case RID_PROTECTED:
21460 case RID_PRIVATE:
21461 /* If more than one access specifier appears, issue an
21462 error. */
21463 if (access != access_default_node
21464 && !duplicate_access_error_issued_p)
21466 cp_parser_error (parser,
21467 "more than one access specifier in base-specified");
21468 duplicate_access_error_issued_p = true;
21471 access = ridpointers[(int) token->keyword];
21473 /* Consume the access-specifier. */
21474 cp_lexer_consume_token (parser->lexer);
21476 break;
21478 default:
21479 done = true;
21480 break;
21483 /* It is not uncommon to see programs mechanically, erroneously, use
21484 the 'typename' keyword to denote (dependent) qualified types
21485 as base classes. */
21486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21488 token = cp_lexer_peek_token (parser->lexer);
21489 if (!processing_template_decl)
21490 error_at (token->location,
21491 "keyword %<typename%> not allowed outside of templates");
21492 else
21493 error_at (token->location,
21494 "keyword %<typename%> not allowed in this context "
21495 "(the base class is implicitly a type)");
21496 cp_lexer_consume_token (parser->lexer);
21499 /* Look for the optional `::' operator. */
21500 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21501 /* Look for the nested-name-specifier. The simplest way to
21502 implement:
21504 [temp.res]
21506 The keyword `typename' is not permitted in a base-specifier or
21507 mem-initializer; in these contexts a qualified name that
21508 depends on a template-parameter is implicitly assumed to be a
21509 type name.
21511 is to pretend that we have seen the `typename' keyword at this
21512 point. */
21513 cp_parser_nested_name_specifier_opt (parser,
21514 /*typename_keyword_p=*/true,
21515 /*check_dependency_p=*/true,
21516 typename_type,
21517 /*is_declaration=*/true);
21518 /* If the base class is given by a qualified name, assume that names
21519 we see are type names or templates, as appropriate. */
21520 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21521 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21523 if (!parser->scope
21524 && cp_lexer_next_token_is_decltype (parser->lexer))
21525 /* DR 950 allows decltype as a base-specifier. */
21526 type = cp_parser_decltype (parser);
21527 else
21529 /* Otherwise, look for the class-name. */
21530 type = cp_parser_class_name (parser,
21531 class_scope_p,
21532 template_p,
21533 typename_type,
21534 /*check_dependency_p=*/true,
21535 /*class_head_p=*/false,
21536 /*is_declaration=*/true);
21537 type = TREE_TYPE (type);
21540 if (type == error_mark_node)
21541 return error_mark_node;
21543 return finish_base_specifier (type, access, virtual_p);
21546 /* Exception handling [gram.exception] */
21548 /* Parse an (optional) noexcept-specification.
21550 noexcept-specification:
21551 noexcept ( constant-expression ) [opt]
21553 If no noexcept-specification is present, returns NULL_TREE.
21554 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21555 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21556 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21557 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21558 in which case a boolean condition is returned instead. */
21560 static tree
21561 cp_parser_noexcept_specification_opt (cp_parser* parser,
21562 bool require_constexpr,
21563 bool* consumed_expr,
21564 bool return_cond)
21566 cp_token *token;
21567 const char *saved_message;
21569 /* Peek at the next token. */
21570 token = cp_lexer_peek_token (parser->lexer);
21572 /* Is it a noexcept-specification? */
21573 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21575 tree expr;
21576 cp_lexer_consume_token (parser->lexer);
21578 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21580 cp_lexer_consume_token (parser->lexer);
21582 if (require_constexpr)
21584 /* Types may not be defined in an exception-specification. */
21585 saved_message = parser->type_definition_forbidden_message;
21586 parser->type_definition_forbidden_message
21587 = G_("types may not be defined in an exception-specification");
21589 expr = cp_parser_constant_expression (parser);
21591 /* Restore the saved message. */
21592 parser->type_definition_forbidden_message = saved_message;
21594 else
21596 expr = cp_parser_expression (parser);
21597 *consumed_expr = true;
21600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21602 else
21604 expr = boolean_true_node;
21605 if (!require_constexpr)
21606 *consumed_expr = false;
21609 /* We cannot build a noexcept-spec right away because this will check
21610 that expr is a constexpr. */
21611 if (!return_cond)
21612 return build_noexcept_spec (expr, tf_warning_or_error);
21613 else
21614 return expr;
21616 else
21617 return NULL_TREE;
21620 /* Parse an (optional) exception-specification.
21622 exception-specification:
21623 throw ( type-id-list [opt] )
21625 Returns a TREE_LIST representing the exception-specification. The
21626 TREE_VALUE of each node is a type. */
21628 static tree
21629 cp_parser_exception_specification_opt (cp_parser* parser)
21631 cp_token *token;
21632 tree type_id_list;
21633 const char *saved_message;
21635 /* Peek at the next token. */
21636 token = cp_lexer_peek_token (parser->lexer);
21638 /* Is it a noexcept-specification? */
21639 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21640 false);
21641 if (type_id_list != NULL_TREE)
21642 return type_id_list;
21644 /* If it's not `throw', then there's no exception-specification. */
21645 if (!cp_parser_is_keyword (token, RID_THROW))
21646 return NULL_TREE;
21648 #if 0
21649 /* Enable this once a lot of code has transitioned to noexcept? */
21650 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21651 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21652 "deprecated in C++0x; use %<noexcept%> instead");
21653 #endif
21655 /* Consume the `throw'. */
21656 cp_lexer_consume_token (parser->lexer);
21658 /* Look for the `('. */
21659 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21661 /* Peek at the next token. */
21662 token = cp_lexer_peek_token (parser->lexer);
21663 /* If it's not a `)', then there is a type-id-list. */
21664 if (token->type != CPP_CLOSE_PAREN)
21666 /* Types may not be defined in an exception-specification. */
21667 saved_message = parser->type_definition_forbidden_message;
21668 parser->type_definition_forbidden_message
21669 = G_("types may not be defined in an exception-specification");
21670 /* Parse the type-id-list. */
21671 type_id_list = cp_parser_type_id_list (parser);
21672 /* Restore the saved message. */
21673 parser->type_definition_forbidden_message = saved_message;
21675 else
21676 type_id_list = empty_except_spec;
21678 /* Look for the `)'. */
21679 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21681 return type_id_list;
21684 /* Parse an (optional) type-id-list.
21686 type-id-list:
21687 type-id ... [opt]
21688 type-id-list , type-id ... [opt]
21690 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21691 in the order that the types were presented. */
21693 static tree
21694 cp_parser_type_id_list (cp_parser* parser)
21696 tree types = NULL_TREE;
21698 while (true)
21700 cp_token *token;
21701 tree type;
21703 /* Get the next type-id. */
21704 type = cp_parser_type_id (parser);
21705 /* Parse the optional ellipsis. */
21706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21708 /* Consume the `...'. */
21709 cp_lexer_consume_token (parser->lexer);
21711 /* Turn the type into a pack expansion expression. */
21712 type = make_pack_expansion (type);
21714 /* Add it to the list. */
21715 types = add_exception_specifier (types, type, /*complain=*/1);
21716 /* Peek at the next token. */
21717 token = cp_lexer_peek_token (parser->lexer);
21718 /* If it is not a `,', we are done. */
21719 if (token->type != CPP_COMMA)
21720 break;
21721 /* Consume the `,'. */
21722 cp_lexer_consume_token (parser->lexer);
21725 return nreverse (types);
21728 /* Parse a try-block.
21730 try-block:
21731 try compound-statement handler-seq */
21733 static tree
21734 cp_parser_try_block (cp_parser* parser)
21736 tree try_block;
21738 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21739 if (parser->in_function_body
21740 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21741 error ("%<try%> in %<constexpr%> function");
21743 try_block = begin_try_block ();
21744 cp_parser_compound_statement (parser, NULL, true, false);
21745 finish_try_block (try_block);
21746 cp_parser_handler_seq (parser);
21747 finish_handler_sequence (try_block);
21749 return try_block;
21752 /* Parse a function-try-block.
21754 function-try-block:
21755 try ctor-initializer [opt] function-body handler-seq */
21757 static bool
21758 cp_parser_function_try_block (cp_parser* parser)
21760 tree compound_stmt;
21761 tree try_block;
21762 bool ctor_initializer_p;
21764 /* Look for the `try' keyword. */
21765 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21766 return false;
21767 /* Let the rest of the front end know where we are. */
21768 try_block = begin_function_try_block (&compound_stmt);
21769 /* Parse the function-body. */
21770 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21771 (parser, /*in_function_try_block=*/true);
21772 /* We're done with the `try' part. */
21773 finish_function_try_block (try_block);
21774 /* Parse the handlers. */
21775 cp_parser_handler_seq (parser);
21776 /* We're done with the handlers. */
21777 finish_function_handler_sequence (try_block, compound_stmt);
21779 return ctor_initializer_p;
21782 /* Parse a handler-seq.
21784 handler-seq:
21785 handler handler-seq [opt] */
21787 static void
21788 cp_parser_handler_seq (cp_parser* parser)
21790 while (true)
21792 cp_token *token;
21794 /* Parse the handler. */
21795 cp_parser_handler (parser);
21796 /* Peek at the next token. */
21797 token = cp_lexer_peek_token (parser->lexer);
21798 /* If it's not `catch' then there are no more handlers. */
21799 if (!cp_parser_is_keyword (token, RID_CATCH))
21800 break;
21804 /* Parse a handler.
21806 handler:
21807 catch ( exception-declaration ) compound-statement */
21809 static void
21810 cp_parser_handler (cp_parser* parser)
21812 tree handler;
21813 tree declaration;
21815 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21816 handler = begin_handler ();
21817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21818 declaration = cp_parser_exception_declaration (parser);
21819 finish_handler_parms (declaration, handler);
21820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21821 cp_parser_compound_statement (parser, NULL, false, false);
21822 finish_handler (handler);
21825 /* Parse an exception-declaration.
21827 exception-declaration:
21828 type-specifier-seq declarator
21829 type-specifier-seq abstract-declarator
21830 type-specifier-seq
21833 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21834 ellipsis variant is used. */
21836 static tree
21837 cp_parser_exception_declaration (cp_parser* parser)
21839 cp_decl_specifier_seq type_specifiers;
21840 cp_declarator *declarator;
21841 const char *saved_message;
21843 /* If it's an ellipsis, it's easy to handle. */
21844 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21846 /* Consume the `...' token. */
21847 cp_lexer_consume_token (parser->lexer);
21848 return NULL_TREE;
21851 /* Types may not be defined in exception-declarations. */
21852 saved_message = parser->type_definition_forbidden_message;
21853 parser->type_definition_forbidden_message
21854 = G_("types may not be defined in exception-declarations");
21856 /* Parse the type-specifier-seq. */
21857 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21858 /*is_trailing_return=*/false,
21859 &type_specifiers);
21860 /* If it's a `)', then there is no declarator. */
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21862 declarator = NULL;
21863 else
21864 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21865 /*ctor_dtor_or_conv_p=*/NULL,
21866 /*parenthesized_p=*/NULL,
21867 /*member_p=*/false,
21868 /*friend_p=*/false);
21870 /* Restore the saved message. */
21871 parser->type_definition_forbidden_message = saved_message;
21873 if (!type_specifiers.any_specifiers_p)
21874 return error_mark_node;
21876 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21879 /* Parse a throw-expression.
21881 throw-expression:
21882 throw assignment-expression [opt]
21884 Returns a THROW_EXPR representing the throw-expression. */
21886 static tree
21887 cp_parser_throw_expression (cp_parser* parser)
21889 tree expression;
21890 cp_token* token;
21892 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21893 token = cp_lexer_peek_token (parser->lexer);
21894 /* Figure out whether or not there is an assignment-expression
21895 following the "throw" keyword. */
21896 if (token->type == CPP_COMMA
21897 || token->type == CPP_SEMICOLON
21898 || token->type == CPP_CLOSE_PAREN
21899 || token->type == CPP_CLOSE_SQUARE
21900 || token->type == CPP_CLOSE_BRACE
21901 || token->type == CPP_COLON)
21902 expression = NULL_TREE;
21903 else
21904 expression = cp_parser_assignment_expression (parser);
21906 return build_throw (expression);
21909 /* GNU Extensions */
21911 /* Parse an (optional) asm-specification.
21913 asm-specification:
21914 asm ( string-literal )
21916 If the asm-specification is present, returns a STRING_CST
21917 corresponding to the string-literal. Otherwise, returns
21918 NULL_TREE. */
21920 static tree
21921 cp_parser_asm_specification_opt (cp_parser* parser)
21923 cp_token *token;
21924 tree asm_specification;
21926 /* Peek at the next token. */
21927 token = cp_lexer_peek_token (parser->lexer);
21928 /* If the next token isn't the `asm' keyword, then there's no
21929 asm-specification. */
21930 if (!cp_parser_is_keyword (token, RID_ASM))
21931 return NULL_TREE;
21933 /* Consume the `asm' token. */
21934 cp_lexer_consume_token (parser->lexer);
21935 /* Look for the `('. */
21936 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21938 /* Look for the string-literal. */
21939 asm_specification = cp_parser_string_literal (parser, false, false);
21941 /* Look for the `)'. */
21942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21944 return asm_specification;
21947 /* Parse an asm-operand-list.
21949 asm-operand-list:
21950 asm-operand
21951 asm-operand-list , asm-operand
21953 asm-operand:
21954 string-literal ( expression )
21955 [ string-literal ] string-literal ( expression )
21957 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21958 each node is the expression. The TREE_PURPOSE is itself a
21959 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21960 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21961 is a STRING_CST for the string literal before the parenthesis. Returns
21962 ERROR_MARK_NODE if any of the operands are invalid. */
21964 static tree
21965 cp_parser_asm_operand_list (cp_parser* parser)
21967 tree asm_operands = NULL_TREE;
21968 bool invalid_operands = false;
21970 while (true)
21972 tree string_literal;
21973 tree expression;
21974 tree name;
21976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21978 /* Consume the `[' token. */
21979 cp_lexer_consume_token (parser->lexer);
21980 /* Read the operand name. */
21981 name = cp_parser_identifier (parser);
21982 if (name != error_mark_node)
21983 name = build_string (IDENTIFIER_LENGTH (name),
21984 IDENTIFIER_POINTER (name));
21985 /* Look for the closing `]'. */
21986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21988 else
21989 name = NULL_TREE;
21990 /* Look for the string-literal. */
21991 string_literal = cp_parser_string_literal (parser, false, false);
21993 /* Look for the `('. */
21994 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21995 /* Parse the expression. */
21996 expression = cp_parser_expression (parser);
21997 /* Look for the `)'. */
21998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22000 if (name == error_mark_node
22001 || string_literal == error_mark_node
22002 || expression == error_mark_node)
22003 invalid_operands = true;
22005 /* Add this operand to the list. */
22006 asm_operands = tree_cons (build_tree_list (name, string_literal),
22007 expression,
22008 asm_operands);
22009 /* If the next token is not a `,', there are no more
22010 operands. */
22011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22012 break;
22013 /* Consume the `,'. */
22014 cp_lexer_consume_token (parser->lexer);
22017 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22020 /* Parse an asm-clobber-list.
22022 asm-clobber-list:
22023 string-literal
22024 asm-clobber-list , string-literal
22026 Returns a TREE_LIST, indicating the clobbers in the order that they
22027 appeared. The TREE_VALUE of each node is a STRING_CST. */
22029 static tree
22030 cp_parser_asm_clobber_list (cp_parser* parser)
22032 tree clobbers = NULL_TREE;
22034 while (true)
22036 tree string_literal;
22038 /* Look for the string literal. */
22039 string_literal = cp_parser_string_literal (parser, false, false);
22040 /* Add it to the list. */
22041 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22042 /* If the next token is not a `,', then the list is
22043 complete. */
22044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22045 break;
22046 /* Consume the `,' token. */
22047 cp_lexer_consume_token (parser->lexer);
22050 return clobbers;
22053 /* Parse an asm-label-list.
22055 asm-label-list:
22056 identifier
22057 asm-label-list , identifier
22059 Returns a TREE_LIST, indicating the labels in the order that they
22060 appeared. The TREE_VALUE of each node is a label. */
22062 static tree
22063 cp_parser_asm_label_list (cp_parser* parser)
22065 tree labels = NULL_TREE;
22067 while (true)
22069 tree identifier, label, name;
22071 /* Look for the identifier. */
22072 identifier = cp_parser_identifier (parser);
22073 if (!error_operand_p (identifier))
22075 label = lookup_label (identifier);
22076 if (TREE_CODE (label) == LABEL_DECL)
22078 TREE_USED (label) = 1;
22079 check_goto (label);
22080 name = build_string (IDENTIFIER_LENGTH (identifier),
22081 IDENTIFIER_POINTER (identifier));
22082 labels = tree_cons (name, label, labels);
22085 /* If the next token is not a `,', then the list is
22086 complete. */
22087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22088 break;
22089 /* Consume the `,' token. */
22090 cp_lexer_consume_token (parser->lexer);
22093 return nreverse (labels);
22096 /* Return TRUE iff the next tokens in the stream are possibly the
22097 beginning of a GNU extension attribute. */
22099 static bool
22100 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22102 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22105 /* Return TRUE iff the next tokens in the stream are possibly the
22106 beginning of a standard C++-11 attribute specifier. */
22108 static bool
22109 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22111 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22114 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22115 beginning of a standard C++-11 attribute specifier. */
22117 static bool
22118 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22120 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22122 return (cxx_dialect >= cxx11
22123 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22124 || (token->type == CPP_OPEN_SQUARE
22125 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22126 && token->type == CPP_OPEN_SQUARE)));
22129 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22130 beginning of a GNU extension attribute. */
22132 static bool
22133 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22135 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22137 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22140 /* Return true iff the next tokens can be the beginning of either a
22141 GNU attribute list, or a standard C++11 attribute sequence. */
22143 static bool
22144 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22146 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22147 || cp_next_tokens_can_be_std_attribute_p (parser));
22150 /* Return true iff the next Nth tokens can be the beginning of either
22151 a GNU attribute list, or a standard C++11 attribute sequence. */
22153 static bool
22154 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22156 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22157 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22160 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22161 of GNU attributes, or return NULL. */
22163 static tree
22164 cp_parser_attributes_opt (cp_parser *parser)
22166 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22167 return cp_parser_gnu_attributes_opt (parser);
22168 return cp_parser_std_attribute_spec_seq (parser);
22171 #define CILK_SIMD_FN_CLAUSE_MASK \
22172 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22173 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22174 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22175 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22176 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22178 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22179 vector [(<clauses>)] */
22181 static void
22182 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22184 bool first_p = parser->cilk_simd_fn_info == NULL;
22185 cp_token *token = v_token;
22186 if (first_p)
22188 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22189 parser->cilk_simd_fn_info->error_seen = false;
22190 parser->cilk_simd_fn_info->fndecl_seen = false;
22191 parser->cilk_simd_fn_info->tokens = vNULL;
22193 int paren_scope = 0;
22194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22196 cp_lexer_consume_token (parser->lexer);
22197 v_token = cp_lexer_peek_token (parser->lexer);
22198 paren_scope++;
22200 while (paren_scope > 0)
22202 token = cp_lexer_peek_token (parser->lexer);
22203 if (token->type == CPP_OPEN_PAREN)
22204 paren_scope++;
22205 else if (token->type == CPP_CLOSE_PAREN)
22206 paren_scope--;
22207 /* Do not push the last ')' */
22208 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22209 cp_lexer_consume_token (parser->lexer);
22212 token->type = CPP_PRAGMA_EOL;
22213 parser->lexer->next_token = token;
22214 cp_lexer_consume_token (parser->lexer);
22216 struct cp_token_cache *cp
22217 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22218 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22221 /* Parse an (optional) series of attributes.
22223 attributes:
22224 attributes attribute
22226 attribute:
22227 __attribute__ (( attribute-list [opt] ))
22229 The return value is as for cp_parser_gnu_attribute_list. */
22231 static tree
22232 cp_parser_gnu_attributes_opt (cp_parser* parser)
22234 tree attributes = NULL_TREE;
22236 while (true)
22238 cp_token *token;
22239 tree attribute_list;
22240 bool ok = true;
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_token (parser->lexer);
22244 /* If it's not `__attribute__', then we're done. */
22245 if (token->keyword != RID_ATTRIBUTE)
22246 break;
22248 /* Consume the `__attribute__' keyword. */
22249 cp_lexer_consume_token (parser->lexer);
22250 /* Look for the two `(' tokens. */
22251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22252 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22254 /* Peek at the next token. */
22255 token = cp_lexer_peek_token (parser->lexer);
22256 if (token->type != CPP_CLOSE_PAREN)
22257 /* Parse the attribute-list. */
22258 attribute_list = cp_parser_gnu_attribute_list (parser);
22259 else
22260 /* If the next token is a `)', then there is no attribute
22261 list. */
22262 attribute_list = NULL;
22264 /* Look for the two `)' tokens. */
22265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22266 ok = false;
22267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22268 ok = false;
22269 if (!ok)
22270 cp_parser_skip_to_end_of_statement (parser);
22272 /* Add these new attributes to the list. */
22273 attributes = chainon (attributes, attribute_list);
22276 return attributes;
22279 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22280 "__vector" or "__vector__." */
22282 static inline bool
22283 is_cilkplus_vector_p (tree name)
22285 if (flag_cilkplus && is_attribute_p ("vector", name))
22286 return true;
22287 return false;
22290 /* Parse a GNU attribute-list.
22292 attribute-list:
22293 attribute
22294 attribute-list , attribute
22296 attribute:
22297 identifier
22298 identifier ( identifier )
22299 identifier ( identifier , expression-list )
22300 identifier ( expression-list )
22302 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22303 to an attribute. The TREE_PURPOSE of each node is the identifier
22304 indicating which attribute is in use. The TREE_VALUE represents
22305 the arguments, if any. */
22307 static tree
22308 cp_parser_gnu_attribute_list (cp_parser* parser)
22310 tree attribute_list = NULL_TREE;
22311 bool save_translate_strings_p = parser->translate_strings_p;
22313 parser->translate_strings_p = false;
22314 while (true)
22316 cp_token *token;
22317 tree identifier;
22318 tree attribute;
22320 /* Look for the identifier. We also allow keywords here; for
22321 example `__attribute__ ((const))' is legal. */
22322 token = cp_lexer_peek_token (parser->lexer);
22323 if (token->type == CPP_NAME
22324 || token->type == CPP_KEYWORD)
22326 tree arguments = NULL_TREE;
22328 /* Consume the token, but save it since we need it for the
22329 SIMD enabled function parsing. */
22330 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22332 /* Save away the identifier that indicates which attribute
22333 this is. */
22334 identifier = (token->type == CPP_KEYWORD)
22335 /* For keywords, use the canonical spelling, not the
22336 parsed identifier. */
22337 ? ridpointers[(int) token->keyword]
22338 : id_token->u.value;
22340 attribute = build_tree_list (identifier, NULL_TREE);
22342 /* Peek at the next token. */
22343 token = cp_lexer_peek_token (parser->lexer);
22344 /* If it's an `(', then parse the attribute arguments. */
22345 if (token->type == CPP_OPEN_PAREN)
22347 vec<tree, va_gc> *vec;
22348 int attr_flag = (attribute_takes_identifier_p (identifier)
22349 ? id_attr : normal_attr);
22350 if (is_cilkplus_vector_p (identifier))
22352 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22353 continue;
22355 else
22356 vec = cp_parser_parenthesized_expression_list
22357 (parser, attr_flag, /*cast_p=*/false,
22358 /*allow_expansion_p=*/false,
22359 /*non_constant_p=*/NULL);
22360 if (vec == NULL)
22361 arguments = error_mark_node;
22362 else
22364 arguments = build_tree_list_vec (vec);
22365 release_tree_vector (vec);
22367 /* Save the arguments away. */
22368 TREE_VALUE (attribute) = arguments;
22370 else if (is_cilkplus_vector_p (identifier))
22372 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22373 continue;
22376 if (arguments != error_mark_node)
22378 /* Add this attribute to the list. */
22379 TREE_CHAIN (attribute) = attribute_list;
22380 attribute_list = attribute;
22383 token = cp_lexer_peek_token (parser->lexer);
22385 /* Now, look for more attributes. If the next token isn't a
22386 `,', we're done. */
22387 if (token->type != CPP_COMMA)
22388 break;
22390 /* Consume the comma and keep going. */
22391 cp_lexer_consume_token (parser->lexer);
22393 parser->translate_strings_p = save_translate_strings_p;
22395 /* We built up the list in reverse order. */
22396 return nreverse (attribute_list);
22399 /* Parse a standard C++11 attribute.
22401 The returned representation is a TREE_LIST which TREE_PURPOSE is
22402 the scoped name of the attribute, and the TREE_VALUE is its
22403 arguments list.
22405 Note that the scoped name of the attribute is itself a TREE_LIST
22406 which TREE_PURPOSE is the namespace of the attribute, and
22407 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22408 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22409 and which TREE_PURPOSE is directly the attribute name.
22411 Clients of the attribute code should use get_attribute_namespace
22412 and get_attribute_name to get the actual namespace and name of
22413 attributes, regardless of their being GNU or C++11 attributes.
22415 attribute:
22416 attribute-token attribute-argument-clause [opt]
22418 attribute-token:
22419 identifier
22420 attribute-scoped-token
22422 attribute-scoped-token:
22423 attribute-namespace :: identifier
22425 attribute-namespace:
22426 identifier
22428 attribute-argument-clause:
22429 ( balanced-token-seq )
22431 balanced-token-seq:
22432 balanced-token [opt]
22433 balanced-token-seq balanced-token
22435 balanced-token:
22436 ( balanced-token-seq )
22437 [ balanced-token-seq ]
22438 { balanced-token-seq }. */
22440 static tree
22441 cp_parser_std_attribute (cp_parser *parser)
22443 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22444 cp_token *token;
22446 /* First, parse name of the the attribute, a.k.a
22447 attribute-token. */
22449 token = cp_lexer_peek_token (parser->lexer);
22450 if (token->type == CPP_NAME)
22451 attr_id = token->u.value;
22452 else if (token->type == CPP_KEYWORD)
22453 attr_id = ridpointers[(int) token->keyword];
22454 else if (token->flags & NAMED_OP)
22455 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22457 if (attr_id == NULL_TREE)
22458 return NULL_TREE;
22460 cp_lexer_consume_token (parser->lexer);
22462 token = cp_lexer_peek_token (parser->lexer);
22463 if (token->type == CPP_SCOPE)
22465 /* We are seeing a scoped attribute token. */
22467 cp_lexer_consume_token (parser->lexer);
22468 attr_ns = attr_id;
22470 token = cp_lexer_consume_token (parser->lexer);
22471 if (token->type == CPP_NAME)
22472 attr_id = token->u.value;
22473 else if (token->type == CPP_KEYWORD)
22474 attr_id = ridpointers[(int) token->keyword];
22475 else
22477 error_at (token->location,
22478 "expected an identifier for the attribute name");
22479 return error_mark_node;
22481 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22482 NULL_TREE);
22483 token = cp_lexer_peek_token (parser->lexer);
22485 else
22487 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22488 NULL_TREE);
22489 /* C++11 noreturn attribute is equivalent to GNU's. */
22490 if (is_attribute_p ("noreturn", attr_id))
22491 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22492 /* C++14 deprecated attribute is equivalent to GNU's. */
22493 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22495 if (cxx_dialect == cxx11)
22496 pedwarn (token->location, OPT_Wpedantic,
22497 "%<deprecated%> is a C++14 feature;"
22498 " use %<gnu::deprecated%>");
22499 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22503 /* Now parse the optional argument clause of the attribute. */
22505 if (token->type != CPP_OPEN_PAREN)
22506 return attribute;
22509 vec<tree, va_gc> *vec;
22510 int attr_flag = normal_attr;
22512 if (attr_ns == get_identifier ("gnu")
22513 && attribute_takes_identifier_p (attr_id))
22514 /* A GNU attribute that takes an identifier in parameter. */
22515 attr_flag = id_attr;
22517 vec = cp_parser_parenthesized_expression_list
22518 (parser, attr_flag, /*cast_p=*/false,
22519 /*allow_expansion_p=*/true,
22520 /*non_constant_p=*/NULL);
22521 if (vec == NULL)
22522 arguments = error_mark_node;
22523 else
22525 arguments = build_tree_list_vec (vec);
22526 release_tree_vector (vec);
22529 if (arguments == error_mark_node)
22530 attribute = error_mark_node;
22531 else
22532 TREE_VALUE (attribute) = arguments;
22535 return attribute;
22538 /* 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 ("depend", p))
27687 result = PRAGMA_OMP_CLAUSE_DEPEND;
27688 else if (!strcmp ("device", p))
27689 result = PRAGMA_OMP_CLAUSE_DEVICE;
27690 else if (!strcmp ("deviceptr", p))
27691 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27692 else if (!strcmp ("dist_schedule", p))
27693 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27694 break;
27695 case 'f':
27696 if (!strcmp ("final", p))
27697 result = PRAGMA_OMP_CLAUSE_FINAL;
27698 else if (!strcmp ("firstprivate", p))
27699 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27700 else if (!strcmp ("from", p))
27701 result = PRAGMA_OMP_CLAUSE_FROM;
27702 break;
27703 case 'h':
27704 if (!strcmp ("host", p))
27705 result = PRAGMA_OACC_CLAUSE_HOST;
27706 break;
27707 case 'i':
27708 if (!strcmp ("inbranch", p))
27709 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27710 break;
27711 case 'l':
27712 if (!strcmp ("lastprivate", p))
27713 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27714 else if (!strcmp ("linear", p))
27715 result = PRAGMA_OMP_CLAUSE_LINEAR;
27716 break;
27717 case 'm':
27718 if (!strcmp ("map", p))
27719 result = PRAGMA_OMP_CLAUSE_MAP;
27720 else if (!strcmp ("mergeable", p))
27721 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27722 else if (flag_cilkplus && !strcmp ("mask", p))
27723 result = PRAGMA_CILK_CLAUSE_MASK;
27724 break;
27725 case 'n':
27726 if (!strcmp ("notinbranch", p))
27727 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27728 else if (!strcmp ("nowait", p))
27729 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27730 else if (flag_cilkplus && !strcmp ("nomask", p))
27731 result = PRAGMA_CILK_CLAUSE_NOMASK;
27732 else if (!strcmp ("num_gangs", p))
27733 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27734 else if (!strcmp ("num_teams", p))
27735 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27736 else if (!strcmp ("num_threads", p))
27737 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27738 else if (!strcmp ("num_workers", p))
27739 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27740 break;
27741 case 'o':
27742 if (!strcmp ("ordered", p))
27743 result = PRAGMA_OMP_CLAUSE_ORDERED;
27744 break;
27745 case 'p':
27746 if (!strcmp ("parallel", p))
27747 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27748 else if (!strcmp ("present", p))
27749 result = PRAGMA_OACC_CLAUSE_PRESENT;
27750 else if (!strcmp ("present_or_copy", p)
27751 || !strcmp ("pcopy", p))
27752 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27753 else if (!strcmp ("present_or_copyin", p)
27754 || !strcmp ("pcopyin", p))
27755 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27756 else if (!strcmp ("present_or_copyout", p)
27757 || !strcmp ("pcopyout", p))
27758 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27759 else if (!strcmp ("present_or_create", p)
27760 || !strcmp ("pcreate", p))
27761 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27762 else if (!strcmp ("proc_bind", p))
27763 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27764 break;
27765 case 'r':
27766 if (!strcmp ("reduction", p))
27767 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27768 break;
27769 case 's':
27770 if (!strcmp ("safelen", p))
27771 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27772 else if (!strcmp ("schedule", p))
27773 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27774 else if (!strcmp ("sections", p))
27775 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27776 else if (!strcmp ("self", p))
27777 result = PRAGMA_OACC_CLAUSE_SELF;
27778 else if (!strcmp ("shared", p))
27779 result = PRAGMA_OMP_CLAUSE_SHARED;
27780 else if (!strcmp ("simdlen", p))
27781 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27782 break;
27783 case 't':
27784 if (!strcmp ("taskgroup", p))
27785 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27786 else if (!strcmp ("thread_limit", p))
27787 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27788 else if (!strcmp ("to", p))
27789 result = PRAGMA_OMP_CLAUSE_TO;
27790 break;
27791 case 'u':
27792 if (!strcmp ("uniform", p))
27793 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27794 else if (!strcmp ("untied", p))
27795 result = PRAGMA_OMP_CLAUSE_UNTIED;
27796 break;
27797 case 'v':
27798 if (!strcmp ("vector_length", p))
27799 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27800 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27801 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27802 break;
27803 case 'w':
27804 if (!strcmp ("wait", p))
27805 result = PRAGMA_OACC_CLAUSE_WAIT;
27806 break;
27810 if (result != PRAGMA_OMP_CLAUSE_NONE)
27811 cp_lexer_consume_token (parser->lexer);
27813 return result;
27816 /* Validate that a clause of the given type does not already exist. */
27818 static void
27819 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27820 const char *name, location_t location)
27822 tree c;
27824 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27825 if (OMP_CLAUSE_CODE (c) == code)
27827 error_at (location, "too many %qs clauses", name);
27828 break;
27832 /* OpenMP 2.5:
27833 variable-list:
27834 identifier
27835 variable-list , identifier
27837 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27838 colon). An opening parenthesis will have been consumed by the caller.
27840 If KIND is nonzero, create the appropriate node and install the decl
27841 in OMP_CLAUSE_DECL and add the node to the head of the list.
27843 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27844 return the list created.
27846 COLON can be NULL if only closing parenthesis should end the list,
27847 or pointer to bool which will receive false if the list is terminated
27848 by closing parenthesis or true if the list is terminated by colon. */
27850 static tree
27851 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27852 tree list, bool *colon)
27854 cp_token *token;
27855 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27856 if (colon)
27858 parser->colon_corrects_to_scope_p = false;
27859 *colon = false;
27861 while (1)
27863 tree name, decl;
27865 token = cp_lexer_peek_token (parser->lexer);
27866 name = cp_parser_id_expression (parser, /*template_p=*/false,
27867 /*check_dependency_p=*/true,
27868 /*template_p=*/NULL,
27869 /*declarator_p=*/false,
27870 /*optional_p=*/false);
27871 if (name == error_mark_node)
27872 goto skip_comma;
27874 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27875 if (decl == error_mark_node)
27876 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27877 token->location);
27878 else if (kind != 0)
27880 switch (kind)
27882 case OMP_CLAUSE__CACHE_:
27883 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27885 error_at (token->location, "expected %<[%>");
27886 decl = error_mark_node;
27887 break;
27889 /* FALL THROUGH. */
27890 case OMP_CLAUSE_MAP:
27891 case OMP_CLAUSE_FROM:
27892 case OMP_CLAUSE_TO:
27893 case OMP_CLAUSE_DEPEND:
27894 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27896 tree low_bound = NULL_TREE, length = NULL_TREE;
27898 parser->colon_corrects_to_scope_p = false;
27899 cp_lexer_consume_token (parser->lexer);
27900 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27901 low_bound = cp_parser_expression (parser);
27902 if (!colon)
27903 parser->colon_corrects_to_scope_p
27904 = saved_colon_corrects_to_scope_p;
27905 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27906 length = integer_one_node;
27907 else
27909 /* Look for `:'. */
27910 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27911 goto skip_comma;
27912 if (!cp_lexer_next_token_is (parser->lexer,
27913 CPP_CLOSE_SQUARE))
27914 length = cp_parser_expression (parser);
27916 /* Look for the closing `]'. */
27917 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27918 RT_CLOSE_SQUARE))
27919 goto skip_comma;
27921 if (kind == OMP_CLAUSE__CACHE_)
27923 if (TREE_CODE (low_bound) != INTEGER_CST
27924 && !TREE_READONLY (low_bound))
27926 error_at (token->location,
27927 "%qD is not a constant", low_bound);
27928 decl = error_mark_node;
27931 if (TREE_CODE (length) != INTEGER_CST
27932 && !TREE_READONLY (length))
27934 error_at (token->location,
27935 "%qD is not a constant", length);
27936 decl = error_mark_node;
27940 decl = tree_cons (low_bound, length, decl);
27942 break;
27943 default:
27944 break;
27947 tree u = build_omp_clause (token->location, kind);
27948 OMP_CLAUSE_DECL (u) = decl;
27949 OMP_CLAUSE_CHAIN (u) = list;
27950 list = u;
27952 else
27953 list = tree_cons (decl, NULL_TREE, list);
27955 get_comma:
27956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27957 break;
27958 cp_lexer_consume_token (parser->lexer);
27961 if (colon)
27962 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27964 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27966 *colon = true;
27967 cp_parser_require (parser, CPP_COLON, RT_COLON);
27968 return list;
27971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27973 int ending;
27975 /* Try to resync to an unnested comma. Copied from
27976 cp_parser_parenthesized_expression_list. */
27977 skip_comma:
27978 if (colon)
27979 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27980 ending = cp_parser_skip_to_closing_parenthesis (parser,
27981 /*recovering=*/true,
27982 /*or_comma=*/true,
27983 /*consume_paren=*/true);
27984 if (ending < 0)
27985 goto get_comma;
27988 return list;
27991 /* Similarly, but expect leading and trailing parenthesis. This is a very
27992 common case for omp clauses. */
27994 static tree
27995 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27997 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27998 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27999 return list;
28002 /* OpenACC 2.0:
28003 copy ( variable-list )
28004 copyin ( variable-list )
28005 copyout ( variable-list )
28006 create ( variable-list )
28007 delete ( variable-list )
28008 present ( variable-list )
28009 present_or_copy ( variable-list )
28010 pcopy ( variable-list )
28011 present_or_copyin ( variable-list )
28012 pcopyin ( variable-list )
28013 present_or_copyout ( variable-list )
28014 pcopyout ( variable-list )
28015 present_or_create ( variable-list )
28016 pcreate ( variable-list ) */
28018 static tree
28019 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28020 tree list)
28022 enum gomp_map_kind kind;
28023 switch (c_kind)
28025 case PRAGMA_OACC_CLAUSE_COPY:
28026 kind = GOMP_MAP_FORCE_TOFROM;
28027 break;
28028 case PRAGMA_OACC_CLAUSE_COPYIN:
28029 kind = GOMP_MAP_FORCE_TO;
28030 break;
28031 case PRAGMA_OACC_CLAUSE_COPYOUT:
28032 kind = GOMP_MAP_FORCE_FROM;
28033 break;
28034 case PRAGMA_OACC_CLAUSE_CREATE:
28035 kind = GOMP_MAP_FORCE_ALLOC;
28036 break;
28037 case PRAGMA_OACC_CLAUSE_DELETE:
28038 kind = GOMP_MAP_FORCE_DEALLOC;
28039 break;
28040 case PRAGMA_OACC_CLAUSE_DEVICE:
28041 kind = GOMP_MAP_FORCE_TO;
28042 break;
28043 case PRAGMA_OACC_CLAUSE_HOST:
28044 case PRAGMA_OACC_CLAUSE_SELF:
28045 kind = GOMP_MAP_FORCE_FROM;
28046 break;
28047 case PRAGMA_OACC_CLAUSE_PRESENT:
28048 kind = GOMP_MAP_FORCE_PRESENT;
28049 break;
28050 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28051 kind = GOMP_MAP_TOFROM;
28052 break;
28053 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28054 kind = GOMP_MAP_TO;
28055 break;
28056 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28057 kind = GOMP_MAP_FROM;
28058 break;
28059 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28060 kind = GOMP_MAP_ALLOC;
28061 break;
28062 default:
28063 gcc_unreachable ();
28065 tree nl, c;
28066 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28068 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28069 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28071 return nl;
28074 /* OpenACC 2.0:
28075 deviceptr ( variable-list ) */
28077 static tree
28078 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28081 tree vars, t;
28083 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28084 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28085 variable-list must only allow for pointer variables. */
28086 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28087 for (t = vars; t; t = TREE_CHAIN (t))
28089 tree v = TREE_PURPOSE (t);
28091 /* FIXME diagnostics: Ideally we should keep individual
28092 locations for all the variables in the var list to make the
28093 following errors more precise. Perhaps
28094 c_parser_omp_var_list_parens should construct a list of
28095 locations to go along with the var list. */
28097 if (!VAR_P (v))
28098 error_at (loc, "%qD is not a variable", v);
28099 else if (TREE_TYPE (v) == error_mark_node)
28101 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28102 error_at (loc, "%qD is not a pointer variable", v);
28104 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28105 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28106 OMP_CLAUSE_DECL (u) = v;
28107 OMP_CLAUSE_CHAIN (u) = list;
28108 list = u;
28111 return list;
28114 /* OpenACC:
28115 vector_length ( expression ) */
28117 static tree
28118 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28120 tree t, c;
28121 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28122 bool error = false;
28124 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28125 return list;
28127 t = cp_parser_condition (parser);
28128 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28130 error_at (location, "expected positive integer expression");
28131 error = true;
28134 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28136 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28137 /*or_comma=*/false,
28138 /*consume_paren=*/true);
28139 return list;
28142 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28143 location);
28145 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28146 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28147 OMP_CLAUSE_CHAIN (c) = list;
28148 list = c;
28150 return list;
28153 /* OpenACC 2.0
28154 Parse wait clause or directive parameters. */
28156 static tree
28157 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28159 vec<tree, va_gc> *args;
28160 tree t, args_tree;
28162 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28163 /*cast_p=*/false,
28164 /*allow_expansion_p=*/true,
28165 /*non_constant_p=*/NULL);
28167 if (args == NULL || args->length () == 0)
28169 cp_parser_error (parser, "expected integer expression before ')'");
28170 if (args != NULL)
28171 release_tree_vector (args);
28172 return list;
28175 args_tree = build_tree_list_vec (args);
28177 release_tree_vector (args);
28179 for (t = args_tree; t; t = TREE_CHAIN (t))
28181 tree targ = TREE_VALUE (t);
28183 if (targ != error_mark_node)
28185 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28186 error ("%<wait%> expression must be integral");
28187 else
28189 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28191 mark_rvalue_use (targ);
28192 OMP_CLAUSE_DECL (c) = targ;
28193 OMP_CLAUSE_CHAIN (c) = list;
28194 list = c;
28199 return list;
28202 /* OpenACC:
28203 wait ( int-expr-list ) */
28205 static tree
28206 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28208 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28210 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28211 return list;
28213 list = cp_parser_oacc_wait_list (parser, location, list);
28215 return list;
28218 /* OpenMP 3.0:
28219 collapse ( constant-expression ) */
28221 static tree
28222 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28224 tree c, num;
28225 location_t loc;
28226 HOST_WIDE_INT n;
28228 loc = cp_lexer_peek_token (parser->lexer)->location;
28229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28230 return list;
28232 num = cp_parser_constant_expression (parser);
28234 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28235 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28236 /*or_comma=*/false,
28237 /*consume_paren=*/true);
28239 if (num == error_mark_node)
28240 return list;
28241 num = fold_non_dependent_expr (num);
28242 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28243 || !tree_fits_shwi_p (num)
28244 || (n = tree_to_shwi (num)) <= 0
28245 || (int) n != n)
28247 error_at (loc, "collapse argument needs positive constant integer expression");
28248 return list;
28251 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28252 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28253 OMP_CLAUSE_CHAIN (c) = list;
28254 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28256 return c;
28259 /* OpenMP 2.5:
28260 default ( shared | none ) */
28262 static tree
28263 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28265 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28266 tree c;
28268 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28269 return list;
28270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28273 const char *p = IDENTIFIER_POINTER (id);
28275 switch (p[0])
28277 case 'n':
28278 if (strcmp ("none", p) != 0)
28279 goto invalid_kind;
28280 kind = OMP_CLAUSE_DEFAULT_NONE;
28281 break;
28283 case 's':
28284 if (strcmp ("shared", p) != 0)
28285 goto invalid_kind;
28286 kind = OMP_CLAUSE_DEFAULT_SHARED;
28287 break;
28289 default:
28290 goto invalid_kind;
28293 cp_lexer_consume_token (parser->lexer);
28295 else
28297 invalid_kind:
28298 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28301 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28303 /*or_comma=*/false,
28304 /*consume_paren=*/true);
28306 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28307 return list;
28309 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28310 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28311 OMP_CLAUSE_CHAIN (c) = list;
28312 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28314 return c;
28317 /* OpenMP 3.1:
28318 final ( expression ) */
28320 static tree
28321 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28323 tree t, c;
28325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28326 return list;
28328 t = cp_parser_condition (parser);
28330 if (t == error_mark_node
28331 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28332 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28333 /*or_comma=*/false,
28334 /*consume_paren=*/true);
28336 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28338 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28339 OMP_CLAUSE_FINAL_EXPR (c) = t;
28340 OMP_CLAUSE_CHAIN (c) = list;
28342 return c;
28345 /* OpenMP 2.5:
28346 if ( expression ) */
28348 static tree
28349 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28351 tree t, c;
28353 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28354 return list;
28356 t = cp_parser_condition (parser);
28358 if (t == error_mark_node
28359 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28361 /*or_comma=*/false,
28362 /*consume_paren=*/true);
28364 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28366 c = build_omp_clause (location, OMP_CLAUSE_IF);
28367 OMP_CLAUSE_IF_EXPR (c) = t;
28368 OMP_CLAUSE_CHAIN (c) = list;
28370 return c;
28373 /* OpenMP 3.1:
28374 mergeable */
28376 static tree
28377 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28378 tree list, location_t location)
28380 tree c;
28382 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28383 location);
28385 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28386 OMP_CLAUSE_CHAIN (c) = list;
28387 return c;
28390 /* OpenMP 2.5:
28391 nowait */
28393 static tree
28394 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28395 tree list, location_t location)
28397 tree c;
28399 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28401 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28402 OMP_CLAUSE_CHAIN (c) = list;
28403 return c;
28406 /* OpenACC:
28407 num_gangs ( expression ) */
28409 static tree
28410 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28412 tree t, c;
28413 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28415 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28416 return list;
28418 t = cp_parser_condition (parser);
28420 if (t == error_mark_node
28421 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28423 /*or_comma=*/false,
28424 /*consume_paren=*/true);
28426 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28428 error_at (location, "expected positive integer expression");
28429 return list;
28432 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28434 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28435 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28436 OMP_CLAUSE_CHAIN (c) = list;
28437 list = c;
28439 return list;
28442 /* OpenMP 2.5:
28443 num_threads ( expression ) */
28445 static tree
28446 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28447 location_t location)
28449 tree t, c;
28451 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28452 return list;
28454 t = cp_parser_expression (parser);
28456 if (t == error_mark_node
28457 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28459 /*or_comma=*/false,
28460 /*consume_paren=*/true);
28462 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28463 "num_threads", location);
28465 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28466 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28467 OMP_CLAUSE_CHAIN (c) = list;
28469 return c;
28472 /* OpenACC:
28473 num_workers ( expression ) */
28475 static tree
28476 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28478 tree t, c;
28479 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28481 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28482 return list;
28484 t = cp_parser_condition (parser);
28486 if (t == error_mark_node
28487 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28489 /*or_comma=*/false,
28490 /*consume_paren=*/true);
28492 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28494 error_at (location, "expected positive integer expression");
28495 return list;
28498 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28499 location);
28501 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28502 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28503 OMP_CLAUSE_CHAIN (c) = list;
28504 list = c;
28506 return list;
28509 /* OpenMP 2.5:
28510 ordered */
28512 static tree
28513 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28514 tree list, location_t location)
28516 tree c;
28518 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28519 "ordered", location);
28521 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28522 OMP_CLAUSE_CHAIN (c) = list;
28523 return c;
28526 /* OpenMP 2.5:
28527 reduction ( reduction-operator : variable-list )
28529 reduction-operator:
28530 One of: + * - & ^ | && ||
28532 OpenMP 3.1:
28534 reduction-operator:
28535 One of: + * - & ^ | && || min max
28537 OpenMP 4.0:
28539 reduction-operator:
28540 One of: + * - & ^ | && ||
28541 id-expression */
28543 static tree
28544 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28546 enum tree_code code = ERROR_MARK;
28547 tree nlist, c, id = NULL_TREE;
28549 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28550 return list;
28552 switch (cp_lexer_peek_token (parser->lexer)->type)
28554 case CPP_PLUS: code = PLUS_EXPR; break;
28555 case CPP_MULT: code = MULT_EXPR; break;
28556 case CPP_MINUS: code = MINUS_EXPR; break;
28557 case CPP_AND: code = BIT_AND_EXPR; break;
28558 case CPP_XOR: code = BIT_XOR_EXPR; break;
28559 case CPP_OR: code = BIT_IOR_EXPR; break;
28560 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28561 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28562 default: break;
28565 if (code != ERROR_MARK)
28566 cp_lexer_consume_token (parser->lexer);
28567 else
28569 bool saved_colon_corrects_to_scope_p;
28570 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28571 parser->colon_corrects_to_scope_p = false;
28572 id = cp_parser_id_expression (parser, /*template_p=*/false,
28573 /*check_dependency_p=*/true,
28574 /*template_p=*/NULL,
28575 /*declarator_p=*/false,
28576 /*optional_p=*/false);
28577 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28578 if (identifier_p (id))
28580 const char *p = IDENTIFIER_POINTER (id);
28582 if (strcmp (p, "min") == 0)
28583 code = MIN_EXPR;
28584 else if (strcmp (p, "max") == 0)
28585 code = MAX_EXPR;
28586 else if (id == ansi_opname (PLUS_EXPR))
28587 code = PLUS_EXPR;
28588 else if (id == ansi_opname (MULT_EXPR))
28589 code = MULT_EXPR;
28590 else if (id == ansi_opname (MINUS_EXPR))
28591 code = MINUS_EXPR;
28592 else if (id == ansi_opname (BIT_AND_EXPR))
28593 code = BIT_AND_EXPR;
28594 else if (id == ansi_opname (BIT_IOR_EXPR))
28595 code = BIT_IOR_EXPR;
28596 else if (id == ansi_opname (BIT_XOR_EXPR))
28597 code = BIT_XOR_EXPR;
28598 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28599 code = TRUTH_ANDIF_EXPR;
28600 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28601 code = TRUTH_ORIF_EXPR;
28602 id = omp_reduction_id (code, id, NULL_TREE);
28603 tree scope = parser->scope;
28604 if (scope)
28605 id = build_qualified_name (NULL_TREE, scope, id, false);
28606 parser->scope = NULL_TREE;
28607 parser->qualifying_scope = NULL_TREE;
28608 parser->object_scope = NULL_TREE;
28610 else
28612 error ("invalid reduction-identifier");
28613 resync_fail:
28614 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28615 /*or_comma=*/false,
28616 /*consume_paren=*/true);
28617 return list;
28621 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28622 goto resync_fail;
28624 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28625 NULL);
28626 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28628 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28629 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28632 return nlist;
28635 /* OpenMP 2.5:
28636 schedule ( schedule-kind )
28637 schedule ( schedule-kind , expression )
28639 schedule-kind:
28640 static | dynamic | guided | runtime | auto */
28642 static tree
28643 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28645 tree c, t;
28647 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28648 return list;
28650 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28655 const char *p = IDENTIFIER_POINTER (id);
28657 switch (p[0])
28659 case 'd':
28660 if (strcmp ("dynamic", p) != 0)
28661 goto invalid_kind;
28662 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28663 break;
28665 case 'g':
28666 if (strcmp ("guided", p) != 0)
28667 goto invalid_kind;
28668 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28669 break;
28671 case 'r':
28672 if (strcmp ("runtime", p) != 0)
28673 goto invalid_kind;
28674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28675 break;
28677 default:
28678 goto invalid_kind;
28681 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28682 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28683 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28684 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28685 else
28686 goto invalid_kind;
28687 cp_lexer_consume_token (parser->lexer);
28689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28691 cp_token *token;
28692 cp_lexer_consume_token (parser->lexer);
28694 token = cp_lexer_peek_token (parser->lexer);
28695 t = cp_parser_assignment_expression (parser);
28697 if (t == error_mark_node)
28698 goto resync_fail;
28699 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28700 error_at (token->location, "schedule %<runtime%> does not take "
28701 "a %<chunk_size%> parameter");
28702 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28703 error_at (token->location, "schedule %<auto%> does not take "
28704 "a %<chunk_size%> parameter");
28705 else
28706 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28708 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28709 goto resync_fail;
28711 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28712 goto resync_fail;
28714 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28715 OMP_CLAUSE_CHAIN (c) = list;
28716 return c;
28718 invalid_kind:
28719 cp_parser_error (parser, "invalid schedule kind");
28720 resync_fail:
28721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28722 /*or_comma=*/false,
28723 /*consume_paren=*/true);
28724 return list;
28727 /* OpenMP 3.0:
28728 untied */
28730 static tree
28731 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28732 tree list, location_t location)
28734 tree c;
28736 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28738 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28739 OMP_CLAUSE_CHAIN (c) = list;
28740 return c;
28743 /* OpenMP 4.0:
28744 inbranch
28745 notinbranch */
28747 static tree
28748 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28749 tree list, location_t location)
28751 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28752 tree c = build_omp_clause (location, code);
28753 OMP_CLAUSE_CHAIN (c) = list;
28754 return c;
28757 /* OpenMP 4.0:
28758 parallel
28760 sections
28761 taskgroup */
28763 static tree
28764 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28765 enum omp_clause_code code,
28766 tree list, location_t location)
28768 tree c = build_omp_clause (location, code);
28769 OMP_CLAUSE_CHAIN (c) = list;
28770 return c;
28773 /* OpenMP 4.0:
28774 num_teams ( expression ) */
28776 static tree
28777 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28778 location_t location)
28780 tree t, c;
28782 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28783 return list;
28785 t = cp_parser_expression (parser);
28787 if (t == error_mark_node
28788 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28790 /*or_comma=*/false,
28791 /*consume_paren=*/true);
28793 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28794 "num_teams", location);
28796 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28797 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28798 OMP_CLAUSE_CHAIN (c) = list;
28800 return c;
28803 /* OpenMP 4.0:
28804 thread_limit ( expression ) */
28806 static tree
28807 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28808 location_t location)
28810 tree t, c;
28812 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28813 return list;
28815 t = cp_parser_expression (parser);
28817 if (t == error_mark_node
28818 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28820 /*or_comma=*/false,
28821 /*consume_paren=*/true);
28823 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28824 "thread_limit", location);
28826 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28827 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28828 OMP_CLAUSE_CHAIN (c) = list;
28830 return c;
28833 /* OpenMP 4.0:
28834 aligned ( variable-list )
28835 aligned ( variable-list : constant-expression ) */
28837 static tree
28838 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28840 tree nlist, c, alignment = NULL_TREE;
28841 bool colon;
28843 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28844 return list;
28846 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28847 &colon);
28849 if (colon)
28851 alignment = cp_parser_constant_expression (parser);
28853 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28854 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28855 /*or_comma=*/false,
28856 /*consume_paren=*/true);
28858 if (alignment == error_mark_node)
28859 alignment = NULL_TREE;
28862 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28863 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28865 return nlist;
28868 /* OpenMP 4.0:
28869 linear ( variable-list )
28870 linear ( variable-list : expression ) */
28872 static tree
28873 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28874 bool is_cilk_simd_fn)
28876 tree nlist, c, step = integer_one_node;
28877 bool colon;
28879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28880 return list;
28882 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28883 &colon);
28885 if (colon)
28887 step = cp_parser_expression (parser);
28889 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28891 sorry ("using parameters for %<linear%> step is not supported yet");
28892 step = integer_one_node;
28894 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28896 /*or_comma=*/false,
28897 /*consume_paren=*/true);
28899 if (step == error_mark_node)
28900 return list;
28903 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28904 OMP_CLAUSE_LINEAR_STEP (c) = step;
28906 return nlist;
28909 /* OpenMP 4.0:
28910 safelen ( constant-expression ) */
28912 static tree
28913 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28914 location_t location)
28916 tree t, c;
28918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28919 return list;
28921 t = cp_parser_constant_expression (parser);
28923 if (t == error_mark_node
28924 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28926 /*or_comma=*/false,
28927 /*consume_paren=*/true);
28929 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28931 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28932 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28933 OMP_CLAUSE_CHAIN (c) = list;
28935 return c;
28938 /* OpenMP 4.0:
28939 simdlen ( constant-expression ) */
28941 static tree
28942 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28943 location_t location)
28945 tree t, c;
28947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28948 return list;
28950 t = cp_parser_constant_expression (parser);
28952 if (t == error_mark_node
28953 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28955 /*or_comma=*/false,
28956 /*consume_paren=*/true);
28958 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28960 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28961 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28962 OMP_CLAUSE_CHAIN (c) = list;
28964 return c;
28967 /* OpenMP 4.0:
28968 depend ( depend-kind : variable-list )
28970 depend-kind:
28971 in | out | inout */
28973 static tree
28974 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28976 tree nlist, c;
28977 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28979 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28980 return list;
28982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28985 const char *p = IDENTIFIER_POINTER (id);
28987 if (strcmp ("in", p) == 0)
28988 kind = OMP_CLAUSE_DEPEND_IN;
28989 else if (strcmp ("inout", p) == 0)
28990 kind = OMP_CLAUSE_DEPEND_INOUT;
28991 else if (strcmp ("out", p) == 0)
28992 kind = OMP_CLAUSE_DEPEND_OUT;
28993 else
28994 goto invalid_kind;
28996 else
28997 goto invalid_kind;
28999 cp_lexer_consume_token (parser->lexer);
29000 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29001 goto resync_fail;
29003 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29004 NULL);
29006 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29007 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29009 return nlist;
29011 invalid_kind:
29012 cp_parser_error (parser, "invalid depend kind");
29013 resync_fail:
29014 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29015 /*or_comma=*/false,
29016 /*consume_paren=*/true);
29017 return list;
29020 /* OpenMP 4.0:
29021 map ( map-kind : variable-list )
29022 map ( variable-list )
29024 map-kind:
29025 alloc | to | from | tofrom */
29027 static tree
29028 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29030 tree nlist, c;
29031 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29033 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29034 return list;
29036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29037 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29039 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29040 const char *p = IDENTIFIER_POINTER (id);
29042 if (strcmp ("alloc", p) == 0)
29043 kind = GOMP_MAP_ALLOC;
29044 else if (strcmp ("to", p) == 0)
29045 kind = GOMP_MAP_TO;
29046 else if (strcmp ("from", p) == 0)
29047 kind = GOMP_MAP_FROM;
29048 else if (strcmp ("tofrom", p) == 0)
29049 kind = GOMP_MAP_TOFROM;
29050 else
29052 cp_parser_error (parser, "invalid map kind");
29053 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29054 /*or_comma=*/false,
29055 /*consume_paren=*/true);
29056 return list;
29058 cp_lexer_consume_token (parser->lexer);
29059 cp_lexer_consume_token (parser->lexer);
29062 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29063 NULL);
29065 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29066 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29068 return nlist;
29071 /* OpenMP 4.0:
29072 device ( expression ) */
29074 static tree
29075 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29076 location_t location)
29078 tree t, c;
29080 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29081 return list;
29083 t = cp_parser_expression (parser);
29085 if (t == error_mark_node
29086 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29087 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29088 /*or_comma=*/false,
29089 /*consume_paren=*/true);
29091 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29092 "device", location);
29094 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29095 OMP_CLAUSE_DEVICE_ID (c) = t;
29096 OMP_CLAUSE_CHAIN (c) = list;
29098 return c;
29101 /* OpenMP 4.0:
29102 dist_schedule ( static )
29103 dist_schedule ( static , expression ) */
29105 static tree
29106 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29107 location_t location)
29109 tree c, t;
29111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29112 return list;
29114 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29116 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29117 goto invalid_kind;
29118 cp_lexer_consume_token (parser->lexer);
29120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29122 cp_lexer_consume_token (parser->lexer);
29124 t = cp_parser_assignment_expression (parser);
29126 if (t == error_mark_node)
29127 goto resync_fail;
29128 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29130 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29131 goto resync_fail;
29133 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29134 goto resync_fail;
29136 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29137 location);
29138 OMP_CLAUSE_CHAIN (c) = list;
29139 return c;
29141 invalid_kind:
29142 cp_parser_error (parser, "invalid dist_schedule kind");
29143 resync_fail:
29144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29145 /*or_comma=*/false,
29146 /*consume_paren=*/true);
29147 return list;
29150 /* OpenMP 4.0:
29151 proc_bind ( proc-bind-kind )
29153 proc-bind-kind:
29154 master | close | spread */
29156 static tree
29157 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29158 location_t location)
29160 tree c;
29161 enum omp_clause_proc_bind_kind kind;
29163 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29164 return list;
29166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29169 const char *p = IDENTIFIER_POINTER (id);
29171 if (strcmp ("master", p) == 0)
29172 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29173 else if (strcmp ("close", p) == 0)
29174 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29175 else if (strcmp ("spread", p) == 0)
29176 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29177 else
29178 goto invalid_kind;
29180 else
29181 goto invalid_kind;
29183 cp_lexer_consume_token (parser->lexer);
29184 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29185 goto resync_fail;
29187 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29188 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29189 location);
29190 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29191 OMP_CLAUSE_CHAIN (c) = list;
29192 return c;
29194 invalid_kind:
29195 cp_parser_error (parser, "invalid depend kind");
29196 resync_fail:
29197 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29198 /*or_comma=*/false,
29199 /*consume_paren=*/true);
29200 return list;
29203 /* OpenACC:
29204 async [( int-expr )] */
29206 static tree
29207 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29209 tree c, t;
29210 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29212 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29214 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29216 cp_lexer_consume_token (parser->lexer);
29218 t = cp_parser_expression (parser);
29219 if (t == error_mark_node
29220 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29222 /*or_comma=*/false,
29223 /*consume_paren=*/true);
29226 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29228 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29229 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29230 OMP_CLAUSE_CHAIN (c) = list;
29231 list = c;
29233 return list;
29236 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29237 is a bitmask in MASK. Return the list of clauses found. */
29239 static tree
29240 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29241 const char *where, cp_token *pragma_tok,
29242 bool finish_p = true)
29244 tree clauses = NULL;
29245 bool first = true;
29247 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29249 location_t here;
29250 pragma_omp_clause c_kind;
29251 const char *c_name;
29252 tree prev = clauses;
29254 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29255 cp_lexer_consume_token (parser->lexer);
29257 here = cp_lexer_peek_token (parser->lexer)->location;
29258 c_kind = cp_parser_omp_clause_name (parser);
29260 switch (c_kind)
29262 case PRAGMA_OACC_CLAUSE_ASYNC:
29263 clauses = cp_parser_oacc_clause_async (parser, clauses);
29264 c_name = "async";
29265 break;
29266 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29267 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29268 c_name = "collapse";
29269 break;
29270 case PRAGMA_OACC_CLAUSE_COPY:
29271 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29272 c_name = "copy";
29273 break;
29274 case PRAGMA_OACC_CLAUSE_COPYIN:
29275 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29276 c_name = "copyin";
29277 break;
29278 case PRAGMA_OACC_CLAUSE_COPYOUT:
29279 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29280 c_name = "copyout";
29281 break;
29282 case PRAGMA_OACC_CLAUSE_CREATE:
29283 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29284 c_name = "create";
29285 break;
29286 case PRAGMA_OACC_CLAUSE_DELETE:
29287 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29288 c_name = "delete";
29289 break;
29290 case PRAGMA_OACC_CLAUSE_DEVICE:
29291 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29292 c_name = "device";
29293 break;
29294 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29295 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29296 c_name = "deviceptr";
29297 break;
29298 case PRAGMA_OACC_CLAUSE_HOST:
29299 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29300 c_name = "host";
29301 break;
29302 case PRAGMA_OACC_CLAUSE_IF:
29303 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29304 c_name = "if";
29305 break;
29306 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29307 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29308 c_name = "num_gangs";
29309 break;
29310 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29311 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29312 c_name = "num_workers";
29313 break;
29314 case PRAGMA_OACC_CLAUSE_PRESENT:
29315 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29316 c_name = "present";
29317 break;
29318 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29319 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29320 c_name = "present_or_copy";
29321 break;
29322 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29323 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29324 c_name = "present_or_copyin";
29325 break;
29326 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29327 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29328 c_name = "present_or_copyout";
29329 break;
29330 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29331 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29332 c_name = "present_or_create";
29333 break;
29334 case PRAGMA_OACC_CLAUSE_REDUCTION:
29335 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29336 c_name = "reduction";
29337 break;
29338 case PRAGMA_OACC_CLAUSE_SELF:
29339 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29340 c_name = "self";
29341 break;
29342 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29343 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29344 c_name = "vector_length";
29345 break;
29346 case PRAGMA_OACC_CLAUSE_WAIT:
29347 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29348 c_name = "wait";
29349 break;
29350 default:
29351 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29352 goto saw_error;
29355 first = false;
29357 if (((mask >> c_kind) & 1) == 0)
29359 /* Remove the invalid clause(s) from the list to avoid
29360 confusing the rest of the compiler. */
29361 clauses = prev;
29362 error_at (here, "%qs is not valid for %qs", c_name, where);
29366 saw_error:
29367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29369 if (finish_p)
29370 return finish_omp_clauses (clauses);
29372 return clauses;
29375 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29376 is a bitmask in MASK. Return the list of clauses found; the result
29377 of clause default goes in *pdefault. */
29379 static tree
29380 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29381 const char *where, cp_token *pragma_tok,
29382 bool finish_p = true)
29384 tree clauses = NULL;
29385 bool first = true;
29386 cp_token *token = NULL;
29387 bool cilk_simd_fn = false;
29389 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29391 pragma_omp_clause c_kind;
29392 const char *c_name;
29393 tree prev = clauses;
29395 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29396 cp_lexer_consume_token (parser->lexer);
29398 token = cp_lexer_peek_token (parser->lexer);
29399 c_kind = cp_parser_omp_clause_name (parser);
29401 switch (c_kind)
29403 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29404 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29405 token->location);
29406 c_name = "collapse";
29407 break;
29408 case PRAGMA_OMP_CLAUSE_COPYIN:
29409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29410 c_name = "copyin";
29411 break;
29412 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29414 clauses);
29415 c_name = "copyprivate";
29416 break;
29417 case PRAGMA_OMP_CLAUSE_DEFAULT:
29418 clauses = cp_parser_omp_clause_default (parser, clauses,
29419 token->location);
29420 c_name = "default";
29421 break;
29422 case PRAGMA_OMP_CLAUSE_FINAL:
29423 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29424 c_name = "final";
29425 break;
29426 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29428 clauses);
29429 c_name = "firstprivate";
29430 break;
29431 case PRAGMA_OMP_CLAUSE_IF:
29432 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29433 c_name = "if";
29434 break;
29435 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29436 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29437 clauses);
29438 c_name = "lastprivate";
29439 break;
29440 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29441 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29442 token->location);
29443 c_name = "mergeable";
29444 break;
29445 case PRAGMA_OMP_CLAUSE_NOWAIT:
29446 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29447 c_name = "nowait";
29448 break;
29449 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29450 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29451 token->location);
29452 c_name = "num_threads";
29453 break;
29454 case PRAGMA_OMP_CLAUSE_ORDERED:
29455 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29456 token->location);
29457 c_name = "ordered";
29458 break;
29459 case PRAGMA_OMP_CLAUSE_PRIVATE:
29460 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29461 clauses);
29462 c_name = "private";
29463 break;
29464 case PRAGMA_OMP_CLAUSE_REDUCTION:
29465 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29466 c_name = "reduction";
29467 break;
29468 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29469 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29470 token->location);
29471 c_name = "schedule";
29472 break;
29473 case PRAGMA_OMP_CLAUSE_SHARED:
29474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29475 clauses);
29476 c_name = "shared";
29477 break;
29478 case PRAGMA_OMP_CLAUSE_UNTIED:
29479 clauses = cp_parser_omp_clause_untied (parser, clauses,
29480 token->location);
29481 c_name = "untied";
29482 break;
29483 case PRAGMA_OMP_CLAUSE_INBRANCH:
29484 case PRAGMA_CILK_CLAUSE_MASK:
29485 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29486 clauses, token->location);
29487 c_name = "inbranch";
29488 break;
29489 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29490 case PRAGMA_CILK_CLAUSE_NOMASK:
29491 clauses = cp_parser_omp_clause_branch (parser,
29492 OMP_CLAUSE_NOTINBRANCH,
29493 clauses, token->location);
29494 c_name = "notinbranch";
29495 break;
29496 case PRAGMA_OMP_CLAUSE_PARALLEL:
29497 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29498 clauses, token->location);
29499 c_name = "parallel";
29500 if (!first)
29502 clause_not_first:
29503 error_at (token->location, "%qs must be the first clause of %qs",
29504 c_name, where);
29505 clauses = prev;
29507 break;
29508 case PRAGMA_OMP_CLAUSE_FOR:
29509 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29510 clauses, token->location);
29511 c_name = "for";
29512 if (!first)
29513 goto clause_not_first;
29514 break;
29515 case PRAGMA_OMP_CLAUSE_SECTIONS:
29516 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29517 clauses, token->location);
29518 c_name = "sections";
29519 if (!first)
29520 goto clause_not_first;
29521 break;
29522 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29523 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29524 clauses, token->location);
29525 c_name = "taskgroup";
29526 if (!first)
29527 goto clause_not_first;
29528 break;
29529 case PRAGMA_OMP_CLAUSE_TO:
29530 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29531 clauses);
29532 c_name = "to";
29533 break;
29534 case PRAGMA_OMP_CLAUSE_FROM:
29535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29536 clauses);
29537 c_name = "from";
29538 break;
29539 case PRAGMA_OMP_CLAUSE_UNIFORM:
29540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29541 clauses);
29542 c_name = "uniform";
29543 break;
29544 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29545 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29546 token->location);
29547 c_name = "num_teams";
29548 break;
29549 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29550 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29551 token->location);
29552 c_name = "thread_limit";
29553 break;
29554 case PRAGMA_OMP_CLAUSE_ALIGNED:
29555 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29556 c_name = "aligned";
29557 break;
29558 case PRAGMA_OMP_CLAUSE_LINEAR:
29559 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29560 cilk_simd_fn = true;
29561 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29562 c_name = "linear";
29563 break;
29564 case PRAGMA_OMP_CLAUSE_DEPEND:
29565 clauses = cp_parser_omp_clause_depend (parser, clauses);
29566 c_name = "depend";
29567 break;
29568 case PRAGMA_OMP_CLAUSE_MAP:
29569 clauses = cp_parser_omp_clause_map (parser, clauses);
29570 c_name = "map";
29571 break;
29572 case PRAGMA_OMP_CLAUSE_DEVICE:
29573 clauses = cp_parser_omp_clause_device (parser, clauses,
29574 token->location);
29575 c_name = "device";
29576 break;
29577 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29578 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29579 token->location);
29580 c_name = "dist_schedule";
29581 break;
29582 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29583 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29584 token->location);
29585 c_name = "proc_bind";
29586 break;
29587 case PRAGMA_OMP_CLAUSE_SAFELEN:
29588 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29589 token->location);
29590 c_name = "safelen";
29591 break;
29592 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29593 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29594 token->location);
29595 c_name = "simdlen";
29596 break;
29597 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29598 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29599 c_name = "simdlen";
29600 break;
29601 default:
29602 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29603 goto saw_error;
29606 first = false;
29608 if (((mask >> c_kind) & 1) == 0)
29610 /* Remove the invalid clause(s) from the list to avoid
29611 confusing the rest of the compiler. */
29612 clauses = prev;
29613 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29616 saw_error:
29617 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29618 no reason to skip to the end. */
29619 if (!(flag_cilkplus && pragma_tok == NULL))
29620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29621 if (finish_p)
29622 return finish_omp_clauses (clauses);
29623 return clauses;
29626 /* OpenMP 2.5:
29627 structured-block:
29628 statement
29630 In practice, we're also interested in adding the statement to an
29631 outer node. So it is convenient if we work around the fact that
29632 cp_parser_statement calls add_stmt. */
29634 static unsigned
29635 cp_parser_begin_omp_structured_block (cp_parser *parser)
29637 unsigned save = parser->in_statement;
29639 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29640 This preserves the "not within loop or switch" style error messages
29641 for nonsense cases like
29642 void foo() {
29643 #pragma omp single
29644 break;
29647 if (parser->in_statement)
29648 parser->in_statement = IN_OMP_BLOCK;
29650 return save;
29653 static void
29654 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29656 parser->in_statement = save;
29659 static tree
29660 cp_parser_omp_structured_block (cp_parser *parser)
29662 tree stmt = begin_omp_structured_block ();
29663 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29665 cp_parser_statement (parser, NULL_TREE, false, NULL);
29667 cp_parser_end_omp_structured_block (parser, save);
29668 return finish_omp_structured_block (stmt);
29671 /* OpenMP 2.5:
29672 # pragma omp atomic new-line
29673 expression-stmt
29675 expression-stmt:
29676 x binop= expr | x++ | ++x | x-- | --x
29677 binop:
29678 +, *, -, /, &, ^, |, <<, >>
29680 where x is an lvalue expression with scalar type.
29682 OpenMP 3.1:
29683 # pragma omp atomic new-line
29684 update-stmt
29686 # pragma omp atomic read new-line
29687 read-stmt
29689 # pragma omp atomic write new-line
29690 write-stmt
29692 # pragma omp atomic update new-line
29693 update-stmt
29695 # pragma omp atomic capture new-line
29696 capture-stmt
29698 # pragma omp atomic capture new-line
29699 capture-block
29701 read-stmt:
29702 v = x
29703 write-stmt:
29704 x = expr
29705 update-stmt:
29706 expression-stmt | x = x binop expr
29707 capture-stmt:
29708 v = expression-stmt
29709 capture-block:
29710 { v = x; update-stmt; } | { update-stmt; v = x; }
29712 OpenMP 4.0:
29713 update-stmt:
29714 expression-stmt | x = x binop expr | x = expr binop x
29715 capture-stmt:
29716 v = update-stmt
29717 capture-block:
29718 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29720 where x and v are lvalue expressions with scalar type. */
29722 static void
29723 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29725 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29726 tree rhs1 = NULL_TREE, orig_lhs;
29727 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29728 bool structured_block = false;
29729 bool seq_cst = false;
29731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29733 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29734 const char *p = IDENTIFIER_POINTER (id);
29736 if (!strcmp (p, "seq_cst"))
29738 seq_cst = true;
29739 cp_lexer_consume_token (parser->lexer);
29740 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29741 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29742 cp_lexer_consume_token (parser->lexer);
29745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29748 const char *p = IDENTIFIER_POINTER (id);
29750 if (!strcmp (p, "read"))
29751 code = OMP_ATOMIC_READ;
29752 else if (!strcmp (p, "write"))
29753 code = NOP_EXPR;
29754 else if (!strcmp (p, "update"))
29755 code = OMP_ATOMIC;
29756 else if (!strcmp (p, "capture"))
29757 code = OMP_ATOMIC_CAPTURE_NEW;
29758 else
29759 p = NULL;
29760 if (p)
29761 cp_lexer_consume_token (parser->lexer);
29763 if (!seq_cst)
29765 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29766 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29767 cp_lexer_consume_token (parser->lexer);
29769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29772 const char *p = IDENTIFIER_POINTER (id);
29774 if (!strcmp (p, "seq_cst"))
29776 seq_cst = true;
29777 cp_lexer_consume_token (parser->lexer);
29781 cp_parser_require_pragma_eol (parser, pragma_tok);
29783 switch (code)
29785 case OMP_ATOMIC_READ:
29786 case NOP_EXPR: /* atomic write */
29787 v = cp_parser_unary_expression (parser);
29788 if (v == error_mark_node)
29789 goto saw_error;
29790 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29791 goto saw_error;
29792 if (code == NOP_EXPR)
29793 lhs = cp_parser_expression (parser);
29794 else
29795 lhs = cp_parser_unary_expression (parser);
29796 if (lhs == error_mark_node)
29797 goto saw_error;
29798 if (code == NOP_EXPR)
29800 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29801 opcode. */
29802 code = OMP_ATOMIC;
29803 rhs = lhs;
29804 lhs = v;
29805 v = NULL_TREE;
29807 goto done;
29808 case OMP_ATOMIC_CAPTURE_NEW:
29809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29811 cp_lexer_consume_token (parser->lexer);
29812 structured_block = true;
29814 else
29816 v = cp_parser_unary_expression (parser);
29817 if (v == error_mark_node)
29818 goto saw_error;
29819 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29820 goto saw_error;
29822 default:
29823 break;
29826 restart:
29827 lhs = cp_parser_unary_expression (parser);
29828 orig_lhs = lhs;
29829 switch (TREE_CODE (lhs))
29831 case ERROR_MARK:
29832 goto saw_error;
29834 case POSTINCREMENT_EXPR:
29835 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29836 code = OMP_ATOMIC_CAPTURE_OLD;
29837 /* FALLTHROUGH */
29838 case PREINCREMENT_EXPR:
29839 lhs = TREE_OPERAND (lhs, 0);
29840 opcode = PLUS_EXPR;
29841 rhs = integer_one_node;
29842 break;
29844 case POSTDECREMENT_EXPR:
29845 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29846 code = OMP_ATOMIC_CAPTURE_OLD;
29847 /* FALLTHROUGH */
29848 case PREDECREMENT_EXPR:
29849 lhs = TREE_OPERAND (lhs, 0);
29850 opcode = MINUS_EXPR;
29851 rhs = integer_one_node;
29852 break;
29854 case COMPOUND_EXPR:
29855 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29856 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29857 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29858 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29859 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29860 (TREE_OPERAND (lhs, 1), 0), 0)))
29861 == BOOLEAN_TYPE)
29862 /* Undo effects of boolean_increment for post {in,de}crement. */
29863 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29864 /* FALLTHRU */
29865 case MODIFY_EXPR:
29866 if (TREE_CODE (lhs) == MODIFY_EXPR
29867 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29869 /* Undo effects of boolean_increment. */
29870 if (integer_onep (TREE_OPERAND (lhs, 1)))
29872 /* This is pre or post increment. */
29873 rhs = TREE_OPERAND (lhs, 1);
29874 lhs = TREE_OPERAND (lhs, 0);
29875 opcode = NOP_EXPR;
29876 if (code == OMP_ATOMIC_CAPTURE_NEW
29877 && !structured_block
29878 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29879 code = OMP_ATOMIC_CAPTURE_OLD;
29880 break;
29883 /* FALLTHRU */
29884 default:
29885 switch (cp_lexer_peek_token (parser->lexer)->type)
29887 case CPP_MULT_EQ:
29888 opcode = MULT_EXPR;
29889 break;
29890 case CPP_DIV_EQ:
29891 opcode = TRUNC_DIV_EXPR;
29892 break;
29893 case CPP_PLUS_EQ:
29894 opcode = PLUS_EXPR;
29895 break;
29896 case CPP_MINUS_EQ:
29897 opcode = MINUS_EXPR;
29898 break;
29899 case CPP_LSHIFT_EQ:
29900 opcode = LSHIFT_EXPR;
29901 break;
29902 case CPP_RSHIFT_EQ:
29903 opcode = RSHIFT_EXPR;
29904 break;
29905 case CPP_AND_EQ:
29906 opcode = BIT_AND_EXPR;
29907 break;
29908 case CPP_OR_EQ:
29909 opcode = BIT_IOR_EXPR;
29910 break;
29911 case CPP_XOR_EQ:
29912 opcode = BIT_XOR_EXPR;
29913 break;
29914 case CPP_EQ:
29915 enum cp_parser_prec oprec;
29916 cp_token *token;
29917 cp_lexer_consume_token (parser->lexer);
29918 cp_parser_parse_tentatively (parser);
29919 rhs1 = cp_parser_simple_cast_expression (parser);
29920 if (rhs1 == error_mark_node)
29922 cp_parser_abort_tentative_parse (parser);
29923 cp_parser_simple_cast_expression (parser);
29924 goto saw_error;
29926 token = cp_lexer_peek_token (parser->lexer);
29927 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29929 cp_parser_abort_tentative_parse (parser);
29930 cp_parser_parse_tentatively (parser);
29931 rhs = cp_parser_binary_expression (parser, false, true,
29932 PREC_NOT_OPERATOR, NULL);
29933 if (rhs == error_mark_node)
29935 cp_parser_abort_tentative_parse (parser);
29936 cp_parser_binary_expression (parser, false, true,
29937 PREC_NOT_OPERATOR, NULL);
29938 goto saw_error;
29940 switch (TREE_CODE (rhs))
29942 case MULT_EXPR:
29943 case TRUNC_DIV_EXPR:
29944 case RDIV_EXPR:
29945 case PLUS_EXPR:
29946 case MINUS_EXPR:
29947 case LSHIFT_EXPR:
29948 case RSHIFT_EXPR:
29949 case BIT_AND_EXPR:
29950 case BIT_IOR_EXPR:
29951 case BIT_XOR_EXPR:
29952 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29954 if (cp_parser_parse_definitely (parser))
29956 opcode = TREE_CODE (rhs);
29957 rhs1 = TREE_OPERAND (rhs, 0);
29958 rhs = TREE_OPERAND (rhs, 1);
29959 goto stmt_done;
29961 else
29962 goto saw_error;
29964 break;
29965 default:
29966 break;
29968 cp_parser_abort_tentative_parse (parser);
29969 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29971 rhs = cp_parser_expression (parser);
29972 if (rhs == error_mark_node)
29973 goto saw_error;
29974 opcode = NOP_EXPR;
29975 rhs1 = NULL_TREE;
29976 goto stmt_done;
29978 cp_parser_error (parser,
29979 "invalid form of %<#pragma omp atomic%>");
29980 goto saw_error;
29982 if (!cp_parser_parse_definitely (parser))
29983 goto saw_error;
29984 switch (token->type)
29986 case CPP_SEMICOLON:
29987 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29989 code = OMP_ATOMIC_CAPTURE_OLD;
29990 v = lhs;
29991 lhs = NULL_TREE;
29992 lhs1 = rhs1;
29993 rhs1 = NULL_TREE;
29994 cp_lexer_consume_token (parser->lexer);
29995 goto restart;
29997 else if (structured_block)
29999 opcode = NOP_EXPR;
30000 rhs = rhs1;
30001 rhs1 = NULL_TREE;
30002 goto stmt_done;
30004 cp_parser_error (parser,
30005 "invalid form of %<#pragma omp atomic%>");
30006 goto saw_error;
30007 case CPP_MULT:
30008 opcode = MULT_EXPR;
30009 break;
30010 case CPP_DIV:
30011 opcode = TRUNC_DIV_EXPR;
30012 break;
30013 case CPP_PLUS:
30014 opcode = PLUS_EXPR;
30015 break;
30016 case CPP_MINUS:
30017 opcode = MINUS_EXPR;
30018 break;
30019 case CPP_LSHIFT:
30020 opcode = LSHIFT_EXPR;
30021 break;
30022 case CPP_RSHIFT:
30023 opcode = RSHIFT_EXPR;
30024 break;
30025 case CPP_AND:
30026 opcode = BIT_AND_EXPR;
30027 break;
30028 case CPP_OR:
30029 opcode = BIT_IOR_EXPR;
30030 break;
30031 case CPP_XOR:
30032 opcode = BIT_XOR_EXPR;
30033 break;
30034 default:
30035 cp_parser_error (parser,
30036 "invalid operator for %<#pragma omp atomic%>");
30037 goto saw_error;
30039 oprec = TOKEN_PRECEDENCE (token);
30040 gcc_assert (oprec != PREC_NOT_OPERATOR);
30041 if (commutative_tree_code (opcode))
30042 oprec = (enum cp_parser_prec) (oprec - 1);
30043 cp_lexer_consume_token (parser->lexer);
30044 rhs = cp_parser_binary_expression (parser, false, false,
30045 oprec, NULL);
30046 if (rhs == error_mark_node)
30047 goto saw_error;
30048 goto stmt_done;
30049 /* FALLTHROUGH */
30050 default:
30051 cp_parser_error (parser,
30052 "invalid operator for %<#pragma omp atomic%>");
30053 goto saw_error;
30055 cp_lexer_consume_token (parser->lexer);
30057 rhs = cp_parser_expression (parser);
30058 if (rhs == error_mark_node)
30059 goto saw_error;
30060 break;
30062 stmt_done:
30063 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30065 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30066 goto saw_error;
30067 v = cp_parser_unary_expression (parser);
30068 if (v == error_mark_node)
30069 goto saw_error;
30070 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30071 goto saw_error;
30072 lhs1 = cp_parser_unary_expression (parser);
30073 if (lhs1 == error_mark_node)
30074 goto saw_error;
30076 if (structured_block)
30078 cp_parser_consume_semicolon_at_end_of_statement (parser);
30079 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30081 done:
30082 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30083 if (!structured_block)
30084 cp_parser_consume_semicolon_at_end_of_statement (parser);
30085 return;
30087 saw_error:
30088 cp_parser_skip_to_end_of_block_or_statement (parser);
30089 if (structured_block)
30091 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30092 cp_lexer_consume_token (parser->lexer);
30093 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30095 cp_parser_skip_to_end_of_block_or_statement (parser);
30096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30097 cp_lexer_consume_token (parser->lexer);
30103 /* OpenMP 2.5:
30104 # pragma omp barrier new-line */
30106 static void
30107 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30109 cp_parser_require_pragma_eol (parser, pragma_tok);
30110 finish_omp_barrier ();
30113 /* OpenMP 2.5:
30114 # pragma omp critical [(name)] new-line
30115 structured-block */
30117 static tree
30118 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30120 tree stmt, name = NULL;
30122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30124 cp_lexer_consume_token (parser->lexer);
30126 name = cp_parser_identifier (parser);
30128 if (name == error_mark_node
30129 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30131 /*or_comma=*/false,
30132 /*consume_paren=*/true);
30133 if (name == error_mark_node)
30134 name = NULL;
30136 cp_parser_require_pragma_eol (parser, pragma_tok);
30138 stmt = cp_parser_omp_structured_block (parser);
30139 return c_finish_omp_critical (input_location, stmt, name);
30142 /* OpenMP 2.5:
30143 # pragma omp flush flush-vars[opt] new-line
30145 flush-vars:
30146 ( variable-list ) */
30148 static void
30149 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30152 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30153 cp_parser_require_pragma_eol (parser, pragma_tok);
30155 finish_omp_flush ();
30158 /* Helper function, to parse omp for increment expression. */
30160 static tree
30161 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30163 tree cond = cp_parser_binary_expression (parser, false, true,
30164 PREC_NOT_OPERATOR, NULL);
30165 if (cond == error_mark_node
30166 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30168 cp_parser_skip_to_end_of_statement (parser);
30169 return error_mark_node;
30172 switch (TREE_CODE (cond))
30174 case GT_EXPR:
30175 case GE_EXPR:
30176 case LT_EXPR:
30177 case LE_EXPR:
30178 break;
30179 case NE_EXPR:
30180 if (code == CILK_SIMD || code == CILK_FOR)
30181 break;
30182 /* Fall through: OpenMP disallows NE_EXPR. */
30183 default:
30184 return error_mark_node;
30187 /* If decl is an iterator, preserve LHS and RHS of the relational
30188 expr until finish_omp_for. */
30189 if (decl
30190 && (type_dependent_expression_p (decl)
30191 || CLASS_TYPE_P (TREE_TYPE (decl))))
30192 return cond;
30194 return build_x_binary_op (input_location, TREE_CODE (cond),
30195 TREE_OPERAND (cond, 0), ERROR_MARK,
30196 TREE_OPERAND (cond, 1), ERROR_MARK,
30197 /*overload=*/NULL, tf_warning_or_error);
30200 /* Helper function, to parse omp for increment expression. */
30202 static tree
30203 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30205 cp_token *token = cp_lexer_peek_token (parser->lexer);
30206 enum tree_code op;
30207 tree lhs, rhs;
30208 cp_id_kind idk;
30209 bool decl_first;
30211 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30213 op = (token->type == CPP_PLUS_PLUS
30214 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30215 cp_lexer_consume_token (parser->lexer);
30216 lhs = cp_parser_simple_cast_expression (parser);
30217 if (lhs != decl)
30218 return error_mark_node;
30219 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30222 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30223 if (lhs != decl)
30224 return error_mark_node;
30226 token = cp_lexer_peek_token (parser->lexer);
30227 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30229 op = (token->type == CPP_PLUS_PLUS
30230 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30231 cp_lexer_consume_token (parser->lexer);
30232 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30235 op = cp_parser_assignment_operator_opt (parser);
30236 if (op == ERROR_MARK)
30237 return error_mark_node;
30239 if (op != NOP_EXPR)
30241 rhs = cp_parser_assignment_expression (parser);
30242 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30243 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30246 lhs = cp_parser_binary_expression (parser, false, false,
30247 PREC_ADDITIVE_EXPRESSION, NULL);
30248 token = cp_lexer_peek_token (parser->lexer);
30249 decl_first = lhs == decl;
30250 if (decl_first)
30251 lhs = NULL_TREE;
30252 if (token->type != CPP_PLUS
30253 && token->type != CPP_MINUS)
30254 return error_mark_node;
30258 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30259 cp_lexer_consume_token (parser->lexer);
30260 rhs = cp_parser_binary_expression (parser, false, false,
30261 PREC_ADDITIVE_EXPRESSION, NULL);
30262 token = cp_lexer_peek_token (parser->lexer);
30263 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30265 if (lhs == NULL_TREE)
30267 if (op == PLUS_EXPR)
30268 lhs = rhs;
30269 else
30270 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30271 tf_warning_or_error);
30273 else
30274 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30275 ERROR_MARK, NULL, tf_warning_or_error);
30278 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30280 if (!decl_first)
30282 if (rhs != decl || op == MINUS_EXPR)
30283 return error_mark_node;
30284 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30286 else
30287 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30289 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30292 /* Parse the initialization statement of either an OpenMP for loop or
30293 a Cilk Plus for loop.
30295 Return true if the resulting construct should have an
30296 OMP_CLAUSE_PRIVATE added to it. */
30298 static bool
30299 cp_parser_omp_for_loop_init (cp_parser *parser,
30300 enum tree_code code,
30301 tree &this_pre_body,
30302 vec<tree, va_gc> *for_block,
30303 tree &init,
30304 tree &decl,
30305 tree &real_decl)
30307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30308 return false;
30310 bool add_private_clause = false;
30312 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30314 init-expr:
30315 var = lb
30316 integer-type var = lb
30317 random-access-iterator-type var = lb
30318 pointer-type var = lb
30320 cp_decl_specifier_seq type_specifiers;
30322 /* First, try to parse as an initialized declaration. See
30323 cp_parser_condition, from whence the bulk of this is copied. */
30325 cp_parser_parse_tentatively (parser);
30326 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30327 /*is_trailing_return=*/false,
30328 &type_specifiers);
30329 if (cp_parser_parse_definitely (parser))
30331 /* If parsing a type specifier seq succeeded, then this
30332 MUST be a initialized declaration. */
30333 tree asm_specification, attributes;
30334 cp_declarator *declarator;
30336 declarator = cp_parser_declarator (parser,
30337 CP_PARSER_DECLARATOR_NAMED,
30338 /*ctor_dtor_or_conv_p=*/NULL,
30339 /*parenthesized_p=*/NULL,
30340 /*member_p=*/false,
30341 /*friend_p=*/false);
30342 attributes = cp_parser_attributes_opt (parser);
30343 asm_specification = cp_parser_asm_specification_opt (parser);
30345 if (declarator == cp_error_declarator)
30346 cp_parser_skip_to_end_of_statement (parser);
30348 else
30350 tree pushed_scope, auto_node;
30352 decl = start_decl (declarator, &type_specifiers,
30353 SD_INITIALIZED, attributes,
30354 /*prefix_attributes=*/NULL_TREE,
30355 &pushed_scope);
30357 auto_node = type_uses_auto (TREE_TYPE (decl));
30358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30360 if (cp_lexer_next_token_is (parser->lexer,
30361 CPP_OPEN_PAREN))
30363 if (code != CILK_SIMD && code != CILK_FOR)
30364 error ("parenthesized initialization is not allowed in "
30365 "OpenMP %<for%> loop");
30366 else
30367 error ("parenthesized initialization is "
30368 "not allowed in for-loop");
30370 else
30371 /* Trigger an error. */
30372 cp_parser_require (parser, CPP_EQ, RT_EQ);
30374 init = error_mark_node;
30375 cp_parser_skip_to_end_of_statement (parser);
30377 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30378 || type_dependent_expression_p (decl)
30379 || auto_node)
30381 bool is_direct_init, is_non_constant_init;
30383 init = cp_parser_initializer (parser,
30384 &is_direct_init,
30385 &is_non_constant_init);
30387 if (auto_node)
30389 TREE_TYPE (decl)
30390 = do_auto_deduction (TREE_TYPE (decl), init,
30391 auto_node);
30393 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30394 && !type_dependent_expression_p (decl))
30395 goto non_class;
30398 cp_finish_decl (decl, init, !is_non_constant_init,
30399 asm_specification,
30400 LOOKUP_ONLYCONVERTING);
30401 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30403 vec_safe_push (for_block, this_pre_body);
30404 init = NULL_TREE;
30406 else
30407 init = pop_stmt_list (this_pre_body);
30408 this_pre_body = NULL_TREE;
30410 else
30412 /* Consume '='. */
30413 cp_lexer_consume_token (parser->lexer);
30414 init = cp_parser_assignment_expression (parser);
30416 non_class:
30417 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30418 init = error_mark_node;
30419 else
30420 cp_finish_decl (decl, NULL_TREE,
30421 /*init_const_expr_p=*/false,
30422 asm_specification,
30423 LOOKUP_ONLYCONVERTING);
30426 if (pushed_scope)
30427 pop_scope (pushed_scope);
30430 else
30432 cp_id_kind idk;
30433 /* If parsing a type specifier sequence failed, then
30434 this MUST be a simple expression. */
30435 if (code == CILK_FOR)
30436 error ("%<_Cilk_for%> allows expression instead of declaration only "
30437 "in C, not in C++");
30438 cp_parser_parse_tentatively (parser);
30439 decl = cp_parser_primary_expression (parser, false, false,
30440 false, &idk);
30441 if (!cp_parser_error_occurred (parser)
30442 && decl
30443 && DECL_P (decl)
30444 && CLASS_TYPE_P (TREE_TYPE (decl)))
30446 tree rhs;
30448 cp_parser_parse_definitely (parser);
30449 cp_parser_require (parser, CPP_EQ, RT_EQ);
30450 rhs = cp_parser_assignment_expression (parser);
30451 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30452 decl, NOP_EXPR,
30453 rhs,
30454 tf_warning_or_error));
30455 add_private_clause = true;
30457 else
30459 decl = NULL;
30460 cp_parser_abort_tentative_parse (parser);
30461 init = cp_parser_expression (parser);
30462 if (init)
30464 if (TREE_CODE (init) == MODIFY_EXPR
30465 || TREE_CODE (init) == MODOP_EXPR)
30466 real_decl = TREE_OPERAND (init, 0);
30470 return add_private_clause;
30473 /* Parse the restricted form of the for statement allowed by OpenMP. */
30475 static tree
30476 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30477 tree *cclauses)
30479 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30480 tree real_decl, initv, condv, incrv, declv;
30481 tree this_pre_body, cl;
30482 location_t loc_first;
30483 bool collapse_err = false;
30484 int i, collapse = 1, nbraces = 0;
30485 vec<tree, va_gc> *for_block = make_tree_vector ();
30487 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30488 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30489 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30491 gcc_assert (collapse >= 1);
30493 declv = make_tree_vec (collapse);
30494 initv = make_tree_vec (collapse);
30495 condv = make_tree_vec (collapse);
30496 incrv = make_tree_vec (collapse);
30498 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30500 for (i = 0; i < collapse; i++)
30502 int bracecount = 0;
30503 bool add_private_clause = false;
30504 location_t loc;
30506 if (code != CILK_FOR
30507 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30509 cp_parser_error (parser, "for statement expected");
30510 return NULL;
30512 if (code == CILK_FOR
30513 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30515 cp_parser_error (parser, "_Cilk_for statement expected");
30516 return NULL;
30518 loc = cp_lexer_consume_token (parser->lexer)->location;
30520 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30521 return NULL;
30523 init = decl = real_decl = NULL;
30524 this_pre_body = push_stmt_list ();
30526 add_private_clause
30527 |= cp_parser_omp_for_loop_init (parser, code,
30528 this_pre_body, for_block,
30529 init, decl, real_decl);
30531 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30532 if (this_pre_body)
30534 this_pre_body = pop_stmt_list (this_pre_body);
30535 if (pre_body)
30537 tree t = pre_body;
30538 pre_body = push_stmt_list ();
30539 add_stmt (t);
30540 add_stmt (this_pre_body);
30541 pre_body = pop_stmt_list (pre_body);
30543 else
30544 pre_body = this_pre_body;
30547 if (decl)
30548 real_decl = decl;
30549 if (cclauses != NULL
30550 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30551 && real_decl != NULL_TREE)
30553 tree *c;
30554 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30555 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30556 && OMP_CLAUSE_DECL (*c) == real_decl)
30558 error_at (loc, "iteration variable %qD"
30559 " should not be firstprivate", real_decl);
30560 *c = OMP_CLAUSE_CHAIN (*c);
30562 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30563 && OMP_CLAUSE_DECL (*c) == real_decl)
30565 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
30566 tree l = *c;
30567 *c = OMP_CLAUSE_CHAIN (*c);
30568 if (code == OMP_SIMD)
30570 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30571 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30573 else
30575 OMP_CLAUSE_CHAIN (l) = clauses;
30576 clauses = l;
30578 add_private_clause = false;
30580 else
30582 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30583 && OMP_CLAUSE_DECL (*c) == real_decl)
30584 add_private_clause = false;
30585 c = &OMP_CLAUSE_CHAIN (*c);
30589 if (add_private_clause)
30591 tree c;
30592 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30594 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30595 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30596 && OMP_CLAUSE_DECL (c) == decl)
30597 break;
30598 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30599 && OMP_CLAUSE_DECL (c) == decl)
30600 error_at (loc, "iteration variable %qD "
30601 "should not be firstprivate",
30602 decl);
30603 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30604 && OMP_CLAUSE_DECL (c) == decl)
30605 error_at (loc, "iteration variable %qD should not be reduction",
30606 decl);
30608 if (c == NULL)
30610 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30611 OMP_CLAUSE_DECL (c) = decl;
30612 c = finish_omp_clauses (c);
30613 if (c)
30615 OMP_CLAUSE_CHAIN (c) = clauses;
30616 clauses = c;
30621 cond = NULL;
30622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30623 cond = cp_parser_omp_for_cond (parser, decl, code);
30624 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30626 incr = NULL;
30627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30629 /* If decl is an iterator, preserve the operator on decl
30630 until finish_omp_for. */
30631 if (real_decl
30632 && ((processing_template_decl
30633 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30634 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30635 incr = cp_parser_omp_for_incr (parser, real_decl);
30636 else
30637 incr = cp_parser_expression (parser);
30638 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30639 SET_EXPR_LOCATION (incr, input_location);
30642 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30643 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30644 /*or_comma=*/false,
30645 /*consume_paren=*/true);
30647 TREE_VEC_ELT (declv, i) = decl;
30648 TREE_VEC_ELT (initv, i) = init;
30649 TREE_VEC_ELT (condv, i) = cond;
30650 TREE_VEC_ELT (incrv, i) = incr;
30652 if (i == collapse - 1)
30653 break;
30655 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30656 in between the collapsed for loops to be still considered perfectly
30657 nested. Hopefully the final version clarifies this.
30658 For now handle (multiple) {'s and empty statements. */
30659 cp_parser_parse_tentatively (parser);
30662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30663 break;
30664 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30666 cp_lexer_consume_token (parser->lexer);
30667 bracecount++;
30669 else if (bracecount
30670 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30671 cp_lexer_consume_token (parser->lexer);
30672 else
30674 loc = cp_lexer_peek_token (parser->lexer)->location;
30675 error_at (loc, "not enough collapsed for loops");
30676 collapse_err = true;
30677 cp_parser_abort_tentative_parse (parser);
30678 declv = NULL_TREE;
30679 break;
30682 while (1);
30684 if (declv)
30686 cp_parser_parse_definitely (parser);
30687 nbraces += bracecount;
30691 /* Note that we saved the original contents of this flag when we entered
30692 the structured block, and so we don't need to re-save it here. */
30693 if (code == CILK_SIMD || code == CILK_FOR)
30694 parser->in_statement = IN_CILK_SIMD_FOR;
30695 else
30696 parser->in_statement = IN_OMP_FOR;
30698 /* Note that the grammar doesn't call for a structured block here,
30699 though the loop as a whole is a structured block. */
30700 body = push_stmt_list ();
30701 cp_parser_statement (parser, NULL_TREE, false, NULL);
30702 body = pop_stmt_list (body);
30704 if (declv == NULL_TREE)
30705 ret = NULL_TREE;
30706 else
30707 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30708 pre_body, clauses);
30710 while (nbraces)
30712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30714 cp_lexer_consume_token (parser->lexer);
30715 nbraces--;
30717 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30718 cp_lexer_consume_token (parser->lexer);
30719 else
30721 if (!collapse_err)
30723 error_at (cp_lexer_peek_token (parser->lexer)->location,
30724 "collapsed loops not perfectly nested");
30726 collapse_err = true;
30727 cp_parser_statement_seq_opt (parser, NULL);
30728 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30729 break;
30733 while (!for_block->is_empty ())
30734 add_stmt (pop_stmt_list (for_block->pop ()));
30735 release_tree_vector (for_block);
30737 return ret;
30740 /* Helper function for OpenMP parsing, split clauses and call
30741 finish_omp_clauses on each of the set of clauses afterwards. */
30743 static void
30744 cp_omp_split_clauses (location_t loc, enum tree_code code,
30745 omp_clause_mask mask, tree clauses, tree *cclauses)
30747 int i;
30748 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30749 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30750 if (cclauses[i])
30751 cclauses[i] = finish_omp_clauses (cclauses[i]);
30754 /* OpenMP 4.0:
30755 #pragma omp simd simd-clause[optseq] new-line
30756 for-loop */
30758 #define OMP_SIMD_CLAUSE_MASK \
30759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30767 static tree
30768 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30769 char *p_name, omp_clause_mask mask, tree *cclauses)
30771 tree clauses, sb, ret;
30772 unsigned int save;
30773 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30775 strcat (p_name, " simd");
30776 mask |= OMP_SIMD_CLAUSE_MASK;
30777 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30780 cclauses == NULL);
30781 if (cclauses)
30783 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30787 sb = begin_omp_structured_block ();
30788 save = cp_parser_begin_omp_structured_block (parser);
30790 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30792 cp_parser_end_omp_structured_block (parser, save);
30793 add_stmt (finish_omp_structured_block (sb));
30795 return ret;
30798 /* OpenMP 2.5:
30799 #pragma omp for for-clause[optseq] new-line
30800 for-loop
30802 OpenMP 4.0:
30803 #pragma omp for simd for-simd-clause[optseq] new-line
30804 for-loop */
30806 #define OMP_FOR_CLAUSE_MASK \
30807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30816 static tree
30817 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30818 char *p_name, omp_clause_mask mask, tree *cclauses)
30820 tree clauses, sb, ret;
30821 unsigned int save;
30822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30824 strcat (p_name, " for");
30825 mask |= OMP_FOR_CLAUSE_MASK;
30826 if (cclauses)
30827 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30829 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30831 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30832 const char *p = IDENTIFIER_POINTER (id);
30834 if (strcmp (p, "simd") == 0)
30836 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30837 if (cclauses == NULL)
30838 cclauses = cclauses_buf;
30840 cp_lexer_consume_token (parser->lexer);
30841 if (!flag_openmp) /* flag_openmp_simd */
30842 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30843 cclauses);
30844 sb = begin_omp_structured_block ();
30845 save = cp_parser_begin_omp_structured_block (parser);
30846 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30847 cclauses);
30848 cp_parser_end_omp_structured_block (parser, save);
30849 tree body = finish_omp_structured_block (sb);
30850 if (ret == NULL)
30851 return ret;
30852 ret = make_node (OMP_FOR);
30853 TREE_TYPE (ret) = void_type_node;
30854 OMP_FOR_BODY (ret) = body;
30855 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30856 SET_EXPR_LOCATION (ret, loc);
30857 add_stmt (ret);
30858 return ret;
30861 if (!flag_openmp) /* flag_openmp_simd */
30863 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30864 return NULL_TREE;
30867 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30868 cclauses == NULL);
30869 if (cclauses)
30871 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30872 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30875 sb = begin_omp_structured_block ();
30876 save = cp_parser_begin_omp_structured_block (parser);
30878 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30880 cp_parser_end_omp_structured_block (parser, save);
30881 add_stmt (finish_omp_structured_block (sb));
30883 return ret;
30886 /* OpenMP 2.5:
30887 # pragma omp master new-line
30888 structured-block */
30890 static tree
30891 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30893 cp_parser_require_pragma_eol (parser, pragma_tok);
30894 return c_finish_omp_master (input_location,
30895 cp_parser_omp_structured_block (parser));
30898 /* OpenMP 2.5:
30899 # pragma omp ordered new-line
30900 structured-block */
30902 static tree
30903 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30905 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30906 cp_parser_require_pragma_eol (parser, pragma_tok);
30907 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30910 /* OpenMP 2.5:
30912 section-scope:
30913 { section-sequence }
30915 section-sequence:
30916 section-directive[opt] structured-block
30917 section-sequence section-directive structured-block */
30919 static tree
30920 cp_parser_omp_sections_scope (cp_parser *parser)
30922 tree stmt, substmt;
30923 bool error_suppress = false;
30924 cp_token *tok;
30926 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30927 return NULL_TREE;
30929 stmt = push_stmt_list ();
30931 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30933 substmt = cp_parser_omp_structured_block (parser);
30934 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30935 add_stmt (substmt);
30938 while (1)
30940 tok = cp_lexer_peek_token (parser->lexer);
30941 if (tok->type == CPP_CLOSE_BRACE)
30942 break;
30943 if (tok->type == CPP_EOF)
30944 break;
30946 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30948 cp_lexer_consume_token (parser->lexer);
30949 cp_parser_require_pragma_eol (parser, tok);
30950 error_suppress = false;
30952 else if (!error_suppress)
30954 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30955 error_suppress = true;
30958 substmt = cp_parser_omp_structured_block (parser);
30959 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30960 add_stmt (substmt);
30962 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30964 substmt = pop_stmt_list (stmt);
30966 stmt = make_node (OMP_SECTIONS);
30967 TREE_TYPE (stmt) = void_type_node;
30968 OMP_SECTIONS_BODY (stmt) = substmt;
30970 add_stmt (stmt);
30971 return stmt;
30974 /* OpenMP 2.5:
30975 # pragma omp sections sections-clause[optseq] newline
30976 sections-scope */
30978 #define OMP_SECTIONS_CLAUSE_MASK \
30979 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30985 static tree
30986 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30987 char *p_name, omp_clause_mask mask, tree *cclauses)
30989 tree clauses, ret;
30990 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30992 strcat (p_name, " sections");
30993 mask |= OMP_SECTIONS_CLAUSE_MASK;
30994 if (cclauses)
30995 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30997 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30998 cclauses == NULL);
30999 if (cclauses)
31001 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31002 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31005 ret = cp_parser_omp_sections_scope (parser);
31006 if (ret)
31007 OMP_SECTIONS_CLAUSES (ret) = clauses;
31009 return ret;
31012 /* OpenMP 2.5:
31013 # pragma omp parallel parallel-clause[optseq] new-line
31014 structured-block
31015 # pragma omp parallel for parallel-for-clause[optseq] new-line
31016 structured-block
31017 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31018 structured-block
31020 OpenMP 4.0:
31021 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31022 structured-block */
31024 #define OMP_PARALLEL_CLAUSE_MASK \
31025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31035 static tree
31036 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31037 char *p_name, omp_clause_mask mask, tree *cclauses)
31039 tree stmt, clauses, block;
31040 unsigned int save;
31041 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31043 strcat (p_name, " parallel");
31044 mask |= OMP_PARALLEL_CLAUSE_MASK;
31046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31048 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31049 if (cclauses == NULL)
31050 cclauses = cclauses_buf;
31052 cp_lexer_consume_token (parser->lexer);
31053 if (!flag_openmp) /* flag_openmp_simd */
31054 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31055 block = begin_omp_parallel ();
31056 save = cp_parser_begin_omp_structured_block (parser);
31057 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31058 cp_parser_end_omp_structured_block (parser, save);
31059 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31060 block);
31061 if (ret == NULL_TREE)
31062 return ret;
31063 OMP_PARALLEL_COMBINED (stmt) = 1;
31064 return stmt;
31066 else if (cclauses)
31068 error_at (loc, "expected %<for%> after %qs", p_name);
31069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31070 return NULL_TREE;
31072 else if (!flag_openmp) /* flag_openmp_simd */
31074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31075 return NULL_TREE;
31077 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31079 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31080 const char *p = IDENTIFIER_POINTER (id);
31081 if (strcmp (p, "sections") == 0)
31083 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31084 cclauses = cclauses_buf;
31086 cp_lexer_consume_token (parser->lexer);
31087 block = begin_omp_parallel ();
31088 save = cp_parser_begin_omp_structured_block (parser);
31089 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31090 cp_parser_end_omp_structured_block (parser, save);
31091 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31092 block);
31093 OMP_PARALLEL_COMBINED (stmt) = 1;
31094 return stmt;
31098 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31100 block = begin_omp_parallel ();
31101 save = cp_parser_begin_omp_structured_block (parser);
31102 cp_parser_statement (parser, NULL_TREE, false, NULL);
31103 cp_parser_end_omp_structured_block (parser, save);
31104 stmt = finish_omp_parallel (clauses, block);
31105 return stmt;
31108 /* OpenMP 2.5:
31109 # pragma omp single single-clause[optseq] new-line
31110 structured-block */
31112 #define OMP_SINGLE_CLAUSE_MASK \
31113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31118 static tree
31119 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31121 tree stmt = make_node (OMP_SINGLE);
31122 TREE_TYPE (stmt) = void_type_node;
31124 OMP_SINGLE_CLAUSES (stmt)
31125 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31126 "#pragma omp single", pragma_tok);
31127 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31129 return add_stmt (stmt);
31132 /* OpenMP 3.0:
31133 # pragma omp task task-clause[optseq] new-line
31134 structured-block */
31136 #define OMP_TASK_CLAUSE_MASK \
31137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31147 static tree
31148 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31150 tree clauses, block;
31151 unsigned int save;
31153 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31154 "#pragma omp task", pragma_tok);
31155 block = begin_omp_task ();
31156 save = cp_parser_begin_omp_structured_block (parser);
31157 cp_parser_statement (parser, NULL_TREE, false, NULL);
31158 cp_parser_end_omp_structured_block (parser, save);
31159 return finish_omp_task (clauses, block);
31162 /* OpenMP 3.0:
31163 # pragma omp taskwait new-line */
31165 static void
31166 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31168 cp_parser_require_pragma_eol (parser, pragma_tok);
31169 finish_omp_taskwait ();
31172 /* OpenMP 3.1:
31173 # pragma omp taskyield new-line */
31175 static void
31176 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31178 cp_parser_require_pragma_eol (parser, pragma_tok);
31179 finish_omp_taskyield ();
31182 /* OpenMP 4.0:
31183 # pragma omp taskgroup new-line
31184 structured-block */
31186 static tree
31187 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31189 cp_parser_require_pragma_eol (parser, pragma_tok);
31190 return c_finish_omp_taskgroup (input_location,
31191 cp_parser_omp_structured_block (parser));
31195 /* OpenMP 2.5:
31196 # pragma omp threadprivate (variable-list) */
31198 static void
31199 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31201 tree vars;
31203 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31204 cp_parser_require_pragma_eol (parser, pragma_tok);
31206 finish_omp_threadprivate (vars);
31209 /* OpenMP 4.0:
31210 # pragma omp cancel cancel-clause[optseq] new-line */
31212 #define OMP_CANCEL_CLAUSE_MASK \
31213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31219 static void
31220 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31222 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31223 "#pragma omp cancel", pragma_tok);
31224 finish_omp_cancel (clauses);
31227 /* OpenMP 4.0:
31228 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31230 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31231 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31236 static void
31237 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31239 tree clauses;
31240 bool point_seen = false;
31242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31245 const char *p = IDENTIFIER_POINTER (id);
31247 if (strcmp (p, "point") == 0)
31249 cp_lexer_consume_token (parser->lexer);
31250 point_seen = true;
31253 if (!point_seen)
31255 cp_parser_error (parser, "expected %<point%>");
31256 cp_parser_require_pragma_eol (parser, pragma_tok);
31257 return;
31260 clauses = cp_parser_omp_all_clauses (parser,
31261 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31262 "#pragma omp cancellation point",
31263 pragma_tok);
31264 finish_omp_cancellation_point (clauses);
31267 /* OpenMP 4.0:
31268 #pragma omp distribute distribute-clause[optseq] new-line
31269 for-loop */
31271 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31277 static tree
31278 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31279 char *p_name, omp_clause_mask mask, tree *cclauses)
31281 tree clauses, sb, ret;
31282 unsigned int save;
31283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31285 strcat (p_name, " distribute");
31286 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31288 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31290 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31291 const char *p = IDENTIFIER_POINTER (id);
31292 bool simd = false;
31293 bool parallel = false;
31295 if (strcmp (p, "simd") == 0)
31296 simd = true;
31297 else
31298 parallel = strcmp (p, "parallel") == 0;
31299 if (parallel || simd)
31301 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31302 if (cclauses == NULL)
31303 cclauses = cclauses_buf;
31304 cp_lexer_consume_token (parser->lexer);
31305 if (!flag_openmp) /* flag_openmp_simd */
31307 if (simd)
31308 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31309 cclauses);
31310 else
31311 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31312 cclauses);
31314 sb = begin_omp_structured_block ();
31315 save = cp_parser_begin_omp_structured_block (parser);
31316 if (simd)
31317 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31318 cclauses);
31319 else
31320 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31321 cclauses);
31322 cp_parser_end_omp_structured_block (parser, save);
31323 tree body = finish_omp_structured_block (sb);
31324 if (ret == NULL)
31325 return ret;
31326 ret = make_node (OMP_DISTRIBUTE);
31327 TREE_TYPE (ret) = void_type_node;
31328 OMP_FOR_BODY (ret) = body;
31329 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31330 SET_EXPR_LOCATION (ret, loc);
31331 add_stmt (ret);
31332 return ret;
31335 if (!flag_openmp) /* flag_openmp_simd */
31337 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31338 return NULL_TREE;
31341 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31342 cclauses == NULL);
31343 if (cclauses)
31345 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31346 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31349 sb = begin_omp_structured_block ();
31350 save = cp_parser_begin_omp_structured_block (parser);
31352 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31354 cp_parser_end_omp_structured_block (parser, save);
31355 add_stmt (finish_omp_structured_block (sb));
31357 return ret;
31360 /* OpenMP 4.0:
31361 # pragma omp teams teams-clause[optseq] new-line
31362 structured-block */
31364 #define OMP_TEAMS_CLAUSE_MASK \
31365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31373 static tree
31374 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31375 char *p_name, omp_clause_mask mask, tree *cclauses)
31377 tree clauses, sb, ret;
31378 unsigned int save;
31379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31381 strcat (p_name, " teams");
31382 mask |= OMP_TEAMS_CLAUSE_MASK;
31384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31387 const char *p = IDENTIFIER_POINTER (id);
31388 if (strcmp (p, "distribute") == 0)
31390 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31391 if (cclauses == NULL)
31392 cclauses = cclauses_buf;
31394 cp_lexer_consume_token (parser->lexer);
31395 if (!flag_openmp) /* flag_openmp_simd */
31396 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31397 cclauses);
31398 sb = begin_omp_structured_block ();
31399 save = cp_parser_begin_omp_structured_block (parser);
31400 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31401 cclauses);
31402 cp_parser_end_omp_structured_block (parser, save);
31403 tree body = finish_omp_structured_block (sb);
31404 if (ret == NULL)
31405 return ret;
31406 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31407 ret = make_node (OMP_TEAMS);
31408 TREE_TYPE (ret) = void_type_node;
31409 OMP_TEAMS_CLAUSES (ret) = clauses;
31410 OMP_TEAMS_BODY (ret) = body;
31411 OMP_TEAMS_COMBINED (ret) = 1;
31412 return add_stmt (ret);
31415 if (!flag_openmp) /* flag_openmp_simd */
31417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31418 return NULL_TREE;
31421 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31422 cclauses == NULL);
31423 if (cclauses)
31425 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31426 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31429 tree stmt = make_node (OMP_TEAMS);
31430 TREE_TYPE (stmt) = void_type_node;
31431 OMP_TEAMS_CLAUSES (stmt) = clauses;
31432 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31434 return add_stmt (stmt);
31437 /* OpenMP 4.0:
31438 # pragma omp target data target-data-clause[optseq] new-line
31439 structured-block */
31441 #define OMP_TARGET_DATA_CLAUSE_MASK \
31442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31446 static tree
31447 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31449 tree stmt = make_node (OMP_TARGET_DATA);
31450 TREE_TYPE (stmt) = void_type_node;
31452 OMP_TARGET_DATA_CLAUSES (stmt)
31453 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31454 "#pragma omp target data", pragma_tok);
31455 keep_next_level (true);
31456 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31458 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31459 return add_stmt (stmt);
31462 /* OpenMP 4.0:
31463 # pragma omp target update target-update-clause[optseq] new-line */
31465 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31466 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31471 static bool
31472 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31473 enum pragma_context context)
31475 if (context == pragma_stmt)
31477 error_at (pragma_tok->location,
31478 "%<#pragma omp target update%> may only be "
31479 "used in compound statements");
31480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31481 return false;
31484 tree clauses
31485 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31486 "#pragma omp target update", pragma_tok);
31487 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31488 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31490 error_at (pragma_tok->location,
31491 "%<#pragma omp target update%> must contain at least one "
31492 "%<from%> or %<to%> clauses");
31493 return false;
31496 tree stmt = make_node (OMP_TARGET_UPDATE);
31497 TREE_TYPE (stmt) = void_type_node;
31498 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31499 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31500 add_stmt (stmt);
31501 return false;
31504 /* OpenMP 4.0:
31505 # pragma omp target target-clause[optseq] new-line
31506 structured-block */
31508 #define OMP_TARGET_CLAUSE_MASK \
31509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31513 static bool
31514 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31515 enum pragma_context context)
31517 if (context != pragma_stmt && context != pragma_compound)
31519 cp_parser_error (parser, "expected declaration specifiers");
31520 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31521 return false;
31524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31527 const char *p = IDENTIFIER_POINTER (id);
31529 if (strcmp (p, "teams") == 0)
31531 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31532 char p_name[sizeof ("#pragma omp target teams distribute "
31533 "parallel for simd")];
31535 cp_lexer_consume_token (parser->lexer);
31536 strcpy (p_name, "#pragma omp target");
31537 if (!flag_openmp) /* flag_openmp_simd */
31539 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31540 OMP_TARGET_CLAUSE_MASK,
31541 cclauses);
31542 return stmt != NULL_TREE;
31544 keep_next_level (true);
31545 tree sb = begin_omp_structured_block ();
31546 unsigned save = cp_parser_begin_omp_structured_block (parser);
31547 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31548 OMP_TARGET_CLAUSE_MASK, cclauses);
31549 cp_parser_end_omp_structured_block (parser, save);
31550 tree body = finish_omp_structured_block (sb);
31551 if (ret == NULL_TREE)
31552 return false;
31553 tree stmt = make_node (OMP_TARGET);
31554 TREE_TYPE (stmt) = void_type_node;
31555 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31556 OMP_TARGET_BODY (stmt) = body;
31557 add_stmt (stmt);
31558 return true;
31560 else if (!flag_openmp) /* flag_openmp_simd */
31562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31563 return false;
31565 else if (strcmp (p, "data") == 0)
31567 cp_lexer_consume_token (parser->lexer);
31568 cp_parser_omp_target_data (parser, pragma_tok);
31569 return true;
31571 else if (strcmp (p, "update") == 0)
31573 cp_lexer_consume_token (parser->lexer);
31574 return cp_parser_omp_target_update (parser, pragma_tok, context);
31578 tree stmt = make_node (OMP_TARGET);
31579 TREE_TYPE (stmt) = void_type_node;
31581 OMP_TARGET_CLAUSES (stmt)
31582 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31583 "#pragma omp target", pragma_tok);
31584 keep_next_level (true);
31585 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31587 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31588 add_stmt (stmt);
31589 return true;
31592 /* OpenACC 2.0:
31593 # pragma acc cache (variable-list) new-line
31596 static tree
31597 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31599 tree stmt, clauses;
31601 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31602 clauses = finish_omp_clauses (clauses);
31604 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31606 stmt = make_node (OACC_CACHE);
31607 TREE_TYPE (stmt) = void_type_node;
31608 OACC_CACHE_CLAUSES (stmt) = clauses;
31609 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31610 add_stmt (stmt);
31612 return stmt;
31615 /* OpenACC 2.0:
31616 # pragma acc data oacc-data-clause[optseq] new-line
31617 structured-block */
31619 #define OACC_DATA_CLAUSE_MASK \
31620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31632 static tree
31633 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31635 tree stmt, clauses, block;
31636 unsigned int save;
31638 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31639 "#pragma acc data", pragma_tok);
31641 block = begin_omp_parallel ();
31642 save = cp_parser_begin_omp_structured_block (parser);
31643 cp_parser_statement (parser, NULL_TREE, false, NULL);
31644 cp_parser_end_omp_structured_block (parser, save);
31645 stmt = finish_oacc_data (clauses, block);
31646 return stmt;
31649 /* OpenACC 2.0:
31650 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31654 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31656 LOC is the location of the #pragma token.
31659 #define OACC_ENTER_DATA_CLAUSE_MASK \
31660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31668 #define OACC_EXIT_DATA_CLAUSE_MASK \
31669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31675 static tree
31676 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31677 bool enter)
31679 tree stmt, clauses;
31681 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31682 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31684 cp_parser_error (parser, enter
31685 ? "expected %<data%> in %<#pragma acc enter data%>"
31686 : "expected %<data%> in %<#pragma acc exit data%>");
31687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31688 return NULL_TREE;
31691 const char *p =
31692 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31693 if (strcmp (p, "data") != 0)
31695 cp_parser_error (parser, "invalid pragma");
31696 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31697 return NULL_TREE;
31700 cp_lexer_consume_token (parser->lexer);
31702 if (enter)
31703 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31704 "#pragma acc enter data", pragma_tok);
31705 else
31706 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31707 "#pragma acc exit data", pragma_tok);
31709 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31711 error_at (pragma_tok->location,
31712 "%<#pragma acc enter data%> has no data movement clause");
31713 return NULL_TREE;
31716 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31717 TREE_TYPE (stmt) = void_type_node;
31718 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31719 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31720 add_stmt (stmt);
31721 return stmt;
31724 /* OpenACC 2.0:
31725 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31726 structured-block */
31728 #define OACC_KERNELS_CLAUSE_MASK \
31729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31743 static tree
31744 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31746 tree stmt, clauses, block;
31747 unsigned int save;
31749 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31750 "#pragma acc kernels", pragma_tok);
31752 block = begin_omp_parallel ();
31753 save = cp_parser_begin_omp_structured_block (parser);
31754 cp_parser_statement (parser, NULL_TREE, false, NULL);
31755 cp_parser_end_omp_structured_block (parser, save);
31756 stmt = finish_oacc_kernels (clauses, block);
31757 return stmt;
31760 /* OpenACC 2.0:
31761 # pragma acc loop oacc-loop-clause[optseq] new-line
31762 structured-block */
31764 #define OACC_LOOP_CLAUSE_MASK \
31765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31768 static tree
31769 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31771 tree stmt, clauses, block;
31772 int save;
31774 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31775 "#pragma acc loop", pragma_tok);
31777 block = begin_omp_structured_block ();
31778 save = cp_parser_begin_omp_structured_block (parser);
31779 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31780 cp_parser_end_omp_structured_block (parser, save);
31781 add_stmt (finish_omp_structured_block (block));
31782 return stmt;
31785 /* OpenACC 2.0:
31786 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31787 structured-block */
31789 #define OACC_PARALLEL_CLAUSE_MASK \
31790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31808 static tree
31809 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31811 tree stmt, clauses, block;
31812 unsigned int save;
31814 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31815 "#pragma acc parallel", pragma_tok);
31817 block = begin_omp_parallel ();
31818 save = cp_parser_begin_omp_structured_block (parser);
31819 cp_parser_statement (parser, NULL_TREE, false, NULL);
31820 cp_parser_end_omp_structured_block (parser, save);
31821 stmt = finish_oacc_parallel (clauses, block);
31822 return stmt;
31825 /* OpenACC 2.0:
31826 # pragma acc update oacc-update-clause[optseq] new-line
31829 #define OACC_UPDATE_CLAUSE_MASK \
31830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31837 static tree
31838 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31840 tree stmt, clauses;
31842 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31843 "#pragma acc update", pragma_tok);
31845 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31847 error_at (pragma_tok->location,
31848 "%<#pragma acc update%> must contain at least one "
31849 "%<device%> or %<host/self%> clause");
31850 return NULL_TREE;
31853 stmt = make_node (OACC_UPDATE);
31854 TREE_TYPE (stmt) = void_type_node;
31855 OACC_UPDATE_CLAUSES (stmt) = clauses;
31856 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31857 add_stmt (stmt);
31858 return stmt;
31861 /* OpenACC 2.0:
31862 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31864 LOC is the location of the #pragma token.
31867 #define OACC_WAIT_CLAUSE_MASK \
31868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31870 static tree
31871 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31873 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31876 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31877 list = cp_parser_oacc_wait_list (parser, loc, list);
31879 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31880 "#pragma acc wait", pragma_tok);
31882 stmt = c_finish_oacc_wait (loc, list, clauses);
31884 return stmt;
31887 /* OpenMP 4.0:
31888 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31890 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31898 static void
31899 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31900 enum pragma_context context)
31902 bool first_p = parser->omp_declare_simd == NULL;
31903 cp_omp_declare_simd_data data;
31904 if (first_p)
31906 data.error_seen = false;
31907 data.fndecl_seen = false;
31908 data.tokens = vNULL;
31909 parser->omp_declare_simd = &data;
31911 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31912 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31913 cp_lexer_consume_token (parser->lexer);
31914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31915 parser->omp_declare_simd->error_seen = true;
31916 cp_parser_require_pragma_eol (parser, pragma_tok);
31917 struct cp_token_cache *cp
31918 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31919 parser->omp_declare_simd->tokens.safe_push (cp);
31920 if (first_p)
31922 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31923 cp_parser_pragma (parser, context);
31924 switch (context)
31926 case pragma_external:
31927 cp_parser_declaration (parser);
31928 break;
31929 case pragma_member:
31930 cp_parser_member_declaration (parser);
31931 break;
31932 case pragma_objc_icode:
31933 cp_parser_block_declaration (parser, /*statement_p=*/false);
31934 break;
31935 default:
31936 cp_parser_declaration_statement (parser);
31937 break;
31939 if (parser->omp_declare_simd
31940 && !parser->omp_declare_simd->error_seen
31941 && !parser->omp_declare_simd->fndecl_seen)
31942 error_at (pragma_tok->location,
31943 "%<#pragma omp declare simd%> not immediately followed by "
31944 "function declaration or definition");
31945 data.tokens.release ();
31946 parser->omp_declare_simd = NULL;
31950 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31951 This function is modelled similar to the late parsing of omp declare
31952 simd. */
31954 static tree
31955 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31957 struct cp_token_cache *ce;
31958 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31959 int ii = 0;
31961 if (parser->omp_declare_simd != NULL)
31963 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31964 " marked as a Cilk Plus SIMD-enabled function");
31965 XDELETE (parser->cilk_simd_fn_info);
31966 parser->cilk_simd_fn_info = NULL;
31967 return attrs;
31969 if (!info->error_seen && info->fndecl_seen)
31971 error ("vector attribute not immediately followed by a single function"
31972 " declaration or definition");
31973 info->error_seen = true;
31975 if (info->error_seen)
31976 return attrs;
31978 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31980 tree c, cl;
31982 cp_parser_push_lexer_for_tokens (parser, ce);
31983 parser->lexer->in_pragma = true;
31984 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31985 "SIMD-enabled functions attribute",
31986 NULL);
31987 cp_parser_pop_lexer (parser);
31988 if (cl)
31989 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31991 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31992 TREE_CHAIN (c) = attrs;
31993 attrs = c;
31995 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31996 TREE_CHAIN (c) = attrs;
31997 if (processing_template_decl)
31998 ATTR_IS_DEPENDENT (c) = 1;
31999 attrs = c;
32001 info->fndecl_seen = true;
32002 XDELETE (parser->cilk_simd_fn_info);
32003 parser->cilk_simd_fn_info = NULL;
32004 return attrs;
32007 /* Finalize #pragma omp declare simd clauses after direct declarator has
32008 been parsed, and put that into "omp declare simd" attribute. */
32010 static tree
32011 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
32013 struct cp_token_cache *ce;
32014 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32015 int i;
32017 if (!data->error_seen && data->fndecl_seen)
32019 error ("%<#pragma omp declare simd%> not immediately followed by "
32020 "a single function declaration or definition");
32021 data->error_seen = true;
32022 return attrs;
32024 if (data->error_seen)
32025 return attrs;
32027 FOR_EACH_VEC_ELT (data->tokens, i, ce)
32029 tree c, cl;
32031 cp_parser_push_lexer_for_tokens (parser, ce);
32032 parser->lexer->in_pragma = true;
32033 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32034 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32035 cp_lexer_consume_token (parser->lexer);
32036 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32037 "#pragma omp declare simd", pragma_tok);
32038 cp_parser_pop_lexer (parser);
32039 if (cl)
32040 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32041 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32042 TREE_CHAIN (c) = attrs;
32043 if (processing_template_decl)
32044 ATTR_IS_DEPENDENT (c) = 1;
32045 attrs = c;
32048 data->fndecl_seen = true;
32049 return attrs;
32053 /* OpenMP 4.0:
32054 # pragma omp declare target new-line
32055 declarations and definitions
32056 # pragma omp end declare target new-line */
32058 static void
32059 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32062 scope_chain->omp_declare_target_attribute++;
32065 static void
32066 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32068 const char *p = "";
32069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32072 p = IDENTIFIER_POINTER (id);
32074 if (strcmp (p, "declare") == 0)
32076 cp_lexer_consume_token (parser->lexer);
32077 p = "";
32078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32081 p = IDENTIFIER_POINTER (id);
32083 if (strcmp (p, "target") == 0)
32084 cp_lexer_consume_token (parser->lexer);
32085 else
32087 cp_parser_error (parser, "expected %<target%>");
32088 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32089 return;
32092 else
32094 cp_parser_error (parser, "expected %<declare%>");
32095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32096 return;
32098 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32099 if (!scope_chain->omp_declare_target_attribute)
32100 error_at (pragma_tok->location,
32101 "%<#pragma omp end declare target%> without corresponding "
32102 "%<#pragma omp declare target%>");
32103 else
32104 scope_chain->omp_declare_target_attribute--;
32107 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32108 expression and optional initializer clause of
32109 #pragma omp declare reduction. We store the expression(s) as
32110 either 3, 6 or 7 special statements inside of the artificial function's
32111 body. The first two statements are DECL_EXPRs for the artificial
32112 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32113 expression that uses those variables.
32114 If there was any INITIALIZER clause, this is followed by further statements,
32115 the fourth and fifth statements are DECL_EXPRs for the artificial
32116 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32117 constructor variant (first token after open paren is not omp_priv),
32118 then the sixth statement is a statement with the function call expression
32119 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32120 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32121 to initialize the OMP_PRIV artificial variable and there is seventh
32122 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32124 static bool
32125 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32127 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32128 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32129 type = TREE_TYPE (type);
32130 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32131 DECL_ARTIFICIAL (omp_out) = 1;
32132 pushdecl (omp_out);
32133 add_decl_expr (omp_out);
32134 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32135 DECL_ARTIFICIAL (omp_in) = 1;
32136 pushdecl (omp_in);
32137 add_decl_expr (omp_in);
32138 tree combiner;
32139 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32141 keep_next_level (true);
32142 tree block = begin_omp_structured_block ();
32143 combiner = cp_parser_expression (parser);
32144 finish_expr_stmt (combiner);
32145 block = finish_omp_structured_block (block);
32146 add_stmt (block);
32148 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32149 return false;
32151 const char *p = "";
32152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32155 p = IDENTIFIER_POINTER (id);
32158 if (strcmp (p, "initializer") == 0)
32160 cp_lexer_consume_token (parser->lexer);
32161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32162 return false;
32164 p = "";
32165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32167 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32168 p = IDENTIFIER_POINTER (id);
32171 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32172 DECL_ARTIFICIAL (omp_priv) = 1;
32173 pushdecl (omp_priv);
32174 add_decl_expr (omp_priv);
32175 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32176 DECL_ARTIFICIAL (omp_orig) = 1;
32177 pushdecl (omp_orig);
32178 add_decl_expr (omp_orig);
32180 keep_next_level (true);
32181 block = begin_omp_structured_block ();
32183 bool ctor = false;
32184 if (strcmp (p, "omp_priv") == 0)
32186 bool is_direct_init, is_non_constant_init;
32187 ctor = true;
32188 cp_lexer_consume_token (parser->lexer);
32189 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32190 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32191 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32192 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32193 == CPP_CLOSE_PAREN
32194 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32195 == CPP_CLOSE_PAREN))
32197 finish_omp_structured_block (block);
32198 error ("invalid initializer clause");
32199 return false;
32201 initializer = cp_parser_initializer (parser, &is_direct_init,
32202 &is_non_constant_init);
32203 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32204 NULL_TREE, LOOKUP_ONLYCONVERTING);
32206 else
32208 cp_parser_parse_tentatively (parser);
32209 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32210 /*check_dependency_p=*/true,
32211 /*template_p=*/NULL,
32212 /*declarator_p=*/false,
32213 /*optional_p=*/false);
32214 vec<tree, va_gc> *args;
32215 if (fn_name == error_mark_node
32216 || cp_parser_error_occurred (parser)
32217 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32218 || ((args = cp_parser_parenthesized_expression_list
32219 (parser, non_attr, /*cast_p=*/false,
32220 /*allow_expansion_p=*/true,
32221 /*non_constant_p=*/NULL)),
32222 cp_parser_error_occurred (parser)))
32224 finish_omp_structured_block (block);
32225 cp_parser_abort_tentative_parse (parser);
32226 cp_parser_error (parser, "expected id-expression (arguments)");
32227 return false;
32229 unsigned int i;
32230 tree arg;
32231 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32232 if (arg == omp_priv
32233 || (TREE_CODE (arg) == ADDR_EXPR
32234 && TREE_OPERAND (arg, 0) == omp_priv))
32235 break;
32236 cp_parser_abort_tentative_parse (parser);
32237 if (arg == NULL_TREE)
32238 error ("one of the initializer call arguments should be %<omp_priv%>"
32239 " or %<&omp_priv%>");
32240 initializer = cp_parser_postfix_expression (parser, false, false, false,
32241 false, NULL);
32242 finish_expr_stmt (initializer);
32245 block = finish_omp_structured_block (block);
32246 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32247 add_stmt (block);
32249 if (ctor)
32250 add_decl_expr (omp_orig);
32252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32253 return false;
32256 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32257 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32259 return true;
32262 /* OpenMP 4.0
32263 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32264 initializer-clause[opt] new-line
32266 initializer-clause:
32267 initializer (omp_priv initializer)
32268 initializer (function-name (argument-list)) */
32270 static void
32271 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32272 enum pragma_context)
32274 auto_vec<tree> types;
32275 enum tree_code reduc_code = ERROR_MARK;
32276 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32277 unsigned int i;
32278 cp_token *first_token;
32279 cp_token_cache *cp;
32280 int errs;
32281 void *p;
32283 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32284 p = obstack_alloc (&declarator_obstack, 0);
32286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32287 goto fail;
32289 switch (cp_lexer_peek_token (parser->lexer)->type)
32291 case CPP_PLUS:
32292 reduc_code = PLUS_EXPR;
32293 break;
32294 case CPP_MULT:
32295 reduc_code = MULT_EXPR;
32296 break;
32297 case CPP_MINUS:
32298 reduc_code = MINUS_EXPR;
32299 break;
32300 case CPP_AND:
32301 reduc_code = BIT_AND_EXPR;
32302 break;
32303 case CPP_XOR:
32304 reduc_code = BIT_XOR_EXPR;
32305 break;
32306 case CPP_OR:
32307 reduc_code = BIT_IOR_EXPR;
32308 break;
32309 case CPP_AND_AND:
32310 reduc_code = TRUTH_ANDIF_EXPR;
32311 break;
32312 case CPP_OR_OR:
32313 reduc_code = TRUTH_ORIF_EXPR;
32314 break;
32315 case CPP_NAME:
32316 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32317 break;
32318 default:
32319 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32320 "%<|%>, %<&&%>, %<||%> or identifier");
32321 goto fail;
32324 if (reduc_code != ERROR_MARK)
32325 cp_lexer_consume_token (parser->lexer);
32327 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32328 if (reduc_id == error_mark_node)
32329 goto fail;
32331 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32332 goto fail;
32334 /* Types may not be defined in declare reduction type list. */
32335 const char *saved_message;
32336 saved_message = parser->type_definition_forbidden_message;
32337 parser->type_definition_forbidden_message
32338 = G_("types may not be defined in declare reduction type list");
32339 bool saved_colon_corrects_to_scope_p;
32340 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32341 parser->colon_corrects_to_scope_p = false;
32342 bool saved_colon_doesnt_start_class_def_p;
32343 saved_colon_doesnt_start_class_def_p
32344 = parser->colon_doesnt_start_class_def_p;
32345 parser->colon_doesnt_start_class_def_p = true;
32347 while (true)
32349 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32350 type = cp_parser_type_id (parser);
32351 if (type == error_mark_node)
32353 else if (ARITHMETIC_TYPE_P (type)
32354 && (orig_reduc_id == NULL_TREE
32355 || (TREE_CODE (type) != COMPLEX_TYPE
32356 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32357 "min") == 0
32358 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32359 "max") == 0))))
32360 error_at (loc, "predeclared arithmetic type %qT in "
32361 "%<#pragma omp declare reduction%>", type);
32362 else if (TREE_CODE (type) == FUNCTION_TYPE
32363 || TREE_CODE (type) == METHOD_TYPE
32364 || TREE_CODE (type) == ARRAY_TYPE)
32365 error_at (loc, "function or array type %qT in "
32366 "%<#pragma omp declare reduction%>", type);
32367 else if (TREE_CODE (type) == REFERENCE_TYPE)
32368 error_at (loc, "reference type %qT in "
32369 "%<#pragma omp declare reduction%>", type);
32370 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32371 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32372 "%<#pragma omp declare reduction%>", type);
32373 else
32374 types.safe_push (type);
32376 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32377 cp_lexer_consume_token (parser->lexer);
32378 else
32379 break;
32382 /* Restore the saved message. */
32383 parser->type_definition_forbidden_message = saved_message;
32384 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32385 parser->colon_doesnt_start_class_def_p
32386 = saved_colon_doesnt_start_class_def_p;
32388 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32389 || types.is_empty ())
32391 fail:
32392 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32393 goto done;
32396 first_token = cp_lexer_peek_token (parser->lexer);
32397 cp = NULL;
32398 errs = errorcount;
32399 FOR_EACH_VEC_ELT (types, i, type)
32401 tree fntype
32402 = build_function_type_list (void_type_node,
32403 cp_build_reference_type (type, false),
32404 NULL_TREE);
32405 tree this_reduc_id = reduc_id;
32406 if (!dependent_type_p (type))
32407 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32408 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32409 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32410 DECL_ARTIFICIAL (fndecl) = 1;
32411 DECL_EXTERNAL (fndecl) = 1;
32412 DECL_DECLARED_INLINE_P (fndecl) = 1;
32413 DECL_IGNORED_P (fndecl) = 1;
32414 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32415 DECL_ATTRIBUTES (fndecl)
32416 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32417 DECL_ATTRIBUTES (fndecl));
32418 if (processing_template_decl)
32419 fndecl = push_template_decl (fndecl);
32420 bool block_scope = false;
32421 tree block = NULL_TREE;
32422 if (current_function_decl)
32424 block_scope = true;
32425 DECL_CONTEXT (fndecl) = global_namespace;
32426 if (!processing_template_decl)
32427 pushdecl (fndecl);
32429 else if (current_class_type)
32431 if (cp == NULL)
32433 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32434 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32435 cp_lexer_consume_token (parser->lexer);
32436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32437 goto fail;
32438 cp = cp_token_cache_new (first_token,
32439 cp_lexer_peek_nth_token (parser->lexer,
32440 2));
32442 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32443 finish_member_declaration (fndecl);
32444 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32445 DECL_PENDING_INLINE_P (fndecl) = 1;
32446 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32447 continue;
32449 else
32451 DECL_CONTEXT (fndecl) = current_namespace;
32452 pushdecl (fndecl);
32454 if (!block_scope)
32455 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32456 else
32457 block = begin_omp_structured_block ();
32458 if (cp)
32460 cp_parser_push_lexer_for_tokens (parser, cp);
32461 parser->lexer->in_pragma = true;
32463 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32465 if (!block_scope)
32466 finish_function (0);
32467 else
32468 DECL_CONTEXT (fndecl) = current_function_decl;
32469 if (cp)
32470 cp_parser_pop_lexer (parser);
32471 goto fail;
32473 if (cp)
32474 cp_parser_pop_lexer (parser);
32475 if (!block_scope)
32476 finish_function (0);
32477 else
32479 DECL_CONTEXT (fndecl) = current_function_decl;
32480 block = finish_omp_structured_block (block);
32481 if (TREE_CODE (block) == BIND_EXPR)
32482 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32483 else if (TREE_CODE (block) == STATEMENT_LIST)
32484 DECL_SAVED_TREE (fndecl) = block;
32485 if (processing_template_decl)
32486 add_decl_expr (fndecl);
32488 cp_check_omp_declare_reduction (fndecl);
32489 if (cp == NULL && types.length () > 1)
32490 cp = cp_token_cache_new (first_token,
32491 cp_lexer_peek_nth_token (parser->lexer, 2));
32492 if (errs != errorcount)
32493 break;
32496 cp_parser_require_pragma_eol (parser, pragma_tok);
32498 done:
32499 /* Free any declarators allocated. */
32500 obstack_free (&declarator_obstack, p);
32503 /* OpenMP 4.0
32504 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32505 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32506 initializer-clause[opt] new-line
32507 #pragma omp declare target new-line */
32509 static void
32510 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32511 enum pragma_context context)
32513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32515 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32516 const char *p = IDENTIFIER_POINTER (id);
32518 if (strcmp (p, "simd") == 0)
32520 cp_lexer_consume_token (parser->lexer);
32521 cp_parser_omp_declare_simd (parser, pragma_tok,
32522 context);
32523 return;
32525 cp_ensure_no_omp_declare_simd (parser);
32526 if (strcmp (p, "reduction") == 0)
32528 cp_lexer_consume_token (parser->lexer);
32529 cp_parser_omp_declare_reduction (parser, pragma_tok,
32530 context);
32531 return;
32533 if (!flag_openmp) /* flag_openmp_simd */
32535 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32536 return;
32538 if (strcmp (p, "target") == 0)
32540 cp_lexer_consume_token (parser->lexer);
32541 cp_parser_omp_declare_target (parser, pragma_tok);
32542 return;
32545 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32546 "or %<target%>");
32547 cp_parser_require_pragma_eol (parser, pragma_tok);
32550 /* Main entry point to OpenMP statement pragmas. */
32552 static void
32553 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32555 tree stmt;
32556 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32557 omp_clause_mask mask (0);
32559 switch (pragma_tok->pragma_kind)
32561 case PRAGMA_OACC_CACHE:
32562 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32563 break;
32564 case PRAGMA_OACC_DATA:
32565 stmt = cp_parser_oacc_data (parser, pragma_tok);
32566 break;
32567 case PRAGMA_OACC_ENTER_DATA:
32568 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32569 break;
32570 case PRAGMA_OACC_EXIT_DATA:
32571 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32572 break;
32573 case PRAGMA_OACC_KERNELS:
32574 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32575 break;
32576 case PRAGMA_OACC_LOOP:
32577 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32578 break;
32579 case PRAGMA_OACC_PARALLEL:
32580 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32581 break;
32582 case PRAGMA_OACC_UPDATE:
32583 stmt = cp_parser_oacc_update (parser, pragma_tok);
32584 break;
32585 case PRAGMA_OACC_WAIT:
32586 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32587 break;
32588 case PRAGMA_OMP_ATOMIC:
32589 cp_parser_omp_atomic (parser, pragma_tok);
32590 return;
32591 case PRAGMA_OMP_CRITICAL:
32592 stmt = cp_parser_omp_critical (parser, pragma_tok);
32593 break;
32594 case PRAGMA_OMP_DISTRIBUTE:
32595 strcpy (p_name, "#pragma omp");
32596 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32597 break;
32598 case PRAGMA_OMP_FOR:
32599 strcpy (p_name, "#pragma omp");
32600 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32601 break;
32602 case PRAGMA_OMP_MASTER:
32603 stmt = cp_parser_omp_master (parser, pragma_tok);
32604 break;
32605 case PRAGMA_OMP_ORDERED:
32606 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32607 break;
32608 case PRAGMA_OMP_PARALLEL:
32609 strcpy (p_name, "#pragma omp");
32610 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32611 break;
32612 case PRAGMA_OMP_SECTIONS:
32613 strcpy (p_name, "#pragma omp");
32614 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32615 break;
32616 case PRAGMA_OMP_SIMD:
32617 strcpy (p_name, "#pragma omp");
32618 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32619 break;
32620 case PRAGMA_OMP_SINGLE:
32621 stmt = cp_parser_omp_single (parser, pragma_tok);
32622 break;
32623 case PRAGMA_OMP_TASK:
32624 stmt = cp_parser_omp_task (parser, pragma_tok);
32625 break;
32626 case PRAGMA_OMP_TASKGROUP:
32627 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32628 break;
32629 case PRAGMA_OMP_TEAMS:
32630 strcpy (p_name, "#pragma omp");
32631 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32632 break;
32633 default:
32634 gcc_unreachable ();
32637 if (stmt)
32638 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32641 /* Transactional Memory parsing routines. */
32643 /* Parse a transaction attribute.
32645 txn-attribute:
32646 attribute
32647 [ [ identifier ] ]
32649 ??? Simplify this when C++0x bracket attributes are
32650 implemented properly. */
32652 static tree
32653 cp_parser_txn_attribute_opt (cp_parser *parser)
32655 cp_token *token;
32656 tree attr_name, attr = NULL;
32658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32659 return cp_parser_attributes_opt (parser);
32661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32662 return NULL_TREE;
32663 cp_lexer_consume_token (parser->lexer);
32664 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32665 goto error1;
32667 token = cp_lexer_peek_token (parser->lexer);
32668 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32670 token = cp_lexer_consume_token (parser->lexer);
32672 attr_name = (token->type == CPP_KEYWORD
32673 /* For keywords, use the canonical spelling,
32674 not the parsed identifier. */
32675 ? ridpointers[(int) token->keyword]
32676 : token->u.value);
32677 attr = build_tree_list (attr_name, NULL_TREE);
32679 else
32680 cp_parser_error (parser, "expected identifier");
32682 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32683 error1:
32684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32685 return attr;
32688 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32690 transaction-statement:
32691 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32692 compound-statement
32693 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32696 static tree
32697 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32699 unsigned char old_in = parser->in_transaction;
32700 unsigned char this_in = 1, new_in;
32701 cp_token *token;
32702 tree stmt, attrs, noex;
32704 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32705 || keyword == RID_TRANSACTION_RELAXED);
32706 token = cp_parser_require_keyword (parser, keyword,
32707 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32708 : RT_TRANSACTION_RELAXED));
32709 gcc_assert (token != NULL);
32711 if (keyword == RID_TRANSACTION_RELAXED)
32712 this_in |= TM_STMT_ATTR_RELAXED;
32713 else
32715 attrs = cp_parser_txn_attribute_opt (parser);
32716 if (attrs)
32717 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32720 /* Parse a noexcept specification. */
32721 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32723 /* Keep track if we're in the lexical scope of an outer transaction. */
32724 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32726 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32728 parser->in_transaction = new_in;
32729 cp_parser_compound_statement (parser, NULL, false, false);
32730 parser->in_transaction = old_in;
32732 finish_transaction_stmt (stmt, NULL, this_in, noex);
32734 return stmt;
32737 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32739 transaction-expression:
32740 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32741 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32744 static tree
32745 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32747 unsigned char old_in = parser->in_transaction;
32748 unsigned char this_in = 1;
32749 cp_token *token;
32750 tree expr, noex;
32751 bool noex_expr;
32753 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32754 || keyword == RID_TRANSACTION_RELAXED);
32756 if (!flag_tm)
32757 error (keyword == RID_TRANSACTION_RELAXED
32758 ? G_("%<__transaction_relaxed%> without transactional memory "
32759 "support enabled")
32760 : G_("%<__transaction_atomic%> without transactional memory "
32761 "support enabled"));
32763 token = cp_parser_require_keyword (parser, keyword,
32764 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32765 : RT_TRANSACTION_RELAXED));
32766 gcc_assert (token != NULL);
32768 if (keyword == RID_TRANSACTION_RELAXED)
32769 this_in |= TM_STMT_ATTR_RELAXED;
32771 /* Set this early. This might mean that we allow transaction_cancel in
32772 an expression that we find out later actually has to be a constexpr.
32773 However, we expect that cxx_constant_value will be able to deal with
32774 this; also, if the noexcept has no constexpr, then what we parse next
32775 really is a transaction's body. */
32776 parser->in_transaction = this_in;
32778 /* Parse a noexcept specification. */
32779 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32780 true);
32782 if (!noex || !noex_expr
32783 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32787 expr = cp_parser_expression (parser);
32788 expr = finish_parenthesized_expr (expr);
32790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32792 else
32794 /* The only expression that is available got parsed for the noexcept
32795 already. noexcept is true then. */
32796 expr = noex;
32797 noex = boolean_true_node;
32800 expr = build_transaction_expr (token->location, expr, this_in, noex);
32801 parser->in_transaction = old_in;
32803 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32804 return error_mark_node;
32806 return (flag_tm ? expr : error_mark_node);
32809 /* Parse a function-transaction-block.
32811 function-transaction-block:
32812 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32813 function-body
32814 __transaction_atomic txn-attribute[opt] function-try-block
32815 __transaction_relaxed ctor-initializer[opt] function-body
32816 __transaction_relaxed function-try-block
32819 static bool
32820 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32822 unsigned char old_in = parser->in_transaction;
32823 unsigned char new_in = 1;
32824 tree compound_stmt, stmt, attrs;
32825 bool ctor_initializer_p;
32826 cp_token *token;
32828 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32829 || keyword == RID_TRANSACTION_RELAXED);
32830 token = cp_parser_require_keyword (parser, keyword,
32831 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32832 : RT_TRANSACTION_RELAXED));
32833 gcc_assert (token != NULL);
32835 if (keyword == RID_TRANSACTION_RELAXED)
32836 new_in |= TM_STMT_ATTR_RELAXED;
32837 else
32839 attrs = cp_parser_txn_attribute_opt (parser);
32840 if (attrs)
32841 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32844 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32846 parser->in_transaction = new_in;
32848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32849 ctor_initializer_p = cp_parser_function_try_block (parser);
32850 else
32851 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32852 (parser, /*in_function_try_block=*/false);
32854 parser->in_transaction = old_in;
32856 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32858 return ctor_initializer_p;
32861 /* Parse a __transaction_cancel statement.
32863 cancel-statement:
32864 __transaction_cancel txn-attribute[opt] ;
32865 __transaction_cancel txn-attribute[opt] throw-expression ;
32867 ??? Cancel and throw is not yet implemented. */
32869 static tree
32870 cp_parser_transaction_cancel (cp_parser *parser)
32872 cp_token *token;
32873 bool is_outer = false;
32874 tree stmt, attrs;
32876 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32877 RT_TRANSACTION_CANCEL);
32878 gcc_assert (token != NULL);
32880 attrs = cp_parser_txn_attribute_opt (parser);
32881 if (attrs)
32882 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32884 /* ??? Parse cancel-and-throw here. */
32886 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32888 if (!flag_tm)
32890 error_at (token->location, "%<__transaction_cancel%> without "
32891 "transactional memory support enabled");
32892 return error_mark_node;
32894 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32896 error_at (token->location, "%<__transaction_cancel%> within a "
32897 "%<__transaction_relaxed%>");
32898 return error_mark_node;
32900 else if (is_outer)
32902 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32903 && !is_tm_may_cancel_outer (current_function_decl))
32905 error_at (token->location, "outer %<__transaction_cancel%> not "
32906 "within outer %<__transaction_atomic%>");
32907 error_at (token->location,
32908 " or a %<transaction_may_cancel_outer%> function");
32909 return error_mark_node;
32912 else if (parser->in_transaction == 0)
32914 error_at (token->location, "%<__transaction_cancel%> not within "
32915 "%<__transaction_atomic%>");
32916 return error_mark_node;
32919 stmt = build_tm_abort_call (token->location, is_outer);
32920 add_stmt (stmt);
32922 return stmt;
32925 /* The parser. */
32927 static GTY (()) cp_parser *the_parser;
32930 /* Special handling for the first token or line in the file. The first
32931 thing in the file might be #pragma GCC pch_preprocess, which loads a
32932 PCH file, which is a GC collection point. So we need to handle this
32933 first pragma without benefit of an existing lexer structure.
32935 Always returns one token to the caller in *FIRST_TOKEN. This is
32936 either the true first token of the file, or the first token after
32937 the initial pragma. */
32939 static void
32940 cp_parser_initial_pragma (cp_token *first_token)
32942 tree name = NULL;
32944 cp_lexer_get_preprocessor_token (NULL, first_token);
32945 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32946 return;
32948 cp_lexer_get_preprocessor_token (NULL, first_token);
32949 if (first_token->type == CPP_STRING)
32951 name = first_token->u.value;
32953 cp_lexer_get_preprocessor_token (NULL, first_token);
32954 if (first_token->type != CPP_PRAGMA_EOL)
32955 error_at (first_token->location,
32956 "junk at end of %<#pragma GCC pch_preprocess%>");
32958 else
32959 error_at (first_token->location, "expected string literal");
32961 /* Skip to the end of the pragma. */
32962 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32963 cp_lexer_get_preprocessor_token (NULL, first_token);
32965 /* Now actually load the PCH file. */
32966 if (name)
32967 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32969 /* Read one more token to return to our caller. We have to do this
32970 after reading the PCH file in, since its pointers have to be
32971 live. */
32972 cp_lexer_get_preprocessor_token (NULL, first_token);
32975 /* Parses the grainsize pragma for the _Cilk_for statement.
32976 Syntax:
32977 #pragma cilk grainsize = <VALUE>. */
32979 static void
32980 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32982 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32984 tree exp = cp_parser_binary_expression (parser, false, false,
32985 PREC_NOT_OPERATOR, NULL);
32986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32987 if (!exp || exp == error_mark_node)
32989 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32990 return;
32993 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32995 cp_parser_cilk_for (parser, exp);
32996 else
32997 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32998 "%<#pragma cilk grainsize%> is not followed by "
32999 "%<_Cilk_for%>");
33000 return;
33002 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33005 /* Normal parsing of a pragma token. Here we can (and must) use the
33006 regular lexer. */
33008 static bool
33009 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33011 cp_token *pragma_tok;
33012 unsigned int id;
33014 pragma_tok = cp_lexer_consume_token (parser->lexer);
33015 gcc_assert (pragma_tok->type == CPP_PRAGMA);
33016 parser->lexer->in_pragma = true;
33018 id = pragma_tok->pragma_kind;
33019 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33020 cp_ensure_no_omp_declare_simd (parser);
33021 switch (id)
33023 case PRAGMA_GCC_PCH_PREPROCESS:
33024 error_at (pragma_tok->location,
33025 "%<#pragma GCC pch_preprocess%> must be first");
33026 break;
33028 case PRAGMA_OMP_BARRIER:
33029 switch (context)
33031 case pragma_compound:
33032 cp_parser_omp_barrier (parser, pragma_tok);
33033 return false;
33034 case pragma_stmt:
33035 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33036 "used in compound statements");
33037 break;
33038 default:
33039 goto bad_stmt;
33041 break;
33043 case PRAGMA_OMP_FLUSH:
33044 switch (context)
33046 case pragma_compound:
33047 cp_parser_omp_flush (parser, pragma_tok);
33048 return false;
33049 case pragma_stmt:
33050 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33051 "used in compound statements");
33052 break;
33053 default:
33054 goto bad_stmt;
33056 break;
33058 case PRAGMA_OMP_TASKWAIT:
33059 switch (context)
33061 case pragma_compound:
33062 cp_parser_omp_taskwait (parser, pragma_tok);
33063 return false;
33064 case pragma_stmt:
33065 error_at (pragma_tok->location,
33066 "%<#pragma omp taskwait%> may only be "
33067 "used in compound statements");
33068 break;
33069 default:
33070 goto bad_stmt;
33072 break;
33074 case PRAGMA_OMP_TASKYIELD:
33075 switch (context)
33077 case pragma_compound:
33078 cp_parser_omp_taskyield (parser, pragma_tok);
33079 return false;
33080 case pragma_stmt:
33081 error_at (pragma_tok->location,
33082 "%<#pragma omp taskyield%> may only be "
33083 "used in compound statements");
33084 break;
33085 default:
33086 goto bad_stmt;
33088 break;
33090 case PRAGMA_OMP_CANCEL:
33091 switch (context)
33093 case pragma_compound:
33094 cp_parser_omp_cancel (parser, pragma_tok);
33095 return false;
33096 case pragma_stmt:
33097 error_at (pragma_tok->location,
33098 "%<#pragma omp cancel%> may only be "
33099 "used in compound statements");
33100 break;
33101 default:
33102 goto bad_stmt;
33104 break;
33106 case PRAGMA_OMP_CANCELLATION_POINT:
33107 switch (context)
33109 case pragma_compound:
33110 cp_parser_omp_cancellation_point (parser, pragma_tok);
33111 return false;
33112 case pragma_stmt:
33113 error_at (pragma_tok->location,
33114 "%<#pragma omp cancellation point%> may only be "
33115 "used in compound statements");
33116 break;
33117 default:
33118 goto bad_stmt;
33120 break;
33122 case PRAGMA_OMP_THREADPRIVATE:
33123 cp_parser_omp_threadprivate (parser, pragma_tok);
33124 return false;
33126 case PRAGMA_OMP_DECLARE_REDUCTION:
33127 cp_parser_omp_declare (parser, pragma_tok, context);
33128 return false;
33130 case PRAGMA_OACC_CACHE:
33131 case PRAGMA_OACC_DATA:
33132 case PRAGMA_OACC_ENTER_DATA:
33133 case PRAGMA_OACC_EXIT_DATA:
33134 case PRAGMA_OACC_KERNELS:
33135 case PRAGMA_OACC_PARALLEL:
33136 case PRAGMA_OACC_LOOP:
33137 case PRAGMA_OACC_UPDATE:
33138 case PRAGMA_OACC_WAIT:
33139 case PRAGMA_OMP_ATOMIC:
33140 case PRAGMA_OMP_CRITICAL:
33141 case PRAGMA_OMP_DISTRIBUTE:
33142 case PRAGMA_OMP_FOR:
33143 case PRAGMA_OMP_MASTER:
33144 case PRAGMA_OMP_ORDERED:
33145 case PRAGMA_OMP_PARALLEL:
33146 case PRAGMA_OMP_SECTIONS:
33147 case PRAGMA_OMP_SIMD:
33148 case PRAGMA_OMP_SINGLE:
33149 case PRAGMA_OMP_TASK:
33150 case PRAGMA_OMP_TASKGROUP:
33151 case PRAGMA_OMP_TEAMS:
33152 if (context != pragma_stmt && context != pragma_compound)
33153 goto bad_stmt;
33154 cp_parser_omp_construct (parser, pragma_tok);
33155 return true;
33157 case PRAGMA_OMP_TARGET:
33158 return cp_parser_omp_target (parser, pragma_tok, context);
33160 case PRAGMA_OMP_END_DECLARE_TARGET:
33161 cp_parser_omp_end_declare_target (parser, pragma_tok);
33162 return false;
33164 case PRAGMA_OMP_SECTION:
33165 error_at (pragma_tok->location,
33166 "%<#pragma omp section%> may only be used in "
33167 "%<#pragma omp sections%> construct");
33168 break;
33170 case PRAGMA_IVDEP:
33172 if (context == pragma_external)
33174 error_at (pragma_tok->location,
33175 "%<#pragma GCC ivdep%> must be inside a function");
33176 break;
33178 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33179 cp_token *tok;
33180 tok = cp_lexer_peek_token (the_parser->lexer);
33181 if (tok->type != CPP_KEYWORD
33182 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33183 && tok->keyword != RID_DO))
33185 cp_parser_error (parser, "for, while or do statement expected");
33186 return false;
33188 cp_parser_iteration_statement (parser, true);
33189 return true;
33192 case PRAGMA_CILK_SIMD:
33193 if (context == pragma_external)
33195 error_at (pragma_tok->location,
33196 "%<#pragma simd%> must be inside a function");
33197 break;
33199 cp_parser_cilk_simd (parser, pragma_tok);
33200 return true;
33202 case PRAGMA_CILK_GRAINSIZE:
33203 if (context == pragma_external)
33205 error_at (pragma_tok->location,
33206 "%<#pragma cilk grainsize%> must be inside a function");
33207 break;
33210 /* Ignore the pragma if Cilk Plus is not enabled. */
33211 if (flag_cilkplus)
33213 cp_parser_cilk_grainsize (parser, pragma_tok);
33214 return true;
33216 else
33218 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33219 "%<#pragma cilk grainsize%>");
33220 break;
33223 default:
33224 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33225 c_invoke_pragma_handler (id);
33226 break;
33228 bad_stmt:
33229 cp_parser_error (parser, "expected declaration specifiers");
33230 break;
33233 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33234 return false;
33237 /* The interface the pragma parsers have to the lexer. */
33239 enum cpp_ttype
33240 pragma_lex (tree *value)
33242 cp_token *tok;
33243 enum cpp_ttype ret;
33245 tok = cp_lexer_peek_token (the_parser->lexer);
33247 ret = tok->type;
33248 *value = tok->u.value;
33250 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33251 ret = CPP_EOF;
33252 else if (ret == CPP_STRING)
33253 *value = cp_parser_string_literal (the_parser, false, false);
33254 else
33256 cp_lexer_consume_token (the_parser->lexer);
33257 if (ret == CPP_KEYWORD)
33258 ret = CPP_NAME;
33261 return ret;
33265 /* External interface. */
33267 /* Parse one entire translation unit. */
33269 void
33270 c_parse_file (void)
33272 static bool already_called = false;
33274 if (already_called)
33275 fatal_error (input_location,
33276 "inter-module optimizations not implemented for C++");
33277 already_called = true;
33279 the_parser = cp_parser_new ();
33280 push_deferring_access_checks (flag_access_control
33281 ? dk_no_deferred : dk_no_check);
33282 cp_parser_translation_unit (the_parser);
33283 the_parser = NULL;
33286 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33287 vectorlength clause:
33288 Syntax:
33289 vectorlength ( constant-expression ) */
33291 static tree
33292 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33293 bool is_simd_fn)
33295 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33296 tree expr;
33297 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33298 safelen clause. Thus, vectorlength is represented as OMP 4.0
33299 safelen. For SIMD-enabled function it is represented by OMP 4.0
33300 simdlen. */
33301 if (!is_simd_fn)
33302 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33303 loc);
33304 else
33305 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33306 loc);
33308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33309 return error_mark_node;
33311 expr = cp_parser_constant_expression (parser);
33312 expr = maybe_constant_value (expr);
33314 /* If expr == error_mark_node, then don't emit any errors nor
33315 create a clause. if any of the above functions returns
33316 error mark node then they would have emitted an error message. */
33317 if (expr == error_mark_node)
33319 else if (!TREE_TYPE (expr)
33320 || !TREE_CONSTANT (expr)
33321 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33322 error_at (loc, "vectorlength must be an integer constant");
33323 else if (TREE_CONSTANT (expr)
33324 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33325 error_at (loc, "vectorlength must be a power of 2");
33326 else
33328 tree c;
33329 if (!is_simd_fn)
33331 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33332 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33333 OMP_CLAUSE_CHAIN (c) = clauses;
33334 clauses = c;
33336 else
33338 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33339 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33340 OMP_CLAUSE_CHAIN (c) = clauses;
33341 clauses = c;
33345 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33346 return error_mark_node;
33347 return clauses;
33350 /* Handles the Cilk Plus #pragma simd linear clause.
33351 Syntax:
33352 linear ( simd-linear-variable-list )
33354 simd-linear-variable-list:
33355 simd-linear-variable
33356 simd-linear-variable-list , simd-linear-variable
33358 simd-linear-variable:
33359 id-expression
33360 id-expression : simd-linear-step
33362 simd-linear-step:
33363 conditional-expression */
33365 static tree
33366 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33368 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33371 return clauses;
33372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33374 cp_parser_error (parser, "expected identifier");
33375 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33376 return error_mark_node;
33379 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33380 parser->colon_corrects_to_scope_p = false;
33381 while (1)
33383 cp_token *token = cp_lexer_peek_token (parser->lexer);
33384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33386 cp_parser_error (parser, "expected variable-name");
33387 clauses = error_mark_node;
33388 break;
33391 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33392 false, false);
33393 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33394 token->location);
33395 if (decl == error_mark_node)
33397 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33398 token->location);
33399 clauses = error_mark_node;
33401 else
33403 tree e = NULL_TREE;
33404 tree step_size = integer_one_node;
33406 /* If present, parse the linear step. Otherwise, assume the default
33407 value of 1. */
33408 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33410 cp_lexer_consume_token (parser->lexer);
33412 e = cp_parser_assignment_expression (parser);
33413 e = maybe_constant_value (e);
33415 if (e == error_mark_node)
33417 /* If an error has occurred, then the whole pragma is
33418 considered ill-formed. Thus, no reason to keep
33419 parsing. */
33420 clauses = error_mark_node;
33421 break;
33423 else if (type_dependent_expression_p (e)
33424 || value_dependent_expression_p (e)
33425 || (TREE_TYPE (e)
33426 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33427 && (TREE_CONSTANT (e)
33428 || DECL_P (e))))
33429 step_size = e;
33430 else
33431 cp_parser_error (parser,
33432 "step size must be an integer constant "
33433 "expression or an integer variable");
33436 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33437 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33438 OMP_CLAUSE_DECL (l) = decl;
33439 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33440 OMP_CLAUSE_CHAIN (l) = clauses;
33441 clauses = l;
33443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33444 cp_lexer_consume_token (parser->lexer);
33445 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33446 break;
33447 else
33449 error_at (cp_lexer_peek_token (parser->lexer)->location,
33450 "expected %<,%> or %<)%> after %qE", decl);
33451 clauses = error_mark_node;
33452 break;
33455 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33456 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33457 return clauses;
33460 /* Returns the name of the next clause. If the clause is not
33461 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33462 token is not consumed. Otherwise, the appropriate enum from the
33463 pragma_simd_clause is returned and the token is consumed. */
33465 static pragma_omp_clause
33466 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33468 pragma_omp_clause clause_type;
33469 cp_token *token = cp_lexer_peek_token (parser->lexer);
33471 if (token->keyword == RID_PRIVATE)
33472 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33473 else if (!token->u.value || token->type != CPP_NAME)
33474 return PRAGMA_CILK_CLAUSE_NONE;
33475 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33476 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33477 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33478 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33479 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33480 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33481 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33482 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33483 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33484 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33485 else
33486 return PRAGMA_CILK_CLAUSE_NONE;
33488 cp_lexer_consume_token (parser->lexer);
33489 return clause_type;
33492 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33494 static tree
33495 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33497 tree clauses = NULL_TREE;
33499 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33500 && clauses != error_mark_node)
33502 pragma_omp_clause c_kind;
33503 c_kind = cp_parser_cilk_simd_clause_name (parser);
33504 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33505 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33506 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33507 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33508 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33509 /* Use the OpenMP 4.0 equivalent function. */
33510 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33511 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33512 /* Use the OpenMP 4.0 equivalent function. */
33513 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33514 clauses);
33515 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33516 /* Use the OMP 4.0 equivalent function. */
33517 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33518 clauses);
33519 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33520 /* Use the OMP 4.0 equivalent function. */
33521 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33522 else
33524 clauses = error_mark_node;
33525 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33526 break;
33530 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33532 if (clauses == error_mark_node)
33533 return error_mark_node;
33534 else
33535 return c_finish_cilk_clauses (clauses);
33538 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33540 static void
33541 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33543 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33545 if (clauses == error_mark_node)
33546 return;
33548 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33550 error_at (cp_lexer_peek_token (parser->lexer)->location,
33551 "for statement expected");
33552 return;
33555 tree sb = begin_omp_structured_block ();
33556 int save = cp_parser_begin_omp_structured_block (parser);
33557 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33558 if (ret)
33559 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33560 cp_parser_end_omp_structured_block (parser, save);
33561 add_stmt (finish_omp_structured_block (sb));
33564 /* Main entry-point for parsing Cilk Plus _Cilk_for
33565 loops. The return value is error_mark_node
33566 when errors happen and CILK_FOR tree on success. */
33568 static tree
33569 cp_parser_cilk_for (cp_parser *parser, tree grain)
33571 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33572 gcc_unreachable ();
33574 tree sb = begin_omp_structured_block ();
33575 int save = cp_parser_begin_omp_structured_block (parser);
33577 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33578 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33579 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33580 clauses = finish_omp_clauses (clauses);
33582 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33583 if (ret)
33584 cpp_validate_cilk_plus_loop (ret);
33585 else
33586 ret = error_mark_node;
33588 cp_parser_end_omp_structured_block (parser, save);
33589 add_stmt (finish_omp_structured_block (sb));
33590 return ret;
33593 /* Create an identifier for a generic parameter type (a synthesized
33594 template parameter implied by `auto' or a concept identifier). */
33596 static GTY(()) int generic_parm_count;
33597 static tree
33598 make_generic_type_name ()
33600 char buf[32];
33601 sprintf (buf, "auto:%d", ++generic_parm_count);
33602 return get_identifier (buf);
33605 /* Predicate that behaves as is_auto_or_concept but matches the parent
33606 node of the generic type rather than the generic type itself. This
33607 allows for type transformation in add_implicit_template_parms. */
33609 static inline bool
33610 tree_type_is_auto_or_concept (const_tree t)
33612 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33615 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33616 (creating a new template parameter list if necessary). Returns the newly
33617 created template type parm. */
33619 tree
33620 synthesize_implicit_template_parm (cp_parser *parser)
33622 gcc_assert (current_binding_level->kind == sk_function_parms);
33624 /* We are either continuing a function template that already contains implicit
33625 template parameters, creating a new fully-implicit function template, or
33626 extending an existing explicit function template with implicit template
33627 parameters. */
33629 cp_binding_level *const entry_scope = current_binding_level;
33631 bool become_template = false;
33632 cp_binding_level *parent_scope = 0;
33634 if (parser->implicit_template_scope)
33636 gcc_assert (parser->implicit_template_parms);
33638 current_binding_level = parser->implicit_template_scope;
33640 else
33642 /* Roll back to the existing template parameter scope (in the case of
33643 extending an explicit function template) or introduce a new template
33644 parameter scope ahead of the function parameter scope (or class scope
33645 in the case of out-of-line member definitions). The function scope is
33646 added back after template parameter synthesis below. */
33648 cp_binding_level *scope = entry_scope;
33650 while (scope->kind == sk_function_parms)
33652 parent_scope = scope;
33653 scope = scope->level_chain;
33655 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33657 /* If not defining a class, then any class scope is a scope level in
33658 an out-of-line member definition. In this case simply wind back
33659 beyond the first such scope to inject the template parameter list.
33660 Otherwise wind back to the class being defined. The latter can
33661 occur in class member friend declarations such as:
33663 class A {
33664 void foo (auto);
33666 class B {
33667 friend void A::foo (auto);
33670 The template parameter list synthesized for the friend declaration
33671 must be injected in the scope of 'B'. This can also occur in
33672 erroneous cases such as:
33674 struct A {
33675 struct B {
33676 void foo (auto);
33678 void B::foo (auto) {}
33681 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33682 but, nevertheless, the template parameter list synthesized for the
33683 declarator should be injected into the scope of 'A' as if the
33684 ill-formed template was specified explicitly. */
33686 while (scope->kind == sk_class && !scope->defining_class_p)
33688 parent_scope = scope;
33689 scope = scope->level_chain;
33693 current_binding_level = scope;
33695 if (scope->kind != sk_template_parms
33696 || !function_being_declared_is_template_p (parser))
33698 /* Introduce a new template parameter list for implicit template
33699 parameters. */
33701 become_template = true;
33703 parser->implicit_template_scope
33704 = begin_scope (sk_template_parms, NULL);
33706 ++processing_template_decl;
33708 parser->fully_implicit_function_template_p = true;
33709 ++parser->num_template_parameter_lists;
33711 else
33713 /* Synthesize implicit template parameters at the end of the explicit
33714 template parameter list. */
33716 gcc_assert (current_template_parms);
33718 parser->implicit_template_scope = scope;
33720 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33721 parser->implicit_template_parms
33722 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33726 /* Synthesize a new template parameter and track the current template
33727 parameter chain with implicit_template_parms. */
33729 tree synth_id = make_generic_type_name ();
33730 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33731 synth_id);
33732 tree new_parm
33733 = process_template_parm (parser->implicit_template_parms,
33734 input_location,
33735 build_tree_list (NULL_TREE, synth_tmpl_parm),
33736 /*non_type=*/false,
33737 /*param_pack=*/false);
33740 if (parser->implicit_template_parms)
33741 parser->implicit_template_parms
33742 = TREE_CHAIN (parser->implicit_template_parms);
33743 else
33744 parser->implicit_template_parms = new_parm;
33746 tree new_type = TREE_TYPE (getdecls ());
33748 /* If creating a fully implicit function template, start the new implicit
33749 template parameter list with this synthesized type, otherwise grow the
33750 current template parameter list. */
33752 if (become_template)
33754 parent_scope->level_chain = current_binding_level;
33756 tree new_parms = make_tree_vec (1);
33757 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33758 current_template_parms = tree_cons (size_int (processing_template_decl),
33759 new_parms, current_template_parms);
33761 else
33763 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33764 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33765 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33766 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33769 current_binding_level = entry_scope;
33771 return new_type;
33774 /* Finish the declaration of a fully implicit function template. Such a
33775 template has no explicit template parameter list so has not been through the
33776 normal template head and tail processing. synthesize_implicit_template_parm
33777 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33778 provided if the declaration is a class member such that its template
33779 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33780 form is returned. Otherwise NULL_TREE is returned. */
33782 tree
33783 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33785 gcc_assert (parser->fully_implicit_function_template_p);
33787 if (member_decl_opt && member_decl_opt != error_mark_node
33788 && DECL_VIRTUAL_P (member_decl_opt))
33790 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33791 "implicit templates may not be %<virtual%>");
33792 DECL_VIRTUAL_P (member_decl_opt) = false;
33795 if (member_decl_opt)
33796 member_decl_opt = finish_member_template_decl (member_decl_opt);
33797 end_template_decl ();
33799 parser->fully_implicit_function_template_p = false;
33800 --parser->num_template_parameter_lists;
33802 return member_decl_opt;
33805 #include "gt-cp-parser.h"