compiler: Remove obsolete hidden_fields_are_ok code.
[official-gcc.git] / gcc / cp / parser.c
blob62a815eb42cf3d272a1b7ee4b325337b09a964dd
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2014 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 "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static tree cp_parser_cilk_for
243 (cp_parser *, tree);
244 static bool cp_parser_omp_declare_reduction_exprs
245 (tree, cp_parser *);
246 static tree cp_parser_cilk_simd_vectorlength
247 (cp_parser *, tree, bool);
249 /* Manifest constants. */
250 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
251 #define CP_SAVED_TOKEN_STACK 5
253 /* Variables. */
255 /* The stream to which debugging output should be written. */
256 static FILE *cp_lexer_debug_stream;
258 /* Nonzero if we are parsing an unevaluated operand: an operand to
259 sizeof, typeof, or alignof. */
260 int cp_unevaluated_operand;
262 /* Dump up to NUM tokens in BUFFER to FILE starting with token
263 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
264 first token in BUFFER. If NUM is 0, dump all the tokens. If
265 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
266 highlighted by surrounding it in [[ ]]. */
268 static void
269 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
270 cp_token *start_token, unsigned num,
271 cp_token *curr_token)
273 unsigned i, nprinted;
274 cp_token *token;
275 bool do_print;
277 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
279 if (buffer == NULL)
280 return;
282 if (num == 0)
283 num = buffer->length ();
285 if (start_token == NULL)
286 start_token = buffer->address ();
288 if (start_token > buffer->address ())
290 cp_lexer_print_token (file, &(*buffer)[0]);
291 fprintf (file, " ... ");
294 do_print = false;
295 nprinted = 0;
296 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
298 if (token == start_token)
299 do_print = true;
301 if (!do_print)
302 continue;
304 nprinted++;
305 if (token == curr_token)
306 fprintf (file, "[[");
308 cp_lexer_print_token (file, token);
310 if (token == curr_token)
311 fprintf (file, "]]");
313 switch (token->type)
315 case CPP_SEMICOLON:
316 case CPP_OPEN_BRACE:
317 case CPP_CLOSE_BRACE:
318 case CPP_EOF:
319 fputc ('\n', file);
320 break;
322 default:
323 fputc (' ', file);
327 if (i == num && i < buffer->length ())
329 fprintf (file, " ... ");
330 cp_lexer_print_token (file, &buffer->last ());
333 fprintf (file, "\n");
337 /* Dump all tokens in BUFFER to stderr. */
339 void
340 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
342 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
345 DEBUG_FUNCTION void
346 debug (vec<cp_token, va_gc> &ref)
348 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
351 DEBUG_FUNCTION void
352 debug (vec<cp_token, va_gc> *ptr)
354 if (ptr)
355 debug (*ptr);
356 else
357 fprintf (stderr, "<nil>\n");
361 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
362 description for T. */
364 static void
365 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
367 if (t)
369 fprintf (file, "%s: ", desc);
370 print_node_brief (file, "", t, 0);
375 /* Dump parser context C to FILE. */
377 static void
378 cp_debug_print_context (FILE *file, cp_parser_context *c)
380 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
381 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
382 print_node_brief (file, "", c->object_type, 0);
383 fprintf (file, "}\n");
387 /* Print the stack of parsing contexts to FILE starting with FIRST. */
389 static void
390 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
392 unsigned i;
393 cp_parser_context *c;
395 fprintf (file, "Parsing context stack:\n");
396 for (i = 0, c = first; c; c = c->next, i++)
398 fprintf (file, "\t#%u: ", i);
399 cp_debug_print_context (file, c);
404 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
406 static void
407 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
409 if (flag)
410 fprintf (file, "%s: true\n", desc);
414 /* Print an unparsed function entry UF to FILE. */
416 static void
417 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
419 unsigned i;
420 cp_default_arg_entry *default_arg_fn;
421 tree fn;
423 fprintf (file, "\tFunctions with default args:\n");
424 for (i = 0;
425 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
426 i++)
428 fprintf (file, "\t\tClass type: ");
429 print_node_brief (file, "", default_arg_fn->class_type, 0);
430 fprintf (file, "\t\tDeclaration: ");
431 print_node_brief (file, "", default_arg_fn->decl, 0);
432 fprintf (file, "\n");
435 fprintf (file, "\n\tFunctions with definitions that require "
436 "post-processing\n\t\t");
437 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
439 print_node_brief (file, "", fn, 0);
440 fprintf (file, " ");
442 fprintf (file, "\n");
444 fprintf (file, "\n\tNon-static data members with initializers that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
455 /* Print the stack of unparsed member functions S to FILE. */
457 static void
458 cp_debug_print_unparsed_queues (FILE *file,
459 vec<cp_unparsed_functions_entry, va_gc> *s)
461 unsigned i;
462 cp_unparsed_functions_entry *uf;
464 fprintf (file, "Unparsed functions\n");
465 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
467 fprintf (file, "#%u:\n", i);
468 cp_debug_print_unparsed_function (file, uf);
473 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
474 the given PARSER. If FILE is NULL, the output is printed on stderr. */
476 static void
477 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
479 cp_token *next_token, *first_token, *start_token;
481 if (file == NULL)
482 file = stderr;
484 next_token = parser->lexer->next_token;
485 first_token = parser->lexer->buffer->address ();
486 start_token = (next_token > first_token + window_size / 2)
487 ? next_token - window_size / 2
488 : first_token;
489 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
490 next_token);
494 /* Dump debugging information for the given PARSER. If FILE is NULL,
495 the output is printed on stderr. */
497 void
498 cp_debug_parser (FILE *file, cp_parser *parser)
500 const size_t window_size = 20;
501 cp_token *token;
502 expanded_location eloc;
504 if (file == NULL)
505 file = stderr;
507 fprintf (file, "Parser state\n\n");
508 fprintf (file, "Number of tokens: %u\n",
509 vec_safe_length (parser->lexer->buffer));
510 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
511 cp_debug_print_tree_if_set (file, "Object scope",
512 parser->object_scope);
513 cp_debug_print_tree_if_set (file, "Qualifying scope",
514 parser->qualifying_scope);
515 cp_debug_print_context_stack (file, parser->context);
516 cp_debug_print_flag (file, "Allow GNU extensions",
517 parser->allow_gnu_extensions_p);
518 cp_debug_print_flag (file, "'>' token is greater-than",
519 parser->greater_than_is_operator_p);
520 cp_debug_print_flag (file, "Default args allowed in current "
521 "parameter list", parser->default_arg_ok_p);
522 cp_debug_print_flag (file, "Parsing integral constant-expression",
523 parser->integral_constant_expression_p);
524 cp_debug_print_flag (file, "Allow non-constant expression in current "
525 "constant-expression",
526 parser->allow_non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Seen non-constant expression",
528 parser->non_integral_constant_expression_p);
529 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
530 "current context",
531 parser->local_variables_forbidden_p);
532 cp_debug_print_flag (file, "In unbraced linkage specification",
533 parser->in_unbraced_linkage_specification_p);
534 cp_debug_print_flag (file, "Parsing a declarator",
535 parser->in_declarator_p);
536 cp_debug_print_flag (file, "In template argument list",
537 parser->in_template_argument_list_p);
538 cp_debug_print_flag (file, "Parsing an iteration statement",
539 parser->in_statement & IN_ITERATION_STMT);
540 cp_debug_print_flag (file, "Parsing a switch statement",
541 parser->in_statement & IN_SWITCH_STMT);
542 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
543 parser->in_statement & IN_OMP_BLOCK);
544 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
545 parser->in_statement & IN_CILK_SIMD_FOR);
546 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
547 parser->in_statement & IN_OMP_FOR);
548 cp_debug_print_flag (file, "Parsing an if statement",
549 parser->in_statement & IN_IF_STMT);
550 cp_debug_print_flag (file, "Parsing a type-id in an expression "
551 "context", parser->in_type_id_in_expr_p);
552 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
553 parser->implicit_extern_c);
554 cp_debug_print_flag (file, "String expressions should be translated "
555 "to execution character set",
556 parser->translate_strings_p);
557 cp_debug_print_flag (file, "Parsing function body outside of a "
558 "local class", parser->in_function_body);
559 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
560 parser->colon_corrects_to_scope_p);
561 cp_debug_print_flag (file, "Colon doesn't start a class definition",
562 parser->colon_doesnt_start_class_def_p);
563 if (parser->type_definition_forbidden_message)
564 fprintf (file, "Error message for forbidden type definitions: %s\n",
565 parser->type_definition_forbidden_message);
566 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
567 fprintf (file, "Number of class definitions in progress: %u\n",
568 parser->num_classes_being_defined);
569 fprintf (file, "Number of template parameter lists for the current "
570 "declaration: %u\n", parser->num_template_parameter_lists);
571 cp_debug_parser_tokens (file, parser, window_size);
572 token = parser->lexer->next_token;
573 fprintf (file, "Next token to parse:\n");
574 fprintf (file, "\tToken: ");
575 cp_lexer_print_token (file, token);
576 eloc = expand_location (token->location);
577 fprintf (file, "\n\tFile: %s\n", eloc.file);
578 fprintf (file, "\tLine: %d\n", eloc.line);
579 fprintf (file, "\tColumn: %d\n", eloc.column);
582 DEBUG_FUNCTION void
583 debug (cp_parser &ref)
585 cp_debug_parser (stderr, &ref);
588 DEBUG_FUNCTION void
589 debug (cp_parser *ptr)
591 if (ptr)
592 debug (*ptr);
593 else
594 fprintf (stderr, "<nil>\n");
597 /* Allocate memory for a new lexer object and return it. */
599 static cp_lexer *
600 cp_lexer_alloc (void)
602 cp_lexer *lexer;
604 c_common_no_more_pch ();
606 /* Allocate the memory. */
607 lexer = ggc_cleared_alloc<cp_lexer> ();
609 /* Initially we are not debugging. */
610 lexer->debugging_p = false;
612 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
614 /* Create the buffer. */
615 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
617 return lexer;
621 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 preprocessor. */
624 static cp_lexer *
625 cp_lexer_new_main (void)
627 cp_lexer *lexer;
628 cp_token token;
630 /* It's possible that parsing the first pragma will load a PCH file,
631 which is a GC collection point. So we have to do that before
632 allocating any memory. */
633 cp_parser_initial_pragma (&token);
635 lexer = cp_lexer_alloc ();
637 /* Put the first token in the buffer. */
638 lexer->buffer->quick_push (token);
640 /* Get the remaining tokens from the preprocessor. */
641 while (token.type != CPP_EOF)
643 cp_lexer_get_preprocessor_token (lexer, &token);
644 vec_safe_push (lexer->buffer, token);
647 lexer->last_token = lexer->buffer->address ()
648 + lexer->buffer->length ()
649 - 1;
650 lexer->next_token = lexer->buffer->length ()
651 ? lexer->buffer->address ()
652 : &eof_token;
654 /* Subsequent preprocessor diagnostics should use compiler
655 diagnostic functions to get the compiler source location. */
656 done_lexing = true;
658 gcc_assert (!lexer->next_token->purged_p);
659 return lexer;
662 /* Create a new lexer whose token stream is primed with the tokens in
663 CACHE. When these tokens are exhausted, no new tokens will be read. */
665 static cp_lexer *
666 cp_lexer_new_from_tokens (cp_token_cache *cache)
668 cp_token *first = cache->first;
669 cp_token *last = cache->last;
670 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
672 /* We do not own the buffer. */
673 lexer->buffer = NULL;
674 lexer->next_token = first == last ? &eof_token : first;
675 lexer->last_token = last;
677 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
679 /* Initially we are not debugging. */
680 lexer->debugging_p = false;
682 gcc_assert (!lexer->next_token->purged_p);
683 return lexer;
686 /* Frees all resources associated with LEXER. */
688 static void
689 cp_lexer_destroy (cp_lexer *lexer)
691 vec_free (lexer->buffer);
692 lexer->saved_tokens.release ();
693 ggc_free (lexer);
696 /* Returns nonzero if debugging information should be output. */
698 static inline bool
699 cp_lexer_debugging_p (cp_lexer *lexer)
701 return lexer->debugging_p;
705 static inline cp_token_position
706 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
708 gcc_assert (!previous_p || lexer->next_token != &eof_token);
710 return lexer->next_token - previous_p;
713 static inline cp_token *
714 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
716 return pos;
719 static inline void
720 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
722 lexer->next_token = cp_lexer_token_at (lexer, pos);
725 static inline cp_token_position
726 cp_lexer_previous_token_position (cp_lexer *lexer)
728 if (lexer->next_token == &eof_token)
729 return lexer->last_token - 1;
730 else
731 return cp_lexer_token_position (lexer, true);
734 static inline cp_token *
735 cp_lexer_previous_token (cp_lexer *lexer)
737 cp_token_position tp = cp_lexer_previous_token_position (lexer);
739 return cp_lexer_token_at (lexer, tp);
742 /* nonzero if we are presently saving tokens. */
744 static inline int
745 cp_lexer_saving_tokens (const cp_lexer* lexer)
747 return lexer->saved_tokens.length () != 0;
750 /* Store the next token from the preprocessor in *TOKEN. Return true
751 if we reach EOF. If LEXER is NULL, assume we are handling an
752 initial #pragma pch_preprocess, and thus want the lexer to return
753 processed strings. */
755 static void
756 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
758 static int is_extern_c = 0;
760 /* Get a new token from the preprocessor. */
761 token->type
762 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
763 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
764 token->keyword = RID_MAX;
765 token->pragma_kind = PRAGMA_NONE;
766 token->purged_p = false;
767 token->error_reported = false;
769 /* On some systems, some header files are surrounded by an
770 implicit extern "C" block. Set a flag in the token if it
771 comes from such a header. */
772 is_extern_c += pending_lang_change;
773 pending_lang_change = 0;
774 token->implicit_extern_c = is_extern_c > 0;
776 /* Check to see if this token is a keyword. */
777 if (token->type == CPP_NAME)
779 if (C_IS_RESERVED_WORD (token->u.value))
781 /* Mark this token as a keyword. */
782 token->type = CPP_KEYWORD;
783 /* Record which keyword. */
784 token->keyword = C_RID_CODE (token->u.value);
786 else
788 if (warn_cxx0x_compat
789 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
790 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
792 /* Warn about the C++0x keyword (but still treat it as
793 an identifier). */
794 warning (OPT_Wc__0x_compat,
795 "identifier %qE is a keyword in C++11",
796 token->u.value);
798 /* Clear out the C_RID_CODE so we don't warn about this
799 particular identifier-turned-keyword again. */
800 C_SET_RID_CODE (token->u.value, RID_MAX);
803 token->keyword = RID_MAX;
806 else if (token->type == CPP_AT_NAME)
808 /* This only happens in Objective-C++; it must be a keyword. */
809 token->type = CPP_KEYWORD;
810 switch (C_RID_CODE (token->u.value))
812 /* Replace 'class' with '@class', 'private' with '@private',
813 etc. This prevents confusion with the C++ keyword
814 'class', and makes the tokens consistent with other
815 Objective-C 'AT' keywords. For example '@class' is
816 reported as RID_AT_CLASS which is consistent with
817 '@synchronized', which is reported as
818 RID_AT_SYNCHRONIZED.
820 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
821 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
822 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
823 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
824 case RID_THROW: token->keyword = RID_AT_THROW; break;
825 case RID_TRY: token->keyword = RID_AT_TRY; break;
826 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
827 default: token->keyword = C_RID_CODE (token->u.value);
830 else if (token->type == CPP_PRAGMA)
832 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
833 token->pragma_kind = ((enum pragma_kind)
834 TREE_INT_CST_LOW (token->u.value));
835 token->u.value = NULL_TREE;
839 /* Update the globals input_location and the input file stack from TOKEN. */
840 static inline void
841 cp_lexer_set_source_position_from_token (cp_token *token)
843 if (token->type != CPP_EOF)
845 input_location = token->location;
849 /* Update the globals input_location and the input file stack from LEXER. */
850 static inline void
851 cp_lexer_set_source_position (cp_lexer *lexer)
853 cp_token *token = cp_lexer_peek_token (lexer);
854 cp_lexer_set_source_position_from_token (token);
857 /* Return a pointer to the next token in the token stream, but do not
858 consume it. */
860 static inline cp_token *
861 cp_lexer_peek_token (cp_lexer *lexer)
863 if (cp_lexer_debugging_p (lexer))
865 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
866 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
867 putc ('\n', cp_lexer_debug_stream);
869 return lexer->next_token;
872 /* Return true if the next token has the indicated TYPE. */
874 static inline bool
875 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
877 return cp_lexer_peek_token (lexer)->type == type;
880 /* Return true if the next token does not have the indicated TYPE. */
882 static inline bool
883 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
885 return !cp_lexer_next_token_is (lexer, type);
888 /* Return true if the next token is the indicated KEYWORD. */
890 static inline bool
891 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
893 return cp_lexer_peek_token (lexer)->keyword == keyword;
896 static inline bool
897 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
899 return cp_lexer_peek_nth_token (lexer, n)->type == type;
902 static inline bool
903 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
905 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
908 /* Return true if the next token is not the indicated KEYWORD. */
910 static inline bool
911 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
913 return cp_lexer_peek_token (lexer)->keyword != keyword;
916 /* Return true if the next token is a keyword for a decl-specifier. */
918 static bool
919 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
921 cp_token *token;
923 token = cp_lexer_peek_token (lexer);
924 switch (token->keyword)
926 /* auto specifier: storage-class-specifier in C++,
927 simple-type-specifier in C++0x. */
928 case RID_AUTO:
929 /* Storage classes. */
930 case RID_REGISTER:
931 case RID_STATIC:
932 case RID_EXTERN:
933 case RID_MUTABLE:
934 case RID_THREAD:
935 /* Elaborated type specifiers. */
936 case RID_ENUM:
937 case RID_CLASS:
938 case RID_STRUCT:
939 case RID_UNION:
940 case RID_TYPENAME:
941 /* Simple type specifiers. */
942 case RID_CHAR:
943 case RID_CHAR16:
944 case RID_CHAR32:
945 case RID_WCHAR:
946 case RID_BOOL:
947 case RID_SHORT:
948 case RID_INT:
949 case RID_LONG:
950 case RID_SIGNED:
951 case RID_UNSIGNED:
952 case RID_FLOAT:
953 case RID_DOUBLE:
954 case RID_VOID:
955 /* GNU extensions. */
956 case RID_ATTRIBUTE:
957 case RID_TYPEOF:
958 /* C++0x extensions. */
959 case RID_DECLTYPE:
960 case RID_UNDERLYING_TYPE:
961 return true;
963 default:
964 if (token->keyword >= RID_FIRST_INT_N
965 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
966 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
967 return true;
968 return false;
972 /* Returns TRUE iff the token T begins a decltype type. */
974 static bool
975 token_is_decltype (cp_token *t)
977 return (t->keyword == RID_DECLTYPE
978 || t->type == CPP_DECLTYPE);
981 /* Returns TRUE iff the next token begins a decltype type. */
983 static bool
984 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
986 cp_token *t = cp_lexer_peek_token (lexer);
987 return token_is_decltype (t);
990 /* Return a pointer to the Nth token in the token stream. If N is 1,
991 then this is precisely equivalent to cp_lexer_peek_token (except
992 that it is not inline). One would like to disallow that case, but
993 there is one case (cp_parser_nth_token_starts_template_id) where
994 the caller passes a variable for N and it might be 1. */
996 static cp_token *
997 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
999 cp_token *token;
1001 /* N is 1-based, not zero-based. */
1002 gcc_assert (n > 0);
1004 if (cp_lexer_debugging_p (lexer))
1005 fprintf (cp_lexer_debug_stream,
1006 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1008 --n;
1009 token = lexer->next_token;
1010 gcc_assert (!n || token != &eof_token);
1011 while (n != 0)
1013 ++token;
1014 if (token == lexer->last_token)
1016 token = &eof_token;
1017 break;
1020 if (!token->purged_p)
1021 --n;
1024 if (cp_lexer_debugging_p (lexer))
1026 cp_lexer_print_token (cp_lexer_debug_stream, token);
1027 putc ('\n', cp_lexer_debug_stream);
1030 return token;
1033 /* Return the next token, and advance the lexer's next_token pointer
1034 to point to the next non-purged token. */
1036 static cp_token *
1037 cp_lexer_consume_token (cp_lexer* lexer)
1039 cp_token *token = lexer->next_token;
1041 gcc_assert (token != &eof_token);
1042 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1046 lexer->next_token++;
1047 if (lexer->next_token == lexer->last_token)
1049 lexer->next_token = &eof_token;
1050 break;
1054 while (lexer->next_token->purged_p);
1056 cp_lexer_set_source_position_from_token (token);
1058 /* Provide debugging output. */
1059 if (cp_lexer_debugging_p (lexer))
1061 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1062 cp_lexer_print_token (cp_lexer_debug_stream, token);
1063 putc ('\n', cp_lexer_debug_stream);
1066 return token;
1069 /* Permanently remove the next token from the token stream, and
1070 advance the next_token pointer to refer to the next non-purged
1071 token. */
1073 static void
1074 cp_lexer_purge_token (cp_lexer *lexer)
1076 cp_token *tok = lexer->next_token;
1078 gcc_assert (tok != &eof_token);
1079 tok->purged_p = true;
1080 tok->location = UNKNOWN_LOCATION;
1081 tok->u.value = NULL_TREE;
1082 tok->keyword = RID_MAX;
1086 tok++;
1087 if (tok == lexer->last_token)
1089 tok = &eof_token;
1090 break;
1093 while (tok->purged_p);
1094 lexer->next_token = tok;
1097 /* Permanently remove all tokens after TOK, up to, but not
1098 including, the token that will be returned next by
1099 cp_lexer_peek_token. */
1101 static void
1102 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1104 cp_token *peek = lexer->next_token;
1106 if (peek == &eof_token)
1107 peek = lexer->last_token;
1109 gcc_assert (tok < peek);
1111 for ( tok += 1; tok != peek; tok += 1)
1113 tok->purged_p = true;
1114 tok->location = UNKNOWN_LOCATION;
1115 tok->u.value = NULL_TREE;
1116 tok->keyword = RID_MAX;
1120 /* Begin saving tokens. All tokens consumed after this point will be
1121 preserved. */
1123 static void
1124 cp_lexer_save_tokens (cp_lexer* lexer)
1126 /* Provide debugging output. */
1127 if (cp_lexer_debugging_p (lexer))
1128 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1130 lexer->saved_tokens.safe_push (lexer->next_token);
1133 /* Commit to the portion of the token stream most recently saved. */
1135 static void
1136 cp_lexer_commit_tokens (cp_lexer* lexer)
1138 /* Provide debugging output. */
1139 if (cp_lexer_debugging_p (lexer))
1140 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1142 lexer->saved_tokens.pop ();
1145 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1146 to the token stream. Stop saving tokens. */
1148 static void
1149 cp_lexer_rollback_tokens (cp_lexer* lexer)
1151 /* Provide debugging output. */
1152 if (cp_lexer_debugging_p (lexer))
1153 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1155 lexer->next_token = lexer->saved_tokens.pop ();
1158 /* RAII wrapper around the above functions, with sanity checking. Creating
1159 a variable saves tokens, which are committed when the variable is
1160 destroyed unless they are explicitly rolled back by calling the rollback
1161 member function. */
1163 struct saved_token_sentinel
1165 cp_lexer *lexer;
1166 unsigned len;
1167 bool commit;
1168 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1170 len = lexer->saved_tokens.length ();
1171 cp_lexer_save_tokens (lexer);
1173 void rollback ()
1175 cp_lexer_rollback_tokens (lexer);
1176 commit = false;
1178 ~saved_token_sentinel()
1180 if (commit)
1181 cp_lexer_commit_tokens (lexer);
1182 gcc_assert (lexer->saved_tokens.length () == len);
1186 /* Print a representation of the TOKEN on the STREAM. */
1188 static void
1189 cp_lexer_print_token (FILE * stream, cp_token *token)
1191 /* We don't use cpp_type2name here because the parser defines
1192 a few tokens of its own. */
1193 static const char *const token_names[] = {
1194 /* cpplib-defined token types */
1195 #define OP(e, s) #e,
1196 #define TK(e, s) #e,
1197 TTYPE_TABLE
1198 #undef OP
1199 #undef TK
1200 /* C++ parser token types - see "Manifest constants", above. */
1201 "KEYWORD",
1202 "TEMPLATE_ID",
1203 "NESTED_NAME_SPECIFIER",
1206 /* For some tokens, print the associated data. */
1207 switch (token->type)
1209 case CPP_KEYWORD:
1210 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1211 For example, `struct' is mapped to an INTEGER_CST. */
1212 if (!identifier_p (token->u.value))
1213 break;
1214 /* else fall through */
1215 case CPP_NAME:
1216 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1217 break;
1219 case CPP_STRING:
1220 case CPP_STRING16:
1221 case CPP_STRING32:
1222 case CPP_WSTRING:
1223 case CPP_UTF8STRING:
1224 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1225 break;
1227 case CPP_NUMBER:
1228 print_generic_expr (stream, token->u.value, 0);
1229 break;
1231 default:
1232 /* If we have a name for the token, print it out. Otherwise, we
1233 simply give the numeric code. */
1234 if (token->type < ARRAY_SIZE(token_names))
1235 fputs (token_names[token->type], stream);
1236 else
1237 fprintf (stream, "[%d]", token->type);
1238 break;
1242 DEBUG_FUNCTION void
1243 debug (cp_token &ref)
1245 cp_lexer_print_token (stderr, &ref);
1246 fprintf (stderr, "\n");
1249 DEBUG_FUNCTION void
1250 debug (cp_token *ptr)
1252 if (ptr)
1253 debug (*ptr);
1254 else
1255 fprintf (stderr, "<nil>\n");
1259 /* Start emitting debugging information. */
1261 static void
1262 cp_lexer_start_debugging (cp_lexer* lexer)
1264 lexer->debugging_p = true;
1265 cp_lexer_debug_stream = stderr;
1268 /* Stop emitting debugging information. */
1270 static void
1271 cp_lexer_stop_debugging (cp_lexer* lexer)
1273 lexer->debugging_p = false;
1274 cp_lexer_debug_stream = NULL;
1277 /* Create a new cp_token_cache, representing a range of tokens. */
1279 static cp_token_cache *
1280 cp_token_cache_new (cp_token *first, cp_token *last)
1282 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1283 cache->first = first;
1284 cache->last = last;
1285 return cache;
1288 /* Diagnose if #pragma omp declare simd isn't followed immediately
1289 by function declaration or definition. */
1291 static inline void
1292 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1294 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1296 error ("%<#pragma omp declare simd%> not immediately followed by "
1297 "function declaration or definition");
1298 parser->omp_declare_simd = NULL;
1302 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1303 and put that into "omp declare simd" attribute. */
1305 static inline void
1306 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1308 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1310 if (fndecl == error_mark_node)
1312 parser->omp_declare_simd = NULL;
1313 return;
1315 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1317 cp_ensure_no_omp_declare_simd (parser);
1318 return;
1323 /* Decl-specifiers. */
1325 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1327 static void
1328 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1330 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1333 /* Declarators. */
1335 /* Nothing other than the parser should be creating declarators;
1336 declarators are a semi-syntactic representation of C++ entities.
1337 Other parts of the front end that need to create entities (like
1338 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1340 static cp_declarator *make_call_declarator
1341 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1342 static cp_declarator *make_array_declarator
1343 (cp_declarator *, tree);
1344 static cp_declarator *make_pointer_declarator
1345 (cp_cv_quals, cp_declarator *, tree);
1346 static cp_declarator *make_reference_declarator
1347 (cp_cv_quals, cp_declarator *, bool, tree);
1348 static cp_parameter_declarator *make_parameter_declarator
1349 (cp_decl_specifier_seq *, cp_declarator *, tree);
1350 static cp_declarator *make_ptrmem_declarator
1351 (cp_cv_quals, tree, cp_declarator *, tree);
1353 /* An erroneous declarator. */
1354 static cp_declarator *cp_error_declarator;
1356 /* The obstack on which declarators and related data structures are
1357 allocated. */
1358 static struct obstack declarator_obstack;
1360 /* Alloc BYTES from the declarator memory pool. */
1362 static inline void *
1363 alloc_declarator (size_t bytes)
1365 return obstack_alloc (&declarator_obstack, bytes);
1368 /* Allocate a declarator of the indicated KIND. Clear fields that are
1369 common to all declarators. */
1371 static cp_declarator *
1372 make_declarator (cp_declarator_kind kind)
1374 cp_declarator *declarator;
1376 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1377 declarator->kind = kind;
1378 declarator->attributes = NULL_TREE;
1379 declarator->std_attributes = NULL_TREE;
1380 declarator->declarator = NULL;
1381 declarator->parameter_pack_p = false;
1382 declarator->id_loc = UNKNOWN_LOCATION;
1384 return declarator;
1387 /* Make a declarator for a generalized identifier. If
1388 QUALIFYING_SCOPE is non-NULL, the identifier is
1389 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1390 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1391 is, if any. */
1393 static cp_declarator *
1394 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1395 special_function_kind sfk)
1397 cp_declarator *declarator;
1399 /* It is valid to write:
1401 class C { void f(); };
1402 typedef C D;
1403 void D::f();
1405 The standard is not clear about whether `typedef const C D' is
1406 legal; as of 2002-09-15 the committee is considering that
1407 question. EDG 3.0 allows that syntax. Therefore, we do as
1408 well. */
1409 if (qualifying_scope && TYPE_P (qualifying_scope))
1410 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1412 gcc_assert (identifier_p (unqualified_name)
1413 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1414 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1416 declarator = make_declarator (cdk_id);
1417 declarator->u.id.qualifying_scope = qualifying_scope;
1418 declarator->u.id.unqualified_name = unqualified_name;
1419 declarator->u.id.sfk = sfk;
1421 return declarator;
1424 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1425 of modifiers such as const or volatile to apply to the pointer
1426 type, represented as identifiers. ATTRIBUTES represent the attributes that
1427 appertain to the pointer or reference. */
1429 cp_declarator *
1430 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1431 tree attributes)
1433 cp_declarator *declarator;
1435 declarator = make_declarator (cdk_pointer);
1436 declarator->declarator = target;
1437 declarator->u.pointer.qualifiers = cv_qualifiers;
1438 declarator->u.pointer.class_type = NULL_TREE;
1439 if (target)
1441 declarator->id_loc = target->id_loc;
1442 declarator->parameter_pack_p = target->parameter_pack_p;
1443 target->parameter_pack_p = false;
1445 else
1446 declarator->parameter_pack_p = false;
1448 declarator->std_attributes = attributes;
1450 return declarator;
1453 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1454 represent the attributes that appertain to the pointer or
1455 reference. */
1457 cp_declarator *
1458 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1459 bool rvalue_ref, tree attributes)
1461 cp_declarator *declarator;
1463 declarator = make_declarator (cdk_reference);
1464 declarator->declarator = target;
1465 declarator->u.reference.qualifiers = cv_qualifiers;
1466 declarator->u.reference.rvalue_ref = rvalue_ref;
1467 if (target)
1469 declarator->id_loc = target->id_loc;
1470 declarator->parameter_pack_p = target->parameter_pack_p;
1471 target->parameter_pack_p = false;
1473 else
1474 declarator->parameter_pack_p = false;
1476 declarator->std_attributes = attributes;
1478 return declarator;
1481 /* Like make_pointer_declarator -- but for a pointer to a non-static
1482 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1483 appertain to the pointer or reference. */
1485 cp_declarator *
1486 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1487 cp_declarator *pointee,
1488 tree attributes)
1490 cp_declarator *declarator;
1492 declarator = make_declarator (cdk_ptrmem);
1493 declarator->declarator = pointee;
1494 declarator->u.pointer.qualifiers = cv_qualifiers;
1495 declarator->u.pointer.class_type = class_type;
1497 if (pointee)
1499 declarator->parameter_pack_p = pointee->parameter_pack_p;
1500 pointee->parameter_pack_p = false;
1502 else
1503 declarator->parameter_pack_p = false;
1505 declarator->std_attributes = attributes;
1507 return declarator;
1510 /* Make a declarator for the function given by TARGET, with the
1511 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1512 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1513 indicates what exceptions can be thrown. */
1515 cp_declarator *
1516 make_call_declarator (cp_declarator *target,
1517 tree parms,
1518 cp_cv_quals cv_qualifiers,
1519 cp_virt_specifiers virt_specifiers,
1520 cp_ref_qualifier ref_qualifier,
1521 tree exception_specification,
1522 tree late_return_type)
1524 cp_declarator *declarator;
1526 declarator = make_declarator (cdk_function);
1527 declarator->declarator = target;
1528 declarator->u.function.parameters = parms;
1529 declarator->u.function.qualifiers = cv_qualifiers;
1530 declarator->u.function.virt_specifiers = virt_specifiers;
1531 declarator->u.function.ref_qualifier = ref_qualifier;
1532 declarator->u.function.exception_specification = exception_specification;
1533 declarator->u.function.late_return_type = late_return_type;
1534 if (target)
1536 declarator->id_loc = target->id_loc;
1537 declarator->parameter_pack_p = target->parameter_pack_p;
1538 target->parameter_pack_p = false;
1540 else
1541 declarator->parameter_pack_p = false;
1543 return declarator;
1546 /* Make a declarator for an array of BOUNDS elements, each of which is
1547 defined by ELEMENT. */
1549 cp_declarator *
1550 make_array_declarator (cp_declarator *element, tree bounds)
1552 cp_declarator *declarator;
1554 declarator = make_declarator (cdk_array);
1555 declarator->declarator = element;
1556 declarator->u.array.bounds = bounds;
1557 if (element)
1559 declarator->id_loc = element->id_loc;
1560 declarator->parameter_pack_p = element->parameter_pack_p;
1561 element->parameter_pack_p = false;
1563 else
1564 declarator->parameter_pack_p = false;
1566 return declarator;
1569 /* Determine whether the declarator we've seen so far can be a
1570 parameter pack, when followed by an ellipsis. */
1571 static bool
1572 declarator_can_be_parameter_pack (cp_declarator *declarator)
1574 /* Search for a declarator name, or any other declarator that goes
1575 after the point where the ellipsis could appear in a parameter
1576 pack. If we find any of these, then this declarator can not be
1577 made into a parameter pack. */
1578 bool found = false;
1579 while (declarator && !found)
1581 switch ((int)declarator->kind)
1583 case cdk_id:
1584 case cdk_array:
1585 found = true;
1586 break;
1588 case cdk_error:
1589 return true;
1591 default:
1592 declarator = declarator->declarator;
1593 break;
1597 return !found;
1600 cp_parameter_declarator *no_parameters;
1602 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1603 DECLARATOR and DEFAULT_ARGUMENT. */
1605 cp_parameter_declarator *
1606 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1607 cp_declarator *declarator,
1608 tree default_argument)
1610 cp_parameter_declarator *parameter;
1612 parameter = ((cp_parameter_declarator *)
1613 alloc_declarator (sizeof (cp_parameter_declarator)));
1614 parameter->next = NULL;
1615 if (decl_specifiers)
1616 parameter->decl_specifiers = *decl_specifiers;
1617 else
1618 clear_decl_specs (&parameter->decl_specifiers);
1619 parameter->declarator = declarator;
1620 parameter->default_argument = default_argument;
1621 parameter->ellipsis_p = false;
1623 return parameter;
1626 /* Returns true iff DECLARATOR is a declaration for a function. */
1628 static bool
1629 function_declarator_p (const cp_declarator *declarator)
1631 while (declarator)
1633 if (declarator->kind == cdk_function
1634 && declarator->declarator->kind == cdk_id)
1635 return true;
1636 if (declarator->kind == cdk_id
1637 || declarator->kind == cdk_error)
1638 return false;
1639 declarator = declarator->declarator;
1641 return false;
1644 /* The parser. */
1646 /* Overview
1647 --------
1649 A cp_parser parses the token stream as specified by the C++
1650 grammar. Its job is purely parsing, not semantic analysis. For
1651 example, the parser breaks the token stream into declarators,
1652 expressions, statements, and other similar syntactic constructs.
1653 It does not check that the types of the expressions on either side
1654 of an assignment-statement are compatible, or that a function is
1655 not declared with a parameter of type `void'.
1657 The parser invokes routines elsewhere in the compiler to perform
1658 semantic analysis and to build up the abstract syntax tree for the
1659 code processed.
1661 The parser (and the template instantiation code, which is, in a
1662 way, a close relative of parsing) are the only parts of the
1663 compiler that should be calling push_scope and pop_scope, or
1664 related functions. The parser (and template instantiation code)
1665 keeps track of what scope is presently active; everything else
1666 should simply honor that. (The code that generates static
1667 initializers may also need to set the scope, in order to check
1668 access control correctly when emitting the initializers.)
1670 Methodology
1671 -----------
1673 The parser is of the standard recursive-descent variety. Upcoming
1674 tokens in the token stream are examined in order to determine which
1675 production to use when parsing a non-terminal. Some C++ constructs
1676 require arbitrary look ahead to disambiguate. For example, it is
1677 impossible, in the general case, to tell whether a statement is an
1678 expression or declaration without scanning the entire statement.
1679 Therefore, the parser is capable of "parsing tentatively." When the
1680 parser is not sure what construct comes next, it enters this mode.
1681 Then, while we attempt to parse the construct, the parser queues up
1682 error messages, rather than issuing them immediately, and saves the
1683 tokens it consumes. If the construct is parsed successfully, the
1684 parser "commits", i.e., it issues any queued error messages and
1685 the tokens that were being preserved are permanently discarded.
1686 If, however, the construct is not parsed successfully, the parser
1687 rolls back its state completely so that it can resume parsing using
1688 a different alternative.
1690 Future Improvements
1691 -------------------
1693 The performance of the parser could probably be improved substantially.
1694 We could often eliminate the need to parse tentatively by looking ahead
1695 a little bit. In some places, this approach might not entirely eliminate
1696 the need to parse tentatively, but it might still speed up the average
1697 case. */
1699 /* Flags that are passed to some parsing functions. These values can
1700 be bitwise-ored together. */
1702 enum
1704 /* No flags. */
1705 CP_PARSER_FLAGS_NONE = 0x0,
1706 /* The construct is optional. If it is not present, then no error
1707 should be issued. */
1708 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1709 /* When parsing a type-specifier, treat user-defined type-names
1710 as non-type identifiers. */
1711 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1712 /* When parsing a type-specifier, do not try to parse a class-specifier
1713 or enum-specifier. */
1714 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1715 /* When parsing a decl-specifier-seq, only allow type-specifier or
1716 constexpr. */
1717 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1720 /* This type is used for parameters and variables which hold
1721 combinations of the above flags. */
1722 typedef int cp_parser_flags;
1724 /* The different kinds of declarators we want to parse. */
1726 typedef enum cp_parser_declarator_kind
1728 /* We want an abstract declarator. */
1729 CP_PARSER_DECLARATOR_ABSTRACT,
1730 /* We want a named declarator. */
1731 CP_PARSER_DECLARATOR_NAMED,
1732 /* We don't mind, but the name must be an unqualified-id. */
1733 CP_PARSER_DECLARATOR_EITHER
1734 } cp_parser_declarator_kind;
1736 /* The precedence values used to parse binary expressions. The minimum value
1737 of PREC must be 1, because zero is reserved to quickly discriminate
1738 binary operators from other tokens. */
1740 enum cp_parser_prec
1742 PREC_NOT_OPERATOR,
1743 PREC_LOGICAL_OR_EXPRESSION,
1744 PREC_LOGICAL_AND_EXPRESSION,
1745 PREC_INCLUSIVE_OR_EXPRESSION,
1746 PREC_EXCLUSIVE_OR_EXPRESSION,
1747 PREC_AND_EXPRESSION,
1748 PREC_EQUALITY_EXPRESSION,
1749 PREC_RELATIONAL_EXPRESSION,
1750 PREC_SHIFT_EXPRESSION,
1751 PREC_ADDITIVE_EXPRESSION,
1752 PREC_MULTIPLICATIVE_EXPRESSION,
1753 PREC_PM_EXPRESSION,
1754 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1757 /* A mapping from a token type to a corresponding tree node type, with a
1758 precedence value. */
1760 typedef struct cp_parser_binary_operations_map_node
1762 /* The token type. */
1763 enum cpp_ttype token_type;
1764 /* The corresponding tree code. */
1765 enum tree_code tree_type;
1766 /* The precedence of this operator. */
1767 enum cp_parser_prec prec;
1768 } cp_parser_binary_operations_map_node;
1770 typedef struct cp_parser_expression_stack_entry
1772 /* Left hand side of the binary operation we are currently
1773 parsing. */
1774 tree lhs;
1775 /* Original tree code for left hand side, if it was a binary
1776 expression itself (used for -Wparentheses). */
1777 enum tree_code lhs_type;
1778 /* Tree code for the binary operation we are parsing. */
1779 enum tree_code tree_type;
1780 /* Precedence of the binary operation we are parsing. */
1781 enum cp_parser_prec prec;
1782 /* Location of the binary operation we are parsing. */
1783 location_t loc;
1784 } cp_parser_expression_stack_entry;
1786 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1787 entries because precedence levels on the stack are monotonically
1788 increasing. */
1789 typedef struct cp_parser_expression_stack_entry
1790 cp_parser_expression_stack[NUM_PREC_VALUES];
1792 /* Prototypes. */
1794 /* Constructors and destructors. */
1796 static cp_parser_context *cp_parser_context_new
1797 (cp_parser_context *);
1799 /* Class variables. */
1801 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1803 /* The operator-precedence table used by cp_parser_binary_expression.
1804 Transformed into an associative array (binops_by_token) by
1805 cp_parser_new. */
1807 static const cp_parser_binary_operations_map_node binops[] = {
1808 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1809 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1811 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1812 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1813 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1815 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1816 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1818 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1819 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1821 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1822 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1823 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1824 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1826 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1827 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1829 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1831 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1833 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1835 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1837 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1840 /* The same as binops, but initialized by cp_parser_new so that
1841 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1842 for speed. */
1843 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1845 /* Constructors and destructors. */
1847 /* Construct a new context. The context below this one on the stack
1848 is given by NEXT. */
1850 static cp_parser_context *
1851 cp_parser_context_new (cp_parser_context* next)
1853 cp_parser_context *context;
1855 /* Allocate the storage. */
1856 if (cp_parser_context_free_list != NULL)
1858 /* Pull the first entry from the free list. */
1859 context = cp_parser_context_free_list;
1860 cp_parser_context_free_list = context->next;
1861 memset (context, 0, sizeof (*context));
1863 else
1864 context = ggc_cleared_alloc<cp_parser_context> ();
1866 /* No errors have occurred yet in this context. */
1867 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1868 /* If this is not the bottommost context, copy information that we
1869 need from the previous context. */
1870 if (next)
1872 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1873 expression, then we are parsing one in this context, too. */
1874 context->object_type = next->object_type;
1875 /* Thread the stack. */
1876 context->next = next;
1879 return context;
1882 /* Managing the unparsed function queues. */
1884 #define unparsed_funs_with_default_args \
1885 parser->unparsed_queues->last ().funs_with_default_args
1886 #define unparsed_funs_with_definitions \
1887 parser->unparsed_queues->last ().funs_with_definitions
1888 #define unparsed_nsdmis \
1889 parser->unparsed_queues->last ().nsdmis
1890 #define unparsed_classes \
1891 parser->unparsed_queues->last ().classes
1893 static void
1894 push_unparsed_function_queues (cp_parser *parser)
1896 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1897 vec_safe_push (parser->unparsed_queues, e);
1900 static void
1901 pop_unparsed_function_queues (cp_parser *parser)
1903 release_tree_vector (unparsed_funs_with_definitions);
1904 parser->unparsed_queues->pop ();
1907 /* Prototypes. */
1909 /* Constructors and destructors. */
1911 static cp_parser *cp_parser_new
1912 (void);
1914 /* Routines to parse various constructs.
1916 Those that return `tree' will return the error_mark_node (rather
1917 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1918 Sometimes, they will return an ordinary node if error-recovery was
1919 attempted, even though a parse error occurred. So, to check
1920 whether or not a parse error occurred, you should always use
1921 cp_parser_error_occurred. If the construct is optional (indicated
1922 either by an `_opt' in the name of the function that does the
1923 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1924 the construct is not present. */
1926 /* Lexical conventions [gram.lex] */
1928 static tree cp_parser_identifier
1929 (cp_parser *);
1930 static tree cp_parser_string_literal
1931 (cp_parser *, bool, bool, bool);
1932 static tree cp_parser_userdef_char_literal
1933 (cp_parser *);
1934 static tree cp_parser_userdef_string_literal
1935 (tree);
1936 static tree cp_parser_userdef_numeric_literal
1937 (cp_parser *);
1939 /* Basic concepts [gram.basic] */
1941 static bool cp_parser_translation_unit
1942 (cp_parser *);
1944 /* Expressions [gram.expr] */
1946 static tree cp_parser_primary_expression
1947 (cp_parser *, bool, bool, bool, cp_id_kind *);
1948 static tree cp_parser_id_expression
1949 (cp_parser *, bool, bool, bool *, bool, bool);
1950 static tree cp_parser_unqualified_id
1951 (cp_parser *, bool, bool, bool, bool);
1952 static tree cp_parser_nested_name_specifier_opt
1953 (cp_parser *, bool, bool, bool, bool);
1954 static tree cp_parser_nested_name_specifier
1955 (cp_parser *, bool, bool, bool, bool);
1956 static tree cp_parser_qualifying_entity
1957 (cp_parser *, bool, bool, bool, bool, bool);
1958 static tree cp_parser_postfix_expression
1959 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1960 static tree cp_parser_postfix_open_square_expression
1961 (cp_parser *, tree, bool, bool);
1962 static tree cp_parser_postfix_dot_deref_expression
1963 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1964 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1965 (cp_parser *, int, bool, bool, bool *, bool = false);
1966 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1967 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1968 static void cp_parser_pseudo_destructor_name
1969 (cp_parser *, tree, tree *, tree *);
1970 static tree cp_parser_unary_expression
1971 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1972 static enum tree_code cp_parser_unary_operator
1973 (cp_token *);
1974 static tree cp_parser_new_expression
1975 (cp_parser *);
1976 static vec<tree, va_gc> *cp_parser_new_placement
1977 (cp_parser *);
1978 static tree cp_parser_new_type_id
1979 (cp_parser *, tree *);
1980 static cp_declarator *cp_parser_new_declarator_opt
1981 (cp_parser *);
1982 static cp_declarator *cp_parser_direct_new_declarator
1983 (cp_parser *);
1984 static vec<tree, va_gc> *cp_parser_new_initializer
1985 (cp_parser *);
1986 static tree cp_parser_delete_expression
1987 (cp_parser *);
1988 static tree cp_parser_cast_expression
1989 (cp_parser *, bool, bool, bool, cp_id_kind *);
1990 static tree cp_parser_binary_expression
1991 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1992 static tree cp_parser_question_colon_clause
1993 (cp_parser *, tree);
1994 static tree cp_parser_assignment_expression
1995 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1996 static enum tree_code cp_parser_assignment_operator_opt
1997 (cp_parser *);
1998 static tree cp_parser_expression
1999 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2000 static tree cp_parser_constant_expression
2001 (cp_parser *, bool = false, bool * = NULL);
2002 static tree cp_parser_builtin_offsetof
2003 (cp_parser *);
2004 static tree cp_parser_lambda_expression
2005 (cp_parser *);
2006 static void cp_parser_lambda_introducer
2007 (cp_parser *, tree);
2008 static bool cp_parser_lambda_declarator_opt
2009 (cp_parser *, tree);
2010 static void cp_parser_lambda_body
2011 (cp_parser *, tree);
2013 /* Statements [gram.stmt.stmt] */
2015 static void cp_parser_statement
2016 (cp_parser *, tree, bool, bool *);
2017 static void cp_parser_label_for_labeled_statement
2018 (cp_parser *, tree);
2019 static tree cp_parser_expression_statement
2020 (cp_parser *, tree);
2021 static tree cp_parser_compound_statement
2022 (cp_parser *, tree, bool, bool);
2023 static void cp_parser_statement_seq_opt
2024 (cp_parser *, tree);
2025 static tree cp_parser_selection_statement
2026 (cp_parser *, bool *);
2027 static tree cp_parser_condition
2028 (cp_parser *);
2029 static tree cp_parser_iteration_statement
2030 (cp_parser *, bool);
2031 static bool cp_parser_for_init_statement
2032 (cp_parser *, tree *decl);
2033 static tree cp_parser_for
2034 (cp_parser *, bool);
2035 static tree cp_parser_c_for
2036 (cp_parser *, tree, tree, bool);
2037 static tree cp_parser_range_for
2038 (cp_parser *, tree, tree, tree, bool);
2039 static void do_range_for_auto_deduction
2040 (tree, tree);
2041 static tree cp_parser_perform_range_for_lookup
2042 (tree, tree *, tree *);
2043 static tree cp_parser_range_for_member_function
2044 (tree, tree);
2045 static tree cp_parser_jump_statement
2046 (cp_parser *);
2047 static void cp_parser_declaration_statement
2048 (cp_parser *);
2050 static tree cp_parser_implicitly_scoped_statement
2051 (cp_parser *, bool *);
2052 static void cp_parser_already_scoped_statement
2053 (cp_parser *);
2055 /* Declarations [gram.dcl.dcl] */
2057 static void cp_parser_declaration_seq_opt
2058 (cp_parser *);
2059 static void cp_parser_declaration
2060 (cp_parser *);
2061 static void cp_parser_block_declaration
2062 (cp_parser *, bool);
2063 static void cp_parser_simple_declaration
2064 (cp_parser *, bool, tree *);
2065 static void cp_parser_decl_specifier_seq
2066 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2067 static tree cp_parser_storage_class_specifier_opt
2068 (cp_parser *);
2069 static tree cp_parser_function_specifier_opt
2070 (cp_parser *, cp_decl_specifier_seq *);
2071 static tree cp_parser_type_specifier
2072 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2073 int *, bool *);
2074 static tree cp_parser_simple_type_specifier
2075 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2076 static tree cp_parser_type_name
2077 (cp_parser *);
2078 static tree cp_parser_nonclass_name
2079 (cp_parser* parser);
2080 static tree cp_parser_elaborated_type_specifier
2081 (cp_parser *, bool, bool);
2082 static tree cp_parser_enum_specifier
2083 (cp_parser *);
2084 static void cp_parser_enumerator_list
2085 (cp_parser *, tree);
2086 static void cp_parser_enumerator_definition
2087 (cp_parser *, tree);
2088 static tree cp_parser_namespace_name
2089 (cp_parser *);
2090 static void cp_parser_namespace_definition
2091 (cp_parser *);
2092 static void cp_parser_namespace_body
2093 (cp_parser *);
2094 static tree cp_parser_qualified_namespace_specifier
2095 (cp_parser *);
2096 static void cp_parser_namespace_alias_definition
2097 (cp_parser *);
2098 static bool cp_parser_using_declaration
2099 (cp_parser *, bool);
2100 static void cp_parser_using_directive
2101 (cp_parser *);
2102 static tree cp_parser_alias_declaration
2103 (cp_parser *);
2104 static void cp_parser_asm_definition
2105 (cp_parser *);
2106 static void cp_parser_linkage_specification
2107 (cp_parser *);
2108 static void cp_parser_static_assert
2109 (cp_parser *, bool);
2110 static tree cp_parser_decltype
2111 (cp_parser *);
2113 /* Declarators [gram.dcl.decl] */
2115 static tree cp_parser_init_declarator
2116 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2117 static cp_declarator *cp_parser_declarator
2118 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2119 static cp_declarator *cp_parser_direct_declarator
2120 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2121 static enum tree_code cp_parser_ptr_operator
2122 (cp_parser *, tree *, cp_cv_quals *, tree *);
2123 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2124 (cp_parser *);
2125 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2126 (cp_parser *);
2127 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2128 (cp_parser *);
2129 static tree cp_parser_late_return_type_opt
2130 (cp_parser *, cp_declarator *, cp_cv_quals);
2131 static tree cp_parser_declarator_id
2132 (cp_parser *, bool);
2133 static tree cp_parser_type_id
2134 (cp_parser *);
2135 static tree cp_parser_template_type_arg
2136 (cp_parser *);
2137 static tree cp_parser_trailing_type_id (cp_parser *);
2138 static tree cp_parser_type_id_1
2139 (cp_parser *, bool, bool);
2140 static void cp_parser_type_specifier_seq
2141 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2142 static tree cp_parser_parameter_declaration_clause
2143 (cp_parser *);
2144 static tree cp_parser_parameter_declaration_list
2145 (cp_parser *, bool *);
2146 static cp_parameter_declarator *cp_parser_parameter_declaration
2147 (cp_parser *, bool, bool *);
2148 static tree cp_parser_default_argument
2149 (cp_parser *, bool);
2150 static void cp_parser_function_body
2151 (cp_parser *, bool);
2152 static tree cp_parser_initializer
2153 (cp_parser *, bool *, bool *);
2154 static tree cp_parser_initializer_clause
2155 (cp_parser *, bool *);
2156 static tree cp_parser_braced_list
2157 (cp_parser*, bool*);
2158 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2159 (cp_parser *, bool *);
2161 static bool cp_parser_ctor_initializer_opt_and_function_body
2162 (cp_parser *, bool);
2164 static tree cp_parser_late_parsing_omp_declare_simd
2165 (cp_parser *, tree);
2167 static tree cp_parser_late_parsing_cilk_simd_fn_info
2168 (cp_parser *, tree);
2170 static tree synthesize_implicit_template_parm
2171 (cp_parser *);
2172 static tree finish_fully_implicit_template
2173 (cp_parser *, tree);
2175 /* Classes [gram.class] */
2177 static tree cp_parser_class_name
2178 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2179 static tree cp_parser_class_specifier
2180 (cp_parser *);
2181 static tree cp_parser_class_head
2182 (cp_parser *, bool *);
2183 static enum tag_types cp_parser_class_key
2184 (cp_parser *);
2185 static void cp_parser_type_parameter_key
2186 (cp_parser* parser);
2187 static void cp_parser_member_specification_opt
2188 (cp_parser *);
2189 static void cp_parser_member_declaration
2190 (cp_parser *);
2191 static tree cp_parser_pure_specifier
2192 (cp_parser *);
2193 static tree cp_parser_constant_initializer
2194 (cp_parser *);
2196 /* Derived classes [gram.class.derived] */
2198 static tree cp_parser_base_clause
2199 (cp_parser *);
2200 static tree cp_parser_base_specifier
2201 (cp_parser *);
2203 /* Special member functions [gram.special] */
2205 static tree cp_parser_conversion_function_id
2206 (cp_parser *);
2207 static tree cp_parser_conversion_type_id
2208 (cp_parser *);
2209 static cp_declarator *cp_parser_conversion_declarator_opt
2210 (cp_parser *);
2211 static bool cp_parser_ctor_initializer_opt
2212 (cp_parser *);
2213 static void cp_parser_mem_initializer_list
2214 (cp_parser *);
2215 static tree cp_parser_mem_initializer
2216 (cp_parser *);
2217 static tree cp_parser_mem_initializer_id
2218 (cp_parser *);
2220 /* Overloading [gram.over] */
2222 static tree cp_parser_operator_function_id
2223 (cp_parser *);
2224 static tree cp_parser_operator
2225 (cp_parser *);
2227 /* Templates [gram.temp] */
2229 static void cp_parser_template_declaration
2230 (cp_parser *, bool);
2231 static tree cp_parser_template_parameter_list
2232 (cp_parser *);
2233 static tree cp_parser_template_parameter
2234 (cp_parser *, bool *, bool *);
2235 static tree cp_parser_type_parameter
2236 (cp_parser *, bool *);
2237 static tree cp_parser_template_id
2238 (cp_parser *, bool, bool, enum tag_types, bool);
2239 static tree cp_parser_template_name
2240 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2241 static tree cp_parser_template_argument_list
2242 (cp_parser *);
2243 static tree cp_parser_template_argument
2244 (cp_parser *);
2245 static void cp_parser_explicit_instantiation
2246 (cp_parser *);
2247 static void cp_parser_explicit_specialization
2248 (cp_parser *);
2250 /* Exception handling [gram.exception] */
2252 static tree cp_parser_try_block
2253 (cp_parser *);
2254 static bool cp_parser_function_try_block
2255 (cp_parser *);
2256 static void cp_parser_handler_seq
2257 (cp_parser *);
2258 static void cp_parser_handler
2259 (cp_parser *);
2260 static tree cp_parser_exception_declaration
2261 (cp_parser *);
2262 static tree cp_parser_throw_expression
2263 (cp_parser *);
2264 static tree cp_parser_exception_specification_opt
2265 (cp_parser *);
2266 static tree cp_parser_type_id_list
2267 (cp_parser *);
2269 /* GNU Extensions */
2271 static tree cp_parser_asm_specification_opt
2272 (cp_parser *);
2273 static tree cp_parser_asm_operand_list
2274 (cp_parser *);
2275 static tree cp_parser_asm_clobber_list
2276 (cp_parser *);
2277 static tree cp_parser_asm_label_list
2278 (cp_parser *);
2279 static bool cp_next_tokens_can_be_attribute_p
2280 (cp_parser *);
2281 static bool cp_next_tokens_can_be_gnu_attribute_p
2282 (cp_parser *);
2283 static bool cp_next_tokens_can_be_std_attribute_p
2284 (cp_parser *);
2285 static bool cp_nth_tokens_can_be_std_attribute_p
2286 (cp_parser *, size_t);
2287 static bool cp_nth_tokens_can_be_gnu_attribute_p
2288 (cp_parser *, size_t);
2289 static bool cp_nth_tokens_can_be_attribute_p
2290 (cp_parser *, size_t);
2291 static tree cp_parser_attributes_opt
2292 (cp_parser *);
2293 static tree cp_parser_gnu_attributes_opt
2294 (cp_parser *);
2295 static tree cp_parser_gnu_attribute_list
2296 (cp_parser *);
2297 static tree cp_parser_std_attribute
2298 (cp_parser *);
2299 static tree cp_parser_std_attribute_spec
2300 (cp_parser *);
2301 static tree cp_parser_std_attribute_spec_seq
2302 (cp_parser *);
2303 static bool cp_parser_extension_opt
2304 (cp_parser *, int *);
2305 static void cp_parser_label_declaration
2306 (cp_parser *);
2308 /* Transactional Memory Extensions */
2310 static tree cp_parser_transaction
2311 (cp_parser *, enum rid);
2312 static tree cp_parser_transaction_expression
2313 (cp_parser *, enum rid);
2314 static bool cp_parser_function_transaction
2315 (cp_parser *, enum rid);
2316 static tree cp_parser_transaction_cancel
2317 (cp_parser *);
2319 enum pragma_context {
2320 pragma_external,
2321 pragma_member,
2322 pragma_objc_icode,
2323 pragma_stmt,
2324 pragma_compound
2326 static bool cp_parser_pragma
2327 (cp_parser *, enum pragma_context);
2329 /* Objective-C++ Productions */
2331 static tree cp_parser_objc_message_receiver
2332 (cp_parser *);
2333 static tree cp_parser_objc_message_args
2334 (cp_parser *);
2335 static tree cp_parser_objc_message_expression
2336 (cp_parser *);
2337 static tree cp_parser_objc_encode_expression
2338 (cp_parser *);
2339 static tree cp_parser_objc_defs_expression
2340 (cp_parser *);
2341 static tree cp_parser_objc_protocol_expression
2342 (cp_parser *);
2343 static tree cp_parser_objc_selector_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_expression
2346 (cp_parser *);
2347 static bool cp_parser_objc_selector_p
2348 (enum cpp_ttype);
2349 static tree cp_parser_objc_selector
2350 (cp_parser *);
2351 static tree cp_parser_objc_protocol_refs_opt
2352 (cp_parser *);
2353 static void cp_parser_objc_declaration
2354 (cp_parser *, tree);
2355 static tree cp_parser_objc_statement
2356 (cp_parser *);
2357 static bool cp_parser_objc_valid_prefix_attributes
2358 (cp_parser *, tree *);
2359 static void cp_parser_objc_at_property_declaration
2360 (cp_parser *) ;
2361 static void cp_parser_objc_at_synthesize_declaration
2362 (cp_parser *) ;
2363 static void cp_parser_objc_at_dynamic_declaration
2364 (cp_parser *) ;
2365 static tree cp_parser_objc_struct_declaration
2366 (cp_parser *) ;
2368 /* Utility Routines */
2370 static tree cp_parser_lookup_name
2371 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2372 static tree cp_parser_lookup_name_simple
2373 (cp_parser *, tree, location_t);
2374 static tree cp_parser_maybe_treat_template_as_class
2375 (tree, bool);
2376 static bool cp_parser_check_declarator_template_parameters
2377 (cp_parser *, cp_declarator *, location_t);
2378 static bool cp_parser_check_template_parameters
2379 (cp_parser *, unsigned, location_t, cp_declarator *);
2380 static tree cp_parser_simple_cast_expression
2381 (cp_parser *);
2382 static tree cp_parser_global_scope_opt
2383 (cp_parser *, bool);
2384 static bool cp_parser_constructor_declarator_p
2385 (cp_parser *, bool);
2386 static tree cp_parser_function_definition_from_specifiers_and_declarator
2387 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2388 static tree cp_parser_function_definition_after_declarator
2389 (cp_parser *, bool);
2390 static void cp_parser_template_declaration_after_export
2391 (cp_parser *, bool);
2392 static void cp_parser_perform_template_parameter_access_checks
2393 (vec<deferred_access_check, va_gc> *);
2394 static tree cp_parser_single_declaration
2395 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2396 static tree cp_parser_functional_cast
2397 (cp_parser *, tree);
2398 static tree cp_parser_save_member_function_body
2399 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2400 static tree cp_parser_save_nsdmi
2401 (cp_parser *);
2402 static tree cp_parser_enclosed_template_argument_list
2403 (cp_parser *);
2404 static void cp_parser_save_default_args
2405 (cp_parser *, tree);
2406 static void cp_parser_late_parsing_for_member
2407 (cp_parser *, tree);
2408 static tree cp_parser_late_parse_one_default_arg
2409 (cp_parser *, tree, tree, tree);
2410 static void cp_parser_late_parsing_nsdmi
2411 (cp_parser *, tree);
2412 static void cp_parser_late_parsing_default_args
2413 (cp_parser *, tree);
2414 static tree cp_parser_sizeof_operand
2415 (cp_parser *, enum rid);
2416 static tree cp_parser_trait_expr
2417 (cp_parser *, enum rid);
2418 static bool cp_parser_declares_only_class_p
2419 (cp_parser *);
2420 static void cp_parser_set_storage_class
2421 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2422 static void cp_parser_set_decl_spec_type
2423 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2424 static void set_and_check_decl_spec_loc
2425 (cp_decl_specifier_seq *decl_specs,
2426 cp_decl_spec ds, cp_token *);
2427 static bool cp_parser_friend_p
2428 (const cp_decl_specifier_seq *);
2429 static void cp_parser_required_error
2430 (cp_parser *, required_token, bool);
2431 static cp_token *cp_parser_require
2432 (cp_parser *, enum cpp_ttype, required_token);
2433 static cp_token *cp_parser_require_keyword
2434 (cp_parser *, enum rid, required_token);
2435 static bool cp_parser_token_starts_function_definition_p
2436 (cp_token *);
2437 static bool cp_parser_next_token_starts_class_definition_p
2438 (cp_parser *);
2439 static bool cp_parser_next_token_ends_template_argument_p
2440 (cp_parser *);
2441 static bool cp_parser_nth_token_starts_template_argument_list_p
2442 (cp_parser *, size_t);
2443 static enum tag_types cp_parser_token_is_class_key
2444 (cp_token *);
2445 static enum tag_types cp_parser_token_is_type_parameter_key
2446 (cp_token *);
2447 static void cp_parser_check_class_key
2448 (enum tag_types, tree type);
2449 static void cp_parser_check_access_in_redeclaration
2450 (tree type, location_t location);
2451 static bool cp_parser_optional_template_keyword
2452 (cp_parser *);
2453 static void cp_parser_pre_parsed_nested_name_specifier
2454 (cp_parser *);
2455 static bool cp_parser_cache_group
2456 (cp_parser *, enum cpp_ttype, unsigned);
2457 static tree cp_parser_cache_defarg
2458 (cp_parser *parser, bool nsdmi);
2459 static void cp_parser_parse_tentatively
2460 (cp_parser *);
2461 static void cp_parser_commit_to_tentative_parse
2462 (cp_parser *);
2463 static void cp_parser_commit_to_topmost_tentative_parse
2464 (cp_parser *);
2465 static void cp_parser_abort_tentative_parse
2466 (cp_parser *);
2467 static bool cp_parser_parse_definitely
2468 (cp_parser *);
2469 static inline bool cp_parser_parsing_tentatively
2470 (cp_parser *);
2471 static bool cp_parser_uncommitted_to_tentative_parse_p
2472 (cp_parser *);
2473 static void cp_parser_error
2474 (cp_parser *, const char *);
2475 static void cp_parser_name_lookup_error
2476 (cp_parser *, tree, tree, name_lookup_error, location_t);
2477 static bool cp_parser_simulate_error
2478 (cp_parser *);
2479 static bool cp_parser_check_type_definition
2480 (cp_parser *);
2481 static void cp_parser_check_for_definition_in_return_type
2482 (cp_declarator *, tree, location_t type_location);
2483 static void cp_parser_check_for_invalid_template_id
2484 (cp_parser *, tree, enum tag_types, location_t location);
2485 static bool cp_parser_non_integral_constant_expression
2486 (cp_parser *, non_integral_constant);
2487 static void cp_parser_diagnose_invalid_type_name
2488 (cp_parser *, tree, location_t);
2489 static bool cp_parser_parse_and_diagnose_invalid_type_name
2490 (cp_parser *);
2491 static int cp_parser_skip_to_closing_parenthesis
2492 (cp_parser *, bool, bool, bool);
2493 static void cp_parser_skip_to_end_of_statement
2494 (cp_parser *);
2495 static void cp_parser_consume_semicolon_at_end_of_statement
2496 (cp_parser *);
2497 static void cp_parser_skip_to_end_of_block_or_statement
2498 (cp_parser *);
2499 static bool cp_parser_skip_to_closing_brace
2500 (cp_parser *);
2501 static void cp_parser_skip_to_end_of_template_parameter_list
2502 (cp_parser *);
2503 static void cp_parser_skip_to_pragma_eol
2504 (cp_parser*, cp_token *);
2505 static bool cp_parser_error_occurred
2506 (cp_parser *);
2507 static bool cp_parser_allow_gnu_extensions_p
2508 (cp_parser *);
2509 static bool cp_parser_is_pure_string_literal
2510 (cp_token *);
2511 static bool cp_parser_is_string_literal
2512 (cp_token *);
2513 static bool cp_parser_is_keyword
2514 (cp_token *, enum rid);
2515 static tree cp_parser_make_typename_type
2516 (cp_parser *, tree, location_t location);
2517 static cp_declarator * cp_parser_make_indirect_declarator
2518 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2519 static bool cp_parser_compound_literal_p
2520 (cp_parser *);
2521 static bool cp_parser_array_designator_p
2522 (cp_parser *);
2523 static bool cp_parser_skip_to_closing_square_bracket
2524 (cp_parser *);
2526 /* Returns nonzero if we are parsing tentatively. */
2528 static inline bool
2529 cp_parser_parsing_tentatively (cp_parser* parser)
2531 return parser->context->next != NULL;
2534 /* Returns nonzero if TOKEN is a string literal. */
2536 static bool
2537 cp_parser_is_pure_string_literal (cp_token* token)
2539 return (token->type == CPP_STRING ||
2540 token->type == CPP_STRING16 ||
2541 token->type == CPP_STRING32 ||
2542 token->type == CPP_WSTRING ||
2543 token->type == CPP_UTF8STRING);
2546 /* Returns nonzero if TOKEN is a string literal
2547 of a user-defined string literal. */
2549 static bool
2550 cp_parser_is_string_literal (cp_token* token)
2552 return (cp_parser_is_pure_string_literal (token) ||
2553 token->type == CPP_STRING_USERDEF ||
2554 token->type == CPP_STRING16_USERDEF ||
2555 token->type == CPP_STRING32_USERDEF ||
2556 token->type == CPP_WSTRING_USERDEF ||
2557 token->type == CPP_UTF8STRING_USERDEF);
2560 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2562 static bool
2563 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2565 return token->keyword == keyword;
2568 /* If not parsing tentatively, issue a diagnostic of the form
2569 FILE:LINE: MESSAGE before TOKEN
2570 where TOKEN is the next token in the input stream. MESSAGE
2571 (specified by the caller) is usually of the form "expected
2572 OTHER-TOKEN". */
2574 static void
2575 cp_parser_error (cp_parser* parser, const char* gmsgid)
2577 if (!cp_parser_simulate_error (parser))
2579 cp_token *token = cp_lexer_peek_token (parser->lexer);
2580 /* This diagnostic makes more sense if it is tagged to the line
2581 of the token we just peeked at. */
2582 cp_lexer_set_source_position_from_token (token);
2584 if (token->type == CPP_PRAGMA)
2586 error_at (token->location,
2587 "%<#pragma%> is not allowed here");
2588 cp_parser_skip_to_pragma_eol (parser, token);
2589 return;
2592 c_parse_error (gmsgid,
2593 /* Because c_parser_error does not understand
2594 CPP_KEYWORD, keywords are treated like
2595 identifiers. */
2596 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2597 token->u.value, token->flags);
2601 /* Issue an error about name-lookup failing. NAME is the
2602 IDENTIFIER_NODE DECL is the result of
2603 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2604 the thing that we hoped to find. */
2606 static void
2607 cp_parser_name_lookup_error (cp_parser* parser,
2608 tree name,
2609 tree decl,
2610 name_lookup_error desired,
2611 location_t location)
2613 /* If name lookup completely failed, tell the user that NAME was not
2614 declared. */
2615 if (decl == error_mark_node)
2617 if (parser->scope && parser->scope != global_namespace)
2618 error_at (location, "%<%E::%E%> has not been declared",
2619 parser->scope, name);
2620 else if (parser->scope == global_namespace)
2621 error_at (location, "%<::%E%> has not been declared", name);
2622 else if (parser->object_scope
2623 && !CLASS_TYPE_P (parser->object_scope))
2624 error_at (location, "request for member %qE in non-class type %qT",
2625 name, parser->object_scope);
2626 else if (parser->object_scope)
2627 error_at (location, "%<%T::%E%> has not been declared",
2628 parser->object_scope, name);
2629 else
2630 error_at (location, "%qE has not been declared", name);
2632 else if (parser->scope && parser->scope != global_namespace)
2634 switch (desired)
2636 case NLE_TYPE:
2637 error_at (location, "%<%E::%E%> is not a type",
2638 parser->scope, name);
2639 break;
2640 case NLE_CXX98:
2641 error_at (location, "%<%E::%E%> is not a class or namespace",
2642 parser->scope, name);
2643 break;
2644 case NLE_NOT_CXX98:
2645 error_at (location,
2646 "%<%E::%E%> is not a class, namespace, or enumeration",
2647 parser->scope, name);
2648 break;
2649 default:
2650 gcc_unreachable ();
2654 else if (parser->scope == global_namespace)
2656 switch (desired)
2658 case NLE_TYPE:
2659 error_at (location, "%<::%E%> is not a type", name);
2660 break;
2661 case NLE_CXX98:
2662 error_at (location, "%<::%E%> is not a class or namespace", name);
2663 break;
2664 case NLE_NOT_CXX98:
2665 error_at (location,
2666 "%<::%E%> is not a class, namespace, or enumeration",
2667 name);
2668 break;
2669 default:
2670 gcc_unreachable ();
2673 else
2675 switch (desired)
2677 case NLE_TYPE:
2678 error_at (location, "%qE is not a type", name);
2679 break;
2680 case NLE_CXX98:
2681 error_at (location, "%qE is not a class or namespace", name);
2682 break;
2683 case NLE_NOT_CXX98:
2684 error_at (location,
2685 "%qE is not a class, namespace, or enumeration", name);
2686 break;
2687 default:
2688 gcc_unreachable ();
2693 /* If we are parsing tentatively, remember that an error has occurred
2694 during this tentative parse. Returns true if the error was
2695 simulated; false if a message should be issued by the caller. */
2697 static bool
2698 cp_parser_simulate_error (cp_parser* parser)
2700 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2702 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2703 return true;
2705 return false;
2708 /* This function is called when a type is defined. If type
2709 definitions are forbidden at this point, an error message is
2710 issued. */
2712 static bool
2713 cp_parser_check_type_definition (cp_parser* parser)
2715 /* If types are forbidden here, issue a message. */
2716 if (parser->type_definition_forbidden_message)
2718 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2719 in the message need to be interpreted. */
2720 error (parser->type_definition_forbidden_message);
2721 return false;
2723 return true;
2726 /* This function is called when the DECLARATOR is processed. The TYPE
2727 was a type defined in the decl-specifiers. If it is invalid to
2728 define a type in the decl-specifiers for DECLARATOR, an error is
2729 issued. TYPE_LOCATION is the location of TYPE and is used
2730 for error reporting. */
2732 static void
2733 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2734 tree type, location_t type_location)
2736 /* [dcl.fct] forbids type definitions in return types.
2737 Unfortunately, it's not easy to know whether or not we are
2738 processing a return type until after the fact. */
2739 while (declarator
2740 && (declarator->kind == cdk_pointer
2741 || declarator->kind == cdk_reference
2742 || declarator->kind == cdk_ptrmem))
2743 declarator = declarator->declarator;
2744 if (declarator
2745 && declarator->kind == cdk_function)
2747 error_at (type_location,
2748 "new types may not be defined in a return type");
2749 inform (type_location,
2750 "(perhaps a semicolon is missing after the definition of %qT)",
2751 type);
2755 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2756 "<" in any valid C++ program. If the next token is indeed "<",
2757 issue a message warning the user about what appears to be an
2758 invalid attempt to form a template-id. LOCATION is the location
2759 of the type-specifier (TYPE) */
2761 static void
2762 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2763 tree type,
2764 enum tag_types tag_type,
2765 location_t location)
2767 cp_token_position start = 0;
2769 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2771 if (TYPE_P (type))
2772 error_at (location, "%qT is not a template", type);
2773 else if (identifier_p (type))
2775 if (tag_type != none_type)
2776 error_at (location, "%qE is not a class template", type);
2777 else
2778 error_at (location, "%qE is not a template", type);
2780 else
2781 error_at (location, "invalid template-id");
2782 /* Remember the location of the invalid "<". */
2783 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2784 start = cp_lexer_token_position (parser->lexer, true);
2785 /* Consume the "<". */
2786 cp_lexer_consume_token (parser->lexer);
2787 /* Parse the template arguments. */
2788 cp_parser_enclosed_template_argument_list (parser);
2789 /* Permanently remove the invalid template arguments so that
2790 this error message is not issued again. */
2791 if (start)
2792 cp_lexer_purge_tokens_after (parser->lexer, start);
2796 /* If parsing an integral constant-expression, issue an error message
2797 about the fact that THING appeared and return true. Otherwise,
2798 return false. In either case, set
2799 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2801 static bool
2802 cp_parser_non_integral_constant_expression (cp_parser *parser,
2803 non_integral_constant thing)
2805 parser->non_integral_constant_expression_p = true;
2806 if (parser->integral_constant_expression_p)
2808 if (!parser->allow_non_integral_constant_expression_p)
2810 const char *msg = NULL;
2811 switch (thing)
2813 case NIC_FLOAT:
2814 error ("floating-point literal "
2815 "cannot appear in a constant-expression");
2816 return true;
2817 case NIC_CAST:
2818 error ("a cast to a type other than an integral or "
2819 "enumeration type cannot appear in a "
2820 "constant-expression");
2821 return true;
2822 case NIC_TYPEID:
2823 error ("%<typeid%> operator "
2824 "cannot appear in a constant-expression");
2825 return true;
2826 case NIC_NCC:
2827 error ("non-constant compound literals "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_FUNC_CALL:
2831 error ("a function call "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_INC:
2835 error ("an increment "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_DEC:
2839 error ("an decrement "
2840 "cannot appear in a constant-expression");
2841 return true;
2842 case NIC_ARRAY_REF:
2843 error ("an array reference "
2844 "cannot appear in a constant-expression");
2845 return true;
2846 case NIC_ADDR_LABEL:
2847 error ("the address of a label "
2848 "cannot appear in a constant-expression");
2849 return true;
2850 case NIC_OVERLOADED:
2851 error ("calls to overloaded operators "
2852 "cannot appear in a constant-expression");
2853 return true;
2854 case NIC_ASSIGNMENT:
2855 error ("an assignment cannot appear in a constant-expression");
2856 return true;
2857 case NIC_COMMA:
2858 error ("a comma operator "
2859 "cannot appear in a constant-expression");
2860 return true;
2861 case NIC_CONSTRUCTOR:
2862 error ("a call to a constructor "
2863 "cannot appear in a constant-expression");
2864 return true;
2865 case NIC_TRANSACTION:
2866 error ("a transaction expression "
2867 "cannot appear in a constant-expression");
2868 return true;
2869 case NIC_THIS:
2870 msg = "this";
2871 break;
2872 case NIC_FUNC_NAME:
2873 msg = "__FUNCTION__";
2874 break;
2875 case NIC_PRETTY_FUNC:
2876 msg = "__PRETTY_FUNCTION__";
2877 break;
2878 case NIC_C99_FUNC:
2879 msg = "__func__";
2880 break;
2881 case NIC_VA_ARG:
2882 msg = "va_arg";
2883 break;
2884 case NIC_ARROW:
2885 msg = "->";
2886 break;
2887 case NIC_POINT:
2888 msg = ".";
2889 break;
2890 case NIC_STAR:
2891 msg = "*";
2892 break;
2893 case NIC_ADDR:
2894 msg = "&";
2895 break;
2896 case NIC_PREINCREMENT:
2897 msg = "++";
2898 break;
2899 case NIC_PREDECREMENT:
2900 msg = "--";
2901 break;
2902 case NIC_NEW:
2903 msg = "new";
2904 break;
2905 case NIC_DEL:
2906 msg = "delete";
2907 break;
2908 default:
2909 gcc_unreachable ();
2911 if (msg)
2912 error ("%qs cannot appear in a constant-expression", msg);
2913 return true;
2916 return false;
2919 /* Emit a diagnostic for an invalid type name. This function commits
2920 to the current active tentative parse, if any. (Otherwise, the
2921 problematic construct might be encountered again later, resulting
2922 in duplicate error messages.) LOCATION is the location of ID. */
2924 static void
2925 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2926 location_t location)
2928 tree decl, ambiguous_decls;
2929 cp_parser_commit_to_tentative_parse (parser);
2930 /* Try to lookup the identifier. */
2931 decl = cp_parser_lookup_name (parser, id, none_type,
2932 /*is_template=*/false,
2933 /*is_namespace=*/false,
2934 /*check_dependency=*/true,
2935 &ambiguous_decls, location);
2936 if (ambiguous_decls)
2937 /* If the lookup was ambiguous, an error will already have
2938 been issued. */
2939 return;
2940 /* If the lookup found a template-name, it means that the user forgot
2941 to specify an argument list. Emit a useful error message. */
2942 if (TREE_CODE (decl) == TEMPLATE_DECL)
2943 error_at (location,
2944 "invalid use of template-name %qE without an argument list",
2945 decl);
2946 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2947 error_at (location, "invalid use of destructor %qD as a type", id);
2948 else if (TREE_CODE (decl) == TYPE_DECL)
2949 /* Something like 'unsigned A a;' */
2950 error_at (location, "invalid combination of multiple type-specifiers");
2951 else if (!parser->scope)
2953 /* Issue an error message. */
2954 error_at (location, "%qE does not name a type", id);
2955 /* If we're in a template class, it's possible that the user was
2956 referring to a type from a base class. For example:
2958 template <typename T> struct A { typedef T X; };
2959 template <typename T> struct B : public A<T> { X x; };
2961 The user should have said "typename A<T>::X". */
2962 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2963 inform (location, "C++11 %<constexpr%> only available with "
2964 "-std=c++11 or -std=gnu++11");
2965 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2966 inform (location, "C++11 %<noexcept%> only available with "
2967 "-std=c++11 or -std=gnu++11");
2968 else if (cxx_dialect < cxx11
2969 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2970 inform (location, "C++11 %<thread_local%> only available with "
2971 "-std=c++11 or -std=gnu++11");
2972 else if (processing_template_decl && current_class_type
2973 && TYPE_BINFO (current_class_type))
2975 tree b;
2977 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2979 b = TREE_CHAIN (b))
2981 tree base_type = BINFO_TYPE (b);
2982 if (CLASS_TYPE_P (base_type)
2983 && dependent_type_p (base_type))
2985 tree field;
2986 /* Go from a particular instantiation of the
2987 template (which will have an empty TYPE_FIELDs),
2988 to the main version. */
2989 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2990 for (field = TYPE_FIELDS (base_type);
2991 field;
2992 field = DECL_CHAIN (field))
2993 if (TREE_CODE (field) == TYPE_DECL
2994 && DECL_NAME (field) == id)
2996 inform (location,
2997 "(perhaps %<typename %T::%E%> was intended)",
2998 BINFO_TYPE (b), id);
2999 break;
3001 if (field)
3002 break;
3007 /* Here we diagnose qualified-ids where the scope is actually correct,
3008 but the identifier does not resolve to a valid type name. */
3009 else if (parser->scope != error_mark_node)
3011 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3013 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3014 error_at (location_of (id),
3015 "%qE in namespace %qE does not name a template type",
3016 id, parser->scope);
3017 else
3018 error_at (location_of (id),
3019 "%qE in namespace %qE does not name a type",
3020 id, parser->scope);
3022 else if (CLASS_TYPE_P (parser->scope)
3023 && constructor_name_p (id, parser->scope))
3025 /* A<T>::A<T>() */
3026 error_at (location, "%<%T::%E%> names the constructor, not"
3027 " the type", parser->scope, id);
3028 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3029 error_at (location, "and %qT has no template constructors",
3030 parser->scope);
3032 else if (TYPE_P (parser->scope)
3033 && dependent_scope_p (parser->scope))
3034 error_at (location, "need %<typename%> before %<%T::%E%> because "
3035 "%qT is a dependent scope",
3036 parser->scope, id, parser->scope);
3037 else if (TYPE_P (parser->scope))
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3040 error_at (location_of (id),
3041 "%qE in %q#T does not name a template type",
3042 id, parser->scope);
3043 else
3044 error_at (location_of (id),
3045 "%qE in %q#T does not name a type",
3046 id, parser->scope);
3048 else
3049 gcc_unreachable ();
3053 /* Check for a common situation where a type-name should be present,
3054 but is not, and issue a sensible error message. Returns true if an
3055 invalid type-name was detected.
3057 The situation handled by this function are variable declarations of the
3058 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3059 Usually, `ID' should name a type, but if we got here it means that it
3060 does not. We try to emit the best possible error message depending on
3061 how exactly the id-expression looks like. */
3063 static bool
3064 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3066 tree id;
3067 cp_token *token = cp_lexer_peek_token (parser->lexer);
3069 /* Avoid duplicate error about ambiguous lookup. */
3070 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3072 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3073 if (next->type == CPP_NAME && next->error_reported)
3074 goto out;
3077 cp_parser_parse_tentatively (parser);
3078 id = cp_parser_id_expression (parser,
3079 /*template_keyword_p=*/false,
3080 /*check_dependency_p=*/true,
3081 /*template_p=*/NULL,
3082 /*declarator_p=*/true,
3083 /*optional_p=*/false);
3084 /* If the next token is a (, this is a function with no explicit return
3085 type, i.e. constructor, destructor or conversion op. */
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3087 || TREE_CODE (id) == TYPE_DECL)
3089 cp_parser_abort_tentative_parse (parser);
3090 return false;
3092 if (!cp_parser_parse_definitely (parser))
3093 return false;
3095 /* Emit a diagnostic for the invalid type. */
3096 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3097 out:
3098 /* If we aren't in the middle of a declarator (i.e. in a
3099 parameter-declaration-clause), skip to the end of the declaration;
3100 there's no point in trying to process it. */
3101 if (!parser->in_declarator_p)
3102 cp_parser_skip_to_end_of_block_or_statement (parser);
3103 return true;
3106 /* Consume tokens up to, and including, the next non-nested closing `)'.
3107 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3108 are doing error recovery. Returns -1 if OR_COMMA is true and we
3109 found an unnested comma. */
3111 static int
3112 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3113 bool recovering,
3114 bool or_comma,
3115 bool consume_paren)
3117 unsigned paren_depth = 0;
3118 unsigned brace_depth = 0;
3119 unsigned square_depth = 0;
3121 if (recovering && !or_comma
3122 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3123 return 0;
3125 while (true)
3127 cp_token * token = cp_lexer_peek_token (parser->lexer);
3129 switch (token->type)
3131 case CPP_EOF:
3132 case CPP_PRAGMA_EOL:
3133 /* If we've run out of tokens, then there is no closing `)'. */
3134 return 0;
3136 /* This is good for lambda expression capture-lists. */
3137 case CPP_OPEN_SQUARE:
3138 ++square_depth;
3139 break;
3140 case CPP_CLOSE_SQUARE:
3141 if (!square_depth--)
3142 return 0;
3143 break;
3145 case CPP_SEMICOLON:
3146 /* This matches the processing in skip_to_end_of_statement. */
3147 if (!brace_depth)
3148 return 0;
3149 break;
3151 case CPP_OPEN_BRACE:
3152 ++brace_depth;
3153 break;
3154 case CPP_CLOSE_BRACE:
3155 if (!brace_depth--)
3156 return 0;
3157 break;
3159 case CPP_COMMA:
3160 if (recovering && or_comma && !brace_depth && !paren_depth
3161 && !square_depth)
3162 return -1;
3163 break;
3165 case CPP_OPEN_PAREN:
3166 if (!brace_depth)
3167 ++paren_depth;
3168 break;
3170 case CPP_CLOSE_PAREN:
3171 if (!brace_depth && !paren_depth--)
3173 if (consume_paren)
3174 cp_lexer_consume_token (parser->lexer);
3175 return 1;
3177 break;
3179 default:
3180 break;
3183 /* Consume the token. */
3184 cp_lexer_consume_token (parser->lexer);
3188 /* Consume tokens until we reach the end of the current statement.
3189 Normally, that will be just before consuming a `;'. However, if a
3190 non-nested `}' comes first, then we stop before consuming that. */
3192 static void
3193 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3195 unsigned nesting_depth = 0;
3197 /* Unwind generic function template scope if necessary. */
3198 if (parser->fully_implicit_function_template_p)
3199 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3201 while (true)
3203 cp_token *token = cp_lexer_peek_token (parser->lexer);
3205 switch (token->type)
3207 case CPP_EOF:
3208 case CPP_PRAGMA_EOL:
3209 /* If we've run out of tokens, stop. */
3210 return;
3212 case CPP_SEMICOLON:
3213 /* If the next token is a `;', we have reached the end of the
3214 statement. */
3215 if (!nesting_depth)
3216 return;
3217 break;
3219 case CPP_CLOSE_BRACE:
3220 /* If this is a non-nested '}', stop before consuming it.
3221 That way, when confronted with something like:
3223 { 3 + }
3225 we stop before consuming the closing '}', even though we
3226 have not yet reached a `;'. */
3227 if (nesting_depth == 0)
3228 return;
3230 /* If it is the closing '}' for a block that we have
3231 scanned, stop -- but only after consuming the token.
3232 That way given:
3234 void f g () { ... }
3235 typedef int I;
3237 we will stop after the body of the erroneously declared
3238 function, but before consuming the following `typedef'
3239 declaration. */
3240 if (--nesting_depth == 0)
3242 cp_lexer_consume_token (parser->lexer);
3243 return;
3246 case CPP_OPEN_BRACE:
3247 ++nesting_depth;
3248 break;
3250 default:
3251 break;
3254 /* Consume the token. */
3255 cp_lexer_consume_token (parser->lexer);
3259 /* This function is called at the end of a statement or declaration.
3260 If the next token is a semicolon, it is consumed; otherwise, error
3261 recovery is attempted. */
3263 static void
3264 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3266 /* Look for the trailing `;'. */
3267 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3269 /* If there is additional (erroneous) input, skip to the end of
3270 the statement. */
3271 cp_parser_skip_to_end_of_statement (parser);
3272 /* If the next token is now a `;', consume it. */
3273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3274 cp_lexer_consume_token (parser->lexer);
3278 /* Skip tokens until we have consumed an entire block, or until we
3279 have consumed a non-nested `;'. */
3281 static void
3282 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3284 int nesting_depth = 0;
3286 /* Unwind generic function template scope if necessary. */
3287 if (parser->fully_implicit_function_template_p)
3288 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3290 while (nesting_depth >= 0)
3292 cp_token *token = cp_lexer_peek_token (parser->lexer);
3294 switch (token->type)
3296 case CPP_EOF:
3297 case CPP_PRAGMA_EOL:
3298 /* If we've run out of tokens, stop. */
3299 return;
3301 case CPP_SEMICOLON:
3302 /* Stop if this is an unnested ';'. */
3303 if (!nesting_depth)
3304 nesting_depth = -1;
3305 break;
3307 case CPP_CLOSE_BRACE:
3308 /* Stop if this is an unnested '}', or closes the outermost
3309 nesting level. */
3310 nesting_depth--;
3311 if (nesting_depth < 0)
3312 return;
3313 if (!nesting_depth)
3314 nesting_depth = -1;
3315 break;
3317 case CPP_OPEN_BRACE:
3318 /* Nest. */
3319 nesting_depth++;
3320 break;
3322 default:
3323 break;
3326 /* Consume the token. */
3327 cp_lexer_consume_token (parser->lexer);
3331 /* Skip tokens until a non-nested closing curly brace is the next
3332 token, or there are no more tokens. Return true in the first case,
3333 false otherwise. */
3335 static bool
3336 cp_parser_skip_to_closing_brace (cp_parser *parser)
3338 unsigned nesting_depth = 0;
3340 while (true)
3342 cp_token *token = cp_lexer_peek_token (parser->lexer);
3344 switch (token->type)
3346 case CPP_EOF:
3347 case CPP_PRAGMA_EOL:
3348 /* If we've run out of tokens, stop. */
3349 return false;
3351 case CPP_CLOSE_BRACE:
3352 /* If the next token is a non-nested `}', then we have reached
3353 the end of the current block. */
3354 if (nesting_depth-- == 0)
3355 return true;
3356 break;
3358 case CPP_OPEN_BRACE:
3359 /* If it the next token is a `{', then we are entering a new
3360 block. Consume the entire block. */
3361 ++nesting_depth;
3362 break;
3364 default:
3365 break;
3368 /* Consume the token. */
3369 cp_lexer_consume_token (parser->lexer);
3373 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3374 parameter is the PRAGMA token, allowing us to purge the entire pragma
3375 sequence. */
3377 static void
3378 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3380 cp_token *token;
3382 parser->lexer->in_pragma = false;
3385 token = cp_lexer_consume_token (parser->lexer);
3386 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3388 /* Ensure that the pragma is not parsed again. */
3389 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3392 /* Require pragma end of line, resyncing with it as necessary. The
3393 arguments are as for cp_parser_skip_to_pragma_eol. */
3395 static void
3396 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3398 parser->lexer->in_pragma = false;
3399 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3403 /* This is a simple wrapper around make_typename_type. When the id is
3404 an unresolved identifier node, we can provide a superior diagnostic
3405 using cp_parser_diagnose_invalid_type_name. */
3407 static tree
3408 cp_parser_make_typename_type (cp_parser *parser, tree id,
3409 location_t id_location)
3411 tree result;
3412 if (identifier_p (id))
3414 result = make_typename_type (parser->scope, id, typename_type,
3415 /*complain=*/tf_none);
3416 if (result == error_mark_node)
3417 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3418 return result;
3420 return make_typename_type (parser->scope, id, typename_type, tf_error);
3423 /* This is a wrapper around the
3424 make_{pointer,ptrmem,reference}_declarator functions that decides
3425 which one to call based on the CODE and CLASS_TYPE arguments. The
3426 CODE argument should be one of the values returned by
3427 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3428 appertain to the pointer or reference. */
3430 static cp_declarator *
3431 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3432 cp_cv_quals cv_qualifiers,
3433 cp_declarator *target,
3434 tree attributes)
3436 if (code == ERROR_MARK)
3437 return cp_error_declarator;
3439 if (code == INDIRECT_REF)
3440 if (class_type == NULL_TREE)
3441 return make_pointer_declarator (cv_qualifiers, target, attributes);
3442 else
3443 return make_ptrmem_declarator (cv_qualifiers, class_type,
3444 target, attributes);
3445 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3446 return make_reference_declarator (cv_qualifiers, target,
3447 false, attributes);
3448 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3449 return make_reference_declarator (cv_qualifiers, target,
3450 true, attributes);
3451 gcc_unreachable ();
3454 /* Create a new C++ parser. */
3456 static cp_parser *
3457 cp_parser_new (void)
3459 cp_parser *parser;
3460 cp_lexer *lexer;
3461 unsigned i;
3463 /* cp_lexer_new_main is called before doing GC allocation because
3464 cp_lexer_new_main might load a PCH file. */
3465 lexer = cp_lexer_new_main ();
3467 /* Initialize the binops_by_token so that we can get the tree
3468 directly from the token. */
3469 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3470 binops_by_token[binops[i].token_type] = binops[i];
3472 parser = ggc_cleared_alloc<cp_parser> ();
3473 parser->lexer = lexer;
3474 parser->context = cp_parser_context_new (NULL);
3476 /* For now, we always accept GNU extensions. */
3477 parser->allow_gnu_extensions_p = 1;
3479 /* The `>' token is a greater-than operator, not the end of a
3480 template-id. */
3481 parser->greater_than_is_operator_p = true;
3483 parser->default_arg_ok_p = true;
3485 /* We are not parsing a constant-expression. */
3486 parser->integral_constant_expression_p = false;
3487 parser->allow_non_integral_constant_expression_p = false;
3488 parser->non_integral_constant_expression_p = false;
3490 /* Local variable names are not forbidden. */
3491 parser->local_variables_forbidden_p = false;
3493 /* We are not processing an `extern "C"' declaration. */
3494 parser->in_unbraced_linkage_specification_p = false;
3496 /* We are not processing a declarator. */
3497 parser->in_declarator_p = false;
3499 /* We are not processing a template-argument-list. */
3500 parser->in_template_argument_list_p = false;
3502 /* We are not in an iteration statement. */
3503 parser->in_statement = 0;
3505 /* We are not in a switch statement. */
3506 parser->in_switch_statement_p = false;
3508 /* We are not parsing a type-id inside an expression. */
3509 parser->in_type_id_in_expr_p = false;
3511 /* Declarations aren't implicitly extern "C". */
3512 parser->implicit_extern_c = false;
3514 /* String literals should be translated to the execution character set. */
3515 parser->translate_strings_p = true;
3517 /* We are not parsing a function body. */
3518 parser->in_function_body = false;
3520 /* We can correct until told otherwise. */
3521 parser->colon_corrects_to_scope_p = true;
3523 /* The unparsed function queue is empty. */
3524 push_unparsed_function_queues (parser);
3526 /* There are no classes being defined. */
3527 parser->num_classes_being_defined = 0;
3529 /* No template parameters apply. */
3530 parser->num_template_parameter_lists = 0;
3532 /* Not declaring an implicit function template. */
3533 parser->auto_is_implicit_function_template_parm_p = false;
3534 parser->fully_implicit_function_template_p = false;
3535 parser->implicit_template_parms = 0;
3536 parser->implicit_template_scope = 0;
3538 return parser;
3541 /* Create a cp_lexer structure which will emit the tokens in CACHE
3542 and push it onto the parser's lexer stack. This is used for delayed
3543 parsing of in-class method bodies and default arguments, and should
3544 not be confused with tentative parsing. */
3545 static void
3546 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3548 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3549 lexer->next = parser->lexer;
3550 parser->lexer = lexer;
3552 /* Move the current source position to that of the first token in the
3553 new lexer. */
3554 cp_lexer_set_source_position_from_token (lexer->next_token);
3557 /* Pop the top lexer off the parser stack. This is never used for the
3558 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3559 static void
3560 cp_parser_pop_lexer (cp_parser *parser)
3562 cp_lexer *lexer = parser->lexer;
3563 parser->lexer = lexer->next;
3564 cp_lexer_destroy (lexer);
3566 /* Put the current source position back where it was before this
3567 lexer was pushed. */
3568 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3571 /* Lexical conventions [gram.lex] */
3573 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3574 identifier. */
3576 static tree
3577 cp_parser_identifier (cp_parser* parser)
3579 cp_token *token;
3581 /* Look for the identifier. */
3582 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3583 /* Return the value. */
3584 return token ? token->u.value : error_mark_node;
3587 /* Parse a sequence of adjacent string constants. Returns a
3588 TREE_STRING representing the combined, nul-terminated string
3589 constant. If TRANSLATE is true, translate the string to the
3590 execution character set. If WIDE_OK is true, a wide string is
3591 invalid here.
3593 C++98 [lex.string] says that if a narrow string literal token is
3594 adjacent to a wide string literal token, the behavior is undefined.
3595 However, C99 6.4.5p4 says that this results in a wide string literal.
3596 We follow C99 here, for consistency with the C front end.
3598 This code is largely lifted from lex_string() in c-lex.c.
3600 FUTURE: ObjC++ will need to handle @-strings here. */
3601 static tree
3602 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3603 bool lookup_udlit = true)
3605 tree value;
3606 size_t count;
3607 struct obstack str_ob;
3608 cpp_string str, istr, *strs;
3609 cp_token *tok;
3610 enum cpp_ttype type, curr_type;
3611 int have_suffix_p = 0;
3612 tree string_tree;
3613 tree suffix_id = NULL_TREE;
3614 bool curr_tok_is_userdef_p = false;
3616 tok = cp_lexer_peek_token (parser->lexer);
3617 if (!cp_parser_is_string_literal (tok))
3619 cp_parser_error (parser, "expected string-literal");
3620 return error_mark_node;
3623 if (cpp_userdef_string_p (tok->type))
3625 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3626 curr_type = cpp_userdef_string_remove_type (tok->type);
3627 curr_tok_is_userdef_p = true;
3629 else
3631 string_tree = tok->u.value;
3632 curr_type = tok->type;
3634 type = curr_type;
3636 /* Try to avoid the overhead of creating and destroying an obstack
3637 for the common case of just one string. */
3638 if (!cp_parser_is_string_literal
3639 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3641 cp_lexer_consume_token (parser->lexer);
3643 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3644 str.len = TREE_STRING_LENGTH (string_tree);
3645 count = 1;
3647 if (curr_tok_is_userdef_p)
3649 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3650 have_suffix_p = 1;
3651 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 else
3654 curr_type = tok->type;
3656 strs = &str;
3658 else
3660 gcc_obstack_init (&str_ob);
3661 count = 0;
3665 cp_lexer_consume_token (parser->lexer);
3666 count++;
3667 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3668 str.len = TREE_STRING_LENGTH (string_tree);
3670 if (curr_tok_is_userdef_p)
3672 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3673 if (have_suffix_p == 0)
3675 suffix_id = curr_suffix_id;
3676 have_suffix_p = 1;
3678 else if (have_suffix_p == 1
3679 && curr_suffix_id != suffix_id)
3681 error ("inconsistent user-defined literal suffixes"
3682 " %qD and %qD in string literal",
3683 suffix_id, curr_suffix_id);
3684 have_suffix_p = -1;
3686 curr_type = cpp_userdef_string_remove_type (tok->type);
3688 else
3689 curr_type = tok->type;
3691 if (type != curr_type)
3693 if (type == CPP_STRING)
3694 type = curr_type;
3695 else if (curr_type != CPP_STRING)
3696 error_at (tok->location,
3697 "unsupported non-standard concatenation "
3698 "of string literals");
3701 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3703 tok = cp_lexer_peek_token (parser->lexer);
3704 if (cpp_userdef_string_p (tok->type))
3706 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3707 curr_type = cpp_userdef_string_remove_type (tok->type);
3708 curr_tok_is_userdef_p = true;
3710 else
3712 string_tree = tok->u.value;
3713 curr_type = tok->type;
3714 curr_tok_is_userdef_p = false;
3717 while (cp_parser_is_string_literal (tok));
3719 strs = (cpp_string *) obstack_finish (&str_ob);
3722 if (type != CPP_STRING && !wide_ok)
3724 cp_parser_error (parser, "a wide string is invalid in this context");
3725 type = CPP_STRING;
3728 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3729 (parse_in, strs, count, &istr, type))
3731 value = build_string (istr.len, (const char *)istr.text);
3732 free (CONST_CAST (unsigned char *, istr.text));
3734 switch (type)
3736 default:
3737 case CPP_STRING:
3738 case CPP_UTF8STRING:
3739 TREE_TYPE (value) = char_array_type_node;
3740 break;
3741 case CPP_STRING16:
3742 TREE_TYPE (value) = char16_array_type_node;
3743 break;
3744 case CPP_STRING32:
3745 TREE_TYPE (value) = char32_array_type_node;
3746 break;
3747 case CPP_WSTRING:
3748 TREE_TYPE (value) = wchar_array_type_node;
3749 break;
3752 value = fix_string_type (value);
3754 if (have_suffix_p)
3756 tree literal = build_userdef_literal (suffix_id, value,
3757 OT_NONE, NULL_TREE);
3758 if (lookup_udlit)
3759 value = cp_parser_userdef_string_literal (literal);
3760 else
3761 value = literal;
3764 else
3765 /* cpp_interpret_string has issued an error. */
3766 value = error_mark_node;
3768 if (count > 1)
3769 obstack_free (&str_ob, 0);
3771 return value;
3774 /* Look up a literal operator with the name and the exact arguments. */
3776 static tree
3777 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3779 tree decl, fns;
3780 decl = lookup_name (name);
3781 if (!decl || !is_overloaded_fn (decl))
3782 return error_mark_node;
3784 for (fns = decl; fns; fns = OVL_NEXT (fns))
3786 unsigned int ix;
3787 bool found = true;
3788 tree fn = OVL_CURRENT (fns);
3789 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3790 if (parmtypes != NULL_TREE)
3792 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3793 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3795 tree tparm = TREE_VALUE (parmtypes);
3796 tree targ = TREE_TYPE ((*args)[ix]);
3797 bool ptr = TYPE_PTR_P (tparm);
3798 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3799 if ((ptr || arr || !same_type_p (tparm, targ))
3800 && (!ptr || !arr
3801 || !same_type_p (TREE_TYPE (tparm),
3802 TREE_TYPE (targ))))
3803 found = false;
3805 if (found
3806 && ix == vec_safe_length (args)
3807 /* May be this should be sufficient_parms_p instead,
3808 depending on how exactly should user-defined literals
3809 work in presence of default arguments on the literal
3810 operator parameters. */
3811 && parmtypes == void_list_node)
3812 return fn;
3816 return error_mark_node;
3819 /* Parse a user-defined char constant. Returns a call to a user-defined
3820 literal operator taking the character as an argument. */
3822 static tree
3823 cp_parser_userdef_char_literal (cp_parser *parser)
3825 cp_token *token = cp_lexer_consume_token (parser->lexer);
3826 tree literal = token->u.value;
3827 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3828 tree value = USERDEF_LITERAL_VALUE (literal);
3829 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3830 tree decl, result;
3832 /* Build up a call to the user-defined operator */
3833 /* Lookup the name we got back from the id-expression. */
3834 vec<tree, va_gc> *args = make_tree_vector ();
3835 vec_safe_push (args, value);
3836 decl = lookup_literal_operator (name, args);
3837 if (!decl || decl == error_mark_node)
3839 error ("unable to find character literal operator %qD with %qT argument",
3840 name, TREE_TYPE (value));
3841 release_tree_vector (args);
3842 return error_mark_node;
3844 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3845 release_tree_vector (args);
3846 if (result != error_mark_node)
3847 return result;
3849 error ("unable to find character literal operator %qD with %qT argument",
3850 name, TREE_TYPE (value));
3851 return error_mark_node;
3854 /* A subroutine of cp_parser_userdef_numeric_literal to
3855 create a char... template parameter pack from a string node. */
3857 static tree
3858 make_char_string_pack (tree value)
3860 tree charvec;
3861 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3862 const char *str = TREE_STRING_POINTER (value);
3863 int i, len = TREE_STRING_LENGTH (value) - 1;
3864 tree argvec = make_tree_vec (1);
3866 /* Fill in CHARVEC with all of the parameters. */
3867 charvec = make_tree_vec (len);
3868 for (i = 0; i < len; ++i)
3869 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3871 /* Build the argument packs. */
3872 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3873 TREE_TYPE (argpack) = char_type_node;
3875 TREE_VEC_ELT (argvec, 0) = argpack;
3877 return argvec;
3880 /* A subroutine of cp_parser_userdef_numeric_literal to
3881 create a char... template parameter pack from a string node. */
3883 static tree
3884 make_string_pack (tree value)
3886 tree charvec;
3887 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3888 const unsigned char *str
3889 = (const unsigned char *) TREE_STRING_POINTER (value);
3890 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3891 int len = TREE_STRING_LENGTH (value) / sz - 1;
3892 tree argvec = make_tree_vec (2);
3894 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3895 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3897 /* First template parm is character type. */
3898 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3900 /* Fill in CHARVEC with all of the parameters. */
3901 charvec = make_tree_vec (len);
3902 for (int i = 0; i < len; ++i)
3903 TREE_VEC_ELT (charvec, i)
3904 = double_int_to_tree (str_char_type_node,
3905 double_int::from_buffer (str + i * sz, sz));
3907 /* Build the argument packs. */
3908 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3909 TREE_TYPE (argpack) = str_char_type_node;
3911 TREE_VEC_ELT (argvec, 1) = argpack;
3913 return argvec;
3916 /* Parse a user-defined numeric constant. returns a call to a user-defined
3917 literal operator. */
3919 static tree
3920 cp_parser_userdef_numeric_literal (cp_parser *parser)
3922 cp_token *token = cp_lexer_consume_token (parser->lexer);
3923 tree literal = token->u.value;
3924 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3925 tree value = USERDEF_LITERAL_VALUE (literal);
3926 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3927 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3928 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3929 tree decl, result;
3930 vec<tree, va_gc> *args;
3932 /* Look for a literal operator taking the exact type of numeric argument
3933 as the literal value. */
3934 args = make_tree_vector ();
3935 vec_safe_push (args, value);
3936 decl = lookup_literal_operator (name, args);
3937 if (decl && decl != error_mark_node)
3939 result = finish_call_expr (decl, &args, false, true, tf_none);
3940 if (result != error_mark_node)
3942 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3943 warning_at (token->location, OPT_Woverflow,
3944 "integer literal exceeds range of %qT type",
3945 long_long_unsigned_type_node);
3946 else
3948 if (overflow > 0)
3949 warning_at (token->location, OPT_Woverflow,
3950 "floating literal exceeds range of %qT type",
3951 long_double_type_node);
3952 else if (overflow < 0)
3953 warning_at (token->location, OPT_Woverflow,
3954 "floating literal truncated to zero");
3956 release_tree_vector (args);
3957 return result;
3960 release_tree_vector (args);
3962 /* If the numeric argument didn't work, look for a raw literal
3963 operator taking a const char* argument consisting of the number
3964 in string format. */
3965 args = make_tree_vector ();
3966 vec_safe_push (args, num_string);
3967 decl = lookup_literal_operator (name, args);
3968 if (decl && decl != error_mark_node)
3970 result = finish_call_expr (decl, &args, false, true, tf_none);
3971 if (result != error_mark_node)
3973 release_tree_vector (args);
3974 return result;
3977 release_tree_vector (args);
3979 /* If the raw literal didn't work, look for a non-type template
3980 function with parameter pack char.... Call the function with
3981 template parameter characters representing the number. */
3982 args = make_tree_vector ();
3983 decl = lookup_literal_operator (name, args);
3984 if (decl && decl != error_mark_node)
3986 tree tmpl_args = make_char_string_pack (num_string);
3987 decl = lookup_template_function (decl, tmpl_args);
3988 result = finish_call_expr (decl, &args, false, true, tf_none);
3989 if (result != error_mark_node)
3991 release_tree_vector (args);
3992 return result;
3995 release_tree_vector (args);
3997 error ("unable to find numeric literal operator %qD", name);
3998 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3999 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4000 "to enable more built-in suffixes");
4001 return error_mark_node;
4004 /* Parse a user-defined string constant. Returns a call to a user-defined
4005 literal operator taking a character pointer and the length of the string
4006 as arguments. */
4008 static tree
4009 cp_parser_userdef_string_literal (tree literal)
4011 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4012 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4013 tree value = USERDEF_LITERAL_VALUE (literal);
4014 int len = TREE_STRING_LENGTH (value)
4015 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4016 tree decl, result;
4017 vec<tree, va_gc> *args;
4019 /* Look for a template function with typename parameter CharT
4020 and parameter pack CharT... Call the function with
4021 template parameter characters representing the string. */
4022 args = make_tree_vector ();
4023 decl = lookup_literal_operator (name, args);
4024 if (decl && decl != error_mark_node)
4026 tree tmpl_args = make_string_pack (value);
4027 decl = lookup_template_function (decl, tmpl_args);
4028 result = finish_call_expr (decl, &args, false, true, tf_none);
4029 if (result != error_mark_node)
4031 release_tree_vector (args);
4032 return result;
4035 release_tree_vector (args);
4037 /* Build up a call to the user-defined operator */
4038 /* Lookup the name we got back from the id-expression. */
4039 args = make_tree_vector ();
4040 vec_safe_push (args, value);
4041 vec_safe_push (args, build_int_cst (size_type_node, len));
4042 decl = lookup_name (name);
4043 if (!decl || decl == error_mark_node)
4045 error ("unable to find string literal operator %qD", name);
4046 release_tree_vector (args);
4047 return error_mark_node;
4049 result = finish_call_expr (decl, &args, false, true, tf_none);
4050 release_tree_vector (args);
4051 if (result != error_mark_node)
4052 return result;
4054 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4055 name, TREE_TYPE (value), size_type_node);
4056 return error_mark_node;
4060 /* Basic concepts [gram.basic] */
4062 /* Parse a translation-unit.
4064 translation-unit:
4065 declaration-seq [opt]
4067 Returns TRUE if all went well. */
4069 static bool
4070 cp_parser_translation_unit (cp_parser* parser)
4072 /* The address of the first non-permanent object on the declarator
4073 obstack. */
4074 static void *declarator_obstack_base;
4076 bool success;
4078 /* Create the declarator obstack, if necessary. */
4079 if (!cp_error_declarator)
4081 gcc_obstack_init (&declarator_obstack);
4082 /* Create the error declarator. */
4083 cp_error_declarator = make_declarator (cdk_error);
4084 /* Create the empty parameter list. */
4085 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4086 /* Remember where the base of the declarator obstack lies. */
4087 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4090 cp_parser_declaration_seq_opt (parser);
4092 /* If there are no tokens left then all went well. */
4093 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4095 /* Get rid of the token array; we don't need it any more. */
4096 cp_lexer_destroy (parser->lexer);
4097 parser->lexer = NULL;
4099 /* This file might have been a context that's implicitly extern
4100 "C". If so, pop the lang context. (Only relevant for PCH.) */
4101 if (parser->implicit_extern_c)
4103 pop_lang_context ();
4104 parser->implicit_extern_c = false;
4107 /* Finish up. */
4108 finish_translation_unit ();
4110 success = true;
4112 else
4114 cp_parser_error (parser, "expected declaration");
4115 success = false;
4118 /* Make sure the declarator obstack was fully cleaned up. */
4119 gcc_assert (obstack_next_free (&declarator_obstack)
4120 == declarator_obstack_base);
4122 /* All went well. */
4123 return success;
4126 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4127 decltype context. */
4129 static inline tsubst_flags_t
4130 complain_flags (bool decltype_p)
4132 tsubst_flags_t complain = tf_warning_or_error;
4133 if (decltype_p)
4134 complain |= tf_decltype;
4135 return complain;
4138 /* We're about to parse a collection of statements. If we're currently
4139 parsing tentatively, set up a firewall so that any nested
4140 cp_parser_commit_to_tentative_parse won't affect the current context. */
4142 static cp_token_position
4143 cp_parser_start_tentative_firewall (cp_parser *parser)
4145 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4146 return 0;
4148 cp_parser_parse_tentatively (parser);
4149 cp_parser_commit_to_topmost_tentative_parse (parser);
4150 return cp_lexer_token_position (parser->lexer, false);
4153 /* We've finished parsing the collection of statements. Wrap up the
4154 firewall and replace the relevant tokens with the parsed form. */
4156 static void
4157 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4158 tree expr)
4160 if (!start)
4161 return;
4163 /* Finish the firewall level. */
4164 cp_parser_parse_definitely (parser);
4165 /* And remember the result of the parse for when we try again. */
4166 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4167 token->type = CPP_PREPARSED_EXPR;
4168 token->u.value = expr;
4169 token->keyword = RID_MAX;
4170 cp_lexer_purge_tokens_after (parser->lexer, start);
4173 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4174 enclosing parentheses. */
4176 static tree
4177 cp_parser_statement_expr (cp_parser *parser)
4179 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4181 /* Consume the '('. */
4182 cp_lexer_consume_token (parser->lexer);
4183 /* Start the statement-expression. */
4184 tree expr = begin_stmt_expr ();
4185 /* Parse the compound-statement. */
4186 cp_parser_compound_statement (parser, expr, false, false);
4187 /* Finish up. */
4188 expr = finish_stmt_expr (expr, false);
4189 /* Consume the ')'. */
4190 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4191 cp_parser_skip_to_end_of_statement (parser);
4193 cp_parser_end_tentative_firewall (parser, start, expr);
4194 return expr;
4197 /* Expressions [gram.expr] */
4199 /* Parse a primary-expression.
4201 primary-expression:
4202 literal
4203 this
4204 ( expression )
4205 id-expression
4206 lambda-expression (C++11)
4208 GNU Extensions:
4210 primary-expression:
4211 ( compound-statement )
4212 __builtin_va_arg ( assignment-expression , type-id )
4213 __builtin_offsetof ( type-id , offsetof-expression )
4215 C++ Extensions:
4216 __has_nothrow_assign ( type-id )
4217 __has_nothrow_constructor ( type-id )
4218 __has_nothrow_copy ( type-id )
4219 __has_trivial_assign ( type-id )
4220 __has_trivial_constructor ( type-id )
4221 __has_trivial_copy ( type-id )
4222 __has_trivial_destructor ( type-id )
4223 __has_virtual_destructor ( type-id )
4224 __is_abstract ( type-id )
4225 __is_base_of ( type-id , type-id )
4226 __is_class ( type-id )
4227 __is_empty ( type-id )
4228 __is_enum ( type-id )
4229 __is_final ( type-id )
4230 __is_literal_type ( type-id )
4231 __is_pod ( type-id )
4232 __is_polymorphic ( type-id )
4233 __is_std_layout ( type-id )
4234 __is_trivial ( type-id )
4235 __is_union ( type-id )
4237 Objective-C++ Extension:
4239 primary-expression:
4240 objc-expression
4242 literal:
4243 __null
4245 ADDRESS_P is true iff this expression was immediately preceded by
4246 "&" and therefore might denote a pointer-to-member. CAST_P is true
4247 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4248 true iff this expression is a template argument.
4250 Returns a representation of the expression. Upon return, *IDK
4251 indicates what kind of id-expression (if any) was present. */
4253 static tree
4254 cp_parser_primary_expression (cp_parser *parser,
4255 bool address_p,
4256 bool cast_p,
4257 bool template_arg_p,
4258 bool decltype_p,
4259 cp_id_kind *idk)
4261 cp_token *token = NULL;
4263 /* Assume the primary expression is not an id-expression. */
4264 *idk = CP_ID_KIND_NONE;
4266 /* Peek at the next token. */
4267 token = cp_lexer_peek_token (parser->lexer);
4268 switch ((int) token->type)
4270 /* literal:
4271 integer-literal
4272 character-literal
4273 floating-literal
4274 string-literal
4275 boolean-literal
4276 pointer-literal
4277 user-defined-literal */
4278 case CPP_CHAR:
4279 case CPP_CHAR16:
4280 case CPP_CHAR32:
4281 case CPP_WCHAR:
4282 case CPP_NUMBER:
4283 case CPP_PREPARSED_EXPR:
4284 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4285 return cp_parser_userdef_numeric_literal (parser);
4286 token = cp_lexer_consume_token (parser->lexer);
4287 if (TREE_CODE (token->u.value) == FIXED_CST)
4289 error_at (token->location,
4290 "fixed-point types not supported in C++");
4291 return error_mark_node;
4293 /* Floating-point literals are only allowed in an integral
4294 constant expression if they are cast to an integral or
4295 enumeration type. */
4296 if (TREE_CODE (token->u.value) == REAL_CST
4297 && parser->integral_constant_expression_p
4298 && pedantic)
4300 /* CAST_P will be set even in invalid code like "int(2.7 +
4301 ...)". Therefore, we have to check that the next token
4302 is sure to end the cast. */
4303 if (cast_p)
4305 cp_token *next_token;
4307 next_token = cp_lexer_peek_token (parser->lexer);
4308 if (/* The comma at the end of an
4309 enumerator-definition. */
4310 next_token->type != CPP_COMMA
4311 /* The curly brace at the end of an enum-specifier. */
4312 && next_token->type != CPP_CLOSE_BRACE
4313 /* The end of a statement. */
4314 && next_token->type != CPP_SEMICOLON
4315 /* The end of the cast-expression. */
4316 && next_token->type != CPP_CLOSE_PAREN
4317 /* The end of an array bound. */
4318 && next_token->type != CPP_CLOSE_SQUARE
4319 /* The closing ">" in a template-argument-list. */
4320 && (next_token->type != CPP_GREATER
4321 || parser->greater_than_is_operator_p)
4322 /* C++0x only: A ">>" treated like two ">" tokens,
4323 in a template-argument-list. */
4324 && (next_token->type != CPP_RSHIFT
4325 || (cxx_dialect == cxx98)
4326 || parser->greater_than_is_operator_p))
4327 cast_p = false;
4330 /* If we are within a cast, then the constraint that the
4331 cast is to an integral or enumeration type will be
4332 checked at that point. If we are not within a cast, then
4333 this code is invalid. */
4334 if (!cast_p)
4335 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4337 return token->u.value;
4339 case CPP_CHAR_USERDEF:
4340 case CPP_CHAR16_USERDEF:
4341 case CPP_CHAR32_USERDEF:
4342 case CPP_WCHAR_USERDEF:
4343 return cp_parser_userdef_char_literal (parser);
4345 case CPP_STRING:
4346 case CPP_STRING16:
4347 case CPP_STRING32:
4348 case CPP_WSTRING:
4349 case CPP_UTF8STRING:
4350 case CPP_STRING_USERDEF:
4351 case CPP_STRING16_USERDEF:
4352 case CPP_STRING32_USERDEF:
4353 case CPP_WSTRING_USERDEF:
4354 case CPP_UTF8STRING_USERDEF:
4355 /* ??? Should wide strings be allowed when parser->translate_strings_p
4356 is false (i.e. in attributes)? If not, we can kill the third
4357 argument to cp_parser_string_literal. */
4358 return cp_parser_string_literal (parser,
4359 parser->translate_strings_p,
4360 true);
4362 case CPP_OPEN_PAREN:
4363 /* If we see `( { ' then we are looking at the beginning of
4364 a GNU statement-expression. */
4365 if (cp_parser_allow_gnu_extensions_p (parser)
4366 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4368 /* Statement-expressions are not allowed by the standard. */
4369 pedwarn (token->location, OPT_Wpedantic,
4370 "ISO C++ forbids braced-groups within expressions");
4372 /* And they're not allowed outside of a function-body; you
4373 cannot, for example, write:
4375 int i = ({ int j = 3; j + 1; });
4377 at class or namespace scope. */
4378 if (!parser->in_function_body
4379 || parser->in_template_argument_list_p)
4381 error_at (token->location,
4382 "statement-expressions are not allowed outside "
4383 "functions nor in template-argument lists");
4384 cp_parser_skip_to_end_of_block_or_statement (parser);
4385 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4386 cp_lexer_consume_token (parser->lexer);
4387 return error_mark_node;
4389 else
4390 return cp_parser_statement_expr (parser);
4392 /* Otherwise it's a normal parenthesized expression. */
4394 tree expr;
4395 bool saved_greater_than_is_operator_p;
4397 /* Consume the `('. */
4398 cp_lexer_consume_token (parser->lexer);
4399 /* Within a parenthesized expression, a `>' token is always
4400 the greater-than operator. */
4401 saved_greater_than_is_operator_p
4402 = parser->greater_than_is_operator_p;
4403 parser->greater_than_is_operator_p = true;
4405 /* Parse the parenthesized expression. */
4406 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4407 /* Let the front end know that this expression was
4408 enclosed in parentheses. This matters in case, for
4409 example, the expression is of the form `A::B', since
4410 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4411 not. */
4412 expr = finish_parenthesized_expr (expr);
4413 /* DR 705: Wrapping an unqualified name in parentheses
4414 suppresses arg-dependent lookup. We want to pass back
4415 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4416 (c++/37862), but none of the others. */
4417 if (*idk != CP_ID_KIND_QUALIFIED)
4418 *idk = CP_ID_KIND_NONE;
4420 /* The `>' token might be the end of a template-id or
4421 template-parameter-list now. */
4422 parser->greater_than_is_operator_p
4423 = saved_greater_than_is_operator_p;
4424 /* Consume the `)'. */
4425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4426 cp_parser_skip_to_end_of_statement (parser);
4428 return expr;
4431 case CPP_OPEN_SQUARE:
4432 if (c_dialect_objc ())
4433 /* We have an Objective-C++ message. */
4434 return cp_parser_objc_expression (parser);
4436 tree lam = cp_parser_lambda_expression (parser);
4437 /* Don't warn about a failed tentative parse. */
4438 if (cp_parser_error_occurred (parser))
4439 return error_mark_node;
4440 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4441 return lam;
4444 case CPP_OBJC_STRING:
4445 if (c_dialect_objc ())
4446 /* We have an Objective-C++ string literal. */
4447 return cp_parser_objc_expression (parser);
4448 cp_parser_error (parser, "expected primary-expression");
4449 return error_mark_node;
4451 case CPP_KEYWORD:
4452 switch (token->keyword)
4454 /* These two are the boolean literals. */
4455 case RID_TRUE:
4456 cp_lexer_consume_token (parser->lexer);
4457 return boolean_true_node;
4458 case RID_FALSE:
4459 cp_lexer_consume_token (parser->lexer);
4460 return boolean_false_node;
4462 /* The `__null' literal. */
4463 case RID_NULL:
4464 cp_lexer_consume_token (parser->lexer);
4465 return null_node;
4467 /* The `nullptr' literal. */
4468 case RID_NULLPTR:
4469 cp_lexer_consume_token (parser->lexer);
4470 return nullptr_node;
4472 /* Recognize the `this' keyword. */
4473 case RID_THIS:
4474 cp_lexer_consume_token (parser->lexer);
4475 if (parser->local_variables_forbidden_p)
4477 error_at (token->location,
4478 "%<this%> may not be used in this context");
4479 return error_mark_node;
4481 /* Pointers cannot appear in constant-expressions. */
4482 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4483 return error_mark_node;
4484 return finish_this_expr ();
4486 /* The `operator' keyword can be the beginning of an
4487 id-expression. */
4488 case RID_OPERATOR:
4489 goto id_expression;
4491 case RID_FUNCTION_NAME:
4492 case RID_PRETTY_FUNCTION_NAME:
4493 case RID_C99_FUNCTION_NAME:
4495 non_integral_constant name;
4497 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4498 __func__ are the names of variables -- but they are
4499 treated specially. Therefore, they are handled here,
4500 rather than relying on the generic id-expression logic
4501 below. Grammatically, these names are id-expressions.
4503 Consume the token. */
4504 token = cp_lexer_consume_token (parser->lexer);
4506 switch (token->keyword)
4508 case RID_FUNCTION_NAME:
4509 name = NIC_FUNC_NAME;
4510 break;
4511 case RID_PRETTY_FUNCTION_NAME:
4512 name = NIC_PRETTY_FUNC;
4513 break;
4514 case RID_C99_FUNCTION_NAME:
4515 name = NIC_C99_FUNC;
4516 break;
4517 default:
4518 gcc_unreachable ();
4521 if (cp_parser_non_integral_constant_expression (parser, name))
4522 return error_mark_node;
4524 /* Look up the name. */
4525 return finish_fname (token->u.value);
4528 case RID_VA_ARG:
4530 tree expression;
4531 tree type;
4532 source_location type_location;
4534 /* The `__builtin_va_arg' construct is used to handle
4535 `va_arg'. Consume the `__builtin_va_arg' token. */
4536 cp_lexer_consume_token (parser->lexer);
4537 /* Look for the opening `('. */
4538 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4539 /* Now, parse the assignment-expression. */
4540 expression = cp_parser_assignment_expression (parser);
4541 /* Look for the `,'. */
4542 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4543 type_location = cp_lexer_peek_token (parser->lexer)->location;
4544 /* Parse the type-id. */
4545 type = cp_parser_type_id (parser);
4546 /* Look for the closing `)'. */
4547 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4548 /* Using `va_arg' in a constant-expression is not
4549 allowed. */
4550 if (cp_parser_non_integral_constant_expression (parser,
4551 NIC_VA_ARG))
4552 return error_mark_node;
4553 return build_x_va_arg (type_location, expression, type);
4556 case RID_OFFSETOF:
4557 return cp_parser_builtin_offsetof (parser);
4559 case RID_HAS_NOTHROW_ASSIGN:
4560 case RID_HAS_NOTHROW_CONSTRUCTOR:
4561 case RID_HAS_NOTHROW_COPY:
4562 case RID_HAS_TRIVIAL_ASSIGN:
4563 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4564 case RID_HAS_TRIVIAL_COPY:
4565 case RID_HAS_TRIVIAL_DESTRUCTOR:
4566 case RID_HAS_VIRTUAL_DESTRUCTOR:
4567 case RID_IS_ABSTRACT:
4568 case RID_IS_BASE_OF:
4569 case RID_IS_CLASS:
4570 case RID_IS_EMPTY:
4571 case RID_IS_ENUM:
4572 case RID_IS_FINAL:
4573 case RID_IS_LITERAL_TYPE:
4574 case RID_IS_POD:
4575 case RID_IS_POLYMORPHIC:
4576 case RID_IS_STD_LAYOUT:
4577 case RID_IS_TRIVIAL:
4578 case RID_IS_TRIVIALLY_ASSIGNABLE:
4579 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4580 case RID_IS_TRIVIALLY_COPYABLE:
4581 case RID_IS_UNION:
4582 return cp_parser_trait_expr (parser, token->keyword);
4584 /* Objective-C++ expressions. */
4585 case RID_AT_ENCODE:
4586 case RID_AT_PROTOCOL:
4587 case RID_AT_SELECTOR:
4588 return cp_parser_objc_expression (parser);
4590 case RID_TEMPLATE:
4591 if (parser->in_function_body
4592 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4593 == CPP_LESS))
4595 error_at (token->location,
4596 "a template declaration cannot appear at block scope");
4597 cp_parser_skip_to_end_of_block_or_statement (parser);
4598 return error_mark_node;
4600 default:
4601 cp_parser_error (parser, "expected primary-expression");
4602 return error_mark_node;
4605 /* An id-expression can start with either an identifier, a
4606 `::' as the beginning of a qualified-id, or the "operator"
4607 keyword. */
4608 case CPP_NAME:
4609 case CPP_SCOPE:
4610 case CPP_TEMPLATE_ID:
4611 case CPP_NESTED_NAME_SPECIFIER:
4613 tree id_expression;
4614 tree decl;
4615 const char *error_msg;
4616 bool template_p;
4617 bool done;
4618 cp_token *id_expr_token;
4620 id_expression:
4621 /* Parse the id-expression. */
4622 id_expression
4623 = cp_parser_id_expression (parser,
4624 /*template_keyword_p=*/false,
4625 /*check_dependency_p=*/true,
4626 &template_p,
4627 /*declarator_p=*/false,
4628 /*optional_p=*/false);
4629 if (id_expression == error_mark_node)
4630 return error_mark_node;
4631 id_expr_token = token;
4632 token = cp_lexer_peek_token (parser->lexer);
4633 done = (token->type != CPP_OPEN_SQUARE
4634 && token->type != CPP_OPEN_PAREN
4635 && token->type != CPP_DOT
4636 && token->type != CPP_DEREF
4637 && token->type != CPP_PLUS_PLUS
4638 && token->type != CPP_MINUS_MINUS);
4639 /* If we have a template-id, then no further lookup is
4640 required. If the template-id was for a template-class, we
4641 will sometimes have a TYPE_DECL at this point. */
4642 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4643 || TREE_CODE (id_expression) == TYPE_DECL)
4644 decl = id_expression;
4645 /* Look up the name. */
4646 else
4648 tree ambiguous_decls;
4650 /* If we already know that this lookup is ambiguous, then
4651 we've already issued an error message; there's no reason
4652 to check again. */
4653 if (id_expr_token->type == CPP_NAME
4654 && id_expr_token->error_reported)
4656 cp_parser_simulate_error (parser);
4657 return error_mark_node;
4660 decl = cp_parser_lookup_name (parser, id_expression,
4661 none_type,
4662 template_p,
4663 /*is_namespace=*/false,
4664 /*check_dependency=*/true,
4665 &ambiguous_decls,
4666 id_expr_token->location);
4667 /* If the lookup was ambiguous, an error will already have
4668 been issued. */
4669 if (ambiguous_decls)
4670 return error_mark_node;
4672 /* In Objective-C++, we may have an Objective-C 2.0
4673 dot-syntax for classes here. */
4674 if (c_dialect_objc ()
4675 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4676 && TREE_CODE (decl) == TYPE_DECL
4677 && objc_is_class_name (decl))
4679 tree component;
4680 cp_lexer_consume_token (parser->lexer);
4681 component = cp_parser_identifier (parser);
4682 if (component == error_mark_node)
4683 return error_mark_node;
4685 return objc_build_class_component_ref (id_expression, component);
4688 /* In Objective-C++, an instance variable (ivar) may be preferred
4689 to whatever cp_parser_lookup_name() found. */
4690 decl = objc_lookup_ivar (decl, id_expression);
4692 /* If name lookup gives us a SCOPE_REF, then the
4693 qualifying scope was dependent. */
4694 if (TREE_CODE (decl) == SCOPE_REF)
4696 /* At this point, we do not know if DECL is a valid
4697 integral constant expression. We assume that it is
4698 in fact such an expression, so that code like:
4700 template <int N> struct A {
4701 int a[B<N>::i];
4704 is accepted. At template-instantiation time, we
4705 will check that B<N>::i is actually a constant. */
4706 return decl;
4708 /* Check to see if DECL is a local variable in a context
4709 where that is forbidden. */
4710 if (parser->local_variables_forbidden_p
4711 && local_variable_p (decl))
4713 /* It might be that we only found DECL because we are
4714 trying to be generous with pre-ISO scoping rules.
4715 For example, consider:
4717 int i;
4718 void g() {
4719 for (int i = 0; i < 10; ++i) {}
4720 extern void f(int j = i);
4723 Here, name look up will originally find the out
4724 of scope `i'. We need to issue a warning message,
4725 but then use the global `i'. */
4726 decl = check_for_out_of_scope_variable (decl);
4727 if (local_variable_p (decl))
4729 error_at (id_expr_token->location,
4730 "local variable %qD may not appear in this context",
4731 decl);
4732 return error_mark_node;
4737 decl = (finish_id_expression
4738 (id_expression, decl, parser->scope,
4739 idk,
4740 parser->integral_constant_expression_p,
4741 parser->allow_non_integral_constant_expression_p,
4742 &parser->non_integral_constant_expression_p,
4743 template_p, done, address_p,
4744 template_arg_p,
4745 &error_msg,
4746 id_expr_token->location));
4747 if (error_msg)
4748 cp_parser_error (parser, error_msg);
4749 return decl;
4752 /* Anything else is an error. */
4753 default:
4754 cp_parser_error (parser, "expected primary-expression");
4755 return error_mark_node;
4759 static inline tree
4760 cp_parser_primary_expression (cp_parser *parser,
4761 bool address_p,
4762 bool cast_p,
4763 bool template_arg_p,
4764 cp_id_kind *idk)
4766 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4767 /*decltype*/false, idk);
4770 /* Parse an id-expression.
4772 id-expression:
4773 unqualified-id
4774 qualified-id
4776 qualified-id:
4777 :: [opt] nested-name-specifier template [opt] unqualified-id
4778 :: identifier
4779 :: operator-function-id
4780 :: template-id
4782 Return a representation of the unqualified portion of the
4783 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4784 a `::' or nested-name-specifier.
4786 Often, if the id-expression was a qualified-id, the caller will
4787 want to make a SCOPE_REF to represent the qualified-id. This
4788 function does not do this in order to avoid wastefully creating
4789 SCOPE_REFs when they are not required.
4791 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4792 `template' keyword.
4794 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4795 uninstantiated templates.
4797 If *TEMPLATE_P is non-NULL, it is set to true iff the
4798 `template' keyword is used to explicitly indicate that the entity
4799 named is a template.
4801 If DECLARATOR_P is true, the id-expression is appearing as part of
4802 a declarator, rather than as part of an expression. */
4804 static tree
4805 cp_parser_id_expression (cp_parser *parser,
4806 bool template_keyword_p,
4807 bool check_dependency_p,
4808 bool *template_p,
4809 bool declarator_p,
4810 bool optional_p)
4812 bool global_scope_p;
4813 bool nested_name_specifier_p;
4815 /* Assume the `template' keyword was not used. */
4816 if (template_p)
4817 *template_p = template_keyword_p;
4819 /* Look for the optional `::' operator. */
4820 global_scope_p
4821 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4822 != NULL_TREE);
4823 /* Look for the optional nested-name-specifier. */
4824 nested_name_specifier_p
4825 = (cp_parser_nested_name_specifier_opt (parser,
4826 /*typename_keyword_p=*/false,
4827 check_dependency_p,
4828 /*type_p=*/false,
4829 declarator_p)
4830 != NULL_TREE);
4831 /* If there is a nested-name-specifier, then we are looking at
4832 the first qualified-id production. */
4833 if (nested_name_specifier_p)
4835 tree saved_scope;
4836 tree saved_object_scope;
4837 tree saved_qualifying_scope;
4838 tree unqualified_id;
4839 bool is_template;
4841 /* See if the next token is the `template' keyword. */
4842 if (!template_p)
4843 template_p = &is_template;
4844 *template_p = cp_parser_optional_template_keyword (parser);
4845 /* Name lookup we do during the processing of the
4846 unqualified-id might obliterate SCOPE. */
4847 saved_scope = parser->scope;
4848 saved_object_scope = parser->object_scope;
4849 saved_qualifying_scope = parser->qualifying_scope;
4850 /* Process the final unqualified-id. */
4851 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4852 check_dependency_p,
4853 declarator_p,
4854 /*optional_p=*/false);
4855 /* Restore the SAVED_SCOPE for our caller. */
4856 parser->scope = saved_scope;
4857 parser->object_scope = saved_object_scope;
4858 parser->qualifying_scope = saved_qualifying_scope;
4860 return unqualified_id;
4862 /* Otherwise, if we are in global scope, then we are looking at one
4863 of the other qualified-id productions. */
4864 else if (global_scope_p)
4866 cp_token *token;
4867 tree id;
4869 /* Peek at the next token. */
4870 token = cp_lexer_peek_token (parser->lexer);
4872 /* If it's an identifier, and the next token is not a "<", then
4873 we can avoid the template-id case. This is an optimization
4874 for this common case. */
4875 if (token->type == CPP_NAME
4876 && !cp_parser_nth_token_starts_template_argument_list_p
4877 (parser, 2))
4878 return cp_parser_identifier (parser);
4880 cp_parser_parse_tentatively (parser);
4881 /* Try a template-id. */
4882 id = cp_parser_template_id (parser,
4883 /*template_keyword_p=*/false,
4884 /*check_dependency_p=*/true,
4885 none_type,
4886 declarator_p);
4887 /* If that worked, we're done. */
4888 if (cp_parser_parse_definitely (parser))
4889 return id;
4891 /* Peek at the next token. (Changes in the token buffer may
4892 have invalidated the pointer obtained above.) */
4893 token = cp_lexer_peek_token (parser->lexer);
4895 switch (token->type)
4897 case CPP_NAME:
4898 return cp_parser_identifier (parser);
4900 case CPP_KEYWORD:
4901 if (token->keyword == RID_OPERATOR)
4902 return cp_parser_operator_function_id (parser);
4903 /* Fall through. */
4905 default:
4906 cp_parser_error (parser, "expected id-expression");
4907 return error_mark_node;
4910 else
4911 return cp_parser_unqualified_id (parser, template_keyword_p,
4912 /*check_dependency_p=*/true,
4913 declarator_p,
4914 optional_p);
4917 /* Parse an unqualified-id.
4919 unqualified-id:
4920 identifier
4921 operator-function-id
4922 conversion-function-id
4923 ~ class-name
4924 template-id
4926 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4927 keyword, in a construct like `A::template ...'.
4929 Returns a representation of unqualified-id. For the `identifier'
4930 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4931 production a BIT_NOT_EXPR is returned; the operand of the
4932 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4933 other productions, see the documentation accompanying the
4934 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4935 names are looked up in uninstantiated templates. If DECLARATOR_P
4936 is true, the unqualified-id is appearing as part of a declarator,
4937 rather than as part of an expression. */
4939 static tree
4940 cp_parser_unqualified_id (cp_parser* parser,
4941 bool template_keyword_p,
4942 bool check_dependency_p,
4943 bool declarator_p,
4944 bool optional_p)
4946 cp_token *token;
4948 /* Peek at the next token. */
4949 token = cp_lexer_peek_token (parser->lexer);
4951 switch ((int) token->type)
4953 case CPP_NAME:
4955 tree id;
4957 /* We don't know yet whether or not this will be a
4958 template-id. */
4959 cp_parser_parse_tentatively (parser);
4960 /* Try a template-id. */
4961 id = cp_parser_template_id (parser, template_keyword_p,
4962 check_dependency_p,
4963 none_type,
4964 declarator_p);
4965 /* If it worked, we're done. */
4966 if (cp_parser_parse_definitely (parser))
4967 return id;
4968 /* Otherwise, it's an ordinary identifier. */
4969 return cp_parser_identifier (parser);
4972 case CPP_TEMPLATE_ID:
4973 return cp_parser_template_id (parser, template_keyword_p,
4974 check_dependency_p,
4975 none_type,
4976 declarator_p);
4978 case CPP_COMPL:
4980 tree type_decl;
4981 tree qualifying_scope;
4982 tree object_scope;
4983 tree scope;
4984 bool done;
4986 /* Consume the `~' token. */
4987 cp_lexer_consume_token (parser->lexer);
4988 /* Parse the class-name. The standard, as written, seems to
4989 say that:
4991 template <typename T> struct S { ~S (); };
4992 template <typename T> S<T>::~S() {}
4994 is invalid, since `~' must be followed by a class-name, but
4995 `S<T>' is dependent, and so not known to be a class.
4996 That's not right; we need to look in uninstantiated
4997 templates. A further complication arises from:
4999 template <typename T> void f(T t) {
5000 t.T::~T();
5003 Here, it is not possible to look up `T' in the scope of `T'
5004 itself. We must look in both the current scope, and the
5005 scope of the containing complete expression.
5007 Yet another issue is:
5009 struct S {
5010 int S;
5011 ~S();
5014 S::~S() {}
5016 The standard does not seem to say that the `S' in `~S'
5017 should refer to the type `S' and not the data member
5018 `S::S'. */
5020 /* DR 244 says that we look up the name after the "~" in the
5021 same scope as we looked up the qualifying name. That idea
5022 isn't fully worked out; it's more complicated than that. */
5023 scope = parser->scope;
5024 object_scope = parser->object_scope;
5025 qualifying_scope = parser->qualifying_scope;
5027 /* Check for invalid scopes. */
5028 if (scope == error_mark_node)
5030 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5031 cp_lexer_consume_token (parser->lexer);
5032 return error_mark_node;
5034 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5036 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5037 error_at (token->location,
5038 "scope %qT before %<~%> is not a class-name",
5039 scope);
5040 cp_parser_simulate_error (parser);
5041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5042 cp_lexer_consume_token (parser->lexer);
5043 return error_mark_node;
5045 gcc_assert (!scope || TYPE_P (scope));
5047 /* If the name is of the form "X::~X" it's OK even if X is a
5048 typedef. */
5049 token = cp_lexer_peek_token (parser->lexer);
5050 if (scope
5051 && token->type == CPP_NAME
5052 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5053 != CPP_LESS)
5054 && (token->u.value == TYPE_IDENTIFIER (scope)
5055 || (CLASS_TYPE_P (scope)
5056 && constructor_name_p (token->u.value, scope))))
5058 cp_lexer_consume_token (parser->lexer);
5059 return build_nt (BIT_NOT_EXPR, scope);
5062 /* ~auto means the destructor of whatever the object is. */
5063 if (cp_parser_is_keyword (token, RID_AUTO))
5065 if (cxx_dialect < cxx14)
5066 pedwarn (input_location, 0,
5067 "%<~auto%> only available with "
5068 "-std=c++14 or -std=gnu++14");
5069 cp_lexer_consume_token (parser->lexer);
5070 return build_nt (BIT_NOT_EXPR, make_auto ());
5073 /* If there was an explicit qualification (S::~T), first look
5074 in the scope given by the qualification (i.e., S).
5076 Note: in the calls to cp_parser_class_name below we pass
5077 typename_type so that lookup finds the injected-class-name
5078 rather than the constructor. */
5079 done = false;
5080 type_decl = NULL_TREE;
5081 if (scope)
5083 cp_parser_parse_tentatively (parser);
5084 type_decl = cp_parser_class_name (parser,
5085 /*typename_keyword_p=*/false,
5086 /*template_keyword_p=*/false,
5087 typename_type,
5088 /*check_dependency=*/false,
5089 /*class_head_p=*/false,
5090 declarator_p);
5091 if (cp_parser_parse_definitely (parser))
5092 done = true;
5094 /* In "N::S::~S", look in "N" as well. */
5095 if (!done && scope && qualifying_scope)
5097 cp_parser_parse_tentatively (parser);
5098 parser->scope = qualifying_scope;
5099 parser->object_scope = NULL_TREE;
5100 parser->qualifying_scope = NULL_TREE;
5101 type_decl
5102 = cp_parser_class_name (parser,
5103 /*typename_keyword_p=*/false,
5104 /*template_keyword_p=*/false,
5105 typename_type,
5106 /*check_dependency=*/false,
5107 /*class_head_p=*/false,
5108 declarator_p);
5109 if (cp_parser_parse_definitely (parser))
5110 done = true;
5112 /* In "p->S::~T", look in the scope given by "*p" as well. */
5113 else if (!done && object_scope)
5115 cp_parser_parse_tentatively (parser);
5116 parser->scope = object_scope;
5117 parser->object_scope = NULL_TREE;
5118 parser->qualifying_scope = NULL_TREE;
5119 type_decl
5120 = cp_parser_class_name (parser,
5121 /*typename_keyword_p=*/false,
5122 /*template_keyword_p=*/false,
5123 typename_type,
5124 /*check_dependency=*/false,
5125 /*class_head_p=*/false,
5126 declarator_p);
5127 if (cp_parser_parse_definitely (parser))
5128 done = true;
5130 /* Look in the surrounding context. */
5131 if (!done)
5133 parser->scope = NULL_TREE;
5134 parser->object_scope = NULL_TREE;
5135 parser->qualifying_scope = NULL_TREE;
5136 if (processing_template_decl)
5137 cp_parser_parse_tentatively (parser);
5138 type_decl
5139 = cp_parser_class_name (parser,
5140 /*typename_keyword_p=*/false,
5141 /*template_keyword_p=*/false,
5142 typename_type,
5143 /*check_dependency=*/false,
5144 /*class_head_p=*/false,
5145 declarator_p);
5146 if (processing_template_decl
5147 && ! cp_parser_parse_definitely (parser))
5149 /* We couldn't find a type with this name, so just accept
5150 it and check for a match at instantiation time. */
5151 type_decl = cp_parser_identifier (parser);
5152 if (type_decl != error_mark_node)
5153 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5154 return type_decl;
5157 /* If an error occurred, assume that the name of the
5158 destructor is the same as the name of the qualifying
5159 class. That allows us to keep parsing after running
5160 into ill-formed destructor names. */
5161 if (type_decl == error_mark_node && scope)
5162 return build_nt (BIT_NOT_EXPR, scope);
5163 else if (type_decl == error_mark_node)
5164 return error_mark_node;
5166 /* Check that destructor name and scope match. */
5167 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5169 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5170 error_at (token->location,
5171 "declaration of %<~%T%> as member of %qT",
5172 type_decl, scope);
5173 cp_parser_simulate_error (parser);
5174 return error_mark_node;
5177 /* [class.dtor]
5179 A typedef-name that names a class shall not be used as the
5180 identifier in the declarator for a destructor declaration. */
5181 if (declarator_p
5182 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5183 && !DECL_SELF_REFERENCE_P (type_decl)
5184 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5185 error_at (token->location,
5186 "typedef-name %qD used as destructor declarator",
5187 type_decl);
5189 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5192 case CPP_KEYWORD:
5193 if (token->keyword == RID_OPERATOR)
5195 tree id;
5197 /* This could be a template-id, so we try that first. */
5198 cp_parser_parse_tentatively (parser);
5199 /* Try a template-id. */
5200 id = cp_parser_template_id (parser, template_keyword_p,
5201 /*check_dependency_p=*/true,
5202 none_type,
5203 declarator_p);
5204 /* If that worked, we're done. */
5205 if (cp_parser_parse_definitely (parser))
5206 return id;
5207 /* We still don't know whether we're looking at an
5208 operator-function-id or a conversion-function-id. */
5209 cp_parser_parse_tentatively (parser);
5210 /* Try an operator-function-id. */
5211 id = cp_parser_operator_function_id (parser);
5212 /* If that didn't work, try a conversion-function-id. */
5213 if (!cp_parser_parse_definitely (parser))
5214 id = cp_parser_conversion_function_id (parser);
5215 else if (UDLIT_OPER_P (id))
5217 /* 17.6.3.3.5 */
5218 const char *name = UDLIT_OP_SUFFIX (id);
5219 if (name[0] != '_' && !in_system_header_at (input_location)
5220 && declarator_p)
5221 warning (0, "literal operator suffixes not preceded by %<_%>"
5222 " are reserved for future standardization");
5225 return id;
5227 /* Fall through. */
5229 default:
5230 if (optional_p)
5231 return NULL_TREE;
5232 cp_parser_error (parser, "expected unqualified-id");
5233 return error_mark_node;
5237 /* Parse an (optional) nested-name-specifier.
5239 nested-name-specifier: [C++98]
5240 class-or-namespace-name :: nested-name-specifier [opt]
5241 class-or-namespace-name :: template nested-name-specifier [opt]
5243 nested-name-specifier: [C++0x]
5244 type-name ::
5245 namespace-name ::
5246 nested-name-specifier identifier ::
5247 nested-name-specifier template [opt] simple-template-id ::
5249 PARSER->SCOPE should be set appropriately before this function is
5250 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5251 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5252 in name lookups.
5254 Sets PARSER->SCOPE to the class (TYPE) or namespace
5255 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5256 it unchanged if there is no nested-name-specifier. Returns the new
5257 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5259 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5260 part of a declaration and/or decl-specifier. */
5262 static tree
5263 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5264 bool typename_keyword_p,
5265 bool check_dependency_p,
5266 bool type_p,
5267 bool is_declaration)
5269 bool success = false;
5270 cp_token_position start = 0;
5271 cp_token *token;
5273 /* Remember where the nested-name-specifier starts. */
5274 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5276 start = cp_lexer_token_position (parser->lexer, false);
5277 push_deferring_access_checks (dk_deferred);
5280 while (true)
5282 tree new_scope;
5283 tree old_scope;
5284 tree saved_qualifying_scope;
5285 bool template_keyword_p;
5287 /* Spot cases that cannot be the beginning of a
5288 nested-name-specifier. */
5289 token = cp_lexer_peek_token (parser->lexer);
5291 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5292 the already parsed nested-name-specifier. */
5293 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5295 /* Grab the nested-name-specifier and continue the loop. */
5296 cp_parser_pre_parsed_nested_name_specifier (parser);
5297 /* If we originally encountered this nested-name-specifier
5298 with IS_DECLARATION set to false, we will not have
5299 resolved TYPENAME_TYPEs, so we must do so here. */
5300 if (is_declaration
5301 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5303 new_scope = resolve_typename_type (parser->scope,
5304 /*only_current_p=*/false);
5305 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5306 parser->scope = new_scope;
5308 success = true;
5309 continue;
5312 /* Spot cases that cannot be the beginning of a
5313 nested-name-specifier. On the second and subsequent times
5314 through the loop, we look for the `template' keyword. */
5315 if (success && token->keyword == RID_TEMPLATE)
5317 /* A template-id can start a nested-name-specifier. */
5318 else if (token->type == CPP_TEMPLATE_ID)
5320 /* DR 743: decltype can be used in a nested-name-specifier. */
5321 else if (token_is_decltype (token))
5323 else
5325 /* If the next token is not an identifier, then it is
5326 definitely not a type-name or namespace-name. */
5327 if (token->type != CPP_NAME)
5328 break;
5329 /* If the following token is neither a `<' (to begin a
5330 template-id), nor a `::', then we are not looking at a
5331 nested-name-specifier. */
5332 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5334 if (token->type == CPP_COLON
5335 && parser->colon_corrects_to_scope_p
5336 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5338 error_at (token->location,
5339 "found %<:%> in nested-name-specifier, expected %<::%>");
5340 token->type = CPP_SCOPE;
5343 if (token->type != CPP_SCOPE
5344 && !cp_parser_nth_token_starts_template_argument_list_p
5345 (parser, 2))
5346 break;
5349 /* The nested-name-specifier is optional, so we parse
5350 tentatively. */
5351 cp_parser_parse_tentatively (parser);
5353 /* Look for the optional `template' keyword, if this isn't the
5354 first time through the loop. */
5355 if (success)
5356 template_keyword_p = cp_parser_optional_template_keyword (parser);
5357 else
5358 template_keyword_p = false;
5360 /* Save the old scope since the name lookup we are about to do
5361 might destroy it. */
5362 old_scope = parser->scope;
5363 saved_qualifying_scope = parser->qualifying_scope;
5364 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5365 look up names in "X<T>::I" in order to determine that "Y" is
5366 a template. So, if we have a typename at this point, we make
5367 an effort to look through it. */
5368 if (is_declaration
5369 && !typename_keyword_p
5370 && parser->scope
5371 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5372 parser->scope = resolve_typename_type (parser->scope,
5373 /*only_current_p=*/false);
5374 /* Parse the qualifying entity. */
5375 new_scope
5376 = cp_parser_qualifying_entity (parser,
5377 typename_keyword_p,
5378 template_keyword_p,
5379 check_dependency_p,
5380 type_p,
5381 is_declaration);
5382 /* Look for the `::' token. */
5383 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5385 /* If we found what we wanted, we keep going; otherwise, we're
5386 done. */
5387 if (!cp_parser_parse_definitely (parser))
5389 bool error_p = false;
5391 /* Restore the OLD_SCOPE since it was valid before the
5392 failed attempt at finding the last
5393 class-or-namespace-name. */
5394 parser->scope = old_scope;
5395 parser->qualifying_scope = saved_qualifying_scope;
5397 /* If the next token is a decltype, and the one after that is a
5398 `::', then the decltype has failed to resolve to a class or
5399 enumeration type. Give this error even when parsing
5400 tentatively since it can't possibly be valid--and we're going
5401 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5402 won't get another chance.*/
5403 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5404 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5405 == CPP_SCOPE))
5407 token = cp_lexer_consume_token (parser->lexer);
5408 error_at (token->location, "decltype evaluates to %qT, "
5409 "which is not a class or enumeration type",
5410 token->u.value);
5411 parser->scope = error_mark_node;
5412 error_p = true;
5413 /* As below. */
5414 success = true;
5415 cp_lexer_consume_token (parser->lexer);
5418 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5419 break;
5420 /* If the next token is an identifier, and the one after
5421 that is a `::', then any valid interpretation would have
5422 found a class-or-namespace-name. */
5423 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5425 == CPP_SCOPE)
5426 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5427 != CPP_COMPL))
5429 token = cp_lexer_consume_token (parser->lexer);
5430 if (!error_p)
5432 if (!token->error_reported)
5434 tree decl;
5435 tree ambiguous_decls;
5437 decl = cp_parser_lookup_name (parser, token->u.value,
5438 none_type,
5439 /*is_template=*/false,
5440 /*is_namespace=*/false,
5441 /*check_dependency=*/true,
5442 &ambiguous_decls,
5443 token->location);
5444 if (TREE_CODE (decl) == TEMPLATE_DECL)
5445 error_at (token->location,
5446 "%qD used without template parameters",
5447 decl);
5448 else if (ambiguous_decls)
5450 // cp_parser_lookup_name has the same diagnostic,
5451 // thus make sure to emit it at most once.
5452 if (cp_parser_uncommitted_to_tentative_parse_p
5453 (parser))
5455 error_at (token->location,
5456 "reference to %qD is ambiguous",
5457 token->u.value);
5458 print_candidates (ambiguous_decls);
5460 decl = error_mark_node;
5462 else
5464 if (cxx_dialect != cxx98)
5465 cp_parser_name_lookup_error
5466 (parser, token->u.value, decl, NLE_NOT_CXX98,
5467 token->location);
5468 else
5469 cp_parser_name_lookup_error
5470 (parser, token->u.value, decl, NLE_CXX98,
5471 token->location);
5474 parser->scope = error_mark_node;
5475 error_p = true;
5476 /* Treat this as a successful nested-name-specifier
5477 due to:
5479 [basic.lookup.qual]
5481 If the name found is not a class-name (clause
5482 _class_) or namespace-name (_namespace.def_), the
5483 program is ill-formed. */
5484 success = true;
5486 cp_lexer_consume_token (parser->lexer);
5488 break;
5490 /* We've found one valid nested-name-specifier. */
5491 success = true;
5492 /* Name lookup always gives us a DECL. */
5493 if (TREE_CODE (new_scope) == TYPE_DECL)
5494 new_scope = TREE_TYPE (new_scope);
5495 /* Uses of "template" must be followed by actual templates. */
5496 if (template_keyword_p
5497 && !(CLASS_TYPE_P (new_scope)
5498 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5499 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5500 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5501 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5502 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5503 == TEMPLATE_ID_EXPR)))
5504 permerror (input_location, TYPE_P (new_scope)
5505 ? G_("%qT is not a template")
5506 : G_("%qD is not a template"),
5507 new_scope);
5508 /* If it is a class scope, try to complete it; we are about to
5509 be looking up names inside the class. */
5510 if (TYPE_P (new_scope)
5511 /* Since checking types for dependency can be expensive,
5512 avoid doing it if the type is already complete. */
5513 && !COMPLETE_TYPE_P (new_scope)
5514 /* Do not try to complete dependent types. */
5515 && !dependent_type_p (new_scope))
5517 new_scope = complete_type (new_scope);
5518 /* If it is a typedef to current class, use the current
5519 class instead, as the typedef won't have any names inside
5520 it yet. */
5521 if (!COMPLETE_TYPE_P (new_scope)
5522 && currently_open_class (new_scope))
5523 new_scope = TYPE_MAIN_VARIANT (new_scope);
5525 /* Make sure we look in the right scope the next time through
5526 the loop. */
5527 parser->scope = new_scope;
5530 /* If parsing tentatively, replace the sequence of tokens that makes
5531 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5532 token. That way, should we re-parse the token stream, we will
5533 not have to repeat the effort required to do the parse, nor will
5534 we issue duplicate error messages. */
5535 if (success && start)
5537 cp_token *token;
5539 token = cp_lexer_token_at (parser->lexer, start);
5540 /* Reset the contents of the START token. */
5541 token->type = CPP_NESTED_NAME_SPECIFIER;
5542 /* Retrieve any deferred checks. Do not pop this access checks yet
5543 so the memory will not be reclaimed during token replacing below. */
5544 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5545 token->u.tree_check_value->value = parser->scope;
5546 token->u.tree_check_value->checks = get_deferred_access_checks ();
5547 token->u.tree_check_value->qualifying_scope =
5548 parser->qualifying_scope;
5549 token->keyword = RID_MAX;
5551 /* Purge all subsequent tokens. */
5552 cp_lexer_purge_tokens_after (parser->lexer, start);
5555 if (start)
5556 pop_to_parent_deferring_access_checks ();
5558 return success ? parser->scope : NULL_TREE;
5561 /* Parse a nested-name-specifier. See
5562 cp_parser_nested_name_specifier_opt for details. This function
5563 behaves identically, except that it will an issue an error if no
5564 nested-name-specifier is present. */
5566 static tree
5567 cp_parser_nested_name_specifier (cp_parser *parser,
5568 bool typename_keyword_p,
5569 bool check_dependency_p,
5570 bool type_p,
5571 bool is_declaration)
5573 tree scope;
5575 /* Look for the nested-name-specifier. */
5576 scope = cp_parser_nested_name_specifier_opt (parser,
5577 typename_keyword_p,
5578 check_dependency_p,
5579 type_p,
5580 is_declaration);
5581 /* If it was not present, issue an error message. */
5582 if (!scope)
5584 cp_parser_error (parser, "expected nested-name-specifier");
5585 parser->scope = NULL_TREE;
5588 return scope;
5591 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5592 this is either a class-name or a namespace-name (which corresponds
5593 to the class-or-namespace-name production in the grammar). For
5594 C++0x, it can also be a type-name that refers to an enumeration
5595 type or a simple-template-id.
5597 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5598 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5599 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5600 TYPE_P is TRUE iff the next name should be taken as a class-name,
5601 even the same name is declared to be another entity in the same
5602 scope.
5604 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5605 specified by the class-or-namespace-name. If neither is found the
5606 ERROR_MARK_NODE is returned. */
5608 static tree
5609 cp_parser_qualifying_entity (cp_parser *parser,
5610 bool typename_keyword_p,
5611 bool template_keyword_p,
5612 bool check_dependency_p,
5613 bool type_p,
5614 bool is_declaration)
5616 tree saved_scope;
5617 tree saved_qualifying_scope;
5618 tree saved_object_scope;
5619 tree scope;
5620 bool only_class_p;
5621 bool successful_parse_p;
5623 /* DR 743: decltype can appear in a nested-name-specifier. */
5624 if (cp_lexer_next_token_is_decltype (parser->lexer))
5626 scope = cp_parser_decltype (parser);
5627 if (TREE_CODE (scope) != ENUMERAL_TYPE
5628 && !MAYBE_CLASS_TYPE_P (scope))
5630 cp_parser_simulate_error (parser);
5631 return error_mark_node;
5633 if (TYPE_NAME (scope))
5634 scope = TYPE_NAME (scope);
5635 return scope;
5638 /* Before we try to parse the class-name, we must save away the
5639 current PARSER->SCOPE since cp_parser_class_name will destroy
5640 it. */
5641 saved_scope = parser->scope;
5642 saved_qualifying_scope = parser->qualifying_scope;
5643 saved_object_scope = parser->object_scope;
5644 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5645 there is no need to look for a namespace-name. */
5646 only_class_p = template_keyword_p
5647 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5648 if (!only_class_p)
5649 cp_parser_parse_tentatively (parser);
5650 scope = cp_parser_class_name (parser,
5651 typename_keyword_p,
5652 template_keyword_p,
5653 type_p ? class_type : none_type,
5654 check_dependency_p,
5655 /*class_head_p=*/false,
5656 is_declaration);
5657 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5658 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5659 if (!only_class_p
5660 && cxx_dialect != cxx98
5661 && !successful_parse_p)
5663 /* Restore the saved scope. */
5664 parser->scope = saved_scope;
5665 parser->qualifying_scope = saved_qualifying_scope;
5666 parser->object_scope = saved_object_scope;
5668 /* Parse tentatively. */
5669 cp_parser_parse_tentatively (parser);
5671 /* Parse a type-name */
5672 scope = cp_parser_type_name (parser);
5674 /* "If the name found does not designate a namespace or a class,
5675 enumeration, or dependent type, the program is ill-formed."
5677 We cover classes and dependent types above and namespaces below,
5678 so this code is only looking for enums. */
5679 if (!scope || TREE_CODE (scope) != TYPE_DECL
5680 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5681 cp_parser_simulate_error (parser);
5683 successful_parse_p = cp_parser_parse_definitely (parser);
5685 /* If that didn't work, try for a namespace-name. */
5686 if (!only_class_p && !successful_parse_p)
5688 /* Restore the saved scope. */
5689 parser->scope = saved_scope;
5690 parser->qualifying_scope = saved_qualifying_scope;
5691 parser->object_scope = saved_object_scope;
5692 /* If we are not looking at an identifier followed by the scope
5693 resolution operator, then this is not part of a
5694 nested-name-specifier. (Note that this function is only used
5695 to parse the components of a nested-name-specifier.) */
5696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5697 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5698 return error_mark_node;
5699 scope = cp_parser_namespace_name (parser);
5702 return scope;
5705 /* Return true if we are looking at a compound-literal, false otherwise. */
5707 static bool
5708 cp_parser_compound_literal_p (cp_parser *parser)
5710 /* Consume the `('. */
5711 cp_lexer_consume_token (parser->lexer);
5713 cp_lexer_save_tokens (parser->lexer);
5715 /* Skip tokens until the next token is a closing parenthesis.
5716 If we find the closing `)', and the next token is a `{', then
5717 we are looking at a compound-literal. */
5718 bool compound_literal_p
5719 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5720 /*consume_paren=*/true)
5721 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5723 /* Roll back the tokens we skipped. */
5724 cp_lexer_rollback_tokens (parser->lexer);
5726 return compound_literal_p;
5729 /* Parse a postfix-expression.
5731 postfix-expression:
5732 primary-expression
5733 postfix-expression [ expression ]
5734 postfix-expression ( expression-list [opt] )
5735 simple-type-specifier ( expression-list [opt] )
5736 typename :: [opt] nested-name-specifier identifier
5737 ( expression-list [opt] )
5738 typename :: [opt] nested-name-specifier template [opt] template-id
5739 ( expression-list [opt] )
5740 postfix-expression . template [opt] id-expression
5741 postfix-expression -> template [opt] id-expression
5742 postfix-expression . pseudo-destructor-name
5743 postfix-expression -> pseudo-destructor-name
5744 postfix-expression ++
5745 postfix-expression --
5746 dynamic_cast < type-id > ( expression )
5747 static_cast < type-id > ( expression )
5748 reinterpret_cast < type-id > ( expression )
5749 const_cast < type-id > ( expression )
5750 typeid ( expression )
5751 typeid ( type-id )
5753 GNU Extension:
5755 postfix-expression:
5756 ( type-id ) { initializer-list , [opt] }
5758 This extension is a GNU version of the C99 compound-literal
5759 construct. (The C99 grammar uses `type-name' instead of `type-id',
5760 but they are essentially the same concept.)
5762 If ADDRESS_P is true, the postfix expression is the operand of the
5763 `&' operator. CAST_P is true if this expression is the target of a
5764 cast.
5766 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5767 class member access expressions [expr.ref].
5769 Returns a representation of the expression. */
5771 static tree
5772 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5773 bool member_access_only_p, bool decltype_p,
5774 cp_id_kind * pidk_return)
5776 cp_token *token;
5777 location_t loc;
5778 enum rid keyword;
5779 cp_id_kind idk = CP_ID_KIND_NONE;
5780 tree postfix_expression = NULL_TREE;
5781 bool is_member_access = false;
5782 int saved_in_statement = -1;
5784 /* Peek at the next token. */
5785 token = cp_lexer_peek_token (parser->lexer);
5786 loc = token->location;
5787 /* Some of the productions are determined by keywords. */
5788 keyword = token->keyword;
5789 switch (keyword)
5791 case RID_DYNCAST:
5792 case RID_STATCAST:
5793 case RID_REINTCAST:
5794 case RID_CONSTCAST:
5796 tree type;
5797 tree expression;
5798 const char *saved_message;
5799 bool saved_in_type_id_in_expr_p;
5801 /* All of these can be handled in the same way from the point
5802 of view of parsing. Begin by consuming the token
5803 identifying the cast. */
5804 cp_lexer_consume_token (parser->lexer);
5806 /* New types cannot be defined in the cast. */
5807 saved_message = parser->type_definition_forbidden_message;
5808 parser->type_definition_forbidden_message
5809 = G_("types may not be defined in casts");
5811 /* Look for the opening `<'. */
5812 cp_parser_require (parser, CPP_LESS, RT_LESS);
5813 /* Parse the type to which we are casting. */
5814 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5815 parser->in_type_id_in_expr_p = true;
5816 type = cp_parser_type_id (parser);
5817 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5818 /* Look for the closing `>'. */
5819 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5820 /* Restore the old message. */
5821 parser->type_definition_forbidden_message = saved_message;
5823 bool saved_greater_than_is_operator_p
5824 = parser->greater_than_is_operator_p;
5825 parser->greater_than_is_operator_p = true;
5827 /* And the expression which is being cast. */
5828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5829 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5832 parser->greater_than_is_operator_p
5833 = saved_greater_than_is_operator_p;
5835 /* Only type conversions to integral or enumeration types
5836 can be used in constant-expressions. */
5837 if (!cast_valid_in_integral_constant_expression_p (type)
5838 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5839 return error_mark_node;
5841 switch (keyword)
5843 case RID_DYNCAST:
5844 postfix_expression
5845 = build_dynamic_cast (type, expression, tf_warning_or_error);
5846 break;
5847 case RID_STATCAST:
5848 postfix_expression
5849 = build_static_cast (type, expression, tf_warning_or_error);
5850 break;
5851 case RID_REINTCAST:
5852 postfix_expression
5853 = build_reinterpret_cast (type, expression,
5854 tf_warning_or_error);
5855 break;
5856 case RID_CONSTCAST:
5857 postfix_expression
5858 = build_const_cast (type, expression, tf_warning_or_error);
5859 break;
5860 default:
5861 gcc_unreachable ();
5864 break;
5866 case RID_TYPEID:
5868 tree type;
5869 const char *saved_message;
5870 bool saved_in_type_id_in_expr_p;
5872 /* Consume the `typeid' token. */
5873 cp_lexer_consume_token (parser->lexer);
5874 /* Look for the `(' token. */
5875 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5876 /* Types cannot be defined in a `typeid' expression. */
5877 saved_message = parser->type_definition_forbidden_message;
5878 parser->type_definition_forbidden_message
5879 = G_("types may not be defined in a %<typeid%> expression");
5880 /* We can't be sure yet whether we're looking at a type-id or an
5881 expression. */
5882 cp_parser_parse_tentatively (parser);
5883 /* Try a type-id first. */
5884 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5885 parser->in_type_id_in_expr_p = true;
5886 type = cp_parser_type_id (parser);
5887 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5888 /* Look for the `)' token. Otherwise, we can't be sure that
5889 we're not looking at an expression: consider `typeid (int
5890 (3))', for example. */
5891 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5892 /* If all went well, simply lookup the type-id. */
5893 if (cp_parser_parse_definitely (parser))
5894 postfix_expression = get_typeid (type, tf_warning_or_error);
5895 /* Otherwise, fall back to the expression variant. */
5896 else
5898 tree expression;
5900 /* Look for an expression. */
5901 expression = cp_parser_expression (parser, & idk);
5902 /* Compute its typeid. */
5903 postfix_expression = build_typeid (expression, tf_warning_or_error);
5904 /* Look for the `)' token. */
5905 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5907 /* Restore the saved message. */
5908 parser->type_definition_forbidden_message = saved_message;
5909 /* `typeid' may not appear in an integral constant expression. */
5910 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5911 return error_mark_node;
5913 break;
5915 case RID_TYPENAME:
5917 tree type;
5918 /* The syntax permitted here is the same permitted for an
5919 elaborated-type-specifier. */
5920 type = cp_parser_elaborated_type_specifier (parser,
5921 /*is_friend=*/false,
5922 /*is_declaration=*/false);
5923 postfix_expression = cp_parser_functional_cast (parser, type);
5925 break;
5927 case RID_CILK_SPAWN:
5929 cp_lexer_consume_token (parser->lexer);
5930 token = cp_lexer_peek_token (parser->lexer);
5931 if (token->type == CPP_SEMICOLON)
5933 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5934 "an expression");
5935 postfix_expression = error_mark_node;
5936 break;
5938 else if (!current_function_decl)
5940 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5941 "inside a function");
5942 postfix_expression = error_mark_node;
5943 break;
5945 else
5947 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5948 saved_in_statement = parser->in_statement;
5949 parser->in_statement |= IN_CILK_SPAWN;
5951 cfun->calls_cilk_spawn = 1;
5952 postfix_expression =
5953 cp_parser_postfix_expression (parser, false, false,
5954 false, false, &idk);
5955 if (!flag_cilkplus)
5957 error_at (token->location, "-fcilkplus must be enabled to use"
5958 " %<_Cilk_spawn%>");
5959 cfun->calls_cilk_spawn = 0;
5961 else if (saved_in_statement & IN_CILK_SPAWN)
5963 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5964 "are not permitted");
5965 postfix_expression = error_mark_node;
5966 cfun->calls_cilk_spawn = 0;
5968 else
5970 postfix_expression = build_cilk_spawn (token->location,
5971 postfix_expression);
5972 if (postfix_expression != error_mark_node)
5973 SET_EXPR_LOCATION (postfix_expression, input_location);
5974 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5976 break;
5979 case RID_BUILTIN_SHUFFLE:
5981 vec<tree, va_gc> *vec;
5982 unsigned int i;
5983 tree p;
5985 cp_lexer_consume_token (parser->lexer);
5986 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5987 /*cast_p=*/false, /*allow_expansion_p=*/true,
5988 /*non_constant_p=*/NULL);
5989 if (vec == NULL)
5990 return error_mark_node;
5992 FOR_EACH_VEC_ELT (*vec, i, p)
5993 mark_exp_read (p);
5995 if (vec->length () == 2)
5996 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5997 tf_warning_or_error);
5998 else if (vec->length () == 3)
5999 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6000 tf_warning_or_error);
6001 else
6003 error_at (loc, "wrong number of arguments to "
6004 "%<__builtin_shuffle%>");
6005 return error_mark_node;
6007 break;
6010 default:
6012 tree type;
6014 /* If the next thing is a simple-type-specifier, we may be
6015 looking at a functional cast. We could also be looking at
6016 an id-expression. So, we try the functional cast, and if
6017 that doesn't work we fall back to the primary-expression. */
6018 cp_parser_parse_tentatively (parser);
6019 /* Look for the simple-type-specifier. */
6020 type = cp_parser_simple_type_specifier (parser,
6021 /*decl_specs=*/NULL,
6022 CP_PARSER_FLAGS_NONE);
6023 /* Parse the cast itself. */
6024 if (!cp_parser_error_occurred (parser))
6025 postfix_expression
6026 = cp_parser_functional_cast (parser, type);
6027 /* If that worked, we're done. */
6028 if (cp_parser_parse_definitely (parser))
6029 break;
6031 /* If the functional-cast didn't work out, try a
6032 compound-literal. */
6033 if (cp_parser_allow_gnu_extensions_p (parser)
6034 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6036 tree initializer = NULL_TREE;
6038 cp_parser_parse_tentatively (parser);
6040 /* Avoid calling cp_parser_type_id pointlessly, see comment
6041 in cp_parser_cast_expression about c++/29234. */
6042 if (!cp_parser_compound_literal_p (parser))
6043 cp_parser_simulate_error (parser);
6044 else
6046 /* Parse the type. */
6047 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6048 parser->in_type_id_in_expr_p = true;
6049 type = cp_parser_type_id (parser);
6050 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6051 /* Look for the `)'. */
6052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6055 /* If things aren't going well, there's no need to
6056 keep going. */
6057 if (!cp_parser_error_occurred (parser))
6059 bool non_constant_p;
6060 /* Parse the brace-enclosed initializer list. */
6061 initializer = cp_parser_braced_list (parser,
6062 &non_constant_p);
6064 /* If that worked, we're definitely looking at a
6065 compound-literal expression. */
6066 if (cp_parser_parse_definitely (parser))
6068 /* Warn the user that a compound literal is not
6069 allowed in standard C++. */
6070 pedwarn (input_location, OPT_Wpedantic,
6071 "ISO C++ forbids compound-literals");
6072 /* For simplicity, we disallow compound literals in
6073 constant-expressions. We could
6074 allow compound literals of integer type, whose
6075 initializer was a constant, in constant
6076 expressions. Permitting that usage, as a further
6077 extension, would not change the meaning of any
6078 currently accepted programs. (Of course, as
6079 compound literals are not part of ISO C++, the
6080 standard has nothing to say.) */
6081 if (cp_parser_non_integral_constant_expression (parser,
6082 NIC_NCC))
6084 postfix_expression = error_mark_node;
6085 break;
6087 /* Form the representation of the compound-literal. */
6088 postfix_expression
6089 = finish_compound_literal (type, initializer,
6090 tf_warning_or_error);
6091 break;
6095 /* It must be a primary-expression. */
6096 postfix_expression
6097 = cp_parser_primary_expression (parser, address_p, cast_p,
6098 /*template_arg_p=*/false,
6099 decltype_p,
6100 &idk);
6102 break;
6105 /* Note that we don't need to worry about calling build_cplus_new on a
6106 class-valued CALL_EXPR in decltype when it isn't the end of the
6107 postfix-expression; unary_complex_lvalue will take care of that for
6108 all these cases. */
6110 /* Keep looping until the postfix-expression is complete. */
6111 while (true)
6113 if (idk == CP_ID_KIND_UNQUALIFIED
6114 && identifier_p (postfix_expression)
6115 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6116 /* It is not a Koenig lookup function call. */
6117 postfix_expression
6118 = unqualified_name_lookup_error (postfix_expression);
6120 /* Peek at the next token. */
6121 token = cp_lexer_peek_token (parser->lexer);
6123 switch (token->type)
6125 case CPP_OPEN_SQUARE:
6126 if (cp_next_tokens_can_be_std_attribute_p (parser))
6128 cp_parser_error (parser,
6129 "two consecutive %<[%> shall "
6130 "only introduce an attribute");
6131 return error_mark_node;
6133 postfix_expression
6134 = cp_parser_postfix_open_square_expression (parser,
6135 postfix_expression,
6136 false,
6137 decltype_p);
6138 idk = CP_ID_KIND_NONE;
6139 is_member_access = false;
6140 break;
6142 case CPP_OPEN_PAREN:
6143 /* postfix-expression ( expression-list [opt] ) */
6145 bool koenig_p;
6146 bool is_builtin_constant_p;
6147 bool saved_integral_constant_expression_p = false;
6148 bool saved_non_integral_constant_expression_p = false;
6149 tsubst_flags_t complain = complain_flags (decltype_p);
6150 vec<tree, va_gc> *args;
6152 is_member_access = false;
6154 is_builtin_constant_p
6155 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6156 if (is_builtin_constant_p)
6158 /* The whole point of __builtin_constant_p is to allow
6159 non-constant expressions to appear as arguments. */
6160 saved_integral_constant_expression_p
6161 = parser->integral_constant_expression_p;
6162 saved_non_integral_constant_expression_p
6163 = parser->non_integral_constant_expression_p;
6164 parser->integral_constant_expression_p = false;
6166 args = (cp_parser_parenthesized_expression_list
6167 (parser, non_attr,
6168 /*cast_p=*/false, /*allow_expansion_p=*/true,
6169 /*non_constant_p=*/NULL,
6170 /*want_literal_zero_p=*/warn_memset_transposed_args));
6171 if (is_builtin_constant_p)
6173 parser->integral_constant_expression_p
6174 = saved_integral_constant_expression_p;
6175 parser->non_integral_constant_expression_p
6176 = saved_non_integral_constant_expression_p;
6179 if (args == NULL)
6181 postfix_expression = error_mark_node;
6182 break;
6185 /* Function calls are not permitted in
6186 constant-expressions. */
6187 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6188 && cp_parser_non_integral_constant_expression (parser,
6189 NIC_FUNC_CALL))
6191 postfix_expression = error_mark_node;
6192 release_tree_vector (args);
6193 break;
6196 koenig_p = false;
6197 if (idk == CP_ID_KIND_UNQUALIFIED
6198 || idk == CP_ID_KIND_TEMPLATE_ID)
6200 if (identifier_p (postfix_expression))
6202 if (!args->is_empty ())
6204 koenig_p = true;
6205 if (!any_type_dependent_arguments_p (args))
6206 postfix_expression
6207 = perform_koenig_lookup (postfix_expression, args,
6208 complain);
6210 else
6211 postfix_expression
6212 = unqualified_fn_lookup_error (postfix_expression);
6214 /* We do not perform argument-dependent lookup if
6215 normal lookup finds a non-function, in accordance
6216 with the expected resolution of DR 218. */
6217 else if (!args->is_empty ()
6218 && is_overloaded_fn (postfix_expression))
6220 tree fn = get_first_fn (postfix_expression);
6221 fn = STRIP_TEMPLATE (fn);
6223 /* Do not do argument dependent lookup if regular
6224 lookup finds a member function or a block-scope
6225 function declaration. [basic.lookup.argdep]/3 */
6226 if (!DECL_FUNCTION_MEMBER_P (fn)
6227 && !DECL_LOCAL_FUNCTION_P (fn))
6229 koenig_p = true;
6230 if (!any_type_dependent_arguments_p (args))
6231 postfix_expression
6232 = perform_koenig_lookup (postfix_expression, args,
6233 complain);
6238 if (warn_memset_transposed_args)
6240 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6241 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6242 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6243 && vec_safe_length (args) == 3
6244 && integer_zerop ((*args)[2])
6245 && LITERAL_ZERO_P ((*args)[2])
6246 && !(integer_zerop ((*args)[1])
6247 && LITERAL_ZERO_P ((*args)[1])))
6248 warning (OPT_Wmemset_transposed_args,
6249 "%<memset%> used with constant zero length "
6250 "parameter; this could be due to transposed "
6251 "parameters");
6253 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6254 to avoid leaking those into folder and middle-end. */
6255 unsigned int i;
6256 tree arg;
6257 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6258 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6259 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6262 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6264 tree instance = TREE_OPERAND (postfix_expression, 0);
6265 tree fn = TREE_OPERAND (postfix_expression, 1);
6267 if (processing_template_decl
6268 && (type_dependent_expression_p (instance)
6269 || (!BASELINK_P (fn)
6270 && TREE_CODE (fn) != FIELD_DECL)
6271 || type_dependent_expression_p (fn)
6272 || any_type_dependent_arguments_p (args)))
6274 postfix_expression
6275 = build_nt_call_vec (postfix_expression, args);
6276 release_tree_vector (args);
6277 break;
6280 if (BASELINK_P (fn))
6282 postfix_expression
6283 = (build_new_method_call
6284 (instance, fn, &args, NULL_TREE,
6285 (idk == CP_ID_KIND_QUALIFIED
6286 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6287 : LOOKUP_NORMAL),
6288 /*fn_p=*/NULL,
6289 complain));
6291 else
6292 postfix_expression
6293 = finish_call_expr (postfix_expression, &args,
6294 /*disallow_virtual=*/false,
6295 /*koenig_p=*/false,
6296 complain);
6298 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6299 || TREE_CODE (postfix_expression) == MEMBER_REF
6300 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6301 postfix_expression = (build_offset_ref_call_from_tree
6302 (postfix_expression, &args,
6303 complain));
6304 else if (idk == CP_ID_KIND_QUALIFIED)
6305 /* A call to a static class member, or a namespace-scope
6306 function. */
6307 postfix_expression
6308 = finish_call_expr (postfix_expression, &args,
6309 /*disallow_virtual=*/true,
6310 koenig_p,
6311 complain);
6312 else
6313 /* All other function calls. */
6314 postfix_expression
6315 = finish_call_expr (postfix_expression, &args,
6316 /*disallow_virtual=*/false,
6317 koenig_p,
6318 complain);
6320 protected_set_expr_location (postfix_expression, token->location);
6322 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6323 idk = CP_ID_KIND_NONE;
6325 release_tree_vector (args);
6327 break;
6329 case CPP_DOT:
6330 case CPP_DEREF:
6331 /* postfix-expression . template [opt] id-expression
6332 postfix-expression . pseudo-destructor-name
6333 postfix-expression -> template [opt] id-expression
6334 postfix-expression -> pseudo-destructor-name */
6336 /* Consume the `.' or `->' operator. */
6337 cp_lexer_consume_token (parser->lexer);
6339 postfix_expression
6340 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6341 postfix_expression,
6342 false, &idk, loc);
6344 is_member_access = true;
6345 break;
6347 case CPP_PLUS_PLUS:
6348 /* postfix-expression ++ */
6349 /* Consume the `++' token. */
6350 cp_lexer_consume_token (parser->lexer);
6351 /* Generate a representation for the complete expression. */
6352 postfix_expression
6353 = finish_increment_expr (postfix_expression,
6354 POSTINCREMENT_EXPR);
6355 /* Increments may not appear in constant-expressions. */
6356 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6357 postfix_expression = error_mark_node;
6358 idk = CP_ID_KIND_NONE;
6359 is_member_access = false;
6360 break;
6362 case CPP_MINUS_MINUS:
6363 /* postfix-expression -- */
6364 /* Consume the `--' token. */
6365 cp_lexer_consume_token (parser->lexer);
6366 /* Generate a representation for the complete expression. */
6367 postfix_expression
6368 = finish_increment_expr (postfix_expression,
6369 POSTDECREMENT_EXPR);
6370 /* Decrements may not appear in constant-expressions. */
6371 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6372 postfix_expression = error_mark_node;
6373 idk = CP_ID_KIND_NONE;
6374 is_member_access = false;
6375 break;
6377 default:
6378 if (pidk_return != NULL)
6379 * pidk_return = idk;
6380 if (member_access_only_p)
6381 return is_member_access? postfix_expression : error_mark_node;
6382 else
6383 return postfix_expression;
6387 /* We should never get here. */
6388 gcc_unreachable ();
6389 return error_mark_node;
6392 /* This function parses Cilk Plus array notations. If a normal array expr. is
6393 parsed then the array index is passed back to the caller through *INIT_INDEX
6394 and the function returns a NULL_TREE. If array notation expr. is parsed,
6395 then *INIT_INDEX is ignored by the caller and the function returns
6396 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6397 error_mark_node. */
6399 static tree
6400 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6401 tree array_value)
6403 cp_token *token = NULL;
6404 tree length_index, stride = NULL_TREE, value_tree, array_type;
6405 if (!array_value || array_value == error_mark_node)
6407 cp_parser_skip_to_end_of_statement (parser);
6408 return error_mark_node;
6411 array_type = TREE_TYPE (array_value);
6413 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6414 parser->colon_corrects_to_scope_p = false;
6415 token = cp_lexer_peek_token (parser->lexer);
6417 if (!token)
6419 cp_parser_error (parser, "expected %<:%> or numeral");
6420 return error_mark_node;
6422 else if (token->type == CPP_COLON)
6424 /* Consume the ':'. */
6425 cp_lexer_consume_token (parser->lexer);
6427 /* If we are here, then we have a case like this A[:]. */
6428 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6430 cp_parser_error (parser, "expected %<]%>");
6431 cp_parser_skip_to_end_of_statement (parser);
6432 return error_mark_node;
6434 *init_index = NULL_TREE;
6435 stride = NULL_TREE;
6436 length_index = NULL_TREE;
6438 else
6440 /* If we are here, then there are three valid possibilities:
6441 1. ARRAY [ EXP ]
6442 2. ARRAY [ EXP : EXP ]
6443 3. ARRAY [ EXP : EXP : EXP ] */
6445 *init_index = cp_parser_expression (parser);
6446 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6448 /* This indicates that we have a normal array expression. */
6449 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6450 return NULL_TREE;
6453 /* Consume the ':'. */
6454 cp_lexer_consume_token (parser->lexer);
6455 length_index = cp_parser_expression (parser);
6456 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6458 cp_lexer_consume_token (parser->lexer);
6459 stride = cp_parser_expression (parser);
6462 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6464 if (*init_index == error_mark_node || length_index == error_mark_node
6465 || stride == error_mark_node || array_type == error_mark_node)
6467 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6468 cp_lexer_consume_token (parser->lexer);
6469 return error_mark_node;
6471 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6473 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6474 length_index, stride, array_type);
6475 return value_tree;
6478 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6479 by cp_parser_builtin_offsetof. We're looking for
6481 postfix-expression [ expression ]
6482 postfix-expression [ braced-init-list ] (C++11)
6484 FOR_OFFSETOF is set if we're being called in that context, which
6485 changes how we deal with integer constant expressions. */
6487 static tree
6488 cp_parser_postfix_open_square_expression (cp_parser *parser,
6489 tree postfix_expression,
6490 bool for_offsetof,
6491 bool decltype_p)
6493 tree index = NULL_TREE;
6494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6495 bool saved_greater_than_is_operator_p;
6497 /* Consume the `[' token. */
6498 cp_lexer_consume_token (parser->lexer);
6500 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6501 parser->greater_than_is_operator_p = true;
6503 /* Parse the index expression. */
6504 /* ??? For offsetof, there is a question of what to allow here. If
6505 offsetof is not being used in an integral constant expression context,
6506 then we *could* get the right answer by computing the value at runtime.
6507 If we are in an integral constant expression context, then we might
6508 could accept any constant expression; hard to say without analysis.
6509 Rather than open the barn door too wide right away, allow only integer
6510 constant expressions here. */
6511 if (for_offsetof)
6512 index = cp_parser_constant_expression (parser);
6513 else
6515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6517 bool expr_nonconst_p;
6518 cp_lexer_set_source_position (parser->lexer);
6519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6520 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6521 if (flag_cilkplus
6522 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6524 error_at (cp_lexer_peek_token (parser->lexer)->location,
6525 "braced list index is not allowed with array "
6526 "notation");
6527 cp_parser_skip_to_end_of_statement (parser);
6528 return error_mark_node;
6531 else if (flag_cilkplus)
6533 /* Here are have these two options:
6534 ARRAY[EXP : EXP] - Array notation expr with default
6535 stride of 1.
6536 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6537 stride. */
6538 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6539 postfix_expression);
6540 if (an_exp)
6541 return an_exp;
6543 else
6544 index = cp_parser_expression (parser);
6547 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6549 /* Look for the closing `]'. */
6550 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6552 /* Build the ARRAY_REF. */
6553 postfix_expression = grok_array_decl (loc, postfix_expression,
6554 index, decltype_p);
6556 /* When not doing offsetof, array references are not permitted in
6557 constant-expressions. */
6558 if (!for_offsetof
6559 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6560 postfix_expression = error_mark_node;
6562 return postfix_expression;
6565 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6566 by cp_parser_builtin_offsetof. We're looking for
6568 postfix-expression . template [opt] id-expression
6569 postfix-expression . pseudo-destructor-name
6570 postfix-expression -> template [opt] id-expression
6571 postfix-expression -> pseudo-destructor-name
6573 FOR_OFFSETOF is set if we're being called in that context. That sorta
6574 limits what of the above we'll actually accept, but nevermind.
6575 TOKEN_TYPE is the "." or "->" token, which will already have been
6576 removed from the stream. */
6578 static tree
6579 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6580 enum cpp_ttype token_type,
6581 tree postfix_expression,
6582 bool for_offsetof, cp_id_kind *idk,
6583 location_t location)
6585 tree name;
6586 bool dependent_p;
6587 bool pseudo_destructor_p;
6588 tree scope = NULL_TREE;
6590 /* If this is a `->' operator, dereference the pointer. */
6591 if (token_type == CPP_DEREF)
6592 postfix_expression = build_x_arrow (location, postfix_expression,
6593 tf_warning_or_error);
6594 /* Check to see whether or not the expression is type-dependent. */
6595 dependent_p = type_dependent_expression_p (postfix_expression);
6596 /* The identifier following the `->' or `.' is not qualified. */
6597 parser->scope = NULL_TREE;
6598 parser->qualifying_scope = NULL_TREE;
6599 parser->object_scope = NULL_TREE;
6600 *idk = CP_ID_KIND_NONE;
6602 /* Enter the scope corresponding to the type of the object
6603 given by the POSTFIX_EXPRESSION. */
6604 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6606 scope = TREE_TYPE (postfix_expression);
6607 /* According to the standard, no expression should ever have
6608 reference type. Unfortunately, we do not currently match
6609 the standard in this respect in that our internal representation
6610 of an expression may have reference type even when the standard
6611 says it does not. Therefore, we have to manually obtain the
6612 underlying type here. */
6613 scope = non_reference (scope);
6614 /* The type of the POSTFIX_EXPRESSION must be complete. */
6615 if (scope == unknown_type_node)
6617 error_at (location, "%qE does not have class type",
6618 postfix_expression);
6619 scope = NULL_TREE;
6621 /* Unlike the object expression in other contexts, *this is not
6622 required to be of complete type for purposes of class member
6623 access (5.2.5) outside the member function body. */
6624 else if (postfix_expression != current_class_ref
6625 && !(processing_template_decl && scope == current_class_type))
6626 scope = complete_type_or_else (scope, NULL_TREE);
6627 /* Let the name lookup machinery know that we are processing a
6628 class member access expression. */
6629 parser->context->object_type = scope;
6630 /* If something went wrong, we want to be able to discern that case,
6631 as opposed to the case where there was no SCOPE due to the type
6632 of expression being dependent. */
6633 if (!scope)
6634 scope = error_mark_node;
6635 /* If the SCOPE was erroneous, make the various semantic analysis
6636 functions exit quickly -- and without issuing additional error
6637 messages. */
6638 if (scope == error_mark_node)
6639 postfix_expression = error_mark_node;
6642 /* Assume this expression is not a pseudo-destructor access. */
6643 pseudo_destructor_p = false;
6645 /* If the SCOPE is a scalar type, then, if this is a valid program,
6646 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6647 is type dependent, it can be pseudo-destructor-name or something else.
6648 Try to parse it as pseudo-destructor-name first. */
6649 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6651 tree s;
6652 tree type;
6654 cp_parser_parse_tentatively (parser);
6655 /* Parse the pseudo-destructor-name. */
6656 s = NULL_TREE;
6657 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6658 &s, &type);
6659 if (dependent_p
6660 && (cp_parser_error_occurred (parser)
6661 || !SCALAR_TYPE_P (type)))
6662 cp_parser_abort_tentative_parse (parser);
6663 else if (cp_parser_parse_definitely (parser))
6665 pseudo_destructor_p = true;
6666 postfix_expression
6667 = finish_pseudo_destructor_expr (postfix_expression,
6668 s, type, location);
6672 if (!pseudo_destructor_p)
6674 /* If the SCOPE is not a scalar type, we are looking at an
6675 ordinary class member access expression, rather than a
6676 pseudo-destructor-name. */
6677 bool template_p;
6678 cp_token *token = cp_lexer_peek_token (parser->lexer);
6679 /* Parse the id-expression. */
6680 name = (cp_parser_id_expression
6681 (parser,
6682 cp_parser_optional_template_keyword (parser),
6683 /*check_dependency_p=*/true,
6684 &template_p,
6685 /*declarator_p=*/false,
6686 /*optional_p=*/false));
6687 /* In general, build a SCOPE_REF if the member name is qualified.
6688 However, if the name was not dependent and has already been
6689 resolved; there is no need to build the SCOPE_REF. For example;
6691 struct X { void f(); };
6692 template <typename T> void f(T* t) { t->X::f(); }
6694 Even though "t" is dependent, "X::f" is not and has been resolved
6695 to a BASELINK; there is no need to include scope information. */
6697 /* But we do need to remember that there was an explicit scope for
6698 virtual function calls. */
6699 if (parser->scope)
6700 *idk = CP_ID_KIND_QUALIFIED;
6702 /* If the name is a template-id that names a type, we will get a
6703 TYPE_DECL here. That is invalid code. */
6704 if (TREE_CODE (name) == TYPE_DECL)
6706 error_at (token->location, "invalid use of %qD", name);
6707 postfix_expression = error_mark_node;
6709 else
6711 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6713 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6715 error_at (token->location, "%<%D::%D%> is not a class member",
6716 parser->scope, name);
6717 postfix_expression = error_mark_node;
6719 else
6720 name = build_qualified_name (/*type=*/NULL_TREE,
6721 parser->scope,
6722 name,
6723 template_p);
6724 parser->scope = NULL_TREE;
6725 parser->qualifying_scope = NULL_TREE;
6726 parser->object_scope = NULL_TREE;
6728 if (parser->scope && name && BASELINK_P (name))
6729 adjust_result_of_qualified_name_lookup
6730 (name, parser->scope, scope);
6731 postfix_expression
6732 = finish_class_member_access_expr (postfix_expression, name,
6733 template_p,
6734 tf_warning_or_error);
6738 /* We no longer need to look up names in the scope of the object on
6739 the left-hand side of the `.' or `->' operator. */
6740 parser->context->object_type = NULL_TREE;
6742 /* Outside of offsetof, these operators may not appear in
6743 constant-expressions. */
6744 if (!for_offsetof
6745 && (cp_parser_non_integral_constant_expression
6746 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6747 postfix_expression = error_mark_node;
6749 return postfix_expression;
6752 /* Cache of LITERAL_ZERO_P constants. */
6754 static GTY(()) tree literal_zeros[itk_none];
6756 /* Parse a parenthesized expression-list.
6758 expression-list:
6759 assignment-expression
6760 expression-list, assignment-expression
6762 attribute-list:
6763 expression-list
6764 identifier
6765 identifier, expression-list
6767 CAST_P is true if this expression is the target of a cast.
6769 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6770 argument pack.
6772 Returns a vector of trees. Each element is a representation of an
6773 assignment-expression. NULL is returned if the ( and or ) are
6774 missing. An empty, but allocated, vector is returned on no
6775 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6776 if we are parsing an attribute list for an attribute that wants a
6777 plain identifier argument, normal_attr for an attribute that wants
6778 an expression, or non_attr if we aren't parsing an attribute list. If
6779 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6780 not all of the expressions in the list were constant.
6781 WANT_LITERAL_ZERO_P is true if the caller is interested in
6782 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6783 immediately, this can be removed. */
6785 static vec<tree, va_gc> *
6786 cp_parser_parenthesized_expression_list (cp_parser* parser,
6787 int is_attribute_list,
6788 bool cast_p,
6789 bool allow_expansion_p,
6790 bool *non_constant_p,
6791 bool want_literal_zero_p)
6793 vec<tree, va_gc> *expression_list;
6794 bool fold_expr_p = is_attribute_list != non_attr;
6795 tree identifier = NULL_TREE;
6796 bool saved_greater_than_is_operator_p;
6798 /* Assume all the expressions will be constant. */
6799 if (non_constant_p)
6800 *non_constant_p = false;
6802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6803 return NULL;
6805 expression_list = make_tree_vector ();
6807 /* Within a parenthesized expression, a `>' token is always
6808 the greater-than operator. */
6809 saved_greater_than_is_operator_p
6810 = parser->greater_than_is_operator_p;
6811 parser->greater_than_is_operator_p = true;
6813 /* Consume expressions until there are no more. */
6814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6815 while (true)
6817 tree expr;
6819 /* At the beginning of attribute lists, check to see if the
6820 next token is an identifier. */
6821 if (is_attribute_list == id_attr
6822 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6824 cp_token *token;
6826 /* Consume the identifier. */
6827 token = cp_lexer_consume_token (parser->lexer);
6828 /* Save the identifier. */
6829 identifier = token->u.value;
6831 else
6833 bool expr_non_constant_p;
6835 /* Parse the next assignment-expression. */
6836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6838 /* A braced-init-list. */
6839 cp_lexer_set_source_position (parser->lexer);
6840 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6841 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6842 if (non_constant_p && expr_non_constant_p)
6843 *non_constant_p = true;
6845 else if (non_constant_p)
6847 expr = (cp_parser_constant_expression
6848 (parser, /*allow_non_constant_p=*/true,
6849 &expr_non_constant_p));
6850 if (expr_non_constant_p)
6851 *non_constant_p = true;
6853 else
6855 expr = NULL_TREE;
6856 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6857 switch (tok->type)
6859 case CPP_NUMBER:
6860 case CPP_CHAR:
6861 case CPP_WCHAR:
6862 case CPP_CHAR16:
6863 case CPP_CHAR32:
6864 /* If a parameter is literal zero alone, remember it
6865 for -Wmemset-transposed-args warning. */
6866 if (integer_zerop (tok->u.value)
6867 && !TREE_OVERFLOW (tok->u.value)
6868 && want_literal_zero_p
6869 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6870 == CPP_COMMA
6871 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6872 == CPP_CLOSE_PAREN))
6874 unsigned int i;
6875 for (i = 0; i < itk_none; ++i)
6876 if (TREE_TYPE (tok->u.value) == integer_types[i])
6877 break;
6878 if (i < itk_none && literal_zeros[i])
6879 expr = literal_zeros[i];
6880 else
6882 expr = copy_node (tok->u.value);
6883 LITERAL_ZERO_P (expr) = 1;
6884 if (i < itk_none)
6885 literal_zeros[i] = expr;
6887 /* Consume the 0 token (or '\0', 0LL etc.). */
6888 cp_lexer_consume_token (parser->lexer);
6890 break;
6891 default:
6892 break;
6894 if (expr == NULL_TREE)
6895 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6896 cast_p);
6899 if (fold_expr_p)
6900 expr = fold_non_dependent_expr (expr);
6902 /* If we have an ellipsis, then this is an expression
6903 expansion. */
6904 if (allow_expansion_p
6905 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6907 /* Consume the `...'. */
6908 cp_lexer_consume_token (parser->lexer);
6910 /* Build the argument pack. */
6911 expr = make_pack_expansion (expr);
6914 /* Add it to the list. We add error_mark_node
6915 expressions to the list, so that we can still tell if
6916 the correct form for a parenthesized expression-list
6917 is found. That gives better errors. */
6918 vec_safe_push (expression_list, expr);
6920 if (expr == error_mark_node)
6921 goto skip_comma;
6924 /* After the first item, attribute lists look the same as
6925 expression lists. */
6926 is_attribute_list = non_attr;
6928 get_comma:;
6929 /* If the next token isn't a `,', then we are done. */
6930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6931 break;
6933 /* Otherwise, consume the `,' and keep going. */
6934 cp_lexer_consume_token (parser->lexer);
6937 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6939 int ending;
6941 skip_comma:;
6942 /* We try and resync to an unnested comma, as that will give the
6943 user better diagnostics. */
6944 ending = cp_parser_skip_to_closing_parenthesis (parser,
6945 /*recovering=*/true,
6946 /*or_comma=*/true,
6947 /*consume_paren=*/true);
6948 if (ending < 0)
6949 goto get_comma;
6950 if (!ending)
6952 parser->greater_than_is_operator_p
6953 = saved_greater_than_is_operator_p;
6954 return NULL;
6958 parser->greater_than_is_operator_p
6959 = saved_greater_than_is_operator_p;
6961 if (identifier)
6962 vec_safe_insert (expression_list, 0, identifier);
6964 return expression_list;
6967 /* Parse a pseudo-destructor-name.
6969 pseudo-destructor-name:
6970 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6971 :: [opt] nested-name-specifier template template-id :: ~ type-name
6972 :: [opt] nested-name-specifier [opt] ~ type-name
6974 If either of the first two productions is used, sets *SCOPE to the
6975 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6976 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6977 or ERROR_MARK_NODE if the parse fails. */
6979 static void
6980 cp_parser_pseudo_destructor_name (cp_parser* parser,
6981 tree object,
6982 tree* scope,
6983 tree* type)
6985 bool nested_name_specifier_p;
6987 /* Handle ~auto. */
6988 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6989 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6990 && !type_dependent_expression_p (object))
6992 if (cxx_dialect < cxx14)
6993 pedwarn (input_location, 0,
6994 "%<~auto%> only available with "
6995 "-std=c++14 or -std=gnu++14");
6996 cp_lexer_consume_token (parser->lexer);
6997 cp_lexer_consume_token (parser->lexer);
6998 *scope = NULL_TREE;
6999 *type = TREE_TYPE (object);
7000 return;
7003 /* Assume that things will not work out. */
7004 *type = error_mark_node;
7006 /* Look for the optional `::' operator. */
7007 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7008 /* Look for the optional nested-name-specifier. */
7009 nested_name_specifier_p
7010 = (cp_parser_nested_name_specifier_opt (parser,
7011 /*typename_keyword_p=*/false,
7012 /*check_dependency_p=*/true,
7013 /*type_p=*/false,
7014 /*is_declaration=*/false)
7015 != NULL_TREE);
7016 /* Now, if we saw a nested-name-specifier, we might be doing the
7017 second production. */
7018 if (nested_name_specifier_p
7019 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7021 /* Consume the `template' keyword. */
7022 cp_lexer_consume_token (parser->lexer);
7023 /* Parse the template-id. */
7024 cp_parser_template_id (parser,
7025 /*template_keyword_p=*/true,
7026 /*check_dependency_p=*/false,
7027 class_type,
7028 /*is_declaration=*/true);
7029 /* Look for the `::' token. */
7030 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7032 /* If the next token is not a `~', then there might be some
7033 additional qualification. */
7034 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7036 /* At this point, we're looking for "type-name :: ~". The type-name
7037 must not be a class-name, since this is a pseudo-destructor. So,
7038 it must be either an enum-name, or a typedef-name -- both of which
7039 are just identifiers. So, we peek ahead to check that the "::"
7040 and "~" tokens are present; if they are not, then we can avoid
7041 calling type_name. */
7042 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7043 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7044 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7046 cp_parser_error (parser, "non-scalar type");
7047 return;
7050 /* Look for the type-name. */
7051 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7052 if (*scope == error_mark_node)
7053 return;
7055 /* Look for the `::' token. */
7056 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7058 else
7059 *scope = NULL_TREE;
7061 /* Look for the `~'. */
7062 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7064 /* Once we see the ~, this has to be a pseudo-destructor. */
7065 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7066 cp_parser_commit_to_topmost_tentative_parse (parser);
7068 /* Look for the type-name again. We are not responsible for
7069 checking that it matches the first type-name. */
7070 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7073 /* Parse a unary-expression.
7075 unary-expression:
7076 postfix-expression
7077 ++ cast-expression
7078 -- cast-expression
7079 unary-operator cast-expression
7080 sizeof unary-expression
7081 sizeof ( type-id )
7082 alignof ( type-id ) [C++0x]
7083 new-expression
7084 delete-expression
7086 GNU Extensions:
7088 unary-expression:
7089 __extension__ cast-expression
7090 __alignof__ unary-expression
7091 __alignof__ ( type-id )
7092 alignof unary-expression [C++0x]
7093 __real__ cast-expression
7094 __imag__ cast-expression
7095 && identifier
7096 sizeof ( type-id ) { initializer-list , [opt] }
7097 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7098 __alignof__ ( type-id ) { initializer-list , [opt] }
7100 ADDRESS_P is true iff the unary-expression is appearing as the
7101 operand of the `&' operator. CAST_P is true if this expression is
7102 the target of a cast.
7104 Returns a representation of the expression. */
7106 static tree
7107 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7108 bool address_p, bool cast_p, bool decltype_p)
7110 cp_token *token;
7111 enum tree_code unary_operator;
7113 /* Peek at the next token. */
7114 token = cp_lexer_peek_token (parser->lexer);
7115 /* Some keywords give away the kind of expression. */
7116 if (token->type == CPP_KEYWORD)
7118 enum rid keyword = token->keyword;
7120 switch (keyword)
7122 case RID_ALIGNOF:
7123 case RID_SIZEOF:
7125 tree operand, ret;
7126 enum tree_code op;
7127 location_t first_loc;
7129 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7130 /* Consume the token. */
7131 cp_lexer_consume_token (parser->lexer);
7132 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7133 /* Parse the operand. */
7134 operand = cp_parser_sizeof_operand (parser, keyword);
7136 if (TYPE_P (operand))
7137 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7138 else
7140 /* ISO C++ defines alignof only with types, not with
7141 expressions. So pedwarn if alignof is used with a non-
7142 type expression. However, __alignof__ is ok. */
7143 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7144 pedwarn (token->location, OPT_Wpedantic,
7145 "ISO C++ does not allow %<alignof%> "
7146 "with a non-type");
7148 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7150 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7151 SIZEOF_EXPR with the original operand. */
7152 if (op == SIZEOF_EXPR && ret != error_mark_node)
7154 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7156 if (!processing_template_decl && TYPE_P (operand))
7158 ret = build_min (SIZEOF_EXPR, size_type_node,
7159 build1 (NOP_EXPR, operand,
7160 error_mark_node));
7161 SIZEOF_EXPR_TYPE_P (ret) = 1;
7163 else
7164 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7165 TREE_SIDE_EFFECTS (ret) = 0;
7166 TREE_READONLY (ret) = 1;
7168 SET_EXPR_LOCATION (ret, first_loc);
7170 return ret;
7173 case RID_NEW:
7174 return cp_parser_new_expression (parser);
7176 case RID_DELETE:
7177 return cp_parser_delete_expression (parser);
7179 case RID_EXTENSION:
7181 /* The saved value of the PEDANTIC flag. */
7182 int saved_pedantic;
7183 tree expr;
7185 /* Save away the PEDANTIC flag. */
7186 cp_parser_extension_opt (parser, &saved_pedantic);
7187 /* Parse the cast-expression. */
7188 expr = cp_parser_simple_cast_expression (parser);
7189 /* Restore the PEDANTIC flag. */
7190 pedantic = saved_pedantic;
7192 return expr;
7195 case RID_REALPART:
7196 case RID_IMAGPART:
7198 tree expression;
7200 /* Consume the `__real__' or `__imag__' token. */
7201 cp_lexer_consume_token (parser->lexer);
7202 /* Parse the cast-expression. */
7203 expression = cp_parser_simple_cast_expression (parser);
7204 /* Create the complete representation. */
7205 return build_x_unary_op (token->location,
7206 (keyword == RID_REALPART
7207 ? REALPART_EXPR : IMAGPART_EXPR),
7208 expression,
7209 tf_warning_or_error);
7211 break;
7213 case RID_TRANSACTION_ATOMIC:
7214 case RID_TRANSACTION_RELAXED:
7215 return cp_parser_transaction_expression (parser, keyword);
7217 case RID_NOEXCEPT:
7219 tree expr;
7220 const char *saved_message;
7221 bool saved_integral_constant_expression_p;
7222 bool saved_non_integral_constant_expression_p;
7223 bool saved_greater_than_is_operator_p;
7225 cp_lexer_consume_token (parser->lexer);
7226 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7228 saved_message = parser->type_definition_forbidden_message;
7229 parser->type_definition_forbidden_message
7230 = G_("types may not be defined in %<noexcept%> expressions");
7232 saved_integral_constant_expression_p
7233 = parser->integral_constant_expression_p;
7234 saved_non_integral_constant_expression_p
7235 = parser->non_integral_constant_expression_p;
7236 parser->integral_constant_expression_p = false;
7238 saved_greater_than_is_operator_p
7239 = parser->greater_than_is_operator_p;
7240 parser->greater_than_is_operator_p = true;
7242 ++cp_unevaluated_operand;
7243 ++c_inhibit_evaluation_warnings;
7244 ++cp_noexcept_operand;
7245 expr = cp_parser_expression (parser);
7246 --cp_noexcept_operand;
7247 --c_inhibit_evaluation_warnings;
7248 --cp_unevaluated_operand;
7250 parser->greater_than_is_operator_p
7251 = saved_greater_than_is_operator_p;
7253 parser->integral_constant_expression_p
7254 = saved_integral_constant_expression_p;
7255 parser->non_integral_constant_expression_p
7256 = saved_non_integral_constant_expression_p;
7258 parser->type_definition_forbidden_message = saved_message;
7260 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7261 return finish_noexcept_expr (expr, tf_warning_or_error);
7264 default:
7265 break;
7269 /* Look for the `:: new' and `:: delete', which also signal the
7270 beginning of a new-expression, or delete-expression,
7271 respectively. If the next token is `::', then it might be one of
7272 these. */
7273 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7275 enum rid keyword;
7277 /* See if the token after the `::' is one of the keywords in
7278 which we're interested. */
7279 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7280 /* If it's `new', we have a new-expression. */
7281 if (keyword == RID_NEW)
7282 return cp_parser_new_expression (parser);
7283 /* Similarly, for `delete'. */
7284 else if (keyword == RID_DELETE)
7285 return cp_parser_delete_expression (parser);
7288 /* Look for a unary operator. */
7289 unary_operator = cp_parser_unary_operator (token);
7290 /* The `++' and `--' operators can be handled similarly, even though
7291 they are not technically unary-operators in the grammar. */
7292 if (unary_operator == ERROR_MARK)
7294 if (token->type == CPP_PLUS_PLUS)
7295 unary_operator = PREINCREMENT_EXPR;
7296 else if (token->type == CPP_MINUS_MINUS)
7297 unary_operator = PREDECREMENT_EXPR;
7298 /* Handle the GNU address-of-label extension. */
7299 else if (cp_parser_allow_gnu_extensions_p (parser)
7300 && token->type == CPP_AND_AND)
7302 tree identifier;
7303 tree expression;
7304 location_t loc = token->location;
7306 /* Consume the '&&' token. */
7307 cp_lexer_consume_token (parser->lexer);
7308 /* Look for the identifier. */
7309 identifier = cp_parser_identifier (parser);
7310 /* Create an expression representing the address. */
7311 expression = finish_label_address_expr (identifier, loc);
7312 if (cp_parser_non_integral_constant_expression (parser,
7313 NIC_ADDR_LABEL))
7314 expression = error_mark_node;
7315 return expression;
7318 if (unary_operator != ERROR_MARK)
7320 tree cast_expression;
7321 tree expression = error_mark_node;
7322 non_integral_constant non_constant_p = NIC_NONE;
7323 location_t loc = token->location;
7324 tsubst_flags_t complain = complain_flags (decltype_p);
7326 /* Consume the operator token. */
7327 token = cp_lexer_consume_token (parser->lexer);
7328 /* Parse the cast-expression. */
7329 cast_expression
7330 = cp_parser_cast_expression (parser,
7331 unary_operator == ADDR_EXPR,
7332 /*cast_p=*/false,
7333 /*decltype*/false,
7334 pidk);
7335 /* Now, build an appropriate representation. */
7336 switch (unary_operator)
7338 case INDIRECT_REF:
7339 non_constant_p = NIC_STAR;
7340 expression = build_x_indirect_ref (loc, cast_expression,
7341 RO_UNARY_STAR,
7342 complain);
7343 break;
7345 case ADDR_EXPR:
7346 non_constant_p = NIC_ADDR;
7347 /* Fall through. */
7348 case BIT_NOT_EXPR:
7349 expression = build_x_unary_op (loc, unary_operator,
7350 cast_expression,
7351 complain);
7352 break;
7354 case PREINCREMENT_EXPR:
7355 case PREDECREMENT_EXPR:
7356 non_constant_p = unary_operator == PREINCREMENT_EXPR
7357 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7358 /* Fall through. */
7359 case UNARY_PLUS_EXPR:
7360 case NEGATE_EXPR:
7361 case TRUTH_NOT_EXPR:
7362 expression = finish_unary_op_expr (loc, unary_operator,
7363 cast_expression, complain);
7364 break;
7366 default:
7367 gcc_unreachable ();
7370 if (non_constant_p != NIC_NONE
7371 && cp_parser_non_integral_constant_expression (parser,
7372 non_constant_p))
7373 expression = error_mark_node;
7375 return expression;
7378 return cp_parser_postfix_expression (parser, address_p, cast_p,
7379 /*member_access_only_p=*/false,
7380 decltype_p,
7381 pidk);
7384 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7385 unary-operator, the corresponding tree code is returned. */
7387 static enum tree_code
7388 cp_parser_unary_operator (cp_token* token)
7390 switch (token->type)
7392 case CPP_MULT:
7393 return INDIRECT_REF;
7395 case CPP_AND:
7396 return ADDR_EXPR;
7398 case CPP_PLUS:
7399 return UNARY_PLUS_EXPR;
7401 case CPP_MINUS:
7402 return NEGATE_EXPR;
7404 case CPP_NOT:
7405 return TRUTH_NOT_EXPR;
7407 case CPP_COMPL:
7408 return BIT_NOT_EXPR;
7410 default:
7411 return ERROR_MARK;
7415 /* Parse a new-expression.
7417 new-expression:
7418 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7419 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7421 Returns a representation of the expression. */
7423 static tree
7424 cp_parser_new_expression (cp_parser* parser)
7426 bool global_scope_p;
7427 vec<tree, va_gc> *placement;
7428 tree type;
7429 vec<tree, va_gc> *initializer;
7430 tree nelts = NULL_TREE;
7431 tree ret;
7433 /* Look for the optional `::' operator. */
7434 global_scope_p
7435 = (cp_parser_global_scope_opt (parser,
7436 /*current_scope_valid_p=*/false)
7437 != NULL_TREE);
7438 /* Look for the `new' operator. */
7439 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7440 /* There's no easy way to tell a new-placement from the
7441 `( type-id )' construct. */
7442 cp_parser_parse_tentatively (parser);
7443 /* Look for a new-placement. */
7444 placement = cp_parser_new_placement (parser);
7445 /* If that didn't work out, there's no new-placement. */
7446 if (!cp_parser_parse_definitely (parser))
7448 if (placement != NULL)
7449 release_tree_vector (placement);
7450 placement = NULL;
7453 /* If the next token is a `(', then we have a parenthesized
7454 type-id. */
7455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7457 cp_token *token;
7458 const char *saved_message = parser->type_definition_forbidden_message;
7460 /* Consume the `('. */
7461 cp_lexer_consume_token (parser->lexer);
7463 /* Parse the type-id. */
7464 parser->type_definition_forbidden_message
7465 = G_("types may not be defined in a new-expression");
7466 type = cp_parser_type_id (parser);
7467 parser->type_definition_forbidden_message = saved_message;
7469 /* Look for the closing `)'. */
7470 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7471 token = cp_lexer_peek_token (parser->lexer);
7472 /* There should not be a direct-new-declarator in this production,
7473 but GCC used to allowed this, so we check and emit a sensible error
7474 message for this case. */
7475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7477 error_at (token->location,
7478 "array bound forbidden after parenthesized type-id");
7479 inform (token->location,
7480 "try removing the parentheses around the type-id");
7481 cp_parser_direct_new_declarator (parser);
7484 /* Otherwise, there must be a new-type-id. */
7485 else
7486 type = cp_parser_new_type_id (parser, &nelts);
7488 /* If the next token is a `(' or '{', then we have a new-initializer. */
7489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7490 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7491 initializer = cp_parser_new_initializer (parser);
7492 else
7493 initializer = NULL;
7495 /* A new-expression may not appear in an integral constant
7496 expression. */
7497 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7498 ret = error_mark_node;
7499 else
7501 /* Create a representation of the new-expression. */
7502 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7503 tf_warning_or_error);
7506 if (placement != NULL)
7507 release_tree_vector (placement);
7508 if (initializer != NULL)
7509 release_tree_vector (initializer);
7511 return ret;
7514 /* Parse a new-placement.
7516 new-placement:
7517 ( expression-list )
7519 Returns the same representation as for an expression-list. */
7521 static vec<tree, va_gc> *
7522 cp_parser_new_placement (cp_parser* parser)
7524 vec<tree, va_gc> *expression_list;
7526 /* Parse the expression-list. */
7527 expression_list = (cp_parser_parenthesized_expression_list
7528 (parser, non_attr, /*cast_p=*/false,
7529 /*allow_expansion_p=*/true,
7530 /*non_constant_p=*/NULL));
7532 return expression_list;
7535 /* Parse a new-type-id.
7537 new-type-id:
7538 type-specifier-seq new-declarator [opt]
7540 Returns the TYPE allocated. If the new-type-id indicates an array
7541 type, *NELTS is set to the number of elements in the last array
7542 bound; the TYPE will not include the last array bound. */
7544 static tree
7545 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7547 cp_decl_specifier_seq type_specifier_seq;
7548 cp_declarator *new_declarator;
7549 cp_declarator *declarator;
7550 cp_declarator *outer_declarator;
7551 const char *saved_message;
7553 /* The type-specifier sequence must not contain type definitions.
7554 (It cannot contain declarations of new types either, but if they
7555 are not definitions we will catch that because they are not
7556 complete.) */
7557 saved_message = parser->type_definition_forbidden_message;
7558 parser->type_definition_forbidden_message
7559 = G_("types may not be defined in a new-type-id");
7560 /* Parse the type-specifier-seq. */
7561 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7562 /*is_trailing_return=*/false,
7563 &type_specifier_seq);
7564 /* Restore the old message. */
7565 parser->type_definition_forbidden_message = saved_message;
7567 if (type_specifier_seq.type == error_mark_node)
7568 return error_mark_node;
7570 /* Parse the new-declarator. */
7571 new_declarator = cp_parser_new_declarator_opt (parser);
7573 /* Determine the number of elements in the last array dimension, if
7574 any. */
7575 *nelts = NULL_TREE;
7576 /* Skip down to the last array dimension. */
7577 declarator = new_declarator;
7578 outer_declarator = NULL;
7579 while (declarator && (declarator->kind == cdk_pointer
7580 || declarator->kind == cdk_ptrmem))
7582 outer_declarator = declarator;
7583 declarator = declarator->declarator;
7585 while (declarator
7586 && declarator->kind == cdk_array
7587 && declarator->declarator
7588 && declarator->declarator->kind == cdk_array)
7590 outer_declarator = declarator;
7591 declarator = declarator->declarator;
7594 if (declarator && declarator->kind == cdk_array)
7596 *nelts = declarator->u.array.bounds;
7597 if (*nelts == error_mark_node)
7598 *nelts = integer_one_node;
7600 if (outer_declarator)
7601 outer_declarator->declarator = declarator->declarator;
7602 else
7603 new_declarator = NULL;
7606 return groktypename (&type_specifier_seq, new_declarator, false);
7609 /* Parse an (optional) new-declarator.
7611 new-declarator:
7612 ptr-operator new-declarator [opt]
7613 direct-new-declarator
7615 Returns the declarator. */
7617 static cp_declarator *
7618 cp_parser_new_declarator_opt (cp_parser* parser)
7620 enum tree_code code;
7621 tree type, std_attributes = NULL_TREE;
7622 cp_cv_quals cv_quals;
7624 /* We don't know if there's a ptr-operator next, or not. */
7625 cp_parser_parse_tentatively (parser);
7626 /* Look for a ptr-operator. */
7627 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7628 /* If that worked, look for more new-declarators. */
7629 if (cp_parser_parse_definitely (parser))
7631 cp_declarator *declarator;
7633 /* Parse another optional declarator. */
7634 declarator = cp_parser_new_declarator_opt (parser);
7636 declarator = cp_parser_make_indirect_declarator
7637 (code, type, cv_quals, declarator, std_attributes);
7639 return declarator;
7642 /* If the next token is a `[', there is a direct-new-declarator. */
7643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7644 return cp_parser_direct_new_declarator (parser);
7646 return NULL;
7649 /* Parse a direct-new-declarator.
7651 direct-new-declarator:
7652 [ expression ]
7653 direct-new-declarator [constant-expression]
7657 static cp_declarator *
7658 cp_parser_direct_new_declarator (cp_parser* parser)
7660 cp_declarator *declarator = NULL;
7662 while (true)
7664 tree expression;
7665 cp_token *token;
7667 /* Look for the opening `['. */
7668 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7670 token = cp_lexer_peek_token (parser->lexer);
7671 expression = cp_parser_expression (parser);
7672 /* The standard requires that the expression have integral
7673 type. DR 74 adds enumeration types. We believe that the
7674 real intent is that these expressions be handled like the
7675 expression in a `switch' condition, which also allows
7676 classes with a single conversion to integral or
7677 enumeration type. */
7678 if (!processing_template_decl)
7680 expression
7681 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7682 expression,
7683 /*complain=*/true);
7684 if (!expression)
7686 error_at (token->location,
7687 "expression in new-declarator must have integral "
7688 "or enumeration type");
7689 expression = error_mark_node;
7693 /* Look for the closing `]'. */
7694 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7696 /* Add this bound to the declarator. */
7697 declarator = make_array_declarator (declarator, expression);
7699 /* If the next token is not a `[', then there are no more
7700 bounds. */
7701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7702 break;
7705 return declarator;
7708 /* Parse a new-initializer.
7710 new-initializer:
7711 ( expression-list [opt] )
7712 braced-init-list
7714 Returns a representation of the expression-list. */
7716 static vec<tree, va_gc> *
7717 cp_parser_new_initializer (cp_parser* parser)
7719 vec<tree, va_gc> *expression_list;
7721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7723 tree t;
7724 bool expr_non_constant_p;
7725 cp_lexer_set_source_position (parser->lexer);
7726 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7727 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7728 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7729 expression_list = make_tree_vector_single (t);
7731 else
7732 expression_list = (cp_parser_parenthesized_expression_list
7733 (parser, non_attr, /*cast_p=*/false,
7734 /*allow_expansion_p=*/true,
7735 /*non_constant_p=*/NULL));
7737 return expression_list;
7740 /* Parse a delete-expression.
7742 delete-expression:
7743 :: [opt] delete cast-expression
7744 :: [opt] delete [ ] cast-expression
7746 Returns a representation of the expression. */
7748 static tree
7749 cp_parser_delete_expression (cp_parser* parser)
7751 bool global_scope_p;
7752 bool array_p;
7753 tree expression;
7755 /* Look for the optional `::' operator. */
7756 global_scope_p
7757 = (cp_parser_global_scope_opt (parser,
7758 /*current_scope_valid_p=*/false)
7759 != NULL_TREE);
7760 /* Look for the `delete' keyword. */
7761 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7762 /* See if the array syntax is in use. */
7763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7765 /* Consume the `[' token. */
7766 cp_lexer_consume_token (parser->lexer);
7767 /* Look for the `]' token. */
7768 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7769 /* Remember that this is the `[]' construct. */
7770 array_p = true;
7772 else
7773 array_p = false;
7775 /* Parse the cast-expression. */
7776 expression = cp_parser_simple_cast_expression (parser);
7778 /* A delete-expression may not appear in an integral constant
7779 expression. */
7780 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7781 return error_mark_node;
7783 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7784 tf_warning_or_error);
7787 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7788 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7789 0 otherwise. */
7791 static int
7792 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7794 cp_token *token = cp_lexer_peek_token (parser->lexer);
7795 switch (token->type)
7797 case CPP_COMMA:
7798 case CPP_SEMICOLON:
7799 case CPP_QUERY:
7800 case CPP_COLON:
7801 case CPP_CLOSE_SQUARE:
7802 case CPP_CLOSE_PAREN:
7803 case CPP_CLOSE_BRACE:
7804 case CPP_OPEN_BRACE:
7805 case CPP_DOT:
7806 case CPP_DOT_STAR:
7807 case CPP_DEREF:
7808 case CPP_DEREF_STAR:
7809 case CPP_DIV:
7810 case CPP_MOD:
7811 case CPP_LSHIFT:
7812 case CPP_RSHIFT:
7813 case CPP_LESS:
7814 case CPP_GREATER:
7815 case CPP_LESS_EQ:
7816 case CPP_GREATER_EQ:
7817 case CPP_EQ_EQ:
7818 case CPP_NOT_EQ:
7819 case CPP_EQ:
7820 case CPP_MULT_EQ:
7821 case CPP_DIV_EQ:
7822 case CPP_MOD_EQ:
7823 case CPP_PLUS_EQ:
7824 case CPP_MINUS_EQ:
7825 case CPP_RSHIFT_EQ:
7826 case CPP_LSHIFT_EQ:
7827 case CPP_AND_EQ:
7828 case CPP_XOR_EQ:
7829 case CPP_OR_EQ:
7830 case CPP_XOR:
7831 case CPP_OR:
7832 case CPP_OR_OR:
7833 case CPP_EOF:
7834 case CPP_ELLIPSIS:
7835 return 0;
7837 case CPP_OPEN_PAREN:
7838 /* In ((type ()) () the last () isn't a valid cast-expression,
7839 so the whole must be parsed as postfix-expression. */
7840 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7841 != CPP_CLOSE_PAREN;
7843 case CPP_OPEN_SQUARE:
7844 /* '[' may start a primary-expression in obj-c++ and in C++11,
7845 as a lambda-expression, eg, '(void)[]{}'. */
7846 if (cxx_dialect >= cxx11)
7847 return -1;
7848 return c_dialect_objc ();
7850 case CPP_PLUS_PLUS:
7851 case CPP_MINUS_MINUS:
7852 /* '++' and '--' may or may not start a cast-expression:
7854 struct T { void operator++(int); };
7855 void f() { (T())++; }
7859 int a;
7860 (int)++a; */
7861 return -1;
7863 default:
7864 return 1;
7868 /* Parse a cast-expression.
7870 cast-expression:
7871 unary-expression
7872 ( type-id ) cast-expression
7874 ADDRESS_P is true iff the unary-expression is appearing as the
7875 operand of the `&' operator. CAST_P is true if this expression is
7876 the target of a cast.
7878 Returns a representation of the expression. */
7880 static tree
7881 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7882 bool decltype_p, cp_id_kind * pidk)
7884 /* If it's a `(', then we might be looking at a cast. */
7885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7887 tree type = NULL_TREE;
7888 tree expr = NULL_TREE;
7889 int cast_expression = 0;
7890 const char *saved_message;
7892 /* There's no way to know yet whether or not this is a cast.
7893 For example, `(int (3))' is a unary-expression, while `(int)
7894 3' is a cast. So, we resort to parsing tentatively. */
7895 cp_parser_parse_tentatively (parser);
7896 /* Types may not be defined in a cast. */
7897 saved_message = parser->type_definition_forbidden_message;
7898 parser->type_definition_forbidden_message
7899 = G_("types may not be defined in casts");
7900 /* Consume the `('. */
7901 cp_lexer_consume_token (parser->lexer);
7902 /* A very tricky bit is that `(struct S) { 3 }' is a
7903 compound-literal (which we permit in C++ as an extension).
7904 But, that construct is not a cast-expression -- it is a
7905 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7906 is legal; if the compound-literal were a cast-expression,
7907 you'd need an extra set of parentheses.) But, if we parse
7908 the type-id, and it happens to be a class-specifier, then we
7909 will commit to the parse at that point, because we cannot
7910 undo the action that is done when creating a new class. So,
7911 then we cannot back up and do a postfix-expression.
7913 Another tricky case is the following (c++/29234):
7915 struct S { void operator () (); };
7917 void foo ()
7919 ( S()() );
7922 As a type-id we parse the parenthesized S()() as a function
7923 returning a function, groktypename complains and we cannot
7924 back up in this case either.
7926 Therefore, we scan ahead to the closing `)', and check to see
7927 if the tokens after the `)' can start a cast-expression. Otherwise
7928 we are dealing with an unary-expression, a postfix-expression
7929 or something else.
7931 Yet another tricky case, in C++11, is the following (c++/54891):
7933 (void)[]{};
7935 The issue is that usually, besides the case of lambda-expressions,
7936 the parenthesized type-id cannot be followed by '[', and, eg, we
7937 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7938 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7939 we don't commit, we try a cast-expression, then an unary-expression.
7941 Save tokens so that we can put them back. */
7942 cp_lexer_save_tokens (parser->lexer);
7944 /* We may be looking at a cast-expression. */
7945 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7946 /*consume_paren=*/true))
7947 cast_expression
7948 = cp_parser_tokens_start_cast_expression (parser);
7950 /* Roll back the tokens we skipped. */
7951 cp_lexer_rollback_tokens (parser->lexer);
7952 /* If we aren't looking at a cast-expression, simulate an error so
7953 that the call to cp_parser_error_occurred below returns true. */
7954 if (!cast_expression)
7955 cp_parser_simulate_error (parser);
7956 else
7958 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7959 parser->in_type_id_in_expr_p = true;
7960 /* Look for the type-id. */
7961 type = cp_parser_type_id (parser);
7962 /* Look for the closing `)'. */
7963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7964 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7967 /* Restore the saved message. */
7968 parser->type_definition_forbidden_message = saved_message;
7970 /* At this point this can only be either a cast or a
7971 parenthesized ctor such as `(T ())' that looks like a cast to
7972 function returning T. */
7973 if (!cp_parser_error_occurred (parser))
7975 /* Only commit if the cast-expression doesn't start with
7976 '++', '--', or '[' in C++11. */
7977 if (cast_expression > 0)
7978 cp_parser_commit_to_topmost_tentative_parse (parser);
7980 expr = cp_parser_cast_expression (parser,
7981 /*address_p=*/false,
7982 /*cast_p=*/true,
7983 /*decltype_p=*/false,
7984 pidk);
7986 if (cp_parser_parse_definitely (parser))
7988 /* Warn about old-style casts, if so requested. */
7989 if (warn_old_style_cast
7990 && !in_system_header_at (input_location)
7991 && !VOID_TYPE_P (type)
7992 && current_lang_name != lang_name_c)
7993 warning (OPT_Wold_style_cast, "use of old-style cast");
7995 /* Only type conversions to integral or enumeration types
7996 can be used in constant-expressions. */
7997 if (!cast_valid_in_integral_constant_expression_p (type)
7998 && cp_parser_non_integral_constant_expression (parser,
7999 NIC_CAST))
8000 return error_mark_node;
8002 /* Perform the cast. */
8003 expr = build_c_cast (input_location, type, expr);
8004 return expr;
8007 else
8008 cp_parser_abort_tentative_parse (parser);
8011 /* If we get here, then it's not a cast, so it must be a
8012 unary-expression. */
8013 return cp_parser_unary_expression (parser, pidk, address_p,
8014 cast_p, decltype_p);
8017 /* Parse a binary expression of the general form:
8019 pm-expression:
8020 cast-expression
8021 pm-expression .* cast-expression
8022 pm-expression ->* cast-expression
8024 multiplicative-expression:
8025 pm-expression
8026 multiplicative-expression * pm-expression
8027 multiplicative-expression / pm-expression
8028 multiplicative-expression % pm-expression
8030 additive-expression:
8031 multiplicative-expression
8032 additive-expression + multiplicative-expression
8033 additive-expression - multiplicative-expression
8035 shift-expression:
8036 additive-expression
8037 shift-expression << additive-expression
8038 shift-expression >> additive-expression
8040 relational-expression:
8041 shift-expression
8042 relational-expression < shift-expression
8043 relational-expression > shift-expression
8044 relational-expression <= shift-expression
8045 relational-expression >= shift-expression
8047 GNU Extension:
8049 relational-expression:
8050 relational-expression <? shift-expression
8051 relational-expression >? shift-expression
8053 equality-expression:
8054 relational-expression
8055 equality-expression == relational-expression
8056 equality-expression != relational-expression
8058 and-expression:
8059 equality-expression
8060 and-expression & equality-expression
8062 exclusive-or-expression:
8063 and-expression
8064 exclusive-or-expression ^ and-expression
8066 inclusive-or-expression:
8067 exclusive-or-expression
8068 inclusive-or-expression | exclusive-or-expression
8070 logical-and-expression:
8071 inclusive-or-expression
8072 logical-and-expression && inclusive-or-expression
8074 logical-or-expression:
8075 logical-and-expression
8076 logical-or-expression || logical-and-expression
8078 All these are implemented with a single function like:
8080 binary-expression:
8081 simple-cast-expression
8082 binary-expression <token> binary-expression
8084 CAST_P is true if this expression is the target of a cast.
8086 The binops_by_token map is used to get the tree codes for each <token> type.
8087 binary-expressions are associated according to a precedence table. */
8089 #define TOKEN_PRECEDENCE(token) \
8090 (((token->type == CPP_GREATER \
8091 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8092 && !parser->greater_than_is_operator_p) \
8093 ? PREC_NOT_OPERATOR \
8094 : binops_by_token[token->type].prec)
8096 static tree
8097 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8098 bool no_toplevel_fold_p,
8099 bool decltype_p,
8100 enum cp_parser_prec prec,
8101 cp_id_kind * pidk)
8103 cp_parser_expression_stack stack;
8104 cp_parser_expression_stack_entry *sp = &stack[0];
8105 cp_parser_expression_stack_entry current;
8106 tree rhs;
8107 cp_token *token;
8108 enum tree_code rhs_type;
8109 enum cp_parser_prec new_prec, lookahead_prec;
8110 tree overload;
8112 /* Parse the first expression. */
8113 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8114 ? TRUTH_NOT_EXPR : ERROR_MARK);
8115 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8116 cast_p, decltype_p, pidk);
8117 current.prec = prec;
8119 if (cp_parser_error_occurred (parser))
8120 return error_mark_node;
8122 for (;;)
8124 /* Get an operator token. */
8125 token = cp_lexer_peek_token (parser->lexer);
8127 if (warn_cxx0x_compat
8128 && token->type == CPP_RSHIFT
8129 && !parser->greater_than_is_operator_p)
8131 if (warning_at (token->location, OPT_Wc__0x_compat,
8132 "%<>>%> operator is treated"
8133 " as two right angle brackets in C++11"))
8134 inform (token->location,
8135 "suggest parentheses around %<>>%> expression");
8138 new_prec = TOKEN_PRECEDENCE (token);
8140 /* Popping an entry off the stack means we completed a subexpression:
8141 - either we found a token which is not an operator (`>' where it is not
8142 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8143 will happen repeatedly;
8144 - or, we found an operator which has lower priority. This is the case
8145 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8146 parsing `3 * 4'. */
8147 if (new_prec <= current.prec)
8149 if (sp == stack)
8150 break;
8151 else
8152 goto pop;
8155 get_rhs:
8156 current.tree_type = binops_by_token[token->type].tree_type;
8157 current.loc = token->location;
8159 /* We used the operator token. */
8160 cp_lexer_consume_token (parser->lexer);
8162 /* For "false && x" or "true || x", x will never be executed;
8163 disable warnings while evaluating it. */
8164 if (current.tree_type == TRUTH_ANDIF_EXPR)
8165 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8166 else if (current.tree_type == TRUTH_ORIF_EXPR)
8167 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8169 /* Extract another operand. It may be the RHS of this expression
8170 or the LHS of a new, higher priority expression. */
8171 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8172 ? TRUTH_NOT_EXPR : ERROR_MARK);
8173 rhs = cp_parser_simple_cast_expression (parser);
8175 /* Get another operator token. Look up its precedence to avoid
8176 building a useless (immediately popped) stack entry for common
8177 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8178 token = cp_lexer_peek_token (parser->lexer);
8179 lookahead_prec = TOKEN_PRECEDENCE (token);
8180 if (lookahead_prec > new_prec)
8182 /* ... and prepare to parse the RHS of the new, higher priority
8183 expression. Since precedence levels on the stack are
8184 monotonically increasing, we do not have to care about
8185 stack overflows. */
8186 *sp = current;
8187 ++sp;
8188 current.lhs = rhs;
8189 current.lhs_type = rhs_type;
8190 current.prec = new_prec;
8191 new_prec = lookahead_prec;
8192 goto get_rhs;
8194 pop:
8195 lookahead_prec = new_prec;
8196 /* If the stack is not empty, we have parsed into LHS the right side
8197 (`4' in the example above) of an expression we had suspended.
8198 We can use the information on the stack to recover the LHS (`3')
8199 from the stack together with the tree code (`MULT_EXPR'), and
8200 the precedence of the higher level subexpression
8201 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8202 which will be used to actually build the additive expression. */
8203 rhs = current.lhs;
8204 rhs_type = current.lhs_type;
8205 --sp;
8206 current = *sp;
8209 /* Undo the disabling of warnings done above. */
8210 if (current.tree_type == TRUTH_ANDIF_EXPR)
8211 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8212 else if (current.tree_type == TRUTH_ORIF_EXPR)
8213 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8215 if (warn_logical_not_paren
8216 && current.lhs_type == TRUTH_NOT_EXPR)
8217 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8219 overload = NULL;
8220 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8221 ERROR_MARK for everything that is not a binary expression.
8222 This makes warn_about_parentheses miss some warnings that
8223 involve unary operators. For unary expressions we should
8224 pass the correct tree_code unless the unary expression was
8225 surrounded by parentheses.
8227 if (no_toplevel_fold_p
8228 && lookahead_prec <= current.prec
8229 && sp == stack)
8230 current.lhs = build2 (current.tree_type,
8231 TREE_CODE_CLASS (current.tree_type)
8232 == tcc_comparison
8233 ? boolean_type_node : TREE_TYPE (current.lhs),
8234 current.lhs, rhs);
8235 else
8236 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8237 current.lhs, current.lhs_type,
8238 rhs, rhs_type, &overload,
8239 complain_flags (decltype_p));
8240 current.lhs_type = current.tree_type;
8241 if (EXPR_P (current.lhs))
8242 SET_EXPR_LOCATION (current.lhs, current.loc);
8244 /* If the binary operator required the use of an overloaded operator,
8245 then this expression cannot be an integral constant-expression.
8246 An overloaded operator can be used even if both operands are
8247 otherwise permissible in an integral constant-expression if at
8248 least one of the operands is of enumeration type. */
8250 if (overload
8251 && cp_parser_non_integral_constant_expression (parser,
8252 NIC_OVERLOADED))
8253 return error_mark_node;
8256 return current.lhs;
8259 static tree
8260 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8261 bool no_toplevel_fold_p,
8262 enum cp_parser_prec prec,
8263 cp_id_kind * pidk)
8265 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8266 /*decltype*/false, prec, pidk);
8269 /* Parse the `? expression : assignment-expression' part of a
8270 conditional-expression. The LOGICAL_OR_EXPR is the
8271 logical-or-expression that started the conditional-expression.
8272 Returns a representation of the entire conditional-expression.
8274 This routine is used by cp_parser_assignment_expression.
8276 ? expression : assignment-expression
8278 GNU Extensions:
8280 ? : assignment-expression */
8282 static tree
8283 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8285 tree expr;
8286 tree assignment_expr;
8287 struct cp_token *token;
8288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8290 /* Consume the `?' token. */
8291 cp_lexer_consume_token (parser->lexer);
8292 token = cp_lexer_peek_token (parser->lexer);
8293 if (cp_parser_allow_gnu_extensions_p (parser)
8294 && token->type == CPP_COLON)
8296 pedwarn (token->location, OPT_Wpedantic,
8297 "ISO C++ does not allow ?: with omitted middle operand");
8298 /* Implicit true clause. */
8299 expr = NULL_TREE;
8300 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8301 warn_for_omitted_condop (token->location, logical_or_expr);
8303 else
8305 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8306 parser->colon_corrects_to_scope_p = false;
8307 /* Parse the expression. */
8308 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8309 expr = cp_parser_expression (parser);
8310 c_inhibit_evaluation_warnings +=
8311 ((logical_or_expr == truthvalue_true_node)
8312 - (logical_or_expr == truthvalue_false_node));
8313 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8316 /* The next token should be a `:'. */
8317 cp_parser_require (parser, CPP_COLON, RT_COLON);
8318 /* Parse the assignment-expression. */
8319 assignment_expr = cp_parser_assignment_expression (parser);
8320 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8322 /* Build the conditional-expression. */
8323 return build_x_conditional_expr (loc, logical_or_expr,
8324 expr,
8325 assignment_expr,
8326 tf_warning_or_error);
8329 /* Parse an assignment-expression.
8331 assignment-expression:
8332 conditional-expression
8333 logical-or-expression assignment-operator assignment_expression
8334 throw-expression
8336 CAST_P is true if this expression is the target of a cast.
8337 DECLTYPE_P is true if this expression is the operand of decltype.
8339 Returns a representation for the expression. */
8341 static tree
8342 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8343 bool cast_p, bool decltype_p)
8345 tree expr;
8347 /* If the next token is the `throw' keyword, then we're looking at
8348 a throw-expression. */
8349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8350 expr = cp_parser_throw_expression (parser);
8351 /* Otherwise, it must be that we are looking at a
8352 logical-or-expression. */
8353 else
8355 /* Parse the binary expressions (logical-or-expression). */
8356 expr = cp_parser_binary_expression (parser, cast_p, false,
8357 decltype_p,
8358 PREC_NOT_OPERATOR, pidk);
8359 /* If the next token is a `?' then we're actually looking at a
8360 conditional-expression. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8362 return cp_parser_question_colon_clause (parser, expr);
8363 else
8365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8367 /* If it's an assignment-operator, we're using the second
8368 production. */
8369 enum tree_code assignment_operator
8370 = cp_parser_assignment_operator_opt (parser);
8371 if (assignment_operator != ERROR_MARK)
8373 bool non_constant_p;
8374 location_t saved_input_location;
8376 /* Parse the right-hand side of the assignment. */
8377 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8379 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8380 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8382 /* An assignment may not appear in a
8383 constant-expression. */
8384 if (cp_parser_non_integral_constant_expression (parser,
8385 NIC_ASSIGNMENT))
8386 return error_mark_node;
8387 /* Build the assignment expression. Its default
8388 location is the location of the '=' token. */
8389 saved_input_location = input_location;
8390 input_location = loc;
8391 expr = build_x_modify_expr (loc, expr,
8392 assignment_operator,
8393 rhs,
8394 complain_flags (decltype_p));
8395 input_location = saved_input_location;
8400 return expr;
8403 /* Parse an (optional) assignment-operator.
8405 assignment-operator: one of
8406 = *= /= %= += -= >>= <<= &= ^= |=
8408 GNU Extension:
8410 assignment-operator: one of
8411 <?= >?=
8413 If the next token is an assignment operator, the corresponding tree
8414 code is returned, and the token is consumed. For example, for
8415 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8416 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8417 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8418 operator, ERROR_MARK is returned. */
8420 static enum tree_code
8421 cp_parser_assignment_operator_opt (cp_parser* parser)
8423 enum tree_code op;
8424 cp_token *token;
8426 /* Peek at the next token. */
8427 token = cp_lexer_peek_token (parser->lexer);
8429 switch (token->type)
8431 case CPP_EQ:
8432 op = NOP_EXPR;
8433 break;
8435 case CPP_MULT_EQ:
8436 op = MULT_EXPR;
8437 break;
8439 case CPP_DIV_EQ:
8440 op = TRUNC_DIV_EXPR;
8441 break;
8443 case CPP_MOD_EQ:
8444 op = TRUNC_MOD_EXPR;
8445 break;
8447 case CPP_PLUS_EQ:
8448 op = PLUS_EXPR;
8449 break;
8451 case CPP_MINUS_EQ:
8452 op = MINUS_EXPR;
8453 break;
8455 case CPP_RSHIFT_EQ:
8456 op = RSHIFT_EXPR;
8457 break;
8459 case CPP_LSHIFT_EQ:
8460 op = LSHIFT_EXPR;
8461 break;
8463 case CPP_AND_EQ:
8464 op = BIT_AND_EXPR;
8465 break;
8467 case CPP_XOR_EQ:
8468 op = BIT_XOR_EXPR;
8469 break;
8471 case CPP_OR_EQ:
8472 op = BIT_IOR_EXPR;
8473 break;
8475 default:
8476 /* Nothing else is an assignment operator. */
8477 op = ERROR_MARK;
8480 /* If it was an assignment operator, consume it. */
8481 if (op != ERROR_MARK)
8482 cp_lexer_consume_token (parser->lexer);
8484 return op;
8487 /* Parse an expression.
8489 expression:
8490 assignment-expression
8491 expression , assignment-expression
8493 CAST_P is true if this expression is the target of a cast.
8494 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8495 except possibly parenthesized or on the RHS of a comma (N3276).
8497 Returns a representation of the expression. */
8499 static tree
8500 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8501 bool cast_p, bool decltype_p)
8503 tree expression = NULL_TREE;
8504 location_t loc = UNKNOWN_LOCATION;
8506 while (true)
8508 tree assignment_expression;
8510 /* Parse the next assignment-expression. */
8511 assignment_expression
8512 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8514 /* We don't create a temporary for a call that is the immediate operand
8515 of decltype or on the RHS of a comma. But when we see a comma, we
8516 need to create a temporary for a call on the LHS. */
8517 if (decltype_p && !processing_template_decl
8518 && TREE_CODE (assignment_expression) == CALL_EXPR
8519 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8520 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8521 assignment_expression
8522 = build_cplus_new (TREE_TYPE (assignment_expression),
8523 assignment_expression, tf_warning_or_error);
8525 /* If this is the first assignment-expression, we can just
8526 save it away. */
8527 if (!expression)
8528 expression = assignment_expression;
8529 else
8530 expression = build_x_compound_expr (loc, expression,
8531 assignment_expression,
8532 complain_flags (decltype_p));
8533 /* If the next token is not a comma, then we are done with the
8534 expression. */
8535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8536 break;
8537 /* Consume the `,'. */
8538 loc = cp_lexer_peek_token (parser->lexer)->location;
8539 cp_lexer_consume_token (parser->lexer);
8540 /* A comma operator cannot appear in a constant-expression. */
8541 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8542 expression = error_mark_node;
8545 return expression;
8548 /* Parse a constant-expression.
8550 constant-expression:
8551 conditional-expression
8553 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8554 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8555 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8556 is false, NON_CONSTANT_P should be NULL. */
8558 static tree
8559 cp_parser_constant_expression (cp_parser* parser,
8560 bool allow_non_constant_p,
8561 bool *non_constant_p)
8563 bool saved_integral_constant_expression_p;
8564 bool saved_allow_non_integral_constant_expression_p;
8565 bool saved_non_integral_constant_expression_p;
8566 tree expression;
8568 /* It might seem that we could simply parse the
8569 conditional-expression, and then check to see if it were
8570 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8571 one that the compiler can figure out is constant, possibly after
8572 doing some simplifications or optimizations. The standard has a
8573 precise definition of constant-expression, and we must honor
8574 that, even though it is somewhat more restrictive.
8576 For example:
8578 int i[(2, 3)];
8580 is not a legal declaration, because `(2, 3)' is not a
8581 constant-expression. The `,' operator is forbidden in a
8582 constant-expression. However, GCC's constant-folding machinery
8583 will fold this operation to an INTEGER_CST for `3'. */
8585 /* Save the old settings. */
8586 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8587 saved_allow_non_integral_constant_expression_p
8588 = parser->allow_non_integral_constant_expression_p;
8589 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8590 /* We are now parsing a constant-expression. */
8591 parser->integral_constant_expression_p = true;
8592 parser->allow_non_integral_constant_expression_p
8593 = (allow_non_constant_p || cxx_dialect >= cxx11);
8594 parser->non_integral_constant_expression_p = false;
8595 /* Although the grammar says "conditional-expression", we parse an
8596 "assignment-expression", which also permits "throw-expression"
8597 and the use of assignment operators. In the case that
8598 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8599 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8600 actually essential that we look for an assignment-expression.
8601 For example, cp_parser_initializer_clauses uses this function to
8602 determine whether a particular assignment-expression is in fact
8603 constant. */
8604 expression = cp_parser_assignment_expression (parser);
8605 /* Restore the old settings. */
8606 parser->integral_constant_expression_p
8607 = saved_integral_constant_expression_p;
8608 parser->allow_non_integral_constant_expression_p
8609 = saved_allow_non_integral_constant_expression_p;
8610 if (cxx_dialect >= cxx11)
8612 /* Require an rvalue constant expression here; that's what our
8613 callers expect. Reference constant expressions are handled
8614 separately in e.g. cp_parser_template_argument. */
8615 bool is_const = potential_rvalue_constant_expression (expression);
8616 parser->non_integral_constant_expression_p = !is_const;
8617 if (!is_const && !allow_non_constant_p)
8618 require_potential_rvalue_constant_expression (expression);
8620 if (allow_non_constant_p)
8621 *non_constant_p = parser->non_integral_constant_expression_p;
8622 parser->non_integral_constant_expression_p
8623 = saved_non_integral_constant_expression_p;
8625 return expression;
8628 /* Parse __builtin_offsetof.
8630 offsetof-expression:
8631 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8633 offsetof-member-designator:
8634 id-expression
8635 | offsetof-member-designator "." id-expression
8636 | offsetof-member-designator "[" expression "]"
8637 | offsetof-member-designator "->" id-expression */
8639 static tree
8640 cp_parser_builtin_offsetof (cp_parser *parser)
8642 int save_ice_p, save_non_ice_p;
8643 tree type, expr;
8644 cp_id_kind dummy;
8645 cp_token *token;
8647 /* We're about to accept non-integral-constant things, but will
8648 definitely yield an integral constant expression. Save and
8649 restore these values around our local parsing. */
8650 save_ice_p = parser->integral_constant_expression_p;
8651 save_non_ice_p = parser->non_integral_constant_expression_p;
8653 /* Consume the "__builtin_offsetof" token. */
8654 cp_lexer_consume_token (parser->lexer);
8655 /* Consume the opening `('. */
8656 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8657 /* Parse the type-id. */
8658 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8659 type = cp_parser_type_id (parser);
8660 /* Look for the `,'. */
8661 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8662 token = cp_lexer_peek_token (parser->lexer);
8664 /* Build the (type *)null that begins the traditional offsetof macro. */
8665 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8666 tf_warning_or_error);
8668 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8669 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8670 true, &dummy, token->location);
8671 while (true)
8673 token = cp_lexer_peek_token (parser->lexer);
8674 switch (token->type)
8676 case CPP_OPEN_SQUARE:
8677 /* offsetof-member-designator "[" expression "]" */
8678 expr = cp_parser_postfix_open_square_expression (parser, expr,
8679 true, false);
8680 break;
8682 case CPP_DEREF:
8683 /* offsetof-member-designator "->" identifier */
8684 expr = grok_array_decl (token->location, expr,
8685 integer_zero_node, false);
8686 /* FALLTHRU */
8688 case CPP_DOT:
8689 /* offsetof-member-designator "." identifier */
8690 cp_lexer_consume_token (parser->lexer);
8691 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8692 expr, true, &dummy,
8693 token->location);
8694 break;
8696 case CPP_CLOSE_PAREN:
8697 /* Consume the ")" token. */
8698 cp_lexer_consume_token (parser->lexer);
8699 goto success;
8701 default:
8702 /* Error. We know the following require will fail, but
8703 that gives the proper error message. */
8704 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8705 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8706 expr = error_mark_node;
8707 goto failure;
8711 success:
8712 /* If we're processing a template, we can't finish the semantics yet.
8713 Otherwise we can fold the entire expression now. */
8714 if (processing_template_decl)
8716 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8717 SET_EXPR_LOCATION (expr, loc);
8719 else
8720 expr = finish_offsetof (expr, loc);
8722 failure:
8723 parser->integral_constant_expression_p = save_ice_p;
8724 parser->non_integral_constant_expression_p = save_non_ice_p;
8726 return expr;
8729 /* Parse a trait expression.
8731 Returns a representation of the expression, the underlying type
8732 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8734 static tree
8735 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8737 cp_trait_kind kind;
8738 tree type1, type2 = NULL_TREE;
8739 bool binary = false;
8740 bool variadic = false;
8742 switch (keyword)
8744 case RID_HAS_NOTHROW_ASSIGN:
8745 kind = CPTK_HAS_NOTHROW_ASSIGN;
8746 break;
8747 case RID_HAS_NOTHROW_CONSTRUCTOR:
8748 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8749 break;
8750 case RID_HAS_NOTHROW_COPY:
8751 kind = CPTK_HAS_NOTHROW_COPY;
8752 break;
8753 case RID_HAS_TRIVIAL_ASSIGN:
8754 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8755 break;
8756 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8757 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8758 break;
8759 case RID_HAS_TRIVIAL_COPY:
8760 kind = CPTK_HAS_TRIVIAL_COPY;
8761 break;
8762 case RID_HAS_TRIVIAL_DESTRUCTOR:
8763 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8764 break;
8765 case RID_HAS_VIRTUAL_DESTRUCTOR:
8766 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8767 break;
8768 case RID_IS_ABSTRACT:
8769 kind = CPTK_IS_ABSTRACT;
8770 break;
8771 case RID_IS_BASE_OF:
8772 kind = CPTK_IS_BASE_OF;
8773 binary = true;
8774 break;
8775 case RID_IS_CLASS:
8776 kind = CPTK_IS_CLASS;
8777 break;
8778 case RID_IS_EMPTY:
8779 kind = CPTK_IS_EMPTY;
8780 break;
8781 case RID_IS_ENUM:
8782 kind = CPTK_IS_ENUM;
8783 break;
8784 case RID_IS_FINAL:
8785 kind = CPTK_IS_FINAL;
8786 break;
8787 case RID_IS_LITERAL_TYPE:
8788 kind = CPTK_IS_LITERAL_TYPE;
8789 break;
8790 case RID_IS_POD:
8791 kind = CPTK_IS_POD;
8792 break;
8793 case RID_IS_POLYMORPHIC:
8794 kind = CPTK_IS_POLYMORPHIC;
8795 break;
8796 case RID_IS_STD_LAYOUT:
8797 kind = CPTK_IS_STD_LAYOUT;
8798 break;
8799 case RID_IS_TRIVIAL:
8800 kind = CPTK_IS_TRIVIAL;
8801 break;
8802 case RID_IS_TRIVIALLY_ASSIGNABLE:
8803 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8804 binary = true;
8805 break;
8806 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8807 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8808 variadic = true;
8809 break;
8810 case RID_IS_TRIVIALLY_COPYABLE:
8811 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8812 break;
8813 case RID_IS_UNION:
8814 kind = CPTK_IS_UNION;
8815 break;
8816 case RID_UNDERLYING_TYPE:
8817 kind = CPTK_UNDERLYING_TYPE;
8818 break;
8819 case RID_BASES:
8820 kind = CPTK_BASES;
8821 break;
8822 case RID_DIRECT_BASES:
8823 kind = CPTK_DIRECT_BASES;
8824 break;
8825 default:
8826 gcc_unreachable ();
8829 /* Consume the token. */
8830 cp_lexer_consume_token (parser->lexer);
8832 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8834 type1 = cp_parser_type_id (parser);
8836 if (type1 == error_mark_node)
8837 return error_mark_node;
8839 if (binary)
8841 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8843 type2 = cp_parser_type_id (parser);
8845 if (type2 == error_mark_node)
8846 return error_mark_node;
8848 else if (variadic)
8850 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8852 cp_lexer_consume_token (parser->lexer);
8853 tree elt = cp_parser_type_id (parser);
8854 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8856 cp_lexer_consume_token (parser->lexer);
8857 elt = make_pack_expansion (elt);
8859 if (elt == error_mark_node)
8860 return error_mark_node;
8861 type2 = tree_cons (NULL_TREE, elt, type2);
8865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8867 /* Complete the trait expression, which may mean either processing
8868 the trait expr now or saving it for template instantiation. */
8869 switch(kind)
8871 case CPTK_UNDERLYING_TYPE:
8872 return finish_underlying_type (type1);
8873 case CPTK_BASES:
8874 return finish_bases (type1, false);
8875 case CPTK_DIRECT_BASES:
8876 return finish_bases (type1, true);
8877 default:
8878 return finish_trait_expr (kind, type1, type2);
8882 /* Lambdas that appear in variable initializer or default argument scope
8883 get that in their mangling, so we need to record it. We might as well
8884 use the count for function and namespace scopes as well. */
8885 static GTY(()) tree lambda_scope;
8886 static GTY(()) int lambda_count;
8887 typedef struct GTY(()) tree_int
8889 tree t;
8890 int i;
8891 } tree_int;
8892 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8894 static void
8895 start_lambda_scope (tree decl)
8897 tree_int ti;
8898 gcc_assert (decl);
8899 /* Once we're inside a function, we ignore other scopes and just push
8900 the function again so that popping works properly. */
8901 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8902 decl = current_function_decl;
8903 ti.t = lambda_scope;
8904 ti.i = lambda_count;
8905 vec_safe_push (lambda_scope_stack, ti);
8906 if (lambda_scope != decl)
8908 /* Don't reset the count if we're still in the same function. */
8909 lambda_scope = decl;
8910 lambda_count = 0;
8914 static void
8915 record_lambda_scope (tree lambda)
8917 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8918 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8921 static void
8922 finish_lambda_scope (void)
8924 tree_int *p = &lambda_scope_stack->last ();
8925 if (lambda_scope != p->t)
8927 lambda_scope = p->t;
8928 lambda_count = p->i;
8930 lambda_scope_stack->pop ();
8933 /* Parse a lambda expression.
8935 lambda-expression:
8936 lambda-introducer lambda-declarator [opt] compound-statement
8938 Returns a representation of the expression. */
8940 static tree
8941 cp_parser_lambda_expression (cp_parser* parser)
8943 tree lambda_expr = build_lambda_expr ();
8944 tree type;
8945 bool ok = true;
8946 cp_token *token = cp_lexer_peek_token (parser->lexer);
8947 cp_token_position start = 0;
8949 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8951 if (cp_unevaluated_operand)
8953 if (!token->error_reported)
8955 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8956 "lambda-expression in unevaluated context");
8957 token->error_reported = true;
8959 ok = false;
8961 else if (parser->in_template_argument_list_p)
8963 if (!token->error_reported)
8965 error_at (token->location, "lambda-expression in template-argument");
8966 token->error_reported = true;
8968 ok = false;
8971 /* We may be in the middle of deferred access check. Disable
8972 it now. */
8973 push_deferring_access_checks (dk_no_deferred);
8975 cp_parser_lambda_introducer (parser, lambda_expr);
8977 type = begin_lambda_type (lambda_expr);
8978 if (type == error_mark_node)
8979 return error_mark_node;
8981 record_lambda_scope (lambda_expr);
8983 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8984 determine_visibility (TYPE_NAME (type));
8986 /* Now that we've started the type, add the capture fields for any
8987 explicit captures. */
8988 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8991 /* Inside the class, surrounding template-parameter-lists do not apply. */
8992 unsigned int saved_num_template_parameter_lists
8993 = parser->num_template_parameter_lists;
8994 unsigned char in_statement = parser->in_statement;
8995 bool in_switch_statement_p = parser->in_switch_statement_p;
8996 bool fully_implicit_function_template_p
8997 = parser->fully_implicit_function_template_p;
8998 tree implicit_template_parms = parser->implicit_template_parms;
8999 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9000 bool auto_is_implicit_function_template_parm_p
9001 = parser->auto_is_implicit_function_template_parm_p;
9003 parser->num_template_parameter_lists = 0;
9004 parser->in_statement = 0;
9005 parser->in_switch_statement_p = false;
9006 parser->fully_implicit_function_template_p = false;
9007 parser->implicit_template_parms = 0;
9008 parser->implicit_template_scope = 0;
9009 parser->auto_is_implicit_function_template_parm_p = false;
9011 /* By virtue of defining a local class, a lambda expression has access to
9012 the private variables of enclosing classes. */
9014 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9016 if (ok)
9018 if (!cp_parser_error_occurred (parser)
9019 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9020 && cp_parser_start_tentative_firewall (parser))
9021 start = token;
9022 cp_parser_lambda_body (parser, lambda_expr);
9024 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9026 if (cp_parser_skip_to_closing_brace (parser))
9027 cp_lexer_consume_token (parser->lexer);
9030 /* The capture list was built up in reverse order; fix that now. */
9031 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9032 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9034 if (ok)
9035 maybe_add_lambda_conv_op (type);
9037 type = finish_struct (type, /*attributes=*/NULL_TREE);
9039 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9040 parser->in_statement = in_statement;
9041 parser->in_switch_statement_p = in_switch_statement_p;
9042 parser->fully_implicit_function_template_p
9043 = fully_implicit_function_template_p;
9044 parser->implicit_template_parms = implicit_template_parms;
9045 parser->implicit_template_scope = implicit_template_scope;
9046 parser->auto_is_implicit_function_template_parm_p
9047 = auto_is_implicit_function_template_parm_p;
9050 pop_deferring_access_checks ();
9052 /* This field is only used during parsing of the lambda. */
9053 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9055 /* This lambda shouldn't have any proxies left at this point. */
9056 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9057 /* And now that we're done, push proxies for an enclosing lambda. */
9058 insert_pending_capture_proxies ();
9060 if (ok)
9061 lambda_expr = build_lambda_object (lambda_expr);
9062 else
9063 lambda_expr = error_mark_node;
9065 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9067 return lambda_expr;
9070 /* Parse the beginning of a lambda expression.
9072 lambda-introducer:
9073 [ lambda-capture [opt] ]
9075 LAMBDA_EXPR is the current representation of the lambda expression. */
9077 static void
9078 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9080 /* Need commas after the first capture. */
9081 bool first = true;
9083 /* Eat the leading `['. */
9084 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9086 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9087 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9088 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9089 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9090 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9091 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9093 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9095 cp_lexer_consume_token (parser->lexer);
9096 first = false;
9099 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9101 cp_token* capture_token;
9102 tree capture_id;
9103 tree capture_init_expr;
9104 cp_id_kind idk = CP_ID_KIND_NONE;
9105 bool explicit_init_p = false;
9107 enum capture_kind_type
9109 BY_COPY,
9110 BY_REFERENCE
9112 enum capture_kind_type capture_kind = BY_COPY;
9114 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9116 error ("expected end of capture-list");
9117 return;
9120 if (first)
9121 first = false;
9122 else
9123 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9125 /* Possibly capture `this'. */
9126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9128 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9129 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9130 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9131 "with by-copy capture default");
9132 cp_lexer_consume_token (parser->lexer);
9133 add_capture (lambda_expr,
9134 /*id=*/this_identifier,
9135 /*initializer=*/finish_this_expr(),
9136 /*by_reference_p=*/false,
9137 explicit_init_p);
9138 continue;
9141 /* Remember whether we want to capture as a reference or not. */
9142 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9144 capture_kind = BY_REFERENCE;
9145 cp_lexer_consume_token (parser->lexer);
9148 /* Get the identifier. */
9149 capture_token = cp_lexer_peek_token (parser->lexer);
9150 capture_id = cp_parser_identifier (parser);
9152 if (capture_id == error_mark_node)
9153 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9154 delimiters, but I modified this to stop on unnested ']' as well. It
9155 was already changed to stop on unnested '}', so the
9156 "closing_parenthesis" name is no more misleading with my change. */
9158 cp_parser_skip_to_closing_parenthesis (parser,
9159 /*recovering=*/true,
9160 /*or_comma=*/true,
9161 /*consume_paren=*/true);
9162 break;
9165 /* Find the initializer for this capture. */
9166 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9167 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9168 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9170 bool direct, non_constant;
9171 /* An explicit initializer exists. */
9172 if (cxx_dialect < cxx14)
9173 pedwarn (input_location, 0,
9174 "lambda capture initializers "
9175 "only available with -std=c++14 or -std=gnu++14");
9176 capture_init_expr = cp_parser_initializer (parser, &direct,
9177 &non_constant);
9178 explicit_init_p = true;
9179 if (capture_init_expr == NULL_TREE)
9181 error ("empty initializer for lambda init-capture");
9182 capture_init_expr = error_mark_node;
9185 else
9187 const char* error_msg;
9189 /* Turn the identifier into an id-expression. */
9190 capture_init_expr
9191 = cp_parser_lookup_name_simple (parser, capture_id,
9192 capture_token->location);
9194 if (capture_init_expr == error_mark_node)
9196 unqualified_name_lookup_error (capture_id);
9197 continue;
9199 else if (DECL_P (capture_init_expr)
9200 && (!VAR_P (capture_init_expr)
9201 && TREE_CODE (capture_init_expr) != PARM_DECL))
9203 error_at (capture_token->location,
9204 "capture of non-variable %qD ",
9205 capture_init_expr);
9206 inform (0, "%q+#D declared here", capture_init_expr);
9207 continue;
9209 if (VAR_P (capture_init_expr)
9210 && decl_storage_duration (capture_init_expr) != dk_auto)
9212 if (pedwarn (capture_token->location, 0, "capture of variable "
9213 "%qD with non-automatic storage duration",
9214 capture_init_expr))
9215 inform (0, "%q+#D declared here", capture_init_expr);
9216 continue;
9219 capture_init_expr
9220 = finish_id_expression
9221 (capture_id,
9222 capture_init_expr,
9223 parser->scope,
9224 &idk,
9225 /*integral_constant_expression_p=*/false,
9226 /*allow_non_integral_constant_expression_p=*/false,
9227 /*non_integral_constant_expression_p=*/NULL,
9228 /*template_p=*/false,
9229 /*done=*/true,
9230 /*address_p=*/false,
9231 /*template_arg_p=*/false,
9232 &error_msg,
9233 capture_token->location);
9235 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9237 cp_lexer_consume_token (parser->lexer);
9238 capture_init_expr = make_pack_expansion (capture_init_expr);
9240 else
9241 check_for_bare_parameter_packs (capture_init_expr);
9244 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9245 && !explicit_init_p)
9247 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9248 && capture_kind == BY_COPY)
9249 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9250 "of %qD redundant with by-copy capture default",
9251 capture_id);
9252 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9253 && capture_kind == BY_REFERENCE)
9254 pedwarn (capture_token->location, 0, "explicit by-reference "
9255 "capture of %qD redundant with by-reference capture "
9256 "default", capture_id);
9259 add_capture (lambda_expr,
9260 capture_id,
9261 capture_init_expr,
9262 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9263 explicit_init_p);
9266 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9269 /* Parse the (optional) middle of a lambda expression.
9271 lambda-declarator:
9272 < template-parameter-list [opt] >
9273 ( parameter-declaration-clause [opt] )
9274 attribute-specifier [opt]
9275 mutable [opt]
9276 exception-specification [opt]
9277 lambda-return-type-clause [opt]
9279 LAMBDA_EXPR is the current representation of the lambda expression. */
9281 static bool
9282 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9284 /* 5.1.1.4 of the standard says:
9285 If a lambda-expression does not include a lambda-declarator, it is as if
9286 the lambda-declarator were ().
9287 This means an empty parameter list, no attributes, and no exception
9288 specification. */
9289 tree param_list = void_list_node;
9290 tree attributes = NULL_TREE;
9291 tree exception_spec = NULL_TREE;
9292 tree template_param_list = NULL_TREE;
9294 /* The template-parameter-list is optional, but must begin with
9295 an opening angle if present. */
9296 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9298 if (cxx_dialect < cxx14)
9299 pedwarn (parser->lexer->next_token->location, 0,
9300 "lambda templates are only available with "
9301 "-std=c++14 or -std=gnu++14");
9303 cp_lexer_consume_token (parser->lexer);
9305 template_param_list = cp_parser_template_parameter_list (parser);
9307 cp_parser_skip_to_end_of_template_parameter_list (parser);
9309 /* We just processed one more parameter list. */
9310 ++parser->num_template_parameter_lists;
9313 /* The parameter-declaration-clause is optional (unless
9314 template-parameter-list was given), but must begin with an
9315 opening parenthesis if present. */
9316 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9318 cp_lexer_consume_token (parser->lexer);
9320 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9322 /* Parse parameters. */
9323 param_list = cp_parser_parameter_declaration_clause (parser);
9325 /* Default arguments shall not be specified in the
9326 parameter-declaration-clause of a lambda-declarator. */
9327 for (tree t = param_list; t; t = TREE_CHAIN (t))
9328 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9329 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9330 "default argument specified for lambda parameter");
9332 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9334 attributes = cp_parser_attributes_opt (parser);
9336 /* Parse optional `mutable' keyword. */
9337 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9339 cp_lexer_consume_token (parser->lexer);
9340 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9343 /* Parse optional exception specification. */
9344 exception_spec = cp_parser_exception_specification_opt (parser);
9346 /* Parse optional trailing return type. */
9347 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9349 cp_lexer_consume_token (parser->lexer);
9350 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9351 = cp_parser_trailing_type_id (parser);
9354 /* The function parameters must be in scope all the way until after the
9355 trailing-return-type in case of decltype. */
9356 pop_bindings_and_leave_scope ();
9358 else if (template_param_list != NULL_TREE) // generate diagnostic
9359 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9361 /* Create the function call operator.
9363 Messing with declarators like this is no uglier than building up the
9364 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9365 other code. */
9367 cp_decl_specifier_seq return_type_specs;
9368 cp_declarator* declarator;
9369 tree fco;
9370 int quals;
9371 void *p;
9373 clear_decl_specs (&return_type_specs);
9374 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9375 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9376 else
9377 /* Maybe we will deduce the return type later. */
9378 return_type_specs.type = make_auto ();
9380 p = obstack_alloc (&declarator_obstack, 0);
9382 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9383 sfk_none);
9385 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9386 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9387 declarator = make_call_declarator (declarator, param_list, quals,
9388 VIRT_SPEC_UNSPECIFIED,
9389 REF_QUAL_NONE,
9390 exception_spec,
9391 /*late_return_type=*/NULL_TREE);
9392 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9394 fco = grokmethod (&return_type_specs,
9395 declarator,
9396 attributes);
9397 if (fco != error_mark_node)
9399 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9400 DECL_ARTIFICIAL (fco) = 1;
9401 /* Give the object parameter a different name. */
9402 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9403 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9404 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9406 if (template_param_list)
9408 fco = finish_member_template_decl (fco);
9409 finish_template_decl (template_param_list);
9410 --parser->num_template_parameter_lists;
9412 else if (parser->fully_implicit_function_template_p)
9413 fco = finish_fully_implicit_template (parser, fco);
9415 finish_member_declaration (fco);
9417 obstack_free (&declarator_obstack, p);
9419 return (fco != error_mark_node);
9423 /* Parse the body of a lambda expression, which is simply
9425 compound-statement
9427 but which requires special handling.
9428 LAMBDA_EXPR is the current representation of the lambda expression. */
9430 static void
9431 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9433 bool nested = (current_function_decl != NULL_TREE);
9434 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9435 if (nested)
9436 push_function_context ();
9437 else
9438 /* Still increment function_depth so that we don't GC in the
9439 middle of an expression. */
9440 ++function_depth;
9441 /* Clear this in case we're in the middle of a default argument. */
9442 parser->local_variables_forbidden_p = false;
9444 /* Finish the function call operator
9445 - class_specifier
9446 + late_parsing_for_member
9447 + function_definition_after_declarator
9448 + ctor_initializer_opt_and_function_body */
9450 tree fco = lambda_function (lambda_expr);
9451 tree body;
9452 bool done = false;
9453 tree compound_stmt;
9454 tree cap;
9456 /* Let the front end know that we are going to be defining this
9457 function. */
9458 start_preparsed_function (fco,
9459 NULL_TREE,
9460 SF_PRE_PARSED | SF_INCLASS_INLINE);
9462 start_lambda_scope (fco);
9463 body = begin_function_body ();
9465 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9466 goto out;
9468 /* Push the proxies for any explicit captures. */
9469 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9470 cap = TREE_CHAIN (cap))
9471 build_capture_proxy (TREE_PURPOSE (cap));
9473 compound_stmt = begin_compound_stmt (0);
9475 /* 5.1.1.4 of the standard says:
9476 If a lambda-expression does not include a trailing-return-type, it
9477 is as if the trailing-return-type denotes the following type:
9478 * if the compound-statement is of the form
9479 { return attribute-specifier [opt] expression ; }
9480 the type of the returned expression after lvalue-to-rvalue
9481 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9482 (_conv.array_ 4.2), and function-to-pointer conversion
9483 (_conv.func_ 4.3);
9484 * otherwise, void. */
9486 /* In a lambda that has neither a lambda-return-type-clause
9487 nor a deducible form, errors should be reported for return statements
9488 in the body. Since we used void as the placeholder return type, parsing
9489 the body as usual will give such desired behavior. */
9490 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9491 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9492 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9494 tree expr = NULL_TREE;
9495 cp_id_kind idk = CP_ID_KIND_NONE;
9497 /* Parse tentatively in case there's more after the initial return
9498 statement. */
9499 cp_parser_parse_tentatively (parser);
9501 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9503 expr = cp_parser_expression (parser, &idk);
9505 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9506 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9508 if (cp_parser_parse_definitely (parser))
9510 if (!processing_template_decl)
9511 apply_deduced_return_type (fco, lambda_return_type (expr));
9513 /* Will get error here if type not deduced yet. */
9514 finish_return_stmt (expr);
9516 done = true;
9520 if (!done)
9522 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9523 cp_parser_label_declaration (parser);
9524 cp_parser_statement_seq_opt (parser, NULL_TREE);
9525 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9528 finish_compound_stmt (compound_stmt);
9530 out:
9531 finish_function_body (body);
9532 finish_lambda_scope ();
9534 /* Finish the function and generate code for it if necessary. */
9535 tree fn = finish_function (/*inline*/2);
9537 /* Only expand if the call op is not a template. */
9538 if (!DECL_TEMPLATE_INFO (fco))
9539 expand_or_defer_fn (fn);
9542 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9543 if (nested)
9544 pop_function_context();
9545 else
9546 --function_depth;
9549 /* Statements [gram.stmt.stmt] */
9551 /* Parse a statement.
9553 statement:
9554 labeled-statement
9555 expression-statement
9556 compound-statement
9557 selection-statement
9558 iteration-statement
9559 jump-statement
9560 declaration-statement
9561 try-block
9563 C++11:
9565 statement:
9566 labeled-statement
9567 attribute-specifier-seq (opt) expression-statement
9568 attribute-specifier-seq (opt) compound-statement
9569 attribute-specifier-seq (opt) selection-statement
9570 attribute-specifier-seq (opt) iteration-statement
9571 attribute-specifier-seq (opt) jump-statement
9572 declaration-statement
9573 attribute-specifier-seq (opt) try-block
9575 TM Extension:
9577 statement:
9578 atomic-statement
9580 IN_COMPOUND is true when the statement is nested inside a
9581 cp_parser_compound_statement; this matters for certain pragmas.
9583 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9584 is a (possibly labeled) if statement which is not enclosed in braces
9585 and has an else clause. This is used to implement -Wparentheses. */
9587 static void
9588 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9589 bool in_compound, bool *if_p)
9591 tree statement, std_attrs = NULL_TREE;
9592 cp_token *token;
9593 location_t statement_location, attrs_location;
9595 restart:
9596 if (if_p != NULL)
9597 *if_p = false;
9598 /* There is no statement yet. */
9599 statement = NULL_TREE;
9601 saved_token_sentinel saved_tokens (parser->lexer);
9602 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9603 if (c_dialect_objc ())
9604 /* In obj-c++, seeing '[[' might be the either the beginning of
9605 c++11 attributes, or a nested objc-message-expression. So
9606 let's parse the c++11 attributes tentatively. */
9607 cp_parser_parse_tentatively (parser);
9608 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9609 if (c_dialect_objc ())
9611 if (!cp_parser_parse_definitely (parser))
9612 std_attrs = NULL_TREE;
9615 /* Peek at the next token. */
9616 token = cp_lexer_peek_token (parser->lexer);
9617 /* Remember the location of the first token in the statement. */
9618 statement_location = token->location;
9619 /* If this is a keyword, then that will often determine what kind of
9620 statement we have. */
9621 if (token->type == CPP_KEYWORD)
9623 enum rid keyword = token->keyword;
9625 switch (keyword)
9627 case RID_CASE:
9628 case RID_DEFAULT:
9629 /* Looks like a labeled-statement with a case label.
9630 Parse the label, and then use tail recursion to parse
9631 the statement. */
9632 cp_parser_label_for_labeled_statement (parser, std_attrs);
9633 goto restart;
9635 case RID_IF:
9636 case RID_SWITCH:
9637 statement = cp_parser_selection_statement (parser, if_p);
9638 break;
9640 case RID_WHILE:
9641 case RID_DO:
9642 case RID_FOR:
9643 statement = cp_parser_iteration_statement (parser, false);
9644 break;
9646 case RID_CILK_FOR:
9647 if (!flag_cilkplus)
9649 error_at (cp_lexer_peek_token (parser->lexer)->location,
9650 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9651 cp_lexer_consume_token (parser->lexer);
9652 statement = error_mark_node;
9654 else
9655 statement = cp_parser_cilk_for (parser, integer_zero_node);
9656 break;
9658 case RID_BREAK:
9659 case RID_CONTINUE:
9660 case RID_RETURN:
9661 case RID_GOTO:
9662 statement = cp_parser_jump_statement (parser);
9663 break;
9665 case RID_CILK_SYNC:
9666 cp_lexer_consume_token (parser->lexer);
9667 if (flag_cilkplus)
9669 tree sync_expr = build_cilk_sync ();
9670 SET_EXPR_LOCATION (sync_expr,
9671 token->location);
9672 statement = finish_expr_stmt (sync_expr);
9674 else
9676 error_at (token->location, "-fcilkplus must be enabled to use"
9677 " %<_Cilk_sync%>");
9678 statement = error_mark_node;
9680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9681 break;
9683 /* Objective-C++ exception-handling constructs. */
9684 case RID_AT_TRY:
9685 case RID_AT_CATCH:
9686 case RID_AT_FINALLY:
9687 case RID_AT_SYNCHRONIZED:
9688 case RID_AT_THROW:
9689 statement = cp_parser_objc_statement (parser);
9690 break;
9692 case RID_TRY:
9693 statement = cp_parser_try_block (parser);
9694 break;
9696 case RID_NAMESPACE:
9697 /* This must be a namespace alias definition. */
9698 cp_parser_declaration_statement (parser);
9699 return;
9701 case RID_TRANSACTION_ATOMIC:
9702 case RID_TRANSACTION_RELAXED:
9703 statement = cp_parser_transaction (parser, keyword);
9704 break;
9705 case RID_TRANSACTION_CANCEL:
9706 statement = cp_parser_transaction_cancel (parser);
9707 break;
9709 default:
9710 /* It might be a keyword like `int' that can start a
9711 declaration-statement. */
9712 break;
9715 else if (token->type == CPP_NAME)
9717 /* If the next token is a `:', then we are looking at a
9718 labeled-statement. */
9719 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9720 if (token->type == CPP_COLON)
9722 /* Looks like a labeled-statement with an ordinary label.
9723 Parse the label, and then use tail recursion to parse
9724 the statement. */
9726 cp_parser_label_for_labeled_statement (parser, std_attrs);
9727 goto restart;
9730 /* Anything that starts with a `{' must be a compound-statement. */
9731 else if (token->type == CPP_OPEN_BRACE)
9732 statement = cp_parser_compound_statement (parser, NULL, false, false);
9733 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9734 a statement all its own. */
9735 else if (token->type == CPP_PRAGMA)
9737 /* Only certain OpenMP pragmas are attached to statements, and thus
9738 are considered statements themselves. All others are not. In
9739 the context of a compound, accept the pragma as a "statement" and
9740 return so that we can check for a close brace. Otherwise we
9741 require a real statement and must go back and read one. */
9742 if (in_compound)
9743 cp_parser_pragma (parser, pragma_compound);
9744 else if (!cp_parser_pragma (parser, pragma_stmt))
9745 goto restart;
9746 return;
9748 else if (token->type == CPP_EOF)
9750 cp_parser_error (parser, "expected statement");
9751 return;
9754 /* Everything else must be a declaration-statement or an
9755 expression-statement. Try for the declaration-statement
9756 first, unless we are looking at a `;', in which case we know that
9757 we have an expression-statement. */
9758 if (!statement)
9760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9762 if (std_attrs != NULL_TREE)
9764 /* Attributes should be parsed as part of the the
9765 declaration, so let's un-parse them. */
9766 saved_tokens.rollback();
9767 std_attrs = NULL_TREE;
9770 cp_parser_parse_tentatively (parser);
9771 /* Try to parse the declaration-statement. */
9772 cp_parser_declaration_statement (parser);
9773 /* If that worked, we're done. */
9774 if (cp_parser_parse_definitely (parser))
9775 return;
9777 /* Look for an expression-statement instead. */
9778 statement = cp_parser_expression_statement (parser, in_statement_expr);
9781 /* Set the line number for the statement. */
9782 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9783 SET_EXPR_LOCATION (statement, statement_location);
9785 /* Note that for now, we don't do anything with c++11 statements
9786 parsed at this level. */
9787 if (std_attrs != NULL_TREE)
9788 warning_at (attrs_location,
9789 OPT_Wattributes,
9790 "attributes at the beginning of statement are ignored");
9793 /* Parse the label for a labeled-statement, i.e.
9795 identifier :
9796 case constant-expression :
9797 default :
9799 GNU Extension:
9800 case constant-expression ... constant-expression : statement
9802 When a label is parsed without errors, the label is added to the
9803 parse tree by the finish_* functions, so this function doesn't
9804 have to return the label. */
9806 static void
9807 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9809 cp_token *token;
9810 tree label = NULL_TREE;
9811 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9813 /* The next token should be an identifier. */
9814 token = cp_lexer_peek_token (parser->lexer);
9815 if (token->type != CPP_NAME
9816 && token->type != CPP_KEYWORD)
9818 cp_parser_error (parser, "expected labeled-statement");
9819 return;
9822 parser->colon_corrects_to_scope_p = false;
9823 switch (token->keyword)
9825 case RID_CASE:
9827 tree expr, expr_hi;
9828 cp_token *ellipsis;
9830 /* Consume the `case' token. */
9831 cp_lexer_consume_token (parser->lexer);
9832 /* Parse the constant-expression. */
9833 expr = cp_parser_constant_expression (parser);
9835 ellipsis = cp_lexer_peek_token (parser->lexer);
9836 if (ellipsis->type == CPP_ELLIPSIS)
9838 /* Consume the `...' token. */
9839 cp_lexer_consume_token (parser->lexer);
9840 expr_hi =
9841 cp_parser_constant_expression (parser);
9843 /* We don't need to emit warnings here, as the common code
9844 will do this for us. */
9846 else
9847 expr_hi = NULL_TREE;
9849 if (parser->in_switch_statement_p)
9850 finish_case_label (token->location, expr, expr_hi);
9851 else
9852 error_at (token->location,
9853 "case label %qE not within a switch statement",
9854 expr);
9856 break;
9858 case RID_DEFAULT:
9859 /* Consume the `default' token. */
9860 cp_lexer_consume_token (parser->lexer);
9862 if (parser->in_switch_statement_p)
9863 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9864 else
9865 error_at (token->location, "case label not within a switch statement");
9866 break;
9868 default:
9869 /* Anything else must be an ordinary label. */
9870 label = finish_label_stmt (cp_parser_identifier (parser));
9871 break;
9874 /* Require the `:' token. */
9875 cp_parser_require (parser, CPP_COLON, RT_COLON);
9877 /* An ordinary label may optionally be followed by attributes.
9878 However, this is only permitted if the attributes are then
9879 followed by a semicolon. This is because, for backward
9880 compatibility, when parsing
9881 lab: __attribute__ ((unused)) int i;
9882 we want the attribute to attach to "i", not "lab". */
9883 if (label != NULL_TREE
9884 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9886 tree attrs;
9887 cp_parser_parse_tentatively (parser);
9888 attrs = cp_parser_gnu_attributes_opt (parser);
9889 if (attrs == NULL_TREE
9890 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9891 cp_parser_abort_tentative_parse (parser);
9892 else if (!cp_parser_parse_definitely (parser))
9894 else
9895 attributes = chainon (attributes, attrs);
9898 if (attributes != NULL_TREE)
9899 cplus_decl_attributes (&label, attributes, 0);
9901 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9904 /* Parse an expression-statement.
9906 expression-statement:
9907 expression [opt] ;
9909 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9910 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9911 indicates whether this expression-statement is part of an
9912 expression statement. */
9914 static tree
9915 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9917 tree statement = NULL_TREE;
9918 cp_token *token = cp_lexer_peek_token (parser->lexer);
9920 /* If the next token is a ';', then there is no expression
9921 statement. */
9922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9924 statement = cp_parser_expression (parser);
9925 if (statement == error_mark_node
9926 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9928 cp_parser_skip_to_end_of_block_or_statement (parser);
9929 return error_mark_node;
9933 /* Give a helpful message for "A<T>::type t;" and the like. */
9934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9935 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9937 if (TREE_CODE (statement) == SCOPE_REF)
9938 error_at (token->location, "need %<typename%> before %qE because "
9939 "%qT is a dependent scope",
9940 statement, TREE_OPERAND (statement, 0));
9941 else if (is_overloaded_fn (statement)
9942 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9944 /* A::A a; */
9945 tree fn = get_first_fn (statement);
9946 error_at (token->location,
9947 "%<%T::%D%> names the constructor, not the type",
9948 DECL_CONTEXT (fn), DECL_NAME (fn));
9952 /* Consume the final `;'. */
9953 cp_parser_consume_semicolon_at_end_of_statement (parser);
9955 if (in_statement_expr
9956 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9957 /* This is the final expression statement of a statement
9958 expression. */
9959 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9960 else if (statement)
9961 statement = finish_expr_stmt (statement);
9963 return statement;
9966 /* Parse a compound-statement.
9968 compound-statement:
9969 { statement-seq [opt] }
9971 GNU extension:
9973 compound-statement:
9974 { label-declaration-seq [opt] statement-seq [opt] }
9976 label-declaration-seq:
9977 label-declaration
9978 label-declaration-seq label-declaration
9980 Returns a tree representing the statement. */
9982 static tree
9983 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9984 bool in_try, bool function_body)
9986 tree compound_stmt;
9988 /* Consume the `{'. */
9989 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9990 return error_mark_node;
9991 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9992 && !function_body && cxx_dialect < cxx14)
9993 pedwarn (input_location, OPT_Wpedantic,
9994 "compound-statement in constexpr function");
9995 /* Begin the compound-statement. */
9996 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9997 /* If the next keyword is `__label__' we have a label declaration. */
9998 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9999 cp_parser_label_declaration (parser);
10000 /* Parse an (optional) statement-seq. */
10001 cp_parser_statement_seq_opt (parser, in_statement_expr);
10002 /* Finish the compound-statement. */
10003 finish_compound_stmt (compound_stmt);
10004 /* Consume the `}'. */
10005 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10007 return compound_stmt;
10010 /* Parse an (optional) statement-seq.
10012 statement-seq:
10013 statement
10014 statement-seq [opt] statement */
10016 static void
10017 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10019 /* Scan statements until there aren't any more. */
10020 while (true)
10022 cp_token *token = cp_lexer_peek_token (parser->lexer);
10024 /* If we are looking at a `}', then we have run out of
10025 statements; the same is true if we have reached the end
10026 of file, or have stumbled upon a stray '@end'. */
10027 if (token->type == CPP_CLOSE_BRACE
10028 || token->type == CPP_EOF
10029 || token->type == CPP_PRAGMA_EOL
10030 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10031 break;
10033 /* If we are in a compound statement and find 'else' then
10034 something went wrong. */
10035 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10037 if (parser->in_statement & IN_IF_STMT)
10038 break;
10039 else
10041 token = cp_lexer_consume_token (parser->lexer);
10042 error_at (token->location, "%<else%> without a previous %<if%>");
10046 /* Parse the statement. */
10047 cp_parser_statement (parser, in_statement_expr, true, NULL);
10051 /* Parse a selection-statement.
10053 selection-statement:
10054 if ( condition ) statement
10055 if ( condition ) statement else statement
10056 switch ( condition ) statement
10058 Returns the new IF_STMT or SWITCH_STMT.
10060 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10061 is a (possibly labeled) if statement which is not enclosed in
10062 braces and has an else clause. This is used to implement
10063 -Wparentheses. */
10065 static tree
10066 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10068 cp_token *token;
10069 enum rid keyword;
10071 if (if_p != NULL)
10072 *if_p = false;
10074 /* Peek at the next token. */
10075 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10077 /* See what kind of keyword it is. */
10078 keyword = token->keyword;
10079 switch (keyword)
10081 case RID_IF:
10082 case RID_SWITCH:
10084 tree statement;
10085 tree condition;
10087 /* Look for the `('. */
10088 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10090 cp_parser_skip_to_end_of_statement (parser);
10091 return error_mark_node;
10094 /* Begin the selection-statement. */
10095 if (keyword == RID_IF)
10096 statement = begin_if_stmt ();
10097 else
10098 statement = begin_switch_stmt ();
10100 /* Parse the condition. */
10101 condition = cp_parser_condition (parser);
10102 /* Look for the `)'. */
10103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10104 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10105 /*consume_paren=*/true);
10107 if (keyword == RID_IF)
10109 bool nested_if;
10110 unsigned char in_statement;
10112 /* Add the condition. */
10113 finish_if_stmt_cond (condition, statement);
10115 /* Parse the then-clause. */
10116 in_statement = parser->in_statement;
10117 parser->in_statement |= IN_IF_STMT;
10118 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10121 add_stmt (build_empty_stmt (loc));
10122 cp_lexer_consume_token (parser->lexer);
10123 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10124 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10125 "empty body in an %<if%> statement");
10126 nested_if = false;
10128 else
10129 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10130 parser->in_statement = in_statement;
10132 finish_then_clause (statement);
10134 /* If the next token is `else', parse the else-clause. */
10135 if (cp_lexer_next_token_is_keyword (parser->lexer,
10136 RID_ELSE))
10138 /* Consume the `else' keyword. */
10139 cp_lexer_consume_token (parser->lexer);
10140 begin_else_clause (statement);
10141 /* Parse the else-clause. */
10142 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10144 location_t loc;
10145 loc = cp_lexer_peek_token (parser->lexer)->location;
10146 warning_at (loc,
10147 OPT_Wempty_body, "suggest braces around "
10148 "empty body in an %<else%> statement");
10149 add_stmt (build_empty_stmt (loc));
10150 cp_lexer_consume_token (parser->lexer);
10152 else
10153 cp_parser_implicitly_scoped_statement (parser, NULL);
10155 finish_else_clause (statement);
10157 /* If we are currently parsing a then-clause, then
10158 IF_P will not be NULL. We set it to true to
10159 indicate that this if statement has an else clause.
10160 This may trigger the Wparentheses warning below
10161 when we get back up to the parent if statement. */
10162 if (if_p != NULL)
10163 *if_p = true;
10165 else
10167 /* This if statement does not have an else clause. If
10168 NESTED_IF is true, then the then-clause is an if
10169 statement which does have an else clause. We warn
10170 about the potential ambiguity. */
10171 if (nested_if)
10172 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10173 "suggest explicit braces to avoid ambiguous"
10174 " %<else%>");
10177 /* Now we're all done with the if-statement. */
10178 finish_if_stmt (statement);
10180 else
10182 bool in_switch_statement_p;
10183 unsigned char in_statement;
10185 /* Add the condition. */
10186 finish_switch_cond (condition, statement);
10188 /* Parse the body of the switch-statement. */
10189 in_switch_statement_p = parser->in_switch_statement_p;
10190 in_statement = parser->in_statement;
10191 parser->in_switch_statement_p = true;
10192 parser->in_statement |= IN_SWITCH_STMT;
10193 cp_parser_implicitly_scoped_statement (parser, NULL);
10194 parser->in_switch_statement_p = in_switch_statement_p;
10195 parser->in_statement = in_statement;
10197 /* Now we're all done with the switch-statement. */
10198 finish_switch_stmt (statement);
10201 return statement;
10203 break;
10205 default:
10206 cp_parser_error (parser, "expected selection-statement");
10207 return error_mark_node;
10211 /* Parse a condition.
10213 condition:
10214 expression
10215 type-specifier-seq declarator = initializer-clause
10216 type-specifier-seq declarator braced-init-list
10218 GNU Extension:
10220 condition:
10221 type-specifier-seq declarator asm-specification [opt]
10222 attributes [opt] = assignment-expression
10224 Returns the expression that should be tested. */
10226 static tree
10227 cp_parser_condition (cp_parser* parser)
10229 cp_decl_specifier_seq type_specifiers;
10230 const char *saved_message;
10231 int declares_class_or_enum;
10233 /* Try the declaration first. */
10234 cp_parser_parse_tentatively (parser);
10235 /* New types are not allowed in the type-specifier-seq for a
10236 condition. */
10237 saved_message = parser->type_definition_forbidden_message;
10238 parser->type_definition_forbidden_message
10239 = G_("types may not be defined in conditions");
10240 /* Parse the type-specifier-seq. */
10241 cp_parser_decl_specifier_seq (parser,
10242 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10243 &type_specifiers,
10244 &declares_class_or_enum);
10245 /* Restore the saved message. */
10246 parser->type_definition_forbidden_message = saved_message;
10247 /* If all is well, we might be looking at a declaration. */
10248 if (!cp_parser_error_occurred (parser))
10250 tree decl;
10251 tree asm_specification;
10252 tree attributes;
10253 cp_declarator *declarator;
10254 tree initializer = NULL_TREE;
10256 /* Parse the declarator. */
10257 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10258 /*ctor_dtor_or_conv_p=*/NULL,
10259 /*parenthesized_p=*/NULL,
10260 /*member_p=*/false,
10261 /*friend_p=*/false);
10262 /* Parse the attributes. */
10263 attributes = cp_parser_attributes_opt (parser);
10264 /* Parse the asm-specification. */
10265 asm_specification = cp_parser_asm_specification_opt (parser);
10266 /* If the next token is not an `=' or '{', then we might still be
10267 looking at an expression. For example:
10269 if (A(a).x)
10271 looks like a decl-specifier-seq and a declarator -- but then
10272 there is no `=', so this is an expression. */
10273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10274 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10275 cp_parser_simulate_error (parser);
10277 /* If we did see an `=' or '{', then we are looking at a declaration
10278 for sure. */
10279 if (cp_parser_parse_definitely (parser))
10281 tree pushed_scope;
10282 bool non_constant_p;
10283 bool flags = LOOKUP_ONLYCONVERTING;
10285 /* Create the declaration. */
10286 decl = start_decl (declarator, &type_specifiers,
10287 /*initialized_p=*/true,
10288 attributes, /*prefix_attributes=*/NULL_TREE,
10289 &pushed_scope);
10291 /* Parse the initializer. */
10292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10294 initializer = cp_parser_braced_list (parser, &non_constant_p);
10295 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10296 flags = 0;
10298 else
10300 /* Consume the `='. */
10301 cp_parser_require (parser, CPP_EQ, RT_EQ);
10302 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10304 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10307 /* Process the initializer. */
10308 cp_finish_decl (decl,
10309 initializer, !non_constant_p,
10310 asm_specification,
10311 flags);
10313 if (pushed_scope)
10314 pop_scope (pushed_scope);
10316 return convert_from_reference (decl);
10319 /* If we didn't even get past the declarator successfully, we are
10320 definitely not looking at a declaration. */
10321 else
10322 cp_parser_abort_tentative_parse (parser);
10324 /* Otherwise, we are looking at an expression. */
10325 return cp_parser_expression (parser);
10328 /* Parses a for-statement or range-for-statement until the closing ')',
10329 not included. */
10331 static tree
10332 cp_parser_for (cp_parser *parser, bool ivdep)
10334 tree init, scope, decl;
10335 bool is_range_for;
10337 /* Begin the for-statement. */
10338 scope = begin_for_scope (&init);
10340 /* Parse the initialization. */
10341 is_range_for = cp_parser_for_init_statement (parser, &decl);
10343 if (is_range_for)
10344 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10345 else
10346 return cp_parser_c_for (parser, scope, init, ivdep);
10349 static tree
10350 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10352 /* Normal for loop */
10353 tree condition = NULL_TREE;
10354 tree expression = NULL_TREE;
10355 tree stmt;
10357 stmt = begin_for_stmt (scope, init);
10358 /* The for-init-statement has already been parsed in
10359 cp_parser_for_init_statement, so no work is needed here. */
10360 finish_for_init_stmt (stmt);
10362 /* If there's a condition, process it. */
10363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10364 condition = cp_parser_condition (parser);
10365 else if (ivdep)
10367 cp_parser_error (parser, "missing loop condition in loop with "
10368 "%<GCC ivdep%> pragma");
10369 condition = error_mark_node;
10371 finish_for_cond (condition, stmt, ivdep);
10372 /* Look for the `;'. */
10373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10375 /* If there's an expression, process it. */
10376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10377 expression = cp_parser_expression (parser);
10378 finish_for_expr (expression, stmt);
10380 return stmt;
10383 /* Tries to parse a range-based for-statement:
10385 range-based-for:
10386 decl-specifier-seq declarator : expression
10388 The decl-specifier-seq declarator and the `:' are already parsed by
10389 cp_parser_for_init_statement. If processing_template_decl it returns a
10390 newly created RANGE_FOR_STMT; if not, it is converted to a
10391 regular FOR_STMT. */
10393 static tree
10394 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10395 bool ivdep)
10397 tree stmt, range_expr;
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10401 bool expr_non_constant_p;
10402 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10404 else
10405 range_expr = cp_parser_expression (parser);
10407 /* If in template, STMT is converted to a normal for-statement
10408 at instantiation. If not, it is done just ahead. */
10409 if (processing_template_decl)
10411 if (check_for_bare_parameter_packs (range_expr))
10412 range_expr = error_mark_node;
10413 stmt = begin_range_for_stmt (scope, init);
10414 if (ivdep)
10415 RANGE_FOR_IVDEP (stmt) = 1;
10416 finish_range_for_decl (stmt, range_decl, range_expr);
10417 if (!type_dependent_expression_p (range_expr)
10418 /* do_auto_deduction doesn't mess with template init-lists. */
10419 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10420 do_range_for_auto_deduction (range_decl, range_expr);
10422 else
10424 stmt = begin_for_stmt (scope, init);
10425 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10427 return stmt;
10430 /* Subroutine of cp_convert_range_for: given the initializer expression,
10431 builds up the range temporary. */
10433 static tree
10434 build_range_temp (tree range_expr)
10436 tree range_type, range_temp;
10438 /* Find out the type deduced by the declaration
10439 `auto &&__range = range_expr'. */
10440 range_type = cp_build_reference_type (make_auto (), true);
10441 range_type = do_auto_deduction (range_type, range_expr,
10442 type_uses_auto (range_type));
10444 /* Create the __range variable. */
10445 range_temp = build_decl (input_location, VAR_DECL,
10446 get_identifier ("__for_range"), range_type);
10447 TREE_USED (range_temp) = 1;
10448 DECL_ARTIFICIAL (range_temp) = 1;
10450 return range_temp;
10453 /* Used by cp_parser_range_for in template context: we aren't going to
10454 do a full conversion yet, but we still need to resolve auto in the
10455 type of the for-range-declaration if present. This is basically
10456 a shortcut version of cp_convert_range_for. */
10458 static void
10459 do_range_for_auto_deduction (tree decl, tree range_expr)
10461 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10462 if (auto_node)
10464 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10465 range_temp = convert_from_reference (build_range_temp (range_expr));
10466 iter_type = (cp_parser_perform_range_for_lookup
10467 (range_temp, &begin_dummy, &end_dummy));
10468 if (iter_type)
10470 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10471 iter_type);
10472 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10473 tf_warning_or_error);
10474 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10475 iter_decl, auto_node);
10480 /* Converts a range-based for-statement into a normal
10481 for-statement, as per the definition.
10483 for (RANGE_DECL : RANGE_EXPR)
10484 BLOCK
10486 should be equivalent to:
10489 auto &&__range = RANGE_EXPR;
10490 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10491 __begin != __end;
10492 ++__begin)
10494 RANGE_DECL = *__begin;
10495 BLOCK
10499 If RANGE_EXPR is an array:
10500 BEGIN_EXPR = __range
10501 END_EXPR = __range + ARRAY_SIZE(__range)
10502 Else if RANGE_EXPR has a member 'begin' or 'end':
10503 BEGIN_EXPR = __range.begin()
10504 END_EXPR = __range.end()
10505 Else:
10506 BEGIN_EXPR = begin(__range)
10507 END_EXPR = end(__range);
10509 If __range has a member 'begin' but not 'end', or vice versa, we must
10510 still use the second alternative (it will surely fail, however).
10511 When calling begin()/end() in the third alternative we must use
10512 argument dependent lookup, but always considering 'std' as an associated
10513 namespace. */
10515 tree
10516 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10517 bool ivdep)
10519 tree begin, end;
10520 tree iter_type, begin_expr, end_expr;
10521 tree condition, expression;
10523 if (range_decl == error_mark_node || range_expr == error_mark_node)
10524 /* If an error happened previously do nothing or else a lot of
10525 unhelpful errors would be issued. */
10526 begin_expr = end_expr = iter_type = error_mark_node;
10527 else
10529 tree range_temp;
10531 if (TREE_CODE (range_expr) == VAR_DECL
10532 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10533 /* Can't bind a reference to an array of runtime bound. */
10534 range_temp = range_expr;
10535 else
10537 range_temp = build_range_temp (range_expr);
10538 pushdecl (range_temp);
10539 cp_finish_decl (range_temp, range_expr,
10540 /*is_constant_init*/false, NULL_TREE,
10541 LOOKUP_ONLYCONVERTING);
10542 range_temp = convert_from_reference (range_temp);
10544 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10545 &begin_expr, &end_expr);
10548 /* The new for initialization statement. */
10549 begin = build_decl (input_location, VAR_DECL,
10550 get_identifier ("__for_begin"), iter_type);
10551 TREE_USED (begin) = 1;
10552 DECL_ARTIFICIAL (begin) = 1;
10553 pushdecl (begin);
10554 cp_finish_decl (begin, begin_expr,
10555 /*is_constant_init*/false, NULL_TREE,
10556 LOOKUP_ONLYCONVERTING);
10558 end = build_decl (input_location, VAR_DECL,
10559 get_identifier ("__for_end"), iter_type);
10560 TREE_USED (end) = 1;
10561 DECL_ARTIFICIAL (end) = 1;
10562 pushdecl (end);
10563 cp_finish_decl (end, end_expr,
10564 /*is_constant_init*/false, NULL_TREE,
10565 LOOKUP_ONLYCONVERTING);
10567 finish_for_init_stmt (statement);
10569 /* The new for condition. */
10570 condition = build_x_binary_op (input_location, NE_EXPR,
10571 begin, ERROR_MARK,
10572 end, ERROR_MARK,
10573 NULL, tf_warning_or_error);
10574 finish_for_cond (condition, statement, ivdep);
10576 /* The new increment expression. */
10577 expression = finish_unary_op_expr (input_location,
10578 PREINCREMENT_EXPR, begin,
10579 tf_warning_or_error);
10580 finish_for_expr (expression, statement);
10582 /* The declaration is initialized with *__begin inside the loop body. */
10583 cp_finish_decl (range_decl,
10584 build_x_indirect_ref (input_location, begin, RO_NULL,
10585 tf_warning_or_error),
10586 /*is_constant_init*/false, NULL_TREE,
10587 LOOKUP_ONLYCONVERTING);
10589 return statement;
10592 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10593 We need to solve both at the same time because the method used
10594 depends on the existence of members begin or end.
10595 Returns the type deduced for the iterator expression. */
10597 static tree
10598 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10600 if (error_operand_p (range))
10602 *begin = *end = error_mark_node;
10603 return error_mark_node;
10606 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10608 error ("range-based %<for%> expression of type %qT "
10609 "has incomplete type", TREE_TYPE (range));
10610 *begin = *end = error_mark_node;
10611 return error_mark_node;
10613 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10615 /* If RANGE is an array, we will use pointer arithmetic. */
10616 *begin = range;
10617 *end = build_binary_op (input_location, PLUS_EXPR,
10618 range,
10619 array_type_nelts_top (TREE_TYPE (range)),
10621 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10623 else
10625 /* If it is not an array, we must do a bit of magic. */
10626 tree id_begin, id_end;
10627 tree member_begin, member_end;
10629 *begin = *end = error_mark_node;
10631 id_begin = get_identifier ("begin");
10632 id_end = get_identifier ("end");
10633 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10634 /*protect=*/2, /*want_type=*/false,
10635 tf_warning_or_error);
10636 member_end = lookup_member (TREE_TYPE (range), id_end,
10637 /*protect=*/2, /*want_type=*/false,
10638 tf_warning_or_error);
10640 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10642 /* Use the member functions. */
10643 if (member_begin != NULL_TREE)
10644 *begin = cp_parser_range_for_member_function (range, id_begin);
10645 else
10646 error ("range-based %<for%> expression of type %qT has an "
10647 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10649 if (member_end != NULL_TREE)
10650 *end = cp_parser_range_for_member_function (range, id_end);
10651 else
10652 error ("range-based %<for%> expression of type %qT has a "
10653 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10655 else
10657 /* Use global functions with ADL. */
10658 vec<tree, va_gc> *vec;
10659 vec = make_tree_vector ();
10661 vec_safe_push (vec, range);
10663 member_begin = perform_koenig_lookup (id_begin, vec,
10664 tf_warning_or_error);
10665 *begin = finish_call_expr (member_begin, &vec, false, true,
10666 tf_warning_or_error);
10667 member_end = perform_koenig_lookup (id_end, vec,
10668 tf_warning_or_error);
10669 *end = finish_call_expr (member_end, &vec, false, true,
10670 tf_warning_or_error);
10672 release_tree_vector (vec);
10675 /* Last common checks. */
10676 if (*begin == error_mark_node || *end == error_mark_node)
10678 /* If one of the expressions is an error do no more checks. */
10679 *begin = *end = error_mark_node;
10680 return error_mark_node;
10682 else if (type_dependent_expression_p (*begin)
10683 || type_dependent_expression_p (*end))
10684 /* Can happen, when, eg, in a template context, Koenig lookup
10685 can't resolve begin/end (c++/58503). */
10686 return NULL_TREE;
10687 else
10689 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10690 /* The unqualified type of the __begin and __end temporaries should
10691 be the same, as required by the multiple auto declaration. */
10692 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10693 error ("inconsistent begin/end types in range-based %<for%> "
10694 "statement: %qT and %qT",
10695 TREE_TYPE (*begin), TREE_TYPE (*end));
10696 return iter_type;
10701 /* Helper function for cp_parser_perform_range_for_lookup.
10702 Builds a tree for RANGE.IDENTIFIER(). */
10704 static tree
10705 cp_parser_range_for_member_function (tree range, tree identifier)
10707 tree member, res;
10708 vec<tree, va_gc> *vec;
10710 member = finish_class_member_access_expr (range, identifier,
10711 false, tf_warning_or_error);
10712 if (member == error_mark_node)
10713 return error_mark_node;
10715 vec = make_tree_vector ();
10716 res = finish_call_expr (member, &vec,
10717 /*disallow_virtual=*/false,
10718 /*koenig_p=*/false,
10719 tf_warning_or_error);
10720 release_tree_vector (vec);
10721 return res;
10724 /* Parse an iteration-statement.
10726 iteration-statement:
10727 while ( condition ) statement
10728 do statement while ( expression ) ;
10729 for ( for-init-statement condition [opt] ; expression [opt] )
10730 statement
10732 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10734 static tree
10735 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10737 cp_token *token;
10738 enum rid keyword;
10739 tree statement;
10740 unsigned char in_statement;
10742 /* Peek at the next token. */
10743 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10744 if (!token)
10745 return error_mark_node;
10747 /* Remember whether or not we are already within an iteration
10748 statement. */
10749 in_statement = parser->in_statement;
10751 /* See what kind of keyword it is. */
10752 keyword = token->keyword;
10753 switch (keyword)
10755 case RID_WHILE:
10757 tree condition;
10759 /* Begin the while-statement. */
10760 statement = begin_while_stmt ();
10761 /* Look for the `('. */
10762 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10763 /* Parse the condition. */
10764 condition = cp_parser_condition (parser);
10765 finish_while_stmt_cond (condition, statement, ivdep);
10766 /* Look for the `)'. */
10767 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10768 /* Parse the dependent statement. */
10769 parser->in_statement = IN_ITERATION_STMT;
10770 cp_parser_already_scoped_statement (parser);
10771 parser->in_statement = in_statement;
10772 /* We're done with the while-statement. */
10773 finish_while_stmt (statement);
10775 break;
10777 case RID_DO:
10779 tree expression;
10781 /* Begin the do-statement. */
10782 statement = begin_do_stmt ();
10783 /* Parse the body of the do-statement. */
10784 parser->in_statement = IN_ITERATION_STMT;
10785 cp_parser_implicitly_scoped_statement (parser, NULL);
10786 parser->in_statement = in_statement;
10787 finish_do_body (statement);
10788 /* Look for the `while' keyword. */
10789 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10790 /* Look for the `('. */
10791 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10792 /* Parse the expression. */
10793 expression = cp_parser_expression (parser);
10794 /* We're done with the do-statement. */
10795 finish_do_stmt (expression, statement, ivdep);
10796 /* Look for the `)'. */
10797 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10798 /* Look for the `;'. */
10799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10801 break;
10803 case RID_FOR:
10805 /* Look for the `('. */
10806 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10808 statement = cp_parser_for (parser, ivdep);
10810 /* Look for the `)'. */
10811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10813 /* Parse the body of the for-statement. */
10814 parser->in_statement = IN_ITERATION_STMT;
10815 cp_parser_already_scoped_statement (parser);
10816 parser->in_statement = in_statement;
10818 /* We're done with the for-statement. */
10819 finish_for_stmt (statement);
10821 break;
10823 default:
10824 cp_parser_error (parser, "expected iteration-statement");
10825 statement = error_mark_node;
10826 break;
10829 return statement;
10832 /* Parse a for-init-statement or the declarator of a range-based-for.
10833 Returns true if a range-based-for declaration is seen.
10835 for-init-statement:
10836 expression-statement
10837 simple-declaration */
10839 static bool
10840 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10842 /* If the next token is a `;', then we have an empty
10843 expression-statement. Grammatically, this is also a
10844 simple-declaration, but an invalid one, because it does not
10845 declare anything. Therefore, if we did not handle this case
10846 specially, we would issue an error message about an invalid
10847 declaration. */
10848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10850 bool is_range_for = false;
10851 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10854 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10856 /* N3994 -- for (id : init) ... */
10857 if (cxx_dialect < cxx1z)
10858 pedwarn (input_location, 0, "range-based for loop without a "
10859 "type-specifier only available with "
10860 "-std=c++1z or -std=gnu++1z");
10861 tree name = cp_parser_identifier (parser);
10862 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10863 *decl = build_decl (input_location, VAR_DECL, name, type);
10864 pushdecl (*decl);
10865 cp_lexer_consume_token (parser->lexer);
10866 return true;
10869 /* A colon is used in range-based for. */
10870 parser->colon_corrects_to_scope_p = false;
10872 /* We're going to speculatively look for a declaration, falling back
10873 to an expression, if necessary. */
10874 cp_parser_parse_tentatively (parser);
10875 /* Parse the declaration. */
10876 cp_parser_simple_declaration (parser,
10877 /*function_definition_allowed_p=*/false,
10878 decl);
10879 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10880 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10882 /* It is a range-for, consume the ':' */
10883 cp_lexer_consume_token (parser->lexer);
10884 is_range_for = true;
10885 if (cxx_dialect < cxx11)
10887 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10888 "range-based %<for%> loops only available with "
10889 "-std=c++11 or -std=gnu++11");
10890 *decl = error_mark_node;
10893 else
10894 /* The ';' is not consumed yet because we told
10895 cp_parser_simple_declaration not to. */
10896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10898 if (cp_parser_parse_definitely (parser))
10899 return is_range_for;
10900 /* If the tentative parse failed, then we shall need to look for an
10901 expression-statement. */
10903 /* If we are here, it is an expression-statement. */
10904 cp_parser_expression_statement (parser, NULL_TREE);
10905 return false;
10908 /* Parse a jump-statement.
10910 jump-statement:
10911 break ;
10912 continue ;
10913 return expression [opt] ;
10914 return braced-init-list ;
10915 goto identifier ;
10917 GNU extension:
10919 jump-statement:
10920 goto * expression ;
10922 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10924 static tree
10925 cp_parser_jump_statement (cp_parser* parser)
10927 tree statement = error_mark_node;
10928 cp_token *token;
10929 enum rid keyword;
10930 unsigned char in_statement;
10932 /* Peek at the next token. */
10933 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10934 if (!token)
10935 return error_mark_node;
10937 /* See what kind of keyword it is. */
10938 keyword = token->keyword;
10939 switch (keyword)
10941 case RID_BREAK:
10942 in_statement = parser->in_statement & ~IN_IF_STMT;
10943 switch (in_statement)
10945 case 0:
10946 error_at (token->location, "break statement not within loop or switch");
10947 break;
10948 default:
10949 gcc_assert ((in_statement & IN_SWITCH_STMT)
10950 || in_statement == IN_ITERATION_STMT);
10951 statement = finish_break_stmt ();
10952 if (in_statement == IN_ITERATION_STMT)
10953 break_maybe_infinite_loop ();
10954 break;
10955 case IN_OMP_BLOCK:
10956 error_at (token->location, "invalid exit from OpenMP structured block");
10957 break;
10958 case IN_OMP_FOR:
10959 error_at (token->location, "break statement used with OpenMP for loop");
10960 break;
10961 case IN_CILK_SIMD_FOR:
10962 error_at (token->location, "break statement used with Cilk Plus for loop");
10963 break;
10965 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10966 break;
10968 case RID_CONTINUE:
10969 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10971 case 0:
10972 error_at (token->location, "continue statement not within a loop");
10973 break;
10974 case IN_CILK_SIMD_FOR:
10975 error_at (token->location,
10976 "continue statement within %<#pragma simd%> loop body");
10977 /* Fall through. */
10978 case IN_ITERATION_STMT:
10979 case IN_OMP_FOR:
10980 statement = finish_continue_stmt ();
10981 break;
10982 case IN_OMP_BLOCK:
10983 error_at (token->location, "invalid exit from OpenMP structured block");
10984 break;
10985 default:
10986 gcc_unreachable ();
10988 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10989 break;
10991 case RID_RETURN:
10993 tree expr;
10994 bool expr_non_constant_p;
10996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10998 cp_lexer_set_source_position (parser->lexer);
10999 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11000 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11002 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11003 expr = cp_parser_expression (parser);
11004 else
11005 /* If the next token is a `;', then there is no
11006 expression. */
11007 expr = NULL_TREE;
11008 /* Build the return-statement. */
11009 statement = finish_return_stmt (expr);
11010 /* Look for the final `;'. */
11011 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11013 break;
11015 case RID_GOTO:
11016 if (parser->in_function_body
11017 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11018 error ("%<goto%> in %<constexpr%> function");
11020 /* Create the goto-statement. */
11021 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11023 /* Issue a warning about this use of a GNU extension. */
11024 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11025 /* Consume the '*' token. */
11026 cp_lexer_consume_token (parser->lexer);
11027 /* Parse the dependent expression. */
11028 finish_goto_stmt (cp_parser_expression (parser));
11030 else
11031 finish_goto_stmt (cp_parser_identifier (parser));
11032 /* Look for the final `;'. */
11033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11034 break;
11036 default:
11037 cp_parser_error (parser, "expected jump-statement");
11038 break;
11041 return statement;
11044 /* Parse a declaration-statement.
11046 declaration-statement:
11047 block-declaration */
11049 static void
11050 cp_parser_declaration_statement (cp_parser* parser)
11052 void *p;
11054 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11055 p = obstack_alloc (&declarator_obstack, 0);
11057 /* Parse the block-declaration. */
11058 cp_parser_block_declaration (parser, /*statement_p=*/true);
11060 /* Free any declarators allocated. */
11061 obstack_free (&declarator_obstack, p);
11064 /* Some dependent statements (like `if (cond) statement'), are
11065 implicitly in their own scope. In other words, if the statement is
11066 a single statement (as opposed to a compound-statement), it is
11067 none-the-less treated as if it were enclosed in braces. Any
11068 declarations appearing in the dependent statement are out of scope
11069 after control passes that point. This function parses a statement,
11070 but ensures that is in its own scope, even if it is not a
11071 compound-statement.
11073 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11074 is a (possibly labeled) if statement which is not enclosed in
11075 braces and has an else clause. This is used to implement
11076 -Wparentheses.
11078 Returns the new statement. */
11080 static tree
11081 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11083 tree statement;
11085 if (if_p != NULL)
11086 *if_p = false;
11088 /* Mark if () ; with a special NOP_EXPR. */
11089 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11091 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11092 cp_lexer_consume_token (parser->lexer);
11093 statement = add_stmt (build_empty_stmt (loc));
11095 /* if a compound is opened, we simply parse the statement directly. */
11096 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11097 statement = cp_parser_compound_statement (parser, NULL, false, false);
11098 /* If the token is not a `{', then we must take special action. */
11099 else
11101 /* Create a compound-statement. */
11102 statement = begin_compound_stmt (0);
11103 /* Parse the dependent-statement. */
11104 cp_parser_statement (parser, NULL_TREE, false, if_p);
11105 /* Finish the dummy compound-statement. */
11106 finish_compound_stmt (statement);
11109 /* Return the statement. */
11110 return statement;
11113 /* For some dependent statements (like `while (cond) statement'), we
11114 have already created a scope. Therefore, even if the dependent
11115 statement is a compound-statement, we do not want to create another
11116 scope. */
11118 static void
11119 cp_parser_already_scoped_statement (cp_parser* parser)
11121 /* If the token is a `{', then we must take special action. */
11122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11123 cp_parser_statement (parser, NULL_TREE, false, NULL);
11124 else
11126 /* Avoid calling cp_parser_compound_statement, so that we
11127 don't create a new scope. Do everything else by hand. */
11128 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11129 /* If the next keyword is `__label__' we have a label declaration. */
11130 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11131 cp_parser_label_declaration (parser);
11132 /* Parse an (optional) statement-seq. */
11133 cp_parser_statement_seq_opt (parser, NULL_TREE);
11134 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11138 /* Declarations [gram.dcl.dcl] */
11140 /* Parse an optional declaration-sequence.
11142 declaration-seq:
11143 declaration
11144 declaration-seq declaration */
11146 static void
11147 cp_parser_declaration_seq_opt (cp_parser* parser)
11149 while (true)
11151 cp_token *token;
11153 token = cp_lexer_peek_token (parser->lexer);
11155 if (token->type == CPP_CLOSE_BRACE
11156 || token->type == CPP_EOF
11157 || token->type == CPP_PRAGMA_EOL)
11158 break;
11160 if (token->type == CPP_SEMICOLON)
11162 /* A declaration consisting of a single semicolon is
11163 invalid. Allow it unless we're being pedantic. */
11164 cp_lexer_consume_token (parser->lexer);
11165 if (!in_system_header_at (input_location))
11166 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11167 continue;
11170 /* If we're entering or exiting a region that's implicitly
11171 extern "C", modify the lang context appropriately. */
11172 if (!parser->implicit_extern_c && token->implicit_extern_c)
11174 push_lang_context (lang_name_c);
11175 parser->implicit_extern_c = true;
11177 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11179 pop_lang_context ();
11180 parser->implicit_extern_c = false;
11183 if (token->type == CPP_PRAGMA)
11185 /* A top-level declaration can consist solely of a #pragma.
11186 A nested declaration cannot, so this is done here and not
11187 in cp_parser_declaration. (A #pragma at block scope is
11188 handled in cp_parser_statement.) */
11189 cp_parser_pragma (parser, pragma_external);
11190 continue;
11193 /* Parse the declaration itself. */
11194 cp_parser_declaration (parser);
11198 /* Parse a declaration.
11200 declaration:
11201 block-declaration
11202 function-definition
11203 template-declaration
11204 explicit-instantiation
11205 explicit-specialization
11206 linkage-specification
11207 namespace-definition
11209 GNU extension:
11211 declaration:
11212 __extension__ declaration */
11214 static void
11215 cp_parser_declaration (cp_parser* parser)
11217 cp_token token1;
11218 cp_token token2;
11219 int saved_pedantic;
11220 void *p;
11221 tree attributes = NULL_TREE;
11223 /* Check for the `__extension__' keyword. */
11224 if (cp_parser_extension_opt (parser, &saved_pedantic))
11226 /* Parse the qualified declaration. */
11227 cp_parser_declaration (parser);
11228 /* Restore the PEDANTIC flag. */
11229 pedantic = saved_pedantic;
11231 return;
11234 /* Try to figure out what kind of declaration is present. */
11235 token1 = *cp_lexer_peek_token (parser->lexer);
11237 if (token1.type != CPP_EOF)
11238 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11239 else
11241 token2.type = CPP_EOF;
11242 token2.keyword = RID_MAX;
11245 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11246 p = obstack_alloc (&declarator_obstack, 0);
11248 /* If the next token is `extern' and the following token is a string
11249 literal, then we have a linkage specification. */
11250 if (token1.keyword == RID_EXTERN
11251 && cp_parser_is_pure_string_literal (&token2))
11252 cp_parser_linkage_specification (parser);
11253 /* If the next token is `template', then we have either a template
11254 declaration, an explicit instantiation, or an explicit
11255 specialization. */
11256 else if (token1.keyword == RID_TEMPLATE)
11258 /* `template <>' indicates a template specialization. */
11259 if (token2.type == CPP_LESS
11260 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11261 cp_parser_explicit_specialization (parser);
11262 /* `template <' indicates a template declaration. */
11263 else if (token2.type == CPP_LESS)
11264 cp_parser_template_declaration (parser, /*member_p=*/false);
11265 /* Anything else must be an explicit instantiation. */
11266 else
11267 cp_parser_explicit_instantiation (parser);
11269 /* If the next token is `export', then we have a template
11270 declaration. */
11271 else if (token1.keyword == RID_EXPORT)
11272 cp_parser_template_declaration (parser, /*member_p=*/false);
11273 /* If the next token is `extern', 'static' or 'inline' and the one
11274 after that is `template', we have a GNU extended explicit
11275 instantiation directive. */
11276 else if (cp_parser_allow_gnu_extensions_p (parser)
11277 && (token1.keyword == RID_EXTERN
11278 || token1.keyword == RID_STATIC
11279 || token1.keyword == RID_INLINE)
11280 && token2.keyword == RID_TEMPLATE)
11281 cp_parser_explicit_instantiation (parser);
11282 /* If the next token is `namespace', check for a named or unnamed
11283 namespace definition. */
11284 else if (token1.keyword == RID_NAMESPACE
11285 && (/* A named namespace definition. */
11286 (token2.type == CPP_NAME
11287 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11288 != CPP_EQ))
11289 /* An unnamed namespace definition. */
11290 || token2.type == CPP_OPEN_BRACE
11291 || token2.keyword == RID_ATTRIBUTE))
11292 cp_parser_namespace_definition (parser);
11293 /* An inline (associated) namespace definition. */
11294 else if (token1.keyword == RID_INLINE
11295 && token2.keyword == RID_NAMESPACE)
11296 cp_parser_namespace_definition (parser);
11297 /* Objective-C++ declaration/definition. */
11298 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11299 cp_parser_objc_declaration (parser, NULL_TREE);
11300 else if (c_dialect_objc ()
11301 && token1.keyword == RID_ATTRIBUTE
11302 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11303 cp_parser_objc_declaration (parser, attributes);
11304 /* We must have either a block declaration or a function
11305 definition. */
11306 else
11307 /* Try to parse a block-declaration, or a function-definition. */
11308 cp_parser_block_declaration (parser, /*statement_p=*/false);
11310 /* Free any declarators allocated. */
11311 obstack_free (&declarator_obstack, p);
11314 /* Parse a block-declaration.
11316 block-declaration:
11317 simple-declaration
11318 asm-definition
11319 namespace-alias-definition
11320 using-declaration
11321 using-directive
11323 GNU Extension:
11325 block-declaration:
11326 __extension__ block-declaration
11328 C++0x Extension:
11330 block-declaration:
11331 static_assert-declaration
11333 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11334 part of a declaration-statement. */
11336 static void
11337 cp_parser_block_declaration (cp_parser *parser,
11338 bool statement_p)
11340 cp_token *token1;
11341 int saved_pedantic;
11343 /* Check for the `__extension__' keyword. */
11344 if (cp_parser_extension_opt (parser, &saved_pedantic))
11346 /* Parse the qualified declaration. */
11347 cp_parser_block_declaration (parser, statement_p);
11348 /* Restore the PEDANTIC flag. */
11349 pedantic = saved_pedantic;
11351 return;
11354 /* Peek at the next token to figure out which kind of declaration is
11355 present. */
11356 token1 = cp_lexer_peek_token (parser->lexer);
11358 /* If the next keyword is `asm', we have an asm-definition. */
11359 if (token1->keyword == RID_ASM)
11361 if (statement_p)
11362 cp_parser_commit_to_tentative_parse (parser);
11363 cp_parser_asm_definition (parser);
11365 /* If the next keyword is `namespace', we have a
11366 namespace-alias-definition. */
11367 else if (token1->keyword == RID_NAMESPACE)
11368 cp_parser_namespace_alias_definition (parser);
11369 /* If the next keyword is `using', we have a
11370 using-declaration, a using-directive, or an alias-declaration. */
11371 else if (token1->keyword == RID_USING)
11373 cp_token *token2;
11375 if (statement_p)
11376 cp_parser_commit_to_tentative_parse (parser);
11377 /* If the token after `using' is `namespace', then we have a
11378 using-directive. */
11379 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11380 if (token2->keyword == RID_NAMESPACE)
11381 cp_parser_using_directive (parser);
11382 /* If the second token after 'using' is '=', then we have an
11383 alias-declaration. */
11384 else if (cxx_dialect >= cxx11
11385 && token2->type == CPP_NAME
11386 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11387 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11388 cp_parser_alias_declaration (parser);
11389 /* Otherwise, it's a using-declaration. */
11390 else
11391 cp_parser_using_declaration (parser,
11392 /*access_declaration_p=*/false);
11394 /* If the next keyword is `__label__' we have a misplaced label
11395 declaration. */
11396 else if (token1->keyword == RID_LABEL)
11398 cp_lexer_consume_token (parser->lexer);
11399 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11400 cp_parser_skip_to_end_of_statement (parser);
11401 /* If the next token is now a `;', consume it. */
11402 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11403 cp_lexer_consume_token (parser->lexer);
11405 /* If the next token is `static_assert' we have a static assertion. */
11406 else if (token1->keyword == RID_STATIC_ASSERT)
11407 cp_parser_static_assert (parser, /*member_p=*/false);
11408 /* Anything else must be a simple-declaration. */
11409 else
11410 cp_parser_simple_declaration (parser, !statement_p,
11411 /*maybe_range_for_decl*/NULL);
11414 /* Parse a simple-declaration.
11416 simple-declaration:
11417 decl-specifier-seq [opt] init-declarator-list [opt] ;
11419 init-declarator-list:
11420 init-declarator
11421 init-declarator-list , init-declarator
11423 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11424 function-definition as a simple-declaration.
11426 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11427 parsed declaration if it is an uninitialized single declarator not followed
11428 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11429 if present, will not be consumed. */
11431 static void
11432 cp_parser_simple_declaration (cp_parser* parser,
11433 bool function_definition_allowed_p,
11434 tree *maybe_range_for_decl)
11436 cp_decl_specifier_seq decl_specifiers;
11437 int declares_class_or_enum;
11438 bool saw_declarator;
11440 if (maybe_range_for_decl)
11441 *maybe_range_for_decl = NULL_TREE;
11443 /* Defer access checks until we know what is being declared; the
11444 checks for names appearing in the decl-specifier-seq should be
11445 done as if we were in the scope of the thing being declared. */
11446 push_deferring_access_checks (dk_deferred);
11448 /* Parse the decl-specifier-seq. We have to keep track of whether
11449 or not the decl-specifier-seq declares a named class or
11450 enumeration type, since that is the only case in which the
11451 init-declarator-list is allowed to be empty.
11453 [dcl.dcl]
11455 In a simple-declaration, the optional init-declarator-list can be
11456 omitted only when declaring a class or enumeration, that is when
11457 the decl-specifier-seq contains either a class-specifier, an
11458 elaborated-type-specifier, or an enum-specifier. */
11459 cp_parser_decl_specifier_seq (parser,
11460 CP_PARSER_FLAGS_OPTIONAL,
11461 &decl_specifiers,
11462 &declares_class_or_enum);
11463 /* We no longer need to defer access checks. */
11464 stop_deferring_access_checks ();
11466 /* In a block scope, a valid declaration must always have a
11467 decl-specifier-seq. By not trying to parse declarators, we can
11468 resolve the declaration/expression ambiguity more quickly. */
11469 if (!function_definition_allowed_p
11470 && !decl_specifiers.any_specifiers_p)
11472 cp_parser_error (parser, "expected declaration");
11473 goto done;
11476 /* If the next two tokens are both identifiers, the code is
11477 erroneous. The usual cause of this situation is code like:
11479 T t;
11481 where "T" should name a type -- but does not. */
11482 if (!decl_specifiers.any_type_specifiers_p
11483 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11485 /* If parsing tentatively, we should commit; we really are
11486 looking at a declaration. */
11487 cp_parser_commit_to_tentative_parse (parser);
11488 /* Give up. */
11489 goto done;
11492 /* If we have seen at least one decl-specifier, and the next token
11493 is not a parenthesis, then we must be looking at a declaration.
11494 (After "int (" we might be looking at a functional cast.) */
11495 if (decl_specifiers.any_specifiers_p
11496 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11497 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11498 && !cp_parser_error_occurred (parser))
11499 cp_parser_commit_to_tentative_parse (parser);
11501 /* Keep going until we hit the `;' at the end of the simple
11502 declaration. */
11503 saw_declarator = false;
11504 while (cp_lexer_next_token_is_not (parser->lexer,
11505 CPP_SEMICOLON))
11507 cp_token *token;
11508 bool function_definition_p;
11509 tree decl;
11511 if (saw_declarator)
11513 /* If we are processing next declarator, coma is expected */
11514 token = cp_lexer_peek_token (parser->lexer);
11515 gcc_assert (token->type == CPP_COMMA);
11516 cp_lexer_consume_token (parser->lexer);
11517 if (maybe_range_for_decl)
11518 *maybe_range_for_decl = error_mark_node;
11520 else
11521 saw_declarator = true;
11523 /* Parse the init-declarator. */
11524 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11525 /*checks=*/NULL,
11526 function_definition_allowed_p,
11527 /*member_p=*/false,
11528 declares_class_or_enum,
11529 &function_definition_p,
11530 maybe_range_for_decl);
11531 /* If an error occurred while parsing tentatively, exit quickly.
11532 (That usually happens when in the body of a function; each
11533 statement is treated as a declaration-statement until proven
11534 otherwise.) */
11535 if (cp_parser_error_occurred (parser))
11536 goto done;
11537 /* Handle function definitions specially. */
11538 if (function_definition_p)
11540 /* If the next token is a `,', then we are probably
11541 processing something like:
11543 void f() {}, *p;
11545 which is erroneous. */
11546 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11548 cp_token *token = cp_lexer_peek_token (parser->lexer);
11549 error_at (token->location,
11550 "mixing"
11551 " declarations and function-definitions is forbidden");
11553 /* Otherwise, we're done with the list of declarators. */
11554 else
11556 pop_deferring_access_checks ();
11557 return;
11560 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11561 *maybe_range_for_decl = decl;
11562 /* The next token should be either a `,' or a `;'. */
11563 token = cp_lexer_peek_token (parser->lexer);
11564 /* If it's a `,', there are more declarators to come. */
11565 if (token->type == CPP_COMMA)
11566 /* will be consumed next time around */;
11567 /* If it's a `;', we are done. */
11568 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11569 break;
11570 /* Anything else is an error. */
11571 else
11573 /* If we have already issued an error message we don't need
11574 to issue another one. */
11575 if (decl != error_mark_node
11576 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11577 cp_parser_error (parser, "expected %<,%> or %<;%>");
11578 /* Skip tokens until we reach the end of the statement. */
11579 cp_parser_skip_to_end_of_statement (parser);
11580 /* If the next token is now a `;', consume it. */
11581 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11582 cp_lexer_consume_token (parser->lexer);
11583 goto done;
11585 /* After the first time around, a function-definition is not
11586 allowed -- even if it was OK at first. For example:
11588 int i, f() {}
11590 is not valid. */
11591 function_definition_allowed_p = false;
11594 /* Issue an error message if no declarators are present, and the
11595 decl-specifier-seq does not itself declare a class or
11596 enumeration: [dcl.dcl]/3. */
11597 if (!saw_declarator)
11599 if (cp_parser_declares_only_class_p (parser))
11601 if (!declares_class_or_enum
11602 && decl_specifiers.type
11603 && OVERLOAD_TYPE_P (decl_specifiers.type))
11604 /* Ensure an error is issued anyway when finish_decltype_type,
11605 called via cp_parser_decl_specifier_seq, returns a class or
11606 an enumeration (c++/51786). */
11607 decl_specifiers.type = NULL_TREE;
11608 shadow_tag (&decl_specifiers);
11610 /* Perform any deferred access checks. */
11611 perform_deferred_access_checks (tf_warning_or_error);
11614 /* Consume the `;'. */
11615 if (!maybe_range_for_decl)
11616 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11618 done:
11619 pop_deferring_access_checks ();
11622 /* Parse a decl-specifier-seq.
11624 decl-specifier-seq:
11625 decl-specifier-seq [opt] decl-specifier
11626 decl-specifier attribute-specifier-seq [opt] (C++11)
11628 decl-specifier:
11629 storage-class-specifier
11630 type-specifier
11631 function-specifier
11632 friend
11633 typedef
11635 GNU Extension:
11637 decl-specifier:
11638 attributes
11640 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11642 The parser flags FLAGS is used to control type-specifier parsing.
11644 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11645 flags:
11647 1: one of the decl-specifiers is an elaborated-type-specifier
11648 (i.e., a type declaration)
11649 2: one of the decl-specifiers is an enum-specifier or a
11650 class-specifier (i.e., a type definition)
11654 static void
11655 cp_parser_decl_specifier_seq (cp_parser* parser,
11656 cp_parser_flags flags,
11657 cp_decl_specifier_seq *decl_specs,
11658 int* declares_class_or_enum)
11660 bool constructor_possible_p = !parser->in_declarator_p;
11661 bool found_decl_spec = false;
11662 cp_token *start_token = NULL;
11663 cp_decl_spec ds;
11665 /* Clear DECL_SPECS. */
11666 clear_decl_specs (decl_specs);
11668 /* Assume no class or enumeration type is declared. */
11669 *declares_class_or_enum = 0;
11671 /* Keep reading specifiers until there are no more to read. */
11672 while (true)
11674 bool constructor_p;
11675 cp_token *token;
11676 ds = ds_last;
11678 /* Peek at the next token. */
11679 token = cp_lexer_peek_token (parser->lexer);
11681 /* Save the first token of the decl spec list for error
11682 reporting. */
11683 if (!start_token)
11684 start_token = token;
11685 /* Handle attributes. */
11686 if (cp_next_tokens_can_be_attribute_p (parser))
11688 /* Parse the attributes. */
11689 tree attrs = cp_parser_attributes_opt (parser);
11691 /* In a sequence of declaration specifiers, c++11 attributes
11692 appertain to the type that precede them. In that case
11693 [dcl.spec]/1 says:
11695 The attribute-specifier-seq affects the type only for
11696 the declaration it appears in, not other declarations
11697 involving the same type.
11699 But for now let's force the user to position the
11700 attribute either at the beginning of the declaration or
11701 after the declarator-id, which would clearly mean that it
11702 applies to the declarator. */
11703 if (cxx11_attribute_p (attrs))
11705 if (!found_decl_spec)
11706 /* The c++11 attribute is at the beginning of the
11707 declaration. It appertains to the entity being
11708 declared. */;
11709 else
11711 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11713 /* This is an attribute following a
11714 class-specifier. */
11715 if (decl_specs->type_definition_p)
11716 warn_misplaced_attr_for_class_type (token->location,
11717 decl_specs->type);
11718 attrs = NULL_TREE;
11720 else
11722 decl_specs->std_attributes
11723 = chainon (decl_specs->std_attributes,
11724 attrs);
11725 if (decl_specs->locations[ds_std_attribute] == 0)
11726 decl_specs->locations[ds_std_attribute] = token->location;
11728 continue;
11732 decl_specs->attributes
11733 = chainon (decl_specs->attributes,
11734 attrs);
11735 if (decl_specs->locations[ds_attribute] == 0)
11736 decl_specs->locations[ds_attribute] = token->location;
11737 continue;
11739 /* Assume we will find a decl-specifier keyword. */
11740 found_decl_spec = true;
11741 /* If the next token is an appropriate keyword, we can simply
11742 add it to the list. */
11743 switch (token->keyword)
11745 /* decl-specifier:
11746 friend
11747 constexpr */
11748 case RID_FRIEND:
11749 if (!at_class_scope_p ())
11751 error_at (token->location, "%<friend%> used outside of class");
11752 cp_lexer_purge_token (parser->lexer);
11754 else
11756 ds = ds_friend;
11757 /* Consume the token. */
11758 cp_lexer_consume_token (parser->lexer);
11760 break;
11762 case RID_CONSTEXPR:
11763 ds = ds_constexpr;
11764 cp_lexer_consume_token (parser->lexer);
11765 break;
11767 /* function-specifier:
11768 inline
11769 virtual
11770 explicit */
11771 case RID_INLINE:
11772 case RID_VIRTUAL:
11773 case RID_EXPLICIT:
11774 cp_parser_function_specifier_opt (parser, decl_specs);
11775 break;
11777 /* decl-specifier:
11778 typedef */
11779 case RID_TYPEDEF:
11780 ds = ds_typedef;
11781 /* Consume the token. */
11782 cp_lexer_consume_token (parser->lexer);
11783 /* A constructor declarator cannot appear in a typedef. */
11784 constructor_possible_p = false;
11785 /* The "typedef" keyword can only occur in a declaration; we
11786 may as well commit at this point. */
11787 cp_parser_commit_to_tentative_parse (parser);
11789 if (decl_specs->storage_class != sc_none)
11790 decl_specs->conflicting_specifiers_p = true;
11791 break;
11793 /* storage-class-specifier:
11794 auto
11795 register
11796 static
11797 extern
11798 mutable
11800 GNU Extension:
11801 thread */
11802 case RID_AUTO:
11803 if (cxx_dialect == cxx98)
11805 /* Consume the token. */
11806 cp_lexer_consume_token (parser->lexer);
11808 /* Complain about `auto' as a storage specifier, if
11809 we're complaining about C++0x compatibility. */
11810 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11811 " changes meaning in C++11; please remove it");
11813 /* Set the storage class anyway. */
11814 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11815 token);
11817 else
11818 /* C++0x auto type-specifier. */
11819 found_decl_spec = false;
11820 break;
11822 case RID_REGISTER:
11823 case RID_STATIC:
11824 case RID_EXTERN:
11825 case RID_MUTABLE:
11826 /* Consume the token. */
11827 cp_lexer_consume_token (parser->lexer);
11828 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11829 token);
11830 break;
11831 case RID_THREAD:
11832 /* Consume the token. */
11833 ds = ds_thread;
11834 cp_lexer_consume_token (parser->lexer);
11835 break;
11837 default:
11838 /* We did not yet find a decl-specifier yet. */
11839 found_decl_spec = false;
11840 break;
11843 if (found_decl_spec
11844 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11845 && token->keyword != RID_CONSTEXPR)
11846 error ("decl-specifier invalid in condition");
11848 if (ds != ds_last)
11849 set_and_check_decl_spec_loc (decl_specs, ds, token);
11851 /* Constructors are a special case. The `S' in `S()' is not a
11852 decl-specifier; it is the beginning of the declarator. */
11853 constructor_p
11854 = (!found_decl_spec
11855 && constructor_possible_p
11856 && (cp_parser_constructor_declarator_p
11857 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11859 /* If we don't have a DECL_SPEC yet, then we must be looking at
11860 a type-specifier. */
11861 if (!found_decl_spec && !constructor_p)
11863 int decl_spec_declares_class_or_enum;
11864 bool is_cv_qualifier;
11865 tree type_spec;
11867 type_spec
11868 = cp_parser_type_specifier (parser, flags,
11869 decl_specs,
11870 /*is_declaration=*/true,
11871 &decl_spec_declares_class_or_enum,
11872 &is_cv_qualifier);
11873 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11875 /* If this type-specifier referenced a user-defined type
11876 (a typedef, class-name, etc.), then we can't allow any
11877 more such type-specifiers henceforth.
11879 [dcl.spec]
11881 The longest sequence of decl-specifiers that could
11882 possibly be a type name is taken as the
11883 decl-specifier-seq of a declaration. The sequence shall
11884 be self-consistent as described below.
11886 [dcl.type]
11888 As a general rule, at most one type-specifier is allowed
11889 in the complete decl-specifier-seq of a declaration. The
11890 only exceptions are the following:
11892 -- const or volatile can be combined with any other
11893 type-specifier.
11895 -- signed or unsigned can be combined with char, long,
11896 short, or int.
11898 -- ..
11900 Example:
11902 typedef char* Pc;
11903 void g (const int Pc);
11905 Here, Pc is *not* part of the decl-specifier seq; it's
11906 the declarator. Therefore, once we see a type-specifier
11907 (other than a cv-qualifier), we forbid any additional
11908 user-defined types. We *do* still allow things like `int
11909 int' to be considered a decl-specifier-seq, and issue the
11910 error message later. */
11911 if (type_spec && !is_cv_qualifier)
11912 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11913 /* A constructor declarator cannot follow a type-specifier. */
11914 if (type_spec)
11916 constructor_possible_p = false;
11917 found_decl_spec = true;
11918 if (!is_cv_qualifier)
11919 decl_specs->any_type_specifiers_p = true;
11923 /* If we still do not have a DECL_SPEC, then there are no more
11924 decl-specifiers. */
11925 if (!found_decl_spec)
11926 break;
11928 decl_specs->any_specifiers_p = true;
11929 /* After we see one decl-specifier, further decl-specifiers are
11930 always optional. */
11931 flags |= CP_PARSER_FLAGS_OPTIONAL;
11934 /* Don't allow a friend specifier with a class definition. */
11935 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11936 && (*declares_class_or_enum & 2))
11937 error_at (decl_specs->locations[ds_friend],
11938 "class definition may not be declared a friend");
11941 /* Parse an (optional) storage-class-specifier.
11943 storage-class-specifier:
11944 auto
11945 register
11946 static
11947 extern
11948 mutable
11950 GNU Extension:
11952 storage-class-specifier:
11953 thread
11955 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11957 static tree
11958 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11960 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11962 case RID_AUTO:
11963 if (cxx_dialect != cxx98)
11964 return NULL_TREE;
11965 /* Fall through for C++98. */
11967 case RID_REGISTER:
11968 case RID_STATIC:
11969 case RID_EXTERN:
11970 case RID_MUTABLE:
11971 case RID_THREAD:
11972 /* Consume the token. */
11973 return cp_lexer_consume_token (parser->lexer)->u.value;
11975 default:
11976 return NULL_TREE;
11980 /* Parse an (optional) function-specifier.
11982 function-specifier:
11983 inline
11984 virtual
11985 explicit
11987 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11988 Updates DECL_SPECS, if it is non-NULL. */
11990 static tree
11991 cp_parser_function_specifier_opt (cp_parser* parser,
11992 cp_decl_specifier_seq *decl_specs)
11994 cp_token *token = cp_lexer_peek_token (parser->lexer);
11995 switch (token->keyword)
11997 case RID_INLINE:
11998 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11999 break;
12001 case RID_VIRTUAL:
12002 /* 14.5.2.3 [temp.mem]
12004 A member function template shall not be virtual. */
12005 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12006 error_at (token->location, "templates may not be %<virtual%>");
12007 else
12008 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12009 break;
12011 case RID_EXPLICIT:
12012 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12013 break;
12015 default:
12016 return NULL_TREE;
12019 /* Consume the token. */
12020 return cp_lexer_consume_token (parser->lexer)->u.value;
12023 /* Parse a linkage-specification.
12025 linkage-specification:
12026 extern string-literal { declaration-seq [opt] }
12027 extern string-literal declaration */
12029 static void
12030 cp_parser_linkage_specification (cp_parser* parser)
12032 tree linkage;
12034 /* Look for the `extern' keyword. */
12035 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12037 /* Look for the string-literal. */
12038 linkage = cp_parser_string_literal (parser, false, false);
12040 /* Transform the literal into an identifier. If the literal is a
12041 wide-character string, or contains embedded NULs, then we can't
12042 handle it as the user wants. */
12043 if (strlen (TREE_STRING_POINTER (linkage))
12044 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12046 cp_parser_error (parser, "invalid linkage-specification");
12047 /* Assume C++ linkage. */
12048 linkage = lang_name_cplusplus;
12050 else
12051 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12053 /* We're now using the new linkage. */
12054 push_lang_context (linkage);
12056 /* If the next token is a `{', then we're using the first
12057 production. */
12058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12060 cp_ensure_no_omp_declare_simd (parser);
12062 /* Consume the `{' token. */
12063 cp_lexer_consume_token (parser->lexer);
12064 /* Parse the declarations. */
12065 cp_parser_declaration_seq_opt (parser);
12066 /* Look for the closing `}'. */
12067 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12069 /* Otherwise, there's just one declaration. */
12070 else
12072 bool saved_in_unbraced_linkage_specification_p;
12074 saved_in_unbraced_linkage_specification_p
12075 = parser->in_unbraced_linkage_specification_p;
12076 parser->in_unbraced_linkage_specification_p = true;
12077 cp_parser_declaration (parser);
12078 parser->in_unbraced_linkage_specification_p
12079 = saved_in_unbraced_linkage_specification_p;
12082 /* We're done with the linkage-specification. */
12083 pop_lang_context ();
12086 /* Parse a static_assert-declaration.
12088 static_assert-declaration:
12089 static_assert ( constant-expression , string-literal ) ;
12091 If MEMBER_P, this static_assert is a class member. */
12093 static void
12094 cp_parser_static_assert(cp_parser *parser, bool member_p)
12096 tree condition;
12097 tree message;
12098 cp_token *token;
12099 location_t saved_loc;
12100 bool dummy;
12102 /* Peek at the `static_assert' token so we can keep track of exactly
12103 where the static assertion started. */
12104 token = cp_lexer_peek_token (parser->lexer);
12105 saved_loc = token->location;
12107 /* Look for the `static_assert' keyword. */
12108 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12109 RT_STATIC_ASSERT))
12110 return;
12112 /* We know we are in a static assertion; commit to any tentative
12113 parse. */
12114 if (cp_parser_parsing_tentatively (parser))
12115 cp_parser_commit_to_tentative_parse (parser);
12117 /* Parse the `(' starting the static assertion condition. */
12118 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12120 /* Parse the constant-expression. Allow a non-constant expression
12121 here in order to give better diagnostics in finish_static_assert. */
12122 condition =
12123 cp_parser_constant_expression (parser,
12124 /*allow_non_constant_p=*/true,
12125 /*non_constant_p=*/&dummy);
12127 /* Parse the separating `,'. */
12128 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12130 /* Parse the string-literal message. */
12131 message = cp_parser_string_literal (parser,
12132 /*translate=*/false,
12133 /*wide_ok=*/true);
12135 /* A `)' completes the static assertion. */
12136 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12137 cp_parser_skip_to_closing_parenthesis (parser,
12138 /*recovering=*/true,
12139 /*or_comma=*/false,
12140 /*consume_paren=*/true);
12142 /* A semicolon terminates the declaration. */
12143 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12145 /* Complete the static assertion, which may mean either processing
12146 the static assert now or saving it for template instantiation. */
12147 finish_static_assert (condition, message, saved_loc, member_p);
12150 /* Parse the expression in decltype ( expression ). */
12152 static tree
12153 cp_parser_decltype_expr (cp_parser *parser,
12154 bool &id_expression_or_member_access_p)
12156 cp_token *id_expr_start_token;
12157 tree expr;
12159 /* First, try parsing an id-expression. */
12160 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12161 cp_parser_parse_tentatively (parser);
12162 expr = cp_parser_id_expression (parser,
12163 /*template_keyword_p=*/false,
12164 /*check_dependency_p=*/true,
12165 /*template_p=*/NULL,
12166 /*declarator_p=*/false,
12167 /*optional_p=*/false);
12169 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12171 bool non_integral_constant_expression_p = false;
12172 tree id_expression = expr;
12173 cp_id_kind idk;
12174 const char *error_msg;
12176 if (identifier_p (expr))
12177 /* Lookup the name we got back from the id-expression. */
12178 expr = cp_parser_lookup_name_simple (parser, expr,
12179 id_expr_start_token->location);
12181 if (expr
12182 && expr != error_mark_node
12183 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12184 && TREE_CODE (expr) != TYPE_DECL
12185 && (TREE_CODE (expr) != BIT_NOT_EXPR
12186 || !TYPE_P (TREE_OPERAND (expr, 0)))
12187 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12189 /* Complete lookup of the id-expression. */
12190 expr = (finish_id_expression
12191 (id_expression, expr, parser->scope, &idk,
12192 /*integral_constant_expression_p=*/false,
12193 /*allow_non_integral_constant_expression_p=*/true,
12194 &non_integral_constant_expression_p,
12195 /*template_p=*/false,
12196 /*done=*/true,
12197 /*address_p=*/false,
12198 /*template_arg_p=*/false,
12199 &error_msg,
12200 id_expr_start_token->location));
12202 if (expr == error_mark_node)
12203 /* We found an id-expression, but it was something that we
12204 should not have found. This is an error, not something
12205 we can recover from, so note that we found an
12206 id-expression and we'll recover as gracefully as
12207 possible. */
12208 id_expression_or_member_access_p = true;
12211 if (expr
12212 && expr != error_mark_node
12213 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12214 /* We have an id-expression. */
12215 id_expression_or_member_access_p = true;
12218 if (!id_expression_or_member_access_p)
12220 /* Abort the id-expression parse. */
12221 cp_parser_abort_tentative_parse (parser);
12223 /* Parsing tentatively, again. */
12224 cp_parser_parse_tentatively (parser);
12226 /* Parse a class member access. */
12227 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12228 /*cast_p=*/false, /*decltype*/true,
12229 /*member_access_only_p=*/true, NULL);
12231 if (expr
12232 && expr != error_mark_node
12233 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12234 /* We have an id-expression. */
12235 id_expression_or_member_access_p = true;
12238 if (id_expression_or_member_access_p)
12239 /* We have parsed the complete id-expression or member access. */
12240 cp_parser_parse_definitely (parser);
12241 else
12243 /* Abort our attempt to parse an id-expression or member access
12244 expression. */
12245 cp_parser_abort_tentative_parse (parser);
12247 /* Parse a full expression. */
12248 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12249 /*decltype_p=*/true);
12252 return expr;
12255 /* Parse a `decltype' type. Returns the type.
12257 simple-type-specifier:
12258 decltype ( expression )
12259 C++14 proposal:
12260 decltype ( auto ) */
12262 static tree
12263 cp_parser_decltype (cp_parser *parser)
12265 tree expr;
12266 bool id_expression_or_member_access_p = false;
12267 const char *saved_message;
12268 bool saved_integral_constant_expression_p;
12269 bool saved_non_integral_constant_expression_p;
12270 bool saved_greater_than_is_operator_p;
12271 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12273 if (start_token->type == CPP_DECLTYPE)
12275 /* Already parsed. */
12276 cp_lexer_consume_token (parser->lexer);
12277 return start_token->u.value;
12280 /* Look for the `decltype' token. */
12281 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12282 return error_mark_node;
12284 /* Parse the opening `('. */
12285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12286 return error_mark_node;
12288 /* decltype (auto) */
12289 if (cxx_dialect >= cxx14
12290 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12292 cp_lexer_consume_token (parser->lexer);
12293 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12294 return error_mark_node;
12295 expr = make_decltype_auto ();
12296 AUTO_IS_DECLTYPE (expr) = true;
12297 goto rewrite;
12300 /* Types cannot be defined in a `decltype' expression. Save away the
12301 old message. */
12302 saved_message = parser->type_definition_forbidden_message;
12304 /* And create the new one. */
12305 parser->type_definition_forbidden_message
12306 = G_("types may not be defined in %<decltype%> expressions");
12308 /* The restrictions on constant-expressions do not apply inside
12309 decltype expressions. */
12310 saved_integral_constant_expression_p
12311 = parser->integral_constant_expression_p;
12312 saved_non_integral_constant_expression_p
12313 = parser->non_integral_constant_expression_p;
12314 parser->integral_constant_expression_p = false;
12316 /* Within a parenthesized expression, a `>' token is always
12317 the greater-than operator. */
12318 saved_greater_than_is_operator_p
12319 = parser->greater_than_is_operator_p;
12320 parser->greater_than_is_operator_p = true;
12322 /* Do not actually evaluate the expression. */
12323 ++cp_unevaluated_operand;
12325 /* Do not warn about problems with the expression. */
12326 ++c_inhibit_evaluation_warnings;
12328 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12330 /* Go back to evaluating expressions. */
12331 --cp_unevaluated_operand;
12332 --c_inhibit_evaluation_warnings;
12334 /* The `>' token might be the end of a template-id or
12335 template-parameter-list now. */
12336 parser->greater_than_is_operator_p
12337 = saved_greater_than_is_operator_p;
12339 /* Restore the old message and the integral constant expression
12340 flags. */
12341 parser->type_definition_forbidden_message = saved_message;
12342 parser->integral_constant_expression_p
12343 = saved_integral_constant_expression_p;
12344 parser->non_integral_constant_expression_p
12345 = saved_non_integral_constant_expression_p;
12347 /* Parse to the closing `)'. */
12348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12350 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12351 /*consume_paren=*/true);
12352 return error_mark_node;
12355 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12356 tf_warning_or_error);
12358 rewrite:
12359 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12360 it again. */
12361 start_token->type = CPP_DECLTYPE;
12362 start_token->u.value = expr;
12363 start_token->keyword = RID_MAX;
12364 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12366 return expr;
12369 /* Special member functions [gram.special] */
12371 /* Parse a conversion-function-id.
12373 conversion-function-id:
12374 operator conversion-type-id
12376 Returns an IDENTIFIER_NODE representing the operator. */
12378 static tree
12379 cp_parser_conversion_function_id (cp_parser* parser)
12381 tree type;
12382 tree saved_scope;
12383 tree saved_qualifying_scope;
12384 tree saved_object_scope;
12385 tree pushed_scope = NULL_TREE;
12387 /* Look for the `operator' token. */
12388 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12389 return error_mark_node;
12390 /* When we parse the conversion-type-id, the current scope will be
12391 reset. However, we need that information in able to look up the
12392 conversion function later, so we save it here. */
12393 saved_scope = parser->scope;
12394 saved_qualifying_scope = parser->qualifying_scope;
12395 saved_object_scope = parser->object_scope;
12396 /* We must enter the scope of the class so that the names of
12397 entities declared within the class are available in the
12398 conversion-type-id. For example, consider:
12400 struct S {
12401 typedef int I;
12402 operator I();
12405 S::operator I() { ... }
12407 In order to see that `I' is a type-name in the definition, we
12408 must be in the scope of `S'. */
12409 if (saved_scope)
12410 pushed_scope = push_scope (saved_scope);
12411 /* Parse the conversion-type-id. */
12412 type = cp_parser_conversion_type_id (parser);
12413 /* Leave the scope of the class, if any. */
12414 if (pushed_scope)
12415 pop_scope (pushed_scope);
12416 /* Restore the saved scope. */
12417 parser->scope = saved_scope;
12418 parser->qualifying_scope = saved_qualifying_scope;
12419 parser->object_scope = saved_object_scope;
12420 /* If the TYPE is invalid, indicate failure. */
12421 if (type == error_mark_node)
12422 return error_mark_node;
12423 return mangle_conv_op_name_for_type (type);
12426 /* Parse a conversion-type-id:
12428 conversion-type-id:
12429 type-specifier-seq conversion-declarator [opt]
12431 Returns the TYPE specified. */
12433 static tree
12434 cp_parser_conversion_type_id (cp_parser* parser)
12436 tree attributes;
12437 cp_decl_specifier_seq type_specifiers;
12438 cp_declarator *declarator;
12439 tree type_specified;
12440 const char *saved_message;
12442 /* Parse the attributes. */
12443 attributes = cp_parser_attributes_opt (parser);
12445 saved_message = parser->type_definition_forbidden_message;
12446 parser->type_definition_forbidden_message
12447 = G_("types may not be defined in a conversion-type-id");
12449 /* Parse the type-specifiers. */
12450 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12451 /*is_trailing_return=*/false,
12452 &type_specifiers);
12454 parser->type_definition_forbidden_message = saved_message;
12456 /* If that didn't work, stop. */
12457 if (type_specifiers.type == error_mark_node)
12458 return error_mark_node;
12459 /* Parse the conversion-declarator. */
12460 declarator = cp_parser_conversion_declarator_opt (parser);
12462 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12463 /*initialized=*/0, &attributes);
12464 if (attributes)
12465 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12467 /* Don't give this error when parsing tentatively. This happens to
12468 work because we always parse this definitively once. */
12469 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12470 && type_uses_auto (type_specified))
12472 if (cxx_dialect < cxx14)
12474 error ("invalid use of %<auto%> in conversion operator");
12475 return error_mark_node;
12477 else if (template_parm_scope_p ())
12478 warning (0, "use of %<auto%> in member template "
12479 "conversion operator can never be deduced");
12482 return type_specified;
12485 /* Parse an (optional) conversion-declarator.
12487 conversion-declarator:
12488 ptr-operator conversion-declarator [opt]
12492 static cp_declarator *
12493 cp_parser_conversion_declarator_opt (cp_parser* parser)
12495 enum tree_code code;
12496 tree class_type, std_attributes = NULL_TREE;
12497 cp_cv_quals cv_quals;
12499 /* We don't know if there's a ptr-operator next, or not. */
12500 cp_parser_parse_tentatively (parser);
12501 /* Try the ptr-operator. */
12502 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12503 &std_attributes);
12504 /* If it worked, look for more conversion-declarators. */
12505 if (cp_parser_parse_definitely (parser))
12507 cp_declarator *declarator;
12509 /* Parse another optional declarator. */
12510 declarator = cp_parser_conversion_declarator_opt (parser);
12512 declarator = cp_parser_make_indirect_declarator
12513 (code, class_type, cv_quals, declarator, std_attributes);
12515 return declarator;
12518 return NULL;
12521 /* Parse an (optional) ctor-initializer.
12523 ctor-initializer:
12524 : mem-initializer-list
12526 Returns TRUE iff the ctor-initializer was actually present. */
12528 static bool
12529 cp_parser_ctor_initializer_opt (cp_parser* parser)
12531 /* If the next token is not a `:', then there is no
12532 ctor-initializer. */
12533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12535 /* Do default initialization of any bases and members. */
12536 if (DECL_CONSTRUCTOR_P (current_function_decl))
12537 finish_mem_initializers (NULL_TREE);
12539 return false;
12542 /* Consume the `:' token. */
12543 cp_lexer_consume_token (parser->lexer);
12544 /* And the mem-initializer-list. */
12545 cp_parser_mem_initializer_list (parser);
12547 return true;
12550 /* Parse a mem-initializer-list.
12552 mem-initializer-list:
12553 mem-initializer ... [opt]
12554 mem-initializer ... [opt] , mem-initializer-list */
12556 static void
12557 cp_parser_mem_initializer_list (cp_parser* parser)
12559 tree mem_initializer_list = NULL_TREE;
12560 tree target_ctor = error_mark_node;
12561 cp_token *token = cp_lexer_peek_token (parser->lexer);
12563 /* Let the semantic analysis code know that we are starting the
12564 mem-initializer-list. */
12565 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12566 error_at (token->location,
12567 "only constructors take member initializers");
12569 /* Loop through the list. */
12570 while (true)
12572 tree mem_initializer;
12574 token = cp_lexer_peek_token (parser->lexer);
12575 /* Parse the mem-initializer. */
12576 mem_initializer = cp_parser_mem_initializer (parser);
12577 /* If the next token is a `...', we're expanding member initializers. */
12578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12580 /* Consume the `...'. */
12581 cp_lexer_consume_token (parser->lexer);
12583 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12584 can be expanded but members cannot. */
12585 if (mem_initializer != error_mark_node
12586 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12588 error_at (token->location,
12589 "cannot expand initializer for member %<%D%>",
12590 TREE_PURPOSE (mem_initializer));
12591 mem_initializer = error_mark_node;
12594 /* Construct the pack expansion type. */
12595 if (mem_initializer != error_mark_node)
12596 mem_initializer = make_pack_expansion (mem_initializer);
12598 if (target_ctor != error_mark_node
12599 && mem_initializer != error_mark_node)
12601 error ("mem-initializer for %qD follows constructor delegation",
12602 TREE_PURPOSE (mem_initializer));
12603 mem_initializer = error_mark_node;
12605 /* Look for a target constructor. */
12606 if (mem_initializer != error_mark_node
12607 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12608 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12610 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12611 if (mem_initializer_list)
12613 error ("constructor delegation follows mem-initializer for %qD",
12614 TREE_PURPOSE (mem_initializer_list));
12615 mem_initializer = error_mark_node;
12617 target_ctor = mem_initializer;
12619 /* Add it to the list, unless it was erroneous. */
12620 if (mem_initializer != error_mark_node)
12622 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12623 mem_initializer_list = mem_initializer;
12625 /* If the next token is not a `,', we're done. */
12626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12627 break;
12628 /* Consume the `,' token. */
12629 cp_lexer_consume_token (parser->lexer);
12632 /* Perform semantic analysis. */
12633 if (DECL_CONSTRUCTOR_P (current_function_decl))
12634 finish_mem_initializers (mem_initializer_list);
12637 /* Parse a mem-initializer.
12639 mem-initializer:
12640 mem-initializer-id ( expression-list [opt] )
12641 mem-initializer-id braced-init-list
12643 GNU extension:
12645 mem-initializer:
12646 ( expression-list [opt] )
12648 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12649 class) or FIELD_DECL (for a non-static data member) to initialize;
12650 the TREE_VALUE is the expression-list. An empty initialization
12651 list is represented by void_list_node. */
12653 static tree
12654 cp_parser_mem_initializer (cp_parser* parser)
12656 tree mem_initializer_id;
12657 tree expression_list;
12658 tree member;
12659 cp_token *token = cp_lexer_peek_token (parser->lexer);
12661 /* Find out what is being initialized. */
12662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12664 permerror (token->location,
12665 "anachronistic old-style base class initializer");
12666 mem_initializer_id = NULL_TREE;
12668 else
12670 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12671 if (mem_initializer_id == error_mark_node)
12672 return mem_initializer_id;
12674 member = expand_member_init (mem_initializer_id);
12675 if (member && !DECL_P (member))
12676 in_base_initializer = 1;
12678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12680 bool expr_non_constant_p;
12681 cp_lexer_set_source_position (parser->lexer);
12682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12683 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12684 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12685 expression_list = build_tree_list (NULL_TREE, expression_list);
12687 else
12689 vec<tree, va_gc> *vec;
12690 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12691 /*cast_p=*/false,
12692 /*allow_expansion_p=*/true,
12693 /*non_constant_p=*/NULL);
12694 if (vec == NULL)
12695 return error_mark_node;
12696 expression_list = build_tree_list_vec (vec);
12697 release_tree_vector (vec);
12700 if (expression_list == error_mark_node)
12701 return error_mark_node;
12702 if (!expression_list)
12703 expression_list = void_type_node;
12705 in_base_initializer = 0;
12707 return member ? build_tree_list (member, expression_list) : error_mark_node;
12710 /* Parse a mem-initializer-id.
12712 mem-initializer-id:
12713 :: [opt] nested-name-specifier [opt] class-name
12714 identifier
12716 Returns a TYPE indicating the class to be initializer for the first
12717 production. Returns an IDENTIFIER_NODE indicating the data member
12718 to be initialized for the second production. */
12720 static tree
12721 cp_parser_mem_initializer_id (cp_parser* parser)
12723 bool global_scope_p;
12724 bool nested_name_specifier_p;
12725 bool template_p = false;
12726 tree id;
12728 cp_token *token = cp_lexer_peek_token (parser->lexer);
12730 /* `typename' is not allowed in this context ([temp.res]). */
12731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12733 error_at (token->location,
12734 "keyword %<typename%> not allowed in this context (a qualified "
12735 "member initializer is implicitly a type)");
12736 cp_lexer_consume_token (parser->lexer);
12738 /* Look for the optional `::' operator. */
12739 global_scope_p
12740 = (cp_parser_global_scope_opt (parser,
12741 /*current_scope_valid_p=*/false)
12742 != NULL_TREE);
12743 /* Look for the optional nested-name-specifier. The simplest way to
12744 implement:
12746 [temp.res]
12748 The keyword `typename' is not permitted in a base-specifier or
12749 mem-initializer; in these contexts a qualified name that
12750 depends on a template-parameter is implicitly assumed to be a
12751 type name.
12753 is to assume that we have seen the `typename' keyword at this
12754 point. */
12755 nested_name_specifier_p
12756 = (cp_parser_nested_name_specifier_opt (parser,
12757 /*typename_keyword_p=*/true,
12758 /*check_dependency_p=*/true,
12759 /*type_p=*/true,
12760 /*is_declaration=*/true)
12761 != NULL_TREE);
12762 if (nested_name_specifier_p)
12763 template_p = cp_parser_optional_template_keyword (parser);
12764 /* If there is a `::' operator or a nested-name-specifier, then we
12765 are definitely looking for a class-name. */
12766 if (global_scope_p || nested_name_specifier_p)
12767 return cp_parser_class_name (parser,
12768 /*typename_keyword_p=*/true,
12769 /*template_keyword_p=*/template_p,
12770 typename_type,
12771 /*check_dependency_p=*/true,
12772 /*class_head_p=*/false,
12773 /*is_declaration=*/true);
12774 /* Otherwise, we could also be looking for an ordinary identifier. */
12775 cp_parser_parse_tentatively (parser);
12776 /* Try a class-name. */
12777 id = cp_parser_class_name (parser,
12778 /*typename_keyword_p=*/true,
12779 /*template_keyword_p=*/false,
12780 none_type,
12781 /*check_dependency_p=*/true,
12782 /*class_head_p=*/false,
12783 /*is_declaration=*/true);
12784 /* If we found one, we're done. */
12785 if (cp_parser_parse_definitely (parser))
12786 return id;
12787 /* Otherwise, look for an ordinary identifier. */
12788 return cp_parser_identifier (parser);
12791 /* Overloading [gram.over] */
12793 /* Parse an operator-function-id.
12795 operator-function-id:
12796 operator operator
12798 Returns an IDENTIFIER_NODE for the operator which is a
12799 human-readable spelling of the identifier, e.g., `operator +'. */
12801 static tree
12802 cp_parser_operator_function_id (cp_parser* parser)
12804 /* Look for the `operator' keyword. */
12805 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12806 return error_mark_node;
12807 /* And then the name of the operator itself. */
12808 return cp_parser_operator (parser);
12811 /* Return an identifier node for a user-defined literal operator.
12812 The suffix identifier is chained to the operator name identifier. */
12814 static tree
12815 cp_literal_operator_id (const char* name)
12817 tree identifier;
12818 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12819 + strlen (name) + 10);
12820 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12821 identifier = get_identifier (buffer);
12823 return identifier;
12826 /* Parse an operator.
12828 operator:
12829 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12830 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12831 || ++ -- , ->* -> () []
12833 GNU Extensions:
12835 operator:
12836 <? >? <?= >?=
12838 Returns an IDENTIFIER_NODE for the operator which is a
12839 human-readable spelling of the identifier, e.g., `operator +'. */
12841 static tree
12842 cp_parser_operator (cp_parser* parser)
12844 tree id = NULL_TREE;
12845 cp_token *token;
12846 bool utf8 = false;
12848 /* Peek at the next token. */
12849 token = cp_lexer_peek_token (parser->lexer);
12850 /* Figure out which operator we have. */
12851 switch (token->type)
12853 case CPP_KEYWORD:
12855 enum tree_code op;
12857 /* The keyword should be either `new' or `delete'. */
12858 if (token->keyword == RID_NEW)
12859 op = NEW_EXPR;
12860 else if (token->keyword == RID_DELETE)
12861 op = DELETE_EXPR;
12862 else
12863 break;
12865 /* Consume the `new' or `delete' token. */
12866 cp_lexer_consume_token (parser->lexer);
12868 /* Peek at the next token. */
12869 token = cp_lexer_peek_token (parser->lexer);
12870 /* If it's a `[' token then this is the array variant of the
12871 operator. */
12872 if (token->type == CPP_OPEN_SQUARE)
12874 /* Consume the `[' token. */
12875 cp_lexer_consume_token (parser->lexer);
12876 /* Look for the `]' token. */
12877 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12878 id = ansi_opname (op == NEW_EXPR
12879 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12881 /* Otherwise, we have the non-array variant. */
12882 else
12883 id = ansi_opname (op);
12885 return id;
12888 case CPP_PLUS:
12889 id = ansi_opname (PLUS_EXPR);
12890 break;
12892 case CPP_MINUS:
12893 id = ansi_opname (MINUS_EXPR);
12894 break;
12896 case CPP_MULT:
12897 id = ansi_opname (MULT_EXPR);
12898 break;
12900 case CPP_DIV:
12901 id = ansi_opname (TRUNC_DIV_EXPR);
12902 break;
12904 case CPP_MOD:
12905 id = ansi_opname (TRUNC_MOD_EXPR);
12906 break;
12908 case CPP_XOR:
12909 id = ansi_opname (BIT_XOR_EXPR);
12910 break;
12912 case CPP_AND:
12913 id = ansi_opname (BIT_AND_EXPR);
12914 break;
12916 case CPP_OR:
12917 id = ansi_opname (BIT_IOR_EXPR);
12918 break;
12920 case CPP_COMPL:
12921 id = ansi_opname (BIT_NOT_EXPR);
12922 break;
12924 case CPP_NOT:
12925 id = ansi_opname (TRUTH_NOT_EXPR);
12926 break;
12928 case CPP_EQ:
12929 id = ansi_assopname (NOP_EXPR);
12930 break;
12932 case CPP_LESS:
12933 id = ansi_opname (LT_EXPR);
12934 break;
12936 case CPP_GREATER:
12937 id = ansi_opname (GT_EXPR);
12938 break;
12940 case CPP_PLUS_EQ:
12941 id = ansi_assopname (PLUS_EXPR);
12942 break;
12944 case CPP_MINUS_EQ:
12945 id = ansi_assopname (MINUS_EXPR);
12946 break;
12948 case CPP_MULT_EQ:
12949 id = ansi_assopname (MULT_EXPR);
12950 break;
12952 case CPP_DIV_EQ:
12953 id = ansi_assopname (TRUNC_DIV_EXPR);
12954 break;
12956 case CPP_MOD_EQ:
12957 id = ansi_assopname (TRUNC_MOD_EXPR);
12958 break;
12960 case CPP_XOR_EQ:
12961 id = ansi_assopname (BIT_XOR_EXPR);
12962 break;
12964 case CPP_AND_EQ:
12965 id = ansi_assopname (BIT_AND_EXPR);
12966 break;
12968 case CPP_OR_EQ:
12969 id = ansi_assopname (BIT_IOR_EXPR);
12970 break;
12972 case CPP_LSHIFT:
12973 id = ansi_opname (LSHIFT_EXPR);
12974 break;
12976 case CPP_RSHIFT:
12977 id = ansi_opname (RSHIFT_EXPR);
12978 break;
12980 case CPP_LSHIFT_EQ:
12981 id = ansi_assopname (LSHIFT_EXPR);
12982 break;
12984 case CPP_RSHIFT_EQ:
12985 id = ansi_assopname (RSHIFT_EXPR);
12986 break;
12988 case CPP_EQ_EQ:
12989 id = ansi_opname (EQ_EXPR);
12990 break;
12992 case CPP_NOT_EQ:
12993 id = ansi_opname (NE_EXPR);
12994 break;
12996 case CPP_LESS_EQ:
12997 id = ansi_opname (LE_EXPR);
12998 break;
13000 case CPP_GREATER_EQ:
13001 id = ansi_opname (GE_EXPR);
13002 break;
13004 case CPP_AND_AND:
13005 id = ansi_opname (TRUTH_ANDIF_EXPR);
13006 break;
13008 case CPP_OR_OR:
13009 id = ansi_opname (TRUTH_ORIF_EXPR);
13010 break;
13012 case CPP_PLUS_PLUS:
13013 id = ansi_opname (POSTINCREMENT_EXPR);
13014 break;
13016 case CPP_MINUS_MINUS:
13017 id = ansi_opname (PREDECREMENT_EXPR);
13018 break;
13020 case CPP_COMMA:
13021 id = ansi_opname (COMPOUND_EXPR);
13022 break;
13024 case CPP_DEREF_STAR:
13025 id = ansi_opname (MEMBER_REF);
13026 break;
13028 case CPP_DEREF:
13029 id = ansi_opname (COMPONENT_REF);
13030 break;
13032 case CPP_OPEN_PAREN:
13033 /* Consume the `('. */
13034 cp_lexer_consume_token (parser->lexer);
13035 /* Look for the matching `)'. */
13036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13037 return ansi_opname (CALL_EXPR);
13039 case CPP_OPEN_SQUARE:
13040 /* Consume the `['. */
13041 cp_lexer_consume_token (parser->lexer);
13042 /* Look for the matching `]'. */
13043 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13044 return ansi_opname (ARRAY_REF);
13046 case CPP_UTF8STRING:
13047 case CPP_UTF8STRING_USERDEF:
13048 utf8 = true;
13049 case CPP_STRING:
13050 case CPP_WSTRING:
13051 case CPP_STRING16:
13052 case CPP_STRING32:
13053 case CPP_STRING_USERDEF:
13054 case CPP_WSTRING_USERDEF:
13055 case CPP_STRING16_USERDEF:
13056 case CPP_STRING32_USERDEF:
13058 tree str, string_tree;
13059 int sz, len;
13061 if (cxx_dialect == cxx98)
13062 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13064 /* Consume the string. */
13065 str = cp_parser_string_literal (parser, /*translate=*/true,
13066 /*wide_ok=*/true, /*lookup_udlit=*/false);
13067 if (str == error_mark_node)
13068 return error_mark_node;
13069 else if (TREE_CODE (str) == USERDEF_LITERAL)
13071 string_tree = USERDEF_LITERAL_VALUE (str);
13072 id = USERDEF_LITERAL_SUFFIX_ID (str);
13074 else
13076 string_tree = str;
13077 /* Look for the suffix identifier. */
13078 token = cp_lexer_peek_token (parser->lexer);
13079 if (token->type == CPP_NAME)
13080 id = cp_parser_identifier (parser);
13081 else if (token->type == CPP_KEYWORD)
13083 error ("unexpected keyword;"
13084 " remove space between quotes and suffix identifier");
13085 return error_mark_node;
13087 else
13089 error ("expected suffix identifier");
13090 return error_mark_node;
13093 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13094 (TREE_TYPE (TREE_TYPE (string_tree))));
13095 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13096 if (len != 0)
13098 error ("expected empty string after %<operator%> keyword");
13099 return error_mark_node;
13101 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13102 != char_type_node)
13104 error ("invalid encoding prefix in literal operator");
13105 return error_mark_node;
13107 if (id != error_mark_node)
13109 const char *name = IDENTIFIER_POINTER (id);
13110 id = cp_literal_operator_id (name);
13112 return id;
13115 default:
13116 /* Anything else is an error. */
13117 break;
13120 /* If we have selected an identifier, we need to consume the
13121 operator token. */
13122 if (id)
13123 cp_lexer_consume_token (parser->lexer);
13124 /* Otherwise, no valid operator name was present. */
13125 else
13127 cp_parser_error (parser, "expected operator");
13128 id = error_mark_node;
13131 return id;
13134 /* Parse a template-declaration.
13136 template-declaration:
13137 export [opt] template < template-parameter-list > declaration
13139 If MEMBER_P is TRUE, this template-declaration occurs within a
13140 class-specifier.
13142 The grammar rule given by the standard isn't correct. What
13143 is really meant is:
13145 template-declaration:
13146 export [opt] template-parameter-list-seq
13147 decl-specifier-seq [opt] init-declarator [opt] ;
13148 export [opt] template-parameter-list-seq
13149 function-definition
13151 template-parameter-list-seq:
13152 template-parameter-list-seq [opt]
13153 template < template-parameter-list > */
13155 static void
13156 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13158 /* Check for `export'. */
13159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13161 /* Consume the `export' token. */
13162 cp_lexer_consume_token (parser->lexer);
13163 /* Warn that we do not support `export'. */
13164 warning (0, "keyword %<export%> not implemented, and will be ignored");
13167 cp_parser_template_declaration_after_export (parser, member_p);
13170 /* Parse a template-parameter-list.
13172 template-parameter-list:
13173 template-parameter
13174 template-parameter-list , template-parameter
13176 Returns a TREE_LIST. Each node represents a template parameter.
13177 The nodes are connected via their TREE_CHAINs. */
13179 static tree
13180 cp_parser_template_parameter_list (cp_parser* parser)
13182 tree parameter_list = NULL_TREE;
13184 begin_template_parm_list ();
13186 /* The loop below parses the template parms. We first need to know
13187 the total number of template parms to be able to compute proper
13188 canonical types of each dependent type. So after the loop, when
13189 we know the total number of template parms,
13190 end_template_parm_list computes the proper canonical types and
13191 fixes up the dependent types accordingly. */
13192 while (true)
13194 tree parameter;
13195 bool is_non_type;
13196 bool is_parameter_pack;
13197 location_t parm_loc;
13199 /* Parse the template-parameter. */
13200 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13201 parameter = cp_parser_template_parameter (parser,
13202 &is_non_type,
13203 &is_parameter_pack);
13204 /* Add it to the list. */
13205 if (parameter != error_mark_node)
13206 parameter_list = process_template_parm (parameter_list,
13207 parm_loc,
13208 parameter,
13209 is_non_type,
13210 is_parameter_pack);
13211 else
13213 tree err_parm = build_tree_list (parameter, parameter);
13214 parameter_list = chainon (parameter_list, err_parm);
13217 /* If the next token is not a `,', we're done. */
13218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13219 break;
13220 /* Otherwise, consume the `,' token. */
13221 cp_lexer_consume_token (parser->lexer);
13224 return end_template_parm_list (parameter_list);
13227 /* Parse a template-parameter.
13229 template-parameter:
13230 type-parameter
13231 parameter-declaration
13233 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13234 the parameter. The TREE_PURPOSE is the default value, if any.
13235 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13236 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13237 set to true iff this parameter is a parameter pack. */
13239 static tree
13240 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13241 bool *is_parameter_pack)
13243 cp_token *token;
13244 cp_parameter_declarator *parameter_declarator;
13245 cp_declarator *id_declarator;
13246 tree parm;
13248 /* Assume it is a type parameter or a template parameter. */
13249 *is_non_type = false;
13250 /* Assume it not a parameter pack. */
13251 *is_parameter_pack = false;
13252 /* Peek at the next token. */
13253 token = cp_lexer_peek_token (parser->lexer);
13254 /* If it is `class' or `template', we have a type-parameter. */
13255 if (token->keyword == RID_TEMPLATE)
13256 return cp_parser_type_parameter (parser, is_parameter_pack);
13257 /* If it is `class' or `typename' we do not know yet whether it is a
13258 type parameter or a non-type parameter. Consider:
13260 template <typename T, typename T::X X> ...
13264 template <class C, class D*> ...
13266 Here, the first parameter is a type parameter, and the second is
13267 a non-type parameter. We can tell by looking at the token after
13268 the identifier -- if it is a `,', `=', or `>' then we have a type
13269 parameter. */
13270 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13272 /* Peek at the token after `class' or `typename'. */
13273 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13274 /* If it's an ellipsis, we have a template type parameter
13275 pack. */
13276 if (token->type == CPP_ELLIPSIS)
13277 return cp_parser_type_parameter (parser, is_parameter_pack);
13278 /* If it's an identifier, skip it. */
13279 if (token->type == CPP_NAME)
13280 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13281 /* Now, see if the token looks like the end of a template
13282 parameter. */
13283 if (token->type == CPP_COMMA
13284 || token->type == CPP_EQ
13285 || token->type == CPP_GREATER)
13286 return cp_parser_type_parameter (parser, is_parameter_pack);
13289 /* Otherwise, it is a non-type parameter.
13291 [temp.param]
13293 When parsing a default template-argument for a non-type
13294 template-parameter, the first non-nested `>' is taken as the end
13295 of the template parameter-list rather than a greater-than
13296 operator. */
13297 *is_non_type = true;
13298 parameter_declarator
13299 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13300 /*parenthesized_p=*/NULL);
13302 if (!parameter_declarator)
13303 return error_mark_node;
13305 /* If the parameter declaration is marked as a parameter pack, set
13306 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13307 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13308 grokdeclarator. */
13309 if (parameter_declarator->declarator
13310 && parameter_declarator->declarator->parameter_pack_p)
13312 *is_parameter_pack = true;
13313 parameter_declarator->declarator->parameter_pack_p = false;
13316 if (parameter_declarator->default_argument)
13318 /* Can happen in some cases of erroneous input (c++/34892). */
13319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13320 /* Consume the `...' for better error recovery. */
13321 cp_lexer_consume_token (parser->lexer);
13323 /* If the next token is an ellipsis, and we don't already have it
13324 marked as a parameter pack, then we have a parameter pack (that
13325 has no declarator). */
13326 else if (!*is_parameter_pack
13327 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13328 && (declarator_can_be_parameter_pack
13329 (parameter_declarator->declarator)))
13331 /* Consume the `...'. */
13332 cp_lexer_consume_token (parser->lexer);
13333 maybe_warn_variadic_templates ();
13335 *is_parameter_pack = true;
13337 /* We might end up with a pack expansion as the type of the non-type
13338 template parameter, in which case this is a non-type template
13339 parameter pack. */
13340 else if (parameter_declarator->decl_specifiers.type
13341 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13343 *is_parameter_pack = true;
13344 parameter_declarator->decl_specifiers.type =
13345 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13348 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13350 /* Parameter packs cannot have default arguments. However, a
13351 user may try to do so, so we'll parse them and give an
13352 appropriate diagnostic here. */
13354 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13356 /* Find the name of the parameter pack. */
13357 id_declarator = parameter_declarator->declarator;
13358 while (id_declarator && id_declarator->kind != cdk_id)
13359 id_declarator = id_declarator->declarator;
13361 if (id_declarator && id_declarator->kind == cdk_id)
13362 error_at (start_token->location,
13363 "template parameter pack %qD cannot have a default argument",
13364 id_declarator->u.id.unqualified_name);
13365 else
13366 error_at (start_token->location,
13367 "template parameter pack cannot have a default argument");
13369 /* Parse the default argument, but throw away the result. */
13370 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13373 parm = grokdeclarator (parameter_declarator->declarator,
13374 &parameter_declarator->decl_specifiers,
13375 TPARM, /*initialized=*/0,
13376 /*attrlist=*/NULL);
13377 if (parm == error_mark_node)
13378 return error_mark_node;
13380 return build_tree_list (parameter_declarator->default_argument, parm);
13383 /* Parse a type-parameter.
13385 type-parameter:
13386 class identifier [opt]
13387 class identifier [opt] = type-id
13388 typename identifier [opt]
13389 typename identifier [opt] = type-id
13390 template < template-parameter-list > class identifier [opt]
13391 template < template-parameter-list > class identifier [opt]
13392 = id-expression
13394 GNU Extension (variadic templates):
13396 type-parameter:
13397 class ... identifier [opt]
13398 typename ... identifier [opt]
13400 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13401 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13402 the declaration of the parameter.
13404 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13406 static tree
13407 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13409 cp_token *token;
13410 tree parameter;
13412 /* Look for a keyword to tell us what kind of parameter this is. */
13413 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13414 if (!token)
13415 return error_mark_node;
13417 switch (token->keyword)
13419 case RID_CLASS:
13420 case RID_TYPENAME:
13422 tree identifier;
13423 tree default_argument;
13425 /* If the next token is an ellipsis, we have a template
13426 argument pack. */
13427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13429 /* Consume the `...' token. */
13430 cp_lexer_consume_token (parser->lexer);
13431 maybe_warn_variadic_templates ();
13433 *is_parameter_pack = true;
13436 /* If the next token is an identifier, then it names the
13437 parameter. */
13438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13439 identifier = cp_parser_identifier (parser);
13440 else
13441 identifier = NULL_TREE;
13443 /* Create the parameter. */
13444 parameter = finish_template_type_parm (class_type_node, identifier);
13446 /* If the next token is an `=', we have a default argument. */
13447 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13449 /* Consume the `=' token. */
13450 cp_lexer_consume_token (parser->lexer);
13451 /* Parse the default-argument. */
13452 push_deferring_access_checks (dk_no_deferred);
13453 default_argument = cp_parser_type_id (parser);
13455 /* Template parameter packs cannot have default
13456 arguments. */
13457 if (*is_parameter_pack)
13459 if (identifier)
13460 error_at (token->location,
13461 "template parameter pack %qD cannot have a "
13462 "default argument", identifier);
13463 else
13464 error_at (token->location,
13465 "template parameter packs cannot have "
13466 "default arguments");
13467 default_argument = NULL_TREE;
13469 pop_deferring_access_checks ();
13471 else
13472 default_argument = NULL_TREE;
13474 /* Create the combined representation of the parameter and the
13475 default argument. */
13476 parameter = build_tree_list (default_argument, parameter);
13478 break;
13480 case RID_TEMPLATE:
13482 tree identifier;
13483 tree default_argument;
13485 /* Look for the `<'. */
13486 cp_parser_require (parser, CPP_LESS, RT_LESS);
13487 /* Parse the template-parameter-list. */
13488 cp_parser_template_parameter_list (parser);
13489 /* Look for the `>'. */
13490 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13491 /* Look for the `class' or 'typename' keywords. */
13492 cp_parser_type_parameter_key (parser);
13493 /* If the next token is an ellipsis, we have a template
13494 argument pack. */
13495 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13497 /* Consume the `...' token. */
13498 cp_lexer_consume_token (parser->lexer);
13499 maybe_warn_variadic_templates ();
13501 *is_parameter_pack = true;
13503 /* If the next token is an `=', then there is a
13504 default-argument. If the next token is a `>', we are at
13505 the end of the parameter-list. If the next token is a `,',
13506 then we are at the end of this parameter. */
13507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13508 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13509 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13511 identifier = cp_parser_identifier (parser);
13512 /* Treat invalid names as if the parameter were nameless. */
13513 if (identifier == error_mark_node)
13514 identifier = NULL_TREE;
13516 else
13517 identifier = NULL_TREE;
13519 /* Create the template parameter. */
13520 parameter = finish_template_template_parm (class_type_node,
13521 identifier);
13523 /* If the next token is an `=', then there is a
13524 default-argument. */
13525 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13527 bool is_template;
13529 /* Consume the `='. */
13530 cp_lexer_consume_token (parser->lexer);
13531 /* Parse the id-expression. */
13532 push_deferring_access_checks (dk_no_deferred);
13533 /* save token before parsing the id-expression, for error
13534 reporting */
13535 token = cp_lexer_peek_token (parser->lexer);
13536 default_argument
13537 = cp_parser_id_expression (parser,
13538 /*template_keyword_p=*/false,
13539 /*check_dependency_p=*/true,
13540 /*template_p=*/&is_template,
13541 /*declarator_p=*/false,
13542 /*optional_p=*/false);
13543 if (TREE_CODE (default_argument) == TYPE_DECL)
13544 /* If the id-expression was a template-id that refers to
13545 a template-class, we already have the declaration here,
13546 so no further lookup is needed. */
13548 else
13549 /* Look up the name. */
13550 default_argument
13551 = cp_parser_lookup_name (parser, default_argument,
13552 none_type,
13553 /*is_template=*/is_template,
13554 /*is_namespace=*/false,
13555 /*check_dependency=*/true,
13556 /*ambiguous_decls=*/NULL,
13557 token->location);
13558 /* See if the default argument is valid. */
13559 default_argument
13560 = check_template_template_default_arg (default_argument);
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 "
13569 "have a default argument",
13570 identifier);
13571 else
13572 error_at (token->location, "template parameter packs cannot "
13573 "have default arguments");
13574 default_argument = NULL_TREE;
13576 pop_deferring_access_checks ();
13578 else
13579 default_argument = NULL_TREE;
13581 /* Create the combined representation of the parameter and the
13582 default argument. */
13583 parameter = build_tree_list (default_argument, parameter);
13585 break;
13587 default:
13588 gcc_unreachable ();
13589 break;
13592 return parameter;
13595 /* Parse a template-id.
13597 template-id:
13598 template-name < template-argument-list [opt] >
13600 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13601 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13602 returned. Otherwise, if the template-name names a function, or set
13603 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13604 names a class, returns a TYPE_DECL for the specialization.
13606 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13607 uninstantiated templates. */
13609 static tree
13610 cp_parser_template_id (cp_parser *parser,
13611 bool template_keyword_p,
13612 bool check_dependency_p,
13613 enum tag_types tag_type,
13614 bool is_declaration)
13616 int i;
13617 tree templ;
13618 tree arguments;
13619 tree template_id;
13620 cp_token_position start_of_id = 0;
13621 deferred_access_check *chk;
13622 vec<deferred_access_check, va_gc> *access_check;
13623 cp_token *next_token = NULL, *next_token_2 = NULL;
13624 bool is_identifier;
13626 /* If the next token corresponds to a template-id, there is no need
13627 to reparse it. */
13628 next_token = cp_lexer_peek_token (parser->lexer);
13629 if (next_token->type == CPP_TEMPLATE_ID)
13631 struct tree_check *check_value;
13633 /* Get the stored value. */
13634 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13635 /* Perform any access checks that were deferred. */
13636 access_check = check_value->checks;
13637 if (access_check)
13639 FOR_EACH_VEC_ELT (*access_check, i, chk)
13640 perform_or_defer_access_check (chk->binfo,
13641 chk->decl,
13642 chk->diag_decl,
13643 tf_warning_or_error);
13645 /* Return the stored value. */
13646 return check_value->value;
13649 /* Avoid performing name lookup if there is no possibility of
13650 finding a template-id. */
13651 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13652 || (next_token->type == CPP_NAME
13653 && !cp_parser_nth_token_starts_template_argument_list_p
13654 (parser, 2)))
13656 cp_parser_error (parser, "expected template-id");
13657 return error_mark_node;
13660 /* Remember where the template-id starts. */
13661 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13662 start_of_id = cp_lexer_token_position (parser->lexer, false);
13664 push_deferring_access_checks (dk_deferred);
13666 /* Parse the template-name. */
13667 is_identifier = false;
13668 templ = cp_parser_template_name (parser, template_keyword_p,
13669 check_dependency_p,
13670 is_declaration,
13671 tag_type,
13672 &is_identifier);
13673 if (templ == error_mark_node || is_identifier)
13675 pop_deferring_access_checks ();
13676 return templ;
13679 /* If we find the sequence `[:' after a template-name, it's probably
13680 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13681 parse correctly the argument list. */
13682 next_token = cp_lexer_peek_token (parser->lexer);
13683 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13684 if (next_token->type == CPP_OPEN_SQUARE
13685 && next_token->flags & DIGRAPH
13686 && next_token_2->type == CPP_COLON
13687 && !(next_token_2->flags & PREV_WHITE))
13689 cp_parser_parse_tentatively (parser);
13690 /* Change `:' into `::'. */
13691 next_token_2->type = CPP_SCOPE;
13692 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13693 CPP_LESS. */
13694 cp_lexer_consume_token (parser->lexer);
13696 /* Parse the arguments. */
13697 arguments = cp_parser_enclosed_template_argument_list (parser);
13698 if (!cp_parser_parse_definitely (parser))
13700 /* If we couldn't parse an argument list, then we revert our changes
13701 and return simply an error. Maybe this is not a template-id
13702 after all. */
13703 next_token_2->type = CPP_COLON;
13704 cp_parser_error (parser, "expected %<<%>");
13705 pop_deferring_access_checks ();
13706 return error_mark_node;
13708 /* Otherwise, emit an error about the invalid digraph, but continue
13709 parsing because we got our argument list. */
13710 if (permerror (next_token->location,
13711 "%<<::%> cannot begin a template-argument list"))
13713 static bool hint = false;
13714 inform (next_token->location,
13715 "%<<:%> is an alternate spelling for %<[%>."
13716 " Insert whitespace between %<<%> and %<::%>");
13717 if (!hint && !flag_permissive)
13719 inform (next_token->location, "(if you use %<-fpermissive%> "
13720 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13721 "accept your code)");
13722 hint = true;
13726 else
13728 /* Look for the `<' that starts the template-argument-list. */
13729 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13731 pop_deferring_access_checks ();
13732 return error_mark_node;
13734 /* Parse the arguments. */
13735 arguments = cp_parser_enclosed_template_argument_list (parser);
13738 /* Build a representation of the specialization. */
13739 if (identifier_p (templ))
13740 template_id = build_min_nt_loc (next_token->location,
13741 TEMPLATE_ID_EXPR,
13742 templ, arguments);
13743 else if (DECL_TYPE_TEMPLATE_P (templ)
13744 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13746 bool entering_scope;
13747 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13748 template (rather than some instantiation thereof) only if
13749 is not nested within some other construct. For example, in
13750 "template <typename T> void f(T) { A<T>::", A<T> is just an
13751 instantiation of A. */
13752 entering_scope = (template_parm_scope_p ()
13753 && cp_lexer_next_token_is (parser->lexer,
13754 CPP_SCOPE));
13755 template_id
13756 = finish_template_type (templ, arguments, entering_scope);
13758 else if (variable_template_p (templ))
13760 template_id = lookup_template_variable (templ, arguments);
13762 else
13764 /* If it's not a class-template or a template-template, it should be
13765 a function-template. */
13766 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13767 || TREE_CODE (templ) == OVERLOAD
13768 || BASELINK_P (templ)));
13770 template_id = lookup_template_function (templ, arguments);
13773 /* If parsing tentatively, replace the sequence of tokens that makes
13774 up the template-id with a CPP_TEMPLATE_ID token. That way,
13775 should we re-parse the token stream, we will not have to repeat
13776 the effort required to do the parse, nor will we issue duplicate
13777 error messages about problems during instantiation of the
13778 template. */
13779 if (start_of_id
13780 /* Don't do this if we had a parse error in a declarator; re-parsing
13781 might succeed if a name changes meaning (60361). */
13782 && !(cp_parser_error_occurred (parser)
13783 && cp_parser_parsing_tentatively (parser)
13784 && parser->in_declarator_p))
13786 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13788 /* Reset the contents of the START_OF_ID token. */
13789 token->type = CPP_TEMPLATE_ID;
13790 /* Retrieve any deferred checks. Do not pop this access checks yet
13791 so the memory will not be reclaimed during token replacing below. */
13792 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13793 token->u.tree_check_value->value = template_id;
13794 token->u.tree_check_value->checks = get_deferred_access_checks ();
13795 token->keyword = RID_MAX;
13797 /* Purge all subsequent tokens. */
13798 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13800 /* ??? Can we actually assume that, if template_id ==
13801 error_mark_node, we will have issued a diagnostic to the
13802 user, as opposed to simply marking the tentative parse as
13803 failed? */
13804 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13805 error_at (token->location, "parse error in template argument list");
13808 pop_to_parent_deferring_access_checks ();
13809 return template_id;
13812 /* Parse a template-name.
13814 template-name:
13815 identifier
13817 The standard should actually say:
13819 template-name:
13820 identifier
13821 operator-function-id
13823 A defect report has been filed about this issue.
13825 A conversion-function-id cannot be a template name because they cannot
13826 be part of a template-id. In fact, looking at this code:
13828 a.operator K<int>()
13830 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13831 It is impossible to call a templated conversion-function-id with an
13832 explicit argument list, since the only allowed template parameter is
13833 the type to which it is converting.
13835 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13836 `template' keyword, in a construction like:
13838 T::template f<3>()
13840 In that case `f' is taken to be a template-name, even though there
13841 is no way of knowing for sure.
13843 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13844 name refers to a set of overloaded functions, at least one of which
13845 is a template, or an IDENTIFIER_NODE with the name of the template,
13846 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13847 names are looked up inside uninstantiated templates. */
13849 static tree
13850 cp_parser_template_name (cp_parser* parser,
13851 bool template_keyword_p,
13852 bool check_dependency_p,
13853 bool is_declaration,
13854 enum tag_types tag_type,
13855 bool *is_identifier)
13857 tree identifier;
13858 tree decl;
13859 tree fns;
13860 cp_token *token = cp_lexer_peek_token (parser->lexer);
13862 /* If the next token is `operator', then we have either an
13863 operator-function-id or a conversion-function-id. */
13864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13866 /* We don't know whether we're looking at an
13867 operator-function-id or a conversion-function-id. */
13868 cp_parser_parse_tentatively (parser);
13869 /* Try an operator-function-id. */
13870 identifier = cp_parser_operator_function_id (parser);
13871 /* If that didn't work, try a conversion-function-id. */
13872 if (!cp_parser_parse_definitely (parser))
13874 cp_parser_error (parser, "expected template-name");
13875 return error_mark_node;
13878 /* Look for the identifier. */
13879 else
13880 identifier = cp_parser_identifier (parser);
13882 /* If we didn't find an identifier, we don't have a template-id. */
13883 if (identifier == error_mark_node)
13884 return error_mark_node;
13886 /* If the name immediately followed the `template' keyword, then it
13887 is a template-name. However, if the next token is not `<', then
13888 we do not treat it as a template-name, since it is not being used
13889 as part of a template-id. This enables us to handle constructs
13890 like:
13892 template <typename T> struct S { S(); };
13893 template <typename T> S<T>::S();
13895 correctly. We would treat `S' as a template -- if it were `S<T>'
13896 -- but we do not if there is no `<'. */
13898 if (processing_template_decl
13899 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13901 /* In a declaration, in a dependent context, we pretend that the
13902 "template" keyword was present in order to improve error
13903 recovery. For example, given:
13905 template <typename T> void f(T::X<int>);
13907 we want to treat "X<int>" as a template-id. */
13908 if (is_declaration
13909 && !template_keyword_p
13910 && parser->scope && TYPE_P (parser->scope)
13911 && check_dependency_p
13912 && dependent_scope_p (parser->scope)
13913 /* Do not do this for dtors (or ctors), since they never
13914 need the template keyword before their name. */
13915 && !constructor_name_p (identifier, parser->scope))
13917 cp_token_position start = 0;
13919 /* Explain what went wrong. */
13920 error_at (token->location, "non-template %qD used as template",
13921 identifier);
13922 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13923 parser->scope, identifier);
13924 /* If parsing tentatively, find the location of the "<" token. */
13925 if (cp_parser_simulate_error (parser))
13926 start = cp_lexer_token_position (parser->lexer, true);
13927 /* Parse the template arguments so that we can issue error
13928 messages about them. */
13929 cp_lexer_consume_token (parser->lexer);
13930 cp_parser_enclosed_template_argument_list (parser);
13931 /* Skip tokens until we find a good place from which to
13932 continue parsing. */
13933 cp_parser_skip_to_closing_parenthesis (parser,
13934 /*recovering=*/true,
13935 /*or_comma=*/true,
13936 /*consume_paren=*/false);
13937 /* If parsing tentatively, permanently remove the
13938 template argument list. That will prevent duplicate
13939 error messages from being issued about the missing
13940 "template" keyword. */
13941 if (start)
13942 cp_lexer_purge_tokens_after (parser->lexer, start);
13943 if (is_identifier)
13944 *is_identifier = true;
13945 return identifier;
13948 /* If the "template" keyword is present, then there is generally
13949 no point in doing name-lookup, so we just return IDENTIFIER.
13950 But, if the qualifying scope is non-dependent then we can
13951 (and must) do name-lookup normally. */
13952 if (template_keyword_p
13953 && (!parser->scope
13954 || (TYPE_P (parser->scope)
13955 && dependent_type_p (parser->scope))))
13956 return identifier;
13959 /* Look up the name. */
13960 decl = cp_parser_lookup_name (parser, identifier,
13961 tag_type,
13962 /*is_template=*/true,
13963 /*is_namespace=*/false,
13964 check_dependency_p,
13965 /*ambiguous_decls=*/NULL,
13966 token->location);
13968 /* If DECL is a template, then the name was a template-name. */
13969 if (TREE_CODE (decl) == TEMPLATE_DECL)
13971 else
13973 tree fn = NULL_TREE;
13975 /* The standard does not explicitly indicate whether a name that
13976 names a set of overloaded declarations, some of which are
13977 templates, is a template-name. However, such a name should
13978 be a template-name; otherwise, there is no way to form a
13979 template-id for the overloaded templates. */
13980 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13981 if (TREE_CODE (fns) == OVERLOAD)
13982 for (fn = fns; fn; fn = OVL_NEXT (fn))
13983 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13984 break;
13986 if (!fn)
13988 /* The name does not name a template. */
13989 cp_parser_error (parser, "expected template-name");
13990 return error_mark_node;
13994 /* If DECL is dependent, and refers to a function, then just return
13995 its name; we will look it up again during template instantiation. */
13996 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13998 tree scope = ovl_scope (decl);
13999 if (TYPE_P (scope) && dependent_type_p (scope))
14000 return identifier;
14003 return decl;
14006 /* Parse a template-argument-list.
14008 template-argument-list:
14009 template-argument ... [opt]
14010 template-argument-list , template-argument ... [opt]
14012 Returns a TREE_VEC containing the arguments. */
14014 static tree
14015 cp_parser_template_argument_list (cp_parser* parser)
14017 tree fixed_args[10];
14018 unsigned n_args = 0;
14019 unsigned alloced = 10;
14020 tree *arg_ary = fixed_args;
14021 tree vec;
14022 bool saved_in_template_argument_list_p;
14023 bool saved_ice_p;
14024 bool saved_non_ice_p;
14026 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14027 parser->in_template_argument_list_p = true;
14028 /* Even if the template-id appears in an integral
14029 constant-expression, the contents of the argument list do
14030 not. */
14031 saved_ice_p = parser->integral_constant_expression_p;
14032 parser->integral_constant_expression_p = false;
14033 saved_non_ice_p = parser->non_integral_constant_expression_p;
14034 parser->non_integral_constant_expression_p = false;
14036 /* Parse the arguments. */
14039 tree argument;
14041 if (n_args)
14042 /* Consume the comma. */
14043 cp_lexer_consume_token (parser->lexer);
14045 /* Parse the template-argument. */
14046 argument = cp_parser_template_argument (parser);
14048 /* If the next token is an ellipsis, we're expanding a template
14049 argument pack. */
14050 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14052 if (argument == error_mark_node)
14054 cp_token *token = cp_lexer_peek_token (parser->lexer);
14055 error_at (token->location,
14056 "expected parameter pack before %<...%>");
14058 /* Consume the `...' token. */
14059 cp_lexer_consume_token (parser->lexer);
14061 /* Make the argument into a TYPE_PACK_EXPANSION or
14062 EXPR_PACK_EXPANSION. */
14063 argument = make_pack_expansion (argument);
14066 if (n_args == alloced)
14068 alloced *= 2;
14070 if (arg_ary == fixed_args)
14072 arg_ary = XNEWVEC (tree, alloced);
14073 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14075 else
14076 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14078 arg_ary[n_args++] = argument;
14080 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14082 vec = make_tree_vec (n_args);
14084 while (n_args--)
14085 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14087 if (arg_ary != fixed_args)
14088 free (arg_ary);
14089 parser->non_integral_constant_expression_p = saved_non_ice_p;
14090 parser->integral_constant_expression_p = saved_ice_p;
14091 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14092 #ifdef ENABLE_CHECKING
14093 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14094 #endif
14095 return vec;
14098 /* Parse a template-argument.
14100 template-argument:
14101 assignment-expression
14102 type-id
14103 id-expression
14105 The representation is that of an assignment-expression, type-id, or
14106 id-expression -- except that the qualified id-expression is
14107 evaluated, so that the value returned is either a DECL or an
14108 OVERLOAD.
14110 Although the standard says "assignment-expression", it forbids
14111 throw-expressions or assignments in the template argument.
14112 Therefore, we use "conditional-expression" instead. */
14114 static tree
14115 cp_parser_template_argument (cp_parser* parser)
14117 tree argument;
14118 bool template_p;
14119 bool address_p;
14120 bool maybe_type_id = false;
14121 cp_token *token = NULL, *argument_start_token = NULL;
14122 location_t loc = 0;
14123 cp_id_kind idk;
14125 /* There's really no way to know what we're looking at, so we just
14126 try each alternative in order.
14128 [temp.arg]
14130 In a template-argument, an ambiguity between a type-id and an
14131 expression is resolved to a type-id, regardless of the form of
14132 the corresponding template-parameter.
14134 Therefore, we try a type-id first. */
14135 cp_parser_parse_tentatively (parser);
14136 argument = cp_parser_template_type_arg (parser);
14137 /* If there was no error parsing the type-id but the next token is a
14138 '>>', our behavior depends on which dialect of C++ we're
14139 parsing. In C++98, we probably found a typo for '> >'. But there
14140 are type-id which are also valid expressions. For instance:
14142 struct X { int operator >> (int); };
14143 template <int V> struct Foo {};
14144 Foo<X () >> 5> r;
14146 Here 'X()' is a valid type-id of a function type, but the user just
14147 wanted to write the expression "X() >> 5". Thus, we remember that we
14148 found a valid type-id, but we still try to parse the argument as an
14149 expression to see what happens.
14151 In C++0x, the '>>' will be considered two separate '>'
14152 tokens. */
14153 if (!cp_parser_error_occurred (parser)
14154 && cxx_dialect == cxx98
14155 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14157 maybe_type_id = true;
14158 cp_parser_abort_tentative_parse (parser);
14160 else
14162 /* If the next token isn't a `,' or a `>', then this argument wasn't
14163 really finished. This means that the argument is not a valid
14164 type-id. */
14165 if (!cp_parser_next_token_ends_template_argument_p (parser))
14166 cp_parser_error (parser, "expected template-argument");
14167 /* If that worked, we're done. */
14168 if (cp_parser_parse_definitely (parser))
14169 return argument;
14171 /* We're still not sure what the argument will be. */
14172 cp_parser_parse_tentatively (parser);
14173 /* Try a template. */
14174 argument_start_token = cp_lexer_peek_token (parser->lexer);
14175 argument = cp_parser_id_expression (parser,
14176 /*template_keyword_p=*/false,
14177 /*check_dependency_p=*/true,
14178 &template_p,
14179 /*declarator_p=*/false,
14180 /*optional_p=*/false);
14181 /* If the next token isn't a `,' or a `>', then this argument wasn't
14182 really finished. */
14183 if (!cp_parser_next_token_ends_template_argument_p (parser))
14184 cp_parser_error (parser, "expected template-argument");
14185 if (!cp_parser_error_occurred (parser))
14187 /* Figure out what is being referred to. If the id-expression
14188 was for a class template specialization, then we will have a
14189 TYPE_DECL at this point. There is no need to do name lookup
14190 at this point in that case. */
14191 if (TREE_CODE (argument) != TYPE_DECL)
14192 argument = cp_parser_lookup_name (parser, argument,
14193 none_type,
14194 /*is_template=*/template_p,
14195 /*is_namespace=*/false,
14196 /*check_dependency=*/true,
14197 /*ambiguous_decls=*/NULL,
14198 argument_start_token->location);
14199 if (TREE_CODE (argument) != TEMPLATE_DECL
14200 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14201 cp_parser_error (parser, "expected template-name");
14203 if (cp_parser_parse_definitely (parser))
14204 return argument;
14205 /* It must be a non-type argument. There permitted cases are given
14206 in [temp.arg.nontype]:
14208 -- an integral constant-expression of integral or enumeration
14209 type; or
14211 -- the name of a non-type template-parameter; or
14213 -- the name of an object or function with external linkage...
14215 -- the address of an object or function with external linkage...
14217 -- a pointer to member... */
14218 /* Look for a non-type template parameter. */
14219 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14221 cp_parser_parse_tentatively (parser);
14222 argument = cp_parser_primary_expression (parser,
14223 /*address_p=*/false,
14224 /*cast_p=*/false,
14225 /*template_arg_p=*/true,
14226 &idk);
14227 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14228 || !cp_parser_next_token_ends_template_argument_p (parser))
14229 cp_parser_simulate_error (parser);
14230 if (cp_parser_parse_definitely (parser))
14231 return argument;
14234 /* If the next token is "&", the argument must be the address of an
14235 object or function with external linkage. */
14236 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14237 if (address_p)
14239 loc = cp_lexer_peek_token (parser->lexer)->location;
14240 cp_lexer_consume_token (parser->lexer);
14242 /* See if we might have an id-expression. */
14243 token = cp_lexer_peek_token (parser->lexer);
14244 if (token->type == CPP_NAME
14245 || token->keyword == RID_OPERATOR
14246 || token->type == CPP_SCOPE
14247 || token->type == CPP_TEMPLATE_ID
14248 || token->type == CPP_NESTED_NAME_SPECIFIER)
14250 cp_parser_parse_tentatively (parser);
14251 argument = cp_parser_primary_expression (parser,
14252 address_p,
14253 /*cast_p=*/false,
14254 /*template_arg_p=*/true,
14255 &idk);
14256 if (cp_parser_error_occurred (parser)
14257 || !cp_parser_next_token_ends_template_argument_p (parser))
14258 cp_parser_abort_tentative_parse (parser);
14259 else
14261 tree probe;
14263 if (INDIRECT_REF_P (argument))
14265 /* Strip the dereference temporarily. */
14266 gcc_assert (REFERENCE_REF_P (argument));
14267 argument = TREE_OPERAND (argument, 0);
14270 /* If we're in a template, we represent a qualified-id referring
14271 to a static data member as a SCOPE_REF even if the scope isn't
14272 dependent so that we can check access control later. */
14273 probe = argument;
14274 if (TREE_CODE (probe) == SCOPE_REF)
14275 probe = TREE_OPERAND (probe, 1);
14276 if (VAR_P (probe))
14278 /* A variable without external linkage might still be a
14279 valid constant-expression, so no error is issued here
14280 if the external-linkage check fails. */
14281 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14282 cp_parser_simulate_error (parser);
14284 else if (is_overloaded_fn (argument))
14285 /* All overloaded functions are allowed; if the external
14286 linkage test does not pass, an error will be issued
14287 later. */
14289 else if (address_p
14290 && (TREE_CODE (argument) == OFFSET_REF
14291 || TREE_CODE (argument) == SCOPE_REF))
14292 /* A pointer-to-member. */
14294 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14296 else
14297 cp_parser_simulate_error (parser);
14299 if (cp_parser_parse_definitely (parser))
14301 if (address_p)
14302 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14303 tf_warning_or_error);
14304 else
14305 argument = convert_from_reference (argument);
14306 return argument;
14310 /* If the argument started with "&", there are no other valid
14311 alternatives at this point. */
14312 if (address_p)
14314 cp_parser_error (parser, "invalid non-type template argument");
14315 return error_mark_node;
14318 /* If the argument wasn't successfully parsed as a type-id followed
14319 by '>>', the argument can only be a constant expression now.
14320 Otherwise, we try parsing the constant-expression tentatively,
14321 because the argument could really be a type-id. */
14322 if (maybe_type_id)
14323 cp_parser_parse_tentatively (parser);
14324 argument = cp_parser_constant_expression (parser);
14326 if (!maybe_type_id)
14327 return argument;
14328 if (!cp_parser_next_token_ends_template_argument_p (parser))
14329 cp_parser_error (parser, "expected template-argument");
14330 if (cp_parser_parse_definitely (parser))
14331 return argument;
14332 /* We did our best to parse the argument as a non type-id, but that
14333 was the only alternative that matched (albeit with a '>' after
14334 it). We can assume it's just a typo from the user, and a
14335 diagnostic will then be issued. */
14336 return cp_parser_template_type_arg (parser);
14339 /* Parse an explicit-instantiation.
14341 explicit-instantiation:
14342 template declaration
14344 Although the standard says `declaration', what it really means is:
14346 explicit-instantiation:
14347 template decl-specifier-seq [opt] declarator [opt] ;
14349 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14350 supposed to be allowed. A defect report has been filed about this
14351 issue.
14353 GNU Extension:
14355 explicit-instantiation:
14356 storage-class-specifier template
14357 decl-specifier-seq [opt] declarator [opt] ;
14358 function-specifier template
14359 decl-specifier-seq [opt] declarator [opt] ; */
14361 static void
14362 cp_parser_explicit_instantiation (cp_parser* parser)
14364 int declares_class_or_enum;
14365 cp_decl_specifier_seq decl_specifiers;
14366 tree extension_specifier = NULL_TREE;
14368 timevar_push (TV_TEMPLATE_INST);
14370 /* Look for an (optional) storage-class-specifier or
14371 function-specifier. */
14372 if (cp_parser_allow_gnu_extensions_p (parser))
14374 extension_specifier
14375 = cp_parser_storage_class_specifier_opt (parser);
14376 if (!extension_specifier)
14377 extension_specifier
14378 = cp_parser_function_specifier_opt (parser,
14379 /*decl_specs=*/NULL);
14382 /* Look for the `template' keyword. */
14383 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14384 /* Let the front end know that we are processing an explicit
14385 instantiation. */
14386 begin_explicit_instantiation ();
14387 /* [temp.explicit] says that we are supposed to ignore access
14388 control while processing explicit instantiation directives. */
14389 push_deferring_access_checks (dk_no_check);
14390 /* Parse a decl-specifier-seq. */
14391 cp_parser_decl_specifier_seq (parser,
14392 CP_PARSER_FLAGS_OPTIONAL,
14393 &decl_specifiers,
14394 &declares_class_or_enum);
14395 /* If there was exactly one decl-specifier, and it declared a class,
14396 and there's no declarator, then we have an explicit type
14397 instantiation. */
14398 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14400 tree type;
14402 type = check_tag_decl (&decl_specifiers,
14403 /*explicit_type_instantiation_p=*/true);
14404 /* Turn access control back on for names used during
14405 template instantiation. */
14406 pop_deferring_access_checks ();
14407 if (type)
14408 do_type_instantiation (type, extension_specifier,
14409 /*complain=*/tf_error);
14411 else
14413 cp_declarator *declarator;
14414 tree decl;
14416 /* Parse the declarator. */
14417 declarator
14418 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14419 /*ctor_dtor_or_conv_p=*/NULL,
14420 /*parenthesized_p=*/NULL,
14421 /*member_p=*/false,
14422 /*friend_p=*/false);
14423 if (declares_class_or_enum & 2)
14424 cp_parser_check_for_definition_in_return_type (declarator,
14425 decl_specifiers.type,
14426 decl_specifiers.locations[ds_type_spec]);
14427 if (declarator != cp_error_declarator)
14429 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14430 permerror (decl_specifiers.locations[ds_inline],
14431 "explicit instantiation shall not use"
14432 " %<inline%> specifier");
14433 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14434 permerror (decl_specifiers.locations[ds_constexpr],
14435 "explicit instantiation shall not use"
14436 " %<constexpr%> specifier");
14438 decl = grokdeclarator (declarator, &decl_specifiers,
14439 NORMAL, 0, &decl_specifiers.attributes);
14440 /* Turn access control back on for names used during
14441 template instantiation. */
14442 pop_deferring_access_checks ();
14443 /* Do the explicit instantiation. */
14444 do_decl_instantiation (decl, extension_specifier);
14446 else
14448 pop_deferring_access_checks ();
14449 /* Skip the body of the explicit instantiation. */
14450 cp_parser_skip_to_end_of_statement (parser);
14453 /* We're done with the instantiation. */
14454 end_explicit_instantiation ();
14456 cp_parser_consume_semicolon_at_end_of_statement (parser);
14458 timevar_pop (TV_TEMPLATE_INST);
14461 /* Parse an explicit-specialization.
14463 explicit-specialization:
14464 template < > declaration
14466 Although the standard says `declaration', what it really means is:
14468 explicit-specialization:
14469 template <> decl-specifier [opt] init-declarator [opt] ;
14470 template <> function-definition
14471 template <> explicit-specialization
14472 template <> template-declaration */
14474 static void
14475 cp_parser_explicit_specialization (cp_parser* parser)
14477 bool need_lang_pop;
14478 cp_token *token = cp_lexer_peek_token (parser->lexer);
14480 /* Look for the `template' keyword. */
14481 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14482 /* Look for the `<'. */
14483 cp_parser_require (parser, CPP_LESS, RT_LESS);
14484 /* Look for the `>'. */
14485 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14486 /* We have processed another parameter list. */
14487 ++parser->num_template_parameter_lists;
14488 /* [temp]
14490 A template ... explicit specialization ... shall not have C
14491 linkage. */
14492 if (current_lang_name == lang_name_c)
14494 error_at (token->location, "template specialization with C linkage");
14495 /* Give it C++ linkage to avoid confusing other parts of the
14496 front end. */
14497 push_lang_context (lang_name_cplusplus);
14498 need_lang_pop = true;
14500 else
14501 need_lang_pop = false;
14502 /* Let the front end know that we are beginning a specialization. */
14503 if (!begin_specialization ())
14505 end_specialization ();
14506 return;
14509 /* If the next keyword is `template', we need to figure out whether
14510 or not we're looking a template-declaration. */
14511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14513 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14514 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14515 cp_parser_template_declaration_after_export (parser,
14516 /*member_p=*/false);
14517 else
14518 cp_parser_explicit_specialization (parser);
14520 else
14521 /* Parse the dependent declaration. */
14522 cp_parser_single_declaration (parser,
14523 /*checks=*/NULL,
14524 /*member_p=*/false,
14525 /*explicit_specialization_p=*/true,
14526 /*friend_p=*/NULL);
14527 /* We're done with the specialization. */
14528 end_specialization ();
14529 /* For the erroneous case of a template with C linkage, we pushed an
14530 implicit C++ linkage scope; exit that scope now. */
14531 if (need_lang_pop)
14532 pop_lang_context ();
14533 /* We're done with this parameter list. */
14534 --parser->num_template_parameter_lists;
14537 /* Parse a type-specifier.
14539 type-specifier:
14540 simple-type-specifier
14541 class-specifier
14542 enum-specifier
14543 elaborated-type-specifier
14544 cv-qualifier
14546 GNU Extension:
14548 type-specifier:
14549 __complex__
14551 Returns a representation of the type-specifier. For a
14552 class-specifier, enum-specifier, or elaborated-type-specifier, a
14553 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14555 The parser flags FLAGS is used to control type-specifier parsing.
14557 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14558 in a decl-specifier-seq.
14560 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14561 class-specifier, enum-specifier, or elaborated-type-specifier, then
14562 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14563 if a type is declared; 2 if it is defined. Otherwise, it is set to
14564 zero.
14566 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14567 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14568 is set to FALSE. */
14570 static tree
14571 cp_parser_type_specifier (cp_parser* parser,
14572 cp_parser_flags flags,
14573 cp_decl_specifier_seq *decl_specs,
14574 bool is_declaration,
14575 int* declares_class_or_enum,
14576 bool* is_cv_qualifier)
14578 tree type_spec = NULL_TREE;
14579 cp_token *token;
14580 enum rid keyword;
14581 cp_decl_spec ds = ds_last;
14583 /* Assume this type-specifier does not declare a new type. */
14584 if (declares_class_or_enum)
14585 *declares_class_or_enum = 0;
14586 /* And that it does not specify a cv-qualifier. */
14587 if (is_cv_qualifier)
14588 *is_cv_qualifier = false;
14589 /* Peek at the next token. */
14590 token = cp_lexer_peek_token (parser->lexer);
14592 /* If we're looking at a keyword, we can use that to guide the
14593 production we choose. */
14594 keyword = token->keyword;
14595 switch (keyword)
14597 case RID_ENUM:
14598 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14599 goto elaborated_type_specifier;
14601 /* Look for the enum-specifier. */
14602 type_spec = cp_parser_enum_specifier (parser);
14603 /* If that worked, we're done. */
14604 if (type_spec)
14606 if (declares_class_or_enum)
14607 *declares_class_or_enum = 2;
14608 if (decl_specs)
14609 cp_parser_set_decl_spec_type (decl_specs,
14610 type_spec,
14611 token,
14612 /*type_definition_p=*/true);
14613 return type_spec;
14615 else
14616 goto elaborated_type_specifier;
14618 /* Any of these indicate either a class-specifier, or an
14619 elaborated-type-specifier. */
14620 case RID_CLASS:
14621 case RID_STRUCT:
14622 case RID_UNION:
14623 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14624 goto elaborated_type_specifier;
14626 /* Parse tentatively so that we can back up if we don't find a
14627 class-specifier. */
14628 cp_parser_parse_tentatively (parser);
14629 /* Look for the class-specifier. */
14630 type_spec = cp_parser_class_specifier (parser);
14631 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14632 /* If that worked, we're done. */
14633 if (cp_parser_parse_definitely (parser))
14635 if (declares_class_or_enum)
14636 *declares_class_or_enum = 2;
14637 if (decl_specs)
14638 cp_parser_set_decl_spec_type (decl_specs,
14639 type_spec,
14640 token,
14641 /*type_definition_p=*/true);
14642 return type_spec;
14645 /* Fall through. */
14646 elaborated_type_specifier:
14647 /* We're declaring (not defining) a class or enum. */
14648 if (declares_class_or_enum)
14649 *declares_class_or_enum = 1;
14651 /* Fall through. */
14652 case RID_TYPENAME:
14653 /* Look for an elaborated-type-specifier. */
14654 type_spec
14655 = (cp_parser_elaborated_type_specifier
14656 (parser,
14657 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14658 is_declaration));
14659 if (decl_specs)
14660 cp_parser_set_decl_spec_type (decl_specs,
14661 type_spec,
14662 token,
14663 /*type_definition_p=*/false);
14664 return type_spec;
14666 case RID_CONST:
14667 ds = ds_const;
14668 if (is_cv_qualifier)
14669 *is_cv_qualifier = true;
14670 break;
14672 case RID_VOLATILE:
14673 ds = ds_volatile;
14674 if (is_cv_qualifier)
14675 *is_cv_qualifier = true;
14676 break;
14678 case RID_RESTRICT:
14679 ds = ds_restrict;
14680 if (is_cv_qualifier)
14681 *is_cv_qualifier = true;
14682 break;
14684 case RID_COMPLEX:
14685 /* The `__complex__' keyword is a GNU extension. */
14686 ds = ds_complex;
14687 break;
14689 default:
14690 break;
14693 /* Handle simple keywords. */
14694 if (ds != ds_last)
14696 if (decl_specs)
14698 set_and_check_decl_spec_loc (decl_specs, ds, token);
14699 decl_specs->any_specifiers_p = true;
14701 return cp_lexer_consume_token (parser->lexer)->u.value;
14704 /* If we do not already have a type-specifier, assume we are looking
14705 at a simple-type-specifier. */
14706 type_spec = cp_parser_simple_type_specifier (parser,
14707 decl_specs,
14708 flags);
14710 /* If we didn't find a type-specifier, and a type-specifier was not
14711 optional in this context, issue an error message. */
14712 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14714 cp_parser_error (parser, "expected type specifier");
14715 return error_mark_node;
14718 return type_spec;
14721 /* Parse a simple-type-specifier.
14723 simple-type-specifier:
14724 :: [opt] nested-name-specifier [opt] type-name
14725 :: [opt] nested-name-specifier template template-id
14726 char
14727 wchar_t
14728 bool
14729 short
14731 long
14732 signed
14733 unsigned
14734 float
14735 double
14736 void
14738 C++0x Extension:
14740 simple-type-specifier:
14741 auto
14742 decltype ( expression )
14743 char16_t
14744 char32_t
14745 __underlying_type ( type-id )
14747 GNU Extension:
14749 simple-type-specifier:
14750 __int128
14751 __typeof__ unary-expression
14752 __typeof__ ( type-id )
14753 __typeof__ ( type-id ) { initializer-list , [opt] }
14755 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14756 appropriately updated. */
14758 static tree
14759 cp_parser_simple_type_specifier (cp_parser* parser,
14760 cp_decl_specifier_seq *decl_specs,
14761 cp_parser_flags flags)
14763 tree type = NULL_TREE;
14764 cp_token *token;
14765 int idx;
14767 /* Peek at the next token. */
14768 token = cp_lexer_peek_token (parser->lexer);
14770 /* If we're looking at a keyword, things are easy. */
14771 switch (token->keyword)
14773 case RID_CHAR:
14774 if (decl_specs)
14775 decl_specs->explicit_char_p = true;
14776 type = char_type_node;
14777 break;
14778 case RID_CHAR16:
14779 type = char16_type_node;
14780 break;
14781 case RID_CHAR32:
14782 type = char32_type_node;
14783 break;
14784 case RID_WCHAR:
14785 type = wchar_type_node;
14786 break;
14787 case RID_BOOL:
14788 type = boolean_type_node;
14789 break;
14790 case RID_SHORT:
14791 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14792 type = short_integer_type_node;
14793 break;
14794 case RID_INT:
14795 if (decl_specs)
14796 decl_specs->explicit_int_p = true;
14797 type = integer_type_node;
14798 break;
14799 case RID_INT_N_0:
14800 case RID_INT_N_1:
14801 case RID_INT_N_2:
14802 case RID_INT_N_3:
14803 idx = token->keyword - RID_INT_N_0;
14804 if (! int_n_enabled_p [idx])
14805 break;
14806 if (decl_specs)
14808 decl_specs->explicit_intN_p = true;
14809 decl_specs->int_n_idx = idx;
14811 type = int_n_trees [idx].signed_type;
14812 break;
14813 case RID_LONG:
14814 if (decl_specs)
14815 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14816 type = long_integer_type_node;
14817 break;
14818 case RID_SIGNED:
14819 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14820 type = integer_type_node;
14821 break;
14822 case RID_UNSIGNED:
14823 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14824 type = unsigned_type_node;
14825 break;
14826 case RID_FLOAT:
14827 type = float_type_node;
14828 break;
14829 case RID_DOUBLE:
14830 type = double_type_node;
14831 break;
14832 case RID_VOID:
14833 type = void_type_node;
14834 break;
14836 case RID_AUTO:
14837 maybe_warn_cpp0x (CPP0X_AUTO);
14838 if (parser->auto_is_implicit_function_template_parm_p)
14840 type = synthesize_implicit_template_parm (parser);
14842 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14844 if (cxx_dialect < cxx14)
14845 pedwarn (location_of (type), 0,
14846 "use of %<auto%> in lambda parameter declaration "
14847 "only available with "
14848 "-std=c++14 or -std=gnu++14");
14850 else if (cxx_dialect < cxx14)
14851 pedwarn (location_of (type), 0,
14852 "use of %<auto%> in parameter declaration "
14853 "only available with "
14854 "-std=c++14 or -std=gnu++14");
14855 else
14856 pedwarn (location_of (type), OPT_Wpedantic,
14857 "ISO C++ forbids use of %<auto%> in parameter "
14858 "declaration");
14860 else
14861 type = make_auto ();
14862 break;
14864 case RID_DECLTYPE:
14865 /* Since DR 743, decltype can either be a simple-type-specifier by
14866 itself or begin a nested-name-specifier. Parsing it will replace
14867 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14868 handling below decide what to do. */
14869 cp_parser_decltype (parser);
14870 cp_lexer_set_token_position (parser->lexer, token);
14871 break;
14873 case RID_TYPEOF:
14874 /* Consume the `typeof' token. */
14875 cp_lexer_consume_token (parser->lexer);
14876 /* Parse the operand to `typeof'. */
14877 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14878 /* If it is not already a TYPE, take its type. */
14879 if (!TYPE_P (type))
14880 type = finish_typeof (type);
14882 if (decl_specs)
14883 cp_parser_set_decl_spec_type (decl_specs, type,
14884 token,
14885 /*type_definition_p=*/false);
14887 return type;
14889 case RID_UNDERLYING_TYPE:
14890 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14891 if (decl_specs)
14892 cp_parser_set_decl_spec_type (decl_specs, type,
14893 token,
14894 /*type_definition_p=*/false);
14896 return type;
14898 case RID_BASES:
14899 case RID_DIRECT_BASES:
14900 type = cp_parser_trait_expr (parser, token->keyword);
14901 if (decl_specs)
14902 cp_parser_set_decl_spec_type (decl_specs, type,
14903 token,
14904 /*type_definition_p=*/false);
14905 return type;
14906 default:
14907 break;
14910 /* If token is an already-parsed decltype not followed by ::,
14911 it's a simple-type-specifier. */
14912 if (token->type == CPP_DECLTYPE
14913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14915 type = token->u.value;
14916 if (decl_specs)
14917 cp_parser_set_decl_spec_type (decl_specs, type,
14918 token,
14919 /*type_definition_p=*/false);
14920 cp_lexer_consume_token (parser->lexer);
14921 return type;
14924 /* If the type-specifier was for a built-in type, we're done. */
14925 if (type)
14927 /* Record the type. */
14928 if (decl_specs
14929 && (token->keyword != RID_SIGNED
14930 && token->keyword != RID_UNSIGNED
14931 && token->keyword != RID_SHORT
14932 && token->keyword != RID_LONG))
14933 cp_parser_set_decl_spec_type (decl_specs,
14934 type,
14935 token,
14936 /*type_definition_p=*/false);
14937 if (decl_specs)
14938 decl_specs->any_specifiers_p = true;
14940 /* Consume the token. */
14941 cp_lexer_consume_token (parser->lexer);
14943 /* There is no valid C++ program where a non-template type is
14944 followed by a "<". That usually indicates that the user thought
14945 that the type was a template. */
14946 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14947 token->location);
14949 return TYPE_NAME (type);
14952 /* The type-specifier must be a user-defined type. */
14953 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14955 bool qualified_p;
14956 bool global_p;
14958 /* Don't gobble tokens or issue error messages if this is an
14959 optional type-specifier. */
14960 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14961 cp_parser_parse_tentatively (parser);
14963 /* Look for the optional `::' operator. */
14964 global_p
14965 = (cp_parser_global_scope_opt (parser,
14966 /*current_scope_valid_p=*/false)
14967 != NULL_TREE);
14968 /* Look for the nested-name specifier. */
14969 qualified_p
14970 = (cp_parser_nested_name_specifier_opt (parser,
14971 /*typename_keyword_p=*/false,
14972 /*check_dependency_p=*/true,
14973 /*type_p=*/false,
14974 /*is_declaration=*/false)
14975 != NULL_TREE);
14976 token = cp_lexer_peek_token (parser->lexer);
14977 /* If we have seen a nested-name-specifier, and the next token
14978 is `template', then we are using the template-id production. */
14979 if (parser->scope
14980 && cp_parser_optional_template_keyword (parser))
14982 /* Look for the template-id. */
14983 type = cp_parser_template_id (parser,
14984 /*template_keyword_p=*/true,
14985 /*check_dependency_p=*/true,
14986 none_type,
14987 /*is_declaration=*/false);
14988 /* If the template-id did not name a type, we are out of
14989 luck. */
14990 if (TREE_CODE (type) != TYPE_DECL)
14992 cp_parser_error (parser, "expected template-id for type");
14993 type = NULL_TREE;
14996 /* Otherwise, look for a type-name. */
14997 else
14998 type = cp_parser_type_name (parser);
14999 /* Keep track of all name-lookups performed in class scopes. */
15000 if (type
15001 && !global_p
15002 && !qualified_p
15003 && TREE_CODE (type) == TYPE_DECL
15004 && identifier_p (DECL_NAME (type)))
15005 maybe_note_name_used_in_class (DECL_NAME (type), type);
15006 /* If it didn't work out, we don't have a TYPE. */
15007 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15008 && !cp_parser_parse_definitely (parser))
15009 type = NULL_TREE;
15010 if (type && decl_specs)
15011 cp_parser_set_decl_spec_type (decl_specs, type,
15012 token,
15013 /*type_definition_p=*/false);
15016 /* If we didn't get a type-name, issue an error message. */
15017 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15019 cp_parser_error (parser, "expected type-name");
15020 return error_mark_node;
15023 if (type && type != error_mark_node)
15025 /* See if TYPE is an Objective-C type, and if so, parse and
15026 accept any protocol references following it. Do this before
15027 the cp_parser_check_for_invalid_template_id() call, because
15028 Objective-C types can be followed by '<...>' which would
15029 enclose protocol names rather than template arguments, and so
15030 everything is fine. */
15031 if (c_dialect_objc () && !parser->scope
15032 && (objc_is_id (type) || objc_is_class_name (type)))
15034 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15035 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15037 /* Clobber the "unqualified" type previously entered into
15038 DECL_SPECS with the new, improved protocol-qualified version. */
15039 if (decl_specs)
15040 decl_specs->type = qual_type;
15042 return qual_type;
15045 /* There is no valid C++ program where a non-template type is
15046 followed by a "<". That usually indicates that the user
15047 thought that the type was a template. */
15048 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15049 none_type,
15050 token->location);
15053 return type;
15056 /* Parse a type-name.
15058 type-name:
15059 class-name
15060 enum-name
15061 typedef-name
15062 simple-template-id [in c++0x]
15064 enum-name:
15065 identifier
15067 typedef-name:
15068 identifier
15070 Returns a TYPE_DECL for the type. */
15072 static tree
15073 cp_parser_type_name (cp_parser* parser)
15075 tree type_decl;
15077 /* We can't know yet whether it is a class-name or not. */
15078 cp_parser_parse_tentatively (parser);
15079 /* Try a class-name. */
15080 type_decl = cp_parser_class_name (parser,
15081 /*typename_keyword_p=*/false,
15082 /*template_keyword_p=*/false,
15083 none_type,
15084 /*check_dependency_p=*/true,
15085 /*class_head_p=*/false,
15086 /*is_declaration=*/false);
15087 /* If it's not a class-name, keep looking. */
15088 if (!cp_parser_parse_definitely (parser))
15090 if (cxx_dialect < cxx11)
15091 /* It must be a typedef-name or an enum-name. */
15092 return cp_parser_nonclass_name (parser);
15094 cp_parser_parse_tentatively (parser);
15095 /* It is either a simple-template-id representing an
15096 instantiation of an alias template... */
15097 type_decl = cp_parser_template_id (parser,
15098 /*template_keyword_p=*/false,
15099 /*check_dependency_p=*/true,
15100 none_type,
15101 /*is_declaration=*/false);
15102 /* Note that this must be an instantiation of an alias template
15103 because [temp.names]/6 says:
15105 A template-id that names an alias template specialization
15106 is a type-name.
15108 Whereas [temp.names]/7 says:
15110 A simple-template-id that names a class template
15111 specialization is a class-name. */
15112 if (type_decl != NULL_TREE
15113 && TREE_CODE (type_decl) == TYPE_DECL
15114 && TYPE_DECL_ALIAS_P (type_decl))
15115 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15116 else
15117 cp_parser_simulate_error (parser);
15119 if (!cp_parser_parse_definitely (parser))
15120 /* ... Or a typedef-name or an enum-name. */
15121 return cp_parser_nonclass_name (parser);
15124 return type_decl;
15127 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15129 enum-name:
15130 identifier
15132 typedef-name:
15133 identifier
15135 Returns a TYPE_DECL for the type. */
15137 static tree
15138 cp_parser_nonclass_name (cp_parser* parser)
15140 tree type_decl;
15141 tree identifier;
15143 cp_token *token = cp_lexer_peek_token (parser->lexer);
15144 identifier = cp_parser_identifier (parser);
15145 if (identifier == error_mark_node)
15146 return error_mark_node;
15148 /* Look up the type-name. */
15149 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15151 type_decl = strip_using_decl (type_decl);
15153 if (TREE_CODE (type_decl) != TYPE_DECL
15154 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15156 /* See if this is an Objective-C type. */
15157 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15158 tree type = objc_get_protocol_qualified_type (identifier, protos);
15159 if (type)
15160 type_decl = TYPE_NAME (type);
15163 /* Issue an error if we did not find a type-name. */
15164 if (TREE_CODE (type_decl) != TYPE_DECL
15165 /* In Objective-C, we have the complication that class names are
15166 normally type names and start declarations (eg, the
15167 "NSObject" in "NSObject *object;"), but can be used in an
15168 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15169 is an expression. So, a classname followed by a dot is not a
15170 valid type-name. */
15171 || (objc_is_class_name (TREE_TYPE (type_decl))
15172 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15174 if (!cp_parser_simulate_error (parser))
15175 cp_parser_name_lookup_error (parser, identifier, type_decl,
15176 NLE_TYPE, token->location);
15177 return error_mark_node;
15179 /* Remember that the name was used in the definition of the
15180 current class so that we can check later to see if the
15181 meaning would have been different after the class was
15182 entirely defined. */
15183 else if (type_decl != error_mark_node
15184 && !parser->scope)
15185 maybe_note_name_used_in_class (identifier, type_decl);
15187 return type_decl;
15190 /* Parse an elaborated-type-specifier. Note that the grammar given
15191 here incorporates the resolution to DR68.
15193 elaborated-type-specifier:
15194 class-key :: [opt] nested-name-specifier [opt] identifier
15195 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15196 enum-key :: [opt] nested-name-specifier [opt] identifier
15197 typename :: [opt] nested-name-specifier identifier
15198 typename :: [opt] nested-name-specifier template [opt]
15199 template-id
15201 GNU extension:
15203 elaborated-type-specifier:
15204 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15205 class-key attributes :: [opt] nested-name-specifier [opt]
15206 template [opt] template-id
15207 enum attributes :: [opt] nested-name-specifier [opt] identifier
15209 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15210 declared `friend'. If IS_DECLARATION is TRUE, then this
15211 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15212 something is being declared.
15214 Returns the TYPE specified. */
15216 static tree
15217 cp_parser_elaborated_type_specifier (cp_parser* parser,
15218 bool is_friend,
15219 bool is_declaration)
15221 enum tag_types tag_type;
15222 tree identifier;
15223 tree type = NULL_TREE;
15224 tree attributes = NULL_TREE;
15225 tree globalscope;
15226 cp_token *token = NULL;
15228 /* See if we're looking at the `enum' keyword. */
15229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15231 /* Consume the `enum' token. */
15232 cp_lexer_consume_token (parser->lexer);
15233 /* Remember that it's an enumeration type. */
15234 tag_type = enum_type;
15235 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15236 enums) is used here. */
15237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15238 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15240 pedwarn (input_location, 0, "elaborated-type-specifier "
15241 "for a scoped enum must not use the %<%D%> keyword",
15242 cp_lexer_peek_token (parser->lexer)->u.value);
15243 /* Consume the `struct' or `class' and parse it anyway. */
15244 cp_lexer_consume_token (parser->lexer);
15246 /* Parse the attributes. */
15247 attributes = cp_parser_attributes_opt (parser);
15249 /* Or, it might be `typename'. */
15250 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15251 RID_TYPENAME))
15253 /* Consume the `typename' token. */
15254 cp_lexer_consume_token (parser->lexer);
15255 /* Remember that it's a `typename' type. */
15256 tag_type = typename_type;
15258 /* Otherwise it must be a class-key. */
15259 else
15261 tag_type = cp_parser_class_key (parser);
15262 if (tag_type == none_type)
15263 return error_mark_node;
15264 /* Parse the attributes. */
15265 attributes = cp_parser_attributes_opt (parser);
15268 /* Look for the `::' operator. */
15269 globalscope = cp_parser_global_scope_opt (parser,
15270 /*current_scope_valid_p=*/false);
15271 /* Look for the nested-name-specifier. */
15272 if (tag_type == typename_type && !globalscope)
15274 if (!cp_parser_nested_name_specifier (parser,
15275 /*typename_keyword_p=*/true,
15276 /*check_dependency_p=*/true,
15277 /*type_p=*/true,
15278 is_declaration))
15279 return error_mark_node;
15281 else
15282 /* Even though `typename' is not present, the proposed resolution
15283 to Core Issue 180 says that in `class A<T>::B', `B' should be
15284 considered a type-name, even if `A<T>' is dependent. */
15285 cp_parser_nested_name_specifier_opt (parser,
15286 /*typename_keyword_p=*/true,
15287 /*check_dependency_p=*/true,
15288 /*type_p=*/true,
15289 is_declaration);
15290 /* For everything but enumeration types, consider a template-id.
15291 For an enumeration type, consider only a plain identifier. */
15292 if (tag_type != enum_type)
15294 bool template_p = false;
15295 tree decl;
15297 /* Allow the `template' keyword. */
15298 template_p = cp_parser_optional_template_keyword (parser);
15299 /* If we didn't see `template', we don't know if there's a
15300 template-id or not. */
15301 if (!template_p)
15302 cp_parser_parse_tentatively (parser);
15303 /* Parse the template-id. */
15304 token = cp_lexer_peek_token (parser->lexer);
15305 decl = cp_parser_template_id (parser, template_p,
15306 /*check_dependency_p=*/true,
15307 tag_type,
15308 is_declaration);
15309 /* If we didn't find a template-id, look for an ordinary
15310 identifier. */
15311 if (!template_p && !cp_parser_parse_definitely (parser))
15313 /* We can get here when cp_parser_template_id, called by
15314 cp_parser_class_name with tag_type == none_type, succeeds
15315 and caches a BASELINK. Then, when called again here,
15316 instead of failing and returning an error_mark_node
15317 returns it (see template/typename17.C in C++11).
15318 ??? Could we diagnose this earlier? */
15319 else if (tag_type == typename_type && BASELINK_P (decl))
15321 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15322 type = error_mark_node;
15324 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15325 in effect, then we must assume that, upon instantiation, the
15326 template will correspond to a class. */
15327 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15328 && tag_type == typename_type)
15329 type = make_typename_type (parser->scope, decl,
15330 typename_type,
15331 /*complain=*/tf_error);
15332 /* If the `typename' keyword is in effect and DECL is not a type
15333 decl, then type is non existent. */
15334 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15336 else if (TREE_CODE (decl) == TYPE_DECL)
15337 type = check_elaborated_type_specifier (tag_type, decl,
15338 /*allow_template_p=*/true);
15339 else if (decl == error_mark_node)
15340 type = error_mark_node;
15343 if (!type)
15345 token = cp_lexer_peek_token (parser->lexer);
15346 identifier = cp_parser_identifier (parser);
15348 if (identifier == error_mark_node)
15350 parser->scope = NULL_TREE;
15351 return error_mark_node;
15354 /* For a `typename', we needn't call xref_tag. */
15355 if (tag_type == typename_type
15356 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15357 return cp_parser_make_typename_type (parser, identifier,
15358 token->location);
15360 /* Template parameter lists apply only if we are not within a
15361 function parameter list. */
15362 bool template_parm_lists_apply
15363 = parser->num_template_parameter_lists;
15364 if (template_parm_lists_apply)
15365 for (cp_binding_level *s = current_binding_level;
15366 s && s->kind != sk_template_parms;
15367 s = s->level_chain)
15368 if (s->kind == sk_function_parms)
15369 template_parm_lists_apply = false;
15371 /* Look up a qualified name in the usual way. */
15372 if (parser->scope)
15374 tree decl;
15375 tree ambiguous_decls;
15377 decl = cp_parser_lookup_name (parser, identifier,
15378 tag_type,
15379 /*is_template=*/false,
15380 /*is_namespace=*/false,
15381 /*check_dependency=*/true,
15382 &ambiguous_decls,
15383 token->location);
15385 /* If the lookup was ambiguous, an error will already have been
15386 issued. */
15387 if (ambiguous_decls)
15388 return error_mark_node;
15390 /* If we are parsing friend declaration, DECL may be a
15391 TEMPLATE_DECL tree node here. However, we need to check
15392 whether this TEMPLATE_DECL results in valid code. Consider
15393 the following example:
15395 namespace N {
15396 template <class T> class C {};
15398 class X {
15399 template <class T> friend class N::C; // #1, valid code
15401 template <class T> class Y {
15402 friend class N::C; // #2, invalid code
15405 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15406 name lookup of `N::C'. We see that friend declaration must
15407 be template for the code to be valid. Note that
15408 processing_template_decl does not work here since it is
15409 always 1 for the above two cases. */
15411 decl = (cp_parser_maybe_treat_template_as_class
15412 (decl, /*tag_name_p=*/is_friend
15413 && template_parm_lists_apply));
15415 if (TREE_CODE (decl) != TYPE_DECL)
15417 cp_parser_diagnose_invalid_type_name (parser,
15418 identifier,
15419 token->location);
15420 return error_mark_node;
15423 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15425 bool allow_template = (template_parm_lists_apply
15426 || DECL_SELF_REFERENCE_P (decl));
15427 type = check_elaborated_type_specifier (tag_type, decl,
15428 allow_template);
15430 if (type == error_mark_node)
15431 return error_mark_node;
15434 /* Forward declarations of nested types, such as
15436 class C1::C2;
15437 class C1::C2::C3;
15439 are invalid unless all components preceding the final '::'
15440 are complete. If all enclosing types are complete, these
15441 declarations become merely pointless.
15443 Invalid forward declarations of nested types are errors
15444 caught elsewhere in parsing. Those that are pointless arrive
15445 here. */
15447 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15448 && !is_friend && !processing_explicit_instantiation)
15449 warning (0, "declaration %qD does not declare anything", decl);
15451 type = TREE_TYPE (decl);
15453 else
15455 /* An elaborated-type-specifier sometimes introduces a new type and
15456 sometimes names an existing type. Normally, the rule is that it
15457 introduces a new type only if there is not an existing type of
15458 the same name already in scope. For example, given:
15460 struct S {};
15461 void f() { struct S s; }
15463 the `struct S' in the body of `f' is the same `struct S' as in
15464 the global scope; the existing definition is used. However, if
15465 there were no global declaration, this would introduce a new
15466 local class named `S'.
15468 An exception to this rule applies to the following code:
15470 namespace N { struct S; }
15472 Here, the elaborated-type-specifier names a new type
15473 unconditionally; even if there is already an `S' in the
15474 containing scope this declaration names a new type.
15475 This exception only applies if the elaborated-type-specifier
15476 forms the complete declaration:
15478 [class.name]
15480 A declaration consisting solely of `class-key identifier ;' is
15481 either a redeclaration of the name in the current scope or a
15482 forward declaration of the identifier as a class name. It
15483 introduces the name into the current scope.
15485 We are in this situation precisely when the next token is a `;'.
15487 An exception to the exception is that a `friend' declaration does
15488 *not* name a new type; i.e., given:
15490 struct S { friend struct T; };
15492 `T' is not a new type in the scope of `S'.
15494 Also, `new struct S' or `sizeof (struct S)' never results in the
15495 definition of a new type; a new type can only be declared in a
15496 declaration context. */
15498 tag_scope ts;
15499 bool template_p;
15501 if (is_friend)
15502 /* Friends have special name lookup rules. */
15503 ts = ts_within_enclosing_non_class;
15504 else if (is_declaration
15505 && cp_lexer_next_token_is (parser->lexer,
15506 CPP_SEMICOLON))
15507 /* This is a `class-key identifier ;' */
15508 ts = ts_current;
15509 else
15510 ts = ts_global;
15512 template_p =
15513 (template_parm_lists_apply
15514 && (cp_parser_next_token_starts_class_definition_p (parser)
15515 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15516 /* An unqualified name was used to reference this type, so
15517 there were no qualifying templates. */
15518 if (template_parm_lists_apply
15519 && !cp_parser_check_template_parameters (parser,
15520 /*num_templates=*/0,
15521 token->location,
15522 /*declarator=*/NULL))
15523 return error_mark_node;
15524 type = xref_tag (tag_type, identifier, ts, template_p);
15528 if (type == error_mark_node)
15529 return error_mark_node;
15531 /* Allow attributes on forward declarations of classes. */
15532 if (attributes)
15534 if (TREE_CODE (type) == TYPENAME_TYPE)
15535 warning (OPT_Wattributes,
15536 "attributes ignored on uninstantiated type");
15537 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15538 && ! processing_explicit_instantiation)
15539 warning (OPT_Wattributes,
15540 "attributes ignored on template instantiation");
15541 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15542 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15543 else
15544 warning (OPT_Wattributes,
15545 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15548 if (tag_type != enum_type)
15550 /* Indicate whether this class was declared as a `class' or as a
15551 `struct'. */
15552 if (TREE_CODE (type) == RECORD_TYPE)
15553 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15554 cp_parser_check_class_key (tag_type, type);
15557 /* A "<" cannot follow an elaborated type specifier. If that
15558 happens, the user was probably trying to form a template-id. */
15559 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15560 token->location);
15562 return type;
15565 /* Parse an enum-specifier.
15567 enum-specifier:
15568 enum-head { enumerator-list [opt] }
15569 enum-head { enumerator-list , } [C++0x]
15571 enum-head:
15572 enum-key identifier [opt] enum-base [opt]
15573 enum-key nested-name-specifier identifier enum-base [opt]
15575 enum-key:
15576 enum
15577 enum class [C++0x]
15578 enum struct [C++0x]
15580 enum-base: [C++0x]
15581 : type-specifier-seq
15583 opaque-enum-specifier:
15584 enum-key identifier enum-base [opt] ;
15586 GNU Extensions:
15587 enum-key attributes[opt] identifier [opt] enum-base [opt]
15588 { enumerator-list [opt] }attributes[opt]
15589 enum-key attributes[opt] identifier [opt] enum-base [opt]
15590 { enumerator-list, }attributes[opt] [C++0x]
15592 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15593 if the token stream isn't an enum-specifier after all. */
15595 static tree
15596 cp_parser_enum_specifier (cp_parser* parser)
15598 tree identifier;
15599 tree type = NULL_TREE;
15600 tree prev_scope;
15601 tree nested_name_specifier = NULL_TREE;
15602 tree attributes;
15603 bool scoped_enum_p = false;
15604 bool has_underlying_type = false;
15605 bool nested_being_defined = false;
15606 bool new_value_list = false;
15607 bool is_new_type = false;
15608 bool is_anonymous = false;
15609 tree underlying_type = NULL_TREE;
15610 cp_token *type_start_token = NULL;
15611 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15613 parser->colon_corrects_to_scope_p = false;
15615 /* Parse tentatively so that we can back up if we don't find a
15616 enum-specifier. */
15617 cp_parser_parse_tentatively (parser);
15619 /* Caller guarantees that the current token is 'enum', an identifier
15620 possibly follows, and the token after that is an opening brace.
15621 If we don't have an identifier, fabricate an anonymous name for
15622 the enumeration being defined. */
15623 cp_lexer_consume_token (parser->lexer);
15625 /* Parse the "class" or "struct", which indicates a scoped
15626 enumeration type in C++0x. */
15627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15628 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15630 if (cxx_dialect < cxx11)
15631 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15633 /* Consume the `struct' or `class' token. */
15634 cp_lexer_consume_token (parser->lexer);
15636 scoped_enum_p = true;
15639 attributes = cp_parser_attributes_opt (parser);
15641 /* Clear the qualification. */
15642 parser->scope = NULL_TREE;
15643 parser->qualifying_scope = NULL_TREE;
15644 parser->object_scope = NULL_TREE;
15646 /* Figure out in what scope the declaration is being placed. */
15647 prev_scope = current_scope ();
15649 type_start_token = cp_lexer_peek_token (parser->lexer);
15651 push_deferring_access_checks (dk_no_check);
15652 nested_name_specifier
15653 = cp_parser_nested_name_specifier_opt (parser,
15654 /*typename_keyword_p=*/true,
15655 /*check_dependency_p=*/false,
15656 /*type_p=*/false,
15657 /*is_declaration=*/false);
15659 if (nested_name_specifier)
15661 tree name;
15663 identifier = cp_parser_identifier (parser);
15664 name = cp_parser_lookup_name (parser, identifier,
15665 enum_type,
15666 /*is_template=*/false,
15667 /*is_namespace=*/false,
15668 /*check_dependency=*/true,
15669 /*ambiguous_decls=*/NULL,
15670 input_location);
15671 if (name && name != error_mark_node)
15673 type = TREE_TYPE (name);
15674 if (TREE_CODE (type) == TYPENAME_TYPE)
15676 /* Are template enums allowed in ISO? */
15677 if (template_parm_scope_p ())
15678 pedwarn (type_start_token->location, OPT_Wpedantic,
15679 "%qD is an enumeration template", name);
15680 /* ignore a typename reference, for it will be solved by name
15681 in start_enum. */
15682 type = NULL_TREE;
15685 else if (nested_name_specifier == error_mark_node)
15686 /* We already issued an error. */;
15687 else
15688 error_at (type_start_token->location,
15689 "%qD is not an enumerator-name", identifier);
15691 else
15693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15694 identifier = cp_parser_identifier (parser);
15695 else
15697 identifier = make_anon_name ();
15698 is_anonymous = true;
15699 if (scoped_enum_p)
15700 error_at (type_start_token->location,
15701 "anonymous scoped enum is not allowed");
15704 pop_deferring_access_checks ();
15706 /* Check for the `:' that denotes a specified underlying type in C++0x.
15707 Note that a ':' could also indicate a bitfield width, however. */
15708 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15710 cp_decl_specifier_seq type_specifiers;
15712 /* Consume the `:'. */
15713 cp_lexer_consume_token (parser->lexer);
15715 /* Parse the type-specifier-seq. */
15716 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15717 /*is_trailing_return=*/false,
15718 &type_specifiers);
15720 /* At this point this is surely not elaborated type specifier. */
15721 if (!cp_parser_parse_definitely (parser))
15722 return NULL_TREE;
15724 if (cxx_dialect < cxx11)
15725 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15727 has_underlying_type = true;
15729 /* If that didn't work, stop. */
15730 if (type_specifiers.type != error_mark_node)
15732 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15733 /*initialized=*/0, NULL);
15734 if (underlying_type == error_mark_node
15735 || check_for_bare_parameter_packs (underlying_type))
15736 underlying_type = NULL_TREE;
15740 /* Look for the `{' but don't consume it yet. */
15741 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15743 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15745 cp_parser_error (parser, "expected %<{%>");
15746 if (has_underlying_type)
15748 type = NULL_TREE;
15749 goto out;
15752 /* An opaque-enum-specifier must have a ';' here. */
15753 if ((scoped_enum_p || underlying_type)
15754 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15756 cp_parser_error (parser, "expected %<;%> or %<{%>");
15757 if (has_underlying_type)
15759 type = NULL_TREE;
15760 goto out;
15765 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15766 return NULL_TREE;
15768 if (nested_name_specifier)
15770 if (CLASS_TYPE_P (nested_name_specifier))
15772 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15773 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15774 push_scope (nested_name_specifier);
15776 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15778 push_nested_namespace (nested_name_specifier);
15782 /* Issue an error message if type-definitions are forbidden here. */
15783 if (!cp_parser_check_type_definition (parser))
15784 type = error_mark_node;
15785 else
15786 /* Create the new type. We do this before consuming the opening
15787 brace so the enum will be recorded as being on the line of its
15788 tag (or the 'enum' keyword, if there is no tag). */
15789 type = start_enum (identifier, type, underlying_type,
15790 scoped_enum_p, &is_new_type);
15792 /* If the next token is not '{' it is an opaque-enum-specifier or an
15793 elaborated-type-specifier. */
15794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15796 timevar_push (TV_PARSE_ENUM);
15797 if (nested_name_specifier
15798 && nested_name_specifier != error_mark_node)
15800 /* The following catches invalid code such as:
15801 enum class S<int>::E { A, B, C }; */
15802 if (!processing_specialization
15803 && CLASS_TYPE_P (nested_name_specifier)
15804 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15805 error_at (type_start_token->location, "cannot add an enumerator "
15806 "list to a template instantiation");
15808 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15810 error_at (type_start_token->location,
15811 "%<%T::%E%> has not been declared",
15812 TYPE_CONTEXT (nested_name_specifier),
15813 nested_name_specifier);
15814 type = error_mark_node;
15816 /* If that scope does not contain the scope in which the
15817 class was originally declared, the program is invalid. */
15818 else if (prev_scope && !is_ancestor (prev_scope,
15819 nested_name_specifier))
15821 if (at_namespace_scope_p ())
15822 error_at (type_start_token->location,
15823 "declaration of %qD in namespace %qD which does not "
15824 "enclose %qD",
15825 type, prev_scope, nested_name_specifier);
15826 else
15827 error_at (type_start_token->location,
15828 "declaration of %qD in %qD which does not "
15829 "enclose %qD",
15830 type, prev_scope, nested_name_specifier);
15831 type = error_mark_node;
15835 if (scoped_enum_p)
15836 begin_scope (sk_scoped_enum, type);
15838 /* Consume the opening brace. */
15839 cp_lexer_consume_token (parser->lexer);
15841 if (type == error_mark_node)
15842 ; /* Nothing to add */
15843 else if (OPAQUE_ENUM_P (type)
15844 || (cxx_dialect > cxx98 && processing_specialization))
15846 new_value_list = true;
15847 SET_OPAQUE_ENUM_P (type, false);
15848 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15850 else
15852 error_at (type_start_token->location,
15853 "multiple definition of %q#T", type);
15854 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15855 "previous definition here");
15856 type = error_mark_node;
15859 if (type == error_mark_node)
15860 cp_parser_skip_to_end_of_block_or_statement (parser);
15861 /* If the next token is not '}', then there are some enumerators. */
15862 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15864 if (is_anonymous && !scoped_enum_p)
15865 pedwarn (type_start_token->location, OPT_Wpedantic,
15866 "ISO C++ forbids empty anonymous enum");
15868 else
15869 cp_parser_enumerator_list (parser, type);
15871 /* Consume the final '}'. */
15872 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15874 if (scoped_enum_p)
15875 finish_scope ();
15876 timevar_pop (TV_PARSE_ENUM);
15878 else
15880 /* If a ';' follows, then it is an opaque-enum-specifier
15881 and additional restrictions apply. */
15882 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15884 if (is_anonymous)
15885 error_at (type_start_token->location,
15886 "opaque-enum-specifier without name");
15887 else if (nested_name_specifier)
15888 error_at (type_start_token->location,
15889 "opaque-enum-specifier must use a simple identifier");
15893 /* Look for trailing attributes to apply to this enumeration, and
15894 apply them if appropriate. */
15895 if (cp_parser_allow_gnu_extensions_p (parser))
15897 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15898 trailing_attr = chainon (trailing_attr, attributes);
15899 cplus_decl_attributes (&type,
15900 trailing_attr,
15901 (int) ATTR_FLAG_TYPE_IN_PLACE);
15904 /* Finish up the enumeration. */
15905 if (type != error_mark_node)
15907 if (new_value_list)
15908 finish_enum_value_list (type);
15909 if (is_new_type)
15910 finish_enum (type);
15913 if (nested_name_specifier)
15915 if (CLASS_TYPE_P (nested_name_specifier))
15917 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15918 pop_scope (nested_name_specifier);
15920 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15922 pop_nested_namespace (nested_name_specifier);
15925 out:
15926 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15927 return type;
15930 /* Parse an enumerator-list. The enumerators all have the indicated
15931 TYPE.
15933 enumerator-list:
15934 enumerator-definition
15935 enumerator-list , enumerator-definition */
15937 static void
15938 cp_parser_enumerator_list (cp_parser* parser, tree type)
15940 while (true)
15942 /* Parse an enumerator-definition. */
15943 cp_parser_enumerator_definition (parser, type);
15945 /* If the next token is not a ',', we've reached the end of
15946 the list. */
15947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15948 break;
15949 /* Otherwise, consume the `,' and keep going. */
15950 cp_lexer_consume_token (parser->lexer);
15951 /* If the next token is a `}', there is a trailing comma. */
15952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15954 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15955 pedwarn (input_location, OPT_Wpedantic,
15956 "comma at end of enumerator list");
15957 break;
15962 /* Parse an enumerator-definition. The enumerator has the indicated
15963 TYPE.
15965 enumerator-definition:
15966 enumerator
15967 enumerator = constant-expression
15969 enumerator:
15970 identifier */
15972 static void
15973 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15975 tree identifier;
15976 tree value;
15977 location_t loc;
15979 /* Save the input location because we are interested in the location
15980 of the identifier and not the location of the explicit value. */
15981 loc = cp_lexer_peek_token (parser->lexer)->location;
15983 /* Look for the identifier. */
15984 identifier = cp_parser_identifier (parser);
15985 if (identifier == error_mark_node)
15986 return;
15988 /* If the next token is an '=', then there is an explicit value. */
15989 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15991 /* Consume the `=' token. */
15992 cp_lexer_consume_token (parser->lexer);
15993 /* Parse the value. */
15994 value = cp_parser_constant_expression (parser);
15996 else
15997 value = NULL_TREE;
15999 /* If we are processing a template, make sure the initializer of the
16000 enumerator doesn't contain any bare template parameter pack. */
16001 if (check_for_bare_parameter_packs (value))
16002 value = error_mark_node;
16004 /* integral_constant_value will pull out this expression, so make sure
16005 it's folded as appropriate. */
16006 value = fold_non_dependent_expr (value);
16008 /* Create the enumerator. */
16009 build_enumerator (identifier, value, type, loc);
16012 /* Parse a namespace-name.
16014 namespace-name:
16015 original-namespace-name
16016 namespace-alias
16018 Returns the NAMESPACE_DECL for the namespace. */
16020 static tree
16021 cp_parser_namespace_name (cp_parser* parser)
16023 tree identifier;
16024 tree namespace_decl;
16026 cp_token *token = cp_lexer_peek_token (parser->lexer);
16028 /* Get the name of the namespace. */
16029 identifier = cp_parser_identifier (parser);
16030 if (identifier == error_mark_node)
16031 return error_mark_node;
16033 /* Look up the identifier in the currently active scope. Look only
16034 for namespaces, due to:
16036 [basic.lookup.udir]
16038 When looking up a namespace-name in a using-directive or alias
16039 definition, only namespace names are considered.
16041 And:
16043 [basic.lookup.qual]
16045 During the lookup of a name preceding the :: scope resolution
16046 operator, object, function, and enumerator names are ignored.
16048 (Note that cp_parser_qualifying_entity only calls this
16049 function if the token after the name is the scope resolution
16050 operator.) */
16051 namespace_decl = cp_parser_lookup_name (parser, identifier,
16052 none_type,
16053 /*is_template=*/false,
16054 /*is_namespace=*/true,
16055 /*check_dependency=*/true,
16056 /*ambiguous_decls=*/NULL,
16057 token->location);
16058 /* If it's not a namespace, issue an error. */
16059 if (namespace_decl == error_mark_node
16060 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16062 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16063 error_at (token->location, "%qD is not a namespace-name", identifier);
16064 cp_parser_error (parser, "expected namespace-name");
16065 namespace_decl = error_mark_node;
16068 return namespace_decl;
16071 /* Parse a namespace-definition.
16073 namespace-definition:
16074 named-namespace-definition
16075 unnamed-namespace-definition
16077 named-namespace-definition:
16078 original-namespace-definition
16079 extension-namespace-definition
16081 original-namespace-definition:
16082 namespace identifier { namespace-body }
16084 extension-namespace-definition:
16085 namespace original-namespace-name { namespace-body }
16087 unnamed-namespace-definition:
16088 namespace { namespace-body } */
16090 static void
16091 cp_parser_namespace_definition (cp_parser* parser)
16093 tree identifier, attribs;
16094 bool has_visibility;
16095 bool is_inline;
16097 cp_ensure_no_omp_declare_simd (parser);
16098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16100 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16101 is_inline = true;
16102 cp_lexer_consume_token (parser->lexer);
16104 else
16105 is_inline = false;
16107 /* Look for the `namespace' keyword. */
16108 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16110 /* Get the name of the namespace. We do not attempt to distinguish
16111 between an original-namespace-definition and an
16112 extension-namespace-definition at this point. The semantic
16113 analysis routines are responsible for that. */
16114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16115 identifier = cp_parser_identifier (parser);
16116 else
16117 identifier = NULL_TREE;
16119 /* Parse any specified attributes. */
16120 attribs = cp_parser_attributes_opt (parser);
16122 /* Look for the `{' to start the namespace. */
16123 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16124 /* Start the namespace. */
16125 push_namespace (identifier);
16127 /* "inline namespace" is equivalent to a stub namespace definition
16128 followed by a strong using directive. */
16129 if (is_inline)
16131 tree name_space = current_namespace;
16132 /* Set up namespace association. */
16133 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16134 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16135 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16136 /* Import the contents of the inline namespace. */
16137 pop_namespace ();
16138 do_using_directive (name_space);
16139 push_namespace (identifier);
16142 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16144 /* Parse the body of the namespace. */
16145 cp_parser_namespace_body (parser);
16147 if (has_visibility)
16148 pop_visibility (1);
16150 /* Finish the namespace. */
16151 pop_namespace ();
16152 /* Look for the final `}'. */
16153 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16156 /* Parse a namespace-body.
16158 namespace-body:
16159 declaration-seq [opt] */
16161 static void
16162 cp_parser_namespace_body (cp_parser* parser)
16164 cp_parser_declaration_seq_opt (parser);
16167 /* Parse a namespace-alias-definition.
16169 namespace-alias-definition:
16170 namespace identifier = qualified-namespace-specifier ; */
16172 static void
16173 cp_parser_namespace_alias_definition (cp_parser* parser)
16175 tree identifier;
16176 tree namespace_specifier;
16178 cp_token *token = cp_lexer_peek_token (parser->lexer);
16180 /* Look for the `namespace' keyword. */
16181 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16182 /* Look for the identifier. */
16183 identifier = cp_parser_identifier (parser);
16184 if (identifier == error_mark_node)
16185 return;
16186 /* Look for the `=' token. */
16187 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16188 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16190 error_at (token->location, "%<namespace%> definition is not allowed here");
16191 /* Skip the definition. */
16192 cp_lexer_consume_token (parser->lexer);
16193 if (cp_parser_skip_to_closing_brace (parser))
16194 cp_lexer_consume_token (parser->lexer);
16195 return;
16197 cp_parser_require (parser, CPP_EQ, RT_EQ);
16198 /* Look for the qualified-namespace-specifier. */
16199 namespace_specifier
16200 = cp_parser_qualified_namespace_specifier (parser);
16201 /* Look for the `;' token. */
16202 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16204 /* Register the alias in the symbol table. */
16205 do_namespace_alias (identifier, namespace_specifier);
16208 /* Parse a qualified-namespace-specifier.
16210 qualified-namespace-specifier:
16211 :: [opt] nested-name-specifier [opt] namespace-name
16213 Returns a NAMESPACE_DECL corresponding to the specified
16214 namespace. */
16216 static tree
16217 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16219 /* Look for the optional `::'. */
16220 cp_parser_global_scope_opt (parser,
16221 /*current_scope_valid_p=*/false);
16223 /* Look for the optional nested-name-specifier. */
16224 cp_parser_nested_name_specifier_opt (parser,
16225 /*typename_keyword_p=*/false,
16226 /*check_dependency_p=*/true,
16227 /*type_p=*/false,
16228 /*is_declaration=*/true);
16230 return cp_parser_namespace_name (parser);
16233 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16234 access declaration.
16236 using-declaration:
16237 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16238 using :: unqualified-id ;
16240 access-declaration:
16241 qualified-id ;
16245 static bool
16246 cp_parser_using_declaration (cp_parser* parser,
16247 bool access_declaration_p)
16249 cp_token *token;
16250 bool typename_p = false;
16251 bool global_scope_p;
16252 tree decl;
16253 tree identifier;
16254 tree qscope;
16255 int oldcount = errorcount;
16256 cp_token *diag_token = NULL;
16258 if (access_declaration_p)
16260 diag_token = cp_lexer_peek_token (parser->lexer);
16261 cp_parser_parse_tentatively (parser);
16263 else
16265 /* Look for the `using' keyword. */
16266 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16268 /* Peek at the next token. */
16269 token = cp_lexer_peek_token (parser->lexer);
16270 /* See if it's `typename'. */
16271 if (token->keyword == RID_TYPENAME)
16273 /* Remember that we've seen it. */
16274 typename_p = true;
16275 /* Consume the `typename' token. */
16276 cp_lexer_consume_token (parser->lexer);
16280 /* Look for the optional global scope qualification. */
16281 global_scope_p
16282 = (cp_parser_global_scope_opt (parser,
16283 /*current_scope_valid_p=*/false)
16284 != NULL_TREE);
16286 /* If we saw `typename', or didn't see `::', then there must be a
16287 nested-name-specifier present. */
16288 if (typename_p || !global_scope_p)
16290 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16291 /*check_dependency_p=*/true,
16292 /*type_p=*/false,
16293 /*is_declaration=*/true);
16294 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16296 cp_parser_skip_to_end_of_block_or_statement (parser);
16297 return false;
16300 /* Otherwise, we could be in either of the two productions. In that
16301 case, treat the nested-name-specifier as optional. */
16302 else
16303 qscope = cp_parser_nested_name_specifier_opt (parser,
16304 /*typename_keyword_p=*/false,
16305 /*check_dependency_p=*/true,
16306 /*type_p=*/false,
16307 /*is_declaration=*/true);
16308 if (!qscope)
16309 qscope = global_namespace;
16310 else if (UNSCOPED_ENUM_P (qscope))
16311 qscope = CP_TYPE_CONTEXT (qscope);
16313 if (access_declaration_p && cp_parser_error_occurred (parser))
16314 /* Something has already gone wrong; there's no need to parse
16315 further. Since an error has occurred, the return value of
16316 cp_parser_parse_definitely will be false, as required. */
16317 return cp_parser_parse_definitely (parser);
16319 token = cp_lexer_peek_token (parser->lexer);
16320 /* Parse the unqualified-id. */
16321 identifier = cp_parser_unqualified_id (parser,
16322 /*template_keyword_p=*/false,
16323 /*check_dependency_p=*/true,
16324 /*declarator_p=*/true,
16325 /*optional_p=*/false);
16327 if (access_declaration_p)
16329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16330 cp_parser_simulate_error (parser);
16331 if (!cp_parser_parse_definitely (parser))
16332 return false;
16335 /* The function we call to handle a using-declaration is different
16336 depending on what scope we are in. */
16337 if (qscope == error_mark_node || identifier == error_mark_node)
16339 else if (!identifier_p (identifier)
16340 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16341 /* [namespace.udecl]
16343 A using declaration shall not name a template-id. */
16344 error_at (token->location,
16345 "a template-id may not appear in a using-declaration");
16346 else
16348 if (at_class_scope_p ())
16350 /* Create the USING_DECL. */
16351 decl = do_class_using_decl (parser->scope, identifier);
16353 if (decl && typename_p)
16354 USING_DECL_TYPENAME_P (decl) = 1;
16356 if (check_for_bare_parameter_packs (decl))
16358 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16359 return false;
16361 else
16362 /* Add it to the list of members in this class. */
16363 finish_member_declaration (decl);
16365 else
16367 decl = cp_parser_lookup_name_simple (parser,
16368 identifier,
16369 token->location);
16370 if (decl == error_mark_node)
16371 cp_parser_name_lookup_error (parser, identifier,
16372 decl, NLE_NULL,
16373 token->location);
16374 else if (check_for_bare_parameter_packs (decl))
16376 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16377 return false;
16379 else if (!at_namespace_scope_p ())
16380 do_local_using_decl (decl, qscope, identifier);
16381 else
16382 do_toplevel_using_decl (decl, qscope, identifier);
16386 /* Look for the final `;'. */
16387 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16389 if (access_declaration_p && errorcount == oldcount)
16390 warning_at (diag_token->location, OPT_Wdeprecated,
16391 "access declarations are deprecated "
16392 "in favour of using-declarations; "
16393 "suggestion: add the %<using%> keyword");
16395 return true;
16398 /* Parse an alias-declaration.
16400 alias-declaration:
16401 using identifier attribute-specifier-seq [opt] = type-id */
16403 static tree
16404 cp_parser_alias_declaration (cp_parser* parser)
16406 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16407 location_t id_location;
16408 cp_declarator *declarator;
16409 cp_decl_specifier_seq decl_specs;
16410 bool member_p;
16411 const char *saved_message = NULL;
16413 /* Look for the `using' keyword. */
16414 cp_token *using_token
16415 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16416 if (using_token == NULL)
16417 return error_mark_node;
16419 id_location = cp_lexer_peek_token (parser->lexer)->location;
16420 id = cp_parser_identifier (parser);
16421 if (id == error_mark_node)
16422 return error_mark_node;
16424 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16425 attributes = cp_parser_attributes_opt (parser);
16426 if (attributes == error_mark_node)
16427 return error_mark_node;
16429 cp_parser_require (parser, CPP_EQ, RT_EQ);
16431 if (cp_parser_error_occurred (parser))
16432 return error_mark_node;
16434 cp_parser_commit_to_tentative_parse (parser);
16436 /* Now we are going to parse the type-id of the declaration. */
16439 [dcl.type]/3 says:
16441 "A type-specifier-seq shall not define a class or enumeration
16442 unless it appears in the type-id of an alias-declaration (7.1.3) that
16443 is not the declaration of a template-declaration."
16445 In other words, if we currently are in an alias template, the
16446 type-id should not define a type.
16448 So let's set parser->type_definition_forbidden_message in that
16449 case; cp_parser_check_type_definition (called by
16450 cp_parser_class_specifier) will then emit an error if a type is
16451 defined in the type-id. */
16452 if (parser->num_template_parameter_lists)
16454 saved_message = parser->type_definition_forbidden_message;
16455 parser->type_definition_forbidden_message =
16456 G_("types may not be defined in alias template declarations");
16459 type = cp_parser_type_id (parser);
16461 /* Restore the error message if need be. */
16462 if (parser->num_template_parameter_lists)
16463 parser->type_definition_forbidden_message = saved_message;
16465 if (type == error_mark_node
16466 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16468 cp_parser_skip_to_end_of_block_or_statement (parser);
16469 return error_mark_node;
16472 /* A typedef-name can also be introduced by an alias-declaration. The
16473 identifier following the using keyword becomes a typedef-name. It has
16474 the same semantics as if it were introduced by the typedef
16475 specifier. In particular, it does not define a new type and it shall
16476 not appear in the type-id. */
16478 clear_decl_specs (&decl_specs);
16479 decl_specs.type = type;
16480 if (attributes != NULL_TREE)
16482 decl_specs.attributes = attributes;
16483 set_and_check_decl_spec_loc (&decl_specs,
16484 ds_attribute,
16485 attrs_token);
16487 set_and_check_decl_spec_loc (&decl_specs,
16488 ds_typedef,
16489 using_token);
16490 set_and_check_decl_spec_loc (&decl_specs,
16491 ds_alias,
16492 using_token);
16494 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16495 declarator->id_loc = id_location;
16497 member_p = at_class_scope_p ();
16498 if (member_p)
16499 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16500 NULL_TREE, attributes);
16501 else
16502 decl = start_decl (declarator, &decl_specs, 0,
16503 attributes, NULL_TREE, &pushed_scope);
16504 if (decl == error_mark_node)
16505 return decl;
16507 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16509 if (pushed_scope)
16510 pop_scope (pushed_scope);
16512 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16513 added into the symbol table; otherwise, return the TYPE_DECL. */
16514 if (DECL_LANG_SPECIFIC (decl)
16515 && DECL_TEMPLATE_INFO (decl)
16516 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16518 decl = DECL_TI_TEMPLATE (decl);
16519 if (member_p)
16520 check_member_template (decl);
16523 return decl;
16526 /* Parse a using-directive.
16528 using-directive:
16529 using namespace :: [opt] nested-name-specifier [opt]
16530 namespace-name ; */
16532 static void
16533 cp_parser_using_directive (cp_parser* parser)
16535 tree namespace_decl;
16536 tree attribs;
16538 /* Look for the `using' keyword. */
16539 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16540 /* And the `namespace' keyword. */
16541 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16542 /* Look for the optional `::' operator. */
16543 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16544 /* And the optional nested-name-specifier. */
16545 cp_parser_nested_name_specifier_opt (parser,
16546 /*typename_keyword_p=*/false,
16547 /*check_dependency_p=*/true,
16548 /*type_p=*/false,
16549 /*is_declaration=*/true);
16550 /* Get the namespace being used. */
16551 namespace_decl = cp_parser_namespace_name (parser);
16552 /* And any specified attributes. */
16553 attribs = cp_parser_attributes_opt (parser);
16554 /* Update the symbol table. */
16555 parse_using_directive (namespace_decl, attribs);
16556 /* Look for the final `;'. */
16557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16560 /* Parse an asm-definition.
16562 asm-definition:
16563 asm ( string-literal ) ;
16565 GNU Extension:
16567 asm-definition:
16568 asm volatile [opt] ( string-literal ) ;
16569 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16570 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16571 : asm-operand-list [opt] ) ;
16572 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16573 : asm-operand-list [opt]
16574 : asm-clobber-list [opt] ) ;
16575 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16576 : asm-clobber-list [opt]
16577 : asm-goto-list ) ; */
16579 static void
16580 cp_parser_asm_definition (cp_parser* parser)
16582 tree string;
16583 tree outputs = NULL_TREE;
16584 tree inputs = NULL_TREE;
16585 tree clobbers = NULL_TREE;
16586 tree labels = NULL_TREE;
16587 tree asm_stmt;
16588 bool volatile_p = false;
16589 bool extended_p = false;
16590 bool invalid_inputs_p = false;
16591 bool invalid_outputs_p = false;
16592 bool goto_p = false;
16593 required_token missing = RT_NONE;
16595 /* Look for the `asm' keyword. */
16596 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16598 if (parser->in_function_body
16599 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16600 error ("%<asm%> in %<constexpr%> function");
16602 /* See if the next token is `volatile'. */
16603 if (cp_parser_allow_gnu_extensions_p (parser)
16604 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16606 /* Remember that we saw the `volatile' keyword. */
16607 volatile_p = true;
16608 /* Consume the token. */
16609 cp_lexer_consume_token (parser->lexer);
16611 if (cp_parser_allow_gnu_extensions_p (parser)
16612 && parser->in_function_body
16613 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16615 /* Remember that we saw the `goto' keyword. */
16616 goto_p = true;
16617 /* Consume the token. */
16618 cp_lexer_consume_token (parser->lexer);
16620 /* Look for the opening `('. */
16621 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16622 return;
16623 /* Look for the string. */
16624 string = cp_parser_string_literal (parser, false, false);
16625 if (string == error_mark_node)
16627 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16628 /*consume_paren=*/true);
16629 return;
16632 /* If we're allowing GNU extensions, check for the extended assembly
16633 syntax. Unfortunately, the `:' tokens need not be separated by
16634 a space in C, and so, for compatibility, we tolerate that here
16635 too. Doing that means that we have to treat the `::' operator as
16636 two `:' tokens. */
16637 if (cp_parser_allow_gnu_extensions_p (parser)
16638 && parser->in_function_body
16639 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16640 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16642 bool inputs_p = false;
16643 bool clobbers_p = false;
16644 bool labels_p = false;
16646 /* The extended syntax was used. */
16647 extended_p = true;
16649 /* Look for outputs. */
16650 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16652 /* Consume the `:'. */
16653 cp_lexer_consume_token (parser->lexer);
16654 /* Parse the output-operands. */
16655 if (cp_lexer_next_token_is_not (parser->lexer,
16656 CPP_COLON)
16657 && cp_lexer_next_token_is_not (parser->lexer,
16658 CPP_SCOPE)
16659 && cp_lexer_next_token_is_not (parser->lexer,
16660 CPP_CLOSE_PAREN)
16661 && !goto_p)
16662 outputs = cp_parser_asm_operand_list (parser);
16664 if (outputs == error_mark_node)
16665 invalid_outputs_p = true;
16667 /* If the next token is `::', there are no outputs, and the
16668 next token is the beginning of the inputs. */
16669 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16670 /* The inputs are coming next. */
16671 inputs_p = true;
16673 /* Look for inputs. */
16674 if (inputs_p
16675 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16677 /* Consume the `:' or `::'. */
16678 cp_lexer_consume_token (parser->lexer);
16679 /* Parse the output-operands. */
16680 if (cp_lexer_next_token_is_not (parser->lexer,
16681 CPP_COLON)
16682 && cp_lexer_next_token_is_not (parser->lexer,
16683 CPP_SCOPE)
16684 && cp_lexer_next_token_is_not (parser->lexer,
16685 CPP_CLOSE_PAREN))
16686 inputs = cp_parser_asm_operand_list (parser);
16688 if (inputs == error_mark_node)
16689 invalid_inputs_p = true;
16691 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16692 /* The clobbers are coming next. */
16693 clobbers_p = true;
16695 /* Look for clobbers. */
16696 if (clobbers_p
16697 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16699 clobbers_p = true;
16700 /* Consume the `:' or `::'. */
16701 cp_lexer_consume_token (parser->lexer);
16702 /* Parse the clobbers. */
16703 if (cp_lexer_next_token_is_not (parser->lexer,
16704 CPP_COLON)
16705 && cp_lexer_next_token_is_not (parser->lexer,
16706 CPP_CLOSE_PAREN))
16707 clobbers = cp_parser_asm_clobber_list (parser);
16709 else if (goto_p
16710 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16711 /* The labels are coming next. */
16712 labels_p = true;
16714 /* Look for labels. */
16715 if (labels_p
16716 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16718 labels_p = true;
16719 /* Consume the `:' or `::'. */
16720 cp_lexer_consume_token (parser->lexer);
16721 /* Parse the labels. */
16722 labels = cp_parser_asm_label_list (parser);
16725 if (goto_p && !labels_p)
16726 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16728 else if (goto_p)
16729 missing = RT_COLON_SCOPE;
16731 /* Look for the closing `)'. */
16732 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16733 missing ? missing : RT_CLOSE_PAREN))
16734 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16735 /*consume_paren=*/true);
16736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16738 if (!invalid_inputs_p && !invalid_outputs_p)
16740 /* Create the ASM_EXPR. */
16741 if (parser->in_function_body)
16743 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16744 inputs, clobbers, labels);
16745 /* If the extended syntax was not used, mark the ASM_EXPR. */
16746 if (!extended_p)
16748 tree temp = asm_stmt;
16749 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16750 temp = TREE_OPERAND (temp, 0);
16752 ASM_INPUT_P (temp) = 1;
16755 else
16756 symtab->finalize_toplevel_asm (string);
16760 /* Declarators [gram.dcl.decl] */
16762 /* Parse an init-declarator.
16764 init-declarator:
16765 declarator initializer [opt]
16767 GNU Extension:
16769 init-declarator:
16770 declarator asm-specification [opt] attributes [opt] initializer [opt]
16772 function-definition:
16773 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16774 function-body
16775 decl-specifier-seq [opt] declarator function-try-block
16777 GNU Extension:
16779 function-definition:
16780 __extension__ function-definition
16782 TM Extension:
16784 function-definition:
16785 decl-specifier-seq [opt] declarator function-transaction-block
16787 The DECL_SPECIFIERS apply to this declarator. Returns a
16788 representation of the entity declared. If MEMBER_P is TRUE, then
16789 this declarator appears in a class scope. The new DECL created by
16790 this declarator is returned.
16792 The CHECKS are access checks that should be performed once we know
16793 what entity is being declared (and, therefore, what classes have
16794 befriended it).
16796 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16797 for a function-definition here as well. If the declarator is a
16798 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16799 be TRUE upon return. By that point, the function-definition will
16800 have been completely parsed.
16802 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16803 is FALSE.
16805 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16806 parsed declaration if it is an uninitialized single declarator not followed
16807 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16808 if present, will not be consumed. If returned, this declarator will be
16809 created with SD_INITIALIZED but will not call cp_finish_decl. */
16811 static tree
16812 cp_parser_init_declarator (cp_parser* parser,
16813 cp_decl_specifier_seq *decl_specifiers,
16814 vec<deferred_access_check, va_gc> *checks,
16815 bool function_definition_allowed_p,
16816 bool member_p,
16817 int declares_class_or_enum,
16818 bool* function_definition_p,
16819 tree* maybe_range_for_decl)
16821 cp_token *token = NULL, *asm_spec_start_token = NULL,
16822 *attributes_start_token = NULL;
16823 cp_declarator *declarator;
16824 tree prefix_attributes;
16825 tree attributes = NULL;
16826 tree asm_specification;
16827 tree initializer;
16828 tree decl = NULL_TREE;
16829 tree scope;
16830 int is_initialized;
16831 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16832 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16833 "(...)". */
16834 enum cpp_ttype initialization_kind;
16835 bool is_direct_init = false;
16836 bool is_non_constant_init;
16837 int ctor_dtor_or_conv_p;
16838 bool friend_p = cp_parser_friend_p (decl_specifiers);
16839 tree pushed_scope = NULL_TREE;
16840 bool range_for_decl_p = false;
16841 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16843 /* Gather the attributes that were provided with the
16844 decl-specifiers. */
16845 prefix_attributes = decl_specifiers->attributes;
16847 /* Assume that this is not the declarator for a function
16848 definition. */
16849 if (function_definition_p)
16850 *function_definition_p = false;
16852 /* Default arguments are only permitted for function parameters. */
16853 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16854 parser->default_arg_ok_p = false;
16856 /* Defer access checks while parsing the declarator; we cannot know
16857 what names are accessible until we know what is being
16858 declared. */
16859 resume_deferring_access_checks ();
16861 /* Parse the declarator. */
16862 token = cp_lexer_peek_token (parser->lexer);
16863 declarator
16864 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16865 &ctor_dtor_or_conv_p,
16866 /*parenthesized_p=*/NULL,
16867 member_p, friend_p);
16868 /* Gather up the deferred checks. */
16869 stop_deferring_access_checks ();
16871 parser->default_arg_ok_p = saved_default_arg_ok_p;
16873 /* If the DECLARATOR was erroneous, there's no need to go
16874 further. */
16875 if (declarator == cp_error_declarator)
16876 return error_mark_node;
16878 /* Check that the number of template-parameter-lists is OK. */
16879 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16880 token->location))
16881 return error_mark_node;
16883 if (declares_class_or_enum & 2)
16884 cp_parser_check_for_definition_in_return_type (declarator,
16885 decl_specifiers->type,
16886 decl_specifiers->locations[ds_type_spec]);
16888 /* Figure out what scope the entity declared by the DECLARATOR is
16889 located in. `grokdeclarator' sometimes changes the scope, so
16890 we compute it now. */
16891 scope = get_scope_of_declarator (declarator);
16893 /* Perform any lookups in the declared type which were thought to be
16894 dependent, but are not in the scope of the declarator. */
16895 decl_specifiers->type
16896 = maybe_update_decl_type (decl_specifiers->type, scope);
16898 /* If we're allowing GNU extensions, look for an
16899 asm-specification. */
16900 if (cp_parser_allow_gnu_extensions_p (parser))
16902 /* Look for an asm-specification. */
16903 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16904 asm_specification = cp_parser_asm_specification_opt (parser);
16906 else
16907 asm_specification = NULL_TREE;
16909 /* Look for attributes. */
16910 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16911 attributes = cp_parser_attributes_opt (parser);
16913 /* Peek at the next token. */
16914 token = cp_lexer_peek_token (parser->lexer);
16916 bool bogus_implicit_tmpl = false;
16918 if (function_declarator_p (declarator))
16920 /* Check to see if the token indicates the start of a
16921 function-definition. */
16922 if (cp_parser_token_starts_function_definition_p (token))
16924 if (!function_definition_allowed_p)
16926 /* If a function-definition should not appear here, issue an
16927 error message. */
16928 cp_parser_error (parser,
16929 "a function-definition is not allowed here");
16930 return error_mark_node;
16933 location_t func_brace_location
16934 = cp_lexer_peek_token (parser->lexer)->location;
16936 /* Neither attributes nor an asm-specification are allowed
16937 on a function-definition. */
16938 if (asm_specification)
16939 error_at (asm_spec_start_token->location,
16940 "an asm-specification is not allowed "
16941 "on a function-definition");
16942 if (attributes)
16943 error_at (attributes_start_token->location,
16944 "attributes are not allowed "
16945 "on a function-definition");
16946 /* This is a function-definition. */
16947 *function_definition_p = true;
16949 /* Parse the function definition. */
16950 if (member_p)
16951 decl = cp_parser_save_member_function_body (parser,
16952 decl_specifiers,
16953 declarator,
16954 prefix_attributes);
16955 else
16956 decl =
16957 (cp_parser_function_definition_from_specifiers_and_declarator
16958 (parser, decl_specifiers, prefix_attributes, declarator));
16960 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16962 /* This is where the prologue starts... */
16963 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16964 = func_brace_location;
16967 return decl;
16970 else if (parser->fully_implicit_function_template_p)
16972 /* A non-template declaration involving a function parameter list
16973 containing an implicit template parameter will be made into a
16974 template. If the resulting declaration is not going to be an
16975 actual function then finish the template scope here to prevent it.
16976 An error message will be issued once we have a decl to talk about.
16978 FIXME probably we should do type deduction rather than create an
16979 implicit template, but the standard currently doesn't allow it. */
16980 bogus_implicit_tmpl = true;
16981 finish_fully_implicit_template (parser, NULL_TREE);
16984 /* [dcl.dcl]
16986 Only in function declarations for constructors, destructors, and
16987 type conversions can the decl-specifier-seq be omitted.
16989 We explicitly postpone this check past the point where we handle
16990 function-definitions because we tolerate function-definitions
16991 that are missing their return types in some modes. */
16992 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16994 cp_parser_error (parser,
16995 "expected constructor, destructor, or type conversion");
16996 return error_mark_node;
16999 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17000 if (token->type == CPP_EQ
17001 || token->type == CPP_OPEN_PAREN
17002 || token->type == CPP_OPEN_BRACE)
17004 is_initialized = SD_INITIALIZED;
17005 initialization_kind = token->type;
17006 if (maybe_range_for_decl)
17007 *maybe_range_for_decl = error_mark_node;
17009 if (token->type == CPP_EQ
17010 && function_declarator_p (declarator))
17012 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17013 if (t2->keyword == RID_DEFAULT)
17014 is_initialized = SD_DEFAULTED;
17015 else if (t2->keyword == RID_DELETE)
17016 is_initialized = SD_DELETED;
17019 else
17021 /* If the init-declarator isn't initialized and isn't followed by a
17022 `,' or `;', it's not a valid init-declarator. */
17023 if (token->type != CPP_COMMA
17024 && token->type != CPP_SEMICOLON)
17026 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17027 range_for_decl_p = true;
17028 else
17030 cp_parser_error (parser, "expected initializer");
17031 return error_mark_node;
17034 is_initialized = SD_UNINITIALIZED;
17035 initialization_kind = CPP_EOF;
17038 /* Because start_decl has side-effects, we should only call it if we
17039 know we're going ahead. By this point, we know that we cannot
17040 possibly be looking at any other construct. */
17041 cp_parser_commit_to_tentative_parse (parser);
17043 /* Enter the newly declared entry in the symbol table. If we're
17044 processing a declaration in a class-specifier, we wait until
17045 after processing the initializer. */
17046 if (!member_p)
17048 if (parser->in_unbraced_linkage_specification_p)
17049 decl_specifiers->storage_class = sc_extern;
17050 decl = start_decl (declarator, decl_specifiers,
17051 range_for_decl_p? SD_INITIALIZED : is_initialized,
17052 attributes, prefix_attributes, &pushed_scope);
17053 cp_finalize_omp_declare_simd (parser, decl);
17054 /* Adjust location of decl if declarator->id_loc is more appropriate:
17055 set, and decl wasn't merged with another decl, in which case its
17056 location would be different from input_location, and more accurate. */
17057 if (DECL_P (decl)
17058 && declarator->id_loc != UNKNOWN_LOCATION
17059 && DECL_SOURCE_LOCATION (decl) == input_location)
17060 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17062 else if (scope)
17063 /* Enter the SCOPE. That way unqualified names appearing in the
17064 initializer will be looked up in SCOPE. */
17065 pushed_scope = push_scope (scope);
17067 /* Perform deferred access control checks, now that we know in which
17068 SCOPE the declared entity resides. */
17069 if (!member_p && decl)
17071 tree saved_current_function_decl = NULL_TREE;
17073 /* If the entity being declared is a function, pretend that we
17074 are in its scope. If it is a `friend', it may have access to
17075 things that would not otherwise be accessible. */
17076 if (TREE_CODE (decl) == FUNCTION_DECL)
17078 saved_current_function_decl = current_function_decl;
17079 current_function_decl = decl;
17082 /* Perform access checks for template parameters. */
17083 cp_parser_perform_template_parameter_access_checks (checks);
17085 /* Perform the access control checks for the declarator and the
17086 decl-specifiers. */
17087 perform_deferred_access_checks (tf_warning_or_error);
17089 /* Restore the saved value. */
17090 if (TREE_CODE (decl) == FUNCTION_DECL)
17091 current_function_decl = saved_current_function_decl;
17094 /* Parse the initializer. */
17095 initializer = NULL_TREE;
17096 is_direct_init = false;
17097 is_non_constant_init = true;
17098 if (is_initialized)
17100 if (function_declarator_p (declarator))
17102 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17103 if (initialization_kind == CPP_EQ)
17104 initializer = cp_parser_pure_specifier (parser);
17105 else
17107 /* If the declaration was erroneous, we don't really
17108 know what the user intended, so just silently
17109 consume the initializer. */
17110 if (decl != error_mark_node)
17111 error_at (initializer_start_token->location,
17112 "initializer provided for function");
17113 cp_parser_skip_to_closing_parenthesis (parser,
17114 /*recovering=*/true,
17115 /*or_comma=*/false,
17116 /*consume_paren=*/true);
17119 else
17121 /* We want to record the extra mangling scope for in-class
17122 initializers of class members and initializers of static data
17123 member templates. The former involves deferring
17124 parsing of the initializer until end of class as with default
17125 arguments. So right here we only handle the latter. */
17126 if (!member_p && processing_template_decl)
17127 start_lambda_scope (decl);
17128 initializer = cp_parser_initializer (parser,
17129 &is_direct_init,
17130 &is_non_constant_init);
17131 if (!member_p && processing_template_decl)
17132 finish_lambda_scope ();
17133 if (initializer == error_mark_node)
17134 cp_parser_skip_to_end_of_statement (parser);
17138 /* The old parser allows attributes to appear after a parenthesized
17139 initializer. Mark Mitchell proposed removing this functionality
17140 on the GCC mailing lists on 2002-08-13. This parser accepts the
17141 attributes -- but ignores them. */
17142 if (cp_parser_allow_gnu_extensions_p (parser)
17143 && initialization_kind == CPP_OPEN_PAREN)
17144 if (cp_parser_attributes_opt (parser))
17145 warning (OPT_Wattributes,
17146 "attributes after parenthesized initializer ignored");
17148 /* And now complain about a non-function implicit template. */
17149 if (bogus_implicit_tmpl)
17150 error_at (DECL_SOURCE_LOCATION (decl),
17151 "non-function %qD declared as implicit template", decl);
17153 /* For an in-class declaration, use `grokfield' to create the
17154 declaration. */
17155 if (member_p)
17157 if (pushed_scope)
17159 pop_scope (pushed_scope);
17160 pushed_scope = NULL_TREE;
17162 decl = grokfield (declarator, decl_specifiers,
17163 initializer, !is_non_constant_init,
17164 /*asmspec=*/NULL_TREE,
17165 chainon (attributes, prefix_attributes));
17166 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17167 cp_parser_save_default_args (parser, decl);
17168 cp_finalize_omp_declare_simd (parser, decl);
17171 /* Finish processing the declaration. But, skip member
17172 declarations. */
17173 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17175 cp_finish_decl (decl,
17176 initializer, !is_non_constant_init,
17177 asm_specification,
17178 /* If the initializer is in parentheses, then this is
17179 a direct-initialization, which means that an
17180 `explicit' constructor is OK. Otherwise, an
17181 `explicit' constructor cannot be used. */
17182 ((is_direct_init || !is_initialized)
17183 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17185 else if ((cxx_dialect != cxx98) && friend_p
17186 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17187 /* Core issue #226 (C++0x only): A default template-argument
17188 shall not be specified in a friend class template
17189 declaration. */
17190 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17191 /*is_partial=*/false, /*is_friend_decl=*/1);
17193 if (!friend_p && pushed_scope)
17194 pop_scope (pushed_scope);
17196 if (function_declarator_p (declarator)
17197 && parser->fully_implicit_function_template_p)
17199 if (member_p)
17200 decl = finish_fully_implicit_template (parser, decl);
17201 else
17202 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17205 return decl;
17208 /* Parse a declarator.
17210 declarator:
17211 direct-declarator
17212 ptr-operator declarator
17214 abstract-declarator:
17215 ptr-operator abstract-declarator [opt]
17216 direct-abstract-declarator
17218 GNU Extensions:
17220 declarator:
17221 attributes [opt] direct-declarator
17222 attributes [opt] ptr-operator declarator
17224 abstract-declarator:
17225 attributes [opt] ptr-operator abstract-declarator [opt]
17226 attributes [opt] direct-abstract-declarator
17228 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17229 detect constructor, destructor or conversion operators. It is set
17230 to -1 if the declarator is a name, and +1 if it is a
17231 function. Otherwise it is set to zero. Usually you just want to
17232 test for >0, but internally the negative value is used.
17234 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17235 a decl-specifier-seq unless it declares a constructor, destructor,
17236 or conversion. It might seem that we could check this condition in
17237 semantic analysis, rather than parsing, but that makes it difficult
17238 to handle something like `f()'. We want to notice that there are
17239 no decl-specifiers, and therefore realize that this is an
17240 expression, not a declaration.)
17242 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17243 the declarator is a direct-declarator of the form "(...)".
17245 MEMBER_P is true iff this declarator is a member-declarator.
17247 FRIEND_P is true iff this declarator is a friend. */
17249 static cp_declarator *
17250 cp_parser_declarator (cp_parser* parser,
17251 cp_parser_declarator_kind dcl_kind,
17252 int* ctor_dtor_or_conv_p,
17253 bool* parenthesized_p,
17254 bool member_p, bool friend_p)
17256 cp_declarator *declarator;
17257 enum tree_code code;
17258 cp_cv_quals cv_quals;
17259 tree class_type;
17260 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17262 /* Assume this is not a constructor, destructor, or type-conversion
17263 operator. */
17264 if (ctor_dtor_or_conv_p)
17265 *ctor_dtor_or_conv_p = 0;
17267 if (cp_parser_allow_gnu_extensions_p (parser))
17268 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17270 /* Check for the ptr-operator production. */
17271 cp_parser_parse_tentatively (parser);
17272 /* Parse the ptr-operator. */
17273 code = cp_parser_ptr_operator (parser,
17274 &class_type,
17275 &cv_quals,
17276 &std_attributes);
17278 /* If that worked, then we have a ptr-operator. */
17279 if (cp_parser_parse_definitely (parser))
17281 /* If a ptr-operator was found, then this declarator was not
17282 parenthesized. */
17283 if (parenthesized_p)
17284 *parenthesized_p = true;
17285 /* The dependent declarator is optional if we are parsing an
17286 abstract-declarator. */
17287 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17288 cp_parser_parse_tentatively (parser);
17290 /* Parse the dependent declarator. */
17291 declarator = cp_parser_declarator (parser, dcl_kind,
17292 /*ctor_dtor_or_conv_p=*/NULL,
17293 /*parenthesized_p=*/NULL,
17294 /*member_p=*/false,
17295 friend_p);
17297 /* If we are parsing an abstract-declarator, we must handle the
17298 case where the dependent declarator is absent. */
17299 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17300 && !cp_parser_parse_definitely (parser))
17301 declarator = NULL;
17303 declarator = cp_parser_make_indirect_declarator
17304 (code, class_type, cv_quals, declarator, std_attributes);
17306 /* Everything else is a direct-declarator. */
17307 else
17309 if (parenthesized_p)
17310 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17311 CPP_OPEN_PAREN);
17312 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17313 ctor_dtor_or_conv_p,
17314 member_p, friend_p);
17317 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17318 declarator->attributes = gnu_attributes;
17319 return declarator;
17322 /* Parse a direct-declarator or direct-abstract-declarator.
17324 direct-declarator:
17325 declarator-id
17326 direct-declarator ( parameter-declaration-clause )
17327 cv-qualifier-seq [opt]
17328 ref-qualifier [opt]
17329 exception-specification [opt]
17330 direct-declarator [ constant-expression [opt] ]
17331 ( declarator )
17333 direct-abstract-declarator:
17334 direct-abstract-declarator [opt]
17335 ( parameter-declaration-clause )
17336 cv-qualifier-seq [opt]
17337 ref-qualifier [opt]
17338 exception-specification [opt]
17339 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17340 ( abstract-declarator )
17342 Returns a representation of the declarator. DCL_KIND is
17343 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17344 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17345 we are parsing a direct-declarator. It is
17346 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17347 of ambiguity we prefer an abstract declarator, as per
17348 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17349 as for cp_parser_declarator. */
17351 static cp_declarator *
17352 cp_parser_direct_declarator (cp_parser* parser,
17353 cp_parser_declarator_kind dcl_kind,
17354 int* ctor_dtor_or_conv_p,
17355 bool member_p, bool friend_p)
17357 cp_token *token;
17358 cp_declarator *declarator = NULL;
17359 tree scope = NULL_TREE;
17360 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17361 bool saved_in_declarator_p = parser->in_declarator_p;
17362 bool first = true;
17363 tree pushed_scope = NULL_TREE;
17365 while (true)
17367 /* Peek at the next token. */
17368 token = cp_lexer_peek_token (parser->lexer);
17369 if (token->type == CPP_OPEN_PAREN)
17371 /* This is either a parameter-declaration-clause, or a
17372 parenthesized declarator. When we know we are parsing a
17373 named declarator, it must be a parenthesized declarator
17374 if FIRST is true. For instance, `(int)' is a
17375 parameter-declaration-clause, with an omitted
17376 direct-abstract-declarator. But `((*))', is a
17377 parenthesized abstract declarator. Finally, when T is a
17378 template parameter `(T)' is a
17379 parameter-declaration-clause, and not a parenthesized
17380 named declarator.
17382 We first try and parse a parameter-declaration-clause,
17383 and then try a nested declarator (if FIRST is true).
17385 It is not an error for it not to be a
17386 parameter-declaration-clause, even when FIRST is
17387 false. Consider,
17389 int i (int);
17390 int i (3);
17392 The first is the declaration of a function while the
17393 second is the definition of a variable, including its
17394 initializer.
17396 Having seen only the parenthesis, we cannot know which of
17397 these two alternatives should be selected. Even more
17398 complex are examples like:
17400 int i (int (a));
17401 int i (int (3));
17403 The former is a function-declaration; the latter is a
17404 variable initialization.
17406 Thus again, we try a parameter-declaration-clause, and if
17407 that fails, we back out and return. */
17409 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17411 tree params;
17412 bool is_declarator = false;
17414 /* In a member-declarator, the only valid interpretation
17415 of a parenthesis is the start of a
17416 parameter-declaration-clause. (It is invalid to
17417 initialize a static data member with a parenthesized
17418 initializer; only the "=" form of initialization is
17419 permitted.) */
17420 if (!member_p)
17421 cp_parser_parse_tentatively (parser);
17423 /* Consume the `('. */
17424 cp_lexer_consume_token (parser->lexer);
17425 if (first)
17427 /* If this is going to be an abstract declarator, we're
17428 in a declarator and we can't have default args. */
17429 parser->default_arg_ok_p = false;
17430 parser->in_declarator_p = true;
17433 begin_scope (sk_function_parms, NULL_TREE);
17435 /* Parse the parameter-declaration-clause. */
17436 params = cp_parser_parameter_declaration_clause (parser);
17438 /* Consume the `)'. */
17439 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17441 /* If all went well, parse the cv-qualifier-seq,
17442 ref-qualifier and the exception-specification. */
17443 if (member_p || cp_parser_parse_definitely (parser))
17445 cp_cv_quals cv_quals;
17446 cp_virt_specifiers virt_specifiers;
17447 cp_ref_qualifier ref_qual;
17448 tree exception_specification;
17449 tree late_return;
17450 tree attrs;
17451 bool memfn = (member_p || (pushed_scope
17452 && CLASS_TYPE_P (pushed_scope)));
17454 is_declarator = true;
17456 if (ctor_dtor_or_conv_p)
17457 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17458 first = false;
17460 /* Parse the cv-qualifier-seq. */
17461 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17462 /* Parse the ref-qualifier. */
17463 ref_qual = cp_parser_ref_qualifier_opt (parser);
17464 /* And the exception-specification. */
17465 exception_specification
17466 = cp_parser_exception_specification_opt (parser);
17468 attrs = cp_parser_std_attribute_spec_seq (parser);
17470 /* In here, we handle cases where attribute is used after
17471 the function declaration. For example:
17472 void func (int x) __attribute__((vector(..))); */
17473 if (flag_cilkplus
17474 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17476 cp_parser_parse_tentatively (parser);
17477 tree attr = cp_parser_gnu_attributes_opt (parser);
17478 if (cp_lexer_next_token_is_not (parser->lexer,
17479 CPP_SEMICOLON)
17480 && cp_lexer_next_token_is_not (parser->lexer,
17481 CPP_OPEN_BRACE))
17482 cp_parser_abort_tentative_parse (parser);
17483 else if (!cp_parser_parse_definitely (parser))
17485 else
17486 attrs = chainon (attr, attrs);
17488 late_return = (cp_parser_late_return_type_opt
17489 (parser, declarator,
17490 memfn ? cv_quals : -1));
17493 /* Parse the virt-specifier-seq. */
17494 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17496 /* Create the function-declarator. */
17497 declarator = make_call_declarator (declarator,
17498 params,
17499 cv_quals,
17500 virt_specifiers,
17501 ref_qual,
17502 exception_specification,
17503 late_return);
17504 declarator->std_attributes = attrs;
17505 /* Any subsequent parameter lists are to do with
17506 return type, so are not those of the declared
17507 function. */
17508 parser->default_arg_ok_p = false;
17511 /* Remove the function parms from scope. */
17512 pop_bindings_and_leave_scope ();
17514 if (is_declarator)
17515 /* Repeat the main loop. */
17516 continue;
17519 /* If this is the first, we can try a parenthesized
17520 declarator. */
17521 if (first)
17523 bool saved_in_type_id_in_expr_p;
17525 parser->default_arg_ok_p = saved_default_arg_ok_p;
17526 parser->in_declarator_p = saved_in_declarator_p;
17528 /* Consume the `('. */
17529 cp_lexer_consume_token (parser->lexer);
17530 /* Parse the nested declarator. */
17531 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17532 parser->in_type_id_in_expr_p = true;
17533 declarator
17534 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17535 /*parenthesized_p=*/NULL,
17536 member_p, friend_p);
17537 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17538 first = false;
17539 /* Expect a `)'. */
17540 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17541 declarator = cp_error_declarator;
17542 if (declarator == cp_error_declarator)
17543 break;
17545 goto handle_declarator;
17547 /* Otherwise, we must be done. */
17548 else
17549 break;
17551 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17552 && token->type == CPP_OPEN_SQUARE
17553 && !cp_next_tokens_can_be_attribute_p (parser))
17555 /* Parse an array-declarator. */
17556 tree bounds, attrs;
17558 if (ctor_dtor_or_conv_p)
17559 *ctor_dtor_or_conv_p = 0;
17561 first = false;
17562 parser->default_arg_ok_p = false;
17563 parser->in_declarator_p = true;
17564 /* Consume the `['. */
17565 cp_lexer_consume_token (parser->lexer);
17566 /* Peek at the next token. */
17567 token = cp_lexer_peek_token (parser->lexer);
17568 /* If the next token is `]', then there is no
17569 constant-expression. */
17570 if (token->type != CPP_CLOSE_SQUARE)
17572 bool non_constant_p;
17573 bounds
17574 = cp_parser_constant_expression (parser,
17575 /*allow_non_constant=*/true,
17576 &non_constant_p);
17577 if (!non_constant_p)
17578 /* OK */;
17579 else if (error_operand_p (bounds))
17580 /* Already gave an error. */;
17581 else if (!parser->in_function_body
17582 || current_binding_level->kind == sk_function_parms)
17584 /* Normally, the array bound must be an integral constant
17585 expression. However, as an extension, we allow VLAs
17586 in function scopes as long as they aren't part of a
17587 parameter declaration. */
17588 cp_parser_error (parser,
17589 "array bound is not an integer constant");
17590 bounds = error_mark_node;
17592 else if (processing_template_decl
17593 && !type_dependent_expression_p (bounds))
17595 /* Remember this wasn't a constant-expression. */
17596 bounds = build_nop (TREE_TYPE (bounds), bounds);
17597 TREE_SIDE_EFFECTS (bounds) = 1;
17600 else
17601 bounds = NULL_TREE;
17602 /* Look for the closing `]'. */
17603 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17605 declarator = cp_error_declarator;
17606 break;
17609 attrs = cp_parser_std_attribute_spec_seq (parser);
17610 declarator = make_array_declarator (declarator, bounds);
17611 declarator->std_attributes = attrs;
17613 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17616 tree qualifying_scope;
17617 tree unqualified_name;
17618 tree attrs;
17619 special_function_kind sfk;
17620 bool abstract_ok;
17621 bool pack_expansion_p = false;
17622 cp_token *declarator_id_start_token;
17624 /* Parse a declarator-id */
17625 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17626 if (abstract_ok)
17628 cp_parser_parse_tentatively (parser);
17630 /* If we see an ellipsis, we should be looking at a
17631 parameter pack. */
17632 if (token->type == CPP_ELLIPSIS)
17634 /* Consume the `...' */
17635 cp_lexer_consume_token (parser->lexer);
17637 pack_expansion_p = true;
17641 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17642 unqualified_name
17643 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17644 qualifying_scope = parser->scope;
17645 if (abstract_ok)
17647 bool okay = false;
17649 if (!unqualified_name && pack_expansion_p)
17651 /* Check whether an error occurred. */
17652 okay = !cp_parser_error_occurred (parser);
17654 /* We already consumed the ellipsis to mark a
17655 parameter pack, but we have no way to report it,
17656 so abort the tentative parse. We will be exiting
17657 immediately anyway. */
17658 cp_parser_abort_tentative_parse (parser);
17660 else
17661 okay = cp_parser_parse_definitely (parser);
17663 if (!okay)
17664 unqualified_name = error_mark_node;
17665 else if (unqualified_name
17666 && (qualifying_scope
17667 || (!identifier_p (unqualified_name))))
17669 cp_parser_error (parser, "expected unqualified-id");
17670 unqualified_name = error_mark_node;
17674 if (!unqualified_name)
17675 return NULL;
17676 if (unqualified_name == error_mark_node)
17678 declarator = cp_error_declarator;
17679 pack_expansion_p = false;
17680 declarator->parameter_pack_p = false;
17681 break;
17684 attrs = cp_parser_std_attribute_spec_seq (parser);
17686 if (qualifying_scope && at_namespace_scope_p ()
17687 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17689 /* In the declaration of a member of a template class
17690 outside of the class itself, the SCOPE will sometimes
17691 be a TYPENAME_TYPE. For example, given:
17693 template <typename T>
17694 int S<T>::R::i = 3;
17696 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17697 this context, we must resolve S<T>::R to an ordinary
17698 type, rather than a typename type.
17700 The reason we normally avoid resolving TYPENAME_TYPEs
17701 is that a specialization of `S' might render
17702 `S<T>::R' not a type. However, if `S' is
17703 specialized, then this `i' will not be used, so there
17704 is no harm in resolving the types here. */
17705 tree type;
17707 /* Resolve the TYPENAME_TYPE. */
17708 type = resolve_typename_type (qualifying_scope,
17709 /*only_current_p=*/false);
17710 /* If that failed, the declarator is invalid. */
17711 if (TREE_CODE (type) == TYPENAME_TYPE)
17713 if (typedef_variant_p (type))
17714 error_at (declarator_id_start_token->location,
17715 "cannot define member of dependent typedef "
17716 "%qT", type);
17717 else
17718 error_at (declarator_id_start_token->location,
17719 "%<%T::%E%> is not a type",
17720 TYPE_CONTEXT (qualifying_scope),
17721 TYPE_IDENTIFIER (qualifying_scope));
17723 qualifying_scope = type;
17726 sfk = sfk_none;
17728 if (unqualified_name)
17730 tree class_type;
17732 if (qualifying_scope
17733 && CLASS_TYPE_P (qualifying_scope))
17734 class_type = qualifying_scope;
17735 else
17736 class_type = current_class_type;
17738 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17740 tree name_type = TREE_TYPE (unqualified_name);
17741 if (class_type && same_type_p (name_type, class_type))
17743 if (qualifying_scope
17744 && CLASSTYPE_USE_TEMPLATE (name_type))
17746 error_at (declarator_id_start_token->location,
17747 "invalid use of constructor as a template");
17748 inform (declarator_id_start_token->location,
17749 "use %<%T::%D%> instead of %<%T::%D%> to "
17750 "name the constructor in a qualified name",
17751 class_type,
17752 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17753 class_type, name_type);
17754 declarator = cp_error_declarator;
17755 break;
17757 else
17758 unqualified_name = constructor_name (class_type);
17760 else
17762 /* We do not attempt to print the declarator
17763 here because we do not have enough
17764 information about its original syntactic
17765 form. */
17766 cp_parser_error (parser, "invalid declarator");
17767 declarator = cp_error_declarator;
17768 break;
17772 if (class_type)
17774 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17775 sfk = sfk_destructor;
17776 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17777 sfk = sfk_conversion;
17778 else if (/* There's no way to declare a constructor
17779 for an anonymous type, even if the type
17780 got a name for linkage purposes. */
17781 !TYPE_WAS_ANONYMOUS (class_type)
17782 /* Handle correctly (c++/19200):
17784 struct S {
17785 struct T{};
17786 friend void S(T);
17789 and also:
17791 namespace N {
17792 void S();
17795 struct S {
17796 friend void N::S();
17797 }; */
17798 && !(friend_p
17799 && class_type != qualifying_scope)
17800 && constructor_name_p (unqualified_name,
17801 class_type))
17803 unqualified_name = constructor_name (class_type);
17804 sfk = sfk_constructor;
17806 else if (is_overloaded_fn (unqualified_name)
17807 && DECL_CONSTRUCTOR_P (get_first_fn
17808 (unqualified_name)))
17809 sfk = sfk_constructor;
17811 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17812 *ctor_dtor_or_conv_p = -1;
17815 declarator = make_id_declarator (qualifying_scope,
17816 unqualified_name,
17817 sfk);
17818 declarator->std_attributes = attrs;
17819 declarator->id_loc = token->location;
17820 declarator->parameter_pack_p = pack_expansion_p;
17822 if (pack_expansion_p)
17823 maybe_warn_variadic_templates ();
17826 handle_declarator:;
17827 scope = get_scope_of_declarator (declarator);
17828 if (scope)
17830 /* Any names that appear after the declarator-id for a
17831 member are looked up in the containing scope. */
17832 if (at_function_scope_p ())
17834 /* But declarations with qualified-ids can't appear in a
17835 function. */
17836 cp_parser_error (parser, "qualified-id in declaration");
17837 declarator = cp_error_declarator;
17838 break;
17840 pushed_scope = push_scope (scope);
17842 parser->in_declarator_p = true;
17843 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17844 || (declarator && declarator->kind == cdk_id))
17845 /* Default args are only allowed on function
17846 declarations. */
17847 parser->default_arg_ok_p = saved_default_arg_ok_p;
17848 else
17849 parser->default_arg_ok_p = false;
17851 first = false;
17853 /* We're done. */
17854 else
17855 break;
17858 /* For an abstract declarator, we might wind up with nothing at this
17859 point. That's an error; the declarator is not optional. */
17860 if (!declarator)
17861 cp_parser_error (parser, "expected declarator");
17863 /* If we entered a scope, we must exit it now. */
17864 if (pushed_scope)
17865 pop_scope (pushed_scope);
17867 parser->default_arg_ok_p = saved_default_arg_ok_p;
17868 parser->in_declarator_p = saved_in_declarator_p;
17870 return declarator;
17873 /* Parse a ptr-operator.
17875 ptr-operator:
17876 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17877 * cv-qualifier-seq [opt]
17879 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17880 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17882 GNU Extension:
17884 ptr-operator:
17885 & cv-qualifier-seq [opt]
17887 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17888 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17889 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17890 filled in with the TYPE containing the member. *CV_QUALS is
17891 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17892 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17893 Note that the tree codes returned by this function have nothing
17894 to do with the types of trees that will be eventually be created
17895 to represent the pointer or reference type being parsed. They are
17896 just constants with suggestive names. */
17897 static enum tree_code
17898 cp_parser_ptr_operator (cp_parser* parser,
17899 tree* type,
17900 cp_cv_quals *cv_quals,
17901 tree *attributes)
17903 enum tree_code code = ERROR_MARK;
17904 cp_token *token;
17905 tree attrs = NULL_TREE;
17907 /* Assume that it's not a pointer-to-member. */
17908 *type = NULL_TREE;
17909 /* And that there are no cv-qualifiers. */
17910 *cv_quals = TYPE_UNQUALIFIED;
17912 /* Peek at the next token. */
17913 token = cp_lexer_peek_token (parser->lexer);
17915 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17916 if (token->type == CPP_MULT)
17917 code = INDIRECT_REF;
17918 else if (token->type == CPP_AND)
17919 code = ADDR_EXPR;
17920 else if ((cxx_dialect != cxx98) &&
17921 token->type == CPP_AND_AND) /* C++0x only */
17922 code = NON_LVALUE_EXPR;
17924 if (code != ERROR_MARK)
17926 /* Consume the `*', `&' or `&&'. */
17927 cp_lexer_consume_token (parser->lexer);
17929 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17930 `&', if we are allowing GNU extensions. (The only qualifier
17931 that can legally appear after `&' is `restrict', but that is
17932 enforced during semantic analysis. */
17933 if (code == INDIRECT_REF
17934 || cp_parser_allow_gnu_extensions_p (parser))
17935 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17937 attrs = cp_parser_std_attribute_spec_seq (parser);
17938 if (attributes != NULL)
17939 *attributes = attrs;
17941 else
17943 /* Try the pointer-to-member case. */
17944 cp_parser_parse_tentatively (parser);
17945 /* Look for the optional `::' operator. */
17946 cp_parser_global_scope_opt (parser,
17947 /*current_scope_valid_p=*/false);
17948 /* Look for the nested-name specifier. */
17949 token = cp_lexer_peek_token (parser->lexer);
17950 cp_parser_nested_name_specifier (parser,
17951 /*typename_keyword_p=*/false,
17952 /*check_dependency_p=*/true,
17953 /*type_p=*/false,
17954 /*is_declaration=*/false);
17955 /* If we found it, and the next token is a `*', then we are
17956 indeed looking at a pointer-to-member operator. */
17957 if (!cp_parser_error_occurred (parser)
17958 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17960 /* Indicate that the `*' operator was used. */
17961 code = INDIRECT_REF;
17963 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17964 error_at (token->location, "%qD is a namespace", parser->scope);
17965 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17966 error_at (token->location, "cannot form pointer to member of "
17967 "non-class %q#T", parser->scope);
17968 else
17970 /* The type of which the member is a member is given by the
17971 current SCOPE. */
17972 *type = parser->scope;
17973 /* The next name will not be qualified. */
17974 parser->scope = NULL_TREE;
17975 parser->qualifying_scope = NULL_TREE;
17976 parser->object_scope = NULL_TREE;
17977 /* Look for optional c++11 attributes. */
17978 attrs = cp_parser_std_attribute_spec_seq (parser);
17979 if (attributes != NULL)
17980 *attributes = attrs;
17981 /* Look for the optional cv-qualifier-seq. */
17982 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17985 /* If that didn't work we don't have a ptr-operator. */
17986 if (!cp_parser_parse_definitely (parser))
17987 cp_parser_error (parser, "expected ptr-operator");
17990 return code;
17993 /* Parse an (optional) cv-qualifier-seq.
17995 cv-qualifier-seq:
17996 cv-qualifier cv-qualifier-seq [opt]
17998 cv-qualifier:
17999 const
18000 volatile
18002 GNU Extension:
18004 cv-qualifier:
18005 __restrict__
18007 Returns a bitmask representing the cv-qualifiers. */
18009 static cp_cv_quals
18010 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18012 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18014 while (true)
18016 cp_token *token;
18017 cp_cv_quals cv_qualifier;
18019 /* Peek at the next token. */
18020 token = cp_lexer_peek_token (parser->lexer);
18021 /* See if it's a cv-qualifier. */
18022 switch (token->keyword)
18024 case RID_CONST:
18025 cv_qualifier = TYPE_QUAL_CONST;
18026 break;
18028 case RID_VOLATILE:
18029 cv_qualifier = TYPE_QUAL_VOLATILE;
18030 break;
18032 case RID_RESTRICT:
18033 cv_qualifier = TYPE_QUAL_RESTRICT;
18034 break;
18036 default:
18037 cv_qualifier = TYPE_UNQUALIFIED;
18038 break;
18041 if (!cv_qualifier)
18042 break;
18044 if (cv_quals & cv_qualifier)
18046 error_at (token->location, "duplicate cv-qualifier");
18047 cp_lexer_purge_token (parser->lexer);
18049 else
18051 cp_lexer_consume_token (parser->lexer);
18052 cv_quals |= cv_qualifier;
18056 return cv_quals;
18059 /* Parse an (optional) ref-qualifier
18061 ref-qualifier:
18065 Returns cp_ref_qualifier representing ref-qualifier. */
18067 static cp_ref_qualifier
18068 cp_parser_ref_qualifier_opt (cp_parser* parser)
18070 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18072 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18073 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18074 return ref_qual;
18076 while (true)
18078 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18079 cp_token *token = cp_lexer_peek_token (parser->lexer);
18081 switch (token->type)
18083 case CPP_AND:
18084 curr_ref_qual = REF_QUAL_LVALUE;
18085 break;
18087 case CPP_AND_AND:
18088 curr_ref_qual = REF_QUAL_RVALUE;
18089 break;
18091 default:
18092 curr_ref_qual = REF_QUAL_NONE;
18093 break;
18096 if (!curr_ref_qual)
18097 break;
18098 else if (ref_qual)
18100 error_at (token->location, "multiple ref-qualifiers");
18101 cp_lexer_purge_token (parser->lexer);
18103 else
18105 ref_qual = curr_ref_qual;
18106 cp_lexer_consume_token (parser->lexer);
18110 return ref_qual;
18113 /* Parse an (optional) virt-specifier-seq.
18115 virt-specifier-seq:
18116 virt-specifier virt-specifier-seq [opt]
18118 virt-specifier:
18119 override
18120 final
18122 Returns a bitmask representing the virt-specifiers. */
18124 static cp_virt_specifiers
18125 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18127 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18129 while (true)
18131 cp_token *token;
18132 cp_virt_specifiers virt_specifier;
18134 /* Peek at the next token. */
18135 token = cp_lexer_peek_token (parser->lexer);
18136 /* See if it's a virt-specifier-qualifier. */
18137 if (token->type != CPP_NAME)
18138 break;
18139 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18141 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18142 virt_specifier = VIRT_SPEC_OVERRIDE;
18144 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18146 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18147 virt_specifier = VIRT_SPEC_FINAL;
18149 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18151 virt_specifier = VIRT_SPEC_FINAL;
18153 else
18154 break;
18156 if (virt_specifiers & virt_specifier)
18158 error_at (token->location, "duplicate virt-specifier");
18159 cp_lexer_purge_token (parser->lexer);
18161 else
18163 cp_lexer_consume_token (parser->lexer);
18164 virt_specifiers |= virt_specifier;
18167 return virt_specifiers;
18170 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18171 is in scope even though it isn't real. */
18173 void
18174 inject_this_parameter (tree ctype, cp_cv_quals quals)
18176 tree this_parm;
18178 if (current_class_ptr)
18180 /* We don't clear this between NSDMIs. Is it already what we want? */
18181 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18182 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18183 && cp_type_quals (type) == quals)
18184 return;
18187 this_parm = build_this_parm (ctype, quals);
18188 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18189 current_class_ptr = NULL_TREE;
18190 current_class_ref
18191 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18192 current_class_ptr = this_parm;
18195 /* Return true iff our current scope is a non-static data member
18196 initializer. */
18198 bool
18199 parsing_nsdmi (void)
18201 /* We recognize NSDMI context by the context-less 'this' pointer set up
18202 by the function above. */
18203 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18204 return true;
18205 return false;
18208 /* Parse a late-specified return type, if any. This is not a separate
18209 non-terminal, but part of a function declarator, which looks like
18211 -> trailing-type-specifier-seq abstract-declarator(opt)
18213 Returns the type indicated by the type-id.
18215 In addition to this this parses any queued up omp declare simd
18216 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18218 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18219 function. */
18221 static tree
18222 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18223 cp_cv_quals quals)
18225 cp_token *token;
18226 tree type = NULL_TREE;
18227 bool declare_simd_p = (parser->omp_declare_simd
18228 && declarator
18229 && declarator->kind == cdk_id);
18231 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18232 && declarator && declarator->kind == cdk_id);
18234 /* Peek at the next token. */
18235 token = cp_lexer_peek_token (parser->lexer);
18236 /* A late-specified return type is indicated by an initial '->'. */
18237 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18238 return NULL_TREE;
18240 tree save_ccp = current_class_ptr;
18241 tree save_ccr = current_class_ref;
18242 if (quals >= 0)
18244 /* DR 1207: 'this' is in scope in the trailing return type. */
18245 inject_this_parameter (current_class_type, quals);
18248 if (token->type == CPP_DEREF)
18250 /* Consume the ->. */
18251 cp_lexer_consume_token (parser->lexer);
18253 type = cp_parser_trailing_type_id (parser);
18256 if (cilk_simd_fn_vector_p)
18257 declarator->std_attributes
18258 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18259 declarator->std_attributes);
18260 if (declare_simd_p)
18261 declarator->std_attributes
18262 = cp_parser_late_parsing_omp_declare_simd (parser,
18263 declarator->std_attributes);
18265 if (quals >= 0)
18267 current_class_ptr = save_ccp;
18268 current_class_ref = save_ccr;
18271 return type;
18274 /* Parse a declarator-id.
18276 declarator-id:
18277 id-expression
18278 :: [opt] nested-name-specifier [opt] type-name
18280 In the `id-expression' case, the value returned is as for
18281 cp_parser_id_expression if the id-expression was an unqualified-id.
18282 If the id-expression was a qualified-id, then a SCOPE_REF is
18283 returned. The first operand is the scope (either a NAMESPACE_DECL
18284 or TREE_TYPE), but the second is still just a representation of an
18285 unqualified-id. */
18287 static tree
18288 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18290 tree id;
18291 /* The expression must be an id-expression. Assume that qualified
18292 names are the names of types so that:
18294 template <class T>
18295 int S<T>::R::i = 3;
18297 will work; we must treat `S<T>::R' as the name of a type.
18298 Similarly, assume that qualified names are templates, where
18299 required, so that:
18301 template <class T>
18302 int S<T>::R<T>::i = 3;
18304 will work, too. */
18305 id = cp_parser_id_expression (parser,
18306 /*template_keyword_p=*/false,
18307 /*check_dependency_p=*/false,
18308 /*template_p=*/NULL,
18309 /*declarator_p=*/true,
18310 optional_p);
18311 if (id && BASELINK_P (id))
18312 id = BASELINK_FUNCTIONS (id);
18313 return id;
18316 /* Parse a type-id.
18318 type-id:
18319 type-specifier-seq abstract-declarator [opt]
18321 Returns the TYPE specified. */
18323 static tree
18324 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18325 bool is_trailing_return)
18327 cp_decl_specifier_seq type_specifier_seq;
18328 cp_declarator *abstract_declarator;
18330 /* Parse the type-specifier-seq. */
18331 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18332 is_trailing_return,
18333 &type_specifier_seq);
18334 if (type_specifier_seq.type == error_mark_node)
18335 return error_mark_node;
18337 /* There might or might not be an abstract declarator. */
18338 cp_parser_parse_tentatively (parser);
18339 /* Look for the declarator. */
18340 abstract_declarator
18341 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18342 /*parenthesized_p=*/NULL,
18343 /*member_p=*/false,
18344 /*friend_p=*/false);
18345 /* Check to see if there really was a declarator. */
18346 if (!cp_parser_parse_definitely (parser))
18347 abstract_declarator = NULL;
18349 if (type_specifier_seq.type
18350 /* None of the valid uses of 'auto' in C++14 involve the type-id
18351 nonterminal, but it is valid in a trailing-return-type. */
18352 && !(cxx_dialect >= cxx14 && is_trailing_return)
18353 && type_uses_auto (type_specifier_seq.type))
18355 /* A type-id with type 'auto' is only ok if the abstract declarator
18356 is a function declarator with a late-specified return type. */
18357 if (abstract_declarator
18358 && abstract_declarator->kind == cdk_function
18359 && abstract_declarator->u.function.late_return_type)
18360 /* OK */;
18361 else
18363 error ("invalid use of %<auto%>");
18364 return error_mark_node;
18368 return groktypename (&type_specifier_seq, abstract_declarator,
18369 is_template_arg);
18372 static tree cp_parser_type_id (cp_parser *parser)
18374 return cp_parser_type_id_1 (parser, false, false);
18377 static tree cp_parser_template_type_arg (cp_parser *parser)
18379 tree r;
18380 const char *saved_message = parser->type_definition_forbidden_message;
18381 parser->type_definition_forbidden_message
18382 = G_("types may not be defined in template arguments");
18383 r = cp_parser_type_id_1 (parser, true, false);
18384 parser->type_definition_forbidden_message = saved_message;
18385 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18387 error ("invalid use of %<auto%> in template argument");
18388 r = error_mark_node;
18390 return r;
18393 static tree cp_parser_trailing_type_id (cp_parser *parser)
18395 return cp_parser_type_id_1 (parser, false, true);
18398 /* Parse a type-specifier-seq.
18400 type-specifier-seq:
18401 type-specifier type-specifier-seq [opt]
18403 GNU extension:
18405 type-specifier-seq:
18406 attributes type-specifier-seq [opt]
18408 If IS_DECLARATION is true, we are at the start of a "condition" or
18409 exception-declaration, so we might be followed by a declarator-id.
18411 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18412 i.e. we've just seen "->".
18414 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18416 static void
18417 cp_parser_type_specifier_seq (cp_parser* parser,
18418 bool is_declaration,
18419 bool is_trailing_return,
18420 cp_decl_specifier_seq *type_specifier_seq)
18422 bool seen_type_specifier = false;
18423 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18424 cp_token *start_token = NULL;
18426 /* Clear the TYPE_SPECIFIER_SEQ. */
18427 clear_decl_specs (type_specifier_seq);
18429 /* In the context of a trailing return type, enum E { } is an
18430 elaborated-type-specifier followed by a function-body, not an
18431 enum-specifier. */
18432 if (is_trailing_return)
18433 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18435 /* Parse the type-specifiers and attributes. */
18436 while (true)
18438 tree type_specifier;
18439 bool is_cv_qualifier;
18441 /* Check for attributes first. */
18442 if (cp_next_tokens_can_be_attribute_p (parser))
18444 type_specifier_seq->attributes =
18445 chainon (type_specifier_seq->attributes,
18446 cp_parser_attributes_opt (parser));
18447 continue;
18450 /* record the token of the beginning of the type specifier seq,
18451 for error reporting purposes*/
18452 if (!start_token)
18453 start_token = cp_lexer_peek_token (parser->lexer);
18455 /* Look for the type-specifier. */
18456 type_specifier = cp_parser_type_specifier (parser,
18457 flags,
18458 type_specifier_seq,
18459 /*is_declaration=*/false,
18460 NULL,
18461 &is_cv_qualifier);
18462 if (!type_specifier)
18464 /* If the first type-specifier could not be found, this is not a
18465 type-specifier-seq at all. */
18466 if (!seen_type_specifier)
18468 /* Set in_declarator_p to avoid skipping to the semicolon. */
18469 int in_decl = parser->in_declarator_p;
18470 parser->in_declarator_p = true;
18472 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18473 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18474 cp_parser_error (parser, "expected type-specifier");
18476 parser->in_declarator_p = in_decl;
18478 type_specifier_seq->type = error_mark_node;
18479 return;
18481 /* If subsequent type-specifiers could not be found, the
18482 type-specifier-seq is complete. */
18483 break;
18486 seen_type_specifier = true;
18487 /* The standard says that a condition can be:
18489 type-specifier-seq declarator = assignment-expression
18491 However, given:
18493 struct S {};
18494 if (int S = ...)
18496 we should treat the "S" as a declarator, not as a
18497 type-specifier. The standard doesn't say that explicitly for
18498 type-specifier-seq, but it does say that for
18499 decl-specifier-seq in an ordinary declaration. Perhaps it
18500 would be clearer just to allow a decl-specifier-seq here, and
18501 then add a semantic restriction that if any decl-specifiers
18502 that are not type-specifiers appear, the program is invalid. */
18503 if (is_declaration && !is_cv_qualifier)
18504 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18508 /* Return whether the function currently being declared has an associated
18509 template parameter list. */
18511 static bool
18512 function_being_declared_is_template_p (cp_parser* parser)
18514 if (!current_template_parms || processing_template_parmlist)
18515 return false;
18517 if (parser->implicit_template_scope)
18518 return true;
18520 if (at_class_scope_p ()
18521 && TYPE_BEING_DEFINED (current_class_type))
18522 return parser->num_template_parameter_lists != 0;
18524 return ((int) parser->num_template_parameter_lists > template_class_depth
18525 (current_class_type));
18528 /* Parse a parameter-declaration-clause.
18530 parameter-declaration-clause:
18531 parameter-declaration-list [opt] ... [opt]
18532 parameter-declaration-list , ...
18534 Returns a representation for the parameter declarations. A return
18535 value of NULL indicates a parameter-declaration-clause consisting
18536 only of an ellipsis. */
18538 static tree
18539 cp_parser_parameter_declaration_clause (cp_parser* parser)
18541 tree parameters;
18542 cp_token *token;
18543 bool ellipsis_p;
18544 bool is_error;
18546 struct cleanup {
18547 cp_parser* parser;
18548 int auto_is_implicit_function_template_parm_p;
18549 ~cleanup() {
18550 parser->auto_is_implicit_function_template_parm_p
18551 = auto_is_implicit_function_template_parm_p;
18553 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18555 (void) cleanup;
18557 if (!processing_specialization
18558 && !processing_template_parmlist
18559 && !processing_explicit_instantiation)
18560 if (!current_function_decl
18561 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18562 parser->auto_is_implicit_function_template_parm_p = true;
18564 /* Peek at the next token. */
18565 token = cp_lexer_peek_token (parser->lexer);
18566 /* Check for trivial parameter-declaration-clauses. */
18567 if (token->type == CPP_ELLIPSIS)
18569 /* Consume the `...' token. */
18570 cp_lexer_consume_token (parser->lexer);
18571 return NULL_TREE;
18573 else if (token->type == CPP_CLOSE_PAREN)
18574 /* There are no parameters. */
18576 #ifndef NO_IMPLICIT_EXTERN_C
18577 if (in_system_header_at (input_location)
18578 && current_class_type == NULL
18579 && current_lang_name == lang_name_c)
18580 return NULL_TREE;
18581 else
18582 #endif
18583 return void_list_node;
18585 /* Check for `(void)', too, which is a special case. */
18586 else if (token->keyword == RID_VOID
18587 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18588 == CPP_CLOSE_PAREN))
18590 /* Consume the `void' token. */
18591 cp_lexer_consume_token (parser->lexer);
18592 /* There are no parameters. */
18593 return void_list_node;
18596 /* Parse the parameter-declaration-list. */
18597 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18598 /* If a parse error occurred while parsing the
18599 parameter-declaration-list, then the entire
18600 parameter-declaration-clause is erroneous. */
18601 if (is_error)
18602 return NULL;
18604 /* Peek at the next token. */
18605 token = cp_lexer_peek_token (parser->lexer);
18606 /* If it's a `,', the clause should terminate with an ellipsis. */
18607 if (token->type == CPP_COMMA)
18609 /* Consume the `,'. */
18610 cp_lexer_consume_token (parser->lexer);
18611 /* Expect an ellipsis. */
18612 ellipsis_p
18613 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18615 /* It might also be `...' if the optional trailing `,' was
18616 omitted. */
18617 else if (token->type == CPP_ELLIPSIS)
18619 /* Consume the `...' token. */
18620 cp_lexer_consume_token (parser->lexer);
18621 /* And remember that we saw it. */
18622 ellipsis_p = true;
18624 else
18625 ellipsis_p = false;
18627 /* Finish the parameter list. */
18628 if (!ellipsis_p)
18629 parameters = chainon (parameters, void_list_node);
18631 return parameters;
18634 /* Parse a parameter-declaration-list.
18636 parameter-declaration-list:
18637 parameter-declaration
18638 parameter-declaration-list , parameter-declaration
18640 Returns a representation of the parameter-declaration-list, as for
18641 cp_parser_parameter_declaration_clause. However, the
18642 `void_list_node' is never appended to the list. Upon return,
18643 *IS_ERROR will be true iff an error occurred. */
18645 static tree
18646 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18648 tree parameters = NULL_TREE;
18649 tree *tail = &parameters;
18650 bool saved_in_unbraced_linkage_specification_p;
18651 int index = 0;
18653 /* Assume all will go well. */
18654 *is_error = false;
18655 /* The special considerations that apply to a function within an
18656 unbraced linkage specifications do not apply to the parameters
18657 to the function. */
18658 saved_in_unbraced_linkage_specification_p
18659 = parser->in_unbraced_linkage_specification_p;
18660 parser->in_unbraced_linkage_specification_p = false;
18662 /* Look for more parameters. */
18663 while (true)
18665 cp_parameter_declarator *parameter;
18666 tree decl = error_mark_node;
18667 bool parenthesized_p = false;
18668 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18669 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18670 (current_template_parms)) : 0);
18672 /* Parse the parameter. */
18673 parameter
18674 = cp_parser_parameter_declaration (parser,
18675 /*template_parm_p=*/false,
18676 &parenthesized_p);
18678 /* We don't know yet if the enclosing context is deprecated, so wait
18679 and warn in grokparms if appropriate. */
18680 deprecated_state = DEPRECATED_SUPPRESS;
18682 if (parameter)
18684 /* If a function parameter pack was specified and an implicit template
18685 parameter was introduced during cp_parser_parameter_declaration,
18686 change any implicit parameters introduced into packs. */
18687 if (parser->implicit_template_parms
18688 && parameter->declarator
18689 && parameter->declarator->parameter_pack_p)
18691 int latest_template_parm_idx = TREE_VEC_LENGTH
18692 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18694 if (latest_template_parm_idx != template_parm_idx)
18695 parameter->decl_specifiers.type = convert_generic_types_to_packs
18696 (parameter->decl_specifiers.type,
18697 template_parm_idx, latest_template_parm_idx);
18700 decl = grokdeclarator (parameter->declarator,
18701 &parameter->decl_specifiers,
18702 PARM,
18703 parameter->default_argument != NULL_TREE,
18704 &parameter->decl_specifiers.attributes);
18707 deprecated_state = DEPRECATED_NORMAL;
18709 /* If a parse error occurred parsing the parameter declaration,
18710 then the entire parameter-declaration-list is erroneous. */
18711 if (decl == error_mark_node)
18713 *is_error = true;
18714 parameters = error_mark_node;
18715 break;
18718 if (parameter->decl_specifiers.attributes)
18719 cplus_decl_attributes (&decl,
18720 parameter->decl_specifiers.attributes,
18722 if (DECL_NAME (decl))
18723 decl = pushdecl (decl);
18725 if (decl != error_mark_node)
18727 retrofit_lang_decl (decl);
18728 DECL_PARM_INDEX (decl) = ++index;
18729 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18732 /* Add the new parameter to the list. */
18733 *tail = build_tree_list (parameter->default_argument, decl);
18734 tail = &TREE_CHAIN (*tail);
18736 /* Peek at the next token. */
18737 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18738 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18739 /* These are for Objective-C++ */
18740 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18741 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18742 /* The parameter-declaration-list is complete. */
18743 break;
18744 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18746 cp_token *token;
18748 /* Peek at the next token. */
18749 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18750 /* If it's an ellipsis, then the list is complete. */
18751 if (token->type == CPP_ELLIPSIS)
18752 break;
18753 /* Otherwise, there must be more parameters. Consume the
18754 `,'. */
18755 cp_lexer_consume_token (parser->lexer);
18756 /* When parsing something like:
18758 int i(float f, double d)
18760 we can tell after seeing the declaration for "f" that we
18761 are not looking at an initialization of a variable "i",
18762 but rather at the declaration of a function "i".
18764 Due to the fact that the parsing of template arguments
18765 (as specified to a template-id) requires backtracking we
18766 cannot use this technique when inside a template argument
18767 list. */
18768 if (!parser->in_template_argument_list_p
18769 && !parser->in_type_id_in_expr_p
18770 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18771 /* However, a parameter-declaration of the form
18772 "float(f)" (which is a valid declaration of a
18773 parameter "f") can also be interpreted as an
18774 expression (the conversion of "f" to "float"). */
18775 && !parenthesized_p)
18776 cp_parser_commit_to_tentative_parse (parser);
18778 else
18780 cp_parser_error (parser, "expected %<,%> or %<...%>");
18781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18782 cp_parser_skip_to_closing_parenthesis (parser,
18783 /*recovering=*/true,
18784 /*or_comma=*/false,
18785 /*consume_paren=*/false);
18786 break;
18790 parser->in_unbraced_linkage_specification_p
18791 = saved_in_unbraced_linkage_specification_p;
18793 /* Reset implicit_template_scope if we are about to leave the function
18794 parameter list that introduced it. Note that for out-of-line member
18795 definitions, there will be one or more class scopes before we get to
18796 the template parameter scope. */
18798 if (cp_binding_level *its = parser->implicit_template_scope)
18799 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18801 while (maybe_its->kind == sk_class)
18802 maybe_its = maybe_its->level_chain;
18803 if (maybe_its == its)
18805 parser->implicit_template_parms = 0;
18806 parser->implicit_template_scope = 0;
18810 return parameters;
18813 /* Parse a parameter declaration.
18815 parameter-declaration:
18816 decl-specifier-seq ... [opt] declarator
18817 decl-specifier-seq declarator = assignment-expression
18818 decl-specifier-seq ... [opt] abstract-declarator [opt]
18819 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18821 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18822 declares a template parameter. (In that case, a non-nested `>'
18823 token encountered during the parsing of the assignment-expression
18824 is not interpreted as a greater-than operator.)
18826 Returns a representation of the parameter, or NULL if an error
18827 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18828 true iff the declarator is of the form "(p)". */
18830 static cp_parameter_declarator *
18831 cp_parser_parameter_declaration (cp_parser *parser,
18832 bool template_parm_p,
18833 bool *parenthesized_p)
18835 int declares_class_or_enum;
18836 cp_decl_specifier_seq decl_specifiers;
18837 cp_declarator *declarator;
18838 tree default_argument;
18839 cp_token *token = NULL, *declarator_token_start = NULL;
18840 const char *saved_message;
18842 /* In a template parameter, `>' is not an operator.
18844 [temp.param]
18846 When parsing a default template-argument for a non-type
18847 template-parameter, the first non-nested `>' is taken as the end
18848 of the template parameter-list rather than a greater-than
18849 operator. */
18851 /* Type definitions may not appear in parameter types. */
18852 saved_message = parser->type_definition_forbidden_message;
18853 parser->type_definition_forbidden_message
18854 = G_("types may not be defined in parameter types");
18856 /* Parse the declaration-specifiers. */
18857 cp_parser_decl_specifier_seq (parser,
18858 CP_PARSER_FLAGS_NONE,
18859 &decl_specifiers,
18860 &declares_class_or_enum);
18862 /* Complain about missing 'typename' or other invalid type names. */
18863 if (!decl_specifiers.any_type_specifiers_p
18864 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18865 decl_specifiers.type = error_mark_node;
18867 /* If an error occurred, there's no reason to attempt to parse the
18868 rest of the declaration. */
18869 if (cp_parser_error_occurred (parser))
18871 parser->type_definition_forbidden_message = saved_message;
18872 return NULL;
18875 /* Peek at the next token. */
18876 token = cp_lexer_peek_token (parser->lexer);
18878 /* If the next token is a `)', `,', `=', `>', or `...', then there
18879 is no declarator. However, when variadic templates are enabled,
18880 there may be a declarator following `...'. */
18881 if (token->type == CPP_CLOSE_PAREN
18882 || token->type == CPP_COMMA
18883 || token->type == CPP_EQ
18884 || token->type == CPP_GREATER)
18886 declarator = NULL;
18887 if (parenthesized_p)
18888 *parenthesized_p = false;
18890 /* Otherwise, there should be a declarator. */
18891 else
18893 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18894 parser->default_arg_ok_p = false;
18896 /* After seeing a decl-specifier-seq, if the next token is not a
18897 "(", there is no possibility that the code is a valid
18898 expression. Therefore, if parsing tentatively, we commit at
18899 this point. */
18900 if (!parser->in_template_argument_list_p
18901 /* In an expression context, having seen:
18903 (int((char ...
18905 we cannot be sure whether we are looking at a
18906 function-type (taking a "char" as a parameter) or a cast
18907 of some object of type "char" to "int". */
18908 && !parser->in_type_id_in_expr_p
18909 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18910 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18911 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18912 cp_parser_commit_to_tentative_parse (parser);
18913 /* Parse the declarator. */
18914 declarator_token_start = token;
18915 declarator = cp_parser_declarator (parser,
18916 CP_PARSER_DECLARATOR_EITHER,
18917 /*ctor_dtor_or_conv_p=*/NULL,
18918 parenthesized_p,
18919 /*member_p=*/false,
18920 /*friend_p=*/false);
18921 parser->default_arg_ok_p = saved_default_arg_ok_p;
18922 /* After the declarator, allow more attributes. */
18923 decl_specifiers.attributes
18924 = chainon (decl_specifiers.attributes,
18925 cp_parser_attributes_opt (parser));
18928 /* If the next token is an ellipsis, and we have not seen a
18929 declarator name, and the type of the declarator contains parameter
18930 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18931 a parameter pack expansion expression. Otherwise, leave the
18932 ellipsis for a C-style variadic function. */
18933 token = cp_lexer_peek_token (parser->lexer);
18934 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18936 tree type = decl_specifiers.type;
18938 if (type && DECL_P (type))
18939 type = TREE_TYPE (type);
18941 if (type
18942 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18943 && declarator_can_be_parameter_pack (declarator)
18944 && (!declarator || !declarator->parameter_pack_p)
18945 && uses_parameter_packs (type))
18947 /* Consume the `...'. */
18948 cp_lexer_consume_token (parser->lexer);
18949 maybe_warn_variadic_templates ();
18951 /* Build a pack expansion type */
18952 if (declarator)
18953 declarator->parameter_pack_p = true;
18954 else
18955 decl_specifiers.type = make_pack_expansion (type);
18959 /* The restriction on defining new types applies only to the type
18960 of the parameter, not to the default argument. */
18961 parser->type_definition_forbidden_message = saved_message;
18963 /* If the next token is `=', then process a default argument. */
18964 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18966 token = cp_lexer_peek_token (parser->lexer);
18967 /* If we are defining a class, then the tokens that make up the
18968 default argument must be saved and processed later. */
18969 if (!template_parm_p && at_class_scope_p ()
18970 && TYPE_BEING_DEFINED (current_class_type)
18971 && !LAMBDA_TYPE_P (current_class_type))
18972 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18973 /* Outside of a class definition, we can just parse the
18974 assignment-expression. */
18975 else
18976 default_argument
18977 = cp_parser_default_argument (parser, template_parm_p);
18979 if (!parser->default_arg_ok_p)
18981 if (flag_permissive)
18982 warning (0, "deprecated use of default argument for parameter of non-function");
18983 else
18985 error_at (token->location,
18986 "default arguments are only "
18987 "permitted for function parameters");
18988 default_argument = NULL_TREE;
18991 else if ((declarator && declarator->parameter_pack_p)
18992 || (decl_specifiers.type
18993 && PACK_EXPANSION_P (decl_specifiers.type)))
18995 /* Find the name of the parameter pack. */
18996 cp_declarator *id_declarator = declarator;
18997 while (id_declarator && id_declarator->kind != cdk_id)
18998 id_declarator = id_declarator->declarator;
19000 if (id_declarator && id_declarator->kind == cdk_id)
19001 error_at (declarator_token_start->location,
19002 template_parm_p
19003 ? G_("template parameter pack %qD "
19004 "cannot have a default argument")
19005 : G_("parameter pack %qD cannot have "
19006 "a default argument"),
19007 id_declarator->u.id.unqualified_name);
19008 else
19009 error_at (declarator_token_start->location,
19010 template_parm_p
19011 ? G_("template parameter pack cannot have "
19012 "a default argument")
19013 : G_("parameter pack cannot have a "
19014 "default argument"));
19016 default_argument = NULL_TREE;
19019 else
19020 default_argument = NULL_TREE;
19022 return make_parameter_declarator (&decl_specifiers,
19023 declarator,
19024 default_argument);
19027 /* Parse a default argument and return it.
19029 TEMPLATE_PARM_P is true if this is a default argument for a
19030 non-type template parameter. */
19031 static tree
19032 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19034 tree default_argument = NULL_TREE;
19035 bool saved_greater_than_is_operator_p;
19036 bool saved_local_variables_forbidden_p;
19037 bool non_constant_p, is_direct_init;
19039 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19040 set correctly. */
19041 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19042 parser->greater_than_is_operator_p = !template_parm_p;
19043 /* Local variable names (and the `this' keyword) may not
19044 appear in a default argument. */
19045 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19046 parser->local_variables_forbidden_p = true;
19047 /* Parse the assignment-expression. */
19048 if (template_parm_p)
19049 push_deferring_access_checks (dk_no_deferred);
19050 tree saved_class_ptr = NULL_TREE;
19051 tree saved_class_ref = NULL_TREE;
19052 /* The "this" pointer is not valid in a default argument. */
19053 if (cfun)
19055 saved_class_ptr = current_class_ptr;
19056 cp_function_chain->x_current_class_ptr = NULL_TREE;
19057 saved_class_ref = current_class_ref;
19058 cp_function_chain->x_current_class_ref = NULL_TREE;
19060 default_argument
19061 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19062 /* Restore the "this" pointer. */
19063 if (cfun)
19065 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19066 cp_function_chain->x_current_class_ref = saved_class_ref;
19068 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19069 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19070 if (template_parm_p)
19071 pop_deferring_access_checks ();
19072 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19073 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19075 return default_argument;
19078 /* Parse a function-body.
19080 function-body:
19081 compound_statement */
19083 static void
19084 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19086 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19089 /* Parse a ctor-initializer-opt followed by a function-body. Return
19090 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19091 is true we are parsing a function-try-block. */
19093 static bool
19094 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19095 bool in_function_try_block)
19097 tree body, list;
19098 bool ctor_initializer_p;
19099 const bool check_body_p =
19100 DECL_CONSTRUCTOR_P (current_function_decl)
19101 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19102 tree last = NULL;
19104 /* Begin the function body. */
19105 body = begin_function_body ();
19106 /* Parse the optional ctor-initializer. */
19107 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19109 /* If we're parsing a constexpr constructor definition, we need
19110 to check that the constructor body is indeed empty. However,
19111 before we get to cp_parser_function_body lot of junk has been
19112 generated, so we can't just check that we have an empty block.
19113 Rather we take a snapshot of the outermost block, and check whether
19114 cp_parser_function_body changed its state. */
19115 if (check_body_p)
19117 list = cur_stmt_list;
19118 if (STATEMENT_LIST_TAIL (list))
19119 last = STATEMENT_LIST_TAIL (list)->stmt;
19121 /* Parse the function-body. */
19122 cp_parser_function_body (parser, in_function_try_block);
19123 if (check_body_p)
19124 check_constexpr_ctor_body (last, list, /*complain=*/true);
19125 /* Finish the function body. */
19126 finish_function_body (body);
19128 return ctor_initializer_p;
19131 /* Parse an initializer.
19133 initializer:
19134 = initializer-clause
19135 ( expression-list )
19137 Returns an expression representing the initializer. If no
19138 initializer is present, NULL_TREE is returned.
19140 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19141 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19142 set to TRUE if there is no initializer present. If there is an
19143 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19144 is set to true; otherwise it is set to false. */
19146 static tree
19147 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19148 bool* non_constant_p)
19150 cp_token *token;
19151 tree init;
19153 /* Peek at the next token. */
19154 token = cp_lexer_peek_token (parser->lexer);
19156 /* Let our caller know whether or not this initializer was
19157 parenthesized. */
19158 *is_direct_init = (token->type != CPP_EQ);
19159 /* Assume that the initializer is constant. */
19160 *non_constant_p = false;
19162 if (token->type == CPP_EQ)
19164 /* Consume the `='. */
19165 cp_lexer_consume_token (parser->lexer);
19166 /* Parse the initializer-clause. */
19167 init = cp_parser_initializer_clause (parser, non_constant_p);
19169 else if (token->type == CPP_OPEN_PAREN)
19171 vec<tree, va_gc> *vec;
19172 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19173 /*cast_p=*/false,
19174 /*allow_expansion_p=*/true,
19175 non_constant_p);
19176 if (vec == NULL)
19177 return error_mark_node;
19178 init = build_tree_list_vec (vec);
19179 release_tree_vector (vec);
19181 else if (token->type == CPP_OPEN_BRACE)
19183 cp_lexer_set_source_position (parser->lexer);
19184 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19185 init = cp_parser_braced_list (parser, non_constant_p);
19186 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19188 else
19190 /* Anything else is an error. */
19191 cp_parser_error (parser, "expected initializer");
19192 init = error_mark_node;
19195 return init;
19198 /* Parse an initializer-clause.
19200 initializer-clause:
19201 assignment-expression
19202 braced-init-list
19204 Returns an expression representing the initializer.
19206 If the `assignment-expression' production is used the value
19207 returned is simply a representation for the expression.
19209 Otherwise, calls cp_parser_braced_list. */
19211 static tree
19212 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19214 tree initializer;
19216 /* Assume the expression is constant. */
19217 *non_constant_p = false;
19219 /* If it is not a `{', then we are looking at an
19220 assignment-expression. */
19221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19223 initializer
19224 = cp_parser_constant_expression (parser,
19225 /*allow_non_constant_p=*/true,
19226 non_constant_p);
19228 else
19229 initializer = cp_parser_braced_list (parser, non_constant_p);
19231 return initializer;
19234 /* Parse a brace-enclosed initializer list.
19236 braced-init-list:
19237 { initializer-list , [opt] }
19240 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19241 the elements of the initializer-list (or NULL, if the last
19242 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19243 NULL_TREE. There is no way to detect whether or not the optional
19244 trailing `,' was provided. NON_CONSTANT_P is as for
19245 cp_parser_initializer. */
19247 static tree
19248 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19250 tree initializer;
19252 /* Consume the `{' token. */
19253 cp_lexer_consume_token (parser->lexer);
19254 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19255 initializer = make_node (CONSTRUCTOR);
19256 /* If it's not a `}', then there is a non-trivial initializer. */
19257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19259 /* Parse the initializer list. */
19260 CONSTRUCTOR_ELTS (initializer)
19261 = cp_parser_initializer_list (parser, non_constant_p);
19262 /* A trailing `,' token is allowed. */
19263 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19264 cp_lexer_consume_token (parser->lexer);
19266 else
19267 *non_constant_p = false;
19268 /* Now, there should be a trailing `}'. */
19269 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19270 TREE_TYPE (initializer) = init_list_type_node;
19271 return initializer;
19274 /* Consume tokens up to, and including, the next non-nested closing `]'.
19275 Returns true iff we found a closing `]'. */
19277 static bool
19278 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19280 unsigned square_depth = 0;
19282 while (true)
19284 cp_token * token = cp_lexer_peek_token (parser->lexer);
19286 switch (token->type)
19288 case CPP_EOF:
19289 case CPP_PRAGMA_EOL:
19290 /* If we've run out of tokens, then there is no closing `]'. */
19291 return false;
19293 case CPP_OPEN_SQUARE:
19294 ++square_depth;
19295 break;
19297 case CPP_CLOSE_SQUARE:
19298 if (!square_depth--)
19300 cp_lexer_consume_token (parser->lexer);
19301 return true;
19303 break;
19305 default:
19306 break;
19309 /* Consume the token. */
19310 cp_lexer_consume_token (parser->lexer);
19314 /* Return true if we are looking at an array-designator, false otherwise. */
19316 static bool
19317 cp_parser_array_designator_p (cp_parser *parser)
19319 /* Consume the `['. */
19320 cp_lexer_consume_token (parser->lexer);
19322 cp_lexer_save_tokens (parser->lexer);
19324 /* Skip tokens until the next token is a closing square bracket.
19325 If we find the closing `]', and the next token is a `=', then
19326 we are looking at an array designator. */
19327 bool array_designator_p
19328 = (cp_parser_skip_to_closing_square_bracket (parser)
19329 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19331 /* Roll back the tokens we skipped. */
19332 cp_lexer_rollback_tokens (parser->lexer);
19334 return array_designator_p;
19337 /* Parse an initializer-list.
19339 initializer-list:
19340 initializer-clause ... [opt]
19341 initializer-list , initializer-clause ... [opt]
19343 GNU Extension:
19345 initializer-list:
19346 designation initializer-clause ...[opt]
19347 initializer-list , designation initializer-clause ...[opt]
19349 designation:
19350 . identifier =
19351 identifier :
19352 [ constant-expression ] =
19354 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19355 for the initializer. If the INDEX of the elt is non-NULL, it is the
19356 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19357 as for cp_parser_initializer. */
19359 static vec<constructor_elt, va_gc> *
19360 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19362 vec<constructor_elt, va_gc> *v = NULL;
19364 /* Assume all of the expressions are constant. */
19365 *non_constant_p = false;
19367 /* Parse the rest of the list. */
19368 while (true)
19370 cp_token *token;
19371 tree designator;
19372 tree initializer;
19373 bool clause_non_constant_p;
19375 /* If the next token is an identifier and the following one is a
19376 colon, we are looking at the GNU designated-initializer
19377 syntax. */
19378 if (cp_parser_allow_gnu_extensions_p (parser)
19379 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19382 /* Warn the user that they are using an extension. */
19383 pedwarn (input_location, OPT_Wpedantic,
19384 "ISO C++ does not allow designated initializers");
19385 /* Consume the identifier. */
19386 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19387 /* Consume the `:'. */
19388 cp_lexer_consume_token (parser->lexer);
19390 /* Also handle the C99 syntax, '. id ='. */
19391 else if (cp_parser_allow_gnu_extensions_p (parser)
19392 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19393 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19394 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19396 /* Warn the user that they are using an extension. */
19397 pedwarn (input_location, OPT_Wpedantic,
19398 "ISO C++ does not allow C99 designated initializers");
19399 /* Consume the `.'. */
19400 cp_lexer_consume_token (parser->lexer);
19401 /* Consume the identifier. */
19402 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19403 /* Consume the `='. */
19404 cp_lexer_consume_token (parser->lexer);
19406 /* Also handle C99 array designators, '[ const ] ='. */
19407 else if (cp_parser_allow_gnu_extensions_p (parser)
19408 && !c_dialect_objc ()
19409 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19411 /* In C++11, [ could start a lambda-introducer. */
19412 bool non_const = false;
19414 cp_parser_parse_tentatively (parser);
19416 if (!cp_parser_array_designator_p (parser))
19418 cp_parser_simulate_error (parser);
19419 designator = NULL_TREE;
19421 else
19423 designator = cp_parser_constant_expression (parser, true,
19424 &non_const);
19425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19426 cp_parser_require (parser, CPP_EQ, RT_EQ);
19429 if (!cp_parser_parse_definitely (parser))
19430 designator = NULL_TREE;
19431 else if (non_const)
19432 require_potential_rvalue_constant_expression (designator);
19434 else
19435 designator = NULL_TREE;
19437 /* Parse the initializer. */
19438 initializer = cp_parser_initializer_clause (parser,
19439 &clause_non_constant_p);
19440 /* If any clause is non-constant, so is the entire initializer. */
19441 if (clause_non_constant_p)
19442 *non_constant_p = true;
19444 /* If we have an ellipsis, this is an initializer pack
19445 expansion. */
19446 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19448 /* Consume the `...'. */
19449 cp_lexer_consume_token (parser->lexer);
19451 /* Turn the initializer into an initializer expansion. */
19452 initializer = make_pack_expansion (initializer);
19455 /* Add it to the vector. */
19456 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19458 /* If the next token is not a comma, we have reached the end of
19459 the list. */
19460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19461 break;
19463 /* Peek at the next token. */
19464 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19465 /* If the next token is a `}', then we're still done. An
19466 initializer-clause can have a trailing `,' after the
19467 initializer-list and before the closing `}'. */
19468 if (token->type == CPP_CLOSE_BRACE)
19469 break;
19471 /* Consume the `,' token. */
19472 cp_lexer_consume_token (parser->lexer);
19475 return v;
19478 /* Classes [gram.class] */
19480 /* Parse a class-name.
19482 class-name:
19483 identifier
19484 template-id
19486 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19487 to indicate that names looked up in dependent types should be
19488 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19489 keyword has been used to indicate that the name that appears next
19490 is a template. TAG_TYPE indicates the explicit tag given before
19491 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19492 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19493 is the class being defined in a class-head.
19495 Returns the TYPE_DECL representing the class. */
19497 static tree
19498 cp_parser_class_name (cp_parser *parser,
19499 bool typename_keyword_p,
19500 bool template_keyword_p,
19501 enum tag_types tag_type,
19502 bool check_dependency_p,
19503 bool class_head_p,
19504 bool is_declaration)
19506 tree decl;
19507 tree scope;
19508 bool typename_p;
19509 cp_token *token;
19510 tree identifier = NULL_TREE;
19512 /* All class-names start with an identifier. */
19513 token = cp_lexer_peek_token (parser->lexer);
19514 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19516 cp_parser_error (parser, "expected class-name");
19517 return error_mark_node;
19520 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19521 to a template-id, so we save it here. */
19522 scope = parser->scope;
19523 if (scope == error_mark_node)
19524 return error_mark_node;
19526 /* Any name names a type if we're following the `typename' keyword
19527 in a qualified name where the enclosing scope is type-dependent. */
19528 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19529 && dependent_type_p (scope));
19530 /* Handle the common case (an identifier, but not a template-id)
19531 efficiently. */
19532 if (token->type == CPP_NAME
19533 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19535 cp_token *identifier_token;
19536 bool ambiguous_p;
19538 /* Look for the identifier. */
19539 identifier_token = cp_lexer_peek_token (parser->lexer);
19540 ambiguous_p = identifier_token->error_reported;
19541 identifier = cp_parser_identifier (parser);
19542 /* If the next token isn't an identifier, we are certainly not
19543 looking at a class-name. */
19544 if (identifier == error_mark_node)
19545 decl = error_mark_node;
19546 /* If we know this is a type-name, there's no need to look it
19547 up. */
19548 else if (typename_p)
19549 decl = identifier;
19550 else
19552 tree ambiguous_decls;
19553 /* If we already know that this lookup is ambiguous, then
19554 we've already issued an error message; there's no reason
19555 to check again. */
19556 if (ambiguous_p)
19558 cp_parser_simulate_error (parser);
19559 return error_mark_node;
19561 /* If the next token is a `::', then the name must be a type
19562 name.
19564 [basic.lookup.qual]
19566 During the lookup for a name preceding the :: scope
19567 resolution operator, object, function, and enumerator
19568 names are ignored. */
19569 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19570 tag_type = typename_type;
19571 /* Look up the name. */
19572 decl = cp_parser_lookup_name (parser, identifier,
19573 tag_type,
19574 /*is_template=*/false,
19575 /*is_namespace=*/false,
19576 check_dependency_p,
19577 &ambiguous_decls,
19578 identifier_token->location);
19579 if (ambiguous_decls)
19581 if (cp_parser_parsing_tentatively (parser))
19582 cp_parser_simulate_error (parser);
19583 return error_mark_node;
19587 else
19589 /* Try a template-id. */
19590 decl = cp_parser_template_id (parser, template_keyword_p,
19591 check_dependency_p,
19592 tag_type,
19593 is_declaration);
19594 if (decl == error_mark_node)
19595 return error_mark_node;
19598 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19600 /* If this is a typename, create a TYPENAME_TYPE. */
19601 if (typename_p && decl != error_mark_node)
19603 decl = make_typename_type (scope, decl, typename_type,
19604 /*complain=*/tf_error);
19605 if (decl != error_mark_node)
19606 decl = TYPE_NAME (decl);
19609 decl = strip_using_decl (decl);
19611 /* Check to see that it is really the name of a class. */
19612 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19613 && identifier_p (TREE_OPERAND (decl, 0))
19614 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19615 /* Situations like this:
19617 template <typename T> struct A {
19618 typename T::template X<int>::I i;
19621 are problematic. Is `T::template X<int>' a class-name? The
19622 standard does not seem to be definitive, but there is no other
19623 valid interpretation of the following `::'. Therefore, those
19624 names are considered class-names. */
19626 decl = make_typename_type (scope, decl, tag_type, tf_error);
19627 if (decl != error_mark_node)
19628 decl = TYPE_NAME (decl);
19630 else if (TREE_CODE (decl) != TYPE_DECL
19631 || TREE_TYPE (decl) == error_mark_node
19632 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19633 /* In Objective-C 2.0, a classname followed by '.' starts a
19634 dot-syntax expression, and it's not a type-name. */
19635 || (c_dialect_objc ()
19636 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19637 && objc_is_class_name (decl)))
19638 decl = error_mark_node;
19640 if (decl == error_mark_node)
19641 cp_parser_error (parser, "expected class-name");
19642 else if (identifier && !parser->scope)
19643 maybe_note_name_used_in_class (identifier, decl);
19645 return decl;
19648 /* Parse a class-specifier.
19650 class-specifier:
19651 class-head { member-specification [opt] }
19653 Returns the TREE_TYPE representing the class. */
19655 static tree
19656 cp_parser_class_specifier_1 (cp_parser* parser)
19658 tree type;
19659 tree attributes = NULL_TREE;
19660 bool nested_name_specifier_p;
19661 unsigned saved_num_template_parameter_lists;
19662 bool saved_in_function_body;
19663 unsigned char in_statement;
19664 bool in_switch_statement_p;
19665 bool saved_in_unbraced_linkage_specification_p;
19666 tree old_scope = NULL_TREE;
19667 tree scope = NULL_TREE;
19668 cp_token *closing_brace;
19670 push_deferring_access_checks (dk_no_deferred);
19672 /* Parse the class-head. */
19673 type = cp_parser_class_head (parser,
19674 &nested_name_specifier_p);
19675 /* If the class-head was a semantic disaster, skip the entire body
19676 of the class. */
19677 if (!type)
19679 cp_parser_skip_to_end_of_block_or_statement (parser);
19680 pop_deferring_access_checks ();
19681 return error_mark_node;
19684 /* Look for the `{'. */
19685 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19687 pop_deferring_access_checks ();
19688 return error_mark_node;
19691 cp_ensure_no_omp_declare_simd (parser);
19693 /* Issue an error message if type-definitions are forbidden here. */
19694 cp_parser_check_type_definition (parser);
19695 /* Remember that we are defining one more class. */
19696 ++parser->num_classes_being_defined;
19697 /* Inside the class, surrounding template-parameter-lists do not
19698 apply. */
19699 saved_num_template_parameter_lists
19700 = parser->num_template_parameter_lists;
19701 parser->num_template_parameter_lists = 0;
19702 /* We are not in a function body. */
19703 saved_in_function_body = parser->in_function_body;
19704 parser->in_function_body = false;
19705 /* Or in a loop. */
19706 in_statement = parser->in_statement;
19707 parser->in_statement = 0;
19708 /* Or in a switch. */
19709 in_switch_statement_p = parser->in_switch_statement_p;
19710 parser->in_switch_statement_p = false;
19711 /* We are not immediately inside an extern "lang" block. */
19712 saved_in_unbraced_linkage_specification_p
19713 = parser->in_unbraced_linkage_specification_p;
19714 parser->in_unbraced_linkage_specification_p = false;
19716 /* Start the class. */
19717 if (nested_name_specifier_p)
19719 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19720 old_scope = push_inner_scope (scope);
19722 type = begin_class_definition (type);
19724 if (type == error_mark_node)
19725 /* If the type is erroneous, skip the entire body of the class. */
19726 cp_parser_skip_to_closing_brace (parser);
19727 else
19728 /* Parse the member-specification. */
19729 cp_parser_member_specification_opt (parser);
19731 /* Look for the trailing `}'. */
19732 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19733 /* Look for trailing attributes to apply to this class. */
19734 if (cp_parser_allow_gnu_extensions_p (parser))
19735 attributes = cp_parser_gnu_attributes_opt (parser);
19736 if (type != error_mark_node)
19737 type = finish_struct (type, attributes);
19738 if (nested_name_specifier_p)
19739 pop_inner_scope (old_scope, scope);
19741 /* We've finished a type definition. Check for the common syntax
19742 error of forgetting a semicolon after the definition. We need to
19743 be careful, as we can't just check for not-a-semicolon and be done
19744 with it; the user might have typed:
19746 class X { } c = ...;
19747 class X { } *p = ...;
19749 and so forth. Instead, enumerate all the possible tokens that
19750 might follow this production; if we don't see one of them, then
19751 complain and silently insert the semicolon. */
19753 cp_token *token = cp_lexer_peek_token (parser->lexer);
19754 bool want_semicolon = true;
19756 if (cp_next_tokens_can_be_std_attribute_p (parser))
19757 /* Don't try to parse c++11 attributes here. As per the
19758 grammar, that should be a task for
19759 cp_parser_decl_specifier_seq. */
19760 want_semicolon = false;
19762 switch (token->type)
19764 case CPP_NAME:
19765 case CPP_SEMICOLON:
19766 case CPP_MULT:
19767 case CPP_AND:
19768 case CPP_OPEN_PAREN:
19769 case CPP_CLOSE_PAREN:
19770 case CPP_COMMA:
19771 want_semicolon = false;
19772 break;
19774 /* While it's legal for type qualifiers and storage class
19775 specifiers to follow type definitions in the grammar, only
19776 compiler testsuites contain code like that. Assume that if
19777 we see such code, then what we're really seeing is a case
19778 like:
19780 class X { }
19781 const <type> var = ...;
19785 class Y { }
19786 static <type> func (...) ...
19788 i.e. the qualifier or specifier applies to the next
19789 declaration. To do so, however, we need to look ahead one
19790 more token to see if *that* token is a type specifier.
19792 This code could be improved to handle:
19794 class Z { }
19795 static const <type> var = ...; */
19796 case CPP_KEYWORD:
19797 if (keyword_is_decl_specifier (token->keyword))
19799 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19801 /* Handling user-defined types here would be nice, but very
19802 tricky. */
19803 want_semicolon
19804 = (lookahead->type == CPP_KEYWORD
19805 && keyword_begins_type_specifier (lookahead->keyword));
19807 break;
19808 default:
19809 break;
19812 /* If we don't have a type, then something is very wrong and we
19813 shouldn't try to do anything clever. Likewise for not seeing the
19814 closing brace. */
19815 if (closing_brace && TYPE_P (type) && want_semicolon)
19817 cp_token_position prev
19818 = cp_lexer_previous_token_position (parser->lexer);
19819 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19820 location_t loc = prev_token->location;
19822 if (CLASSTYPE_DECLARED_CLASS (type))
19823 error_at (loc, "expected %<;%> after class definition");
19824 else if (TREE_CODE (type) == RECORD_TYPE)
19825 error_at (loc, "expected %<;%> after struct definition");
19826 else if (TREE_CODE (type) == UNION_TYPE)
19827 error_at (loc, "expected %<;%> after union definition");
19828 else
19829 gcc_unreachable ();
19831 /* Unget one token and smash it to look as though we encountered
19832 a semicolon in the input stream. */
19833 cp_lexer_set_token_position (parser->lexer, prev);
19834 token = cp_lexer_peek_token (parser->lexer);
19835 token->type = CPP_SEMICOLON;
19836 token->keyword = RID_MAX;
19840 /* If this class is not itself within the scope of another class,
19841 then we need to parse the bodies of all of the queued function
19842 definitions. Note that the queued functions defined in a class
19843 are not always processed immediately following the
19844 class-specifier for that class. Consider:
19846 struct A {
19847 struct B { void f() { sizeof (A); } };
19850 If `f' were processed before the processing of `A' were
19851 completed, there would be no way to compute the size of `A'.
19852 Note that the nesting we are interested in here is lexical --
19853 not the semantic nesting given by TYPE_CONTEXT. In particular,
19854 for:
19856 struct A { struct B; };
19857 struct A::B { void f() { } };
19859 there is no need to delay the parsing of `A::B::f'. */
19860 if (--parser->num_classes_being_defined == 0)
19862 tree decl;
19863 tree class_type = NULL_TREE;
19864 tree pushed_scope = NULL_TREE;
19865 unsigned ix;
19866 cp_default_arg_entry *e;
19867 tree save_ccp, save_ccr;
19869 /* In a first pass, parse default arguments to the functions.
19870 Then, in a second pass, parse the bodies of the functions.
19871 This two-phased approach handles cases like:
19873 struct S {
19874 void f() { g(); }
19875 void g(int i = 3);
19879 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19881 decl = e->decl;
19882 /* If there are default arguments that have not yet been processed,
19883 take care of them now. */
19884 if (class_type != e->class_type)
19886 if (pushed_scope)
19887 pop_scope (pushed_scope);
19888 class_type = e->class_type;
19889 pushed_scope = push_scope (class_type);
19891 /* Make sure that any template parameters are in scope. */
19892 maybe_begin_member_template_processing (decl);
19893 /* Parse the default argument expressions. */
19894 cp_parser_late_parsing_default_args (parser, decl);
19895 /* Remove any template parameters from the symbol table. */
19896 maybe_end_member_template_processing ();
19898 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19899 /* Now parse any NSDMIs. */
19900 save_ccp = current_class_ptr;
19901 save_ccr = current_class_ref;
19902 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19904 if (class_type != DECL_CONTEXT (decl))
19906 if (pushed_scope)
19907 pop_scope (pushed_scope);
19908 class_type = DECL_CONTEXT (decl);
19909 pushed_scope = push_scope (class_type);
19911 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19912 cp_parser_late_parsing_nsdmi (parser, decl);
19914 vec_safe_truncate (unparsed_nsdmis, 0);
19915 current_class_ptr = save_ccp;
19916 current_class_ref = save_ccr;
19917 if (pushed_scope)
19918 pop_scope (pushed_scope);
19920 /* Now do some post-NSDMI bookkeeping. */
19921 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19922 after_nsdmi_defaulted_late_checks (class_type);
19923 vec_safe_truncate (unparsed_classes, 0);
19924 after_nsdmi_defaulted_late_checks (type);
19926 /* Now parse the body of the functions. */
19927 if (flag_openmp)
19929 /* OpenMP UDRs need to be parsed before all other functions. */
19930 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19931 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19932 cp_parser_late_parsing_for_member (parser, decl);
19933 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19934 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19935 cp_parser_late_parsing_for_member (parser, decl);
19937 else
19938 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19939 cp_parser_late_parsing_for_member (parser, decl);
19940 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19942 else
19943 vec_safe_push (unparsed_classes, type);
19945 /* Put back any saved access checks. */
19946 pop_deferring_access_checks ();
19948 /* Restore saved state. */
19949 parser->in_switch_statement_p = in_switch_statement_p;
19950 parser->in_statement = in_statement;
19951 parser->in_function_body = saved_in_function_body;
19952 parser->num_template_parameter_lists
19953 = saved_num_template_parameter_lists;
19954 parser->in_unbraced_linkage_specification_p
19955 = saved_in_unbraced_linkage_specification_p;
19957 return type;
19960 static tree
19961 cp_parser_class_specifier (cp_parser* parser)
19963 tree ret;
19964 timevar_push (TV_PARSE_STRUCT);
19965 ret = cp_parser_class_specifier_1 (parser);
19966 timevar_pop (TV_PARSE_STRUCT);
19967 return ret;
19970 /* Parse a class-head.
19972 class-head:
19973 class-key identifier [opt] base-clause [opt]
19974 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19975 class-key nested-name-specifier [opt] template-id
19976 base-clause [opt]
19978 class-virt-specifier:
19979 final
19981 GNU Extensions:
19982 class-key attributes identifier [opt] base-clause [opt]
19983 class-key attributes nested-name-specifier identifier base-clause [opt]
19984 class-key attributes nested-name-specifier [opt] template-id
19985 base-clause [opt]
19987 Upon return BASES is initialized to the list of base classes (or
19988 NULL, if there are none) in the same form returned by
19989 cp_parser_base_clause.
19991 Returns the TYPE of the indicated class. Sets
19992 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19993 involving a nested-name-specifier was used, and FALSE otherwise.
19995 Returns error_mark_node if this is not a class-head.
19997 Returns NULL_TREE if the class-head is syntactically valid, but
19998 semantically invalid in a way that means we should skip the entire
19999 body of the class. */
20001 static tree
20002 cp_parser_class_head (cp_parser* parser,
20003 bool* nested_name_specifier_p)
20005 tree nested_name_specifier;
20006 enum tag_types class_key;
20007 tree id = NULL_TREE;
20008 tree type = NULL_TREE;
20009 tree attributes;
20010 tree bases;
20011 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20012 bool template_id_p = false;
20013 bool qualified_p = false;
20014 bool invalid_nested_name_p = false;
20015 bool invalid_explicit_specialization_p = false;
20016 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20017 tree pushed_scope = NULL_TREE;
20018 unsigned num_templates;
20019 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20020 /* Assume no nested-name-specifier will be present. */
20021 *nested_name_specifier_p = false;
20022 /* Assume no template parameter lists will be used in defining the
20023 type. */
20024 num_templates = 0;
20025 parser->colon_corrects_to_scope_p = false;
20027 /* Look for the class-key. */
20028 class_key = cp_parser_class_key (parser);
20029 if (class_key == none_type)
20030 return error_mark_node;
20032 /* Parse the attributes. */
20033 attributes = cp_parser_attributes_opt (parser);
20035 /* If the next token is `::', that is invalid -- but sometimes
20036 people do try to write:
20038 struct ::S {};
20040 Handle this gracefully by accepting the extra qualifier, and then
20041 issuing an error about it later if this really is a
20042 class-head. If it turns out just to be an elaborated type
20043 specifier, remain silent. */
20044 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20045 qualified_p = true;
20047 push_deferring_access_checks (dk_no_check);
20049 /* Determine the name of the class. Begin by looking for an
20050 optional nested-name-specifier. */
20051 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20052 nested_name_specifier
20053 = cp_parser_nested_name_specifier_opt (parser,
20054 /*typename_keyword_p=*/false,
20055 /*check_dependency_p=*/false,
20056 /*type_p=*/true,
20057 /*is_declaration=*/false);
20058 /* If there was a nested-name-specifier, then there *must* be an
20059 identifier. */
20060 if (nested_name_specifier)
20062 type_start_token = cp_lexer_peek_token (parser->lexer);
20063 /* Although the grammar says `identifier', it really means
20064 `class-name' or `template-name'. You are only allowed to
20065 define a class that has already been declared with this
20066 syntax.
20068 The proposed resolution for Core Issue 180 says that wherever
20069 you see `class T::X' you should treat `X' as a type-name.
20071 It is OK to define an inaccessible class; for example:
20073 class A { class B; };
20074 class A::B {};
20076 We do not know if we will see a class-name, or a
20077 template-name. We look for a class-name first, in case the
20078 class-name is a template-id; if we looked for the
20079 template-name first we would stop after the template-name. */
20080 cp_parser_parse_tentatively (parser);
20081 type = cp_parser_class_name (parser,
20082 /*typename_keyword_p=*/false,
20083 /*template_keyword_p=*/false,
20084 class_type,
20085 /*check_dependency_p=*/false,
20086 /*class_head_p=*/true,
20087 /*is_declaration=*/false);
20088 /* If that didn't work, ignore the nested-name-specifier. */
20089 if (!cp_parser_parse_definitely (parser))
20091 invalid_nested_name_p = true;
20092 type_start_token = cp_lexer_peek_token (parser->lexer);
20093 id = cp_parser_identifier (parser);
20094 if (id == error_mark_node)
20095 id = NULL_TREE;
20097 /* If we could not find a corresponding TYPE, treat this
20098 declaration like an unqualified declaration. */
20099 if (type == error_mark_node)
20100 nested_name_specifier = NULL_TREE;
20101 /* Otherwise, count the number of templates used in TYPE and its
20102 containing scopes. */
20103 else
20105 tree scope;
20107 for (scope = TREE_TYPE (type);
20108 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20109 scope = get_containing_scope (scope))
20110 if (TYPE_P (scope)
20111 && CLASS_TYPE_P (scope)
20112 && CLASSTYPE_TEMPLATE_INFO (scope)
20113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20114 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20115 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20116 ++num_templates;
20119 /* Otherwise, the identifier is optional. */
20120 else
20122 /* We don't know whether what comes next is a template-id,
20123 an identifier, or nothing at all. */
20124 cp_parser_parse_tentatively (parser);
20125 /* Check for a template-id. */
20126 type_start_token = cp_lexer_peek_token (parser->lexer);
20127 id = cp_parser_template_id (parser,
20128 /*template_keyword_p=*/false,
20129 /*check_dependency_p=*/true,
20130 class_key,
20131 /*is_declaration=*/true);
20132 /* If that didn't work, it could still be an identifier. */
20133 if (!cp_parser_parse_definitely (parser))
20135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20137 type_start_token = cp_lexer_peek_token (parser->lexer);
20138 id = cp_parser_identifier (parser);
20140 else
20141 id = NULL_TREE;
20143 else
20145 template_id_p = true;
20146 ++num_templates;
20150 pop_deferring_access_checks ();
20152 if (id)
20154 cp_parser_check_for_invalid_template_id (parser, id,
20155 class_key,
20156 type_start_token->location);
20158 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20160 /* If it's not a `:' or a `{' then we can't really be looking at a
20161 class-head, since a class-head only appears as part of a
20162 class-specifier. We have to detect this situation before calling
20163 xref_tag, since that has irreversible side-effects. */
20164 if (!cp_parser_next_token_starts_class_definition_p (parser))
20166 cp_parser_error (parser, "expected %<{%> or %<:%>");
20167 type = error_mark_node;
20168 goto out;
20171 /* At this point, we're going ahead with the class-specifier, even
20172 if some other problem occurs. */
20173 cp_parser_commit_to_tentative_parse (parser);
20174 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20176 cp_parser_error (parser,
20177 "cannot specify %<override%> for a class");
20178 type = error_mark_node;
20179 goto out;
20181 /* Issue the error about the overly-qualified name now. */
20182 if (qualified_p)
20184 cp_parser_error (parser,
20185 "global qualification of class name is invalid");
20186 type = error_mark_node;
20187 goto out;
20189 else if (invalid_nested_name_p)
20191 cp_parser_error (parser,
20192 "qualified name does not name a class");
20193 type = error_mark_node;
20194 goto out;
20196 else if (nested_name_specifier)
20198 tree scope;
20200 /* Reject typedef-names in class heads. */
20201 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20203 error_at (type_start_token->location,
20204 "invalid class name in declaration of %qD",
20205 type);
20206 type = NULL_TREE;
20207 goto done;
20210 /* Figure out in what scope the declaration is being placed. */
20211 scope = current_scope ();
20212 /* If that scope does not contain the scope in which the
20213 class was originally declared, the program is invalid. */
20214 if (scope && !is_ancestor (scope, nested_name_specifier))
20216 if (at_namespace_scope_p ())
20217 error_at (type_start_token->location,
20218 "declaration of %qD in namespace %qD which does not "
20219 "enclose %qD",
20220 type, scope, nested_name_specifier);
20221 else
20222 error_at (type_start_token->location,
20223 "declaration of %qD in %qD which does not enclose %qD",
20224 type, scope, nested_name_specifier);
20225 type = NULL_TREE;
20226 goto done;
20228 /* [dcl.meaning]
20230 A declarator-id shall not be qualified except for the
20231 definition of a ... nested class outside of its class
20232 ... [or] the definition or explicit instantiation of a
20233 class member of a namespace outside of its namespace. */
20234 if (scope == nested_name_specifier)
20236 permerror (nested_name_specifier_token_start->location,
20237 "extra qualification not allowed");
20238 nested_name_specifier = NULL_TREE;
20239 num_templates = 0;
20242 /* An explicit-specialization must be preceded by "template <>". If
20243 it is not, try to recover gracefully. */
20244 if (at_namespace_scope_p ()
20245 && parser->num_template_parameter_lists == 0
20246 && template_id_p)
20248 error_at (type_start_token->location,
20249 "an explicit specialization must be preceded by %<template <>%>");
20250 invalid_explicit_specialization_p = true;
20251 /* Take the same action that would have been taken by
20252 cp_parser_explicit_specialization. */
20253 ++parser->num_template_parameter_lists;
20254 begin_specialization ();
20256 /* There must be no "return" statements between this point and the
20257 end of this function; set "type "to the correct return value and
20258 use "goto done;" to return. */
20259 /* Make sure that the right number of template parameters were
20260 present. */
20261 if (!cp_parser_check_template_parameters (parser, num_templates,
20262 type_start_token->location,
20263 /*declarator=*/NULL))
20265 /* If something went wrong, there is no point in even trying to
20266 process the class-definition. */
20267 type = NULL_TREE;
20268 goto done;
20271 /* Look up the type. */
20272 if (template_id_p)
20274 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20275 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20276 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20278 error_at (type_start_token->location,
20279 "function template %qD redeclared as a class template", id);
20280 type = error_mark_node;
20282 else
20284 type = TREE_TYPE (id);
20285 type = maybe_process_partial_specialization (type);
20287 if (nested_name_specifier)
20288 pushed_scope = push_scope (nested_name_specifier);
20290 else if (nested_name_specifier)
20292 tree class_type;
20294 /* Given:
20296 template <typename T> struct S { struct T };
20297 template <typename T> struct S<T>::T { };
20299 we will get a TYPENAME_TYPE when processing the definition of
20300 `S::T'. We need to resolve it to the actual type before we
20301 try to define it. */
20302 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20304 class_type = resolve_typename_type (TREE_TYPE (type),
20305 /*only_current_p=*/false);
20306 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20307 type = TYPE_NAME (class_type);
20308 else
20310 cp_parser_error (parser, "could not resolve typename type");
20311 type = error_mark_node;
20315 if (maybe_process_partial_specialization (TREE_TYPE (type))
20316 == error_mark_node)
20318 type = NULL_TREE;
20319 goto done;
20322 class_type = current_class_type;
20323 /* Enter the scope indicated by the nested-name-specifier. */
20324 pushed_scope = push_scope (nested_name_specifier);
20325 /* Get the canonical version of this type. */
20326 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20327 /* Call push_template_decl if it seems like we should be defining a
20328 template either from the template headers or the type we're
20329 defining, so that we diagnose both extra and missing headers. */
20330 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20331 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20332 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20334 type = push_template_decl (type);
20335 if (type == error_mark_node)
20337 type = NULL_TREE;
20338 goto done;
20342 type = TREE_TYPE (type);
20343 *nested_name_specifier_p = true;
20345 else /* The name is not a nested name. */
20347 /* If the class was unnamed, create a dummy name. */
20348 if (!id)
20349 id = make_anon_name ();
20350 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20351 parser->num_template_parameter_lists);
20354 /* Indicate whether this class was declared as a `class' or as a
20355 `struct'. */
20356 if (TREE_CODE (type) == RECORD_TYPE)
20357 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20358 cp_parser_check_class_key (class_key, type);
20360 /* If this type was already complete, and we see another definition,
20361 that's an error. */
20362 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20364 error_at (type_start_token->location, "redefinition of %q#T",
20365 type);
20366 error_at (type_start_token->location, "previous definition of %q+#T",
20367 type);
20368 type = NULL_TREE;
20369 goto done;
20371 else if (type == error_mark_node)
20372 type = NULL_TREE;
20374 if (type)
20376 /* Apply attributes now, before any use of the class as a template
20377 argument in its base list. */
20378 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20379 fixup_attribute_variants (type);
20382 /* We will have entered the scope containing the class; the names of
20383 base classes should be looked up in that context. For example:
20385 struct A { struct B {}; struct C; };
20386 struct A::C : B {};
20388 is valid. */
20390 /* Get the list of base-classes, if there is one. */
20391 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20393 /* PR59482: enter the class scope so that base-specifiers are looked
20394 up correctly. */
20395 if (type)
20396 pushclass (type);
20397 bases = cp_parser_base_clause (parser);
20398 /* PR59482: get out of the previously pushed class scope so that the
20399 subsequent pops pop the right thing. */
20400 if (type)
20401 popclass ();
20403 else
20404 bases = NULL_TREE;
20406 /* If we're really defining a class, process the base classes.
20407 If they're invalid, fail. */
20408 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20409 && !xref_basetypes (type, bases))
20410 type = NULL_TREE;
20412 done:
20413 /* Leave the scope given by the nested-name-specifier. We will
20414 enter the class scope itself while processing the members. */
20415 if (pushed_scope)
20416 pop_scope (pushed_scope);
20418 if (invalid_explicit_specialization_p)
20420 end_specialization ();
20421 --parser->num_template_parameter_lists;
20424 if (type)
20425 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20426 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20427 CLASSTYPE_FINAL (type) = 1;
20428 out:
20429 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20430 return type;
20433 /* Parse a class-key.
20435 class-key:
20436 class
20437 struct
20438 union
20440 Returns the kind of class-key specified, or none_type to indicate
20441 error. */
20443 static enum tag_types
20444 cp_parser_class_key (cp_parser* parser)
20446 cp_token *token;
20447 enum tag_types tag_type;
20449 /* Look for the class-key. */
20450 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20451 if (!token)
20452 return none_type;
20454 /* Check to see if the TOKEN is a class-key. */
20455 tag_type = cp_parser_token_is_class_key (token);
20456 if (!tag_type)
20457 cp_parser_error (parser, "expected class-key");
20458 return tag_type;
20461 /* Parse a type-parameter-key.
20463 type-parameter-key:
20464 class
20465 typename
20468 static void
20469 cp_parser_type_parameter_key (cp_parser* parser)
20471 /* Look for the type-parameter-key. */
20472 enum tag_types tag_type = none_type;
20473 cp_token *token = cp_lexer_peek_token (parser->lexer);
20474 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20476 cp_lexer_consume_token (parser->lexer);
20477 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20478 /* typename is not allowed in a template template parameter
20479 by the standard until C++1Z. */
20480 pedwarn (token->location, OPT_Wpedantic,
20481 "ISO C++ forbids typename key in template template parameter;"
20482 " use -std=c++1z or -std=gnu++1z");
20484 else
20485 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20487 return;
20490 /* Parse an (optional) member-specification.
20492 member-specification:
20493 member-declaration member-specification [opt]
20494 access-specifier : member-specification [opt] */
20496 static void
20497 cp_parser_member_specification_opt (cp_parser* parser)
20499 while (true)
20501 cp_token *token;
20502 enum rid keyword;
20504 /* Peek at the next token. */
20505 token = cp_lexer_peek_token (parser->lexer);
20506 /* If it's a `}', or EOF then we've seen all the members. */
20507 if (token->type == CPP_CLOSE_BRACE
20508 || token->type == CPP_EOF
20509 || token->type == CPP_PRAGMA_EOL)
20510 break;
20512 /* See if this token is a keyword. */
20513 keyword = token->keyword;
20514 switch (keyword)
20516 case RID_PUBLIC:
20517 case RID_PROTECTED:
20518 case RID_PRIVATE:
20519 /* Consume the access-specifier. */
20520 cp_lexer_consume_token (parser->lexer);
20521 /* Remember which access-specifier is active. */
20522 current_access_specifier = token->u.value;
20523 /* Look for the `:'. */
20524 cp_parser_require (parser, CPP_COLON, RT_COLON);
20525 break;
20527 default:
20528 /* Accept #pragmas at class scope. */
20529 if (token->type == CPP_PRAGMA)
20531 cp_parser_pragma (parser, pragma_member);
20532 break;
20535 /* Otherwise, the next construction must be a
20536 member-declaration. */
20537 cp_parser_member_declaration (parser);
20542 /* Parse a member-declaration.
20544 member-declaration:
20545 decl-specifier-seq [opt] member-declarator-list [opt] ;
20546 function-definition ; [opt]
20547 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20548 using-declaration
20549 template-declaration
20550 alias-declaration
20552 member-declarator-list:
20553 member-declarator
20554 member-declarator-list , member-declarator
20556 member-declarator:
20557 declarator pure-specifier [opt]
20558 declarator constant-initializer [opt]
20559 identifier [opt] : constant-expression
20561 GNU Extensions:
20563 member-declaration:
20564 __extension__ member-declaration
20566 member-declarator:
20567 declarator attributes [opt] pure-specifier [opt]
20568 declarator attributes [opt] constant-initializer [opt]
20569 identifier [opt] attributes [opt] : constant-expression
20571 C++0x Extensions:
20573 member-declaration:
20574 static_assert-declaration */
20576 static void
20577 cp_parser_member_declaration (cp_parser* parser)
20579 cp_decl_specifier_seq decl_specifiers;
20580 tree prefix_attributes;
20581 tree decl;
20582 int declares_class_or_enum;
20583 bool friend_p;
20584 cp_token *token = NULL;
20585 cp_token *decl_spec_token_start = NULL;
20586 cp_token *initializer_token_start = NULL;
20587 int saved_pedantic;
20588 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20590 /* Check for the `__extension__' keyword. */
20591 if (cp_parser_extension_opt (parser, &saved_pedantic))
20593 /* Recurse. */
20594 cp_parser_member_declaration (parser);
20595 /* Restore the old value of the PEDANTIC flag. */
20596 pedantic = saved_pedantic;
20598 return;
20601 /* Check for a template-declaration. */
20602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20604 /* An explicit specialization here is an error condition, and we
20605 expect the specialization handler to detect and report this. */
20606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20608 cp_parser_explicit_specialization (parser);
20609 else
20610 cp_parser_template_declaration (parser, /*member_p=*/true);
20612 return;
20615 /* Check for a using-declaration. */
20616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20618 if (cxx_dialect < cxx11)
20620 /* Parse the using-declaration. */
20621 cp_parser_using_declaration (parser,
20622 /*access_declaration_p=*/false);
20623 return;
20625 else
20627 tree decl;
20628 bool alias_decl_expected;
20629 cp_parser_parse_tentatively (parser);
20630 decl = cp_parser_alias_declaration (parser);
20631 /* Note that if we actually see the '=' token after the
20632 identifier, cp_parser_alias_declaration commits the
20633 tentative parse. In that case, we really expects an
20634 alias-declaration. Otherwise, we expect a using
20635 declaration. */
20636 alias_decl_expected =
20637 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20638 cp_parser_parse_definitely (parser);
20640 if (alias_decl_expected)
20641 finish_member_declaration (decl);
20642 else
20643 cp_parser_using_declaration (parser,
20644 /*access_declaration_p=*/false);
20645 return;
20649 /* Check for @defs. */
20650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20652 tree ivar, member;
20653 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20654 ivar = ivar_chains;
20655 while (ivar)
20657 member = ivar;
20658 ivar = TREE_CHAIN (member);
20659 TREE_CHAIN (member) = NULL_TREE;
20660 finish_member_declaration (member);
20662 return;
20665 /* If the next token is `static_assert' we have a static assertion. */
20666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20668 cp_parser_static_assert (parser, /*member_p=*/true);
20669 return;
20672 parser->colon_corrects_to_scope_p = false;
20674 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20675 goto out;
20677 /* Parse the decl-specifier-seq. */
20678 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20679 cp_parser_decl_specifier_seq (parser,
20680 CP_PARSER_FLAGS_OPTIONAL,
20681 &decl_specifiers,
20682 &declares_class_or_enum);
20683 /* Check for an invalid type-name. */
20684 if (!decl_specifiers.any_type_specifiers_p
20685 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20686 goto out;
20687 /* If there is no declarator, then the decl-specifier-seq should
20688 specify a type. */
20689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20691 /* If there was no decl-specifier-seq, and the next token is a
20692 `;', then we have something like:
20694 struct S { ; };
20696 [class.mem]
20698 Each member-declaration shall declare at least one member
20699 name of the class. */
20700 if (!decl_specifiers.any_specifiers_p)
20702 cp_token *token = cp_lexer_peek_token (parser->lexer);
20703 if (!in_system_header_at (token->location))
20704 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20706 else
20708 tree type;
20710 /* See if this declaration is a friend. */
20711 friend_p = cp_parser_friend_p (&decl_specifiers);
20712 /* If there were decl-specifiers, check to see if there was
20713 a class-declaration. */
20714 type = check_tag_decl (&decl_specifiers,
20715 /*explicit_type_instantiation_p=*/false);
20716 /* Nested classes have already been added to the class, but
20717 a `friend' needs to be explicitly registered. */
20718 if (friend_p)
20720 /* If the `friend' keyword was present, the friend must
20721 be introduced with a class-key. */
20722 if (!declares_class_or_enum && cxx_dialect < cxx11)
20723 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20724 "in C++03 a class-key must be used "
20725 "when declaring a friend");
20726 /* In this case:
20728 template <typename T> struct A {
20729 friend struct A<T>::B;
20732 A<T>::B will be represented by a TYPENAME_TYPE, and
20733 therefore not recognized by check_tag_decl. */
20734 if (!type)
20736 type = decl_specifiers.type;
20737 if (type && TREE_CODE (type) == TYPE_DECL)
20738 type = TREE_TYPE (type);
20740 if (!type || !TYPE_P (type))
20741 error_at (decl_spec_token_start->location,
20742 "friend declaration does not name a class or "
20743 "function");
20744 else
20745 make_friend_class (current_class_type, type,
20746 /*complain=*/true);
20748 /* If there is no TYPE, an error message will already have
20749 been issued. */
20750 else if (!type || type == error_mark_node)
20752 /* An anonymous aggregate has to be handled specially; such
20753 a declaration really declares a data member (with a
20754 particular type), as opposed to a nested class. */
20755 else if (ANON_AGGR_TYPE_P (type))
20757 /* C++11 9.5/6. */
20758 if (decl_specifiers.storage_class != sc_none)
20759 error_at (decl_spec_token_start->location,
20760 "a storage class on an anonymous aggregate "
20761 "in class scope is not allowed");
20763 /* Remove constructors and such from TYPE, now that we
20764 know it is an anonymous aggregate. */
20765 fixup_anonymous_aggr (type);
20766 /* And make the corresponding data member. */
20767 decl = build_decl (decl_spec_token_start->location,
20768 FIELD_DECL, NULL_TREE, type);
20769 /* Add it to the class. */
20770 finish_member_declaration (decl);
20772 else
20773 cp_parser_check_access_in_redeclaration
20774 (TYPE_NAME (type),
20775 decl_spec_token_start->location);
20778 else
20780 bool assume_semicolon = false;
20782 /* Clear attributes from the decl_specifiers but keep them
20783 around as prefix attributes that apply them to the entity
20784 being declared. */
20785 prefix_attributes = decl_specifiers.attributes;
20786 decl_specifiers.attributes = NULL_TREE;
20788 /* See if these declarations will be friends. */
20789 friend_p = cp_parser_friend_p (&decl_specifiers);
20791 /* Keep going until we hit the `;' at the end of the
20792 declaration. */
20793 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20795 tree attributes = NULL_TREE;
20796 tree first_attribute;
20798 /* Peek at the next token. */
20799 token = cp_lexer_peek_token (parser->lexer);
20801 /* Check for a bitfield declaration. */
20802 if (token->type == CPP_COLON
20803 || (token->type == CPP_NAME
20804 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20805 == CPP_COLON))
20807 tree identifier;
20808 tree width;
20810 /* Get the name of the bitfield. Note that we cannot just
20811 check TOKEN here because it may have been invalidated by
20812 the call to cp_lexer_peek_nth_token above. */
20813 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20814 identifier = cp_parser_identifier (parser);
20815 else
20816 identifier = NULL_TREE;
20818 /* Consume the `:' token. */
20819 cp_lexer_consume_token (parser->lexer);
20820 /* Get the width of the bitfield. */
20821 width
20822 = cp_parser_constant_expression (parser);
20824 /* Look for attributes that apply to the bitfield. */
20825 attributes = cp_parser_attributes_opt (parser);
20826 /* Remember which attributes are prefix attributes and
20827 which are not. */
20828 first_attribute = attributes;
20829 /* Combine the attributes. */
20830 attributes = chainon (prefix_attributes, attributes);
20832 /* Create the bitfield declaration. */
20833 decl = grokbitfield (identifier
20834 ? make_id_declarator (NULL_TREE,
20835 identifier,
20836 sfk_none)
20837 : NULL,
20838 &decl_specifiers,
20839 width,
20840 attributes);
20842 else
20844 cp_declarator *declarator;
20845 tree initializer;
20846 tree asm_specification;
20847 int ctor_dtor_or_conv_p;
20849 /* Parse the declarator. */
20850 declarator
20851 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20852 &ctor_dtor_or_conv_p,
20853 /*parenthesized_p=*/NULL,
20854 /*member_p=*/true,
20855 friend_p);
20857 /* If something went wrong parsing the declarator, make sure
20858 that we at least consume some tokens. */
20859 if (declarator == cp_error_declarator)
20861 /* Skip to the end of the statement. */
20862 cp_parser_skip_to_end_of_statement (parser);
20863 /* If the next token is not a semicolon, that is
20864 probably because we just skipped over the body of
20865 a function. So, we consume a semicolon if
20866 present, but do not issue an error message if it
20867 is not present. */
20868 if (cp_lexer_next_token_is (parser->lexer,
20869 CPP_SEMICOLON))
20870 cp_lexer_consume_token (parser->lexer);
20871 goto out;
20874 if (declares_class_or_enum & 2)
20875 cp_parser_check_for_definition_in_return_type
20876 (declarator, decl_specifiers.type,
20877 decl_specifiers.locations[ds_type_spec]);
20879 /* Look for an asm-specification. */
20880 asm_specification = cp_parser_asm_specification_opt (parser);
20881 /* Look for attributes that apply to the declaration. */
20882 attributes = cp_parser_attributes_opt (parser);
20883 /* Remember which attributes are prefix attributes and
20884 which are not. */
20885 first_attribute = attributes;
20886 /* Combine the attributes. */
20887 attributes = chainon (prefix_attributes, attributes);
20889 /* If it's an `=', then we have a constant-initializer or a
20890 pure-specifier. It is not correct to parse the
20891 initializer before registering the member declaration
20892 since the member declaration should be in scope while
20893 its initializer is processed. However, the rest of the
20894 front end does not yet provide an interface that allows
20895 us to handle this correctly. */
20896 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20898 /* In [class.mem]:
20900 A pure-specifier shall be used only in the declaration of
20901 a virtual function.
20903 A member-declarator can contain a constant-initializer
20904 only if it declares a static member of integral or
20905 enumeration type.
20907 Therefore, if the DECLARATOR is for a function, we look
20908 for a pure-specifier; otherwise, we look for a
20909 constant-initializer. When we call `grokfield', it will
20910 perform more stringent semantics checks. */
20911 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20912 if (function_declarator_p (declarator)
20913 || (decl_specifiers.type
20914 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20915 && declarator->kind == cdk_id
20916 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20917 == FUNCTION_TYPE)))
20918 initializer = cp_parser_pure_specifier (parser);
20919 else if (decl_specifiers.storage_class != sc_static)
20920 initializer = cp_parser_save_nsdmi (parser);
20921 else if (cxx_dialect >= cxx11)
20923 bool nonconst;
20924 /* Don't require a constant rvalue in C++11, since we
20925 might want a reference constant. We'll enforce
20926 constancy later. */
20927 cp_lexer_consume_token (parser->lexer);
20928 /* Parse the initializer. */
20929 initializer = cp_parser_initializer_clause (parser,
20930 &nonconst);
20932 else
20933 /* Parse the initializer. */
20934 initializer = cp_parser_constant_initializer (parser);
20936 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20937 && !function_declarator_p (declarator))
20939 bool x;
20940 if (decl_specifiers.storage_class != sc_static)
20941 initializer = cp_parser_save_nsdmi (parser);
20942 else
20943 initializer = cp_parser_initializer (parser, &x, &x);
20945 /* Otherwise, there is no initializer. */
20946 else
20947 initializer = NULL_TREE;
20949 /* See if we are probably looking at a function
20950 definition. We are certainly not looking at a
20951 member-declarator. Calling `grokfield' has
20952 side-effects, so we must not do it unless we are sure
20953 that we are looking at a member-declarator. */
20954 if (cp_parser_token_starts_function_definition_p
20955 (cp_lexer_peek_token (parser->lexer)))
20957 /* The grammar does not allow a pure-specifier to be
20958 used when a member function is defined. (It is
20959 possible that this fact is an oversight in the
20960 standard, since a pure function may be defined
20961 outside of the class-specifier. */
20962 if (initializer && initializer_token_start)
20963 error_at (initializer_token_start->location,
20964 "pure-specifier on function-definition");
20965 decl = cp_parser_save_member_function_body (parser,
20966 &decl_specifiers,
20967 declarator,
20968 attributes);
20969 if (parser->fully_implicit_function_template_p)
20970 decl = finish_fully_implicit_template (parser, decl);
20971 /* If the member was not a friend, declare it here. */
20972 if (!friend_p)
20973 finish_member_declaration (decl);
20974 /* Peek at the next token. */
20975 token = cp_lexer_peek_token (parser->lexer);
20976 /* If the next token is a semicolon, consume it. */
20977 if (token->type == CPP_SEMICOLON)
20978 cp_lexer_consume_token (parser->lexer);
20979 goto out;
20981 else
20982 if (declarator->kind == cdk_function)
20983 declarator->id_loc = token->location;
20984 /* Create the declaration. */
20985 decl = grokfield (declarator, &decl_specifiers,
20986 initializer, /*init_const_expr_p=*/true,
20987 asm_specification, attributes);
20988 if (parser->fully_implicit_function_template_p)
20990 if (friend_p)
20991 finish_fully_implicit_template (parser, 0);
20992 else
20993 decl = finish_fully_implicit_template (parser, decl);
20997 cp_finalize_omp_declare_simd (parser, decl);
20999 /* Reset PREFIX_ATTRIBUTES. */
21000 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21001 attributes = TREE_CHAIN (attributes);
21002 if (attributes)
21003 TREE_CHAIN (attributes) = NULL_TREE;
21005 /* If there is any qualification still in effect, clear it
21006 now; we will be starting fresh with the next declarator. */
21007 parser->scope = NULL_TREE;
21008 parser->qualifying_scope = NULL_TREE;
21009 parser->object_scope = NULL_TREE;
21010 /* If it's a `,', then there are more declarators. */
21011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21013 cp_lexer_consume_token (parser->lexer);
21014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21016 cp_token *token = cp_lexer_previous_token (parser->lexer);
21017 error_at (token->location,
21018 "stray %<,%> at end of member declaration");
21021 /* If the next token isn't a `;', then we have a parse error. */
21022 else if (cp_lexer_next_token_is_not (parser->lexer,
21023 CPP_SEMICOLON))
21025 /* The next token might be a ways away from where the
21026 actual semicolon is missing. Find the previous token
21027 and use that for our error position. */
21028 cp_token *token = cp_lexer_previous_token (parser->lexer);
21029 error_at (token->location,
21030 "expected %<;%> at end of member declaration");
21032 /* Assume that the user meant to provide a semicolon. If
21033 we were to cp_parser_skip_to_end_of_statement, we might
21034 skip to a semicolon inside a member function definition
21035 and issue nonsensical error messages. */
21036 assume_semicolon = true;
21039 if (decl)
21041 /* Add DECL to the list of members. */
21042 if (!friend_p)
21043 finish_member_declaration (decl);
21045 if (TREE_CODE (decl) == FUNCTION_DECL)
21046 cp_parser_save_default_args (parser, decl);
21047 else if (TREE_CODE (decl) == FIELD_DECL
21048 && !DECL_C_BIT_FIELD (decl)
21049 && DECL_INITIAL (decl))
21050 /* Add DECL to the queue of NSDMI to be parsed later. */
21051 vec_safe_push (unparsed_nsdmis, decl);
21054 if (assume_semicolon)
21055 goto out;
21059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21060 out:
21061 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21064 /* Parse a pure-specifier.
21066 pure-specifier:
21069 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21070 Otherwise, ERROR_MARK_NODE is returned. */
21072 static tree
21073 cp_parser_pure_specifier (cp_parser* parser)
21075 cp_token *token;
21077 /* Look for the `=' token. */
21078 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21079 return error_mark_node;
21080 /* Look for the `0' token. */
21081 token = cp_lexer_peek_token (parser->lexer);
21083 if (token->type == CPP_EOF
21084 || token->type == CPP_PRAGMA_EOL)
21085 return error_mark_node;
21087 cp_lexer_consume_token (parser->lexer);
21089 /* Accept = default or = delete in c++0x mode. */
21090 if (token->keyword == RID_DEFAULT
21091 || token->keyword == RID_DELETE)
21093 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21094 return token->u.value;
21097 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21098 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21100 cp_parser_error (parser,
21101 "invalid pure specifier (only %<= 0%> is allowed)");
21102 cp_parser_skip_to_end_of_statement (parser);
21103 return error_mark_node;
21105 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21107 error_at (token->location, "templates may not be %<virtual%>");
21108 return error_mark_node;
21111 return integer_zero_node;
21114 /* Parse a constant-initializer.
21116 constant-initializer:
21117 = constant-expression
21119 Returns a representation of the constant-expression. */
21121 static tree
21122 cp_parser_constant_initializer (cp_parser* parser)
21124 /* Look for the `=' token. */
21125 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21126 return error_mark_node;
21128 /* It is invalid to write:
21130 struct S { static const int i = { 7 }; };
21133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21135 cp_parser_error (parser,
21136 "a brace-enclosed initializer is not allowed here");
21137 /* Consume the opening brace. */
21138 cp_lexer_consume_token (parser->lexer);
21139 /* Skip the initializer. */
21140 cp_parser_skip_to_closing_brace (parser);
21141 /* Look for the trailing `}'. */
21142 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21144 return error_mark_node;
21147 return cp_parser_constant_expression (parser);
21150 /* Derived classes [gram.class.derived] */
21152 /* Parse a base-clause.
21154 base-clause:
21155 : base-specifier-list
21157 base-specifier-list:
21158 base-specifier ... [opt]
21159 base-specifier-list , base-specifier ... [opt]
21161 Returns a TREE_LIST representing the base-classes, in the order in
21162 which they were declared. The representation of each node is as
21163 described by cp_parser_base_specifier.
21165 In the case that no bases are specified, this function will return
21166 NULL_TREE, not ERROR_MARK_NODE. */
21168 static tree
21169 cp_parser_base_clause (cp_parser* parser)
21171 tree bases = NULL_TREE;
21173 /* Look for the `:' that begins the list. */
21174 cp_parser_require (parser, CPP_COLON, RT_COLON);
21176 /* Scan the base-specifier-list. */
21177 while (true)
21179 cp_token *token;
21180 tree base;
21181 bool pack_expansion_p = false;
21183 /* Look for the base-specifier. */
21184 base = cp_parser_base_specifier (parser);
21185 /* Look for the (optional) ellipsis. */
21186 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21188 /* Consume the `...'. */
21189 cp_lexer_consume_token (parser->lexer);
21191 pack_expansion_p = true;
21194 /* Add BASE to the front of the list. */
21195 if (base && base != error_mark_node)
21197 if (pack_expansion_p)
21198 /* Make this a pack expansion type. */
21199 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21201 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21203 TREE_CHAIN (base) = bases;
21204 bases = base;
21207 /* Peek at the next token. */
21208 token = cp_lexer_peek_token (parser->lexer);
21209 /* If it's not a comma, then the list is complete. */
21210 if (token->type != CPP_COMMA)
21211 break;
21212 /* Consume the `,'. */
21213 cp_lexer_consume_token (parser->lexer);
21216 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21217 base class had a qualified name. However, the next name that
21218 appears is certainly not qualified. */
21219 parser->scope = NULL_TREE;
21220 parser->qualifying_scope = NULL_TREE;
21221 parser->object_scope = NULL_TREE;
21223 return nreverse (bases);
21226 /* Parse a base-specifier.
21228 base-specifier:
21229 :: [opt] nested-name-specifier [opt] class-name
21230 virtual access-specifier [opt] :: [opt] nested-name-specifier
21231 [opt] class-name
21232 access-specifier virtual [opt] :: [opt] nested-name-specifier
21233 [opt] class-name
21235 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21236 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21237 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21238 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21240 static tree
21241 cp_parser_base_specifier (cp_parser* parser)
21243 cp_token *token;
21244 bool done = false;
21245 bool virtual_p = false;
21246 bool duplicate_virtual_error_issued_p = false;
21247 bool duplicate_access_error_issued_p = false;
21248 bool class_scope_p, template_p;
21249 tree access = access_default_node;
21250 tree type;
21252 /* Process the optional `virtual' and `access-specifier'. */
21253 while (!done)
21255 /* Peek at the next token. */
21256 token = cp_lexer_peek_token (parser->lexer);
21257 /* Process `virtual'. */
21258 switch (token->keyword)
21260 case RID_VIRTUAL:
21261 /* If `virtual' appears more than once, issue an error. */
21262 if (virtual_p && !duplicate_virtual_error_issued_p)
21264 cp_parser_error (parser,
21265 "%<virtual%> specified more than once in base-specified");
21266 duplicate_virtual_error_issued_p = true;
21269 virtual_p = true;
21271 /* Consume the `virtual' token. */
21272 cp_lexer_consume_token (parser->lexer);
21274 break;
21276 case RID_PUBLIC:
21277 case RID_PROTECTED:
21278 case RID_PRIVATE:
21279 /* If more than one access specifier appears, issue an
21280 error. */
21281 if (access != access_default_node
21282 && !duplicate_access_error_issued_p)
21284 cp_parser_error (parser,
21285 "more than one access specifier in base-specified");
21286 duplicate_access_error_issued_p = true;
21289 access = ridpointers[(int) token->keyword];
21291 /* Consume the access-specifier. */
21292 cp_lexer_consume_token (parser->lexer);
21294 break;
21296 default:
21297 done = true;
21298 break;
21301 /* It is not uncommon to see programs mechanically, erroneously, use
21302 the 'typename' keyword to denote (dependent) qualified types
21303 as base classes. */
21304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21306 token = cp_lexer_peek_token (parser->lexer);
21307 if (!processing_template_decl)
21308 error_at (token->location,
21309 "keyword %<typename%> not allowed outside of templates");
21310 else
21311 error_at (token->location,
21312 "keyword %<typename%> not allowed in this context "
21313 "(the base class is implicitly a type)");
21314 cp_lexer_consume_token (parser->lexer);
21317 /* Look for the optional `::' operator. */
21318 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21319 /* Look for the nested-name-specifier. The simplest way to
21320 implement:
21322 [temp.res]
21324 The keyword `typename' is not permitted in a base-specifier or
21325 mem-initializer; in these contexts a qualified name that
21326 depends on a template-parameter is implicitly assumed to be a
21327 type name.
21329 is to pretend that we have seen the `typename' keyword at this
21330 point. */
21331 cp_parser_nested_name_specifier_opt (parser,
21332 /*typename_keyword_p=*/true,
21333 /*check_dependency_p=*/true,
21334 typename_type,
21335 /*is_declaration=*/true);
21336 /* If the base class is given by a qualified name, assume that names
21337 we see are type names or templates, as appropriate. */
21338 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21339 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21341 if (!parser->scope
21342 && cp_lexer_next_token_is_decltype (parser->lexer))
21343 /* DR 950 allows decltype as a base-specifier. */
21344 type = cp_parser_decltype (parser);
21345 else
21347 /* Otherwise, look for the class-name. */
21348 type = cp_parser_class_name (parser,
21349 class_scope_p,
21350 template_p,
21351 typename_type,
21352 /*check_dependency_p=*/true,
21353 /*class_head_p=*/false,
21354 /*is_declaration=*/true);
21355 type = TREE_TYPE (type);
21358 if (type == error_mark_node)
21359 return error_mark_node;
21361 return finish_base_specifier (type, access, virtual_p);
21364 /* Exception handling [gram.exception] */
21366 /* Parse an (optional) noexcept-specification.
21368 noexcept-specification:
21369 noexcept ( constant-expression ) [opt]
21371 If no noexcept-specification is present, returns NULL_TREE.
21372 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21373 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21374 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21375 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21376 in which case a boolean condition is returned instead. */
21378 static tree
21379 cp_parser_noexcept_specification_opt (cp_parser* parser,
21380 bool require_constexpr,
21381 bool* consumed_expr,
21382 bool return_cond)
21384 cp_token *token;
21385 const char *saved_message;
21387 /* Peek at the next token. */
21388 token = cp_lexer_peek_token (parser->lexer);
21390 /* Is it a noexcept-specification? */
21391 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21393 tree expr;
21394 cp_lexer_consume_token (parser->lexer);
21396 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21398 cp_lexer_consume_token (parser->lexer);
21400 if (require_constexpr)
21402 /* Types may not be defined in an exception-specification. */
21403 saved_message = parser->type_definition_forbidden_message;
21404 parser->type_definition_forbidden_message
21405 = G_("types may not be defined in an exception-specification");
21407 expr = cp_parser_constant_expression (parser);
21409 /* Restore the saved message. */
21410 parser->type_definition_forbidden_message = saved_message;
21412 else
21414 expr = cp_parser_expression (parser);
21415 *consumed_expr = true;
21418 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21420 else
21422 expr = boolean_true_node;
21423 if (!require_constexpr)
21424 *consumed_expr = false;
21427 /* We cannot build a noexcept-spec right away because this will check
21428 that expr is a constexpr. */
21429 if (!return_cond)
21430 return build_noexcept_spec (expr, tf_warning_or_error);
21431 else
21432 return expr;
21434 else
21435 return NULL_TREE;
21438 /* Parse an (optional) exception-specification.
21440 exception-specification:
21441 throw ( type-id-list [opt] )
21443 Returns a TREE_LIST representing the exception-specification. The
21444 TREE_VALUE of each node is a type. */
21446 static tree
21447 cp_parser_exception_specification_opt (cp_parser* parser)
21449 cp_token *token;
21450 tree type_id_list;
21451 const char *saved_message;
21453 /* Peek at the next token. */
21454 token = cp_lexer_peek_token (parser->lexer);
21456 /* Is it a noexcept-specification? */
21457 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21458 false);
21459 if (type_id_list != NULL_TREE)
21460 return type_id_list;
21462 /* If it's not `throw', then there's no exception-specification. */
21463 if (!cp_parser_is_keyword (token, RID_THROW))
21464 return NULL_TREE;
21466 #if 0
21467 /* Enable this once a lot of code has transitioned to noexcept? */
21468 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21469 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21470 "deprecated in C++0x; use %<noexcept%> instead");
21471 #endif
21473 /* Consume the `throw'. */
21474 cp_lexer_consume_token (parser->lexer);
21476 /* Look for the `('. */
21477 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21479 /* Peek at the next token. */
21480 token = cp_lexer_peek_token (parser->lexer);
21481 /* If it's not a `)', then there is a type-id-list. */
21482 if (token->type != CPP_CLOSE_PAREN)
21484 /* Types may not be defined in an exception-specification. */
21485 saved_message = parser->type_definition_forbidden_message;
21486 parser->type_definition_forbidden_message
21487 = G_("types may not be defined in an exception-specification");
21488 /* Parse the type-id-list. */
21489 type_id_list = cp_parser_type_id_list (parser);
21490 /* Restore the saved message. */
21491 parser->type_definition_forbidden_message = saved_message;
21493 else
21494 type_id_list = empty_except_spec;
21496 /* Look for the `)'. */
21497 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21499 return type_id_list;
21502 /* Parse an (optional) type-id-list.
21504 type-id-list:
21505 type-id ... [opt]
21506 type-id-list , type-id ... [opt]
21508 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21509 in the order that the types were presented. */
21511 static tree
21512 cp_parser_type_id_list (cp_parser* parser)
21514 tree types = NULL_TREE;
21516 while (true)
21518 cp_token *token;
21519 tree type;
21521 /* Get the next type-id. */
21522 type = cp_parser_type_id (parser);
21523 /* Parse the optional ellipsis. */
21524 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21526 /* Consume the `...'. */
21527 cp_lexer_consume_token (parser->lexer);
21529 /* Turn the type into a pack expansion expression. */
21530 type = make_pack_expansion (type);
21532 /* Add it to the list. */
21533 types = add_exception_specifier (types, type, /*complain=*/1);
21534 /* Peek at the next token. */
21535 token = cp_lexer_peek_token (parser->lexer);
21536 /* If it is not a `,', we are done. */
21537 if (token->type != CPP_COMMA)
21538 break;
21539 /* Consume the `,'. */
21540 cp_lexer_consume_token (parser->lexer);
21543 return nreverse (types);
21546 /* Parse a try-block.
21548 try-block:
21549 try compound-statement handler-seq */
21551 static tree
21552 cp_parser_try_block (cp_parser* parser)
21554 tree try_block;
21556 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21557 if (parser->in_function_body
21558 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21559 error ("%<try%> in %<constexpr%> function");
21561 try_block = begin_try_block ();
21562 cp_parser_compound_statement (parser, NULL, true, false);
21563 finish_try_block (try_block);
21564 cp_parser_handler_seq (parser);
21565 finish_handler_sequence (try_block);
21567 return try_block;
21570 /* Parse a function-try-block.
21572 function-try-block:
21573 try ctor-initializer [opt] function-body handler-seq */
21575 static bool
21576 cp_parser_function_try_block (cp_parser* parser)
21578 tree compound_stmt;
21579 tree try_block;
21580 bool ctor_initializer_p;
21582 /* Look for the `try' keyword. */
21583 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21584 return false;
21585 /* Let the rest of the front end know where we are. */
21586 try_block = begin_function_try_block (&compound_stmt);
21587 /* Parse the function-body. */
21588 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21589 (parser, /*in_function_try_block=*/true);
21590 /* We're done with the `try' part. */
21591 finish_function_try_block (try_block);
21592 /* Parse the handlers. */
21593 cp_parser_handler_seq (parser);
21594 /* We're done with the handlers. */
21595 finish_function_handler_sequence (try_block, compound_stmt);
21597 return ctor_initializer_p;
21600 /* Parse a handler-seq.
21602 handler-seq:
21603 handler handler-seq [opt] */
21605 static void
21606 cp_parser_handler_seq (cp_parser* parser)
21608 while (true)
21610 cp_token *token;
21612 /* Parse the handler. */
21613 cp_parser_handler (parser);
21614 /* Peek at the next token. */
21615 token = cp_lexer_peek_token (parser->lexer);
21616 /* If it's not `catch' then there are no more handlers. */
21617 if (!cp_parser_is_keyword (token, RID_CATCH))
21618 break;
21622 /* Parse a handler.
21624 handler:
21625 catch ( exception-declaration ) compound-statement */
21627 static void
21628 cp_parser_handler (cp_parser* parser)
21630 tree handler;
21631 tree declaration;
21633 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21634 handler = begin_handler ();
21635 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21636 declaration = cp_parser_exception_declaration (parser);
21637 finish_handler_parms (declaration, handler);
21638 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21639 cp_parser_compound_statement (parser, NULL, false, false);
21640 finish_handler (handler);
21643 /* Parse an exception-declaration.
21645 exception-declaration:
21646 type-specifier-seq declarator
21647 type-specifier-seq abstract-declarator
21648 type-specifier-seq
21651 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21652 ellipsis variant is used. */
21654 static tree
21655 cp_parser_exception_declaration (cp_parser* parser)
21657 cp_decl_specifier_seq type_specifiers;
21658 cp_declarator *declarator;
21659 const char *saved_message;
21661 /* If it's an ellipsis, it's easy to handle. */
21662 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21664 /* Consume the `...' token. */
21665 cp_lexer_consume_token (parser->lexer);
21666 return NULL_TREE;
21669 /* Types may not be defined in exception-declarations. */
21670 saved_message = parser->type_definition_forbidden_message;
21671 parser->type_definition_forbidden_message
21672 = G_("types may not be defined in exception-declarations");
21674 /* Parse the type-specifier-seq. */
21675 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21676 /*is_trailing_return=*/false,
21677 &type_specifiers);
21678 /* If it's a `)', then there is no declarator. */
21679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21680 declarator = NULL;
21681 else
21682 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21683 /*ctor_dtor_or_conv_p=*/NULL,
21684 /*parenthesized_p=*/NULL,
21685 /*member_p=*/false,
21686 /*friend_p=*/false);
21688 /* Restore the saved message. */
21689 parser->type_definition_forbidden_message = saved_message;
21691 if (!type_specifiers.any_specifiers_p)
21692 return error_mark_node;
21694 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21697 /* Parse a throw-expression.
21699 throw-expression:
21700 throw assignment-expression [opt]
21702 Returns a THROW_EXPR representing the throw-expression. */
21704 static tree
21705 cp_parser_throw_expression (cp_parser* parser)
21707 tree expression;
21708 cp_token* token;
21710 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21711 token = cp_lexer_peek_token (parser->lexer);
21712 /* Figure out whether or not there is an assignment-expression
21713 following the "throw" keyword. */
21714 if (token->type == CPP_COMMA
21715 || token->type == CPP_SEMICOLON
21716 || token->type == CPP_CLOSE_PAREN
21717 || token->type == CPP_CLOSE_SQUARE
21718 || token->type == CPP_CLOSE_BRACE
21719 || token->type == CPP_COLON)
21720 expression = NULL_TREE;
21721 else
21722 expression = cp_parser_assignment_expression (parser);
21724 return build_throw (expression);
21727 /* GNU Extensions */
21729 /* Parse an (optional) asm-specification.
21731 asm-specification:
21732 asm ( string-literal )
21734 If the asm-specification is present, returns a STRING_CST
21735 corresponding to the string-literal. Otherwise, returns
21736 NULL_TREE. */
21738 static tree
21739 cp_parser_asm_specification_opt (cp_parser* parser)
21741 cp_token *token;
21742 tree asm_specification;
21744 /* Peek at the next token. */
21745 token = cp_lexer_peek_token (parser->lexer);
21746 /* If the next token isn't the `asm' keyword, then there's no
21747 asm-specification. */
21748 if (!cp_parser_is_keyword (token, RID_ASM))
21749 return NULL_TREE;
21751 /* Consume the `asm' token. */
21752 cp_lexer_consume_token (parser->lexer);
21753 /* Look for the `('. */
21754 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21756 /* Look for the string-literal. */
21757 asm_specification = cp_parser_string_literal (parser, false, false);
21759 /* Look for the `)'. */
21760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21762 return asm_specification;
21765 /* Parse an asm-operand-list.
21767 asm-operand-list:
21768 asm-operand
21769 asm-operand-list , asm-operand
21771 asm-operand:
21772 string-literal ( expression )
21773 [ string-literal ] string-literal ( expression )
21775 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21776 each node is the expression. The TREE_PURPOSE is itself a
21777 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21778 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21779 is a STRING_CST for the string literal before the parenthesis. Returns
21780 ERROR_MARK_NODE if any of the operands are invalid. */
21782 static tree
21783 cp_parser_asm_operand_list (cp_parser* parser)
21785 tree asm_operands = NULL_TREE;
21786 bool invalid_operands = false;
21788 while (true)
21790 tree string_literal;
21791 tree expression;
21792 tree name;
21794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21796 /* Consume the `[' token. */
21797 cp_lexer_consume_token (parser->lexer);
21798 /* Read the operand name. */
21799 name = cp_parser_identifier (parser);
21800 if (name != error_mark_node)
21801 name = build_string (IDENTIFIER_LENGTH (name),
21802 IDENTIFIER_POINTER (name));
21803 /* Look for the closing `]'. */
21804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21806 else
21807 name = NULL_TREE;
21808 /* Look for the string-literal. */
21809 string_literal = cp_parser_string_literal (parser, false, false);
21811 /* Look for the `('. */
21812 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21813 /* Parse the expression. */
21814 expression = cp_parser_expression (parser);
21815 /* Look for the `)'. */
21816 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21818 if (name == error_mark_node
21819 || string_literal == error_mark_node
21820 || expression == error_mark_node)
21821 invalid_operands = true;
21823 /* Add this operand to the list. */
21824 asm_operands = tree_cons (build_tree_list (name, string_literal),
21825 expression,
21826 asm_operands);
21827 /* If the next token is not a `,', there are no more
21828 operands. */
21829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21830 break;
21831 /* Consume the `,'. */
21832 cp_lexer_consume_token (parser->lexer);
21835 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21838 /* Parse an asm-clobber-list.
21840 asm-clobber-list:
21841 string-literal
21842 asm-clobber-list , string-literal
21844 Returns a TREE_LIST, indicating the clobbers in the order that they
21845 appeared. The TREE_VALUE of each node is a STRING_CST. */
21847 static tree
21848 cp_parser_asm_clobber_list (cp_parser* parser)
21850 tree clobbers = NULL_TREE;
21852 while (true)
21854 tree string_literal;
21856 /* Look for the string literal. */
21857 string_literal = cp_parser_string_literal (parser, false, false);
21858 /* Add it to the list. */
21859 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21860 /* If the next token is not a `,', then the list is
21861 complete. */
21862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21863 break;
21864 /* Consume the `,' token. */
21865 cp_lexer_consume_token (parser->lexer);
21868 return clobbers;
21871 /* Parse an asm-label-list.
21873 asm-label-list:
21874 identifier
21875 asm-label-list , identifier
21877 Returns a TREE_LIST, indicating the labels in the order that they
21878 appeared. The TREE_VALUE of each node is a label. */
21880 static tree
21881 cp_parser_asm_label_list (cp_parser* parser)
21883 tree labels = NULL_TREE;
21885 while (true)
21887 tree identifier, label, name;
21889 /* Look for the identifier. */
21890 identifier = cp_parser_identifier (parser);
21891 if (!error_operand_p (identifier))
21893 label = lookup_label (identifier);
21894 if (TREE_CODE (label) == LABEL_DECL)
21896 TREE_USED (label) = 1;
21897 check_goto (label);
21898 name = build_string (IDENTIFIER_LENGTH (identifier),
21899 IDENTIFIER_POINTER (identifier));
21900 labels = tree_cons (name, label, labels);
21903 /* If the next token is not a `,', then the list is
21904 complete. */
21905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21906 break;
21907 /* Consume the `,' token. */
21908 cp_lexer_consume_token (parser->lexer);
21911 return nreverse (labels);
21914 /* Return TRUE iff the next tokens in the stream are possibly the
21915 beginning of a GNU extension attribute. */
21917 static bool
21918 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21920 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21923 /* Return TRUE iff the next tokens in the stream are possibly the
21924 beginning of a standard C++-11 attribute specifier. */
21926 static bool
21927 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21929 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21932 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21933 beginning of a standard C++-11 attribute specifier. */
21935 static bool
21936 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21938 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21940 return (cxx_dialect >= cxx11
21941 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21942 || (token->type == CPP_OPEN_SQUARE
21943 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21944 && token->type == CPP_OPEN_SQUARE)));
21947 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21948 beginning of a GNU extension attribute. */
21950 static bool
21951 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21953 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21955 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21958 /* Return true iff the next tokens can be the beginning of either a
21959 GNU attribute list, or a standard C++11 attribute sequence. */
21961 static bool
21962 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21964 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21965 || cp_next_tokens_can_be_std_attribute_p (parser));
21968 /* Return true iff the next Nth tokens can be the beginning of either
21969 a GNU attribute list, or a standard C++11 attribute sequence. */
21971 static bool
21972 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21974 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21975 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21978 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21979 of GNU attributes, or return NULL. */
21981 static tree
21982 cp_parser_attributes_opt (cp_parser *parser)
21984 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21985 return cp_parser_gnu_attributes_opt (parser);
21986 return cp_parser_std_attribute_spec_seq (parser);
21989 #define CILK_SIMD_FN_CLAUSE_MASK \
21990 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21991 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21992 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21993 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21994 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21996 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21997 vector [(<clauses>)] */
21999 static void
22000 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22002 bool first_p = parser->cilk_simd_fn_info == NULL;
22003 cp_token *token = v_token;
22004 if (first_p)
22006 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22007 parser->cilk_simd_fn_info->error_seen = false;
22008 parser->cilk_simd_fn_info->fndecl_seen = false;
22009 parser->cilk_simd_fn_info->tokens = vNULL;
22011 int paren_scope = 0;
22012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22014 cp_lexer_consume_token (parser->lexer);
22015 v_token = cp_lexer_peek_token (parser->lexer);
22016 paren_scope++;
22018 while (paren_scope > 0)
22020 token = cp_lexer_peek_token (parser->lexer);
22021 if (token->type == CPP_OPEN_PAREN)
22022 paren_scope++;
22023 else if (token->type == CPP_CLOSE_PAREN)
22024 paren_scope--;
22025 /* Do not push the last ')' */
22026 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22027 cp_lexer_consume_token (parser->lexer);
22030 token->type = CPP_PRAGMA_EOL;
22031 parser->lexer->next_token = token;
22032 cp_lexer_consume_token (parser->lexer);
22034 struct cp_token_cache *cp
22035 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22036 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22039 /* Parse an (optional) series of attributes.
22041 attributes:
22042 attributes attribute
22044 attribute:
22045 __attribute__ (( attribute-list [opt] ))
22047 The return value is as for cp_parser_gnu_attribute_list. */
22049 static tree
22050 cp_parser_gnu_attributes_opt (cp_parser* parser)
22052 tree attributes = NULL_TREE;
22054 while (true)
22056 cp_token *token;
22057 tree attribute_list;
22058 bool ok = true;
22060 /* Peek at the next token. */
22061 token = cp_lexer_peek_token (parser->lexer);
22062 /* If it's not `__attribute__', then we're done. */
22063 if (token->keyword != RID_ATTRIBUTE)
22064 break;
22066 /* Consume the `__attribute__' keyword. */
22067 cp_lexer_consume_token (parser->lexer);
22068 /* Look for the two `(' tokens. */
22069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22070 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22072 /* Peek at the next token. */
22073 token = cp_lexer_peek_token (parser->lexer);
22074 if (token->type != CPP_CLOSE_PAREN)
22075 /* Parse the attribute-list. */
22076 attribute_list = cp_parser_gnu_attribute_list (parser);
22077 else
22078 /* If the next token is a `)', then there is no attribute
22079 list. */
22080 attribute_list = NULL;
22082 /* Look for the two `)' tokens. */
22083 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22084 ok = false;
22085 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22086 ok = false;
22087 if (!ok)
22088 cp_parser_skip_to_end_of_statement (parser);
22090 /* Add these new attributes to the list. */
22091 attributes = chainon (attributes, attribute_list);
22094 return attributes;
22097 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22098 "__vector" or "__vector__." */
22100 static inline bool
22101 is_cilkplus_vector_p (tree name)
22103 if (flag_cilkplus && is_attribute_p ("vector", name))
22104 return true;
22105 return false;
22108 /* Parse a GNU attribute-list.
22110 attribute-list:
22111 attribute
22112 attribute-list , attribute
22114 attribute:
22115 identifier
22116 identifier ( identifier )
22117 identifier ( identifier , expression-list )
22118 identifier ( expression-list )
22120 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22121 to an attribute. The TREE_PURPOSE of each node is the identifier
22122 indicating which attribute is in use. The TREE_VALUE represents
22123 the arguments, if any. */
22125 static tree
22126 cp_parser_gnu_attribute_list (cp_parser* parser)
22128 tree attribute_list = NULL_TREE;
22129 bool save_translate_strings_p = parser->translate_strings_p;
22131 parser->translate_strings_p = false;
22132 while (true)
22134 cp_token *token;
22135 tree identifier;
22136 tree attribute;
22138 /* Look for the identifier. We also allow keywords here; for
22139 example `__attribute__ ((const))' is legal. */
22140 token = cp_lexer_peek_token (parser->lexer);
22141 if (token->type == CPP_NAME
22142 || token->type == CPP_KEYWORD)
22144 tree arguments = NULL_TREE;
22146 /* Consume the token, but save it since we need it for the
22147 SIMD enabled function parsing. */
22148 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22150 /* Save away the identifier that indicates which attribute
22151 this is. */
22152 identifier = (token->type == CPP_KEYWORD)
22153 /* For keywords, use the canonical spelling, not the
22154 parsed identifier. */
22155 ? ridpointers[(int) token->keyword]
22156 : id_token->u.value;
22158 attribute = build_tree_list (identifier, NULL_TREE);
22160 /* Peek at the next token. */
22161 token = cp_lexer_peek_token (parser->lexer);
22162 /* If it's an `(', then parse the attribute arguments. */
22163 if (token->type == CPP_OPEN_PAREN)
22165 vec<tree, va_gc> *vec;
22166 int attr_flag = (attribute_takes_identifier_p (identifier)
22167 ? id_attr : normal_attr);
22168 if (is_cilkplus_vector_p (identifier))
22170 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22171 continue;
22173 else
22174 vec = cp_parser_parenthesized_expression_list
22175 (parser, attr_flag, /*cast_p=*/false,
22176 /*allow_expansion_p=*/false,
22177 /*non_constant_p=*/NULL);
22178 if (vec == NULL)
22179 arguments = error_mark_node;
22180 else
22182 arguments = build_tree_list_vec (vec);
22183 release_tree_vector (vec);
22185 /* Save the arguments away. */
22186 TREE_VALUE (attribute) = arguments;
22188 else if (is_cilkplus_vector_p (identifier))
22190 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22191 continue;
22194 if (arguments != error_mark_node)
22196 /* Add this attribute to the list. */
22197 TREE_CHAIN (attribute) = attribute_list;
22198 attribute_list = attribute;
22201 token = cp_lexer_peek_token (parser->lexer);
22203 /* Now, look for more attributes. If the next token isn't a
22204 `,', we're done. */
22205 if (token->type != CPP_COMMA)
22206 break;
22208 /* Consume the comma and keep going. */
22209 cp_lexer_consume_token (parser->lexer);
22211 parser->translate_strings_p = save_translate_strings_p;
22213 /* We built up the list in reverse order. */
22214 return nreverse (attribute_list);
22217 /* Parse a standard C++11 attribute.
22219 The returned representation is a TREE_LIST which TREE_PURPOSE is
22220 the scoped name of the attribute, and the TREE_VALUE is its
22221 arguments list.
22223 Note that the scoped name of the attribute is itself a TREE_LIST
22224 which TREE_PURPOSE is the namespace of the attribute, and
22225 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22226 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22227 and which TREE_PURPOSE is directly the attribute name.
22229 Clients of the attribute code should use get_attribute_namespace
22230 and get_attribute_name to get the actual namespace and name of
22231 attributes, regardless of their being GNU or C++11 attributes.
22233 attribute:
22234 attribute-token attribute-argument-clause [opt]
22236 attribute-token:
22237 identifier
22238 attribute-scoped-token
22240 attribute-scoped-token:
22241 attribute-namespace :: identifier
22243 attribute-namespace:
22244 identifier
22246 attribute-argument-clause:
22247 ( balanced-token-seq )
22249 balanced-token-seq:
22250 balanced-token [opt]
22251 balanced-token-seq balanced-token
22253 balanced-token:
22254 ( balanced-token-seq )
22255 [ balanced-token-seq ]
22256 { balanced-token-seq }. */
22258 static tree
22259 cp_parser_std_attribute (cp_parser *parser)
22261 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22262 cp_token *token;
22264 /* First, parse name of the the attribute, a.k.a
22265 attribute-token. */
22267 token = cp_lexer_peek_token (parser->lexer);
22268 if (token->type == CPP_NAME)
22269 attr_id = token->u.value;
22270 else if (token->type == CPP_KEYWORD)
22271 attr_id = ridpointers[(int) token->keyword];
22272 else if (token->flags & NAMED_OP)
22273 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22275 if (attr_id == NULL_TREE)
22276 return NULL_TREE;
22278 cp_lexer_consume_token (parser->lexer);
22280 token = cp_lexer_peek_token (parser->lexer);
22281 if (token->type == CPP_SCOPE)
22283 /* We are seeing a scoped attribute token. */
22285 cp_lexer_consume_token (parser->lexer);
22286 attr_ns = attr_id;
22288 token = cp_lexer_consume_token (parser->lexer);
22289 if (token->type == CPP_NAME)
22290 attr_id = token->u.value;
22291 else if (token->type == CPP_KEYWORD)
22292 attr_id = ridpointers[(int) token->keyword];
22293 else
22295 error_at (token->location,
22296 "expected an identifier for the attribute name");
22297 return error_mark_node;
22299 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22300 NULL_TREE);
22301 token = cp_lexer_peek_token (parser->lexer);
22303 else
22305 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22306 NULL_TREE);
22307 /* C++11 noreturn attribute is equivalent to GNU's. */
22308 if (is_attribute_p ("noreturn", attr_id))
22309 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22310 /* C++14 deprecated attribute is equivalent to GNU's. */
22311 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22313 if (cxx_dialect == cxx11)
22314 pedwarn (token->location, OPT_Wpedantic,
22315 "%<deprecated%> is a C++14 feature;"
22316 " use %<gnu::deprecated%>");
22317 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22321 /* Now parse the optional argument clause of the attribute. */
22323 if (token->type != CPP_OPEN_PAREN)
22324 return attribute;
22327 vec<tree, va_gc> *vec;
22328 int attr_flag = normal_attr;
22330 if (attr_ns == get_identifier ("gnu")
22331 && attribute_takes_identifier_p (attr_id))
22332 /* A GNU attribute that takes an identifier in parameter. */
22333 attr_flag = id_attr;
22335 vec = cp_parser_parenthesized_expression_list
22336 (parser, attr_flag, /*cast_p=*/false,
22337 /*allow_expansion_p=*/true,
22338 /*non_constant_p=*/NULL);
22339 if (vec == NULL)
22340 arguments = error_mark_node;
22341 else
22343 arguments = build_tree_list_vec (vec);
22344 release_tree_vector (vec);
22347 if (arguments == error_mark_node)
22348 attribute = error_mark_node;
22349 else
22350 TREE_VALUE (attribute) = arguments;
22353 return attribute;
22356 /* Parse a list of standard C++-11 attributes.
22358 attribute-list:
22359 attribute [opt]
22360 attribute-list , attribute[opt]
22361 attribute ...
22362 attribute-list , attribute ...
22365 static tree
22366 cp_parser_std_attribute_list (cp_parser *parser)
22368 tree attributes = NULL_TREE, attribute = NULL_TREE;
22369 cp_token *token = NULL;
22371 while (true)
22373 attribute = cp_parser_std_attribute (parser);
22374 if (attribute == error_mark_node)
22375 break;
22376 if (attribute != NULL_TREE)
22378 TREE_CHAIN (attribute) = attributes;
22379 attributes = attribute;
22381 token = cp_lexer_peek_token (parser->lexer);
22382 if (token->type != CPP_COMMA)
22383 break;
22384 cp_lexer_consume_token (parser->lexer);
22386 attributes = nreverse (attributes);
22387 return attributes;
22390 /* Parse a standard C++-11 attribute specifier.
22392 attribute-specifier:
22393 [ [ attribute-list ] ]
22394 alignment-specifier
22396 alignment-specifier:
22397 alignas ( type-id ... [opt] )
22398 alignas ( alignment-expression ... [opt] ). */
22400 static tree
22401 cp_parser_std_attribute_spec (cp_parser *parser)
22403 tree attributes = NULL_TREE;
22404 cp_token *token = cp_lexer_peek_token (parser->lexer);
22406 if (token->type == CPP_OPEN_SQUARE
22407 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22409 cp_lexer_consume_token (parser->lexer);
22410 cp_lexer_consume_token (parser->lexer);
22412 attributes = cp_parser_std_attribute_list (parser);
22414 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22415 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22416 cp_parser_skip_to_end_of_statement (parser);
22417 else
22418 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22419 when we are sure that we have actually parsed them. */
22420 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22422 else
22424 tree alignas_expr;
22426 /* Look for an alignment-specifier. */
22428 token = cp_lexer_peek_token (parser->lexer);
22430 if (token->type != CPP_KEYWORD
22431 || token->keyword != RID_ALIGNAS)
22432 return NULL_TREE;
22434 cp_lexer_consume_token (parser->lexer);
22435 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22437 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22439 cp_parser_error (parser, "expected %<(%>");
22440 return error_mark_node;
22443 cp_parser_parse_tentatively (parser);
22444 alignas_expr = cp_parser_type_id (parser);
22446 if (!cp_parser_parse_definitely (parser))
22448 gcc_assert (alignas_expr == error_mark_node
22449 || alignas_expr == NULL_TREE);
22451 alignas_expr =
22452 cp_parser_assignment_expression (parser);
22453 if (alignas_expr == error_mark_node)
22454 cp_parser_skip_to_end_of_statement (parser);
22455 if (alignas_expr == NULL_TREE
22456 || alignas_expr == error_mark_node)
22457 return alignas_expr;
22460 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22462 cp_parser_error (parser, "expected %<)%>");
22463 return error_mark_node;
22466 alignas_expr = cxx_alignas_expr (alignas_expr);
22468 /* Build the C++-11 representation of an 'aligned'
22469 attribute. */
22470 attributes =
22471 build_tree_list (build_tree_list (get_identifier ("gnu"),
22472 get_identifier ("aligned")),
22473 build_tree_list (NULL_TREE, alignas_expr));
22476 return attributes;
22479 /* Parse a standard C++-11 attribute-specifier-seq.
22481 attribute-specifier-seq:
22482 attribute-specifier-seq [opt] attribute-specifier
22485 static tree
22486 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22488 tree attr_specs = NULL;
22490 while (true)
22492 tree attr_spec = cp_parser_std_attribute_spec (parser);
22493 if (attr_spec == NULL_TREE)
22494 break;
22495 if (attr_spec == error_mark_node)
22496 return error_mark_node;
22498 TREE_CHAIN (attr_spec) = attr_specs;
22499 attr_specs = attr_spec;
22502 attr_specs = nreverse (attr_specs);
22503 return attr_specs;
22506 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22507 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22508 current value of the PEDANTIC flag, regardless of whether or not
22509 the `__extension__' keyword is present. The caller is responsible
22510 for restoring the value of the PEDANTIC flag. */
22512 static bool
22513 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22515 /* Save the old value of the PEDANTIC flag. */
22516 *saved_pedantic = pedantic;
22518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22520 /* Consume the `__extension__' token. */
22521 cp_lexer_consume_token (parser->lexer);
22522 /* We're not being pedantic while the `__extension__' keyword is
22523 in effect. */
22524 pedantic = 0;
22526 return true;
22529 return false;
22532 /* Parse a label declaration.
22534 label-declaration:
22535 __label__ label-declarator-seq ;
22537 label-declarator-seq:
22538 identifier , label-declarator-seq
22539 identifier */
22541 static void
22542 cp_parser_label_declaration (cp_parser* parser)
22544 /* Look for the `__label__' keyword. */
22545 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22547 while (true)
22549 tree identifier;
22551 /* Look for an identifier. */
22552 identifier = cp_parser_identifier (parser);
22553 /* If we failed, stop. */
22554 if (identifier == error_mark_node)
22555 break;
22556 /* Declare it as a label. */
22557 finish_label_decl (identifier);
22558 /* If the next token is a `;', stop. */
22559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22560 break;
22561 /* Look for the `,' separating the label declarations. */
22562 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22565 /* Look for the final `;'. */
22566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22569 /* Support Functions */
22571 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22572 NAME should have one of the representations used for an
22573 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22574 is returned. If PARSER->SCOPE is a dependent type, then a
22575 SCOPE_REF is returned.
22577 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22578 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22579 was formed. Abstractly, such entities should not be passed to this
22580 function, because they do not need to be looked up, but it is
22581 simpler to check for this special case here, rather than at the
22582 call-sites.
22584 In cases not explicitly covered above, this function returns a
22585 DECL, OVERLOAD, or baselink representing the result of the lookup.
22586 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22587 is returned.
22589 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22590 (e.g., "struct") that was used. In that case bindings that do not
22591 refer to types are ignored.
22593 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22594 ignored.
22596 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22597 are ignored.
22599 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22600 types.
22602 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22603 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22604 NULL_TREE otherwise. */
22606 static tree
22607 cp_parser_lookup_name (cp_parser *parser, tree name,
22608 enum tag_types tag_type,
22609 bool is_template,
22610 bool is_namespace,
22611 bool check_dependency,
22612 tree *ambiguous_decls,
22613 location_t name_location)
22615 tree decl;
22616 tree object_type = parser->context->object_type;
22618 /* Assume that the lookup will be unambiguous. */
22619 if (ambiguous_decls)
22620 *ambiguous_decls = NULL_TREE;
22622 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22623 no longer valid. Note that if we are parsing tentatively, and
22624 the parse fails, OBJECT_TYPE will be automatically restored. */
22625 parser->context->object_type = NULL_TREE;
22627 if (name == error_mark_node)
22628 return error_mark_node;
22630 /* A template-id has already been resolved; there is no lookup to
22631 do. */
22632 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22633 return name;
22634 if (BASELINK_P (name))
22636 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22637 == TEMPLATE_ID_EXPR);
22638 return name;
22641 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22642 it should already have been checked to make sure that the name
22643 used matches the type being destroyed. */
22644 if (TREE_CODE (name) == BIT_NOT_EXPR)
22646 tree type;
22648 /* Figure out to which type this destructor applies. */
22649 if (parser->scope)
22650 type = parser->scope;
22651 else if (object_type)
22652 type = object_type;
22653 else
22654 type = current_class_type;
22655 /* If that's not a class type, there is no destructor. */
22656 if (!type || !CLASS_TYPE_P (type))
22657 return error_mark_node;
22658 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22659 lazily_declare_fn (sfk_destructor, type);
22660 if (!CLASSTYPE_DESTRUCTORS (type))
22661 return error_mark_node;
22662 /* If it was a class type, return the destructor. */
22663 return CLASSTYPE_DESTRUCTORS (type);
22666 /* By this point, the NAME should be an ordinary identifier. If
22667 the id-expression was a qualified name, the qualifying scope is
22668 stored in PARSER->SCOPE at this point. */
22669 gcc_assert (identifier_p (name));
22671 /* Perform the lookup. */
22672 if (parser->scope)
22674 bool dependent_p;
22676 if (parser->scope == error_mark_node)
22677 return error_mark_node;
22679 /* If the SCOPE is dependent, the lookup must be deferred until
22680 the template is instantiated -- unless we are explicitly
22681 looking up names in uninstantiated templates. Even then, we
22682 cannot look up the name if the scope is not a class type; it
22683 might, for example, be a template type parameter. */
22684 dependent_p = (TYPE_P (parser->scope)
22685 && dependent_scope_p (parser->scope));
22686 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22687 && dependent_p)
22688 /* Defer lookup. */
22689 decl = error_mark_node;
22690 else
22692 tree pushed_scope = NULL_TREE;
22694 /* If PARSER->SCOPE is a dependent type, then it must be a
22695 class type, and we must not be checking dependencies;
22696 otherwise, we would have processed this lookup above. So
22697 that PARSER->SCOPE is not considered a dependent base by
22698 lookup_member, we must enter the scope here. */
22699 if (dependent_p)
22700 pushed_scope = push_scope (parser->scope);
22702 /* If the PARSER->SCOPE is a template specialization, it
22703 may be instantiated during name lookup. In that case,
22704 errors may be issued. Even if we rollback the current
22705 tentative parse, those errors are valid. */
22706 decl = lookup_qualified_name (parser->scope, name,
22707 tag_type != none_type,
22708 /*complain=*/true);
22710 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22711 lookup result and the nested-name-specifier nominates a class C:
22712 * if the name specified after the nested-name-specifier, when
22713 looked up in C, is the injected-class-name of C (Clause 9), or
22714 * if the name specified after the nested-name-specifier is the
22715 same as the identifier or the simple-template-id's template-
22716 name in the last component of the nested-name-specifier,
22717 the name is instead considered to name the constructor of
22718 class C. [ Note: for example, the constructor is not an
22719 acceptable lookup result in an elaborated-type-specifier so
22720 the constructor would not be used in place of the
22721 injected-class-name. --end note ] Such a constructor name
22722 shall be used only in the declarator-id of a declaration that
22723 names a constructor or in a using-declaration. */
22724 if (tag_type == none_type
22725 && DECL_SELF_REFERENCE_P (decl)
22726 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22727 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22728 tag_type != none_type,
22729 /*complain=*/true);
22731 /* If we have a single function from a using decl, pull it out. */
22732 if (TREE_CODE (decl) == OVERLOAD
22733 && !really_overloaded_fn (decl))
22734 decl = OVL_FUNCTION (decl);
22736 if (pushed_scope)
22737 pop_scope (pushed_scope);
22740 /* If the scope is a dependent type and either we deferred lookup or
22741 we did lookup but didn't find the name, rememeber the name. */
22742 if (decl == error_mark_node && TYPE_P (parser->scope)
22743 && dependent_type_p (parser->scope))
22745 if (tag_type)
22747 tree type;
22749 /* The resolution to Core Issue 180 says that `struct
22750 A::B' should be considered a type-name, even if `A'
22751 is dependent. */
22752 type = make_typename_type (parser->scope, name, tag_type,
22753 /*complain=*/tf_error);
22754 if (type != error_mark_node)
22755 decl = TYPE_NAME (type);
22757 else if (is_template
22758 && (cp_parser_next_token_ends_template_argument_p (parser)
22759 || cp_lexer_next_token_is (parser->lexer,
22760 CPP_CLOSE_PAREN)))
22761 decl = make_unbound_class_template (parser->scope,
22762 name, NULL_TREE,
22763 /*complain=*/tf_error);
22764 else
22765 decl = build_qualified_name (/*type=*/NULL_TREE,
22766 parser->scope, name,
22767 is_template);
22769 parser->qualifying_scope = parser->scope;
22770 parser->object_scope = NULL_TREE;
22772 else if (object_type)
22774 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22775 OBJECT_TYPE is not a class. */
22776 if (CLASS_TYPE_P (object_type))
22777 /* If the OBJECT_TYPE is a template specialization, it may
22778 be instantiated during name lookup. In that case, errors
22779 may be issued. Even if we rollback the current tentative
22780 parse, those errors are valid. */
22781 decl = lookup_member (object_type,
22782 name,
22783 /*protect=*/0,
22784 tag_type != none_type,
22785 tf_warning_or_error);
22786 else
22787 decl = NULL_TREE;
22789 if (!decl)
22790 /* Look it up in the enclosing context. */
22791 decl = lookup_name_real (name, tag_type != none_type,
22792 /*nonclass=*/0,
22793 /*block_p=*/true, is_namespace, 0);
22794 parser->object_scope = object_type;
22795 parser->qualifying_scope = NULL_TREE;
22797 else
22799 decl = lookup_name_real (name, tag_type != none_type,
22800 /*nonclass=*/0,
22801 /*block_p=*/true, is_namespace, 0);
22802 parser->qualifying_scope = NULL_TREE;
22803 parser->object_scope = NULL_TREE;
22806 /* If the lookup failed, let our caller know. */
22807 if (!decl || decl == error_mark_node)
22808 return error_mark_node;
22810 /* Pull out the template from an injected-class-name (or multiple). */
22811 if (is_template)
22812 decl = maybe_get_template_decl_from_type_decl (decl);
22814 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22815 if (TREE_CODE (decl) == TREE_LIST)
22817 if (ambiguous_decls)
22818 *ambiguous_decls = decl;
22819 /* The error message we have to print is too complicated for
22820 cp_parser_error, so we incorporate its actions directly. */
22821 if (!cp_parser_simulate_error (parser))
22823 error_at (name_location, "reference to %qD is ambiguous",
22824 name);
22825 print_candidates (decl);
22827 return error_mark_node;
22830 gcc_assert (DECL_P (decl)
22831 || TREE_CODE (decl) == OVERLOAD
22832 || TREE_CODE (decl) == SCOPE_REF
22833 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22834 || BASELINK_P (decl));
22836 /* If we have resolved the name of a member declaration, check to
22837 see if the declaration is accessible. When the name resolves to
22838 set of overloaded functions, accessibility is checked when
22839 overload resolution is done.
22841 During an explicit instantiation, access is not checked at all,
22842 as per [temp.explicit]. */
22843 if (DECL_P (decl))
22844 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22846 maybe_record_typedef_use (decl);
22848 return decl;
22851 /* Like cp_parser_lookup_name, but for use in the typical case where
22852 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22853 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22855 static tree
22856 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22858 return cp_parser_lookup_name (parser, name,
22859 none_type,
22860 /*is_template=*/false,
22861 /*is_namespace=*/false,
22862 /*check_dependency=*/true,
22863 /*ambiguous_decls=*/NULL,
22864 location);
22867 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22868 the current context, return the TYPE_DECL. If TAG_NAME_P is
22869 true, the DECL indicates the class being defined in a class-head,
22870 or declared in an elaborated-type-specifier.
22872 Otherwise, return DECL. */
22874 static tree
22875 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22877 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22878 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22880 struct A {
22881 template <typename T> struct B;
22884 template <typename T> struct A::B {};
22886 Similarly, in an elaborated-type-specifier:
22888 namespace N { struct X{}; }
22890 struct A {
22891 template <typename T> friend struct N::X;
22894 However, if the DECL refers to a class type, and we are in
22895 the scope of the class, then the name lookup automatically
22896 finds the TYPE_DECL created by build_self_reference rather
22897 than a TEMPLATE_DECL. For example, in:
22899 template <class T> struct S {
22900 S s;
22903 there is no need to handle such case. */
22905 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22906 return DECL_TEMPLATE_RESULT (decl);
22908 return decl;
22911 /* If too many, or too few, template-parameter lists apply to the
22912 declarator, issue an error message. Returns TRUE if all went well,
22913 and FALSE otherwise. */
22915 static bool
22916 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22917 cp_declarator *declarator,
22918 location_t declarator_location)
22920 switch (declarator->kind)
22922 case cdk_id:
22924 unsigned num_templates = 0;
22925 tree scope = declarator->u.id.qualifying_scope;
22927 if (scope)
22928 num_templates = num_template_headers_for_class (scope);
22929 else if (TREE_CODE (declarator->u.id.unqualified_name)
22930 == TEMPLATE_ID_EXPR)
22931 /* If the DECLARATOR has the form `X<y>' then it uses one
22932 additional level of template parameters. */
22933 ++num_templates;
22935 return cp_parser_check_template_parameters
22936 (parser, num_templates, declarator_location, declarator);
22939 case cdk_function:
22940 case cdk_array:
22941 case cdk_pointer:
22942 case cdk_reference:
22943 case cdk_ptrmem:
22944 return (cp_parser_check_declarator_template_parameters
22945 (parser, declarator->declarator, declarator_location));
22947 case cdk_error:
22948 return true;
22950 default:
22951 gcc_unreachable ();
22953 return false;
22956 /* NUM_TEMPLATES were used in the current declaration. If that is
22957 invalid, return FALSE and issue an error messages. Otherwise,
22958 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22959 declarator and we can print more accurate diagnostics. */
22961 static bool
22962 cp_parser_check_template_parameters (cp_parser* parser,
22963 unsigned num_templates,
22964 location_t location,
22965 cp_declarator *declarator)
22967 /* If there are the same number of template classes and parameter
22968 lists, that's OK. */
22969 if (parser->num_template_parameter_lists == num_templates)
22970 return true;
22971 /* If there are more, but only one more, then we are referring to a
22972 member template. That's OK too. */
22973 if (parser->num_template_parameter_lists == num_templates + 1)
22974 return true;
22975 /* If there are more template classes than parameter lists, we have
22976 something like:
22978 template <class T> void S<T>::R<T>::f (); */
22979 if (parser->num_template_parameter_lists < num_templates)
22981 if (declarator && !current_function_decl)
22982 error_at (location, "specializing member %<%T::%E%> "
22983 "requires %<template<>%> syntax",
22984 declarator->u.id.qualifying_scope,
22985 declarator->u.id.unqualified_name);
22986 else if (declarator)
22987 error_at (location, "invalid declaration of %<%T::%E%>",
22988 declarator->u.id.qualifying_scope,
22989 declarator->u.id.unqualified_name);
22990 else
22991 error_at (location, "too few template-parameter-lists");
22992 return false;
22994 /* Otherwise, there are too many template parameter lists. We have
22995 something like:
22997 template <class T> template <class U> void S::f(); */
22998 error_at (location, "too many template-parameter-lists");
22999 return false;
23002 /* Parse an optional `::' token indicating that the following name is
23003 from the global namespace. If so, PARSER->SCOPE is set to the
23004 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23005 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23006 Returns the new value of PARSER->SCOPE, if the `::' token is
23007 present, and NULL_TREE otherwise. */
23009 static tree
23010 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23012 cp_token *token;
23014 /* Peek at the next token. */
23015 token = cp_lexer_peek_token (parser->lexer);
23016 /* If we're looking at a `::' token then we're starting from the
23017 global namespace, not our current location. */
23018 if (token->type == CPP_SCOPE)
23020 /* Consume the `::' token. */
23021 cp_lexer_consume_token (parser->lexer);
23022 /* Set the SCOPE so that we know where to start the lookup. */
23023 parser->scope = global_namespace;
23024 parser->qualifying_scope = global_namespace;
23025 parser->object_scope = NULL_TREE;
23027 return parser->scope;
23029 else if (!current_scope_valid_p)
23031 parser->scope = NULL_TREE;
23032 parser->qualifying_scope = NULL_TREE;
23033 parser->object_scope = NULL_TREE;
23036 return NULL_TREE;
23039 /* Returns TRUE if the upcoming token sequence is the start of a
23040 constructor declarator. If FRIEND_P is true, the declarator is
23041 preceded by the `friend' specifier. */
23043 static bool
23044 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23046 bool constructor_p;
23047 bool outside_class_specifier_p;
23048 tree nested_name_specifier;
23049 cp_token *next_token;
23051 /* The common case is that this is not a constructor declarator, so
23052 try to avoid doing lots of work if at all possible. It's not
23053 valid declare a constructor at function scope. */
23054 if (parser->in_function_body)
23055 return false;
23056 /* And only certain tokens can begin a constructor declarator. */
23057 next_token = cp_lexer_peek_token (parser->lexer);
23058 if (next_token->type != CPP_NAME
23059 && next_token->type != CPP_SCOPE
23060 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23061 && next_token->type != CPP_TEMPLATE_ID)
23062 return false;
23064 /* Parse tentatively; we are going to roll back all of the tokens
23065 consumed here. */
23066 cp_parser_parse_tentatively (parser);
23067 /* Assume that we are looking at a constructor declarator. */
23068 constructor_p = true;
23070 /* Look for the optional `::' operator. */
23071 cp_parser_global_scope_opt (parser,
23072 /*current_scope_valid_p=*/false);
23073 /* Look for the nested-name-specifier. */
23074 nested_name_specifier
23075 = (cp_parser_nested_name_specifier_opt (parser,
23076 /*typename_keyword_p=*/false,
23077 /*check_dependency_p=*/false,
23078 /*type_p=*/false,
23079 /*is_declaration=*/false));
23081 outside_class_specifier_p = (!at_class_scope_p ()
23082 || !TYPE_BEING_DEFINED (current_class_type)
23083 || friend_p);
23085 /* Outside of a class-specifier, there must be a
23086 nested-name-specifier. */
23087 if (!nested_name_specifier && outside_class_specifier_p)
23088 constructor_p = false;
23089 else if (nested_name_specifier == error_mark_node)
23090 constructor_p = false;
23092 /* If we have a class scope, this is easy; DR 147 says that S::S always
23093 names the constructor, and no other qualified name could. */
23094 if (constructor_p && nested_name_specifier
23095 && CLASS_TYPE_P (nested_name_specifier))
23097 tree id = cp_parser_unqualified_id (parser,
23098 /*template_keyword_p=*/false,
23099 /*check_dependency_p=*/false,
23100 /*declarator_p=*/true,
23101 /*optional_p=*/false);
23102 if (is_overloaded_fn (id))
23103 id = DECL_NAME (get_first_fn (id));
23104 if (!constructor_name_p (id, nested_name_specifier))
23105 constructor_p = false;
23107 /* If we still think that this might be a constructor-declarator,
23108 look for a class-name. */
23109 else if (constructor_p)
23111 /* If we have:
23113 template <typename T> struct S {
23114 S();
23117 we must recognize that the nested `S' names a class. */
23118 tree type_decl;
23119 type_decl = cp_parser_class_name (parser,
23120 /*typename_keyword_p=*/false,
23121 /*template_keyword_p=*/false,
23122 none_type,
23123 /*check_dependency_p=*/false,
23124 /*class_head_p=*/false,
23125 /*is_declaration=*/false);
23126 /* If there was no class-name, then this is not a constructor.
23127 Otherwise, if we are in a class-specifier and we aren't
23128 handling a friend declaration, check that its type matches
23129 current_class_type (c++/38313). Note: error_mark_node
23130 is left alone for error recovery purposes. */
23131 constructor_p = (!cp_parser_error_occurred (parser)
23132 && (outside_class_specifier_p
23133 || type_decl == error_mark_node
23134 || same_type_p (current_class_type,
23135 TREE_TYPE (type_decl))));
23137 /* If we're still considering a constructor, we have to see a `(',
23138 to begin the parameter-declaration-clause, followed by either a
23139 `)', an `...', or a decl-specifier. We need to check for a
23140 type-specifier to avoid being fooled into thinking that:
23142 S (f) (int);
23144 is a constructor. (It is actually a function named `f' that
23145 takes one parameter (of type `int') and returns a value of type
23146 `S'. */
23147 if (constructor_p
23148 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23149 constructor_p = false;
23151 if (constructor_p
23152 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23153 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23154 /* A parameter declaration begins with a decl-specifier,
23155 which is either the "attribute" keyword, a storage class
23156 specifier, or (usually) a type-specifier. */
23157 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23159 tree type;
23160 tree pushed_scope = NULL_TREE;
23161 unsigned saved_num_template_parameter_lists;
23163 /* Names appearing in the type-specifier should be looked up
23164 in the scope of the class. */
23165 if (current_class_type)
23166 type = NULL_TREE;
23167 else
23169 type = TREE_TYPE (type_decl);
23170 if (TREE_CODE (type) == TYPENAME_TYPE)
23172 type = resolve_typename_type (type,
23173 /*only_current_p=*/false);
23174 if (TREE_CODE (type) == TYPENAME_TYPE)
23176 cp_parser_abort_tentative_parse (parser);
23177 return false;
23180 pushed_scope = push_scope (type);
23183 /* Inside the constructor parameter list, surrounding
23184 template-parameter-lists do not apply. */
23185 saved_num_template_parameter_lists
23186 = parser->num_template_parameter_lists;
23187 parser->num_template_parameter_lists = 0;
23189 /* Look for the type-specifier. */
23190 cp_parser_type_specifier (parser,
23191 CP_PARSER_FLAGS_NONE,
23192 /*decl_specs=*/NULL,
23193 /*is_declarator=*/true,
23194 /*declares_class_or_enum=*/NULL,
23195 /*is_cv_qualifier=*/NULL);
23197 parser->num_template_parameter_lists
23198 = saved_num_template_parameter_lists;
23200 /* Leave the scope of the class. */
23201 if (pushed_scope)
23202 pop_scope (pushed_scope);
23204 constructor_p = !cp_parser_error_occurred (parser);
23208 /* We did not really want to consume any tokens. */
23209 cp_parser_abort_tentative_parse (parser);
23211 return constructor_p;
23214 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23215 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23216 they must be performed once we are in the scope of the function.
23218 Returns the function defined. */
23220 static tree
23221 cp_parser_function_definition_from_specifiers_and_declarator
23222 (cp_parser* parser,
23223 cp_decl_specifier_seq *decl_specifiers,
23224 tree attributes,
23225 const cp_declarator *declarator)
23227 tree fn;
23228 bool success_p;
23230 /* Begin the function-definition. */
23231 success_p = start_function (decl_specifiers, declarator, attributes);
23233 /* The things we're about to see are not directly qualified by any
23234 template headers we've seen thus far. */
23235 reset_specialization ();
23237 /* If there were names looked up in the decl-specifier-seq that we
23238 did not check, check them now. We must wait until we are in the
23239 scope of the function to perform the checks, since the function
23240 might be a friend. */
23241 perform_deferred_access_checks (tf_warning_or_error);
23243 if (success_p)
23245 cp_finalize_omp_declare_simd (parser, current_function_decl);
23246 parser->omp_declare_simd = NULL;
23249 if (!success_p)
23251 /* Skip the entire function. */
23252 cp_parser_skip_to_end_of_block_or_statement (parser);
23253 fn = error_mark_node;
23255 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23257 /* Seen already, skip it. An error message has already been output. */
23258 cp_parser_skip_to_end_of_block_or_statement (parser);
23259 fn = current_function_decl;
23260 current_function_decl = NULL_TREE;
23261 /* If this is a function from a class, pop the nested class. */
23262 if (current_class_name)
23263 pop_nested_class ();
23265 else
23267 timevar_id_t tv;
23268 if (DECL_DECLARED_INLINE_P (current_function_decl))
23269 tv = TV_PARSE_INLINE;
23270 else
23271 tv = TV_PARSE_FUNC;
23272 timevar_push (tv);
23273 fn = cp_parser_function_definition_after_declarator (parser,
23274 /*inline_p=*/false);
23275 timevar_pop (tv);
23278 return fn;
23281 /* Parse the part of a function-definition that follows the
23282 declarator. INLINE_P is TRUE iff this function is an inline
23283 function defined within a class-specifier.
23285 Returns the function defined. */
23287 static tree
23288 cp_parser_function_definition_after_declarator (cp_parser* parser,
23289 bool inline_p)
23291 tree fn;
23292 bool ctor_initializer_p = false;
23293 bool saved_in_unbraced_linkage_specification_p;
23294 bool saved_in_function_body;
23295 unsigned saved_num_template_parameter_lists;
23296 cp_token *token;
23297 bool fully_implicit_function_template_p
23298 = parser->fully_implicit_function_template_p;
23299 parser->fully_implicit_function_template_p = false;
23300 tree implicit_template_parms
23301 = parser->implicit_template_parms;
23302 parser->implicit_template_parms = 0;
23303 cp_binding_level* implicit_template_scope
23304 = parser->implicit_template_scope;
23305 parser->implicit_template_scope = 0;
23307 saved_in_function_body = parser->in_function_body;
23308 parser->in_function_body = true;
23309 /* If the next token is `return', then the code may be trying to
23310 make use of the "named return value" extension that G++ used to
23311 support. */
23312 token = cp_lexer_peek_token (parser->lexer);
23313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23315 /* Consume the `return' keyword. */
23316 cp_lexer_consume_token (parser->lexer);
23317 /* Look for the identifier that indicates what value is to be
23318 returned. */
23319 cp_parser_identifier (parser);
23320 /* Issue an error message. */
23321 error_at (token->location,
23322 "named return values are no longer supported");
23323 /* Skip tokens until we reach the start of the function body. */
23324 while (true)
23326 cp_token *token = cp_lexer_peek_token (parser->lexer);
23327 if (token->type == CPP_OPEN_BRACE
23328 || token->type == CPP_EOF
23329 || token->type == CPP_PRAGMA_EOL)
23330 break;
23331 cp_lexer_consume_token (parser->lexer);
23334 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23335 anything declared inside `f'. */
23336 saved_in_unbraced_linkage_specification_p
23337 = parser->in_unbraced_linkage_specification_p;
23338 parser->in_unbraced_linkage_specification_p = false;
23339 /* Inside the function, surrounding template-parameter-lists do not
23340 apply. */
23341 saved_num_template_parameter_lists
23342 = parser->num_template_parameter_lists;
23343 parser->num_template_parameter_lists = 0;
23345 start_lambda_scope (current_function_decl);
23347 /* If the next token is `try', `__transaction_atomic', or
23348 `__transaction_relaxed`, then we are looking at either function-try-block
23349 or function-transaction-block. Note that all of these include the
23350 function-body. */
23351 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23352 ctor_initializer_p = cp_parser_function_transaction (parser,
23353 RID_TRANSACTION_ATOMIC);
23354 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23355 RID_TRANSACTION_RELAXED))
23356 ctor_initializer_p = cp_parser_function_transaction (parser,
23357 RID_TRANSACTION_RELAXED);
23358 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23359 ctor_initializer_p = cp_parser_function_try_block (parser);
23360 else
23361 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23362 (parser, /*in_function_try_block=*/false);
23364 finish_lambda_scope ();
23366 /* Finish the function. */
23367 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23368 (inline_p ? 2 : 0));
23369 /* Generate code for it, if necessary. */
23370 expand_or_defer_fn (fn);
23371 /* Restore the saved values. */
23372 parser->in_unbraced_linkage_specification_p
23373 = saved_in_unbraced_linkage_specification_p;
23374 parser->num_template_parameter_lists
23375 = saved_num_template_parameter_lists;
23376 parser->in_function_body = saved_in_function_body;
23378 parser->fully_implicit_function_template_p
23379 = fully_implicit_function_template_p;
23380 parser->implicit_template_parms
23381 = implicit_template_parms;
23382 parser->implicit_template_scope
23383 = implicit_template_scope;
23385 if (parser->fully_implicit_function_template_p)
23386 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23388 return fn;
23391 /* Parse a template-declaration, assuming that the `export' (and
23392 `extern') keywords, if present, has already been scanned. MEMBER_P
23393 is as for cp_parser_template_declaration. */
23395 static void
23396 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23398 tree decl = NULL_TREE;
23399 vec<deferred_access_check, va_gc> *checks;
23400 tree parameter_list;
23401 bool friend_p = false;
23402 bool need_lang_pop;
23403 cp_token *token;
23405 /* Look for the `template' keyword. */
23406 token = cp_lexer_peek_token (parser->lexer);
23407 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23408 return;
23410 /* And the `<'. */
23411 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23412 return;
23413 if (at_class_scope_p () && current_function_decl)
23415 /* 14.5.2.2 [temp.mem]
23417 A local class shall not have member templates. */
23418 error_at (token->location,
23419 "invalid declaration of member template in local class");
23420 cp_parser_skip_to_end_of_block_or_statement (parser);
23421 return;
23423 /* [temp]
23425 A template ... shall not have C linkage. */
23426 if (current_lang_name == lang_name_c)
23428 error_at (token->location, "template with C linkage");
23429 /* Give it C++ linkage to avoid confusing other parts of the
23430 front end. */
23431 push_lang_context (lang_name_cplusplus);
23432 need_lang_pop = true;
23434 else
23435 need_lang_pop = false;
23437 /* We cannot perform access checks on the template parameter
23438 declarations until we know what is being declared, just as we
23439 cannot check the decl-specifier list. */
23440 push_deferring_access_checks (dk_deferred);
23442 /* If the next token is `>', then we have an invalid
23443 specialization. Rather than complain about an invalid template
23444 parameter, issue an error message here. */
23445 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23447 cp_parser_error (parser, "invalid explicit specialization");
23448 begin_specialization ();
23449 parameter_list = NULL_TREE;
23451 else
23453 /* Parse the template parameters. */
23454 parameter_list = cp_parser_template_parameter_list (parser);
23457 /* Get the deferred access checks from the parameter list. These
23458 will be checked once we know what is being declared, as for a
23459 member template the checks must be performed in the scope of the
23460 class containing the member. */
23461 checks = get_deferred_access_checks ();
23463 /* Look for the `>'. */
23464 cp_parser_skip_to_end_of_template_parameter_list (parser);
23465 /* We just processed one more parameter list. */
23466 ++parser->num_template_parameter_lists;
23467 /* If the next token is `template', there are more template
23468 parameters. */
23469 if (cp_lexer_next_token_is_keyword (parser->lexer,
23470 RID_TEMPLATE))
23471 cp_parser_template_declaration_after_export (parser, member_p);
23472 else if (cxx_dialect >= cxx11
23473 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23474 decl = cp_parser_alias_declaration (parser);
23475 else
23477 /* There are no access checks when parsing a template, as we do not
23478 know if a specialization will be a friend. */
23479 push_deferring_access_checks (dk_no_check);
23480 token = cp_lexer_peek_token (parser->lexer);
23481 decl = cp_parser_single_declaration (parser,
23482 checks,
23483 member_p,
23484 /*explicit_specialization_p=*/false,
23485 &friend_p);
23486 pop_deferring_access_checks ();
23488 /* If this is a member template declaration, let the front
23489 end know. */
23490 if (member_p && !friend_p && decl)
23492 if (TREE_CODE (decl) == TYPE_DECL)
23493 cp_parser_check_access_in_redeclaration (decl, token->location);
23495 decl = finish_member_template_decl (decl);
23497 else if (friend_p && decl
23498 && DECL_DECLARES_TYPE_P (decl))
23499 make_friend_class (current_class_type, TREE_TYPE (decl),
23500 /*complain=*/true);
23502 /* We are done with the current parameter list. */
23503 --parser->num_template_parameter_lists;
23505 pop_deferring_access_checks ();
23507 /* Finish up. */
23508 finish_template_decl (parameter_list);
23510 /* Check the template arguments for a literal operator template. */
23511 if (decl
23512 && DECL_DECLARES_FUNCTION_P (decl)
23513 && UDLIT_OPER_P (DECL_NAME (decl)))
23515 bool ok = true;
23516 if (parameter_list == NULL_TREE)
23517 ok = false;
23518 else
23520 int num_parms = TREE_VEC_LENGTH (parameter_list);
23521 if (num_parms == 1)
23523 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23524 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23525 if (TREE_TYPE (parm) != char_type_node
23526 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23527 ok = false;
23529 else if (num_parms == 2 && cxx_dialect >= cxx14)
23531 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23532 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23533 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23534 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23535 if (TREE_TYPE (parm) != TREE_TYPE (type)
23536 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23537 ok = false;
23539 else
23540 ok = false;
23542 if (!ok)
23544 if (cxx_dialect >= cxx14)
23545 error ("literal operator template %qD has invalid parameter list."
23546 " Expected non-type template argument pack <char...>"
23547 " or <typename CharT, CharT...>",
23548 decl);
23549 else
23550 error ("literal operator template %qD has invalid parameter list."
23551 " Expected non-type template argument pack <char...>",
23552 decl);
23555 /* Register member declarations. */
23556 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23557 finish_member_declaration (decl);
23558 /* For the erroneous case of a template with C linkage, we pushed an
23559 implicit C++ linkage scope; exit that scope now. */
23560 if (need_lang_pop)
23561 pop_lang_context ();
23562 /* If DECL is a function template, we must return to parse it later.
23563 (Even though there is no definition, there might be default
23564 arguments that need handling.) */
23565 if (member_p && decl
23566 && DECL_DECLARES_FUNCTION_P (decl))
23567 vec_safe_push (unparsed_funs_with_definitions, decl);
23570 /* Perform the deferred access checks from a template-parameter-list.
23571 CHECKS is a TREE_LIST of access checks, as returned by
23572 get_deferred_access_checks. */
23574 static void
23575 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23577 ++processing_template_parmlist;
23578 perform_access_checks (checks, tf_warning_or_error);
23579 --processing_template_parmlist;
23582 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23583 `function-definition' sequence that follows a template header.
23584 If MEMBER_P is true, this declaration appears in a class scope.
23586 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23587 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23589 static tree
23590 cp_parser_single_declaration (cp_parser* parser,
23591 vec<deferred_access_check, va_gc> *checks,
23592 bool member_p,
23593 bool explicit_specialization_p,
23594 bool* friend_p)
23596 int declares_class_or_enum;
23597 tree decl = NULL_TREE;
23598 cp_decl_specifier_seq decl_specifiers;
23599 bool function_definition_p = false;
23600 cp_token *decl_spec_token_start;
23602 /* This function is only used when processing a template
23603 declaration. */
23604 gcc_assert (innermost_scope_kind () == sk_template_parms
23605 || innermost_scope_kind () == sk_template_spec);
23607 /* Defer access checks until we know what is being declared. */
23608 push_deferring_access_checks (dk_deferred);
23610 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23611 alternative. */
23612 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23613 cp_parser_decl_specifier_seq (parser,
23614 CP_PARSER_FLAGS_OPTIONAL,
23615 &decl_specifiers,
23616 &declares_class_or_enum);
23617 if (friend_p)
23618 *friend_p = cp_parser_friend_p (&decl_specifiers);
23620 /* There are no template typedefs. */
23621 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23623 error_at (decl_spec_token_start->location,
23624 "template declaration of %<typedef%>");
23625 decl = error_mark_node;
23628 /* Gather up the access checks that occurred the
23629 decl-specifier-seq. */
23630 stop_deferring_access_checks ();
23632 /* Check for the declaration of a template class. */
23633 if (declares_class_or_enum)
23635 if (cp_parser_declares_only_class_p (parser))
23637 decl = shadow_tag (&decl_specifiers);
23639 /* In this case:
23641 struct C {
23642 friend template <typename T> struct A<T>::B;
23645 A<T>::B will be represented by a TYPENAME_TYPE, and
23646 therefore not recognized by shadow_tag. */
23647 if (friend_p && *friend_p
23648 && !decl
23649 && decl_specifiers.type
23650 && TYPE_P (decl_specifiers.type))
23651 decl = decl_specifiers.type;
23653 if (decl && decl != error_mark_node)
23654 decl = TYPE_NAME (decl);
23655 else
23656 decl = error_mark_node;
23658 /* Perform access checks for template parameters. */
23659 cp_parser_perform_template_parameter_access_checks (checks);
23663 /* Complain about missing 'typename' or other invalid type names. */
23664 if (!decl_specifiers.any_type_specifiers_p
23665 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23667 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23668 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23669 the rest of this declaration. */
23670 decl = error_mark_node;
23671 goto out;
23674 /* If it's not a template class, try for a template function. If
23675 the next token is a `;', then this declaration does not declare
23676 anything. But, if there were errors in the decl-specifiers, then
23677 the error might well have come from an attempted class-specifier.
23678 In that case, there's no need to warn about a missing declarator. */
23679 if (!decl
23680 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23681 || decl_specifiers.type != error_mark_node))
23683 decl = cp_parser_init_declarator (parser,
23684 &decl_specifiers,
23685 checks,
23686 /*function_definition_allowed_p=*/true,
23687 member_p,
23688 declares_class_or_enum,
23689 &function_definition_p,
23690 NULL);
23692 /* 7.1.1-1 [dcl.stc]
23694 A storage-class-specifier shall not be specified in an explicit
23695 specialization... */
23696 if (decl
23697 && explicit_specialization_p
23698 && decl_specifiers.storage_class != sc_none)
23700 error_at (decl_spec_token_start->location,
23701 "explicit template specialization cannot have a storage class");
23702 decl = error_mark_node;
23705 if (decl && VAR_P (decl))
23706 check_template_variable (decl);
23709 /* Look for a trailing `;' after the declaration. */
23710 if (!function_definition_p
23711 && (decl == error_mark_node
23712 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23713 cp_parser_skip_to_end_of_block_or_statement (parser);
23715 out:
23716 pop_deferring_access_checks ();
23718 /* Clear any current qualification; whatever comes next is the start
23719 of something new. */
23720 parser->scope = NULL_TREE;
23721 parser->qualifying_scope = NULL_TREE;
23722 parser->object_scope = NULL_TREE;
23724 return decl;
23727 /* Parse a cast-expression that is not the operand of a unary "&". */
23729 static tree
23730 cp_parser_simple_cast_expression (cp_parser *parser)
23732 return cp_parser_cast_expression (parser, /*address_p=*/false,
23733 /*cast_p=*/false, /*decltype*/false, NULL);
23736 /* Parse a functional cast to TYPE. Returns an expression
23737 representing the cast. */
23739 static tree
23740 cp_parser_functional_cast (cp_parser* parser, tree type)
23742 vec<tree, va_gc> *vec;
23743 tree expression_list;
23744 tree cast;
23745 bool nonconst_p;
23747 if (!type)
23748 type = error_mark_node;
23750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23752 cp_lexer_set_source_position (parser->lexer);
23753 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23754 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23755 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23756 if (TREE_CODE (type) == TYPE_DECL)
23757 type = TREE_TYPE (type);
23758 return finish_compound_literal (type, expression_list,
23759 tf_warning_or_error);
23763 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23764 /*cast_p=*/true,
23765 /*allow_expansion_p=*/true,
23766 /*non_constant_p=*/NULL);
23767 if (vec == NULL)
23768 expression_list = error_mark_node;
23769 else
23771 expression_list = build_tree_list_vec (vec);
23772 release_tree_vector (vec);
23775 cast = build_functional_cast (type, expression_list,
23776 tf_warning_or_error);
23777 /* [expr.const]/1: In an integral constant expression "only type
23778 conversions to integral or enumeration type can be used". */
23779 if (TREE_CODE (type) == TYPE_DECL)
23780 type = TREE_TYPE (type);
23781 if (cast != error_mark_node
23782 && !cast_valid_in_integral_constant_expression_p (type)
23783 && cp_parser_non_integral_constant_expression (parser,
23784 NIC_CONSTRUCTOR))
23785 return error_mark_node;
23786 return cast;
23789 /* Save the tokens that make up the body of a member function defined
23790 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23791 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23792 specifiers applied to the declaration. Returns the FUNCTION_DECL
23793 for the member function. */
23795 static tree
23796 cp_parser_save_member_function_body (cp_parser* parser,
23797 cp_decl_specifier_seq *decl_specifiers,
23798 cp_declarator *declarator,
23799 tree attributes)
23801 cp_token *first;
23802 cp_token *last;
23803 tree fn;
23805 /* Create the FUNCTION_DECL. */
23806 fn = grokmethod (decl_specifiers, declarator, attributes);
23807 cp_finalize_omp_declare_simd (parser, fn);
23808 /* If something went badly wrong, bail out now. */
23809 if (fn == error_mark_node)
23811 /* If there's a function-body, skip it. */
23812 if (cp_parser_token_starts_function_definition_p
23813 (cp_lexer_peek_token (parser->lexer)))
23814 cp_parser_skip_to_end_of_block_or_statement (parser);
23815 return error_mark_node;
23818 /* Remember it, if there default args to post process. */
23819 cp_parser_save_default_args (parser, fn);
23821 /* Save away the tokens that make up the body of the
23822 function. */
23823 first = parser->lexer->next_token;
23824 /* Handle function try blocks. */
23825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23826 cp_lexer_consume_token (parser->lexer);
23827 /* We can have braced-init-list mem-initializers before the fn body. */
23828 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23830 cp_lexer_consume_token (parser->lexer);
23831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23833 /* cache_group will stop after an un-nested { } pair, too. */
23834 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23835 break;
23837 /* variadic mem-inits have ... after the ')'. */
23838 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23839 cp_lexer_consume_token (parser->lexer);
23842 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23843 /* Handle function try blocks. */
23844 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23845 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23846 last = parser->lexer->next_token;
23848 /* Save away the inline definition; we will process it when the
23849 class is complete. */
23850 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23851 DECL_PENDING_INLINE_P (fn) = 1;
23853 /* We need to know that this was defined in the class, so that
23854 friend templates are handled correctly. */
23855 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23857 /* Add FN to the queue of functions to be parsed later. */
23858 vec_safe_push (unparsed_funs_with_definitions, fn);
23860 return fn;
23863 /* Save the tokens that make up the in-class initializer for a non-static
23864 data member. Returns a DEFAULT_ARG. */
23866 static tree
23867 cp_parser_save_nsdmi (cp_parser* parser)
23869 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23872 /* Parse a template-argument-list, as well as the trailing ">" (but
23873 not the opening "<"). See cp_parser_template_argument_list for the
23874 return value. */
23876 static tree
23877 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23879 tree arguments;
23880 tree saved_scope;
23881 tree saved_qualifying_scope;
23882 tree saved_object_scope;
23883 bool saved_greater_than_is_operator_p;
23884 int saved_unevaluated_operand;
23885 int saved_inhibit_evaluation_warnings;
23887 /* [temp.names]
23889 When parsing a template-id, the first non-nested `>' is taken as
23890 the end of the template-argument-list rather than a greater-than
23891 operator. */
23892 saved_greater_than_is_operator_p
23893 = parser->greater_than_is_operator_p;
23894 parser->greater_than_is_operator_p = false;
23895 /* Parsing the argument list may modify SCOPE, so we save it
23896 here. */
23897 saved_scope = parser->scope;
23898 saved_qualifying_scope = parser->qualifying_scope;
23899 saved_object_scope = parser->object_scope;
23900 /* We need to evaluate the template arguments, even though this
23901 template-id may be nested within a "sizeof". */
23902 saved_unevaluated_operand = cp_unevaluated_operand;
23903 cp_unevaluated_operand = 0;
23904 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23905 c_inhibit_evaluation_warnings = 0;
23906 /* Parse the template-argument-list itself. */
23907 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23908 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23909 arguments = NULL_TREE;
23910 else
23911 arguments = cp_parser_template_argument_list (parser);
23912 /* Look for the `>' that ends the template-argument-list. If we find
23913 a '>>' instead, it's probably just a typo. */
23914 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23916 if (cxx_dialect != cxx98)
23918 /* In C++0x, a `>>' in a template argument list or cast
23919 expression is considered to be two separate `>'
23920 tokens. So, change the current token to a `>', but don't
23921 consume it: it will be consumed later when the outer
23922 template argument list (or cast expression) is parsed.
23923 Note that this replacement of `>' for `>>' is necessary
23924 even if we are parsing tentatively: in the tentative
23925 case, after calling
23926 cp_parser_enclosed_template_argument_list we will always
23927 throw away all of the template arguments and the first
23928 closing `>', either because the template argument list
23929 was erroneous or because we are replacing those tokens
23930 with a CPP_TEMPLATE_ID token. The second `>' (which will
23931 not have been thrown away) is needed either to close an
23932 outer template argument list or to complete a new-style
23933 cast. */
23934 cp_token *token = cp_lexer_peek_token (parser->lexer);
23935 token->type = CPP_GREATER;
23937 else if (!saved_greater_than_is_operator_p)
23939 /* If we're in a nested template argument list, the '>>' has
23940 to be a typo for '> >'. We emit the error message, but we
23941 continue parsing and we push a '>' as next token, so that
23942 the argument list will be parsed correctly. Note that the
23943 global source location is still on the token before the
23944 '>>', so we need to say explicitly where we want it. */
23945 cp_token *token = cp_lexer_peek_token (parser->lexer);
23946 error_at (token->location, "%<>>%> should be %<> >%> "
23947 "within a nested template argument list");
23949 token->type = CPP_GREATER;
23951 else
23953 /* If this is not a nested template argument list, the '>>'
23954 is a typo for '>'. Emit an error message and continue.
23955 Same deal about the token location, but here we can get it
23956 right by consuming the '>>' before issuing the diagnostic. */
23957 cp_token *token = cp_lexer_consume_token (parser->lexer);
23958 error_at (token->location,
23959 "spurious %<>>%>, use %<>%> to terminate "
23960 "a template argument list");
23963 else
23964 cp_parser_skip_to_end_of_template_parameter_list (parser);
23965 /* The `>' token might be a greater-than operator again now. */
23966 parser->greater_than_is_operator_p
23967 = saved_greater_than_is_operator_p;
23968 /* Restore the SAVED_SCOPE. */
23969 parser->scope = saved_scope;
23970 parser->qualifying_scope = saved_qualifying_scope;
23971 parser->object_scope = saved_object_scope;
23972 cp_unevaluated_operand = saved_unevaluated_operand;
23973 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23975 return arguments;
23978 /* MEMBER_FUNCTION is a member function, or a friend. If default
23979 arguments, or the body of the function have not yet been parsed,
23980 parse them now. */
23982 static void
23983 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23985 timevar_push (TV_PARSE_INMETH);
23986 /* If this member is a template, get the underlying
23987 FUNCTION_DECL. */
23988 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23989 member_function = DECL_TEMPLATE_RESULT (member_function);
23991 /* There should not be any class definitions in progress at this
23992 point; the bodies of members are only parsed outside of all class
23993 definitions. */
23994 gcc_assert (parser->num_classes_being_defined == 0);
23995 /* While we're parsing the member functions we might encounter more
23996 classes. We want to handle them right away, but we don't want
23997 them getting mixed up with functions that are currently in the
23998 queue. */
23999 push_unparsed_function_queues (parser);
24001 /* Make sure that any template parameters are in scope. */
24002 maybe_begin_member_template_processing (member_function);
24004 /* If the body of the function has not yet been parsed, parse it
24005 now. */
24006 if (DECL_PENDING_INLINE_P (member_function))
24008 tree function_scope;
24009 cp_token_cache *tokens;
24011 /* The function is no longer pending; we are processing it. */
24012 tokens = DECL_PENDING_INLINE_INFO (member_function);
24013 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24014 DECL_PENDING_INLINE_P (member_function) = 0;
24016 /* If this is a local class, enter the scope of the containing
24017 function. */
24018 function_scope = current_function_decl;
24019 if (function_scope)
24020 push_function_context ();
24022 /* Push the body of the function onto the lexer stack. */
24023 cp_parser_push_lexer_for_tokens (parser, tokens);
24025 /* Let the front end know that we going to be defining this
24026 function. */
24027 start_preparsed_function (member_function, NULL_TREE,
24028 SF_PRE_PARSED | SF_INCLASS_INLINE);
24030 /* Don't do access checking if it is a templated function. */
24031 if (processing_template_decl)
24032 push_deferring_access_checks (dk_no_check);
24034 /* #pragma omp declare reduction needs special parsing. */
24035 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24037 parser->lexer->in_pragma = true;
24038 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24039 finish_function (/*inline*/2);
24040 cp_check_omp_declare_reduction (member_function);
24042 else
24043 /* Now, parse the body of the function. */
24044 cp_parser_function_definition_after_declarator (parser,
24045 /*inline_p=*/true);
24047 if (processing_template_decl)
24048 pop_deferring_access_checks ();
24050 /* Leave the scope of the containing function. */
24051 if (function_scope)
24052 pop_function_context ();
24053 cp_parser_pop_lexer (parser);
24056 /* Remove any template parameters from the symbol table. */
24057 maybe_end_member_template_processing ();
24059 /* Restore the queue. */
24060 pop_unparsed_function_queues (parser);
24061 timevar_pop (TV_PARSE_INMETH);
24064 /* If DECL contains any default args, remember it on the unparsed
24065 functions queue. */
24067 static void
24068 cp_parser_save_default_args (cp_parser* parser, tree decl)
24070 tree probe;
24072 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24073 probe;
24074 probe = TREE_CHAIN (probe))
24075 if (TREE_PURPOSE (probe))
24077 cp_default_arg_entry entry = {current_class_type, decl};
24078 vec_safe_push (unparsed_funs_with_default_args, entry);
24079 break;
24083 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24084 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24085 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24086 from the parameter-type-list. */
24088 static tree
24089 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24090 tree default_arg, tree parmtype)
24092 cp_token_cache *tokens;
24093 tree parsed_arg;
24094 bool dummy;
24096 if (default_arg == error_mark_node)
24097 return error_mark_node;
24099 /* Push the saved tokens for the default argument onto the parser's
24100 lexer stack. */
24101 tokens = DEFARG_TOKENS (default_arg);
24102 cp_parser_push_lexer_for_tokens (parser, tokens);
24104 start_lambda_scope (decl);
24106 /* Parse the default argument. */
24107 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24108 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24111 finish_lambda_scope ();
24113 if (parsed_arg == error_mark_node)
24114 cp_parser_skip_to_end_of_statement (parser);
24116 if (!processing_template_decl)
24118 /* In a non-template class, check conversions now. In a template,
24119 we'll wait and instantiate these as needed. */
24120 if (TREE_CODE (decl) == PARM_DECL)
24121 parsed_arg = check_default_argument (parmtype, parsed_arg,
24122 tf_warning_or_error);
24123 else
24124 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24127 /* If the token stream has not been completely used up, then
24128 there was extra junk after the end of the default
24129 argument. */
24130 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24132 if (TREE_CODE (decl) == PARM_DECL)
24133 cp_parser_error (parser, "expected %<,%>");
24134 else
24135 cp_parser_error (parser, "expected %<;%>");
24138 /* Revert to the main lexer. */
24139 cp_parser_pop_lexer (parser);
24141 return parsed_arg;
24144 /* FIELD is a non-static data member with an initializer which we saved for
24145 later; parse it now. */
24147 static void
24148 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24150 tree def;
24152 maybe_begin_member_template_processing (field);
24154 push_unparsed_function_queues (parser);
24155 def = cp_parser_late_parse_one_default_arg (parser, field,
24156 DECL_INITIAL (field),
24157 NULL_TREE);
24158 pop_unparsed_function_queues (parser);
24160 maybe_end_member_template_processing ();
24162 DECL_INITIAL (field) = def;
24165 /* FN is a FUNCTION_DECL which may contains a parameter with an
24166 unparsed DEFAULT_ARG. Parse the default args now. This function
24167 assumes that the current scope is the scope in which the default
24168 argument should be processed. */
24170 static void
24171 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24173 bool saved_local_variables_forbidden_p;
24174 tree parm, parmdecl;
24176 /* While we're parsing the default args, we might (due to the
24177 statement expression extension) encounter more classes. We want
24178 to handle them right away, but we don't want them getting mixed
24179 up with default args that are currently in the queue. */
24180 push_unparsed_function_queues (parser);
24182 /* Local variable names (and the `this' keyword) may not appear
24183 in a default argument. */
24184 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24185 parser->local_variables_forbidden_p = true;
24187 push_defarg_context (fn);
24189 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24190 parmdecl = DECL_ARGUMENTS (fn);
24191 parm && parm != void_list_node;
24192 parm = TREE_CHAIN (parm),
24193 parmdecl = DECL_CHAIN (parmdecl))
24195 tree default_arg = TREE_PURPOSE (parm);
24196 tree parsed_arg;
24197 vec<tree, va_gc> *insts;
24198 tree copy;
24199 unsigned ix;
24201 if (!default_arg)
24202 continue;
24204 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24205 /* This can happen for a friend declaration for a function
24206 already declared with default arguments. */
24207 continue;
24209 parsed_arg
24210 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24211 default_arg,
24212 TREE_VALUE (parm));
24213 if (parsed_arg == error_mark_node)
24215 continue;
24218 TREE_PURPOSE (parm) = parsed_arg;
24220 /* Update any instantiations we've already created. */
24221 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24222 vec_safe_iterate (insts, ix, &copy); ix++)
24223 TREE_PURPOSE (copy) = parsed_arg;
24226 pop_defarg_context ();
24228 /* Make sure no default arg is missing. */
24229 check_default_args (fn);
24231 /* Restore the state of local_variables_forbidden_p. */
24232 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24234 /* Restore the queue. */
24235 pop_unparsed_function_queues (parser);
24238 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24240 sizeof ... ( identifier )
24242 where the 'sizeof' token has already been consumed. */
24244 static tree
24245 cp_parser_sizeof_pack (cp_parser *parser)
24247 /* Consume the `...'. */
24248 cp_lexer_consume_token (parser->lexer);
24249 maybe_warn_variadic_templates ();
24251 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24252 if (paren)
24253 cp_lexer_consume_token (parser->lexer);
24254 else
24255 permerror (cp_lexer_peek_token (parser->lexer)->location,
24256 "%<sizeof...%> argument must be surrounded by parentheses");
24258 cp_token *token = cp_lexer_peek_token (parser->lexer);
24259 tree name = cp_parser_identifier (parser);
24260 if (name == error_mark_node)
24261 return error_mark_node;
24262 /* The name is not qualified. */
24263 parser->scope = NULL_TREE;
24264 parser->qualifying_scope = NULL_TREE;
24265 parser->object_scope = NULL_TREE;
24266 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24267 if (expr == error_mark_node)
24268 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24269 token->location);
24270 if (TREE_CODE (expr) == TYPE_DECL)
24271 expr = TREE_TYPE (expr);
24272 else if (TREE_CODE (expr) == CONST_DECL)
24273 expr = DECL_INITIAL (expr);
24274 expr = make_pack_expansion (expr);
24276 if (paren)
24277 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24279 return expr;
24282 /* Parse the operand of `sizeof' (or a similar operator). Returns
24283 either a TYPE or an expression, depending on the form of the
24284 input. The KEYWORD indicates which kind of expression we have
24285 encountered. */
24287 static tree
24288 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24290 tree expr = NULL_TREE;
24291 const char *saved_message;
24292 char *tmp;
24293 bool saved_integral_constant_expression_p;
24294 bool saved_non_integral_constant_expression_p;
24296 /* If it's a `...', then we are computing the length of a parameter
24297 pack. */
24298 if (keyword == RID_SIZEOF
24299 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24300 return cp_parser_sizeof_pack (parser);
24302 /* Types cannot be defined in a `sizeof' expression. Save away the
24303 old message. */
24304 saved_message = parser->type_definition_forbidden_message;
24305 /* And create the new one. */
24306 tmp = concat ("types may not be defined in %<",
24307 IDENTIFIER_POINTER (ridpointers[keyword]),
24308 "%> expressions", NULL);
24309 parser->type_definition_forbidden_message = tmp;
24311 /* The restrictions on constant-expressions do not apply inside
24312 sizeof expressions. */
24313 saved_integral_constant_expression_p
24314 = parser->integral_constant_expression_p;
24315 saved_non_integral_constant_expression_p
24316 = parser->non_integral_constant_expression_p;
24317 parser->integral_constant_expression_p = false;
24319 /* Do not actually evaluate the expression. */
24320 ++cp_unevaluated_operand;
24321 ++c_inhibit_evaluation_warnings;
24322 /* If it's a `(', then we might be looking at the type-id
24323 construction. */
24324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24326 tree type = NULL_TREE;
24328 /* We can't be sure yet whether we're looking at a type-id or an
24329 expression. */
24330 cp_parser_parse_tentatively (parser);
24331 /* Note: as a GNU Extension, compound literals are considered
24332 postfix-expressions as they are in C99, so they are valid
24333 arguments to sizeof. See comment in cp_parser_cast_expression
24334 for details. */
24335 if (cp_parser_compound_literal_p (parser))
24336 cp_parser_simulate_error (parser);
24337 else
24339 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24340 parser->in_type_id_in_expr_p = true;
24341 /* Look for the type-id. */
24342 type = cp_parser_type_id (parser);
24343 /* Look for the closing `)'. */
24344 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24345 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24348 /* If all went well, then we're done. */
24349 if (cp_parser_parse_definitely (parser))
24351 cp_decl_specifier_seq decl_specs;
24353 /* Build a trivial decl-specifier-seq. */
24354 clear_decl_specs (&decl_specs);
24355 decl_specs.type = type;
24357 /* Call grokdeclarator to figure out what type this is. */
24358 expr = grokdeclarator (NULL,
24359 &decl_specs,
24360 TYPENAME,
24361 /*initialized=*/0,
24362 /*attrlist=*/NULL);
24366 /* If the type-id production did not work out, then we must be
24367 looking at the unary-expression production. */
24368 if (!expr)
24369 expr = cp_parser_unary_expression (parser);
24371 /* Go back to evaluating expressions. */
24372 --cp_unevaluated_operand;
24373 --c_inhibit_evaluation_warnings;
24375 /* Free the message we created. */
24376 free (tmp);
24377 /* And restore the old one. */
24378 parser->type_definition_forbidden_message = saved_message;
24379 parser->integral_constant_expression_p
24380 = saved_integral_constant_expression_p;
24381 parser->non_integral_constant_expression_p
24382 = saved_non_integral_constant_expression_p;
24384 return expr;
24387 /* If the current declaration has no declarator, return true. */
24389 static bool
24390 cp_parser_declares_only_class_p (cp_parser *parser)
24392 /* If the next token is a `;' or a `,' then there is no
24393 declarator. */
24394 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24395 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24398 /* Update the DECL_SPECS to reflect the storage class indicated by
24399 KEYWORD. */
24401 static void
24402 cp_parser_set_storage_class (cp_parser *parser,
24403 cp_decl_specifier_seq *decl_specs,
24404 enum rid keyword,
24405 cp_token *token)
24407 cp_storage_class storage_class;
24409 if (parser->in_unbraced_linkage_specification_p)
24411 error_at (token->location, "invalid use of %qD in linkage specification",
24412 ridpointers[keyword]);
24413 return;
24415 else if (decl_specs->storage_class != sc_none)
24417 decl_specs->conflicting_specifiers_p = true;
24418 return;
24421 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24422 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24423 && decl_specs->gnu_thread_keyword_p)
24425 pedwarn (decl_specs->locations[ds_thread], 0,
24426 "%<__thread%> before %qD", ridpointers[keyword]);
24429 switch (keyword)
24431 case RID_AUTO:
24432 storage_class = sc_auto;
24433 break;
24434 case RID_REGISTER:
24435 storage_class = sc_register;
24436 break;
24437 case RID_STATIC:
24438 storage_class = sc_static;
24439 break;
24440 case RID_EXTERN:
24441 storage_class = sc_extern;
24442 break;
24443 case RID_MUTABLE:
24444 storage_class = sc_mutable;
24445 break;
24446 default:
24447 gcc_unreachable ();
24449 decl_specs->storage_class = storage_class;
24450 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24452 /* A storage class specifier cannot be applied alongside a typedef
24453 specifier. If there is a typedef specifier present then set
24454 conflicting_specifiers_p which will trigger an error later
24455 on in grokdeclarator. */
24456 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24457 decl_specs->conflicting_specifiers_p = true;
24460 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24461 is true, the type is a class or enum definition. */
24463 static void
24464 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24465 tree type_spec,
24466 cp_token *token,
24467 bool type_definition_p)
24469 decl_specs->any_specifiers_p = true;
24471 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24472 (with, for example, in "typedef int wchar_t;") we remember that
24473 this is what happened. In system headers, we ignore these
24474 declarations so that G++ can work with system headers that are not
24475 C++-safe. */
24476 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24477 && !type_definition_p
24478 && (type_spec == boolean_type_node
24479 || type_spec == char16_type_node
24480 || type_spec == char32_type_node
24481 || type_spec == wchar_type_node)
24482 && (decl_specs->type
24483 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24484 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24485 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24486 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24488 decl_specs->redefined_builtin_type = type_spec;
24489 set_and_check_decl_spec_loc (decl_specs,
24490 ds_redefined_builtin_type_spec,
24491 token);
24492 if (!decl_specs->type)
24494 decl_specs->type = type_spec;
24495 decl_specs->type_definition_p = false;
24496 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24499 else if (decl_specs->type)
24500 decl_specs->multiple_types_p = true;
24501 else
24503 decl_specs->type = type_spec;
24504 decl_specs->type_definition_p = type_definition_p;
24505 decl_specs->redefined_builtin_type = NULL_TREE;
24506 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24510 /* True iff TOKEN is the GNU keyword __thread. */
24512 static bool
24513 token_is__thread (cp_token *token)
24515 gcc_assert (token->keyword == RID_THREAD);
24516 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24519 /* Set the location for a declarator specifier and check if it is
24520 duplicated.
24522 DECL_SPECS is the sequence of declarator specifiers onto which to
24523 set the location.
24525 DS is the single declarator specifier to set which location is to
24526 be set onto the existing sequence of declarators.
24528 LOCATION is the location for the declarator specifier to
24529 consider. */
24531 static void
24532 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24533 cp_decl_spec ds, cp_token *token)
24535 gcc_assert (ds < ds_last);
24537 if (decl_specs == NULL)
24538 return;
24540 source_location location = token->location;
24542 if (decl_specs->locations[ds] == 0)
24544 decl_specs->locations[ds] = location;
24545 if (ds == ds_thread)
24546 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24548 else
24550 if (ds == ds_long)
24552 if (decl_specs->locations[ds_long_long] != 0)
24553 error_at (location,
24554 "%<long long long%> is too long for GCC");
24555 else
24557 decl_specs->locations[ds_long_long] = location;
24558 pedwarn_cxx98 (location,
24559 OPT_Wlong_long,
24560 "ISO C++ 1998 does not support %<long long%>");
24563 else if (ds == ds_thread)
24565 bool gnu = token_is__thread (token);
24566 if (gnu != decl_specs->gnu_thread_keyword_p)
24567 error_at (location,
24568 "both %<__thread%> and %<thread_local%> specified");
24569 else
24570 error_at (location, "duplicate %qD", token->u.value);
24572 else
24574 static const char *const decl_spec_names[] = {
24575 "signed",
24576 "unsigned",
24577 "short",
24578 "long",
24579 "const",
24580 "volatile",
24581 "restrict",
24582 "inline",
24583 "virtual",
24584 "explicit",
24585 "friend",
24586 "typedef",
24587 "using",
24588 "constexpr",
24589 "__complex"
24591 error_at (location,
24592 "duplicate %qs", decl_spec_names[ds]);
24597 /* Return true iff the declarator specifier DS is present in the
24598 sequence of declarator specifiers DECL_SPECS. */
24600 bool
24601 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24602 cp_decl_spec ds)
24604 gcc_assert (ds < ds_last);
24606 if (decl_specs == NULL)
24607 return false;
24609 return decl_specs->locations[ds] != 0;
24612 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24613 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24615 static bool
24616 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24618 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24621 /* Issue an error message indicating that TOKEN_DESC was expected.
24622 If KEYWORD is true, it indicated this function is called by
24623 cp_parser_require_keword and the required token can only be
24624 a indicated keyword. */
24626 static void
24627 cp_parser_required_error (cp_parser *parser,
24628 required_token token_desc,
24629 bool keyword)
24631 switch (token_desc)
24633 case RT_NEW:
24634 cp_parser_error (parser, "expected %<new%>");
24635 return;
24636 case RT_DELETE:
24637 cp_parser_error (parser, "expected %<delete%>");
24638 return;
24639 case RT_RETURN:
24640 cp_parser_error (parser, "expected %<return%>");
24641 return;
24642 case RT_WHILE:
24643 cp_parser_error (parser, "expected %<while%>");
24644 return;
24645 case RT_EXTERN:
24646 cp_parser_error (parser, "expected %<extern%>");
24647 return;
24648 case RT_STATIC_ASSERT:
24649 cp_parser_error (parser, "expected %<static_assert%>");
24650 return;
24651 case RT_DECLTYPE:
24652 cp_parser_error (parser, "expected %<decltype%>");
24653 return;
24654 case RT_OPERATOR:
24655 cp_parser_error (parser, "expected %<operator%>");
24656 return;
24657 case RT_CLASS:
24658 cp_parser_error (parser, "expected %<class%>");
24659 return;
24660 case RT_TEMPLATE:
24661 cp_parser_error (parser, "expected %<template%>");
24662 return;
24663 case RT_NAMESPACE:
24664 cp_parser_error (parser, "expected %<namespace%>");
24665 return;
24666 case RT_USING:
24667 cp_parser_error (parser, "expected %<using%>");
24668 return;
24669 case RT_ASM:
24670 cp_parser_error (parser, "expected %<asm%>");
24671 return;
24672 case RT_TRY:
24673 cp_parser_error (parser, "expected %<try%>");
24674 return;
24675 case RT_CATCH:
24676 cp_parser_error (parser, "expected %<catch%>");
24677 return;
24678 case RT_THROW:
24679 cp_parser_error (parser, "expected %<throw%>");
24680 return;
24681 case RT_LABEL:
24682 cp_parser_error (parser, "expected %<__label__%>");
24683 return;
24684 case RT_AT_TRY:
24685 cp_parser_error (parser, "expected %<@try%>");
24686 return;
24687 case RT_AT_SYNCHRONIZED:
24688 cp_parser_error (parser, "expected %<@synchronized%>");
24689 return;
24690 case RT_AT_THROW:
24691 cp_parser_error (parser, "expected %<@throw%>");
24692 return;
24693 case RT_TRANSACTION_ATOMIC:
24694 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24695 return;
24696 case RT_TRANSACTION_RELAXED:
24697 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24698 return;
24699 default:
24700 break;
24702 if (!keyword)
24704 switch (token_desc)
24706 case RT_SEMICOLON:
24707 cp_parser_error (parser, "expected %<;%>");
24708 return;
24709 case RT_OPEN_PAREN:
24710 cp_parser_error (parser, "expected %<(%>");
24711 return;
24712 case RT_CLOSE_BRACE:
24713 cp_parser_error (parser, "expected %<}%>");
24714 return;
24715 case RT_OPEN_BRACE:
24716 cp_parser_error (parser, "expected %<{%>");
24717 return;
24718 case RT_CLOSE_SQUARE:
24719 cp_parser_error (parser, "expected %<]%>");
24720 return;
24721 case RT_OPEN_SQUARE:
24722 cp_parser_error (parser, "expected %<[%>");
24723 return;
24724 case RT_COMMA:
24725 cp_parser_error (parser, "expected %<,%>");
24726 return;
24727 case RT_SCOPE:
24728 cp_parser_error (parser, "expected %<::%>");
24729 return;
24730 case RT_LESS:
24731 cp_parser_error (parser, "expected %<<%>");
24732 return;
24733 case RT_GREATER:
24734 cp_parser_error (parser, "expected %<>%>");
24735 return;
24736 case RT_EQ:
24737 cp_parser_error (parser, "expected %<=%>");
24738 return;
24739 case RT_ELLIPSIS:
24740 cp_parser_error (parser, "expected %<...%>");
24741 return;
24742 case RT_MULT:
24743 cp_parser_error (parser, "expected %<*%>");
24744 return;
24745 case RT_COMPL:
24746 cp_parser_error (parser, "expected %<~%>");
24747 return;
24748 case RT_COLON:
24749 cp_parser_error (parser, "expected %<:%>");
24750 return;
24751 case RT_COLON_SCOPE:
24752 cp_parser_error (parser, "expected %<:%> or %<::%>");
24753 return;
24754 case RT_CLOSE_PAREN:
24755 cp_parser_error (parser, "expected %<)%>");
24756 return;
24757 case RT_COMMA_CLOSE_PAREN:
24758 cp_parser_error (parser, "expected %<,%> or %<)%>");
24759 return;
24760 case RT_PRAGMA_EOL:
24761 cp_parser_error (parser, "expected end of line");
24762 return;
24763 case RT_NAME:
24764 cp_parser_error (parser, "expected identifier");
24765 return;
24766 case RT_SELECT:
24767 cp_parser_error (parser, "expected selection-statement");
24768 return;
24769 case RT_INTERATION:
24770 cp_parser_error (parser, "expected iteration-statement");
24771 return;
24772 case RT_JUMP:
24773 cp_parser_error (parser, "expected jump-statement");
24774 return;
24775 case RT_CLASS_KEY:
24776 cp_parser_error (parser, "expected class-key");
24777 return;
24778 case RT_CLASS_TYPENAME_TEMPLATE:
24779 cp_parser_error (parser,
24780 "expected %<class%>, %<typename%>, or %<template%>");
24781 return;
24782 default:
24783 gcc_unreachable ();
24786 else
24787 gcc_unreachable ();
24792 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24793 issue an error message indicating that TOKEN_DESC was expected.
24795 Returns the token consumed, if the token had the appropriate type.
24796 Otherwise, returns NULL. */
24798 static cp_token *
24799 cp_parser_require (cp_parser* parser,
24800 enum cpp_ttype type,
24801 required_token token_desc)
24803 if (cp_lexer_next_token_is (parser->lexer, type))
24804 return cp_lexer_consume_token (parser->lexer);
24805 else
24807 /* Output the MESSAGE -- unless we're parsing tentatively. */
24808 if (!cp_parser_simulate_error (parser))
24809 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24810 return NULL;
24814 /* An error message is produced if the next token is not '>'.
24815 All further tokens are skipped until the desired token is
24816 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24818 static void
24819 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24821 /* Current level of '< ... >'. */
24822 unsigned level = 0;
24823 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24824 unsigned nesting_depth = 0;
24826 /* Are we ready, yet? If not, issue error message. */
24827 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24828 return;
24830 /* Skip tokens until the desired token is found. */
24831 while (true)
24833 /* Peek at the next token. */
24834 switch (cp_lexer_peek_token (parser->lexer)->type)
24836 case CPP_LESS:
24837 if (!nesting_depth)
24838 ++level;
24839 break;
24841 case CPP_RSHIFT:
24842 if (cxx_dialect == cxx98)
24843 /* C++0x views the `>>' operator as two `>' tokens, but
24844 C++98 does not. */
24845 break;
24846 else if (!nesting_depth && level-- == 0)
24848 /* We've hit a `>>' where the first `>' closes the
24849 template argument list, and the second `>' is
24850 spurious. Just consume the `>>' and stop; we've
24851 already produced at least one error. */
24852 cp_lexer_consume_token (parser->lexer);
24853 return;
24855 /* Fall through for C++0x, so we handle the second `>' in
24856 the `>>'. */
24858 case CPP_GREATER:
24859 if (!nesting_depth && level-- == 0)
24861 /* We've reached the token we want, consume it and stop. */
24862 cp_lexer_consume_token (parser->lexer);
24863 return;
24865 break;
24867 case CPP_OPEN_PAREN:
24868 case CPP_OPEN_SQUARE:
24869 ++nesting_depth;
24870 break;
24872 case CPP_CLOSE_PAREN:
24873 case CPP_CLOSE_SQUARE:
24874 if (nesting_depth-- == 0)
24875 return;
24876 break;
24878 case CPP_EOF:
24879 case CPP_PRAGMA_EOL:
24880 case CPP_SEMICOLON:
24881 case CPP_OPEN_BRACE:
24882 case CPP_CLOSE_BRACE:
24883 /* The '>' was probably forgotten, don't look further. */
24884 return;
24886 default:
24887 break;
24890 /* Consume this token. */
24891 cp_lexer_consume_token (parser->lexer);
24895 /* If the next token is the indicated keyword, consume it. Otherwise,
24896 issue an error message indicating that TOKEN_DESC was expected.
24898 Returns the token consumed, if the token had the appropriate type.
24899 Otherwise, returns NULL. */
24901 static cp_token *
24902 cp_parser_require_keyword (cp_parser* parser,
24903 enum rid keyword,
24904 required_token token_desc)
24906 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24908 if (token && token->keyword != keyword)
24910 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24911 return NULL;
24914 return token;
24917 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24918 function-definition. */
24920 static bool
24921 cp_parser_token_starts_function_definition_p (cp_token* token)
24923 return (/* An ordinary function-body begins with an `{'. */
24924 token->type == CPP_OPEN_BRACE
24925 /* A ctor-initializer begins with a `:'. */
24926 || token->type == CPP_COLON
24927 /* A function-try-block begins with `try'. */
24928 || token->keyword == RID_TRY
24929 /* A function-transaction-block begins with `__transaction_atomic'
24930 or `__transaction_relaxed'. */
24931 || token->keyword == RID_TRANSACTION_ATOMIC
24932 || token->keyword == RID_TRANSACTION_RELAXED
24933 /* The named return value extension begins with `return'. */
24934 || token->keyword == RID_RETURN);
24937 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24938 definition. */
24940 static bool
24941 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24943 cp_token *token;
24945 token = cp_lexer_peek_token (parser->lexer);
24946 return (token->type == CPP_OPEN_BRACE
24947 || (token->type == CPP_COLON
24948 && !parser->colon_doesnt_start_class_def_p));
24951 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24952 C++0x) ending a template-argument. */
24954 static bool
24955 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24957 cp_token *token;
24959 token = cp_lexer_peek_token (parser->lexer);
24960 return (token->type == CPP_COMMA
24961 || token->type == CPP_GREATER
24962 || token->type == CPP_ELLIPSIS
24963 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24966 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24967 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24969 static bool
24970 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24971 size_t n)
24973 cp_token *token;
24975 token = cp_lexer_peek_nth_token (parser->lexer, n);
24976 if (token->type == CPP_LESS)
24977 return true;
24978 /* Check for the sequence `<::' in the original code. It would be lexed as
24979 `[:', where `[' is a digraph, and there is no whitespace before
24980 `:'. */
24981 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24983 cp_token *token2;
24984 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24985 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24986 return true;
24988 return false;
24991 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24992 or none_type otherwise. */
24994 static enum tag_types
24995 cp_parser_token_is_class_key (cp_token* token)
24997 switch (token->keyword)
24999 case RID_CLASS:
25000 return class_type;
25001 case RID_STRUCT:
25002 return record_type;
25003 case RID_UNION:
25004 return union_type;
25006 default:
25007 return none_type;
25011 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25012 or none_type otherwise or if the token is null. */
25014 static enum tag_types
25015 cp_parser_token_is_type_parameter_key (cp_token* token)
25017 if (!token)
25018 return none_type;
25020 switch (token->keyword)
25022 case RID_CLASS:
25023 return class_type;
25024 case RID_TYPENAME:
25025 return typename_type;
25027 default:
25028 return none_type;
25032 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25034 static void
25035 cp_parser_check_class_key (enum tag_types class_key, tree type)
25037 if (type == error_mark_node)
25038 return;
25039 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25041 if (permerror (input_location, "%qs tag used in naming %q#T",
25042 class_key == union_type ? "union"
25043 : class_key == record_type ? "struct" : "class",
25044 type))
25045 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25046 "%q#T was previously declared here", type);
25050 /* Issue an error message if DECL is redeclared with different
25051 access than its original declaration [class.access.spec/3].
25052 This applies to nested classes and nested class templates.
25053 [class.mem/1]. */
25055 static void
25056 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25058 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25059 return;
25061 if ((TREE_PRIVATE (decl)
25062 != (current_access_specifier == access_private_node))
25063 || (TREE_PROTECTED (decl)
25064 != (current_access_specifier == access_protected_node)))
25065 error_at (location, "%qD redeclared with different access", decl);
25068 /* Look for the `template' keyword, as a syntactic disambiguator.
25069 Return TRUE iff it is present, in which case it will be
25070 consumed. */
25072 static bool
25073 cp_parser_optional_template_keyword (cp_parser *parser)
25075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25077 /* In C++98 the `template' keyword can only be used within templates;
25078 outside templates the parser can always figure out what is a
25079 template and what is not. In C++11, per the resolution of DR 468,
25080 `template' is allowed in cases where it is not strictly necessary. */
25081 if (!processing_template_decl
25082 && pedantic && cxx_dialect == cxx98)
25084 cp_token *token = cp_lexer_peek_token (parser->lexer);
25085 pedwarn (token->location, OPT_Wpedantic,
25086 "in C++98 %<template%> (as a disambiguator) is only "
25087 "allowed within templates");
25088 /* If this part of the token stream is rescanned, the same
25089 error message would be generated. So, we purge the token
25090 from the stream. */
25091 cp_lexer_purge_token (parser->lexer);
25092 return false;
25094 else
25096 /* Consume the `template' keyword. */
25097 cp_lexer_consume_token (parser->lexer);
25098 return true;
25101 return false;
25104 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25105 set PARSER->SCOPE, and perform other related actions. */
25107 static void
25108 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25110 int i;
25111 struct tree_check *check_value;
25112 deferred_access_check *chk;
25113 vec<deferred_access_check, va_gc> *checks;
25115 /* Get the stored value. */
25116 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25117 /* Perform any access checks that were deferred. */
25118 checks = check_value->checks;
25119 if (checks)
25121 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25122 perform_or_defer_access_check (chk->binfo,
25123 chk->decl,
25124 chk->diag_decl, tf_warning_or_error);
25126 /* Set the scope from the stored value. */
25127 parser->scope = check_value->value;
25128 parser->qualifying_scope = check_value->qualifying_scope;
25129 parser->object_scope = NULL_TREE;
25132 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25133 encounter the end of a block before what we were looking for. */
25135 static bool
25136 cp_parser_cache_group (cp_parser *parser,
25137 enum cpp_ttype end,
25138 unsigned depth)
25140 while (true)
25142 cp_token *token = cp_lexer_peek_token (parser->lexer);
25144 /* Abort a parenthesized expression if we encounter a semicolon. */
25145 if ((end == CPP_CLOSE_PAREN || depth == 0)
25146 && token->type == CPP_SEMICOLON)
25147 return true;
25148 /* If we've reached the end of the file, stop. */
25149 if (token->type == CPP_EOF
25150 || (end != CPP_PRAGMA_EOL
25151 && token->type == CPP_PRAGMA_EOL))
25152 return true;
25153 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25154 /* We've hit the end of an enclosing block, so there's been some
25155 kind of syntax error. */
25156 return true;
25158 /* Consume the token. */
25159 cp_lexer_consume_token (parser->lexer);
25160 /* See if it starts a new group. */
25161 if (token->type == CPP_OPEN_BRACE)
25163 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25164 /* In theory this should probably check end == '}', but
25165 cp_parser_save_member_function_body needs it to exit
25166 after either '}' or ')' when called with ')'. */
25167 if (depth == 0)
25168 return false;
25170 else if (token->type == CPP_OPEN_PAREN)
25172 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25173 if (depth == 0 && end == CPP_CLOSE_PAREN)
25174 return false;
25176 else if (token->type == CPP_PRAGMA)
25177 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25178 else if (token->type == end)
25179 return false;
25183 /* Like above, for caching a default argument or NSDMI. Both of these are
25184 terminated by a non-nested comma, but it can be unclear whether or not a
25185 comma is nested in a template argument list unless we do more parsing.
25186 In order to handle this ambiguity, when we encounter a ',' after a '<'
25187 we try to parse what follows as a parameter-declaration-list (in the
25188 case of a default argument) or a member-declarator (in the case of an
25189 NSDMI). If that succeeds, then we stop caching. */
25191 static tree
25192 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25194 unsigned depth = 0;
25195 int maybe_template_id = 0;
25196 cp_token *first_token;
25197 cp_token *token;
25198 tree default_argument;
25200 /* Add tokens until we have processed the entire default
25201 argument. We add the range [first_token, token). */
25202 first_token = cp_lexer_peek_token (parser->lexer);
25203 if (first_token->type == CPP_OPEN_BRACE)
25205 /* For list-initialization, this is straightforward. */
25206 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25207 token = cp_lexer_peek_token (parser->lexer);
25209 else while (true)
25211 bool done = false;
25213 /* Peek at the next token. */
25214 token = cp_lexer_peek_token (parser->lexer);
25215 /* What we do depends on what token we have. */
25216 switch (token->type)
25218 /* In valid code, a default argument must be
25219 immediately followed by a `,' `)', or `...'. */
25220 case CPP_COMMA:
25221 if (depth == 0 && maybe_template_id)
25223 /* If we've seen a '<', we might be in a
25224 template-argument-list. Until Core issue 325 is
25225 resolved, we don't know how this situation ought
25226 to be handled, so try to DTRT. We check whether
25227 what comes after the comma is a valid parameter
25228 declaration list. If it is, then the comma ends
25229 the default argument; otherwise the default
25230 argument continues. */
25231 bool error = false;
25233 /* Set ITALP so cp_parser_parameter_declaration_list
25234 doesn't decide to commit to this parse. */
25235 bool saved_italp = parser->in_template_argument_list_p;
25236 parser->in_template_argument_list_p = true;
25238 cp_parser_parse_tentatively (parser);
25239 cp_lexer_consume_token (parser->lexer);
25241 if (nsdmi)
25243 int ctor_dtor_or_conv_p;
25244 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25245 &ctor_dtor_or_conv_p,
25246 /*parenthesized_p=*/NULL,
25247 /*member_p=*/true,
25248 /*friend_p=*/false);
25250 else
25252 begin_scope (sk_function_parms, NULL_TREE);
25253 cp_parser_parameter_declaration_list (parser, &error);
25254 pop_bindings_and_leave_scope ();
25256 if (!cp_parser_error_occurred (parser) && !error)
25257 done = true;
25258 cp_parser_abort_tentative_parse (parser);
25260 parser->in_template_argument_list_p = saved_italp;
25261 break;
25263 case CPP_CLOSE_PAREN:
25264 case CPP_ELLIPSIS:
25265 /* If we run into a non-nested `;', `}', or `]',
25266 then the code is invalid -- but the default
25267 argument is certainly over. */
25268 case CPP_SEMICOLON:
25269 case CPP_CLOSE_BRACE:
25270 case CPP_CLOSE_SQUARE:
25271 if (depth == 0
25272 /* Handle correctly int n = sizeof ... ( p ); */
25273 && token->type != CPP_ELLIPSIS)
25274 done = true;
25275 /* Update DEPTH, if necessary. */
25276 else if (token->type == CPP_CLOSE_PAREN
25277 || token->type == CPP_CLOSE_BRACE
25278 || token->type == CPP_CLOSE_SQUARE)
25279 --depth;
25280 break;
25282 case CPP_OPEN_PAREN:
25283 case CPP_OPEN_SQUARE:
25284 case CPP_OPEN_BRACE:
25285 ++depth;
25286 break;
25288 case CPP_LESS:
25289 if (depth == 0)
25290 /* This might be the comparison operator, or it might
25291 start a template argument list. */
25292 ++maybe_template_id;
25293 break;
25295 case CPP_RSHIFT:
25296 if (cxx_dialect == cxx98)
25297 break;
25298 /* Fall through for C++0x, which treats the `>>'
25299 operator like two `>' tokens in certain
25300 cases. */
25302 case CPP_GREATER:
25303 if (depth == 0)
25305 /* This might be an operator, or it might close a
25306 template argument list. But if a previous '<'
25307 started a template argument list, this will have
25308 closed it, so we can't be in one anymore. */
25309 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25310 if (maybe_template_id < 0)
25311 maybe_template_id = 0;
25313 break;
25315 /* If we run out of tokens, issue an error message. */
25316 case CPP_EOF:
25317 case CPP_PRAGMA_EOL:
25318 error_at (token->location, "file ends in default argument");
25319 done = true;
25320 break;
25322 case CPP_NAME:
25323 case CPP_SCOPE:
25324 /* In these cases, we should look for template-ids.
25325 For example, if the default argument is
25326 `X<int, double>()', we need to do name lookup to
25327 figure out whether or not `X' is a template; if
25328 so, the `,' does not end the default argument.
25330 That is not yet done. */
25331 break;
25333 default:
25334 break;
25337 /* If we've reached the end, stop. */
25338 if (done)
25339 break;
25341 /* Add the token to the token block. */
25342 token = cp_lexer_consume_token (parser->lexer);
25345 /* Create a DEFAULT_ARG to represent the unparsed default
25346 argument. */
25347 default_argument = make_node (DEFAULT_ARG);
25348 DEFARG_TOKENS (default_argument)
25349 = cp_token_cache_new (first_token, token);
25350 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25352 return default_argument;
25355 /* Begin parsing tentatively. We always save tokens while parsing
25356 tentatively so that if the tentative parsing fails we can restore the
25357 tokens. */
25359 static void
25360 cp_parser_parse_tentatively (cp_parser* parser)
25362 /* Enter a new parsing context. */
25363 parser->context = cp_parser_context_new (parser->context);
25364 /* Begin saving tokens. */
25365 cp_lexer_save_tokens (parser->lexer);
25366 /* In order to avoid repetitive access control error messages,
25367 access checks are queued up until we are no longer parsing
25368 tentatively. */
25369 push_deferring_access_checks (dk_deferred);
25372 /* Commit to the currently active tentative parse. */
25374 static void
25375 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25377 cp_parser_context *context;
25378 cp_lexer *lexer;
25380 /* Mark all of the levels as committed. */
25381 lexer = parser->lexer;
25382 for (context = parser->context; context->next; context = context->next)
25384 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25385 break;
25386 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25387 while (!cp_lexer_saving_tokens (lexer))
25388 lexer = lexer->next;
25389 cp_lexer_commit_tokens (lexer);
25393 /* Commit to the topmost currently active tentative parse.
25395 Note that this function shouldn't be called when there are
25396 irreversible side-effects while in a tentative state. For
25397 example, we shouldn't create a permanent entry in the symbol
25398 table, or issue an error message that might not apply if the
25399 tentative parse is aborted. */
25401 static void
25402 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25404 cp_parser_context *context = parser->context;
25405 cp_lexer *lexer = parser->lexer;
25407 if (context)
25409 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25410 return;
25411 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25413 while (!cp_lexer_saving_tokens (lexer))
25414 lexer = lexer->next;
25415 cp_lexer_commit_tokens (lexer);
25419 /* Abort the currently active tentative parse. All consumed tokens
25420 will be rolled back, and no diagnostics will be issued. */
25422 static void
25423 cp_parser_abort_tentative_parse (cp_parser* parser)
25425 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25426 || errorcount > 0);
25427 cp_parser_simulate_error (parser);
25428 /* Now, pretend that we want to see if the construct was
25429 successfully parsed. */
25430 cp_parser_parse_definitely (parser);
25433 /* Stop parsing tentatively. If a parse error has occurred, restore the
25434 token stream. Otherwise, commit to the tokens we have consumed.
25435 Returns true if no error occurred; false otherwise. */
25437 static bool
25438 cp_parser_parse_definitely (cp_parser* parser)
25440 bool error_occurred;
25441 cp_parser_context *context;
25443 /* Remember whether or not an error occurred, since we are about to
25444 destroy that information. */
25445 error_occurred = cp_parser_error_occurred (parser);
25446 /* Remove the topmost context from the stack. */
25447 context = parser->context;
25448 parser->context = context->next;
25449 /* If no parse errors occurred, commit to the tentative parse. */
25450 if (!error_occurred)
25452 /* Commit to the tokens read tentatively, unless that was
25453 already done. */
25454 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25455 cp_lexer_commit_tokens (parser->lexer);
25457 pop_to_parent_deferring_access_checks ();
25459 /* Otherwise, if errors occurred, roll back our state so that things
25460 are just as they were before we began the tentative parse. */
25461 else
25463 cp_lexer_rollback_tokens (parser->lexer);
25464 pop_deferring_access_checks ();
25466 /* Add the context to the front of the free list. */
25467 context->next = cp_parser_context_free_list;
25468 cp_parser_context_free_list = context;
25470 return !error_occurred;
25473 /* Returns true if we are parsing tentatively and are not committed to
25474 this tentative parse. */
25476 static bool
25477 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25479 return (cp_parser_parsing_tentatively (parser)
25480 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25483 /* Returns nonzero iff an error has occurred during the most recent
25484 tentative parse. */
25486 static bool
25487 cp_parser_error_occurred (cp_parser* parser)
25489 return (cp_parser_parsing_tentatively (parser)
25490 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25493 /* Returns nonzero if GNU extensions are allowed. */
25495 static bool
25496 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25498 return parser->allow_gnu_extensions_p;
25501 /* Objective-C++ Productions */
25504 /* Parse an Objective-C expression, which feeds into a primary-expression
25505 above.
25507 objc-expression:
25508 objc-message-expression
25509 objc-string-literal
25510 objc-encode-expression
25511 objc-protocol-expression
25512 objc-selector-expression
25514 Returns a tree representation of the expression. */
25516 static tree
25517 cp_parser_objc_expression (cp_parser* parser)
25519 /* Try to figure out what kind of declaration is present. */
25520 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25522 switch (kwd->type)
25524 case CPP_OPEN_SQUARE:
25525 return cp_parser_objc_message_expression (parser);
25527 case CPP_OBJC_STRING:
25528 kwd = cp_lexer_consume_token (parser->lexer);
25529 return objc_build_string_object (kwd->u.value);
25531 case CPP_KEYWORD:
25532 switch (kwd->keyword)
25534 case RID_AT_ENCODE:
25535 return cp_parser_objc_encode_expression (parser);
25537 case RID_AT_PROTOCOL:
25538 return cp_parser_objc_protocol_expression (parser);
25540 case RID_AT_SELECTOR:
25541 return cp_parser_objc_selector_expression (parser);
25543 default:
25544 break;
25546 default:
25547 error_at (kwd->location,
25548 "misplaced %<@%D%> Objective-C++ construct",
25549 kwd->u.value);
25550 cp_parser_skip_to_end_of_block_or_statement (parser);
25553 return error_mark_node;
25556 /* Parse an Objective-C message expression.
25558 objc-message-expression:
25559 [ objc-message-receiver objc-message-args ]
25561 Returns a representation of an Objective-C message. */
25563 static tree
25564 cp_parser_objc_message_expression (cp_parser* parser)
25566 tree receiver, messageargs;
25568 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25569 receiver = cp_parser_objc_message_receiver (parser);
25570 messageargs = cp_parser_objc_message_args (parser);
25571 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25573 return objc_build_message_expr (receiver, messageargs);
25576 /* Parse an objc-message-receiver.
25578 objc-message-receiver:
25579 expression
25580 simple-type-specifier
25582 Returns a representation of the type or expression. */
25584 static tree
25585 cp_parser_objc_message_receiver (cp_parser* parser)
25587 tree rcv;
25589 /* An Objective-C message receiver may be either (1) a type
25590 or (2) an expression. */
25591 cp_parser_parse_tentatively (parser);
25592 rcv = cp_parser_expression (parser);
25594 if (cp_parser_parse_definitely (parser))
25595 return rcv;
25597 rcv = cp_parser_simple_type_specifier (parser,
25598 /*decl_specs=*/NULL,
25599 CP_PARSER_FLAGS_NONE);
25601 return objc_get_class_reference (rcv);
25604 /* Parse the arguments and selectors comprising an Objective-C message.
25606 objc-message-args:
25607 objc-selector
25608 objc-selector-args
25609 objc-selector-args , objc-comma-args
25611 objc-selector-args:
25612 objc-selector [opt] : assignment-expression
25613 objc-selector-args objc-selector [opt] : assignment-expression
25615 objc-comma-args:
25616 assignment-expression
25617 objc-comma-args , assignment-expression
25619 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25620 selector arguments and TREE_VALUE containing a list of comma
25621 arguments. */
25623 static tree
25624 cp_parser_objc_message_args (cp_parser* parser)
25626 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25627 bool maybe_unary_selector_p = true;
25628 cp_token *token = cp_lexer_peek_token (parser->lexer);
25630 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25632 tree selector = NULL_TREE, arg;
25634 if (token->type != CPP_COLON)
25635 selector = cp_parser_objc_selector (parser);
25637 /* Detect if we have a unary selector. */
25638 if (maybe_unary_selector_p
25639 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25640 return build_tree_list (selector, NULL_TREE);
25642 maybe_unary_selector_p = false;
25643 cp_parser_require (parser, CPP_COLON, RT_COLON);
25644 arg = cp_parser_assignment_expression (parser);
25646 sel_args
25647 = chainon (sel_args,
25648 build_tree_list (selector, arg));
25650 token = cp_lexer_peek_token (parser->lexer);
25653 /* Handle non-selector arguments, if any. */
25654 while (token->type == CPP_COMMA)
25656 tree arg;
25658 cp_lexer_consume_token (parser->lexer);
25659 arg = cp_parser_assignment_expression (parser);
25661 addl_args
25662 = chainon (addl_args,
25663 build_tree_list (NULL_TREE, arg));
25665 token = cp_lexer_peek_token (parser->lexer);
25668 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25670 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25671 return build_tree_list (error_mark_node, error_mark_node);
25674 return build_tree_list (sel_args, addl_args);
25677 /* Parse an Objective-C encode expression.
25679 objc-encode-expression:
25680 @encode objc-typename
25682 Returns an encoded representation of the type argument. */
25684 static tree
25685 cp_parser_objc_encode_expression (cp_parser* parser)
25687 tree type;
25688 cp_token *token;
25690 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25691 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25692 token = cp_lexer_peek_token (parser->lexer);
25693 type = complete_type (cp_parser_type_id (parser));
25694 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25696 if (!type)
25698 error_at (token->location,
25699 "%<@encode%> must specify a type as an argument");
25700 return error_mark_node;
25703 /* This happens if we find @encode(T) (where T is a template
25704 typename or something dependent on a template typename) when
25705 parsing a template. In that case, we can't compile it
25706 immediately, but we rather create an AT_ENCODE_EXPR which will
25707 need to be instantiated when the template is used.
25709 if (dependent_type_p (type))
25711 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25712 TREE_READONLY (value) = 1;
25713 return value;
25716 return objc_build_encode_expr (type);
25719 /* Parse an Objective-C @defs expression. */
25721 static tree
25722 cp_parser_objc_defs_expression (cp_parser *parser)
25724 tree name;
25726 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25727 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25728 name = cp_parser_identifier (parser);
25729 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25731 return objc_get_class_ivars (name);
25734 /* Parse an Objective-C protocol expression.
25736 objc-protocol-expression:
25737 @protocol ( identifier )
25739 Returns a representation of the protocol expression. */
25741 static tree
25742 cp_parser_objc_protocol_expression (cp_parser* parser)
25744 tree proto;
25746 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25747 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25748 proto = cp_parser_identifier (parser);
25749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25751 return objc_build_protocol_expr (proto);
25754 /* Parse an Objective-C selector expression.
25756 objc-selector-expression:
25757 @selector ( objc-method-signature )
25759 objc-method-signature:
25760 objc-selector
25761 objc-selector-seq
25763 objc-selector-seq:
25764 objc-selector :
25765 objc-selector-seq objc-selector :
25767 Returns a representation of the method selector. */
25769 static tree
25770 cp_parser_objc_selector_expression (cp_parser* parser)
25772 tree sel_seq = NULL_TREE;
25773 bool maybe_unary_selector_p = true;
25774 cp_token *token;
25775 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25777 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25779 token = cp_lexer_peek_token (parser->lexer);
25781 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25782 || token->type == CPP_SCOPE)
25784 tree selector = NULL_TREE;
25786 if (token->type != CPP_COLON
25787 || token->type == CPP_SCOPE)
25788 selector = cp_parser_objc_selector (parser);
25790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25791 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25793 /* Detect if we have a unary selector. */
25794 if (maybe_unary_selector_p)
25796 sel_seq = selector;
25797 goto finish_selector;
25799 else
25801 cp_parser_error (parser, "expected %<:%>");
25804 maybe_unary_selector_p = false;
25805 token = cp_lexer_consume_token (parser->lexer);
25807 if (token->type == CPP_SCOPE)
25809 sel_seq
25810 = chainon (sel_seq,
25811 build_tree_list (selector, NULL_TREE));
25812 sel_seq
25813 = chainon (sel_seq,
25814 build_tree_list (NULL_TREE, NULL_TREE));
25816 else
25817 sel_seq
25818 = chainon (sel_seq,
25819 build_tree_list (selector, NULL_TREE));
25821 token = cp_lexer_peek_token (parser->lexer);
25824 finish_selector:
25825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25827 return objc_build_selector_expr (loc, sel_seq);
25830 /* Parse a list of identifiers.
25832 objc-identifier-list:
25833 identifier
25834 objc-identifier-list , identifier
25836 Returns a TREE_LIST of identifier nodes. */
25838 static tree
25839 cp_parser_objc_identifier_list (cp_parser* parser)
25841 tree identifier;
25842 tree list;
25843 cp_token *sep;
25845 identifier = cp_parser_identifier (parser);
25846 if (identifier == error_mark_node)
25847 return error_mark_node;
25849 list = build_tree_list (NULL_TREE, identifier);
25850 sep = cp_lexer_peek_token (parser->lexer);
25852 while (sep->type == CPP_COMMA)
25854 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25855 identifier = cp_parser_identifier (parser);
25856 if (identifier == error_mark_node)
25857 return list;
25859 list = chainon (list, build_tree_list (NULL_TREE,
25860 identifier));
25861 sep = cp_lexer_peek_token (parser->lexer);
25864 return list;
25867 /* Parse an Objective-C alias declaration.
25869 objc-alias-declaration:
25870 @compatibility_alias identifier identifier ;
25872 This function registers the alias mapping with the Objective-C front end.
25873 It returns nothing. */
25875 static void
25876 cp_parser_objc_alias_declaration (cp_parser* parser)
25878 tree alias, orig;
25880 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25881 alias = cp_parser_identifier (parser);
25882 orig = cp_parser_identifier (parser);
25883 objc_declare_alias (alias, orig);
25884 cp_parser_consume_semicolon_at_end_of_statement (parser);
25887 /* Parse an Objective-C class forward-declaration.
25889 objc-class-declaration:
25890 @class objc-identifier-list ;
25892 The function registers the forward declarations with the Objective-C
25893 front end. It returns nothing. */
25895 static void
25896 cp_parser_objc_class_declaration (cp_parser* parser)
25898 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25899 while (true)
25901 tree id;
25903 id = cp_parser_identifier (parser);
25904 if (id == error_mark_node)
25905 break;
25907 objc_declare_class (id);
25909 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25910 cp_lexer_consume_token (parser->lexer);
25911 else
25912 break;
25914 cp_parser_consume_semicolon_at_end_of_statement (parser);
25917 /* Parse a list of Objective-C protocol references.
25919 objc-protocol-refs-opt:
25920 objc-protocol-refs [opt]
25922 objc-protocol-refs:
25923 < objc-identifier-list >
25925 Returns a TREE_LIST of identifiers, if any. */
25927 static tree
25928 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25930 tree protorefs = NULL_TREE;
25932 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25934 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25935 protorefs = cp_parser_objc_identifier_list (parser);
25936 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25939 return protorefs;
25942 /* Parse a Objective-C visibility specification. */
25944 static void
25945 cp_parser_objc_visibility_spec (cp_parser* parser)
25947 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25949 switch (vis->keyword)
25951 case RID_AT_PRIVATE:
25952 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25953 break;
25954 case RID_AT_PROTECTED:
25955 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25956 break;
25957 case RID_AT_PUBLIC:
25958 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25959 break;
25960 case RID_AT_PACKAGE:
25961 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25962 break;
25963 default:
25964 return;
25967 /* Eat '@private'/'@protected'/'@public'. */
25968 cp_lexer_consume_token (parser->lexer);
25971 /* Parse an Objective-C method type. Return 'true' if it is a class
25972 (+) method, and 'false' if it is an instance (-) method. */
25974 static inline bool
25975 cp_parser_objc_method_type (cp_parser* parser)
25977 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25978 return true;
25979 else
25980 return false;
25983 /* Parse an Objective-C protocol qualifier. */
25985 static tree
25986 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25988 tree quals = NULL_TREE, node;
25989 cp_token *token = cp_lexer_peek_token (parser->lexer);
25991 node = token->u.value;
25993 while (node && identifier_p (node)
25994 && (node == ridpointers [(int) RID_IN]
25995 || node == ridpointers [(int) RID_OUT]
25996 || node == ridpointers [(int) RID_INOUT]
25997 || node == ridpointers [(int) RID_BYCOPY]
25998 || node == ridpointers [(int) RID_BYREF]
25999 || node == ridpointers [(int) RID_ONEWAY]))
26001 quals = tree_cons (NULL_TREE, node, quals);
26002 cp_lexer_consume_token (parser->lexer);
26003 token = cp_lexer_peek_token (parser->lexer);
26004 node = token->u.value;
26007 return quals;
26010 /* Parse an Objective-C typename. */
26012 static tree
26013 cp_parser_objc_typename (cp_parser* parser)
26015 tree type_name = NULL_TREE;
26017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26019 tree proto_quals, cp_type = NULL_TREE;
26021 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26022 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26024 /* An ObjC type name may consist of just protocol qualifiers, in which
26025 case the type shall default to 'id'. */
26026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26028 cp_type = cp_parser_type_id (parser);
26030 /* If the type could not be parsed, an error has already
26031 been produced. For error recovery, behave as if it had
26032 not been specified, which will use the default type
26033 'id'. */
26034 if (cp_type == error_mark_node)
26036 cp_type = NULL_TREE;
26037 /* We need to skip to the closing parenthesis as
26038 cp_parser_type_id() does not seem to do it for
26039 us. */
26040 cp_parser_skip_to_closing_parenthesis (parser,
26041 /*recovering=*/true,
26042 /*or_comma=*/false,
26043 /*consume_paren=*/false);
26047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26048 type_name = build_tree_list (proto_quals, cp_type);
26051 return type_name;
26054 /* Check to see if TYPE refers to an Objective-C selector name. */
26056 static bool
26057 cp_parser_objc_selector_p (enum cpp_ttype type)
26059 return (type == CPP_NAME || type == CPP_KEYWORD
26060 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26061 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26062 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26063 || type == CPP_XOR || type == CPP_XOR_EQ);
26066 /* Parse an Objective-C selector. */
26068 static tree
26069 cp_parser_objc_selector (cp_parser* parser)
26071 cp_token *token = cp_lexer_consume_token (parser->lexer);
26073 if (!cp_parser_objc_selector_p (token->type))
26075 error_at (token->location, "invalid Objective-C++ selector name");
26076 return error_mark_node;
26079 /* C++ operator names are allowed to appear in ObjC selectors. */
26080 switch (token->type)
26082 case CPP_AND_AND: return get_identifier ("and");
26083 case CPP_AND_EQ: return get_identifier ("and_eq");
26084 case CPP_AND: return get_identifier ("bitand");
26085 case CPP_OR: return get_identifier ("bitor");
26086 case CPP_COMPL: return get_identifier ("compl");
26087 case CPP_NOT: return get_identifier ("not");
26088 case CPP_NOT_EQ: return get_identifier ("not_eq");
26089 case CPP_OR_OR: return get_identifier ("or");
26090 case CPP_OR_EQ: return get_identifier ("or_eq");
26091 case CPP_XOR: return get_identifier ("xor");
26092 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26093 default: return token->u.value;
26097 /* Parse an Objective-C params list. */
26099 static tree
26100 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26102 tree params = NULL_TREE;
26103 bool maybe_unary_selector_p = true;
26104 cp_token *token = cp_lexer_peek_token (parser->lexer);
26106 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26108 tree selector = NULL_TREE, type_name, identifier;
26109 tree parm_attr = NULL_TREE;
26111 if (token->keyword == RID_ATTRIBUTE)
26112 break;
26114 if (token->type != CPP_COLON)
26115 selector = cp_parser_objc_selector (parser);
26117 /* Detect if we have a unary selector. */
26118 if (maybe_unary_selector_p
26119 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26121 params = selector; /* Might be followed by attributes. */
26122 break;
26125 maybe_unary_selector_p = false;
26126 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26128 /* Something went quite wrong. There should be a colon
26129 here, but there is not. Stop parsing parameters. */
26130 break;
26132 type_name = cp_parser_objc_typename (parser);
26133 /* New ObjC allows attributes on parameters too. */
26134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26135 parm_attr = cp_parser_attributes_opt (parser);
26136 identifier = cp_parser_identifier (parser);
26138 params
26139 = chainon (params,
26140 objc_build_keyword_decl (selector,
26141 type_name,
26142 identifier,
26143 parm_attr));
26145 token = cp_lexer_peek_token (parser->lexer);
26148 if (params == NULL_TREE)
26150 cp_parser_error (parser, "objective-c++ method declaration is expected");
26151 return error_mark_node;
26154 /* We allow tail attributes for the method. */
26155 if (token->keyword == RID_ATTRIBUTE)
26157 *attributes = cp_parser_attributes_opt (parser);
26158 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26159 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26160 return params;
26161 cp_parser_error (parser,
26162 "method attributes must be specified at the end");
26163 return error_mark_node;
26166 if (params == NULL_TREE)
26168 cp_parser_error (parser, "objective-c++ method declaration is expected");
26169 return error_mark_node;
26171 return params;
26174 /* Parse the non-keyword Objective-C params. */
26176 static tree
26177 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26178 tree* attributes)
26180 tree params = make_node (TREE_LIST);
26181 cp_token *token = cp_lexer_peek_token (parser->lexer);
26182 *ellipsisp = false; /* Initially, assume no ellipsis. */
26184 while (token->type == CPP_COMMA)
26186 cp_parameter_declarator *parmdecl;
26187 tree parm;
26189 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26190 token = cp_lexer_peek_token (parser->lexer);
26192 if (token->type == CPP_ELLIPSIS)
26194 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26195 *ellipsisp = true;
26196 token = cp_lexer_peek_token (parser->lexer);
26197 break;
26200 /* TODO: parse attributes for tail parameters. */
26201 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26202 parm = grokdeclarator (parmdecl->declarator,
26203 &parmdecl->decl_specifiers,
26204 PARM, /*initialized=*/0,
26205 /*attrlist=*/NULL);
26207 chainon (params, build_tree_list (NULL_TREE, parm));
26208 token = cp_lexer_peek_token (parser->lexer);
26211 /* We allow tail attributes for the method. */
26212 if (token->keyword == RID_ATTRIBUTE)
26214 if (*attributes == NULL_TREE)
26216 *attributes = cp_parser_attributes_opt (parser);
26217 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26218 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26219 return params;
26221 else
26222 /* We have an error, but parse the attributes, so that we can
26223 carry on. */
26224 *attributes = cp_parser_attributes_opt (parser);
26226 cp_parser_error (parser,
26227 "method attributes must be specified at the end");
26228 return error_mark_node;
26231 return params;
26234 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26236 static void
26237 cp_parser_objc_interstitial_code (cp_parser* parser)
26239 cp_token *token = cp_lexer_peek_token (parser->lexer);
26241 /* If the next token is `extern' and the following token is a string
26242 literal, then we have a linkage specification. */
26243 if (token->keyword == RID_EXTERN
26244 && cp_parser_is_pure_string_literal
26245 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26246 cp_parser_linkage_specification (parser);
26247 /* Handle #pragma, if any. */
26248 else if (token->type == CPP_PRAGMA)
26249 cp_parser_pragma (parser, pragma_objc_icode);
26250 /* Allow stray semicolons. */
26251 else if (token->type == CPP_SEMICOLON)
26252 cp_lexer_consume_token (parser->lexer);
26253 /* Mark methods as optional or required, when building protocols. */
26254 else if (token->keyword == RID_AT_OPTIONAL)
26256 cp_lexer_consume_token (parser->lexer);
26257 objc_set_method_opt (true);
26259 else if (token->keyword == RID_AT_REQUIRED)
26261 cp_lexer_consume_token (parser->lexer);
26262 objc_set_method_opt (false);
26264 else if (token->keyword == RID_NAMESPACE)
26265 cp_parser_namespace_definition (parser);
26266 /* Other stray characters must generate errors. */
26267 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26269 cp_lexer_consume_token (parser->lexer);
26270 error ("stray %qs between Objective-C++ methods",
26271 token->type == CPP_OPEN_BRACE ? "{" : "}");
26273 /* Finally, try to parse a block-declaration, or a function-definition. */
26274 else
26275 cp_parser_block_declaration (parser, /*statement_p=*/false);
26278 /* Parse a method signature. */
26280 static tree
26281 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26283 tree rettype, kwdparms, optparms;
26284 bool ellipsis = false;
26285 bool is_class_method;
26287 is_class_method = cp_parser_objc_method_type (parser);
26288 rettype = cp_parser_objc_typename (parser);
26289 *attributes = NULL_TREE;
26290 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26291 if (kwdparms == error_mark_node)
26292 return error_mark_node;
26293 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26294 if (optparms == error_mark_node)
26295 return error_mark_node;
26297 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26300 static bool
26301 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26303 tree tattr;
26304 cp_lexer_save_tokens (parser->lexer);
26305 tattr = cp_parser_attributes_opt (parser);
26306 gcc_assert (tattr) ;
26308 /* If the attributes are followed by a method introducer, this is not allowed.
26309 Dump the attributes and flag the situation. */
26310 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26311 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26312 return true;
26314 /* Otherwise, the attributes introduce some interstitial code, possibly so
26315 rewind to allow that check. */
26316 cp_lexer_rollback_tokens (parser->lexer);
26317 return false;
26320 /* Parse an Objective-C method prototype list. */
26322 static void
26323 cp_parser_objc_method_prototype_list (cp_parser* parser)
26325 cp_token *token = cp_lexer_peek_token (parser->lexer);
26327 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26329 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26331 tree attributes, sig;
26332 bool is_class_method;
26333 if (token->type == CPP_PLUS)
26334 is_class_method = true;
26335 else
26336 is_class_method = false;
26337 sig = cp_parser_objc_method_signature (parser, &attributes);
26338 if (sig == error_mark_node)
26340 cp_parser_skip_to_end_of_block_or_statement (parser);
26341 token = cp_lexer_peek_token (parser->lexer);
26342 continue;
26344 objc_add_method_declaration (is_class_method, sig, attributes);
26345 cp_parser_consume_semicolon_at_end_of_statement (parser);
26347 else if (token->keyword == RID_AT_PROPERTY)
26348 cp_parser_objc_at_property_declaration (parser);
26349 else if (token->keyword == RID_ATTRIBUTE
26350 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26351 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26352 OPT_Wattributes,
26353 "prefix attributes are ignored for methods");
26354 else
26355 /* Allow for interspersed non-ObjC++ code. */
26356 cp_parser_objc_interstitial_code (parser);
26358 token = cp_lexer_peek_token (parser->lexer);
26361 if (token->type != CPP_EOF)
26362 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26363 else
26364 cp_parser_error (parser, "expected %<@end%>");
26366 objc_finish_interface ();
26369 /* Parse an Objective-C method definition list. */
26371 static void
26372 cp_parser_objc_method_definition_list (cp_parser* parser)
26374 cp_token *token = cp_lexer_peek_token (parser->lexer);
26376 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26378 tree meth;
26380 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26382 cp_token *ptk;
26383 tree sig, attribute;
26384 bool is_class_method;
26385 if (token->type == CPP_PLUS)
26386 is_class_method = true;
26387 else
26388 is_class_method = false;
26389 push_deferring_access_checks (dk_deferred);
26390 sig = cp_parser_objc_method_signature (parser, &attribute);
26391 if (sig == error_mark_node)
26393 cp_parser_skip_to_end_of_block_or_statement (parser);
26394 token = cp_lexer_peek_token (parser->lexer);
26395 continue;
26397 objc_start_method_definition (is_class_method, sig, attribute,
26398 NULL_TREE);
26400 /* For historical reasons, we accept an optional semicolon. */
26401 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26402 cp_lexer_consume_token (parser->lexer);
26404 ptk = cp_lexer_peek_token (parser->lexer);
26405 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26406 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26408 perform_deferred_access_checks (tf_warning_or_error);
26409 stop_deferring_access_checks ();
26410 meth = cp_parser_function_definition_after_declarator (parser,
26411 false);
26412 pop_deferring_access_checks ();
26413 objc_finish_method_definition (meth);
26416 /* The following case will be removed once @synthesize is
26417 completely implemented. */
26418 else if (token->keyword == RID_AT_PROPERTY)
26419 cp_parser_objc_at_property_declaration (parser);
26420 else if (token->keyword == RID_AT_SYNTHESIZE)
26421 cp_parser_objc_at_synthesize_declaration (parser);
26422 else if (token->keyword == RID_AT_DYNAMIC)
26423 cp_parser_objc_at_dynamic_declaration (parser);
26424 else if (token->keyword == RID_ATTRIBUTE
26425 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26426 warning_at (token->location, OPT_Wattributes,
26427 "prefix attributes are ignored for methods");
26428 else
26429 /* Allow for interspersed non-ObjC++ code. */
26430 cp_parser_objc_interstitial_code (parser);
26432 token = cp_lexer_peek_token (parser->lexer);
26435 if (token->type != CPP_EOF)
26436 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26437 else
26438 cp_parser_error (parser, "expected %<@end%>");
26440 objc_finish_implementation ();
26443 /* Parse Objective-C ivars. */
26445 static void
26446 cp_parser_objc_class_ivars (cp_parser* parser)
26448 cp_token *token = cp_lexer_peek_token (parser->lexer);
26450 if (token->type != CPP_OPEN_BRACE)
26451 return; /* No ivars specified. */
26453 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26454 token = cp_lexer_peek_token (parser->lexer);
26456 while (token->type != CPP_CLOSE_BRACE
26457 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26459 cp_decl_specifier_seq declspecs;
26460 int decl_class_or_enum_p;
26461 tree prefix_attributes;
26463 cp_parser_objc_visibility_spec (parser);
26465 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26466 break;
26468 cp_parser_decl_specifier_seq (parser,
26469 CP_PARSER_FLAGS_OPTIONAL,
26470 &declspecs,
26471 &decl_class_or_enum_p);
26473 /* auto, register, static, extern, mutable. */
26474 if (declspecs.storage_class != sc_none)
26476 cp_parser_error (parser, "invalid type for instance variable");
26477 declspecs.storage_class = sc_none;
26480 /* thread_local. */
26481 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26483 cp_parser_error (parser, "invalid type for instance variable");
26484 declspecs.locations[ds_thread] = 0;
26487 /* typedef. */
26488 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26490 cp_parser_error (parser, "invalid type for instance variable");
26491 declspecs.locations[ds_typedef] = 0;
26494 prefix_attributes = declspecs.attributes;
26495 declspecs.attributes = NULL_TREE;
26497 /* Keep going until we hit the `;' at the end of the
26498 declaration. */
26499 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26501 tree width = NULL_TREE, attributes, first_attribute, decl;
26502 cp_declarator *declarator = NULL;
26503 int ctor_dtor_or_conv_p;
26505 /* Check for a (possibly unnamed) bitfield declaration. */
26506 token = cp_lexer_peek_token (parser->lexer);
26507 if (token->type == CPP_COLON)
26508 goto eat_colon;
26510 if (token->type == CPP_NAME
26511 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26512 == CPP_COLON))
26514 /* Get the name of the bitfield. */
26515 declarator = make_id_declarator (NULL_TREE,
26516 cp_parser_identifier (parser),
26517 sfk_none);
26519 eat_colon:
26520 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26521 /* Get the width of the bitfield. */
26522 width
26523 = cp_parser_constant_expression (parser);
26525 else
26527 /* Parse the declarator. */
26528 declarator
26529 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26530 &ctor_dtor_or_conv_p,
26531 /*parenthesized_p=*/NULL,
26532 /*member_p=*/false,
26533 /*friend_p=*/false);
26536 /* Look for attributes that apply to the ivar. */
26537 attributes = cp_parser_attributes_opt (parser);
26538 /* Remember which attributes are prefix attributes and
26539 which are not. */
26540 first_attribute = attributes;
26541 /* Combine the attributes. */
26542 attributes = chainon (prefix_attributes, attributes);
26544 if (width)
26545 /* Create the bitfield declaration. */
26546 decl = grokbitfield (declarator, &declspecs,
26547 width,
26548 attributes);
26549 else
26550 decl = grokfield (declarator, &declspecs,
26551 NULL_TREE, /*init_const_expr_p=*/false,
26552 NULL_TREE, attributes);
26554 /* Add the instance variable. */
26555 if (decl != error_mark_node && decl != NULL_TREE)
26556 objc_add_instance_variable (decl);
26558 /* Reset PREFIX_ATTRIBUTES. */
26559 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26560 attributes = TREE_CHAIN (attributes);
26561 if (attributes)
26562 TREE_CHAIN (attributes) = NULL_TREE;
26564 token = cp_lexer_peek_token (parser->lexer);
26566 if (token->type == CPP_COMMA)
26568 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26569 continue;
26571 break;
26574 cp_parser_consume_semicolon_at_end_of_statement (parser);
26575 token = cp_lexer_peek_token (parser->lexer);
26578 if (token->keyword == RID_AT_END)
26579 cp_parser_error (parser, "expected %<}%>");
26581 /* Do not consume the RID_AT_END, so it will be read again as terminating
26582 the @interface of @implementation. */
26583 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26584 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26586 /* For historical reasons, we accept an optional semicolon. */
26587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26588 cp_lexer_consume_token (parser->lexer);
26591 /* Parse an Objective-C protocol declaration. */
26593 static void
26594 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26596 tree proto, protorefs;
26597 cp_token *tok;
26599 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26602 tok = cp_lexer_peek_token (parser->lexer);
26603 error_at (tok->location, "identifier expected after %<@protocol%>");
26604 cp_parser_consume_semicolon_at_end_of_statement (parser);
26605 return;
26608 /* See if we have a forward declaration or a definition. */
26609 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26611 /* Try a forward declaration first. */
26612 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26614 while (true)
26616 tree id;
26618 id = cp_parser_identifier (parser);
26619 if (id == error_mark_node)
26620 break;
26622 objc_declare_protocol (id, attributes);
26624 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26625 cp_lexer_consume_token (parser->lexer);
26626 else
26627 break;
26629 cp_parser_consume_semicolon_at_end_of_statement (parser);
26632 /* Ok, we got a full-fledged definition (or at least should). */
26633 else
26635 proto = cp_parser_identifier (parser);
26636 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26637 objc_start_protocol (proto, protorefs, attributes);
26638 cp_parser_objc_method_prototype_list (parser);
26642 /* Parse an Objective-C superclass or category. */
26644 static void
26645 cp_parser_objc_superclass_or_category (cp_parser *parser,
26646 bool iface_p,
26647 tree *super,
26648 tree *categ, bool *is_class_extension)
26650 cp_token *next = cp_lexer_peek_token (parser->lexer);
26652 *super = *categ = NULL_TREE;
26653 *is_class_extension = false;
26654 if (next->type == CPP_COLON)
26656 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26657 *super = cp_parser_identifier (parser);
26659 else if (next->type == CPP_OPEN_PAREN)
26661 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26663 /* If there is no category name, and this is an @interface, we
26664 have a class extension. */
26665 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26667 *categ = NULL_TREE;
26668 *is_class_extension = true;
26670 else
26671 *categ = cp_parser_identifier (parser);
26673 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26677 /* Parse an Objective-C class interface. */
26679 static void
26680 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26682 tree name, super, categ, protos;
26683 bool is_class_extension;
26685 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26686 name = cp_parser_identifier (parser);
26687 if (name == error_mark_node)
26689 /* It's hard to recover because even if valid @interface stuff
26690 is to follow, we can't compile it (or validate it) if we
26691 don't even know which class it refers to. Let's assume this
26692 was a stray '@interface' token in the stream and skip it.
26694 return;
26696 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26697 &is_class_extension);
26698 protos = cp_parser_objc_protocol_refs_opt (parser);
26700 /* We have either a class or a category on our hands. */
26701 if (categ || is_class_extension)
26702 objc_start_category_interface (name, categ, protos, attributes);
26703 else
26705 objc_start_class_interface (name, super, protos, attributes);
26706 /* Handle instance variable declarations, if any. */
26707 cp_parser_objc_class_ivars (parser);
26708 objc_continue_interface ();
26711 cp_parser_objc_method_prototype_list (parser);
26714 /* Parse an Objective-C class implementation. */
26716 static void
26717 cp_parser_objc_class_implementation (cp_parser* parser)
26719 tree name, super, categ;
26720 bool is_class_extension;
26722 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26723 name = cp_parser_identifier (parser);
26724 if (name == error_mark_node)
26726 /* It's hard to recover because even if valid @implementation
26727 stuff is to follow, we can't compile it (or validate it) if
26728 we don't even know which class it refers to. Let's assume
26729 this was a stray '@implementation' token in the stream and
26730 skip it.
26732 return;
26734 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26735 &is_class_extension);
26737 /* We have either a class or a category on our hands. */
26738 if (categ)
26739 objc_start_category_implementation (name, categ);
26740 else
26742 objc_start_class_implementation (name, super);
26743 /* Handle instance variable declarations, if any. */
26744 cp_parser_objc_class_ivars (parser);
26745 objc_continue_implementation ();
26748 cp_parser_objc_method_definition_list (parser);
26751 /* Consume the @end token and finish off the implementation. */
26753 static void
26754 cp_parser_objc_end_implementation (cp_parser* parser)
26756 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26757 objc_finish_implementation ();
26760 /* Parse an Objective-C declaration. */
26762 static void
26763 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26765 /* Try to figure out what kind of declaration is present. */
26766 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26768 if (attributes)
26769 switch (kwd->keyword)
26771 case RID_AT_ALIAS:
26772 case RID_AT_CLASS:
26773 case RID_AT_END:
26774 error_at (kwd->location, "attributes may not be specified before"
26775 " the %<@%D%> Objective-C++ keyword",
26776 kwd->u.value);
26777 attributes = NULL;
26778 break;
26779 case RID_AT_IMPLEMENTATION:
26780 warning_at (kwd->location, OPT_Wattributes,
26781 "prefix attributes are ignored before %<@%D%>",
26782 kwd->u.value);
26783 attributes = NULL;
26784 default:
26785 break;
26788 switch (kwd->keyword)
26790 case RID_AT_ALIAS:
26791 cp_parser_objc_alias_declaration (parser);
26792 break;
26793 case RID_AT_CLASS:
26794 cp_parser_objc_class_declaration (parser);
26795 break;
26796 case RID_AT_PROTOCOL:
26797 cp_parser_objc_protocol_declaration (parser, attributes);
26798 break;
26799 case RID_AT_INTERFACE:
26800 cp_parser_objc_class_interface (parser, attributes);
26801 break;
26802 case RID_AT_IMPLEMENTATION:
26803 cp_parser_objc_class_implementation (parser);
26804 break;
26805 case RID_AT_END:
26806 cp_parser_objc_end_implementation (parser);
26807 break;
26808 default:
26809 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26810 kwd->u.value);
26811 cp_parser_skip_to_end_of_block_or_statement (parser);
26815 /* Parse an Objective-C try-catch-finally statement.
26817 objc-try-catch-finally-stmt:
26818 @try compound-statement objc-catch-clause-seq [opt]
26819 objc-finally-clause [opt]
26821 objc-catch-clause-seq:
26822 objc-catch-clause objc-catch-clause-seq [opt]
26824 objc-catch-clause:
26825 @catch ( objc-exception-declaration ) compound-statement
26827 objc-finally-clause:
26828 @finally compound-statement
26830 objc-exception-declaration:
26831 parameter-declaration
26832 '...'
26834 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26836 Returns NULL_TREE.
26838 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26839 for C. Keep them in sync. */
26841 static tree
26842 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26844 location_t location;
26845 tree stmt;
26847 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26848 location = cp_lexer_peek_token (parser->lexer)->location;
26849 objc_maybe_warn_exceptions (location);
26850 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26851 node, lest it get absorbed into the surrounding block. */
26852 stmt = push_stmt_list ();
26853 cp_parser_compound_statement (parser, NULL, false, false);
26854 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26856 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26858 cp_parameter_declarator *parm;
26859 tree parameter_declaration = error_mark_node;
26860 bool seen_open_paren = false;
26862 cp_lexer_consume_token (parser->lexer);
26863 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26864 seen_open_paren = true;
26865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26867 /* We have "@catch (...)" (where the '...' are literally
26868 what is in the code). Skip the '...'.
26869 parameter_declaration is set to NULL_TREE, and
26870 objc_being_catch_clauses() knows that that means
26871 '...'. */
26872 cp_lexer_consume_token (parser->lexer);
26873 parameter_declaration = NULL_TREE;
26875 else
26877 /* We have "@catch (NSException *exception)" or something
26878 like that. Parse the parameter declaration. */
26879 parm = cp_parser_parameter_declaration (parser, false, NULL);
26880 if (parm == NULL)
26881 parameter_declaration = error_mark_node;
26882 else
26883 parameter_declaration = grokdeclarator (parm->declarator,
26884 &parm->decl_specifiers,
26885 PARM, /*initialized=*/0,
26886 /*attrlist=*/NULL);
26888 if (seen_open_paren)
26889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26890 else
26892 /* If there was no open parenthesis, we are recovering from
26893 an error, and we are trying to figure out what mistake
26894 the user has made. */
26896 /* If there is an immediate closing parenthesis, the user
26897 probably forgot the opening one (ie, they typed "@catch
26898 NSException *e)". Parse the closing parenthesis and keep
26899 going. */
26900 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26901 cp_lexer_consume_token (parser->lexer);
26903 /* If these is no immediate closing parenthesis, the user
26904 probably doesn't know that parenthesis are required at
26905 all (ie, they typed "@catch NSException *e"). So, just
26906 forget about the closing parenthesis and keep going. */
26908 objc_begin_catch_clause (parameter_declaration);
26909 cp_parser_compound_statement (parser, NULL, false, false);
26910 objc_finish_catch_clause ();
26912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26914 cp_lexer_consume_token (parser->lexer);
26915 location = cp_lexer_peek_token (parser->lexer)->location;
26916 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26917 node, lest it get absorbed into the surrounding block. */
26918 stmt = push_stmt_list ();
26919 cp_parser_compound_statement (parser, NULL, false, false);
26920 objc_build_finally_clause (location, pop_stmt_list (stmt));
26923 return objc_finish_try_stmt ();
26926 /* Parse an Objective-C synchronized statement.
26928 objc-synchronized-stmt:
26929 @synchronized ( expression ) compound-statement
26931 Returns NULL_TREE. */
26933 static tree
26934 cp_parser_objc_synchronized_statement (cp_parser *parser)
26936 location_t location;
26937 tree lock, stmt;
26939 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26941 location = cp_lexer_peek_token (parser->lexer)->location;
26942 objc_maybe_warn_exceptions (location);
26943 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26944 lock = cp_parser_expression (parser);
26945 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26947 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26948 node, lest it get absorbed into the surrounding block. */
26949 stmt = push_stmt_list ();
26950 cp_parser_compound_statement (parser, NULL, false, false);
26952 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26955 /* Parse an Objective-C throw statement.
26957 objc-throw-stmt:
26958 @throw assignment-expression [opt] ;
26960 Returns a constructed '@throw' statement. */
26962 static tree
26963 cp_parser_objc_throw_statement (cp_parser *parser)
26965 tree expr = NULL_TREE;
26966 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26968 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26971 expr = cp_parser_expression (parser);
26973 cp_parser_consume_semicolon_at_end_of_statement (parser);
26975 return objc_build_throw_stmt (loc, expr);
26978 /* Parse an Objective-C statement. */
26980 static tree
26981 cp_parser_objc_statement (cp_parser * parser)
26983 /* Try to figure out what kind of declaration is present. */
26984 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26986 switch (kwd->keyword)
26988 case RID_AT_TRY:
26989 return cp_parser_objc_try_catch_finally_statement (parser);
26990 case RID_AT_SYNCHRONIZED:
26991 return cp_parser_objc_synchronized_statement (parser);
26992 case RID_AT_THROW:
26993 return cp_parser_objc_throw_statement (parser);
26994 default:
26995 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26996 kwd->u.value);
26997 cp_parser_skip_to_end_of_block_or_statement (parser);
27000 return error_mark_node;
27003 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27004 look ahead to see if an objc keyword follows the attributes. This
27005 is to detect the use of prefix attributes on ObjC @interface and
27006 @protocol. */
27008 static bool
27009 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27011 cp_lexer_save_tokens (parser->lexer);
27012 *attrib = cp_parser_attributes_opt (parser);
27013 gcc_assert (*attrib);
27014 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27016 cp_lexer_commit_tokens (parser->lexer);
27017 return true;
27019 cp_lexer_rollback_tokens (parser->lexer);
27020 return false;
27023 /* This routine is a minimal replacement for
27024 c_parser_struct_declaration () used when parsing the list of
27025 types/names or ObjC++ properties. For example, when parsing the
27026 code
27028 @property (readonly) int a, b, c;
27030 this function is responsible for parsing "int a, int b, int c" and
27031 returning the declarations as CHAIN of DECLs.
27033 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27034 similar parsing. */
27035 static tree
27036 cp_parser_objc_struct_declaration (cp_parser *parser)
27038 tree decls = NULL_TREE;
27039 cp_decl_specifier_seq declspecs;
27040 int decl_class_or_enum_p;
27041 tree prefix_attributes;
27043 cp_parser_decl_specifier_seq (parser,
27044 CP_PARSER_FLAGS_NONE,
27045 &declspecs,
27046 &decl_class_or_enum_p);
27048 if (declspecs.type == error_mark_node)
27049 return error_mark_node;
27051 /* auto, register, static, extern, mutable. */
27052 if (declspecs.storage_class != sc_none)
27054 cp_parser_error (parser, "invalid type for property");
27055 declspecs.storage_class = sc_none;
27058 /* thread_local. */
27059 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27061 cp_parser_error (parser, "invalid type for property");
27062 declspecs.locations[ds_thread] = 0;
27065 /* typedef. */
27066 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27068 cp_parser_error (parser, "invalid type for property");
27069 declspecs.locations[ds_typedef] = 0;
27072 prefix_attributes = declspecs.attributes;
27073 declspecs.attributes = NULL_TREE;
27075 /* Keep going until we hit the `;' at the end of the declaration. */
27076 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27078 tree attributes, first_attribute, decl;
27079 cp_declarator *declarator;
27080 cp_token *token;
27082 /* Parse the declarator. */
27083 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27084 NULL, NULL, false, false);
27086 /* Look for attributes that apply to the ivar. */
27087 attributes = cp_parser_attributes_opt (parser);
27088 /* Remember which attributes are prefix attributes and
27089 which are not. */
27090 first_attribute = attributes;
27091 /* Combine the attributes. */
27092 attributes = chainon (prefix_attributes, attributes);
27094 decl = grokfield (declarator, &declspecs,
27095 NULL_TREE, /*init_const_expr_p=*/false,
27096 NULL_TREE, attributes);
27098 if (decl == error_mark_node || decl == NULL_TREE)
27099 return error_mark_node;
27101 /* Reset PREFIX_ATTRIBUTES. */
27102 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27103 attributes = TREE_CHAIN (attributes);
27104 if (attributes)
27105 TREE_CHAIN (attributes) = NULL_TREE;
27107 DECL_CHAIN (decl) = decls;
27108 decls = decl;
27110 token = cp_lexer_peek_token (parser->lexer);
27111 if (token->type == CPP_COMMA)
27113 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27114 continue;
27116 else
27117 break;
27119 return decls;
27122 /* Parse an Objective-C @property declaration. The syntax is:
27124 objc-property-declaration:
27125 '@property' objc-property-attributes[opt] struct-declaration ;
27127 objc-property-attributes:
27128 '(' objc-property-attribute-list ')'
27130 objc-property-attribute-list:
27131 objc-property-attribute
27132 objc-property-attribute-list, objc-property-attribute
27134 objc-property-attribute
27135 'getter' = identifier
27136 'setter' = identifier
27137 'readonly'
27138 'readwrite'
27139 'assign'
27140 'retain'
27141 'copy'
27142 'nonatomic'
27144 For example:
27145 @property NSString *name;
27146 @property (readonly) id object;
27147 @property (retain, nonatomic, getter=getTheName) id name;
27148 @property int a, b, c;
27150 PS: This function is identical to
27151 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27152 static void
27153 cp_parser_objc_at_property_declaration (cp_parser *parser)
27155 /* The following variables hold the attributes of the properties as
27156 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27157 seen. When we see an attribute, we set them to 'true' (if they
27158 are boolean properties) or to the identifier (if they have an
27159 argument, ie, for getter and setter). Note that here we only
27160 parse the list of attributes, check the syntax and accumulate the
27161 attributes that we find. objc_add_property_declaration() will
27162 then process the information. */
27163 bool property_assign = false;
27164 bool property_copy = false;
27165 tree property_getter_ident = NULL_TREE;
27166 bool property_nonatomic = false;
27167 bool property_readonly = false;
27168 bool property_readwrite = false;
27169 bool property_retain = false;
27170 tree property_setter_ident = NULL_TREE;
27172 /* 'properties' is the list of properties that we read. Usually a
27173 single one, but maybe more (eg, in "@property int a, b, c;" there
27174 are three). */
27175 tree properties;
27176 location_t loc;
27178 loc = cp_lexer_peek_token (parser->lexer)->location;
27180 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27182 /* Parse the optional attribute list... */
27183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27185 /* Eat the '('. */
27186 cp_lexer_consume_token (parser->lexer);
27188 while (true)
27190 bool syntax_error = false;
27191 cp_token *token = cp_lexer_peek_token (parser->lexer);
27192 enum rid keyword;
27194 if (token->type != CPP_NAME)
27196 cp_parser_error (parser, "expected identifier");
27197 break;
27199 keyword = C_RID_CODE (token->u.value);
27200 cp_lexer_consume_token (parser->lexer);
27201 switch (keyword)
27203 case RID_ASSIGN: property_assign = true; break;
27204 case RID_COPY: property_copy = true; break;
27205 case RID_NONATOMIC: property_nonatomic = true; break;
27206 case RID_READONLY: property_readonly = true; break;
27207 case RID_READWRITE: property_readwrite = true; break;
27208 case RID_RETAIN: property_retain = true; break;
27210 case RID_GETTER:
27211 case RID_SETTER:
27212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27214 if (keyword == RID_GETTER)
27215 cp_parser_error (parser,
27216 "missing %<=%> (after %<getter%> attribute)");
27217 else
27218 cp_parser_error (parser,
27219 "missing %<=%> (after %<setter%> attribute)");
27220 syntax_error = true;
27221 break;
27223 cp_lexer_consume_token (parser->lexer); /* eat the = */
27224 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27226 cp_parser_error (parser, "expected identifier");
27227 syntax_error = true;
27228 break;
27230 if (keyword == RID_SETTER)
27232 if (property_setter_ident != NULL_TREE)
27234 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27235 cp_lexer_consume_token (parser->lexer);
27237 else
27238 property_setter_ident = cp_parser_objc_selector (parser);
27239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27240 cp_parser_error (parser, "setter name must terminate with %<:%>");
27241 else
27242 cp_lexer_consume_token (parser->lexer);
27244 else
27246 if (property_getter_ident != NULL_TREE)
27248 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27249 cp_lexer_consume_token (parser->lexer);
27251 else
27252 property_getter_ident = cp_parser_objc_selector (parser);
27254 break;
27255 default:
27256 cp_parser_error (parser, "unknown property attribute");
27257 syntax_error = true;
27258 break;
27261 if (syntax_error)
27262 break;
27264 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27265 cp_lexer_consume_token (parser->lexer);
27266 else
27267 break;
27270 /* FIXME: "@property (setter, assign);" will generate a spurious
27271 "error: expected ‘)’ before ‘,’ token". This is because
27272 cp_parser_require, unlike the C counterpart, will produce an
27273 error even if we are in error recovery. */
27274 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27276 cp_parser_skip_to_closing_parenthesis (parser,
27277 /*recovering=*/true,
27278 /*or_comma=*/false,
27279 /*consume_paren=*/true);
27283 /* ... and the property declaration(s). */
27284 properties = cp_parser_objc_struct_declaration (parser);
27286 if (properties == error_mark_node)
27288 cp_parser_skip_to_end_of_statement (parser);
27289 /* If the next token is now a `;', consume it. */
27290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27291 cp_lexer_consume_token (parser->lexer);
27292 return;
27295 if (properties == NULL_TREE)
27296 cp_parser_error (parser, "expected identifier");
27297 else
27299 /* Comma-separated properties are chained together in
27300 reverse order; add them one by one. */
27301 properties = nreverse (properties);
27303 for (; properties; properties = TREE_CHAIN (properties))
27304 objc_add_property_declaration (loc, copy_node (properties),
27305 property_readonly, property_readwrite,
27306 property_assign, property_retain,
27307 property_copy, property_nonatomic,
27308 property_getter_ident, property_setter_ident);
27311 cp_parser_consume_semicolon_at_end_of_statement (parser);
27314 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27316 objc-synthesize-declaration:
27317 @synthesize objc-synthesize-identifier-list ;
27319 objc-synthesize-identifier-list:
27320 objc-synthesize-identifier
27321 objc-synthesize-identifier-list, objc-synthesize-identifier
27323 objc-synthesize-identifier
27324 identifier
27325 identifier = identifier
27327 For example:
27328 @synthesize MyProperty;
27329 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27331 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27332 for C. Keep them in sync.
27334 static void
27335 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27337 tree list = NULL_TREE;
27338 location_t loc;
27339 loc = cp_lexer_peek_token (parser->lexer)->location;
27341 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27342 while (true)
27344 tree property, ivar;
27345 property = cp_parser_identifier (parser);
27346 if (property == error_mark_node)
27348 cp_parser_consume_semicolon_at_end_of_statement (parser);
27349 return;
27351 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27353 cp_lexer_consume_token (parser->lexer);
27354 ivar = cp_parser_identifier (parser);
27355 if (ivar == error_mark_node)
27357 cp_parser_consume_semicolon_at_end_of_statement (parser);
27358 return;
27361 else
27362 ivar = NULL_TREE;
27363 list = chainon (list, build_tree_list (ivar, property));
27364 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27365 cp_lexer_consume_token (parser->lexer);
27366 else
27367 break;
27369 cp_parser_consume_semicolon_at_end_of_statement (parser);
27370 objc_add_synthesize_declaration (loc, list);
27373 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27375 objc-dynamic-declaration:
27376 @dynamic identifier-list ;
27378 For example:
27379 @dynamic MyProperty;
27380 @dynamic MyProperty, AnotherProperty;
27382 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27383 for C. Keep them in sync.
27385 static void
27386 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27388 tree list = NULL_TREE;
27389 location_t loc;
27390 loc = cp_lexer_peek_token (parser->lexer)->location;
27392 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27393 while (true)
27395 tree property;
27396 property = cp_parser_identifier (parser);
27397 if (property == error_mark_node)
27399 cp_parser_consume_semicolon_at_end_of_statement (parser);
27400 return;
27402 list = chainon (list, build_tree_list (NULL, property));
27403 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27404 cp_lexer_consume_token (parser->lexer);
27405 else
27406 break;
27408 cp_parser_consume_semicolon_at_end_of_statement (parser);
27409 objc_add_dynamic_declaration (loc, list);
27413 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27415 /* Returns name of the next clause.
27416 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27417 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27418 returned and the token is consumed. */
27420 static pragma_omp_clause
27421 cp_parser_omp_clause_name (cp_parser *parser)
27423 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27426 result = PRAGMA_OMP_CLAUSE_IF;
27427 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27428 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27429 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27430 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27431 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27432 result = PRAGMA_OMP_CLAUSE_FOR;
27433 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27435 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27436 const char *p = IDENTIFIER_POINTER (id);
27438 switch (p[0])
27440 case 'a':
27441 if (!strcmp ("aligned", p))
27442 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27443 break;
27444 case 'c':
27445 if (!strcmp ("collapse", p))
27446 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27447 else if (!strcmp ("copyin", p))
27448 result = PRAGMA_OMP_CLAUSE_COPYIN;
27449 else if (!strcmp ("copyprivate", p))
27450 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27451 break;
27452 case 'd':
27453 if (!strcmp ("depend", p))
27454 result = PRAGMA_OMP_CLAUSE_DEPEND;
27455 else if (!strcmp ("device", p))
27456 result = PRAGMA_OMP_CLAUSE_DEVICE;
27457 else if (!strcmp ("dist_schedule", p))
27458 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27459 break;
27460 case 'f':
27461 if (!strcmp ("final", p))
27462 result = PRAGMA_OMP_CLAUSE_FINAL;
27463 else if (!strcmp ("firstprivate", p))
27464 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27465 else if (!strcmp ("from", p))
27466 result = PRAGMA_OMP_CLAUSE_FROM;
27467 break;
27468 case 'i':
27469 if (!strcmp ("inbranch", p))
27470 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27471 break;
27472 case 'l':
27473 if (!strcmp ("lastprivate", p))
27474 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27475 else if (!strcmp ("linear", p))
27476 result = PRAGMA_OMP_CLAUSE_LINEAR;
27477 break;
27478 case 'm':
27479 if (!strcmp ("map", p))
27480 result = PRAGMA_OMP_CLAUSE_MAP;
27481 else if (!strcmp ("mergeable", p))
27482 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27483 else if (flag_cilkplus && !strcmp ("mask", p))
27484 result = PRAGMA_CILK_CLAUSE_MASK;
27485 break;
27486 case 'n':
27487 if (!strcmp ("notinbranch", p))
27488 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27489 else if (!strcmp ("nowait", p))
27490 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27491 else if (flag_cilkplus && !strcmp ("nomask", p))
27492 result = PRAGMA_CILK_CLAUSE_NOMASK;
27493 else if (!strcmp ("num_teams", p))
27494 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27495 else if (!strcmp ("num_threads", p))
27496 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27497 break;
27498 case 'o':
27499 if (!strcmp ("ordered", p))
27500 result = PRAGMA_OMP_CLAUSE_ORDERED;
27501 break;
27502 case 'p':
27503 if (!strcmp ("parallel", p))
27504 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27505 else if (!strcmp ("proc_bind", p))
27506 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27507 break;
27508 case 'r':
27509 if (!strcmp ("reduction", p))
27510 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27511 break;
27512 case 's':
27513 if (!strcmp ("safelen", p))
27514 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27515 else if (!strcmp ("schedule", p))
27516 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27517 else if (!strcmp ("sections", p))
27518 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27519 else if (!strcmp ("shared", p))
27520 result = PRAGMA_OMP_CLAUSE_SHARED;
27521 else if (!strcmp ("simdlen", p))
27522 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27523 break;
27524 case 't':
27525 if (!strcmp ("taskgroup", p))
27526 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27527 else if (!strcmp ("thread_limit", p))
27528 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27529 else if (!strcmp ("to", p))
27530 result = PRAGMA_OMP_CLAUSE_TO;
27531 break;
27532 case 'u':
27533 if (!strcmp ("uniform", p))
27534 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27535 else if (!strcmp ("untied", p))
27536 result = PRAGMA_OMP_CLAUSE_UNTIED;
27537 break;
27538 case 'v':
27539 if (flag_cilkplus && !strcmp ("vectorlength", p))
27540 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27541 break;
27545 if (result != PRAGMA_OMP_CLAUSE_NONE)
27546 cp_lexer_consume_token (parser->lexer);
27548 return result;
27551 /* Validate that a clause of the given type does not already exist. */
27553 static void
27554 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27555 const char *name, location_t location)
27557 tree c;
27559 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27560 if (OMP_CLAUSE_CODE (c) == code)
27562 error_at (location, "too many %qs clauses", name);
27563 break;
27567 /* OpenMP 2.5:
27568 variable-list:
27569 identifier
27570 variable-list , identifier
27572 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27573 colon). An opening parenthesis will have been consumed by the caller.
27575 If KIND is nonzero, create the appropriate node and install the decl
27576 in OMP_CLAUSE_DECL and add the node to the head of the list.
27578 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27579 return the list created.
27581 COLON can be NULL if only closing parenthesis should end the list,
27582 or pointer to bool which will receive false if the list is terminated
27583 by closing parenthesis or true if the list is terminated by colon. */
27585 static tree
27586 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27587 tree list, bool *colon)
27589 cp_token *token;
27590 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27591 if (colon)
27593 parser->colon_corrects_to_scope_p = false;
27594 *colon = false;
27596 while (1)
27598 tree name, decl;
27600 token = cp_lexer_peek_token (parser->lexer);
27601 name = cp_parser_id_expression (parser, /*template_p=*/false,
27602 /*check_dependency_p=*/true,
27603 /*template_p=*/NULL,
27604 /*declarator_p=*/false,
27605 /*optional_p=*/false);
27606 if (name == error_mark_node)
27607 goto skip_comma;
27609 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27610 if (decl == error_mark_node)
27611 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27612 token->location);
27613 else if (kind != 0)
27615 switch (kind)
27617 case OMP_CLAUSE_MAP:
27618 case OMP_CLAUSE_FROM:
27619 case OMP_CLAUSE_TO:
27620 case OMP_CLAUSE_DEPEND:
27621 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27623 tree low_bound = NULL_TREE, length = NULL_TREE;
27625 parser->colon_corrects_to_scope_p = false;
27626 cp_lexer_consume_token (parser->lexer);
27627 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27628 low_bound = cp_parser_expression (parser);
27629 if (!colon)
27630 parser->colon_corrects_to_scope_p
27631 = saved_colon_corrects_to_scope_p;
27632 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27633 length = integer_one_node;
27634 else
27636 /* Look for `:'. */
27637 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27638 goto skip_comma;
27639 if (!cp_lexer_next_token_is (parser->lexer,
27640 CPP_CLOSE_SQUARE))
27641 length = cp_parser_expression (parser);
27643 /* Look for the closing `]'. */
27644 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27645 RT_CLOSE_SQUARE))
27646 goto skip_comma;
27647 decl = tree_cons (low_bound, length, decl);
27649 break;
27650 default:
27651 break;
27654 tree u = build_omp_clause (token->location, kind);
27655 OMP_CLAUSE_DECL (u) = decl;
27656 OMP_CLAUSE_CHAIN (u) = list;
27657 list = u;
27659 else
27660 list = tree_cons (decl, NULL_TREE, list);
27662 get_comma:
27663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27664 break;
27665 cp_lexer_consume_token (parser->lexer);
27668 if (colon)
27669 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27671 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27673 *colon = true;
27674 cp_parser_require (parser, CPP_COLON, RT_COLON);
27675 return list;
27678 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27680 int ending;
27682 /* Try to resync to an unnested comma. Copied from
27683 cp_parser_parenthesized_expression_list. */
27684 skip_comma:
27685 if (colon)
27686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27687 ending = cp_parser_skip_to_closing_parenthesis (parser,
27688 /*recovering=*/true,
27689 /*or_comma=*/true,
27690 /*consume_paren=*/true);
27691 if (ending < 0)
27692 goto get_comma;
27695 return list;
27698 /* Similarly, but expect leading and trailing parenthesis. This is a very
27699 common case for omp clauses. */
27701 static tree
27702 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27704 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27705 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27706 return list;
27709 /* OpenMP 3.0:
27710 collapse ( constant-expression ) */
27712 static tree
27713 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27715 tree c, num;
27716 location_t loc;
27717 HOST_WIDE_INT n;
27719 loc = cp_lexer_peek_token (parser->lexer)->location;
27720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27721 return list;
27723 num = cp_parser_constant_expression (parser);
27725 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27727 /*or_comma=*/false,
27728 /*consume_paren=*/true);
27730 if (num == error_mark_node)
27731 return list;
27732 num = fold_non_dependent_expr (num);
27733 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27734 || !tree_fits_shwi_p (num)
27735 || (n = tree_to_shwi (num)) <= 0
27736 || (int) n != n)
27738 error_at (loc, "collapse argument needs positive constant integer expression");
27739 return list;
27742 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27743 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27744 OMP_CLAUSE_CHAIN (c) = list;
27745 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27747 return c;
27750 /* OpenMP 2.5:
27751 default ( shared | none ) */
27753 static tree
27754 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27756 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27757 tree c;
27759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27760 return list;
27761 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27764 const char *p = IDENTIFIER_POINTER (id);
27766 switch (p[0])
27768 case 'n':
27769 if (strcmp ("none", p) != 0)
27770 goto invalid_kind;
27771 kind = OMP_CLAUSE_DEFAULT_NONE;
27772 break;
27774 case 's':
27775 if (strcmp ("shared", p) != 0)
27776 goto invalid_kind;
27777 kind = OMP_CLAUSE_DEFAULT_SHARED;
27778 break;
27780 default:
27781 goto invalid_kind;
27784 cp_lexer_consume_token (parser->lexer);
27786 else
27788 invalid_kind:
27789 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27792 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27793 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27794 /*or_comma=*/false,
27795 /*consume_paren=*/true);
27797 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27798 return list;
27800 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27801 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27802 OMP_CLAUSE_CHAIN (c) = list;
27803 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27805 return c;
27808 /* OpenMP 3.1:
27809 final ( expression ) */
27811 static tree
27812 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27814 tree t, c;
27816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27817 return list;
27819 t = cp_parser_condition (parser);
27821 if (t == error_mark_node
27822 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27824 /*or_comma=*/false,
27825 /*consume_paren=*/true);
27827 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27829 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27830 OMP_CLAUSE_FINAL_EXPR (c) = t;
27831 OMP_CLAUSE_CHAIN (c) = list;
27833 return c;
27836 /* OpenMP 2.5:
27837 if ( expression ) */
27839 static tree
27840 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27842 tree t, c;
27844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27845 return list;
27847 t = cp_parser_condition (parser);
27849 if (t == error_mark_node
27850 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27851 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27852 /*or_comma=*/false,
27853 /*consume_paren=*/true);
27855 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27857 c = build_omp_clause (location, OMP_CLAUSE_IF);
27858 OMP_CLAUSE_IF_EXPR (c) = t;
27859 OMP_CLAUSE_CHAIN (c) = list;
27861 return c;
27864 /* OpenMP 3.1:
27865 mergeable */
27867 static tree
27868 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27869 tree list, location_t location)
27871 tree c;
27873 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27874 location);
27876 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27877 OMP_CLAUSE_CHAIN (c) = list;
27878 return c;
27881 /* OpenMP 2.5:
27882 nowait */
27884 static tree
27885 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27886 tree list, location_t location)
27888 tree c;
27890 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27892 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27893 OMP_CLAUSE_CHAIN (c) = list;
27894 return c;
27897 /* OpenMP 2.5:
27898 num_threads ( expression ) */
27900 static tree
27901 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27902 location_t location)
27904 tree t, c;
27906 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27907 return list;
27909 t = cp_parser_expression (parser);
27911 if (t == error_mark_node
27912 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27914 /*or_comma=*/false,
27915 /*consume_paren=*/true);
27917 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27918 "num_threads", location);
27920 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27921 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27922 OMP_CLAUSE_CHAIN (c) = list;
27924 return c;
27927 /* OpenMP 2.5:
27928 ordered */
27930 static tree
27931 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27932 tree list, location_t location)
27934 tree c;
27936 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27937 "ordered", location);
27939 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27940 OMP_CLAUSE_CHAIN (c) = list;
27941 return c;
27944 /* OpenMP 2.5:
27945 reduction ( reduction-operator : variable-list )
27947 reduction-operator:
27948 One of: + * - & ^ | && ||
27950 OpenMP 3.1:
27952 reduction-operator:
27953 One of: + * - & ^ | && || min max
27955 OpenMP 4.0:
27957 reduction-operator:
27958 One of: + * - & ^ | && ||
27959 id-expression */
27961 static tree
27962 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27964 enum tree_code code = ERROR_MARK;
27965 tree nlist, c, id = NULL_TREE;
27967 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27968 return list;
27970 switch (cp_lexer_peek_token (parser->lexer)->type)
27972 case CPP_PLUS: code = PLUS_EXPR; break;
27973 case CPP_MULT: code = MULT_EXPR; break;
27974 case CPP_MINUS: code = MINUS_EXPR; break;
27975 case CPP_AND: code = BIT_AND_EXPR; break;
27976 case CPP_XOR: code = BIT_XOR_EXPR; break;
27977 case CPP_OR: code = BIT_IOR_EXPR; break;
27978 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27979 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27980 default: break;
27983 if (code != ERROR_MARK)
27984 cp_lexer_consume_token (parser->lexer);
27985 else
27987 bool saved_colon_corrects_to_scope_p;
27988 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27989 parser->colon_corrects_to_scope_p = false;
27990 id = cp_parser_id_expression (parser, /*template_p=*/false,
27991 /*check_dependency_p=*/true,
27992 /*template_p=*/NULL,
27993 /*declarator_p=*/false,
27994 /*optional_p=*/false);
27995 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27996 if (identifier_p (id))
27998 const char *p = IDENTIFIER_POINTER (id);
28000 if (strcmp (p, "min") == 0)
28001 code = MIN_EXPR;
28002 else if (strcmp (p, "max") == 0)
28003 code = MAX_EXPR;
28004 else if (id == ansi_opname (PLUS_EXPR))
28005 code = PLUS_EXPR;
28006 else if (id == ansi_opname (MULT_EXPR))
28007 code = MULT_EXPR;
28008 else if (id == ansi_opname (MINUS_EXPR))
28009 code = MINUS_EXPR;
28010 else if (id == ansi_opname (BIT_AND_EXPR))
28011 code = BIT_AND_EXPR;
28012 else if (id == ansi_opname (BIT_IOR_EXPR))
28013 code = BIT_IOR_EXPR;
28014 else if (id == ansi_opname (BIT_XOR_EXPR))
28015 code = BIT_XOR_EXPR;
28016 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28017 code = TRUTH_ANDIF_EXPR;
28018 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28019 code = TRUTH_ORIF_EXPR;
28020 id = omp_reduction_id (code, id, NULL_TREE);
28021 tree scope = parser->scope;
28022 if (scope)
28023 id = build_qualified_name (NULL_TREE, scope, id, false);
28024 parser->scope = NULL_TREE;
28025 parser->qualifying_scope = NULL_TREE;
28026 parser->object_scope = NULL_TREE;
28028 else
28030 error ("invalid reduction-identifier");
28031 resync_fail:
28032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28033 /*or_comma=*/false,
28034 /*consume_paren=*/true);
28035 return list;
28039 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28040 goto resync_fail;
28042 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28043 NULL);
28044 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28046 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28047 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28050 return nlist;
28053 /* OpenMP 2.5:
28054 schedule ( schedule-kind )
28055 schedule ( schedule-kind , expression )
28057 schedule-kind:
28058 static | dynamic | guided | runtime | auto */
28060 static tree
28061 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28063 tree c, t;
28065 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28066 return list;
28068 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28073 const char *p = IDENTIFIER_POINTER (id);
28075 switch (p[0])
28077 case 'd':
28078 if (strcmp ("dynamic", p) != 0)
28079 goto invalid_kind;
28080 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28081 break;
28083 case 'g':
28084 if (strcmp ("guided", p) != 0)
28085 goto invalid_kind;
28086 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28087 break;
28089 case 'r':
28090 if (strcmp ("runtime", p) != 0)
28091 goto invalid_kind;
28092 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28093 break;
28095 default:
28096 goto invalid_kind;
28099 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28100 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28101 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28102 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28103 else
28104 goto invalid_kind;
28105 cp_lexer_consume_token (parser->lexer);
28107 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28109 cp_token *token;
28110 cp_lexer_consume_token (parser->lexer);
28112 token = cp_lexer_peek_token (parser->lexer);
28113 t = cp_parser_assignment_expression (parser);
28115 if (t == error_mark_node)
28116 goto resync_fail;
28117 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28118 error_at (token->location, "schedule %<runtime%> does not take "
28119 "a %<chunk_size%> parameter");
28120 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28121 error_at (token->location, "schedule %<auto%> does not take "
28122 "a %<chunk_size%> parameter");
28123 else
28124 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28127 goto resync_fail;
28129 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28130 goto resync_fail;
28132 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28133 OMP_CLAUSE_CHAIN (c) = list;
28134 return c;
28136 invalid_kind:
28137 cp_parser_error (parser, "invalid schedule kind");
28138 resync_fail:
28139 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28140 /*or_comma=*/false,
28141 /*consume_paren=*/true);
28142 return list;
28145 /* OpenMP 3.0:
28146 untied */
28148 static tree
28149 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28150 tree list, location_t location)
28152 tree c;
28154 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28156 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28157 OMP_CLAUSE_CHAIN (c) = list;
28158 return c;
28161 /* OpenMP 4.0:
28162 inbranch
28163 notinbranch */
28165 static tree
28166 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28167 tree list, location_t location)
28169 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28170 tree c = build_omp_clause (location, code);
28171 OMP_CLAUSE_CHAIN (c) = list;
28172 return c;
28175 /* OpenMP 4.0:
28176 parallel
28178 sections
28179 taskgroup */
28181 static tree
28182 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28183 enum omp_clause_code code,
28184 tree list, location_t location)
28186 tree c = build_omp_clause (location, code);
28187 OMP_CLAUSE_CHAIN (c) = list;
28188 return c;
28191 /* OpenMP 4.0:
28192 num_teams ( expression ) */
28194 static tree
28195 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28196 location_t location)
28198 tree t, c;
28200 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28201 return list;
28203 t = cp_parser_expression (parser);
28205 if (t == error_mark_node
28206 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28207 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28208 /*or_comma=*/false,
28209 /*consume_paren=*/true);
28211 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28212 "num_teams", location);
28214 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28215 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28216 OMP_CLAUSE_CHAIN (c) = list;
28218 return c;
28221 /* OpenMP 4.0:
28222 thread_limit ( expression ) */
28224 static tree
28225 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28226 location_t location)
28228 tree t, c;
28230 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28231 return list;
28233 t = cp_parser_expression (parser);
28235 if (t == error_mark_node
28236 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28238 /*or_comma=*/false,
28239 /*consume_paren=*/true);
28241 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28242 "thread_limit", location);
28244 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28245 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28246 OMP_CLAUSE_CHAIN (c) = list;
28248 return c;
28251 /* OpenMP 4.0:
28252 aligned ( variable-list )
28253 aligned ( variable-list : constant-expression ) */
28255 static tree
28256 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28258 tree nlist, c, alignment = NULL_TREE;
28259 bool colon;
28261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28262 return list;
28264 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28265 &colon);
28267 if (colon)
28269 alignment = cp_parser_constant_expression (parser);
28271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28273 /*or_comma=*/false,
28274 /*consume_paren=*/true);
28276 if (alignment == error_mark_node)
28277 alignment = NULL_TREE;
28280 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28281 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28283 return nlist;
28286 /* OpenMP 4.0:
28287 linear ( variable-list )
28288 linear ( variable-list : expression ) */
28290 static tree
28291 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28292 bool is_cilk_simd_fn)
28294 tree nlist, c, step = integer_one_node;
28295 bool colon;
28297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28298 return list;
28300 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28301 &colon);
28303 if (colon)
28305 step = cp_parser_expression (parser);
28307 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28309 sorry ("using parameters for %<linear%> step is not supported yet");
28310 step = integer_one_node;
28312 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28314 /*or_comma=*/false,
28315 /*consume_paren=*/true);
28317 if (step == error_mark_node)
28318 return list;
28321 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28322 OMP_CLAUSE_LINEAR_STEP (c) = step;
28324 return nlist;
28327 /* OpenMP 4.0:
28328 safelen ( constant-expression ) */
28330 static tree
28331 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28332 location_t location)
28334 tree t, c;
28336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28337 return list;
28339 t = cp_parser_constant_expression (parser);
28341 if (t == error_mark_node
28342 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28344 /*or_comma=*/false,
28345 /*consume_paren=*/true);
28347 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28349 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28350 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28351 OMP_CLAUSE_CHAIN (c) = list;
28353 return c;
28356 /* OpenMP 4.0:
28357 simdlen ( constant-expression ) */
28359 static tree
28360 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28361 location_t location)
28363 tree t, c;
28365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28366 return list;
28368 t = cp_parser_constant_expression (parser);
28370 if (t == error_mark_node
28371 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28372 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28373 /*or_comma=*/false,
28374 /*consume_paren=*/true);
28376 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28378 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28379 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28380 OMP_CLAUSE_CHAIN (c) = list;
28382 return c;
28385 /* OpenMP 4.0:
28386 depend ( depend-kind : variable-list )
28388 depend-kind:
28389 in | out | inout */
28391 static tree
28392 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28394 tree nlist, c;
28395 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28398 return list;
28400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28402 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28403 const char *p = IDENTIFIER_POINTER (id);
28405 if (strcmp ("in", p) == 0)
28406 kind = OMP_CLAUSE_DEPEND_IN;
28407 else if (strcmp ("inout", p) == 0)
28408 kind = OMP_CLAUSE_DEPEND_INOUT;
28409 else if (strcmp ("out", p) == 0)
28410 kind = OMP_CLAUSE_DEPEND_OUT;
28411 else
28412 goto invalid_kind;
28414 else
28415 goto invalid_kind;
28417 cp_lexer_consume_token (parser->lexer);
28418 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28419 goto resync_fail;
28421 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28422 NULL);
28424 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28425 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28427 return nlist;
28429 invalid_kind:
28430 cp_parser_error (parser, "invalid depend kind");
28431 resync_fail:
28432 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28433 /*or_comma=*/false,
28434 /*consume_paren=*/true);
28435 return list;
28438 /* OpenMP 4.0:
28439 map ( map-kind : variable-list )
28440 map ( variable-list )
28442 map-kind:
28443 alloc | to | from | tofrom */
28445 static tree
28446 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28448 tree nlist, c;
28449 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28451 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28452 return list;
28454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28455 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28457 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28458 const char *p = IDENTIFIER_POINTER (id);
28460 if (strcmp ("alloc", p) == 0)
28461 kind = OMP_CLAUSE_MAP_ALLOC;
28462 else if (strcmp ("to", p) == 0)
28463 kind = OMP_CLAUSE_MAP_TO;
28464 else if (strcmp ("from", p) == 0)
28465 kind = OMP_CLAUSE_MAP_FROM;
28466 else if (strcmp ("tofrom", p) == 0)
28467 kind = OMP_CLAUSE_MAP_TOFROM;
28468 else
28470 cp_parser_error (parser, "invalid map kind");
28471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28472 /*or_comma=*/false,
28473 /*consume_paren=*/true);
28474 return list;
28476 cp_lexer_consume_token (parser->lexer);
28477 cp_lexer_consume_token (parser->lexer);
28480 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28481 NULL);
28483 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28484 OMP_CLAUSE_MAP_KIND (c) = kind;
28486 return nlist;
28489 /* OpenMP 4.0:
28490 device ( expression ) */
28492 static tree
28493 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28494 location_t location)
28496 tree t, c;
28498 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28499 return list;
28501 t = cp_parser_expression (parser);
28503 if (t == error_mark_node
28504 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28506 /*or_comma=*/false,
28507 /*consume_paren=*/true);
28509 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28510 "device", location);
28512 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28513 OMP_CLAUSE_DEVICE_ID (c) = t;
28514 OMP_CLAUSE_CHAIN (c) = list;
28516 return c;
28519 /* OpenMP 4.0:
28520 dist_schedule ( static )
28521 dist_schedule ( static , expression ) */
28523 static tree
28524 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28525 location_t location)
28527 tree c, t;
28529 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28530 return list;
28532 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28534 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28535 goto invalid_kind;
28536 cp_lexer_consume_token (parser->lexer);
28538 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28540 cp_lexer_consume_token (parser->lexer);
28542 t = cp_parser_assignment_expression (parser);
28544 if (t == error_mark_node)
28545 goto resync_fail;
28546 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28548 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28549 goto resync_fail;
28551 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28552 goto resync_fail;
28554 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28555 location);
28556 OMP_CLAUSE_CHAIN (c) = list;
28557 return c;
28559 invalid_kind:
28560 cp_parser_error (parser, "invalid dist_schedule kind");
28561 resync_fail:
28562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28563 /*or_comma=*/false,
28564 /*consume_paren=*/true);
28565 return list;
28568 /* OpenMP 4.0:
28569 proc_bind ( proc-bind-kind )
28571 proc-bind-kind:
28572 master | close | spread */
28574 static tree
28575 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28576 location_t location)
28578 tree c;
28579 enum omp_clause_proc_bind_kind kind;
28581 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28582 return list;
28584 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28586 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28587 const char *p = IDENTIFIER_POINTER (id);
28589 if (strcmp ("master", p) == 0)
28590 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28591 else if (strcmp ("close", p) == 0)
28592 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28593 else if (strcmp ("spread", p) == 0)
28594 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28595 else
28596 goto invalid_kind;
28598 else
28599 goto invalid_kind;
28601 cp_lexer_consume_token (parser->lexer);
28602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28603 goto resync_fail;
28605 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28606 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28607 location);
28608 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28609 OMP_CLAUSE_CHAIN (c) = list;
28610 return c;
28612 invalid_kind:
28613 cp_parser_error (parser, "invalid depend kind");
28614 resync_fail:
28615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28616 /*or_comma=*/false,
28617 /*consume_paren=*/true);
28618 return list;
28621 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28622 is a bitmask in MASK. Return the list of clauses found; the result
28623 of clause default goes in *pdefault. */
28625 static tree
28626 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28627 const char *where, cp_token *pragma_tok,
28628 bool finish_p = true)
28630 tree clauses = NULL;
28631 bool first = true;
28632 cp_token *token = NULL;
28633 bool cilk_simd_fn = false;
28635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28637 pragma_omp_clause c_kind;
28638 const char *c_name;
28639 tree prev = clauses;
28641 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28642 cp_lexer_consume_token (parser->lexer);
28644 token = cp_lexer_peek_token (parser->lexer);
28645 c_kind = cp_parser_omp_clause_name (parser);
28647 switch (c_kind)
28649 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28650 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28651 token->location);
28652 c_name = "collapse";
28653 break;
28654 case PRAGMA_OMP_CLAUSE_COPYIN:
28655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28656 c_name = "copyin";
28657 break;
28658 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28659 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28660 clauses);
28661 c_name = "copyprivate";
28662 break;
28663 case PRAGMA_OMP_CLAUSE_DEFAULT:
28664 clauses = cp_parser_omp_clause_default (parser, clauses,
28665 token->location);
28666 c_name = "default";
28667 break;
28668 case PRAGMA_OMP_CLAUSE_FINAL:
28669 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28670 c_name = "final";
28671 break;
28672 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28673 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28674 clauses);
28675 c_name = "firstprivate";
28676 break;
28677 case PRAGMA_OMP_CLAUSE_IF:
28678 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28679 c_name = "if";
28680 break;
28681 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28682 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28683 clauses);
28684 c_name = "lastprivate";
28685 break;
28686 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28687 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28688 token->location);
28689 c_name = "mergeable";
28690 break;
28691 case PRAGMA_OMP_CLAUSE_NOWAIT:
28692 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28693 c_name = "nowait";
28694 break;
28695 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28696 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28697 token->location);
28698 c_name = "num_threads";
28699 break;
28700 case PRAGMA_OMP_CLAUSE_ORDERED:
28701 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28702 token->location);
28703 c_name = "ordered";
28704 break;
28705 case PRAGMA_OMP_CLAUSE_PRIVATE:
28706 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28707 clauses);
28708 c_name = "private";
28709 break;
28710 case PRAGMA_OMP_CLAUSE_REDUCTION:
28711 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28712 c_name = "reduction";
28713 break;
28714 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28715 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28716 token->location);
28717 c_name = "schedule";
28718 break;
28719 case PRAGMA_OMP_CLAUSE_SHARED:
28720 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28721 clauses);
28722 c_name = "shared";
28723 break;
28724 case PRAGMA_OMP_CLAUSE_UNTIED:
28725 clauses = cp_parser_omp_clause_untied (parser, clauses,
28726 token->location);
28727 c_name = "untied";
28728 break;
28729 case PRAGMA_OMP_CLAUSE_INBRANCH:
28730 case PRAGMA_CILK_CLAUSE_MASK:
28731 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28732 clauses, token->location);
28733 c_name = "inbranch";
28734 break;
28735 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28736 case PRAGMA_CILK_CLAUSE_NOMASK:
28737 clauses = cp_parser_omp_clause_branch (parser,
28738 OMP_CLAUSE_NOTINBRANCH,
28739 clauses, token->location);
28740 c_name = "notinbranch";
28741 break;
28742 case PRAGMA_OMP_CLAUSE_PARALLEL:
28743 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28744 clauses, token->location);
28745 c_name = "parallel";
28746 if (!first)
28748 clause_not_first:
28749 error_at (token->location, "%qs must be the first clause of %qs",
28750 c_name, where);
28751 clauses = prev;
28753 break;
28754 case PRAGMA_OMP_CLAUSE_FOR:
28755 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28756 clauses, token->location);
28757 c_name = "for";
28758 if (!first)
28759 goto clause_not_first;
28760 break;
28761 case PRAGMA_OMP_CLAUSE_SECTIONS:
28762 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28763 clauses, token->location);
28764 c_name = "sections";
28765 if (!first)
28766 goto clause_not_first;
28767 break;
28768 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28769 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28770 clauses, token->location);
28771 c_name = "taskgroup";
28772 if (!first)
28773 goto clause_not_first;
28774 break;
28775 case PRAGMA_OMP_CLAUSE_TO:
28776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28777 clauses);
28778 c_name = "to";
28779 break;
28780 case PRAGMA_OMP_CLAUSE_FROM:
28781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28782 clauses);
28783 c_name = "from";
28784 break;
28785 case PRAGMA_OMP_CLAUSE_UNIFORM:
28786 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28787 clauses);
28788 c_name = "uniform";
28789 break;
28790 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28791 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28792 token->location);
28793 c_name = "num_teams";
28794 break;
28795 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28796 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28797 token->location);
28798 c_name = "thread_limit";
28799 break;
28800 case PRAGMA_OMP_CLAUSE_ALIGNED:
28801 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28802 c_name = "aligned";
28803 break;
28804 case PRAGMA_OMP_CLAUSE_LINEAR:
28805 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28806 cilk_simd_fn = true;
28807 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28808 c_name = "linear";
28809 break;
28810 case PRAGMA_OMP_CLAUSE_DEPEND:
28811 clauses = cp_parser_omp_clause_depend (parser, clauses);
28812 c_name = "depend";
28813 break;
28814 case PRAGMA_OMP_CLAUSE_MAP:
28815 clauses = cp_parser_omp_clause_map (parser, clauses);
28816 c_name = "map";
28817 break;
28818 case PRAGMA_OMP_CLAUSE_DEVICE:
28819 clauses = cp_parser_omp_clause_device (parser, clauses,
28820 token->location);
28821 c_name = "device";
28822 break;
28823 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28824 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28825 token->location);
28826 c_name = "dist_schedule";
28827 break;
28828 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28829 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28830 token->location);
28831 c_name = "proc_bind";
28832 break;
28833 case PRAGMA_OMP_CLAUSE_SAFELEN:
28834 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28835 token->location);
28836 c_name = "safelen";
28837 break;
28838 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28839 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28840 token->location);
28841 c_name = "simdlen";
28842 break;
28843 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28844 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28845 c_name = "simdlen";
28846 break;
28847 default:
28848 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28849 goto saw_error;
28852 first = false;
28854 if (((mask >> c_kind) & 1) == 0)
28856 /* Remove the invalid clause(s) from the list to avoid
28857 confusing the rest of the compiler. */
28858 clauses = prev;
28859 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28862 saw_error:
28863 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28864 no reason to skip to the end. */
28865 if (!(flag_cilkplus && pragma_tok == NULL))
28866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28867 if (finish_p)
28868 return finish_omp_clauses (clauses);
28869 return clauses;
28872 /* OpenMP 2.5:
28873 structured-block:
28874 statement
28876 In practice, we're also interested in adding the statement to an
28877 outer node. So it is convenient if we work around the fact that
28878 cp_parser_statement calls add_stmt. */
28880 static unsigned
28881 cp_parser_begin_omp_structured_block (cp_parser *parser)
28883 unsigned save = parser->in_statement;
28885 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28886 This preserves the "not within loop or switch" style error messages
28887 for nonsense cases like
28888 void foo() {
28889 #pragma omp single
28890 break;
28893 if (parser->in_statement)
28894 parser->in_statement = IN_OMP_BLOCK;
28896 return save;
28899 static void
28900 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28902 parser->in_statement = save;
28905 static tree
28906 cp_parser_omp_structured_block (cp_parser *parser)
28908 tree stmt = begin_omp_structured_block ();
28909 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28911 cp_parser_statement (parser, NULL_TREE, false, NULL);
28913 cp_parser_end_omp_structured_block (parser, save);
28914 return finish_omp_structured_block (stmt);
28917 /* OpenMP 2.5:
28918 # pragma omp atomic new-line
28919 expression-stmt
28921 expression-stmt:
28922 x binop= expr | x++ | ++x | x-- | --x
28923 binop:
28924 +, *, -, /, &, ^, |, <<, >>
28926 where x is an lvalue expression with scalar type.
28928 OpenMP 3.1:
28929 # pragma omp atomic new-line
28930 update-stmt
28932 # pragma omp atomic read new-line
28933 read-stmt
28935 # pragma omp atomic write new-line
28936 write-stmt
28938 # pragma omp atomic update new-line
28939 update-stmt
28941 # pragma omp atomic capture new-line
28942 capture-stmt
28944 # pragma omp atomic capture new-line
28945 capture-block
28947 read-stmt:
28948 v = x
28949 write-stmt:
28950 x = expr
28951 update-stmt:
28952 expression-stmt | x = x binop expr
28953 capture-stmt:
28954 v = expression-stmt
28955 capture-block:
28956 { v = x; update-stmt; } | { update-stmt; v = x; }
28958 OpenMP 4.0:
28959 update-stmt:
28960 expression-stmt | x = x binop expr | x = expr binop x
28961 capture-stmt:
28962 v = update-stmt
28963 capture-block:
28964 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28966 where x and v are lvalue expressions with scalar type. */
28968 static void
28969 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28971 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28972 tree rhs1 = NULL_TREE, orig_lhs;
28973 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28974 bool structured_block = false;
28975 bool seq_cst = false;
28977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28980 const char *p = IDENTIFIER_POINTER (id);
28982 if (!strcmp (p, "seq_cst"))
28984 seq_cst = true;
28985 cp_lexer_consume_token (parser->lexer);
28986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28987 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28988 cp_lexer_consume_token (parser->lexer);
28991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28994 const char *p = IDENTIFIER_POINTER (id);
28996 if (!strcmp (p, "read"))
28997 code = OMP_ATOMIC_READ;
28998 else if (!strcmp (p, "write"))
28999 code = NOP_EXPR;
29000 else if (!strcmp (p, "update"))
29001 code = OMP_ATOMIC;
29002 else if (!strcmp (p, "capture"))
29003 code = OMP_ATOMIC_CAPTURE_NEW;
29004 else
29005 p = NULL;
29006 if (p)
29007 cp_lexer_consume_token (parser->lexer);
29009 if (!seq_cst)
29011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29012 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29013 cp_lexer_consume_token (parser->lexer);
29015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29017 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29018 const char *p = IDENTIFIER_POINTER (id);
29020 if (!strcmp (p, "seq_cst"))
29022 seq_cst = true;
29023 cp_lexer_consume_token (parser->lexer);
29027 cp_parser_require_pragma_eol (parser, pragma_tok);
29029 switch (code)
29031 case OMP_ATOMIC_READ:
29032 case NOP_EXPR: /* atomic write */
29033 v = cp_parser_unary_expression (parser);
29034 if (v == error_mark_node)
29035 goto saw_error;
29036 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29037 goto saw_error;
29038 if (code == NOP_EXPR)
29039 lhs = cp_parser_expression (parser);
29040 else
29041 lhs = cp_parser_unary_expression (parser);
29042 if (lhs == error_mark_node)
29043 goto saw_error;
29044 if (code == NOP_EXPR)
29046 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29047 opcode. */
29048 code = OMP_ATOMIC;
29049 rhs = lhs;
29050 lhs = v;
29051 v = NULL_TREE;
29053 goto done;
29054 case OMP_ATOMIC_CAPTURE_NEW:
29055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29057 cp_lexer_consume_token (parser->lexer);
29058 structured_block = true;
29060 else
29062 v = cp_parser_unary_expression (parser);
29063 if (v == error_mark_node)
29064 goto saw_error;
29065 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29066 goto saw_error;
29068 default:
29069 break;
29072 restart:
29073 lhs = cp_parser_unary_expression (parser);
29074 orig_lhs = lhs;
29075 switch (TREE_CODE (lhs))
29077 case ERROR_MARK:
29078 goto saw_error;
29080 case POSTINCREMENT_EXPR:
29081 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29082 code = OMP_ATOMIC_CAPTURE_OLD;
29083 /* FALLTHROUGH */
29084 case PREINCREMENT_EXPR:
29085 lhs = TREE_OPERAND (lhs, 0);
29086 opcode = PLUS_EXPR;
29087 rhs = integer_one_node;
29088 break;
29090 case POSTDECREMENT_EXPR:
29091 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29092 code = OMP_ATOMIC_CAPTURE_OLD;
29093 /* FALLTHROUGH */
29094 case PREDECREMENT_EXPR:
29095 lhs = TREE_OPERAND (lhs, 0);
29096 opcode = MINUS_EXPR;
29097 rhs = integer_one_node;
29098 break;
29100 case COMPOUND_EXPR:
29101 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29102 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29103 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29104 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29105 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29106 (TREE_OPERAND (lhs, 1), 0), 0)))
29107 == BOOLEAN_TYPE)
29108 /* Undo effects of boolean_increment for post {in,de}crement. */
29109 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29110 /* FALLTHRU */
29111 case MODIFY_EXPR:
29112 if (TREE_CODE (lhs) == MODIFY_EXPR
29113 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29115 /* Undo effects of boolean_increment. */
29116 if (integer_onep (TREE_OPERAND (lhs, 1)))
29118 /* This is pre or post increment. */
29119 rhs = TREE_OPERAND (lhs, 1);
29120 lhs = TREE_OPERAND (lhs, 0);
29121 opcode = NOP_EXPR;
29122 if (code == OMP_ATOMIC_CAPTURE_NEW
29123 && !structured_block
29124 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29125 code = OMP_ATOMIC_CAPTURE_OLD;
29126 break;
29129 /* FALLTHRU */
29130 default:
29131 switch (cp_lexer_peek_token (parser->lexer)->type)
29133 case CPP_MULT_EQ:
29134 opcode = MULT_EXPR;
29135 break;
29136 case CPP_DIV_EQ:
29137 opcode = TRUNC_DIV_EXPR;
29138 break;
29139 case CPP_PLUS_EQ:
29140 opcode = PLUS_EXPR;
29141 break;
29142 case CPP_MINUS_EQ:
29143 opcode = MINUS_EXPR;
29144 break;
29145 case CPP_LSHIFT_EQ:
29146 opcode = LSHIFT_EXPR;
29147 break;
29148 case CPP_RSHIFT_EQ:
29149 opcode = RSHIFT_EXPR;
29150 break;
29151 case CPP_AND_EQ:
29152 opcode = BIT_AND_EXPR;
29153 break;
29154 case CPP_OR_EQ:
29155 opcode = BIT_IOR_EXPR;
29156 break;
29157 case CPP_XOR_EQ:
29158 opcode = BIT_XOR_EXPR;
29159 break;
29160 case CPP_EQ:
29161 enum cp_parser_prec oprec;
29162 cp_token *token;
29163 cp_lexer_consume_token (parser->lexer);
29164 cp_parser_parse_tentatively (parser);
29165 rhs1 = cp_parser_simple_cast_expression (parser);
29166 if (rhs1 == error_mark_node)
29168 cp_parser_abort_tentative_parse (parser);
29169 cp_parser_simple_cast_expression (parser);
29170 goto saw_error;
29172 token = cp_lexer_peek_token (parser->lexer);
29173 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29175 cp_parser_abort_tentative_parse (parser);
29176 cp_parser_parse_tentatively (parser);
29177 rhs = cp_parser_binary_expression (parser, false, true,
29178 PREC_NOT_OPERATOR, NULL);
29179 if (rhs == error_mark_node)
29181 cp_parser_abort_tentative_parse (parser);
29182 cp_parser_binary_expression (parser, false, true,
29183 PREC_NOT_OPERATOR, NULL);
29184 goto saw_error;
29186 switch (TREE_CODE (rhs))
29188 case MULT_EXPR:
29189 case TRUNC_DIV_EXPR:
29190 case PLUS_EXPR:
29191 case MINUS_EXPR:
29192 case LSHIFT_EXPR:
29193 case RSHIFT_EXPR:
29194 case BIT_AND_EXPR:
29195 case BIT_IOR_EXPR:
29196 case BIT_XOR_EXPR:
29197 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29199 if (cp_parser_parse_definitely (parser))
29201 opcode = TREE_CODE (rhs);
29202 rhs1 = TREE_OPERAND (rhs, 0);
29203 rhs = TREE_OPERAND (rhs, 1);
29204 goto stmt_done;
29206 else
29207 goto saw_error;
29209 break;
29210 default:
29211 break;
29213 cp_parser_abort_tentative_parse (parser);
29214 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29216 rhs = cp_parser_expression (parser);
29217 if (rhs == error_mark_node)
29218 goto saw_error;
29219 opcode = NOP_EXPR;
29220 rhs1 = NULL_TREE;
29221 goto stmt_done;
29223 cp_parser_error (parser,
29224 "invalid form of %<#pragma omp atomic%>");
29225 goto saw_error;
29227 if (!cp_parser_parse_definitely (parser))
29228 goto saw_error;
29229 switch (token->type)
29231 case CPP_SEMICOLON:
29232 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29234 code = OMP_ATOMIC_CAPTURE_OLD;
29235 v = lhs;
29236 lhs = NULL_TREE;
29237 lhs1 = rhs1;
29238 rhs1 = NULL_TREE;
29239 cp_lexer_consume_token (parser->lexer);
29240 goto restart;
29242 else if (structured_block)
29244 opcode = NOP_EXPR;
29245 rhs = rhs1;
29246 rhs1 = NULL_TREE;
29247 goto stmt_done;
29249 cp_parser_error (parser,
29250 "invalid form of %<#pragma omp atomic%>");
29251 goto saw_error;
29252 case CPP_MULT:
29253 opcode = MULT_EXPR;
29254 break;
29255 case CPP_DIV:
29256 opcode = TRUNC_DIV_EXPR;
29257 break;
29258 case CPP_PLUS:
29259 opcode = PLUS_EXPR;
29260 break;
29261 case CPP_MINUS:
29262 opcode = MINUS_EXPR;
29263 break;
29264 case CPP_LSHIFT:
29265 opcode = LSHIFT_EXPR;
29266 break;
29267 case CPP_RSHIFT:
29268 opcode = RSHIFT_EXPR;
29269 break;
29270 case CPP_AND:
29271 opcode = BIT_AND_EXPR;
29272 break;
29273 case CPP_OR:
29274 opcode = BIT_IOR_EXPR;
29275 break;
29276 case CPP_XOR:
29277 opcode = BIT_XOR_EXPR;
29278 break;
29279 default:
29280 cp_parser_error (parser,
29281 "invalid operator for %<#pragma omp atomic%>");
29282 goto saw_error;
29284 oprec = TOKEN_PRECEDENCE (token);
29285 gcc_assert (oprec != PREC_NOT_OPERATOR);
29286 if (commutative_tree_code (opcode))
29287 oprec = (enum cp_parser_prec) (oprec - 1);
29288 cp_lexer_consume_token (parser->lexer);
29289 rhs = cp_parser_binary_expression (parser, false, false,
29290 oprec, NULL);
29291 if (rhs == error_mark_node)
29292 goto saw_error;
29293 goto stmt_done;
29294 /* FALLTHROUGH */
29295 default:
29296 cp_parser_error (parser,
29297 "invalid operator for %<#pragma omp atomic%>");
29298 goto saw_error;
29300 cp_lexer_consume_token (parser->lexer);
29302 rhs = cp_parser_expression (parser);
29303 if (rhs == error_mark_node)
29304 goto saw_error;
29305 break;
29307 stmt_done:
29308 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29310 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29311 goto saw_error;
29312 v = cp_parser_unary_expression (parser);
29313 if (v == error_mark_node)
29314 goto saw_error;
29315 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29316 goto saw_error;
29317 lhs1 = cp_parser_unary_expression (parser);
29318 if (lhs1 == error_mark_node)
29319 goto saw_error;
29321 if (structured_block)
29323 cp_parser_consume_semicolon_at_end_of_statement (parser);
29324 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29326 done:
29327 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29328 if (!structured_block)
29329 cp_parser_consume_semicolon_at_end_of_statement (parser);
29330 return;
29332 saw_error:
29333 cp_parser_skip_to_end_of_block_or_statement (parser);
29334 if (structured_block)
29336 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29337 cp_lexer_consume_token (parser->lexer);
29338 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29340 cp_parser_skip_to_end_of_block_or_statement (parser);
29341 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29342 cp_lexer_consume_token (parser->lexer);
29348 /* OpenMP 2.5:
29349 # pragma omp barrier new-line */
29351 static void
29352 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29354 cp_parser_require_pragma_eol (parser, pragma_tok);
29355 finish_omp_barrier ();
29358 /* OpenMP 2.5:
29359 # pragma omp critical [(name)] new-line
29360 structured-block */
29362 static tree
29363 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29365 tree stmt, name = NULL;
29367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29369 cp_lexer_consume_token (parser->lexer);
29371 name = cp_parser_identifier (parser);
29373 if (name == error_mark_node
29374 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29375 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29376 /*or_comma=*/false,
29377 /*consume_paren=*/true);
29378 if (name == error_mark_node)
29379 name = NULL;
29381 cp_parser_require_pragma_eol (parser, pragma_tok);
29383 stmt = cp_parser_omp_structured_block (parser);
29384 return c_finish_omp_critical (input_location, stmt, name);
29387 /* OpenMP 2.5:
29388 # pragma omp flush flush-vars[opt] new-line
29390 flush-vars:
29391 ( variable-list ) */
29393 static void
29394 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29397 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29398 cp_parser_require_pragma_eol (parser, pragma_tok);
29400 finish_omp_flush ();
29403 /* Helper function, to parse omp for increment expression. */
29405 static tree
29406 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29408 tree cond = cp_parser_binary_expression (parser, false, true,
29409 PREC_NOT_OPERATOR, NULL);
29410 if (cond == error_mark_node
29411 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29413 cp_parser_skip_to_end_of_statement (parser);
29414 return error_mark_node;
29417 switch (TREE_CODE (cond))
29419 case GT_EXPR:
29420 case GE_EXPR:
29421 case LT_EXPR:
29422 case LE_EXPR:
29423 break;
29424 case NE_EXPR:
29425 if (code == CILK_SIMD || code == CILK_FOR)
29426 break;
29427 /* Fall through: OpenMP disallows NE_EXPR. */
29428 default:
29429 return error_mark_node;
29432 /* If decl is an iterator, preserve LHS and RHS of the relational
29433 expr until finish_omp_for. */
29434 if (decl
29435 && (type_dependent_expression_p (decl)
29436 || CLASS_TYPE_P (TREE_TYPE (decl))))
29437 return cond;
29439 return build_x_binary_op (input_location, TREE_CODE (cond),
29440 TREE_OPERAND (cond, 0), ERROR_MARK,
29441 TREE_OPERAND (cond, 1), ERROR_MARK,
29442 /*overload=*/NULL, tf_warning_or_error);
29445 /* Helper function, to parse omp for increment expression. */
29447 static tree
29448 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29450 cp_token *token = cp_lexer_peek_token (parser->lexer);
29451 enum tree_code op;
29452 tree lhs, rhs;
29453 cp_id_kind idk;
29454 bool decl_first;
29456 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29458 op = (token->type == CPP_PLUS_PLUS
29459 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29460 cp_lexer_consume_token (parser->lexer);
29461 lhs = cp_parser_simple_cast_expression (parser);
29462 if (lhs != decl)
29463 return error_mark_node;
29464 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29467 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29468 if (lhs != decl)
29469 return error_mark_node;
29471 token = cp_lexer_peek_token (parser->lexer);
29472 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29474 op = (token->type == CPP_PLUS_PLUS
29475 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29476 cp_lexer_consume_token (parser->lexer);
29477 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29480 op = cp_parser_assignment_operator_opt (parser);
29481 if (op == ERROR_MARK)
29482 return error_mark_node;
29484 if (op != NOP_EXPR)
29486 rhs = cp_parser_assignment_expression (parser);
29487 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29488 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29491 lhs = cp_parser_binary_expression (parser, false, false,
29492 PREC_ADDITIVE_EXPRESSION, NULL);
29493 token = cp_lexer_peek_token (parser->lexer);
29494 decl_first = lhs == decl;
29495 if (decl_first)
29496 lhs = NULL_TREE;
29497 if (token->type != CPP_PLUS
29498 && token->type != CPP_MINUS)
29499 return error_mark_node;
29503 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29504 cp_lexer_consume_token (parser->lexer);
29505 rhs = cp_parser_binary_expression (parser, false, false,
29506 PREC_ADDITIVE_EXPRESSION, NULL);
29507 token = cp_lexer_peek_token (parser->lexer);
29508 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29510 if (lhs == NULL_TREE)
29512 if (op == PLUS_EXPR)
29513 lhs = rhs;
29514 else
29515 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29516 tf_warning_or_error);
29518 else
29519 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29520 ERROR_MARK, NULL, tf_warning_or_error);
29523 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29525 if (!decl_first)
29527 if (rhs != decl || op == MINUS_EXPR)
29528 return error_mark_node;
29529 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29531 else
29532 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29534 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29537 /* Parse the initialization statement of either an OpenMP for loop or
29538 a Cilk Plus for loop.
29540 Return true if the resulting construct should have an
29541 OMP_CLAUSE_PRIVATE added to it. */
29543 static bool
29544 cp_parser_omp_for_loop_init (cp_parser *parser,
29545 enum tree_code code,
29546 tree &this_pre_body,
29547 vec<tree, va_gc> *for_block,
29548 tree &init,
29549 tree &decl,
29550 tree &real_decl)
29552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29553 return false;
29555 bool add_private_clause = false;
29557 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29559 init-expr:
29560 var = lb
29561 integer-type var = lb
29562 random-access-iterator-type var = lb
29563 pointer-type var = lb
29565 cp_decl_specifier_seq type_specifiers;
29567 /* First, try to parse as an initialized declaration. See
29568 cp_parser_condition, from whence the bulk of this is copied. */
29570 cp_parser_parse_tentatively (parser);
29571 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29572 /*is_trailing_return=*/false,
29573 &type_specifiers);
29574 if (cp_parser_parse_definitely (parser))
29576 /* If parsing a type specifier seq succeeded, then this
29577 MUST be a initialized declaration. */
29578 tree asm_specification, attributes;
29579 cp_declarator *declarator;
29581 declarator = cp_parser_declarator (parser,
29582 CP_PARSER_DECLARATOR_NAMED,
29583 /*ctor_dtor_or_conv_p=*/NULL,
29584 /*parenthesized_p=*/NULL,
29585 /*member_p=*/false,
29586 /*friend_p=*/false);
29587 attributes = cp_parser_attributes_opt (parser);
29588 asm_specification = cp_parser_asm_specification_opt (parser);
29590 if (declarator == cp_error_declarator)
29591 cp_parser_skip_to_end_of_statement (parser);
29593 else
29595 tree pushed_scope, auto_node;
29597 decl = start_decl (declarator, &type_specifiers,
29598 SD_INITIALIZED, attributes,
29599 /*prefix_attributes=*/NULL_TREE,
29600 &pushed_scope);
29602 auto_node = type_uses_auto (TREE_TYPE (decl));
29603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29605 if (cp_lexer_next_token_is (parser->lexer,
29606 CPP_OPEN_PAREN))
29608 if (code != CILK_SIMD && code != CILK_FOR)
29609 error ("parenthesized initialization is not allowed in "
29610 "OpenMP %<for%> loop");
29611 else
29612 error ("parenthesized initialization is "
29613 "not allowed in for-loop");
29615 else
29616 /* Trigger an error. */
29617 cp_parser_require (parser, CPP_EQ, RT_EQ);
29619 init = error_mark_node;
29620 cp_parser_skip_to_end_of_statement (parser);
29622 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29623 || type_dependent_expression_p (decl)
29624 || auto_node)
29626 bool is_direct_init, is_non_constant_init;
29628 init = cp_parser_initializer (parser,
29629 &is_direct_init,
29630 &is_non_constant_init);
29632 if (auto_node)
29634 TREE_TYPE (decl)
29635 = do_auto_deduction (TREE_TYPE (decl), init,
29636 auto_node);
29638 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29639 && !type_dependent_expression_p (decl))
29640 goto non_class;
29643 cp_finish_decl (decl, init, !is_non_constant_init,
29644 asm_specification,
29645 LOOKUP_ONLYCONVERTING);
29646 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29648 vec_safe_push (for_block, this_pre_body);
29649 init = NULL_TREE;
29651 else
29652 init = pop_stmt_list (this_pre_body);
29653 this_pre_body = NULL_TREE;
29655 else
29657 /* Consume '='. */
29658 cp_lexer_consume_token (parser->lexer);
29659 init = cp_parser_assignment_expression (parser);
29661 non_class:
29662 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29663 init = error_mark_node;
29664 else
29665 cp_finish_decl (decl, NULL_TREE,
29666 /*init_const_expr_p=*/false,
29667 asm_specification,
29668 LOOKUP_ONLYCONVERTING);
29671 if (pushed_scope)
29672 pop_scope (pushed_scope);
29675 else
29677 cp_id_kind idk;
29678 /* If parsing a type specifier sequence failed, then
29679 this MUST be a simple expression. */
29680 if (code == CILK_FOR)
29681 error ("%<_Cilk_for%> allows expression instead of declaration only "
29682 "in C, not in C++");
29683 cp_parser_parse_tentatively (parser);
29684 decl = cp_parser_primary_expression (parser, false, false,
29685 false, &idk);
29686 if (!cp_parser_error_occurred (parser)
29687 && decl
29688 && DECL_P (decl)
29689 && CLASS_TYPE_P (TREE_TYPE (decl)))
29691 tree rhs;
29693 cp_parser_parse_definitely (parser);
29694 cp_parser_require (parser, CPP_EQ, RT_EQ);
29695 rhs = cp_parser_assignment_expression (parser);
29696 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29697 decl, NOP_EXPR,
29698 rhs,
29699 tf_warning_or_error));
29700 add_private_clause = true;
29702 else
29704 decl = NULL;
29705 cp_parser_abort_tentative_parse (parser);
29706 init = cp_parser_expression (parser);
29707 if (init)
29709 if (TREE_CODE (init) == MODIFY_EXPR
29710 || TREE_CODE (init) == MODOP_EXPR)
29711 real_decl = TREE_OPERAND (init, 0);
29715 return add_private_clause;
29718 /* Parse the restricted form of the for statement allowed by OpenMP. */
29720 static tree
29721 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29722 tree *cclauses)
29724 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29725 tree real_decl, initv, condv, incrv, declv;
29726 tree this_pre_body, cl;
29727 location_t loc_first;
29728 bool collapse_err = false;
29729 int i, collapse = 1, nbraces = 0;
29730 vec<tree, va_gc> *for_block = make_tree_vector ();
29732 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29733 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29734 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29736 gcc_assert (collapse >= 1);
29738 declv = make_tree_vec (collapse);
29739 initv = make_tree_vec (collapse);
29740 condv = make_tree_vec (collapse);
29741 incrv = make_tree_vec (collapse);
29743 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29745 for (i = 0; i < collapse; i++)
29747 int bracecount = 0;
29748 bool add_private_clause = false;
29749 location_t loc;
29751 if (code != CILK_FOR
29752 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29754 cp_parser_error (parser, "for statement expected");
29755 return NULL;
29757 if (code == CILK_FOR
29758 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29760 cp_parser_error (parser, "_Cilk_for statement expected");
29761 return NULL;
29763 loc = cp_lexer_consume_token (parser->lexer)->location;
29765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29766 return NULL;
29768 init = decl = real_decl = NULL;
29769 this_pre_body = push_stmt_list ();
29771 add_private_clause
29772 |= cp_parser_omp_for_loop_init (parser, code,
29773 this_pre_body, for_block,
29774 init, decl, real_decl);
29776 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29777 if (this_pre_body)
29779 this_pre_body = pop_stmt_list (this_pre_body);
29780 if (pre_body)
29782 tree t = pre_body;
29783 pre_body = push_stmt_list ();
29784 add_stmt (t);
29785 add_stmt (this_pre_body);
29786 pre_body = pop_stmt_list (pre_body);
29788 else
29789 pre_body = this_pre_body;
29792 if (decl)
29793 real_decl = decl;
29794 if (cclauses != NULL
29795 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29796 && real_decl != NULL_TREE)
29798 tree *c;
29799 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29800 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29801 && OMP_CLAUSE_DECL (*c) == real_decl)
29803 error_at (loc, "iteration variable %qD"
29804 " should not be firstprivate", real_decl);
29805 *c = OMP_CLAUSE_CHAIN (*c);
29807 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29808 && OMP_CLAUSE_DECL (*c) == real_decl)
29810 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29811 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29812 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29813 OMP_CLAUSE_DECL (l) = real_decl;
29814 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29815 if (code == OMP_SIMD)
29817 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29818 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29820 else
29822 OMP_CLAUSE_CHAIN (l) = clauses;
29823 clauses = l;
29825 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29826 CP_OMP_CLAUSE_INFO (*c) = NULL;
29827 add_private_clause = false;
29829 else
29831 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29832 && OMP_CLAUSE_DECL (*c) == real_decl)
29833 add_private_clause = false;
29834 c = &OMP_CLAUSE_CHAIN (*c);
29838 if (add_private_clause)
29840 tree c;
29841 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29843 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29844 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29845 && OMP_CLAUSE_DECL (c) == decl)
29846 break;
29847 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29848 && OMP_CLAUSE_DECL (c) == decl)
29849 error_at (loc, "iteration variable %qD "
29850 "should not be firstprivate",
29851 decl);
29852 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29853 && OMP_CLAUSE_DECL (c) == decl)
29854 error_at (loc, "iteration variable %qD should not be reduction",
29855 decl);
29857 if (c == NULL)
29859 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29860 OMP_CLAUSE_DECL (c) = decl;
29861 c = finish_omp_clauses (c);
29862 if (c)
29864 OMP_CLAUSE_CHAIN (c) = clauses;
29865 clauses = c;
29870 cond = NULL;
29871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29872 cond = cp_parser_omp_for_cond (parser, decl, code);
29873 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29875 incr = NULL;
29876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29878 /* If decl is an iterator, preserve the operator on decl
29879 until finish_omp_for. */
29880 if (real_decl
29881 && ((processing_template_decl
29882 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29883 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29884 incr = cp_parser_omp_for_incr (parser, real_decl);
29885 else
29886 incr = cp_parser_expression (parser);
29887 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29888 SET_EXPR_LOCATION (incr, input_location);
29891 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29893 /*or_comma=*/false,
29894 /*consume_paren=*/true);
29896 TREE_VEC_ELT (declv, i) = decl;
29897 TREE_VEC_ELT (initv, i) = init;
29898 TREE_VEC_ELT (condv, i) = cond;
29899 TREE_VEC_ELT (incrv, i) = incr;
29901 if (i == collapse - 1)
29902 break;
29904 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29905 in between the collapsed for loops to be still considered perfectly
29906 nested. Hopefully the final version clarifies this.
29907 For now handle (multiple) {'s and empty statements. */
29908 cp_parser_parse_tentatively (parser);
29911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29912 break;
29913 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29915 cp_lexer_consume_token (parser->lexer);
29916 bracecount++;
29918 else if (bracecount
29919 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29920 cp_lexer_consume_token (parser->lexer);
29921 else
29923 loc = cp_lexer_peek_token (parser->lexer)->location;
29924 error_at (loc, "not enough collapsed for loops");
29925 collapse_err = true;
29926 cp_parser_abort_tentative_parse (parser);
29927 declv = NULL_TREE;
29928 break;
29931 while (1);
29933 if (declv)
29935 cp_parser_parse_definitely (parser);
29936 nbraces += bracecount;
29940 /* Note that we saved the original contents of this flag when we entered
29941 the structured block, and so we don't need to re-save it here. */
29942 if (code == CILK_SIMD || code == CILK_FOR)
29943 parser->in_statement = IN_CILK_SIMD_FOR;
29944 else
29945 parser->in_statement = IN_OMP_FOR;
29947 /* Note that the grammar doesn't call for a structured block here,
29948 though the loop as a whole is a structured block. */
29949 body = push_stmt_list ();
29950 cp_parser_statement (parser, NULL_TREE, false, NULL);
29951 body = pop_stmt_list (body);
29953 if (declv == NULL_TREE)
29954 ret = NULL_TREE;
29955 else
29956 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29957 pre_body, clauses);
29959 while (nbraces)
29961 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29963 cp_lexer_consume_token (parser->lexer);
29964 nbraces--;
29966 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29967 cp_lexer_consume_token (parser->lexer);
29968 else
29970 if (!collapse_err)
29972 error_at (cp_lexer_peek_token (parser->lexer)->location,
29973 "collapsed loops not perfectly nested");
29975 collapse_err = true;
29976 cp_parser_statement_seq_opt (parser, NULL);
29977 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29978 break;
29982 while (!for_block->is_empty ())
29983 add_stmt (pop_stmt_list (for_block->pop ()));
29984 release_tree_vector (for_block);
29986 return ret;
29989 /* Helper function for OpenMP parsing, split clauses and call
29990 finish_omp_clauses on each of the set of clauses afterwards. */
29992 static void
29993 cp_omp_split_clauses (location_t loc, enum tree_code code,
29994 omp_clause_mask mask, tree clauses, tree *cclauses)
29996 int i;
29997 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29998 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29999 if (cclauses[i])
30000 cclauses[i] = finish_omp_clauses (cclauses[i]);
30003 /* OpenMP 4.0:
30004 #pragma omp simd simd-clause[optseq] new-line
30005 for-loop */
30007 #define OMP_SIMD_CLAUSE_MASK \
30008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30016 static tree
30017 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30018 char *p_name, omp_clause_mask mask, tree *cclauses)
30020 tree clauses, sb, ret;
30021 unsigned int save;
30022 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30024 strcat (p_name, " simd");
30025 mask |= OMP_SIMD_CLAUSE_MASK;
30026 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30028 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30029 cclauses == NULL);
30030 if (cclauses)
30032 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30033 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30036 sb = begin_omp_structured_block ();
30037 save = cp_parser_begin_omp_structured_block (parser);
30039 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30041 cp_parser_end_omp_structured_block (parser, save);
30042 add_stmt (finish_omp_structured_block (sb));
30044 return ret;
30047 /* OpenMP 2.5:
30048 #pragma omp for for-clause[optseq] new-line
30049 for-loop
30051 OpenMP 4.0:
30052 #pragma omp for simd for-simd-clause[optseq] new-line
30053 for-loop */
30055 #define OMP_FOR_CLAUSE_MASK \
30056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30065 static tree
30066 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30067 char *p_name, omp_clause_mask mask, tree *cclauses)
30069 tree clauses, sb, ret;
30070 unsigned int save;
30071 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30073 strcat (p_name, " for");
30074 mask |= OMP_FOR_CLAUSE_MASK;
30075 if (cclauses)
30076 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30081 const char *p = IDENTIFIER_POINTER (id);
30083 if (strcmp (p, "simd") == 0)
30085 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30086 if (cclauses == NULL)
30087 cclauses = cclauses_buf;
30089 cp_lexer_consume_token (parser->lexer);
30090 if (!flag_openmp) /* flag_openmp_simd */
30091 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30092 cclauses);
30093 sb = begin_omp_structured_block ();
30094 save = cp_parser_begin_omp_structured_block (parser);
30095 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30096 cclauses);
30097 cp_parser_end_omp_structured_block (parser, save);
30098 tree body = finish_omp_structured_block (sb);
30099 if (ret == NULL)
30100 return ret;
30101 ret = make_node (OMP_FOR);
30102 TREE_TYPE (ret) = void_type_node;
30103 OMP_FOR_BODY (ret) = body;
30104 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30105 SET_EXPR_LOCATION (ret, loc);
30106 add_stmt (ret);
30107 return ret;
30110 if (!flag_openmp) /* flag_openmp_simd */
30112 cp_parser_require_pragma_eol (parser, pragma_tok);
30113 return NULL_TREE;
30116 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30117 cclauses == NULL);
30118 if (cclauses)
30120 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30121 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30124 sb = begin_omp_structured_block ();
30125 save = cp_parser_begin_omp_structured_block (parser);
30127 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30129 cp_parser_end_omp_structured_block (parser, save);
30130 add_stmt (finish_omp_structured_block (sb));
30132 return ret;
30135 /* OpenMP 2.5:
30136 # pragma omp master new-line
30137 structured-block */
30139 static tree
30140 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30142 cp_parser_require_pragma_eol (parser, pragma_tok);
30143 return c_finish_omp_master (input_location,
30144 cp_parser_omp_structured_block (parser));
30147 /* OpenMP 2.5:
30148 # pragma omp ordered new-line
30149 structured-block */
30151 static tree
30152 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30154 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30155 cp_parser_require_pragma_eol (parser, pragma_tok);
30156 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30159 /* OpenMP 2.5:
30161 section-scope:
30162 { section-sequence }
30164 section-sequence:
30165 section-directive[opt] structured-block
30166 section-sequence section-directive structured-block */
30168 static tree
30169 cp_parser_omp_sections_scope (cp_parser *parser)
30171 tree stmt, substmt;
30172 bool error_suppress = false;
30173 cp_token *tok;
30175 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30176 return NULL_TREE;
30178 stmt = push_stmt_list ();
30180 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30182 substmt = cp_parser_omp_structured_block (parser);
30183 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30184 add_stmt (substmt);
30187 while (1)
30189 tok = cp_lexer_peek_token (parser->lexer);
30190 if (tok->type == CPP_CLOSE_BRACE)
30191 break;
30192 if (tok->type == CPP_EOF)
30193 break;
30195 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30197 cp_lexer_consume_token (parser->lexer);
30198 cp_parser_require_pragma_eol (parser, tok);
30199 error_suppress = false;
30201 else if (!error_suppress)
30203 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30204 error_suppress = true;
30207 substmt = cp_parser_omp_structured_block (parser);
30208 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30209 add_stmt (substmt);
30211 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30213 substmt = pop_stmt_list (stmt);
30215 stmt = make_node (OMP_SECTIONS);
30216 TREE_TYPE (stmt) = void_type_node;
30217 OMP_SECTIONS_BODY (stmt) = substmt;
30219 add_stmt (stmt);
30220 return stmt;
30223 /* OpenMP 2.5:
30224 # pragma omp sections sections-clause[optseq] newline
30225 sections-scope */
30227 #define OMP_SECTIONS_CLAUSE_MASK \
30228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30234 static tree
30235 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30236 char *p_name, omp_clause_mask mask, tree *cclauses)
30238 tree clauses, ret;
30239 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30241 strcat (p_name, " sections");
30242 mask |= OMP_SECTIONS_CLAUSE_MASK;
30243 if (cclauses)
30244 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30246 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30247 cclauses == NULL);
30248 if (cclauses)
30250 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30251 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30254 ret = cp_parser_omp_sections_scope (parser);
30255 if (ret)
30256 OMP_SECTIONS_CLAUSES (ret) = clauses;
30258 return ret;
30261 /* OpenMP 2.5:
30262 # pragma omp parallel parallel-clause[optseq] new-line
30263 structured-block
30264 # pragma omp parallel for parallel-for-clause[optseq] new-line
30265 structured-block
30266 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30267 structured-block
30269 OpenMP 4.0:
30270 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30271 structured-block */
30273 #define OMP_PARALLEL_CLAUSE_MASK \
30274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30284 static tree
30285 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30286 char *p_name, omp_clause_mask mask, tree *cclauses)
30288 tree stmt, clauses, block;
30289 unsigned int save;
30290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30292 strcat (p_name, " parallel");
30293 mask |= OMP_PARALLEL_CLAUSE_MASK;
30295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30297 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30298 if (cclauses == NULL)
30299 cclauses = cclauses_buf;
30301 cp_lexer_consume_token (parser->lexer);
30302 if (!flag_openmp) /* flag_openmp_simd */
30303 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30304 block = begin_omp_parallel ();
30305 save = cp_parser_begin_omp_structured_block (parser);
30306 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30307 cp_parser_end_omp_structured_block (parser, save);
30308 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30309 block);
30310 if (ret == NULL_TREE)
30311 return ret;
30312 OMP_PARALLEL_COMBINED (stmt) = 1;
30313 return stmt;
30315 else if (cclauses)
30317 error_at (loc, "expected %<for%> after %qs", p_name);
30318 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30319 return NULL_TREE;
30321 else if (!flag_openmp) /* flag_openmp_simd */
30323 cp_parser_require_pragma_eol (parser, pragma_tok);
30324 return NULL_TREE;
30326 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30328 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30329 const char *p = IDENTIFIER_POINTER (id);
30330 if (strcmp (p, "sections") == 0)
30332 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30333 cclauses = cclauses_buf;
30335 cp_lexer_consume_token (parser->lexer);
30336 block = begin_omp_parallel ();
30337 save = cp_parser_begin_omp_structured_block (parser);
30338 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30339 cp_parser_end_omp_structured_block (parser, save);
30340 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30341 block);
30342 OMP_PARALLEL_COMBINED (stmt) = 1;
30343 return stmt;
30347 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30349 block = begin_omp_parallel ();
30350 save = cp_parser_begin_omp_structured_block (parser);
30351 cp_parser_statement (parser, NULL_TREE, false, NULL);
30352 cp_parser_end_omp_structured_block (parser, save);
30353 stmt = finish_omp_parallel (clauses, block);
30354 return stmt;
30357 /* OpenMP 2.5:
30358 # pragma omp single single-clause[optseq] new-line
30359 structured-block */
30361 #define OMP_SINGLE_CLAUSE_MASK \
30362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30367 static tree
30368 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30370 tree stmt = make_node (OMP_SINGLE);
30371 TREE_TYPE (stmt) = void_type_node;
30373 OMP_SINGLE_CLAUSES (stmt)
30374 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30375 "#pragma omp single", pragma_tok);
30376 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30378 return add_stmt (stmt);
30381 /* OpenMP 3.0:
30382 # pragma omp task task-clause[optseq] new-line
30383 structured-block */
30385 #define OMP_TASK_CLAUSE_MASK \
30386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30396 static tree
30397 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30399 tree clauses, block;
30400 unsigned int save;
30402 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30403 "#pragma omp task", pragma_tok);
30404 block = begin_omp_task ();
30405 save = cp_parser_begin_omp_structured_block (parser);
30406 cp_parser_statement (parser, NULL_TREE, false, NULL);
30407 cp_parser_end_omp_structured_block (parser, save);
30408 return finish_omp_task (clauses, block);
30411 /* OpenMP 3.0:
30412 # pragma omp taskwait new-line */
30414 static void
30415 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30417 cp_parser_require_pragma_eol (parser, pragma_tok);
30418 finish_omp_taskwait ();
30421 /* OpenMP 3.1:
30422 # pragma omp taskyield new-line */
30424 static void
30425 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30427 cp_parser_require_pragma_eol (parser, pragma_tok);
30428 finish_omp_taskyield ();
30431 /* OpenMP 4.0:
30432 # pragma omp taskgroup new-line
30433 structured-block */
30435 static tree
30436 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30438 cp_parser_require_pragma_eol (parser, pragma_tok);
30439 return c_finish_omp_taskgroup (input_location,
30440 cp_parser_omp_structured_block (parser));
30444 /* OpenMP 2.5:
30445 # pragma omp threadprivate (variable-list) */
30447 static void
30448 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30450 tree vars;
30452 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30453 cp_parser_require_pragma_eol (parser, pragma_tok);
30455 finish_omp_threadprivate (vars);
30458 /* OpenMP 4.0:
30459 # pragma omp cancel cancel-clause[optseq] new-line */
30461 #define OMP_CANCEL_CLAUSE_MASK \
30462 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30468 static void
30469 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30471 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30472 "#pragma omp cancel", pragma_tok);
30473 finish_omp_cancel (clauses);
30476 /* OpenMP 4.0:
30477 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30479 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30485 static void
30486 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30488 tree clauses;
30489 bool point_seen = false;
30491 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30493 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30494 const char *p = IDENTIFIER_POINTER (id);
30496 if (strcmp (p, "point") == 0)
30498 cp_lexer_consume_token (parser->lexer);
30499 point_seen = true;
30502 if (!point_seen)
30504 cp_parser_error (parser, "expected %<point%>");
30505 cp_parser_require_pragma_eol (parser, pragma_tok);
30506 return;
30509 clauses = cp_parser_omp_all_clauses (parser,
30510 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30511 "#pragma omp cancellation point",
30512 pragma_tok);
30513 finish_omp_cancellation_point (clauses);
30516 /* OpenMP 4.0:
30517 #pragma omp distribute distribute-clause[optseq] new-line
30518 for-loop */
30520 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30521 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30526 static tree
30527 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30528 char *p_name, omp_clause_mask mask, tree *cclauses)
30530 tree clauses, sb, ret;
30531 unsigned int save;
30532 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30534 strcat (p_name, " distribute");
30535 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30540 const char *p = IDENTIFIER_POINTER (id);
30541 bool simd = false;
30542 bool parallel = false;
30544 if (strcmp (p, "simd") == 0)
30545 simd = true;
30546 else
30547 parallel = strcmp (p, "parallel") == 0;
30548 if (parallel || simd)
30550 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30551 if (cclauses == NULL)
30552 cclauses = cclauses_buf;
30553 cp_lexer_consume_token (parser->lexer);
30554 if (!flag_openmp) /* flag_openmp_simd */
30556 if (simd)
30557 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30558 cclauses);
30559 else
30560 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30561 cclauses);
30563 sb = begin_omp_structured_block ();
30564 save = cp_parser_begin_omp_structured_block (parser);
30565 if (simd)
30566 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30567 cclauses);
30568 else
30569 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30570 cclauses);
30571 cp_parser_end_omp_structured_block (parser, save);
30572 tree body = finish_omp_structured_block (sb);
30573 if (ret == NULL)
30574 return ret;
30575 ret = make_node (OMP_DISTRIBUTE);
30576 TREE_TYPE (ret) = void_type_node;
30577 OMP_FOR_BODY (ret) = body;
30578 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30579 SET_EXPR_LOCATION (ret, loc);
30580 add_stmt (ret);
30581 return ret;
30584 if (!flag_openmp) /* flag_openmp_simd */
30586 cp_parser_require_pragma_eol (parser, pragma_tok);
30587 return NULL_TREE;
30590 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30591 cclauses == NULL);
30592 if (cclauses)
30594 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30595 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30598 sb = begin_omp_structured_block ();
30599 save = cp_parser_begin_omp_structured_block (parser);
30601 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30603 cp_parser_end_omp_structured_block (parser, save);
30604 add_stmt (finish_omp_structured_block (sb));
30606 return ret;
30609 /* OpenMP 4.0:
30610 # pragma omp teams teams-clause[optseq] new-line
30611 structured-block */
30613 #define OMP_TEAMS_CLAUSE_MASK \
30614 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30622 static tree
30623 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30624 char *p_name, omp_clause_mask mask, tree *cclauses)
30626 tree clauses, sb, ret;
30627 unsigned int save;
30628 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30630 strcat (p_name, " teams");
30631 mask |= OMP_TEAMS_CLAUSE_MASK;
30633 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30635 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30636 const char *p = IDENTIFIER_POINTER (id);
30637 if (strcmp (p, "distribute") == 0)
30639 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30640 if (cclauses == NULL)
30641 cclauses = cclauses_buf;
30643 cp_lexer_consume_token (parser->lexer);
30644 if (!flag_openmp) /* flag_openmp_simd */
30645 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30646 cclauses);
30647 sb = begin_omp_structured_block ();
30648 save = cp_parser_begin_omp_structured_block (parser);
30649 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30650 cclauses);
30651 cp_parser_end_omp_structured_block (parser, save);
30652 tree body = finish_omp_structured_block (sb);
30653 if (ret == NULL)
30654 return ret;
30655 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30656 ret = make_node (OMP_TEAMS);
30657 TREE_TYPE (ret) = void_type_node;
30658 OMP_TEAMS_CLAUSES (ret) = clauses;
30659 OMP_TEAMS_BODY (ret) = body;
30660 return add_stmt (ret);
30663 if (!flag_openmp) /* flag_openmp_simd */
30665 cp_parser_require_pragma_eol (parser, pragma_tok);
30666 return NULL_TREE;
30669 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30670 cclauses == NULL);
30671 if (cclauses)
30673 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30674 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30677 tree stmt = make_node (OMP_TEAMS);
30678 TREE_TYPE (stmt) = void_type_node;
30679 OMP_TEAMS_CLAUSES (stmt) = clauses;
30680 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30682 return add_stmt (stmt);
30685 /* OpenMP 4.0:
30686 # pragma omp target data target-data-clause[optseq] new-line
30687 structured-block */
30689 #define OMP_TARGET_DATA_CLAUSE_MASK \
30690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30694 static tree
30695 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30697 tree stmt = make_node (OMP_TARGET_DATA);
30698 TREE_TYPE (stmt) = void_type_node;
30700 OMP_TARGET_DATA_CLAUSES (stmt)
30701 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30702 "#pragma omp target data", pragma_tok);
30703 keep_next_level (true);
30704 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30706 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30707 return add_stmt (stmt);
30710 /* OpenMP 4.0:
30711 # pragma omp target update target-update-clause[optseq] new-line */
30713 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30719 static bool
30720 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30721 enum pragma_context context)
30723 if (context == pragma_stmt)
30725 error_at (pragma_tok->location,
30726 "%<#pragma omp target update%> may only be "
30727 "used in compound statements");
30728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30729 return false;
30732 tree clauses
30733 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30734 "#pragma omp target update", pragma_tok);
30735 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30736 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30738 error_at (pragma_tok->location,
30739 "%<#pragma omp target update must contain at least one "
30740 "%<from%> or %<to%> clauses");
30741 return false;
30744 tree stmt = make_node (OMP_TARGET_UPDATE);
30745 TREE_TYPE (stmt) = void_type_node;
30746 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30747 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30748 add_stmt (stmt);
30749 return false;
30752 /* OpenMP 4.0:
30753 # pragma omp target target-clause[optseq] new-line
30754 structured-block */
30756 #define OMP_TARGET_CLAUSE_MASK \
30757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30761 static bool
30762 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30763 enum pragma_context context)
30765 if (context != pragma_stmt && context != pragma_compound)
30767 cp_parser_error (parser, "expected declaration specifiers");
30768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30769 return false;
30772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30774 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30775 const char *p = IDENTIFIER_POINTER (id);
30777 if (strcmp (p, "teams") == 0)
30779 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30780 char p_name[sizeof ("#pragma omp target teams distribute "
30781 "parallel for simd")];
30783 cp_lexer_consume_token (parser->lexer);
30784 strcpy (p_name, "#pragma omp target");
30785 if (!flag_openmp) /* flag_openmp_simd */
30787 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30788 OMP_TARGET_CLAUSE_MASK,
30789 cclauses);
30790 return stmt != NULL_TREE;
30792 keep_next_level (true);
30793 tree sb = begin_omp_structured_block ();
30794 unsigned save = cp_parser_begin_omp_structured_block (parser);
30795 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30796 OMP_TARGET_CLAUSE_MASK, cclauses);
30797 cp_parser_end_omp_structured_block (parser, save);
30798 tree body = finish_omp_structured_block (sb);
30799 if (ret == NULL_TREE)
30800 return false;
30801 tree stmt = make_node (OMP_TARGET);
30802 TREE_TYPE (stmt) = void_type_node;
30803 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30804 OMP_TARGET_BODY (stmt) = body;
30805 add_stmt (stmt);
30806 return true;
30808 else if (!flag_openmp) /* flag_openmp_simd */
30810 cp_parser_require_pragma_eol (parser, pragma_tok);
30811 return false;
30813 else if (strcmp (p, "data") == 0)
30815 cp_lexer_consume_token (parser->lexer);
30816 cp_parser_omp_target_data (parser, pragma_tok);
30817 return true;
30819 else if (strcmp (p, "update") == 0)
30821 cp_lexer_consume_token (parser->lexer);
30822 return cp_parser_omp_target_update (parser, pragma_tok, context);
30826 tree stmt = make_node (OMP_TARGET);
30827 TREE_TYPE (stmt) = void_type_node;
30829 OMP_TARGET_CLAUSES (stmt)
30830 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30831 "#pragma omp target", pragma_tok);
30832 keep_next_level (true);
30833 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30835 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30836 add_stmt (stmt);
30837 return true;
30840 /* OpenMP 4.0:
30841 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30843 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30851 static void
30852 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30853 enum pragma_context context)
30855 bool first_p = parser->omp_declare_simd == NULL;
30856 cp_omp_declare_simd_data data;
30857 if (first_p)
30859 data.error_seen = false;
30860 data.fndecl_seen = false;
30861 data.tokens = vNULL;
30862 parser->omp_declare_simd = &data;
30864 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30865 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30866 cp_lexer_consume_token (parser->lexer);
30867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30868 parser->omp_declare_simd->error_seen = true;
30869 cp_parser_require_pragma_eol (parser, pragma_tok);
30870 struct cp_token_cache *cp
30871 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30872 parser->omp_declare_simd->tokens.safe_push (cp);
30873 if (first_p)
30875 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30876 cp_parser_pragma (parser, context);
30877 switch (context)
30879 case pragma_external:
30880 cp_parser_declaration (parser);
30881 break;
30882 case pragma_member:
30883 cp_parser_member_declaration (parser);
30884 break;
30885 case pragma_objc_icode:
30886 cp_parser_block_declaration (parser, /*statement_p=*/false);
30887 break;
30888 default:
30889 cp_parser_declaration_statement (parser);
30890 break;
30892 if (parser->omp_declare_simd
30893 && !parser->omp_declare_simd->error_seen
30894 && !parser->omp_declare_simd->fndecl_seen)
30895 error_at (pragma_tok->location,
30896 "%<#pragma omp declare simd%> not immediately followed by "
30897 "function declaration or definition");
30898 data.tokens.release ();
30899 parser->omp_declare_simd = NULL;
30903 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30904 This function is modelled similar to the late parsing of omp declare
30905 simd. */
30907 static tree
30908 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30910 struct cp_token_cache *ce;
30911 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30912 int ii = 0;
30914 if (parser->omp_declare_simd != NULL)
30916 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30917 " marked as a Cilk Plus SIMD-enabled function");
30918 XDELETE (parser->cilk_simd_fn_info);
30919 parser->cilk_simd_fn_info = NULL;
30920 return attrs;
30922 if (!info->error_seen && info->fndecl_seen)
30924 error ("vector attribute not immediately followed by a single function"
30925 " declaration or definition");
30926 info->error_seen = true;
30928 if (info->error_seen)
30929 return attrs;
30931 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30933 tree c, cl;
30935 cp_parser_push_lexer_for_tokens (parser, ce);
30936 parser->lexer->in_pragma = true;
30937 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30938 "SIMD-enabled functions attribute",
30939 NULL);
30940 cp_parser_pop_lexer (parser);
30941 if (cl)
30942 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30944 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30945 TREE_CHAIN (c) = attrs;
30946 attrs = c;
30948 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30949 TREE_CHAIN (c) = attrs;
30950 if (processing_template_decl)
30951 ATTR_IS_DEPENDENT (c) = 1;
30952 attrs = c;
30954 info->fndecl_seen = true;
30955 XDELETE (parser->cilk_simd_fn_info);
30956 parser->cilk_simd_fn_info = NULL;
30957 return attrs;
30960 /* Finalize #pragma omp declare simd clauses after direct declarator has
30961 been parsed, and put that into "omp declare simd" attribute. */
30963 static tree
30964 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30966 struct cp_token_cache *ce;
30967 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30968 int i;
30970 if (!data->error_seen && data->fndecl_seen)
30972 error ("%<#pragma omp declare simd%> not immediately followed by "
30973 "a single function declaration or definition");
30974 data->error_seen = true;
30975 return attrs;
30977 if (data->error_seen)
30978 return attrs;
30980 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30982 tree c, cl;
30984 cp_parser_push_lexer_for_tokens (parser, ce);
30985 parser->lexer->in_pragma = true;
30986 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30987 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30988 cp_lexer_consume_token (parser->lexer);
30989 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30990 "#pragma omp declare simd", pragma_tok);
30991 cp_parser_pop_lexer (parser);
30992 if (cl)
30993 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30994 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30995 TREE_CHAIN (c) = attrs;
30996 if (processing_template_decl)
30997 ATTR_IS_DEPENDENT (c) = 1;
30998 attrs = c;
31001 data->fndecl_seen = true;
31002 return attrs;
31006 /* OpenMP 4.0:
31007 # pragma omp declare target new-line
31008 declarations and definitions
31009 # pragma omp end declare target new-line */
31011 static void
31012 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31014 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31015 scope_chain->omp_declare_target_attribute++;
31018 static void
31019 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31021 const char *p = "";
31022 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31024 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31025 p = IDENTIFIER_POINTER (id);
31027 if (strcmp (p, "declare") == 0)
31029 cp_lexer_consume_token (parser->lexer);
31030 p = "";
31031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31034 p = IDENTIFIER_POINTER (id);
31036 if (strcmp (p, "target") == 0)
31037 cp_lexer_consume_token (parser->lexer);
31038 else
31040 cp_parser_error (parser, "expected %<target%>");
31041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31042 return;
31045 else
31047 cp_parser_error (parser, "expected %<declare%>");
31048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31049 return;
31051 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31052 if (!scope_chain->omp_declare_target_attribute)
31053 error_at (pragma_tok->location,
31054 "%<#pragma omp end declare target%> without corresponding "
31055 "%<#pragma omp declare target%>");
31056 else
31057 scope_chain->omp_declare_target_attribute--;
31060 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31061 expression and optional initializer clause of
31062 #pragma omp declare reduction. We store the expression(s) as
31063 either 3, 6 or 7 special statements inside of the artificial function's
31064 body. The first two statements are DECL_EXPRs for the artificial
31065 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31066 expression that uses those variables.
31067 If there was any INITIALIZER clause, this is followed by further statements,
31068 the fourth and fifth statements are DECL_EXPRs for the artificial
31069 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31070 constructor variant (first token after open paren is not omp_priv),
31071 then the sixth statement is a statement with the function call expression
31072 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31073 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31074 to initialize the OMP_PRIV artificial variable and there is seventh
31075 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31077 static bool
31078 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31080 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31081 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31082 type = TREE_TYPE (type);
31083 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31084 DECL_ARTIFICIAL (omp_out) = 1;
31085 pushdecl (omp_out);
31086 add_decl_expr (omp_out);
31087 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31088 DECL_ARTIFICIAL (omp_in) = 1;
31089 pushdecl (omp_in);
31090 add_decl_expr (omp_in);
31091 tree combiner;
31092 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31094 keep_next_level (true);
31095 tree block = begin_omp_structured_block ();
31096 combiner = cp_parser_expression (parser);
31097 finish_expr_stmt (combiner);
31098 block = finish_omp_structured_block (block);
31099 add_stmt (block);
31101 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31102 return false;
31104 const char *p = "";
31105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31107 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31108 p = IDENTIFIER_POINTER (id);
31111 if (strcmp (p, "initializer") == 0)
31113 cp_lexer_consume_token (parser->lexer);
31114 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31115 return false;
31117 p = "";
31118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31120 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31121 p = IDENTIFIER_POINTER (id);
31124 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31125 DECL_ARTIFICIAL (omp_priv) = 1;
31126 pushdecl (omp_priv);
31127 add_decl_expr (omp_priv);
31128 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31129 DECL_ARTIFICIAL (omp_orig) = 1;
31130 pushdecl (omp_orig);
31131 add_decl_expr (omp_orig);
31133 keep_next_level (true);
31134 block = begin_omp_structured_block ();
31136 bool ctor = false;
31137 if (strcmp (p, "omp_priv") == 0)
31139 bool is_direct_init, is_non_constant_init;
31140 ctor = true;
31141 cp_lexer_consume_token (parser->lexer);
31142 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31144 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31145 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31146 == CPP_CLOSE_PAREN
31147 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31148 == CPP_CLOSE_PAREN))
31150 finish_omp_structured_block (block);
31151 error ("invalid initializer clause");
31152 return false;
31154 initializer = cp_parser_initializer (parser, &is_direct_init,
31155 &is_non_constant_init);
31156 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31157 NULL_TREE, LOOKUP_ONLYCONVERTING);
31159 else
31161 cp_parser_parse_tentatively (parser);
31162 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31163 /*check_dependency_p=*/true,
31164 /*template_p=*/NULL,
31165 /*declarator_p=*/false,
31166 /*optional_p=*/false);
31167 vec<tree, va_gc> *args;
31168 if (fn_name == error_mark_node
31169 || cp_parser_error_occurred (parser)
31170 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31171 || ((args = cp_parser_parenthesized_expression_list
31172 (parser, non_attr, /*cast_p=*/false,
31173 /*allow_expansion_p=*/true,
31174 /*non_constant_p=*/NULL)),
31175 cp_parser_error_occurred (parser)))
31177 finish_omp_structured_block (block);
31178 cp_parser_abort_tentative_parse (parser);
31179 cp_parser_error (parser, "expected id-expression (arguments)");
31180 return false;
31182 unsigned int i;
31183 tree arg;
31184 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31185 if (arg == omp_priv
31186 || (TREE_CODE (arg) == ADDR_EXPR
31187 && TREE_OPERAND (arg, 0) == omp_priv))
31188 break;
31189 cp_parser_abort_tentative_parse (parser);
31190 if (arg == NULL_TREE)
31191 error ("one of the initializer call arguments should be %<omp_priv%>"
31192 " or %<&omp_priv%>");
31193 initializer = cp_parser_postfix_expression (parser, false, false, false,
31194 false, NULL);
31195 finish_expr_stmt (initializer);
31198 block = finish_omp_structured_block (block);
31199 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31200 finish_expr_stmt (block);
31202 if (ctor)
31203 add_decl_expr (omp_orig);
31205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31206 return false;
31209 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31210 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31212 return true;
31215 /* OpenMP 4.0
31216 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31217 initializer-clause[opt] new-line
31219 initializer-clause:
31220 initializer (omp_priv initializer)
31221 initializer (function-name (argument-list)) */
31223 static void
31224 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31225 enum pragma_context)
31227 auto_vec<tree> types;
31228 enum tree_code reduc_code = ERROR_MARK;
31229 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31230 unsigned int i;
31231 cp_token *first_token;
31232 cp_token_cache *cp;
31233 int errs;
31234 void *p;
31236 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31237 p = obstack_alloc (&declarator_obstack, 0);
31239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31240 goto fail;
31242 switch (cp_lexer_peek_token (parser->lexer)->type)
31244 case CPP_PLUS:
31245 reduc_code = PLUS_EXPR;
31246 break;
31247 case CPP_MULT:
31248 reduc_code = MULT_EXPR;
31249 break;
31250 case CPP_MINUS:
31251 reduc_code = MINUS_EXPR;
31252 break;
31253 case CPP_AND:
31254 reduc_code = BIT_AND_EXPR;
31255 break;
31256 case CPP_XOR:
31257 reduc_code = BIT_XOR_EXPR;
31258 break;
31259 case CPP_OR:
31260 reduc_code = BIT_IOR_EXPR;
31261 break;
31262 case CPP_AND_AND:
31263 reduc_code = TRUTH_ANDIF_EXPR;
31264 break;
31265 case CPP_OR_OR:
31266 reduc_code = TRUTH_ORIF_EXPR;
31267 break;
31268 case CPP_NAME:
31269 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31270 break;
31271 default:
31272 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31273 "%<|%>, %<&&%>, %<||%> or identifier");
31274 goto fail;
31277 if (reduc_code != ERROR_MARK)
31278 cp_lexer_consume_token (parser->lexer);
31280 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31281 if (reduc_id == error_mark_node)
31282 goto fail;
31284 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31285 goto fail;
31287 /* Types may not be defined in declare reduction type list. */
31288 const char *saved_message;
31289 saved_message = parser->type_definition_forbidden_message;
31290 parser->type_definition_forbidden_message
31291 = G_("types may not be defined in declare reduction type list");
31292 bool saved_colon_corrects_to_scope_p;
31293 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31294 parser->colon_corrects_to_scope_p = false;
31295 bool saved_colon_doesnt_start_class_def_p;
31296 saved_colon_doesnt_start_class_def_p
31297 = parser->colon_doesnt_start_class_def_p;
31298 parser->colon_doesnt_start_class_def_p = true;
31300 while (true)
31302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31303 type = cp_parser_type_id (parser);
31304 if (type == error_mark_node)
31306 else if (ARITHMETIC_TYPE_P (type)
31307 && (orig_reduc_id == NULL_TREE
31308 || (TREE_CODE (type) != COMPLEX_TYPE
31309 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31310 "min") == 0
31311 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31312 "max") == 0))))
31313 error_at (loc, "predeclared arithmetic type %qT in "
31314 "%<#pragma omp declare reduction%>", type);
31315 else if (TREE_CODE (type) == FUNCTION_TYPE
31316 || TREE_CODE (type) == METHOD_TYPE
31317 || TREE_CODE (type) == ARRAY_TYPE)
31318 error_at (loc, "function or array type %qT in "
31319 "%<#pragma omp declare reduction%>", type);
31320 else if (TREE_CODE (type) == REFERENCE_TYPE)
31321 error_at (loc, "reference type %qT in "
31322 "%<#pragma omp declare reduction%>", type);
31323 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31324 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31325 "%<#pragma omp declare reduction%>", type);
31326 else
31327 types.safe_push (type);
31329 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31330 cp_lexer_consume_token (parser->lexer);
31331 else
31332 break;
31335 /* Restore the saved message. */
31336 parser->type_definition_forbidden_message = saved_message;
31337 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31338 parser->colon_doesnt_start_class_def_p
31339 = saved_colon_doesnt_start_class_def_p;
31341 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31342 || types.is_empty ())
31344 fail:
31345 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31346 goto done;
31349 first_token = cp_lexer_peek_token (parser->lexer);
31350 cp = NULL;
31351 errs = errorcount;
31352 FOR_EACH_VEC_ELT (types, i, type)
31354 tree fntype
31355 = build_function_type_list (void_type_node,
31356 cp_build_reference_type (type, false),
31357 NULL_TREE);
31358 tree this_reduc_id = reduc_id;
31359 if (!dependent_type_p (type))
31360 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31361 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31362 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31363 DECL_ARTIFICIAL (fndecl) = 1;
31364 DECL_EXTERNAL (fndecl) = 1;
31365 DECL_DECLARED_INLINE_P (fndecl) = 1;
31366 DECL_IGNORED_P (fndecl) = 1;
31367 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31368 DECL_ATTRIBUTES (fndecl)
31369 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31370 DECL_ATTRIBUTES (fndecl));
31371 if (processing_template_decl)
31372 fndecl = push_template_decl (fndecl);
31373 bool block_scope = false;
31374 tree block = NULL_TREE;
31375 if (current_function_decl)
31377 block_scope = true;
31378 DECL_CONTEXT (fndecl) = global_namespace;
31379 if (!processing_template_decl)
31380 pushdecl (fndecl);
31382 else if (current_class_type)
31384 if (cp == NULL)
31386 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31387 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31388 cp_lexer_consume_token (parser->lexer);
31389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31390 goto fail;
31391 cp = cp_token_cache_new (first_token,
31392 cp_lexer_peek_nth_token (parser->lexer,
31393 2));
31395 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31396 finish_member_declaration (fndecl);
31397 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31398 DECL_PENDING_INLINE_P (fndecl) = 1;
31399 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31400 continue;
31402 else
31404 DECL_CONTEXT (fndecl) = current_namespace;
31405 pushdecl (fndecl);
31407 if (!block_scope)
31408 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31409 else
31410 block = begin_omp_structured_block ();
31411 if (cp)
31413 cp_parser_push_lexer_for_tokens (parser, cp);
31414 parser->lexer->in_pragma = true;
31416 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31418 if (!block_scope)
31419 finish_function (0);
31420 else
31421 DECL_CONTEXT (fndecl) = current_function_decl;
31422 if (cp)
31423 cp_parser_pop_lexer (parser);
31424 goto fail;
31426 if (cp)
31427 cp_parser_pop_lexer (parser);
31428 if (!block_scope)
31429 finish_function (0);
31430 else
31432 DECL_CONTEXT (fndecl) = current_function_decl;
31433 block = finish_omp_structured_block (block);
31434 if (TREE_CODE (block) == BIND_EXPR)
31435 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31436 else if (TREE_CODE (block) == STATEMENT_LIST)
31437 DECL_SAVED_TREE (fndecl) = block;
31438 if (processing_template_decl)
31439 add_decl_expr (fndecl);
31441 cp_check_omp_declare_reduction (fndecl);
31442 if (cp == NULL && types.length () > 1)
31443 cp = cp_token_cache_new (first_token,
31444 cp_lexer_peek_nth_token (parser->lexer, 2));
31445 if (errs != errorcount)
31446 break;
31449 cp_parser_require_pragma_eol (parser, pragma_tok);
31451 done:
31452 /* Free any declarators allocated. */
31453 obstack_free (&declarator_obstack, p);
31456 /* OpenMP 4.0
31457 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31458 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31459 initializer-clause[opt] new-line
31460 #pragma omp declare target new-line */
31462 static void
31463 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31464 enum pragma_context context)
31466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31469 const char *p = IDENTIFIER_POINTER (id);
31471 if (strcmp (p, "simd") == 0)
31473 cp_lexer_consume_token (parser->lexer);
31474 cp_parser_omp_declare_simd (parser, pragma_tok,
31475 context);
31476 return;
31478 cp_ensure_no_omp_declare_simd (parser);
31479 if (strcmp (p, "reduction") == 0)
31481 cp_lexer_consume_token (parser->lexer);
31482 cp_parser_omp_declare_reduction (parser, pragma_tok,
31483 context);
31484 return;
31486 if (!flag_openmp) /* flag_openmp_simd */
31488 cp_parser_require_pragma_eol (parser, pragma_tok);
31489 return;
31491 if (strcmp (p, "target") == 0)
31493 cp_lexer_consume_token (parser->lexer);
31494 cp_parser_omp_declare_target (parser, pragma_tok);
31495 return;
31498 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31499 "or %<target%>");
31500 cp_parser_require_pragma_eol (parser, pragma_tok);
31503 /* Main entry point to OpenMP statement pragmas. */
31505 static void
31506 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31508 tree stmt;
31509 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31510 omp_clause_mask mask (0);
31512 switch (pragma_tok->pragma_kind)
31514 case PRAGMA_OMP_ATOMIC:
31515 cp_parser_omp_atomic (parser, pragma_tok);
31516 return;
31517 case PRAGMA_OMP_CRITICAL:
31518 stmt = cp_parser_omp_critical (parser, pragma_tok);
31519 break;
31520 case PRAGMA_OMP_DISTRIBUTE:
31521 strcpy (p_name, "#pragma omp");
31522 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31523 break;
31524 case PRAGMA_OMP_FOR:
31525 strcpy (p_name, "#pragma omp");
31526 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31527 break;
31528 case PRAGMA_OMP_MASTER:
31529 stmt = cp_parser_omp_master (parser, pragma_tok);
31530 break;
31531 case PRAGMA_OMP_ORDERED:
31532 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31533 break;
31534 case PRAGMA_OMP_PARALLEL:
31535 strcpy (p_name, "#pragma omp");
31536 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31537 break;
31538 case PRAGMA_OMP_SECTIONS:
31539 strcpy (p_name, "#pragma omp");
31540 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31541 break;
31542 case PRAGMA_OMP_SIMD:
31543 strcpy (p_name, "#pragma omp");
31544 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31545 break;
31546 case PRAGMA_OMP_SINGLE:
31547 stmt = cp_parser_omp_single (parser, pragma_tok);
31548 break;
31549 case PRAGMA_OMP_TASK:
31550 stmt = cp_parser_omp_task (parser, pragma_tok);
31551 break;
31552 case PRAGMA_OMP_TASKGROUP:
31553 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31554 break;
31555 case PRAGMA_OMP_TEAMS:
31556 strcpy (p_name, "#pragma omp");
31557 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31558 break;
31559 default:
31560 gcc_unreachable ();
31563 if (stmt)
31564 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31567 /* Transactional Memory parsing routines. */
31569 /* Parse a transaction attribute.
31571 txn-attribute:
31572 attribute
31573 [ [ identifier ] ]
31575 ??? Simplify this when C++0x bracket attributes are
31576 implemented properly. */
31578 static tree
31579 cp_parser_txn_attribute_opt (cp_parser *parser)
31581 cp_token *token;
31582 tree attr_name, attr = NULL;
31584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31585 return cp_parser_attributes_opt (parser);
31587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31588 return NULL_TREE;
31589 cp_lexer_consume_token (parser->lexer);
31590 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31591 goto error1;
31593 token = cp_lexer_peek_token (parser->lexer);
31594 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31596 token = cp_lexer_consume_token (parser->lexer);
31598 attr_name = (token->type == CPP_KEYWORD
31599 /* For keywords, use the canonical spelling,
31600 not the parsed identifier. */
31601 ? ridpointers[(int) token->keyword]
31602 : token->u.value);
31603 attr = build_tree_list (attr_name, NULL_TREE);
31605 else
31606 cp_parser_error (parser, "expected identifier");
31608 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31609 error1:
31610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31611 return attr;
31614 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31616 transaction-statement:
31617 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31618 compound-statement
31619 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31622 static tree
31623 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31625 unsigned char old_in = parser->in_transaction;
31626 unsigned char this_in = 1, new_in;
31627 cp_token *token;
31628 tree stmt, attrs, noex;
31630 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31631 || keyword == RID_TRANSACTION_RELAXED);
31632 token = cp_parser_require_keyword (parser, keyword,
31633 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31634 : RT_TRANSACTION_RELAXED));
31635 gcc_assert (token != NULL);
31637 if (keyword == RID_TRANSACTION_RELAXED)
31638 this_in |= TM_STMT_ATTR_RELAXED;
31639 else
31641 attrs = cp_parser_txn_attribute_opt (parser);
31642 if (attrs)
31643 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31646 /* Parse a noexcept specification. */
31647 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31649 /* Keep track if we're in the lexical scope of an outer transaction. */
31650 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31652 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31654 parser->in_transaction = new_in;
31655 cp_parser_compound_statement (parser, NULL, false, false);
31656 parser->in_transaction = old_in;
31658 finish_transaction_stmt (stmt, NULL, this_in, noex);
31660 return stmt;
31663 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31665 transaction-expression:
31666 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31667 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31670 static tree
31671 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31673 unsigned char old_in = parser->in_transaction;
31674 unsigned char this_in = 1;
31675 cp_token *token;
31676 tree expr, noex;
31677 bool noex_expr;
31679 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31680 || keyword == RID_TRANSACTION_RELAXED);
31682 if (!flag_tm)
31683 error (keyword == RID_TRANSACTION_RELAXED
31684 ? G_("%<__transaction_relaxed%> without transactional memory "
31685 "support enabled")
31686 : G_("%<__transaction_atomic%> without transactional memory "
31687 "support enabled"));
31689 token = cp_parser_require_keyword (parser, keyword,
31690 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31691 : RT_TRANSACTION_RELAXED));
31692 gcc_assert (token != NULL);
31694 if (keyword == RID_TRANSACTION_RELAXED)
31695 this_in |= TM_STMT_ATTR_RELAXED;
31697 /* Set this early. This might mean that we allow transaction_cancel in
31698 an expression that we find out later actually has to be a constexpr.
31699 However, we expect that cxx_constant_value will be able to deal with
31700 this; also, if the noexcept has no constexpr, then what we parse next
31701 really is a transaction's body. */
31702 parser->in_transaction = this_in;
31704 /* Parse a noexcept specification. */
31705 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31706 true);
31708 if (!noex || !noex_expr
31709 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31711 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31713 expr = cp_parser_expression (parser);
31714 expr = finish_parenthesized_expr (expr);
31716 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31718 else
31720 /* The only expression that is available got parsed for the noexcept
31721 already. noexcept is true then. */
31722 expr = noex;
31723 noex = boolean_true_node;
31726 expr = build_transaction_expr (token->location, expr, this_in, noex);
31727 parser->in_transaction = old_in;
31729 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31730 return error_mark_node;
31732 return (flag_tm ? expr : error_mark_node);
31735 /* Parse a function-transaction-block.
31737 function-transaction-block:
31738 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31739 function-body
31740 __transaction_atomic txn-attribute[opt] function-try-block
31741 __transaction_relaxed ctor-initializer[opt] function-body
31742 __transaction_relaxed function-try-block
31745 static bool
31746 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31748 unsigned char old_in = parser->in_transaction;
31749 unsigned char new_in = 1;
31750 tree compound_stmt, stmt, attrs;
31751 bool ctor_initializer_p;
31752 cp_token *token;
31754 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31755 || keyword == RID_TRANSACTION_RELAXED);
31756 token = cp_parser_require_keyword (parser, keyword,
31757 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31758 : RT_TRANSACTION_RELAXED));
31759 gcc_assert (token != NULL);
31761 if (keyword == RID_TRANSACTION_RELAXED)
31762 new_in |= TM_STMT_ATTR_RELAXED;
31763 else
31765 attrs = cp_parser_txn_attribute_opt (parser);
31766 if (attrs)
31767 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31770 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31772 parser->in_transaction = new_in;
31774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31775 ctor_initializer_p = cp_parser_function_try_block (parser);
31776 else
31777 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31778 (parser, /*in_function_try_block=*/false);
31780 parser->in_transaction = old_in;
31782 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31784 return ctor_initializer_p;
31787 /* Parse a __transaction_cancel statement.
31789 cancel-statement:
31790 __transaction_cancel txn-attribute[opt] ;
31791 __transaction_cancel txn-attribute[opt] throw-expression ;
31793 ??? Cancel and throw is not yet implemented. */
31795 static tree
31796 cp_parser_transaction_cancel (cp_parser *parser)
31798 cp_token *token;
31799 bool is_outer = false;
31800 tree stmt, attrs;
31802 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31803 RT_TRANSACTION_CANCEL);
31804 gcc_assert (token != NULL);
31806 attrs = cp_parser_txn_attribute_opt (parser);
31807 if (attrs)
31808 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31810 /* ??? Parse cancel-and-throw here. */
31812 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31814 if (!flag_tm)
31816 error_at (token->location, "%<__transaction_cancel%> without "
31817 "transactional memory support enabled");
31818 return error_mark_node;
31820 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31822 error_at (token->location, "%<__transaction_cancel%> within a "
31823 "%<__transaction_relaxed%>");
31824 return error_mark_node;
31826 else if (is_outer)
31828 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31829 && !is_tm_may_cancel_outer (current_function_decl))
31831 error_at (token->location, "outer %<__transaction_cancel%> not "
31832 "within outer %<__transaction_atomic%>");
31833 error_at (token->location,
31834 " or a %<transaction_may_cancel_outer%> function");
31835 return error_mark_node;
31838 else if (parser->in_transaction == 0)
31840 error_at (token->location, "%<__transaction_cancel%> not within "
31841 "%<__transaction_atomic%>");
31842 return error_mark_node;
31845 stmt = build_tm_abort_call (token->location, is_outer);
31846 add_stmt (stmt);
31848 return stmt;
31851 /* The parser. */
31853 static GTY (()) cp_parser *the_parser;
31856 /* Special handling for the first token or line in the file. The first
31857 thing in the file might be #pragma GCC pch_preprocess, which loads a
31858 PCH file, which is a GC collection point. So we need to handle this
31859 first pragma without benefit of an existing lexer structure.
31861 Always returns one token to the caller in *FIRST_TOKEN. This is
31862 either the true first token of the file, or the first token after
31863 the initial pragma. */
31865 static void
31866 cp_parser_initial_pragma (cp_token *first_token)
31868 tree name = NULL;
31870 cp_lexer_get_preprocessor_token (NULL, first_token);
31871 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31872 return;
31874 cp_lexer_get_preprocessor_token (NULL, first_token);
31875 if (first_token->type == CPP_STRING)
31877 name = first_token->u.value;
31879 cp_lexer_get_preprocessor_token (NULL, first_token);
31880 if (first_token->type != CPP_PRAGMA_EOL)
31881 error_at (first_token->location,
31882 "junk at end of %<#pragma GCC pch_preprocess%>");
31884 else
31885 error_at (first_token->location, "expected string literal");
31887 /* Skip to the end of the pragma. */
31888 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31889 cp_lexer_get_preprocessor_token (NULL, first_token);
31891 /* Now actually load the PCH file. */
31892 if (name)
31893 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31895 /* Read one more token to return to our caller. We have to do this
31896 after reading the PCH file in, since its pointers have to be
31897 live. */
31898 cp_lexer_get_preprocessor_token (NULL, first_token);
31901 /* Parses the grainsize pragma for the _Cilk_for statement.
31902 Syntax:
31903 #pragma cilk grainsize = <VALUE>. */
31905 static void
31906 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31908 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31910 tree exp = cp_parser_binary_expression (parser, false, false,
31911 PREC_NOT_OPERATOR, NULL);
31912 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31913 if (!exp || exp == error_mark_node)
31915 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31916 return;
31919 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31921 cp_parser_cilk_for (parser, exp);
31922 else
31923 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31924 "%<#pragma cilk grainsize%> is not followed by "
31925 "%<_Cilk_for%>");
31926 return;
31928 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31931 /* Normal parsing of a pragma token. Here we can (and must) use the
31932 regular lexer. */
31934 static bool
31935 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31937 cp_token *pragma_tok;
31938 unsigned int id;
31940 pragma_tok = cp_lexer_consume_token (parser->lexer);
31941 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31942 parser->lexer->in_pragma = true;
31944 id = pragma_tok->pragma_kind;
31945 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31946 cp_ensure_no_omp_declare_simd (parser);
31947 switch (id)
31949 case PRAGMA_GCC_PCH_PREPROCESS:
31950 error_at (pragma_tok->location,
31951 "%<#pragma GCC pch_preprocess%> must be first");
31952 break;
31954 case PRAGMA_OMP_BARRIER:
31955 switch (context)
31957 case pragma_compound:
31958 cp_parser_omp_barrier (parser, pragma_tok);
31959 return false;
31960 case pragma_stmt:
31961 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31962 "used in compound statements");
31963 break;
31964 default:
31965 goto bad_stmt;
31967 break;
31969 case PRAGMA_OMP_FLUSH:
31970 switch (context)
31972 case pragma_compound:
31973 cp_parser_omp_flush (parser, pragma_tok);
31974 return false;
31975 case pragma_stmt:
31976 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31977 "used in compound statements");
31978 break;
31979 default:
31980 goto bad_stmt;
31982 break;
31984 case PRAGMA_OMP_TASKWAIT:
31985 switch (context)
31987 case pragma_compound:
31988 cp_parser_omp_taskwait (parser, pragma_tok);
31989 return false;
31990 case pragma_stmt:
31991 error_at (pragma_tok->location,
31992 "%<#pragma omp taskwait%> may only be "
31993 "used in compound statements");
31994 break;
31995 default:
31996 goto bad_stmt;
31998 break;
32000 case PRAGMA_OMP_TASKYIELD:
32001 switch (context)
32003 case pragma_compound:
32004 cp_parser_omp_taskyield (parser, pragma_tok);
32005 return false;
32006 case pragma_stmt:
32007 error_at (pragma_tok->location,
32008 "%<#pragma omp taskyield%> may only be "
32009 "used in compound statements");
32010 break;
32011 default:
32012 goto bad_stmt;
32014 break;
32016 case PRAGMA_OMP_CANCEL:
32017 switch (context)
32019 case pragma_compound:
32020 cp_parser_omp_cancel (parser, pragma_tok);
32021 return false;
32022 case pragma_stmt:
32023 error_at (pragma_tok->location,
32024 "%<#pragma omp cancel%> may only be "
32025 "used in compound statements");
32026 break;
32027 default:
32028 goto bad_stmt;
32030 break;
32032 case PRAGMA_OMP_CANCELLATION_POINT:
32033 switch (context)
32035 case pragma_compound:
32036 cp_parser_omp_cancellation_point (parser, pragma_tok);
32037 return false;
32038 case pragma_stmt:
32039 error_at (pragma_tok->location,
32040 "%<#pragma omp cancellation point%> may only be "
32041 "used in compound statements");
32042 break;
32043 default:
32044 goto bad_stmt;
32046 break;
32048 case PRAGMA_OMP_THREADPRIVATE:
32049 cp_parser_omp_threadprivate (parser, pragma_tok);
32050 return false;
32052 case PRAGMA_OMP_DECLARE_REDUCTION:
32053 cp_parser_omp_declare (parser, pragma_tok, context);
32054 return false;
32056 case PRAGMA_OMP_ATOMIC:
32057 case PRAGMA_OMP_CRITICAL:
32058 case PRAGMA_OMP_DISTRIBUTE:
32059 case PRAGMA_OMP_FOR:
32060 case PRAGMA_OMP_MASTER:
32061 case PRAGMA_OMP_ORDERED:
32062 case PRAGMA_OMP_PARALLEL:
32063 case PRAGMA_OMP_SECTIONS:
32064 case PRAGMA_OMP_SIMD:
32065 case PRAGMA_OMP_SINGLE:
32066 case PRAGMA_OMP_TASK:
32067 case PRAGMA_OMP_TASKGROUP:
32068 case PRAGMA_OMP_TEAMS:
32069 if (context != pragma_stmt && context != pragma_compound)
32070 goto bad_stmt;
32071 cp_parser_omp_construct (parser, pragma_tok);
32072 return true;
32074 case PRAGMA_OMP_TARGET:
32075 return cp_parser_omp_target (parser, pragma_tok, context);
32077 case PRAGMA_OMP_END_DECLARE_TARGET:
32078 cp_parser_omp_end_declare_target (parser, pragma_tok);
32079 return false;
32081 case PRAGMA_OMP_SECTION:
32082 error_at (pragma_tok->location,
32083 "%<#pragma omp section%> may only be used in "
32084 "%<#pragma omp sections%> construct");
32085 break;
32087 case PRAGMA_IVDEP:
32089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32090 cp_token *tok;
32091 tok = cp_lexer_peek_token (the_parser->lexer);
32092 if (tok->type != CPP_KEYWORD
32093 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32094 && tok->keyword != RID_DO))
32096 cp_parser_error (parser, "for, while or do statement expected");
32097 return false;
32099 cp_parser_iteration_statement (parser, true);
32100 return true;
32103 case PRAGMA_CILK_SIMD:
32104 if (context == pragma_external)
32106 error_at (pragma_tok->location,
32107 "%<#pragma simd%> must be inside a function");
32108 break;
32110 cp_parser_cilk_simd (parser, pragma_tok);
32111 return true;
32113 case PRAGMA_CILK_GRAINSIZE:
32114 if (context == pragma_external)
32116 error_at (pragma_tok->location,
32117 "%<#pragma cilk grainsize%> must be inside a function");
32118 break;
32121 /* Ignore the pragma if Cilk Plus is not enabled. */
32122 if (flag_cilkplus)
32124 cp_parser_cilk_grainsize (parser, pragma_tok);
32125 return true;
32127 else
32129 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32130 "%<#pragma cilk grainsize%>");
32131 break;
32134 default:
32135 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32136 c_invoke_pragma_handler (id);
32137 break;
32139 bad_stmt:
32140 cp_parser_error (parser, "expected declaration specifiers");
32141 break;
32144 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32145 return false;
32148 /* The interface the pragma parsers have to the lexer. */
32150 enum cpp_ttype
32151 pragma_lex (tree *value)
32153 cp_token *tok;
32154 enum cpp_ttype ret;
32156 tok = cp_lexer_peek_token (the_parser->lexer);
32158 ret = tok->type;
32159 *value = tok->u.value;
32161 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32162 ret = CPP_EOF;
32163 else if (ret == CPP_STRING)
32164 *value = cp_parser_string_literal (the_parser, false, false);
32165 else
32167 cp_lexer_consume_token (the_parser->lexer);
32168 if (ret == CPP_KEYWORD)
32169 ret = CPP_NAME;
32172 return ret;
32176 /* External interface. */
32178 /* Parse one entire translation unit. */
32180 void
32181 c_parse_file (void)
32183 static bool already_called = false;
32185 if (already_called)
32186 fatal_error ("inter-module optimizations not implemented for C++");
32187 already_called = true;
32189 the_parser = cp_parser_new ();
32190 push_deferring_access_checks (flag_access_control
32191 ? dk_no_deferred : dk_no_check);
32192 cp_parser_translation_unit (the_parser);
32193 the_parser = NULL;
32196 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32197 vectorlength clause:
32198 Syntax:
32199 vectorlength ( constant-expression ) */
32201 static tree
32202 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32203 bool is_simd_fn)
32205 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32206 tree expr;
32207 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32208 safelen clause. Thus, vectorlength is represented as OMP 4.0
32209 safelen. For SIMD-enabled function it is represented by OMP 4.0
32210 simdlen. */
32211 if (!is_simd_fn)
32212 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32213 loc);
32214 else
32215 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32216 loc);
32218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32219 return error_mark_node;
32221 expr = cp_parser_constant_expression (parser);
32222 expr = maybe_constant_value (expr);
32224 /* If expr == error_mark_node, then don't emit any errors nor
32225 create a clause. if any of the above functions returns
32226 error mark node then they would have emitted an error message. */
32227 if (expr == error_mark_node)
32229 else if (!TREE_TYPE (expr)
32230 || !TREE_CONSTANT (expr)
32231 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32232 error_at (loc, "vectorlength must be an integer constant");
32233 else if (TREE_CONSTANT (expr)
32234 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32235 error_at (loc, "vectorlength must be a power of 2");
32236 else
32238 tree c;
32239 if (!is_simd_fn)
32241 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32242 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32243 OMP_CLAUSE_CHAIN (c) = clauses;
32244 clauses = c;
32246 else
32248 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32249 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32250 OMP_CLAUSE_CHAIN (c) = clauses;
32251 clauses = c;
32255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32256 return error_mark_node;
32257 return clauses;
32260 /* Handles the Cilk Plus #pragma simd linear clause.
32261 Syntax:
32262 linear ( simd-linear-variable-list )
32264 simd-linear-variable-list:
32265 simd-linear-variable
32266 simd-linear-variable-list , simd-linear-variable
32268 simd-linear-variable:
32269 id-expression
32270 id-expression : simd-linear-step
32272 simd-linear-step:
32273 conditional-expression */
32275 static tree
32276 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32278 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32280 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32281 return clauses;
32282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32284 cp_parser_error (parser, "expected identifier");
32285 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32286 return error_mark_node;
32289 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32290 parser->colon_corrects_to_scope_p = false;
32291 while (1)
32293 cp_token *token = cp_lexer_peek_token (parser->lexer);
32294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32296 cp_parser_error (parser, "expected variable-name");
32297 clauses = error_mark_node;
32298 break;
32301 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32302 false, false);
32303 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32304 token->location);
32305 if (decl == error_mark_node)
32307 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32308 token->location);
32309 clauses = error_mark_node;
32311 else
32313 tree e = NULL_TREE;
32314 tree step_size = integer_one_node;
32316 /* If present, parse the linear step. Otherwise, assume the default
32317 value of 1. */
32318 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32320 cp_lexer_consume_token (parser->lexer);
32322 e = cp_parser_assignment_expression (parser);
32323 e = maybe_constant_value (e);
32325 if (e == error_mark_node)
32327 /* If an error has occurred, then the whole pragma is
32328 considered ill-formed. Thus, no reason to keep
32329 parsing. */
32330 clauses = error_mark_node;
32331 break;
32333 else if (type_dependent_expression_p (e)
32334 || value_dependent_expression_p (e)
32335 || (TREE_TYPE (e)
32336 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32337 && (TREE_CONSTANT (e)
32338 || DECL_P (e))))
32339 step_size = e;
32340 else
32341 cp_parser_error (parser,
32342 "step size must be an integer constant "
32343 "expression or an integer variable");
32346 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32347 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32348 OMP_CLAUSE_DECL (l) = decl;
32349 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32350 OMP_CLAUSE_CHAIN (l) = clauses;
32351 clauses = l;
32353 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32354 cp_lexer_consume_token (parser->lexer);
32355 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32356 break;
32357 else
32359 error_at (cp_lexer_peek_token (parser->lexer)->location,
32360 "expected %<,%> or %<)%> after %qE", decl);
32361 clauses = error_mark_node;
32362 break;
32365 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32366 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32367 return clauses;
32370 /* Returns the name of the next clause. If the clause is not
32371 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32372 token is not consumed. Otherwise, the appropriate enum from the
32373 pragma_simd_clause is returned and the token is consumed. */
32375 static pragma_omp_clause
32376 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32378 pragma_omp_clause clause_type;
32379 cp_token *token = cp_lexer_peek_token (parser->lexer);
32381 if (token->keyword == RID_PRIVATE)
32382 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32383 else if (!token->u.value || token->type != CPP_NAME)
32384 return PRAGMA_CILK_CLAUSE_NONE;
32385 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32386 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32387 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32388 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32389 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32390 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32391 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32392 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32393 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32394 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32395 else
32396 return PRAGMA_CILK_CLAUSE_NONE;
32398 cp_lexer_consume_token (parser->lexer);
32399 return clause_type;
32402 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32404 static tree
32405 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32407 tree clauses = NULL_TREE;
32409 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32410 && clauses != error_mark_node)
32412 pragma_omp_clause c_kind;
32413 c_kind = cp_parser_cilk_simd_clause_name (parser);
32414 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32415 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32416 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32417 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32418 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32419 /* Use the OpenMP 4.0 equivalent function. */
32420 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32421 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32422 /* Use the OpenMP 4.0 equivalent function. */
32423 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32424 clauses);
32425 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32426 /* Use the OMP 4.0 equivalent function. */
32427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32428 clauses);
32429 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32430 /* Use the OMP 4.0 equivalent function. */
32431 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32432 else
32434 clauses = error_mark_node;
32435 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32436 break;
32440 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32442 if (clauses == error_mark_node)
32443 return error_mark_node;
32444 else
32445 return c_finish_cilk_clauses (clauses);
32448 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32450 static void
32451 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32453 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32455 if (clauses == error_mark_node)
32456 return;
32458 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32460 error_at (cp_lexer_peek_token (parser->lexer)->location,
32461 "for statement expected");
32462 return;
32465 tree sb = begin_omp_structured_block ();
32466 int save = cp_parser_begin_omp_structured_block (parser);
32467 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32468 if (ret)
32469 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32470 cp_parser_end_omp_structured_block (parser, save);
32471 add_stmt (finish_omp_structured_block (sb));
32474 /* Main entry-point for parsing Cilk Plus _Cilk_for
32475 loops. The return value is error_mark_node
32476 when errors happen and CILK_FOR tree on success. */
32478 static tree
32479 cp_parser_cilk_for (cp_parser *parser, tree grain)
32481 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32482 gcc_unreachable ();
32484 tree sb = begin_omp_structured_block ();
32485 int save = cp_parser_begin_omp_structured_block (parser);
32487 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32488 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32489 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32490 clauses = finish_omp_clauses (clauses);
32492 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32493 if (ret)
32494 cpp_validate_cilk_plus_loop (ret);
32495 else
32496 ret = error_mark_node;
32498 cp_parser_end_omp_structured_block (parser, save);
32499 add_stmt (finish_omp_structured_block (sb));
32500 return ret;
32503 /* Create an identifier for a generic parameter type (a synthesized
32504 template parameter implied by `auto' or a concept identifier). */
32506 static GTY(()) int generic_parm_count;
32507 static tree
32508 make_generic_type_name ()
32510 char buf[32];
32511 sprintf (buf, "auto:%d", ++generic_parm_count);
32512 return get_identifier (buf);
32515 /* Predicate that behaves as is_auto_or_concept but matches the parent
32516 node of the generic type rather than the generic type itself. This
32517 allows for type transformation in add_implicit_template_parms. */
32519 static inline bool
32520 tree_type_is_auto_or_concept (const_tree t)
32522 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32525 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32526 (creating a new template parameter list if necessary). Returns the newly
32527 created template type parm. */
32529 tree
32530 synthesize_implicit_template_parm (cp_parser *parser)
32532 gcc_assert (current_binding_level->kind == sk_function_parms);
32534 /* We are either continuing a function template that already contains implicit
32535 template parameters, creating a new fully-implicit function template, or
32536 extending an existing explicit function template with implicit template
32537 parameters. */
32539 cp_binding_level *const entry_scope = current_binding_level;
32541 bool become_template = false;
32542 cp_binding_level *parent_scope = 0;
32544 if (parser->implicit_template_scope)
32546 gcc_assert (parser->implicit_template_parms);
32548 current_binding_level = parser->implicit_template_scope;
32550 else
32552 /* Roll back to the existing template parameter scope (in the case of
32553 extending an explicit function template) or introduce a new template
32554 parameter scope ahead of the function parameter scope (or class scope
32555 in the case of out-of-line member definitions). The function scope is
32556 added back after template parameter synthesis below. */
32558 cp_binding_level *scope = entry_scope;
32560 while (scope->kind == sk_function_parms)
32562 parent_scope = scope;
32563 scope = scope->level_chain;
32565 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32567 /* If not defining a class, then any class scope is a scope level in
32568 an out-of-line member definition. In this case simply wind back
32569 beyond the first such scope to inject the template parameter list.
32570 Otherwise wind back to the class being defined. The latter can
32571 occur in class member friend declarations such as:
32573 class A {
32574 void foo (auto);
32576 class B {
32577 friend void A::foo (auto);
32580 The template parameter list synthesized for the friend declaration
32581 must be injected in the scope of 'B'. This can also occur in
32582 erroneous cases such as:
32584 struct A {
32585 struct B {
32586 void foo (auto);
32588 void B::foo (auto) {}
32591 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32592 but, nevertheless, the template parameter list synthesized for the
32593 declarator should be injected into the scope of 'A' as if the
32594 ill-formed template was specified explicitly. */
32596 while (scope->kind == sk_class && !scope->defining_class_p)
32598 parent_scope = scope;
32599 scope = scope->level_chain;
32603 current_binding_level = scope;
32605 if (scope->kind != sk_template_parms
32606 || !function_being_declared_is_template_p (parser))
32608 /* Introduce a new template parameter list for implicit template
32609 parameters. */
32611 become_template = true;
32613 parser->implicit_template_scope
32614 = begin_scope (sk_template_parms, NULL);
32616 ++processing_template_decl;
32618 parser->fully_implicit_function_template_p = true;
32619 ++parser->num_template_parameter_lists;
32621 else
32623 /* Synthesize implicit template parameters at the end of the explicit
32624 template parameter list. */
32626 gcc_assert (current_template_parms);
32628 parser->implicit_template_scope = scope;
32630 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32631 parser->implicit_template_parms
32632 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32636 /* Synthesize a new template parameter and track the current template
32637 parameter chain with implicit_template_parms. */
32639 tree synth_id = make_generic_type_name ();
32640 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32641 synth_id);
32642 tree new_parm
32643 = process_template_parm (parser->implicit_template_parms,
32644 input_location,
32645 build_tree_list (NULL_TREE, synth_tmpl_parm),
32646 /*non_type=*/false,
32647 /*param_pack=*/false);
32650 if (parser->implicit_template_parms)
32651 parser->implicit_template_parms
32652 = TREE_CHAIN (parser->implicit_template_parms);
32653 else
32654 parser->implicit_template_parms = new_parm;
32656 tree new_type = TREE_TYPE (getdecls ());
32658 /* If creating a fully implicit function template, start the new implicit
32659 template parameter list with this synthesized type, otherwise grow the
32660 current template parameter list. */
32662 if (become_template)
32664 parent_scope->level_chain = current_binding_level;
32666 tree new_parms = make_tree_vec (1);
32667 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32668 current_template_parms = tree_cons (size_int (processing_template_decl),
32669 new_parms, current_template_parms);
32671 else
32673 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32674 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32675 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32676 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32679 current_binding_level = entry_scope;
32681 return new_type;
32684 /* Finish the declaration of a fully implicit function template. Such a
32685 template has no explicit template parameter list so has not been through the
32686 normal template head and tail processing. synthesize_implicit_template_parm
32687 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32688 provided if the declaration is a class member such that its template
32689 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32690 form is returned. Otherwise NULL_TREE is returned. */
32692 tree
32693 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32695 gcc_assert (parser->fully_implicit_function_template_p);
32697 if (member_decl_opt && member_decl_opt != error_mark_node
32698 && DECL_VIRTUAL_P (member_decl_opt))
32700 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32701 "implicit templates may not be %<virtual%>");
32702 DECL_VIRTUAL_P (member_decl_opt) = false;
32705 if (member_decl_opt)
32706 member_decl_opt = finish_member_template_decl (member_decl_opt);
32707 end_template_decl ();
32709 parser->fully_implicit_function_template_p = false;
32710 --parser->num_template_parameter_lists;
32712 return member_decl_opt;
32715 #include "gt-cp-parser.h"