]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
7d9f81d314f9eb4c75962d04a5177f9de7083962
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
47
48 \f
49 /* The lexer. */
50
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
53
54 static cp_token eof_token =
55 {
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
57 };
58
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;
117
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;
129
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 */
153
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 */
175
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;
185
186 /* Prototypes. */
187
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;
230
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
233
234 static void cp_parser_initial_pragma
235 (cp_token *);
236
237 static tree cp_literal_operator_id
238 (const char *);
239
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243 (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser *, tree, bool);
246
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
250
251 /* Variables. */
252
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream;
255
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand;
259
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
265
266 static void
267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 cp_token *start_token, unsigned num,
269 cp_token *curr_token)
270 {
271 unsigned i, nprinted;
272 cp_token *token;
273 bool do_print;
274
275 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
276
277 if (buffer == NULL)
278 return;
279
280 if (num == 0)
281 num = buffer->length ();
282
283 if (start_token == NULL)
284 start_token = buffer->address ();
285
286 if (start_token > buffer->address ())
287 {
288 cp_lexer_print_token (file, &(*buffer)[0]);
289 fprintf (file, " ... ");
290 }
291
292 do_print = false;
293 nprinted = 0;
294 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
295 {
296 if (token == start_token)
297 do_print = true;
298
299 if (!do_print)
300 continue;
301
302 nprinted++;
303 if (token == curr_token)
304 fprintf (file, "[[");
305
306 cp_lexer_print_token (file, token);
307
308 if (token == curr_token)
309 fprintf (file, "]]");
310
311 switch (token->type)
312 {
313 case CPP_SEMICOLON:
314 case CPP_OPEN_BRACE:
315 case CPP_CLOSE_BRACE:
316 case CPP_EOF:
317 fputc ('\n', file);
318 break;
319
320 default:
321 fputc (' ', file);
322 }
323 }
324
325 if (i == num && i < buffer->length ())
326 {
327 fprintf (file, " ... ");
328 cp_lexer_print_token (file, &buffer->last ());
329 }
330
331 fprintf (file, "\n");
332 }
333
334
335 /* Dump all tokens in BUFFER to stderr. */
336
337 void
338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
339 {
340 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
341 }
342
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> &ref)
345 {
346 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
347 }
348
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> *ptr)
351 {
352 if (ptr)
353 debug (*ptr);
354 else
355 fprintf (stderr, "<nil>\n");
356 }
357
358
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
361
362 static void
363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
364 {
365 if (t)
366 {
367 fprintf (file, "%s: ", desc);
368 print_node_brief (file, "", t, 0);
369 }
370 }
371
372
373 /* Dump parser context C to FILE. */
374
375 static void
376 cp_debug_print_context (FILE *file, cp_parser_context *c)
377 {
378 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380 print_node_brief (file, "", c->object_type, 0);
381 fprintf (file, "}\n");
382 }
383
384
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
386
387 static void
388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
389 {
390 unsigned i;
391 cp_parser_context *c;
392
393 fprintf (file, "Parsing context stack:\n");
394 for (i = 0, c = first; c; c = c->next, i++)
395 {
396 fprintf (file, "\t#%u: ", i);
397 cp_debug_print_context (file, c);
398 }
399 }
400
401
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
403
404 static void
405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
406 {
407 if (flag)
408 fprintf (file, "%s: true\n", desc);
409 }
410
411
412 /* Print an unparsed function entry UF to FILE. */
413
414 static void
415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
416 {
417 unsigned i;
418 cp_default_arg_entry *default_arg_fn;
419 tree fn;
420
421 fprintf (file, "\tFunctions with default args:\n");
422 for (i = 0;
423 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424 i++)
425 {
426 fprintf (file, "\t\tClass type: ");
427 print_node_brief (file, "", default_arg_fn->class_type, 0);
428 fprintf (file, "\t\tDeclaration: ");
429 print_node_brief (file, "", default_arg_fn->decl, 0);
430 fprintf (file, "\n");
431 }
432
433 fprintf (file, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
436 {
437 print_node_brief (file, "", fn, 0);
438 fprintf (file, " ");
439 }
440 fprintf (file, "\n");
441
442 fprintf (file, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
445 {
446 print_node_brief (file, "", fn, 0);
447 fprintf (file, " ");
448 }
449 fprintf (file, "\n");
450 }
451
452
453 /* Print the stack of unparsed member functions S to FILE. */
454
455 static void
456 cp_debug_print_unparsed_queues (FILE *file,
457 vec<cp_unparsed_functions_entry, va_gc> *s)
458 {
459 unsigned i;
460 cp_unparsed_functions_entry *uf;
461
462 fprintf (file, "Unparsed functions\n");
463 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
464 {
465 fprintf (file, "#%u:\n", i);
466 cp_debug_print_unparsed_function (file, uf);
467 }
468 }
469
470
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
473
474 static void
475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
476 {
477 cp_token *next_token, *first_token, *start_token;
478
479 if (file == NULL)
480 file = stderr;
481
482 next_token = parser->lexer->next_token;
483 first_token = parser->lexer->buffer->address ();
484 start_token = (next_token > first_token + window_size / 2)
485 ? next_token - window_size / 2
486 : first_token;
487 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 next_token);
489 }
490
491
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
494
495 void
496 cp_debug_parser (FILE *file, cp_parser *parser)
497 {
498 const size_t window_size = 20;
499 cp_token *token;
500 expanded_location eloc;
501
502 if (file == NULL)
503 file = stderr;
504
505 fprintf (file, "Parser state\n\n");
506 fprintf (file, "Number of tokens: %u\n",
507 vec_safe_length (parser->lexer->buffer));
508 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509 cp_debug_print_tree_if_set (file, "Object scope",
510 parser->object_scope);
511 cp_debug_print_tree_if_set (file, "Qualifying scope",
512 parser->qualifying_scope);
513 cp_debug_print_context_stack (file, parser->context);
514 cp_debug_print_flag (file, "Allow GNU extensions",
515 parser->allow_gnu_extensions_p);
516 cp_debug_print_flag (file, "'>' token is greater-than",
517 parser->greater_than_is_operator_p);
518 cp_debug_print_flag (file, "Default args allowed in current "
519 "parameter list", parser->default_arg_ok_p);
520 cp_debug_print_flag (file, "Parsing integral constant-expression",
521 parser->integral_constant_expression_p);
522 cp_debug_print_flag (file, "Allow non-constant expression in current "
523 "constant-expression",
524 parser->allow_non_integral_constant_expression_p);
525 cp_debug_print_flag (file, "Seen non-constant expression",
526 parser->non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 "current context",
529 parser->local_variables_forbidden_p);
530 cp_debug_print_flag (file, "In unbraced linkage specification",
531 parser->in_unbraced_linkage_specification_p);
532 cp_debug_print_flag (file, "Parsing a declarator",
533 parser->in_declarator_p);
534 cp_debug_print_flag (file, "In template argument list",
535 parser->in_template_argument_list_p);
536 cp_debug_print_flag (file, "Parsing an iteration statement",
537 parser->in_statement & IN_ITERATION_STMT);
538 cp_debug_print_flag (file, "Parsing a switch statement",
539 parser->in_statement & IN_SWITCH_STMT);
540 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 parser->in_statement & IN_OMP_BLOCK);
542 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 parser->in_statement & IN_CILK_SIMD_FOR);
544 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 parser->in_statement & IN_OMP_FOR);
546 cp_debug_print_flag (file, "Parsing an if statement",
547 parser->in_statement & IN_IF_STMT);
548 cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 "context", parser->in_type_id_in_expr_p);
550 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 parser->implicit_extern_c);
552 cp_debug_print_flag (file, "String expressions should be translated "
553 "to execution character set",
554 parser->translate_strings_p);
555 cp_debug_print_flag (file, "Parsing function body outside of a "
556 "local class", parser->in_function_body);
557 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 parser->colon_corrects_to_scope_p);
559 cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 parser->colon_doesnt_start_class_def_p);
561 if (parser->type_definition_forbidden_message)
562 fprintf (file, "Error message for forbidden type definitions: %s\n",
563 parser->type_definition_forbidden_message);
564 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565 fprintf (file, "Number of class definitions in progress: %u\n",
566 parser->num_classes_being_defined);
567 fprintf (file, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser->num_template_parameter_lists);
569 cp_debug_parser_tokens (file, parser, window_size);
570 token = parser->lexer->next_token;
571 fprintf (file, "Next token to parse:\n");
572 fprintf (file, "\tToken: ");
573 cp_lexer_print_token (file, token);
574 eloc = expand_location (token->location);
575 fprintf (file, "\n\tFile: %s\n", eloc.file);
576 fprintf (file, "\tLine: %d\n", eloc.line);
577 fprintf (file, "\tColumn: %d\n", eloc.column);
578 }
579
580 DEBUG_FUNCTION void
581 debug (cp_parser &ref)
582 {
583 cp_debug_parser (stderr, &ref);
584 }
585
586 DEBUG_FUNCTION void
587 debug (cp_parser *ptr)
588 {
589 if (ptr)
590 debug (*ptr);
591 else
592 fprintf (stderr, "<nil>\n");
593 }
594
595 /* Allocate memory for a new lexer object and return it. */
596
597 static cp_lexer *
598 cp_lexer_alloc (void)
599 {
600 cp_lexer *lexer;
601
602 c_common_no_more_pch ();
603
604 /* Allocate the memory. */
605 lexer = ggc_alloc_cleared_cp_lexer ();
606
607 /* Initially we are not debugging. */
608 lexer->debugging_p = false;
609
610 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
611
612 /* Create the buffer. */
613 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
614
615 return lexer;
616 }
617
618
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620 preprocessor. */
621
622 static cp_lexer *
623 cp_lexer_new_main (void)
624 {
625 cp_lexer *lexer;
626 cp_token token;
627
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token);
632
633 lexer = cp_lexer_alloc ();
634
635 /* Put the first token in the buffer. */
636 lexer->buffer->quick_push (token);
637
638 /* Get the remaining tokens from the preprocessor. */
639 while (token.type != CPP_EOF)
640 {
641 cp_lexer_get_preprocessor_token (lexer, &token);
642 vec_safe_push (lexer->buffer, token);
643 }
644
645 lexer->last_token = lexer->buffer->address ()
646 + lexer->buffer->length ()
647 - 1;
648 lexer->next_token = lexer->buffer->length ()
649 ? lexer->buffer->address ()
650 : &eof_token;
651
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
654 done_lexing = true;
655
656 gcc_assert (!lexer->next_token->purged_p);
657 return lexer;
658 }
659
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
662
663 static cp_lexer *
664 cp_lexer_new_from_tokens (cp_token_cache *cache)
665 {
666 cp_token *first = cache->first;
667 cp_token *last = cache->last;
668 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
669
670 /* We do not own the buffer. */
671 lexer->buffer = NULL;
672 lexer->next_token = first == last ? &eof_token : first;
673 lexer->last_token = last;
674
675 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
676
677 /* Initially we are not debugging. */
678 lexer->debugging_p = false;
679
680 gcc_assert (!lexer->next_token->purged_p);
681 return lexer;
682 }
683
684 /* Frees all resources associated with LEXER. */
685
686 static void
687 cp_lexer_destroy (cp_lexer *lexer)
688 {
689 vec_free (lexer->buffer);
690 lexer->saved_tokens.release ();
691 ggc_free (lexer);
692 }
693
694 /* Returns nonzero if debugging information should be output. */
695
696 static inline bool
697 cp_lexer_debugging_p (cp_lexer *lexer)
698 {
699 return lexer->debugging_p;
700 }
701
702
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
705 {
706 gcc_assert (!previous_p || lexer->next_token != &eof_token);
707
708 return lexer->next_token - previous_p;
709 }
710
711 static inline cp_token *
712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
713 {
714 return pos;
715 }
716
717 static inline void
718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
719 {
720 lexer->next_token = cp_lexer_token_at (lexer, pos);
721 }
722
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer *lexer)
725 {
726 if (lexer->next_token == &eof_token)
727 return lexer->last_token - 1;
728 else
729 return cp_lexer_token_position (lexer, true);
730 }
731
732 static inline cp_token *
733 cp_lexer_previous_token (cp_lexer *lexer)
734 {
735 cp_token_position tp = cp_lexer_previous_token_position (lexer);
736
737 return cp_lexer_token_at (lexer, tp);
738 }
739
740 /* nonzero if we are presently saving tokens. */
741
742 static inline int
743 cp_lexer_saving_tokens (const cp_lexer* lexer)
744 {
745 return lexer->saved_tokens.length () != 0;
746 }
747
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
752
753 static void
754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
755 {
756 static int is_extern_c = 0;
757
758 /* Get a new token from the preprocessor. */
759 token->type
760 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762 token->keyword = RID_MAX;
763 token->pragma_kind = PRAGMA_NONE;
764 token->purged_p = false;
765 token->error_reported = false;
766
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c += pending_lang_change;
771 pending_lang_change = 0;
772 token->implicit_extern_c = is_extern_c > 0;
773
774 /* Check to see if this token is a keyword. */
775 if (token->type == CPP_NAME)
776 {
777 if (C_IS_RESERVED_WORD (token->u.value))
778 {
779 /* Mark this token as a keyword. */
780 token->type = CPP_KEYWORD;
781 /* Record which keyword. */
782 token->keyword = C_RID_CODE (token->u.value);
783 }
784 else
785 {
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
789 {
790 /* Warn about the C++0x keyword (but still treat it as
791 an identifier). */
792 warning (OPT_Wc__0x_compat,
793 "identifier %qE is a keyword in C++11",
794 token->u.value);
795
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token->u.value, RID_MAX);
799 }
800
801 token->keyword = RID_MAX;
802 }
803 }
804 else if (token->type == CPP_AT_NAME)
805 {
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token->type = CPP_KEYWORD;
808 switch (C_RID_CODE (token->u.value))
809 {
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
816 RID_AT_SYNCHRONIZED.
817 */
818 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
819 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
820 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
822 case RID_THROW: token->keyword = RID_AT_THROW; break;
823 case RID_TRY: token->keyword = RID_AT_TRY; break;
824 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
826 }
827 }
828 else if (token->type == CPP_PRAGMA)
829 {
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
834 }
835 }
836
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
840 {
841 if (token->type != CPP_EOF)
842 {
843 input_location = token->location;
844 }
845 }
846
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
850 {
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
853 }
854
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
857
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
860 {
861 if (cp_lexer_debugging_p (lexer))
862 {
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
866 }
867 return lexer->next_token;
868 }
869
870 /* Return true if the next token has the indicated TYPE. */
871
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
874 {
875 return cp_lexer_peek_token (lexer)->type == type;
876 }
877
878 /* Return true if the next token does not have the indicated TYPE. */
879
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
882 {
883 return !cp_lexer_next_token_is (lexer, type);
884 }
885
886 /* Return true if the next token is the indicated KEYWORD. */
887
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
890 {
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
892 }
893
894 static inline bool
895 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
896 {
897 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
898 }
899
900 /* Return true if the next token is not the indicated KEYWORD. */
901
902 static inline bool
903 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
904 {
905 return cp_lexer_peek_token (lexer)->keyword != keyword;
906 }
907
908 /* Return true if the next token is a keyword for a decl-specifier. */
909
910 static bool
911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
912 {
913 cp_token *token;
914
915 token = cp_lexer_peek_token (lexer);
916 switch (token->keyword)
917 {
918 /* auto specifier: storage-class-specifier in C++,
919 simple-type-specifier in C++0x. */
920 case RID_AUTO:
921 /* Storage classes. */
922 case RID_REGISTER:
923 case RID_STATIC:
924 case RID_EXTERN:
925 case RID_MUTABLE:
926 case RID_THREAD:
927 /* Elaborated type specifiers. */
928 case RID_ENUM:
929 case RID_CLASS:
930 case RID_STRUCT:
931 case RID_UNION:
932 case RID_TYPENAME:
933 /* Simple type specifiers. */
934 case RID_CHAR:
935 case RID_CHAR16:
936 case RID_CHAR32:
937 case RID_WCHAR:
938 case RID_BOOL:
939 case RID_SHORT:
940 case RID_INT:
941 case RID_LONG:
942 case RID_INT128:
943 case RID_SIGNED:
944 case RID_UNSIGNED:
945 case RID_FLOAT:
946 case RID_DOUBLE:
947 case RID_VOID:
948 /* GNU extensions. */
949 case RID_ATTRIBUTE:
950 case RID_TYPEOF:
951 /* C++0x extensions. */
952 case RID_DECLTYPE:
953 case RID_UNDERLYING_TYPE:
954 return true;
955
956 default:
957 return false;
958 }
959 }
960
961 /* Returns TRUE iff the token T begins a decltype type. */
962
963 static bool
964 token_is_decltype (cp_token *t)
965 {
966 return (t->keyword == RID_DECLTYPE
967 || t->type == CPP_DECLTYPE);
968 }
969
970 /* Returns TRUE iff the next token begins a decltype type. */
971
972 static bool
973 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
974 {
975 cp_token *t = cp_lexer_peek_token (lexer);
976 return token_is_decltype (t);
977 }
978
979 /* Return a pointer to the Nth token in the token stream. If N is 1,
980 then this is precisely equivalent to cp_lexer_peek_token (except
981 that it is not inline). One would like to disallow that case, but
982 there is one case (cp_parser_nth_token_starts_template_id) where
983 the caller passes a variable for N and it might be 1. */
984
985 static cp_token *
986 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
987 {
988 cp_token *token;
989
990 /* N is 1-based, not zero-based. */
991 gcc_assert (n > 0);
992
993 if (cp_lexer_debugging_p (lexer))
994 fprintf (cp_lexer_debug_stream,
995 "cp_lexer: peeking ahead %ld at token: ", (long)n);
996
997 --n;
998 token = lexer->next_token;
999 gcc_assert (!n || token != &eof_token);
1000 while (n != 0)
1001 {
1002 ++token;
1003 if (token == lexer->last_token)
1004 {
1005 token = &eof_token;
1006 break;
1007 }
1008
1009 if (!token->purged_p)
1010 --n;
1011 }
1012
1013 if (cp_lexer_debugging_p (lexer))
1014 {
1015 cp_lexer_print_token (cp_lexer_debug_stream, token);
1016 putc ('\n', cp_lexer_debug_stream);
1017 }
1018
1019 return token;
1020 }
1021
1022 /* Return the next token, and advance the lexer's next_token pointer
1023 to point to the next non-purged token. */
1024
1025 static cp_token *
1026 cp_lexer_consume_token (cp_lexer* lexer)
1027 {
1028 cp_token *token = lexer->next_token;
1029
1030 gcc_assert (token != &eof_token);
1031 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1032
1033 do
1034 {
1035 lexer->next_token++;
1036 if (lexer->next_token == lexer->last_token)
1037 {
1038 lexer->next_token = &eof_token;
1039 break;
1040 }
1041
1042 }
1043 while (lexer->next_token->purged_p);
1044
1045 cp_lexer_set_source_position_from_token (token);
1046
1047 /* Provide debugging output. */
1048 if (cp_lexer_debugging_p (lexer))
1049 {
1050 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1051 cp_lexer_print_token (cp_lexer_debug_stream, token);
1052 putc ('\n', cp_lexer_debug_stream);
1053 }
1054
1055 return token;
1056 }
1057
1058 /* Permanently remove the next token from the token stream, and
1059 advance the next_token pointer to refer to the next non-purged
1060 token. */
1061
1062 static void
1063 cp_lexer_purge_token (cp_lexer *lexer)
1064 {
1065 cp_token *tok = lexer->next_token;
1066
1067 gcc_assert (tok != &eof_token);
1068 tok->purged_p = true;
1069 tok->location = UNKNOWN_LOCATION;
1070 tok->u.value = NULL_TREE;
1071 tok->keyword = RID_MAX;
1072
1073 do
1074 {
1075 tok++;
1076 if (tok == lexer->last_token)
1077 {
1078 tok = &eof_token;
1079 break;
1080 }
1081 }
1082 while (tok->purged_p);
1083 lexer->next_token = tok;
1084 }
1085
1086 /* Permanently remove all tokens after TOK, up to, but not
1087 including, the token that will be returned next by
1088 cp_lexer_peek_token. */
1089
1090 static void
1091 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1092 {
1093 cp_token *peek = lexer->next_token;
1094
1095 if (peek == &eof_token)
1096 peek = lexer->last_token;
1097
1098 gcc_assert (tok < peek);
1099
1100 for ( tok += 1; tok != peek; tok += 1)
1101 {
1102 tok->purged_p = true;
1103 tok->location = UNKNOWN_LOCATION;
1104 tok->u.value = NULL_TREE;
1105 tok->keyword = RID_MAX;
1106 }
1107 }
1108
1109 /* Begin saving tokens. All tokens consumed after this point will be
1110 preserved. */
1111
1112 static void
1113 cp_lexer_save_tokens (cp_lexer* lexer)
1114 {
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1118
1119 lexer->saved_tokens.safe_push (lexer->next_token);
1120 }
1121
1122 /* Commit to the portion of the token stream most recently saved. */
1123
1124 static void
1125 cp_lexer_commit_tokens (cp_lexer* lexer)
1126 {
1127 /* Provide debugging output. */
1128 if (cp_lexer_debugging_p (lexer))
1129 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1130
1131 lexer->saved_tokens.pop ();
1132 }
1133
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135 to the token stream. Stop saving tokens. */
1136
1137 static void
1138 cp_lexer_rollback_tokens (cp_lexer* lexer)
1139 {
1140 /* Provide debugging output. */
1141 if (cp_lexer_debugging_p (lexer))
1142 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1143
1144 lexer->next_token = lexer->saved_tokens.pop ();
1145 }
1146
1147 /* Print a representation of the TOKEN on the STREAM. */
1148
1149 static void
1150 cp_lexer_print_token (FILE * stream, cp_token *token)
1151 {
1152 /* We don't use cpp_type2name here because the parser defines
1153 a few tokens of its own. */
1154 static const char *const token_names[] = {
1155 /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1158 TTYPE_TABLE
1159 #undef OP
1160 #undef TK
1161 /* C++ parser token types - see "Manifest constants", above. */
1162 "KEYWORD",
1163 "TEMPLATE_ID",
1164 "NESTED_NAME_SPECIFIER",
1165 };
1166
1167 /* For some tokens, print the associated data. */
1168 switch (token->type)
1169 {
1170 case CPP_KEYWORD:
1171 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 For example, `struct' is mapped to an INTEGER_CST. */
1173 if (!identifier_p (token->u.value))
1174 break;
1175 /* else fall through */
1176 case CPP_NAME:
1177 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1178 break;
1179
1180 case CPP_STRING:
1181 case CPP_STRING16:
1182 case CPP_STRING32:
1183 case CPP_WSTRING:
1184 case CPP_UTF8STRING:
1185 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1186 break;
1187
1188 case CPP_NUMBER:
1189 print_generic_expr (stream, token->u.value, 0);
1190 break;
1191
1192 default:
1193 /* If we have a name for the token, print it out. Otherwise, we
1194 simply give the numeric code. */
1195 if (token->type < ARRAY_SIZE(token_names))
1196 fputs (token_names[token->type], stream);
1197 else
1198 fprintf (stream, "[%d]", token->type);
1199 break;
1200 }
1201 }
1202
1203 DEBUG_FUNCTION void
1204 debug (cp_token &ref)
1205 {
1206 cp_lexer_print_token (stderr, &ref);
1207 fprintf (stderr, "\n");
1208 }
1209
1210 DEBUG_FUNCTION void
1211 debug (cp_token *ptr)
1212 {
1213 if (ptr)
1214 debug (*ptr);
1215 else
1216 fprintf (stderr, "<nil>\n");
1217 }
1218
1219
1220 /* Start emitting debugging information. */
1221
1222 static void
1223 cp_lexer_start_debugging (cp_lexer* lexer)
1224 {
1225 lexer->debugging_p = true;
1226 cp_lexer_debug_stream = stderr;
1227 }
1228
1229 /* Stop emitting debugging information. */
1230
1231 static void
1232 cp_lexer_stop_debugging (cp_lexer* lexer)
1233 {
1234 lexer->debugging_p = false;
1235 cp_lexer_debug_stream = NULL;
1236 }
1237
1238 /* Create a new cp_token_cache, representing a range of tokens. */
1239
1240 static cp_token_cache *
1241 cp_token_cache_new (cp_token *first, cp_token *last)
1242 {
1243 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1244 cache->first = first;
1245 cache->last = last;
1246 return cache;
1247 }
1248
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250 by function declaration or definition. */
1251
1252 static inline void
1253 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1254 {
1255 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1256 {
1257 error ("%<#pragma omp declare simd%> not immediately followed by "
1258 "function declaration or definition");
1259 parser->omp_declare_simd = NULL;
1260 }
1261 }
1262
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264 and put that into "omp declare simd" attribute. */
1265
1266 static inline void
1267 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1268 {
1269 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1270 {
1271 if (fndecl == error_mark_node)
1272 {
1273 parser->omp_declare_simd = NULL;
1274 return;
1275 }
1276 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1277 {
1278 cp_ensure_no_omp_declare_simd (parser);
1279 return;
1280 }
1281 }
1282 }
1283 \f
1284 /* Decl-specifiers. */
1285
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1287
1288 static void
1289 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1290 {
1291 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1292 }
1293
1294 /* Declarators. */
1295
1296 /* Nothing other than the parser should be creating declarators;
1297 declarators are a semi-syntactic representation of C++ entities.
1298 Other parts of the front end that need to create entities (like
1299 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1300
1301 static cp_declarator *make_call_declarator
1302 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1303 static cp_declarator *make_array_declarator
1304 (cp_declarator *, tree);
1305 static cp_declarator *make_pointer_declarator
1306 (cp_cv_quals, cp_declarator *, tree);
1307 static cp_declarator *make_reference_declarator
1308 (cp_cv_quals, cp_declarator *, bool, tree);
1309 static cp_parameter_declarator *make_parameter_declarator
1310 (cp_decl_specifier_seq *, cp_declarator *, tree);
1311 static cp_declarator *make_ptrmem_declarator
1312 (cp_cv_quals, tree, cp_declarator *, tree);
1313
1314 /* An erroneous declarator. */
1315 static cp_declarator *cp_error_declarator;
1316
1317 /* The obstack on which declarators and related data structures are
1318 allocated. */
1319 static struct obstack declarator_obstack;
1320
1321 /* Alloc BYTES from the declarator memory pool. */
1322
1323 static inline void *
1324 alloc_declarator (size_t bytes)
1325 {
1326 return obstack_alloc (&declarator_obstack, bytes);
1327 }
1328
1329 /* Allocate a declarator of the indicated KIND. Clear fields that are
1330 common to all declarators. */
1331
1332 static cp_declarator *
1333 make_declarator (cp_declarator_kind kind)
1334 {
1335 cp_declarator *declarator;
1336
1337 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1338 declarator->kind = kind;
1339 declarator->attributes = NULL_TREE;
1340 declarator->std_attributes = NULL_TREE;
1341 declarator->declarator = NULL;
1342 declarator->parameter_pack_p = false;
1343 declarator->id_loc = UNKNOWN_LOCATION;
1344
1345 return declarator;
1346 }
1347
1348 /* Make a declarator for a generalized identifier. If
1349 QUALIFYING_SCOPE is non-NULL, the identifier is
1350 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1352 is, if any. */
1353
1354 static cp_declarator *
1355 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1356 special_function_kind sfk)
1357 {
1358 cp_declarator *declarator;
1359
1360 /* It is valid to write:
1361
1362 class C { void f(); };
1363 typedef C D;
1364 void D::f();
1365
1366 The standard is not clear about whether `typedef const C D' is
1367 legal; as of 2002-09-15 the committee is considering that
1368 question. EDG 3.0 allows that syntax. Therefore, we do as
1369 well. */
1370 if (qualifying_scope && TYPE_P (qualifying_scope))
1371 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1372
1373 gcc_assert (identifier_p (unqualified_name)
1374 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1375 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1376
1377 declarator = make_declarator (cdk_id);
1378 declarator->u.id.qualifying_scope = qualifying_scope;
1379 declarator->u.id.unqualified_name = unqualified_name;
1380 declarator->u.id.sfk = sfk;
1381
1382 return declarator;
1383 }
1384
1385 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1386 of modifiers such as const or volatile to apply to the pointer
1387 type, represented as identifiers. ATTRIBUTES represent the attributes that
1388 appertain to the pointer or reference. */
1389
1390 cp_declarator *
1391 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1392 tree attributes)
1393 {
1394 cp_declarator *declarator;
1395
1396 declarator = make_declarator (cdk_pointer);
1397 declarator->declarator = target;
1398 declarator->u.pointer.qualifiers = cv_qualifiers;
1399 declarator->u.pointer.class_type = NULL_TREE;
1400 if (target)
1401 {
1402 declarator->id_loc = target->id_loc;
1403 declarator->parameter_pack_p = target->parameter_pack_p;
1404 target->parameter_pack_p = false;
1405 }
1406 else
1407 declarator->parameter_pack_p = false;
1408
1409 declarator->std_attributes = attributes;
1410
1411 return declarator;
1412 }
1413
1414 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1415 represent the attributes that appertain to the pointer or
1416 reference. */
1417
1418 cp_declarator *
1419 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1420 bool rvalue_ref, tree attributes)
1421 {
1422 cp_declarator *declarator;
1423
1424 declarator = make_declarator (cdk_reference);
1425 declarator->declarator = target;
1426 declarator->u.reference.qualifiers = cv_qualifiers;
1427 declarator->u.reference.rvalue_ref = rvalue_ref;
1428 if (target)
1429 {
1430 declarator->id_loc = target->id_loc;
1431 declarator->parameter_pack_p = target->parameter_pack_p;
1432 target->parameter_pack_p = false;
1433 }
1434 else
1435 declarator->parameter_pack_p = false;
1436
1437 declarator->std_attributes = attributes;
1438
1439 return declarator;
1440 }
1441
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1445
1446 cp_declarator *
1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1448 cp_declarator *pointee,
1449 tree attributes)
1450 {
1451 cp_declarator *declarator;
1452
1453 declarator = make_declarator (cdk_ptrmem);
1454 declarator->declarator = pointee;
1455 declarator->u.pointer.qualifiers = cv_qualifiers;
1456 declarator->u.pointer.class_type = class_type;
1457
1458 if (pointee)
1459 {
1460 declarator->parameter_pack_p = pointee->parameter_pack_p;
1461 pointee->parameter_pack_p = false;
1462 }
1463 else
1464 declarator->parameter_pack_p = false;
1465
1466 declarator->std_attributes = attributes;
1467
1468 return declarator;
1469 }
1470
1471 /* Make a declarator for the function given by TARGET, with the
1472 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1473 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1474 indicates what exceptions can be thrown. */
1475
1476 cp_declarator *
1477 make_call_declarator (cp_declarator *target,
1478 tree parms,
1479 cp_cv_quals cv_qualifiers,
1480 cp_virt_specifiers virt_specifiers,
1481 cp_ref_qualifier ref_qualifier,
1482 tree exception_specification,
1483 tree late_return_type)
1484 {
1485 cp_declarator *declarator;
1486
1487 declarator = make_declarator (cdk_function);
1488 declarator->declarator = target;
1489 declarator->u.function.parameters = parms;
1490 declarator->u.function.qualifiers = cv_qualifiers;
1491 declarator->u.function.virt_specifiers = virt_specifiers;
1492 declarator->u.function.ref_qualifier = ref_qualifier;
1493 declarator->u.function.exception_specification = exception_specification;
1494 declarator->u.function.late_return_type = late_return_type;
1495 if (target)
1496 {
1497 declarator->id_loc = target->id_loc;
1498 declarator->parameter_pack_p = target->parameter_pack_p;
1499 target->parameter_pack_p = false;
1500 }
1501 else
1502 declarator->parameter_pack_p = false;
1503
1504 return declarator;
1505 }
1506
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508 defined by ELEMENT. */
1509
1510 cp_declarator *
1511 make_array_declarator (cp_declarator *element, tree bounds)
1512 {
1513 cp_declarator *declarator;
1514
1515 declarator = make_declarator (cdk_array);
1516 declarator->declarator = element;
1517 declarator->u.array.bounds = bounds;
1518 if (element)
1519 {
1520 declarator->id_loc = element->id_loc;
1521 declarator->parameter_pack_p = element->parameter_pack_p;
1522 element->parameter_pack_p = false;
1523 }
1524 else
1525 declarator->parameter_pack_p = false;
1526
1527 return declarator;
1528 }
1529
1530 /* Determine whether the declarator we've seen so far can be a
1531 parameter pack, when followed by an ellipsis. */
1532 static bool
1533 declarator_can_be_parameter_pack (cp_declarator *declarator)
1534 {
1535 /* Search for a declarator name, or any other declarator that goes
1536 after the point where the ellipsis could appear in a parameter
1537 pack. If we find any of these, then this declarator can not be
1538 made into a parameter pack. */
1539 bool found = false;
1540 while (declarator && !found)
1541 {
1542 switch ((int)declarator->kind)
1543 {
1544 case cdk_id:
1545 case cdk_array:
1546 found = true;
1547 break;
1548
1549 case cdk_error:
1550 return true;
1551
1552 default:
1553 declarator = declarator->declarator;
1554 break;
1555 }
1556 }
1557
1558 return !found;
1559 }
1560
1561 cp_parameter_declarator *no_parameters;
1562
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564 DECLARATOR and DEFAULT_ARGUMENT. */
1565
1566 cp_parameter_declarator *
1567 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1568 cp_declarator *declarator,
1569 tree default_argument)
1570 {
1571 cp_parameter_declarator *parameter;
1572
1573 parameter = ((cp_parameter_declarator *)
1574 alloc_declarator (sizeof (cp_parameter_declarator)));
1575 parameter->next = NULL;
1576 if (decl_specifiers)
1577 parameter->decl_specifiers = *decl_specifiers;
1578 else
1579 clear_decl_specs (&parameter->decl_specifiers);
1580 parameter->declarator = declarator;
1581 parameter->default_argument = default_argument;
1582 parameter->ellipsis_p = false;
1583
1584 return parameter;
1585 }
1586
1587 /* Returns true iff DECLARATOR is a declaration for a function. */
1588
1589 static bool
1590 function_declarator_p (const cp_declarator *declarator)
1591 {
1592 while (declarator)
1593 {
1594 if (declarator->kind == cdk_function
1595 && declarator->declarator->kind == cdk_id)
1596 return true;
1597 if (declarator->kind == cdk_id
1598 || declarator->kind == cdk_error)
1599 return false;
1600 declarator = declarator->declarator;
1601 }
1602 return false;
1603 }
1604
1605 /* The parser. */
1606
1607 /* Overview
1608 --------
1609
1610 A cp_parser parses the token stream as specified by the C++
1611 grammar. Its job is purely parsing, not semantic analysis. For
1612 example, the parser breaks the token stream into declarators,
1613 expressions, statements, and other similar syntactic constructs.
1614 It does not check that the types of the expressions on either side
1615 of an assignment-statement are compatible, or that a function is
1616 not declared with a parameter of type `void'.
1617
1618 The parser invokes routines elsewhere in the compiler to perform
1619 semantic analysis and to build up the abstract syntax tree for the
1620 code processed.
1621
1622 The parser (and the template instantiation code, which is, in a
1623 way, a close relative of parsing) are the only parts of the
1624 compiler that should be calling push_scope and pop_scope, or
1625 related functions. The parser (and template instantiation code)
1626 keeps track of what scope is presently active; everything else
1627 should simply honor that. (The code that generates static
1628 initializers may also need to set the scope, in order to check
1629 access control correctly when emitting the initializers.)
1630
1631 Methodology
1632 -----------
1633
1634 The parser is of the standard recursive-descent variety. Upcoming
1635 tokens in the token stream are examined in order to determine which
1636 production to use when parsing a non-terminal. Some C++ constructs
1637 require arbitrary look ahead to disambiguate. For example, it is
1638 impossible, in the general case, to tell whether a statement is an
1639 expression or declaration without scanning the entire statement.
1640 Therefore, the parser is capable of "parsing tentatively." When the
1641 parser is not sure what construct comes next, it enters this mode.
1642 Then, while we attempt to parse the construct, the parser queues up
1643 error messages, rather than issuing them immediately, and saves the
1644 tokens it consumes. If the construct is parsed successfully, the
1645 parser "commits", i.e., it issues any queued error messages and
1646 the tokens that were being preserved are permanently discarded.
1647 If, however, the construct is not parsed successfully, the parser
1648 rolls back its state completely so that it can resume parsing using
1649 a different alternative.
1650
1651 Future Improvements
1652 -------------------
1653
1654 The performance of the parser could probably be improved substantially.
1655 We could often eliminate the need to parse tentatively by looking ahead
1656 a little bit. In some places, this approach might not entirely eliminate
1657 the need to parse tentatively, but it might still speed up the average
1658 case. */
1659
1660 /* Flags that are passed to some parsing functions. These values can
1661 be bitwise-ored together. */
1662
1663 enum
1664 {
1665 /* No flags. */
1666 CP_PARSER_FLAGS_NONE = 0x0,
1667 /* The construct is optional. If it is not present, then no error
1668 should be issued. */
1669 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1670 /* When parsing a type-specifier, treat user-defined type-names
1671 as non-type identifiers. */
1672 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1673 /* When parsing a type-specifier, do not try to parse a class-specifier
1674 or enum-specifier. */
1675 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1676 /* When parsing a decl-specifier-seq, only allow type-specifier or
1677 constexpr. */
1678 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1679 };
1680
1681 /* This type is used for parameters and variables which hold
1682 combinations of the above flags. */
1683 typedef int cp_parser_flags;
1684
1685 /* The different kinds of declarators we want to parse. */
1686
1687 typedef enum cp_parser_declarator_kind
1688 {
1689 /* We want an abstract declarator. */
1690 CP_PARSER_DECLARATOR_ABSTRACT,
1691 /* We want a named declarator. */
1692 CP_PARSER_DECLARATOR_NAMED,
1693 /* We don't mind, but the name must be an unqualified-id. */
1694 CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind;
1696
1697 /* The precedence values used to parse binary expressions. The minimum value
1698 of PREC must be 1, because zero is reserved to quickly discriminate
1699 binary operators from other tokens. */
1700
1701 enum cp_parser_prec
1702 {
1703 PREC_NOT_OPERATOR,
1704 PREC_LOGICAL_OR_EXPRESSION,
1705 PREC_LOGICAL_AND_EXPRESSION,
1706 PREC_INCLUSIVE_OR_EXPRESSION,
1707 PREC_EXCLUSIVE_OR_EXPRESSION,
1708 PREC_AND_EXPRESSION,
1709 PREC_EQUALITY_EXPRESSION,
1710 PREC_RELATIONAL_EXPRESSION,
1711 PREC_SHIFT_EXPRESSION,
1712 PREC_ADDITIVE_EXPRESSION,
1713 PREC_MULTIPLICATIVE_EXPRESSION,
1714 PREC_PM_EXPRESSION,
1715 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1716 };
1717
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719 precedence value. */
1720
1721 typedef struct cp_parser_binary_operations_map_node
1722 {
1723 /* The token type. */
1724 enum cpp_ttype token_type;
1725 /* The corresponding tree code. */
1726 enum tree_code tree_type;
1727 /* The precedence of this operator. */
1728 enum cp_parser_prec prec;
1729 } cp_parser_binary_operations_map_node;
1730
1731 typedef struct cp_parser_expression_stack_entry
1732 {
1733 /* Left hand side of the binary operation we are currently
1734 parsing. */
1735 tree lhs;
1736 /* Original tree code for left hand side, if it was a binary
1737 expression itself (used for -Wparentheses). */
1738 enum tree_code lhs_type;
1739 /* Tree code for the binary operation we are parsing. */
1740 enum tree_code tree_type;
1741 /* Precedence of the binary operation we are parsing. */
1742 enum cp_parser_prec prec;
1743 /* Location of the binary operation we are parsing. */
1744 location_t loc;
1745 } cp_parser_expression_stack_entry;
1746
1747 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1748 entries because precedence levels on the stack are monotonically
1749 increasing. */
1750 typedef struct cp_parser_expression_stack_entry
1751 cp_parser_expression_stack[NUM_PREC_VALUES];
1752
1753 /* Prototypes. */
1754
1755 /* Constructors and destructors. */
1756
1757 static cp_parser_context *cp_parser_context_new
1758 (cp_parser_context *);
1759
1760 /* Class variables. */
1761
1762 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1763
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765 Transformed into an associative array (binops_by_token) by
1766 cp_parser_new. */
1767
1768 static const cp_parser_binary_operations_map_node binops[] = {
1769 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1770 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1771
1772 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1773 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1774 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1775
1776 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1777 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1778
1779 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1780 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1781
1782 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1783 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1784 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1785 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1786
1787 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1788 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1789
1790 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1791
1792 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1793
1794 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1795
1796 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1797
1798 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1799 };
1800
1801 /* The same as binops, but initialized by cp_parser_new so that
1802 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1803 for speed. */
1804 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1805
1806 /* Constructors and destructors. */
1807
1808 /* Construct a new context. The context below this one on the stack
1809 is given by NEXT. */
1810
1811 static cp_parser_context *
1812 cp_parser_context_new (cp_parser_context* next)
1813 {
1814 cp_parser_context *context;
1815
1816 /* Allocate the storage. */
1817 if (cp_parser_context_free_list != NULL)
1818 {
1819 /* Pull the first entry from the free list. */
1820 context = cp_parser_context_free_list;
1821 cp_parser_context_free_list = context->next;
1822 memset (context, 0, sizeof (*context));
1823 }
1824 else
1825 context = ggc_alloc_cleared_cp_parser_context ();
1826
1827 /* No errors have occurred yet in this context. */
1828 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1829 /* If this is not the bottommost context, copy information that we
1830 need from the previous context. */
1831 if (next)
1832 {
1833 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 expression, then we are parsing one in this context, too. */
1835 context->object_type = next->object_type;
1836 /* Thread the stack. */
1837 context->next = next;
1838 }
1839
1840 return context;
1841 }
1842
1843 /* Managing the unparsed function queues. */
1844
1845 #define unparsed_funs_with_default_args \
1846 parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848 parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850 parser->unparsed_queues->last ().nsdmis
1851 #define unparsed_classes \
1852 parser->unparsed_queues->last ().classes
1853
1854 static void
1855 push_unparsed_function_queues (cp_parser *parser)
1856 {
1857 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1858 vec_safe_push (parser->unparsed_queues, e);
1859 }
1860
1861 static void
1862 pop_unparsed_function_queues (cp_parser *parser)
1863 {
1864 release_tree_vector (unparsed_funs_with_definitions);
1865 parser->unparsed_queues->pop ();
1866 }
1867
1868 /* Prototypes. */
1869
1870 /* Constructors and destructors. */
1871
1872 static cp_parser *cp_parser_new
1873 (void);
1874
1875 /* Routines to parse various constructs.
1876
1877 Those that return `tree' will return the error_mark_node (rather
1878 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1879 Sometimes, they will return an ordinary node if error-recovery was
1880 attempted, even though a parse error occurred. So, to check
1881 whether or not a parse error occurred, you should always use
1882 cp_parser_error_occurred. If the construct is optional (indicated
1883 either by an `_opt' in the name of the function that does the
1884 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1885 the construct is not present. */
1886
1887 /* Lexical conventions [gram.lex] */
1888
1889 static tree cp_parser_identifier
1890 (cp_parser *);
1891 static tree cp_parser_string_literal
1892 (cp_parser *, bool, bool);
1893 static tree cp_parser_userdef_char_literal
1894 (cp_parser *);
1895 static tree cp_parser_userdef_string_literal
1896 (cp_token *);
1897 static tree cp_parser_userdef_numeric_literal
1898 (cp_parser *);
1899
1900 /* Basic concepts [gram.basic] */
1901
1902 static bool cp_parser_translation_unit
1903 (cp_parser *);
1904
1905 /* Expressions [gram.expr] */
1906
1907 static tree cp_parser_primary_expression
1908 (cp_parser *, bool, bool, bool, cp_id_kind *);
1909 static tree cp_parser_id_expression
1910 (cp_parser *, bool, bool, bool *, bool, bool);
1911 static tree cp_parser_unqualified_id
1912 (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier_opt
1914 (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_nested_name_specifier
1916 (cp_parser *, bool, bool, bool, bool);
1917 static tree cp_parser_qualifying_entity
1918 (cp_parser *, bool, bool, bool, bool, bool);
1919 static tree cp_parser_postfix_expression
1920 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1921 static tree cp_parser_postfix_open_square_expression
1922 (cp_parser *, tree, bool, bool);
1923 static tree cp_parser_postfix_dot_deref_expression
1924 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1925 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1926 (cp_parser *, int, bool, bool, bool *);
1927 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1928 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1929 static void cp_parser_pseudo_destructor_name
1930 (cp_parser *, tree, tree *, tree *);
1931 static tree cp_parser_unary_expression
1932 (cp_parser *, bool, bool, cp_id_kind *);
1933 static enum tree_code cp_parser_unary_operator
1934 (cp_token *);
1935 static tree cp_parser_new_expression
1936 (cp_parser *);
1937 static vec<tree, va_gc> *cp_parser_new_placement
1938 (cp_parser *);
1939 static tree cp_parser_new_type_id
1940 (cp_parser *, tree *);
1941 static cp_declarator *cp_parser_new_declarator_opt
1942 (cp_parser *);
1943 static cp_declarator *cp_parser_direct_new_declarator
1944 (cp_parser *);
1945 static vec<tree, va_gc> *cp_parser_new_initializer
1946 (cp_parser *);
1947 static tree cp_parser_delete_expression
1948 (cp_parser *);
1949 static tree cp_parser_cast_expression
1950 (cp_parser *, bool, bool, bool, cp_id_kind *);
1951 static tree cp_parser_binary_expression
1952 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1953 static tree cp_parser_question_colon_clause
1954 (cp_parser *, tree);
1955 static tree cp_parser_assignment_expression
1956 (cp_parser *, bool, cp_id_kind *);
1957 static enum tree_code cp_parser_assignment_operator_opt
1958 (cp_parser *);
1959 static tree cp_parser_expression
1960 (cp_parser *, bool, cp_id_kind *);
1961 static tree cp_parser_expression
1962 (cp_parser *, bool, bool, cp_id_kind *);
1963 static tree cp_parser_constant_expression
1964 (cp_parser *, bool, bool *);
1965 static tree cp_parser_builtin_offsetof
1966 (cp_parser *);
1967 static tree cp_parser_lambda_expression
1968 (cp_parser *);
1969 static void cp_parser_lambda_introducer
1970 (cp_parser *, tree);
1971 static bool cp_parser_lambda_declarator_opt
1972 (cp_parser *, tree);
1973 static void cp_parser_lambda_body
1974 (cp_parser *, tree);
1975
1976 /* Statements [gram.stmt.stmt] */
1977
1978 static void cp_parser_statement
1979 (cp_parser *, tree, bool, bool *);
1980 static void cp_parser_label_for_labeled_statement
1981 (cp_parser *, tree);
1982 static tree cp_parser_expression_statement
1983 (cp_parser *, tree);
1984 static tree cp_parser_compound_statement
1985 (cp_parser *, tree, bool, bool);
1986 static void cp_parser_statement_seq_opt
1987 (cp_parser *, tree);
1988 static tree cp_parser_selection_statement
1989 (cp_parser *, bool *);
1990 static tree cp_parser_condition
1991 (cp_parser *);
1992 static tree cp_parser_iteration_statement
1993 (cp_parser *, bool);
1994 static bool cp_parser_for_init_statement
1995 (cp_parser *, tree *decl);
1996 static tree cp_parser_for
1997 (cp_parser *, bool);
1998 static tree cp_parser_c_for
1999 (cp_parser *, tree, tree, bool);
2000 static tree cp_parser_range_for
2001 (cp_parser *, tree, tree, tree, bool);
2002 static void do_range_for_auto_deduction
2003 (tree, tree);
2004 static tree cp_parser_perform_range_for_lookup
2005 (tree, tree *, tree *);
2006 static tree cp_parser_range_for_member_function
2007 (tree, tree);
2008 static tree cp_parser_jump_statement
2009 (cp_parser *);
2010 static void cp_parser_declaration_statement
2011 (cp_parser *);
2012
2013 static tree cp_parser_implicitly_scoped_statement
2014 (cp_parser *, bool *);
2015 static void cp_parser_already_scoped_statement
2016 (cp_parser *);
2017
2018 /* Declarations [gram.dcl.dcl] */
2019
2020 static void cp_parser_declaration_seq_opt
2021 (cp_parser *);
2022 static void cp_parser_declaration
2023 (cp_parser *);
2024 static void cp_parser_block_declaration
2025 (cp_parser *, bool);
2026 static void cp_parser_simple_declaration
2027 (cp_parser *, bool, tree *);
2028 static void cp_parser_decl_specifier_seq
2029 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2030 static tree cp_parser_storage_class_specifier_opt
2031 (cp_parser *);
2032 static tree cp_parser_function_specifier_opt
2033 (cp_parser *, cp_decl_specifier_seq *);
2034 static tree cp_parser_type_specifier
2035 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2036 int *, bool *);
2037 static tree cp_parser_simple_type_specifier
2038 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2039 static tree cp_parser_type_name
2040 (cp_parser *);
2041 static tree cp_parser_nonclass_name
2042 (cp_parser* parser);
2043 static tree cp_parser_elaborated_type_specifier
2044 (cp_parser *, bool, bool);
2045 static tree cp_parser_enum_specifier
2046 (cp_parser *);
2047 static void cp_parser_enumerator_list
2048 (cp_parser *, tree);
2049 static void cp_parser_enumerator_definition
2050 (cp_parser *, tree);
2051 static tree cp_parser_namespace_name
2052 (cp_parser *);
2053 static void cp_parser_namespace_definition
2054 (cp_parser *);
2055 static void cp_parser_namespace_body
2056 (cp_parser *);
2057 static tree cp_parser_qualified_namespace_specifier
2058 (cp_parser *);
2059 static void cp_parser_namespace_alias_definition
2060 (cp_parser *);
2061 static bool cp_parser_using_declaration
2062 (cp_parser *, bool);
2063 static void cp_parser_using_directive
2064 (cp_parser *);
2065 static tree cp_parser_alias_declaration
2066 (cp_parser *);
2067 static void cp_parser_asm_definition
2068 (cp_parser *);
2069 static void cp_parser_linkage_specification
2070 (cp_parser *);
2071 static void cp_parser_static_assert
2072 (cp_parser *, bool);
2073 static tree cp_parser_decltype
2074 (cp_parser *);
2075
2076 /* Declarators [gram.dcl.decl] */
2077
2078 static tree cp_parser_init_declarator
2079 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2080 static cp_declarator *cp_parser_declarator
2081 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2082 static cp_declarator *cp_parser_direct_declarator
2083 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2084 static enum tree_code cp_parser_ptr_operator
2085 (cp_parser *, tree *, cp_cv_quals *, tree *);
2086 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2087 (cp_parser *);
2088 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2089 (cp_parser *);
2090 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2091 (cp_parser *);
2092 static tree cp_parser_late_return_type_opt
2093 (cp_parser *, cp_declarator *, cp_cv_quals);
2094 static tree cp_parser_declarator_id
2095 (cp_parser *, bool);
2096 static tree cp_parser_type_id
2097 (cp_parser *);
2098 static tree cp_parser_template_type_arg
2099 (cp_parser *);
2100 static tree cp_parser_trailing_type_id (cp_parser *);
2101 static tree cp_parser_type_id_1
2102 (cp_parser *, bool, bool);
2103 static void cp_parser_type_specifier_seq
2104 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2105 static tree cp_parser_parameter_declaration_clause
2106 (cp_parser *);
2107 static tree cp_parser_parameter_declaration_list
2108 (cp_parser *, bool *);
2109 static cp_parameter_declarator *cp_parser_parameter_declaration
2110 (cp_parser *, bool, bool *);
2111 static tree cp_parser_default_argument
2112 (cp_parser *, bool);
2113 static void cp_parser_function_body
2114 (cp_parser *, bool);
2115 static tree cp_parser_initializer
2116 (cp_parser *, bool *, bool *);
2117 static tree cp_parser_initializer_clause
2118 (cp_parser *, bool *);
2119 static tree cp_parser_braced_list
2120 (cp_parser*, bool*);
2121 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2122 (cp_parser *, bool *);
2123
2124 static bool cp_parser_ctor_initializer_opt_and_function_body
2125 (cp_parser *, bool);
2126
2127 static tree cp_parser_late_parsing_omp_declare_simd
2128 (cp_parser *, tree);
2129
2130 static tree cp_parser_late_parsing_cilk_simd_fn_info
2131 (cp_parser *, tree);
2132
2133 static tree synthesize_implicit_template_parm
2134 (cp_parser *);
2135 static tree finish_fully_implicit_template
2136 (cp_parser *, tree);
2137
2138 /* Classes [gram.class] */
2139
2140 static tree cp_parser_class_name
2141 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2142 static tree cp_parser_class_specifier
2143 (cp_parser *);
2144 static tree cp_parser_class_head
2145 (cp_parser *, bool *);
2146 static enum tag_types cp_parser_class_key
2147 (cp_parser *);
2148 static void cp_parser_member_specification_opt
2149 (cp_parser *);
2150 static void cp_parser_member_declaration
2151 (cp_parser *);
2152 static tree cp_parser_pure_specifier
2153 (cp_parser *);
2154 static tree cp_parser_constant_initializer
2155 (cp_parser *);
2156
2157 /* Derived classes [gram.class.derived] */
2158
2159 static tree cp_parser_base_clause
2160 (cp_parser *);
2161 static tree cp_parser_base_specifier
2162 (cp_parser *);
2163
2164 /* Special member functions [gram.special] */
2165
2166 static tree cp_parser_conversion_function_id
2167 (cp_parser *);
2168 static tree cp_parser_conversion_type_id
2169 (cp_parser *);
2170 static cp_declarator *cp_parser_conversion_declarator_opt
2171 (cp_parser *);
2172 static bool cp_parser_ctor_initializer_opt
2173 (cp_parser *);
2174 static void cp_parser_mem_initializer_list
2175 (cp_parser *);
2176 static tree cp_parser_mem_initializer
2177 (cp_parser *);
2178 static tree cp_parser_mem_initializer_id
2179 (cp_parser *);
2180
2181 /* Overloading [gram.over] */
2182
2183 static tree cp_parser_operator_function_id
2184 (cp_parser *);
2185 static tree cp_parser_operator
2186 (cp_parser *);
2187
2188 /* Templates [gram.temp] */
2189
2190 static void cp_parser_template_declaration
2191 (cp_parser *, bool);
2192 static tree cp_parser_template_parameter_list
2193 (cp_parser *);
2194 static tree cp_parser_template_parameter
2195 (cp_parser *, bool *, bool *);
2196 static tree cp_parser_type_parameter
2197 (cp_parser *, bool *);
2198 static tree cp_parser_template_id
2199 (cp_parser *, bool, bool, enum tag_types, bool);
2200 static tree cp_parser_template_name
2201 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2202 static tree cp_parser_template_argument_list
2203 (cp_parser *);
2204 static tree cp_parser_template_argument
2205 (cp_parser *);
2206 static void cp_parser_explicit_instantiation
2207 (cp_parser *);
2208 static void cp_parser_explicit_specialization
2209 (cp_parser *);
2210
2211 /* Exception handling [gram.exception] */
2212
2213 static tree cp_parser_try_block
2214 (cp_parser *);
2215 static bool cp_parser_function_try_block
2216 (cp_parser *);
2217 static void cp_parser_handler_seq
2218 (cp_parser *);
2219 static void cp_parser_handler
2220 (cp_parser *);
2221 static tree cp_parser_exception_declaration
2222 (cp_parser *);
2223 static tree cp_parser_throw_expression
2224 (cp_parser *);
2225 static tree cp_parser_exception_specification_opt
2226 (cp_parser *);
2227 static tree cp_parser_type_id_list
2228 (cp_parser *);
2229
2230 /* GNU Extensions */
2231
2232 static tree cp_parser_asm_specification_opt
2233 (cp_parser *);
2234 static tree cp_parser_asm_operand_list
2235 (cp_parser *);
2236 static tree cp_parser_asm_clobber_list
2237 (cp_parser *);
2238 static tree cp_parser_asm_label_list
2239 (cp_parser *);
2240 static bool cp_next_tokens_can_be_attribute_p
2241 (cp_parser *);
2242 static bool cp_next_tokens_can_be_gnu_attribute_p
2243 (cp_parser *);
2244 static bool cp_next_tokens_can_be_std_attribute_p
2245 (cp_parser *);
2246 static bool cp_nth_tokens_can_be_std_attribute_p
2247 (cp_parser *, size_t);
2248 static bool cp_nth_tokens_can_be_gnu_attribute_p
2249 (cp_parser *, size_t);
2250 static bool cp_nth_tokens_can_be_attribute_p
2251 (cp_parser *, size_t);
2252 static tree cp_parser_attributes_opt
2253 (cp_parser *);
2254 static tree cp_parser_gnu_attributes_opt
2255 (cp_parser *);
2256 static tree cp_parser_gnu_attribute_list
2257 (cp_parser *);
2258 static tree cp_parser_std_attribute
2259 (cp_parser *);
2260 static tree cp_parser_std_attribute_spec
2261 (cp_parser *);
2262 static tree cp_parser_std_attribute_spec_seq
2263 (cp_parser *);
2264 static bool cp_parser_extension_opt
2265 (cp_parser *, int *);
2266 static void cp_parser_label_declaration
2267 (cp_parser *);
2268
2269 /* Transactional Memory Extensions */
2270
2271 static tree cp_parser_transaction
2272 (cp_parser *, enum rid);
2273 static tree cp_parser_transaction_expression
2274 (cp_parser *, enum rid);
2275 static bool cp_parser_function_transaction
2276 (cp_parser *, enum rid);
2277 static tree cp_parser_transaction_cancel
2278 (cp_parser *);
2279
2280 enum pragma_context {
2281 pragma_external,
2282 pragma_member,
2283 pragma_objc_icode,
2284 pragma_stmt,
2285 pragma_compound
2286 };
2287 static bool cp_parser_pragma
2288 (cp_parser *, enum pragma_context);
2289
2290 /* Objective-C++ Productions */
2291
2292 static tree cp_parser_objc_message_receiver
2293 (cp_parser *);
2294 static tree cp_parser_objc_message_args
2295 (cp_parser *);
2296 static tree cp_parser_objc_message_expression
2297 (cp_parser *);
2298 static tree cp_parser_objc_encode_expression
2299 (cp_parser *);
2300 static tree cp_parser_objc_defs_expression
2301 (cp_parser *);
2302 static tree cp_parser_objc_protocol_expression
2303 (cp_parser *);
2304 static tree cp_parser_objc_selector_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_expression
2307 (cp_parser *);
2308 static bool cp_parser_objc_selector_p
2309 (enum cpp_ttype);
2310 static tree cp_parser_objc_selector
2311 (cp_parser *);
2312 static tree cp_parser_objc_protocol_refs_opt
2313 (cp_parser *);
2314 static void cp_parser_objc_declaration
2315 (cp_parser *, tree);
2316 static tree cp_parser_objc_statement
2317 (cp_parser *);
2318 static bool cp_parser_objc_valid_prefix_attributes
2319 (cp_parser *, tree *);
2320 static void cp_parser_objc_at_property_declaration
2321 (cp_parser *) ;
2322 static void cp_parser_objc_at_synthesize_declaration
2323 (cp_parser *) ;
2324 static void cp_parser_objc_at_dynamic_declaration
2325 (cp_parser *) ;
2326 static tree cp_parser_objc_struct_declaration
2327 (cp_parser *) ;
2328
2329 /* Utility Routines */
2330
2331 static tree cp_parser_lookup_name
2332 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2333 static tree cp_parser_lookup_name_simple
2334 (cp_parser *, tree, location_t);
2335 static tree cp_parser_maybe_treat_template_as_class
2336 (tree, bool);
2337 static bool cp_parser_check_declarator_template_parameters
2338 (cp_parser *, cp_declarator *, location_t);
2339 static bool cp_parser_check_template_parameters
2340 (cp_parser *, unsigned, location_t, cp_declarator *);
2341 static tree cp_parser_simple_cast_expression
2342 (cp_parser *);
2343 static tree cp_parser_global_scope_opt
2344 (cp_parser *, bool);
2345 static bool cp_parser_constructor_declarator_p
2346 (cp_parser *, bool);
2347 static tree cp_parser_function_definition_from_specifiers_and_declarator
2348 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2349 static tree cp_parser_function_definition_after_declarator
2350 (cp_parser *, bool);
2351 static void cp_parser_template_declaration_after_export
2352 (cp_parser *, bool);
2353 static void cp_parser_perform_template_parameter_access_checks
2354 (vec<deferred_access_check, va_gc> *);
2355 static tree cp_parser_single_declaration
2356 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2357 static tree cp_parser_functional_cast
2358 (cp_parser *, tree);
2359 static tree cp_parser_save_member_function_body
2360 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2361 static tree cp_parser_save_nsdmi
2362 (cp_parser *);
2363 static tree cp_parser_enclosed_template_argument_list
2364 (cp_parser *);
2365 static void cp_parser_save_default_args
2366 (cp_parser *, tree);
2367 static void cp_parser_late_parsing_for_member
2368 (cp_parser *, tree);
2369 static tree cp_parser_late_parse_one_default_arg
2370 (cp_parser *, tree, tree, tree);
2371 static void cp_parser_late_parsing_nsdmi
2372 (cp_parser *, tree);
2373 static void cp_parser_late_parsing_default_args
2374 (cp_parser *, tree);
2375 static tree cp_parser_sizeof_operand
2376 (cp_parser *, enum rid);
2377 static tree cp_parser_trait_expr
2378 (cp_parser *, enum rid);
2379 static bool cp_parser_declares_only_class_p
2380 (cp_parser *);
2381 static void cp_parser_set_storage_class
2382 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2383 static void cp_parser_set_decl_spec_type
2384 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2385 static void set_and_check_decl_spec_loc
2386 (cp_decl_specifier_seq *decl_specs,
2387 cp_decl_spec ds, cp_token *);
2388 static bool cp_parser_friend_p
2389 (const cp_decl_specifier_seq *);
2390 static void cp_parser_required_error
2391 (cp_parser *, required_token, bool);
2392 static cp_token *cp_parser_require
2393 (cp_parser *, enum cpp_ttype, required_token);
2394 static cp_token *cp_parser_require_keyword
2395 (cp_parser *, enum rid, required_token);
2396 static bool cp_parser_token_starts_function_definition_p
2397 (cp_token *);
2398 static bool cp_parser_next_token_starts_class_definition_p
2399 (cp_parser *);
2400 static bool cp_parser_next_token_ends_template_argument_p
2401 (cp_parser *);
2402 static bool cp_parser_nth_token_starts_template_argument_list_p
2403 (cp_parser *, size_t);
2404 static enum tag_types cp_parser_token_is_class_key
2405 (cp_token *);
2406 static void cp_parser_check_class_key
2407 (enum tag_types, tree type);
2408 static void cp_parser_check_access_in_redeclaration
2409 (tree type, location_t location);
2410 static bool cp_parser_optional_template_keyword
2411 (cp_parser *);
2412 static void cp_parser_pre_parsed_nested_name_specifier
2413 (cp_parser *);
2414 static bool cp_parser_cache_group
2415 (cp_parser *, enum cpp_ttype, unsigned);
2416 static tree cp_parser_cache_defarg
2417 (cp_parser *parser, bool nsdmi);
2418 static void cp_parser_parse_tentatively
2419 (cp_parser *);
2420 static void cp_parser_commit_to_tentative_parse
2421 (cp_parser *);
2422 static void cp_parser_commit_to_topmost_tentative_parse
2423 (cp_parser *);
2424 static void cp_parser_abort_tentative_parse
2425 (cp_parser *);
2426 static bool cp_parser_parse_definitely
2427 (cp_parser *);
2428 static inline bool cp_parser_parsing_tentatively
2429 (cp_parser *);
2430 static bool cp_parser_uncommitted_to_tentative_parse_p
2431 (cp_parser *);
2432 static void cp_parser_error
2433 (cp_parser *, const char *);
2434 static void cp_parser_name_lookup_error
2435 (cp_parser *, tree, tree, name_lookup_error, location_t);
2436 static bool cp_parser_simulate_error
2437 (cp_parser *);
2438 static bool cp_parser_check_type_definition
2439 (cp_parser *);
2440 static void cp_parser_check_for_definition_in_return_type
2441 (cp_declarator *, tree, location_t type_location);
2442 static void cp_parser_check_for_invalid_template_id
2443 (cp_parser *, tree, enum tag_types, location_t location);
2444 static bool cp_parser_non_integral_constant_expression
2445 (cp_parser *, non_integral_constant);
2446 static void cp_parser_diagnose_invalid_type_name
2447 (cp_parser *, tree, tree, location_t);
2448 static bool cp_parser_parse_and_diagnose_invalid_type_name
2449 (cp_parser *);
2450 static int cp_parser_skip_to_closing_parenthesis
2451 (cp_parser *, bool, bool, bool);
2452 static void cp_parser_skip_to_end_of_statement
2453 (cp_parser *);
2454 static void cp_parser_consume_semicolon_at_end_of_statement
2455 (cp_parser *);
2456 static void cp_parser_skip_to_end_of_block_or_statement
2457 (cp_parser *);
2458 static bool cp_parser_skip_to_closing_brace
2459 (cp_parser *);
2460 static void cp_parser_skip_to_end_of_template_parameter_list
2461 (cp_parser *);
2462 static void cp_parser_skip_to_pragma_eol
2463 (cp_parser*, cp_token *);
2464 static bool cp_parser_error_occurred
2465 (cp_parser *);
2466 static bool cp_parser_allow_gnu_extensions_p
2467 (cp_parser *);
2468 static bool cp_parser_is_pure_string_literal
2469 (cp_token *);
2470 static bool cp_parser_is_string_literal
2471 (cp_token *);
2472 static bool cp_parser_is_keyword
2473 (cp_token *, enum rid);
2474 static tree cp_parser_make_typename_type
2475 (cp_parser *, tree, tree, location_t location);
2476 static cp_declarator * cp_parser_make_indirect_declarator
2477 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2478
2479 /* Returns nonzero if we are parsing tentatively. */
2480
2481 static inline bool
2482 cp_parser_parsing_tentatively (cp_parser* parser)
2483 {
2484 return parser->context->next != NULL;
2485 }
2486
2487 /* Returns nonzero if TOKEN is a string literal. */
2488
2489 static bool
2490 cp_parser_is_pure_string_literal (cp_token* token)
2491 {
2492 return (token->type == CPP_STRING ||
2493 token->type == CPP_STRING16 ||
2494 token->type == CPP_STRING32 ||
2495 token->type == CPP_WSTRING ||
2496 token->type == CPP_UTF8STRING);
2497 }
2498
2499 /* Returns nonzero if TOKEN is a string literal
2500 of a user-defined string literal. */
2501
2502 static bool
2503 cp_parser_is_string_literal (cp_token* token)
2504 {
2505 return (cp_parser_is_pure_string_literal (token) ||
2506 token->type == CPP_STRING_USERDEF ||
2507 token->type == CPP_STRING16_USERDEF ||
2508 token->type == CPP_STRING32_USERDEF ||
2509 token->type == CPP_WSTRING_USERDEF ||
2510 token->type == CPP_UTF8STRING_USERDEF);
2511 }
2512
2513 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2514
2515 static bool
2516 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2517 {
2518 return token->keyword == keyword;
2519 }
2520
2521 /* If not parsing tentatively, issue a diagnostic of the form
2522 FILE:LINE: MESSAGE before TOKEN
2523 where TOKEN is the next token in the input stream. MESSAGE
2524 (specified by the caller) is usually of the form "expected
2525 OTHER-TOKEN". */
2526
2527 static void
2528 cp_parser_error (cp_parser* parser, const char* gmsgid)
2529 {
2530 if (!cp_parser_simulate_error (parser))
2531 {
2532 cp_token *token = cp_lexer_peek_token (parser->lexer);
2533 /* This diagnostic makes more sense if it is tagged to the line
2534 of the token we just peeked at. */
2535 cp_lexer_set_source_position_from_token (token);
2536
2537 if (token->type == CPP_PRAGMA)
2538 {
2539 error_at (token->location,
2540 "%<#pragma%> is not allowed here");
2541 cp_parser_skip_to_pragma_eol (parser, token);
2542 return;
2543 }
2544
2545 c_parse_error (gmsgid,
2546 /* Because c_parser_error does not understand
2547 CPP_KEYWORD, keywords are treated like
2548 identifiers. */
2549 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2550 token->u.value, token->flags);
2551 }
2552 }
2553
2554 /* Issue an error about name-lookup failing. NAME is the
2555 IDENTIFIER_NODE DECL is the result of
2556 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2557 the thing that we hoped to find. */
2558
2559 static void
2560 cp_parser_name_lookup_error (cp_parser* parser,
2561 tree name,
2562 tree decl,
2563 name_lookup_error desired,
2564 location_t location)
2565 {
2566 /* If name lookup completely failed, tell the user that NAME was not
2567 declared. */
2568 if (decl == error_mark_node)
2569 {
2570 if (parser->scope && parser->scope != global_namespace)
2571 error_at (location, "%<%E::%E%> has not been declared",
2572 parser->scope, name);
2573 else if (parser->scope == global_namespace)
2574 error_at (location, "%<::%E%> has not been declared", name);
2575 else if (parser->object_scope
2576 && !CLASS_TYPE_P (parser->object_scope))
2577 error_at (location, "request for member %qE in non-class type %qT",
2578 name, parser->object_scope);
2579 else if (parser->object_scope)
2580 error_at (location, "%<%T::%E%> has not been declared",
2581 parser->object_scope, name);
2582 else
2583 error_at (location, "%qE has not been declared", name);
2584 }
2585 else if (parser->scope && parser->scope != global_namespace)
2586 {
2587 switch (desired)
2588 {
2589 case NLE_TYPE:
2590 error_at (location, "%<%E::%E%> is not a type",
2591 parser->scope, name);
2592 break;
2593 case NLE_CXX98:
2594 error_at (location, "%<%E::%E%> is not a class or namespace",
2595 parser->scope, name);
2596 break;
2597 case NLE_NOT_CXX98:
2598 error_at (location,
2599 "%<%E::%E%> is not a class, namespace, or enumeration",
2600 parser->scope, name);
2601 break;
2602 default:
2603 gcc_unreachable ();
2604
2605 }
2606 }
2607 else if (parser->scope == global_namespace)
2608 {
2609 switch (desired)
2610 {
2611 case NLE_TYPE:
2612 error_at (location, "%<::%E%> is not a type", name);
2613 break;
2614 case NLE_CXX98:
2615 error_at (location, "%<::%E%> is not a class or namespace", name);
2616 break;
2617 case NLE_NOT_CXX98:
2618 error_at (location,
2619 "%<::%E%> is not a class, namespace, or enumeration",
2620 name);
2621 break;
2622 default:
2623 gcc_unreachable ();
2624 }
2625 }
2626 else
2627 {
2628 switch (desired)
2629 {
2630 case NLE_TYPE:
2631 error_at (location, "%qE is not a type", name);
2632 break;
2633 case NLE_CXX98:
2634 error_at (location, "%qE is not a class or namespace", name);
2635 break;
2636 case NLE_NOT_CXX98:
2637 error_at (location,
2638 "%qE is not a class, namespace, or enumeration", name);
2639 break;
2640 default:
2641 gcc_unreachable ();
2642 }
2643 }
2644 }
2645
2646 /* If we are parsing tentatively, remember that an error has occurred
2647 during this tentative parse. Returns true if the error was
2648 simulated; false if a message should be issued by the caller. */
2649
2650 static bool
2651 cp_parser_simulate_error (cp_parser* parser)
2652 {
2653 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2654 {
2655 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2656 return true;
2657 }
2658 return false;
2659 }
2660
2661 /* This function is called when a type is defined. If type
2662 definitions are forbidden at this point, an error message is
2663 issued. */
2664
2665 static bool
2666 cp_parser_check_type_definition (cp_parser* parser)
2667 {
2668 /* If types are forbidden here, issue a message. */
2669 if (parser->type_definition_forbidden_message)
2670 {
2671 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2672 in the message need to be interpreted. */
2673 error (parser->type_definition_forbidden_message);
2674 return false;
2675 }
2676 return true;
2677 }
2678
2679 /* This function is called when the DECLARATOR is processed. The TYPE
2680 was a type defined in the decl-specifiers. If it is invalid to
2681 define a type in the decl-specifiers for DECLARATOR, an error is
2682 issued. TYPE_LOCATION is the location of TYPE and is used
2683 for error reporting. */
2684
2685 static void
2686 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2687 tree type, location_t type_location)
2688 {
2689 /* [dcl.fct] forbids type definitions in return types.
2690 Unfortunately, it's not easy to know whether or not we are
2691 processing a return type until after the fact. */
2692 while (declarator
2693 && (declarator->kind == cdk_pointer
2694 || declarator->kind == cdk_reference
2695 || declarator->kind == cdk_ptrmem))
2696 declarator = declarator->declarator;
2697 if (declarator
2698 && declarator->kind == cdk_function)
2699 {
2700 error_at (type_location,
2701 "new types may not be defined in a return type");
2702 inform (type_location,
2703 "(perhaps a semicolon is missing after the definition of %qT)",
2704 type);
2705 }
2706 }
2707
2708 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2709 "<" in any valid C++ program. If the next token is indeed "<",
2710 issue a message warning the user about what appears to be an
2711 invalid attempt to form a template-id. LOCATION is the location
2712 of the type-specifier (TYPE) */
2713
2714 static void
2715 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2716 tree type,
2717 enum tag_types tag_type,
2718 location_t location)
2719 {
2720 cp_token_position start = 0;
2721
2722 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2723 {
2724 if (TYPE_P (type))
2725 error_at (location, "%qT is not a template", type);
2726 else if (identifier_p (type))
2727 {
2728 if (tag_type != none_type)
2729 error_at (location, "%qE is not a class template", type);
2730 else
2731 error_at (location, "%qE is not a template", type);
2732 }
2733 else
2734 error_at (location, "invalid template-id");
2735 /* Remember the location of the invalid "<". */
2736 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2737 start = cp_lexer_token_position (parser->lexer, true);
2738 /* Consume the "<". */
2739 cp_lexer_consume_token (parser->lexer);
2740 /* Parse the template arguments. */
2741 cp_parser_enclosed_template_argument_list (parser);
2742 /* Permanently remove the invalid template arguments so that
2743 this error message is not issued again. */
2744 if (start)
2745 cp_lexer_purge_tokens_after (parser->lexer, start);
2746 }
2747 }
2748
2749 /* If parsing an integral constant-expression, issue an error message
2750 about the fact that THING appeared and return true. Otherwise,
2751 return false. In either case, set
2752 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2753
2754 static bool
2755 cp_parser_non_integral_constant_expression (cp_parser *parser,
2756 non_integral_constant thing)
2757 {
2758 parser->non_integral_constant_expression_p = true;
2759 if (parser->integral_constant_expression_p)
2760 {
2761 if (!parser->allow_non_integral_constant_expression_p)
2762 {
2763 const char *msg = NULL;
2764 switch (thing)
2765 {
2766 case NIC_FLOAT:
2767 error ("floating-point literal "
2768 "cannot appear in a constant-expression");
2769 return true;
2770 case NIC_CAST:
2771 error ("a cast to a type other than an integral or "
2772 "enumeration type cannot appear in a "
2773 "constant-expression");
2774 return true;
2775 case NIC_TYPEID:
2776 error ("%<typeid%> operator "
2777 "cannot appear in a constant-expression");
2778 return true;
2779 case NIC_NCC:
2780 error ("non-constant compound literals "
2781 "cannot appear in a constant-expression");
2782 return true;
2783 case NIC_FUNC_CALL:
2784 error ("a function call "
2785 "cannot appear in a constant-expression");
2786 return true;
2787 case NIC_INC:
2788 error ("an increment "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_DEC:
2792 error ("an decrement "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_ARRAY_REF:
2796 error ("an array reference "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_ADDR_LABEL:
2800 error ("the address of a label "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_OVERLOADED:
2804 error ("calls to overloaded operators "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_ASSIGNMENT:
2808 error ("an assignment cannot appear in a constant-expression");
2809 return true;
2810 case NIC_COMMA:
2811 error ("a comma operator "
2812 "cannot appear in a constant-expression");
2813 return true;
2814 case NIC_CONSTRUCTOR:
2815 error ("a call to a constructor "
2816 "cannot appear in a constant-expression");
2817 return true;
2818 case NIC_TRANSACTION:
2819 error ("a transaction expression "
2820 "cannot appear in a constant-expression");
2821 return true;
2822 case NIC_THIS:
2823 msg = "this";
2824 break;
2825 case NIC_FUNC_NAME:
2826 msg = "__FUNCTION__";
2827 break;
2828 case NIC_PRETTY_FUNC:
2829 msg = "__PRETTY_FUNCTION__";
2830 break;
2831 case NIC_C99_FUNC:
2832 msg = "__func__";
2833 break;
2834 case NIC_VA_ARG:
2835 msg = "va_arg";
2836 break;
2837 case NIC_ARROW:
2838 msg = "->";
2839 break;
2840 case NIC_POINT:
2841 msg = ".";
2842 break;
2843 case NIC_STAR:
2844 msg = "*";
2845 break;
2846 case NIC_ADDR:
2847 msg = "&";
2848 break;
2849 case NIC_PREINCREMENT:
2850 msg = "++";
2851 break;
2852 case NIC_PREDECREMENT:
2853 msg = "--";
2854 break;
2855 case NIC_NEW:
2856 msg = "new";
2857 break;
2858 case NIC_DEL:
2859 msg = "delete";
2860 break;
2861 default:
2862 gcc_unreachable ();
2863 }
2864 if (msg)
2865 error ("%qs cannot appear in a constant-expression", msg);
2866 return true;
2867 }
2868 }
2869 return false;
2870 }
2871
2872 /* Emit a diagnostic for an invalid type name. SCOPE is the
2873 qualifying scope (or NULL, if none) for ID. This function commits
2874 to the current active tentative parse, if any. (Otherwise, the
2875 problematic construct might be encountered again later, resulting
2876 in duplicate error messages.) LOCATION is the location of ID. */
2877
2878 static void
2879 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2880 tree scope, tree id,
2881 location_t location)
2882 {
2883 tree decl, old_scope, ambiguous_decls;
2884 cp_parser_commit_to_tentative_parse (parser);
2885 /* Try to lookup the identifier. */
2886 old_scope = parser->scope;
2887 parser->scope = scope;
2888 decl = cp_parser_lookup_name (parser, id, none_type,
2889 /*is_template=*/false,
2890 /*is_namespace=*/false,
2891 /*check_dependency=*/true,
2892 &ambiguous_decls, location);
2893 parser->scope = old_scope;
2894 if (ambiguous_decls)
2895 /* If the lookup was ambiguous, an error will already have
2896 been issued. */
2897 return;
2898 /* If the lookup found a template-name, it means that the user forgot
2899 to specify an argument list. Emit a useful error message. */
2900 if (TREE_CODE (decl) == TEMPLATE_DECL)
2901 error_at (location,
2902 "invalid use of template-name %qE without an argument list",
2903 decl);
2904 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2905 error_at (location, "invalid use of destructor %qD as a type", id);
2906 else if (TREE_CODE (decl) == TYPE_DECL)
2907 /* Something like 'unsigned A a;' */
2908 error_at (location, "invalid combination of multiple type-specifiers");
2909 else if (!parser->scope)
2910 {
2911 /* Issue an error message. */
2912 error_at (location, "%qE does not name a type", id);
2913 /* If we're in a template class, it's possible that the user was
2914 referring to a type from a base class. For example:
2915
2916 template <typename T> struct A { typedef T X; };
2917 template <typename T> struct B : public A<T> { X x; };
2918
2919 The user should have said "typename A<T>::X". */
2920 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2921 inform (location, "C++11 %<constexpr%> only available with "
2922 "-std=c++11 or -std=gnu++11");
2923 else if (processing_template_decl && current_class_type
2924 && TYPE_BINFO (current_class_type))
2925 {
2926 tree b;
2927
2928 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2929 b;
2930 b = TREE_CHAIN (b))
2931 {
2932 tree base_type = BINFO_TYPE (b);
2933 if (CLASS_TYPE_P (base_type)
2934 && dependent_type_p (base_type))
2935 {
2936 tree field;
2937 /* Go from a particular instantiation of the
2938 template (which will have an empty TYPE_FIELDs),
2939 to the main version. */
2940 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2941 for (field = TYPE_FIELDS (base_type);
2942 field;
2943 field = DECL_CHAIN (field))
2944 if (TREE_CODE (field) == TYPE_DECL
2945 && DECL_NAME (field) == id)
2946 {
2947 inform (location,
2948 "(perhaps %<typename %T::%E%> was intended)",
2949 BINFO_TYPE (b), id);
2950 break;
2951 }
2952 if (field)
2953 break;
2954 }
2955 }
2956 }
2957 }
2958 /* Here we diagnose qualified-ids where the scope is actually correct,
2959 but the identifier does not resolve to a valid type name. */
2960 else if (parser->scope != error_mark_node)
2961 {
2962 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2963 {
2964 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2965 error_at (location_of (id),
2966 "%qE in namespace %qE does not name a template type",
2967 id, parser->scope);
2968 else
2969 error_at (location_of (id),
2970 "%qE in namespace %qE does not name a type",
2971 id, parser->scope);
2972 }
2973 else if (CLASS_TYPE_P (parser->scope)
2974 && constructor_name_p (id, parser->scope))
2975 {
2976 /* A<T>::A<T>() */
2977 error_at (location, "%<%T::%E%> names the constructor, not"
2978 " the type", parser->scope, id);
2979 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2980 error_at (location, "and %qT has no template constructors",
2981 parser->scope);
2982 }
2983 else if (TYPE_P (parser->scope)
2984 && dependent_scope_p (parser->scope))
2985 error_at (location, "need %<typename%> before %<%T::%E%> because "
2986 "%qT is a dependent scope",
2987 parser->scope, id, parser->scope);
2988 else if (TYPE_P (parser->scope))
2989 {
2990 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2991 error_at (location_of (id),
2992 "%qE in %q#T does not name a template type",
2993 id, parser->scope);
2994 else
2995 error_at (location_of (id),
2996 "%qE in %q#T does not name a type",
2997 id, parser->scope);
2998 }
2999 else
3000 gcc_unreachable ();
3001 }
3002 }
3003
3004 /* Check for a common situation where a type-name should be present,
3005 but is not, and issue a sensible error message. Returns true if an
3006 invalid type-name was detected.
3007
3008 The situation handled by this function are variable declarations of the
3009 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3010 Usually, `ID' should name a type, but if we got here it means that it
3011 does not. We try to emit the best possible error message depending on
3012 how exactly the id-expression looks like. */
3013
3014 static bool
3015 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3016 {
3017 tree id;
3018 cp_token *token = cp_lexer_peek_token (parser->lexer);
3019
3020 /* Avoid duplicate error about ambiguous lookup. */
3021 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3022 {
3023 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3024 if (next->type == CPP_NAME && next->error_reported)
3025 goto out;
3026 }
3027
3028 cp_parser_parse_tentatively (parser);
3029 id = cp_parser_id_expression (parser,
3030 /*template_keyword_p=*/false,
3031 /*check_dependency_p=*/true,
3032 /*template_p=*/NULL,
3033 /*declarator_p=*/true,
3034 /*optional_p=*/false);
3035 /* If the next token is a (, this is a function with no explicit return
3036 type, i.e. constructor, destructor or conversion op. */
3037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3038 || TREE_CODE (id) == TYPE_DECL)
3039 {
3040 cp_parser_abort_tentative_parse (parser);
3041 return false;
3042 }
3043 if (!cp_parser_parse_definitely (parser))
3044 return false;
3045
3046 /* Emit a diagnostic for the invalid type. */
3047 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3048 id, token->location);
3049 out:
3050 /* If we aren't in the middle of a declarator (i.e. in a
3051 parameter-declaration-clause), skip to the end of the declaration;
3052 there's no point in trying to process it. */
3053 if (!parser->in_declarator_p)
3054 cp_parser_skip_to_end_of_block_or_statement (parser);
3055 return true;
3056 }
3057
3058 /* Consume tokens up to, and including, the next non-nested closing `)'.
3059 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3060 are doing error recovery. Returns -1 if OR_COMMA is true and we
3061 found an unnested comma. */
3062
3063 static int
3064 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3065 bool recovering,
3066 bool or_comma,
3067 bool consume_paren)
3068 {
3069 unsigned paren_depth = 0;
3070 unsigned brace_depth = 0;
3071 unsigned square_depth = 0;
3072
3073 if (recovering && !or_comma
3074 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3075 return 0;
3076
3077 while (true)
3078 {
3079 cp_token * token = cp_lexer_peek_token (parser->lexer);
3080
3081 switch (token->type)
3082 {
3083 case CPP_EOF:
3084 case CPP_PRAGMA_EOL:
3085 /* If we've run out of tokens, then there is no closing `)'. */
3086 return 0;
3087
3088 /* This is good for lambda expression capture-lists. */
3089 case CPP_OPEN_SQUARE:
3090 ++square_depth;
3091 break;
3092 case CPP_CLOSE_SQUARE:
3093 if (!square_depth--)
3094 return 0;
3095 break;
3096
3097 case CPP_SEMICOLON:
3098 /* This matches the processing in skip_to_end_of_statement. */
3099 if (!brace_depth)
3100 return 0;
3101 break;
3102
3103 case CPP_OPEN_BRACE:
3104 ++brace_depth;
3105 break;
3106 case CPP_CLOSE_BRACE:
3107 if (!brace_depth--)
3108 return 0;
3109 break;
3110
3111 case CPP_COMMA:
3112 if (recovering && or_comma && !brace_depth && !paren_depth
3113 && !square_depth)
3114 return -1;
3115 break;
3116
3117 case CPP_OPEN_PAREN:
3118 if (!brace_depth)
3119 ++paren_depth;
3120 break;
3121
3122 case CPP_CLOSE_PAREN:
3123 if (!brace_depth && !paren_depth--)
3124 {
3125 if (consume_paren)
3126 cp_lexer_consume_token (parser->lexer);
3127 return 1;
3128 }
3129 break;
3130
3131 default:
3132 break;
3133 }
3134
3135 /* Consume the token. */
3136 cp_lexer_consume_token (parser->lexer);
3137 }
3138 }
3139
3140 /* Consume tokens until we reach the end of the current statement.
3141 Normally, that will be just before consuming a `;'. However, if a
3142 non-nested `}' comes first, then we stop before consuming that. */
3143
3144 static void
3145 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3146 {
3147 unsigned nesting_depth = 0;
3148
3149 /* Unwind generic function template scope if necessary. */
3150 if (parser->fully_implicit_function_template_p)
3151 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3152
3153 while (true)
3154 {
3155 cp_token *token = cp_lexer_peek_token (parser->lexer);
3156
3157 switch (token->type)
3158 {
3159 case CPP_EOF:
3160 case CPP_PRAGMA_EOL:
3161 /* If we've run out of tokens, stop. */
3162 return;
3163
3164 case CPP_SEMICOLON:
3165 /* If the next token is a `;', we have reached the end of the
3166 statement. */
3167 if (!nesting_depth)
3168 return;
3169 break;
3170
3171 case CPP_CLOSE_BRACE:
3172 /* If this is a non-nested '}', stop before consuming it.
3173 That way, when confronted with something like:
3174
3175 { 3 + }
3176
3177 we stop before consuming the closing '}', even though we
3178 have not yet reached a `;'. */
3179 if (nesting_depth == 0)
3180 return;
3181
3182 /* If it is the closing '}' for a block that we have
3183 scanned, stop -- but only after consuming the token.
3184 That way given:
3185
3186 void f g () { ... }
3187 typedef int I;
3188
3189 we will stop after the body of the erroneously declared
3190 function, but before consuming the following `typedef'
3191 declaration. */
3192 if (--nesting_depth == 0)
3193 {
3194 cp_lexer_consume_token (parser->lexer);
3195 return;
3196 }
3197
3198 case CPP_OPEN_BRACE:
3199 ++nesting_depth;
3200 break;
3201
3202 default:
3203 break;
3204 }
3205
3206 /* Consume the token. */
3207 cp_lexer_consume_token (parser->lexer);
3208 }
3209 }
3210
3211 /* This function is called at the end of a statement or declaration.
3212 If the next token is a semicolon, it is consumed; otherwise, error
3213 recovery is attempted. */
3214
3215 static void
3216 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3217 {
3218 /* Look for the trailing `;'. */
3219 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3220 {
3221 /* If there is additional (erroneous) input, skip to the end of
3222 the statement. */
3223 cp_parser_skip_to_end_of_statement (parser);
3224 /* If the next token is now a `;', consume it. */
3225 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3226 cp_lexer_consume_token (parser->lexer);
3227 }
3228 }
3229
3230 /* Skip tokens until we have consumed an entire block, or until we
3231 have consumed a non-nested `;'. */
3232
3233 static void
3234 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3235 {
3236 int nesting_depth = 0;
3237
3238 /* Unwind generic function template scope if necessary. */
3239 if (parser->fully_implicit_function_template_p)
3240 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3241
3242 while (nesting_depth >= 0)
3243 {
3244 cp_token *token = cp_lexer_peek_token (parser->lexer);
3245
3246 switch (token->type)
3247 {
3248 case CPP_EOF:
3249 case CPP_PRAGMA_EOL:
3250 /* If we've run out of tokens, stop. */
3251 return;
3252
3253 case CPP_SEMICOLON:
3254 /* Stop if this is an unnested ';'. */
3255 if (!nesting_depth)
3256 nesting_depth = -1;
3257 break;
3258
3259 case CPP_CLOSE_BRACE:
3260 /* Stop if this is an unnested '}', or closes the outermost
3261 nesting level. */
3262 nesting_depth--;
3263 if (nesting_depth < 0)
3264 return;
3265 if (!nesting_depth)
3266 nesting_depth = -1;
3267 break;
3268
3269 case CPP_OPEN_BRACE:
3270 /* Nest. */
3271 nesting_depth++;
3272 break;
3273
3274 default:
3275 break;
3276 }
3277
3278 /* Consume the token. */
3279 cp_lexer_consume_token (parser->lexer);
3280 }
3281 }
3282
3283 /* Skip tokens until a non-nested closing curly brace is the next
3284 token, or there are no more tokens. Return true in the first case,
3285 false otherwise. */
3286
3287 static bool
3288 cp_parser_skip_to_closing_brace (cp_parser *parser)
3289 {
3290 unsigned nesting_depth = 0;
3291
3292 while (true)
3293 {
3294 cp_token *token = cp_lexer_peek_token (parser->lexer);
3295
3296 switch (token->type)
3297 {
3298 case CPP_EOF:
3299 case CPP_PRAGMA_EOL:
3300 /* If we've run out of tokens, stop. */
3301 return false;
3302
3303 case CPP_CLOSE_BRACE:
3304 /* If the next token is a non-nested `}', then we have reached
3305 the end of the current block. */
3306 if (nesting_depth-- == 0)
3307 return true;
3308 break;
3309
3310 case CPP_OPEN_BRACE:
3311 /* If it the next token is a `{', then we are entering a new
3312 block. Consume the entire block. */
3313 ++nesting_depth;
3314 break;
3315
3316 default:
3317 break;
3318 }
3319
3320 /* Consume the token. */
3321 cp_lexer_consume_token (parser->lexer);
3322 }
3323 }
3324
3325 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3326 parameter is the PRAGMA token, allowing us to purge the entire pragma
3327 sequence. */
3328
3329 static void
3330 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3331 {
3332 cp_token *token;
3333
3334 parser->lexer->in_pragma = false;
3335
3336 do
3337 token = cp_lexer_consume_token (parser->lexer);
3338 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3339
3340 /* Ensure that the pragma is not parsed again. */
3341 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3342 }
3343
3344 /* Require pragma end of line, resyncing with it as necessary. The
3345 arguments are as for cp_parser_skip_to_pragma_eol. */
3346
3347 static void
3348 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3349 {
3350 parser->lexer->in_pragma = false;
3351 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3353 }
3354
3355 /* This is a simple wrapper around make_typename_type. When the id is
3356 an unresolved identifier node, we can provide a superior diagnostic
3357 using cp_parser_diagnose_invalid_type_name. */
3358
3359 static tree
3360 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3361 tree id, location_t id_location)
3362 {
3363 tree result;
3364 if (identifier_p (id))
3365 {
3366 result = make_typename_type (scope, id, typename_type,
3367 /*complain=*/tf_none);
3368 if (result == error_mark_node)
3369 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3370 return result;
3371 }
3372 return make_typename_type (scope, id, typename_type, tf_error);
3373 }
3374
3375 /* This is a wrapper around the
3376 make_{pointer,ptrmem,reference}_declarator functions that decides
3377 which one to call based on the CODE and CLASS_TYPE arguments. The
3378 CODE argument should be one of the values returned by
3379 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3380 appertain to the pointer or reference. */
3381
3382 static cp_declarator *
3383 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3384 cp_cv_quals cv_qualifiers,
3385 cp_declarator *target,
3386 tree attributes)
3387 {
3388 if (code == ERROR_MARK)
3389 return cp_error_declarator;
3390
3391 if (code == INDIRECT_REF)
3392 if (class_type == NULL_TREE)
3393 return make_pointer_declarator (cv_qualifiers, target, attributes);
3394 else
3395 return make_ptrmem_declarator (cv_qualifiers, class_type,
3396 target, attributes);
3397 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3398 return make_reference_declarator (cv_qualifiers, target,
3399 false, attributes);
3400 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3401 return make_reference_declarator (cv_qualifiers, target,
3402 true, attributes);
3403 gcc_unreachable ();
3404 }
3405
3406 /* Create a new C++ parser. */
3407
3408 static cp_parser *
3409 cp_parser_new (void)
3410 {
3411 cp_parser *parser;
3412 cp_lexer *lexer;
3413 unsigned i;
3414
3415 /* cp_lexer_new_main is called before doing GC allocation because
3416 cp_lexer_new_main might load a PCH file. */
3417 lexer = cp_lexer_new_main ();
3418
3419 /* Initialize the binops_by_token so that we can get the tree
3420 directly from the token. */
3421 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3422 binops_by_token[binops[i].token_type] = binops[i];
3423
3424 parser = ggc_alloc_cleared_cp_parser ();
3425 parser->lexer = lexer;
3426 parser->context = cp_parser_context_new (NULL);
3427
3428 /* For now, we always accept GNU extensions. */
3429 parser->allow_gnu_extensions_p = 1;
3430
3431 /* The `>' token is a greater-than operator, not the end of a
3432 template-id. */
3433 parser->greater_than_is_operator_p = true;
3434
3435 parser->default_arg_ok_p = true;
3436
3437 /* We are not parsing a constant-expression. */
3438 parser->integral_constant_expression_p = false;
3439 parser->allow_non_integral_constant_expression_p = false;
3440 parser->non_integral_constant_expression_p = false;
3441
3442 /* Local variable names are not forbidden. */
3443 parser->local_variables_forbidden_p = false;
3444
3445 /* We are not processing an `extern "C"' declaration. */
3446 parser->in_unbraced_linkage_specification_p = false;
3447
3448 /* We are not processing a declarator. */
3449 parser->in_declarator_p = false;
3450
3451 /* We are not processing a template-argument-list. */
3452 parser->in_template_argument_list_p = false;
3453
3454 /* We are not in an iteration statement. */
3455 parser->in_statement = 0;
3456
3457 /* We are not in a switch statement. */
3458 parser->in_switch_statement_p = false;
3459
3460 /* We are not parsing a type-id inside an expression. */
3461 parser->in_type_id_in_expr_p = false;
3462
3463 /* Declarations aren't implicitly extern "C". */
3464 parser->implicit_extern_c = false;
3465
3466 /* String literals should be translated to the execution character set. */
3467 parser->translate_strings_p = true;
3468
3469 /* We are not parsing a function body. */
3470 parser->in_function_body = false;
3471
3472 /* We can correct until told otherwise. */
3473 parser->colon_corrects_to_scope_p = true;
3474
3475 /* The unparsed function queue is empty. */
3476 push_unparsed_function_queues (parser);
3477
3478 /* There are no classes being defined. */
3479 parser->num_classes_being_defined = 0;
3480
3481 /* No template parameters apply. */
3482 parser->num_template_parameter_lists = 0;
3483
3484 /* Not declaring an implicit function template. */
3485 parser->auto_is_implicit_function_template_parm_p = false;
3486 parser->fully_implicit_function_template_p = false;
3487 parser->implicit_template_parms = 0;
3488 parser->implicit_template_scope = 0;
3489
3490 return parser;
3491 }
3492
3493 /* Create a cp_lexer structure which will emit the tokens in CACHE
3494 and push it onto the parser's lexer stack. This is used for delayed
3495 parsing of in-class method bodies and default arguments, and should
3496 not be confused with tentative parsing. */
3497 static void
3498 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3499 {
3500 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3501 lexer->next = parser->lexer;
3502 parser->lexer = lexer;
3503
3504 /* Move the current source position to that of the first token in the
3505 new lexer. */
3506 cp_lexer_set_source_position_from_token (lexer->next_token);
3507 }
3508
3509 /* Pop the top lexer off the parser stack. This is never used for the
3510 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3511 static void
3512 cp_parser_pop_lexer (cp_parser *parser)
3513 {
3514 cp_lexer *lexer = parser->lexer;
3515 parser->lexer = lexer->next;
3516 cp_lexer_destroy (lexer);
3517
3518 /* Put the current source position back where it was before this
3519 lexer was pushed. */
3520 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3521 }
3522
3523 /* Lexical conventions [gram.lex] */
3524
3525 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3526 identifier. */
3527
3528 static tree
3529 cp_parser_identifier (cp_parser* parser)
3530 {
3531 cp_token *token;
3532
3533 /* Look for the identifier. */
3534 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3535 /* Return the value. */
3536 return token ? token->u.value : error_mark_node;
3537 }
3538
3539 /* Parse a sequence of adjacent string constants. Returns a
3540 TREE_STRING representing the combined, nul-terminated string
3541 constant. If TRANSLATE is true, translate the string to the
3542 execution character set. If WIDE_OK is true, a wide string is
3543 invalid here.
3544
3545 C++98 [lex.string] says that if a narrow string literal token is
3546 adjacent to a wide string literal token, the behavior is undefined.
3547 However, C99 6.4.5p4 says that this results in a wide string literal.
3548 We follow C99 here, for consistency with the C front end.
3549
3550 This code is largely lifted from lex_string() in c-lex.c.
3551
3552 FUTURE: ObjC++ will need to handle @-strings here. */
3553 static tree
3554 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3555 {
3556 tree value;
3557 size_t count;
3558 struct obstack str_ob;
3559 cpp_string str, istr, *strs;
3560 cp_token *tok;
3561 enum cpp_ttype type, curr_type;
3562 int have_suffix_p = 0;
3563 tree string_tree;
3564 tree suffix_id = NULL_TREE;
3565 bool curr_tok_is_userdef_p = false;
3566
3567 tok = cp_lexer_peek_token (parser->lexer);
3568 if (!cp_parser_is_string_literal (tok))
3569 {
3570 cp_parser_error (parser, "expected string-literal");
3571 return error_mark_node;
3572 }
3573
3574 if (cpp_userdef_string_p (tok->type))
3575 {
3576 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3577 curr_type = cpp_userdef_string_remove_type (tok->type);
3578 curr_tok_is_userdef_p = true;
3579 }
3580 else
3581 {
3582 string_tree = tok->u.value;
3583 curr_type = tok->type;
3584 }
3585 type = curr_type;
3586
3587 /* Try to avoid the overhead of creating and destroying an obstack
3588 for the common case of just one string. */
3589 if (!cp_parser_is_string_literal
3590 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3591 {
3592 cp_lexer_consume_token (parser->lexer);
3593
3594 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3595 str.len = TREE_STRING_LENGTH (string_tree);
3596 count = 1;
3597
3598 if (curr_tok_is_userdef_p)
3599 {
3600 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3601 have_suffix_p = 1;
3602 curr_type = cpp_userdef_string_remove_type (tok->type);
3603 }
3604 else
3605 curr_type = tok->type;
3606
3607 strs = &str;
3608 }
3609 else
3610 {
3611 gcc_obstack_init (&str_ob);
3612 count = 0;
3613
3614 do
3615 {
3616 cp_lexer_consume_token (parser->lexer);
3617 count++;
3618 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3619 str.len = TREE_STRING_LENGTH (string_tree);
3620
3621 if (curr_tok_is_userdef_p)
3622 {
3623 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3624 if (have_suffix_p == 0)
3625 {
3626 suffix_id = curr_suffix_id;
3627 have_suffix_p = 1;
3628 }
3629 else if (have_suffix_p == 1
3630 && curr_suffix_id != suffix_id)
3631 {
3632 error ("inconsistent user-defined literal suffixes"
3633 " %qD and %qD in string literal",
3634 suffix_id, curr_suffix_id);
3635 have_suffix_p = -1;
3636 }
3637 curr_type = cpp_userdef_string_remove_type (tok->type);
3638 }
3639 else
3640 curr_type = tok->type;
3641
3642 if (type != curr_type)
3643 {
3644 if (type == CPP_STRING)
3645 type = curr_type;
3646 else if (curr_type != CPP_STRING)
3647 error_at (tok->location,
3648 "unsupported non-standard concatenation "
3649 "of string literals");
3650 }
3651
3652 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3653
3654 tok = cp_lexer_peek_token (parser->lexer);
3655 if (cpp_userdef_string_p (tok->type))
3656 {
3657 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3658 curr_type = cpp_userdef_string_remove_type (tok->type);
3659 curr_tok_is_userdef_p = true;
3660 }
3661 else
3662 {
3663 string_tree = tok->u.value;
3664 curr_type = tok->type;
3665 curr_tok_is_userdef_p = false;
3666 }
3667 }
3668 while (cp_parser_is_string_literal (tok));
3669
3670 strs = (cpp_string *) obstack_finish (&str_ob);
3671 }
3672
3673 if (type != CPP_STRING && !wide_ok)
3674 {
3675 cp_parser_error (parser, "a wide string is invalid in this context");
3676 type = CPP_STRING;
3677 }
3678
3679 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3680 (parse_in, strs, count, &istr, type))
3681 {
3682 value = build_string (istr.len, (const char *)istr.text);
3683 free (CONST_CAST (unsigned char *, istr.text));
3684
3685 switch (type)
3686 {
3687 default:
3688 case CPP_STRING:
3689 case CPP_UTF8STRING:
3690 TREE_TYPE (value) = char_array_type_node;
3691 break;
3692 case CPP_STRING16:
3693 TREE_TYPE (value) = char16_array_type_node;
3694 break;
3695 case CPP_STRING32:
3696 TREE_TYPE (value) = char32_array_type_node;
3697 break;
3698 case CPP_WSTRING:
3699 TREE_TYPE (value) = wchar_array_type_node;
3700 break;
3701 }
3702
3703 value = fix_string_type (value);
3704
3705 if (have_suffix_p)
3706 {
3707 tree literal = build_userdef_literal (suffix_id, value,
3708 OT_NONE, NULL_TREE);
3709 tok->u.value = literal;
3710 return cp_parser_userdef_string_literal (tok);
3711 }
3712 }
3713 else
3714 /* cpp_interpret_string has issued an error. */
3715 value = error_mark_node;
3716
3717 if (count > 1)
3718 obstack_free (&str_ob, 0);
3719
3720 return value;
3721 }
3722
3723 /* Look up a literal operator with the name and the exact arguments. */
3724
3725 static tree
3726 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3727 {
3728 tree decl, fns;
3729 decl = lookup_name (name);
3730 if (!decl || !is_overloaded_fn (decl))
3731 return error_mark_node;
3732
3733 for (fns = decl; fns; fns = OVL_NEXT (fns))
3734 {
3735 unsigned int ix;
3736 bool found = true;
3737 tree fn = OVL_CURRENT (fns);
3738 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3739 if (parmtypes != NULL_TREE)
3740 {
3741 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3742 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3743 {
3744 tree tparm = TREE_VALUE (parmtypes);
3745 tree targ = TREE_TYPE ((*args)[ix]);
3746 bool ptr = TYPE_PTR_P (tparm);
3747 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3748 if ((ptr || arr || !same_type_p (tparm, targ))
3749 && (!ptr || !arr
3750 || !same_type_p (TREE_TYPE (tparm),
3751 TREE_TYPE (targ))))
3752 found = false;
3753 }
3754 if (found
3755 && ix == vec_safe_length (args)
3756 /* May be this should be sufficient_parms_p instead,
3757 depending on how exactly should user-defined literals
3758 work in presence of default arguments on the literal
3759 operator parameters. */
3760 && parmtypes == void_list_node)
3761 return fn;
3762 }
3763 }
3764
3765 return error_mark_node;
3766 }
3767
3768 /* Parse a user-defined char constant. Returns a call to a user-defined
3769 literal operator taking the character as an argument. */
3770
3771 static tree
3772 cp_parser_userdef_char_literal (cp_parser *parser)
3773 {
3774 cp_token *token = cp_lexer_consume_token (parser->lexer);
3775 tree literal = token->u.value;
3776 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3777 tree value = USERDEF_LITERAL_VALUE (literal);
3778 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3779 tree decl, result;
3780
3781 /* Build up a call to the user-defined operator */
3782 /* Lookup the name we got back from the id-expression. */
3783 vec<tree, va_gc> *args = make_tree_vector ();
3784 vec_safe_push (args, value);
3785 decl = lookup_literal_operator (name, args);
3786 if (!decl || decl == error_mark_node)
3787 {
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name, TREE_TYPE (value));
3790 release_tree_vector (args);
3791 return error_mark_node;
3792 }
3793 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3794 release_tree_vector (args);
3795 if (result != error_mark_node)
3796 return result;
3797
3798 error ("unable to find character literal operator %qD with %qT argument",
3799 name, TREE_TYPE (value));
3800 return error_mark_node;
3801 }
3802
3803 /* A subroutine of cp_parser_userdef_numeric_literal to
3804 create a char... template parameter pack from a string node. */
3805
3806 static tree
3807 make_char_string_pack (tree value)
3808 {
3809 tree charvec;
3810 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3811 const char *str = TREE_STRING_POINTER (value);
3812 int i, len = TREE_STRING_LENGTH (value) - 1;
3813 tree argvec = make_tree_vec (1);
3814
3815 /* Fill in CHARVEC with all of the parameters. */
3816 charvec = make_tree_vec (len);
3817 for (i = 0; i < len; ++i)
3818 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3819
3820 /* Build the argument packs. */
3821 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3822 TREE_TYPE (argpack) = char_type_node;
3823
3824 TREE_VEC_ELT (argvec, 0) = argpack;
3825
3826 return argvec;
3827 }
3828
3829 /* A subroutine of cp_parser_userdef_numeric_literal to
3830 create a char... template parameter pack from a string node. */
3831
3832 static tree
3833 make_string_pack (tree value)
3834 {
3835 tree charvec;
3836 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3837 const unsigned char *str
3838 = (const unsigned char *) TREE_STRING_POINTER (value);
3839 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3840 int len = TREE_STRING_LENGTH (value) / sz - 1;
3841 tree argvec = make_tree_vec (2);
3842
3843 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3844 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3845
3846 /* First template parm is character type. */
3847 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3848
3849 /* Fill in CHARVEC with all of the parameters. */
3850 charvec = make_tree_vec (len);
3851 for (int i = 0; i < len; ++i)
3852 TREE_VEC_ELT (charvec, i)
3853 = double_int_to_tree (str_char_type_node,
3854 double_int::from_buffer (str + i * sz, sz));
3855
3856 /* Build the argument packs. */
3857 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3858 TREE_TYPE (argpack) = str_char_type_node;
3859
3860 TREE_VEC_ELT (argvec, 1) = argpack;
3861
3862 return argvec;
3863 }
3864
3865 /* Parse a user-defined numeric constant. returns a call to a user-defined
3866 literal operator. */
3867
3868 static tree
3869 cp_parser_userdef_numeric_literal (cp_parser *parser)
3870 {
3871 cp_token *token = cp_lexer_consume_token (parser->lexer);
3872 tree literal = token->u.value;
3873 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3874 tree value = USERDEF_LITERAL_VALUE (literal);
3875 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3876 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3877 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3878 tree decl, result;
3879 vec<tree, va_gc> *args;
3880
3881 /* Look for a literal operator taking the exact type of numeric argument
3882 as the literal value. */
3883 args = make_tree_vector ();
3884 vec_safe_push (args, value);
3885 decl = lookup_literal_operator (name, args);
3886 if (decl && decl != error_mark_node)
3887 {
3888 result = finish_call_expr (decl, &args, false, true, tf_none);
3889 if (result != error_mark_node)
3890 {
3891 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3892 warning_at (token->location, OPT_Woverflow,
3893 "integer literal exceeds range of %qT type",
3894 long_long_unsigned_type_node);
3895 else
3896 {
3897 if (overflow > 0)
3898 warning_at (token->location, OPT_Woverflow,
3899 "floating literal exceeds range of %qT type",
3900 long_double_type_node);
3901 else if (overflow < 0)
3902 warning_at (token->location, OPT_Woverflow,
3903 "floating literal truncated to zero");
3904 }
3905 release_tree_vector (args);
3906 return result;
3907 }
3908 }
3909 release_tree_vector (args);
3910
3911 /* If the numeric argument didn't work, look for a raw literal
3912 operator taking a const char* argument consisting of the number
3913 in string format. */
3914 args = make_tree_vector ();
3915 vec_safe_push (args, num_string);
3916 decl = lookup_literal_operator (name, args);
3917 if (decl && decl != error_mark_node)
3918 {
3919 result = finish_call_expr (decl, &args, false, true, tf_none);
3920 if (result != error_mark_node)
3921 {
3922 release_tree_vector (args);
3923 return result;
3924 }
3925 }
3926 release_tree_vector (args);
3927
3928 /* If the raw literal didn't work, look for a non-type template
3929 function with parameter pack char.... Call the function with
3930 template parameter characters representing the number. */
3931 args = make_tree_vector ();
3932 decl = lookup_literal_operator (name, args);
3933 if (decl && decl != error_mark_node)
3934 {
3935 tree tmpl_args = make_char_string_pack (num_string);
3936 decl = lookup_template_function (decl, tmpl_args);
3937 result = finish_call_expr (decl, &args, false, true, tf_none);
3938 if (result != error_mark_node)
3939 {
3940 release_tree_vector (args);
3941 return result;
3942 }
3943 }
3944 release_tree_vector (args);
3945
3946 error ("unable to find numeric literal operator %qD", name);
3947 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3948 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3949 "to enable more built-in suffixes");
3950 return error_mark_node;
3951 }
3952
3953 /* Parse a user-defined string constant. Returns a call to a user-defined
3954 literal operator taking a character pointer and the length of the string
3955 as arguments. */
3956
3957 static tree
3958 cp_parser_userdef_string_literal (cp_token *token)
3959 {
3960 tree literal = token->u.value;
3961 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3962 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3963 tree value = USERDEF_LITERAL_VALUE (literal);
3964 int len = TREE_STRING_LENGTH (value)
3965 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3966 tree decl, result;
3967 vec<tree, va_gc> *args;
3968
3969 /* Look for a template function with typename parameter CharT
3970 and parameter pack CharT... Call the function with
3971 template parameter characters representing the string. */
3972 args = make_tree_vector ();
3973 decl = lookup_literal_operator (name, args);
3974 if (decl && decl != error_mark_node)
3975 {
3976 tree tmpl_args = make_string_pack (value);
3977 decl = lookup_template_function (decl, tmpl_args);
3978 result = finish_call_expr (decl, &args, false, true, tf_none);
3979 if (result != error_mark_node)
3980 {
3981 release_tree_vector (args);
3982 return result;
3983 }
3984 }
3985 release_tree_vector (args);
3986
3987 /* Build up a call to the user-defined operator */
3988 /* Lookup the name we got back from the id-expression. */
3989 args = make_tree_vector ();
3990 vec_safe_push (args, value);
3991 vec_safe_push (args, build_int_cst (size_type_node, len));
3992 decl = lookup_name (name);
3993 if (!decl || decl == error_mark_node)
3994 {
3995 error ("unable to find string literal operator %qD", name);
3996 release_tree_vector (args);
3997 return error_mark_node;
3998 }
3999 result = finish_call_expr (decl, &args, false, true, tf_none);
4000 release_tree_vector (args);
4001 if (result != error_mark_node)
4002 return result;
4003
4004 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4005 name, TREE_TYPE (value), size_type_node);
4006 return error_mark_node;
4007 }
4008
4009
4010 /* Basic concepts [gram.basic] */
4011
4012 /* Parse a translation-unit.
4013
4014 translation-unit:
4015 declaration-seq [opt]
4016
4017 Returns TRUE if all went well. */
4018
4019 static bool
4020 cp_parser_translation_unit (cp_parser* parser)
4021 {
4022 /* The address of the first non-permanent object on the declarator
4023 obstack. */
4024 static void *declarator_obstack_base;
4025
4026 bool success;
4027
4028 /* Create the declarator obstack, if necessary. */
4029 if (!cp_error_declarator)
4030 {
4031 gcc_obstack_init (&declarator_obstack);
4032 /* Create the error declarator. */
4033 cp_error_declarator = make_declarator (cdk_error);
4034 /* Create the empty parameter list. */
4035 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4036 /* Remember where the base of the declarator obstack lies. */
4037 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4038 }
4039
4040 cp_parser_declaration_seq_opt (parser);
4041
4042 /* If there are no tokens left then all went well. */
4043 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4044 {
4045 /* Get rid of the token array; we don't need it any more. */
4046 cp_lexer_destroy (parser->lexer);
4047 parser->lexer = NULL;
4048
4049 /* This file might have been a context that's implicitly extern
4050 "C". If so, pop the lang context. (Only relevant for PCH.) */
4051 if (parser->implicit_extern_c)
4052 {
4053 pop_lang_context ();
4054 parser->implicit_extern_c = false;
4055 }
4056
4057 /* Finish up. */
4058 finish_translation_unit ();
4059
4060 success = true;
4061 }
4062 else
4063 {
4064 cp_parser_error (parser, "expected declaration");
4065 success = false;
4066 }
4067
4068 /* Make sure the declarator obstack was fully cleaned up. */
4069 gcc_assert (obstack_next_free (&declarator_obstack)
4070 == declarator_obstack_base);
4071
4072 /* All went well. */
4073 return success;
4074 }
4075
4076 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4077 decltype context. */
4078
4079 static inline tsubst_flags_t
4080 complain_flags (bool decltype_p)
4081 {
4082 tsubst_flags_t complain = tf_warning_or_error;
4083 if (decltype_p)
4084 complain |= tf_decltype;
4085 return complain;
4086 }
4087
4088 /* Expressions [gram.expr] */
4089
4090 /* Parse a primary-expression.
4091
4092 primary-expression:
4093 literal
4094 this
4095 ( expression )
4096 id-expression
4097
4098 GNU Extensions:
4099
4100 primary-expression:
4101 ( compound-statement )
4102 __builtin_va_arg ( assignment-expression , type-id )
4103 __builtin_offsetof ( type-id , offsetof-expression )
4104
4105 C++ Extensions:
4106 __has_nothrow_assign ( type-id )
4107 __has_nothrow_constructor ( type-id )
4108 __has_nothrow_copy ( type-id )
4109 __has_trivial_assign ( type-id )
4110 __has_trivial_constructor ( type-id )
4111 __has_trivial_copy ( type-id )
4112 __has_trivial_destructor ( type-id )
4113 __has_virtual_destructor ( type-id )
4114 __is_abstract ( type-id )
4115 __is_base_of ( type-id , type-id )
4116 __is_class ( type-id )
4117 __is_convertible_to ( type-id , type-id )
4118 __is_empty ( type-id )
4119 __is_enum ( type-id )
4120 __is_final ( type-id )
4121 __is_literal_type ( type-id )
4122 __is_pod ( type-id )
4123 __is_polymorphic ( type-id )
4124 __is_std_layout ( type-id )
4125 __is_trivial ( type-id )
4126 __is_union ( type-id )
4127
4128 Objective-C++ Extension:
4129
4130 primary-expression:
4131 objc-expression
4132
4133 literal:
4134 __null
4135
4136 ADDRESS_P is true iff this expression was immediately preceded by
4137 "&" and therefore might denote a pointer-to-member. CAST_P is true
4138 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4139 true iff this expression is a template argument.
4140
4141 Returns a representation of the expression. Upon return, *IDK
4142 indicates what kind of id-expression (if any) was present. */
4143
4144 static tree
4145 cp_parser_primary_expression (cp_parser *parser,
4146 bool address_p,
4147 bool cast_p,
4148 bool template_arg_p,
4149 bool decltype_p,
4150 cp_id_kind *idk)
4151 {
4152 cp_token *token = NULL;
4153
4154 /* Assume the primary expression is not an id-expression. */
4155 *idk = CP_ID_KIND_NONE;
4156
4157 /* Peek at the next token. */
4158 token = cp_lexer_peek_token (parser->lexer);
4159 switch (token->type)
4160 {
4161 /* literal:
4162 integer-literal
4163 character-literal
4164 floating-literal
4165 string-literal
4166 boolean-literal
4167 pointer-literal
4168 user-defined-literal */
4169 case CPP_CHAR:
4170 case CPP_CHAR16:
4171 case CPP_CHAR32:
4172 case CPP_WCHAR:
4173 case CPP_NUMBER:
4174 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4175 return cp_parser_userdef_numeric_literal (parser);
4176 token = cp_lexer_consume_token (parser->lexer);
4177 if (TREE_CODE (token->u.value) == FIXED_CST)
4178 {
4179 error_at (token->location,
4180 "fixed-point types not supported in C++");
4181 return error_mark_node;
4182 }
4183 /* Floating-point literals are only allowed in an integral
4184 constant expression if they are cast to an integral or
4185 enumeration type. */
4186 if (TREE_CODE (token->u.value) == REAL_CST
4187 && parser->integral_constant_expression_p
4188 && pedantic)
4189 {
4190 /* CAST_P will be set even in invalid code like "int(2.7 +
4191 ...)". Therefore, we have to check that the next token
4192 is sure to end the cast. */
4193 if (cast_p)
4194 {
4195 cp_token *next_token;
4196
4197 next_token = cp_lexer_peek_token (parser->lexer);
4198 if (/* The comma at the end of an
4199 enumerator-definition. */
4200 next_token->type != CPP_COMMA
4201 /* The curly brace at the end of an enum-specifier. */
4202 && next_token->type != CPP_CLOSE_BRACE
4203 /* The end of a statement. */
4204 && next_token->type != CPP_SEMICOLON
4205 /* The end of the cast-expression. */
4206 && next_token->type != CPP_CLOSE_PAREN
4207 /* The end of an array bound. */
4208 && next_token->type != CPP_CLOSE_SQUARE
4209 /* The closing ">" in a template-argument-list. */
4210 && (next_token->type != CPP_GREATER
4211 || parser->greater_than_is_operator_p)
4212 /* C++0x only: A ">>" treated like two ">" tokens,
4213 in a template-argument-list. */
4214 && (next_token->type != CPP_RSHIFT
4215 || (cxx_dialect == cxx98)
4216 || parser->greater_than_is_operator_p))
4217 cast_p = false;
4218 }
4219
4220 /* If we are within a cast, then the constraint that the
4221 cast is to an integral or enumeration type will be
4222 checked at that point. If we are not within a cast, then
4223 this code is invalid. */
4224 if (!cast_p)
4225 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4226 }
4227 return token->u.value;
4228
4229 case CPP_CHAR_USERDEF:
4230 case CPP_CHAR16_USERDEF:
4231 case CPP_CHAR32_USERDEF:
4232 case CPP_WCHAR_USERDEF:
4233 return cp_parser_userdef_char_literal (parser);
4234
4235 case CPP_STRING:
4236 case CPP_STRING16:
4237 case CPP_STRING32:
4238 case CPP_WSTRING:
4239 case CPP_UTF8STRING:
4240 case CPP_STRING_USERDEF:
4241 case CPP_STRING16_USERDEF:
4242 case CPP_STRING32_USERDEF:
4243 case CPP_WSTRING_USERDEF:
4244 case CPP_UTF8STRING_USERDEF:
4245 /* ??? Should wide strings be allowed when parser->translate_strings_p
4246 is false (i.e. in attributes)? If not, we can kill the third
4247 argument to cp_parser_string_literal. */
4248 return cp_parser_string_literal (parser,
4249 parser->translate_strings_p,
4250 true);
4251
4252 case CPP_OPEN_PAREN:
4253 {
4254 tree expr;
4255 bool saved_greater_than_is_operator_p;
4256
4257 /* Consume the `('. */
4258 cp_lexer_consume_token (parser->lexer);
4259 /* Within a parenthesized expression, a `>' token is always
4260 the greater-than operator. */
4261 saved_greater_than_is_operator_p
4262 = parser->greater_than_is_operator_p;
4263 parser->greater_than_is_operator_p = true;
4264 /* If we see `( { ' then we are looking at the beginning of
4265 a GNU statement-expression. */
4266 if (cp_parser_allow_gnu_extensions_p (parser)
4267 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4268 {
4269 /* Statement-expressions are not allowed by the standard. */
4270 pedwarn (token->location, OPT_Wpedantic,
4271 "ISO C++ forbids braced-groups within expressions");
4272
4273 /* And they're not allowed outside of a function-body; you
4274 cannot, for example, write:
4275
4276 int i = ({ int j = 3; j + 1; });
4277
4278 at class or namespace scope. */
4279 if (!parser->in_function_body
4280 || parser->in_template_argument_list_p)
4281 {
4282 error_at (token->location,
4283 "statement-expressions are not allowed outside "
4284 "functions nor in template-argument lists");
4285 cp_parser_skip_to_end_of_block_or_statement (parser);
4286 expr = error_mark_node;
4287 }
4288 else
4289 {
4290 /* Start the statement-expression. */
4291 expr = begin_stmt_expr ();
4292 /* Parse the compound-statement. */
4293 cp_parser_compound_statement (parser, expr, false, false);
4294 /* Finish up. */
4295 expr = finish_stmt_expr (expr, false);
4296 }
4297 }
4298 else
4299 {
4300 /* Parse the parenthesized expression. */
4301 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4302 /* Let the front end know that this expression was
4303 enclosed in parentheses. This matters in case, for
4304 example, the expression is of the form `A::B', since
4305 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4306 not. */
4307 expr = finish_parenthesized_expr (expr);
4308 /* DR 705: Wrapping an unqualified name in parentheses
4309 suppresses arg-dependent lookup. We want to pass back
4310 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4311 (c++/37862), but none of the others. */
4312 if (*idk != CP_ID_KIND_QUALIFIED)
4313 *idk = CP_ID_KIND_NONE;
4314 }
4315 /* The `>' token might be the end of a template-id or
4316 template-parameter-list now. */
4317 parser->greater_than_is_operator_p
4318 = saved_greater_than_is_operator_p;
4319 /* Consume the `)'. */
4320 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4321 cp_parser_skip_to_end_of_statement (parser);
4322
4323 return expr;
4324 }
4325
4326 case CPP_OPEN_SQUARE:
4327 if (c_dialect_objc ())
4328 /* We have an Objective-C++ message. */
4329 return cp_parser_objc_expression (parser);
4330 {
4331 tree lam = cp_parser_lambda_expression (parser);
4332 /* Don't warn about a failed tentative parse. */
4333 if (cp_parser_error_occurred (parser))
4334 return error_mark_node;
4335 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4336 return lam;
4337 }
4338
4339 case CPP_OBJC_STRING:
4340 if (c_dialect_objc ())
4341 /* We have an Objective-C++ string literal. */
4342 return cp_parser_objc_expression (parser);
4343 cp_parser_error (parser, "expected primary-expression");
4344 return error_mark_node;
4345
4346 case CPP_KEYWORD:
4347 switch (token->keyword)
4348 {
4349 /* These two are the boolean literals. */
4350 case RID_TRUE:
4351 cp_lexer_consume_token (parser->lexer);
4352 return boolean_true_node;
4353 case RID_FALSE:
4354 cp_lexer_consume_token (parser->lexer);
4355 return boolean_false_node;
4356
4357 /* The `__null' literal. */
4358 case RID_NULL:
4359 cp_lexer_consume_token (parser->lexer);
4360 return null_node;
4361
4362 /* The `nullptr' literal. */
4363 case RID_NULLPTR:
4364 cp_lexer_consume_token (parser->lexer);
4365 return nullptr_node;
4366
4367 /* Recognize the `this' keyword. */
4368 case RID_THIS:
4369 cp_lexer_consume_token (parser->lexer);
4370 if (parser->local_variables_forbidden_p)
4371 {
4372 error_at (token->location,
4373 "%<this%> may not be used in this context");
4374 return error_mark_node;
4375 }
4376 /* Pointers cannot appear in constant-expressions. */
4377 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4378 return error_mark_node;
4379 return finish_this_expr ();
4380
4381 /* The `operator' keyword can be the beginning of an
4382 id-expression. */
4383 case RID_OPERATOR:
4384 goto id_expression;
4385
4386 case RID_FUNCTION_NAME:
4387 case RID_PRETTY_FUNCTION_NAME:
4388 case RID_C99_FUNCTION_NAME:
4389 {
4390 non_integral_constant name;
4391
4392 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4393 __func__ are the names of variables -- but they are
4394 treated specially. Therefore, they are handled here,
4395 rather than relying on the generic id-expression logic
4396 below. Grammatically, these names are id-expressions.
4397
4398 Consume the token. */
4399 token = cp_lexer_consume_token (parser->lexer);
4400
4401 switch (token->keyword)
4402 {
4403 case RID_FUNCTION_NAME:
4404 name = NIC_FUNC_NAME;
4405 break;
4406 case RID_PRETTY_FUNCTION_NAME:
4407 name = NIC_PRETTY_FUNC;
4408 break;
4409 case RID_C99_FUNCTION_NAME:
4410 name = NIC_C99_FUNC;
4411 break;
4412 default:
4413 gcc_unreachable ();
4414 }
4415
4416 if (cp_parser_non_integral_constant_expression (parser, name))
4417 return error_mark_node;
4418
4419 /* Look up the name. */
4420 return finish_fname (token->u.value);
4421 }
4422
4423 case RID_VA_ARG:
4424 {
4425 tree expression;
4426 tree type;
4427 source_location type_location;
4428
4429 /* The `__builtin_va_arg' construct is used to handle
4430 `va_arg'. Consume the `__builtin_va_arg' token. */
4431 cp_lexer_consume_token (parser->lexer);
4432 /* Look for the opening `('. */
4433 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4434 /* Now, parse the assignment-expression. */
4435 expression = cp_parser_assignment_expression (parser,
4436 /*cast_p=*/false, NULL);
4437 /* Look for the `,'. */
4438 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4439 type_location = cp_lexer_peek_token (parser->lexer)->location;
4440 /* Parse the type-id. */
4441 type = cp_parser_type_id (parser);
4442 /* Look for the closing `)'. */
4443 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4444 /* Using `va_arg' in a constant-expression is not
4445 allowed. */
4446 if (cp_parser_non_integral_constant_expression (parser,
4447 NIC_VA_ARG))
4448 return error_mark_node;
4449 return build_x_va_arg (type_location, expression, type);
4450 }
4451
4452 case RID_OFFSETOF:
4453 return cp_parser_builtin_offsetof (parser);
4454
4455 case RID_HAS_NOTHROW_ASSIGN:
4456 case RID_HAS_NOTHROW_CONSTRUCTOR:
4457 case RID_HAS_NOTHROW_COPY:
4458 case RID_HAS_TRIVIAL_ASSIGN:
4459 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4460 case RID_HAS_TRIVIAL_COPY:
4461 case RID_HAS_TRIVIAL_DESTRUCTOR:
4462 case RID_HAS_VIRTUAL_DESTRUCTOR:
4463 case RID_IS_ABSTRACT:
4464 case RID_IS_BASE_OF:
4465 case RID_IS_CLASS:
4466 case RID_IS_CONVERTIBLE_TO:
4467 case RID_IS_EMPTY:
4468 case RID_IS_ENUM:
4469 case RID_IS_FINAL:
4470 case RID_IS_LITERAL_TYPE:
4471 case RID_IS_POD:
4472 case RID_IS_POLYMORPHIC:
4473 case RID_IS_STD_LAYOUT:
4474 case RID_IS_TRIVIAL:
4475 case RID_IS_UNION:
4476 return cp_parser_trait_expr (parser, token->keyword);
4477
4478 /* Objective-C++ expressions. */
4479 case RID_AT_ENCODE:
4480 case RID_AT_PROTOCOL:
4481 case RID_AT_SELECTOR:
4482 return cp_parser_objc_expression (parser);
4483
4484 case RID_TEMPLATE:
4485 if (parser->in_function_body
4486 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4487 == CPP_LESS))
4488 {
4489 error_at (token->location,
4490 "a template declaration cannot appear at block scope");
4491 cp_parser_skip_to_end_of_block_or_statement (parser);
4492 return error_mark_node;
4493 }
4494 default:
4495 cp_parser_error (parser, "expected primary-expression");
4496 return error_mark_node;
4497 }
4498
4499 /* An id-expression can start with either an identifier, a
4500 `::' as the beginning of a qualified-id, or the "operator"
4501 keyword. */
4502 case CPP_NAME:
4503 case CPP_SCOPE:
4504 case CPP_TEMPLATE_ID:
4505 case CPP_NESTED_NAME_SPECIFIER:
4506 {
4507 tree id_expression;
4508 tree decl;
4509 const char *error_msg;
4510 bool template_p;
4511 bool done;
4512 cp_token *id_expr_token;
4513
4514 id_expression:
4515 /* Parse the id-expression. */
4516 id_expression
4517 = cp_parser_id_expression (parser,
4518 /*template_keyword_p=*/false,
4519 /*check_dependency_p=*/true,
4520 &template_p,
4521 /*declarator_p=*/false,
4522 /*optional_p=*/false);
4523 if (id_expression == error_mark_node)
4524 return error_mark_node;
4525 id_expr_token = token;
4526 token = cp_lexer_peek_token (parser->lexer);
4527 done = (token->type != CPP_OPEN_SQUARE
4528 && token->type != CPP_OPEN_PAREN
4529 && token->type != CPP_DOT
4530 && token->type != CPP_DEREF
4531 && token->type != CPP_PLUS_PLUS
4532 && token->type != CPP_MINUS_MINUS);
4533 /* If we have a template-id, then no further lookup is
4534 required. If the template-id was for a template-class, we
4535 will sometimes have a TYPE_DECL at this point. */
4536 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4537 || TREE_CODE (id_expression) == TYPE_DECL)
4538 decl = id_expression;
4539 /* Look up the name. */
4540 else
4541 {
4542 tree ambiguous_decls;
4543
4544 /* If we already know that this lookup is ambiguous, then
4545 we've already issued an error message; there's no reason
4546 to check again. */
4547 if (id_expr_token->type == CPP_NAME
4548 && id_expr_token->error_reported)
4549 {
4550 cp_parser_simulate_error (parser);
4551 return error_mark_node;
4552 }
4553
4554 decl = cp_parser_lookup_name (parser, id_expression,
4555 none_type,
4556 template_p,
4557 /*is_namespace=*/false,
4558 /*check_dependency=*/true,
4559 &ambiguous_decls,
4560 id_expr_token->location);
4561 /* If the lookup was ambiguous, an error will already have
4562 been issued. */
4563 if (ambiguous_decls)
4564 return error_mark_node;
4565
4566 /* In Objective-C++, we may have an Objective-C 2.0
4567 dot-syntax for classes here. */
4568 if (c_dialect_objc ()
4569 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4570 && TREE_CODE (decl) == TYPE_DECL
4571 && objc_is_class_name (decl))
4572 {
4573 tree component;
4574 cp_lexer_consume_token (parser->lexer);
4575 component = cp_parser_identifier (parser);
4576 if (component == error_mark_node)
4577 return error_mark_node;
4578
4579 return objc_build_class_component_ref (id_expression, component);
4580 }
4581
4582 /* In Objective-C++, an instance variable (ivar) may be preferred
4583 to whatever cp_parser_lookup_name() found. */
4584 decl = objc_lookup_ivar (decl, id_expression);
4585
4586 /* If name lookup gives us a SCOPE_REF, then the
4587 qualifying scope was dependent. */
4588 if (TREE_CODE (decl) == SCOPE_REF)
4589 {
4590 /* At this point, we do not know if DECL is a valid
4591 integral constant expression. We assume that it is
4592 in fact such an expression, so that code like:
4593
4594 template <int N> struct A {
4595 int a[B<N>::i];
4596 };
4597
4598 is accepted. At template-instantiation time, we
4599 will check that B<N>::i is actually a constant. */
4600 return decl;
4601 }
4602 /* Check to see if DECL is a local variable in a context
4603 where that is forbidden. */
4604 if (parser->local_variables_forbidden_p
4605 && local_variable_p (decl))
4606 {
4607 /* It might be that we only found DECL because we are
4608 trying to be generous with pre-ISO scoping rules.
4609 For example, consider:
4610
4611 int i;
4612 void g() {
4613 for (int i = 0; i < 10; ++i) {}
4614 extern void f(int j = i);
4615 }
4616
4617 Here, name look up will originally find the out
4618 of scope `i'. We need to issue a warning message,
4619 but then use the global `i'. */
4620 decl = check_for_out_of_scope_variable (decl);
4621 if (local_variable_p (decl))
4622 {
4623 error_at (id_expr_token->location,
4624 "local variable %qD may not appear in this context",
4625 decl);
4626 return error_mark_node;
4627 }
4628 }
4629 }
4630
4631 decl = (finish_id_expression
4632 (id_expression, decl, parser->scope,
4633 idk,
4634 parser->integral_constant_expression_p,
4635 parser->allow_non_integral_constant_expression_p,
4636 &parser->non_integral_constant_expression_p,
4637 template_p, done, address_p,
4638 template_arg_p,
4639 &error_msg,
4640 id_expr_token->location));
4641 if (error_msg)
4642 cp_parser_error (parser, error_msg);
4643 return decl;
4644 }
4645
4646 /* Anything else is an error. */
4647 default:
4648 cp_parser_error (parser, "expected primary-expression");
4649 return error_mark_node;
4650 }
4651 }
4652
4653 static inline tree
4654 cp_parser_primary_expression (cp_parser *parser,
4655 bool address_p,
4656 bool cast_p,
4657 bool template_arg_p,
4658 cp_id_kind *idk)
4659 {
4660 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4661 /*decltype*/false, idk);
4662 }
4663
4664 /* Parse an id-expression.
4665
4666 id-expression:
4667 unqualified-id
4668 qualified-id
4669
4670 qualified-id:
4671 :: [opt] nested-name-specifier template [opt] unqualified-id
4672 :: identifier
4673 :: operator-function-id
4674 :: template-id
4675
4676 Return a representation of the unqualified portion of the
4677 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4678 a `::' or nested-name-specifier.
4679
4680 Often, if the id-expression was a qualified-id, the caller will
4681 want to make a SCOPE_REF to represent the qualified-id. This
4682 function does not do this in order to avoid wastefully creating
4683 SCOPE_REFs when they are not required.
4684
4685 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4686 `template' keyword.
4687
4688 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4689 uninstantiated templates.
4690
4691 If *TEMPLATE_P is non-NULL, it is set to true iff the
4692 `template' keyword is used to explicitly indicate that the entity
4693 named is a template.
4694
4695 If DECLARATOR_P is true, the id-expression is appearing as part of
4696 a declarator, rather than as part of an expression. */
4697
4698 static tree
4699 cp_parser_id_expression (cp_parser *parser,
4700 bool template_keyword_p,
4701 bool check_dependency_p,
4702 bool *template_p,
4703 bool declarator_p,
4704 bool optional_p)
4705 {
4706 bool global_scope_p;
4707 bool nested_name_specifier_p;
4708
4709 /* Assume the `template' keyword was not used. */
4710 if (template_p)
4711 *template_p = template_keyword_p;
4712
4713 /* Look for the optional `::' operator. */
4714 global_scope_p
4715 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4716 != NULL_TREE);
4717 /* Look for the optional nested-name-specifier. */
4718 nested_name_specifier_p
4719 = (cp_parser_nested_name_specifier_opt (parser,
4720 /*typename_keyword_p=*/false,
4721 check_dependency_p,
4722 /*type_p=*/false,
4723 declarator_p)
4724 != NULL_TREE);
4725 /* If there is a nested-name-specifier, then we are looking at
4726 the first qualified-id production. */
4727 if (nested_name_specifier_p)
4728 {
4729 tree saved_scope;
4730 tree saved_object_scope;
4731 tree saved_qualifying_scope;
4732 tree unqualified_id;
4733 bool is_template;
4734
4735 /* See if the next token is the `template' keyword. */
4736 if (!template_p)
4737 template_p = &is_template;
4738 *template_p = cp_parser_optional_template_keyword (parser);
4739 /* Name lookup we do during the processing of the
4740 unqualified-id might obliterate SCOPE. */
4741 saved_scope = parser->scope;
4742 saved_object_scope = parser->object_scope;
4743 saved_qualifying_scope = parser->qualifying_scope;
4744 /* Process the final unqualified-id. */
4745 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4746 check_dependency_p,
4747 declarator_p,
4748 /*optional_p=*/false);
4749 /* Restore the SAVED_SCOPE for our caller. */
4750 parser->scope = saved_scope;
4751 parser->object_scope = saved_object_scope;
4752 parser->qualifying_scope = saved_qualifying_scope;
4753
4754 return unqualified_id;
4755 }
4756 /* Otherwise, if we are in global scope, then we are looking at one
4757 of the other qualified-id productions. */
4758 else if (global_scope_p)
4759 {
4760 cp_token *token;
4761 tree id;
4762
4763 /* Peek at the next token. */
4764 token = cp_lexer_peek_token (parser->lexer);
4765
4766 /* If it's an identifier, and the next token is not a "<", then
4767 we can avoid the template-id case. This is an optimization
4768 for this common case. */
4769 if (token->type == CPP_NAME
4770 && !cp_parser_nth_token_starts_template_argument_list_p
4771 (parser, 2))
4772 return cp_parser_identifier (parser);
4773
4774 cp_parser_parse_tentatively (parser);
4775 /* Try a template-id. */
4776 id = cp_parser_template_id (parser,
4777 /*template_keyword_p=*/false,
4778 /*check_dependency_p=*/true,
4779 none_type,
4780 declarator_p);
4781 /* If that worked, we're done. */
4782 if (cp_parser_parse_definitely (parser))
4783 return id;
4784
4785 /* Peek at the next token. (Changes in the token buffer may
4786 have invalidated the pointer obtained above.) */
4787 token = cp_lexer_peek_token (parser->lexer);
4788
4789 switch (token->type)
4790 {
4791 case CPP_NAME:
4792 return cp_parser_identifier (parser);
4793
4794 case CPP_KEYWORD:
4795 if (token->keyword == RID_OPERATOR)
4796 return cp_parser_operator_function_id (parser);
4797 /* Fall through. */
4798
4799 default:
4800 cp_parser_error (parser, "expected id-expression");
4801 return error_mark_node;
4802 }
4803 }
4804 else
4805 return cp_parser_unqualified_id (parser, template_keyword_p,
4806 /*check_dependency_p=*/true,
4807 declarator_p,
4808 optional_p);
4809 }
4810
4811 /* Parse an unqualified-id.
4812
4813 unqualified-id:
4814 identifier
4815 operator-function-id
4816 conversion-function-id
4817 ~ class-name
4818 template-id
4819
4820 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4821 keyword, in a construct like `A::template ...'.
4822
4823 Returns a representation of unqualified-id. For the `identifier'
4824 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4825 production a BIT_NOT_EXPR is returned; the operand of the
4826 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4827 other productions, see the documentation accompanying the
4828 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4829 names are looked up in uninstantiated templates. If DECLARATOR_P
4830 is true, the unqualified-id is appearing as part of a declarator,
4831 rather than as part of an expression. */
4832
4833 static tree
4834 cp_parser_unqualified_id (cp_parser* parser,
4835 bool template_keyword_p,
4836 bool check_dependency_p,
4837 bool declarator_p,
4838 bool optional_p)
4839 {
4840 cp_token *token;
4841
4842 /* Peek at the next token. */
4843 token = cp_lexer_peek_token (parser->lexer);
4844
4845 switch (token->type)
4846 {
4847 case CPP_NAME:
4848 {
4849 tree id;
4850
4851 /* We don't know yet whether or not this will be a
4852 template-id. */
4853 cp_parser_parse_tentatively (parser);
4854 /* Try a template-id. */
4855 id = cp_parser_template_id (parser, template_keyword_p,
4856 check_dependency_p,
4857 none_type,
4858 declarator_p);
4859 /* If it worked, we're done. */
4860 if (cp_parser_parse_definitely (parser))
4861 return id;
4862 /* Otherwise, it's an ordinary identifier. */
4863 return cp_parser_identifier (parser);
4864 }
4865
4866 case CPP_TEMPLATE_ID:
4867 return cp_parser_template_id (parser, template_keyword_p,
4868 check_dependency_p,
4869 none_type,
4870 declarator_p);
4871
4872 case CPP_COMPL:
4873 {
4874 tree type_decl;
4875 tree qualifying_scope;
4876 tree object_scope;
4877 tree scope;
4878 bool done;
4879
4880 /* Consume the `~' token. */
4881 cp_lexer_consume_token (parser->lexer);
4882 /* Parse the class-name. The standard, as written, seems to
4883 say that:
4884
4885 template <typename T> struct S { ~S (); };
4886 template <typename T> S<T>::~S() {}
4887
4888 is invalid, since `~' must be followed by a class-name, but
4889 `S<T>' is dependent, and so not known to be a class.
4890 That's not right; we need to look in uninstantiated
4891 templates. A further complication arises from:
4892
4893 template <typename T> void f(T t) {
4894 t.T::~T();
4895 }
4896
4897 Here, it is not possible to look up `T' in the scope of `T'
4898 itself. We must look in both the current scope, and the
4899 scope of the containing complete expression.
4900
4901 Yet another issue is:
4902
4903 struct S {
4904 int S;
4905 ~S();
4906 };
4907
4908 S::~S() {}
4909
4910 The standard does not seem to say that the `S' in `~S'
4911 should refer to the type `S' and not the data member
4912 `S::S'. */
4913
4914 /* DR 244 says that we look up the name after the "~" in the
4915 same scope as we looked up the qualifying name. That idea
4916 isn't fully worked out; it's more complicated than that. */
4917 scope = parser->scope;
4918 object_scope = parser->object_scope;
4919 qualifying_scope = parser->qualifying_scope;
4920
4921 /* Check for invalid scopes. */
4922 if (scope == error_mark_node)
4923 {
4924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4925 cp_lexer_consume_token (parser->lexer);
4926 return error_mark_node;
4927 }
4928 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4929 {
4930 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4931 error_at (token->location,
4932 "scope %qT before %<~%> is not a class-name",
4933 scope);
4934 cp_parser_simulate_error (parser);
4935 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4936 cp_lexer_consume_token (parser->lexer);
4937 return error_mark_node;
4938 }
4939 gcc_assert (!scope || TYPE_P (scope));
4940
4941 /* If the name is of the form "X::~X" it's OK even if X is a
4942 typedef. */
4943 token = cp_lexer_peek_token (parser->lexer);
4944 if (scope
4945 && token->type == CPP_NAME
4946 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4947 != CPP_LESS)
4948 && (token->u.value == TYPE_IDENTIFIER (scope)
4949 || (CLASS_TYPE_P (scope)
4950 && constructor_name_p (token->u.value, scope))))
4951 {
4952 cp_lexer_consume_token (parser->lexer);
4953 return build_nt (BIT_NOT_EXPR, scope);
4954 }
4955
4956 /* ~auto means the destructor of whatever the object is. */
4957 if (cp_parser_is_keyword (token, RID_AUTO))
4958 {
4959 if (cxx_dialect < cxx1y)
4960 pedwarn (input_location, 0,
4961 "%<~auto%> only available with "
4962 "-std=c++1y or -std=gnu++1y");
4963 cp_lexer_consume_token (parser->lexer);
4964 return build_nt (BIT_NOT_EXPR, make_auto ());
4965 }
4966
4967 /* If there was an explicit qualification (S::~T), first look
4968 in the scope given by the qualification (i.e., S).
4969
4970 Note: in the calls to cp_parser_class_name below we pass
4971 typename_type so that lookup finds the injected-class-name
4972 rather than the constructor. */
4973 done = false;
4974 type_decl = NULL_TREE;
4975 if (scope)
4976 {
4977 cp_parser_parse_tentatively (parser);
4978 type_decl = cp_parser_class_name (parser,
4979 /*typename_keyword_p=*/false,
4980 /*template_keyword_p=*/false,
4981 typename_type,
4982 /*check_dependency=*/false,
4983 /*class_head_p=*/false,
4984 declarator_p);
4985 if (cp_parser_parse_definitely (parser))
4986 done = true;
4987 }
4988 /* In "N::S::~S", look in "N" as well. */
4989 if (!done && scope && qualifying_scope)
4990 {
4991 cp_parser_parse_tentatively (parser);
4992 parser->scope = qualifying_scope;
4993 parser->object_scope = NULL_TREE;
4994 parser->qualifying_scope = NULL_TREE;
4995 type_decl
4996 = cp_parser_class_name (parser,
4997 /*typename_keyword_p=*/false,
4998 /*template_keyword_p=*/false,
4999 typename_type,
5000 /*check_dependency=*/false,
5001 /*class_head_p=*/false,
5002 declarator_p);
5003 if (cp_parser_parse_definitely (parser))
5004 done = true;
5005 }
5006 /* In "p->S::~T", look in the scope given by "*p" as well. */
5007 else if (!done && object_scope)
5008 {
5009 cp_parser_parse_tentatively (parser);
5010 parser->scope = object_scope;
5011 parser->object_scope = NULL_TREE;
5012 parser->qualifying_scope = NULL_TREE;
5013 type_decl
5014 = cp_parser_class_name (parser,
5015 /*typename_keyword_p=*/false,
5016 /*template_keyword_p=*/false,
5017 typename_type,
5018 /*check_dependency=*/false,
5019 /*class_head_p=*/false,
5020 declarator_p);
5021 if (cp_parser_parse_definitely (parser))
5022 done = true;
5023 }
5024 /* Look in the surrounding context. */
5025 if (!done)
5026 {
5027 parser->scope = NULL_TREE;
5028 parser->object_scope = NULL_TREE;
5029 parser->qualifying_scope = NULL_TREE;
5030 if (processing_template_decl)
5031 cp_parser_parse_tentatively (parser);
5032 type_decl
5033 = cp_parser_class_name (parser,
5034 /*typename_keyword_p=*/false,
5035 /*template_keyword_p=*/false,
5036 typename_type,
5037 /*check_dependency=*/false,
5038 /*class_head_p=*/false,
5039 declarator_p);
5040 if (processing_template_decl
5041 && ! cp_parser_parse_definitely (parser))
5042 {
5043 /* We couldn't find a type with this name, so just accept
5044 it and check for a match at instantiation time. */
5045 type_decl = cp_parser_identifier (parser);
5046 if (type_decl != error_mark_node)
5047 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5048 return type_decl;
5049 }
5050 }
5051 /* If an error occurred, assume that the name of the
5052 destructor is the same as the name of the qualifying
5053 class. That allows us to keep parsing after running
5054 into ill-formed destructor names. */
5055 if (type_decl == error_mark_node && scope)
5056 return build_nt (BIT_NOT_EXPR, scope);
5057 else if (type_decl == error_mark_node)
5058 return error_mark_node;
5059
5060 /* Check that destructor name and scope match. */
5061 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5062 {
5063 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5064 error_at (token->location,
5065 "declaration of %<~%T%> as member of %qT",
5066 type_decl, scope);
5067 cp_parser_simulate_error (parser);
5068 return error_mark_node;
5069 }
5070
5071 /* [class.dtor]
5072
5073 A typedef-name that names a class shall not be used as the
5074 identifier in the declarator for a destructor declaration. */
5075 if (declarator_p
5076 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5077 && !DECL_SELF_REFERENCE_P (type_decl)
5078 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5079 error_at (token->location,
5080 "typedef-name %qD used as destructor declarator",
5081 type_decl);
5082
5083 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5084 }
5085
5086 case CPP_KEYWORD:
5087 if (token->keyword == RID_OPERATOR)
5088 {
5089 tree id;
5090
5091 /* This could be a template-id, so we try that first. */
5092 cp_parser_parse_tentatively (parser);
5093 /* Try a template-id. */
5094 id = cp_parser_template_id (parser, template_keyword_p,
5095 /*check_dependency_p=*/true,
5096 none_type,
5097 declarator_p);
5098 /* If that worked, we're done. */
5099 if (cp_parser_parse_definitely (parser))
5100 return id;
5101 /* We still don't know whether we're looking at an
5102 operator-function-id or a conversion-function-id. */
5103 cp_parser_parse_tentatively (parser);
5104 /* Try an operator-function-id. */
5105 id = cp_parser_operator_function_id (parser);
5106 /* If that didn't work, try a conversion-function-id. */
5107 if (!cp_parser_parse_definitely (parser))
5108 id = cp_parser_conversion_function_id (parser);
5109 else if (UDLIT_OPER_P (id))
5110 {
5111 /* 17.6.3.3.5 */
5112 const char *name = UDLIT_OP_SUFFIX (id);
5113 if (name[0] != '_' && !in_system_header_at (input_location)
5114 && declarator_p)
5115 warning (0, "literal operator suffixes not preceded by %<_%>"
5116 " are reserved for future standardization");
5117 }
5118
5119 return id;
5120 }
5121 /* Fall through. */
5122
5123 default:
5124 if (optional_p)
5125 return NULL_TREE;
5126 cp_parser_error (parser, "expected unqualified-id");
5127 return error_mark_node;
5128 }
5129 }
5130
5131 /* Parse an (optional) nested-name-specifier.
5132
5133 nested-name-specifier: [C++98]
5134 class-or-namespace-name :: nested-name-specifier [opt]
5135 class-or-namespace-name :: template nested-name-specifier [opt]
5136
5137 nested-name-specifier: [C++0x]
5138 type-name ::
5139 namespace-name ::
5140 nested-name-specifier identifier ::
5141 nested-name-specifier template [opt] simple-template-id ::
5142
5143 PARSER->SCOPE should be set appropriately before this function is
5144 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5145 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5146 in name lookups.
5147
5148 Sets PARSER->SCOPE to the class (TYPE) or namespace
5149 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5150 it unchanged if there is no nested-name-specifier. Returns the new
5151 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5152
5153 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5154 part of a declaration and/or decl-specifier. */
5155
5156 static tree
5157 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5158 bool typename_keyword_p,
5159 bool check_dependency_p,
5160 bool type_p,
5161 bool is_declaration)
5162 {
5163 bool success = false;
5164 cp_token_position start = 0;
5165 cp_token *token;
5166
5167 /* Remember where the nested-name-specifier starts. */
5168 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5169 {
5170 start = cp_lexer_token_position (parser->lexer, false);
5171 push_deferring_access_checks (dk_deferred);
5172 }
5173
5174 while (true)
5175 {
5176 tree new_scope;
5177 tree old_scope;
5178 tree saved_qualifying_scope;
5179 bool template_keyword_p;
5180
5181 /* Spot cases that cannot be the beginning of a
5182 nested-name-specifier. */
5183 token = cp_lexer_peek_token (parser->lexer);
5184
5185 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5186 the already parsed nested-name-specifier. */
5187 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5188 {
5189 /* Grab the nested-name-specifier and continue the loop. */
5190 cp_parser_pre_parsed_nested_name_specifier (parser);
5191 /* If we originally encountered this nested-name-specifier
5192 with IS_DECLARATION set to false, we will not have
5193 resolved TYPENAME_TYPEs, so we must do so here. */
5194 if (is_declaration
5195 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5196 {
5197 new_scope = resolve_typename_type (parser->scope,
5198 /*only_current_p=*/false);
5199 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5200 parser->scope = new_scope;
5201 }
5202 success = true;
5203 continue;
5204 }
5205
5206 /* Spot cases that cannot be the beginning of a
5207 nested-name-specifier. On the second and subsequent times
5208 through the loop, we look for the `template' keyword. */
5209 if (success && token->keyword == RID_TEMPLATE)
5210 ;
5211 /* A template-id can start a nested-name-specifier. */
5212 else if (token->type == CPP_TEMPLATE_ID)
5213 ;
5214 /* DR 743: decltype can be used in a nested-name-specifier. */
5215 else if (token_is_decltype (token))
5216 ;
5217 else
5218 {
5219 /* If the next token is not an identifier, then it is
5220 definitely not a type-name or namespace-name. */
5221 if (token->type != CPP_NAME)
5222 break;
5223 /* If the following token is neither a `<' (to begin a
5224 template-id), nor a `::', then we are not looking at a
5225 nested-name-specifier. */
5226 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5227
5228 if (token->type == CPP_COLON
5229 && parser->colon_corrects_to_scope_p
5230 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5231 {
5232 error_at (token->location,
5233 "found %<:%> in nested-name-specifier, expected %<::%>");
5234 token->type = CPP_SCOPE;
5235 }
5236
5237 if (token->type != CPP_SCOPE
5238 && !cp_parser_nth_token_starts_template_argument_list_p
5239 (parser, 2))
5240 break;
5241 }
5242
5243 /* The nested-name-specifier is optional, so we parse
5244 tentatively. */
5245 cp_parser_parse_tentatively (parser);
5246
5247 /* Look for the optional `template' keyword, if this isn't the
5248 first time through the loop. */
5249 if (success)
5250 template_keyword_p = cp_parser_optional_template_keyword (parser);
5251 else
5252 template_keyword_p = false;
5253
5254 /* Save the old scope since the name lookup we are about to do
5255 might destroy it. */
5256 old_scope = parser->scope;
5257 saved_qualifying_scope = parser->qualifying_scope;
5258 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5259 look up names in "X<T>::I" in order to determine that "Y" is
5260 a template. So, if we have a typename at this point, we make
5261 an effort to look through it. */
5262 if (is_declaration
5263 && !typename_keyword_p
5264 && parser->scope
5265 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5266 parser->scope = resolve_typename_type (parser->scope,
5267 /*only_current_p=*/false);
5268 /* Parse the qualifying entity. */
5269 new_scope
5270 = cp_parser_qualifying_entity (parser,
5271 typename_keyword_p,
5272 template_keyword_p,
5273 check_dependency_p,
5274 type_p,
5275 is_declaration);
5276 /* Look for the `::' token. */
5277 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5278
5279 /* If we found what we wanted, we keep going; otherwise, we're
5280 done. */
5281 if (!cp_parser_parse_definitely (parser))
5282 {
5283 bool error_p = false;
5284
5285 /* Restore the OLD_SCOPE since it was valid before the
5286 failed attempt at finding the last
5287 class-or-namespace-name. */
5288 parser->scope = old_scope;
5289 parser->qualifying_scope = saved_qualifying_scope;
5290
5291 /* If the next token is a decltype, and the one after that is a
5292 `::', then the decltype has failed to resolve to a class or
5293 enumeration type. Give this error even when parsing
5294 tentatively since it can't possibly be valid--and we're going
5295 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5296 won't get another chance.*/
5297 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5298 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5299 == CPP_SCOPE))
5300 {
5301 token = cp_lexer_consume_token (parser->lexer);
5302 error_at (token->location, "decltype evaluates to %qT, "
5303 "which is not a class or enumeration type",
5304 token->u.value);
5305 parser->scope = error_mark_node;
5306 error_p = true;
5307 /* As below. */
5308 success = true;
5309 cp_lexer_consume_token (parser->lexer);
5310 }
5311
5312 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5313 break;
5314 /* If the next token is an identifier, and the one after
5315 that is a `::', then any valid interpretation would have
5316 found a class-or-namespace-name. */
5317 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5319 == CPP_SCOPE)
5320 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5321 != CPP_COMPL))
5322 {
5323 token = cp_lexer_consume_token (parser->lexer);
5324 if (!error_p)
5325 {
5326 if (!token->error_reported)
5327 {
5328 tree decl;
5329 tree ambiguous_decls;
5330
5331 decl = cp_parser_lookup_name (parser, token->u.value,
5332 none_type,
5333 /*is_template=*/false,
5334 /*is_namespace=*/false,
5335 /*check_dependency=*/true,
5336 &ambiguous_decls,
5337 token->location);
5338 if (TREE_CODE (decl) == TEMPLATE_DECL)
5339 error_at (token->location,
5340 "%qD used without template parameters",
5341 decl);
5342 else if (ambiguous_decls)
5343 {
5344 // cp_parser_lookup_name has the same diagnostic,
5345 // thus make sure to emit it at most once.
5346 if (cp_parser_uncommitted_to_tentative_parse_p
5347 (parser))
5348 {
5349 error_at (token->location,
5350 "reference to %qD is ambiguous",
5351 token->u.value);
5352 print_candidates (ambiguous_decls);
5353 }
5354 decl = error_mark_node;
5355 }
5356 else
5357 {
5358 if (cxx_dialect != cxx98)
5359 cp_parser_name_lookup_error
5360 (parser, token->u.value, decl, NLE_NOT_CXX98,
5361 token->location);
5362 else
5363 cp_parser_name_lookup_error
5364 (parser, token->u.value, decl, NLE_CXX98,
5365 token->location);
5366 }
5367 }
5368 parser->scope = error_mark_node;
5369 error_p = true;
5370 /* Treat this as a successful nested-name-specifier
5371 due to:
5372
5373 [basic.lookup.qual]
5374
5375 If the name found is not a class-name (clause
5376 _class_) or namespace-name (_namespace.def_), the
5377 program is ill-formed. */
5378 success = true;
5379 }
5380 cp_lexer_consume_token (parser->lexer);
5381 }
5382 break;
5383 }
5384 /* We've found one valid nested-name-specifier. */
5385 success = true;
5386 /* Name lookup always gives us a DECL. */
5387 if (TREE_CODE (new_scope) == TYPE_DECL)
5388 new_scope = TREE_TYPE (new_scope);
5389 /* Uses of "template" must be followed by actual templates. */
5390 if (template_keyword_p
5391 && !(CLASS_TYPE_P (new_scope)
5392 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5393 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5394 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5395 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5396 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5397 == TEMPLATE_ID_EXPR)))
5398 permerror (input_location, TYPE_P (new_scope)
5399 ? G_("%qT is not a template")
5400 : G_("%qD is not a template"),
5401 new_scope);
5402 /* If it is a class scope, try to complete it; we are about to
5403 be looking up names inside the class. */
5404 if (TYPE_P (new_scope)
5405 /* Since checking types for dependency can be expensive,
5406 avoid doing it if the type is already complete. */
5407 && !COMPLETE_TYPE_P (new_scope)
5408 /* Do not try to complete dependent types. */
5409 && !dependent_type_p (new_scope))
5410 {
5411 new_scope = complete_type (new_scope);
5412 /* If it is a typedef to current class, use the current
5413 class instead, as the typedef won't have any names inside
5414 it yet. */
5415 if (!COMPLETE_TYPE_P (new_scope)
5416 && currently_open_class (new_scope))
5417 new_scope = TYPE_MAIN_VARIANT (new_scope);
5418 }
5419 /* Make sure we look in the right scope the next time through
5420 the loop. */
5421 parser->scope = new_scope;
5422 }
5423
5424 /* If parsing tentatively, replace the sequence of tokens that makes
5425 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5426 token. That way, should we re-parse the token stream, we will
5427 not have to repeat the effort required to do the parse, nor will
5428 we issue duplicate error messages. */
5429 if (success && start)
5430 {
5431 cp_token *token;
5432
5433 token = cp_lexer_token_at (parser->lexer, start);
5434 /* Reset the contents of the START token. */
5435 token->type = CPP_NESTED_NAME_SPECIFIER;
5436 /* Retrieve any deferred checks. Do not pop this access checks yet
5437 so the memory will not be reclaimed during token replacing below. */
5438 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5439 token->u.tree_check_value->value = parser->scope;
5440 token->u.tree_check_value->checks = get_deferred_access_checks ();
5441 token->u.tree_check_value->qualifying_scope =
5442 parser->qualifying_scope;
5443 token->keyword = RID_MAX;
5444
5445 /* Purge all subsequent tokens. */
5446 cp_lexer_purge_tokens_after (parser->lexer, start);
5447 }
5448
5449 if (start)
5450 pop_to_parent_deferring_access_checks ();
5451
5452 return success ? parser->scope : NULL_TREE;
5453 }
5454
5455 /* Parse a nested-name-specifier. See
5456 cp_parser_nested_name_specifier_opt for details. This function
5457 behaves identically, except that it will an issue an error if no
5458 nested-name-specifier is present. */
5459
5460 static tree
5461 cp_parser_nested_name_specifier (cp_parser *parser,
5462 bool typename_keyword_p,
5463 bool check_dependency_p,
5464 bool type_p,
5465 bool is_declaration)
5466 {
5467 tree scope;
5468
5469 /* Look for the nested-name-specifier. */
5470 scope = cp_parser_nested_name_specifier_opt (parser,
5471 typename_keyword_p,
5472 check_dependency_p,
5473 type_p,
5474 is_declaration);
5475 /* If it was not present, issue an error message. */
5476 if (!scope)
5477 {
5478 cp_parser_error (parser, "expected nested-name-specifier");
5479 parser->scope = NULL_TREE;
5480 }
5481
5482 return scope;
5483 }
5484
5485 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5486 this is either a class-name or a namespace-name (which corresponds
5487 to the class-or-namespace-name production in the grammar). For
5488 C++0x, it can also be a type-name that refers to an enumeration
5489 type or a simple-template-id.
5490
5491 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5492 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5493 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5494 TYPE_P is TRUE iff the next name should be taken as a class-name,
5495 even the same name is declared to be another entity in the same
5496 scope.
5497
5498 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5499 specified by the class-or-namespace-name. If neither is found the
5500 ERROR_MARK_NODE is returned. */
5501
5502 static tree
5503 cp_parser_qualifying_entity (cp_parser *parser,
5504 bool typename_keyword_p,
5505 bool template_keyword_p,
5506 bool check_dependency_p,
5507 bool type_p,
5508 bool is_declaration)
5509 {
5510 tree saved_scope;
5511 tree saved_qualifying_scope;
5512 tree saved_object_scope;
5513 tree scope;
5514 bool only_class_p;
5515 bool successful_parse_p;
5516
5517 /* DR 743: decltype can appear in a nested-name-specifier. */
5518 if (cp_lexer_next_token_is_decltype (parser->lexer))
5519 {
5520 scope = cp_parser_decltype (parser);
5521 if (TREE_CODE (scope) != ENUMERAL_TYPE
5522 && !MAYBE_CLASS_TYPE_P (scope))
5523 {
5524 cp_parser_simulate_error (parser);
5525 return error_mark_node;
5526 }
5527 if (TYPE_NAME (scope))
5528 scope = TYPE_NAME (scope);
5529 return scope;
5530 }
5531
5532 /* Before we try to parse the class-name, we must save away the
5533 current PARSER->SCOPE since cp_parser_class_name will destroy
5534 it. */
5535 saved_scope = parser->scope;
5536 saved_qualifying_scope = parser->qualifying_scope;
5537 saved_object_scope = parser->object_scope;
5538 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5539 there is no need to look for a namespace-name. */
5540 only_class_p = template_keyword_p
5541 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5542 if (!only_class_p)
5543 cp_parser_parse_tentatively (parser);
5544 scope = cp_parser_class_name (parser,
5545 typename_keyword_p,
5546 template_keyword_p,
5547 type_p ? class_type : none_type,
5548 check_dependency_p,
5549 /*class_head_p=*/false,
5550 is_declaration);
5551 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5552 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5553 if (!only_class_p
5554 && cxx_dialect != cxx98
5555 && !successful_parse_p)
5556 {
5557 /* Restore the saved scope. */
5558 parser->scope = saved_scope;
5559 parser->qualifying_scope = saved_qualifying_scope;
5560 parser->object_scope = saved_object_scope;
5561
5562 /* Parse tentatively. */
5563 cp_parser_parse_tentatively (parser);
5564
5565 /* Parse a type-name */
5566 scope = cp_parser_type_name (parser);
5567
5568 /* "If the name found does not designate a namespace or a class,
5569 enumeration, or dependent type, the program is ill-formed."
5570
5571 We cover classes and dependent types above and namespaces below,
5572 so this code is only looking for enums. */
5573 if (!scope || TREE_CODE (scope) != TYPE_DECL
5574 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5575 cp_parser_simulate_error (parser);
5576
5577 successful_parse_p = cp_parser_parse_definitely (parser);
5578 }
5579 /* If that didn't work, try for a namespace-name. */
5580 if (!only_class_p && !successful_parse_p)
5581 {
5582 /* Restore the saved scope. */
5583 parser->scope = saved_scope;
5584 parser->qualifying_scope = saved_qualifying_scope;
5585 parser->object_scope = saved_object_scope;
5586 /* If we are not looking at an identifier followed by the scope
5587 resolution operator, then this is not part of a
5588 nested-name-specifier. (Note that this function is only used
5589 to parse the components of a nested-name-specifier.) */
5590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5591 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5592 return error_mark_node;
5593 scope = cp_parser_namespace_name (parser);
5594 }
5595
5596 return scope;
5597 }
5598
5599 /* Parse a postfix-expression.
5600
5601 postfix-expression:
5602 primary-expression
5603 postfix-expression [ expression ]
5604 postfix-expression ( expression-list [opt] )
5605 simple-type-specifier ( expression-list [opt] )
5606 typename :: [opt] nested-name-specifier identifier
5607 ( expression-list [opt] )
5608 typename :: [opt] nested-name-specifier template [opt] template-id
5609 ( expression-list [opt] )
5610 postfix-expression . template [opt] id-expression
5611 postfix-expression -> template [opt] id-expression
5612 postfix-expression . pseudo-destructor-name
5613 postfix-expression -> pseudo-destructor-name
5614 postfix-expression ++
5615 postfix-expression --
5616 dynamic_cast < type-id > ( expression )
5617 static_cast < type-id > ( expression )
5618 reinterpret_cast < type-id > ( expression )
5619 const_cast < type-id > ( expression )
5620 typeid ( expression )
5621 typeid ( type-id )
5622
5623 GNU Extension:
5624
5625 postfix-expression:
5626 ( type-id ) { initializer-list , [opt] }
5627
5628 This extension is a GNU version of the C99 compound-literal
5629 construct. (The C99 grammar uses `type-name' instead of `type-id',
5630 but they are essentially the same concept.)
5631
5632 If ADDRESS_P is true, the postfix expression is the operand of the
5633 `&' operator. CAST_P is true if this expression is the target of a
5634 cast.
5635
5636 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5637 class member access expressions [expr.ref].
5638
5639 Returns a representation of the expression. */
5640
5641 static tree
5642 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5643 bool member_access_only_p, bool decltype_p,
5644 cp_id_kind * pidk_return)
5645 {
5646 cp_token *token;
5647 location_t loc;
5648 enum rid keyword;
5649 cp_id_kind idk = CP_ID_KIND_NONE;
5650 tree postfix_expression = NULL_TREE;
5651 bool is_member_access = false;
5652 int saved_in_statement = -1;
5653
5654 /* Peek at the next token. */
5655 token = cp_lexer_peek_token (parser->lexer);
5656 loc = token->location;
5657 /* Some of the productions are determined by keywords. */
5658 keyword = token->keyword;
5659 switch (keyword)
5660 {
5661 case RID_DYNCAST:
5662 case RID_STATCAST:
5663 case RID_REINTCAST:
5664 case RID_CONSTCAST:
5665 {
5666 tree type;
5667 tree expression;
5668 const char *saved_message;
5669 bool saved_in_type_id_in_expr_p;
5670
5671 /* All of these can be handled in the same way from the point
5672 of view of parsing. Begin by consuming the token
5673 identifying the cast. */
5674 cp_lexer_consume_token (parser->lexer);
5675
5676 /* New types cannot be defined in the cast. */
5677 saved_message = parser->type_definition_forbidden_message;
5678 parser->type_definition_forbidden_message
5679 = G_("types may not be defined in casts");
5680
5681 /* Look for the opening `<'. */
5682 cp_parser_require (parser, CPP_LESS, RT_LESS);
5683 /* Parse the type to which we are casting. */
5684 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5685 parser->in_type_id_in_expr_p = true;
5686 type = cp_parser_type_id (parser);
5687 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5688 /* Look for the closing `>'. */
5689 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5690 /* Restore the old message. */
5691 parser->type_definition_forbidden_message = saved_message;
5692
5693 bool saved_greater_than_is_operator_p
5694 = parser->greater_than_is_operator_p;
5695 parser->greater_than_is_operator_p = true;
5696
5697 /* And the expression which is being cast. */
5698 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5699 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5700 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5701
5702 parser->greater_than_is_operator_p
5703 = saved_greater_than_is_operator_p;
5704
5705 /* Only type conversions to integral or enumeration types
5706 can be used in constant-expressions. */
5707 if (!cast_valid_in_integral_constant_expression_p (type)
5708 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5709 return error_mark_node;
5710
5711 switch (keyword)
5712 {
5713 case RID_DYNCAST:
5714 postfix_expression
5715 = build_dynamic_cast (type, expression, tf_warning_or_error);
5716 break;
5717 case RID_STATCAST:
5718 postfix_expression
5719 = build_static_cast (type, expression, tf_warning_or_error);
5720 break;
5721 case RID_REINTCAST:
5722 postfix_expression
5723 = build_reinterpret_cast (type, expression,
5724 tf_warning_or_error);
5725 break;
5726 case RID_CONSTCAST:
5727 postfix_expression
5728 = build_const_cast (type, expression, tf_warning_or_error);
5729 break;
5730 default:
5731 gcc_unreachable ();
5732 }
5733 }
5734 break;
5735
5736 case RID_TYPEID:
5737 {
5738 tree type;
5739 const char *saved_message;
5740 bool saved_in_type_id_in_expr_p;
5741
5742 /* Consume the `typeid' token. */
5743 cp_lexer_consume_token (parser->lexer);
5744 /* Look for the `(' token. */
5745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5746 /* Types cannot be defined in a `typeid' expression. */
5747 saved_message = parser->type_definition_forbidden_message;
5748 parser->type_definition_forbidden_message
5749 = G_("types may not be defined in a %<typeid%> expression");
5750 /* We can't be sure yet whether we're looking at a type-id or an
5751 expression. */
5752 cp_parser_parse_tentatively (parser);
5753 /* Try a type-id first. */
5754 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5755 parser->in_type_id_in_expr_p = true;
5756 type = cp_parser_type_id (parser);
5757 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5758 /* Look for the `)' token. Otherwise, we can't be sure that
5759 we're not looking at an expression: consider `typeid (int
5760 (3))', for example. */
5761 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5762 /* If all went well, simply lookup the type-id. */
5763 if (cp_parser_parse_definitely (parser))
5764 postfix_expression = get_typeid (type, tf_warning_or_error);
5765 /* Otherwise, fall back to the expression variant. */
5766 else
5767 {
5768 tree expression;
5769
5770 /* Look for an expression. */
5771 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5772 /* Compute its typeid. */
5773 postfix_expression = build_typeid (expression, tf_warning_or_error);
5774 /* Look for the `)' token. */
5775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5776 }
5777 /* Restore the saved message. */
5778 parser->type_definition_forbidden_message = saved_message;
5779 /* `typeid' may not appear in an integral constant expression. */
5780 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5781 return error_mark_node;
5782 }
5783 break;
5784
5785 case RID_TYPENAME:
5786 {
5787 tree type;
5788 /* The syntax permitted here is the same permitted for an
5789 elaborated-type-specifier. */
5790 type = cp_parser_elaborated_type_specifier (parser,
5791 /*is_friend=*/false,
5792 /*is_declaration=*/false);
5793 postfix_expression = cp_parser_functional_cast (parser, type);
5794 }
5795 break;
5796
5797 case RID_CILK_SPAWN:
5798 {
5799 cp_lexer_consume_token (parser->lexer);
5800 token = cp_lexer_peek_token (parser->lexer);
5801 if (token->type == CPP_SEMICOLON)
5802 {
5803 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5804 "an expression");
5805 postfix_expression = error_mark_node;
5806 break;
5807 }
5808 else if (!current_function_decl)
5809 {
5810 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5811 "inside a function");
5812 postfix_expression = error_mark_node;
5813 break;
5814 }
5815 else
5816 {
5817 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5818 saved_in_statement = parser->in_statement;
5819 parser->in_statement |= IN_CILK_SPAWN;
5820 }
5821 cfun->calls_cilk_spawn = 1;
5822 postfix_expression =
5823 cp_parser_postfix_expression (parser, false, false,
5824 false, false, &idk);
5825 if (!flag_cilkplus)
5826 {
5827 error_at (token->location, "-fcilkplus must be enabled to use"
5828 " %<_Cilk_spawn%>");
5829 cfun->calls_cilk_spawn = 0;
5830 }
5831 else if (saved_in_statement & IN_CILK_SPAWN)
5832 {
5833 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5834 "are not permitted");
5835 postfix_expression = error_mark_node;
5836 cfun->calls_cilk_spawn = 0;
5837 }
5838 else
5839 {
5840 postfix_expression = build_cilk_spawn (token->location,
5841 postfix_expression);
5842 if (postfix_expression != error_mark_node)
5843 SET_EXPR_LOCATION (postfix_expression, input_location);
5844 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5845 }
5846 break;
5847 }
5848
5849 case RID_CILK_SYNC:
5850 if (flag_cilkplus)
5851 {
5852 tree sync_expr = build_cilk_sync ();
5853 SET_EXPR_LOCATION (sync_expr,
5854 cp_lexer_peek_token (parser->lexer)->location);
5855 finish_expr_stmt (sync_expr);
5856 }
5857 else
5858 error_at (token->location, "-fcilkplus must be enabled to use"
5859 " %<_Cilk_sync%>");
5860 cp_lexer_consume_token (parser->lexer);
5861 break;
5862
5863 case RID_BUILTIN_SHUFFLE:
5864 {
5865 vec<tree, va_gc> *vec;
5866 unsigned int i;
5867 tree p;
5868
5869 cp_lexer_consume_token (parser->lexer);
5870 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5871 /*cast_p=*/false, /*allow_expansion_p=*/true,
5872 /*non_constant_p=*/NULL);
5873 if (vec == NULL)
5874 return error_mark_node;
5875
5876 FOR_EACH_VEC_ELT (*vec, i, p)
5877 mark_exp_read (p);
5878
5879 if (vec->length () == 2)
5880 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5881 tf_warning_or_error);
5882 else if (vec->length () == 3)
5883 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5884 tf_warning_or_error);
5885 else
5886 {
5887 error_at (loc, "wrong number of arguments to "
5888 "%<__builtin_shuffle%>");
5889 return error_mark_node;
5890 }
5891 break;
5892 }
5893
5894 default:
5895 {
5896 tree type;
5897
5898 /* If the next thing is a simple-type-specifier, we may be
5899 looking at a functional cast. We could also be looking at
5900 an id-expression. So, we try the functional cast, and if
5901 that doesn't work we fall back to the primary-expression. */
5902 cp_parser_parse_tentatively (parser);
5903 /* Look for the simple-type-specifier. */
5904 type = cp_parser_simple_type_specifier (parser,
5905 /*decl_specs=*/NULL,
5906 CP_PARSER_FLAGS_NONE);
5907 /* Parse the cast itself. */
5908 if (!cp_parser_error_occurred (parser))
5909 postfix_expression
5910 = cp_parser_functional_cast (parser, type);
5911 /* If that worked, we're done. */
5912 if (cp_parser_parse_definitely (parser))
5913 break;
5914
5915 /* If the functional-cast didn't work out, try a
5916 compound-literal. */
5917 if (cp_parser_allow_gnu_extensions_p (parser)
5918 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5919 {
5920 tree initializer = NULL_TREE;
5921 bool compound_literal_p;
5922
5923 cp_parser_parse_tentatively (parser);
5924 /* Consume the `('. */
5925 cp_lexer_consume_token (parser->lexer);
5926
5927 /* Avoid calling cp_parser_type_id pointlessly, see comment
5928 in cp_parser_cast_expression about c++/29234. */
5929 cp_lexer_save_tokens (parser->lexer);
5930
5931 compound_literal_p
5932 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5933 /*consume_paren=*/true)
5934 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5935
5936 /* Roll back the tokens we skipped. */
5937 cp_lexer_rollback_tokens (parser->lexer);
5938
5939 if (!compound_literal_p)
5940 cp_parser_simulate_error (parser);
5941 else
5942 {
5943 /* Parse the type. */
5944 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5945 parser->in_type_id_in_expr_p = true;
5946 type = cp_parser_type_id (parser);
5947 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5948 /* Look for the `)'. */
5949 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5950 }
5951
5952 /* If things aren't going well, there's no need to
5953 keep going. */
5954 if (!cp_parser_error_occurred (parser))
5955 {
5956 bool non_constant_p;
5957 /* Parse the brace-enclosed initializer list. */
5958 initializer = cp_parser_braced_list (parser,
5959 &non_constant_p);
5960 }
5961 /* If that worked, we're definitely looking at a
5962 compound-literal expression. */
5963 if (cp_parser_parse_definitely (parser))
5964 {
5965 /* Warn the user that a compound literal is not
5966 allowed in standard C++. */
5967 pedwarn (input_location, OPT_Wpedantic,
5968 "ISO C++ forbids compound-literals");
5969 /* For simplicity, we disallow compound literals in
5970 constant-expressions. We could
5971 allow compound literals of integer type, whose
5972 initializer was a constant, in constant
5973 expressions. Permitting that usage, as a further
5974 extension, would not change the meaning of any
5975 currently accepted programs. (Of course, as
5976 compound literals are not part of ISO C++, the
5977 standard has nothing to say.) */
5978 if (cp_parser_non_integral_constant_expression (parser,
5979 NIC_NCC))
5980 {
5981 postfix_expression = error_mark_node;
5982 break;
5983 }
5984 /* Form the representation of the compound-literal. */
5985 postfix_expression
5986 = finish_compound_literal (type, initializer,
5987 tf_warning_or_error);
5988 break;
5989 }
5990 }
5991
5992 /* It must be a primary-expression. */
5993 postfix_expression
5994 = cp_parser_primary_expression (parser, address_p, cast_p,
5995 /*template_arg_p=*/false,
5996 decltype_p,
5997 &idk);
5998 }
5999 break;
6000 }
6001
6002 /* Note that we don't need to worry about calling build_cplus_new on a
6003 class-valued CALL_EXPR in decltype when it isn't the end of the
6004 postfix-expression; unary_complex_lvalue will take care of that for
6005 all these cases. */
6006
6007 /* Keep looping until the postfix-expression is complete. */
6008 while (true)
6009 {
6010 if (idk == CP_ID_KIND_UNQUALIFIED
6011 && identifier_p (postfix_expression)
6012 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6013 /* It is not a Koenig lookup function call. */
6014 postfix_expression
6015 = unqualified_name_lookup_error (postfix_expression);
6016
6017 /* Peek at the next token. */
6018 token = cp_lexer_peek_token (parser->lexer);
6019
6020 switch (token->type)
6021 {
6022 case CPP_OPEN_SQUARE:
6023 if (cp_next_tokens_can_be_std_attribute_p (parser))
6024 {
6025 cp_parser_error (parser,
6026 "two consecutive %<[%> shall "
6027 "only introduce an attribute");
6028 return error_mark_node;
6029 }
6030 postfix_expression
6031 = cp_parser_postfix_open_square_expression (parser,
6032 postfix_expression,
6033 false,
6034 decltype_p);
6035 idk = CP_ID_KIND_NONE;
6036 is_member_access = false;
6037 break;
6038
6039 case CPP_OPEN_PAREN:
6040 /* postfix-expression ( expression-list [opt] ) */
6041 {
6042 bool koenig_p;
6043 bool is_builtin_constant_p;
6044 bool saved_integral_constant_expression_p = false;
6045 bool saved_non_integral_constant_expression_p = false;
6046 tsubst_flags_t complain = complain_flags (decltype_p);
6047 vec<tree, va_gc> *args;
6048
6049 is_member_access = false;
6050
6051 is_builtin_constant_p
6052 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6053 if (is_builtin_constant_p)
6054 {
6055 /* The whole point of __builtin_constant_p is to allow
6056 non-constant expressions to appear as arguments. */
6057 saved_integral_constant_expression_p
6058 = parser->integral_constant_expression_p;
6059 saved_non_integral_constant_expression_p
6060 = parser->non_integral_constant_expression_p;
6061 parser->integral_constant_expression_p = false;
6062 }
6063 args = (cp_parser_parenthesized_expression_list
6064 (parser, non_attr,
6065 /*cast_p=*/false, /*allow_expansion_p=*/true,
6066 /*non_constant_p=*/NULL));
6067 if (is_builtin_constant_p)
6068 {
6069 parser->integral_constant_expression_p
6070 = saved_integral_constant_expression_p;
6071 parser->non_integral_constant_expression_p
6072 = saved_non_integral_constant_expression_p;
6073 }
6074
6075 if (args == NULL)
6076 {
6077 postfix_expression = error_mark_node;
6078 break;
6079 }
6080
6081 /* Function calls are not permitted in
6082 constant-expressions. */
6083 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6084 && cp_parser_non_integral_constant_expression (parser,
6085 NIC_FUNC_CALL))
6086 {
6087 postfix_expression = error_mark_node;
6088 release_tree_vector (args);
6089 break;
6090 }
6091
6092 koenig_p = false;
6093 if (idk == CP_ID_KIND_UNQUALIFIED
6094 || idk == CP_ID_KIND_TEMPLATE_ID)
6095 {
6096 if (identifier_p (postfix_expression))
6097 {
6098 if (!args->is_empty ())
6099 {
6100 koenig_p = true;
6101 if (!any_type_dependent_arguments_p (args))
6102 postfix_expression
6103 = perform_koenig_lookup (postfix_expression, args,
6104 complain);
6105 }
6106 else
6107 postfix_expression
6108 = unqualified_fn_lookup_error (postfix_expression);
6109 }
6110 /* We do not perform argument-dependent lookup if
6111 normal lookup finds a non-function, in accordance
6112 with the expected resolution of DR 218. */
6113 else if (!args->is_empty ()
6114 && is_overloaded_fn (postfix_expression))
6115 {
6116 tree fn = get_first_fn (postfix_expression);
6117 fn = STRIP_TEMPLATE (fn);
6118
6119 /* Do not do argument dependent lookup if regular
6120 lookup finds a member function or a block-scope
6121 function declaration. [basic.lookup.argdep]/3 */
6122 if (!DECL_FUNCTION_MEMBER_P (fn)
6123 && !DECL_LOCAL_FUNCTION_P (fn))
6124 {
6125 koenig_p = true;
6126 if (!any_type_dependent_arguments_p (args))
6127 postfix_expression
6128 = perform_koenig_lookup (postfix_expression, args,
6129 complain);
6130 }
6131 }
6132 }
6133
6134 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6135 {
6136 tree instance = TREE_OPERAND (postfix_expression, 0);
6137 tree fn = TREE_OPERAND (postfix_expression, 1);
6138
6139 if (processing_template_decl
6140 && (type_dependent_expression_p (instance)
6141 || (!BASELINK_P (fn)
6142 && TREE_CODE (fn) != FIELD_DECL)
6143 || type_dependent_expression_p (fn)
6144 || any_type_dependent_arguments_p (args)))
6145 {
6146 postfix_expression
6147 = build_nt_call_vec (postfix_expression, args);
6148 release_tree_vector (args);
6149 break;
6150 }
6151
6152 if (BASELINK_P (fn))
6153 {
6154 postfix_expression
6155 = (build_new_method_call
6156 (instance, fn, &args, NULL_TREE,
6157 (idk == CP_ID_KIND_QUALIFIED
6158 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6159 : LOOKUP_NORMAL),
6160 /*fn_p=*/NULL,
6161 complain));
6162 }
6163 else
6164 postfix_expression
6165 = finish_call_expr (postfix_expression, &args,
6166 /*disallow_virtual=*/false,
6167 /*koenig_p=*/false,
6168 complain);
6169 }
6170 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6171 || TREE_CODE (postfix_expression) == MEMBER_REF
6172 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6173 postfix_expression = (build_offset_ref_call_from_tree
6174 (postfix_expression, &args,
6175 complain));
6176 else if (idk == CP_ID_KIND_QUALIFIED)
6177 /* A call to a static class member, or a namespace-scope
6178 function. */
6179 postfix_expression
6180 = finish_call_expr (postfix_expression, &args,
6181 /*disallow_virtual=*/true,
6182 koenig_p,
6183 complain);
6184 else
6185 /* All other function calls. */
6186 postfix_expression
6187 = finish_call_expr (postfix_expression, &args,
6188 /*disallow_virtual=*/false,
6189 koenig_p,
6190 complain);
6191
6192 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6193 idk = CP_ID_KIND_NONE;
6194
6195 release_tree_vector (args);
6196 }
6197 break;
6198
6199 case CPP_DOT:
6200 case CPP_DEREF:
6201 /* postfix-expression . template [opt] id-expression
6202 postfix-expression . pseudo-destructor-name
6203 postfix-expression -> template [opt] id-expression
6204 postfix-expression -> pseudo-destructor-name */
6205
6206 /* Consume the `.' or `->' operator. */
6207 cp_lexer_consume_token (parser->lexer);
6208
6209 postfix_expression
6210 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6211 postfix_expression,
6212 false, &idk, loc);
6213
6214 is_member_access = true;
6215 break;
6216
6217 case CPP_PLUS_PLUS:
6218 /* postfix-expression ++ */
6219 /* Consume the `++' token. */
6220 cp_lexer_consume_token (parser->lexer);
6221 /* Generate a representation for the complete expression. */
6222 postfix_expression
6223 = finish_increment_expr (postfix_expression,
6224 POSTINCREMENT_EXPR);
6225 /* Increments may not appear in constant-expressions. */
6226 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6227 postfix_expression = error_mark_node;
6228 idk = CP_ID_KIND_NONE;
6229 is_member_access = false;
6230 break;
6231
6232 case CPP_MINUS_MINUS:
6233 /* postfix-expression -- */
6234 /* Consume the `--' token. */
6235 cp_lexer_consume_token (parser->lexer);
6236 /* Generate a representation for the complete expression. */
6237 postfix_expression
6238 = finish_increment_expr (postfix_expression,
6239 POSTDECREMENT_EXPR);
6240 /* Decrements may not appear in constant-expressions. */
6241 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6242 postfix_expression = error_mark_node;
6243 idk = CP_ID_KIND_NONE;
6244 is_member_access = false;
6245 break;
6246
6247 default:
6248 if (pidk_return != NULL)
6249 * pidk_return = idk;
6250 if (member_access_only_p)
6251 return is_member_access? postfix_expression : error_mark_node;
6252 else
6253 return postfix_expression;
6254 }
6255 }
6256
6257 /* We should never get here. */
6258 gcc_unreachable ();
6259 return error_mark_node;
6260 }
6261
6262 /* This function parses Cilk Plus array notations. If a normal array expr. is
6263 parsed then the array index is passed back to the caller through *INIT_INDEX
6264 and the function returns a NULL_TREE. If array notation expr. is parsed,
6265 then *INIT_INDEX is ignored by the caller and the function returns
6266 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6267 error_mark_node. */
6268
6269 static tree
6270 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6271 tree array_value)
6272 {
6273 cp_token *token = NULL;
6274 tree length_index, stride = NULL_TREE, value_tree, array_type;
6275 if (!array_value || array_value == error_mark_node)
6276 {
6277 cp_parser_skip_to_end_of_statement (parser);
6278 return error_mark_node;
6279 }
6280
6281 array_type = TREE_TYPE (array_value);
6282
6283 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6284 parser->colon_corrects_to_scope_p = false;
6285 token = cp_lexer_peek_token (parser->lexer);
6286
6287 if (!token)
6288 {
6289 cp_parser_error (parser, "expected %<:%> or numeral");
6290 return error_mark_node;
6291 }
6292 else if (token->type == CPP_COLON)
6293 {
6294 /* Consume the ':'. */
6295 cp_lexer_consume_token (parser->lexer);
6296
6297 /* If we are here, then we have a case like this A[:]. */
6298 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6299 {
6300 cp_parser_error (parser, "expected %<]%>");
6301 cp_parser_skip_to_end_of_statement (parser);
6302 return error_mark_node;
6303 }
6304 *init_index = NULL_TREE;
6305 stride = NULL_TREE;
6306 length_index = NULL_TREE;
6307 }
6308 else
6309 {
6310 /* If we are here, then there are three valid possibilities:
6311 1. ARRAY [ EXP ]
6312 2. ARRAY [ EXP : EXP ]
6313 3. ARRAY [ EXP : EXP : EXP ] */
6314
6315 *init_index = cp_parser_expression (parser, false, NULL);
6316 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6317 {
6318 /* This indicates that we have a normal array expression. */
6319 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6320 return NULL_TREE;
6321 }
6322
6323 /* Consume the ':'. */
6324 cp_lexer_consume_token (parser->lexer);
6325 length_index = cp_parser_expression (parser, false, NULL);
6326 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6327 {
6328 cp_lexer_consume_token (parser->lexer);
6329 stride = cp_parser_expression (parser, false, NULL);
6330 }
6331 }
6332 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6333
6334 if (*init_index == error_mark_node || length_index == error_mark_node
6335 || stride == error_mark_node)
6336 {
6337 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6338 cp_lexer_consume_token (parser->lexer);
6339 return error_mark_node;
6340 }
6341 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6342
6343 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6344 length_index, stride, array_type);
6345 return value_tree;
6346 }
6347
6348 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6349 by cp_parser_builtin_offsetof. We're looking for
6350
6351 postfix-expression [ expression ]
6352 postfix-expression [ braced-init-list ] (C++11)
6353
6354 FOR_OFFSETOF is set if we're being called in that context, which
6355 changes how we deal with integer constant expressions. */
6356
6357 static tree
6358 cp_parser_postfix_open_square_expression (cp_parser *parser,
6359 tree postfix_expression,
6360 bool for_offsetof,
6361 bool decltype_p)
6362 {
6363 tree index = NULL_TREE;
6364 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6365 bool saved_greater_than_is_operator_p;
6366
6367 /* Consume the `[' token. */
6368 cp_lexer_consume_token (parser->lexer);
6369
6370 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6371 parser->greater_than_is_operator_p = true;
6372
6373 /* Parse the index expression. */
6374 /* ??? For offsetof, there is a question of what to allow here. If
6375 offsetof is not being used in an integral constant expression context,
6376 then we *could* get the right answer by computing the value at runtime.
6377 If we are in an integral constant expression context, then we might
6378 could accept any constant expression; hard to say without analysis.
6379 Rather than open the barn door too wide right away, allow only integer
6380 constant expressions here. */
6381 if (for_offsetof)
6382 index = cp_parser_constant_expression (parser, false, NULL);
6383 else
6384 {
6385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6386 {
6387 bool expr_nonconst_p;
6388 cp_lexer_set_source_position (parser->lexer);
6389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6390 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6391 if (flag_cilkplus
6392 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6393 {
6394 error_at (cp_lexer_peek_token (parser->lexer)->location,
6395 "braced list index is not allowed with array "
6396 "notation");
6397 cp_parser_skip_to_end_of_statement (parser);
6398 return error_mark_node;
6399 }
6400 }
6401 else if (flag_cilkplus)
6402 {
6403 /* Here are have these two options:
6404 ARRAY[EXP : EXP] - Array notation expr with default
6405 stride of 1.
6406 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6407 stride. */
6408 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6409 postfix_expression);
6410 if (an_exp)
6411 return an_exp;
6412 }
6413 else
6414 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6415 }
6416
6417 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6418
6419 /* Look for the closing `]'. */
6420 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6421
6422 /* Build the ARRAY_REF. */
6423 postfix_expression = grok_array_decl (loc, postfix_expression,
6424 index, decltype_p);
6425
6426 /* When not doing offsetof, array references are not permitted in
6427 constant-expressions. */
6428 if (!for_offsetof
6429 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6430 postfix_expression = error_mark_node;
6431
6432 return postfix_expression;
6433 }
6434
6435 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6436 by cp_parser_builtin_offsetof. We're looking for
6437
6438 postfix-expression . template [opt] id-expression
6439 postfix-expression . pseudo-destructor-name
6440 postfix-expression -> template [opt] id-expression
6441 postfix-expression -> pseudo-destructor-name
6442
6443 FOR_OFFSETOF is set if we're being called in that context. That sorta
6444 limits what of the above we'll actually accept, but nevermind.
6445 TOKEN_TYPE is the "." or "->" token, which will already have been
6446 removed from the stream. */
6447
6448 static tree
6449 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6450 enum cpp_ttype token_type,
6451 tree postfix_expression,
6452 bool for_offsetof, cp_id_kind *idk,
6453 location_t location)
6454 {
6455 tree name;
6456 bool dependent_p;
6457 bool pseudo_destructor_p;
6458 tree scope = NULL_TREE;
6459
6460 /* If this is a `->' operator, dereference the pointer. */
6461 if (token_type == CPP_DEREF)
6462 postfix_expression = build_x_arrow (location, postfix_expression,
6463 tf_warning_or_error);
6464 /* Check to see whether or not the expression is type-dependent. */
6465 dependent_p = type_dependent_expression_p (postfix_expression);
6466 /* The identifier following the `->' or `.' is not qualified. */
6467 parser->scope = NULL_TREE;
6468 parser->qualifying_scope = NULL_TREE;
6469 parser->object_scope = NULL_TREE;
6470 *idk = CP_ID_KIND_NONE;
6471
6472 /* Enter the scope corresponding to the type of the object
6473 given by the POSTFIX_EXPRESSION. */
6474 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6475 {
6476 scope = TREE_TYPE (postfix_expression);
6477 /* According to the standard, no expression should ever have
6478 reference type. Unfortunately, we do not currently match
6479 the standard in this respect in that our internal representation
6480 of an expression may have reference type even when the standard
6481 says it does not. Therefore, we have to manually obtain the
6482 underlying type here. */
6483 scope = non_reference (scope);
6484 /* The type of the POSTFIX_EXPRESSION must be complete. */
6485 if (scope == unknown_type_node)
6486 {
6487 error_at (location, "%qE does not have class type",
6488 postfix_expression);
6489 scope = NULL_TREE;
6490 }
6491 /* Unlike the object expression in other contexts, *this is not
6492 required to be of complete type for purposes of class member
6493 access (5.2.5) outside the member function body. */
6494 else if (postfix_expression != current_class_ref
6495 && !(processing_template_decl && scope == current_class_type))
6496 scope = complete_type_or_else (scope, NULL_TREE);
6497 /* Let the name lookup machinery know that we are processing a
6498 class member access expression. */
6499 parser->context->object_type = scope;
6500 /* If something went wrong, we want to be able to discern that case,
6501 as opposed to the case where there was no SCOPE due to the type
6502 of expression being dependent. */
6503 if (!scope)
6504 scope = error_mark_node;
6505 /* If the SCOPE was erroneous, make the various semantic analysis
6506 functions exit quickly -- and without issuing additional error
6507 messages. */
6508 if (scope == error_mark_node)
6509 postfix_expression = error_mark_node;
6510 }
6511
6512 /* Assume this expression is not a pseudo-destructor access. */
6513 pseudo_destructor_p = false;
6514
6515 /* If the SCOPE is a scalar type, then, if this is a valid program,
6516 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6517 is type dependent, it can be pseudo-destructor-name or something else.
6518 Try to parse it as pseudo-destructor-name first. */
6519 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6520 {
6521 tree s;
6522 tree type;
6523
6524 cp_parser_parse_tentatively (parser);
6525 /* Parse the pseudo-destructor-name. */
6526 s = NULL_TREE;
6527 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6528 &s, &type);
6529 if (dependent_p
6530 && (cp_parser_error_occurred (parser)
6531 || !SCALAR_TYPE_P (type)))
6532 cp_parser_abort_tentative_parse (parser);
6533 else if (cp_parser_parse_definitely (parser))
6534 {
6535 pseudo_destructor_p = true;
6536 postfix_expression
6537 = finish_pseudo_destructor_expr (postfix_expression,
6538 s, type, location);
6539 }
6540 }
6541
6542 if (!pseudo_destructor_p)
6543 {
6544 /* If the SCOPE is not a scalar type, we are looking at an
6545 ordinary class member access expression, rather than a
6546 pseudo-destructor-name. */
6547 bool template_p;
6548 cp_token *token = cp_lexer_peek_token (parser->lexer);
6549 /* Parse the id-expression. */
6550 name = (cp_parser_id_expression
6551 (parser,
6552 cp_parser_optional_template_keyword (parser),
6553 /*check_dependency_p=*/true,
6554 &template_p,
6555 /*declarator_p=*/false,
6556 /*optional_p=*/false));
6557 /* In general, build a SCOPE_REF if the member name is qualified.
6558 However, if the name was not dependent and has already been
6559 resolved; there is no need to build the SCOPE_REF. For example;
6560
6561 struct X { void f(); };
6562 template <typename T> void f(T* t) { t->X::f(); }
6563
6564 Even though "t" is dependent, "X::f" is not and has been resolved
6565 to a BASELINK; there is no need to include scope information. */
6566
6567 /* But we do need to remember that there was an explicit scope for
6568 virtual function calls. */
6569 if (parser->scope)
6570 *idk = CP_ID_KIND_QUALIFIED;
6571
6572 /* If the name is a template-id that names a type, we will get a
6573 TYPE_DECL here. That is invalid code. */
6574 if (TREE_CODE (name) == TYPE_DECL)
6575 {
6576 error_at (token->location, "invalid use of %qD", name);
6577 postfix_expression = error_mark_node;
6578 }
6579 else
6580 {
6581 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6582 {
6583 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6584 {
6585 error_at (token->location, "%<%D::%D%> is not a class member",
6586 parser->scope, name);
6587 postfix_expression = error_mark_node;
6588 }
6589 else
6590 name = build_qualified_name (/*type=*/NULL_TREE,
6591 parser->scope,
6592 name,
6593 template_p);
6594 parser->scope = NULL_TREE;
6595 parser->qualifying_scope = NULL_TREE;
6596 parser->object_scope = NULL_TREE;
6597 }
6598 if (parser->scope && name && BASELINK_P (name))
6599 adjust_result_of_qualified_name_lookup
6600 (name, parser->scope, scope);
6601 postfix_expression
6602 = finish_class_member_access_expr (postfix_expression, name,
6603 template_p,
6604 tf_warning_or_error);
6605 }
6606 }
6607
6608 /* We no longer need to look up names in the scope of the object on
6609 the left-hand side of the `.' or `->' operator. */
6610 parser->context->object_type = NULL_TREE;
6611
6612 /* Outside of offsetof, these operators may not appear in
6613 constant-expressions. */
6614 if (!for_offsetof
6615 && (cp_parser_non_integral_constant_expression
6616 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6617 postfix_expression = error_mark_node;
6618
6619 return postfix_expression;
6620 }
6621
6622 /* Parse a parenthesized expression-list.
6623
6624 expression-list:
6625 assignment-expression
6626 expression-list, assignment-expression
6627
6628 attribute-list:
6629 expression-list
6630 identifier
6631 identifier, expression-list
6632
6633 CAST_P is true if this expression is the target of a cast.
6634
6635 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6636 argument pack.
6637
6638 Returns a vector of trees. Each element is a representation of an
6639 assignment-expression. NULL is returned if the ( and or ) are
6640 missing. An empty, but allocated, vector is returned on no
6641 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6642 if we are parsing an attribute list for an attribute that wants a
6643 plain identifier argument, normal_attr for an attribute that wants
6644 an expression, or non_attr if we aren't parsing an attribute list. If
6645 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6646 not all of the expressions in the list were constant. */
6647
6648 static vec<tree, va_gc> *
6649 cp_parser_parenthesized_expression_list (cp_parser* parser,
6650 int is_attribute_list,
6651 bool cast_p,
6652 bool allow_expansion_p,
6653 bool *non_constant_p)
6654 {
6655 vec<tree, va_gc> *expression_list;
6656 bool fold_expr_p = is_attribute_list != non_attr;
6657 tree identifier = NULL_TREE;
6658 bool saved_greater_than_is_operator_p;
6659
6660 /* Assume all the expressions will be constant. */
6661 if (non_constant_p)
6662 *non_constant_p = false;
6663
6664 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6665 return NULL;
6666
6667 expression_list = make_tree_vector ();
6668
6669 /* Within a parenthesized expression, a `>' token is always
6670 the greater-than operator. */
6671 saved_greater_than_is_operator_p
6672 = parser->greater_than_is_operator_p;
6673 parser->greater_than_is_operator_p = true;
6674
6675 /* Consume expressions until there are no more. */
6676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6677 while (true)
6678 {
6679 tree expr;
6680
6681 /* At the beginning of attribute lists, check to see if the
6682 next token is an identifier. */
6683 if (is_attribute_list == id_attr
6684 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6685 {
6686 cp_token *token;
6687
6688 /* Consume the identifier. */
6689 token = cp_lexer_consume_token (parser->lexer);
6690 /* Save the identifier. */
6691 identifier = token->u.value;
6692 }
6693 else
6694 {
6695 bool expr_non_constant_p;
6696
6697 /* Parse the next assignment-expression. */
6698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6699 {
6700 /* A braced-init-list. */
6701 cp_lexer_set_source_position (parser->lexer);
6702 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6703 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6704 if (non_constant_p && expr_non_constant_p)
6705 *non_constant_p = true;
6706 }
6707 else if (non_constant_p)
6708 {
6709 expr = (cp_parser_constant_expression
6710 (parser, /*allow_non_constant_p=*/true,
6711 &expr_non_constant_p));
6712 if (expr_non_constant_p)
6713 *non_constant_p = true;
6714 }
6715 else
6716 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6717
6718 if (fold_expr_p)
6719 expr = fold_non_dependent_expr (expr);
6720
6721 /* If we have an ellipsis, then this is an expression
6722 expansion. */
6723 if (allow_expansion_p
6724 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6725 {
6726 /* Consume the `...'. */
6727 cp_lexer_consume_token (parser->lexer);
6728
6729 /* Build the argument pack. */
6730 expr = make_pack_expansion (expr);
6731 }
6732
6733 /* Add it to the list. We add error_mark_node
6734 expressions to the list, so that we can still tell if
6735 the correct form for a parenthesized expression-list
6736 is found. That gives better errors. */
6737 vec_safe_push (expression_list, expr);
6738
6739 if (expr == error_mark_node)
6740 goto skip_comma;
6741 }
6742
6743 /* After the first item, attribute lists look the same as
6744 expression lists. */
6745 is_attribute_list = non_attr;
6746
6747 get_comma:;
6748 /* If the next token isn't a `,', then we are done. */
6749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6750 break;
6751
6752 /* Otherwise, consume the `,' and keep going. */
6753 cp_lexer_consume_token (parser->lexer);
6754 }
6755
6756 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6757 {
6758 int ending;
6759
6760 skip_comma:;
6761 /* We try and resync to an unnested comma, as that will give the
6762 user better diagnostics. */
6763 ending = cp_parser_skip_to_closing_parenthesis (parser,
6764 /*recovering=*/true,
6765 /*or_comma=*/true,
6766 /*consume_paren=*/true);
6767 if (ending < 0)
6768 goto get_comma;
6769 if (!ending)
6770 {
6771 parser->greater_than_is_operator_p
6772 = saved_greater_than_is_operator_p;
6773 return NULL;
6774 }
6775 }
6776
6777 parser->greater_than_is_operator_p
6778 = saved_greater_than_is_operator_p;
6779
6780 if (identifier)
6781 vec_safe_insert (expression_list, 0, identifier);
6782
6783 return expression_list;
6784 }
6785
6786 /* Parse a pseudo-destructor-name.
6787
6788 pseudo-destructor-name:
6789 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6790 :: [opt] nested-name-specifier template template-id :: ~ type-name
6791 :: [opt] nested-name-specifier [opt] ~ type-name
6792
6793 If either of the first two productions is used, sets *SCOPE to the
6794 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6795 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6796 or ERROR_MARK_NODE if the parse fails. */
6797
6798 static void
6799 cp_parser_pseudo_destructor_name (cp_parser* parser,
6800 tree object,
6801 tree* scope,
6802 tree* type)
6803 {
6804 bool nested_name_specifier_p;
6805
6806 /* Handle ~auto. */
6807 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6808 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6809 && !type_dependent_expression_p (object))
6810 {
6811 if (cxx_dialect < cxx1y)
6812 pedwarn (input_location, 0,
6813 "%<~auto%> only available with "
6814 "-std=c++1y or -std=gnu++1y");
6815 cp_lexer_consume_token (parser->lexer);
6816 cp_lexer_consume_token (parser->lexer);
6817 *scope = NULL_TREE;
6818 *type = TREE_TYPE (object);
6819 return;
6820 }
6821
6822 /* Assume that things will not work out. */
6823 *type = error_mark_node;
6824
6825 /* Look for the optional `::' operator. */
6826 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6827 /* Look for the optional nested-name-specifier. */
6828 nested_name_specifier_p
6829 = (cp_parser_nested_name_specifier_opt (parser,
6830 /*typename_keyword_p=*/false,
6831 /*check_dependency_p=*/true,
6832 /*type_p=*/false,
6833 /*is_declaration=*/false)
6834 != NULL_TREE);
6835 /* Now, if we saw a nested-name-specifier, we might be doing the
6836 second production. */
6837 if (nested_name_specifier_p
6838 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6839 {
6840 /* Consume the `template' keyword. */
6841 cp_lexer_consume_token (parser->lexer);
6842 /* Parse the template-id. */
6843 cp_parser_template_id (parser,
6844 /*template_keyword_p=*/true,
6845 /*check_dependency_p=*/false,
6846 class_type,
6847 /*is_declaration=*/true);
6848 /* Look for the `::' token. */
6849 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6850 }
6851 /* If the next token is not a `~', then there might be some
6852 additional qualification. */
6853 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6854 {
6855 /* At this point, we're looking for "type-name :: ~". The type-name
6856 must not be a class-name, since this is a pseudo-destructor. So,
6857 it must be either an enum-name, or a typedef-name -- both of which
6858 are just identifiers. So, we peek ahead to check that the "::"
6859 and "~" tokens are present; if they are not, then we can avoid
6860 calling type_name. */
6861 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6862 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6863 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6864 {
6865 cp_parser_error (parser, "non-scalar type");
6866 return;
6867 }
6868
6869 /* Look for the type-name. */
6870 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6871 if (*scope == error_mark_node)
6872 return;
6873
6874 /* Look for the `::' token. */
6875 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6876 }
6877 else
6878 *scope = NULL_TREE;
6879
6880 /* Look for the `~'. */
6881 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6882
6883 /* Once we see the ~, this has to be a pseudo-destructor. */
6884 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6885 cp_parser_commit_to_topmost_tentative_parse (parser);
6886
6887 /* Look for the type-name again. We are not responsible for
6888 checking that it matches the first type-name. */
6889 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6890 }
6891
6892 /* Parse a unary-expression.
6893
6894 unary-expression:
6895 postfix-expression
6896 ++ cast-expression
6897 -- cast-expression
6898 unary-operator cast-expression
6899 sizeof unary-expression
6900 sizeof ( type-id )
6901 alignof ( type-id ) [C++0x]
6902 new-expression
6903 delete-expression
6904
6905 GNU Extensions:
6906
6907 unary-expression:
6908 __extension__ cast-expression
6909 __alignof__ unary-expression
6910 __alignof__ ( type-id )
6911 alignof unary-expression [C++0x]
6912 __real__ cast-expression
6913 __imag__ cast-expression
6914 && identifier
6915 sizeof ( type-id ) { initializer-list , [opt] }
6916 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6917 __alignof__ ( type-id ) { initializer-list , [opt] }
6918
6919 ADDRESS_P is true iff the unary-expression is appearing as the
6920 operand of the `&' operator. CAST_P is true if this expression is
6921 the target of a cast.
6922
6923 Returns a representation of the expression. */
6924
6925 static tree
6926 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6927 bool decltype_p, cp_id_kind * pidk)
6928 {
6929 cp_token *token;
6930 enum tree_code unary_operator;
6931
6932 /* Peek at the next token. */
6933 token = cp_lexer_peek_token (parser->lexer);
6934 /* Some keywords give away the kind of expression. */
6935 if (token->type == CPP_KEYWORD)
6936 {
6937 enum rid keyword = token->keyword;
6938
6939 switch (keyword)
6940 {
6941 case RID_ALIGNOF:
6942 case RID_SIZEOF:
6943 {
6944 tree operand, ret;
6945 enum tree_code op;
6946 location_t first_loc;
6947
6948 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6949 /* Consume the token. */
6950 cp_lexer_consume_token (parser->lexer);
6951 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6952 /* Parse the operand. */
6953 operand = cp_parser_sizeof_operand (parser, keyword);
6954
6955 if (TYPE_P (operand))
6956 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6957 else
6958 {
6959 /* ISO C++ defines alignof only with types, not with
6960 expressions. So pedwarn if alignof is used with a non-
6961 type expression. However, __alignof__ is ok. */
6962 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6963 pedwarn (token->location, OPT_Wpedantic,
6964 "ISO C++ does not allow %<alignof%> "
6965 "with a non-type");
6966
6967 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6968 }
6969 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6970 SIZEOF_EXPR with the original operand. */
6971 if (op == SIZEOF_EXPR && ret != error_mark_node)
6972 {
6973 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6974 {
6975 if (!processing_template_decl && TYPE_P (operand))
6976 {
6977 ret = build_min (SIZEOF_EXPR, size_type_node,
6978 build1 (NOP_EXPR, operand,
6979 error_mark_node));
6980 SIZEOF_EXPR_TYPE_P (ret) = 1;
6981 }
6982 else
6983 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6984 TREE_SIDE_EFFECTS (ret) = 0;
6985 TREE_READONLY (ret) = 1;
6986 }
6987 SET_EXPR_LOCATION (ret, first_loc);
6988 }
6989 return ret;
6990 }
6991
6992 case RID_NEW:
6993 return cp_parser_new_expression (parser);
6994
6995 case RID_DELETE:
6996 return cp_parser_delete_expression (parser);
6997
6998 case RID_EXTENSION:
6999 {
7000 /* The saved value of the PEDANTIC flag. */
7001 int saved_pedantic;
7002 tree expr;
7003
7004 /* Save away the PEDANTIC flag. */
7005 cp_parser_extension_opt (parser, &saved_pedantic);
7006 /* Parse the cast-expression. */
7007 expr = cp_parser_simple_cast_expression (parser);
7008 /* Restore the PEDANTIC flag. */
7009 pedantic = saved_pedantic;
7010
7011 return expr;
7012 }
7013
7014 case RID_REALPART:
7015 case RID_IMAGPART:
7016 {
7017 tree expression;
7018
7019 /* Consume the `__real__' or `__imag__' token. */
7020 cp_lexer_consume_token (parser->lexer);
7021 /* Parse the cast-expression. */
7022 expression = cp_parser_simple_cast_expression (parser);
7023 /* Create the complete representation. */
7024 return build_x_unary_op (token->location,
7025 (keyword == RID_REALPART
7026 ? REALPART_EXPR : IMAGPART_EXPR),
7027 expression,
7028 tf_warning_or_error);
7029 }
7030 break;
7031
7032 case RID_TRANSACTION_ATOMIC:
7033 case RID_TRANSACTION_RELAXED:
7034 return cp_parser_transaction_expression (parser, keyword);
7035
7036 case RID_NOEXCEPT:
7037 {
7038 tree expr;
7039 const char *saved_message;
7040 bool saved_integral_constant_expression_p;
7041 bool saved_non_integral_constant_expression_p;
7042 bool saved_greater_than_is_operator_p;
7043
7044 cp_lexer_consume_token (parser->lexer);
7045 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7046
7047 saved_message = parser->type_definition_forbidden_message;
7048 parser->type_definition_forbidden_message
7049 = G_("types may not be defined in %<noexcept%> expressions");
7050
7051 saved_integral_constant_expression_p
7052 = parser->integral_constant_expression_p;
7053 saved_non_integral_constant_expression_p
7054 = parser->non_integral_constant_expression_p;
7055 parser->integral_constant_expression_p = false;
7056
7057 saved_greater_than_is_operator_p
7058 = parser->greater_than_is_operator_p;
7059 parser->greater_than_is_operator_p = true;
7060
7061 ++cp_unevaluated_operand;
7062 ++c_inhibit_evaluation_warnings;
7063 expr = cp_parser_expression (parser, false, NULL);
7064 --c_inhibit_evaluation_warnings;
7065 --cp_unevaluated_operand;
7066
7067 parser->greater_than_is_operator_p
7068 = saved_greater_than_is_operator_p;
7069
7070 parser->integral_constant_expression_p
7071 = saved_integral_constant_expression_p;
7072 parser->non_integral_constant_expression_p
7073 = saved_non_integral_constant_expression_p;
7074
7075 parser->type_definition_forbidden_message = saved_message;
7076
7077 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7078 return finish_noexcept_expr (expr, tf_warning_or_error);
7079 }
7080
7081 default:
7082 break;
7083 }
7084 }
7085
7086 /* Look for the `:: new' and `:: delete', which also signal the
7087 beginning of a new-expression, or delete-expression,
7088 respectively. If the next token is `::', then it might be one of
7089 these. */
7090 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7091 {
7092 enum rid keyword;
7093
7094 /* See if the token after the `::' is one of the keywords in
7095 which we're interested. */
7096 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7097 /* If it's `new', we have a new-expression. */
7098 if (keyword == RID_NEW)
7099 return cp_parser_new_expression (parser);
7100 /* Similarly, for `delete'. */
7101 else if (keyword == RID_DELETE)
7102 return cp_parser_delete_expression (parser);
7103 }
7104
7105 /* Look for a unary operator. */
7106 unary_operator = cp_parser_unary_operator (token);
7107 /* The `++' and `--' operators can be handled similarly, even though
7108 they are not technically unary-operators in the grammar. */
7109 if (unary_operator == ERROR_MARK)
7110 {
7111 if (token->type == CPP_PLUS_PLUS)
7112 unary_operator = PREINCREMENT_EXPR;
7113 else if (token->type == CPP_MINUS_MINUS)
7114 unary_operator = PREDECREMENT_EXPR;
7115 /* Handle the GNU address-of-label extension. */
7116 else if (cp_parser_allow_gnu_extensions_p (parser)
7117 && token->type == CPP_AND_AND)
7118 {
7119 tree identifier;
7120 tree expression;
7121 location_t loc = token->location;
7122
7123 /* Consume the '&&' token. */
7124 cp_lexer_consume_token (parser->lexer);
7125 /* Look for the identifier. */
7126 identifier = cp_parser_identifier (parser);
7127 /* Create an expression representing the address. */
7128 expression = finish_label_address_expr (identifier, loc);
7129 if (cp_parser_non_integral_constant_expression (parser,
7130 NIC_ADDR_LABEL))
7131 expression = error_mark_node;
7132 return expression;
7133 }
7134 }
7135 if (unary_operator != ERROR_MARK)
7136 {
7137 tree cast_expression;
7138 tree expression = error_mark_node;
7139 non_integral_constant non_constant_p = NIC_NONE;
7140 location_t loc = token->location;
7141 tsubst_flags_t complain = complain_flags (decltype_p);
7142
7143 /* Consume the operator token. */
7144 token = cp_lexer_consume_token (parser->lexer);
7145 /* Parse the cast-expression. */
7146 cast_expression
7147 = cp_parser_cast_expression (parser,
7148 unary_operator == ADDR_EXPR,
7149 /*cast_p=*/false,
7150 /*decltype*/false,
7151 pidk);
7152 /* Now, build an appropriate representation. */
7153 switch (unary_operator)
7154 {
7155 case INDIRECT_REF:
7156 non_constant_p = NIC_STAR;
7157 expression = build_x_indirect_ref (loc, cast_expression,
7158 RO_UNARY_STAR,
7159 complain);
7160 break;
7161
7162 case ADDR_EXPR:
7163 non_constant_p = NIC_ADDR;
7164 /* Fall through. */
7165 case BIT_NOT_EXPR:
7166 expression = build_x_unary_op (loc, unary_operator,
7167 cast_expression,
7168 complain);
7169 break;
7170
7171 case PREINCREMENT_EXPR:
7172 case PREDECREMENT_EXPR:
7173 non_constant_p = unary_operator == PREINCREMENT_EXPR
7174 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7175 /* Fall through. */
7176 case UNARY_PLUS_EXPR:
7177 case NEGATE_EXPR:
7178 case TRUTH_NOT_EXPR:
7179 expression = finish_unary_op_expr (loc, unary_operator,
7180 cast_expression, complain);
7181 break;
7182
7183 default:
7184 gcc_unreachable ();
7185 }
7186
7187 if (non_constant_p != NIC_NONE
7188 && cp_parser_non_integral_constant_expression (parser,
7189 non_constant_p))
7190 expression = error_mark_node;
7191
7192 return expression;
7193 }
7194
7195 return cp_parser_postfix_expression (parser, address_p, cast_p,
7196 /*member_access_only_p=*/false,
7197 decltype_p,
7198 pidk);
7199 }
7200
7201 static inline tree
7202 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7203 cp_id_kind * pidk)
7204 {
7205 return cp_parser_unary_expression (parser, address_p, cast_p,
7206 /*decltype*/false, pidk);
7207 }
7208
7209 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7210 unary-operator, the corresponding tree code is returned. */
7211
7212 static enum tree_code
7213 cp_parser_unary_operator (cp_token* token)
7214 {
7215 switch (token->type)
7216 {
7217 case CPP_MULT:
7218 return INDIRECT_REF;
7219
7220 case CPP_AND:
7221 return ADDR_EXPR;
7222
7223 case CPP_PLUS:
7224 return UNARY_PLUS_EXPR;
7225
7226 case CPP_MINUS:
7227 return NEGATE_EXPR;
7228
7229 case CPP_NOT:
7230 return TRUTH_NOT_EXPR;
7231
7232 case CPP_COMPL:
7233 return BIT_NOT_EXPR;
7234
7235 default:
7236 return ERROR_MARK;
7237 }
7238 }
7239
7240 /* Parse a new-expression.
7241
7242 new-expression:
7243 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7244 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7245
7246 Returns a representation of the expression. */
7247
7248 static tree
7249 cp_parser_new_expression (cp_parser* parser)
7250 {
7251 bool global_scope_p;
7252 vec<tree, va_gc> *placement;
7253 tree type;
7254 vec<tree, va_gc> *initializer;
7255 tree nelts = NULL_TREE;
7256 tree ret;
7257
7258 /* Look for the optional `::' operator. */
7259 global_scope_p
7260 = (cp_parser_global_scope_opt (parser,
7261 /*current_scope_valid_p=*/false)
7262 != NULL_TREE);
7263 /* Look for the `new' operator. */
7264 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7265 /* There's no easy way to tell a new-placement from the
7266 `( type-id )' construct. */
7267 cp_parser_parse_tentatively (parser);
7268 /* Look for a new-placement. */
7269 placement = cp_parser_new_placement (parser);
7270 /* If that didn't work out, there's no new-placement. */
7271 if (!cp_parser_parse_definitely (parser))
7272 {
7273 if (placement != NULL)
7274 release_tree_vector (placement);
7275 placement = NULL;
7276 }
7277
7278 /* If the next token is a `(', then we have a parenthesized
7279 type-id. */
7280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7281 {
7282 cp_token *token;
7283 const char *saved_message = parser->type_definition_forbidden_message;
7284
7285 /* Consume the `('. */
7286 cp_lexer_consume_token (parser->lexer);
7287
7288 /* Parse the type-id. */
7289 parser->type_definition_forbidden_message
7290 = G_("types may not be defined in a new-expression");
7291 type = cp_parser_type_id (parser);
7292 parser->type_definition_forbidden_message = saved_message;
7293
7294 /* Look for the closing `)'. */
7295 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7296 token = cp_lexer_peek_token (parser->lexer);
7297 /* There should not be a direct-new-declarator in this production,
7298 but GCC used to allowed this, so we check and emit a sensible error
7299 message for this case. */
7300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7301 {
7302 error_at (token->location,
7303 "array bound forbidden after parenthesized type-id");
7304 inform (token->location,
7305 "try removing the parentheses around the type-id");
7306 cp_parser_direct_new_declarator (parser);
7307 }
7308 }
7309 /* Otherwise, there must be a new-type-id. */
7310 else
7311 type = cp_parser_new_type_id (parser, &nelts);
7312
7313 /* If the next token is a `(' or '{', then we have a new-initializer. */
7314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7315 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7316 initializer = cp_parser_new_initializer (parser);
7317 else
7318 initializer = NULL;
7319
7320 /* A new-expression may not appear in an integral constant
7321 expression. */
7322 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7323 ret = error_mark_node;
7324 else
7325 {
7326 /* Create a representation of the new-expression. */
7327 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7328 tf_warning_or_error);
7329 }
7330
7331 if (placement != NULL)
7332 release_tree_vector (placement);
7333 if (initializer != NULL)
7334 release_tree_vector (initializer);
7335
7336 return ret;
7337 }
7338
7339 /* Parse a new-placement.
7340
7341 new-placement:
7342 ( expression-list )
7343
7344 Returns the same representation as for an expression-list. */
7345
7346 static vec<tree, va_gc> *
7347 cp_parser_new_placement (cp_parser* parser)
7348 {
7349 vec<tree, va_gc> *expression_list;
7350
7351 /* Parse the expression-list. */
7352 expression_list = (cp_parser_parenthesized_expression_list
7353 (parser, non_attr, /*cast_p=*/false,
7354 /*allow_expansion_p=*/true,
7355 /*non_constant_p=*/NULL));
7356
7357 return expression_list;
7358 }
7359
7360 /* Parse a new-type-id.
7361
7362 new-type-id:
7363 type-specifier-seq new-declarator [opt]
7364
7365 Returns the TYPE allocated. If the new-type-id indicates an array
7366 type, *NELTS is set to the number of elements in the last array
7367 bound; the TYPE will not include the last array bound. */
7368
7369 static tree
7370 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7371 {
7372 cp_decl_specifier_seq type_specifier_seq;
7373 cp_declarator *new_declarator;
7374 cp_declarator *declarator;
7375 cp_declarator *outer_declarator;
7376 const char *saved_message;
7377
7378 /* The type-specifier sequence must not contain type definitions.
7379 (It cannot contain declarations of new types either, but if they
7380 are not definitions we will catch that because they are not
7381 complete.) */
7382 saved_message = parser->type_definition_forbidden_message;
7383 parser->type_definition_forbidden_message
7384 = G_("types may not be defined in a new-type-id");
7385 /* Parse the type-specifier-seq. */
7386 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7387 /*is_trailing_return=*/false,
7388 &type_specifier_seq);
7389 /* Restore the old message. */
7390 parser->type_definition_forbidden_message = saved_message;
7391
7392 if (type_specifier_seq.type == error_mark_node)
7393 return error_mark_node;
7394
7395 /* Parse the new-declarator. */
7396 new_declarator = cp_parser_new_declarator_opt (parser);
7397
7398 /* Determine the number of elements in the last array dimension, if
7399 any. */
7400 *nelts = NULL_TREE;
7401 /* Skip down to the last array dimension. */
7402 declarator = new_declarator;
7403 outer_declarator = NULL;
7404 while (declarator && (declarator->kind == cdk_pointer
7405 || declarator->kind == cdk_ptrmem))
7406 {
7407 outer_declarator = declarator;
7408 declarator = declarator->declarator;
7409 }
7410 while (declarator
7411 && declarator->kind == cdk_array
7412 && declarator->declarator
7413 && declarator->declarator->kind == cdk_array)
7414 {
7415 outer_declarator = declarator;
7416 declarator = declarator->declarator;
7417 }
7418
7419 if (declarator && declarator->kind == cdk_array)
7420 {
7421 *nelts = declarator->u.array.bounds;
7422 if (*nelts == error_mark_node)
7423 *nelts = integer_one_node;
7424
7425 if (outer_declarator)
7426 outer_declarator->declarator = declarator->declarator;
7427 else
7428 new_declarator = NULL;
7429 }
7430
7431 return groktypename (&type_specifier_seq, new_declarator, false);
7432 }
7433
7434 /* Parse an (optional) new-declarator.
7435
7436 new-declarator:
7437 ptr-operator new-declarator [opt]
7438 direct-new-declarator
7439
7440 Returns the declarator. */
7441
7442 static cp_declarator *
7443 cp_parser_new_declarator_opt (cp_parser* parser)
7444 {
7445 enum tree_code code;
7446 tree type, std_attributes = NULL_TREE;
7447 cp_cv_quals cv_quals;
7448
7449 /* We don't know if there's a ptr-operator next, or not. */
7450 cp_parser_parse_tentatively (parser);
7451 /* Look for a ptr-operator. */
7452 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7453 /* If that worked, look for more new-declarators. */
7454 if (cp_parser_parse_definitely (parser))
7455 {
7456 cp_declarator *declarator;
7457
7458 /* Parse another optional declarator. */
7459 declarator = cp_parser_new_declarator_opt (parser);
7460
7461 declarator = cp_parser_make_indirect_declarator
7462 (code, type, cv_quals, declarator, std_attributes);
7463
7464 return declarator;
7465 }
7466
7467 /* If the next token is a `[', there is a direct-new-declarator. */
7468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7469 return cp_parser_direct_new_declarator (parser);
7470
7471 return NULL;
7472 }
7473
7474 /* Parse a direct-new-declarator.
7475
7476 direct-new-declarator:
7477 [ expression ]
7478 direct-new-declarator [constant-expression]
7479
7480 */
7481
7482 static cp_declarator *
7483 cp_parser_direct_new_declarator (cp_parser* parser)
7484 {
7485 cp_declarator *declarator = NULL;
7486
7487 while (true)
7488 {
7489 tree expression;
7490 cp_token *token;
7491
7492 /* Look for the opening `['. */
7493 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7494
7495 token = cp_lexer_peek_token (parser->lexer);
7496 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7497 /* The standard requires that the expression have integral
7498 type. DR 74 adds enumeration types. We believe that the
7499 real intent is that these expressions be handled like the
7500 expression in a `switch' condition, which also allows
7501 classes with a single conversion to integral or
7502 enumeration type. */
7503 if (!processing_template_decl)
7504 {
7505 expression
7506 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7507 expression,
7508 /*complain=*/true);
7509 if (!expression)
7510 {
7511 error_at (token->location,
7512 "expression in new-declarator must have integral "
7513 "or enumeration type");
7514 expression = error_mark_node;
7515 }
7516 }
7517
7518 /* Look for the closing `]'. */
7519 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7520
7521 /* Add this bound to the declarator. */
7522 declarator = make_array_declarator (declarator, expression);
7523
7524 /* If the next token is not a `[', then there are no more
7525 bounds. */
7526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7527 break;
7528 }
7529
7530 return declarator;
7531 }
7532
7533 /* Parse a new-initializer.
7534
7535 new-initializer:
7536 ( expression-list [opt] )
7537 braced-init-list
7538
7539 Returns a representation of the expression-list. */
7540
7541 static vec<tree, va_gc> *
7542 cp_parser_new_initializer (cp_parser* parser)
7543 {
7544 vec<tree, va_gc> *expression_list;
7545
7546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7547 {
7548 tree t;
7549 bool expr_non_constant_p;
7550 cp_lexer_set_source_position (parser->lexer);
7551 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7552 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7553 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7554 expression_list = make_tree_vector_single (t);
7555 }
7556 else
7557 expression_list = (cp_parser_parenthesized_expression_list
7558 (parser, non_attr, /*cast_p=*/false,
7559 /*allow_expansion_p=*/true,
7560 /*non_constant_p=*/NULL));
7561
7562 return expression_list;
7563 }
7564
7565 /* Parse a delete-expression.
7566
7567 delete-expression:
7568 :: [opt] delete cast-expression
7569 :: [opt] delete [ ] cast-expression
7570
7571 Returns a representation of the expression. */
7572
7573 static tree
7574 cp_parser_delete_expression (cp_parser* parser)
7575 {
7576 bool global_scope_p;
7577 bool array_p;
7578 tree expression;
7579
7580 /* Look for the optional `::' operator. */
7581 global_scope_p
7582 = (cp_parser_global_scope_opt (parser,
7583 /*current_scope_valid_p=*/false)
7584 != NULL_TREE);
7585 /* Look for the `delete' keyword. */
7586 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7587 /* See if the array syntax is in use. */
7588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7589 {
7590 /* Consume the `[' token. */
7591 cp_lexer_consume_token (parser->lexer);
7592 /* Look for the `]' token. */
7593 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7594 /* Remember that this is the `[]' construct. */
7595 array_p = true;
7596 }
7597 else
7598 array_p = false;
7599
7600 /* Parse the cast-expression. */
7601 expression = cp_parser_simple_cast_expression (parser);
7602
7603 /* A delete-expression may not appear in an integral constant
7604 expression. */
7605 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7606 return error_mark_node;
7607
7608 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7609 tf_warning_or_error);
7610 }
7611
7612 /* Returns true if TOKEN may start a cast-expression and false
7613 otherwise. */
7614
7615 static bool
7616 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7617 {
7618 cp_token *token = cp_lexer_peek_token (parser->lexer);
7619 switch (token->type)
7620 {
7621 case CPP_COMMA:
7622 case CPP_SEMICOLON:
7623 case CPP_QUERY:
7624 case CPP_COLON:
7625 case CPP_CLOSE_SQUARE:
7626 case CPP_CLOSE_PAREN:
7627 case CPP_CLOSE_BRACE:
7628 case CPP_OPEN_BRACE:
7629 case CPP_DOT:
7630 case CPP_DOT_STAR:
7631 case CPP_DEREF:
7632 case CPP_DEREF_STAR:
7633 case CPP_DIV:
7634 case CPP_MOD:
7635 case CPP_LSHIFT:
7636 case CPP_RSHIFT:
7637 case CPP_LESS:
7638 case CPP_GREATER:
7639 case CPP_LESS_EQ:
7640 case CPP_GREATER_EQ:
7641 case CPP_EQ_EQ:
7642 case CPP_NOT_EQ:
7643 case CPP_EQ:
7644 case CPP_MULT_EQ:
7645 case CPP_DIV_EQ:
7646 case CPP_MOD_EQ:
7647 case CPP_PLUS_EQ:
7648 case CPP_MINUS_EQ:
7649 case CPP_RSHIFT_EQ:
7650 case CPP_LSHIFT_EQ:
7651 case CPP_AND_EQ:
7652 case CPP_XOR_EQ:
7653 case CPP_OR_EQ:
7654 case CPP_XOR:
7655 case CPP_OR:
7656 case CPP_OR_OR:
7657 case CPP_EOF:
7658 return false;
7659
7660 case CPP_OPEN_PAREN:
7661 /* In ((type ()) () the last () isn't a valid cast-expression,
7662 so the whole must be parsed as postfix-expression. */
7663 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7664 != CPP_CLOSE_PAREN;
7665
7666 /* '[' may start a primary-expression in obj-c++. */
7667 case CPP_OPEN_SQUARE:
7668 return c_dialect_objc ();
7669
7670 default:
7671 return true;
7672 }
7673 }
7674
7675 /* Parse a cast-expression.
7676
7677 cast-expression:
7678 unary-expression
7679 ( type-id ) cast-expression
7680
7681 ADDRESS_P is true iff the unary-expression is appearing as the
7682 operand of the `&' operator. CAST_P is true if this expression is
7683 the target of a cast.
7684
7685 Returns a representation of the expression. */
7686
7687 static tree
7688 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7689 bool decltype_p, cp_id_kind * pidk)
7690 {
7691 /* If it's a `(', then we might be looking at a cast. */
7692 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7693 {
7694 tree type = NULL_TREE;
7695 tree expr = NULL_TREE;
7696 bool cast_expression_p;
7697 const char *saved_message;
7698
7699 /* There's no way to know yet whether or not this is a cast.
7700 For example, `(int (3))' is a unary-expression, while `(int)
7701 3' is a cast. So, we resort to parsing tentatively. */
7702 cp_parser_parse_tentatively (parser);
7703 /* Types may not be defined in a cast. */
7704 saved_message = parser->type_definition_forbidden_message;
7705 parser->type_definition_forbidden_message
7706 = G_("types may not be defined in casts");
7707 /* Consume the `('. */
7708 cp_lexer_consume_token (parser->lexer);
7709 /* A very tricky bit is that `(struct S) { 3 }' is a
7710 compound-literal (which we permit in C++ as an extension).
7711 But, that construct is not a cast-expression -- it is a
7712 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7713 is legal; if the compound-literal were a cast-expression,
7714 you'd need an extra set of parentheses.) But, if we parse
7715 the type-id, and it happens to be a class-specifier, then we
7716 will commit to the parse at that point, because we cannot
7717 undo the action that is done when creating a new class. So,
7718 then we cannot back up and do a postfix-expression.
7719 Another tricky case is the following (c++/29234):
7720
7721 struct S { void operator () (); };
7722
7723 void foo ()
7724 {
7725 ( S()() );
7726 }
7727
7728 As a type-id we parse the parenthesized S()() as a function
7729 returning a function, groktypename complains and we cannot
7730 back up in this case either.
7731
7732 Therefore, we scan ahead to the closing `)', and check to see
7733 if the tokens after the `)' can start a cast-expression. Otherwise
7734 we are dealing with an unary-expression, a postfix-expression
7735 or something else.
7736
7737 Save tokens so that we can put them back. */
7738 cp_lexer_save_tokens (parser->lexer);
7739
7740 /* We may be looking at a cast-expression. */
7741 cast_expression_p
7742 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7743 /*consume_paren=*/true)
7744 && cp_parser_tokens_start_cast_expression (parser));
7745
7746 /* Roll back the tokens we skipped. */
7747 cp_lexer_rollback_tokens (parser->lexer);
7748 /* If we aren't looking at a cast-expression, simulate an error so
7749 that the call to cp_parser_parse_definitely below will fail. */
7750 if (!cast_expression_p)
7751 cp_parser_simulate_error (parser);
7752 else
7753 {
7754 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7755 parser->in_type_id_in_expr_p = true;
7756 /* Look for the type-id. */
7757 type = cp_parser_type_id (parser);
7758 /* Look for the closing `)'. */
7759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7760 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7761 }
7762
7763 /* Restore the saved message. */
7764 parser->type_definition_forbidden_message = saved_message;
7765
7766 /* At this point this can only be either a cast or a
7767 parenthesized ctor such as `(T ())' that looks like a cast to
7768 function returning T. */
7769 if (!cp_parser_error_occurred (parser))
7770 {
7771 cp_parser_parse_definitely (parser);
7772 expr = cp_parser_cast_expression (parser,
7773 /*address_p=*/false,
7774 /*cast_p=*/true,
7775 /*decltype_p=*/false,
7776 pidk);
7777
7778 /* Warn about old-style casts, if so requested. */
7779 if (warn_old_style_cast
7780 && !in_system_header_at (input_location)
7781 && !VOID_TYPE_P (type)
7782 && current_lang_name != lang_name_c)
7783 warning (OPT_Wold_style_cast, "use of old-style cast");
7784
7785 /* Only type conversions to integral or enumeration types
7786 can be used in constant-expressions. */
7787 if (!cast_valid_in_integral_constant_expression_p (type)
7788 && cp_parser_non_integral_constant_expression (parser,
7789 NIC_CAST))
7790 return error_mark_node;
7791
7792 /* Perform the cast. */
7793 expr = build_c_cast (input_location, type, expr);
7794 return expr;
7795 }
7796 else
7797 cp_parser_abort_tentative_parse (parser);
7798 }
7799
7800 /* If we get here, then it's not a cast, so it must be a
7801 unary-expression. */
7802 return cp_parser_unary_expression (parser, address_p, cast_p,
7803 decltype_p, pidk);
7804 }
7805
7806 /* Parse a binary expression of the general form:
7807
7808 pm-expression:
7809 cast-expression
7810 pm-expression .* cast-expression
7811 pm-expression ->* cast-expression
7812
7813 multiplicative-expression:
7814 pm-expression
7815 multiplicative-expression * pm-expression
7816 multiplicative-expression / pm-expression
7817 multiplicative-expression % pm-expression
7818
7819 additive-expression:
7820 multiplicative-expression
7821 additive-expression + multiplicative-expression
7822 additive-expression - multiplicative-expression
7823
7824 shift-expression:
7825 additive-expression
7826 shift-expression << additive-expression
7827 shift-expression >> additive-expression
7828
7829 relational-expression:
7830 shift-expression
7831 relational-expression < shift-expression
7832 relational-expression > shift-expression
7833 relational-expression <= shift-expression
7834 relational-expression >= shift-expression
7835
7836 GNU Extension:
7837
7838 relational-expression:
7839 relational-expression <? shift-expression
7840 relational-expression >? shift-expression
7841
7842 equality-expression:
7843 relational-expression
7844 equality-expression == relational-expression
7845 equality-expression != relational-expression
7846
7847 and-expression:
7848 equality-expression
7849 and-expression & equality-expression
7850
7851 exclusive-or-expression:
7852 and-expression
7853 exclusive-or-expression ^ and-expression
7854
7855 inclusive-or-expression:
7856 exclusive-or-expression
7857 inclusive-or-expression | exclusive-or-expression
7858
7859 logical-and-expression:
7860 inclusive-or-expression
7861 logical-and-expression && inclusive-or-expression
7862
7863 logical-or-expression:
7864 logical-and-expression
7865 logical-or-expression || logical-and-expression
7866
7867 All these are implemented with a single function like:
7868
7869 binary-expression:
7870 simple-cast-expression
7871 binary-expression <token> binary-expression
7872
7873 CAST_P is true if this expression is the target of a cast.
7874
7875 The binops_by_token map is used to get the tree codes for each <token> type.
7876 binary-expressions are associated according to a precedence table. */
7877
7878 #define TOKEN_PRECEDENCE(token) \
7879 (((token->type == CPP_GREATER \
7880 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7881 && !parser->greater_than_is_operator_p) \
7882 ? PREC_NOT_OPERATOR \
7883 : binops_by_token[token->type].prec)
7884
7885 static tree
7886 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7887 bool no_toplevel_fold_p,
7888 bool decltype_p,
7889 enum cp_parser_prec prec,
7890 cp_id_kind * pidk)
7891 {
7892 cp_parser_expression_stack stack;
7893 cp_parser_expression_stack_entry *sp = &stack[0];
7894 cp_parser_expression_stack_entry current;
7895 tree rhs;
7896 cp_token *token;
7897 enum tree_code rhs_type;
7898 enum cp_parser_prec new_prec, lookahead_prec;
7899 tree overload;
7900
7901 /* Parse the first expression. */
7902 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7903 cast_p, decltype_p, pidk);
7904 current.lhs_type = ERROR_MARK;
7905 current.prec = prec;
7906
7907 if (cp_parser_error_occurred (parser))
7908 return error_mark_node;
7909
7910 for (;;)
7911 {
7912 /* Get an operator token. */
7913 token = cp_lexer_peek_token (parser->lexer);
7914
7915 if (warn_cxx0x_compat
7916 && token->type == CPP_RSHIFT
7917 && !parser->greater_than_is_operator_p)
7918 {
7919 if (warning_at (token->location, OPT_Wc__0x_compat,
7920 "%<>>%> operator is treated"
7921 " as two right angle brackets in C++11"))
7922 inform (token->location,
7923 "suggest parentheses around %<>>%> expression");
7924 }
7925
7926 new_prec = TOKEN_PRECEDENCE (token);
7927
7928 /* Popping an entry off the stack means we completed a subexpression:
7929 - either we found a token which is not an operator (`>' where it is not
7930 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7931 will happen repeatedly;
7932 - or, we found an operator which has lower priority. This is the case
7933 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7934 parsing `3 * 4'. */
7935 if (new_prec <= current.prec)
7936 {
7937 if (sp == stack)
7938 break;
7939 else
7940 goto pop;
7941 }
7942
7943 get_rhs:
7944 current.tree_type = binops_by_token[token->type].tree_type;
7945 current.loc = token->location;
7946
7947 /* We used the operator token. */
7948 cp_lexer_consume_token (parser->lexer);
7949
7950 /* For "false && x" or "true || x", x will never be executed;
7951 disable warnings while evaluating it. */
7952 if (current.tree_type == TRUTH_ANDIF_EXPR)
7953 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7954 else if (current.tree_type == TRUTH_ORIF_EXPR)
7955 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7956
7957 /* Extract another operand. It may be the RHS of this expression
7958 or the LHS of a new, higher priority expression. */
7959 rhs = cp_parser_simple_cast_expression (parser);
7960 rhs_type = ERROR_MARK;
7961
7962 /* Get another operator token. Look up its precedence to avoid
7963 building a useless (immediately popped) stack entry for common
7964 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7965 token = cp_lexer_peek_token (parser->lexer);
7966 lookahead_prec = TOKEN_PRECEDENCE (token);
7967 if (lookahead_prec > new_prec)
7968 {
7969 /* ... and prepare to parse the RHS of the new, higher priority
7970 expression. Since precedence levels on the stack are
7971 monotonically increasing, we do not have to care about
7972 stack overflows. */
7973 *sp = current;
7974 ++sp;
7975 current.lhs = rhs;
7976 current.lhs_type = rhs_type;
7977 current.prec = new_prec;
7978 new_prec = lookahead_prec;
7979 goto get_rhs;
7980
7981 pop:
7982 lookahead_prec = new_prec;
7983 /* If the stack is not empty, we have parsed into LHS the right side
7984 (`4' in the example above) of an expression we had suspended.
7985 We can use the information on the stack to recover the LHS (`3')
7986 from the stack together with the tree code (`MULT_EXPR'), and
7987 the precedence of the higher level subexpression
7988 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7989 which will be used to actually build the additive expression. */
7990 rhs = current.lhs;
7991 rhs_type = current.lhs_type;
7992 --sp;
7993 current = *sp;
7994 }
7995
7996 /* Undo the disabling of warnings done above. */
7997 if (current.tree_type == TRUTH_ANDIF_EXPR)
7998 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7999 else if (current.tree_type == TRUTH_ORIF_EXPR)
8000 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8001
8002 overload = NULL;
8003 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8004 ERROR_MARK for everything that is not a binary expression.
8005 This makes warn_about_parentheses miss some warnings that
8006 involve unary operators. For unary expressions we should
8007 pass the correct tree_code unless the unary expression was
8008 surrounded by parentheses.
8009 */
8010 if (no_toplevel_fold_p
8011 && lookahead_prec <= current.prec
8012 && sp == stack)
8013 current.lhs = build2 (current.tree_type,
8014 TREE_CODE_CLASS (current.tree_type)
8015 == tcc_comparison
8016 ? boolean_type_node : TREE_TYPE (current.lhs),
8017 current.lhs, rhs);
8018 else
8019 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8020 current.lhs, current.lhs_type,
8021 rhs, rhs_type, &overload,
8022 complain_flags (decltype_p));
8023 current.lhs_type = current.tree_type;
8024 if (EXPR_P (current.lhs))
8025 SET_EXPR_LOCATION (current.lhs, current.loc);
8026
8027 /* If the binary operator required the use of an overloaded operator,
8028 then this expression cannot be an integral constant-expression.
8029 An overloaded operator can be used even if both operands are
8030 otherwise permissible in an integral constant-expression if at
8031 least one of the operands is of enumeration type. */
8032
8033 if (overload
8034 && cp_parser_non_integral_constant_expression (parser,
8035 NIC_OVERLOADED))
8036 return error_mark_node;
8037 }
8038
8039 return current.lhs;
8040 }
8041
8042 static tree
8043 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8044 bool no_toplevel_fold_p,
8045 enum cp_parser_prec prec,
8046 cp_id_kind * pidk)
8047 {
8048 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8049 /*decltype*/false, prec, pidk);
8050 }
8051
8052 /* Parse the `? expression : assignment-expression' part of a
8053 conditional-expression. The LOGICAL_OR_EXPR is the
8054 logical-or-expression that started the conditional-expression.
8055 Returns a representation of the entire conditional-expression.
8056
8057 This routine is used by cp_parser_assignment_expression.
8058
8059 ? expression : assignment-expression
8060
8061 GNU Extensions:
8062
8063 ? : assignment-expression */
8064
8065 static tree
8066 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8067 {
8068 tree expr;
8069 tree assignment_expr;
8070 struct cp_token *token;
8071 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8072
8073 /* Consume the `?' token. */
8074 cp_lexer_consume_token (parser->lexer);
8075 token = cp_lexer_peek_token (parser->lexer);
8076 if (cp_parser_allow_gnu_extensions_p (parser)
8077 && token->type == CPP_COLON)
8078 {
8079 pedwarn (token->location, OPT_Wpedantic,
8080 "ISO C++ does not allow ?: with omitted middle operand");
8081 /* Implicit true clause. */
8082 expr = NULL_TREE;
8083 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8084 warn_for_omitted_condop (token->location, logical_or_expr);
8085 }
8086 else
8087 {
8088 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8089 parser->colon_corrects_to_scope_p = false;
8090 /* Parse the expression. */
8091 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8092 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8093 c_inhibit_evaluation_warnings +=
8094 ((logical_or_expr == truthvalue_true_node)
8095 - (logical_or_expr == truthvalue_false_node));
8096 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8097 }
8098
8099 /* The next token should be a `:'. */
8100 cp_parser_require (parser, CPP_COLON, RT_COLON);
8101 /* Parse the assignment-expression. */
8102 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8103 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8104
8105 /* Build the conditional-expression. */
8106 return build_x_conditional_expr (loc, logical_or_expr,
8107 expr,
8108 assignment_expr,
8109 tf_warning_or_error);
8110 }
8111
8112 /* Parse an assignment-expression.
8113
8114 assignment-expression:
8115 conditional-expression
8116 logical-or-expression assignment-operator assignment_expression
8117 throw-expression
8118
8119 CAST_P is true if this expression is the target of a cast.
8120 DECLTYPE_P is true if this expression is the operand of decltype.
8121
8122 Returns a representation for the expression. */
8123
8124 static tree
8125 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8126 bool decltype_p, cp_id_kind * pidk)
8127 {
8128 tree expr;
8129
8130 /* If the next token is the `throw' keyword, then we're looking at
8131 a throw-expression. */
8132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8133 expr = cp_parser_throw_expression (parser);
8134 /* Otherwise, it must be that we are looking at a
8135 logical-or-expression. */
8136 else
8137 {
8138 /* Parse the binary expressions (logical-or-expression). */
8139 expr = cp_parser_binary_expression (parser, cast_p, false,
8140 decltype_p,
8141 PREC_NOT_OPERATOR, pidk);
8142 /* If the next token is a `?' then we're actually looking at a
8143 conditional-expression. */
8144 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8145 return cp_parser_question_colon_clause (parser, expr);
8146 else
8147 {
8148 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8149
8150 /* If it's an assignment-operator, we're using the second
8151 production. */
8152 enum tree_code assignment_operator
8153 = cp_parser_assignment_operator_opt (parser);
8154 if (assignment_operator != ERROR_MARK)
8155 {
8156 bool non_constant_p;
8157 location_t saved_input_location;
8158
8159 /* Parse the right-hand side of the assignment. */
8160 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8161
8162 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8164
8165 /* An assignment may not appear in a
8166 constant-expression. */
8167 if (cp_parser_non_integral_constant_expression (parser,
8168 NIC_ASSIGNMENT))
8169 return error_mark_node;
8170 /* Build the assignment expression. Its default
8171 location is the location of the '=' token. */
8172 saved_input_location = input_location;
8173 input_location = loc;
8174 expr = build_x_modify_expr (loc, expr,
8175 assignment_operator,
8176 rhs,
8177 complain_flags (decltype_p));
8178 input_location = saved_input_location;
8179 }
8180 }
8181 }
8182
8183 return expr;
8184 }
8185
8186 static tree
8187 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8188 cp_id_kind * pidk)
8189 {
8190 return cp_parser_assignment_expression (parser, cast_p,
8191 /*decltype*/false, pidk);
8192 }
8193
8194 /* Parse an (optional) assignment-operator.
8195
8196 assignment-operator: one of
8197 = *= /= %= += -= >>= <<= &= ^= |=
8198
8199 GNU Extension:
8200
8201 assignment-operator: one of
8202 <?= >?=
8203
8204 If the next token is an assignment operator, the corresponding tree
8205 code is returned, and the token is consumed. For example, for
8206 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8207 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8208 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8209 operator, ERROR_MARK is returned. */
8210
8211 static enum tree_code
8212 cp_parser_assignment_operator_opt (cp_parser* parser)
8213 {
8214 enum tree_code op;
8215 cp_token *token;
8216
8217 /* Peek at the next token. */
8218 token = cp_lexer_peek_token (parser->lexer);
8219
8220 switch (token->type)
8221 {
8222 case CPP_EQ:
8223 op = NOP_EXPR;
8224 break;
8225
8226 case CPP_MULT_EQ:
8227 op = MULT_EXPR;
8228 break;
8229
8230 case CPP_DIV_EQ:
8231 op = TRUNC_DIV_EXPR;
8232 break;
8233
8234 case CPP_MOD_EQ:
8235 op = TRUNC_MOD_EXPR;
8236 break;
8237
8238 case CPP_PLUS_EQ:
8239 op = PLUS_EXPR;
8240 break;
8241
8242 case CPP_MINUS_EQ:
8243 op = MINUS_EXPR;
8244 break;
8245
8246 case CPP_RSHIFT_EQ:
8247 op = RSHIFT_EXPR;
8248 break;
8249
8250 case CPP_LSHIFT_EQ:
8251 op = LSHIFT_EXPR;
8252 break;
8253
8254 case CPP_AND_EQ:
8255 op = BIT_AND_EXPR;
8256 break;
8257
8258 case CPP_XOR_EQ:
8259 op = BIT_XOR_EXPR;
8260 break;
8261
8262 case CPP_OR_EQ:
8263 op = BIT_IOR_EXPR;
8264 break;
8265
8266 default:
8267 /* Nothing else is an assignment operator. */
8268 op = ERROR_MARK;
8269 }
8270
8271 /* If it was an assignment operator, consume it. */
8272 if (op != ERROR_MARK)
8273 cp_lexer_consume_token (parser->lexer);
8274
8275 return op;
8276 }
8277
8278 /* Parse an expression.
8279
8280 expression:
8281 assignment-expression
8282 expression , assignment-expression
8283
8284 CAST_P is true if this expression is the target of a cast.
8285 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8286 except possibly parenthesized or on the RHS of a comma (N3276).
8287
8288 Returns a representation of the expression. */
8289
8290 static tree
8291 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8292 cp_id_kind * pidk)
8293 {
8294 tree expression = NULL_TREE;
8295 location_t loc = UNKNOWN_LOCATION;
8296
8297 while (true)
8298 {
8299 tree assignment_expression;
8300
8301 /* Parse the next assignment-expression. */
8302 assignment_expression
8303 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8304
8305 /* We don't create a temporary for a call that is the immediate operand
8306 of decltype or on the RHS of a comma. But when we see a comma, we
8307 need to create a temporary for a call on the LHS. */
8308 if (decltype_p && !processing_template_decl
8309 && TREE_CODE (assignment_expression) == CALL_EXPR
8310 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8311 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8312 assignment_expression
8313 = build_cplus_new (TREE_TYPE (assignment_expression),
8314 assignment_expression, tf_warning_or_error);
8315
8316 /* If this is the first assignment-expression, we can just
8317 save it away. */
8318 if (!expression)
8319 expression = assignment_expression;
8320 else
8321 expression = build_x_compound_expr (loc, expression,
8322 assignment_expression,
8323 complain_flags (decltype_p));
8324 /* If the next token is not a comma, then we are done with the
8325 expression. */
8326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8327 break;
8328 /* Consume the `,'. */
8329 loc = cp_lexer_peek_token (parser->lexer)->location;
8330 cp_lexer_consume_token (parser->lexer);
8331 /* A comma operator cannot appear in a constant-expression. */
8332 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8333 expression = error_mark_node;
8334 }
8335
8336 return expression;
8337 }
8338
8339 static inline tree
8340 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8341 {
8342 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8343 }
8344
8345 /* Parse a constant-expression.
8346
8347 constant-expression:
8348 conditional-expression
8349
8350 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8351 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8352 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8353 is false, NON_CONSTANT_P should be NULL. */
8354
8355 static tree
8356 cp_parser_constant_expression (cp_parser* parser,
8357 bool allow_non_constant_p,
8358 bool *non_constant_p)
8359 {
8360 bool saved_integral_constant_expression_p;
8361 bool saved_allow_non_integral_constant_expression_p;
8362 bool saved_non_integral_constant_expression_p;
8363 tree expression;
8364
8365 /* It might seem that we could simply parse the
8366 conditional-expression, and then check to see if it were
8367 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8368 one that the compiler can figure out is constant, possibly after
8369 doing some simplifications or optimizations. The standard has a
8370 precise definition of constant-expression, and we must honor
8371 that, even though it is somewhat more restrictive.
8372
8373 For example:
8374
8375 int i[(2, 3)];
8376
8377 is not a legal declaration, because `(2, 3)' is not a
8378 constant-expression. The `,' operator is forbidden in a
8379 constant-expression. However, GCC's constant-folding machinery
8380 will fold this operation to an INTEGER_CST for `3'. */
8381
8382 /* Save the old settings. */
8383 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8384 saved_allow_non_integral_constant_expression_p
8385 = parser->allow_non_integral_constant_expression_p;
8386 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8387 /* We are now parsing a constant-expression. */
8388 parser->integral_constant_expression_p = true;
8389 parser->allow_non_integral_constant_expression_p
8390 = (allow_non_constant_p || cxx_dialect >= cxx11);
8391 parser->non_integral_constant_expression_p = false;
8392 /* Although the grammar says "conditional-expression", we parse an
8393 "assignment-expression", which also permits "throw-expression"
8394 and the use of assignment operators. In the case that
8395 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8396 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8397 actually essential that we look for an assignment-expression.
8398 For example, cp_parser_initializer_clauses uses this function to
8399 determine whether a particular assignment-expression is in fact
8400 constant. */
8401 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8402 /* Restore the old settings. */
8403 parser->integral_constant_expression_p
8404 = saved_integral_constant_expression_p;
8405 parser->allow_non_integral_constant_expression_p
8406 = saved_allow_non_integral_constant_expression_p;
8407 if (cxx_dialect >= cxx11)
8408 {
8409 /* Require an rvalue constant expression here; that's what our
8410 callers expect. Reference constant expressions are handled
8411 separately in e.g. cp_parser_template_argument. */
8412 bool is_const = potential_rvalue_constant_expression (expression);
8413 parser->non_integral_constant_expression_p = !is_const;
8414 if (!is_const && !allow_non_constant_p)
8415 require_potential_rvalue_constant_expression (expression);
8416 }
8417 if (allow_non_constant_p)
8418 *non_constant_p = parser->non_integral_constant_expression_p;
8419 parser->non_integral_constant_expression_p
8420 = saved_non_integral_constant_expression_p;
8421
8422 return expression;
8423 }
8424
8425 /* Parse __builtin_offsetof.
8426
8427 offsetof-expression:
8428 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8429
8430 offsetof-member-designator:
8431 id-expression
8432 | offsetof-member-designator "." id-expression
8433 | offsetof-member-designator "[" expression "]"
8434 | offsetof-member-designator "->" id-expression */
8435
8436 static tree
8437 cp_parser_builtin_offsetof (cp_parser *parser)
8438 {
8439 int save_ice_p, save_non_ice_p;
8440 tree type, expr;
8441 cp_id_kind dummy;
8442 cp_token *token;
8443
8444 /* We're about to accept non-integral-constant things, but will
8445 definitely yield an integral constant expression. Save and
8446 restore these values around our local parsing. */
8447 save_ice_p = parser->integral_constant_expression_p;
8448 save_non_ice_p = parser->non_integral_constant_expression_p;
8449
8450 /* Consume the "__builtin_offsetof" token. */
8451 cp_lexer_consume_token (parser->lexer);
8452 /* Consume the opening `('. */
8453 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8454 /* Parse the type-id. */
8455 type = cp_parser_type_id (parser);
8456 /* Look for the `,'. */
8457 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8458 token = cp_lexer_peek_token (parser->lexer);
8459
8460 /* Build the (type *)null that begins the traditional offsetof macro. */
8461 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8462 tf_warning_or_error);
8463
8464 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8465 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8466 true, &dummy, token->location);
8467 while (true)
8468 {
8469 token = cp_lexer_peek_token (parser->lexer);
8470 switch (token->type)
8471 {
8472 case CPP_OPEN_SQUARE:
8473 /* offsetof-member-designator "[" expression "]" */
8474 expr = cp_parser_postfix_open_square_expression (parser, expr,
8475 true, false);
8476 break;
8477
8478 case CPP_DEREF:
8479 /* offsetof-member-designator "->" identifier */
8480 expr = grok_array_decl (token->location, expr,
8481 integer_zero_node, false);
8482 /* FALLTHRU */
8483
8484 case CPP_DOT:
8485 /* offsetof-member-designator "." identifier */
8486 cp_lexer_consume_token (parser->lexer);
8487 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8488 expr, true, &dummy,
8489 token->location);
8490 break;
8491
8492 case CPP_CLOSE_PAREN:
8493 /* Consume the ")" token. */
8494 cp_lexer_consume_token (parser->lexer);
8495 goto success;
8496
8497 default:
8498 /* Error. We know the following require will fail, but
8499 that gives the proper error message. */
8500 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8501 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8502 expr = error_mark_node;
8503 goto failure;
8504 }
8505 }
8506
8507 success:
8508 /* If we're processing a template, we can't finish the semantics yet.
8509 Otherwise we can fold the entire expression now. */
8510 if (processing_template_decl)
8511 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8512 else
8513 expr = finish_offsetof (expr);
8514
8515 failure:
8516 parser->integral_constant_expression_p = save_ice_p;
8517 parser->non_integral_constant_expression_p = save_non_ice_p;
8518
8519 return expr;
8520 }
8521
8522 /* Parse a trait expression.
8523
8524 Returns a representation of the expression, the underlying type
8525 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8526
8527 static tree
8528 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8529 {
8530 cp_trait_kind kind;
8531 tree type1, type2 = NULL_TREE;
8532 bool binary = false;
8533 cp_decl_specifier_seq decl_specs;
8534
8535 switch (keyword)
8536 {
8537 case RID_HAS_NOTHROW_ASSIGN:
8538 kind = CPTK_HAS_NOTHROW_ASSIGN;
8539 break;
8540 case RID_HAS_NOTHROW_CONSTRUCTOR:
8541 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8542 break;
8543 case RID_HAS_NOTHROW_COPY:
8544 kind = CPTK_HAS_NOTHROW_COPY;
8545 break;
8546 case RID_HAS_TRIVIAL_ASSIGN:
8547 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8548 break;
8549 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8550 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8551 break;
8552 case RID_HAS_TRIVIAL_COPY:
8553 kind = CPTK_HAS_TRIVIAL_COPY;
8554 break;
8555 case RID_HAS_TRIVIAL_DESTRUCTOR:
8556 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8557 break;
8558 case RID_HAS_VIRTUAL_DESTRUCTOR:
8559 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8560 break;
8561 case RID_IS_ABSTRACT:
8562 kind = CPTK_IS_ABSTRACT;
8563 break;
8564 case RID_IS_BASE_OF:
8565 kind = CPTK_IS_BASE_OF;
8566 binary = true;
8567 break;
8568 case RID_IS_CLASS:
8569 kind = CPTK_IS_CLASS;
8570 break;
8571 case RID_IS_CONVERTIBLE_TO:
8572 kind = CPTK_IS_CONVERTIBLE_TO;
8573 binary = true;
8574 break;
8575 case RID_IS_EMPTY:
8576 kind = CPTK_IS_EMPTY;
8577 break;
8578 case RID_IS_ENUM:
8579 kind = CPTK_IS_ENUM;
8580 break;
8581 case RID_IS_FINAL:
8582 kind = CPTK_IS_FINAL;
8583 break;
8584 case RID_IS_LITERAL_TYPE:
8585 kind = CPTK_IS_LITERAL_TYPE;
8586 break;
8587 case RID_IS_POD:
8588 kind = CPTK_IS_POD;
8589 break;
8590 case RID_IS_POLYMORPHIC:
8591 kind = CPTK_IS_POLYMORPHIC;
8592 break;
8593 case RID_IS_STD_LAYOUT:
8594 kind = CPTK_IS_STD_LAYOUT;
8595 break;
8596 case RID_IS_TRIVIAL:
8597 kind = CPTK_IS_TRIVIAL;
8598 break;
8599 case RID_IS_UNION:
8600 kind = CPTK_IS_UNION;
8601 break;
8602 case RID_UNDERLYING_TYPE:
8603 kind = CPTK_UNDERLYING_TYPE;
8604 break;
8605 case RID_BASES:
8606 kind = CPTK_BASES;
8607 break;
8608 case RID_DIRECT_BASES:
8609 kind = CPTK_DIRECT_BASES;
8610 break;
8611 default:
8612 gcc_unreachable ();
8613 }
8614
8615 /* Consume the token. */
8616 cp_lexer_consume_token (parser->lexer);
8617
8618 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8619
8620 type1 = cp_parser_type_id (parser);
8621
8622 if (type1 == error_mark_node)
8623 return error_mark_node;
8624
8625 /* Build a trivial decl-specifier-seq. */
8626 clear_decl_specs (&decl_specs);
8627 decl_specs.type = type1;
8628
8629 /* Call grokdeclarator to figure out what type this is. */
8630 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8631 /*initialized=*/0, /*attrlist=*/NULL);
8632
8633 if (binary)
8634 {
8635 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8636
8637 type2 = cp_parser_type_id (parser);
8638
8639 if (type2 == error_mark_node)
8640 return error_mark_node;
8641
8642 /* Build a trivial decl-specifier-seq. */
8643 clear_decl_specs (&decl_specs);
8644 decl_specs.type = type2;
8645
8646 /* Call grokdeclarator to figure out what type this is. */
8647 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8648 /*initialized=*/0, /*attrlist=*/NULL);
8649 }
8650
8651 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8652
8653 /* Complete the trait expression, which may mean either processing
8654 the trait expr now or saving it for template instantiation. */
8655 switch(kind)
8656 {
8657 case CPTK_UNDERLYING_TYPE:
8658 return finish_underlying_type (type1);
8659 case CPTK_BASES:
8660 return finish_bases (type1, false);
8661 case CPTK_DIRECT_BASES:
8662 return finish_bases (type1, true);
8663 default:
8664 return finish_trait_expr (kind, type1, type2);
8665 }
8666 }
8667
8668 /* Lambdas that appear in variable initializer or default argument scope
8669 get that in their mangling, so we need to record it. We might as well
8670 use the count for function and namespace scopes as well. */
8671 static GTY(()) tree lambda_scope;
8672 static GTY(()) int lambda_count;
8673 typedef struct GTY(()) tree_int
8674 {
8675 tree t;
8676 int i;
8677 } tree_int;
8678 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8679
8680 static void
8681 start_lambda_scope (tree decl)
8682 {
8683 tree_int ti;
8684 gcc_assert (decl);
8685 /* Once we're inside a function, we ignore other scopes and just push
8686 the function again so that popping works properly. */
8687 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8688 decl = current_function_decl;
8689 ti.t = lambda_scope;
8690 ti.i = lambda_count;
8691 vec_safe_push (lambda_scope_stack, ti);
8692 if (lambda_scope != decl)
8693 {
8694 /* Don't reset the count if we're still in the same function. */
8695 lambda_scope = decl;
8696 lambda_count = 0;
8697 }
8698 }
8699
8700 static void
8701 record_lambda_scope (tree lambda)
8702 {
8703 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8704 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8705 }
8706
8707 static void
8708 finish_lambda_scope (void)
8709 {
8710 tree_int *p = &lambda_scope_stack->last ();
8711 if (lambda_scope != p->t)
8712 {
8713 lambda_scope = p->t;
8714 lambda_count = p->i;
8715 }
8716 lambda_scope_stack->pop ();
8717 }
8718
8719 /* Parse a lambda expression.
8720
8721 lambda-expression:
8722 lambda-introducer lambda-declarator [opt] compound-statement
8723
8724 Returns a representation of the expression. */
8725
8726 static tree
8727 cp_parser_lambda_expression (cp_parser* parser)
8728 {
8729 tree lambda_expr = build_lambda_expr ();
8730 tree type;
8731 bool ok = true;
8732 cp_token *token = cp_lexer_peek_token (parser->lexer);
8733
8734 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8735
8736 if (cp_unevaluated_operand)
8737 {
8738 if (!token->error_reported)
8739 {
8740 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8741 "lambda-expression in unevaluated context");
8742 token->error_reported = true;
8743 }
8744 ok = false;
8745 }
8746
8747 /* We may be in the middle of deferred access check. Disable
8748 it now. */
8749 push_deferring_access_checks (dk_no_deferred);
8750
8751 cp_parser_lambda_introducer (parser, lambda_expr);
8752
8753 type = begin_lambda_type (lambda_expr);
8754 if (type == error_mark_node)
8755 return error_mark_node;
8756
8757 record_lambda_scope (lambda_expr);
8758
8759 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8760 determine_visibility (TYPE_NAME (type));
8761
8762 /* Now that we've started the type, add the capture fields for any
8763 explicit captures. */
8764 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8765
8766 {
8767 /* Inside the class, surrounding template-parameter-lists do not apply. */
8768 unsigned int saved_num_template_parameter_lists
8769 = parser->num_template_parameter_lists;
8770 unsigned char in_statement = parser->in_statement;
8771 bool in_switch_statement_p = parser->in_switch_statement_p;
8772 bool fully_implicit_function_template_p
8773 = parser->fully_implicit_function_template_p;
8774 tree implicit_template_parms = parser->implicit_template_parms;
8775 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8776 bool auto_is_implicit_function_template_parm_p
8777 = parser->auto_is_implicit_function_template_parm_p;
8778
8779 parser->num_template_parameter_lists = 0;
8780 parser->in_statement = 0;
8781 parser->in_switch_statement_p = false;
8782 parser->fully_implicit_function_template_p = false;
8783 parser->implicit_template_parms = 0;
8784 parser->implicit_template_scope = 0;
8785 parser->auto_is_implicit_function_template_parm_p = false;
8786
8787 /* By virtue of defining a local class, a lambda expression has access to
8788 the private variables of enclosing classes. */
8789
8790 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8791
8792 if (ok)
8793 cp_parser_lambda_body (parser, lambda_expr);
8794 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8795 {
8796 if (cp_parser_skip_to_closing_brace (parser))
8797 cp_lexer_consume_token (parser->lexer);
8798 }
8799
8800 /* The capture list was built up in reverse order; fix that now. */
8801 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8802 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8803
8804 if (ok)
8805 maybe_add_lambda_conv_op (type);
8806
8807 type = finish_struct (type, /*attributes=*/NULL_TREE);
8808
8809 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8810 parser->in_statement = in_statement;
8811 parser->in_switch_statement_p = in_switch_statement_p;
8812 parser->fully_implicit_function_template_p
8813 = fully_implicit_function_template_p;
8814 parser->implicit_template_parms = implicit_template_parms;
8815 parser->implicit_template_scope = implicit_template_scope;
8816 parser->auto_is_implicit_function_template_parm_p
8817 = auto_is_implicit_function_template_parm_p;
8818 }
8819
8820 pop_deferring_access_checks ();
8821
8822 /* This field is only used during parsing of the lambda. */
8823 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8824
8825 /* This lambda shouldn't have any proxies left at this point. */
8826 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8827 /* And now that we're done, push proxies for an enclosing lambda. */
8828 insert_pending_capture_proxies ();
8829
8830 if (ok)
8831 return build_lambda_object (lambda_expr);
8832 else
8833 return error_mark_node;
8834 }
8835
8836 /* Parse the beginning of a lambda expression.
8837
8838 lambda-introducer:
8839 [ lambda-capture [opt] ]
8840
8841 LAMBDA_EXPR is the current representation of the lambda expression. */
8842
8843 static void
8844 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8845 {
8846 /* Need commas after the first capture. */
8847 bool first = true;
8848
8849 /* Eat the leading `['. */
8850 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8851
8852 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8853 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8854 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8855 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8856 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8857 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8858
8859 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8860 {
8861 cp_lexer_consume_token (parser->lexer);
8862 first = false;
8863 }
8864
8865 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8866 {
8867 cp_token* capture_token;
8868 tree capture_id;
8869 tree capture_init_expr;
8870 cp_id_kind idk = CP_ID_KIND_NONE;
8871 bool explicit_init_p = false;
8872
8873 enum capture_kind_type
8874 {
8875 BY_COPY,
8876 BY_REFERENCE
8877 };
8878 enum capture_kind_type capture_kind = BY_COPY;
8879
8880 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8881 {
8882 error ("expected end of capture-list");
8883 return;
8884 }
8885
8886 if (first)
8887 first = false;
8888 else
8889 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8890
8891 /* Possibly capture `this'. */
8892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8893 {
8894 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8895 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8896 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8897 "with by-copy capture default");
8898 cp_lexer_consume_token (parser->lexer);
8899 add_capture (lambda_expr,
8900 /*id=*/this_identifier,
8901 /*initializer=*/finish_this_expr(),
8902 /*by_reference_p=*/false,
8903 explicit_init_p);
8904 continue;
8905 }
8906
8907 /* Remember whether we want to capture as a reference or not. */
8908 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8909 {
8910 capture_kind = BY_REFERENCE;
8911 cp_lexer_consume_token (parser->lexer);
8912 }
8913
8914 /* Get the identifier. */
8915 capture_token = cp_lexer_peek_token (parser->lexer);
8916 capture_id = cp_parser_identifier (parser);
8917
8918 if (capture_id == error_mark_node)
8919 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8920 delimiters, but I modified this to stop on unnested ']' as well. It
8921 was already changed to stop on unnested '}', so the
8922 "closing_parenthesis" name is no more misleading with my change. */
8923 {
8924 cp_parser_skip_to_closing_parenthesis (parser,
8925 /*recovering=*/true,
8926 /*or_comma=*/true,
8927 /*consume_paren=*/true);
8928 break;
8929 }
8930
8931 /* Find the initializer for this capture. */
8932 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8933 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8934 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8935 {
8936 bool direct, non_constant;
8937 /* An explicit initializer exists. */
8938 if (cxx_dialect < cxx1y)
8939 pedwarn (input_location, 0,
8940 "lambda capture initializers "
8941 "only available with -std=c++1y or -std=gnu++1y");
8942 capture_init_expr = cp_parser_initializer (parser, &direct,
8943 &non_constant);
8944 explicit_init_p = true;
8945 if (capture_init_expr == NULL_TREE)
8946 {
8947 error ("empty initializer for lambda init-capture");
8948 capture_init_expr = error_mark_node;
8949 }
8950 }
8951 else
8952 {
8953 const char* error_msg;
8954
8955 /* Turn the identifier into an id-expression. */
8956 capture_init_expr
8957 = cp_parser_lookup_name_simple (parser, capture_id,
8958 capture_token->location);
8959
8960 if (capture_init_expr == error_mark_node)
8961 {
8962 unqualified_name_lookup_error (capture_id);
8963 continue;
8964 }
8965 else if (DECL_P (capture_init_expr)
8966 && (!VAR_P (capture_init_expr)
8967 && TREE_CODE (capture_init_expr) != PARM_DECL))
8968 {
8969 error_at (capture_token->location,
8970 "capture of non-variable %qD ",
8971 capture_init_expr);
8972 inform (0, "%q+#D declared here", capture_init_expr);
8973 continue;
8974 }
8975 if (VAR_P (capture_init_expr)
8976 && decl_storage_duration (capture_init_expr) != dk_auto)
8977 {
8978 if (pedwarn (capture_token->location, 0, "capture of variable "
8979 "%qD with non-automatic storage duration",
8980 capture_init_expr))
8981 inform (0, "%q+#D declared here", capture_init_expr);
8982 continue;
8983 }
8984
8985 capture_init_expr
8986 = finish_id_expression
8987 (capture_id,
8988 capture_init_expr,
8989 parser->scope,
8990 &idk,
8991 /*integral_constant_expression_p=*/false,
8992 /*allow_non_integral_constant_expression_p=*/false,
8993 /*non_integral_constant_expression_p=*/NULL,
8994 /*template_p=*/false,
8995 /*done=*/true,
8996 /*address_p=*/false,
8997 /*template_arg_p=*/false,
8998 &error_msg,
8999 capture_token->location);
9000
9001 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9002 {
9003 cp_lexer_consume_token (parser->lexer);
9004 capture_init_expr = make_pack_expansion (capture_init_expr);
9005 }
9006 else
9007 check_for_bare_parameter_packs (capture_init_expr);
9008 }
9009
9010 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9011 && !explicit_init_p)
9012 {
9013 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9014 && capture_kind == BY_COPY)
9015 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9016 "of %qD redundant with by-copy capture default",
9017 capture_id);
9018 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9019 && capture_kind == BY_REFERENCE)
9020 pedwarn (capture_token->location, 0, "explicit by-reference "
9021 "capture of %qD redundant with by-reference capture "
9022 "default", capture_id);
9023 }
9024
9025 add_capture (lambda_expr,
9026 capture_id,
9027 capture_init_expr,
9028 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9029 explicit_init_p);
9030 }
9031
9032 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9033 }
9034
9035 /* Parse the (optional) middle of a lambda expression.
9036
9037 lambda-declarator:
9038 < template-parameter-list [opt] >
9039 ( parameter-declaration-clause [opt] )
9040 attribute-specifier [opt]
9041 mutable [opt]
9042 exception-specification [opt]
9043 lambda-return-type-clause [opt]
9044
9045 LAMBDA_EXPR is the current representation of the lambda expression. */
9046
9047 static bool
9048 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9049 {
9050 /* 5.1.1.4 of the standard says:
9051 If a lambda-expression does not include a lambda-declarator, it is as if
9052 the lambda-declarator were ().
9053 This means an empty parameter list, no attributes, and no exception
9054 specification. */
9055 tree param_list = void_list_node;
9056 tree attributes = NULL_TREE;
9057 tree exception_spec = NULL_TREE;
9058 tree template_param_list = NULL_TREE;
9059
9060 /* The template-parameter-list is optional, but must begin with
9061 an opening angle if present. */
9062 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9063 {
9064 if (cxx_dialect < cxx1y)
9065 pedwarn (parser->lexer->next_token->location, 0,
9066 "lambda templates are only available with "
9067 "-std=c++1y or -std=gnu++1y");
9068
9069 cp_lexer_consume_token (parser->lexer);
9070
9071 template_param_list = cp_parser_template_parameter_list (parser);
9072
9073 cp_parser_skip_to_end_of_template_parameter_list (parser);
9074
9075 /* We just processed one more parameter list. */
9076 ++parser->num_template_parameter_lists;
9077 }
9078
9079 /* The parameter-declaration-clause is optional (unless
9080 template-parameter-list was given), but must begin with an
9081 opening parenthesis if present. */
9082 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9083 {
9084 cp_lexer_consume_token (parser->lexer);
9085
9086 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9087
9088 /* Parse parameters. */
9089 param_list = cp_parser_parameter_declaration_clause (parser);
9090
9091 /* Default arguments shall not be specified in the
9092 parameter-declaration-clause of a lambda-declarator. */
9093 for (tree t = param_list; t; t = TREE_CHAIN (t))
9094 if (TREE_PURPOSE (t))
9095 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9096 "default argument specified for lambda parameter");
9097
9098 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9099
9100 attributes = cp_parser_attributes_opt (parser);
9101
9102 /* Parse optional `mutable' keyword. */
9103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9104 {
9105 cp_lexer_consume_token (parser->lexer);
9106 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9107 }
9108
9109 /* Parse optional exception specification. */
9110 exception_spec = cp_parser_exception_specification_opt (parser);
9111
9112 /* Parse optional trailing return type. */
9113 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9114 {
9115 cp_lexer_consume_token (parser->lexer);
9116 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9117 = cp_parser_trailing_type_id (parser);
9118 }
9119
9120 /* The function parameters must be in scope all the way until after the
9121 trailing-return-type in case of decltype. */
9122 pop_bindings_and_leave_scope ();
9123 }
9124 else if (template_param_list != NULL_TREE) // generate diagnostic
9125 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9126
9127 /* Create the function call operator.
9128
9129 Messing with declarators like this is no uglier than building up the
9130 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9131 other code. */
9132 {
9133 cp_decl_specifier_seq return_type_specs;
9134 cp_declarator* declarator;
9135 tree fco;
9136 int quals;
9137 void *p;
9138
9139 clear_decl_specs (&return_type_specs);
9140 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9141 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9142 else
9143 /* Maybe we will deduce the return type later. */
9144 return_type_specs.type = make_auto ();
9145
9146 p = obstack_alloc (&declarator_obstack, 0);
9147
9148 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9149 sfk_none);
9150
9151 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9152 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9153 declarator = make_call_declarator (declarator, param_list, quals,
9154 VIRT_SPEC_UNSPECIFIED,
9155 REF_QUAL_NONE,
9156 exception_spec,
9157 /*late_return_type=*/NULL_TREE);
9158 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9159
9160 fco = grokmethod (&return_type_specs,
9161 declarator,
9162 attributes);
9163 if (fco != error_mark_node)
9164 {
9165 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9166 DECL_ARTIFICIAL (fco) = 1;
9167 /* Give the object parameter a different name. */
9168 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9169 }
9170 if (template_param_list)
9171 {
9172 fco = finish_member_template_decl (fco);
9173 finish_template_decl (template_param_list);
9174 --parser->num_template_parameter_lists;
9175 }
9176 else if (parser->fully_implicit_function_template_p)
9177 fco = finish_fully_implicit_template (parser, fco);
9178
9179 finish_member_declaration (fco);
9180
9181 obstack_free (&declarator_obstack, p);
9182
9183 return (fco != error_mark_node);
9184 }
9185 }
9186
9187 /* Parse the body of a lambda expression, which is simply
9188
9189 compound-statement
9190
9191 but which requires special handling.
9192 LAMBDA_EXPR is the current representation of the lambda expression. */
9193
9194 static void
9195 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9196 {
9197 bool nested = (current_function_decl != NULL_TREE);
9198 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9199 if (nested)
9200 push_function_context ();
9201 else
9202 /* Still increment function_depth so that we don't GC in the
9203 middle of an expression. */
9204 ++function_depth;
9205 /* Clear this in case we're in the middle of a default argument. */
9206 parser->local_variables_forbidden_p = false;
9207
9208 /* Finish the function call operator
9209 - class_specifier
9210 + late_parsing_for_member
9211 + function_definition_after_declarator
9212 + ctor_initializer_opt_and_function_body */
9213 {
9214 tree fco = lambda_function (lambda_expr);
9215 tree body;
9216 bool done = false;
9217 tree compound_stmt;
9218 tree cap;
9219
9220 /* Let the front end know that we are going to be defining this
9221 function. */
9222 start_preparsed_function (fco,
9223 NULL_TREE,
9224 SF_PRE_PARSED | SF_INCLASS_INLINE);
9225
9226 start_lambda_scope (fco);
9227 body = begin_function_body ();
9228
9229 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9230 goto out;
9231
9232 /* Push the proxies for any explicit captures. */
9233 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9234 cap = TREE_CHAIN (cap))
9235 build_capture_proxy (TREE_PURPOSE (cap));
9236
9237 compound_stmt = begin_compound_stmt (0);
9238
9239 /* 5.1.1.4 of the standard says:
9240 If a lambda-expression does not include a trailing-return-type, it
9241 is as if the trailing-return-type denotes the following type:
9242 * if the compound-statement is of the form
9243 { return attribute-specifier [opt] expression ; }
9244 the type of the returned expression after lvalue-to-rvalue
9245 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9246 (_conv.array_ 4.2), and function-to-pointer conversion
9247 (_conv.func_ 4.3);
9248 * otherwise, void. */
9249
9250 /* In a lambda that has neither a lambda-return-type-clause
9251 nor a deducible form, errors should be reported for return statements
9252 in the body. Since we used void as the placeholder return type, parsing
9253 the body as usual will give such desired behavior. */
9254 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9255 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9256 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9257 {
9258 tree expr = NULL_TREE;
9259 cp_id_kind idk = CP_ID_KIND_NONE;
9260
9261 /* Parse tentatively in case there's more after the initial return
9262 statement. */
9263 cp_parser_parse_tentatively (parser);
9264
9265 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9266
9267 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9268
9269 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9270 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9271
9272 if (cp_parser_parse_definitely (parser))
9273 {
9274 if (!processing_template_decl)
9275 apply_deduced_return_type (fco, lambda_return_type (expr));
9276
9277 /* Will get error here if type not deduced yet. */
9278 finish_return_stmt (expr);
9279
9280 done = true;
9281 }
9282 }
9283
9284 if (!done)
9285 {
9286 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9287 cp_parser_label_declaration (parser);
9288 cp_parser_statement_seq_opt (parser, NULL_TREE);
9289 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9290 }
9291
9292 finish_compound_stmt (compound_stmt);
9293
9294 out:
9295 finish_function_body (body);
9296 finish_lambda_scope ();
9297
9298 /* Finish the function and generate code for it if necessary. */
9299 tree fn = finish_function (/*inline*/2);
9300
9301 /* Only expand if the call op is not a template. */
9302 if (!DECL_TEMPLATE_INFO (fco))
9303 expand_or_defer_fn (fn);
9304 }
9305
9306 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9307 if (nested)
9308 pop_function_context();
9309 else
9310 --function_depth;
9311 }
9312
9313 /* Statements [gram.stmt.stmt] */
9314
9315 /* Parse a statement.
9316
9317 statement:
9318 labeled-statement
9319 expression-statement
9320 compound-statement
9321 selection-statement
9322 iteration-statement
9323 jump-statement
9324 declaration-statement
9325 try-block
9326
9327 C++11:
9328
9329 statement:
9330 labeled-statement
9331 attribute-specifier-seq (opt) expression-statement
9332 attribute-specifier-seq (opt) compound-statement
9333 attribute-specifier-seq (opt) selection-statement
9334 attribute-specifier-seq (opt) iteration-statement
9335 attribute-specifier-seq (opt) jump-statement
9336 declaration-statement
9337 attribute-specifier-seq (opt) try-block
9338
9339 TM Extension:
9340
9341 statement:
9342 atomic-statement
9343
9344 IN_COMPOUND is true when the statement is nested inside a
9345 cp_parser_compound_statement; this matters for certain pragmas.
9346
9347 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9348 is a (possibly labeled) if statement which is not enclosed in braces
9349 and has an else clause. This is used to implement -Wparentheses. */
9350
9351 static void
9352 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9353 bool in_compound, bool *if_p)
9354 {
9355 tree statement, std_attrs = NULL_TREE;
9356 cp_token *token;
9357 location_t statement_location, attrs_location;
9358
9359 restart:
9360 if (if_p != NULL)
9361 *if_p = false;
9362 /* There is no statement yet. */
9363 statement = NULL_TREE;
9364
9365 cp_lexer_save_tokens (parser->lexer);
9366 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9367 if (c_dialect_objc ())
9368 /* In obj-c++, seeing '[[' might be the either the beginning of
9369 c++11 attributes, or a nested objc-message-expression. So
9370 let's parse the c++11 attributes tentatively. */
9371 cp_parser_parse_tentatively (parser);
9372 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9373 if (c_dialect_objc ())
9374 {
9375 if (!cp_parser_parse_definitely (parser))
9376 std_attrs = NULL_TREE;
9377 }
9378
9379 /* Peek at the next token. */
9380 token = cp_lexer_peek_token (parser->lexer);
9381 /* Remember the location of the first token in the statement. */
9382 statement_location = token->location;
9383 /* If this is a keyword, then that will often determine what kind of
9384 statement we have. */
9385 if (token->type == CPP_KEYWORD)
9386 {
9387 enum rid keyword = token->keyword;
9388
9389 switch (keyword)
9390 {
9391 case RID_CASE:
9392 case RID_DEFAULT:
9393 /* Looks like a labeled-statement with a case label.
9394 Parse the label, and then use tail recursion to parse
9395 the statement. */
9396 cp_parser_label_for_labeled_statement (parser, std_attrs);
9397 goto restart;
9398
9399 case RID_IF:
9400 case RID_SWITCH:
9401 statement = cp_parser_selection_statement (parser, if_p);
9402 break;
9403
9404 case RID_WHILE:
9405 case RID_DO:
9406 case RID_FOR:
9407 statement = cp_parser_iteration_statement (parser, false);
9408 break;
9409
9410 case RID_BREAK:
9411 case RID_CONTINUE:
9412 case RID_RETURN:
9413 case RID_GOTO:
9414 statement = cp_parser_jump_statement (parser);
9415 break;
9416
9417 /* Objective-C++ exception-handling constructs. */
9418 case RID_AT_TRY:
9419 case RID_AT_CATCH:
9420 case RID_AT_FINALLY:
9421 case RID_AT_SYNCHRONIZED:
9422 case RID_AT_THROW:
9423 statement = cp_parser_objc_statement (parser);
9424 break;
9425
9426 case RID_TRY:
9427 statement = cp_parser_try_block (parser);
9428 break;
9429
9430 case RID_NAMESPACE:
9431 /* This must be a namespace alias definition. */
9432 cp_parser_declaration_statement (parser);
9433 return;
9434
9435 case RID_TRANSACTION_ATOMIC:
9436 case RID_TRANSACTION_RELAXED:
9437 statement = cp_parser_transaction (parser, keyword);
9438 break;
9439 case RID_TRANSACTION_CANCEL:
9440 statement = cp_parser_transaction_cancel (parser);
9441 break;
9442
9443 default:
9444 /* It might be a keyword like `int' that can start a
9445 declaration-statement. */
9446 break;
9447 }
9448 }
9449 else if (token->type == CPP_NAME)
9450 {
9451 /* If the next token is a `:', then we are looking at a
9452 labeled-statement. */
9453 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9454 if (token->type == CPP_COLON)
9455 {
9456 /* Looks like a labeled-statement with an ordinary label.
9457 Parse the label, and then use tail recursion to parse
9458 the statement. */
9459
9460 cp_parser_label_for_labeled_statement (parser, std_attrs);
9461 goto restart;
9462 }
9463 }
9464 /* Anything that starts with a `{' must be a compound-statement. */
9465 else if (token->type == CPP_OPEN_BRACE)
9466 statement = cp_parser_compound_statement (parser, NULL, false, false);
9467 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9468 a statement all its own. */
9469 else if (token->type == CPP_PRAGMA)
9470 {
9471 /* Only certain OpenMP pragmas are attached to statements, and thus
9472 are considered statements themselves. All others are not. In
9473 the context of a compound, accept the pragma as a "statement" and
9474 return so that we can check for a close brace. Otherwise we
9475 require a real statement and must go back and read one. */
9476 if (in_compound)
9477 cp_parser_pragma (parser, pragma_compound);
9478 else if (!cp_parser_pragma (parser, pragma_stmt))
9479 goto restart;
9480 return;
9481 }
9482 else if (token->type == CPP_EOF)
9483 {
9484 cp_parser_error (parser, "expected statement");
9485 return;
9486 }
9487
9488 /* Everything else must be a declaration-statement or an
9489 expression-statement. Try for the declaration-statement
9490 first, unless we are looking at a `;', in which case we know that
9491 we have an expression-statement. */
9492 if (!statement)
9493 {
9494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9495 {
9496 if (std_attrs != NULL_TREE)
9497 {
9498 /* Attributes should be parsed as part of the the
9499 declaration, so let's un-parse them. */
9500 cp_lexer_rollback_tokens (parser->lexer);
9501 std_attrs = NULL_TREE;
9502 }
9503
9504 cp_parser_parse_tentatively (parser);
9505 /* Try to parse the declaration-statement. */
9506 cp_parser_declaration_statement (parser);
9507 /* If that worked, we're done. */
9508 if (cp_parser_parse_definitely (parser))
9509 return;
9510 }
9511 /* Look for an expression-statement instead. */
9512 statement = cp_parser_expression_statement (parser, in_statement_expr);
9513 }
9514
9515 /* Set the line number for the statement. */
9516 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9517 SET_EXPR_LOCATION (statement, statement_location);
9518
9519 /* Note that for now, we don't do anything with c++11 statements
9520 parsed at this level. */
9521 if (std_attrs != NULL_TREE)
9522 warning_at (attrs_location,
9523 OPT_Wattributes,
9524 "attributes at the beginning of statement are ignored");
9525 }
9526
9527 /* Parse the label for a labeled-statement, i.e.
9528
9529 identifier :
9530 case constant-expression :
9531 default :
9532
9533 GNU Extension:
9534 case constant-expression ... constant-expression : statement
9535
9536 When a label is parsed without errors, the label is added to the
9537 parse tree by the finish_* functions, so this function doesn't
9538 have to return the label. */
9539
9540 static void
9541 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9542 {
9543 cp_token *token;
9544 tree label = NULL_TREE;
9545 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9546
9547 /* The next token should be an identifier. */
9548 token = cp_lexer_peek_token (parser->lexer);
9549 if (token->type != CPP_NAME
9550 && token->type != CPP_KEYWORD)
9551 {
9552 cp_parser_error (parser, "expected labeled-statement");
9553 return;
9554 }
9555
9556 parser->colon_corrects_to_scope_p = false;
9557 switch (token->keyword)
9558 {
9559 case RID_CASE:
9560 {
9561 tree expr, expr_hi;
9562 cp_token *ellipsis;
9563
9564 /* Consume the `case' token. */
9565 cp_lexer_consume_token (parser->lexer);
9566 /* Parse the constant-expression. */
9567 expr = cp_parser_constant_expression (parser,
9568 /*allow_non_constant_p=*/false,
9569 NULL);
9570
9571 ellipsis = cp_lexer_peek_token (parser->lexer);
9572 if (ellipsis->type == CPP_ELLIPSIS)
9573 {
9574 /* Consume the `...' token. */
9575 cp_lexer_consume_token (parser->lexer);
9576 expr_hi =
9577 cp_parser_constant_expression (parser,
9578 /*allow_non_constant_p=*/false,
9579 NULL);
9580 /* We don't need to emit warnings here, as the common code
9581 will do this for us. */
9582 }
9583 else
9584 expr_hi = NULL_TREE;
9585
9586 if (parser->in_switch_statement_p)
9587 finish_case_label (token->location, expr, expr_hi);
9588 else
9589 error_at (token->location,
9590 "case label %qE not within a switch statement",
9591 expr);
9592 }
9593 break;
9594
9595 case RID_DEFAULT:
9596 /* Consume the `default' token. */
9597 cp_lexer_consume_token (parser->lexer);
9598
9599 if (parser->in_switch_statement_p)
9600 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9601 else
9602 error_at (token->location, "case label not within a switch statement");
9603 break;
9604
9605 default:
9606 /* Anything else must be an ordinary label. */
9607 label = finish_label_stmt (cp_parser_identifier (parser));
9608 break;
9609 }
9610
9611 /* Require the `:' token. */
9612 cp_parser_require (parser, CPP_COLON, RT_COLON);
9613
9614 /* An ordinary label may optionally be followed by attributes.
9615 However, this is only permitted if the attributes are then
9616 followed by a semicolon. This is because, for backward
9617 compatibility, when parsing
9618 lab: __attribute__ ((unused)) int i;
9619 we want the attribute to attach to "i", not "lab". */
9620 if (label != NULL_TREE
9621 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9622 {
9623 tree attrs;
9624 cp_parser_parse_tentatively (parser);
9625 attrs = cp_parser_gnu_attributes_opt (parser);
9626 if (attrs == NULL_TREE
9627 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9628 cp_parser_abort_tentative_parse (parser);
9629 else if (!cp_parser_parse_definitely (parser))
9630 ;
9631 else
9632 attributes = chainon (attributes, attrs);
9633 }
9634
9635 if (attributes != NULL_TREE)
9636 cplus_decl_attributes (&label, attributes, 0);
9637
9638 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9639 }
9640
9641 /* Parse an expression-statement.
9642
9643 expression-statement:
9644 expression [opt] ;
9645
9646 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9647 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9648 indicates whether this expression-statement is part of an
9649 expression statement. */
9650
9651 static tree
9652 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9653 {
9654 tree statement = NULL_TREE;
9655 cp_token *token = cp_lexer_peek_token (parser->lexer);
9656
9657 /* If the next token is a ';', then there is no expression
9658 statement. */
9659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9660 {
9661 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9662 if (statement == error_mark_node
9663 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9664 {
9665 cp_parser_skip_to_end_of_block_or_statement (parser);
9666 return error_mark_node;
9667 }
9668 }
9669
9670 /* Give a helpful message for "A<T>::type t;" and the like. */
9671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9672 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9673 {
9674 if (TREE_CODE (statement) == SCOPE_REF)
9675 error_at (token->location, "need %<typename%> before %qE because "
9676 "%qT is a dependent scope",
9677 statement, TREE_OPERAND (statement, 0));
9678 else if (is_overloaded_fn (statement)
9679 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9680 {
9681 /* A::A a; */
9682 tree fn = get_first_fn (statement);
9683 error_at (token->location,
9684 "%<%T::%D%> names the constructor, not the type",
9685 DECL_CONTEXT (fn), DECL_NAME (fn));
9686 }
9687 }
9688
9689 /* Consume the final `;'. */
9690 cp_parser_consume_semicolon_at_end_of_statement (parser);
9691
9692 if (in_statement_expr
9693 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9694 /* This is the final expression statement of a statement
9695 expression. */
9696 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9697 else if (statement)
9698 statement = finish_expr_stmt (statement);
9699
9700 return statement;
9701 }
9702
9703 /* Parse a compound-statement.
9704
9705 compound-statement:
9706 { statement-seq [opt] }
9707
9708 GNU extension:
9709
9710 compound-statement:
9711 { label-declaration-seq [opt] statement-seq [opt] }
9712
9713 label-declaration-seq:
9714 label-declaration
9715 label-declaration-seq label-declaration
9716
9717 Returns a tree representing the statement. */
9718
9719 static tree
9720 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9721 bool in_try, bool function_body)
9722 {
9723 tree compound_stmt;
9724
9725 /* Consume the `{'. */
9726 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9727 return error_mark_node;
9728 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9729 && !function_body)
9730 pedwarn (input_location, OPT_Wpedantic,
9731 "compound-statement in constexpr function");
9732 /* Begin the compound-statement. */
9733 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9734 /* If the next keyword is `__label__' we have a label declaration. */
9735 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9736 cp_parser_label_declaration (parser);
9737 /* Parse an (optional) statement-seq. */
9738 cp_parser_statement_seq_opt (parser, in_statement_expr);
9739 /* Finish the compound-statement. */
9740 finish_compound_stmt (compound_stmt);
9741 /* Consume the `}'. */
9742 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9743
9744 return compound_stmt;
9745 }
9746
9747 /* Parse an (optional) statement-seq.
9748
9749 statement-seq:
9750 statement
9751 statement-seq [opt] statement */
9752
9753 static void
9754 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9755 {
9756 /* Scan statements until there aren't any more. */
9757 while (true)
9758 {
9759 cp_token *token = cp_lexer_peek_token (parser->lexer);
9760
9761 /* If we are looking at a `}', then we have run out of
9762 statements; the same is true if we have reached the end
9763 of file, or have stumbled upon a stray '@end'. */
9764 if (token->type == CPP_CLOSE_BRACE
9765 || token->type == CPP_EOF
9766 || token->type == CPP_PRAGMA_EOL
9767 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9768 break;
9769
9770 /* If we are in a compound statement and find 'else' then
9771 something went wrong. */
9772 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9773 {
9774 if (parser->in_statement & IN_IF_STMT)
9775 break;
9776 else
9777 {
9778 token = cp_lexer_consume_token (parser->lexer);
9779 error_at (token->location, "%<else%> without a previous %<if%>");
9780 }
9781 }
9782
9783 /* Parse the statement. */
9784 cp_parser_statement (parser, in_statement_expr, true, NULL);
9785 }
9786 }
9787
9788 /* Parse a selection-statement.
9789
9790 selection-statement:
9791 if ( condition ) statement
9792 if ( condition ) statement else statement
9793 switch ( condition ) statement
9794
9795 Returns the new IF_STMT or SWITCH_STMT.
9796
9797 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9798 is a (possibly labeled) if statement which is not enclosed in
9799 braces and has an else clause. This is used to implement
9800 -Wparentheses. */
9801
9802 static tree
9803 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9804 {
9805 cp_token *token;
9806 enum rid keyword;
9807
9808 if (if_p != NULL)
9809 *if_p = false;
9810
9811 /* Peek at the next token. */
9812 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9813
9814 /* See what kind of keyword it is. */
9815 keyword = token->keyword;
9816 switch (keyword)
9817 {
9818 case RID_IF:
9819 case RID_SWITCH:
9820 {
9821 tree statement;
9822 tree condition;
9823
9824 /* Look for the `('. */
9825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9826 {
9827 cp_parser_skip_to_end_of_statement (parser);
9828 return error_mark_node;
9829 }
9830
9831 /* Begin the selection-statement. */
9832 if (keyword == RID_IF)
9833 statement = begin_if_stmt ();
9834 else
9835 statement = begin_switch_stmt ();
9836
9837 /* Parse the condition. */
9838 condition = cp_parser_condition (parser);
9839 /* Look for the `)'. */
9840 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9841 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9842 /*consume_paren=*/true);
9843
9844 if (keyword == RID_IF)
9845 {
9846 bool nested_if;
9847 unsigned char in_statement;
9848
9849 /* Add the condition. */
9850 finish_if_stmt_cond (condition, statement);
9851
9852 /* Parse the then-clause. */
9853 in_statement = parser->in_statement;
9854 parser->in_statement |= IN_IF_STMT;
9855 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9856 {
9857 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9858 add_stmt (build_empty_stmt (loc));
9859 cp_lexer_consume_token (parser->lexer);
9860 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9861 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9862 "empty body in an %<if%> statement");
9863 nested_if = false;
9864 }
9865 else
9866 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9867 parser->in_statement = in_statement;
9868
9869 finish_then_clause (statement);
9870
9871 /* If the next token is `else', parse the else-clause. */
9872 if (cp_lexer_next_token_is_keyword (parser->lexer,
9873 RID_ELSE))
9874 {
9875 /* Consume the `else' keyword. */
9876 cp_lexer_consume_token (parser->lexer);
9877 begin_else_clause (statement);
9878 /* Parse the else-clause. */
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9880 {
9881 location_t loc;
9882 loc = cp_lexer_peek_token (parser->lexer)->location;
9883 warning_at (loc,
9884 OPT_Wempty_body, "suggest braces around "
9885 "empty body in an %<else%> statement");
9886 add_stmt (build_empty_stmt (loc));
9887 cp_lexer_consume_token (parser->lexer);
9888 }
9889 else
9890 cp_parser_implicitly_scoped_statement (parser, NULL);
9891
9892 finish_else_clause (statement);
9893
9894 /* If we are currently parsing a then-clause, then
9895 IF_P will not be NULL. We set it to true to
9896 indicate that this if statement has an else clause.
9897 This may trigger the Wparentheses warning below
9898 when we get back up to the parent if statement. */
9899 if (if_p != NULL)
9900 *if_p = true;
9901 }
9902 else
9903 {
9904 /* This if statement does not have an else clause. If
9905 NESTED_IF is true, then the then-clause is an if
9906 statement which does have an else clause. We warn
9907 about the potential ambiguity. */
9908 if (nested_if)
9909 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9910 "suggest explicit braces to avoid ambiguous"
9911 " %<else%>");
9912 }
9913
9914 /* Now we're all done with the if-statement. */
9915 finish_if_stmt (statement);
9916 }
9917 else
9918 {
9919 bool in_switch_statement_p;
9920 unsigned char in_statement;
9921
9922 /* Add the condition. */
9923 finish_switch_cond (condition, statement);
9924
9925 /* Parse the body of the switch-statement. */
9926 in_switch_statement_p = parser->in_switch_statement_p;
9927 in_statement = parser->in_statement;
9928 parser->in_switch_statement_p = true;
9929 parser->in_statement |= IN_SWITCH_STMT;
9930 cp_parser_implicitly_scoped_statement (parser, NULL);
9931 parser->in_switch_statement_p = in_switch_statement_p;
9932 parser->in_statement = in_statement;
9933
9934 /* Now we're all done with the switch-statement. */
9935 finish_switch_stmt (statement);
9936 }
9937
9938 return statement;
9939 }
9940 break;
9941
9942 default:
9943 cp_parser_error (parser, "expected selection-statement");
9944 return error_mark_node;
9945 }
9946 }
9947
9948 /* Parse a condition.
9949
9950 condition:
9951 expression
9952 type-specifier-seq declarator = initializer-clause
9953 type-specifier-seq declarator braced-init-list
9954
9955 GNU Extension:
9956
9957 condition:
9958 type-specifier-seq declarator asm-specification [opt]
9959 attributes [opt] = assignment-expression
9960
9961 Returns the expression that should be tested. */
9962
9963 static tree
9964 cp_parser_condition (cp_parser* parser)
9965 {
9966 cp_decl_specifier_seq type_specifiers;
9967 const char *saved_message;
9968 int declares_class_or_enum;
9969
9970 /* Try the declaration first. */
9971 cp_parser_parse_tentatively (parser);
9972 /* New types are not allowed in the type-specifier-seq for a
9973 condition. */
9974 saved_message = parser->type_definition_forbidden_message;
9975 parser->type_definition_forbidden_message
9976 = G_("types may not be defined in conditions");
9977 /* Parse the type-specifier-seq. */
9978 cp_parser_decl_specifier_seq (parser,
9979 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9980 &type_specifiers,
9981 &declares_class_or_enum);
9982 /* Restore the saved message. */
9983 parser->type_definition_forbidden_message = saved_message;
9984 /* If all is well, we might be looking at a declaration. */
9985 if (!cp_parser_error_occurred (parser))
9986 {
9987 tree decl;
9988 tree asm_specification;
9989 tree attributes;
9990 cp_declarator *declarator;
9991 tree initializer = NULL_TREE;
9992
9993 /* Parse the declarator. */
9994 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9995 /*ctor_dtor_or_conv_p=*/NULL,
9996 /*parenthesized_p=*/NULL,
9997 /*member_p=*/false);
9998 /* Parse the attributes. */
9999 attributes = cp_parser_attributes_opt (parser);
10000 /* Parse the asm-specification. */
10001 asm_specification = cp_parser_asm_specification_opt (parser);
10002 /* If the next token is not an `=' or '{', then we might still be
10003 looking at an expression. For example:
10004
10005 if (A(a).x)
10006
10007 looks like a decl-specifier-seq and a declarator -- but then
10008 there is no `=', so this is an expression. */
10009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10010 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10011 cp_parser_simulate_error (parser);
10012
10013 /* If we did see an `=' or '{', then we are looking at a declaration
10014 for sure. */
10015 if (cp_parser_parse_definitely (parser))
10016 {
10017 tree pushed_scope;
10018 bool non_constant_p;
10019 bool flags = LOOKUP_ONLYCONVERTING;
10020
10021 /* Create the declaration. */
10022 decl = start_decl (declarator, &type_specifiers,
10023 /*initialized_p=*/true,
10024 attributes, /*prefix_attributes=*/NULL_TREE,
10025 &pushed_scope);
10026
10027 /* Parse the initializer. */
10028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10029 {
10030 initializer = cp_parser_braced_list (parser, &non_constant_p);
10031 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10032 flags = 0;
10033 }
10034 else
10035 {
10036 /* Consume the `='. */
10037 cp_parser_require (parser, CPP_EQ, RT_EQ);
10038 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10039 }
10040 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10041 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10042
10043 /* Process the initializer. */
10044 cp_finish_decl (decl,
10045 initializer, !non_constant_p,
10046 asm_specification,
10047 flags);
10048
10049 if (pushed_scope)
10050 pop_scope (pushed_scope);
10051
10052 return convert_from_reference (decl);
10053 }
10054 }
10055 /* If we didn't even get past the declarator successfully, we are
10056 definitely not looking at a declaration. */
10057 else
10058 cp_parser_abort_tentative_parse (parser);
10059
10060 /* Otherwise, we are looking at an expression. */
10061 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10062 }
10063
10064 /* Parses a for-statement or range-for-statement until the closing ')',
10065 not included. */
10066
10067 static tree
10068 cp_parser_for (cp_parser *parser, bool ivdep)
10069 {
10070 tree init, scope, decl;
10071 bool is_range_for;
10072
10073 /* Begin the for-statement. */
10074 scope = begin_for_scope (&init);
10075
10076 /* Parse the initialization. */
10077 is_range_for = cp_parser_for_init_statement (parser, &decl);
10078
10079 if (is_range_for)
10080 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10081 else
10082 return cp_parser_c_for (parser, scope, init, ivdep);
10083 }
10084
10085 static tree
10086 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10087 {
10088 /* Normal for loop */
10089 tree condition = NULL_TREE;
10090 tree expression = NULL_TREE;
10091 tree stmt;
10092
10093 stmt = begin_for_stmt (scope, init);
10094 /* The for-init-statement has already been parsed in
10095 cp_parser_for_init_statement, so no work is needed here. */
10096 finish_for_init_stmt (stmt);
10097
10098 /* If there's a condition, process it. */
10099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10100 condition = cp_parser_condition (parser);
10101 else if (ivdep)
10102 {
10103 cp_parser_error (parser, "missing loop condition in loop with "
10104 "%<GCC ivdep%> pragma");
10105 condition = error_mark_node;
10106 }
10107 finish_for_cond (condition, stmt, ivdep);
10108 /* Look for the `;'. */
10109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10110
10111 /* If there's an expression, process it. */
10112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10113 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10114 finish_for_expr (expression, stmt);
10115
10116 return stmt;
10117 }
10118
10119 /* Tries to parse a range-based for-statement:
10120
10121 range-based-for:
10122 decl-specifier-seq declarator : expression
10123
10124 The decl-specifier-seq declarator and the `:' are already parsed by
10125 cp_parser_for_init_statement. If processing_template_decl it returns a
10126 newly created RANGE_FOR_STMT; if not, it is converted to a
10127 regular FOR_STMT. */
10128
10129 static tree
10130 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10131 bool ivdep)
10132 {
10133 tree stmt, range_expr;
10134
10135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10136 {
10137 bool expr_non_constant_p;
10138 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10139 }
10140 else
10141 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10142
10143 /* If in template, STMT is converted to a normal for-statement
10144 at instantiation. If not, it is done just ahead. */
10145 if (processing_template_decl)
10146 {
10147 if (check_for_bare_parameter_packs (range_expr))
10148 range_expr = error_mark_node;
10149 stmt = begin_range_for_stmt (scope, init);
10150 if (ivdep)
10151 RANGE_FOR_IVDEP (stmt) = 1;
10152 finish_range_for_decl (stmt, range_decl, range_expr);
10153 if (!type_dependent_expression_p (range_expr)
10154 /* do_auto_deduction doesn't mess with template init-lists. */
10155 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10156 do_range_for_auto_deduction (range_decl, range_expr);
10157 }
10158 else
10159 {
10160 stmt = begin_for_stmt (scope, init);
10161 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10162 }
10163 return stmt;
10164 }
10165
10166 /* Subroutine of cp_convert_range_for: given the initializer expression,
10167 builds up the range temporary. */
10168
10169 static tree
10170 build_range_temp (tree range_expr)
10171 {
10172 tree range_type, range_temp;
10173
10174 /* Find out the type deduced by the declaration
10175 `auto &&__range = range_expr'. */
10176 range_type = cp_build_reference_type (make_auto (), true);
10177 range_type = do_auto_deduction (range_type, range_expr,
10178 type_uses_auto (range_type));
10179
10180 /* Create the __range variable. */
10181 range_temp = build_decl (input_location, VAR_DECL,
10182 get_identifier ("__for_range"), range_type);
10183 TREE_USED (range_temp) = 1;
10184 DECL_ARTIFICIAL (range_temp) = 1;
10185
10186 return range_temp;
10187 }
10188
10189 /* Used by cp_parser_range_for in template context: we aren't going to
10190 do a full conversion yet, but we still need to resolve auto in the
10191 type of the for-range-declaration if present. This is basically
10192 a shortcut version of cp_convert_range_for. */
10193
10194 static void
10195 do_range_for_auto_deduction (tree decl, tree range_expr)
10196 {
10197 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10198 if (auto_node)
10199 {
10200 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10201 range_temp = convert_from_reference (build_range_temp (range_expr));
10202 iter_type = (cp_parser_perform_range_for_lookup
10203 (range_temp, &begin_dummy, &end_dummy));
10204 if (iter_type)
10205 {
10206 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10207 iter_type);
10208 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10209 tf_warning_or_error);
10210 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10211 iter_decl, auto_node);
10212 }
10213 }
10214 }
10215
10216 /* Converts a range-based for-statement into a normal
10217 for-statement, as per the definition.
10218
10219 for (RANGE_DECL : RANGE_EXPR)
10220 BLOCK
10221
10222 should be equivalent to:
10223
10224 {
10225 auto &&__range = RANGE_EXPR;
10226 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10227 __begin != __end;
10228 ++__begin)
10229 {
10230 RANGE_DECL = *__begin;
10231 BLOCK
10232 }
10233 }
10234
10235 If RANGE_EXPR is an array:
10236 BEGIN_EXPR = __range
10237 END_EXPR = __range + ARRAY_SIZE(__range)
10238 Else if RANGE_EXPR has a member 'begin' or 'end':
10239 BEGIN_EXPR = __range.begin()
10240 END_EXPR = __range.end()
10241 Else:
10242 BEGIN_EXPR = begin(__range)
10243 END_EXPR = end(__range);
10244
10245 If __range has a member 'begin' but not 'end', or vice versa, we must
10246 still use the second alternative (it will surely fail, however).
10247 When calling begin()/end() in the third alternative we must use
10248 argument dependent lookup, but always considering 'std' as an associated
10249 namespace. */
10250
10251 tree
10252 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10253 bool ivdep)
10254 {
10255 tree begin, end;
10256 tree iter_type, begin_expr, end_expr;
10257 tree condition, expression;
10258
10259 if (range_decl == error_mark_node || range_expr == error_mark_node)
10260 /* If an error happened previously do nothing or else a lot of
10261 unhelpful errors would be issued. */
10262 begin_expr = end_expr = iter_type = error_mark_node;
10263 else
10264 {
10265 tree range_temp;
10266
10267 if (TREE_CODE (range_expr) == VAR_DECL
10268 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10269 /* Can't bind a reference to an array of runtime bound. */
10270 range_temp = range_expr;
10271 else
10272 {
10273 range_temp = build_range_temp (range_expr);
10274 pushdecl (range_temp);
10275 cp_finish_decl (range_temp, range_expr,
10276 /*is_constant_init*/false, NULL_TREE,
10277 LOOKUP_ONLYCONVERTING);
10278 range_temp = convert_from_reference (range_temp);
10279 }
10280 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10281 &begin_expr, &end_expr);
10282 }
10283
10284 /* The new for initialization statement. */
10285 begin = build_decl (input_location, VAR_DECL,
10286 get_identifier ("__for_begin"), iter_type);
10287 TREE_USED (begin) = 1;
10288 DECL_ARTIFICIAL (begin) = 1;
10289 pushdecl (begin);
10290 cp_finish_decl (begin, begin_expr,
10291 /*is_constant_init*/false, NULL_TREE,
10292 LOOKUP_ONLYCONVERTING);
10293
10294 end = build_decl (input_location, VAR_DECL,
10295 get_identifier ("__for_end"), iter_type);
10296 TREE_USED (end) = 1;
10297 DECL_ARTIFICIAL (end) = 1;
10298 pushdecl (end);
10299 cp_finish_decl (end, end_expr,
10300 /*is_constant_init*/false, NULL_TREE,
10301 LOOKUP_ONLYCONVERTING);
10302
10303 finish_for_init_stmt (statement);
10304
10305 /* The new for condition. */
10306 condition = build_x_binary_op (input_location, NE_EXPR,
10307 begin, ERROR_MARK,
10308 end, ERROR_MARK,
10309 NULL, tf_warning_or_error);
10310 finish_for_cond (condition, statement, ivdep);
10311
10312 /* The new increment expression. */
10313 expression = finish_unary_op_expr (input_location,
10314 PREINCREMENT_EXPR, begin,
10315 tf_warning_or_error);
10316 finish_for_expr (expression, statement);
10317
10318 /* The declaration is initialized with *__begin inside the loop body. */
10319 cp_finish_decl (range_decl,
10320 build_x_indirect_ref (input_location, begin, RO_NULL,
10321 tf_warning_or_error),
10322 /*is_constant_init*/false, NULL_TREE,
10323 LOOKUP_ONLYCONVERTING);
10324
10325 return statement;
10326 }
10327
10328 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10329 We need to solve both at the same time because the method used
10330 depends on the existence of members begin or end.
10331 Returns the type deduced for the iterator expression. */
10332
10333 static tree
10334 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10335 {
10336 if (error_operand_p (range))
10337 {
10338 *begin = *end = error_mark_node;
10339 return error_mark_node;
10340 }
10341
10342 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10343 {
10344 error ("range-based %<for%> expression of type %qT "
10345 "has incomplete type", TREE_TYPE (range));
10346 *begin = *end = error_mark_node;
10347 return error_mark_node;
10348 }
10349 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10350 {
10351 /* If RANGE is an array, we will use pointer arithmetic. */
10352 *begin = range;
10353 *end = build_binary_op (input_location, PLUS_EXPR,
10354 range,
10355 array_type_nelts_top (TREE_TYPE (range)),
10356 0);
10357 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10358 }
10359 else
10360 {
10361 /* If it is not an array, we must do a bit of magic. */
10362 tree id_begin, id_end;
10363 tree member_begin, member_end;
10364
10365 *begin = *end = error_mark_node;
10366
10367 id_begin = get_identifier ("begin");
10368 id_end = get_identifier ("end");
10369 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10370 /*protect=*/2, /*want_type=*/false,
10371 tf_warning_or_error);
10372 member_end = lookup_member (TREE_TYPE (range), id_end,
10373 /*protect=*/2, /*want_type=*/false,
10374 tf_warning_or_error);
10375
10376 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10377 {
10378 /* Use the member functions. */
10379 if (member_begin != NULL_TREE)
10380 *begin = cp_parser_range_for_member_function (range, id_begin);
10381 else
10382 error ("range-based %<for%> expression of type %qT has an "
10383 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10384
10385 if (member_end != NULL_TREE)
10386 *end = cp_parser_range_for_member_function (range, id_end);
10387 else
10388 error ("range-based %<for%> expression of type %qT has a "
10389 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10390 }
10391 else
10392 {
10393 /* Use global functions with ADL. */
10394 vec<tree, va_gc> *vec;
10395 vec = make_tree_vector ();
10396
10397 vec_safe_push (vec, range);
10398
10399 member_begin = perform_koenig_lookup (id_begin, vec,
10400 tf_warning_or_error);
10401 *begin = finish_call_expr (member_begin, &vec, false, true,
10402 tf_warning_or_error);
10403 member_end = perform_koenig_lookup (id_end, vec,
10404 tf_warning_or_error);
10405 *end = finish_call_expr (member_end, &vec, false, true,
10406 tf_warning_or_error);
10407
10408 release_tree_vector (vec);
10409 }
10410
10411 /* Last common checks. */
10412 if (*begin == error_mark_node || *end == error_mark_node)
10413 {
10414 /* If one of the expressions is an error do no more checks. */
10415 *begin = *end = error_mark_node;
10416 return error_mark_node;
10417 }
10418 else if (type_dependent_expression_p (*begin)
10419 || type_dependent_expression_p (*end))
10420 /* Can happen, when, eg, in a template context, Koenig lookup
10421 can't resolve begin/end (c++/58503). */
10422 return NULL_TREE;
10423 else
10424 {
10425 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10426 /* The unqualified type of the __begin and __end temporaries should
10427 be the same, as required by the multiple auto declaration. */
10428 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10429 error ("inconsistent begin/end types in range-based %<for%> "
10430 "statement: %qT and %qT",
10431 TREE_TYPE (*begin), TREE_TYPE (*end));
10432 return iter_type;
10433 }
10434 }
10435 }
10436
10437 /* Helper function for cp_parser_perform_range_for_lookup.
10438 Builds a tree for RANGE.IDENTIFIER(). */
10439
10440 static tree
10441 cp_parser_range_for_member_function (tree range, tree identifier)
10442 {
10443 tree member, res;
10444 vec<tree, va_gc> *vec;
10445
10446 member = finish_class_member_access_expr (range, identifier,
10447 false, tf_warning_or_error);
10448 if (member == error_mark_node)
10449 return error_mark_node;
10450
10451 vec = make_tree_vector ();
10452 res = finish_call_expr (member, &vec,
10453 /*disallow_virtual=*/false,
10454 /*koenig_p=*/false,
10455 tf_warning_or_error);
10456 release_tree_vector (vec);
10457 return res;
10458 }
10459
10460 /* Parse an iteration-statement.
10461
10462 iteration-statement:
10463 while ( condition ) statement
10464 do statement while ( expression ) ;
10465 for ( for-init-statement condition [opt] ; expression [opt] )
10466 statement
10467
10468 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10469
10470 static tree
10471 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10472 {
10473 cp_token *token;
10474 enum rid keyword;
10475 tree statement;
10476 unsigned char in_statement;
10477
10478 /* Peek at the next token. */
10479 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10480 if (!token)
10481 return error_mark_node;
10482
10483 /* Remember whether or not we are already within an iteration
10484 statement. */
10485 in_statement = parser->in_statement;
10486
10487 /* See what kind of keyword it is. */
10488 keyword = token->keyword;
10489 switch (keyword)
10490 {
10491 case RID_WHILE:
10492 {
10493 tree condition;
10494
10495 /* Begin the while-statement. */
10496 statement = begin_while_stmt ();
10497 /* Look for the `('. */
10498 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10499 /* Parse the condition. */
10500 condition = cp_parser_condition (parser);
10501 finish_while_stmt_cond (condition, statement, ivdep);
10502 /* Look for the `)'. */
10503 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10504 /* Parse the dependent statement. */
10505 parser->in_statement = IN_ITERATION_STMT;
10506 cp_parser_already_scoped_statement (parser);
10507 parser->in_statement = in_statement;
10508 /* We're done with the while-statement. */
10509 finish_while_stmt (statement);
10510 }
10511 break;
10512
10513 case RID_DO:
10514 {
10515 tree expression;
10516
10517 /* Begin the do-statement. */
10518 statement = begin_do_stmt ();
10519 /* Parse the body of the do-statement. */
10520 parser->in_statement = IN_ITERATION_STMT;
10521 cp_parser_implicitly_scoped_statement (parser, NULL);
10522 parser->in_statement = in_statement;
10523 finish_do_body (statement);
10524 /* Look for the `while' keyword. */
10525 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10526 /* Look for the `('. */
10527 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10528 /* Parse the expression. */
10529 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10530 /* We're done with the do-statement. */
10531 finish_do_stmt (expression, statement, ivdep);
10532 /* Look for the `)'. */
10533 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10534 /* Look for the `;'. */
10535 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10536 }
10537 break;
10538
10539 case RID_FOR:
10540 {
10541 /* Look for the `('. */
10542 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10543
10544 statement = cp_parser_for (parser, ivdep);
10545
10546 /* Look for the `)'. */
10547 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10548
10549 /* Parse the body of the for-statement. */
10550 parser->in_statement = IN_ITERATION_STMT;
10551 cp_parser_already_scoped_statement (parser);
10552 parser->in_statement = in_statement;
10553
10554 /* We're done with the for-statement. */
10555 finish_for_stmt (statement);
10556 }
10557 break;
10558
10559 default:
10560 cp_parser_error (parser, "expected iteration-statement");
10561 statement = error_mark_node;
10562 break;
10563 }
10564
10565 return statement;
10566 }
10567
10568 /* Parse a for-init-statement or the declarator of a range-based-for.
10569 Returns true if a range-based-for declaration is seen.
10570
10571 for-init-statement:
10572 expression-statement
10573 simple-declaration */
10574
10575 static bool
10576 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10577 {
10578 /* If the next token is a `;', then we have an empty
10579 expression-statement. Grammatically, this is also a
10580 simple-declaration, but an invalid one, because it does not
10581 declare anything. Therefore, if we did not handle this case
10582 specially, we would issue an error message about an invalid
10583 declaration. */
10584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10585 {
10586 bool is_range_for = false;
10587 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10588
10589 parser->colon_corrects_to_scope_p = false;
10590
10591 /* We're going to speculatively look for a declaration, falling back
10592 to an expression, if necessary. */
10593 cp_parser_parse_tentatively (parser);
10594 /* Parse the declaration. */
10595 cp_parser_simple_declaration (parser,
10596 /*function_definition_allowed_p=*/false,
10597 decl);
10598 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10599 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10600 {
10601 /* It is a range-for, consume the ':' */
10602 cp_lexer_consume_token (parser->lexer);
10603 is_range_for = true;
10604 if (cxx_dialect < cxx11)
10605 {
10606 error_at (cp_lexer_peek_token (parser->lexer)->location,
10607 "range-based %<for%> loops are not allowed "
10608 "in C++98 mode");
10609 *decl = error_mark_node;
10610 }
10611 }
10612 else
10613 /* The ';' is not consumed yet because we told
10614 cp_parser_simple_declaration not to. */
10615 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10616
10617 if (cp_parser_parse_definitely (parser))
10618 return is_range_for;
10619 /* If the tentative parse failed, then we shall need to look for an
10620 expression-statement. */
10621 }
10622 /* If we are here, it is an expression-statement. */
10623 cp_parser_expression_statement (parser, NULL_TREE);
10624 return false;
10625 }
10626
10627 /* Parse a jump-statement.
10628
10629 jump-statement:
10630 break ;
10631 continue ;
10632 return expression [opt] ;
10633 return braced-init-list ;
10634 goto identifier ;
10635
10636 GNU extension:
10637
10638 jump-statement:
10639 goto * expression ;
10640
10641 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10642
10643 static tree
10644 cp_parser_jump_statement (cp_parser* parser)
10645 {
10646 tree statement = error_mark_node;
10647 cp_token *token;
10648 enum rid keyword;
10649 unsigned char in_statement;
10650
10651 /* Peek at the next token. */
10652 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10653 if (!token)
10654 return error_mark_node;
10655
10656 /* See what kind of keyword it is. */
10657 keyword = token->keyword;
10658 switch (keyword)
10659 {
10660 case RID_BREAK:
10661 in_statement = parser->in_statement & ~IN_IF_STMT;
10662 switch (in_statement)
10663 {
10664 case 0:
10665 error_at (token->location, "break statement not within loop or switch");
10666 break;
10667 default:
10668 gcc_assert ((in_statement & IN_SWITCH_STMT)
10669 || in_statement == IN_ITERATION_STMT);
10670 statement = finish_break_stmt ();
10671 if (in_statement == IN_ITERATION_STMT)
10672 break_maybe_infinite_loop ();
10673 break;
10674 case IN_OMP_BLOCK:
10675 error_at (token->location, "invalid exit from OpenMP structured block");
10676 break;
10677 case IN_OMP_FOR:
10678 error_at (token->location, "break statement used with OpenMP for loop");
10679 break;
10680 case IN_CILK_SIMD_FOR:
10681 error_at (token->location, "break statement used with Cilk Plus for loop");
10682 break;
10683 }
10684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10685 break;
10686
10687 case RID_CONTINUE:
10688 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10689 {
10690 case 0:
10691 error_at (token->location, "continue statement not within a loop");
10692 break;
10693 case IN_CILK_SIMD_FOR:
10694 error_at (token->location,
10695 "continue statement within %<#pragma simd%> loop body");
10696 /* Fall through. */
10697 case IN_ITERATION_STMT:
10698 case IN_OMP_FOR:
10699 statement = finish_continue_stmt ();
10700 break;
10701 case IN_OMP_BLOCK:
10702 error_at (token->location, "invalid exit from OpenMP structured block");
10703 break;
10704 default:
10705 gcc_unreachable ();
10706 }
10707 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10708 break;
10709
10710 case RID_RETURN:
10711 {
10712 tree expr;
10713 bool expr_non_constant_p;
10714
10715 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10716 {
10717 cp_lexer_set_source_position (parser->lexer);
10718 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10719 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10720 }
10721 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10722 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10723 else
10724 /* If the next token is a `;', then there is no
10725 expression. */
10726 expr = NULL_TREE;
10727 /* Build the return-statement. */
10728 statement = finish_return_stmt (expr);
10729 /* Look for the final `;'. */
10730 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10731 }
10732 break;
10733
10734 case RID_GOTO:
10735 /* Create the goto-statement. */
10736 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10737 {
10738 /* Issue a warning about this use of a GNU extension. */
10739 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10740 /* Consume the '*' token. */
10741 cp_lexer_consume_token (parser->lexer);
10742 /* Parse the dependent expression. */
10743 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10744 }
10745 else
10746 finish_goto_stmt (cp_parser_identifier (parser));
10747 /* Look for the final `;'. */
10748 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10749 break;
10750
10751 default:
10752 cp_parser_error (parser, "expected jump-statement");
10753 break;
10754 }
10755
10756 return statement;
10757 }
10758
10759 /* Parse a declaration-statement.
10760
10761 declaration-statement:
10762 block-declaration */
10763
10764 static void
10765 cp_parser_declaration_statement (cp_parser* parser)
10766 {
10767 void *p;
10768
10769 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10770 p = obstack_alloc (&declarator_obstack, 0);
10771
10772 /* Parse the block-declaration. */
10773 cp_parser_block_declaration (parser, /*statement_p=*/true);
10774
10775 /* Free any declarators allocated. */
10776 obstack_free (&declarator_obstack, p);
10777 }
10778
10779 /* Some dependent statements (like `if (cond) statement'), are
10780 implicitly in their own scope. In other words, if the statement is
10781 a single statement (as opposed to a compound-statement), it is
10782 none-the-less treated as if it were enclosed in braces. Any
10783 declarations appearing in the dependent statement are out of scope
10784 after control passes that point. This function parses a statement,
10785 but ensures that is in its own scope, even if it is not a
10786 compound-statement.
10787
10788 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10789 is a (possibly labeled) if statement which is not enclosed in
10790 braces and has an else clause. This is used to implement
10791 -Wparentheses.
10792
10793 Returns the new statement. */
10794
10795 static tree
10796 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10797 {
10798 tree statement;
10799
10800 if (if_p != NULL)
10801 *if_p = false;
10802
10803 /* Mark if () ; with a special NOP_EXPR. */
10804 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10805 {
10806 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10807 cp_lexer_consume_token (parser->lexer);
10808 statement = add_stmt (build_empty_stmt (loc));
10809 }
10810 /* if a compound is opened, we simply parse the statement directly. */
10811 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10812 statement = cp_parser_compound_statement (parser, NULL, false, false);
10813 /* If the token is not a `{', then we must take special action. */
10814 else
10815 {
10816 /* Create a compound-statement. */
10817 statement = begin_compound_stmt (0);
10818 /* Parse the dependent-statement. */
10819 cp_parser_statement (parser, NULL_TREE, false, if_p);
10820 /* Finish the dummy compound-statement. */
10821 finish_compound_stmt (statement);
10822 }
10823
10824 /* Return the statement. */
10825 return statement;
10826 }
10827
10828 /* For some dependent statements (like `while (cond) statement'), we
10829 have already created a scope. Therefore, even if the dependent
10830 statement is a compound-statement, we do not want to create another
10831 scope. */
10832
10833 static void
10834 cp_parser_already_scoped_statement (cp_parser* parser)
10835 {
10836 /* If the token is a `{', then we must take special action. */
10837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10838 cp_parser_statement (parser, NULL_TREE, false, NULL);
10839 else
10840 {
10841 /* Avoid calling cp_parser_compound_statement, so that we
10842 don't create a new scope. Do everything else by hand. */
10843 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10844 /* If the next keyword is `__label__' we have a label declaration. */
10845 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10846 cp_parser_label_declaration (parser);
10847 /* Parse an (optional) statement-seq. */
10848 cp_parser_statement_seq_opt (parser, NULL_TREE);
10849 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10850 }
10851 }
10852
10853 /* Declarations [gram.dcl.dcl] */
10854
10855 /* Parse an optional declaration-sequence.
10856
10857 declaration-seq:
10858 declaration
10859 declaration-seq declaration */
10860
10861 static void
10862 cp_parser_declaration_seq_opt (cp_parser* parser)
10863 {
10864 while (true)
10865 {
10866 cp_token *token;
10867
10868 token = cp_lexer_peek_token (parser->lexer);
10869
10870 if (token->type == CPP_CLOSE_BRACE
10871 || token->type == CPP_EOF
10872 || token->type == CPP_PRAGMA_EOL)
10873 break;
10874
10875 if (token->type == CPP_SEMICOLON)
10876 {
10877 /* A declaration consisting of a single semicolon is
10878 invalid. Allow it unless we're being pedantic. */
10879 cp_lexer_consume_token (parser->lexer);
10880 if (!in_system_header_at (input_location))
10881 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10882 continue;
10883 }
10884
10885 /* If we're entering or exiting a region that's implicitly
10886 extern "C", modify the lang context appropriately. */
10887 if (!parser->implicit_extern_c && token->implicit_extern_c)
10888 {
10889 push_lang_context (lang_name_c);
10890 parser->implicit_extern_c = true;
10891 }
10892 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10893 {
10894 pop_lang_context ();
10895 parser->implicit_extern_c = false;
10896 }
10897
10898 if (token->type == CPP_PRAGMA)
10899 {
10900 /* A top-level declaration can consist solely of a #pragma.
10901 A nested declaration cannot, so this is done here and not
10902 in cp_parser_declaration. (A #pragma at block scope is
10903 handled in cp_parser_statement.) */
10904 cp_parser_pragma (parser, pragma_external);
10905 continue;
10906 }
10907
10908 /* Parse the declaration itself. */
10909 cp_parser_declaration (parser);
10910 }
10911 }
10912
10913 /* Parse a declaration.
10914
10915 declaration:
10916 block-declaration
10917 function-definition
10918 template-declaration
10919 explicit-instantiation
10920 explicit-specialization
10921 linkage-specification
10922 namespace-definition
10923
10924 GNU extension:
10925
10926 declaration:
10927 __extension__ declaration */
10928
10929 static void
10930 cp_parser_declaration (cp_parser* parser)
10931 {
10932 cp_token token1;
10933 cp_token token2;
10934 int saved_pedantic;
10935 void *p;
10936 tree attributes = NULL_TREE;
10937
10938 /* Check for the `__extension__' keyword. */
10939 if (cp_parser_extension_opt (parser, &saved_pedantic))
10940 {
10941 /* Parse the qualified declaration. */
10942 cp_parser_declaration (parser);
10943 /* Restore the PEDANTIC flag. */
10944 pedantic = saved_pedantic;
10945
10946 return;
10947 }
10948
10949 /* Try to figure out what kind of declaration is present. */
10950 token1 = *cp_lexer_peek_token (parser->lexer);
10951
10952 if (token1.type != CPP_EOF)
10953 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10954 else
10955 {
10956 token2.type = CPP_EOF;
10957 token2.keyword = RID_MAX;
10958 }
10959
10960 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10961 p = obstack_alloc (&declarator_obstack, 0);
10962
10963 /* If the next token is `extern' and the following token is a string
10964 literal, then we have a linkage specification. */
10965 if (token1.keyword == RID_EXTERN
10966 && cp_parser_is_pure_string_literal (&token2))
10967 cp_parser_linkage_specification (parser);
10968 /* If the next token is `template', then we have either a template
10969 declaration, an explicit instantiation, or an explicit
10970 specialization. */
10971 else if (token1.keyword == RID_TEMPLATE)
10972 {
10973 /* `template <>' indicates a template specialization. */
10974 if (token2.type == CPP_LESS
10975 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10976 cp_parser_explicit_specialization (parser);
10977 /* `template <' indicates a template declaration. */
10978 else if (token2.type == CPP_LESS)
10979 cp_parser_template_declaration (parser, /*member_p=*/false);
10980 /* Anything else must be an explicit instantiation. */
10981 else
10982 cp_parser_explicit_instantiation (parser);
10983 }
10984 /* If the next token is `export', then we have a template
10985 declaration. */
10986 else if (token1.keyword == RID_EXPORT)
10987 cp_parser_template_declaration (parser, /*member_p=*/false);
10988 /* If the next token is `extern', 'static' or 'inline' and the one
10989 after that is `template', we have a GNU extended explicit
10990 instantiation directive. */
10991 else if (cp_parser_allow_gnu_extensions_p (parser)
10992 && (token1.keyword == RID_EXTERN
10993 || token1.keyword == RID_STATIC
10994 || token1.keyword == RID_INLINE)
10995 && token2.keyword == RID_TEMPLATE)
10996 cp_parser_explicit_instantiation (parser);
10997 /* If the next token is `namespace', check for a named or unnamed
10998 namespace definition. */
10999 else if (token1.keyword == RID_NAMESPACE
11000 && (/* A named namespace definition. */
11001 (token2.type == CPP_NAME
11002 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11003 != CPP_EQ))
11004 /* An unnamed namespace definition. */
11005 || token2.type == CPP_OPEN_BRACE
11006 || token2.keyword == RID_ATTRIBUTE))
11007 cp_parser_namespace_definition (parser);
11008 /* An inline (associated) namespace definition. */
11009 else if (token1.keyword == RID_INLINE
11010 && token2.keyword == RID_NAMESPACE)
11011 cp_parser_namespace_definition (parser);
11012 /* Objective-C++ declaration/definition. */
11013 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11014 cp_parser_objc_declaration (parser, NULL_TREE);
11015 else if (c_dialect_objc ()
11016 && token1.keyword == RID_ATTRIBUTE
11017 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11018 cp_parser_objc_declaration (parser, attributes);
11019 /* We must have either a block declaration or a function
11020 definition. */
11021 else
11022 /* Try to parse a block-declaration, or a function-definition. */
11023 cp_parser_block_declaration (parser, /*statement_p=*/false);
11024
11025 /* Free any declarators allocated. */
11026 obstack_free (&declarator_obstack, p);
11027 }
11028
11029 /* Parse a block-declaration.
11030
11031 block-declaration:
11032 simple-declaration
11033 asm-definition
11034 namespace-alias-definition
11035 using-declaration
11036 using-directive
11037
11038 GNU Extension:
11039
11040 block-declaration:
11041 __extension__ block-declaration
11042
11043 C++0x Extension:
11044
11045 block-declaration:
11046 static_assert-declaration
11047
11048 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11049 part of a declaration-statement. */
11050
11051 static void
11052 cp_parser_block_declaration (cp_parser *parser,
11053 bool statement_p)
11054 {
11055 cp_token *token1;
11056 int saved_pedantic;
11057
11058 /* Check for the `__extension__' keyword. */
11059 if (cp_parser_extension_opt (parser, &saved_pedantic))
11060 {
11061 /* Parse the qualified declaration. */
11062 cp_parser_block_declaration (parser, statement_p);
11063 /* Restore the PEDANTIC flag. */
11064 pedantic = saved_pedantic;
11065
11066 return;
11067 }
11068
11069 /* Peek at the next token to figure out which kind of declaration is
11070 present. */
11071 token1 = cp_lexer_peek_token (parser->lexer);
11072
11073 /* If the next keyword is `asm', we have an asm-definition. */
11074 if (token1->keyword == RID_ASM)
11075 {
11076 if (statement_p)
11077 cp_parser_commit_to_tentative_parse (parser);
11078 cp_parser_asm_definition (parser);
11079 }
11080 /* If the next keyword is `namespace', we have a
11081 namespace-alias-definition. */
11082 else if (token1->keyword == RID_NAMESPACE)
11083 cp_parser_namespace_alias_definition (parser);
11084 /* If the next keyword is `using', we have a
11085 using-declaration, a using-directive, or an alias-declaration. */
11086 else if (token1->keyword == RID_USING)
11087 {
11088 cp_token *token2;
11089
11090 if (statement_p)
11091 cp_parser_commit_to_tentative_parse (parser);
11092 /* If the token after `using' is `namespace', then we have a
11093 using-directive. */
11094 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11095 if (token2->keyword == RID_NAMESPACE)
11096 cp_parser_using_directive (parser);
11097 /* If the second token after 'using' is '=', then we have an
11098 alias-declaration. */
11099 else if (cxx_dialect >= cxx11
11100 && token2->type == CPP_NAME
11101 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11102 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11103 cp_parser_alias_declaration (parser);
11104 /* Otherwise, it's a using-declaration. */
11105 else
11106 cp_parser_using_declaration (parser,
11107 /*access_declaration_p=*/false);
11108 }
11109 /* If the next keyword is `__label__' we have a misplaced label
11110 declaration. */
11111 else if (token1->keyword == RID_LABEL)
11112 {
11113 cp_lexer_consume_token (parser->lexer);
11114 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11115 cp_parser_skip_to_end_of_statement (parser);
11116 /* If the next token is now a `;', consume it. */
11117 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11118 cp_lexer_consume_token (parser->lexer);
11119 }
11120 /* If the next token is `static_assert' we have a static assertion. */
11121 else if (token1->keyword == RID_STATIC_ASSERT)
11122 cp_parser_static_assert (parser, /*member_p=*/false);
11123 /* Anything else must be a simple-declaration. */
11124 else
11125 cp_parser_simple_declaration (parser, !statement_p,
11126 /*maybe_range_for_decl*/NULL);
11127 }
11128
11129 /* Parse a simple-declaration.
11130
11131 simple-declaration:
11132 decl-specifier-seq [opt] init-declarator-list [opt] ;
11133
11134 init-declarator-list:
11135 init-declarator
11136 init-declarator-list , init-declarator
11137
11138 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11139 function-definition as a simple-declaration.
11140
11141 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11142 parsed declaration if it is an uninitialized single declarator not followed
11143 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11144 if present, will not be consumed. */
11145
11146 static void
11147 cp_parser_simple_declaration (cp_parser* parser,
11148 bool function_definition_allowed_p,
11149 tree *maybe_range_for_decl)
11150 {
11151 cp_decl_specifier_seq decl_specifiers;
11152 int declares_class_or_enum;
11153 bool saw_declarator;
11154
11155 if (maybe_range_for_decl)
11156 *maybe_range_for_decl = NULL_TREE;
11157
11158 /* Defer access checks until we know what is being declared; the
11159 checks for names appearing in the decl-specifier-seq should be
11160 done as if we were in the scope of the thing being declared. */
11161 push_deferring_access_checks (dk_deferred);
11162
11163 /* Parse the decl-specifier-seq. We have to keep track of whether
11164 or not the decl-specifier-seq declares a named class or
11165 enumeration type, since that is the only case in which the
11166 init-declarator-list is allowed to be empty.
11167
11168 [dcl.dcl]
11169
11170 In a simple-declaration, the optional init-declarator-list can be
11171 omitted only when declaring a class or enumeration, that is when
11172 the decl-specifier-seq contains either a class-specifier, an
11173 elaborated-type-specifier, or an enum-specifier. */
11174 cp_parser_decl_specifier_seq (parser,
11175 CP_PARSER_FLAGS_OPTIONAL,
11176 &decl_specifiers,
11177 &declares_class_or_enum);
11178 /* We no longer need to defer access checks. */
11179 stop_deferring_access_checks ();
11180
11181 /* In a block scope, a valid declaration must always have a
11182 decl-specifier-seq. By not trying to parse declarators, we can
11183 resolve the declaration/expression ambiguity more quickly. */
11184 if (!function_definition_allowed_p
11185 && !decl_specifiers.any_specifiers_p)
11186 {
11187 cp_parser_error (parser, "expected declaration");
11188 goto done;
11189 }
11190
11191 /* If the next two tokens are both identifiers, the code is
11192 erroneous. The usual cause of this situation is code like:
11193
11194 T t;
11195
11196 where "T" should name a type -- but does not. */
11197 if (!decl_specifiers.any_type_specifiers_p
11198 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11199 {
11200 /* If parsing tentatively, we should commit; we really are
11201 looking at a declaration. */
11202 cp_parser_commit_to_tentative_parse (parser);
11203 /* Give up. */
11204 goto done;
11205 }
11206
11207 /* If we have seen at least one decl-specifier, and the next token
11208 is not a parenthesis, then we must be looking at a declaration.
11209 (After "int (" we might be looking at a functional cast.) */
11210 if (decl_specifiers.any_specifiers_p
11211 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11212 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11213 && !cp_parser_error_occurred (parser))
11214 cp_parser_commit_to_tentative_parse (parser);
11215
11216 /* Keep going until we hit the `;' at the end of the simple
11217 declaration. */
11218 saw_declarator = false;
11219 while (cp_lexer_next_token_is_not (parser->lexer,
11220 CPP_SEMICOLON))
11221 {
11222 cp_token *token;
11223 bool function_definition_p;
11224 tree decl;
11225
11226 if (saw_declarator)
11227 {
11228 /* If we are processing next declarator, coma is expected */
11229 token = cp_lexer_peek_token (parser->lexer);
11230 gcc_assert (token->type == CPP_COMMA);
11231 cp_lexer_consume_token (parser->lexer);
11232 if (maybe_range_for_decl)
11233 *maybe_range_for_decl = error_mark_node;
11234 }
11235 else
11236 saw_declarator = true;
11237
11238 /* Parse the init-declarator. */
11239 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11240 /*checks=*/NULL,
11241 function_definition_allowed_p,
11242 /*member_p=*/false,
11243 declares_class_or_enum,
11244 &function_definition_p,
11245 maybe_range_for_decl);
11246 /* If an error occurred while parsing tentatively, exit quickly.
11247 (That usually happens when in the body of a function; each
11248 statement is treated as a declaration-statement until proven
11249 otherwise.) */
11250 if (cp_parser_error_occurred (parser))
11251 goto done;
11252 /* Handle function definitions specially. */
11253 if (function_definition_p)
11254 {
11255 /* If the next token is a `,', then we are probably
11256 processing something like:
11257
11258 void f() {}, *p;
11259
11260 which is erroneous. */
11261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11262 {
11263 cp_token *token = cp_lexer_peek_token (parser->lexer);
11264 error_at (token->location,
11265 "mixing"
11266 " declarations and function-definitions is forbidden");
11267 }
11268 /* Otherwise, we're done with the list of declarators. */
11269 else
11270 {
11271 pop_deferring_access_checks ();
11272 return;
11273 }
11274 }
11275 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11276 *maybe_range_for_decl = decl;
11277 /* The next token should be either a `,' or a `;'. */
11278 token = cp_lexer_peek_token (parser->lexer);
11279 /* If it's a `,', there are more declarators to come. */
11280 if (token->type == CPP_COMMA)
11281 /* will be consumed next time around */;
11282 /* If it's a `;', we are done. */
11283 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11284 break;
11285 /* Anything else is an error. */
11286 else
11287 {
11288 /* If we have already issued an error message we don't need
11289 to issue another one. */
11290 if (decl != error_mark_node
11291 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11292 cp_parser_error (parser, "expected %<,%> or %<;%>");
11293 /* Skip tokens until we reach the end of the statement. */
11294 cp_parser_skip_to_end_of_statement (parser);
11295 /* If the next token is now a `;', consume it. */
11296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11297 cp_lexer_consume_token (parser->lexer);
11298 goto done;
11299 }
11300 /* After the first time around, a function-definition is not
11301 allowed -- even if it was OK at first. For example:
11302
11303 int i, f() {}
11304
11305 is not valid. */
11306 function_definition_allowed_p = false;
11307 }
11308
11309 /* Issue an error message if no declarators are present, and the
11310 decl-specifier-seq does not itself declare a class or
11311 enumeration: [dcl.dcl]/3. */
11312 if (!saw_declarator)
11313 {
11314 if (cp_parser_declares_only_class_p (parser))
11315 {
11316 if (!declares_class_or_enum
11317 && decl_specifiers.type
11318 && OVERLOAD_TYPE_P (decl_specifiers.type))
11319 /* Ensure an error is issued anyway when finish_decltype_type,
11320 called via cp_parser_decl_specifier_seq, returns a class or
11321 an enumeration (c++/51786). */
11322 decl_specifiers.type = NULL_TREE;
11323 shadow_tag (&decl_specifiers);
11324 }
11325 /* Perform any deferred access checks. */
11326 perform_deferred_access_checks (tf_warning_or_error);
11327 }
11328
11329 /* Consume the `;'. */
11330 if (!maybe_range_for_decl)
11331 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11332
11333 done:
11334 pop_deferring_access_checks ();
11335 }
11336
11337 /* Parse a decl-specifier-seq.
11338
11339 decl-specifier-seq:
11340 decl-specifier-seq [opt] decl-specifier
11341 decl-specifier attribute-specifier-seq [opt] (C++11)
11342
11343 decl-specifier:
11344 storage-class-specifier
11345 type-specifier
11346 function-specifier
11347 friend
11348 typedef
11349
11350 GNU Extension:
11351
11352 decl-specifier:
11353 attributes
11354
11355 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11356
11357 The parser flags FLAGS is used to control type-specifier parsing.
11358
11359 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11360 flags:
11361
11362 1: one of the decl-specifiers is an elaborated-type-specifier
11363 (i.e., a type declaration)
11364 2: one of the decl-specifiers is an enum-specifier or a
11365 class-specifier (i.e., a type definition)
11366
11367 */
11368
11369 static void
11370 cp_parser_decl_specifier_seq (cp_parser* parser,
11371 cp_parser_flags flags,
11372 cp_decl_specifier_seq *decl_specs,
11373 int* declares_class_or_enum)
11374 {
11375 bool constructor_possible_p = !parser->in_declarator_p;
11376 bool found_decl_spec = false;
11377 cp_token *start_token = NULL;
11378 cp_decl_spec ds;
11379
11380 /* Clear DECL_SPECS. */
11381 clear_decl_specs (decl_specs);
11382
11383 /* Assume no class or enumeration type is declared. */
11384 *declares_class_or_enum = 0;
11385
11386 /* Keep reading specifiers until there are no more to read. */
11387 while (true)
11388 {
11389 bool constructor_p;
11390 cp_token *token;
11391 ds = ds_last;
11392
11393 /* Peek at the next token. */
11394 token = cp_lexer_peek_token (parser->lexer);
11395
11396 /* Save the first token of the decl spec list for error
11397 reporting. */
11398 if (!start_token)
11399 start_token = token;
11400 /* Handle attributes. */
11401 if (cp_next_tokens_can_be_attribute_p (parser))
11402 {
11403 /* Parse the attributes. */
11404 tree attrs = cp_parser_attributes_opt (parser);
11405
11406 /* In a sequence of declaration specifiers, c++11 attributes
11407 appertain to the type that precede them. In that case
11408 [dcl.spec]/1 says:
11409
11410 The attribute-specifier-seq affects the type only for
11411 the declaration it appears in, not other declarations
11412 involving the same type.
11413
11414 But for now let's force the user to position the
11415 attribute either at the beginning of the declaration or
11416 after the declarator-id, which would clearly mean that it
11417 applies to the declarator. */
11418 if (cxx11_attribute_p (attrs))
11419 {
11420 if (!found_decl_spec)
11421 /* The c++11 attribute is at the beginning of the
11422 declaration. It appertains to the entity being
11423 declared. */;
11424 else
11425 {
11426 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11427 {
11428 /* This is an attribute following a
11429 class-specifier. */
11430 if (decl_specs->type_definition_p)
11431 warn_misplaced_attr_for_class_type (token->location,
11432 decl_specs->type);
11433 attrs = NULL_TREE;
11434 }
11435 else
11436 {
11437 decl_specs->std_attributes
11438 = chainon (decl_specs->std_attributes,
11439 attrs);
11440 if (decl_specs->locations[ds_std_attribute] == 0)
11441 decl_specs->locations[ds_std_attribute] = token->location;
11442 }
11443 continue;
11444 }
11445 }
11446
11447 decl_specs->attributes
11448 = chainon (decl_specs->attributes,
11449 attrs);
11450 if (decl_specs->locations[ds_attribute] == 0)
11451 decl_specs->locations[ds_attribute] = token->location;
11452 continue;
11453 }
11454 /* Assume we will find a decl-specifier keyword. */
11455 found_decl_spec = true;
11456 /* If the next token is an appropriate keyword, we can simply
11457 add it to the list. */
11458 switch (token->keyword)
11459 {
11460 /* decl-specifier:
11461 friend
11462 constexpr */
11463 case RID_FRIEND:
11464 if (!at_class_scope_p ())
11465 {
11466 error_at (token->location, "%<friend%> used outside of class");
11467 cp_lexer_purge_token (parser->lexer);
11468 }
11469 else
11470 {
11471 ds = ds_friend;
11472 /* Consume the token. */
11473 cp_lexer_consume_token (parser->lexer);
11474 }
11475 break;
11476
11477 case RID_CONSTEXPR:
11478 ds = ds_constexpr;
11479 cp_lexer_consume_token (parser->lexer);
11480 break;
11481
11482 /* function-specifier:
11483 inline
11484 virtual
11485 explicit */
11486 case RID_INLINE:
11487 case RID_VIRTUAL:
11488 case RID_EXPLICIT:
11489 cp_parser_function_specifier_opt (parser, decl_specs);
11490 break;
11491
11492 /* decl-specifier:
11493 typedef */
11494 case RID_TYPEDEF:
11495 ds = ds_typedef;
11496 /* Consume the token. */
11497 cp_lexer_consume_token (parser->lexer);
11498 /* A constructor declarator cannot appear in a typedef. */
11499 constructor_possible_p = false;
11500 /* The "typedef" keyword can only occur in a declaration; we
11501 may as well commit at this point. */
11502 cp_parser_commit_to_tentative_parse (parser);
11503
11504 if (decl_specs->storage_class != sc_none)
11505 decl_specs->conflicting_specifiers_p = true;
11506 break;
11507
11508 /* storage-class-specifier:
11509 auto
11510 register
11511 static
11512 extern
11513 mutable
11514
11515 GNU Extension:
11516 thread */
11517 case RID_AUTO:
11518 if (cxx_dialect == cxx98)
11519 {
11520 /* Consume the token. */
11521 cp_lexer_consume_token (parser->lexer);
11522
11523 /* Complain about `auto' as a storage specifier, if
11524 we're complaining about C++0x compatibility. */
11525 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11526 " changes meaning in C++11; please remove it");
11527
11528 /* Set the storage class anyway. */
11529 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11530 token);
11531 }
11532 else
11533 /* C++0x auto type-specifier. */
11534 found_decl_spec = false;
11535 break;
11536
11537 case RID_REGISTER:
11538 case RID_STATIC:
11539 case RID_EXTERN:
11540 case RID_MUTABLE:
11541 /* Consume the token. */
11542 cp_lexer_consume_token (parser->lexer);
11543 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11544 token);
11545 break;
11546 case RID_THREAD:
11547 /* Consume the token. */
11548 ds = ds_thread;
11549 cp_lexer_consume_token (parser->lexer);
11550 break;
11551
11552 default:
11553 /* We did not yet find a decl-specifier yet. */
11554 found_decl_spec = false;
11555 break;
11556 }
11557
11558 if (found_decl_spec
11559 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11560 && token->keyword != RID_CONSTEXPR)
11561 error ("decl-specifier invalid in condition");
11562
11563 if (ds != ds_last)
11564 set_and_check_decl_spec_loc (decl_specs, ds, token);
11565
11566 /* Constructors are a special case. The `S' in `S()' is not a
11567 decl-specifier; it is the beginning of the declarator. */
11568 constructor_p
11569 = (!found_decl_spec
11570 && constructor_possible_p
11571 && (cp_parser_constructor_declarator_p
11572 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11573
11574 /* If we don't have a DECL_SPEC yet, then we must be looking at
11575 a type-specifier. */
11576 if (!found_decl_spec && !constructor_p)
11577 {
11578 int decl_spec_declares_class_or_enum;
11579 bool is_cv_qualifier;
11580 tree type_spec;
11581
11582 type_spec
11583 = cp_parser_type_specifier (parser, flags,
11584 decl_specs,
11585 /*is_declaration=*/true,
11586 &decl_spec_declares_class_or_enum,
11587 &is_cv_qualifier);
11588 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11589
11590 /* If this type-specifier referenced a user-defined type
11591 (a typedef, class-name, etc.), then we can't allow any
11592 more such type-specifiers henceforth.
11593
11594 [dcl.spec]
11595
11596 The longest sequence of decl-specifiers that could
11597 possibly be a type name is taken as the
11598 decl-specifier-seq of a declaration. The sequence shall
11599 be self-consistent as described below.
11600
11601 [dcl.type]
11602
11603 As a general rule, at most one type-specifier is allowed
11604 in the complete decl-specifier-seq of a declaration. The
11605 only exceptions are the following:
11606
11607 -- const or volatile can be combined with any other
11608 type-specifier.
11609
11610 -- signed or unsigned can be combined with char, long,
11611 short, or int.
11612
11613 -- ..
11614
11615 Example:
11616
11617 typedef char* Pc;
11618 void g (const int Pc);
11619
11620 Here, Pc is *not* part of the decl-specifier seq; it's
11621 the declarator. Therefore, once we see a type-specifier
11622 (other than a cv-qualifier), we forbid any additional
11623 user-defined types. We *do* still allow things like `int
11624 int' to be considered a decl-specifier-seq, and issue the
11625 error message later. */
11626 if (type_spec && !is_cv_qualifier)
11627 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11628 /* A constructor declarator cannot follow a type-specifier. */
11629 if (type_spec)
11630 {
11631 constructor_possible_p = false;
11632 found_decl_spec = true;
11633 if (!is_cv_qualifier)
11634 decl_specs->any_type_specifiers_p = true;
11635 }
11636 }
11637
11638 /* If we still do not have a DECL_SPEC, then there are no more
11639 decl-specifiers. */
11640 if (!found_decl_spec)
11641 break;
11642
11643 decl_specs->any_specifiers_p = true;
11644 /* After we see one decl-specifier, further decl-specifiers are
11645 always optional. */
11646 flags |= CP_PARSER_FLAGS_OPTIONAL;
11647 }
11648
11649 /* Don't allow a friend specifier with a class definition. */
11650 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11651 && (*declares_class_or_enum & 2))
11652 error_at (decl_specs->locations[ds_friend],
11653 "class definition may not be declared a friend");
11654 }
11655
11656 /* Parse an (optional) storage-class-specifier.
11657
11658 storage-class-specifier:
11659 auto
11660 register
11661 static
11662 extern
11663 mutable
11664
11665 GNU Extension:
11666
11667 storage-class-specifier:
11668 thread
11669
11670 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11671
11672 static tree
11673 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11674 {
11675 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11676 {
11677 case RID_AUTO:
11678 if (cxx_dialect != cxx98)
11679 return NULL_TREE;
11680 /* Fall through for C++98. */
11681
11682 case RID_REGISTER:
11683 case RID_STATIC:
11684 case RID_EXTERN:
11685 case RID_MUTABLE:
11686 case RID_THREAD:
11687 /* Consume the token. */
11688 return cp_lexer_consume_token (parser->lexer)->u.value;
11689
11690 default:
11691 return NULL_TREE;
11692 }
11693 }
11694
11695 /* Parse an (optional) function-specifier.
11696
11697 function-specifier:
11698 inline
11699 virtual
11700 explicit
11701
11702 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11703 Updates DECL_SPECS, if it is non-NULL. */
11704
11705 static tree
11706 cp_parser_function_specifier_opt (cp_parser* parser,
11707 cp_decl_specifier_seq *decl_specs)
11708 {
11709 cp_token *token = cp_lexer_peek_token (parser->lexer);
11710 switch (token->keyword)
11711 {
11712 case RID_INLINE:
11713 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11714 break;
11715
11716 case RID_VIRTUAL:
11717 /* 14.5.2.3 [temp.mem]
11718
11719 A member function template shall not be virtual. */
11720 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11721 error_at (token->location, "templates may not be %<virtual%>");
11722 else
11723 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11724 break;
11725
11726 case RID_EXPLICIT:
11727 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11728 break;
11729
11730 default:
11731 return NULL_TREE;
11732 }
11733
11734 /* Consume the token. */
11735 return cp_lexer_consume_token (parser->lexer)->u.value;
11736 }
11737
11738 /* Parse a linkage-specification.
11739
11740 linkage-specification:
11741 extern string-literal { declaration-seq [opt] }
11742 extern string-literal declaration */
11743
11744 static void
11745 cp_parser_linkage_specification (cp_parser* parser)
11746 {
11747 tree linkage;
11748
11749 /* Look for the `extern' keyword. */
11750 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11751
11752 /* Look for the string-literal. */
11753 linkage = cp_parser_string_literal (parser, false, false);
11754
11755 /* Transform the literal into an identifier. If the literal is a
11756 wide-character string, or contains embedded NULs, then we can't
11757 handle it as the user wants. */
11758 if (strlen (TREE_STRING_POINTER (linkage))
11759 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11760 {
11761 cp_parser_error (parser, "invalid linkage-specification");
11762 /* Assume C++ linkage. */
11763 linkage = lang_name_cplusplus;
11764 }
11765 else
11766 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11767
11768 /* We're now using the new linkage. */
11769 push_lang_context (linkage);
11770
11771 /* If the next token is a `{', then we're using the first
11772 production. */
11773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11774 {
11775 cp_ensure_no_omp_declare_simd (parser);
11776
11777 /* Consume the `{' token. */
11778 cp_lexer_consume_token (parser->lexer);
11779 /* Parse the declarations. */
11780 cp_parser_declaration_seq_opt (parser);
11781 /* Look for the closing `}'. */
11782 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11783 }
11784 /* Otherwise, there's just one declaration. */
11785 else
11786 {
11787 bool saved_in_unbraced_linkage_specification_p;
11788
11789 saved_in_unbraced_linkage_specification_p
11790 = parser->in_unbraced_linkage_specification_p;
11791 parser->in_unbraced_linkage_specification_p = true;
11792 cp_parser_declaration (parser);
11793 parser->in_unbraced_linkage_specification_p
11794 = saved_in_unbraced_linkage_specification_p;
11795 }
11796
11797 /* We're done with the linkage-specification. */
11798 pop_lang_context ();
11799 }
11800
11801 /* Parse a static_assert-declaration.
11802
11803 static_assert-declaration:
11804 static_assert ( constant-expression , string-literal ) ;
11805
11806 If MEMBER_P, this static_assert is a class member. */
11807
11808 static void
11809 cp_parser_static_assert(cp_parser *parser, bool member_p)
11810 {
11811 tree condition;
11812 tree message;
11813 cp_token *token;
11814 location_t saved_loc;
11815 bool dummy;
11816
11817 /* Peek at the `static_assert' token so we can keep track of exactly
11818 where the static assertion started. */
11819 token = cp_lexer_peek_token (parser->lexer);
11820 saved_loc = token->location;
11821
11822 /* Look for the `static_assert' keyword. */
11823 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11824 RT_STATIC_ASSERT))
11825 return;
11826
11827 /* We know we are in a static assertion; commit to any tentative
11828 parse. */
11829 if (cp_parser_parsing_tentatively (parser))
11830 cp_parser_commit_to_tentative_parse (parser);
11831
11832 /* Parse the `(' starting the static assertion condition. */
11833 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11834
11835 /* Parse the constant-expression. Allow a non-constant expression
11836 here in order to give better diagnostics in finish_static_assert. */
11837 condition =
11838 cp_parser_constant_expression (parser,
11839 /*allow_non_constant_p=*/true,
11840 /*non_constant_p=*/&dummy);
11841
11842 /* Parse the separating `,'. */
11843 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11844
11845 /* Parse the string-literal message. */
11846 message = cp_parser_string_literal (parser,
11847 /*translate=*/false,
11848 /*wide_ok=*/true);
11849
11850 /* A `)' completes the static assertion. */
11851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11852 cp_parser_skip_to_closing_parenthesis (parser,
11853 /*recovering=*/true,
11854 /*or_comma=*/false,
11855 /*consume_paren=*/true);
11856
11857 /* A semicolon terminates the declaration. */
11858 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11859
11860 /* Complete the static assertion, which may mean either processing
11861 the static assert now or saving it for template instantiation. */
11862 finish_static_assert (condition, message, saved_loc, member_p);
11863 }
11864
11865 /* Parse the expression in decltype ( expression ). */
11866
11867 static tree
11868 cp_parser_decltype_expr (cp_parser *parser,
11869 bool &id_expression_or_member_access_p)
11870 {
11871 cp_token *id_expr_start_token;
11872 tree expr;
11873
11874 /* First, try parsing an id-expression. */
11875 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11876 cp_parser_parse_tentatively (parser);
11877 expr = cp_parser_id_expression (parser,
11878 /*template_keyword_p=*/false,
11879 /*check_dependency_p=*/true,
11880 /*template_p=*/NULL,
11881 /*declarator_p=*/false,
11882 /*optional_p=*/false);
11883
11884 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11885 {
11886 bool non_integral_constant_expression_p = false;
11887 tree id_expression = expr;
11888 cp_id_kind idk;
11889 const char *error_msg;
11890
11891 if (identifier_p (expr))
11892 /* Lookup the name we got back from the id-expression. */
11893 expr = cp_parser_lookup_name_simple (parser, expr,
11894 id_expr_start_token->location);
11895
11896 if (expr
11897 && expr != error_mark_node
11898 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11899 && TREE_CODE (expr) != TYPE_DECL
11900 && (TREE_CODE (expr) != BIT_NOT_EXPR
11901 || !TYPE_P (TREE_OPERAND (expr, 0)))
11902 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11903 {
11904 /* Complete lookup of the id-expression. */
11905 expr = (finish_id_expression
11906 (id_expression, expr, parser->scope, &idk,
11907 /*integral_constant_expression_p=*/false,
11908 /*allow_non_integral_constant_expression_p=*/true,
11909 &non_integral_constant_expression_p,
11910 /*template_p=*/false,
11911 /*done=*/true,
11912 /*address_p=*/false,
11913 /*template_arg_p=*/false,
11914 &error_msg,
11915 id_expr_start_token->location));
11916
11917 if (expr == error_mark_node)
11918 /* We found an id-expression, but it was something that we
11919 should not have found. This is an error, not something
11920 we can recover from, so note that we found an
11921 id-expression and we'll recover as gracefully as
11922 possible. */
11923 id_expression_or_member_access_p = true;
11924 }
11925
11926 if (expr
11927 && expr != error_mark_node
11928 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11929 /* We have an id-expression. */
11930 id_expression_or_member_access_p = true;
11931 }
11932
11933 if (!id_expression_or_member_access_p)
11934 {
11935 /* Abort the id-expression parse. */
11936 cp_parser_abort_tentative_parse (parser);
11937
11938 /* Parsing tentatively, again. */
11939 cp_parser_parse_tentatively (parser);
11940
11941 /* Parse a class member access. */
11942 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11943 /*cast_p=*/false, /*decltype*/true,
11944 /*member_access_only_p=*/true, NULL);
11945
11946 if (expr
11947 && expr != error_mark_node
11948 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11949 /* We have an id-expression. */
11950 id_expression_or_member_access_p = true;
11951 }
11952
11953 if (id_expression_or_member_access_p)
11954 /* We have parsed the complete id-expression or member access. */
11955 cp_parser_parse_definitely (parser);
11956 else
11957 {
11958 /* Abort our attempt to parse an id-expression or member access
11959 expression. */
11960 cp_parser_abort_tentative_parse (parser);
11961
11962 /* Parse a full expression. */
11963 expr = cp_parser_expression (parser, /*cast_p=*/false,
11964 /*decltype*/true, NULL);
11965 }
11966
11967 return expr;
11968 }
11969
11970 /* Parse a `decltype' type. Returns the type.
11971
11972 simple-type-specifier:
11973 decltype ( expression )
11974 C++14 proposal:
11975 decltype ( auto ) */
11976
11977 static tree
11978 cp_parser_decltype (cp_parser *parser)
11979 {
11980 tree expr;
11981 bool id_expression_or_member_access_p = false;
11982 const char *saved_message;
11983 bool saved_integral_constant_expression_p;
11984 bool saved_non_integral_constant_expression_p;
11985 bool saved_greater_than_is_operator_p;
11986 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11987
11988 if (start_token->type == CPP_DECLTYPE)
11989 {
11990 /* Already parsed. */
11991 cp_lexer_consume_token (parser->lexer);
11992 return start_token->u.value;
11993 }
11994
11995 /* Look for the `decltype' token. */
11996 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11997 return error_mark_node;
11998
11999 /* Parse the opening `('. */
12000 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12001 return error_mark_node;
12002
12003 /* decltype (auto) */
12004 if (cxx_dialect >= cxx1y
12005 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12006 {
12007 cp_lexer_consume_token (parser->lexer);
12008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12009 return error_mark_node;
12010 expr = make_decltype_auto ();
12011 AUTO_IS_DECLTYPE (expr) = true;
12012 goto rewrite;
12013 }
12014
12015 /* Types cannot be defined in a `decltype' expression. Save away the
12016 old message. */
12017 saved_message = parser->type_definition_forbidden_message;
12018
12019 /* And create the new one. */
12020 parser->type_definition_forbidden_message
12021 = G_("types may not be defined in %<decltype%> expressions");
12022
12023 /* The restrictions on constant-expressions do not apply inside
12024 decltype expressions. */
12025 saved_integral_constant_expression_p
12026 = parser->integral_constant_expression_p;
12027 saved_non_integral_constant_expression_p
12028 = parser->non_integral_constant_expression_p;
12029 parser->integral_constant_expression_p = false;
12030
12031 /* Within a parenthesized expression, a `>' token is always
12032 the greater-than operator. */
12033 saved_greater_than_is_operator_p
12034 = parser->greater_than_is_operator_p;
12035 parser->greater_than_is_operator_p = true;
12036
12037 /* Do not actually evaluate the expression. */
12038 ++cp_unevaluated_operand;
12039
12040 /* Do not warn about problems with the expression. */
12041 ++c_inhibit_evaluation_warnings;
12042
12043 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12044
12045 /* Go back to evaluating expressions. */
12046 --cp_unevaluated_operand;
12047 --c_inhibit_evaluation_warnings;
12048
12049 /* The `>' token might be the end of a template-id or
12050 template-parameter-list now. */
12051 parser->greater_than_is_operator_p
12052 = saved_greater_than_is_operator_p;
12053
12054 /* Restore the old message and the integral constant expression
12055 flags. */
12056 parser->type_definition_forbidden_message = saved_message;
12057 parser->integral_constant_expression_p
12058 = saved_integral_constant_expression_p;
12059 parser->non_integral_constant_expression_p
12060 = saved_non_integral_constant_expression_p;
12061
12062 /* Parse to the closing `)'. */
12063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12064 {
12065 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12066 /*consume_paren=*/true);
12067 return error_mark_node;
12068 }
12069
12070 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12071 tf_warning_or_error);
12072
12073 rewrite:
12074 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12075 it again. */
12076 start_token->type = CPP_DECLTYPE;
12077 start_token->u.value = expr;
12078 start_token->keyword = RID_MAX;
12079 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12080
12081 return expr;
12082 }
12083
12084 /* Special member functions [gram.special] */
12085
12086 /* Parse a conversion-function-id.
12087
12088 conversion-function-id:
12089 operator conversion-type-id
12090
12091 Returns an IDENTIFIER_NODE representing the operator. */
12092
12093 static tree
12094 cp_parser_conversion_function_id (cp_parser* parser)
12095 {
12096 tree type;
12097 tree saved_scope;
12098 tree saved_qualifying_scope;
12099 tree saved_object_scope;
12100 tree pushed_scope = NULL_TREE;
12101
12102 /* Look for the `operator' token. */
12103 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12104 return error_mark_node;
12105 /* When we parse the conversion-type-id, the current scope will be
12106 reset. However, we need that information in able to look up the
12107 conversion function later, so we save it here. */
12108 saved_scope = parser->scope;
12109 saved_qualifying_scope = parser->qualifying_scope;
12110 saved_object_scope = parser->object_scope;
12111 /* We must enter the scope of the class so that the names of
12112 entities declared within the class are available in the
12113 conversion-type-id. For example, consider:
12114
12115 struct S {
12116 typedef int I;
12117 operator I();
12118 };
12119
12120 S::operator I() { ... }
12121
12122 In order to see that `I' is a type-name in the definition, we
12123 must be in the scope of `S'. */
12124 if (saved_scope)
12125 pushed_scope = push_scope (saved_scope);
12126 /* Parse the conversion-type-id. */
12127 type = cp_parser_conversion_type_id (parser);
12128 /* Leave the scope of the class, if any. */
12129 if (pushed_scope)
12130 pop_scope (pushed_scope);
12131 /* Restore the saved scope. */
12132 parser->scope = saved_scope;
12133 parser->qualifying_scope = saved_qualifying_scope;
12134 parser->object_scope = saved_object_scope;
12135 /* If the TYPE is invalid, indicate failure. */
12136 if (type == error_mark_node)
12137 return error_mark_node;
12138 return mangle_conv_op_name_for_type (type);
12139 }
12140
12141 /* Parse a conversion-type-id:
12142
12143 conversion-type-id:
12144 type-specifier-seq conversion-declarator [opt]
12145
12146 Returns the TYPE specified. */
12147
12148 static tree
12149 cp_parser_conversion_type_id (cp_parser* parser)
12150 {
12151 tree attributes;
12152 cp_decl_specifier_seq type_specifiers;
12153 cp_declarator *declarator;
12154 tree type_specified;
12155 const char *saved_message;
12156
12157 /* Parse the attributes. */
12158 attributes = cp_parser_attributes_opt (parser);
12159
12160 saved_message = parser->type_definition_forbidden_message;
12161 parser->type_definition_forbidden_message
12162 = G_("types may not be defined in a conversion-type-id");
12163
12164 /* Parse the type-specifiers. */
12165 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12166 /*is_trailing_return=*/false,
12167 &type_specifiers);
12168
12169 parser->type_definition_forbidden_message = saved_message;
12170
12171 /* If that didn't work, stop. */
12172 if (type_specifiers.type == error_mark_node)
12173 return error_mark_node;
12174 /* Parse the conversion-declarator. */
12175 declarator = cp_parser_conversion_declarator_opt (parser);
12176
12177 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12178 /*initialized=*/0, &attributes);
12179 if (attributes)
12180 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12181
12182 /* Don't give this error when parsing tentatively. This happens to
12183 work because we always parse this definitively once. */
12184 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12185 && type_uses_auto (type_specified))
12186 {
12187 if (cxx_dialect < cxx1y)
12188 {
12189 error ("invalid use of %<auto%> in conversion operator");
12190 return error_mark_node;
12191 }
12192 else if (template_parm_scope_p ())
12193 warning (0, "use of %<auto%> in member template "
12194 "conversion operator can never be deduced");
12195 }
12196
12197 return type_specified;
12198 }
12199
12200 /* Parse an (optional) conversion-declarator.
12201
12202 conversion-declarator:
12203 ptr-operator conversion-declarator [opt]
12204
12205 */
12206
12207 static cp_declarator *
12208 cp_parser_conversion_declarator_opt (cp_parser* parser)
12209 {
12210 enum tree_code code;
12211 tree class_type, std_attributes = NULL_TREE;
12212 cp_cv_quals cv_quals;
12213
12214 /* We don't know if there's a ptr-operator next, or not. */
12215 cp_parser_parse_tentatively (parser);
12216 /* Try the ptr-operator. */
12217 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12218 &std_attributes);
12219 /* If it worked, look for more conversion-declarators. */
12220 if (cp_parser_parse_definitely (parser))
12221 {
12222 cp_declarator *declarator;
12223
12224 /* Parse another optional declarator. */
12225 declarator = cp_parser_conversion_declarator_opt (parser);
12226
12227 declarator = cp_parser_make_indirect_declarator
12228 (code, class_type, cv_quals, declarator, std_attributes);
12229
12230 return declarator;
12231 }
12232
12233 return NULL;
12234 }
12235
12236 /* Parse an (optional) ctor-initializer.
12237
12238 ctor-initializer:
12239 : mem-initializer-list
12240
12241 Returns TRUE iff the ctor-initializer was actually present. */
12242
12243 static bool
12244 cp_parser_ctor_initializer_opt (cp_parser* parser)
12245 {
12246 /* If the next token is not a `:', then there is no
12247 ctor-initializer. */
12248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12249 {
12250 /* Do default initialization of any bases and members. */
12251 if (DECL_CONSTRUCTOR_P (current_function_decl))
12252 finish_mem_initializers (NULL_TREE);
12253
12254 return false;
12255 }
12256
12257 /* Consume the `:' token. */
12258 cp_lexer_consume_token (parser->lexer);
12259 /* And the mem-initializer-list. */
12260 cp_parser_mem_initializer_list (parser);
12261
12262 return true;
12263 }
12264
12265 /* Parse a mem-initializer-list.
12266
12267 mem-initializer-list:
12268 mem-initializer ... [opt]
12269 mem-initializer ... [opt] , mem-initializer-list */
12270
12271 static void
12272 cp_parser_mem_initializer_list (cp_parser* parser)
12273 {
12274 tree mem_initializer_list = NULL_TREE;
12275 tree target_ctor = error_mark_node;
12276 cp_token *token = cp_lexer_peek_token (parser->lexer);
12277
12278 /* Let the semantic analysis code know that we are starting the
12279 mem-initializer-list. */
12280 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12281 error_at (token->location,
12282 "only constructors take member initializers");
12283
12284 /* Loop through the list. */
12285 while (true)
12286 {
12287 tree mem_initializer;
12288
12289 token = cp_lexer_peek_token (parser->lexer);
12290 /* Parse the mem-initializer. */
12291 mem_initializer = cp_parser_mem_initializer (parser);
12292 /* If the next token is a `...', we're expanding member initializers. */
12293 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12294 {
12295 /* Consume the `...'. */
12296 cp_lexer_consume_token (parser->lexer);
12297
12298 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12299 can be expanded but members cannot. */
12300 if (mem_initializer != error_mark_node
12301 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12302 {
12303 error_at (token->location,
12304 "cannot expand initializer for member %<%D%>",
12305 TREE_PURPOSE (mem_initializer));
12306 mem_initializer = error_mark_node;
12307 }
12308
12309 /* Construct the pack expansion type. */
12310 if (mem_initializer != error_mark_node)
12311 mem_initializer = make_pack_expansion (mem_initializer);
12312 }
12313 if (target_ctor != error_mark_node
12314 && mem_initializer != error_mark_node)
12315 {
12316 error ("mem-initializer for %qD follows constructor delegation",
12317 TREE_PURPOSE (mem_initializer));
12318 mem_initializer = error_mark_node;
12319 }
12320 /* Look for a target constructor. */
12321 if (mem_initializer != error_mark_node
12322 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12323 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12324 {
12325 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12326 if (mem_initializer_list)
12327 {
12328 error ("constructor delegation follows mem-initializer for %qD",
12329 TREE_PURPOSE (mem_initializer_list));
12330 mem_initializer = error_mark_node;
12331 }
12332 target_ctor = mem_initializer;
12333 }
12334 /* Add it to the list, unless it was erroneous. */
12335 if (mem_initializer != error_mark_node)
12336 {
12337 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12338 mem_initializer_list = mem_initializer;
12339 }
12340 /* If the next token is not a `,', we're done. */
12341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12342 break;
12343 /* Consume the `,' token. */
12344 cp_lexer_consume_token (parser->lexer);
12345 }
12346
12347 /* Perform semantic analysis. */
12348 if (DECL_CONSTRUCTOR_P (current_function_decl))
12349 finish_mem_initializers (mem_initializer_list);
12350 }
12351
12352 /* Parse a mem-initializer.
12353
12354 mem-initializer:
12355 mem-initializer-id ( expression-list [opt] )
12356 mem-initializer-id braced-init-list
12357
12358 GNU extension:
12359
12360 mem-initializer:
12361 ( expression-list [opt] )
12362
12363 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12364 class) or FIELD_DECL (for a non-static data member) to initialize;
12365 the TREE_VALUE is the expression-list. An empty initialization
12366 list is represented by void_list_node. */
12367
12368 static tree
12369 cp_parser_mem_initializer (cp_parser* parser)
12370 {
12371 tree mem_initializer_id;
12372 tree expression_list;
12373 tree member;
12374 cp_token *token = cp_lexer_peek_token (parser->lexer);
12375
12376 /* Find out what is being initialized. */
12377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12378 {
12379 permerror (token->location,
12380 "anachronistic old-style base class initializer");
12381 mem_initializer_id = NULL_TREE;
12382 }
12383 else
12384 {
12385 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12386 if (mem_initializer_id == error_mark_node)
12387 return mem_initializer_id;
12388 }
12389 member = expand_member_init (mem_initializer_id);
12390 if (member && !DECL_P (member))
12391 in_base_initializer = 1;
12392
12393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12394 {
12395 bool expr_non_constant_p;
12396 cp_lexer_set_source_position (parser->lexer);
12397 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12398 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12399 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12400 expression_list = build_tree_list (NULL_TREE, expression_list);
12401 }
12402 else
12403 {
12404 vec<tree, va_gc> *vec;
12405 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12406 /*cast_p=*/false,
12407 /*allow_expansion_p=*/true,
12408 /*non_constant_p=*/NULL);
12409 if (vec == NULL)
12410 return error_mark_node;
12411 expression_list = build_tree_list_vec (vec);
12412 release_tree_vector (vec);
12413 }
12414
12415 if (expression_list == error_mark_node)
12416 return error_mark_node;
12417 if (!expression_list)
12418 expression_list = void_type_node;
12419
12420 in_base_initializer = 0;
12421
12422 return member ? build_tree_list (member, expression_list) : error_mark_node;
12423 }
12424
12425 /* Parse a mem-initializer-id.
12426
12427 mem-initializer-id:
12428 :: [opt] nested-name-specifier [opt] class-name
12429 identifier
12430
12431 Returns a TYPE indicating the class to be initializer for the first
12432 production. Returns an IDENTIFIER_NODE indicating the data member
12433 to be initialized for the second production. */
12434
12435 static tree
12436 cp_parser_mem_initializer_id (cp_parser* parser)
12437 {
12438 bool global_scope_p;
12439 bool nested_name_specifier_p;
12440 bool template_p = false;
12441 tree id;
12442
12443 cp_token *token = cp_lexer_peek_token (parser->lexer);
12444
12445 /* `typename' is not allowed in this context ([temp.res]). */
12446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12447 {
12448 error_at (token->location,
12449 "keyword %<typename%> not allowed in this context (a qualified "
12450 "member initializer is implicitly a type)");
12451 cp_lexer_consume_token (parser->lexer);
12452 }
12453 /* Look for the optional `::' operator. */
12454 global_scope_p
12455 = (cp_parser_global_scope_opt (parser,
12456 /*current_scope_valid_p=*/false)
12457 != NULL_TREE);
12458 /* Look for the optional nested-name-specifier. The simplest way to
12459 implement:
12460
12461 [temp.res]
12462
12463 The keyword `typename' is not permitted in a base-specifier or
12464 mem-initializer; in these contexts a qualified name that
12465 depends on a template-parameter is implicitly assumed to be a
12466 type name.
12467
12468 is to assume that we have seen the `typename' keyword at this
12469 point. */
12470 nested_name_specifier_p
12471 = (cp_parser_nested_name_specifier_opt (parser,
12472 /*typename_keyword_p=*/true,
12473 /*check_dependency_p=*/true,
12474 /*type_p=*/true,
12475 /*is_declaration=*/true)
12476 != NULL_TREE);
12477 if (nested_name_specifier_p)
12478 template_p = cp_parser_optional_template_keyword (parser);
12479 /* If there is a `::' operator or a nested-name-specifier, then we
12480 are definitely looking for a class-name. */
12481 if (global_scope_p || nested_name_specifier_p)
12482 return cp_parser_class_name (parser,
12483 /*typename_keyword_p=*/true,
12484 /*template_keyword_p=*/template_p,
12485 typename_type,
12486 /*check_dependency_p=*/true,
12487 /*class_head_p=*/false,
12488 /*is_declaration=*/true);
12489 /* Otherwise, we could also be looking for an ordinary identifier. */
12490 cp_parser_parse_tentatively (parser);
12491 /* Try a class-name. */
12492 id = cp_parser_class_name (parser,
12493 /*typename_keyword_p=*/true,
12494 /*template_keyword_p=*/false,
12495 none_type,
12496 /*check_dependency_p=*/true,
12497 /*class_head_p=*/false,
12498 /*is_declaration=*/true);
12499 /* If we found one, we're done. */
12500 if (cp_parser_parse_definitely (parser))
12501 return id;
12502 /* Otherwise, look for an ordinary identifier. */
12503 return cp_parser_identifier (parser);
12504 }
12505
12506 /* Overloading [gram.over] */
12507
12508 /* Parse an operator-function-id.
12509
12510 operator-function-id:
12511 operator operator
12512
12513 Returns an IDENTIFIER_NODE for the operator which is a
12514 human-readable spelling of the identifier, e.g., `operator +'. */
12515
12516 static tree
12517 cp_parser_operator_function_id (cp_parser* parser)
12518 {
12519 /* Look for the `operator' keyword. */
12520 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12521 return error_mark_node;
12522 /* And then the name of the operator itself. */
12523 return cp_parser_operator (parser);
12524 }
12525
12526 /* Return an identifier node for a user-defined literal operator.
12527 The suffix identifier is chained to the operator name identifier. */
12528
12529 static tree
12530 cp_literal_operator_id (const char* name)
12531 {
12532 tree identifier;
12533 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12534 + strlen (name) + 10);
12535 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12536 identifier = get_identifier (buffer);
12537
12538 return identifier;
12539 }
12540
12541 /* Parse an operator.
12542
12543 operator:
12544 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12545 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12546 || ++ -- , ->* -> () []
12547
12548 GNU Extensions:
12549
12550 operator:
12551 <? >? <?= >?=
12552
12553 Returns an IDENTIFIER_NODE for the operator which is a
12554 human-readable spelling of the identifier, e.g., `operator +'. */
12555
12556 static tree
12557 cp_parser_operator (cp_parser* parser)
12558 {
12559 tree id = NULL_TREE;
12560 cp_token *token;
12561 bool bad_encoding_prefix = false;
12562
12563 /* Peek at the next token. */
12564 token = cp_lexer_peek_token (parser->lexer);
12565 /* Figure out which operator we have. */
12566 switch (token->type)
12567 {
12568 case CPP_KEYWORD:
12569 {
12570 enum tree_code op;
12571
12572 /* The keyword should be either `new' or `delete'. */
12573 if (token->keyword == RID_NEW)
12574 op = NEW_EXPR;
12575 else if (token->keyword == RID_DELETE)
12576 op = DELETE_EXPR;
12577 else
12578 break;
12579
12580 /* Consume the `new' or `delete' token. */
12581 cp_lexer_consume_token (parser->lexer);
12582
12583 /* Peek at the next token. */
12584 token = cp_lexer_peek_token (parser->lexer);
12585 /* If it's a `[' token then this is the array variant of the
12586 operator. */
12587 if (token->type == CPP_OPEN_SQUARE)
12588 {
12589 /* Consume the `[' token. */
12590 cp_lexer_consume_token (parser->lexer);
12591 /* Look for the `]' token. */
12592 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12593 id = ansi_opname (op == NEW_EXPR
12594 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12595 }
12596 /* Otherwise, we have the non-array variant. */
12597 else
12598 id = ansi_opname (op);
12599
12600 return id;
12601 }
12602
12603 case CPP_PLUS:
12604 id = ansi_opname (PLUS_EXPR);
12605 break;
12606
12607 case CPP_MINUS:
12608 id = ansi_opname (MINUS_EXPR);
12609 break;
12610
12611 case CPP_MULT:
12612 id = ansi_opname (MULT_EXPR);
12613 break;
12614
12615 case CPP_DIV:
12616 id = ansi_opname (TRUNC_DIV_EXPR);
12617 break;
12618
12619 case CPP_MOD:
12620 id = ansi_opname (TRUNC_MOD_EXPR);
12621 break;
12622
12623 case CPP_XOR:
12624 id = ansi_opname (BIT_XOR_EXPR);
12625 break;
12626
12627 case CPP_AND:
12628 id = ansi_opname (BIT_AND_EXPR);
12629 break;
12630
12631 case CPP_OR:
12632 id = ansi_opname (BIT_IOR_EXPR);
12633 break;
12634
12635 case CPP_COMPL:
12636 id = ansi_opname (BIT_NOT_EXPR);
12637 break;
12638
12639 case CPP_NOT:
12640 id = ansi_opname (TRUTH_NOT_EXPR);
12641 break;
12642
12643 case CPP_EQ:
12644 id = ansi_assopname (NOP_EXPR);
12645 break;
12646
12647 case CPP_LESS:
12648 id = ansi_opname (LT_EXPR);
12649 break;
12650
12651 case CPP_GREATER:
12652 id = ansi_opname (GT_EXPR);
12653 break;
12654
12655 case CPP_PLUS_EQ:
12656 id = ansi_assopname (PLUS_EXPR);
12657 break;
12658
12659 case CPP_MINUS_EQ:
12660 id = ansi_assopname (MINUS_EXPR);
12661 break;
12662
12663 case CPP_MULT_EQ:
12664 id = ansi_assopname (MULT_EXPR);
12665 break;
12666
12667 case CPP_DIV_EQ:
12668 id = ansi_assopname (TRUNC_DIV_EXPR);
12669 break;
12670
12671 case CPP_MOD_EQ:
12672 id = ansi_assopname (TRUNC_MOD_EXPR);
12673 break;
12674
12675 case CPP_XOR_EQ:
12676 id = ansi_assopname (BIT_XOR_EXPR);
12677 break;
12678
12679 case CPP_AND_EQ:
12680 id = ansi_assopname (BIT_AND_EXPR);
12681 break;
12682
12683 case CPP_OR_EQ:
12684 id = ansi_assopname (BIT_IOR_EXPR);
12685 break;
12686
12687 case CPP_LSHIFT:
12688 id = ansi_opname (LSHIFT_EXPR);
12689 break;
12690
12691 case CPP_RSHIFT:
12692 id = ansi_opname (RSHIFT_EXPR);
12693 break;
12694
12695 case CPP_LSHIFT_EQ:
12696 id = ansi_assopname (LSHIFT_EXPR);
12697 break;
12698
12699 case CPP_RSHIFT_EQ:
12700 id = ansi_assopname (RSHIFT_EXPR);
12701 break;
12702
12703 case CPP_EQ_EQ:
12704 id = ansi_opname (EQ_EXPR);
12705 break;
12706
12707 case CPP_NOT_EQ:
12708 id = ansi_opname (NE_EXPR);
12709 break;
12710
12711 case CPP_LESS_EQ:
12712 id = ansi_opname (LE_EXPR);
12713 break;
12714
12715 case CPP_GREATER_EQ:
12716 id = ansi_opname (GE_EXPR);
12717 break;
12718
12719 case CPP_AND_AND:
12720 id = ansi_opname (TRUTH_ANDIF_EXPR);
12721 break;
12722
12723 case CPP_OR_OR:
12724 id = ansi_opname (TRUTH_ORIF_EXPR);
12725 break;
12726
12727 case CPP_PLUS_PLUS:
12728 id = ansi_opname (POSTINCREMENT_EXPR);
12729 break;
12730
12731 case CPP_MINUS_MINUS:
12732 id = ansi_opname (PREDECREMENT_EXPR);
12733 break;
12734
12735 case CPP_COMMA:
12736 id = ansi_opname (COMPOUND_EXPR);
12737 break;
12738
12739 case CPP_DEREF_STAR:
12740 id = ansi_opname (MEMBER_REF);
12741 break;
12742
12743 case CPP_DEREF:
12744 id = ansi_opname (COMPONENT_REF);
12745 break;
12746
12747 case CPP_OPEN_PAREN:
12748 /* Consume the `('. */
12749 cp_lexer_consume_token (parser->lexer);
12750 /* Look for the matching `)'. */
12751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12752 return ansi_opname (CALL_EXPR);
12753
12754 case CPP_OPEN_SQUARE:
12755 /* Consume the `['. */
12756 cp_lexer_consume_token (parser->lexer);
12757 /* Look for the matching `]'. */
12758 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12759 return ansi_opname (ARRAY_REF);
12760
12761 case CPP_WSTRING:
12762 case CPP_STRING16:
12763 case CPP_STRING32:
12764 case CPP_UTF8STRING:
12765 bad_encoding_prefix = true;
12766 /* Fall through. */
12767
12768 case CPP_STRING:
12769 if (cxx_dialect == cxx98)
12770 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12771 if (bad_encoding_prefix)
12772 {
12773 error ("invalid encoding prefix in literal operator");
12774 return error_mark_node;
12775 }
12776 if (TREE_STRING_LENGTH (token->u.value) > 2)
12777 {
12778 error ("expected empty string after %<operator%> keyword");
12779 return error_mark_node;
12780 }
12781 /* Consume the string. */
12782 cp_lexer_consume_token (parser->lexer);
12783 /* Look for the suffix identifier. */
12784 token = cp_lexer_peek_token (parser->lexer);
12785 if (token->type == CPP_NAME)
12786 {
12787 id = cp_parser_identifier (parser);
12788 if (id != error_mark_node)
12789 {
12790 const char *name = IDENTIFIER_POINTER (id);
12791 return cp_literal_operator_id (name);
12792 }
12793 }
12794 else if (token->type == CPP_KEYWORD)
12795 {
12796 error ("unexpected keyword;"
12797 " remove space between quotes and suffix identifier");
12798 return error_mark_node;
12799 }
12800 else
12801 {
12802 error ("expected suffix identifier");
12803 return error_mark_node;
12804 }
12805
12806 case CPP_WSTRING_USERDEF:
12807 case CPP_STRING16_USERDEF:
12808 case CPP_STRING32_USERDEF:
12809 case CPP_UTF8STRING_USERDEF:
12810 bad_encoding_prefix = true;
12811 /* Fall through. */
12812
12813 case CPP_STRING_USERDEF:
12814 if (cxx_dialect == cxx98)
12815 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12816 if (bad_encoding_prefix)
12817 {
12818 error ("invalid encoding prefix in literal operator");
12819 return error_mark_node;
12820 }
12821 {
12822 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12823 if (TREE_STRING_LENGTH (string_tree) > 2)
12824 {
12825 error ("expected empty string after %<operator%> keyword");
12826 return error_mark_node;
12827 }
12828 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12829 /* Consume the user-defined string literal. */
12830 cp_lexer_consume_token (parser->lexer);
12831 if (id != error_mark_node)
12832 {
12833 const char *name = IDENTIFIER_POINTER (id);
12834 return cp_literal_operator_id (name);
12835 }
12836 else
12837 return error_mark_node;
12838 }
12839
12840 default:
12841 /* Anything else is an error. */
12842 break;
12843 }
12844
12845 /* If we have selected an identifier, we need to consume the
12846 operator token. */
12847 if (id)
12848 cp_lexer_consume_token (parser->lexer);
12849 /* Otherwise, no valid operator name was present. */
12850 else
12851 {
12852 cp_parser_error (parser, "expected operator");
12853 id = error_mark_node;
12854 }
12855
12856 return id;
12857 }
12858
12859 /* Parse a template-declaration.
12860
12861 template-declaration:
12862 export [opt] template < template-parameter-list > declaration
12863
12864 If MEMBER_P is TRUE, this template-declaration occurs within a
12865 class-specifier.
12866
12867 The grammar rule given by the standard isn't correct. What
12868 is really meant is:
12869
12870 template-declaration:
12871 export [opt] template-parameter-list-seq
12872 decl-specifier-seq [opt] init-declarator [opt] ;
12873 export [opt] template-parameter-list-seq
12874 function-definition
12875
12876 template-parameter-list-seq:
12877 template-parameter-list-seq [opt]
12878 template < template-parameter-list > */
12879
12880 static void
12881 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12882 {
12883 /* Check for `export'. */
12884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12885 {
12886 /* Consume the `export' token. */
12887 cp_lexer_consume_token (parser->lexer);
12888 /* Warn that we do not support `export'. */
12889 warning (0, "keyword %<export%> not implemented, and will be ignored");
12890 }
12891
12892 cp_parser_template_declaration_after_export (parser, member_p);
12893 }
12894
12895 /* Parse a template-parameter-list.
12896
12897 template-parameter-list:
12898 template-parameter
12899 template-parameter-list , template-parameter
12900
12901 Returns a TREE_LIST. Each node represents a template parameter.
12902 The nodes are connected via their TREE_CHAINs. */
12903
12904 static tree
12905 cp_parser_template_parameter_list (cp_parser* parser)
12906 {
12907 tree parameter_list = NULL_TREE;
12908
12909 begin_template_parm_list ();
12910
12911 /* The loop below parses the template parms. We first need to know
12912 the total number of template parms to be able to compute proper
12913 canonical types of each dependent type. So after the loop, when
12914 we know the total number of template parms,
12915 end_template_parm_list computes the proper canonical types and
12916 fixes up the dependent types accordingly. */
12917 while (true)
12918 {
12919 tree parameter;
12920 bool is_non_type;
12921 bool is_parameter_pack;
12922 location_t parm_loc;
12923
12924 /* Parse the template-parameter. */
12925 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12926 parameter = cp_parser_template_parameter (parser,
12927 &is_non_type,
12928 &is_parameter_pack);
12929 /* Add it to the list. */
12930 if (parameter != error_mark_node)
12931 parameter_list = process_template_parm (parameter_list,
12932 parm_loc,
12933 parameter,
12934 is_non_type,
12935 is_parameter_pack);
12936 else
12937 {
12938 tree err_parm = build_tree_list (parameter, parameter);
12939 parameter_list = chainon (parameter_list, err_parm);
12940 }
12941
12942 /* If the next token is not a `,', we're done. */
12943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12944 break;
12945 /* Otherwise, consume the `,' token. */
12946 cp_lexer_consume_token (parser->lexer);
12947 }
12948
12949 return end_template_parm_list (parameter_list);
12950 }
12951
12952 /* Parse a template-parameter.
12953
12954 template-parameter:
12955 type-parameter
12956 parameter-declaration
12957
12958 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12959 the parameter. The TREE_PURPOSE is the default value, if any.
12960 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12961 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12962 set to true iff this parameter is a parameter pack. */
12963
12964 static tree
12965 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12966 bool *is_parameter_pack)
12967 {
12968 cp_token *token;
12969 cp_parameter_declarator *parameter_declarator;
12970 cp_declarator *id_declarator;
12971 tree parm;
12972
12973 /* Assume it is a type parameter or a template parameter. */
12974 *is_non_type = false;
12975 /* Assume it not a parameter pack. */
12976 *is_parameter_pack = false;
12977 /* Peek at the next token. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 /* If it is `class' or `template', we have a type-parameter. */
12980 if (token->keyword == RID_TEMPLATE)
12981 return cp_parser_type_parameter (parser, is_parameter_pack);
12982 /* If it is `class' or `typename' we do not know yet whether it is a
12983 type parameter or a non-type parameter. Consider:
12984
12985 template <typename T, typename T::X X> ...
12986
12987 or:
12988
12989 template <class C, class D*> ...
12990
12991 Here, the first parameter is a type parameter, and the second is
12992 a non-type parameter. We can tell by looking at the token after
12993 the identifier -- if it is a `,', `=', or `>' then we have a type
12994 parameter. */
12995 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12996 {
12997 /* Peek at the token after `class' or `typename'. */
12998 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12999 /* If it's an ellipsis, we have a template type parameter
13000 pack. */
13001 if (token->type == CPP_ELLIPSIS)
13002 return cp_parser_type_parameter (parser, is_parameter_pack);
13003 /* If it's an identifier, skip it. */
13004 if (token->type == CPP_NAME)
13005 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13006 /* Now, see if the token looks like the end of a template
13007 parameter. */
13008 if (token->type == CPP_COMMA
13009 || token->type == CPP_EQ
13010 || token->type == CPP_GREATER)
13011 return cp_parser_type_parameter (parser, is_parameter_pack);
13012 }
13013
13014 /* Otherwise, it is a non-type parameter.
13015
13016 [temp.param]
13017
13018 When parsing a default template-argument for a non-type
13019 template-parameter, the first non-nested `>' is taken as the end
13020 of the template parameter-list rather than a greater-than
13021 operator. */
13022 *is_non_type = true;
13023 parameter_declarator
13024 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13025 /*parenthesized_p=*/NULL);
13026
13027 if (!parameter_declarator)
13028 return error_mark_node;
13029
13030 /* If the parameter declaration is marked as a parameter pack, set
13031 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13032 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13033 grokdeclarator. */
13034 if (parameter_declarator->declarator
13035 && parameter_declarator->declarator->parameter_pack_p)
13036 {
13037 *is_parameter_pack = true;
13038 parameter_declarator->declarator->parameter_pack_p = false;
13039 }
13040
13041 if (parameter_declarator->default_argument)
13042 {
13043 /* Can happen in some cases of erroneous input (c++/34892). */
13044 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13045 /* Consume the `...' for better error recovery. */
13046 cp_lexer_consume_token (parser->lexer);
13047 }
13048 /* If the next token is an ellipsis, and we don't already have it
13049 marked as a parameter pack, then we have a parameter pack (that
13050 has no declarator). */
13051 else if (!*is_parameter_pack
13052 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13053 && (declarator_can_be_parameter_pack
13054 (parameter_declarator->declarator)))
13055 {
13056 /* Consume the `...'. */
13057 cp_lexer_consume_token (parser->lexer);
13058 maybe_warn_variadic_templates ();
13059
13060 *is_parameter_pack = true;
13061 }
13062 /* We might end up with a pack expansion as the type of the non-type
13063 template parameter, in which case this is a non-type template
13064 parameter pack. */
13065 else if (parameter_declarator->decl_specifiers.type
13066 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13067 {
13068 *is_parameter_pack = true;
13069 parameter_declarator->decl_specifiers.type =
13070 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13071 }
13072
13073 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13074 {
13075 /* Parameter packs cannot have default arguments. However, a
13076 user may try to do so, so we'll parse them and give an
13077 appropriate diagnostic here. */
13078
13079 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13080
13081 /* Find the name of the parameter pack. */
13082 id_declarator = parameter_declarator->declarator;
13083 while (id_declarator && id_declarator->kind != cdk_id)
13084 id_declarator = id_declarator->declarator;
13085
13086 if (id_declarator && id_declarator->kind == cdk_id)
13087 error_at (start_token->location,
13088 "template parameter pack %qD cannot have a default argument",
13089 id_declarator->u.id.unqualified_name);
13090 else
13091 error_at (start_token->location,
13092 "template parameter pack cannot have a default argument");
13093
13094 /* Parse the default argument, but throw away the result. */
13095 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13096 }
13097
13098 parm = grokdeclarator (parameter_declarator->declarator,
13099 &parameter_declarator->decl_specifiers,
13100 TPARM, /*initialized=*/0,
13101 /*attrlist=*/NULL);
13102 if (parm == error_mark_node)
13103 return error_mark_node;
13104
13105 return build_tree_list (parameter_declarator->default_argument, parm);
13106 }
13107
13108 /* Parse a type-parameter.
13109
13110 type-parameter:
13111 class identifier [opt]
13112 class identifier [opt] = type-id
13113 typename identifier [opt]
13114 typename identifier [opt] = type-id
13115 template < template-parameter-list > class identifier [opt]
13116 template < template-parameter-list > class identifier [opt]
13117 = id-expression
13118
13119 GNU Extension (variadic templates):
13120
13121 type-parameter:
13122 class ... identifier [opt]
13123 typename ... identifier [opt]
13124
13125 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13126 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13127 the declaration of the parameter.
13128
13129 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13130
13131 static tree
13132 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13133 {
13134 cp_token *token;
13135 tree parameter;
13136
13137 /* Look for a keyword to tell us what kind of parameter this is. */
13138 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13139 if (!token)
13140 return error_mark_node;
13141
13142 switch (token->keyword)
13143 {
13144 case RID_CLASS:
13145 case RID_TYPENAME:
13146 {
13147 tree identifier;
13148 tree default_argument;
13149
13150 /* If the next token is an ellipsis, we have a template
13151 argument pack. */
13152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13153 {
13154 /* Consume the `...' token. */
13155 cp_lexer_consume_token (parser->lexer);
13156 maybe_warn_variadic_templates ();
13157
13158 *is_parameter_pack = true;
13159 }
13160
13161 /* If the next token is an identifier, then it names the
13162 parameter. */
13163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13164 identifier = cp_parser_identifier (parser);
13165 else
13166 identifier = NULL_TREE;
13167
13168 /* Create the parameter. */
13169 parameter = finish_template_type_parm (class_type_node, identifier);
13170
13171 /* If the next token is an `=', we have a default argument. */
13172 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13173 {
13174 /* Consume the `=' token. */
13175 cp_lexer_consume_token (parser->lexer);
13176 /* Parse the default-argument. */
13177 push_deferring_access_checks (dk_no_deferred);
13178 default_argument = cp_parser_type_id (parser);
13179
13180 /* Template parameter packs cannot have default
13181 arguments. */
13182 if (*is_parameter_pack)
13183 {
13184 if (identifier)
13185 error_at (token->location,
13186 "template parameter pack %qD cannot have a "
13187 "default argument", identifier);
13188 else
13189 error_at (token->location,
13190 "template parameter packs cannot have "
13191 "default arguments");
13192 default_argument = NULL_TREE;
13193 }
13194 pop_deferring_access_checks ();
13195 }
13196 else
13197 default_argument = NULL_TREE;
13198
13199 /* Create the combined representation of the parameter and the
13200 default argument. */
13201 parameter = build_tree_list (default_argument, parameter);
13202 }
13203 break;
13204
13205 case RID_TEMPLATE:
13206 {
13207 tree identifier;
13208 tree default_argument;
13209
13210 /* Look for the `<'. */
13211 cp_parser_require (parser, CPP_LESS, RT_LESS);
13212 /* Parse the template-parameter-list. */
13213 cp_parser_template_parameter_list (parser);
13214 /* Look for the `>'. */
13215 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13216 /* Look for the `class' keyword. */
13217 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13218 /* If the next token is an ellipsis, we have a template
13219 argument pack. */
13220 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13221 {
13222 /* Consume the `...' token. */
13223 cp_lexer_consume_token (parser->lexer);
13224 maybe_warn_variadic_templates ();
13225
13226 *is_parameter_pack = true;
13227 }
13228 /* If the next token is an `=', then there is a
13229 default-argument. If the next token is a `>', we are at
13230 the end of the parameter-list. If the next token is a `,',
13231 then we are at the end of this parameter. */
13232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13233 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13234 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13235 {
13236 identifier = cp_parser_identifier (parser);
13237 /* Treat invalid names as if the parameter were nameless. */
13238 if (identifier == error_mark_node)
13239 identifier = NULL_TREE;
13240 }
13241 else
13242 identifier = NULL_TREE;
13243
13244 /* Create the template parameter. */
13245 parameter = finish_template_template_parm (class_type_node,
13246 identifier);
13247
13248 /* If the next token is an `=', then there is a
13249 default-argument. */
13250 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13251 {
13252 bool is_template;
13253
13254 /* Consume the `='. */
13255 cp_lexer_consume_token (parser->lexer);
13256 /* Parse the id-expression. */
13257 push_deferring_access_checks (dk_no_deferred);
13258 /* save token before parsing the id-expression, for error
13259 reporting */
13260 token = cp_lexer_peek_token (parser->lexer);
13261 default_argument
13262 = cp_parser_id_expression (parser,
13263 /*template_keyword_p=*/false,
13264 /*check_dependency_p=*/true,
13265 /*template_p=*/&is_template,
13266 /*declarator_p=*/false,
13267 /*optional_p=*/false);
13268 if (TREE_CODE (default_argument) == TYPE_DECL)
13269 /* If the id-expression was a template-id that refers to
13270 a template-class, we already have the declaration here,
13271 so no further lookup is needed. */
13272 ;
13273 else
13274 /* Look up the name. */
13275 default_argument
13276 = cp_parser_lookup_name (parser, default_argument,
13277 none_type,
13278 /*is_template=*/is_template,
13279 /*is_namespace=*/false,
13280 /*check_dependency=*/true,
13281 /*ambiguous_decls=*/NULL,
13282 token->location);
13283 /* See if the default argument is valid. */
13284 default_argument
13285 = check_template_template_default_arg (default_argument);
13286
13287 /* Template parameter packs cannot have default
13288 arguments. */
13289 if (*is_parameter_pack)
13290 {
13291 if (identifier)
13292 error_at (token->location,
13293 "template parameter pack %qD cannot "
13294 "have a default argument",
13295 identifier);
13296 else
13297 error_at (token->location, "template parameter packs cannot "
13298 "have default arguments");
13299 default_argument = NULL_TREE;
13300 }
13301 pop_deferring_access_checks ();
13302 }
13303 else
13304 default_argument = NULL_TREE;
13305
13306 /* Create the combined representation of the parameter and the
13307 default argument. */
13308 parameter = build_tree_list (default_argument, parameter);
13309 }
13310 break;
13311
13312 default:
13313 gcc_unreachable ();
13314 break;
13315 }
13316
13317 return parameter;
13318 }
13319
13320 /* Parse a template-id.
13321
13322 template-id:
13323 template-name < template-argument-list [opt] >
13324
13325 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13326 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13327 returned. Otherwise, if the template-name names a function, or set
13328 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13329 names a class, returns a TYPE_DECL for the specialization.
13330
13331 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13332 uninstantiated templates. */
13333
13334 static tree
13335 cp_parser_template_id (cp_parser *parser,
13336 bool template_keyword_p,
13337 bool check_dependency_p,
13338 enum tag_types tag_type,
13339 bool is_declaration)
13340 {
13341 int i;
13342 tree templ;
13343 tree arguments;
13344 tree template_id;
13345 cp_token_position start_of_id = 0;
13346 deferred_access_check *chk;
13347 vec<deferred_access_check, va_gc> *access_check;
13348 cp_token *next_token = NULL, *next_token_2 = NULL;
13349 bool is_identifier;
13350
13351 /* If the next token corresponds to a template-id, there is no need
13352 to reparse it. */
13353 next_token = cp_lexer_peek_token (parser->lexer);
13354 if (next_token->type == CPP_TEMPLATE_ID)
13355 {
13356 struct tree_check *check_value;
13357
13358 /* Get the stored value. */
13359 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13360 /* Perform any access checks that were deferred. */
13361 access_check = check_value->checks;
13362 if (access_check)
13363 {
13364 FOR_EACH_VEC_ELT (*access_check, i, chk)
13365 perform_or_defer_access_check (chk->binfo,
13366 chk->decl,
13367 chk->diag_decl,
13368 tf_warning_or_error);
13369 }
13370 /* Return the stored value. */
13371 return check_value->value;
13372 }
13373
13374 /* Avoid performing name lookup if there is no possibility of
13375 finding a template-id. */
13376 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13377 || (next_token->type == CPP_NAME
13378 && !cp_parser_nth_token_starts_template_argument_list_p
13379 (parser, 2)))
13380 {
13381 cp_parser_error (parser, "expected template-id");
13382 return error_mark_node;
13383 }
13384
13385 /* Remember where the template-id starts. */
13386 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13387 start_of_id = cp_lexer_token_position (parser->lexer, false);
13388
13389 push_deferring_access_checks (dk_deferred);
13390
13391 /* Parse the template-name. */
13392 is_identifier = false;
13393 templ = cp_parser_template_name (parser, template_keyword_p,
13394 check_dependency_p,
13395 is_declaration,
13396 tag_type,
13397 &is_identifier);
13398 if (templ == error_mark_node || is_identifier)
13399 {
13400 pop_deferring_access_checks ();
13401 return templ;
13402 }
13403
13404 /* If we find the sequence `[:' after a template-name, it's probably
13405 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13406 parse correctly the argument list. */
13407 next_token = cp_lexer_peek_token (parser->lexer);
13408 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13409 if (next_token->type == CPP_OPEN_SQUARE
13410 && next_token->flags & DIGRAPH
13411 && next_token_2->type == CPP_COLON
13412 && !(next_token_2->flags & PREV_WHITE))
13413 {
13414 cp_parser_parse_tentatively (parser);
13415 /* Change `:' into `::'. */
13416 next_token_2->type = CPP_SCOPE;
13417 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13418 CPP_LESS. */
13419 cp_lexer_consume_token (parser->lexer);
13420
13421 /* Parse the arguments. */
13422 arguments = cp_parser_enclosed_template_argument_list (parser);
13423 if (!cp_parser_parse_definitely (parser))
13424 {
13425 /* If we couldn't parse an argument list, then we revert our changes
13426 and return simply an error. Maybe this is not a template-id
13427 after all. */
13428 next_token_2->type = CPP_COLON;
13429 cp_parser_error (parser, "expected %<<%>");
13430 pop_deferring_access_checks ();
13431 return error_mark_node;
13432 }
13433 /* Otherwise, emit an error about the invalid digraph, but continue
13434 parsing because we got our argument list. */
13435 if (permerror (next_token->location,
13436 "%<<::%> cannot begin a template-argument list"))
13437 {
13438 static bool hint = false;
13439 inform (next_token->location,
13440 "%<<:%> is an alternate spelling for %<[%>."
13441 " Insert whitespace between %<<%> and %<::%>");
13442 if (!hint && !flag_permissive)
13443 {
13444 inform (next_token->location, "(if you use %<-fpermissive%> "
13445 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13446 "accept your code)");
13447 hint = true;
13448 }
13449 }
13450 }
13451 else
13452 {
13453 /* Look for the `<' that starts the template-argument-list. */
13454 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13455 {
13456 pop_deferring_access_checks ();
13457 return error_mark_node;
13458 }
13459 /* Parse the arguments. */
13460 arguments = cp_parser_enclosed_template_argument_list (parser);
13461 }
13462
13463 /* Build a representation of the specialization. */
13464 if (identifier_p (templ))
13465 template_id = build_min_nt_loc (next_token->location,
13466 TEMPLATE_ID_EXPR,
13467 templ, arguments);
13468 else if (DECL_TYPE_TEMPLATE_P (templ)
13469 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13470 {
13471 bool entering_scope;
13472 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13473 template (rather than some instantiation thereof) only if
13474 is not nested within some other construct. For example, in
13475 "template <typename T> void f(T) { A<T>::", A<T> is just an
13476 instantiation of A. */
13477 entering_scope = (template_parm_scope_p ()
13478 && cp_lexer_next_token_is (parser->lexer,
13479 CPP_SCOPE));
13480 template_id
13481 = finish_template_type (templ, arguments, entering_scope);
13482 }
13483 else
13484 {
13485 /* If it's not a class-template or a template-template, it should be
13486 a function-template. */
13487 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13488 || TREE_CODE (templ) == OVERLOAD
13489 || BASELINK_P (templ)));
13490
13491 template_id = lookup_template_function (templ, arguments);
13492 }
13493
13494 /* If parsing tentatively, replace the sequence of tokens that makes
13495 up the template-id with a CPP_TEMPLATE_ID token. That way,
13496 should we re-parse the token stream, we will not have to repeat
13497 the effort required to do the parse, nor will we issue duplicate
13498 error messages about problems during instantiation of the
13499 template. */
13500 if (start_of_id
13501 /* Don't do this if we had a parse error in a declarator; re-parsing
13502 might succeed if a name changes meaning (60361). */
13503 && !(cp_parser_error_occurred (parser)
13504 && cp_parser_parsing_tentatively (parser)
13505 && parser->in_declarator_p))
13506 {
13507 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13508
13509 /* Reset the contents of the START_OF_ID token. */
13510 token->type = CPP_TEMPLATE_ID;
13511 /* Retrieve any deferred checks. Do not pop this access checks yet
13512 so the memory will not be reclaimed during token replacing below. */
13513 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13514 token->u.tree_check_value->value = template_id;
13515 token->u.tree_check_value->checks = get_deferred_access_checks ();
13516 token->keyword = RID_MAX;
13517
13518 /* Purge all subsequent tokens. */
13519 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13520
13521 /* ??? Can we actually assume that, if template_id ==
13522 error_mark_node, we will have issued a diagnostic to the
13523 user, as opposed to simply marking the tentative parse as
13524 failed? */
13525 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13526 error_at (token->location, "parse error in template argument list");
13527 }
13528
13529 pop_to_parent_deferring_access_checks ();
13530 return template_id;
13531 }
13532
13533 /* Parse a template-name.
13534
13535 template-name:
13536 identifier
13537
13538 The standard should actually say:
13539
13540 template-name:
13541 identifier
13542 operator-function-id
13543
13544 A defect report has been filed about this issue.
13545
13546 A conversion-function-id cannot be a template name because they cannot
13547 be part of a template-id. In fact, looking at this code:
13548
13549 a.operator K<int>()
13550
13551 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13552 It is impossible to call a templated conversion-function-id with an
13553 explicit argument list, since the only allowed template parameter is
13554 the type to which it is converting.
13555
13556 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13557 `template' keyword, in a construction like:
13558
13559 T::template f<3>()
13560
13561 In that case `f' is taken to be a template-name, even though there
13562 is no way of knowing for sure.
13563
13564 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13565 name refers to a set of overloaded functions, at least one of which
13566 is a template, or an IDENTIFIER_NODE with the name of the template,
13567 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13568 names are looked up inside uninstantiated templates. */
13569
13570 static tree
13571 cp_parser_template_name (cp_parser* parser,
13572 bool template_keyword_p,
13573 bool check_dependency_p,
13574 bool is_declaration,
13575 enum tag_types tag_type,
13576 bool *is_identifier)
13577 {
13578 tree identifier;
13579 tree decl;
13580 tree fns;
13581 cp_token *token = cp_lexer_peek_token (parser->lexer);
13582
13583 /* If the next token is `operator', then we have either an
13584 operator-function-id or a conversion-function-id. */
13585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13586 {
13587 /* We don't know whether we're looking at an
13588 operator-function-id or a conversion-function-id. */
13589 cp_parser_parse_tentatively (parser);
13590 /* Try an operator-function-id. */
13591 identifier = cp_parser_operator_function_id (parser);
13592 /* If that didn't work, try a conversion-function-id. */
13593 if (!cp_parser_parse_definitely (parser))
13594 {
13595 cp_parser_error (parser, "expected template-name");
13596 return error_mark_node;
13597 }
13598 }
13599 /* Look for the identifier. */
13600 else
13601 identifier = cp_parser_identifier (parser);
13602
13603 /* If we didn't find an identifier, we don't have a template-id. */
13604 if (identifier == error_mark_node)
13605 return error_mark_node;
13606
13607 /* If the name immediately followed the `template' keyword, then it
13608 is a template-name. However, if the next token is not `<', then
13609 we do not treat it as a template-name, since it is not being used
13610 as part of a template-id. This enables us to handle constructs
13611 like:
13612
13613 template <typename T> struct S { S(); };
13614 template <typename T> S<T>::S();
13615
13616 correctly. We would treat `S' as a template -- if it were `S<T>'
13617 -- but we do not if there is no `<'. */
13618
13619 if (processing_template_decl
13620 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13621 {
13622 /* In a declaration, in a dependent context, we pretend that the
13623 "template" keyword was present in order to improve error
13624 recovery. For example, given:
13625
13626 template <typename T> void f(T::X<int>);
13627
13628 we want to treat "X<int>" as a template-id. */
13629 if (is_declaration
13630 && !template_keyword_p
13631 && parser->scope && TYPE_P (parser->scope)
13632 && check_dependency_p
13633 && dependent_scope_p (parser->scope)
13634 /* Do not do this for dtors (or ctors), since they never
13635 need the template keyword before their name. */
13636 && !constructor_name_p (identifier, parser->scope))
13637 {
13638 cp_token_position start = 0;
13639
13640 /* Explain what went wrong. */
13641 error_at (token->location, "non-template %qD used as template",
13642 identifier);
13643 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13644 parser->scope, identifier);
13645 /* If parsing tentatively, find the location of the "<" token. */
13646 if (cp_parser_simulate_error (parser))
13647 start = cp_lexer_token_position (parser->lexer, true);
13648 /* Parse the template arguments so that we can issue error
13649 messages about them. */
13650 cp_lexer_consume_token (parser->lexer);
13651 cp_parser_enclosed_template_argument_list (parser);
13652 /* Skip tokens until we find a good place from which to
13653 continue parsing. */
13654 cp_parser_skip_to_closing_parenthesis (parser,
13655 /*recovering=*/true,
13656 /*or_comma=*/true,
13657 /*consume_paren=*/false);
13658 /* If parsing tentatively, permanently remove the
13659 template argument list. That will prevent duplicate
13660 error messages from being issued about the missing
13661 "template" keyword. */
13662 if (start)
13663 cp_lexer_purge_tokens_after (parser->lexer, start);
13664 if (is_identifier)
13665 *is_identifier = true;
13666 return identifier;
13667 }
13668
13669 /* If the "template" keyword is present, then there is generally
13670 no point in doing name-lookup, so we just return IDENTIFIER.
13671 But, if the qualifying scope is non-dependent then we can
13672 (and must) do name-lookup normally. */
13673 if (template_keyword_p
13674 && (!parser->scope
13675 || (TYPE_P (parser->scope)
13676 && dependent_type_p (parser->scope))))
13677 return identifier;
13678 }
13679
13680 /* Look up the name. */
13681 decl = cp_parser_lookup_name (parser, identifier,
13682 tag_type,
13683 /*is_template=*/true,
13684 /*is_namespace=*/false,
13685 check_dependency_p,
13686 /*ambiguous_decls=*/NULL,
13687 token->location);
13688
13689 /* If DECL is a template, then the name was a template-name. */
13690 if (TREE_CODE (decl) == TEMPLATE_DECL)
13691 ;
13692 else
13693 {
13694 tree fn = NULL_TREE;
13695
13696 /* The standard does not explicitly indicate whether a name that
13697 names a set of overloaded declarations, some of which are
13698 templates, is a template-name. However, such a name should
13699 be a template-name; otherwise, there is no way to form a
13700 template-id for the overloaded templates. */
13701 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13702 if (TREE_CODE (fns) == OVERLOAD)
13703 for (fn = fns; fn; fn = OVL_NEXT (fn))
13704 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13705 break;
13706
13707 if (!fn)
13708 {
13709 /* The name does not name a template. */
13710 cp_parser_error (parser, "expected template-name");
13711 return error_mark_node;
13712 }
13713 }
13714
13715 /* If DECL is dependent, and refers to a function, then just return
13716 its name; we will look it up again during template instantiation. */
13717 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13718 {
13719 tree scope = ovl_scope (decl);
13720 if (TYPE_P (scope) && dependent_type_p (scope))
13721 return identifier;
13722 }
13723
13724 return decl;
13725 }
13726
13727 /* Parse a template-argument-list.
13728
13729 template-argument-list:
13730 template-argument ... [opt]
13731 template-argument-list , template-argument ... [opt]
13732
13733 Returns a TREE_VEC containing the arguments. */
13734
13735 static tree
13736 cp_parser_template_argument_list (cp_parser* parser)
13737 {
13738 tree fixed_args[10];
13739 unsigned n_args = 0;
13740 unsigned alloced = 10;
13741 tree *arg_ary = fixed_args;
13742 tree vec;
13743 bool saved_in_template_argument_list_p;
13744 bool saved_ice_p;
13745 bool saved_non_ice_p;
13746
13747 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13748 parser->in_template_argument_list_p = true;
13749 /* Even if the template-id appears in an integral
13750 constant-expression, the contents of the argument list do
13751 not. */
13752 saved_ice_p = parser->integral_constant_expression_p;
13753 parser->integral_constant_expression_p = false;
13754 saved_non_ice_p = parser->non_integral_constant_expression_p;
13755 parser->non_integral_constant_expression_p = false;
13756
13757 /* Parse the arguments. */
13758 do
13759 {
13760 tree argument;
13761
13762 if (n_args)
13763 /* Consume the comma. */
13764 cp_lexer_consume_token (parser->lexer);
13765
13766 /* Parse the template-argument. */
13767 argument = cp_parser_template_argument (parser);
13768
13769 /* If the next token is an ellipsis, we're expanding a template
13770 argument pack. */
13771 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13772 {
13773 if (argument == error_mark_node)
13774 {
13775 cp_token *token = cp_lexer_peek_token (parser->lexer);
13776 error_at (token->location,
13777 "expected parameter pack before %<...%>");
13778 }
13779 /* Consume the `...' token. */
13780 cp_lexer_consume_token (parser->lexer);
13781
13782 /* Make the argument into a TYPE_PACK_EXPANSION or
13783 EXPR_PACK_EXPANSION. */
13784 argument = make_pack_expansion (argument);
13785 }
13786
13787 if (n_args == alloced)
13788 {
13789 alloced *= 2;
13790
13791 if (arg_ary == fixed_args)
13792 {
13793 arg_ary = XNEWVEC (tree, alloced);
13794 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13795 }
13796 else
13797 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13798 }
13799 arg_ary[n_args++] = argument;
13800 }
13801 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13802
13803 vec = make_tree_vec (n_args);
13804
13805 while (n_args--)
13806 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13807
13808 if (arg_ary != fixed_args)
13809 free (arg_ary);
13810 parser->non_integral_constant_expression_p = saved_non_ice_p;
13811 parser->integral_constant_expression_p = saved_ice_p;
13812 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13813 #ifdef ENABLE_CHECKING
13814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13815 #endif
13816 return vec;
13817 }
13818
13819 /* Parse a template-argument.
13820
13821 template-argument:
13822 assignment-expression
13823 type-id
13824 id-expression
13825
13826 The representation is that of an assignment-expression, type-id, or
13827 id-expression -- except that the qualified id-expression is
13828 evaluated, so that the value returned is either a DECL or an
13829 OVERLOAD.
13830
13831 Although the standard says "assignment-expression", it forbids
13832 throw-expressions or assignments in the template argument.
13833 Therefore, we use "conditional-expression" instead. */
13834
13835 static tree
13836 cp_parser_template_argument (cp_parser* parser)
13837 {
13838 tree argument;
13839 bool template_p;
13840 bool address_p;
13841 bool maybe_type_id = false;
13842 cp_token *token = NULL, *argument_start_token = NULL;
13843 location_t loc = 0;
13844 cp_id_kind idk;
13845
13846 /* There's really no way to know what we're looking at, so we just
13847 try each alternative in order.
13848
13849 [temp.arg]
13850
13851 In a template-argument, an ambiguity between a type-id and an
13852 expression is resolved to a type-id, regardless of the form of
13853 the corresponding template-parameter.
13854
13855 Therefore, we try a type-id first. */
13856 cp_parser_parse_tentatively (parser);
13857 argument = cp_parser_template_type_arg (parser);
13858 /* If there was no error parsing the type-id but the next token is a
13859 '>>', our behavior depends on which dialect of C++ we're
13860 parsing. In C++98, we probably found a typo for '> >'. But there
13861 are type-id which are also valid expressions. For instance:
13862
13863 struct X { int operator >> (int); };
13864 template <int V> struct Foo {};
13865 Foo<X () >> 5> r;
13866
13867 Here 'X()' is a valid type-id of a function type, but the user just
13868 wanted to write the expression "X() >> 5". Thus, we remember that we
13869 found a valid type-id, but we still try to parse the argument as an
13870 expression to see what happens.
13871
13872 In C++0x, the '>>' will be considered two separate '>'
13873 tokens. */
13874 if (!cp_parser_error_occurred (parser)
13875 && cxx_dialect == cxx98
13876 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13877 {
13878 maybe_type_id = true;
13879 cp_parser_abort_tentative_parse (parser);
13880 }
13881 else
13882 {
13883 /* If the next token isn't a `,' or a `>', then this argument wasn't
13884 really finished. This means that the argument is not a valid
13885 type-id. */
13886 if (!cp_parser_next_token_ends_template_argument_p (parser))
13887 cp_parser_error (parser, "expected template-argument");
13888 /* If that worked, we're done. */
13889 if (cp_parser_parse_definitely (parser))
13890 return argument;
13891 }
13892 /* We're still not sure what the argument will be. */
13893 cp_parser_parse_tentatively (parser);
13894 /* Try a template. */
13895 argument_start_token = cp_lexer_peek_token (parser->lexer);
13896 argument = cp_parser_id_expression (parser,
13897 /*template_keyword_p=*/false,
13898 /*check_dependency_p=*/true,
13899 &template_p,
13900 /*declarator_p=*/false,
13901 /*optional_p=*/false);
13902 /* If the next token isn't a `,' or a `>', then this argument wasn't
13903 really finished. */
13904 if (!cp_parser_next_token_ends_template_argument_p (parser))
13905 cp_parser_error (parser, "expected template-argument");
13906 if (!cp_parser_error_occurred (parser))
13907 {
13908 /* Figure out what is being referred to. If the id-expression
13909 was for a class template specialization, then we will have a
13910 TYPE_DECL at this point. There is no need to do name lookup
13911 at this point in that case. */
13912 if (TREE_CODE (argument) != TYPE_DECL)
13913 argument = cp_parser_lookup_name (parser, argument,
13914 none_type,
13915 /*is_template=*/template_p,
13916 /*is_namespace=*/false,
13917 /*check_dependency=*/true,
13918 /*ambiguous_decls=*/NULL,
13919 argument_start_token->location);
13920 if (TREE_CODE (argument) != TEMPLATE_DECL
13921 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13922 cp_parser_error (parser, "expected template-name");
13923 }
13924 if (cp_parser_parse_definitely (parser))
13925 return argument;
13926 /* It must be a non-type argument. There permitted cases are given
13927 in [temp.arg.nontype]:
13928
13929 -- an integral constant-expression of integral or enumeration
13930 type; or
13931
13932 -- the name of a non-type template-parameter; or
13933
13934 -- the name of an object or function with external linkage...
13935
13936 -- the address of an object or function with external linkage...
13937
13938 -- a pointer to member... */
13939 /* Look for a non-type template parameter. */
13940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13941 {
13942 cp_parser_parse_tentatively (parser);
13943 argument = cp_parser_primary_expression (parser,
13944 /*address_p=*/false,
13945 /*cast_p=*/false,
13946 /*template_arg_p=*/true,
13947 &idk);
13948 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13949 || !cp_parser_next_token_ends_template_argument_p (parser))
13950 cp_parser_simulate_error (parser);
13951 if (cp_parser_parse_definitely (parser))
13952 return argument;
13953 }
13954
13955 /* If the next token is "&", the argument must be the address of an
13956 object or function with external linkage. */
13957 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13958 if (address_p)
13959 {
13960 loc = cp_lexer_peek_token (parser->lexer)->location;
13961 cp_lexer_consume_token (parser->lexer);
13962 }
13963 /* See if we might have an id-expression. */
13964 token = cp_lexer_peek_token (parser->lexer);
13965 if (token->type == CPP_NAME
13966 || token->keyword == RID_OPERATOR
13967 || token->type == CPP_SCOPE
13968 || token->type == CPP_TEMPLATE_ID
13969 || token->type == CPP_NESTED_NAME_SPECIFIER)
13970 {
13971 cp_parser_parse_tentatively (parser);
13972 argument = cp_parser_primary_expression (parser,
13973 address_p,
13974 /*cast_p=*/false,
13975 /*template_arg_p=*/true,
13976 &idk);
13977 if (cp_parser_error_occurred (parser)
13978 || !cp_parser_next_token_ends_template_argument_p (parser))
13979 cp_parser_abort_tentative_parse (parser);
13980 else
13981 {
13982 tree probe;
13983
13984 if (INDIRECT_REF_P (argument))
13985 {
13986 /* Strip the dereference temporarily. */
13987 gcc_assert (REFERENCE_REF_P (argument));
13988 argument = TREE_OPERAND (argument, 0);
13989 }
13990
13991 /* If we're in a template, we represent a qualified-id referring
13992 to a static data member as a SCOPE_REF even if the scope isn't
13993 dependent so that we can check access control later. */
13994 probe = argument;
13995 if (TREE_CODE (probe) == SCOPE_REF)
13996 probe = TREE_OPERAND (probe, 1);
13997 if (VAR_P (probe))
13998 {
13999 /* A variable without external linkage might still be a
14000 valid constant-expression, so no error is issued here
14001 if the external-linkage check fails. */
14002 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14003 cp_parser_simulate_error (parser);
14004 }
14005 else if (is_overloaded_fn (argument))
14006 /* All overloaded functions are allowed; if the external
14007 linkage test does not pass, an error will be issued
14008 later. */
14009 ;
14010 else if (address_p
14011 && (TREE_CODE (argument) == OFFSET_REF
14012 || TREE_CODE (argument) == SCOPE_REF))
14013 /* A pointer-to-member. */
14014 ;
14015 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14016 ;
14017 else
14018 cp_parser_simulate_error (parser);
14019
14020 if (cp_parser_parse_definitely (parser))
14021 {
14022 if (address_p)
14023 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14024 tf_warning_or_error);
14025 else
14026 argument = convert_from_reference (argument);
14027 return argument;
14028 }
14029 }
14030 }
14031 /* If the argument started with "&", there are no other valid
14032 alternatives at this point. */
14033 if (address_p)
14034 {
14035 cp_parser_error (parser, "invalid non-type template argument");
14036 return error_mark_node;
14037 }
14038
14039 /* If the argument wasn't successfully parsed as a type-id followed
14040 by '>>', the argument can only be a constant expression now.
14041 Otherwise, we try parsing the constant-expression tentatively,
14042 because the argument could really be a type-id. */
14043 if (maybe_type_id)
14044 cp_parser_parse_tentatively (parser);
14045 argument = cp_parser_constant_expression (parser,
14046 /*allow_non_constant_p=*/false,
14047 /*non_constant_p=*/NULL);
14048 if (!maybe_type_id)
14049 return argument;
14050 if (!cp_parser_next_token_ends_template_argument_p (parser))
14051 cp_parser_error (parser, "expected template-argument");
14052 if (cp_parser_parse_definitely (parser))
14053 return argument;
14054 /* We did our best to parse the argument as a non type-id, but that
14055 was the only alternative that matched (albeit with a '>' after
14056 it). We can assume it's just a typo from the user, and a
14057 diagnostic will then be issued. */
14058 return cp_parser_template_type_arg (parser);
14059 }
14060
14061 /* Parse an explicit-instantiation.
14062
14063 explicit-instantiation:
14064 template declaration
14065
14066 Although the standard says `declaration', what it really means is:
14067
14068 explicit-instantiation:
14069 template decl-specifier-seq [opt] declarator [opt] ;
14070
14071 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14072 supposed to be allowed. A defect report has been filed about this
14073 issue.
14074
14075 GNU Extension:
14076
14077 explicit-instantiation:
14078 storage-class-specifier template
14079 decl-specifier-seq [opt] declarator [opt] ;
14080 function-specifier template
14081 decl-specifier-seq [opt] declarator [opt] ; */
14082
14083 static void
14084 cp_parser_explicit_instantiation (cp_parser* parser)
14085 {
14086 int declares_class_or_enum;
14087 cp_decl_specifier_seq decl_specifiers;
14088 tree extension_specifier = NULL_TREE;
14089
14090 timevar_push (TV_TEMPLATE_INST);
14091
14092 /* Look for an (optional) storage-class-specifier or
14093 function-specifier. */
14094 if (cp_parser_allow_gnu_extensions_p (parser))
14095 {
14096 extension_specifier
14097 = cp_parser_storage_class_specifier_opt (parser);
14098 if (!extension_specifier)
14099 extension_specifier
14100 = cp_parser_function_specifier_opt (parser,
14101 /*decl_specs=*/NULL);
14102 }
14103
14104 /* Look for the `template' keyword. */
14105 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14106 /* Let the front end know that we are processing an explicit
14107 instantiation. */
14108 begin_explicit_instantiation ();
14109 /* [temp.explicit] says that we are supposed to ignore access
14110 control while processing explicit instantiation directives. */
14111 push_deferring_access_checks (dk_no_check);
14112 /* Parse a decl-specifier-seq. */
14113 cp_parser_decl_specifier_seq (parser,
14114 CP_PARSER_FLAGS_OPTIONAL,
14115 &decl_specifiers,
14116 &declares_class_or_enum);
14117 /* If there was exactly one decl-specifier, and it declared a class,
14118 and there's no declarator, then we have an explicit type
14119 instantiation. */
14120 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14121 {
14122 tree type;
14123
14124 type = check_tag_decl (&decl_specifiers,
14125 /*explicit_type_instantiation_p=*/true);
14126 /* Turn access control back on for names used during
14127 template instantiation. */
14128 pop_deferring_access_checks ();
14129 if (type)
14130 do_type_instantiation (type, extension_specifier,
14131 /*complain=*/tf_error);
14132 }
14133 else
14134 {
14135 cp_declarator *declarator;
14136 tree decl;
14137
14138 /* Parse the declarator. */
14139 declarator
14140 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14141 /*ctor_dtor_or_conv_p=*/NULL,
14142 /*parenthesized_p=*/NULL,
14143 /*member_p=*/false);
14144 if (declares_class_or_enum & 2)
14145 cp_parser_check_for_definition_in_return_type (declarator,
14146 decl_specifiers.type,
14147 decl_specifiers.locations[ds_type_spec]);
14148 if (declarator != cp_error_declarator)
14149 {
14150 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14151 permerror (decl_specifiers.locations[ds_inline],
14152 "explicit instantiation shall not use"
14153 " %<inline%> specifier");
14154 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14155 permerror (decl_specifiers.locations[ds_constexpr],
14156 "explicit instantiation shall not use"
14157 " %<constexpr%> specifier");
14158
14159 decl = grokdeclarator (declarator, &decl_specifiers,
14160 NORMAL, 0, &decl_specifiers.attributes);
14161 /* Turn access control back on for names used during
14162 template instantiation. */
14163 pop_deferring_access_checks ();
14164 /* Do the explicit instantiation. */
14165 do_decl_instantiation (decl, extension_specifier);
14166 }
14167 else
14168 {
14169 pop_deferring_access_checks ();
14170 /* Skip the body of the explicit instantiation. */
14171 cp_parser_skip_to_end_of_statement (parser);
14172 }
14173 }
14174 /* We're done with the instantiation. */
14175 end_explicit_instantiation ();
14176
14177 cp_parser_consume_semicolon_at_end_of_statement (parser);
14178
14179 timevar_pop (TV_TEMPLATE_INST);
14180 }
14181
14182 /* Parse an explicit-specialization.
14183
14184 explicit-specialization:
14185 template < > declaration
14186
14187 Although the standard says `declaration', what it really means is:
14188
14189 explicit-specialization:
14190 template <> decl-specifier [opt] init-declarator [opt] ;
14191 template <> function-definition
14192 template <> explicit-specialization
14193 template <> template-declaration */
14194
14195 static void
14196 cp_parser_explicit_specialization (cp_parser* parser)
14197 {
14198 bool need_lang_pop;
14199 cp_token *token = cp_lexer_peek_token (parser->lexer);
14200
14201 /* Look for the `template' keyword. */
14202 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14203 /* Look for the `<'. */
14204 cp_parser_require (parser, CPP_LESS, RT_LESS);
14205 /* Look for the `>'. */
14206 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14207 /* We have processed another parameter list. */
14208 ++parser->num_template_parameter_lists;
14209 /* [temp]
14210
14211 A template ... explicit specialization ... shall not have C
14212 linkage. */
14213 if (current_lang_name == lang_name_c)
14214 {
14215 error_at (token->location, "template specialization with C linkage");
14216 /* Give it C++ linkage to avoid confusing other parts of the
14217 front end. */
14218 push_lang_context (lang_name_cplusplus);
14219 need_lang_pop = true;
14220 }
14221 else
14222 need_lang_pop = false;
14223 /* Let the front end know that we are beginning a specialization. */
14224 if (!begin_specialization ())
14225 {
14226 end_specialization ();
14227 return;
14228 }
14229
14230 /* If the next keyword is `template', we need to figure out whether
14231 or not we're looking a template-declaration. */
14232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14233 {
14234 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14235 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14236 cp_parser_template_declaration_after_export (parser,
14237 /*member_p=*/false);
14238 else
14239 cp_parser_explicit_specialization (parser);
14240 }
14241 else
14242 /* Parse the dependent declaration. */
14243 cp_parser_single_declaration (parser,
14244 /*checks=*/NULL,
14245 /*member_p=*/false,
14246 /*explicit_specialization_p=*/true,
14247 /*friend_p=*/NULL);
14248 /* We're done with the specialization. */
14249 end_specialization ();
14250 /* For the erroneous case of a template with C linkage, we pushed an
14251 implicit C++ linkage scope; exit that scope now. */
14252 if (need_lang_pop)
14253 pop_lang_context ();
14254 /* We're done with this parameter list. */
14255 --parser->num_template_parameter_lists;
14256 }
14257
14258 /* Parse a type-specifier.
14259
14260 type-specifier:
14261 simple-type-specifier
14262 class-specifier
14263 enum-specifier
14264 elaborated-type-specifier
14265 cv-qualifier
14266
14267 GNU Extension:
14268
14269 type-specifier:
14270 __complex__
14271
14272 Returns a representation of the type-specifier. For a
14273 class-specifier, enum-specifier, or elaborated-type-specifier, a
14274 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14275
14276 The parser flags FLAGS is used to control type-specifier parsing.
14277
14278 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14279 in a decl-specifier-seq.
14280
14281 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14282 class-specifier, enum-specifier, or elaborated-type-specifier, then
14283 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14284 if a type is declared; 2 if it is defined. Otherwise, it is set to
14285 zero.
14286
14287 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14288 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14289 is set to FALSE. */
14290
14291 static tree
14292 cp_parser_type_specifier (cp_parser* parser,
14293 cp_parser_flags flags,
14294 cp_decl_specifier_seq *decl_specs,
14295 bool is_declaration,
14296 int* declares_class_or_enum,
14297 bool* is_cv_qualifier)
14298 {
14299 tree type_spec = NULL_TREE;
14300 cp_token *token;
14301 enum rid keyword;
14302 cp_decl_spec ds = ds_last;
14303
14304 /* Assume this type-specifier does not declare a new type. */
14305 if (declares_class_or_enum)
14306 *declares_class_or_enum = 0;
14307 /* And that it does not specify a cv-qualifier. */
14308 if (is_cv_qualifier)
14309 *is_cv_qualifier = false;
14310 /* Peek at the next token. */
14311 token = cp_lexer_peek_token (parser->lexer);
14312
14313 /* If we're looking at a keyword, we can use that to guide the
14314 production we choose. */
14315 keyword = token->keyword;
14316 switch (keyword)
14317 {
14318 case RID_ENUM:
14319 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14320 goto elaborated_type_specifier;
14321
14322 /* Look for the enum-specifier. */
14323 type_spec = cp_parser_enum_specifier (parser);
14324 /* If that worked, we're done. */
14325 if (type_spec)
14326 {
14327 if (declares_class_or_enum)
14328 *declares_class_or_enum = 2;
14329 if (decl_specs)
14330 cp_parser_set_decl_spec_type (decl_specs,
14331 type_spec,
14332 token,
14333 /*type_definition_p=*/true);
14334 return type_spec;
14335 }
14336 else
14337 goto elaborated_type_specifier;
14338
14339 /* Any of these indicate either a class-specifier, or an
14340 elaborated-type-specifier. */
14341 case RID_CLASS:
14342 case RID_STRUCT:
14343 case RID_UNION:
14344 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14345 goto elaborated_type_specifier;
14346
14347 /* Parse tentatively so that we can back up if we don't find a
14348 class-specifier. */
14349 cp_parser_parse_tentatively (parser);
14350 /* Look for the class-specifier. */
14351 type_spec = cp_parser_class_specifier (parser);
14352 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14353 /* If that worked, we're done. */
14354 if (cp_parser_parse_definitely (parser))
14355 {
14356 if (declares_class_or_enum)
14357 *declares_class_or_enum = 2;
14358 if (decl_specs)
14359 cp_parser_set_decl_spec_type (decl_specs,
14360 type_spec,
14361 token,
14362 /*type_definition_p=*/true);
14363 return type_spec;
14364 }
14365
14366 /* Fall through. */
14367 elaborated_type_specifier:
14368 /* We're declaring (not defining) a class or enum. */
14369 if (declares_class_or_enum)
14370 *declares_class_or_enum = 1;
14371
14372 /* Fall through. */
14373 case RID_TYPENAME:
14374 /* Look for an elaborated-type-specifier. */
14375 type_spec
14376 = (cp_parser_elaborated_type_specifier
14377 (parser,
14378 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14379 is_declaration));
14380 if (decl_specs)
14381 cp_parser_set_decl_spec_type (decl_specs,
14382 type_spec,
14383 token,
14384 /*type_definition_p=*/false);
14385 return type_spec;
14386
14387 case RID_CONST:
14388 ds = ds_const;
14389 if (is_cv_qualifier)
14390 *is_cv_qualifier = true;
14391 break;
14392
14393 case RID_VOLATILE:
14394 ds = ds_volatile;
14395 if (is_cv_qualifier)
14396 *is_cv_qualifier = true;
14397 break;
14398
14399 case RID_RESTRICT:
14400 ds = ds_restrict;
14401 if (is_cv_qualifier)
14402 *is_cv_qualifier = true;
14403 break;
14404
14405 case RID_COMPLEX:
14406 /* The `__complex__' keyword is a GNU extension. */
14407 ds = ds_complex;
14408 break;
14409
14410 default:
14411 break;
14412 }
14413
14414 /* Handle simple keywords. */
14415 if (ds != ds_last)
14416 {
14417 if (decl_specs)
14418 {
14419 set_and_check_decl_spec_loc (decl_specs, ds, token);
14420 decl_specs->any_specifiers_p = true;
14421 }
14422 return cp_lexer_consume_token (parser->lexer)->u.value;
14423 }
14424
14425 /* If we do not already have a type-specifier, assume we are looking
14426 at a simple-type-specifier. */
14427 type_spec = cp_parser_simple_type_specifier (parser,
14428 decl_specs,
14429 flags);
14430
14431 /* If we didn't find a type-specifier, and a type-specifier was not
14432 optional in this context, issue an error message. */
14433 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14434 {
14435 cp_parser_error (parser, "expected type specifier");
14436 return error_mark_node;
14437 }
14438
14439 return type_spec;
14440 }
14441
14442 /* Parse a simple-type-specifier.
14443
14444 simple-type-specifier:
14445 :: [opt] nested-name-specifier [opt] type-name
14446 :: [opt] nested-name-specifier template template-id
14447 char
14448 wchar_t
14449 bool
14450 short
14451 int
14452 long
14453 signed
14454 unsigned
14455 float
14456 double
14457 void
14458
14459 C++0x Extension:
14460
14461 simple-type-specifier:
14462 auto
14463 decltype ( expression )
14464 char16_t
14465 char32_t
14466 __underlying_type ( type-id )
14467
14468 GNU Extension:
14469
14470 simple-type-specifier:
14471 __int128
14472 __typeof__ unary-expression
14473 __typeof__ ( type-id )
14474 __typeof__ ( type-id ) { initializer-list , [opt] }
14475
14476 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14477 appropriately updated. */
14478
14479 static tree
14480 cp_parser_simple_type_specifier (cp_parser* parser,
14481 cp_decl_specifier_seq *decl_specs,
14482 cp_parser_flags flags)
14483 {
14484 tree type = NULL_TREE;
14485 cp_token *token;
14486
14487 /* Peek at the next token. */
14488 token = cp_lexer_peek_token (parser->lexer);
14489
14490 /* If we're looking at a keyword, things are easy. */
14491 switch (token->keyword)
14492 {
14493 case RID_CHAR:
14494 if (decl_specs)
14495 decl_specs->explicit_char_p = true;
14496 type = char_type_node;
14497 break;
14498 case RID_CHAR16:
14499 type = char16_type_node;
14500 break;
14501 case RID_CHAR32:
14502 type = char32_type_node;
14503 break;
14504 case RID_WCHAR:
14505 type = wchar_type_node;
14506 break;
14507 case RID_BOOL:
14508 type = boolean_type_node;
14509 break;
14510 case RID_SHORT:
14511 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14512 type = short_integer_type_node;
14513 break;
14514 case RID_INT:
14515 if (decl_specs)
14516 decl_specs->explicit_int_p = true;
14517 type = integer_type_node;
14518 break;
14519 case RID_INT128:
14520 if (!int128_integer_type_node)
14521 break;
14522 if (decl_specs)
14523 decl_specs->explicit_int128_p = true;
14524 type = int128_integer_type_node;
14525 break;
14526 case RID_LONG:
14527 if (decl_specs)
14528 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14529 type = long_integer_type_node;
14530 break;
14531 case RID_SIGNED:
14532 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14533 type = integer_type_node;
14534 break;
14535 case RID_UNSIGNED:
14536 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14537 type = unsigned_type_node;
14538 break;
14539 case RID_FLOAT:
14540 type = float_type_node;
14541 break;
14542 case RID_DOUBLE:
14543 type = double_type_node;
14544 break;
14545 case RID_VOID:
14546 type = void_type_node;
14547 break;
14548
14549 case RID_AUTO:
14550 maybe_warn_cpp0x (CPP0X_AUTO);
14551 if (parser->auto_is_implicit_function_template_parm_p)
14552 {
14553 type = synthesize_implicit_template_parm (parser);
14554
14555 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14556 {
14557 if (cxx_dialect < cxx1y)
14558 pedwarn (location_of (type), 0,
14559 "use of %<auto%> in lambda parameter declaration "
14560 "only available with "
14561 "-std=c++1y or -std=gnu++1y");
14562 }
14563 else if (cxx_dialect < cxx1y)
14564 pedwarn (location_of (type), 0,
14565 "use of %<auto%> in parameter declaration "
14566 "only available with "
14567 "-std=c++1y or -std=gnu++1y");
14568 else
14569 pedwarn (location_of (type), OPT_Wpedantic,
14570 "ISO C++ forbids use of %<auto%> in parameter "
14571 "declaration");
14572 }
14573 else
14574 type = make_auto ();
14575 break;
14576
14577 case RID_DECLTYPE:
14578 /* Since DR 743, decltype can either be a simple-type-specifier by
14579 itself or begin a nested-name-specifier. Parsing it will replace
14580 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14581 handling below decide what to do. */
14582 cp_parser_decltype (parser);
14583 cp_lexer_set_token_position (parser->lexer, token);
14584 break;
14585
14586 case RID_TYPEOF:
14587 /* Consume the `typeof' token. */
14588 cp_lexer_consume_token (parser->lexer);
14589 /* Parse the operand to `typeof'. */
14590 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14591 /* If it is not already a TYPE, take its type. */
14592 if (!TYPE_P (type))
14593 type = finish_typeof (type);
14594
14595 if (decl_specs)
14596 cp_parser_set_decl_spec_type (decl_specs, type,
14597 token,
14598 /*type_definition_p=*/false);
14599
14600 return type;
14601
14602 case RID_UNDERLYING_TYPE:
14603 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14604 if (decl_specs)
14605 cp_parser_set_decl_spec_type (decl_specs, type,
14606 token,
14607 /*type_definition_p=*/false);
14608
14609 return type;
14610
14611 case RID_BASES:
14612 case RID_DIRECT_BASES:
14613 type = cp_parser_trait_expr (parser, token->keyword);
14614 if (decl_specs)
14615 cp_parser_set_decl_spec_type (decl_specs, type,
14616 token,
14617 /*type_definition_p=*/false);
14618 return type;
14619 default:
14620 break;
14621 }
14622
14623 /* If token is an already-parsed decltype not followed by ::,
14624 it's a simple-type-specifier. */
14625 if (token->type == CPP_DECLTYPE
14626 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14627 {
14628 type = token->u.value;
14629 if (decl_specs)
14630 cp_parser_set_decl_spec_type (decl_specs, type,
14631 token,
14632 /*type_definition_p=*/false);
14633 cp_lexer_consume_token (parser->lexer);
14634 return type;
14635 }
14636
14637 /* If the type-specifier was for a built-in type, we're done. */
14638 if (type)
14639 {
14640 /* Record the type. */
14641 if (decl_specs
14642 && (token->keyword != RID_SIGNED
14643 && token->keyword != RID_UNSIGNED
14644 && token->keyword != RID_SHORT
14645 && token->keyword != RID_LONG))
14646 cp_parser_set_decl_spec_type (decl_specs,
14647 type,
14648 token,
14649 /*type_definition_p=*/false);
14650 if (decl_specs)
14651 decl_specs->any_specifiers_p = true;
14652
14653 /* Consume the token. */
14654 cp_lexer_consume_token (parser->lexer);
14655
14656 /* There is no valid C++ program where a non-template type is
14657 followed by a "<". That usually indicates that the user thought
14658 that the type was a template. */
14659 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14660 token->location);
14661
14662 return TYPE_NAME (type);
14663 }
14664
14665 /* The type-specifier must be a user-defined type. */
14666 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14667 {
14668 bool qualified_p;
14669 bool global_p;
14670
14671 /* Don't gobble tokens or issue error messages if this is an
14672 optional type-specifier. */
14673 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14674 cp_parser_parse_tentatively (parser);
14675
14676 /* Look for the optional `::' operator. */
14677 global_p
14678 = (cp_parser_global_scope_opt (parser,
14679 /*current_scope_valid_p=*/false)
14680 != NULL_TREE);
14681 /* Look for the nested-name specifier. */
14682 qualified_p
14683 = (cp_parser_nested_name_specifier_opt (parser,
14684 /*typename_keyword_p=*/false,
14685 /*check_dependency_p=*/true,
14686 /*type_p=*/false,
14687 /*is_declaration=*/false)
14688 != NULL_TREE);
14689 token = cp_lexer_peek_token (parser->lexer);
14690 /* If we have seen a nested-name-specifier, and the next token
14691 is `template', then we are using the template-id production. */
14692 if (parser->scope
14693 && cp_parser_optional_template_keyword (parser))
14694 {
14695 /* Look for the template-id. */
14696 type = cp_parser_template_id (parser,
14697 /*template_keyword_p=*/true,
14698 /*check_dependency_p=*/true,
14699 none_type,
14700 /*is_declaration=*/false);
14701 /* If the template-id did not name a type, we are out of
14702 luck. */
14703 if (TREE_CODE (type) != TYPE_DECL)
14704 {
14705 cp_parser_error (parser, "expected template-id for type");
14706 type = NULL_TREE;
14707 }
14708 }
14709 /* Otherwise, look for a type-name. */
14710 else
14711 type = cp_parser_type_name (parser);
14712 /* Keep track of all name-lookups performed in class scopes. */
14713 if (type
14714 && !global_p
14715 && !qualified_p
14716 && TREE_CODE (type) == TYPE_DECL
14717 && identifier_p (DECL_NAME (type)))
14718 maybe_note_name_used_in_class (DECL_NAME (type), type);
14719 /* If it didn't work out, we don't have a TYPE. */
14720 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14721 && !cp_parser_parse_definitely (parser))
14722 type = NULL_TREE;
14723 if (type && decl_specs)
14724 cp_parser_set_decl_spec_type (decl_specs, type,
14725 token,
14726 /*type_definition_p=*/false);
14727 }
14728
14729 /* If we didn't get a type-name, issue an error message. */
14730 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14731 {
14732 cp_parser_error (parser, "expected type-name");
14733 return error_mark_node;
14734 }
14735
14736 if (type && type != error_mark_node)
14737 {
14738 /* See if TYPE is an Objective-C type, and if so, parse and
14739 accept any protocol references following it. Do this before
14740 the cp_parser_check_for_invalid_template_id() call, because
14741 Objective-C types can be followed by '<...>' which would
14742 enclose protocol names rather than template arguments, and so
14743 everything is fine. */
14744 if (c_dialect_objc () && !parser->scope
14745 && (objc_is_id (type) || objc_is_class_name (type)))
14746 {
14747 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14748 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14749
14750 /* Clobber the "unqualified" type previously entered into
14751 DECL_SPECS with the new, improved protocol-qualified version. */
14752 if (decl_specs)
14753 decl_specs->type = qual_type;
14754
14755 return qual_type;
14756 }
14757
14758 /* There is no valid C++ program where a non-template type is
14759 followed by a "<". That usually indicates that the user
14760 thought that the type was a template. */
14761 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14762 none_type,
14763 token->location);
14764 }
14765
14766 return type;
14767 }
14768
14769 /* Parse a type-name.
14770
14771 type-name:
14772 class-name
14773 enum-name
14774 typedef-name
14775 simple-template-id [in c++0x]
14776
14777 enum-name:
14778 identifier
14779
14780 typedef-name:
14781 identifier
14782
14783 Returns a TYPE_DECL for the type. */
14784
14785 static tree
14786 cp_parser_type_name (cp_parser* parser)
14787 {
14788 tree type_decl;
14789
14790 /* We can't know yet whether it is a class-name or not. */
14791 cp_parser_parse_tentatively (parser);
14792 /* Try a class-name. */
14793 type_decl = cp_parser_class_name (parser,
14794 /*typename_keyword_p=*/false,
14795 /*template_keyword_p=*/false,
14796 none_type,
14797 /*check_dependency_p=*/true,
14798 /*class_head_p=*/false,
14799 /*is_declaration=*/false);
14800 /* If it's not a class-name, keep looking. */
14801 if (!cp_parser_parse_definitely (parser))
14802 {
14803 if (cxx_dialect < cxx11)
14804 /* It must be a typedef-name or an enum-name. */
14805 return cp_parser_nonclass_name (parser);
14806
14807 cp_parser_parse_tentatively (parser);
14808 /* It is either a simple-template-id representing an
14809 instantiation of an alias template... */
14810 type_decl = cp_parser_template_id (parser,
14811 /*template_keyword_p=*/false,
14812 /*check_dependency_p=*/true,
14813 none_type,
14814 /*is_declaration=*/false);
14815 /* Note that this must be an instantiation of an alias template
14816 because [temp.names]/6 says:
14817
14818 A template-id that names an alias template specialization
14819 is a type-name.
14820
14821 Whereas [temp.names]/7 says:
14822
14823 A simple-template-id that names a class template
14824 specialization is a class-name. */
14825 if (type_decl != NULL_TREE
14826 && TREE_CODE (type_decl) == TYPE_DECL
14827 && TYPE_DECL_ALIAS_P (type_decl))
14828 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14829 else
14830 cp_parser_simulate_error (parser);
14831
14832 if (!cp_parser_parse_definitely (parser))
14833 /* ... Or a typedef-name or an enum-name. */
14834 return cp_parser_nonclass_name (parser);
14835 }
14836
14837 return type_decl;
14838 }
14839
14840 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14841
14842 enum-name:
14843 identifier
14844
14845 typedef-name:
14846 identifier
14847
14848 Returns a TYPE_DECL for the type. */
14849
14850 static tree
14851 cp_parser_nonclass_name (cp_parser* parser)
14852 {
14853 tree type_decl;
14854 tree identifier;
14855
14856 cp_token *token = cp_lexer_peek_token (parser->lexer);
14857 identifier = cp_parser_identifier (parser);
14858 if (identifier == error_mark_node)
14859 return error_mark_node;
14860
14861 /* Look up the type-name. */
14862 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14863
14864 type_decl = strip_using_decl (type_decl);
14865
14866 if (TREE_CODE (type_decl) != TYPE_DECL
14867 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14868 {
14869 /* See if this is an Objective-C type. */
14870 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14871 tree type = objc_get_protocol_qualified_type (identifier, protos);
14872 if (type)
14873 type_decl = TYPE_NAME (type);
14874 }
14875
14876 /* Issue an error if we did not find a type-name. */
14877 if (TREE_CODE (type_decl) != TYPE_DECL
14878 /* In Objective-C, we have the complication that class names are
14879 normally type names and start declarations (eg, the
14880 "NSObject" in "NSObject *object;"), but can be used in an
14881 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14882 is an expression. So, a classname followed by a dot is not a
14883 valid type-name. */
14884 || (objc_is_class_name (TREE_TYPE (type_decl))
14885 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14886 {
14887 if (!cp_parser_simulate_error (parser))
14888 cp_parser_name_lookup_error (parser, identifier, type_decl,
14889 NLE_TYPE, token->location);
14890 return error_mark_node;
14891 }
14892 /* Remember that the name was used in the definition of the
14893 current class so that we can check later to see if the
14894 meaning would have been different after the class was
14895 entirely defined. */
14896 else if (type_decl != error_mark_node
14897 && !parser->scope)
14898 maybe_note_name_used_in_class (identifier, type_decl);
14899
14900 return type_decl;
14901 }
14902
14903 /* Parse an elaborated-type-specifier. Note that the grammar given
14904 here incorporates the resolution to DR68.
14905
14906 elaborated-type-specifier:
14907 class-key :: [opt] nested-name-specifier [opt] identifier
14908 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14909 enum-key :: [opt] nested-name-specifier [opt] identifier
14910 typename :: [opt] nested-name-specifier identifier
14911 typename :: [opt] nested-name-specifier template [opt]
14912 template-id
14913
14914 GNU extension:
14915
14916 elaborated-type-specifier:
14917 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14918 class-key attributes :: [opt] nested-name-specifier [opt]
14919 template [opt] template-id
14920 enum attributes :: [opt] nested-name-specifier [opt] identifier
14921
14922 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14923 declared `friend'. If IS_DECLARATION is TRUE, then this
14924 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14925 something is being declared.
14926
14927 Returns the TYPE specified. */
14928
14929 static tree
14930 cp_parser_elaborated_type_specifier (cp_parser* parser,
14931 bool is_friend,
14932 bool is_declaration)
14933 {
14934 enum tag_types tag_type;
14935 tree identifier;
14936 tree type = NULL_TREE;
14937 tree attributes = NULL_TREE;
14938 tree globalscope;
14939 cp_token *token = NULL;
14940
14941 /* See if we're looking at the `enum' keyword. */
14942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14943 {
14944 /* Consume the `enum' token. */
14945 cp_lexer_consume_token (parser->lexer);
14946 /* Remember that it's an enumeration type. */
14947 tag_type = enum_type;
14948 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14949 enums) is used here. */
14950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14951 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14952 {
14953 pedwarn (input_location, 0, "elaborated-type-specifier "
14954 "for a scoped enum must not use the %<%D%> keyword",
14955 cp_lexer_peek_token (parser->lexer)->u.value);
14956 /* Consume the `struct' or `class' and parse it anyway. */
14957 cp_lexer_consume_token (parser->lexer);
14958 }
14959 /* Parse the attributes. */
14960 attributes = cp_parser_attributes_opt (parser);
14961 }
14962 /* Or, it might be `typename'. */
14963 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14964 RID_TYPENAME))
14965 {
14966 /* Consume the `typename' token. */
14967 cp_lexer_consume_token (parser->lexer);
14968 /* Remember that it's a `typename' type. */
14969 tag_type = typename_type;
14970 }
14971 /* Otherwise it must be a class-key. */
14972 else
14973 {
14974 tag_type = cp_parser_class_key (parser);
14975 if (tag_type == none_type)
14976 return error_mark_node;
14977 /* Parse the attributes. */
14978 attributes = cp_parser_attributes_opt (parser);
14979 }
14980
14981 /* Look for the `::' operator. */
14982 globalscope = cp_parser_global_scope_opt (parser,
14983 /*current_scope_valid_p=*/false);
14984 /* Look for the nested-name-specifier. */
14985 if (tag_type == typename_type && !globalscope)
14986 {
14987 if (!cp_parser_nested_name_specifier (parser,
14988 /*typename_keyword_p=*/true,
14989 /*check_dependency_p=*/true,
14990 /*type_p=*/true,
14991 is_declaration))
14992 return error_mark_node;
14993 }
14994 else
14995 /* Even though `typename' is not present, the proposed resolution
14996 to Core Issue 180 says that in `class A<T>::B', `B' should be
14997 considered a type-name, even if `A<T>' is dependent. */
14998 cp_parser_nested_name_specifier_opt (parser,
14999 /*typename_keyword_p=*/true,
15000 /*check_dependency_p=*/true,
15001 /*type_p=*/true,
15002 is_declaration);
15003 /* For everything but enumeration types, consider a template-id.
15004 For an enumeration type, consider only a plain identifier. */
15005 if (tag_type != enum_type)
15006 {
15007 bool template_p = false;
15008 tree decl;
15009
15010 /* Allow the `template' keyword. */
15011 template_p = cp_parser_optional_template_keyword (parser);
15012 /* If we didn't see `template', we don't know if there's a
15013 template-id or not. */
15014 if (!template_p)
15015 cp_parser_parse_tentatively (parser);
15016 /* Parse the template-id. */
15017 token = cp_lexer_peek_token (parser->lexer);
15018 decl = cp_parser_template_id (parser, template_p,
15019 /*check_dependency_p=*/true,
15020 tag_type,
15021 is_declaration);
15022 /* If we didn't find a template-id, look for an ordinary
15023 identifier. */
15024 if (!template_p && !cp_parser_parse_definitely (parser))
15025 ;
15026 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15027 in effect, then we must assume that, upon instantiation, the
15028 template will correspond to a class. */
15029 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15030 && tag_type == typename_type)
15031 type = make_typename_type (parser->scope, decl,
15032 typename_type,
15033 /*complain=*/tf_error);
15034 /* If the `typename' keyword is in effect and DECL is not a type
15035 decl, then type is non existent. */
15036 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15037 ;
15038 else if (TREE_CODE (decl) == TYPE_DECL)
15039 type = check_elaborated_type_specifier (tag_type, decl,
15040 /*allow_template_p=*/true);
15041 else if (decl == error_mark_node)
15042 type = error_mark_node;
15043 }
15044
15045 if (!type)
15046 {
15047 token = cp_lexer_peek_token (parser->lexer);
15048 identifier = cp_parser_identifier (parser);
15049
15050 if (identifier == error_mark_node)
15051 {
15052 parser->scope = NULL_TREE;
15053 return error_mark_node;
15054 }
15055
15056 /* For a `typename', we needn't call xref_tag. */
15057 if (tag_type == typename_type
15058 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15059 return cp_parser_make_typename_type (parser, parser->scope,
15060 identifier,
15061 token->location);
15062 /* Look up a qualified name in the usual way. */
15063 if (parser->scope)
15064 {
15065 tree decl;
15066 tree ambiguous_decls;
15067
15068 decl = cp_parser_lookup_name (parser, identifier,
15069 tag_type,
15070 /*is_template=*/false,
15071 /*is_namespace=*/false,
15072 /*check_dependency=*/true,
15073 &ambiguous_decls,
15074 token->location);
15075
15076 /* If the lookup was ambiguous, an error will already have been
15077 issued. */
15078 if (ambiguous_decls)
15079 return error_mark_node;
15080
15081 /* If we are parsing friend declaration, DECL may be a
15082 TEMPLATE_DECL tree node here. However, we need to check
15083 whether this TEMPLATE_DECL results in valid code. Consider
15084 the following example:
15085
15086 namespace N {
15087 template <class T> class C {};
15088 }
15089 class X {
15090 template <class T> friend class N::C; // #1, valid code
15091 };
15092 template <class T> class Y {
15093 friend class N::C; // #2, invalid code
15094 };
15095
15096 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15097 name lookup of `N::C'. We see that friend declaration must
15098 be template for the code to be valid. Note that
15099 processing_template_decl does not work here since it is
15100 always 1 for the above two cases. */
15101
15102 decl = (cp_parser_maybe_treat_template_as_class
15103 (decl, /*tag_name_p=*/is_friend
15104 && parser->num_template_parameter_lists));
15105
15106 if (TREE_CODE (decl) != TYPE_DECL)
15107 {
15108 cp_parser_diagnose_invalid_type_name (parser,
15109 parser->scope,
15110 identifier,
15111 token->location);
15112 return error_mark_node;
15113 }
15114
15115 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15116 {
15117 bool allow_template = (parser->num_template_parameter_lists
15118 || DECL_SELF_REFERENCE_P (decl));
15119 type = check_elaborated_type_specifier (tag_type, decl,
15120 allow_template);
15121
15122 if (type == error_mark_node)
15123 return error_mark_node;
15124 }
15125
15126 /* Forward declarations of nested types, such as
15127
15128 class C1::C2;
15129 class C1::C2::C3;
15130
15131 are invalid unless all components preceding the final '::'
15132 are complete. If all enclosing types are complete, these
15133 declarations become merely pointless.
15134
15135 Invalid forward declarations of nested types are errors
15136 caught elsewhere in parsing. Those that are pointless arrive
15137 here. */
15138
15139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15140 && !is_friend && !processing_explicit_instantiation)
15141 warning (0, "declaration %qD does not declare anything", decl);
15142
15143 type = TREE_TYPE (decl);
15144 }
15145 else
15146 {
15147 /* An elaborated-type-specifier sometimes introduces a new type and
15148 sometimes names an existing type. Normally, the rule is that it
15149 introduces a new type only if there is not an existing type of
15150 the same name already in scope. For example, given:
15151
15152 struct S {};
15153 void f() { struct S s; }
15154
15155 the `struct S' in the body of `f' is the same `struct S' as in
15156 the global scope; the existing definition is used. However, if
15157 there were no global declaration, this would introduce a new
15158 local class named `S'.
15159
15160 An exception to this rule applies to the following code:
15161
15162 namespace N { struct S; }
15163
15164 Here, the elaborated-type-specifier names a new type
15165 unconditionally; even if there is already an `S' in the
15166 containing scope this declaration names a new type.
15167 This exception only applies if the elaborated-type-specifier
15168 forms the complete declaration:
15169
15170 [class.name]
15171
15172 A declaration consisting solely of `class-key identifier ;' is
15173 either a redeclaration of the name in the current scope or a
15174 forward declaration of the identifier as a class name. It
15175 introduces the name into the current scope.
15176
15177 We are in this situation precisely when the next token is a `;'.
15178
15179 An exception to the exception is that a `friend' declaration does
15180 *not* name a new type; i.e., given:
15181
15182 struct S { friend struct T; };
15183
15184 `T' is not a new type in the scope of `S'.
15185
15186 Also, `new struct S' or `sizeof (struct S)' never results in the
15187 definition of a new type; a new type can only be declared in a
15188 declaration context. */
15189
15190 tag_scope ts;
15191 bool template_p;
15192
15193 if (is_friend)
15194 /* Friends have special name lookup rules. */
15195 ts = ts_within_enclosing_non_class;
15196 else if (is_declaration
15197 && cp_lexer_next_token_is (parser->lexer,
15198 CPP_SEMICOLON))
15199 /* This is a `class-key identifier ;' */
15200 ts = ts_current;
15201 else
15202 ts = ts_global;
15203
15204 template_p =
15205 (parser->num_template_parameter_lists
15206 && (cp_parser_next_token_starts_class_definition_p (parser)
15207 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15208 /* An unqualified name was used to reference this type, so
15209 there were no qualifying templates. */
15210 if (!cp_parser_check_template_parameters (parser,
15211 /*num_templates=*/0,
15212 token->location,
15213 /*declarator=*/NULL))
15214 return error_mark_node;
15215 type = xref_tag (tag_type, identifier, ts, template_p);
15216 }
15217 }
15218
15219 if (type == error_mark_node)
15220 return error_mark_node;
15221
15222 /* Allow attributes on forward declarations of classes. */
15223 if (attributes)
15224 {
15225 if (TREE_CODE (type) == TYPENAME_TYPE)
15226 warning (OPT_Wattributes,
15227 "attributes ignored on uninstantiated type");
15228 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15229 && ! processing_explicit_instantiation)
15230 warning (OPT_Wattributes,
15231 "attributes ignored on template instantiation");
15232 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15233 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15234 else
15235 warning (OPT_Wattributes,
15236 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15237 }
15238
15239 if (tag_type != enum_type)
15240 {
15241 /* Indicate whether this class was declared as a `class' or as a
15242 `struct'. */
15243 if (TREE_CODE (type) == RECORD_TYPE)
15244 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15245 cp_parser_check_class_key (tag_type, type);
15246 }
15247
15248 /* A "<" cannot follow an elaborated type specifier. If that
15249 happens, the user was probably trying to form a template-id. */
15250 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15251 token->location);
15252
15253 return type;
15254 }
15255
15256 /* Parse an enum-specifier.
15257
15258 enum-specifier:
15259 enum-head { enumerator-list [opt] }
15260 enum-head { enumerator-list , } [C++0x]
15261
15262 enum-head:
15263 enum-key identifier [opt] enum-base [opt]
15264 enum-key nested-name-specifier identifier enum-base [opt]
15265
15266 enum-key:
15267 enum
15268 enum class [C++0x]
15269 enum struct [C++0x]
15270
15271 enum-base: [C++0x]
15272 : type-specifier-seq
15273
15274 opaque-enum-specifier:
15275 enum-key identifier enum-base [opt] ;
15276
15277 GNU Extensions:
15278 enum-key attributes[opt] identifier [opt] enum-base [opt]
15279 { enumerator-list [opt] }attributes[opt]
15280 enum-key attributes[opt] identifier [opt] enum-base [opt]
15281 { enumerator-list, }attributes[opt] [C++0x]
15282
15283 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15284 if the token stream isn't an enum-specifier after all. */
15285
15286 static tree
15287 cp_parser_enum_specifier (cp_parser* parser)
15288 {
15289 tree identifier;
15290 tree type = NULL_TREE;
15291 tree prev_scope;
15292 tree nested_name_specifier = NULL_TREE;
15293 tree attributes;
15294 bool scoped_enum_p = false;
15295 bool has_underlying_type = false;
15296 bool nested_being_defined = false;
15297 bool new_value_list = false;
15298 bool is_new_type = false;
15299 bool is_anonymous = false;
15300 tree underlying_type = NULL_TREE;
15301 cp_token *type_start_token = NULL;
15302 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15303
15304 parser->colon_corrects_to_scope_p = false;
15305
15306 /* Parse tentatively so that we can back up if we don't find a
15307 enum-specifier. */
15308 cp_parser_parse_tentatively (parser);
15309
15310 /* Caller guarantees that the current token is 'enum', an identifier
15311 possibly follows, and the token after that is an opening brace.
15312 If we don't have an identifier, fabricate an anonymous name for
15313 the enumeration being defined. */
15314 cp_lexer_consume_token (parser->lexer);
15315
15316 /* Parse the "class" or "struct", which indicates a scoped
15317 enumeration type in C++0x. */
15318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15319 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15320 {
15321 if (cxx_dialect < cxx11)
15322 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15323
15324 /* Consume the `struct' or `class' token. */
15325 cp_lexer_consume_token (parser->lexer);
15326
15327 scoped_enum_p = true;
15328 }
15329
15330 attributes = cp_parser_attributes_opt (parser);
15331
15332 /* Clear the qualification. */
15333 parser->scope = NULL_TREE;
15334 parser->qualifying_scope = NULL_TREE;
15335 parser->object_scope = NULL_TREE;
15336
15337 /* Figure out in what scope the declaration is being placed. */
15338 prev_scope = current_scope ();
15339
15340 type_start_token = cp_lexer_peek_token (parser->lexer);
15341
15342 push_deferring_access_checks (dk_no_check);
15343 nested_name_specifier
15344 = cp_parser_nested_name_specifier_opt (parser,
15345 /*typename_keyword_p=*/true,
15346 /*check_dependency_p=*/false,
15347 /*type_p=*/false,
15348 /*is_declaration=*/false);
15349
15350 if (nested_name_specifier)
15351 {
15352 tree name;
15353
15354 identifier = cp_parser_identifier (parser);
15355 name = cp_parser_lookup_name (parser, identifier,
15356 enum_type,
15357 /*is_template=*/false,
15358 /*is_namespace=*/false,
15359 /*check_dependency=*/true,
15360 /*ambiguous_decls=*/NULL,
15361 input_location);
15362 if (name && name != error_mark_node)
15363 {
15364 type = TREE_TYPE (name);
15365 if (TREE_CODE (type) == TYPENAME_TYPE)
15366 {
15367 /* Are template enums allowed in ISO? */
15368 if (template_parm_scope_p ())
15369 pedwarn (type_start_token->location, OPT_Wpedantic,
15370 "%qD is an enumeration template", name);
15371 /* ignore a typename reference, for it will be solved by name
15372 in start_enum. */
15373 type = NULL_TREE;
15374 }
15375 }
15376 else if (nested_name_specifier == error_mark_node)
15377 /* We already issued an error. */;
15378 else
15379 error_at (type_start_token->location,
15380 "%qD is not an enumerator-name", identifier);
15381 }
15382 else
15383 {
15384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15385 identifier = cp_parser_identifier (parser);
15386 else
15387 {
15388 identifier = make_anon_name ();
15389 is_anonymous = true;
15390 if (scoped_enum_p)
15391 error_at (type_start_token->location,
15392 "anonymous scoped enum is not allowed");
15393 }
15394 }
15395 pop_deferring_access_checks ();
15396
15397 /* Check for the `:' that denotes a specified underlying type in C++0x.
15398 Note that a ':' could also indicate a bitfield width, however. */
15399 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15400 {
15401 cp_decl_specifier_seq type_specifiers;
15402
15403 /* Consume the `:'. */
15404 cp_lexer_consume_token (parser->lexer);
15405
15406 /* Parse the type-specifier-seq. */
15407 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15408 /*is_trailing_return=*/false,
15409 &type_specifiers);
15410
15411 /* At this point this is surely not elaborated type specifier. */
15412 if (!cp_parser_parse_definitely (parser))
15413 return NULL_TREE;
15414
15415 if (cxx_dialect < cxx11)
15416 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15417
15418 has_underlying_type = true;
15419
15420 /* If that didn't work, stop. */
15421 if (type_specifiers.type != error_mark_node)
15422 {
15423 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15424 /*initialized=*/0, NULL);
15425 if (underlying_type == error_mark_node
15426 || check_for_bare_parameter_packs (underlying_type))
15427 underlying_type = NULL_TREE;
15428 }
15429 }
15430
15431 /* Look for the `{' but don't consume it yet. */
15432 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15433 {
15434 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15435 {
15436 cp_parser_error (parser, "expected %<{%>");
15437 if (has_underlying_type)
15438 {
15439 type = NULL_TREE;
15440 goto out;
15441 }
15442 }
15443 /* An opaque-enum-specifier must have a ';' here. */
15444 if ((scoped_enum_p || underlying_type)
15445 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15446 {
15447 cp_parser_error (parser, "expected %<;%> or %<{%>");
15448 if (has_underlying_type)
15449 {
15450 type = NULL_TREE;
15451 goto out;
15452 }
15453 }
15454 }
15455
15456 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15457 return NULL_TREE;
15458
15459 if (nested_name_specifier)
15460 {
15461 if (CLASS_TYPE_P (nested_name_specifier))
15462 {
15463 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15464 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15465 push_scope (nested_name_specifier);
15466 }
15467 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15468 {
15469 push_nested_namespace (nested_name_specifier);
15470 }
15471 }
15472
15473 /* Issue an error message if type-definitions are forbidden here. */
15474 if (!cp_parser_check_type_definition (parser))
15475 type = error_mark_node;
15476 else
15477 /* Create the new type. We do this before consuming the opening
15478 brace so the enum will be recorded as being on the line of its
15479 tag (or the 'enum' keyword, if there is no tag). */
15480 type = start_enum (identifier, type, underlying_type,
15481 scoped_enum_p, &is_new_type);
15482
15483 /* If the next token is not '{' it is an opaque-enum-specifier or an
15484 elaborated-type-specifier. */
15485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15486 {
15487 timevar_push (TV_PARSE_ENUM);
15488 if (nested_name_specifier
15489 && nested_name_specifier != error_mark_node)
15490 {
15491 /* The following catches invalid code such as:
15492 enum class S<int>::E { A, B, C }; */
15493 if (!processing_specialization
15494 && CLASS_TYPE_P (nested_name_specifier)
15495 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15496 error_at (type_start_token->location, "cannot add an enumerator "
15497 "list to a template instantiation");
15498
15499 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15500 {
15501 error_at (type_start_token->location,
15502 "%<%T::%E%> has not been declared",
15503 TYPE_CONTEXT (nested_name_specifier),
15504 nested_name_specifier);
15505 type = error_mark_node;
15506 }
15507 /* If that scope does not contain the scope in which the
15508 class was originally declared, the program is invalid. */
15509 else if (prev_scope && !is_ancestor (prev_scope,
15510 nested_name_specifier))
15511 {
15512 if (at_namespace_scope_p ())
15513 error_at (type_start_token->location,
15514 "declaration of %qD in namespace %qD which does not "
15515 "enclose %qD",
15516 type, prev_scope, nested_name_specifier);
15517 else
15518 error_at (type_start_token->location,
15519 "declaration of %qD in %qD which does not "
15520 "enclose %qD",
15521 type, prev_scope, nested_name_specifier);
15522 type = error_mark_node;
15523 }
15524 }
15525
15526 if (scoped_enum_p)
15527 begin_scope (sk_scoped_enum, type);
15528
15529 /* Consume the opening brace. */
15530 cp_lexer_consume_token (parser->lexer);
15531
15532 if (type == error_mark_node)
15533 ; /* Nothing to add */
15534 else if (OPAQUE_ENUM_P (type)
15535 || (cxx_dialect > cxx98 && processing_specialization))
15536 {
15537 new_value_list = true;
15538 SET_OPAQUE_ENUM_P (type, false);
15539 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15540 }
15541 else
15542 {
15543 error_at (type_start_token->location, "multiple definition of %q#T", type);
15544 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15545 "previous definition here");
15546 type = error_mark_node;
15547 }
15548
15549 if (type == error_mark_node)
15550 cp_parser_skip_to_end_of_block_or_statement (parser);
15551 /* If the next token is not '}', then there are some enumerators. */
15552 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15553 {
15554 if (is_anonymous && !scoped_enum_p)
15555 pedwarn (type_start_token->location, OPT_Wpedantic,
15556 "ISO C++ forbids empty anonymous enum");
15557 }
15558 else
15559 cp_parser_enumerator_list (parser, type);
15560
15561 /* Consume the final '}'. */
15562 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15563
15564 if (scoped_enum_p)
15565 finish_scope ();
15566 timevar_pop (TV_PARSE_ENUM);
15567 }
15568 else
15569 {
15570 /* If a ';' follows, then it is an opaque-enum-specifier
15571 and additional restrictions apply. */
15572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15573 {
15574 if (is_anonymous)
15575 error_at (type_start_token->location,
15576 "opaque-enum-specifier without name");
15577 else if (nested_name_specifier)
15578 error_at (type_start_token->location,
15579 "opaque-enum-specifier must use a simple identifier");
15580 }
15581 }
15582
15583 /* Look for trailing attributes to apply to this enumeration, and
15584 apply them if appropriate. */
15585 if (cp_parser_allow_gnu_extensions_p (parser))
15586 {
15587 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15588 trailing_attr = chainon (trailing_attr, attributes);
15589 cplus_decl_attributes (&type,
15590 trailing_attr,
15591 (int) ATTR_FLAG_TYPE_IN_PLACE);
15592 }
15593
15594 /* Finish up the enumeration. */
15595 if (type != error_mark_node)
15596 {
15597 if (new_value_list)
15598 finish_enum_value_list (type);
15599 if (is_new_type)
15600 finish_enum (type);
15601 }
15602
15603 if (nested_name_specifier)
15604 {
15605 if (CLASS_TYPE_P (nested_name_specifier))
15606 {
15607 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15608 pop_scope (nested_name_specifier);
15609 }
15610 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15611 {
15612 pop_nested_namespace (nested_name_specifier);
15613 }
15614 }
15615 out:
15616 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15617 return type;
15618 }
15619
15620 /* Parse an enumerator-list. The enumerators all have the indicated
15621 TYPE.
15622
15623 enumerator-list:
15624 enumerator-definition
15625 enumerator-list , enumerator-definition */
15626
15627 static void
15628 cp_parser_enumerator_list (cp_parser* parser, tree type)
15629 {
15630 while (true)
15631 {
15632 /* Parse an enumerator-definition. */
15633 cp_parser_enumerator_definition (parser, type);
15634
15635 /* If the next token is not a ',', we've reached the end of
15636 the list. */
15637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15638 break;
15639 /* Otherwise, consume the `,' and keep going. */
15640 cp_lexer_consume_token (parser->lexer);
15641 /* If the next token is a `}', there is a trailing comma. */
15642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15643 {
15644 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15645 pedwarn (input_location, OPT_Wpedantic,
15646 "comma at end of enumerator list");
15647 break;
15648 }
15649 }
15650 }
15651
15652 /* Parse an enumerator-definition. The enumerator has the indicated
15653 TYPE.
15654
15655 enumerator-definition:
15656 enumerator
15657 enumerator = constant-expression
15658
15659 enumerator:
15660 identifier */
15661
15662 static void
15663 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15664 {
15665 tree identifier;
15666 tree value;
15667 location_t loc;
15668
15669 /* Save the input location because we are interested in the location
15670 of the identifier and not the location of the explicit value. */
15671 loc = cp_lexer_peek_token (parser->lexer)->location;
15672
15673 /* Look for the identifier. */
15674 identifier = cp_parser_identifier (parser);
15675 if (identifier == error_mark_node)
15676 return;
15677
15678 /* If the next token is an '=', then there is an explicit value. */
15679 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15680 {
15681 /* Consume the `=' token. */
15682 cp_lexer_consume_token (parser->lexer);
15683 /* Parse the value. */
15684 value = cp_parser_constant_expression (parser,
15685 /*allow_non_constant_p=*/false,
15686 NULL);
15687 }
15688 else
15689 value = NULL_TREE;
15690
15691 /* If we are processing a template, make sure the initializer of the
15692 enumerator doesn't contain any bare template parameter pack. */
15693 if (check_for_bare_parameter_packs (value))
15694 value = error_mark_node;
15695
15696 /* integral_constant_value will pull out this expression, so make sure
15697 it's folded as appropriate. */
15698 value = fold_non_dependent_expr (value);
15699
15700 /* Create the enumerator. */
15701 build_enumerator (identifier, value, type, loc);
15702 }
15703
15704 /* Parse a namespace-name.
15705
15706 namespace-name:
15707 original-namespace-name
15708 namespace-alias
15709
15710 Returns the NAMESPACE_DECL for the namespace. */
15711
15712 static tree
15713 cp_parser_namespace_name (cp_parser* parser)
15714 {
15715 tree identifier;
15716 tree namespace_decl;
15717
15718 cp_token *token = cp_lexer_peek_token (parser->lexer);
15719
15720 /* Get the name of the namespace. */
15721 identifier = cp_parser_identifier (parser);
15722 if (identifier == error_mark_node)
15723 return error_mark_node;
15724
15725 /* Look up the identifier in the currently active scope. Look only
15726 for namespaces, due to:
15727
15728 [basic.lookup.udir]
15729
15730 When looking up a namespace-name in a using-directive or alias
15731 definition, only namespace names are considered.
15732
15733 And:
15734
15735 [basic.lookup.qual]
15736
15737 During the lookup of a name preceding the :: scope resolution
15738 operator, object, function, and enumerator names are ignored.
15739
15740 (Note that cp_parser_qualifying_entity only calls this
15741 function if the token after the name is the scope resolution
15742 operator.) */
15743 namespace_decl = cp_parser_lookup_name (parser, identifier,
15744 none_type,
15745 /*is_template=*/false,
15746 /*is_namespace=*/true,
15747 /*check_dependency=*/true,
15748 /*ambiguous_decls=*/NULL,
15749 token->location);
15750 /* If it's not a namespace, issue an error. */
15751 if (namespace_decl == error_mark_node
15752 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15753 {
15754 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15755 error_at (token->location, "%qD is not a namespace-name", identifier);
15756 cp_parser_error (parser, "expected namespace-name");
15757 namespace_decl = error_mark_node;
15758 }
15759
15760 return namespace_decl;
15761 }
15762
15763 /* Parse a namespace-definition.
15764
15765 namespace-definition:
15766 named-namespace-definition
15767 unnamed-namespace-definition
15768
15769 named-namespace-definition:
15770 original-namespace-definition
15771 extension-namespace-definition
15772
15773 original-namespace-definition:
15774 namespace identifier { namespace-body }
15775
15776 extension-namespace-definition:
15777 namespace original-namespace-name { namespace-body }
15778
15779 unnamed-namespace-definition:
15780 namespace { namespace-body } */
15781
15782 static void
15783 cp_parser_namespace_definition (cp_parser* parser)
15784 {
15785 tree identifier, attribs;
15786 bool has_visibility;
15787 bool is_inline;
15788
15789 cp_ensure_no_omp_declare_simd (parser);
15790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15791 {
15792 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15793 is_inline = true;
15794 cp_lexer_consume_token (parser->lexer);
15795 }
15796 else
15797 is_inline = false;
15798
15799 /* Look for the `namespace' keyword. */
15800 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15801
15802 /* Get the name of the namespace. We do not attempt to distinguish
15803 between an original-namespace-definition and an
15804 extension-namespace-definition at this point. The semantic
15805 analysis routines are responsible for that. */
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15807 identifier = cp_parser_identifier (parser);
15808 else
15809 identifier = NULL_TREE;
15810
15811 /* Parse any specified attributes. */
15812 attribs = cp_parser_attributes_opt (parser);
15813
15814 /* Look for the `{' to start the namespace. */
15815 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15816 /* Start the namespace. */
15817 push_namespace (identifier);
15818
15819 /* "inline namespace" is equivalent to a stub namespace definition
15820 followed by a strong using directive. */
15821 if (is_inline)
15822 {
15823 tree name_space = current_namespace;
15824 /* Set up namespace association. */
15825 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15826 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15827 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15828 /* Import the contents of the inline namespace. */
15829 pop_namespace ();
15830 do_using_directive (name_space);
15831 push_namespace (identifier);
15832 }
15833
15834 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15835
15836 /* Parse the body of the namespace. */
15837 cp_parser_namespace_body (parser);
15838
15839 if (has_visibility)
15840 pop_visibility (1);
15841
15842 /* Finish the namespace. */
15843 pop_namespace ();
15844 /* Look for the final `}'. */
15845 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15846 }
15847
15848 /* Parse a namespace-body.
15849
15850 namespace-body:
15851 declaration-seq [opt] */
15852
15853 static void
15854 cp_parser_namespace_body (cp_parser* parser)
15855 {
15856 cp_parser_declaration_seq_opt (parser);
15857 }
15858
15859 /* Parse a namespace-alias-definition.
15860
15861 namespace-alias-definition:
15862 namespace identifier = qualified-namespace-specifier ; */
15863
15864 static void
15865 cp_parser_namespace_alias_definition (cp_parser* parser)
15866 {
15867 tree identifier;
15868 tree namespace_specifier;
15869
15870 cp_token *token = cp_lexer_peek_token (parser->lexer);
15871
15872 /* Look for the `namespace' keyword. */
15873 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15874 /* Look for the identifier. */
15875 identifier = cp_parser_identifier (parser);
15876 if (identifier == error_mark_node)
15877 return;
15878 /* Look for the `=' token. */
15879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15880 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15881 {
15882 error_at (token->location, "%<namespace%> definition is not allowed here");
15883 /* Skip the definition. */
15884 cp_lexer_consume_token (parser->lexer);
15885 if (cp_parser_skip_to_closing_brace (parser))
15886 cp_lexer_consume_token (parser->lexer);
15887 return;
15888 }
15889 cp_parser_require (parser, CPP_EQ, RT_EQ);
15890 /* Look for the qualified-namespace-specifier. */
15891 namespace_specifier
15892 = cp_parser_qualified_namespace_specifier (parser);
15893 /* Look for the `;' token. */
15894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15895
15896 /* Register the alias in the symbol table. */
15897 do_namespace_alias (identifier, namespace_specifier);
15898 }
15899
15900 /* Parse a qualified-namespace-specifier.
15901
15902 qualified-namespace-specifier:
15903 :: [opt] nested-name-specifier [opt] namespace-name
15904
15905 Returns a NAMESPACE_DECL corresponding to the specified
15906 namespace. */
15907
15908 static tree
15909 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15910 {
15911 /* Look for the optional `::'. */
15912 cp_parser_global_scope_opt (parser,
15913 /*current_scope_valid_p=*/false);
15914
15915 /* Look for the optional nested-name-specifier. */
15916 cp_parser_nested_name_specifier_opt (parser,
15917 /*typename_keyword_p=*/false,
15918 /*check_dependency_p=*/true,
15919 /*type_p=*/false,
15920 /*is_declaration=*/true);
15921
15922 return cp_parser_namespace_name (parser);
15923 }
15924
15925 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15926 access declaration.
15927
15928 using-declaration:
15929 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15930 using :: unqualified-id ;
15931
15932 access-declaration:
15933 qualified-id ;
15934
15935 */
15936
15937 static bool
15938 cp_parser_using_declaration (cp_parser* parser,
15939 bool access_declaration_p)
15940 {
15941 cp_token *token;
15942 bool typename_p = false;
15943 bool global_scope_p;
15944 tree decl;
15945 tree identifier;
15946 tree qscope;
15947 int oldcount = errorcount;
15948 cp_token *diag_token = NULL;
15949
15950 if (access_declaration_p)
15951 {
15952 diag_token = cp_lexer_peek_token (parser->lexer);
15953 cp_parser_parse_tentatively (parser);
15954 }
15955 else
15956 {
15957 /* Look for the `using' keyword. */
15958 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15959
15960 /* Peek at the next token. */
15961 token = cp_lexer_peek_token (parser->lexer);
15962 /* See if it's `typename'. */
15963 if (token->keyword == RID_TYPENAME)
15964 {
15965 /* Remember that we've seen it. */
15966 typename_p = true;
15967 /* Consume the `typename' token. */
15968 cp_lexer_consume_token (parser->lexer);
15969 }
15970 }
15971
15972 /* Look for the optional global scope qualification. */
15973 global_scope_p
15974 = (cp_parser_global_scope_opt (parser,
15975 /*current_scope_valid_p=*/false)
15976 != NULL_TREE);
15977
15978 /* If we saw `typename', or didn't see `::', then there must be a
15979 nested-name-specifier present. */
15980 if (typename_p || !global_scope_p)
15981 {
15982 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15983 /*check_dependency_p=*/true,
15984 /*type_p=*/false,
15985 /*is_declaration=*/true);
15986 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15987 {
15988 cp_parser_skip_to_end_of_block_or_statement (parser);
15989 return false;
15990 }
15991 }
15992 /* Otherwise, we could be in either of the two productions. In that
15993 case, treat the nested-name-specifier as optional. */
15994 else
15995 qscope = cp_parser_nested_name_specifier_opt (parser,
15996 /*typename_keyword_p=*/false,
15997 /*check_dependency_p=*/true,
15998 /*type_p=*/false,
15999 /*is_declaration=*/true);
16000 if (!qscope)
16001 qscope = global_namespace;
16002
16003 if (access_declaration_p && cp_parser_error_occurred (parser))
16004 /* Something has already gone wrong; there's no need to parse
16005 further. Since an error has occurred, the return value of
16006 cp_parser_parse_definitely will be false, as required. */
16007 return cp_parser_parse_definitely (parser);
16008
16009 token = cp_lexer_peek_token (parser->lexer);
16010 /* Parse the unqualified-id. */
16011 identifier = cp_parser_unqualified_id (parser,
16012 /*template_keyword_p=*/false,
16013 /*check_dependency_p=*/true,
16014 /*declarator_p=*/true,
16015 /*optional_p=*/false);
16016
16017 if (access_declaration_p)
16018 {
16019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16020 cp_parser_simulate_error (parser);
16021 if (!cp_parser_parse_definitely (parser))
16022 return false;
16023 }
16024
16025 /* The function we call to handle a using-declaration is different
16026 depending on what scope we are in. */
16027 if (qscope == error_mark_node || identifier == error_mark_node)
16028 ;
16029 else if (!identifier_p (identifier)
16030 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16031 /* [namespace.udecl]
16032
16033 A using declaration shall not name a template-id. */
16034 error_at (token->location,
16035 "a template-id may not appear in a using-declaration");
16036 else
16037 {
16038 if (at_class_scope_p ())
16039 {
16040 /* Create the USING_DECL. */
16041 decl = do_class_using_decl (parser->scope, identifier);
16042
16043 if (decl && typename_p)
16044 USING_DECL_TYPENAME_P (decl) = 1;
16045
16046 if (check_for_bare_parameter_packs (decl))
16047 {
16048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16049 return false;
16050 }
16051 else
16052 /* Add it to the list of members in this class. */
16053 finish_member_declaration (decl);
16054 }
16055 else
16056 {
16057 decl = cp_parser_lookup_name_simple (parser,
16058 identifier,
16059 token->location);
16060 if (decl == error_mark_node)
16061 cp_parser_name_lookup_error (parser, identifier,
16062 decl, NLE_NULL,
16063 token->location);
16064 else if (check_for_bare_parameter_packs (decl))
16065 {
16066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16067 return false;
16068 }
16069 else if (!at_namespace_scope_p ())
16070 do_local_using_decl (decl, qscope, identifier);
16071 else
16072 do_toplevel_using_decl (decl, qscope, identifier);
16073 }
16074 }
16075
16076 /* Look for the final `;'. */
16077 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16078
16079 if (access_declaration_p && errorcount == oldcount)
16080 warning_at (diag_token->location, OPT_Wdeprecated,
16081 "access declarations are deprecated "
16082 "in favour of using-declarations; "
16083 "suggestion: add the %<using%> keyword");
16084
16085 return true;
16086 }
16087
16088 /* Parse an alias-declaration.
16089
16090 alias-declaration:
16091 using identifier attribute-specifier-seq [opt] = type-id */
16092
16093 static tree
16094 cp_parser_alias_declaration (cp_parser* parser)
16095 {
16096 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16097 location_t id_location;
16098 cp_declarator *declarator;
16099 cp_decl_specifier_seq decl_specs;
16100 bool member_p;
16101 const char *saved_message = NULL;
16102
16103 /* Look for the `using' keyword. */
16104 cp_token *using_token
16105 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16106 if (using_token == NULL)
16107 return error_mark_node;
16108
16109 id_location = cp_lexer_peek_token (parser->lexer)->location;
16110 id = cp_parser_identifier (parser);
16111 if (id == error_mark_node)
16112 return error_mark_node;
16113
16114 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16115 attributes = cp_parser_attributes_opt (parser);
16116 if (attributes == error_mark_node)
16117 return error_mark_node;
16118
16119 cp_parser_require (parser, CPP_EQ, RT_EQ);
16120
16121 if (cp_parser_error_occurred (parser))
16122 return error_mark_node;
16123
16124 cp_parser_commit_to_tentative_parse (parser);
16125
16126 /* Now we are going to parse the type-id of the declaration. */
16127
16128 /*
16129 [dcl.type]/3 says:
16130
16131 "A type-specifier-seq shall not define a class or enumeration
16132 unless it appears in the type-id of an alias-declaration (7.1.3) that
16133 is not the declaration of a template-declaration."
16134
16135 In other words, if we currently are in an alias template, the
16136 type-id should not define a type.
16137
16138 So let's set parser->type_definition_forbidden_message in that
16139 case; cp_parser_check_type_definition (called by
16140 cp_parser_class_specifier) will then emit an error if a type is
16141 defined in the type-id. */
16142 if (parser->num_template_parameter_lists)
16143 {
16144 saved_message = parser->type_definition_forbidden_message;
16145 parser->type_definition_forbidden_message =
16146 G_("types may not be defined in alias template declarations");
16147 }
16148
16149 type = cp_parser_type_id (parser);
16150
16151 /* Restore the error message if need be. */
16152 if (parser->num_template_parameter_lists)
16153 parser->type_definition_forbidden_message = saved_message;
16154
16155 if (type == error_mark_node
16156 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16157 {
16158 cp_parser_skip_to_end_of_block_or_statement (parser);
16159 return error_mark_node;
16160 }
16161
16162 /* A typedef-name can also be introduced by an alias-declaration. The
16163 identifier following the using keyword becomes a typedef-name. It has
16164 the same semantics as if it were introduced by the typedef
16165 specifier. In particular, it does not define a new type and it shall
16166 not appear in the type-id. */
16167
16168 clear_decl_specs (&decl_specs);
16169 decl_specs.type = type;
16170 if (attributes != NULL_TREE)
16171 {
16172 decl_specs.attributes = attributes;
16173 set_and_check_decl_spec_loc (&decl_specs,
16174 ds_attribute,
16175 attrs_token);
16176 }
16177 set_and_check_decl_spec_loc (&decl_specs,
16178 ds_typedef,
16179 using_token);
16180 set_and_check_decl_spec_loc (&decl_specs,
16181 ds_alias,
16182 using_token);
16183
16184 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16185 declarator->id_loc = id_location;
16186
16187 member_p = at_class_scope_p ();
16188 if (member_p)
16189 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16190 NULL_TREE, attributes);
16191 else
16192 decl = start_decl (declarator, &decl_specs, 0,
16193 attributes, NULL_TREE, &pushed_scope);
16194 if (decl == error_mark_node)
16195 return decl;
16196
16197 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16198
16199 if (pushed_scope)
16200 pop_scope (pushed_scope);
16201
16202 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16203 added into the symbol table; otherwise, return the TYPE_DECL. */
16204 if (DECL_LANG_SPECIFIC (decl)
16205 && DECL_TEMPLATE_INFO (decl)
16206 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16207 {
16208 decl = DECL_TI_TEMPLATE (decl);
16209 if (member_p)
16210 check_member_template (decl);
16211 }
16212
16213 return decl;
16214 }
16215
16216 /* Parse a using-directive.
16217
16218 using-directive:
16219 using namespace :: [opt] nested-name-specifier [opt]
16220 namespace-name ; */
16221
16222 static void
16223 cp_parser_using_directive (cp_parser* parser)
16224 {
16225 tree namespace_decl;
16226 tree attribs;
16227
16228 /* Look for the `using' keyword. */
16229 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16230 /* And the `namespace' keyword. */
16231 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16232 /* Look for the optional `::' operator. */
16233 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16234 /* And the optional nested-name-specifier. */
16235 cp_parser_nested_name_specifier_opt (parser,
16236 /*typename_keyword_p=*/false,
16237 /*check_dependency_p=*/true,
16238 /*type_p=*/false,
16239 /*is_declaration=*/true);
16240 /* Get the namespace being used. */
16241 namespace_decl = cp_parser_namespace_name (parser);
16242 /* And any specified attributes. */
16243 attribs = cp_parser_attributes_opt (parser);
16244 /* Update the symbol table. */
16245 parse_using_directive (namespace_decl, attribs);
16246 /* Look for the final `;'. */
16247 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16248 }
16249
16250 /* Parse an asm-definition.
16251
16252 asm-definition:
16253 asm ( string-literal ) ;
16254
16255 GNU Extension:
16256
16257 asm-definition:
16258 asm volatile [opt] ( string-literal ) ;
16259 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16260 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16261 : asm-operand-list [opt] ) ;
16262 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16263 : asm-operand-list [opt]
16264 : asm-clobber-list [opt] ) ;
16265 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16266 : asm-clobber-list [opt]
16267 : asm-goto-list ) ; */
16268
16269 static void
16270 cp_parser_asm_definition (cp_parser* parser)
16271 {
16272 tree string;
16273 tree outputs = NULL_TREE;
16274 tree inputs = NULL_TREE;
16275 tree clobbers = NULL_TREE;
16276 tree labels = NULL_TREE;
16277 tree asm_stmt;
16278 bool volatile_p = false;
16279 bool extended_p = false;
16280 bool invalid_inputs_p = false;
16281 bool invalid_outputs_p = false;
16282 bool goto_p = false;
16283 required_token missing = RT_NONE;
16284
16285 /* Look for the `asm' keyword. */
16286 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16287 /* See if the next token is `volatile'. */
16288 if (cp_parser_allow_gnu_extensions_p (parser)
16289 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16290 {
16291 /* Remember that we saw the `volatile' keyword. */
16292 volatile_p = true;
16293 /* Consume the token. */
16294 cp_lexer_consume_token (parser->lexer);
16295 }
16296 if (cp_parser_allow_gnu_extensions_p (parser)
16297 && parser->in_function_body
16298 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16299 {
16300 /* Remember that we saw the `goto' keyword. */
16301 goto_p = true;
16302 /* Consume the token. */
16303 cp_lexer_consume_token (parser->lexer);
16304 }
16305 /* Look for the opening `('. */
16306 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16307 return;
16308 /* Look for the string. */
16309 string = cp_parser_string_literal (parser, false, false);
16310 if (string == error_mark_node)
16311 {
16312 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16313 /*consume_paren=*/true);
16314 return;
16315 }
16316
16317 /* If we're allowing GNU extensions, check for the extended assembly
16318 syntax. Unfortunately, the `:' tokens need not be separated by
16319 a space in C, and so, for compatibility, we tolerate that here
16320 too. Doing that means that we have to treat the `::' operator as
16321 two `:' tokens. */
16322 if (cp_parser_allow_gnu_extensions_p (parser)
16323 && parser->in_function_body
16324 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16325 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16326 {
16327 bool inputs_p = false;
16328 bool clobbers_p = false;
16329 bool labels_p = false;
16330
16331 /* The extended syntax was used. */
16332 extended_p = true;
16333
16334 /* Look for outputs. */
16335 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16336 {
16337 /* Consume the `:'. */
16338 cp_lexer_consume_token (parser->lexer);
16339 /* Parse the output-operands. */
16340 if (cp_lexer_next_token_is_not (parser->lexer,
16341 CPP_COLON)
16342 && cp_lexer_next_token_is_not (parser->lexer,
16343 CPP_SCOPE)
16344 && cp_lexer_next_token_is_not (parser->lexer,
16345 CPP_CLOSE_PAREN)
16346 && !goto_p)
16347 outputs = cp_parser_asm_operand_list (parser);
16348
16349 if (outputs == error_mark_node)
16350 invalid_outputs_p = true;
16351 }
16352 /* If the next token is `::', there are no outputs, and the
16353 next token is the beginning of the inputs. */
16354 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16355 /* The inputs are coming next. */
16356 inputs_p = true;
16357
16358 /* Look for inputs. */
16359 if (inputs_p
16360 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16361 {
16362 /* Consume the `:' or `::'. */
16363 cp_lexer_consume_token (parser->lexer);
16364 /* Parse the output-operands. */
16365 if (cp_lexer_next_token_is_not (parser->lexer,
16366 CPP_COLON)
16367 && cp_lexer_next_token_is_not (parser->lexer,
16368 CPP_SCOPE)
16369 && cp_lexer_next_token_is_not (parser->lexer,
16370 CPP_CLOSE_PAREN))
16371 inputs = cp_parser_asm_operand_list (parser);
16372
16373 if (inputs == error_mark_node)
16374 invalid_inputs_p = true;
16375 }
16376 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16377 /* The clobbers are coming next. */
16378 clobbers_p = true;
16379
16380 /* Look for clobbers. */
16381 if (clobbers_p
16382 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16383 {
16384 clobbers_p = true;
16385 /* Consume the `:' or `::'. */
16386 cp_lexer_consume_token (parser->lexer);
16387 /* Parse the clobbers. */
16388 if (cp_lexer_next_token_is_not (parser->lexer,
16389 CPP_COLON)
16390 && cp_lexer_next_token_is_not (parser->lexer,
16391 CPP_CLOSE_PAREN))
16392 clobbers = cp_parser_asm_clobber_list (parser);
16393 }
16394 else if (goto_p
16395 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16396 /* The labels are coming next. */
16397 labels_p = true;
16398
16399 /* Look for labels. */
16400 if (labels_p
16401 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16402 {
16403 labels_p = true;
16404 /* Consume the `:' or `::'. */
16405 cp_lexer_consume_token (parser->lexer);
16406 /* Parse the labels. */
16407 labels = cp_parser_asm_label_list (parser);
16408 }
16409
16410 if (goto_p && !labels_p)
16411 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16412 }
16413 else if (goto_p)
16414 missing = RT_COLON_SCOPE;
16415
16416 /* Look for the closing `)'. */
16417 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16418 missing ? missing : RT_CLOSE_PAREN))
16419 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16420 /*consume_paren=*/true);
16421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16422
16423 if (!invalid_inputs_p && !invalid_outputs_p)
16424 {
16425 /* Create the ASM_EXPR. */
16426 if (parser->in_function_body)
16427 {
16428 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16429 inputs, clobbers, labels);
16430 /* If the extended syntax was not used, mark the ASM_EXPR. */
16431 if (!extended_p)
16432 {
16433 tree temp = asm_stmt;
16434 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16435 temp = TREE_OPERAND (temp, 0);
16436
16437 ASM_INPUT_P (temp) = 1;
16438 }
16439 }
16440 else
16441 add_asm_node (string);
16442 }
16443 }
16444
16445 /* Declarators [gram.dcl.decl] */
16446
16447 /* Parse an init-declarator.
16448
16449 init-declarator:
16450 declarator initializer [opt]
16451
16452 GNU Extension:
16453
16454 init-declarator:
16455 declarator asm-specification [opt] attributes [opt] initializer [opt]
16456
16457 function-definition:
16458 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16459 function-body
16460 decl-specifier-seq [opt] declarator function-try-block
16461
16462 GNU Extension:
16463
16464 function-definition:
16465 __extension__ function-definition
16466
16467 TM Extension:
16468
16469 function-definition:
16470 decl-specifier-seq [opt] declarator function-transaction-block
16471
16472 The DECL_SPECIFIERS apply to this declarator. Returns a
16473 representation of the entity declared. If MEMBER_P is TRUE, then
16474 this declarator appears in a class scope. The new DECL created by
16475 this declarator is returned.
16476
16477 The CHECKS are access checks that should be performed once we know
16478 what entity is being declared (and, therefore, what classes have
16479 befriended it).
16480
16481 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16482 for a function-definition here as well. If the declarator is a
16483 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16484 be TRUE upon return. By that point, the function-definition will
16485 have been completely parsed.
16486
16487 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16488 is FALSE.
16489
16490 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16491 parsed declaration if it is an uninitialized single declarator not followed
16492 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16493 if present, will not be consumed. If returned, this declarator will be
16494 created with SD_INITIALIZED but will not call cp_finish_decl. */
16495
16496 static tree
16497 cp_parser_init_declarator (cp_parser* parser,
16498 cp_decl_specifier_seq *decl_specifiers,
16499 vec<deferred_access_check, va_gc> *checks,
16500 bool function_definition_allowed_p,
16501 bool member_p,
16502 int declares_class_or_enum,
16503 bool* function_definition_p,
16504 tree* maybe_range_for_decl)
16505 {
16506 cp_token *token = NULL, *asm_spec_start_token = NULL,
16507 *attributes_start_token = NULL;
16508 cp_declarator *declarator;
16509 tree prefix_attributes;
16510 tree attributes = NULL;
16511 tree asm_specification;
16512 tree initializer;
16513 tree decl = NULL_TREE;
16514 tree scope;
16515 int is_initialized;
16516 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16517 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16518 "(...)". */
16519 enum cpp_ttype initialization_kind;
16520 bool is_direct_init = false;
16521 bool is_non_constant_init;
16522 int ctor_dtor_or_conv_p;
16523 bool friend_p;
16524 tree pushed_scope = NULL_TREE;
16525 bool range_for_decl_p = false;
16526 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16527
16528 /* Gather the attributes that were provided with the
16529 decl-specifiers. */
16530 prefix_attributes = decl_specifiers->attributes;
16531
16532 /* Assume that this is not the declarator for a function
16533 definition. */
16534 if (function_definition_p)
16535 *function_definition_p = false;
16536
16537 /* Default arguments are only permitted for function parameters. */
16538 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16539 parser->default_arg_ok_p = false;
16540
16541 /* Defer access checks while parsing the declarator; we cannot know
16542 what names are accessible until we know what is being
16543 declared. */
16544 resume_deferring_access_checks ();
16545
16546 /* Parse the declarator. */
16547 token = cp_lexer_peek_token (parser->lexer);
16548 declarator
16549 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16550 &ctor_dtor_or_conv_p,
16551 /*parenthesized_p=*/NULL,
16552 member_p);
16553 /* Gather up the deferred checks. */
16554 stop_deferring_access_checks ();
16555
16556 parser->default_arg_ok_p = saved_default_arg_ok_p;
16557
16558 /* If the DECLARATOR was erroneous, there's no need to go
16559 further. */
16560 if (declarator == cp_error_declarator)
16561 return error_mark_node;
16562
16563 /* Check that the number of template-parameter-lists is OK. */
16564 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16565 token->location))
16566 return error_mark_node;
16567
16568 if (declares_class_or_enum & 2)
16569 cp_parser_check_for_definition_in_return_type (declarator,
16570 decl_specifiers->type,
16571 decl_specifiers->locations[ds_type_spec]);
16572
16573 /* Figure out what scope the entity declared by the DECLARATOR is
16574 located in. `grokdeclarator' sometimes changes the scope, so
16575 we compute it now. */
16576 scope = get_scope_of_declarator (declarator);
16577
16578 /* Perform any lookups in the declared type which were thought to be
16579 dependent, but are not in the scope of the declarator. */
16580 decl_specifiers->type
16581 = maybe_update_decl_type (decl_specifiers->type, scope);
16582
16583 /* If we're allowing GNU extensions, look for an
16584 asm-specification. */
16585 if (cp_parser_allow_gnu_extensions_p (parser))
16586 {
16587 /* Look for an asm-specification. */
16588 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16589 asm_specification = cp_parser_asm_specification_opt (parser);
16590 }
16591 else
16592 asm_specification = NULL_TREE;
16593
16594 /* Look for attributes. */
16595 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16596 attributes = cp_parser_attributes_opt (parser);
16597
16598 /* Peek at the next token. */
16599 token = cp_lexer_peek_token (parser->lexer);
16600
16601 if (function_declarator_p (declarator))
16602 {
16603 /* Check to see if the token indicates the start of a
16604 function-definition. */
16605 if (cp_parser_token_starts_function_definition_p (token))
16606 {
16607 if (!function_definition_allowed_p)
16608 {
16609 /* If a function-definition should not appear here, issue an
16610 error message. */
16611 cp_parser_error (parser,
16612 "a function-definition is not allowed here");
16613 return error_mark_node;
16614 }
16615
16616 location_t func_brace_location
16617 = cp_lexer_peek_token (parser->lexer)->location;
16618
16619 /* Neither attributes nor an asm-specification are allowed
16620 on a function-definition. */
16621 if (asm_specification)
16622 error_at (asm_spec_start_token->location,
16623 "an asm-specification is not allowed "
16624 "on a function-definition");
16625 if (attributes)
16626 error_at (attributes_start_token->location,
16627 "attributes are not allowed "
16628 "on a function-definition");
16629 /* This is a function-definition. */
16630 *function_definition_p = true;
16631
16632 /* Parse the function definition. */
16633 if (member_p)
16634 decl = cp_parser_save_member_function_body (parser,
16635 decl_specifiers,
16636 declarator,
16637 prefix_attributes);
16638 else
16639 decl =
16640 (cp_parser_function_definition_from_specifiers_and_declarator
16641 (parser, decl_specifiers, prefix_attributes, declarator));
16642
16643 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16644 {
16645 /* This is where the prologue starts... */
16646 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16647 = func_brace_location;
16648 }
16649
16650 return decl;
16651 }
16652 }
16653
16654 /* [dcl.dcl]
16655
16656 Only in function declarations for constructors, destructors, and
16657 type conversions can the decl-specifier-seq be omitted.
16658
16659 We explicitly postpone this check past the point where we handle
16660 function-definitions because we tolerate function-definitions
16661 that are missing their return types in some modes. */
16662 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16663 {
16664 cp_parser_error (parser,
16665 "expected constructor, destructor, or type conversion");
16666 return error_mark_node;
16667 }
16668
16669 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16670 if (token->type == CPP_EQ
16671 || token->type == CPP_OPEN_PAREN
16672 || token->type == CPP_OPEN_BRACE)
16673 {
16674 is_initialized = SD_INITIALIZED;
16675 initialization_kind = token->type;
16676 if (maybe_range_for_decl)
16677 *maybe_range_for_decl = error_mark_node;
16678
16679 if (token->type == CPP_EQ
16680 && function_declarator_p (declarator))
16681 {
16682 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16683 if (t2->keyword == RID_DEFAULT)
16684 is_initialized = SD_DEFAULTED;
16685 else if (t2->keyword == RID_DELETE)
16686 is_initialized = SD_DELETED;
16687 }
16688 }
16689 else
16690 {
16691 /* If the init-declarator isn't initialized and isn't followed by a
16692 `,' or `;', it's not a valid init-declarator. */
16693 if (token->type != CPP_COMMA
16694 && token->type != CPP_SEMICOLON)
16695 {
16696 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16697 range_for_decl_p = true;
16698 else
16699 {
16700 cp_parser_error (parser, "expected initializer");
16701 return error_mark_node;
16702 }
16703 }
16704 is_initialized = SD_UNINITIALIZED;
16705 initialization_kind = CPP_EOF;
16706 }
16707
16708 /* Because start_decl has side-effects, we should only call it if we
16709 know we're going ahead. By this point, we know that we cannot
16710 possibly be looking at any other construct. */
16711 cp_parser_commit_to_tentative_parse (parser);
16712
16713 /* If the decl specifiers were bad, issue an error now that we're
16714 sure this was intended to be a declarator. Then continue
16715 declaring the variable(s), as int, to try to cut down on further
16716 errors. */
16717 if (decl_specifiers->any_specifiers_p
16718 && decl_specifiers->type == error_mark_node)
16719 {
16720 cp_parser_error (parser, "invalid type in declaration");
16721 decl_specifiers->type = integer_type_node;
16722 }
16723
16724 /* Check to see whether or not this declaration is a friend. */
16725 friend_p = cp_parser_friend_p (decl_specifiers);
16726
16727 /* Enter the newly declared entry in the symbol table. If we're
16728 processing a declaration in a class-specifier, we wait until
16729 after processing the initializer. */
16730 if (!member_p)
16731 {
16732 if (parser->in_unbraced_linkage_specification_p)
16733 decl_specifiers->storage_class = sc_extern;
16734 decl = start_decl (declarator, decl_specifiers,
16735 range_for_decl_p? SD_INITIALIZED : is_initialized,
16736 attributes, prefix_attributes, &pushed_scope);
16737 cp_finalize_omp_declare_simd (parser, decl);
16738 /* Adjust location of decl if declarator->id_loc is more appropriate:
16739 set, and decl wasn't merged with another decl, in which case its
16740 location would be different from input_location, and more accurate. */
16741 if (DECL_P (decl)
16742 && declarator->id_loc != UNKNOWN_LOCATION
16743 && DECL_SOURCE_LOCATION (decl) == input_location)
16744 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16745 }
16746 else if (scope)
16747 /* Enter the SCOPE. That way unqualified names appearing in the
16748 initializer will be looked up in SCOPE. */
16749 pushed_scope = push_scope (scope);
16750
16751 /* Perform deferred access control checks, now that we know in which
16752 SCOPE the declared entity resides. */
16753 if (!member_p && decl)
16754 {
16755 tree saved_current_function_decl = NULL_TREE;
16756
16757 /* If the entity being declared is a function, pretend that we
16758 are in its scope. If it is a `friend', it may have access to
16759 things that would not otherwise be accessible. */
16760 if (TREE_CODE (decl) == FUNCTION_DECL)
16761 {
16762 saved_current_function_decl = current_function_decl;
16763 current_function_decl = decl;
16764 }
16765
16766 /* Perform access checks for template parameters. */
16767 cp_parser_perform_template_parameter_access_checks (checks);
16768
16769 /* Perform the access control checks for the declarator and the
16770 decl-specifiers. */
16771 perform_deferred_access_checks (tf_warning_or_error);
16772
16773 /* Restore the saved value. */
16774 if (TREE_CODE (decl) == FUNCTION_DECL)
16775 current_function_decl = saved_current_function_decl;
16776 }
16777
16778 /* Parse the initializer. */
16779 initializer = NULL_TREE;
16780 is_direct_init = false;
16781 is_non_constant_init = true;
16782 if (is_initialized)
16783 {
16784 if (function_declarator_p (declarator))
16785 {
16786 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16787 if (initialization_kind == CPP_EQ)
16788 initializer = cp_parser_pure_specifier (parser);
16789 else
16790 {
16791 /* If the declaration was erroneous, we don't really
16792 know what the user intended, so just silently
16793 consume the initializer. */
16794 if (decl != error_mark_node)
16795 error_at (initializer_start_token->location,
16796 "initializer provided for function");
16797 cp_parser_skip_to_closing_parenthesis (parser,
16798 /*recovering=*/true,
16799 /*or_comma=*/false,
16800 /*consume_paren=*/true);
16801 }
16802 }
16803 else
16804 {
16805 /* We want to record the extra mangling scope for in-class
16806 initializers of class members and initializers of static data
16807 member templates. The former involves deferring
16808 parsing of the initializer until end of class as with default
16809 arguments. So right here we only handle the latter. */
16810 if (!member_p && processing_template_decl)
16811 start_lambda_scope (decl);
16812 initializer = cp_parser_initializer (parser,
16813 &is_direct_init,
16814 &is_non_constant_init);
16815 if (!member_p && processing_template_decl)
16816 finish_lambda_scope ();
16817 if (initializer == error_mark_node)
16818 cp_parser_skip_to_end_of_statement (parser);
16819 }
16820 }
16821
16822 /* The old parser allows attributes to appear after a parenthesized
16823 initializer. Mark Mitchell proposed removing this functionality
16824 on the GCC mailing lists on 2002-08-13. This parser accepts the
16825 attributes -- but ignores them. */
16826 if (cp_parser_allow_gnu_extensions_p (parser)
16827 && initialization_kind == CPP_OPEN_PAREN)
16828 if (cp_parser_attributes_opt (parser))
16829 warning (OPT_Wattributes,
16830 "attributes after parenthesized initializer ignored");
16831
16832 /* A non-template declaration involving a function parameter list containing
16833 an implicit template parameter will have been made into a template. If it
16834 turns out that the resulting declaration is not an actual function then
16835 finish the template declaration here. An error message will already have
16836 been issued. */
16837 if (parser->fully_implicit_function_template_p)
16838 if (!function_declarator_p (declarator))
16839 {
16840 if (pushed_scope)
16841 {
16842 pop_scope (pushed_scope);
16843 pushed_scope = 0;
16844 }
16845 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16846 }
16847
16848 /* For an in-class declaration, use `grokfield' to create the
16849 declaration. */
16850 if (member_p)
16851 {
16852 if (pushed_scope)
16853 {
16854 pop_scope (pushed_scope);
16855 pushed_scope = NULL_TREE;
16856 }
16857 decl = grokfield (declarator, decl_specifiers,
16858 initializer, !is_non_constant_init,
16859 /*asmspec=*/NULL_TREE,
16860 chainon (attributes, prefix_attributes));
16861 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16862 cp_parser_save_default_args (parser, decl);
16863 cp_finalize_omp_declare_simd (parser, decl);
16864 }
16865
16866 /* Finish processing the declaration. But, skip member
16867 declarations. */
16868 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16869 {
16870 cp_finish_decl (decl,
16871 initializer, !is_non_constant_init,
16872 asm_specification,
16873 /* If the initializer is in parentheses, then this is
16874 a direct-initialization, which means that an
16875 `explicit' constructor is OK. Otherwise, an
16876 `explicit' constructor cannot be used. */
16877 ((is_direct_init || !is_initialized)
16878 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16879 }
16880 else if ((cxx_dialect != cxx98) && friend_p
16881 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16882 /* Core issue #226 (C++0x only): A default template-argument
16883 shall not be specified in a friend class template
16884 declaration. */
16885 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16886 /*is_partial=*/false, /*is_friend_decl=*/1);
16887
16888 if (!friend_p && pushed_scope)
16889 pop_scope (pushed_scope);
16890
16891 if (function_declarator_p (declarator)
16892 && parser->fully_implicit_function_template_p)
16893 {
16894 if (member_p)
16895 decl = finish_fully_implicit_template (parser, decl);
16896 else
16897 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16898 }
16899
16900 return decl;
16901 }
16902
16903 /* Parse a declarator.
16904
16905 declarator:
16906 direct-declarator
16907 ptr-operator declarator
16908
16909 abstract-declarator:
16910 ptr-operator abstract-declarator [opt]
16911 direct-abstract-declarator
16912
16913 GNU Extensions:
16914
16915 declarator:
16916 attributes [opt] direct-declarator
16917 attributes [opt] ptr-operator declarator
16918
16919 abstract-declarator:
16920 attributes [opt] ptr-operator abstract-declarator [opt]
16921 attributes [opt] direct-abstract-declarator
16922
16923 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16924 detect constructor, destructor or conversion operators. It is set
16925 to -1 if the declarator is a name, and +1 if it is a
16926 function. Otherwise it is set to zero. Usually you just want to
16927 test for >0, but internally the negative value is used.
16928
16929 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16930 a decl-specifier-seq unless it declares a constructor, destructor,
16931 or conversion. It might seem that we could check this condition in
16932 semantic analysis, rather than parsing, but that makes it difficult
16933 to handle something like `f()'. We want to notice that there are
16934 no decl-specifiers, and therefore realize that this is an
16935 expression, not a declaration.)
16936
16937 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16938 the declarator is a direct-declarator of the form "(...)".
16939
16940 MEMBER_P is true iff this declarator is a member-declarator. */
16941
16942 static cp_declarator *
16943 cp_parser_declarator (cp_parser* parser,
16944 cp_parser_declarator_kind dcl_kind,
16945 int* ctor_dtor_or_conv_p,
16946 bool* parenthesized_p,
16947 bool member_p)
16948 {
16949 cp_declarator *declarator;
16950 enum tree_code code;
16951 cp_cv_quals cv_quals;
16952 tree class_type;
16953 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16954
16955 /* Assume this is not a constructor, destructor, or type-conversion
16956 operator. */
16957 if (ctor_dtor_or_conv_p)
16958 *ctor_dtor_or_conv_p = 0;
16959
16960 if (cp_parser_allow_gnu_extensions_p (parser))
16961 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16962
16963 /* Check for the ptr-operator production. */
16964 cp_parser_parse_tentatively (parser);
16965 /* Parse the ptr-operator. */
16966 code = cp_parser_ptr_operator (parser,
16967 &class_type,
16968 &cv_quals,
16969 &std_attributes);
16970
16971 /* If that worked, then we have a ptr-operator. */
16972 if (cp_parser_parse_definitely (parser))
16973 {
16974 /* If a ptr-operator was found, then this declarator was not
16975 parenthesized. */
16976 if (parenthesized_p)
16977 *parenthesized_p = true;
16978 /* The dependent declarator is optional if we are parsing an
16979 abstract-declarator. */
16980 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16981 cp_parser_parse_tentatively (parser);
16982
16983 /* Parse the dependent declarator. */
16984 declarator = cp_parser_declarator (parser, dcl_kind,
16985 /*ctor_dtor_or_conv_p=*/NULL,
16986 /*parenthesized_p=*/NULL,
16987 /*member_p=*/false);
16988
16989 /* If we are parsing an abstract-declarator, we must handle the
16990 case where the dependent declarator is absent. */
16991 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16992 && !cp_parser_parse_definitely (parser))
16993 declarator = NULL;
16994
16995 declarator = cp_parser_make_indirect_declarator
16996 (code, class_type, cv_quals, declarator, std_attributes);
16997 }
16998 /* Everything else is a direct-declarator. */
16999 else
17000 {
17001 if (parenthesized_p)
17002 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17003 CPP_OPEN_PAREN);
17004 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17005 ctor_dtor_or_conv_p,
17006 member_p);
17007 }
17008
17009 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17010 declarator->attributes = gnu_attributes;
17011 return declarator;
17012 }
17013
17014 /* Parse a direct-declarator or direct-abstract-declarator.
17015
17016 direct-declarator:
17017 declarator-id
17018 direct-declarator ( parameter-declaration-clause )
17019 cv-qualifier-seq [opt]
17020 ref-qualifier [opt]
17021 exception-specification [opt]
17022 direct-declarator [ constant-expression [opt] ]
17023 ( declarator )
17024
17025 direct-abstract-declarator:
17026 direct-abstract-declarator [opt]
17027 ( parameter-declaration-clause )
17028 cv-qualifier-seq [opt]
17029 ref-qualifier [opt]
17030 exception-specification [opt]
17031 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17032 ( abstract-declarator )
17033
17034 Returns a representation of the declarator. DCL_KIND is
17035 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17036 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17037 we are parsing a direct-declarator. It is
17038 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17039 of ambiguity we prefer an abstract declarator, as per
17040 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17041 cp_parser_declarator. */
17042
17043 static cp_declarator *
17044 cp_parser_direct_declarator (cp_parser* parser,
17045 cp_parser_declarator_kind dcl_kind,
17046 int* ctor_dtor_or_conv_p,
17047 bool member_p)
17048 {
17049 cp_token *token;
17050 cp_declarator *declarator = NULL;
17051 tree scope = NULL_TREE;
17052 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17053 bool saved_in_declarator_p = parser->in_declarator_p;
17054 bool first = true;
17055 tree pushed_scope = NULL_TREE;
17056
17057 while (true)
17058 {
17059 /* Peek at the next token. */
17060 token = cp_lexer_peek_token (parser->lexer);
17061 if (token->type == CPP_OPEN_PAREN)
17062 {
17063 /* This is either a parameter-declaration-clause, or a
17064 parenthesized declarator. When we know we are parsing a
17065 named declarator, it must be a parenthesized declarator
17066 if FIRST is true. For instance, `(int)' is a
17067 parameter-declaration-clause, with an omitted
17068 direct-abstract-declarator. But `((*))', is a
17069 parenthesized abstract declarator. Finally, when T is a
17070 template parameter `(T)' is a
17071 parameter-declaration-clause, and not a parenthesized
17072 named declarator.
17073
17074 We first try and parse a parameter-declaration-clause,
17075 and then try a nested declarator (if FIRST is true).
17076
17077 It is not an error for it not to be a
17078 parameter-declaration-clause, even when FIRST is
17079 false. Consider,
17080
17081 int i (int);
17082 int i (3);
17083
17084 The first is the declaration of a function while the
17085 second is the definition of a variable, including its
17086 initializer.
17087
17088 Having seen only the parenthesis, we cannot know which of
17089 these two alternatives should be selected. Even more
17090 complex are examples like:
17091
17092 int i (int (a));
17093 int i (int (3));
17094
17095 The former is a function-declaration; the latter is a
17096 variable initialization.
17097
17098 Thus again, we try a parameter-declaration-clause, and if
17099 that fails, we back out and return. */
17100
17101 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17102 {
17103 tree params;
17104 bool is_declarator = false;
17105
17106 /* In a member-declarator, the only valid interpretation
17107 of a parenthesis is the start of a
17108 parameter-declaration-clause. (It is invalid to
17109 initialize a static data member with a parenthesized
17110 initializer; only the "=" form of initialization is
17111 permitted.) */
17112 if (!member_p)
17113 cp_parser_parse_tentatively (parser);
17114
17115 /* Consume the `('. */
17116 cp_lexer_consume_token (parser->lexer);
17117 if (first)
17118 {
17119 /* If this is going to be an abstract declarator, we're
17120 in a declarator and we can't have default args. */
17121 parser->default_arg_ok_p = false;
17122 parser->in_declarator_p = true;
17123 }
17124
17125 begin_scope (sk_function_parms, NULL_TREE);
17126
17127 /* Parse the parameter-declaration-clause. */
17128 params = cp_parser_parameter_declaration_clause (parser);
17129
17130 /* Consume the `)'. */
17131 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17132
17133 /* If all went well, parse the cv-qualifier-seq,
17134 ref-qualifier and the exception-specification. */
17135 if (member_p || cp_parser_parse_definitely (parser))
17136 {
17137 cp_cv_quals cv_quals;
17138 cp_virt_specifiers virt_specifiers;
17139 cp_ref_qualifier ref_qual;
17140 tree exception_specification;
17141 tree late_return;
17142 tree attrs;
17143 bool memfn = (member_p || (pushed_scope
17144 && CLASS_TYPE_P (pushed_scope)));
17145
17146 is_declarator = true;
17147
17148 if (ctor_dtor_or_conv_p)
17149 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17150 first = false;
17151
17152 /* Parse the cv-qualifier-seq. */
17153 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17154 /* Parse the ref-qualifier. */
17155 ref_qual = cp_parser_ref_qualifier_opt (parser);
17156 /* And the exception-specification. */
17157 exception_specification
17158 = cp_parser_exception_specification_opt (parser);
17159
17160 attrs = cp_parser_std_attribute_spec_seq (parser);
17161
17162 /* In here, we handle cases where attribute is used after
17163 the function declaration. For example:
17164 void func (int x) __attribute__((vector(..))); */
17165 if (flag_cilkplus
17166 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17167 {
17168 cp_parser_parse_tentatively (parser);
17169 tree attr = cp_parser_gnu_attributes_opt (parser);
17170 if (cp_lexer_next_token_is_not (parser->lexer,
17171 CPP_SEMICOLON)
17172 && cp_lexer_next_token_is_not (parser->lexer,
17173 CPP_OPEN_BRACE))
17174 cp_parser_abort_tentative_parse (parser);
17175 else if (!cp_parser_parse_definitely (parser))
17176 ;
17177 else
17178 attrs = chainon (attr, attrs);
17179 }
17180 late_return = (cp_parser_late_return_type_opt
17181 (parser, declarator,
17182 memfn ? cv_quals : -1));
17183
17184
17185 /* Parse the virt-specifier-seq. */
17186 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17187
17188 /* Create the function-declarator. */
17189 declarator = make_call_declarator (declarator,
17190 params,
17191 cv_quals,
17192 virt_specifiers,
17193 ref_qual,
17194 exception_specification,
17195 late_return);
17196 declarator->std_attributes = attrs;
17197 /* Any subsequent parameter lists are to do with
17198 return type, so are not those of the declared
17199 function. */
17200 parser->default_arg_ok_p = false;
17201 }
17202
17203 /* Remove the function parms from scope. */
17204 pop_bindings_and_leave_scope ();
17205
17206 if (is_declarator)
17207 /* Repeat the main loop. */
17208 continue;
17209 }
17210
17211 /* If this is the first, we can try a parenthesized
17212 declarator. */
17213 if (first)
17214 {
17215 bool saved_in_type_id_in_expr_p;
17216
17217 parser->default_arg_ok_p = saved_default_arg_ok_p;
17218 parser->in_declarator_p = saved_in_declarator_p;
17219
17220 /* Consume the `('. */
17221 cp_lexer_consume_token (parser->lexer);
17222 /* Parse the nested declarator. */
17223 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17224 parser->in_type_id_in_expr_p = true;
17225 declarator
17226 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17227 /*parenthesized_p=*/NULL,
17228 member_p);
17229 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17230 first = false;
17231 /* Expect a `)'. */
17232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17233 declarator = cp_error_declarator;
17234 if (declarator == cp_error_declarator)
17235 break;
17236
17237 goto handle_declarator;
17238 }
17239 /* Otherwise, we must be done. */
17240 else
17241 break;
17242 }
17243 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17244 && token->type == CPP_OPEN_SQUARE
17245 && !cp_next_tokens_can_be_attribute_p (parser))
17246 {
17247 /* Parse an array-declarator. */
17248 tree bounds, attrs;
17249
17250 if (ctor_dtor_or_conv_p)
17251 *ctor_dtor_or_conv_p = 0;
17252
17253 first = false;
17254 parser->default_arg_ok_p = false;
17255 parser->in_declarator_p = true;
17256 /* Consume the `['. */
17257 cp_lexer_consume_token (parser->lexer);
17258 /* Peek at the next token. */
17259 token = cp_lexer_peek_token (parser->lexer);
17260 /* If the next token is `]', then there is no
17261 constant-expression. */
17262 if (token->type != CPP_CLOSE_SQUARE)
17263 {
17264 bool non_constant_p;
17265 bounds
17266 = cp_parser_constant_expression (parser,
17267 /*allow_non_constant=*/true,
17268 &non_constant_p);
17269 if (!non_constant_p)
17270 /* OK */;
17271 else if (error_operand_p (bounds))
17272 /* Already gave an error. */;
17273 else if (!parser->in_function_body
17274 || current_binding_level->kind == sk_function_parms)
17275 {
17276 /* Normally, the array bound must be an integral constant
17277 expression. However, as an extension, we allow VLAs
17278 in function scopes as long as they aren't part of a
17279 parameter declaration. */
17280 cp_parser_error (parser,
17281 "array bound is not an integer constant");
17282 bounds = error_mark_node;
17283 }
17284 else if (processing_template_decl
17285 && !type_dependent_expression_p (bounds))
17286 {
17287 /* Remember this wasn't a constant-expression. */
17288 bounds = build_nop (TREE_TYPE (bounds), bounds);
17289 TREE_SIDE_EFFECTS (bounds) = 1;
17290 }
17291 }
17292 else
17293 bounds = NULL_TREE;
17294 /* Look for the closing `]'. */
17295 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17296 {
17297 declarator = cp_error_declarator;
17298 break;
17299 }
17300
17301 attrs = cp_parser_std_attribute_spec_seq (parser);
17302 declarator = make_array_declarator (declarator, bounds);
17303 declarator->std_attributes = attrs;
17304 }
17305 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17306 {
17307 {
17308 tree qualifying_scope;
17309 tree unqualified_name;
17310 tree attrs;
17311 special_function_kind sfk;
17312 bool abstract_ok;
17313 bool pack_expansion_p = false;
17314 cp_token *declarator_id_start_token;
17315
17316 /* Parse a declarator-id */
17317 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17318 if (abstract_ok)
17319 {
17320 cp_parser_parse_tentatively (parser);
17321
17322 /* If we see an ellipsis, we should be looking at a
17323 parameter pack. */
17324 if (token->type == CPP_ELLIPSIS)
17325 {
17326 /* Consume the `...' */
17327 cp_lexer_consume_token (parser->lexer);
17328
17329 pack_expansion_p = true;
17330 }
17331 }
17332
17333 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17334 unqualified_name
17335 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17336 qualifying_scope = parser->scope;
17337 if (abstract_ok)
17338 {
17339 bool okay = false;
17340
17341 if (!unqualified_name && pack_expansion_p)
17342 {
17343 /* Check whether an error occurred. */
17344 okay = !cp_parser_error_occurred (parser);
17345
17346 /* We already consumed the ellipsis to mark a
17347 parameter pack, but we have no way to report it,
17348 so abort the tentative parse. We will be exiting
17349 immediately anyway. */
17350 cp_parser_abort_tentative_parse (parser);
17351 }
17352 else
17353 okay = cp_parser_parse_definitely (parser);
17354
17355 if (!okay)
17356 unqualified_name = error_mark_node;
17357 else if (unqualified_name
17358 && (qualifying_scope
17359 || (!identifier_p (unqualified_name))))
17360 {
17361 cp_parser_error (parser, "expected unqualified-id");
17362 unqualified_name = error_mark_node;
17363 }
17364 }
17365
17366 if (!unqualified_name)
17367 return NULL;
17368 if (unqualified_name == error_mark_node)
17369 {
17370 declarator = cp_error_declarator;
17371 pack_expansion_p = false;
17372 declarator->parameter_pack_p = false;
17373 break;
17374 }
17375
17376 attrs = cp_parser_std_attribute_spec_seq (parser);
17377
17378 if (qualifying_scope && at_namespace_scope_p ()
17379 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17380 {
17381 /* In the declaration of a member of a template class
17382 outside of the class itself, the SCOPE will sometimes
17383 be a TYPENAME_TYPE. For example, given:
17384
17385 template <typename T>
17386 int S<T>::R::i = 3;
17387
17388 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17389 this context, we must resolve S<T>::R to an ordinary
17390 type, rather than a typename type.
17391
17392 The reason we normally avoid resolving TYPENAME_TYPEs
17393 is that a specialization of `S' might render
17394 `S<T>::R' not a type. However, if `S' is
17395 specialized, then this `i' will not be used, so there
17396 is no harm in resolving the types here. */
17397 tree type;
17398
17399 /* Resolve the TYPENAME_TYPE. */
17400 type = resolve_typename_type (qualifying_scope,
17401 /*only_current_p=*/false);
17402 /* If that failed, the declarator is invalid. */
17403 if (TREE_CODE (type) == TYPENAME_TYPE)
17404 {
17405 if (typedef_variant_p (type))
17406 error_at (declarator_id_start_token->location,
17407 "cannot define member of dependent typedef "
17408 "%qT", type);
17409 else
17410 error_at (declarator_id_start_token->location,
17411 "%<%T::%E%> is not a type",
17412 TYPE_CONTEXT (qualifying_scope),
17413 TYPE_IDENTIFIER (qualifying_scope));
17414 }
17415 qualifying_scope = type;
17416 }
17417
17418 sfk = sfk_none;
17419
17420 if (unqualified_name)
17421 {
17422 tree class_type;
17423
17424 if (qualifying_scope
17425 && CLASS_TYPE_P (qualifying_scope))
17426 class_type = qualifying_scope;
17427 else
17428 class_type = current_class_type;
17429
17430 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17431 {
17432 tree name_type = TREE_TYPE (unqualified_name);
17433 if (class_type && same_type_p (name_type, class_type))
17434 {
17435 if (qualifying_scope
17436 && CLASSTYPE_USE_TEMPLATE (name_type))
17437 {
17438 error_at (declarator_id_start_token->location,
17439 "invalid use of constructor as a template");
17440 inform (declarator_id_start_token->location,
17441 "use %<%T::%D%> instead of %<%T::%D%> to "
17442 "name the constructor in a qualified name",
17443 class_type,
17444 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17445 class_type, name_type);
17446 declarator = cp_error_declarator;
17447 break;
17448 }
17449 else
17450 unqualified_name = constructor_name (class_type);
17451 }
17452 else
17453 {
17454 /* We do not attempt to print the declarator
17455 here because we do not have enough
17456 information about its original syntactic
17457 form. */
17458 cp_parser_error (parser, "invalid declarator");
17459 declarator = cp_error_declarator;
17460 break;
17461 }
17462 }
17463
17464 if (class_type)
17465 {
17466 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17467 sfk = sfk_destructor;
17468 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17469 sfk = sfk_conversion;
17470 else if (/* There's no way to declare a constructor
17471 for an anonymous type, even if the type
17472 got a name for linkage purposes. */
17473 !TYPE_WAS_ANONYMOUS (class_type)
17474 && constructor_name_p (unqualified_name,
17475 class_type))
17476 {
17477 unqualified_name = constructor_name (class_type);
17478 sfk = sfk_constructor;
17479 }
17480 else if (is_overloaded_fn (unqualified_name)
17481 && DECL_CONSTRUCTOR_P (get_first_fn
17482 (unqualified_name)))
17483 sfk = sfk_constructor;
17484
17485 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17486 *ctor_dtor_or_conv_p = -1;
17487 }
17488 }
17489 declarator = make_id_declarator (qualifying_scope,
17490 unqualified_name,
17491 sfk);
17492 declarator->std_attributes = attrs;
17493 declarator->id_loc = token->location;
17494 declarator->parameter_pack_p = pack_expansion_p;
17495
17496 if (pack_expansion_p)
17497 maybe_warn_variadic_templates ();
17498 }
17499
17500 handle_declarator:;
17501 scope = get_scope_of_declarator (declarator);
17502 if (scope)
17503 {
17504 /* Any names that appear after the declarator-id for a
17505 member are looked up in the containing scope. */
17506 if (at_function_scope_p ())
17507 {
17508 /* But declarations with qualified-ids can't appear in a
17509 function. */
17510 cp_parser_error (parser, "qualified-id in declaration");
17511 declarator = cp_error_declarator;
17512 break;
17513 }
17514 pushed_scope = push_scope (scope);
17515 }
17516 parser->in_declarator_p = true;
17517 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17518 || (declarator && declarator->kind == cdk_id))
17519 /* Default args are only allowed on function
17520 declarations. */
17521 parser->default_arg_ok_p = saved_default_arg_ok_p;
17522 else
17523 parser->default_arg_ok_p = false;
17524
17525 first = false;
17526 }
17527 /* We're done. */
17528 else
17529 break;
17530 }
17531
17532 /* For an abstract declarator, we might wind up with nothing at this
17533 point. That's an error; the declarator is not optional. */
17534 if (!declarator)
17535 cp_parser_error (parser, "expected declarator");
17536
17537 /* If we entered a scope, we must exit it now. */
17538 if (pushed_scope)
17539 pop_scope (pushed_scope);
17540
17541 parser->default_arg_ok_p = saved_default_arg_ok_p;
17542 parser->in_declarator_p = saved_in_declarator_p;
17543
17544 return declarator;
17545 }
17546
17547 /* Parse a ptr-operator.
17548
17549 ptr-operator:
17550 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17551 * cv-qualifier-seq [opt]
17552 &
17553 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17554 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17555
17556 GNU Extension:
17557
17558 ptr-operator:
17559 & cv-qualifier-seq [opt]
17560
17561 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17562 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17563 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17564 filled in with the TYPE containing the member. *CV_QUALS is
17565 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17566 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17567 Note that the tree codes returned by this function have nothing
17568 to do with the types of trees that will be eventually be created
17569 to represent the pointer or reference type being parsed. They are
17570 just constants with suggestive names. */
17571 static enum tree_code
17572 cp_parser_ptr_operator (cp_parser* parser,
17573 tree* type,
17574 cp_cv_quals *cv_quals,
17575 tree *attributes)
17576 {
17577 enum tree_code code = ERROR_MARK;
17578 cp_token *token;
17579 tree attrs = NULL_TREE;
17580
17581 /* Assume that it's not a pointer-to-member. */
17582 *type = NULL_TREE;
17583 /* And that there are no cv-qualifiers. */
17584 *cv_quals = TYPE_UNQUALIFIED;
17585
17586 /* Peek at the next token. */
17587 token = cp_lexer_peek_token (parser->lexer);
17588
17589 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17590 if (token->type == CPP_MULT)
17591 code = INDIRECT_REF;
17592 else if (token->type == CPP_AND)
17593 code = ADDR_EXPR;
17594 else if ((cxx_dialect != cxx98) &&
17595 token->type == CPP_AND_AND) /* C++0x only */
17596 code = NON_LVALUE_EXPR;
17597
17598 if (code != ERROR_MARK)
17599 {
17600 /* Consume the `*', `&' or `&&'. */
17601 cp_lexer_consume_token (parser->lexer);
17602
17603 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17604 `&', if we are allowing GNU extensions. (The only qualifier
17605 that can legally appear after `&' is `restrict', but that is
17606 enforced during semantic analysis. */
17607 if (code == INDIRECT_REF
17608 || cp_parser_allow_gnu_extensions_p (parser))
17609 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17610
17611 attrs = cp_parser_std_attribute_spec_seq (parser);
17612 if (attributes != NULL)
17613 *attributes = attrs;
17614 }
17615 else
17616 {
17617 /* Try the pointer-to-member case. */
17618 cp_parser_parse_tentatively (parser);
17619 /* Look for the optional `::' operator. */
17620 cp_parser_global_scope_opt (parser,
17621 /*current_scope_valid_p=*/false);
17622 /* Look for the nested-name specifier. */
17623 token = cp_lexer_peek_token (parser->lexer);
17624 cp_parser_nested_name_specifier (parser,
17625 /*typename_keyword_p=*/false,
17626 /*check_dependency_p=*/true,
17627 /*type_p=*/false,
17628 /*is_declaration=*/false);
17629 /* If we found it, and the next token is a `*', then we are
17630 indeed looking at a pointer-to-member operator. */
17631 if (!cp_parser_error_occurred (parser)
17632 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17633 {
17634 /* Indicate that the `*' operator was used. */
17635 code = INDIRECT_REF;
17636
17637 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17638 error_at (token->location, "%qD is a namespace", parser->scope);
17639 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17640 error_at (token->location, "cannot form pointer to member of "
17641 "non-class %q#T", parser->scope);
17642 else
17643 {
17644 /* The type of which the member is a member is given by the
17645 current SCOPE. */
17646 *type = parser->scope;
17647 /* The next name will not be qualified. */
17648 parser->scope = NULL_TREE;
17649 parser->qualifying_scope = NULL_TREE;
17650 parser->object_scope = NULL_TREE;
17651 /* Look for optional c++11 attributes. */
17652 attrs = cp_parser_std_attribute_spec_seq (parser);
17653 if (attributes != NULL)
17654 *attributes = attrs;
17655 /* Look for the optional cv-qualifier-seq. */
17656 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17657 }
17658 }
17659 /* If that didn't work we don't have a ptr-operator. */
17660 if (!cp_parser_parse_definitely (parser))
17661 cp_parser_error (parser, "expected ptr-operator");
17662 }
17663
17664 return code;
17665 }
17666
17667 /* Parse an (optional) cv-qualifier-seq.
17668
17669 cv-qualifier-seq:
17670 cv-qualifier cv-qualifier-seq [opt]
17671
17672 cv-qualifier:
17673 const
17674 volatile
17675
17676 GNU Extension:
17677
17678 cv-qualifier:
17679 __restrict__
17680
17681 Returns a bitmask representing the cv-qualifiers. */
17682
17683 static cp_cv_quals
17684 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17685 {
17686 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17687
17688 while (true)
17689 {
17690 cp_token *token;
17691 cp_cv_quals cv_qualifier;
17692
17693 /* Peek at the next token. */
17694 token = cp_lexer_peek_token (parser->lexer);
17695 /* See if it's a cv-qualifier. */
17696 switch (token->keyword)
17697 {
17698 case RID_CONST:
17699 cv_qualifier = TYPE_QUAL_CONST;
17700 break;
17701
17702 case RID_VOLATILE:
17703 cv_qualifier = TYPE_QUAL_VOLATILE;
17704 break;
17705
17706 case RID_RESTRICT:
17707 cv_qualifier = TYPE_QUAL_RESTRICT;
17708 break;
17709
17710 default:
17711 cv_qualifier = TYPE_UNQUALIFIED;
17712 break;
17713 }
17714
17715 if (!cv_qualifier)
17716 break;
17717
17718 if (cv_quals & cv_qualifier)
17719 {
17720 error_at (token->location, "duplicate cv-qualifier");
17721 cp_lexer_purge_token (parser->lexer);
17722 }
17723 else
17724 {
17725 cp_lexer_consume_token (parser->lexer);
17726 cv_quals |= cv_qualifier;
17727 }
17728 }
17729
17730 return cv_quals;
17731 }
17732
17733 /* Parse an (optional) ref-qualifier
17734
17735 ref-qualifier:
17736 &
17737 &&
17738
17739 Returns cp_ref_qualifier representing ref-qualifier. */
17740
17741 static cp_ref_qualifier
17742 cp_parser_ref_qualifier_opt (cp_parser* parser)
17743 {
17744 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17745
17746 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17747 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17748 return ref_qual;
17749
17750 while (true)
17751 {
17752 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17753 cp_token *token = cp_lexer_peek_token (parser->lexer);
17754
17755 switch (token->type)
17756 {
17757 case CPP_AND:
17758 curr_ref_qual = REF_QUAL_LVALUE;
17759 break;
17760
17761 case CPP_AND_AND:
17762 curr_ref_qual = REF_QUAL_RVALUE;
17763 break;
17764
17765 default:
17766 curr_ref_qual = REF_QUAL_NONE;
17767 break;
17768 }
17769
17770 if (!curr_ref_qual)
17771 break;
17772 else if (ref_qual)
17773 {
17774 error_at (token->location, "multiple ref-qualifiers");
17775 cp_lexer_purge_token (parser->lexer);
17776 }
17777 else
17778 {
17779 ref_qual = curr_ref_qual;
17780 cp_lexer_consume_token (parser->lexer);
17781 }
17782 }
17783
17784 return ref_qual;
17785 }
17786
17787 /* Parse an (optional) virt-specifier-seq.
17788
17789 virt-specifier-seq:
17790 virt-specifier virt-specifier-seq [opt]
17791
17792 virt-specifier:
17793 override
17794 final
17795
17796 Returns a bitmask representing the virt-specifiers. */
17797
17798 static cp_virt_specifiers
17799 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17800 {
17801 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17802
17803 while (true)
17804 {
17805 cp_token *token;
17806 cp_virt_specifiers virt_specifier;
17807
17808 /* Peek at the next token. */
17809 token = cp_lexer_peek_token (parser->lexer);
17810 /* See if it's a virt-specifier-qualifier. */
17811 if (token->type != CPP_NAME)
17812 break;
17813 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17814 {
17815 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17816 virt_specifier = VIRT_SPEC_OVERRIDE;
17817 }
17818 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17819 {
17820 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17821 virt_specifier = VIRT_SPEC_FINAL;
17822 }
17823 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17824 {
17825 virt_specifier = VIRT_SPEC_FINAL;
17826 }
17827 else
17828 break;
17829
17830 if (virt_specifiers & virt_specifier)
17831 {
17832 error_at (token->location, "duplicate virt-specifier");
17833 cp_lexer_purge_token (parser->lexer);
17834 }
17835 else
17836 {
17837 cp_lexer_consume_token (parser->lexer);
17838 virt_specifiers |= virt_specifier;
17839 }
17840 }
17841 return virt_specifiers;
17842 }
17843
17844 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17845 is in scope even though it isn't real. */
17846
17847 void
17848 inject_this_parameter (tree ctype, cp_cv_quals quals)
17849 {
17850 tree this_parm;
17851
17852 if (current_class_ptr)
17853 {
17854 /* We don't clear this between NSDMIs. Is it already what we want? */
17855 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17856 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17857 && cp_type_quals (type) == quals)
17858 return;
17859 }
17860
17861 this_parm = build_this_parm (ctype, quals);
17862 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17863 current_class_ptr = NULL_TREE;
17864 current_class_ref
17865 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17866 current_class_ptr = this_parm;
17867 }
17868
17869 /* Return true iff our current scope is a non-static data member
17870 initializer. */
17871
17872 bool
17873 parsing_nsdmi (void)
17874 {
17875 /* We recognize NSDMI context by the context-less 'this' pointer set up
17876 by the function above. */
17877 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17878 return true;
17879 return false;
17880 }
17881
17882 /* Parse a late-specified return type, if any. This is not a separate
17883 non-terminal, but part of a function declarator, which looks like
17884
17885 -> trailing-type-specifier-seq abstract-declarator(opt)
17886
17887 Returns the type indicated by the type-id.
17888
17889 In addition to this this parses any queued up omp declare simd
17890 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17891
17892 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17893 function. */
17894
17895 static tree
17896 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17897 cp_cv_quals quals)
17898 {
17899 cp_token *token;
17900 tree type = NULL_TREE;
17901 bool declare_simd_p = (parser->omp_declare_simd
17902 && declarator
17903 && declarator->kind == cdk_id);
17904
17905 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17906 && declarator && declarator->kind == cdk_id);
17907
17908 /* Peek at the next token. */
17909 token = cp_lexer_peek_token (parser->lexer);
17910 /* A late-specified return type is indicated by an initial '->'. */
17911 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17912 return NULL_TREE;
17913
17914 tree save_ccp = current_class_ptr;
17915 tree save_ccr = current_class_ref;
17916 if (quals >= 0)
17917 {
17918 /* DR 1207: 'this' is in scope in the trailing return type. */
17919 inject_this_parameter (current_class_type, quals);
17920 }
17921
17922 if (token->type == CPP_DEREF)
17923 {
17924 /* Consume the ->. */
17925 cp_lexer_consume_token (parser->lexer);
17926
17927 type = cp_parser_trailing_type_id (parser);
17928 }
17929
17930 if (cilk_simd_fn_vector_p)
17931 declarator->std_attributes
17932 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17933 declarator->std_attributes);
17934 if (declare_simd_p)
17935 declarator->std_attributes
17936 = cp_parser_late_parsing_omp_declare_simd (parser,
17937 declarator->std_attributes);
17938
17939 if (quals >= 0)
17940 {
17941 current_class_ptr = save_ccp;
17942 current_class_ref = save_ccr;
17943 }
17944
17945 return type;
17946 }
17947
17948 /* Parse a declarator-id.
17949
17950 declarator-id:
17951 id-expression
17952 :: [opt] nested-name-specifier [opt] type-name
17953
17954 In the `id-expression' case, the value returned is as for
17955 cp_parser_id_expression if the id-expression was an unqualified-id.
17956 If the id-expression was a qualified-id, then a SCOPE_REF is
17957 returned. The first operand is the scope (either a NAMESPACE_DECL
17958 or TREE_TYPE), but the second is still just a representation of an
17959 unqualified-id. */
17960
17961 static tree
17962 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17963 {
17964 tree id;
17965 /* The expression must be an id-expression. Assume that qualified
17966 names are the names of types so that:
17967
17968 template <class T>
17969 int S<T>::R::i = 3;
17970
17971 will work; we must treat `S<T>::R' as the name of a type.
17972 Similarly, assume that qualified names are templates, where
17973 required, so that:
17974
17975 template <class T>
17976 int S<T>::R<T>::i = 3;
17977
17978 will work, too. */
17979 id = cp_parser_id_expression (parser,
17980 /*template_keyword_p=*/false,
17981 /*check_dependency_p=*/false,
17982 /*template_p=*/NULL,
17983 /*declarator_p=*/true,
17984 optional_p);
17985 if (id && BASELINK_P (id))
17986 id = BASELINK_FUNCTIONS (id);
17987 return id;
17988 }
17989
17990 /* Parse a type-id.
17991
17992 type-id:
17993 type-specifier-seq abstract-declarator [opt]
17994
17995 Returns the TYPE specified. */
17996
17997 static tree
17998 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17999 bool is_trailing_return)
18000 {
18001 cp_decl_specifier_seq type_specifier_seq;
18002 cp_declarator *abstract_declarator;
18003
18004 /* Parse the type-specifier-seq. */
18005 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18006 is_trailing_return,
18007 &type_specifier_seq);
18008 if (type_specifier_seq.type == error_mark_node)
18009 return error_mark_node;
18010
18011 /* There might or might not be an abstract declarator. */
18012 cp_parser_parse_tentatively (parser);
18013 /* Look for the declarator. */
18014 abstract_declarator
18015 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18016 /*parenthesized_p=*/NULL,
18017 /*member_p=*/false);
18018 /* Check to see if there really was a declarator. */
18019 if (!cp_parser_parse_definitely (parser))
18020 abstract_declarator = NULL;
18021
18022 if (type_specifier_seq.type
18023 /* None of the valid uses of 'auto' in C++14 involve the type-id
18024 nonterminal, but it is valid in a trailing-return-type. */
18025 && !(cxx_dialect >= cxx1y && is_trailing_return)
18026 && type_uses_auto (type_specifier_seq.type))
18027 {
18028 /* A type-id with type 'auto' is only ok if the abstract declarator
18029 is a function declarator with a late-specified return type. */
18030 if (abstract_declarator
18031 && abstract_declarator->kind == cdk_function
18032 && abstract_declarator->u.function.late_return_type)
18033 /* OK */;
18034 else
18035 {
18036 error ("invalid use of %<auto%>");
18037 return error_mark_node;
18038 }
18039 }
18040
18041 return groktypename (&type_specifier_seq, abstract_declarator,
18042 is_template_arg);
18043 }
18044
18045 static tree cp_parser_type_id (cp_parser *parser)
18046 {
18047 return cp_parser_type_id_1 (parser, false, false);
18048 }
18049
18050 static tree cp_parser_template_type_arg (cp_parser *parser)
18051 {
18052 tree r;
18053 const char *saved_message = parser->type_definition_forbidden_message;
18054 parser->type_definition_forbidden_message
18055 = G_("types may not be defined in template arguments");
18056 r = cp_parser_type_id_1 (parser, true, false);
18057 parser->type_definition_forbidden_message = saved_message;
18058 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18059 {
18060 error ("invalid use of %<auto%> in template argument");
18061 r = error_mark_node;
18062 }
18063 return r;
18064 }
18065
18066 static tree cp_parser_trailing_type_id (cp_parser *parser)
18067 {
18068 return cp_parser_type_id_1 (parser, false, true);
18069 }
18070
18071 /* Parse a type-specifier-seq.
18072
18073 type-specifier-seq:
18074 type-specifier type-specifier-seq [opt]
18075
18076 GNU extension:
18077
18078 type-specifier-seq:
18079 attributes type-specifier-seq [opt]
18080
18081 If IS_DECLARATION is true, we are at the start of a "condition" or
18082 exception-declaration, so we might be followed by a declarator-id.
18083
18084 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18085 i.e. we've just seen "->".
18086
18087 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18088
18089 static void
18090 cp_parser_type_specifier_seq (cp_parser* parser,
18091 bool is_declaration,
18092 bool is_trailing_return,
18093 cp_decl_specifier_seq *type_specifier_seq)
18094 {
18095 bool seen_type_specifier = false;
18096 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18097 cp_token *start_token = NULL;
18098
18099 /* Clear the TYPE_SPECIFIER_SEQ. */
18100 clear_decl_specs (type_specifier_seq);
18101
18102 /* In the context of a trailing return type, enum E { } is an
18103 elaborated-type-specifier followed by a function-body, not an
18104 enum-specifier. */
18105 if (is_trailing_return)
18106 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18107
18108 /* Parse the type-specifiers and attributes. */
18109 while (true)
18110 {
18111 tree type_specifier;
18112 bool is_cv_qualifier;
18113
18114 /* Check for attributes first. */
18115 if (cp_next_tokens_can_be_attribute_p (parser))
18116 {
18117 type_specifier_seq->attributes =
18118 chainon (type_specifier_seq->attributes,
18119 cp_parser_attributes_opt (parser));
18120 continue;
18121 }
18122
18123 /* record the token of the beginning of the type specifier seq,
18124 for error reporting purposes*/
18125 if (!start_token)
18126 start_token = cp_lexer_peek_token (parser->lexer);
18127
18128 /* Look for the type-specifier. */
18129 type_specifier = cp_parser_type_specifier (parser,
18130 flags,
18131 type_specifier_seq,
18132 /*is_declaration=*/false,
18133 NULL,
18134 &is_cv_qualifier);
18135 if (!type_specifier)
18136 {
18137 /* If the first type-specifier could not be found, this is not a
18138 type-specifier-seq at all. */
18139 if (!seen_type_specifier)
18140 {
18141 /* Set in_declarator_p to avoid skipping to the semicolon. */
18142 int in_decl = parser->in_declarator_p;
18143 parser->in_declarator_p = true;
18144
18145 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18146 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18147 cp_parser_error (parser, "expected type-specifier");
18148
18149 parser->in_declarator_p = in_decl;
18150
18151 type_specifier_seq->type = error_mark_node;
18152 return;
18153 }
18154 /* If subsequent type-specifiers could not be found, the
18155 type-specifier-seq is complete. */
18156 break;
18157 }
18158
18159 seen_type_specifier = true;
18160 /* The standard says that a condition can be:
18161
18162 type-specifier-seq declarator = assignment-expression
18163
18164 However, given:
18165
18166 struct S {};
18167 if (int S = ...)
18168
18169 we should treat the "S" as a declarator, not as a
18170 type-specifier. The standard doesn't say that explicitly for
18171 type-specifier-seq, but it does say that for
18172 decl-specifier-seq in an ordinary declaration. Perhaps it
18173 would be clearer just to allow a decl-specifier-seq here, and
18174 then add a semantic restriction that if any decl-specifiers
18175 that are not type-specifiers appear, the program is invalid. */
18176 if (is_declaration && !is_cv_qualifier)
18177 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18178 }
18179 }
18180
18181 /* Return whether the function currently being declared has an associated
18182 template parameter list. */
18183
18184 static bool
18185 function_being_declared_is_template_p (cp_parser* parser)
18186 {
18187 if (!current_template_parms || processing_template_parmlist)
18188 return false;
18189
18190 if (parser->implicit_template_scope)
18191 return true;
18192
18193 if (at_class_scope_p ()
18194 && TYPE_BEING_DEFINED (current_class_type))
18195 return parser->num_template_parameter_lists != 0;
18196
18197 return ((int) parser->num_template_parameter_lists > template_class_depth
18198 (current_class_type));
18199 }
18200
18201 /* Parse a parameter-declaration-clause.
18202
18203 parameter-declaration-clause:
18204 parameter-declaration-list [opt] ... [opt]
18205 parameter-declaration-list , ...
18206
18207 Returns a representation for the parameter declarations. A return
18208 value of NULL indicates a parameter-declaration-clause consisting
18209 only of an ellipsis. */
18210
18211 static tree
18212 cp_parser_parameter_declaration_clause (cp_parser* parser)
18213 {
18214 tree parameters;
18215 cp_token *token;
18216 bool ellipsis_p;
18217 bool is_error;
18218
18219 struct cleanup {
18220 cp_parser* parser;
18221 int auto_is_implicit_function_template_parm_p;
18222 ~cleanup() {
18223 parser->auto_is_implicit_function_template_parm_p
18224 = auto_is_implicit_function_template_parm_p;
18225 }
18226 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18227
18228 (void) cleanup;
18229
18230 if (!processing_specialization
18231 && !processing_template_parmlist
18232 && !processing_explicit_instantiation)
18233 if (!current_function_decl
18234 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18235 parser->auto_is_implicit_function_template_parm_p = true;
18236
18237 /* Peek at the next token. */
18238 token = cp_lexer_peek_token (parser->lexer);
18239 /* Check for trivial parameter-declaration-clauses. */
18240 if (token->type == CPP_ELLIPSIS)
18241 {
18242 /* Consume the `...' token. */
18243 cp_lexer_consume_token (parser->lexer);
18244 return NULL_TREE;
18245 }
18246 else if (token->type == CPP_CLOSE_PAREN)
18247 /* There are no parameters. */
18248 {
18249 #ifndef NO_IMPLICIT_EXTERN_C
18250 if (in_system_header_at (input_location)
18251 && current_class_type == NULL
18252 && current_lang_name == lang_name_c)
18253 return NULL_TREE;
18254 else
18255 #endif
18256 return void_list_node;
18257 }
18258 /* Check for `(void)', too, which is a special case. */
18259 else if (token->keyword == RID_VOID
18260 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18261 == CPP_CLOSE_PAREN))
18262 {
18263 /* Consume the `void' token. */
18264 cp_lexer_consume_token (parser->lexer);
18265 /* There are no parameters. */
18266 return void_list_node;
18267 }
18268
18269 /* Parse the parameter-declaration-list. */
18270 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18271 /* If a parse error occurred while parsing the
18272 parameter-declaration-list, then the entire
18273 parameter-declaration-clause is erroneous. */
18274 if (is_error)
18275 return NULL;
18276
18277 /* Peek at the next token. */
18278 token = cp_lexer_peek_token (parser->lexer);
18279 /* If it's a `,', the clause should terminate with an ellipsis. */
18280 if (token->type == CPP_COMMA)
18281 {
18282 /* Consume the `,'. */
18283 cp_lexer_consume_token (parser->lexer);
18284 /* Expect an ellipsis. */
18285 ellipsis_p
18286 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18287 }
18288 /* It might also be `...' if the optional trailing `,' was
18289 omitted. */
18290 else if (token->type == CPP_ELLIPSIS)
18291 {
18292 /* Consume the `...' token. */
18293 cp_lexer_consume_token (parser->lexer);
18294 /* And remember that we saw it. */
18295 ellipsis_p = true;
18296 }
18297 else
18298 ellipsis_p = false;
18299
18300 /* Finish the parameter list. */
18301 if (!ellipsis_p)
18302 parameters = chainon (parameters, void_list_node);
18303
18304 return parameters;
18305 }
18306
18307 /* Parse a parameter-declaration-list.
18308
18309 parameter-declaration-list:
18310 parameter-declaration
18311 parameter-declaration-list , parameter-declaration
18312
18313 Returns a representation of the parameter-declaration-list, as for
18314 cp_parser_parameter_declaration_clause. However, the
18315 `void_list_node' is never appended to the list. Upon return,
18316 *IS_ERROR will be true iff an error occurred. */
18317
18318 static tree
18319 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18320 {
18321 tree parameters = NULL_TREE;
18322 tree *tail = &parameters;
18323 bool saved_in_unbraced_linkage_specification_p;
18324 int index = 0;
18325
18326 /* Assume all will go well. */
18327 *is_error = false;
18328 /* The special considerations that apply to a function within an
18329 unbraced linkage specifications do not apply to the parameters
18330 to the function. */
18331 saved_in_unbraced_linkage_specification_p
18332 = parser->in_unbraced_linkage_specification_p;
18333 parser->in_unbraced_linkage_specification_p = false;
18334
18335 /* Look for more parameters. */
18336 while (true)
18337 {
18338 cp_parameter_declarator *parameter;
18339 tree decl = error_mark_node;
18340 bool parenthesized_p = false;
18341 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18342 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18343 (current_template_parms)) : 0);
18344
18345 /* Parse the parameter. */
18346 parameter
18347 = cp_parser_parameter_declaration (parser,
18348 /*template_parm_p=*/false,
18349 &parenthesized_p);
18350
18351 /* We don't know yet if the enclosing context is deprecated, so wait
18352 and warn in grokparms if appropriate. */
18353 deprecated_state = DEPRECATED_SUPPRESS;
18354
18355 if (parameter)
18356 {
18357 /* If a function parameter pack was specified and an implicit template
18358 parameter was introduced during cp_parser_parameter_declaration,
18359 change any implicit parameters introduced into packs. */
18360 if (parser->implicit_template_parms
18361 && parameter->declarator
18362 && parameter->declarator->parameter_pack_p)
18363 {
18364 int latest_template_parm_idx = TREE_VEC_LENGTH
18365 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18366
18367 if (latest_template_parm_idx != template_parm_idx)
18368 parameter->decl_specifiers.type = convert_generic_types_to_packs
18369 (parameter->decl_specifiers.type,
18370 template_parm_idx, latest_template_parm_idx);
18371 }
18372
18373 decl = grokdeclarator (parameter->declarator,
18374 &parameter->decl_specifiers,
18375 PARM,
18376 parameter->default_argument != NULL_TREE,
18377 &parameter->decl_specifiers.attributes);
18378 }
18379
18380 deprecated_state = DEPRECATED_NORMAL;
18381
18382 /* If a parse error occurred parsing the parameter declaration,
18383 then the entire parameter-declaration-list is erroneous. */
18384 if (decl == error_mark_node)
18385 {
18386 *is_error = true;
18387 parameters = error_mark_node;
18388 break;
18389 }
18390
18391 if (parameter->decl_specifiers.attributes)
18392 cplus_decl_attributes (&decl,
18393 parameter->decl_specifiers.attributes,
18394 0);
18395 if (DECL_NAME (decl))
18396 decl = pushdecl (decl);
18397
18398 if (decl != error_mark_node)
18399 {
18400 retrofit_lang_decl (decl);
18401 DECL_PARM_INDEX (decl) = ++index;
18402 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18403 }
18404
18405 /* Add the new parameter to the list. */
18406 *tail = build_tree_list (parameter->default_argument, decl);
18407 tail = &TREE_CHAIN (*tail);
18408
18409 /* Peek at the next token. */
18410 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18411 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18412 /* These are for Objective-C++ */
18413 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18414 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18415 /* The parameter-declaration-list is complete. */
18416 break;
18417 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18418 {
18419 cp_token *token;
18420
18421 /* Peek at the next token. */
18422 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18423 /* If it's an ellipsis, then the list is complete. */
18424 if (token->type == CPP_ELLIPSIS)
18425 break;
18426 /* Otherwise, there must be more parameters. Consume the
18427 `,'. */
18428 cp_lexer_consume_token (parser->lexer);
18429 /* When parsing something like:
18430
18431 int i(float f, double d)
18432
18433 we can tell after seeing the declaration for "f" that we
18434 are not looking at an initialization of a variable "i",
18435 but rather at the declaration of a function "i".
18436
18437 Due to the fact that the parsing of template arguments
18438 (as specified to a template-id) requires backtracking we
18439 cannot use this technique when inside a template argument
18440 list. */
18441 if (!parser->in_template_argument_list_p
18442 && !parser->in_type_id_in_expr_p
18443 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18444 /* However, a parameter-declaration of the form
18445 "float(f)" (which is a valid declaration of a
18446 parameter "f") can also be interpreted as an
18447 expression (the conversion of "f" to "float"). */
18448 && !parenthesized_p)
18449 cp_parser_commit_to_tentative_parse (parser);
18450 }
18451 else
18452 {
18453 cp_parser_error (parser, "expected %<,%> or %<...%>");
18454 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18455 cp_parser_skip_to_closing_parenthesis (parser,
18456 /*recovering=*/true,
18457 /*or_comma=*/false,
18458 /*consume_paren=*/false);
18459 break;
18460 }
18461 }
18462
18463 parser->in_unbraced_linkage_specification_p
18464 = saved_in_unbraced_linkage_specification_p;
18465
18466 /* Reset implicit_template_scope if we are about to leave the function
18467 parameter list that introduced it. Note that for out-of-line member
18468 definitions, there will be one or more class scopes before we get to
18469 the template parameter scope. */
18470
18471 if (cp_binding_level *its = parser->implicit_template_scope)
18472 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18473 {
18474 while (maybe_its->kind == sk_class)
18475 maybe_its = maybe_its->level_chain;
18476 if (maybe_its == its)
18477 {
18478 parser->implicit_template_parms = 0;
18479 parser->implicit_template_scope = 0;
18480 }
18481 }
18482
18483 return parameters;
18484 }
18485
18486 /* Parse a parameter declaration.
18487
18488 parameter-declaration:
18489 decl-specifier-seq ... [opt] declarator
18490 decl-specifier-seq declarator = assignment-expression
18491 decl-specifier-seq ... [opt] abstract-declarator [opt]
18492 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18493
18494 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18495 declares a template parameter. (In that case, a non-nested `>'
18496 token encountered during the parsing of the assignment-expression
18497 is not interpreted as a greater-than operator.)
18498
18499 Returns a representation of the parameter, or NULL if an error
18500 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18501 true iff the declarator is of the form "(p)". */
18502
18503 static cp_parameter_declarator *
18504 cp_parser_parameter_declaration (cp_parser *parser,
18505 bool template_parm_p,
18506 bool *parenthesized_p)
18507 {
18508 int declares_class_or_enum;
18509 cp_decl_specifier_seq decl_specifiers;
18510 cp_declarator *declarator;
18511 tree default_argument;
18512 cp_token *token = NULL, *declarator_token_start = NULL;
18513 const char *saved_message;
18514
18515 /* In a template parameter, `>' is not an operator.
18516
18517 [temp.param]
18518
18519 When parsing a default template-argument for a non-type
18520 template-parameter, the first non-nested `>' is taken as the end
18521 of the template parameter-list rather than a greater-than
18522 operator. */
18523
18524 /* Type definitions may not appear in parameter types. */
18525 saved_message = parser->type_definition_forbidden_message;
18526 parser->type_definition_forbidden_message
18527 = G_("types may not be defined in parameter types");
18528
18529 /* Parse the declaration-specifiers. */
18530 cp_parser_decl_specifier_seq (parser,
18531 CP_PARSER_FLAGS_NONE,
18532 &decl_specifiers,
18533 &declares_class_or_enum);
18534
18535 /* Complain about missing 'typename' or other invalid type names. */
18536 if (!decl_specifiers.any_type_specifiers_p
18537 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18538 decl_specifiers.type = error_mark_node;
18539
18540 /* If an error occurred, there's no reason to attempt to parse the
18541 rest of the declaration. */
18542 if (cp_parser_error_occurred (parser))
18543 {
18544 parser->type_definition_forbidden_message = saved_message;
18545 return NULL;
18546 }
18547
18548 /* Peek at the next token. */
18549 token = cp_lexer_peek_token (parser->lexer);
18550
18551 /* If the next token is a `)', `,', `=', `>', or `...', then there
18552 is no declarator. However, when variadic templates are enabled,
18553 there may be a declarator following `...'. */
18554 if (token->type == CPP_CLOSE_PAREN
18555 || token->type == CPP_COMMA
18556 || token->type == CPP_EQ
18557 || token->type == CPP_GREATER)
18558 {
18559 declarator = NULL;
18560 if (parenthesized_p)
18561 *parenthesized_p = false;
18562 }
18563 /* Otherwise, there should be a declarator. */
18564 else
18565 {
18566 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18567 parser->default_arg_ok_p = false;
18568
18569 /* After seeing a decl-specifier-seq, if the next token is not a
18570 "(", there is no possibility that the code is a valid
18571 expression. Therefore, if parsing tentatively, we commit at
18572 this point. */
18573 if (!parser->in_template_argument_list_p
18574 /* In an expression context, having seen:
18575
18576 (int((char ...
18577
18578 we cannot be sure whether we are looking at a
18579 function-type (taking a "char" as a parameter) or a cast
18580 of some object of type "char" to "int". */
18581 && !parser->in_type_id_in_expr_p
18582 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18583 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18584 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18585 cp_parser_commit_to_tentative_parse (parser);
18586 /* Parse the declarator. */
18587 declarator_token_start = token;
18588 declarator = cp_parser_declarator (parser,
18589 CP_PARSER_DECLARATOR_EITHER,
18590 /*ctor_dtor_or_conv_p=*/NULL,
18591 parenthesized_p,
18592 /*member_p=*/false);
18593 parser->default_arg_ok_p = saved_default_arg_ok_p;
18594 /* After the declarator, allow more attributes. */
18595 decl_specifiers.attributes
18596 = chainon (decl_specifiers.attributes,
18597 cp_parser_attributes_opt (parser));
18598 }
18599
18600 /* If the next token is an ellipsis, and we have not seen a
18601 declarator name, and the type of the declarator contains parameter
18602 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18603 a parameter pack expansion expression. Otherwise, leave the
18604 ellipsis for a C-style variadic function. */
18605 token = cp_lexer_peek_token (parser->lexer);
18606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18607 {
18608 tree type = decl_specifiers.type;
18609
18610 if (type && DECL_P (type))
18611 type = TREE_TYPE (type);
18612
18613 if (type
18614 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18615 && declarator_can_be_parameter_pack (declarator)
18616 && (!declarator || !declarator->parameter_pack_p)
18617 && uses_parameter_packs (type))
18618 {
18619 /* Consume the `...'. */
18620 cp_lexer_consume_token (parser->lexer);
18621 maybe_warn_variadic_templates ();
18622
18623 /* Build a pack expansion type */
18624 if (declarator)
18625 declarator->parameter_pack_p = true;
18626 else
18627 decl_specifiers.type = make_pack_expansion (type);
18628 }
18629 }
18630
18631 /* The restriction on defining new types applies only to the type
18632 of the parameter, not to the default argument. */
18633 parser->type_definition_forbidden_message = saved_message;
18634
18635 /* If the next token is `=', then process a default argument. */
18636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18637 {
18638 token = cp_lexer_peek_token (parser->lexer);
18639 /* If we are defining a class, then the tokens that make up the
18640 default argument must be saved and processed later. */
18641 if (!template_parm_p && at_class_scope_p ()
18642 && TYPE_BEING_DEFINED (current_class_type)
18643 && !LAMBDA_TYPE_P (current_class_type))
18644 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18645 /* Outside of a class definition, we can just parse the
18646 assignment-expression. */
18647 else
18648 default_argument
18649 = cp_parser_default_argument (parser, template_parm_p);
18650
18651 if (!parser->default_arg_ok_p)
18652 {
18653 if (flag_permissive)
18654 warning (0, "deprecated use of default argument for parameter of non-function");
18655 else
18656 {
18657 error_at (token->location,
18658 "default arguments are only "
18659 "permitted for function parameters");
18660 default_argument = NULL_TREE;
18661 }
18662 }
18663 else if ((declarator && declarator->parameter_pack_p)
18664 || (decl_specifiers.type
18665 && PACK_EXPANSION_P (decl_specifiers.type)))
18666 {
18667 /* Find the name of the parameter pack. */
18668 cp_declarator *id_declarator = declarator;
18669 while (id_declarator && id_declarator->kind != cdk_id)
18670 id_declarator = id_declarator->declarator;
18671
18672 if (id_declarator && id_declarator->kind == cdk_id)
18673 error_at (declarator_token_start->location,
18674 template_parm_p
18675 ? G_("template parameter pack %qD "
18676 "cannot have a default argument")
18677 : G_("parameter pack %qD cannot have "
18678 "a default argument"),
18679 id_declarator->u.id.unqualified_name);
18680 else
18681 error_at (declarator_token_start->location,
18682 template_parm_p
18683 ? G_("template parameter pack cannot have "
18684 "a default argument")
18685 : G_("parameter pack cannot have a "
18686 "default argument"));
18687
18688 default_argument = NULL_TREE;
18689 }
18690 }
18691 else
18692 default_argument = NULL_TREE;
18693
18694 return make_parameter_declarator (&decl_specifiers,
18695 declarator,
18696 default_argument);
18697 }
18698
18699 /* Parse a default argument and return it.
18700
18701 TEMPLATE_PARM_P is true if this is a default argument for a
18702 non-type template parameter. */
18703 static tree
18704 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18705 {
18706 tree default_argument = NULL_TREE;
18707 bool saved_greater_than_is_operator_p;
18708 bool saved_local_variables_forbidden_p;
18709 bool non_constant_p, is_direct_init;
18710
18711 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18712 set correctly. */
18713 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18714 parser->greater_than_is_operator_p = !template_parm_p;
18715 /* Local variable names (and the `this' keyword) may not
18716 appear in a default argument. */
18717 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18718 parser->local_variables_forbidden_p = true;
18719 /* Parse the assignment-expression. */
18720 if (template_parm_p)
18721 push_deferring_access_checks (dk_no_deferred);
18722 tree saved_class_ptr = NULL_TREE;
18723 tree saved_class_ref = NULL_TREE;
18724 /* The "this" pointer is not valid in a default argument. */
18725 if (cfun)
18726 {
18727 saved_class_ptr = current_class_ptr;
18728 cp_function_chain->x_current_class_ptr = NULL_TREE;
18729 saved_class_ref = current_class_ref;
18730 cp_function_chain->x_current_class_ref = NULL_TREE;
18731 }
18732 default_argument
18733 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18734 /* Restore the "this" pointer. */
18735 if (cfun)
18736 {
18737 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18738 cp_function_chain->x_current_class_ref = saved_class_ref;
18739 }
18740 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18741 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18742 if (template_parm_p)
18743 pop_deferring_access_checks ();
18744 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18745 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18746
18747 return default_argument;
18748 }
18749
18750 /* Parse a function-body.
18751
18752 function-body:
18753 compound_statement */
18754
18755 static void
18756 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18757 {
18758 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18759 }
18760
18761 /* Parse a ctor-initializer-opt followed by a function-body. Return
18762 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18763 is true we are parsing a function-try-block. */
18764
18765 static bool
18766 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18767 bool in_function_try_block)
18768 {
18769 tree body, list;
18770 bool ctor_initializer_p;
18771 const bool check_body_p =
18772 DECL_CONSTRUCTOR_P (current_function_decl)
18773 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18774 tree last = NULL;
18775
18776 /* Begin the function body. */
18777 body = begin_function_body ();
18778 /* Parse the optional ctor-initializer. */
18779 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18780
18781 /* If we're parsing a constexpr constructor definition, we need
18782 to check that the constructor body is indeed empty. However,
18783 before we get to cp_parser_function_body lot of junk has been
18784 generated, so we can't just check that we have an empty block.
18785 Rather we take a snapshot of the outermost block, and check whether
18786 cp_parser_function_body changed its state. */
18787 if (check_body_p)
18788 {
18789 list = cur_stmt_list;
18790 if (STATEMENT_LIST_TAIL (list))
18791 last = STATEMENT_LIST_TAIL (list)->stmt;
18792 }
18793 /* Parse the function-body. */
18794 cp_parser_function_body (parser, in_function_try_block);
18795 if (check_body_p)
18796 check_constexpr_ctor_body (last, list);
18797 /* Finish the function body. */
18798 finish_function_body (body);
18799
18800 return ctor_initializer_p;
18801 }
18802
18803 /* Parse an initializer.
18804
18805 initializer:
18806 = initializer-clause
18807 ( expression-list )
18808
18809 Returns an expression representing the initializer. If no
18810 initializer is present, NULL_TREE is returned.
18811
18812 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18813 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18814 set to TRUE if there is no initializer present. If there is an
18815 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18816 is set to true; otherwise it is set to false. */
18817
18818 static tree
18819 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18820 bool* non_constant_p)
18821 {
18822 cp_token *token;
18823 tree init;
18824
18825 /* Peek at the next token. */
18826 token = cp_lexer_peek_token (parser->lexer);
18827
18828 /* Let our caller know whether or not this initializer was
18829 parenthesized. */
18830 *is_direct_init = (token->type != CPP_EQ);
18831 /* Assume that the initializer is constant. */
18832 *non_constant_p = false;
18833
18834 if (token->type == CPP_EQ)
18835 {
18836 /* Consume the `='. */
18837 cp_lexer_consume_token (parser->lexer);
18838 /* Parse the initializer-clause. */
18839 init = cp_parser_initializer_clause (parser, non_constant_p);
18840 }
18841 else if (token->type == CPP_OPEN_PAREN)
18842 {
18843 vec<tree, va_gc> *vec;
18844 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18845 /*cast_p=*/false,
18846 /*allow_expansion_p=*/true,
18847 non_constant_p);
18848 if (vec == NULL)
18849 return error_mark_node;
18850 init = build_tree_list_vec (vec);
18851 release_tree_vector (vec);
18852 }
18853 else if (token->type == CPP_OPEN_BRACE)
18854 {
18855 cp_lexer_set_source_position (parser->lexer);
18856 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18857 init = cp_parser_braced_list (parser, non_constant_p);
18858 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18859 }
18860 else
18861 {
18862 /* Anything else is an error. */
18863 cp_parser_error (parser, "expected initializer");
18864 init = error_mark_node;
18865 }
18866
18867 return init;
18868 }
18869
18870 /* Parse an initializer-clause.
18871
18872 initializer-clause:
18873 assignment-expression
18874 braced-init-list
18875
18876 Returns an expression representing the initializer.
18877
18878 If the `assignment-expression' production is used the value
18879 returned is simply a representation for the expression.
18880
18881 Otherwise, calls cp_parser_braced_list. */
18882
18883 static tree
18884 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18885 {
18886 tree initializer;
18887
18888 /* Assume the expression is constant. */
18889 *non_constant_p = false;
18890
18891 /* If it is not a `{', then we are looking at an
18892 assignment-expression. */
18893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18894 {
18895 initializer
18896 = cp_parser_constant_expression (parser,
18897 /*allow_non_constant_p=*/true,
18898 non_constant_p);
18899 }
18900 else
18901 initializer = cp_parser_braced_list (parser, non_constant_p);
18902
18903 return initializer;
18904 }
18905
18906 /* Parse a brace-enclosed initializer list.
18907
18908 braced-init-list:
18909 { initializer-list , [opt] }
18910 { }
18911
18912 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18913 the elements of the initializer-list (or NULL, if the last
18914 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18915 NULL_TREE. There is no way to detect whether or not the optional
18916 trailing `,' was provided. NON_CONSTANT_P is as for
18917 cp_parser_initializer. */
18918
18919 static tree
18920 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18921 {
18922 tree initializer;
18923
18924 /* Consume the `{' token. */
18925 cp_lexer_consume_token (parser->lexer);
18926 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18927 initializer = make_node (CONSTRUCTOR);
18928 /* If it's not a `}', then there is a non-trivial initializer. */
18929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18930 {
18931 /* Parse the initializer list. */
18932 CONSTRUCTOR_ELTS (initializer)
18933 = cp_parser_initializer_list (parser, non_constant_p);
18934 /* A trailing `,' token is allowed. */
18935 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18936 cp_lexer_consume_token (parser->lexer);
18937 }
18938 else
18939 *non_constant_p = false;
18940 /* Now, there should be a trailing `}'. */
18941 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18942 TREE_TYPE (initializer) = init_list_type_node;
18943 return initializer;
18944 }
18945
18946 /* Parse an initializer-list.
18947
18948 initializer-list:
18949 initializer-clause ... [opt]
18950 initializer-list , initializer-clause ... [opt]
18951
18952 GNU Extension:
18953
18954 initializer-list:
18955 designation initializer-clause ...[opt]
18956 initializer-list , designation initializer-clause ...[opt]
18957
18958 designation:
18959 . identifier =
18960 identifier :
18961 [ constant-expression ] =
18962
18963 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18964 for the initializer. If the INDEX of the elt is non-NULL, it is the
18965 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18966 as for cp_parser_initializer. */
18967
18968 static vec<constructor_elt, va_gc> *
18969 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18970 {
18971 vec<constructor_elt, va_gc> *v = NULL;
18972
18973 /* Assume all of the expressions are constant. */
18974 *non_constant_p = false;
18975
18976 /* Parse the rest of the list. */
18977 while (true)
18978 {
18979 cp_token *token;
18980 tree designator;
18981 tree initializer;
18982 bool clause_non_constant_p;
18983
18984 /* If the next token is an identifier and the following one is a
18985 colon, we are looking at the GNU designated-initializer
18986 syntax. */
18987 if (cp_parser_allow_gnu_extensions_p (parser)
18988 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18989 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18990 {
18991 /* Warn the user that they are using an extension. */
18992 pedwarn (input_location, OPT_Wpedantic,
18993 "ISO C++ does not allow designated initializers");
18994 /* Consume the identifier. */
18995 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18996 /* Consume the `:'. */
18997 cp_lexer_consume_token (parser->lexer);
18998 }
18999 /* Also handle the C99 syntax, '. id ='. */
19000 else if (cp_parser_allow_gnu_extensions_p (parser)
19001 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19002 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19003 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19004 {
19005 /* Warn the user that they are using an extension. */
19006 pedwarn (input_location, OPT_Wpedantic,
19007 "ISO C++ does not allow C99 designated initializers");
19008 /* Consume the `.'. */
19009 cp_lexer_consume_token (parser->lexer);
19010 /* Consume the identifier. */
19011 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19012 /* Consume the `='. */
19013 cp_lexer_consume_token (parser->lexer);
19014 }
19015 /* Also handle C99 array designators, '[ const ] ='. */
19016 else if (cp_parser_allow_gnu_extensions_p (parser)
19017 && !c_dialect_objc ()
19018 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19019 {
19020 /* In C++11, [ could start a lambda-introducer. */
19021 bool non_const = false;
19022
19023 cp_parser_parse_tentatively (parser);
19024 cp_lexer_consume_token (parser->lexer);
19025 designator = cp_parser_constant_expression (parser, true, &non_const);
19026 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19027 cp_parser_require (parser, CPP_EQ, RT_EQ);
19028 if (!cp_parser_parse_definitely (parser))
19029 designator = NULL_TREE;
19030 else if (non_const)
19031 require_potential_rvalue_constant_expression (designator);
19032 }
19033 else
19034 designator = NULL_TREE;
19035
19036 /* Parse the initializer. */
19037 initializer = cp_parser_initializer_clause (parser,
19038 &clause_non_constant_p);
19039 /* If any clause is non-constant, so is the entire initializer. */
19040 if (clause_non_constant_p)
19041 *non_constant_p = true;
19042
19043 /* If we have an ellipsis, this is an initializer pack
19044 expansion. */
19045 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19046 {
19047 /* Consume the `...'. */
19048 cp_lexer_consume_token (parser->lexer);
19049
19050 /* Turn the initializer into an initializer expansion. */
19051 initializer = make_pack_expansion (initializer);
19052 }
19053
19054 /* Add it to the vector. */
19055 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19056
19057 /* If the next token is not a comma, we have reached the end of
19058 the list. */
19059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19060 break;
19061
19062 /* Peek at the next token. */
19063 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19064 /* If the next token is a `}', then we're still done. An
19065 initializer-clause can have a trailing `,' after the
19066 initializer-list and before the closing `}'. */
19067 if (token->type == CPP_CLOSE_BRACE)
19068 break;
19069
19070 /* Consume the `,' token. */
19071 cp_lexer_consume_token (parser->lexer);
19072 }
19073
19074 return v;
19075 }
19076
19077 /* Classes [gram.class] */
19078
19079 /* Parse a class-name.
19080
19081 class-name:
19082 identifier
19083 template-id
19084
19085 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19086 to indicate that names looked up in dependent types should be
19087 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19088 keyword has been used to indicate that the name that appears next
19089 is a template. TAG_TYPE indicates the explicit tag given before
19090 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19091 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19092 is the class being defined in a class-head.
19093
19094 Returns the TYPE_DECL representing the class. */
19095
19096 static tree
19097 cp_parser_class_name (cp_parser *parser,
19098 bool typename_keyword_p,
19099 bool template_keyword_p,
19100 enum tag_types tag_type,
19101 bool check_dependency_p,
19102 bool class_head_p,
19103 bool is_declaration)
19104 {
19105 tree decl;
19106 tree scope;
19107 bool typename_p;
19108 cp_token *token;
19109 tree identifier = NULL_TREE;
19110
19111 /* All class-names start with an identifier. */
19112 token = cp_lexer_peek_token (parser->lexer);
19113 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19114 {
19115 cp_parser_error (parser, "expected class-name");
19116 return error_mark_node;
19117 }
19118
19119 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19120 to a template-id, so we save it here. */
19121 scope = parser->scope;
19122 if (scope == error_mark_node)
19123 return error_mark_node;
19124
19125 /* Any name names a type if we're following the `typename' keyword
19126 in a qualified name where the enclosing scope is type-dependent. */
19127 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19128 && dependent_type_p (scope));
19129 /* Handle the common case (an identifier, but not a template-id)
19130 efficiently. */
19131 if (token->type == CPP_NAME
19132 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19133 {
19134 cp_token *identifier_token;
19135 bool ambiguous_p;
19136
19137 /* Look for the identifier. */
19138 identifier_token = cp_lexer_peek_token (parser->lexer);
19139 ambiguous_p = identifier_token->error_reported;
19140 identifier = cp_parser_identifier (parser);
19141 /* If the next token isn't an identifier, we are certainly not
19142 looking at a class-name. */
19143 if (identifier == error_mark_node)
19144 decl = error_mark_node;
19145 /* If we know this is a type-name, there's no need to look it
19146 up. */
19147 else if (typename_p)
19148 decl = identifier;
19149 else
19150 {
19151 tree ambiguous_decls;
19152 /* If we already know that this lookup is ambiguous, then
19153 we've already issued an error message; there's no reason
19154 to check again. */
19155 if (ambiguous_p)
19156 {
19157 cp_parser_simulate_error (parser);
19158 return error_mark_node;
19159 }
19160 /* If the next token is a `::', then the name must be a type
19161 name.
19162
19163 [basic.lookup.qual]
19164
19165 During the lookup for a name preceding the :: scope
19166 resolution operator, object, function, and enumerator
19167 names are ignored. */
19168 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19169 tag_type = typename_type;
19170 /* Look up the name. */
19171 decl = cp_parser_lookup_name (parser, identifier,
19172 tag_type,
19173 /*is_template=*/false,
19174 /*is_namespace=*/false,
19175 check_dependency_p,
19176 &ambiguous_decls,
19177 identifier_token->location);
19178 if (ambiguous_decls)
19179 {
19180 if (cp_parser_parsing_tentatively (parser))
19181 cp_parser_simulate_error (parser);
19182 return error_mark_node;
19183 }
19184 }
19185 }
19186 else
19187 {
19188 /* Try a template-id. */
19189 decl = cp_parser_template_id (parser, template_keyword_p,
19190 check_dependency_p,
19191 tag_type,
19192 is_declaration);
19193 if (decl == error_mark_node)
19194 return error_mark_node;
19195 }
19196
19197 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19198
19199 /* If this is a typename, create a TYPENAME_TYPE. */
19200 if (typename_p && decl != error_mark_node)
19201 {
19202 decl = make_typename_type (scope, decl, typename_type,
19203 /*complain=*/tf_error);
19204 if (decl != error_mark_node)
19205 decl = TYPE_NAME (decl);
19206 }
19207
19208 decl = strip_using_decl (decl);
19209
19210 /* Check to see that it is really the name of a class. */
19211 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19212 && identifier_p (TREE_OPERAND (decl, 0))
19213 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19214 /* Situations like this:
19215
19216 template <typename T> struct A {
19217 typename T::template X<int>::I i;
19218 };
19219
19220 are problematic. Is `T::template X<int>' a class-name? The
19221 standard does not seem to be definitive, but there is no other
19222 valid interpretation of the following `::'. Therefore, those
19223 names are considered class-names. */
19224 {
19225 decl = make_typename_type (scope, decl, tag_type, tf_error);
19226 if (decl != error_mark_node)
19227 decl = TYPE_NAME (decl);
19228 }
19229 else if (TREE_CODE (decl) != TYPE_DECL
19230 || TREE_TYPE (decl) == error_mark_node
19231 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19232 /* In Objective-C 2.0, a classname followed by '.' starts a
19233 dot-syntax expression, and it's not a type-name. */
19234 || (c_dialect_objc ()
19235 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19236 && objc_is_class_name (decl)))
19237 decl = error_mark_node;
19238
19239 if (decl == error_mark_node)
19240 cp_parser_error (parser, "expected class-name");
19241 else if (identifier && !parser->scope)
19242 maybe_note_name_used_in_class (identifier, decl);
19243
19244 return decl;
19245 }
19246
19247 /* Parse a class-specifier.
19248
19249 class-specifier:
19250 class-head { member-specification [opt] }
19251
19252 Returns the TREE_TYPE representing the class. */
19253
19254 static tree
19255 cp_parser_class_specifier_1 (cp_parser* parser)
19256 {
19257 tree type;
19258 tree attributes = NULL_TREE;
19259 bool nested_name_specifier_p;
19260 unsigned saved_num_template_parameter_lists;
19261 bool saved_in_function_body;
19262 unsigned char in_statement;
19263 bool in_switch_statement_p;
19264 bool saved_in_unbraced_linkage_specification_p;
19265 tree old_scope = NULL_TREE;
19266 tree scope = NULL_TREE;
19267 cp_token *closing_brace;
19268
19269 push_deferring_access_checks (dk_no_deferred);
19270
19271 /* Parse the class-head. */
19272 type = cp_parser_class_head (parser,
19273 &nested_name_specifier_p);
19274 /* If the class-head was a semantic disaster, skip the entire body
19275 of the class. */
19276 if (!type)
19277 {
19278 cp_parser_skip_to_end_of_block_or_statement (parser);
19279 pop_deferring_access_checks ();
19280 return error_mark_node;
19281 }
19282
19283 /* Look for the `{'. */
19284 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19285 {
19286 pop_deferring_access_checks ();
19287 return error_mark_node;
19288 }
19289
19290 cp_ensure_no_omp_declare_simd (parser);
19291
19292 /* Issue an error message if type-definitions are forbidden here. */
19293 cp_parser_check_type_definition (parser);
19294 /* Remember that we are defining one more class. */
19295 ++parser->num_classes_being_defined;
19296 /* Inside the class, surrounding template-parameter-lists do not
19297 apply. */
19298 saved_num_template_parameter_lists
19299 = parser->num_template_parameter_lists;
19300 parser->num_template_parameter_lists = 0;
19301 /* We are not in a function body. */
19302 saved_in_function_body = parser->in_function_body;
19303 parser->in_function_body = false;
19304 /* Or in a loop. */
19305 in_statement = parser->in_statement;
19306 parser->in_statement = 0;
19307 /* Or in a switch. */
19308 in_switch_statement_p = parser->in_switch_statement_p;
19309 parser->in_switch_statement_p = false;
19310 /* We are not immediately inside an extern "lang" block. */
19311 saved_in_unbraced_linkage_specification_p
19312 = parser->in_unbraced_linkage_specification_p;
19313 parser->in_unbraced_linkage_specification_p = false;
19314
19315 /* Start the class. */
19316 if (nested_name_specifier_p)
19317 {
19318 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19319 old_scope = push_inner_scope (scope);
19320 }
19321 type = begin_class_definition (type);
19322
19323 if (type == error_mark_node)
19324 /* If the type is erroneous, skip the entire body of the class. */
19325 cp_parser_skip_to_closing_brace (parser);
19326 else
19327 /* Parse the member-specification. */
19328 cp_parser_member_specification_opt (parser);
19329
19330 /* Look for the trailing `}'. */
19331 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19332 /* Look for trailing attributes to apply to this class. */
19333 if (cp_parser_allow_gnu_extensions_p (parser))
19334 attributes = cp_parser_gnu_attributes_opt (parser);
19335 if (type != error_mark_node)
19336 type = finish_struct (type, attributes);
19337 if (nested_name_specifier_p)
19338 pop_inner_scope (old_scope, scope);
19339
19340 /* We've finished a type definition. Check for the common syntax
19341 error of forgetting a semicolon after the definition. We need to
19342 be careful, as we can't just check for not-a-semicolon and be done
19343 with it; the user might have typed:
19344
19345 class X { } c = ...;
19346 class X { } *p = ...;
19347
19348 and so forth. Instead, enumerate all the possible tokens that
19349 might follow this production; if we don't see one of them, then
19350 complain and silently insert the semicolon. */
19351 {
19352 cp_token *token = cp_lexer_peek_token (parser->lexer);
19353 bool want_semicolon = true;
19354
19355 if (cp_next_tokens_can_be_std_attribute_p (parser))
19356 /* Don't try to parse c++11 attributes here. As per the
19357 grammar, that should be a task for
19358 cp_parser_decl_specifier_seq. */
19359 want_semicolon = false;
19360
19361 switch (token->type)
19362 {
19363 case CPP_NAME:
19364 case CPP_SEMICOLON:
19365 case CPP_MULT:
19366 case CPP_AND:
19367 case CPP_OPEN_PAREN:
19368 case CPP_CLOSE_PAREN:
19369 case CPP_COMMA:
19370 want_semicolon = false;
19371 break;
19372
19373 /* While it's legal for type qualifiers and storage class
19374 specifiers to follow type definitions in the grammar, only
19375 compiler testsuites contain code like that. Assume that if
19376 we see such code, then what we're really seeing is a case
19377 like:
19378
19379 class X { }
19380 const <type> var = ...;
19381
19382 or
19383
19384 class Y { }
19385 static <type> func (...) ...
19386
19387 i.e. the qualifier or specifier applies to the next
19388 declaration. To do so, however, we need to look ahead one
19389 more token to see if *that* token is a type specifier.
19390
19391 This code could be improved to handle:
19392
19393 class Z { }
19394 static const <type> var = ...; */
19395 case CPP_KEYWORD:
19396 if (keyword_is_decl_specifier (token->keyword))
19397 {
19398 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19399
19400 /* Handling user-defined types here would be nice, but very
19401 tricky. */
19402 want_semicolon
19403 = (lookahead->type == CPP_KEYWORD
19404 && keyword_begins_type_specifier (lookahead->keyword));
19405 }
19406 break;
19407 default:
19408 break;
19409 }
19410
19411 /* If we don't have a type, then something is very wrong and we
19412 shouldn't try to do anything clever. Likewise for not seeing the
19413 closing brace. */
19414 if (closing_brace && TYPE_P (type) && want_semicolon)
19415 {
19416 cp_token_position prev
19417 = cp_lexer_previous_token_position (parser->lexer);
19418 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19419 location_t loc = prev_token->location;
19420
19421 if (CLASSTYPE_DECLARED_CLASS (type))
19422 error_at (loc, "expected %<;%> after class definition");
19423 else if (TREE_CODE (type) == RECORD_TYPE)
19424 error_at (loc, "expected %<;%> after struct definition");
19425 else if (TREE_CODE (type) == UNION_TYPE)
19426 error_at (loc, "expected %<;%> after union definition");
19427 else
19428 gcc_unreachable ();
19429
19430 /* Unget one token and smash it to look as though we encountered
19431 a semicolon in the input stream. */
19432 cp_lexer_set_token_position (parser->lexer, prev);
19433 token = cp_lexer_peek_token (parser->lexer);
19434 token->type = CPP_SEMICOLON;
19435 token->keyword = RID_MAX;
19436 }
19437 }
19438
19439 /* If this class is not itself within the scope of another class,
19440 then we need to parse the bodies of all of the queued function
19441 definitions. Note that the queued functions defined in a class
19442 are not always processed immediately following the
19443 class-specifier for that class. Consider:
19444
19445 struct A {
19446 struct B { void f() { sizeof (A); } };
19447 };
19448
19449 If `f' were processed before the processing of `A' were
19450 completed, there would be no way to compute the size of `A'.
19451 Note that the nesting we are interested in here is lexical --
19452 not the semantic nesting given by TYPE_CONTEXT. In particular,
19453 for:
19454
19455 struct A { struct B; };
19456 struct A::B { void f() { } };
19457
19458 there is no need to delay the parsing of `A::B::f'. */
19459 if (--parser->num_classes_being_defined == 0)
19460 {
19461 tree decl;
19462 tree class_type = NULL_TREE;
19463 tree pushed_scope = NULL_TREE;
19464 unsigned ix;
19465 cp_default_arg_entry *e;
19466 tree save_ccp, save_ccr;
19467
19468 /* In a first pass, parse default arguments to the functions.
19469 Then, in a second pass, parse the bodies of the functions.
19470 This two-phased approach handles cases like:
19471
19472 struct S {
19473 void f() { g(); }
19474 void g(int i = 3);
19475 };
19476
19477 */
19478 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19479 {
19480 decl = e->decl;
19481 /* If there are default arguments that have not yet been processed,
19482 take care of them now. */
19483 if (class_type != e->class_type)
19484 {
19485 if (pushed_scope)
19486 pop_scope (pushed_scope);
19487 class_type = e->class_type;
19488 pushed_scope = push_scope (class_type);
19489 }
19490 /* Make sure that any template parameters are in scope. */
19491 maybe_begin_member_template_processing (decl);
19492 /* Parse the default argument expressions. */
19493 cp_parser_late_parsing_default_args (parser, decl);
19494 /* Remove any template parameters from the symbol table. */
19495 maybe_end_member_template_processing ();
19496 }
19497 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19498 /* Now parse any NSDMIs. */
19499 save_ccp = current_class_ptr;
19500 save_ccr = current_class_ref;
19501 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19502 {
19503 if (class_type != DECL_CONTEXT (decl))
19504 {
19505 if (pushed_scope)
19506 pop_scope (pushed_scope);
19507 class_type = DECL_CONTEXT (decl);
19508 pushed_scope = push_scope (class_type);
19509 }
19510 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19511 cp_parser_late_parsing_nsdmi (parser, decl);
19512 }
19513 vec_safe_truncate (unparsed_nsdmis, 0);
19514 current_class_ptr = save_ccp;
19515 current_class_ref = save_ccr;
19516 if (pushed_scope)
19517 pop_scope (pushed_scope);
19518
19519 /* Now do some post-NSDMI bookkeeping. */
19520 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19521 after_nsdmi_defaulted_late_checks (class_type);
19522 vec_safe_truncate (unparsed_classes, 0);
19523 after_nsdmi_defaulted_late_checks (type);
19524
19525 /* Now parse the body of the functions. */
19526 if (flag_openmp)
19527 {
19528 /* OpenMP UDRs need to be parsed before all other functions. */
19529 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19530 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19531 cp_parser_late_parsing_for_member (parser, decl);
19532 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19533 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19534 cp_parser_late_parsing_for_member (parser, decl);
19535 }
19536 else
19537 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19538 cp_parser_late_parsing_for_member (parser, decl);
19539 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19540 }
19541 else
19542 vec_safe_push (unparsed_classes, type);
19543
19544 /* Put back any saved access checks. */
19545 pop_deferring_access_checks ();
19546
19547 /* Restore saved state. */
19548 parser->in_switch_statement_p = in_switch_statement_p;
19549 parser->in_statement = in_statement;
19550 parser->in_function_body = saved_in_function_body;
19551 parser->num_template_parameter_lists
19552 = saved_num_template_parameter_lists;
19553 parser->in_unbraced_linkage_specification_p
19554 = saved_in_unbraced_linkage_specification_p;
19555
19556 return type;
19557 }
19558
19559 static tree
19560 cp_parser_class_specifier (cp_parser* parser)
19561 {
19562 tree ret;
19563 timevar_push (TV_PARSE_STRUCT);
19564 ret = cp_parser_class_specifier_1 (parser);
19565 timevar_pop (TV_PARSE_STRUCT);
19566 return ret;
19567 }
19568
19569 /* Parse a class-head.
19570
19571 class-head:
19572 class-key identifier [opt] base-clause [opt]
19573 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19574 class-key nested-name-specifier [opt] template-id
19575 base-clause [opt]
19576
19577 class-virt-specifier:
19578 final
19579
19580 GNU Extensions:
19581 class-key attributes identifier [opt] base-clause [opt]
19582 class-key attributes nested-name-specifier identifier base-clause [opt]
19583 class-key attributes nested-name-specifier [opt] template-id
19584 base-clause [opt]
19585
19586 Upon return BASES is initialized to the list of base classes (or
19587 NULL, if there are none) in the same form returned by
19588 cp_parser_base_clause.
19589
19590 Returns the TYPE of the indicated class. Sets
19591 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19592 involving a nested-name-specifier was used, and FALSE otherwise.
19593
19594 Returns error_mark_node if this is not a class-head.
19595
19596 Returns NULL_TREE if the class-head is syntactically valid, but
19597 semantically invalid in a way that means we should skip the entire
19598 body of the class. */
19599
19600 static tree
19601 cp_parser_class_head (cp_parser* parser,
19602 bool* nested_name_specifier_p)
19603 {
19604 tree nested_name_specifier;
19605 enum tag_types class_key;
19606 tree id = NULL_TREE;
19607 tree type = NULL_TREE;
19608 tree attributes;
19609 tree bases;
19610 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19611 bool template_id_p = false;
19612 bool qualified_p = false;
19613 bool invalid_nested_name_p = false;
19614 bool invalid_explicit_specialization_p = false;
19615 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19616 tree pushed_scope = NULL_TREE;
19617 unsigned num_templates;
19618 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19619 /* Assume no nested-name-specifier will be present. */
19620 *nested_name_specifier_p = false;
19621 /* Assume no template parameter lists will be used in defining the
19622 type. */
19623 num_templates = 0;
19624 parser->colon_corrects_to_scope_p = false;
19625
19626 /* Look for the class-key. */
19627 class_key = cp_parser_class_key (parser);
19628 if (class_key == none_type)
19629 return error_mark_node;
19630
19631 /* Parse the attributes. */
19632 attributes = cp_parser_attributes_opt (parser);
19633
19634 /* If the next token is `::', that is invalid -- but sometimes
19635 people do try to write:
19636
19637 struct ::S {};
19638
19639 Handle this gracefully by accepting the extra qualifier, and then
19640 issuing an error about it later if this really is a
19641 class-head. If it turns out just to be an elaborated type
19642 specifier, remain silent. */
19643 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19644 qualified_p = true;
19645
19646 push_deferring_access_checks (dk_no_check);
19647
19648 /* Determine the name of the class. Begin by looking for an
19649 optional nested-name-specifier. */
19650 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19651 nested_name_specifier
19652 = cp_parser_nested_name_specifier_opt (parser,
19653 /*typename_keyword_p=*/false,
19654 /*check_dependency_p=*/false,
19655 /*type_p=*/true,
19656 /*is_declaration=*/false);
19657 /* If there was a nested-name-specifier, then there *must* be an
19658 identifier. */
19659 if (nested_name_specifier)
19660 {
19661 type_start_token = cp_lexer_peek_token (parser->lexer);
19662 /* Although the grammar says `identifier', it really means
19663 `class-name' or `template-name'. You are only allowed to
19664 define a class that has already been declared with this
19665 syntax.
19666
19667 The proposed resolution for Core Issue 180 says that wherever
19668 you see `class T::X' you should treat `X' as a type-name.
19669
19670 It is OK to define an inaccessible class; for example:
19671
19672 class A { class B; };
19673 class A::B {};
19674
19675 We do not know if we will see a class-name, or a
19676 template-name. We look for a class-name first, in case the
19677 class-name is a template-id; if we looked for the
19678 template-name first we would stop after the template-name. */
19679 cp_parser_parse_tentatively (parser);
19680 type = cp_parser_class_name (parser,
19681 /*typename_keyword_p=*/false,
19682 /*template_keyword_p=*/false,
19683 class_type,
19684 /*check_dependency_p=*/false,
19685 /*class_head_p=*/true,
19686 /*is_declaration=*/false);
19687 /* If that didn't work, ignore the nested-name-specifier. */
19688 if (!cp_parser_parse_definitely (parser))
19689 {
19690 invalid_nested_name_p = true;
19691 type_start_token = cp_lexer_peek_token (parser->lexer);
19692 id = cp_parser_identifier (parser);
19693 if (id == error_mark_node)
19694 id = NULL_TREE;
19695 }
19696 /* If we could not find a corresponding TYPE, treat this
19697 declaration like an unqualified declaration. */
19698 if (type == error_mark_node)
19699 nested_name_specifier = NULL_TREE;
19700 /* Otherwise, count the number of templates used in TYPE and its
19701 containing scopes. */
19702 else
19703 {
19704 tree scope;
19705
19706 for (scope = TREE_TYPE (type);
19707 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19708 scope = get_containing_scope (scope))
19709 if (TYPE_P (scope)
19710 && CLASS_TYPE_P (scope)
19711 && CLASSTYPE_TEMPLATE_INFO (scope)
19712 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19713 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19714 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19715 ++num_templates;
19716 }
19717 }
19718 /* Otherwise, the identifier is optional. */
19719 else
19720 {
19721 /* We don't know whether what comes next is a template-id,
19722 an identifier, or nothing at all. */
19723 cp_parser_parse_tentatively (parser);
19724 /* Check for a template-id. */
19725 type_start_token = cp_lexer_peek_token (parser->lexer);
19726 id = cp_parser_template_id (parser,
19727 /*template_keyword_p=*/false,
19728 /*check_dependency_p=*/true,
19729 class_key,
19730 /*is_declaration=*/true);
19731 /* If that didn't work, it could still be an identifier. */
19732 if (!cp_parser_parse_definitely (parser))
19733 {
19734 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19735 {
19736 type_start_token = cp_lexer_peek_token (parser->lexer);
19737 id = cp_parser_identifier (parser);
19738 }
19739 else
19740 id = NULL_TREE;
19741 }
19742 else
19743 {
19744 template_id_p = true;
19745 ++num_templates;
19746 }
19747 }
19748
19749 pop_deferring_access_checks ();
19750
19751 if (id)
19752 {
19753 cp_parser_check_for_invalid_template_id (parser, id,
19754 class_key,
19755 type_start_token->location);
19756 }
19757 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19758
19759 /* If it's not a `:' or a `{' then we can't really be looking at a
19760 class-head, since a class-head only appears as part of a
19761 class-specifier. We have to detect this situation before calling
19762 xref_tag, since that has irreversible side-effects. */
19763 if (!cp_parser_next_token_starts_class_definition_p (parser))
19764 {
19765 cp_parser_error (parser, "expected %<{%> or %<:%>");
19766 type = error_mark_node;
19767 goto out;
19768 }
19769
19770 /* At this point, we're going ahead with the class-specifier, even
19771 if some other problem occurs. */
19772 cp_parser_commit_to_tentative_parse (parser);
19773 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19774 {
19775 cp_parser_error (parser,
19776 "cannot specify %<override%> for a class");
19777 type = error_mark_node;
19778 goto out;
19779 }
19780 /* Issue the error about the overly-qualified name now. */
19781 if (qualified_p)
19782 {
19783 cp_parser_error (parser,
19784 "global qualification of class name is invalid");
19785 type = error_mark_node;
19786 goto out;
19787 }
19788 else if (invalid_nested_name_p)
19789 {
19790 cp_parser_error (parser,
19791 "qualified name does not name a class");
19792 type = error_mark_node;
19793 goto out;
19794 }
19795 else if (nested_name_specifier)
19796 {
19797 tree scope;
19798
19799 /* Reject typedef-names in class heads. */
19800 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19801 {
19802 error_at (type_start_token->location,
19803 "invalid class name in declaration of %qD",
19804 type);
19805 type = NULL_TREE;
19806 goto done;
19807 }
19808
19809 /* Figure out in what scope the declaration is being placed. */
19810 scope = current_scope ();
19811 /* If that scope does not contain the scope in which the
19812 class was originally declared, the program is invalid. */
19813 if (scope && !is_ancestor (scope, nested_name_specifier))
19814 {
19815 if (at_namespace_scope_p ())
19816 error_at (type_start_token->location,
19817 "declaration of %qD in namespace %qD which does not "
19818 "enclose %qD",
19819 type, scope, nested_name_specifier);
19820 else
19821 error_at (type_start_token->location,
19822 "declaration of %qD in %qD which does not enclose %qD",
19823 type, scope, nested_name_specifier);
19824 type = NULL_TREE;
19825 goto done;
19826 }
19827 /* [dcl.meaning]
19828
19829 A declarator-id shall not be qualified except for the
19830 definition of a ... nested class outside of its class
19831 ... [or] the definition or explicit instantiation of a
19832 class member of a namespace outside of its namespace. */
19833 if (scope == nested_name_specifier)
19834 {
19835 permerror (nested_name_specifier_token_start->location,
19836 "extra qualification not allowed");
19837 nested_name_specifier = NULL_TREE;
19838 num_templates = 0;
19839 }
19840 }
19841 /* An explicit-specialization must be preceded by "template <>". If
19842 it is not, try to recover gracefully. */
19843 if (at_namespace_scope_p ()
19844 && parser->num_template_parameter_lists == 0
19845 && template_id_p)
19846 {
19847 error_at (type_start_token->location,
19848 "an explicit specialization must be preceded by %<template <>%>");
19849 invalid_explicit_specialization_p = true;
19850 /* Take the same action that would have been taken by
19851 cp_parser_explicit_specialization. */
19852 ++parser->num_template_parameter_lists;
19853 begin_specialization ();
19854 }
19855 /* There must be no "return" statements between this point and the
19856 end of this function; set "type "to the correct return value and
19857 use "goto done;" to return. */
19858 /* Make sure that the right number of template parameters were
19859 present. */
19860 if (!cp_parser_check_template_parameters (parser, num_templates,
19861 type_start_token->location,
19862 /*declarator=*/NULL))
19863 {
19864 /* If something went wrong, there is no point in even trying to
19865 process the class-definition. */
19866 type = NULL_TREE;
19867 goto done;
19868 }
19869
19870 /* Look up the type. */
19871 if (template_id_p)
19872 {
19873 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19874 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19875 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19876 {
19877 error_at (type_start_token->location,
19878 "function template %qD redeclared as a class template", id);
19879 type = error_mark_node;
19880 }
19881 else
19882 {
19883 type = TREE_TYPE (id);
19884 type = maybe_process_partial_specialization (type);
19885 }
19886 if (nested_name_specifier)
19887 pushed_scope = push_scope (nested_name_specifier);
19888 }
19889 else if (nested_name_specifier)
19890 {
19891 tree class_type;
19892
19893 /* Given:
19894
19895 template <typename T> struct S { struct T };
19896 template <typename T> struct S<T>::T { };
19897
19898 we will get a TYPENAME_TYPE when processing the definition of
19899 `S::T'. We need to resolve it to the actual type before we
19900 try to define it. */
19901 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19902 {
19903 class_type = resolve_typename_type (TREE_TYPE (type),
19904 /*only_current_p=*/false);
19905 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19906 type = TYPE_NAME (class_type);
19907 else
19908 {
19909 cp_parser_error (parser, "could not resolve typename type");
19910 type = error_mark_node;
19911 }
19912 }
19913
19914 if (maybe_process_partial_specialization (TREE_TYPE (type))
19915 == error_mark_node)
19916 {
19917 type = NULL_TREE;
19918 goto done;
19919 }
19920
19921 class_type = current_class_type;
19922 /* Enter the scope indicated by the nested-name-specifier. */
19923 pushed_scope = push_scope (nested_name_specifier);
19924 /* Get the canonical version of this type. */
19925 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19926 /* Call push_template_decl if it seems like we should be defining a
19927 template either from the template headers or the type we're
19928 defining, so that we diagnose both extra and missing headers. */
19929 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19930 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19931 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19932 (TREE_TYPE (type)))))
19933 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19934 {
19935 type = push_template_decl (type);
19936 if (type == error_mark_node)
19937 {
19938 type = NULL_TREE;
19939 goto done;
19940 }
19941 }
19942
19943 type = TREE_TYPE (type);
19944 *nested_name_specifier_p = true;
19945 }
19946 else /* The name is not a nested name. */
19947 {
19948 /* If the class was unnamed, create a dummy name. */
19949 if (!id)
19950 id = make_anon_name ();
19951 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19952 parser->num_template_parameter_lists);
19953 }
19954
19955 /* Indicate whether this class was declared as a `class' or as a
19956 `struct'. */
19957 if (TREE_CODE (type) == RECORD_TYPE)
19958 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19959 cp_parser_check_class_key (class_key, type);
19960
19961 /* If this type was already complete, and we see another definition,
19962 that's an error. */
19963 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19964 {
19965 error_at (type_start_token->location, "redefinition of %q#T",
19966 type);
19967 error_at (type_start_token->location, "previous definition of %q+#T",
19968 type);
19969 type = NULL_TREE;
19970 goto done;
19971 }
19972 else if (type == error_mark_node)
19973 type = NULL_TREE;
19974
19975 if (type)
19976 {
19977 /* Apply attributes now, before any use of the class as a template
19978 argument in its base list. */
19979 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19980 fixup_attribute_variants (type);
19981 }
19982
19983 /* We will have entered the scope containing the class; the names of
19984 base classes should be looked up in that context. For example:
19985
19986 struct A { struct B {}; struct C; };
19987 struct A::C : B {};
19988
19989 is valid. */
19990
19991 /* Get the list of base-classes, if there is one. */
19992 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19993 {
19994 /* PR59482: enter the class scope so that base-specifiers are looked
19995 up correctly. */
19996 if (type)
19997 pushclass (type);
19998 bases = cp_parser_base_clause (parser);
19999 /* PR59482: get out of the previously pushed class scope so that the
20000 subsequent pops pop the right thing. */
20001 if (type)
20002 popclass ();
20003 }
20004 else
20005 bases = NULL_TREE;
20006
20007 /* If we're really defining a class, process the base classes.
20008 If they're invalid, fail. */
20009 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20010 && !xref_basetypes (type, bases))
20011 type = NULL_TREE;
20012
20013 done:
20014 /* Leave the scope given by the nested-name-specifier. We will
20015 enter the class scope itself while processing the members. */
20016 if (pushed_scope)
20017 pop_scope (pushed_scope);
20018
20019 if (invalid_explicit_specialization_p)
20020 {
20021 end_specialization ();
20022 --parser->num_template_parameter_lists;
20023 }
20024
20025 if (type)
20026 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20027 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20028 CLASSTYPE_FINAL (type) = 1;
20029 out:
20030 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20031 return type;
20032 }
20033
20034 /* Parse a class-key.
20035
20036 class-key:
20037 class
20038 struct
20039 union
20040
20041 Returns the kind of class-key specified, or none_type to indicate
20042 error. */
20043
20044 static enum tag_types
20045 cp_parser_class_key (cp_parser* parser)
20046 {
20047 cp_token *token;
20048 enum tag_types tag_type;
20049
20050 /* Look for the class-key. */
20051 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20052 if (!token)
20053 return none_type;
20054
20055 /* Check to see if the TOKEN is a class-key. */
20056 tag_type = cp_parser_token_is_class_key (token);
20057 if (!tag_type)
20058 cp_parser_error (parser, "expected class-key");
20059 return tag_type;
20060 }
20061
20062 /* Parse an (optional) member-specification.
20063
20064 member-specification:
20065 member-declaration member-specification [opt]
20066 access-specifier : member-specification [opt] */
20067
20068 static void
20069 cp_parser_member_specification_opt (cp_parser* parser)
20070 {
20071 while (true)
20072 {
20073 cp_token *token;
20074 enum rid keyword;
20075
20076 /* Peek at the next token. */
20077 token = cp_lexer_peek_token (parser->lexer);
20078 /* If it's a `}', or EOF then we've seen all the members. */
20079 if (token->type == CPP_CLOSE_BRACE
20080 || token->type == CPP_EOF
20081 || token->type == CPP_PRAGMA_EOL)
20082 break;
20083
20084 /* See if this token is a keyword. */
20085 keyword = token->keyword;
20086 switch (keyword)
20087 {
20088 case RID_PUBLIC:
20089 case RID_PROTECTED:
20090 case RID_PRIVATE:
20091 /* Consume the access-specifier. */
20092 cp_lexer_consume_token (parser->lexer);
20093 /* Remember which access-specifier is active. */
20094 current_access_specifier = token->u.value;
20095 /* Look for the `:'. */
20096 cp_parser_require (parser, CPP_COLON, RT_COLON);
20097 break;
20098
20099 default:
20100 /* Accept #pragmas at class scope. */
20101 if (token->type == CPP_PRAGMA)
20102 {
20103 cp_parser_pragma (parser, pragma_member);
20104 break;
20105 }
20106
20107 /* Otherwise, the next construction must be a
20108 member-declaration. */
20109 cp_parser_member_declaration (parser);
20110 }
20111 }
20112 }
20113
20114 /* Parse a member-declaration.
20115
20116 member-declaration:
20117 decl-specifier-seq [opt] member-declarator-list [opt] ;
20118 function-definition ; [opt]
20119 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20120 using-declaration
20121 template-declaration
20122 alias-declaration
20123
20124 member-declarator-list:
20125 member-declarator
20126 member-declarator-list , member-declarator
20127
20128 member-declarator:
20129 declarator pure-specifier [opt]
20130 declarator constant-initializer [opt]
20131 identifier [opt] : constant-expression
20132
20133 GNU Extensions:
20134
20135 member-declaration:
20136 __extension__ member-declaration
20137
20138 member-declarator:
20139 declarator attributes [opt] pure-specifier [opt]
20140 declarator attributes [opt] constant-initializer [opt]
20141 identifier [opt] attributes [opt] : constant-expression
20142
20143 C++0x Extensions:
20144
20145 member-declaration:
20146 static_assert-declaration */
20147
20148 static void
20149 cp_parser_member_declaration (cp_parser* parser)
20150 {
20151 cp_decl_specifier_seq decl_specifiers;
20152 tree prefix_attributes;
20153 tree decl;
20154 int declares_class_or_enum;
20155 bool friend_p;
20156 cp_token *token = NULL;
20157 cp_token *decl_spec_token_start = NULL;
20158 cp_token *initializer_token_start = NULL;
20159 int saved_pedantic;
20160 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20161
20162 /* Check for the `__extension__' keyword. */
20163 if (cp_parser_extension_opt (parser, &saved_pedantic))
20164 {
20165 /* Recurse. */
20166 cp_parser_member_declaration (parser);
20167 /* Restore the old value of the PEDANTIC flag. */
20168 pedantic = saved_pedantic;
20169
20170 return;
20171 }
20172
20173 /* Check for a template-declaration. */
20174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20175 {
20176 /* An explicit specialization here is an error condition, and we
20177 expect the specialization handler to detect and report this. */
20178 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20179 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20180 cp_parser_explicit_specialization (parser);
20181 else
20182 cp_parser_template_declaration (parser, /*member_p=*/true);
20183
20184 return;
20185 }
20186
20187 /* Check for a using-declaration. */
20188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20189 {
20190 if (cxx_dialect < cxx11)
20191 {
20192 /* Parse the using-declaration. */
20193 cp_parser_using_declaration (parser,
20194 /*access_declaration_p=*/false);
20195 return;
20196 }
20197 else
20198 {
20199 tree decl;
20200 bool alias_decl_expected;
20201 cp_parser_parse_tentatively (parser);
20202 decl = cp_parser_alias_declaration (parser);
20203 /* Note that if we actually see the '=' token after the
20204 identifier, cp_parser_alias_declaration commits the
20205 tentative parse. In that case, we really expects an
20206 alias-declaration. Otherwise, we expect a using
20207 declaration. */
20208 alias_decl_expected =
20209 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20210 cp_parser_parse_definitely (parser);
20211
20212 if (alias_decl_expected)
20213 finish_member_declaration (decl);
20214 else
20215 cp_parser_using_declaration (parser,
20216 /*access_declaration_p=*/false);
20217 return;
20218 }
20219 }
20220
20221 /* Check for @defs. */
20222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20223 {
20224 tree ivar, member;
20225 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20226 ivar = ivar_chains;
20227 while (ivar)
20228 {
20229 member = ivar;
20230 ivar = TREE_CHAIN (member);
20231 TREE_CHAIN (member) = NULL_TREE;
20232 finish_member_declaration (member);
20233 }
20234 return;
20235 }
20236
20237 /* If the next token is `static_assert' we have a static assertion. */
20238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20239 {
20240 cp_parser_static_assert (parser, /*member_p=*/true);
20241 return;
20242 }
20243
20244 parser->colon_corrects_to_scope_p = false;
20245
20246 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20247 goto out;
20248
20249 /* Parse the decl-specifier-seq. */
20250 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20251 cp_parser_decl_specifier_seq (parser,
20252 CP_PARSER_FLAGS_OPTIONAL,
20253 &decl_specifiers,
20254 &declares_class_or_enum);
20255 /* Check for an invalid type-name. */
20256 if (!decl_specifiers.any_type_specifiers_p
20257 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20258 goto out;
20259 /* If there is no declarator, then the decl-specifier-seq should
20260 specify a type. */
20261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20262 {
20263 /* If there was no decl-specifier-seq, and the next token is a
20264 `;', then we have something like:
20265
20266 struct S { ; };
20267
20268 [class.mem]
20269
20270 Each member-declaration shall declare at least one member
20271 name of the class. */
20272 if (!decl_specifiers.any_specifiers_p)
20273 {
20274 cp_token *token = cp_lexer_peek_token (parser->lexer);
20275 if (!in_system_header_at (token->location))
20276 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20277 }
20278 else
20279 {
20280 tree type;
20281
20282 /* See if this declaration is a friend. */
20283 friend_p = cp_parser_friend_p (&decl_specifiers);
20284 /* If there were decl-specifiers, check to see if there was
20285 a class-declaration. */
20286 type = check_tag_decl (&decl_specifiers,
20287 /*explicit_type_instantiation_p=*/false);
20288 /* Nested classes have already been added to the class, but
20289 a `friend' needs to be explicitly registered. */
20290 if (friend_p)
20291 {
20292 /* If the `friend' keyword was present, the friend must
20293 be introduced with a class-key. */
20294 if (!declares_class_or_enum && cxx_dialect < cxx11)
20295 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20296 "in C++03 a class-key must be used "
20297 "when declaring a friend");
20298 /* In this case:
20299
20300 template <typename T> struct A {
20301 friend struct A<T>::B;
20302 };
20303
20304 A<T>::B will be represented by a TYPENAME_TYPE, and
20305 therefore not recognized by check_tag_decl. */
20306 if (!type)
20307 {
20308 type = decl_specifiers.type;
20309 if (type && TREE_CODE (type) == TYPE_DECL)
20310 type = TREE_TYPE (type);
20311 }
20312 if (!type || !TYPE_P (type))
20313 error_at (decl_spec_token_start->location,
20314 "friend declaration does not name a class or "
20315 "function");
20316 else
20317 make_friend_class (current_class_type, type,
20318 /*complain=*/true);
20319 }
20320 /* If there is no TYPE, an error message will already have
20321 been issued. */
20322 else if (!type || type == error_mark_node)
20323 ;
20324 /* An anonymous aggregate has to be handled specially; such
20325 a declaration really declares a data member (with a
20326 particular type), as opposed to a nested class. */
20327 else if (ANON_AGGR_TYPE_P (type))
20328 {
20329 /* C++11 9.5/6. */
20330 if (decl_specifiers.storage_class != sc_none)
20331 error_at (decl_spec_token_start->location,
20332 "a storage class on an anonymous aggregate "
20333 "in class scope is not allowed");
20334
20335 /* Remove constructors and such from TYPE, now that we
20336 know it is an anonymous aggregate. */
20337 fixup_anonymous_aggr (type);
20338 /* And make the corresponding data member. */
20339 decl = build_decl (decl_spec_token_start->location,
20340 FIELD_DECL, NULL_TREE, type);
20341 /* Add it to the class. */
20342 finish_member_declaration (decl);
20343 }
20344 else
20345 cp_parser_check_access_in_redeclaration
20346 (TYPE_NAME (type),
20347 decl_spec_token_start->location);
20348 }
20349 }
20350 else
20351 {
20352 bool assume_semicolon = false;
20353
20354 /* Clear attributes from the decl_specifiers but keep them
20355 around as prefix attributes that apply them to the entity
20356 being declared. */
20357 prefix_attributes = decl_specifiers.attributes;
20358 decl_specifiers.attributes = NULL_TREE;
20359
20360 /* See if these declarations will be friends. */
20361 friend_p = cp_parser_friend_p (&decl_specifiers);
20362
20363 /* Keep going until we hit the `;' at the end of the
20364 declaration. */
20365 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20366 {
20367 tree attributes = NULL_TREE;
20368 tree first_attribute;
20369
20370 /* Peek at the next token. */
20371 token = cp_lexer_peek_token (parser->lexer);
20372
20373 /* Check for a bitfield declaration. */
20374 if (token->type == CPP_COLON
20375 || (token->type == CPP_NAME
20376 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20377 == CPP_COLON))
20378 {
20379 tree identifier;
20380 tree width;
20381
20382 /* Get the name of the bitfield. Note that we cannot just
20383 check TOKEN here because it may have been invalidated by
20384 the call to cp_lexer_peek_nth_token above. */
20385 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20386 identifier = cp_parser_identifier (parser);
20387 else
20388 identifier = NULL_TREE;
20389
20390 /* Consume the `:' token. */
20391 cp_lexer_consume_token (parser->lexer);
20392 /* Get the width of the bitfield. */
20393 width
20394 = cp_parser_constant_expression (parser,
20395 /*allow_non_constant=*/false,
20396 NULL);
20397
20398 /* Look for attributes that apply to the bitfield. */
20399 attributes = cp_parser_attributes_opt (parser);
20400 /* Remember which attributes are prefix attributes and
20401 which are not. */
20402 first_attribute = attributes;
20403 /* Combine the attributes. */
20404 attributes = chainon (prefix_attributes, attributes);
20405
20406 /* Create the bitfield declaration. */
20407 decl = grokbitfield (identifier
20408 ? make_id_declarator (NULL_TREE,
20409 identifier,
20410 sfk_none)
20411 : NULL,
20412 &decl_specifiers,
20413 width,
20414 attributes);
20415 }
20416 else
20417 {
20418 cp_declarator *declarator;
20419 tree initializer;
20420 tree asm_specification;
20421 int ctor_dtor_or_conv_p;
20422
20423 /* Parse the declarator. */
20424 declarator
20425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20426 &ctor_dtor_or_conv_p,
20427 /*parenthesized_p=*/NULL,
20428 /*member_p=*/true);
20429
20430 /* If something went wrong parsing the declarator, make sure
20431 that we at least consume some tokens. */
20432 if (declarator == cp_error_declarator)
20433 {
20434 /* Skip to the end of the statement. */
20435 cp_parser_skip_to_end_of_statement (parser);
20436 /* If the next token is not a semicolon, that is
20437 probably because we just skipped over the body of
20438 a function. So, we consume a semicolon if
20439 present, but do not issue an error message if it
20440 is not present. */
20441 if (cp_lexer_next_token_is (parser->lexer,
20442 CPP_SEMICOLON))
20443 cp_lexer_consume_token (parser->lexer);
20444 goto out;
20445 }
20446
20447 if (declares_class_or_enum & 2)
20448 cp_parser_check_for_definition_in_return_type
20449 (declarator, decl_specifiers.type,
20450 decl_specifiers.locations[ds_type_spec]);
20451
20452 /* Look for an asm-specification. */
20453 asm_specification = cp_parser_asm_specification_opt (parser);
20454 /* Look for attributes that apply to the declaration. */
20455 attributes = cp_parser_attributes_opt (parser);
20456 /* Remember which attributes are prefix attributes and
20457 which are not. */
20458 first_attribute = attributes;
20459 /* Combine the attributes. */
20460 attributes = chainon (prefix_attributes, attributes);
20461
20462 /* If it's an `=', then we have a constant-initializer or a
20463 pure-specifier. It is not correct to parse the
20464 initializer before registering the member declaration
20465 since the member declaration should be in scope while
20466 its initializer is processed. However, the rest of the
20467 front end does not yet provide an interface that allows
20468 us to handle this correctly. */
20469 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20470 {
20471 /* In [class.mem]:
20472
20473 A pure-specifier shall be used only in the declaration of
20474 a virtual function.
20475
20476 A member-declarator can contain a constant-initializer
20477 only if it declares a static member of integral or
20478 enumeration type.
20479
20480 Therefore, if the DECLARATOR is for a function, we look
20481 for a pure-specifier; otherwise, we look for a
20482 constant-initializer. When we call `grokfield', it will
20483 perform more stringent semantics checks. */
20484 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20485 if (function_declarator_p (declarator)
20486 || (decl_specifiers.type
20487 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20488 && declarator->kind == cdk_id
20489 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20490 == FUNCTION_TYPE)))
20491 initializer = cp_parser_pure_specifier (parser);
20492 else if (decl_specifiers.storage_class != sc_static)
20493 initializer = cp_parser_save_nsdmi (parser);
20494 else if (cxx_dialect >= cxx11)
20495 {
20496 bool nonconst;
20497 /* Don't require a constant rvalue in C++11, since we
20498 might want a reference constant. We'll enforce
20499 constancy later. */
20500 cp_lexer_consume_token (parser->lexer);
20501 /* Parse the initializer. */
20502 initializer = cp_parser_initializer_clause (parser,
20503 &nonconst);
20504 }
20505 else
20506 /* Parse the initializer. */
20507 initializer = cp_parser_constant_initializer (parser);
20508 }
20509 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20510 && !function_declarator_p (declarator))
20511 {
20512 bool x;
20513 if (decl_specifiers.storage_class != sc_static)
20514 initializer = cp_parser_save_nsdmi (parser);
20515 else
20516 initializer = cp_parser_initializer (parser, &x, &x);
20517 }
20518 /* Otherwise, there is no initializer. */
20519 else
20520 initializer = NULL_TREE;
20521
20522 /* See if we are probably looking at a function
20523 definition. We are certainly not looking at a
20524 member-declarator. Calling `grokfield' has
20525 side-effects, so we must not do it unless we are sure
20526 that we are looking at a member-declarator. */
20527 if (cp_parser_token_starts_function_definition_p
20528 (cp_lexer_peek_token (parser->lexer)))
20529 {
20530 /* The grammar does not allow a pure-specifier to be
20531 used when a member function is defined. (It is
20532 possible that this fact is an oversight in the
20533 standard, since a pure function may be defined
20534 outside of the class-specifier. */
20535 if (initializer && initializer_token_start)
20536 error_at (initializer_token_start->location,
20537 "pure-specifier on function-definition");
20538 decl = cp_parser_save_member_function_body (parser,
20539 &decl_specifiers,
20540 declarator,
20541 attributes);
20542 if (parser->fully_implicit_function_template_p)
20543 decl = finish_fully_implicit_template (parser, decl);
20544 /* If the member was not a friend, declare it here. */
20545 if (!friend_p)
20546 finish_member_declaration (decl);
20547 /* Peek at the next token. */
20548 token = cp_lexer_peek_token (parser->lexer);
20549 /* If the next token is a semicolon, consume it. */
20550 if (token->type == CPP_SEMICOLON)
20551 cp_lexer_consume_token (parser->lexer);
20552 goto out;
20553 }
20554 else
20555 if (declarator->kind == cdk_function)
20556 declarator->id_loc = token->location;
20557 /* Create the declaration. */
20558 decl = grokfield (declarator, &decl_specifiers,
20559 initializer, /*init_const_expr_p=*/true,
20560 asm_specification, attributes);
20561 if (parser->fully_implicit_function_template_p)
20562 {
20563 if (friend_p)
20564 finish_fully_implicit_template (parser, 0);
20565 else
20566 decl = finish_fully_implicit_template (parser, decl);
20567 }
20568 }
20569
20570 cp_finalize_omp_declare_simd (parser, decl);
20571
20572 /* Reset PREFIX_ATTRIBUTES. */
20573 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20574 attributes = TREE_CHAIN (attributes);
20575 if (attributes)
20576 TREE_CHAIN (attributes) = NULL_TREE;
20577
20578 /* If there is any qualification still in effect, clear it
20579 now; we will be starting fresh with the next declarator. */
20580 parser->scope = NULL_TREE;
20581 parser->qualifying_scope = NULL_TREE;
20582 parser->object_scope = NULL_TREE;
20583 /* If it's a `,', then there are more declarators. */
20584 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20585 {
20586 cp_lexer_consume_token (parser->lexer);
20587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20588 {
20589 cp_token *token = cp_lexer_previous_token (parser->lexer);
20590 error_at (token->location,
20591 "stray %<,%> at end of member declaration");
20592 }
20593 }
20594 /* If the next token isn't a `;', then we have a parse error. */
20595 else if (cp_lexer_next_token_is_not (parser->lexer,
20596 CPP_SEMICOLON))
20597 {
20598 /* The next token might be a ways away from where the
20599 actual semicolon is missing. Find the previous token
20600 and use that for our error position. */
20601 cp_token *token = cp_lexer_previous_token (parser->lexer);
20602 error_at (token->location,
20603 "expected %<;%> at end of member declaration");
20604
20605 /* Assume that the user meant to provide a semicolon. If
20606 we were to cp_parser_skip_to_end_of_statement, we might
20607 skip to a semicolon inside a member function definition
20608 and issue nonsensical error messages. */
20609 assume_semicolon = true;
20610 }
20611
20612 if (decl)
20613 {
20614 /* Add DECL to the list of members. */
20615 if (!friend_p)
20616 finish_member_declaration (decl);
20617
20618 if (TREE_CODE (decl) == FUNCTION_DECL)
20619 cp_parser_save_default_args (parser, decl);
20620 else if (TREE_CODE (decl) == FIELD_DECL
20621 && !DECL_C_BIT_FIELD (decl)
20622 && DECL_INITIAL (decl))
20623 /* Add DECL to the queue of NSDMI to be parsed later. */
20624 vec_safe_push (unparsed_nsdmis, decl);
20625 }
20626
20627 if (assume_semicolon)
20628 goto out;
20629 }
20630 }
20631
20632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20633 out:
20634 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20635 }
20636
20637 /* Parse a pure-specifier.
20638
20639 pure-specifier:
20640 = 0
20641
20642 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20643 Otherwise, ERROR_MARK_NODE is returned. */
20644
20645 static tree
20646 cp_parser_pure_specifier (cp_parser* parser)
20647 {
20648 cp_token *token;
20649
20650 /* Look for the `=' token. */
20651 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20652 return error_mark_node;
20653 /* Look for the `0' token. */
20654 token = cp_lexer_peek_token (parser->lexer);
20655
20656 if (token->type == CPP_EOF
20657 || token->type == CPP_PRAGMA_EOL)
20658 return error_mark_node;
20659
20660 cp_lexer_consume_token (parser->lexer);
20661
20662 /* Accept = default or = delete in c++0x mode. */
20663 if (token->keyword == RID_DEFAULT
20664 || token->keyword == RID_DELETE)
20665 {
20666 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20667 return token->u.value;
20668 }
20669
20670 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20671 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20672 {
20673 cp_parser_error (parser,
20674 "invalid pure specifier (only %<= 0%> is allowed)");
20675 cp_parser_skip_to_end_of_statement (parser);
20676 return error_mark_node;
20677 }
20678 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20679 {
20680 error_at (token->location, "templates may not be %<virtual%>");
20681 return error_mark_node;
20682 }
20683
20684 return integer_zero_node;
20685 }
20686
20687 /* Parse a constant-initializer.
20688
20689 constant-initializer:
20690 = constant-expression
20691
20692 Returns a representation of the constant-expression. */
20693
20694 static tree
20695 cp_parser_constant_initializer (cp_parser* parser)
20696 {
20697 /* Look for the `=' token. */
20698 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20699 return error_mark_node;
20700
20701 /* It is invalid to write:
20702
20703 struct S { static const int i = { 7 }; };
20704
20705 */
20706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20707 {
20708 cp_parser_error (parser,
20709 "a brace-enclosed initializer is not allowed here");
20710 /* Consume the opening brace. */
20711 cp_lexer_consume_token (parser->lexer);
20712 /* Skip the initializer. */
20713 cp_parser_skip_to_closing_brace (parser);
20714 /* Look for the trailing `}'. */
20715 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20716
20717 return error_mark_node;
20718 }
20719
20720 return cp_parser_constant_expression (parser,
20721 /*allow_non_constant=*/false,
20722 NULL);
20723 }
20724
20725 /* Derived classes [gram.class.derived] */
20726
20727 /* Parse a base-clause.
20728
20729 base-clause:
20730 : base-specifier-list
20731
20732 base-specifier-list:
20733 base-specifier ... [opt]
20734 base-specifier-list , base-specifier ... [opt]
20735
20736 Returns a TREE_LIST representing the base-classes, in the order in
20737 which they were declared. The representation of each node is as
20738 described by cp_parser_base_specifier.
20739
20740 In the case that no bases are specified, this function will return
20741 NULL_TREE, not ERROR_MARK_NODE. */
20742
20743 static tree
20744 cp_parser_base_clause (cp_parser* parser)
20745 {
20746 tree bases = NULL_TREE;
20747
20748 /* Look for the `:' that begins the list. */
20749 cp_parser_require (parser, CPP_COLON, RT_COLON);
20750
20751 /* Scan the base-specifier-list. */
20752 while (true)
20753 {
20754 cp_token *token;
20755 tree base;
20756 bool pack_expansion_p = false;
20757
20758 /* Look for the base-specifier. */
20759 base = cp_parser_base_specifier (parser);
20760 /* Look for the (optional) ellipsis. */
20761 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20762 {
20763 /* Consume the `...'. */
20764 cp_lexer_consume_token (parser->lexer);
20765
20766 pack_expansion_p = true;
20767 }
20768
20769 /* Add BASE to the front of the list. */
20770 if (base && base != error_mark_node)
20771 {
20772 if (pack_expansion_p)
20773 /* Make this a pack expansion type. */
20774 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20775
20776 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20777 {
20778 TREE_CHAIN (base) = bases;
20779 bases = base;
20780 }
20781 }
20782 /* Peek at the next token. */
20783 token = cp_lexer_peek_token (parser->lexer);
20784 /* If it's not a comma, then the list is complete. */
20785 if (token->type != CPP_COMMA)
20786 break;
20787 /* Consume the `,'. */
20788 cp_lexer_consume_token (parser->lexer);
20789 }
20790
20791 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20792 base class had a qualified name. However, the next name that
20793 appears is certainly not qualified. */
20794 parser->scope = NULL_TREE;
20795 parser->qualifying_scope = NULL_TREE;
20796 parser->object_scope = NULL_TREE;
20797
20798 return nreverse (bases);
20799 }
20800
20801 /* Parse a base-specifier.
20802
20803 base-specifier:
20804 :: [opt] nested-name-specifier [opt] class-name
20805 virtual access-specifier [opt] :: [opt] nested-name-specifier
20806 [opt] class-name
20807 access-specifier virtual [opt] :: [opt] nested-name-specifier
20808 [opt] class-name
20809
20810 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20811 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20812 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20813 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20814
20815 static tree
20816 cp_parser_base_specifier (cp_parser* parser)
20817 {
20818 cp_token *token;
20819 bool done = false;
20820 bool virtual_p = false;
20821 bool duplicate_virtual_error_issued_p = false;
20822 bool duplicate_access_error_issued_p = false;
20823 bool class_scope_p, template_p;
20824 tree access = access_default_node;
20825 tree type;
20826
20827 /* Process the optional `virtual' and `access-specifier'. */
20828 while (!done)
20829 {
20830 /* Peek at the next token. */
20831 token = cp_lexer_peek_token (parser->lexer);
20832 /* Process `virtual'. */
20833 switch (token->keyword)
20834 {
20835 case RID_VIRTUAL:
20836 /* If `virtual' appears more than once, issue an error. */
20837 if (virtual_p && !duplicate_virtual_error_issued_p)
20838 {
20839 cp_parser_error (parser,
20840 "%<virtual%> specified more than once in base-specified");
20841 duplicate_virtual_error_issued_p = true;
20842 }
20843
20844 virtual_p = true;
20845
20846 /* Consume the `virtual' token. */
20847 cp_lexer_consume_token (parser->lexer);
20848
20849 break;
20850
20851 case RID_PUBLIC:
20852 case RID_PROTECTED:
20853 case RID_PRIVATE:
20854 /* If more than one access specifier appears, issue an
20855 error. */
20856 if (access != access_default_node
20857 && !duplicate_access_error_issued_p)
20858 {
20859 cp_parser_error (parser,
20860 "more than one access specifier in base-specified");
20861 duplicate_access_error_issued_p = true;
20862 }
20863
20864 access = ridpointers[(int) token->keyword];
20865
20866 /* Consume the access-specifier. */
20867 cp_lexer_consume_token (parser->lexer);
20868
20869 break;
20870
20871 default:
20872 done = true;
20873 break;
20874 }
20875 }
20876 /* It is not uncommon to see programs mechanically, erroneously, use
20877 the 'typename' keyword to denote (dependent) qualified types
20878 as base classes. */
20879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20880 {
20881 token = cp_lexer_peek_token (parser->lexer);
20882 if (!processing_template_decl)
20883 error_at (token->location,
20884 "keyword %<typename%> not allowed outside of templates");
20885 else
20886 error_at (token->location,
20887 "keyword %<typename%> not allowed in this context "
20888 "(the base class is implicitly a type)");
20889 cp_lexer_consume_token (parser->lexer);
20890 }
20891
20892 /* Look for the optional `::' operator. */
20893 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20894 /* Look for the nested-name-specifier. The simplest way to
20895 implement:
20896
20897 [temp.res]
20898
20899 The keyword `typename' is not permitted in a base-specifier or
20900 mem-initializer; in these contexts a qualified name that
20901 depends on a template-parameter is implicitly assumed to be a
20902 type name.
20903
20904 is to pretend that we have seen the `typename' keyword at this
20905 point. */
20906 cp_parser_nested_name_specifier_opt (parser,
20907 /*typename_keyword_p=*/true,
20908 /*check_dependency_p=*/true,
20909 typename_type,
20910 /*is_declaration=*/true);
20911 /* If the base class is given by a qualified name, assume that names
20912 we see are type names or templates, as appropriate. */
20913 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20914 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20915
20916 if (!parser->scope
20917 && cp_lexer_next_token_is_decltype (parser->lexer))
20918 /* DR 950 allows decltype as a base-specifier. */
20919 type = cp_parser_decltype (parser);
20920 else
20921 {
20922 /* Otherwise, look for the class-name. */
20923 type = cp_parser_class_name (parser,
20924 class_scope_p,
20925 template_p,
20926 typename_type,
20927 /*check_dependency_p=*/true,
20928 /*class_head_p=*/false,
20929 /*is_declaration=*/true);
20930 type = TREE_TYPE (type);
20931 }
20932
20933 if (type == error_mark_node)
20934 return error_mark_node;
20935
20936 return finish_base_specifier (type, access, virtual_p);
20937 }
20938
20939 /* Exception handling [gram.exception] */
20940
20941 /* Parse an (optional) noexcept-specification.
20942
20943 noexcept-specification:
20944 noexcept ( constant-expression ) [opt]
20945
20946 If no noexcept-specification is present, returns NULL_TREE.
20947 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20948 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20949 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20950 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20951 in which case a boolean condition is returned instead. */
20952
20953 static tree
20954 cp_parser_noexcept_specification_opt (cp_parser* parser,
20955 bool require_constexpr,
20956 bool* consumed_expr,
20957 bool return_cond)
20958 {
20959 cp_token *token;
20960 const char *saved_message;
20961
20962 /* Peek at the next token. */
20963 token = cp_lexer_peek_token (parser->lexer);
20964
20965 /* Is it a noexcept-specification? */
20966 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20967 {
20968 tree expr;
20969 cp_lexer_consume_token (parser->lexer);
20970
20971 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20972 {
20973 cp_lexer_consume_token (parser->lexer);
20974
20975 if (require_constexpr)
20976 {
20977 /* Types may not be defined in an exception-specification. */
20978 saved_message = parser->type_definition_forbidden_message;
20979 parser->type_definition_forbidden_message
20980 = G_("types may not be defined in an exception-specification");
20981
20982 expr = cp_parser_constant_expression (parser, false, NULL);
20983
20984 /* Restore the saved message. */
20985 parser->type_definition_forbidden_message = saved_message;
20986 }
20987 else
20988 {
20989 expr = cp_parser_expression (parser, false, NULL);
20990 *consumed_expr = true;
20991 }
20992
20993 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20994 }
20995 else
20996 {
20997 expr = boolean_true_node;
20998 if (!require_constexpr)
20999 *consumed_expr = false;
21000 }
21001
21002 /* We cannot build a noexcept-spec right away because this will check
21003 that expr is a constexpr. */
21004 if (!return_cond)
21005 return build_noexcept_spec (expr, tf_warning_or_error);
21006 else
21007 return expr;
21008 }
21009 else
21010 return NULL_TREE;
21011 }
21012
21013 /* Parse an (optional) exception-specification.
21014
21015 exception-specification:
21016 throw ( type-id-list [opt] )
21017
21018 Returns a TREE_LIST representing the exception-specification. The
21019 TREE_VALUE of each node is a type. */
21020
21021 static tree
21022 cp_parser_exception_specification_opt (cp_parser* parser)
21023 {
21024 cp_token *token;
21025 tree type_id_list;
21026 const char *saved_message;
21027
21028 /* Peek at the next token. */
21029 token = cp_lexer_peek_token (parser->lexer);
21030
21031 /* Is it a noexcept-specification? */
21032 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21033 false);
21034 if (type_id_list != NULL_TREE)
21035 return type_id_list;
21036
21037 /* If it's not `throw', then there's no exception-specification. */
21038 if (!cp_parser_is_keyword (token, RID_THROW))
21039 return NULL_TREE;
21040
21041 #if 0
21042 /* Enable this once a lot of code has transitioned to noexcept? */
21043 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21044 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21045 "deprecated in C++0x; use %<noexcept%> instead");
21046 #endif
21047
21048 /* Consume the `throw'. */
21049 cp_lexer_consume_token (parser->lexer);
21050
21051 /* Look for the `('. */
21052 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21053
21054 /* Peek at the next token. */
21055 token = cp_lexer_peek_token (parser->lexer);
21056 /* If it's not a `)', then there is a type-id-list. */
21057 if (token->type != CPP_CLOSE_PAREN)
21058 {
21059 /* Types may not be defined in an exception-specification. */
21060 saved_message = parser->type_definition_forbidden_message;
21061 parser->type_definition_forbidden_message
21062 = G_("types may not be defined in an exception-specification");
21063 /* Parse the type-id-list. */
21064 type_id_list = cp_parser_type_id_list (parser);
21065 /* Restore the saved message. */
21066 parser->type_definition_forbidden_message = saved_message;
21067 }
21068 else
21069 type_id_list = empty_except_spec;
21070
21071 /* Look for the `)'. */
21072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21073
21074 return type_id_list;
21075 }
21076
21077 /* Parse an (optional) type-id-list.
21078
21079 type-id-list:
21080 type-id ... [opt]
21081 type-id-list , type-id ... [opt]
21082
21083 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21084 in the order that the types were presented. */
21085
21086 static tree
21087 cp_parser_type_id_list (cp_parser* parser)
21088 {
21089 tree types = NULL_TREE;
21090
21091 while (true)
21092 {
21093 cp_token *token;
21094 tree type;
21095
21096 /* Get the next type-id. */
21097 type = cp_parser_type_id (parser);
21098 /* Parse the optional ellipsis. */
21099 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21100 {
21101 /* Consume the `...'. */
21102 cp_lexer_consume_token (parser->lexer);
21103
21104 /* Turn the type into a pack expansion expression. */
21105 type = make_pack_expansion (type);
21106 }
21107 /* Add it to the list. */
21108 types = add_exception_specifier (types, type, /*complain=*/1);
21109 /* Peek at the next token. */
21110 token = cp_lexer_peek_token (parser->lexer);
21111 /* If it is not a `,', we are done. */
21112 if (token->type != CPP_COMMA)
21113 break;
21114 /* Consume the `,'. */
21115 cp_lexer_consume_token (parser->lexer);
21116 }
21117
21118 return nreverse (types);
21119 }
21120
21121 /* Parse a try-block.
21122
21123 try-block:
21124 try compound-statement handler-seq */
21125
21126 static tree
21127 cp_parser_try_block (cp_parser* parser)
21128 {
21129 tree try_block;
21130
21131 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21132 try_block = begin_try_block ();
21133 cp_parser_compound_statement (parser, NULL, true, false);
21134 finish_try_block (try_block);
21135 cp_parser_handler_seq (parser);
21136 finish_handler_sequence (try_block);
21137
21138 return try_block;
21139 }
21140
21141 /* Parse a function-try-block.
21142
21143 function-try-block:
21144 try ctor-initializer [opt] function-body handler-seq */
21145
21146 static bool
21147 cp_parser_function_try_block (cp_parser* parser)
21148 {
21149 tree compound_stmt;
21150 tree try_block;
21151 bool ctor_initializer_p;
21152
21153 /* Look for the `try' keyword. */
21154 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21155 return false;
21156 /* Let the rest of the front end know where we are. */
21157 try_block = begin_function_try_block (&compound_stmt);
21158 /* Parse the function-body. */
21159 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21160 (parser, /*in_function_try_block=*/true);
21161 /* We're done with the `try' part. */
21162 finish_function_try_block (try_block);
21163 /* Parse the handlers. */
21164 cp_parser_handler_seq (parser);
21165 /* We're done with the handlers. */
21166 finish_function_handler_sequence (try_block, compound_stmt);
21167
21168 return ctor_initializer_p;
21169 }
21170
21171 /* Parse a handler-seq.
21172
21173 handler-seq:
21174 handler handler-seq [opt] */
21175
21176 static void
21177 cp_parser_handler_seq (cp_parser* parser)
21178 {
21179 while (true)
21180 {
21181 cp_token *token;
21182
21183 /* Parse the handler. */
21184 cp_parser_handler (parser);
21185 /* Peek at the next token. */
21186 token = cp_lexer_peek_token (parser->lexer);
21187 /* If it's not `catch' then there are no more handlers. */
21188 if (!cp_parser_is_keyword (token, RID_CATCH))
21189 break;
21190 }
21191 }
21192
21193 /* Parse a handler.
21194
21195 handler:
21196 catch ( exception-declaration ) compound-statement */
21197
21198 static void
21199 cp_parser_handler (cp_parser* parser)
21200 {
21201 tree handler;
21202 tree declaration;
21203
21204 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21205 handler = begin_handler ();
21206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21207 declaration = cp_parser_exception_declaration (parser);
21208 finish_handler_parms (declaration, handler);
21209 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21210 cp_parser_compound_statement (parser, NULL, false, false);
21211 finish_handler (handler);
21212 }
21213
21214 /* Parse an exception-declaration.
21215
21216 exception-declaration:
21217 type-specifier-seq declarator
21218 type-specifier-seq abstract-declarator
21219 type-specifier-seq
21220 ...
21221
21222 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21223 ellipsis variant is used. */
21224
21225 static tree
21226 cp_parser_exception_declaration (cp_parser* parser)
21227 {
21228 cp_decl_specifier_seq type_specifiers;
21229 cp_declarator *declarator;
21230 const char *saved_message;
21231
21232 /* If it's an ellipsis, it's easy to handle. */
21233 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21234 {
21235 /* Consume the `...' token. */
21236 cp_lexer_consume_token (parser->lexer);
21237 return NULL_TREE;
21238 }
21239
21240 /* Types may not be defined in exception-declarations. */
21241 saved_message = parser->type_definition_forbidden_message;
21242 parser->type_definition_forbidden_message
21243 = G_("types may not be defined in exception-declarations");
21244
21245 /* Parse the type-specifier-seq. */
21246 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21247 /*is_trailing_return=*/false,
21248 &type_specifiers);
21249 /* If it's a `)', then there is no declarator. */
21250 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21251 declarator = NULL;
21252 else
21253 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21254 /*ctor_dtor_or_conv_p=*/NULL,
21255 /*parenthesized_p=*/NULL,
21256 /*member_p=*/false);
21257
21258 /* Restore the saved message. */
21259 parser->type_definition_forbidden_message = saved_message;
21260
21261 if (!type_specifiers.any_specifiers_p)
21262 return error_mark_node;
21263
21264 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21265 }
21266
21267 /* Parse a throw-expression.
21268
21269 throw-expression:
21270 throw assignment-expression [opt]
21271
21272 Returns a THROW_EXPR representing the throw-expression. */
21273
21274 static tree
21275 cp_parser_throw_expression (cp_parser* parser)
21276 {
21277 tree expression;
21278 cp_token* token;
21279
21280 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21281 token = cp_lexer_peek_token (parser->lexer);
21282 /* Figure out whether or not there is an assignment-expression
21283 following the "throw" keyword. */
21284 if (token->type == CPP_COMMA
21285 || token->type == CPP_SEMICOLON
21286 || token->type == CPP_CLOSE_PAREN
21287 || token->type == CPP_CLOSE_SQUARE
21288 || token->type == CPP_CLOSE_BRACE
21289 || token->type == CPP_COLON)
21290 expression = NULL_TREE;
21291 else
21292 expression = cp_parser_assignment_expression (parser,
21293 /*cast_p=*/false, NULL);
21294
21295 return build_throw (expression);
21296 }
21297
21298 /* GNU Extensions */
21299
21300 /* Parse an (optional) asm-specification.
21301
21302 asm-specification:
21303 asm ( string-literal )
21304
21305 If the asm-specification is present, returns a STRING_CST
21306 corresponding to the string-literal. Otherwise, returns
21307 NULL_TREE. */
21308
21309 static tree
21310 cp_parser_asm_specification_opt (cp_parser* parser)
21311 {
21312 cp_token *token;
21313 tree asm_specification;
21314
21315 /* Peek at the next token. */
21316 token = cp_lexer_peek_token (parser->lexer);
21317 /* If the next token isn't the `asm' keyword, then there's no
21318 asm-specification. */
21319 if (!cp_parser_is_keyword (token, RID_ASM))
21320 return NULL_TREE;
21321
21322 /* Consume the `asm' token. */
21323 cp_lexer_consume_token (parser->lexer);
21324 /* Look for the `('. */
21325 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21326
21327 /* Look for the string-literal. */
21328 asm_specification = cp_parser_string_literal (parser, false, false);
21329
21330 /* Look for the `)'. */
21331 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21332
21333 return asm_specification;
21334 }
21335
21336 /* Parse an asm-operand-list.
21337
21338 asm-operand-list:
21339 asm-operand
21340 asm-operand-list , asm-operand
21341
21342 asm-operand:
21343 string-literal ( expression )
21344 [ string-literal ] string-literal ( expression )
21345
21346 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21347 each node is the expression. The TREE_PURPOSE is itself a
21348 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21349 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21350 is a STRING_CST for the string literal before the parenthesis. Returns
21351 ERROR_MARK_NODE if any of the operands are invalid. */
21352
21353 static tree
21354 cp_parser_asm_operand_list (cp_parser* parser)
21355 {
21356 tree asm_operands = NULL_TREE;
21357 bool invalid_operands = false;
21358
21359 while (true)
21360 {
21361 tree string_literal;
21362 tree expression;
21363 tree name;
21364
21365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21366 {
21367 /* Consume the `[' token. */
21368 cp_lexer_consume_token (parser->lexer);
21369 /* Read the operand name. */
21370 name = cp_parser_identifier (parser);
21371 if (name != error_mark_node)
21372 name = build_string (IDENTIFIER_LENGTH (name),
21373 IDENTIFIER_POINTER (name));
21374 /* Look for the closing `]'. */
21375 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21376 }
21377 else
21378 name = NULL_TREE;
21379 /* Look for the string-literal. */
21380 string_literal = cp_parser_string_literal (parser, false, false);
21381
21382 /* Look for the `('. */
21383 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21384 /* Parse the expression. */
21385 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21386 /* Look for the `)'. */
21387 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21388
21389 if (name == error_mark_node
21390 || string_literal == error_mark_node
21391 || expression == error_mark_node)
21392 invalid_operands = true;
21393
21394 /* Add this operand to the list. */
21395 asm_operands = tree_cons (build_tree_list (name, string_literal),
21396 expression,
21397 asm_operands);
21398 /* If the next token is not a `,', there are no more
21399 operands. */
21400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21401 break;
21402 /* Consume the `,'. */
21403 cp_lexer_consume_token (parser->lexer);
21404 }
21405
21406 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21407 }
21408
21409 /* Parse an asm-clobber-list.
21410
21411 asm-clobber-list:
21412 string-literal
21413 asm-clobber-list , string-literal
21414
21415 Returns a TREE_LIST, indicating the clobbers in the order that they
21416 appeared. The TREE_VALUE of each node is a STRING_CST. */
21417
21418 static tree
21419 cp_parser_asm_clobber_list (cp_parser* parser)
21420 {
21421 tree clobbers = NULL_TREE;
21422
21423 while (true)
21424 {
21425 tree string_literal;
21426
21427 /* Look for the string literal. */
21428 string_literal = cp_parser_string_literal (parser, false, false);
21429 /* Add it to the list. */
21430 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21431 /* If the next token is not a `,', then the list is
21432 complete. */
21433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21434 break;
21435 /* Consume the `,' token. */
21436 cp_lexer_consume_token (parser->lexer);
21437 }
21438
21439 return clobbers;
21440 }
21441
21442 /* Parse an asm-label-list.
21443
21444 asm-label-list:
21445 identifier
21446 asm-label-list , identifier
21447
21448 Returns a TREE_LIST, indicating the labels in the order that they
21449 appeared. The TREE_VALUE of each node is a label. */
21450
21451 static tree
21452 cp_parser_asm_label_list (cp_parser* parser)
21453 {
21454 tree labels = NULL_TREE;
21455
21456 while (true)
21457 {
21458 tree identifier, label, name;
21459
21460 /* Look for the identifier. */
21461 identifier = cp_parser_identifier (parser);
21462 if (!error_operand_p (identifier))
21463 {
21464 label = lookup_label (identifier);
21465 if (TREE_CODE (label) == LABEL_DECL)
21466 {
21467 TREE_USED (label) = 1;
21468 check_goto (label);
21469 name = build_string (IDENTIFIER_LENGTH (identifier),
21470 IDENTIFIER_POINTER (identifier));
21471 labels = tree_cons (name, label, labels);
21472 }
21473 }
21474 /* If the next token is not a `,', then the list is
21475 complete. */
21476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21477 break;
21478 /* Consume the `,' token. */
21479 cp_lexer_consume_token (parser->lexer);
21480 }
21481
21482 return nreverse (labels);
21483 }
21484
21485 /* Return TRUE iff the next tokens in the stream are possibly the
21486 beginning of a GNU extension attribute. */
21487
21488 static bool
21489 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21490 {
21491 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21492 }
21493
21494 /* Return TRUE iff the next tokens in the stream are possibly the
21495 beginning of a standard C++-11 attribute specifier. */
21496
21497 static bool
21498 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21499 {
21500 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21501 }
21502
21503 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21504 beginning of a standard C++-11 attribute specifier. */
21505
21506 static bool
21507 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21508 {
21509 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21510
21511 return (cxx_dialect >= cxx11
21512 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21513 || (token->type == CPP_OPEN_SQUARE
21514 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21515 && token->type == CPP_OPEN_SQUARE)));
21516 }
21517
21518 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21519 beginning of a GNU extension attribute. */
21520
21521 static bool
21522 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21523 {
21524 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21525
21526 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21527 }
21528
21529 /* Return true iff the next tokens can be the beginning of either a
21530 GNU attribute list, or a standard C++11 attribute sequence. */
21531
21532 static bool
21533 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21534 {
21535 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21536 || cp_next_tokens_can_be_std_attribute_p (parser));
21537 }
21538
21539 /* Return true iff the next Nth tokens can be the beginning of either
21540 a GNU attribute list, or a standard C++11 attribute sequence. */
21541
21542 static bool
21543 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21544 {
21545 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21546 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21547 }
21548
21549 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21550 of GNU attributes, or return NULL. */
21551
21552 static tree
21553 cp_parser_attributes_opt (cp_parser *parser)
21554 {
21555 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21556 return cp_parser_gnu_attributes_opt (parser);
21557 return cp_parser_std_attribute_spec_seq (parser);
21558 }
21559
21560 #define CILK_SIMD_FN_CLAUSE_MASK \
21561 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21562 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21563 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21564 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21565 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21566
21567 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21568 vector [(<clauses>)] */
21569
21570 static void
21571 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21572 {
21573 bool first_p = parser->cilk_simd_fn_info == NULL;
21574 cp_token *token = v_token;
21575 if (first_p)
21576 {
21577 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21578 parser->cilk_simd_fn_info->error_seen = false;
21579 parser->cilk_simd_fn_info->fndecl_seen = false;
21580 parser->cilk_simd_fn_info->tokens = vNULL;
21581 }
21582 int paren_scope = 0;
21583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21584 {
21585 cp_lexer_consume_token (parser->lexer);
21586 v_token = cp_lexer_peek_token (parser->lexer);
21587 paren_scope++;
21588 }
21589 while (paren_scope > 0)
21590 {
21591 token = cp_lexer_peek_token (parser->lexer);
21592 if (token->type == CPP_OPEN_PAREN)
21593 paren_scope++;
21594 else if (token->type == CPP_CLOSE_PAREN)
21595 paren_scope--;
21596 /* Do not push the last ')' */
21597 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21598 cp_lexer_consume_token (parser->lexer);
21599 }
21600
21601 token->type = CPP_PRAGMA_EOL;
21602 parser->lexer->next_token = token;
21603 cp_lexer_consume_token (parser->lexer);
21604
21605 struct cp_token_cache *cp
21606 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21607 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21608 }
21609
21610 /* Parse an (optional) series of attributes.
21611
21612 attributes:
21613 attributes attribute
21614
21615 attribute:
21616 __attribute__ (( attribute-list [opt] ))
21617
21618 The return value is as for cp_parser_gnu_attribute_list. */
21619
21620 static tree
21621 cp_parser_gnu_attributes_opt (cp_parser* parser)
21622 {
21623 tree attributes = NULL_TREE;
21624
21625 while (true)
21626 {
21627 cp_token *token;
21628 tree attribute_list;
21629 bool ok = true;
21630
21631 /* Peek at the next token. */
21632 token = cp_lexer_peek_token (parser->lexer);
21633 /* If it's not `__attribute__', then we're done. */
21634 if (token->keyword != RID_ATTRIBUTE)
21635 break;
21636
21637 /* Consume the `__attribute__' keyword. */
21638 cp_lexer_consume_token (parser->lexer);
21639 /* Look for the two `(' tokens. */
21640 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21642
21643 /* Peek at the next token. */
21644 token = cp_lexer_peek_token (parser->lexer);
21645 if (token->type != CPP_CLOSE_PAREN)
21646 /* Parse the attribute-list. */
21647 attribute_list = cp_parser_gnu_attribute_list (parser);
21648 else
21649 /* If the next token is a `)', then there is no attribute
21650 list. */
21651 attribute_list = NULL;
21652
21653 /* Look for the two `)' tokens. */
21654 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21655 ok = false;
21656 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21657 ok = false;
21658 if (!ok)
21659 cp_parser_skip_to_end_of_statement (parser);
21660
21661 /* Add these new attributes to the list. */
21662 attributes = chainon (attributes, attribute_list);
21663 }
21664
21665 return attributes;
21666 }
21667
21668 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21669 "__vector" or "__vector__." */
21670
21671 static inline bool
21672 is_cilkplus_vector_p (tree name)
21673 {
21674 if (flag_cilkplus && is_attribute_p ("vector", name))
21675 return true;
21676 return false;
21677 }
21678
21679 /* Parse a GNU attribute-list.
21680
21681 attribute-list:
21682 attribute
21683 attribute-list , attribute
21684
21685 attribute:
21686 identifier
21687 identifier ( identifier )
21688 identifier ( identifier , expression-list )
21689 identifier ( expression-list )
21690
21691 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21692 to an attribute. The TREE_PURPOSE of each node is the identifier
21693 indicating which attribute is in use. The TREE_VALUE represents
21694 the arguments, if any. */
21695
21696 static tree
21697 cp_parser_gnu_attribute_list (cp_parser* parser)
21698 {
21699 tree attribute_list = NULL_TREE;
21700 bool save_translate_strings_p = parser->translate_strings_p;
21701
21702 parser->translate_strings_p = false;
21703 while (true)
21704 {
21705 cp_token *token;
21706 tree identifier;
21707 tree attribute;
21708
21709 /* Look for the identifier. We also allow keywords here; for
21710 example `__attribute__ ((const))' is legal. */
21711 token = cp_lexer_peek_token (parser->lexer);
21712 if (token->type == CPP_NAME
21713 || token->type == CPP_KEYWORD)
21714 {
21715 tree arguments = NULL_TREE;
21716
21717 /* Consume the token, but save it since we need it for the
21718 SIMD enabled function parsing. */
21719 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21720
21721 /* Save away the identifier that indicates which attribute
21722 this is. */
21723 identifier = (token->type == CPP_KEYWORD)
21724 /* For keywords, use the canonical spelling, not the
21725 parsed identifier. */
21726 ? ridpointers[(int) token->keyword]
21727 : id_token->u.value;
21728
21729 attribute = build_tree_list (identifier, NULL_TREE);
21730
21731 /* Peek at the next token. */
21732 token = cp_lexer_peek_token (parser->lexer);
21733 /* If it's an `(', then parse the attribute arguments. */
21734 if (token->type == CPP_OPEN_PAREN)
21735 {
21736 vec<tree, va_gc> *vec;
21737 int attr_flag = (attribute_takes_identifier_p (identifier)
21738 ? id_attr : normal_attr);
21739 if (is_cilkplus_vector_p (identifier))
21740 {
21741 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21742 continue;
21743 }
21744 else
21745 vec = cp_parser_parenthesized_expression_list
21746 (parser, attr_flag, /*cast_p=*/false,
21747 /*allow_expansion_p=*/false,
21748 /*non_constant_p=*/NULL);
21749 if (vec == NULL)
21750 arguments = error_mark_node;
21751 else
21752 {
21753 arguments = build_tree_list_vec (vec);
21754 release_tree_vector (vec);
21755 }
21756 /* Save the arguments away. */
21757 TREE_VALUE (attribute) = arguments;
21758 }
21759 else if (is_cilkplus_vector_p (identifier))
21760 {
21761 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21762 continue;
21763 }
21764
21765 if (arguments != error_mark_node)
21766 {
21767 /* Add this attribute to the list. */
21768 TREE_CHAIN (attribute) = attribute_list;
21769 attribute_list = attribute;
21770 }
21771
21772 token = cp_lexer_peek_token (parser->lexer);
21773 }
21774 /* Now, look for more attributes. If the next token isn't a
21775 `,', we're done. */
21776 if (token->type != CPP_COMMA)
21777 break;
21778
21779 /* Consume the comma and keep going. */
21780 cp_lexer_consume_token (parser->lexer);
21781 }
21782 parser->translate_strings_p = save_translate_strings_p;
21783
21784 /* We built up the list in reverse order. */
21785 return nreverse (attribute_list);
21786 }
21787
21788 /* Parse a standard C++11 attribute.
21789
21790 The returned representation is a TREE_LIST which TREE_PURPOSE is
21791 the scoped name of the attribute, and the TREE_VALUE is its
21792 arguments list.
21793
21794 Note that the scoped name of the attribute is itself a TREE_LIST
21795 which TREE_PURPOSE is the namespace of the attribute, and
21796 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21797 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21798 and which TREE_PURPOSE is directly the attribute name.
21799
21800 Clients of the attribute code should use get_attribute_namespace
21801 and get_attribute_name to get the actual namespace and name of
21802 attributes, regardless of their being GNU or C++11 attributes.
21803
21804 attribute:
21805 attribute-token attribute-argument-clause [opt]
21806
21807 attribute-token:
21808 identifier
21809 attribute-scoped-token
21810
21811 attribute-scoped-token:
21812 attribute-namespace :: identifier
21813
21814 attribute-namespace:
21815 identifier
21816
21817 attribute-argument-clause:
21818 ( balanced-token-seq )
21819
21820 balanced-token-seq:
21821 balanced-token [opt]
21822 balanced-token-seq balanced-token
21823
21824 balanced-token:
21825 ( balanced-token-seq )
21826 [ balanced-token-seq ]
21827 { balanced-token-seq }. */
21828
21829 static tree
21830 cp_parser_std_attribute (cp_parser *parser)
21831 {
21832 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21833 cp_token *token;
21834
21835 /* First, parse name of the the attribute, a.k.a
21836 attribute-token. */
21837
21838 token = cp_lexer_peek_token (parser->lexer);
21839 if (token->type == CPP_NAME)
21840 attr_id = token->u.value;
21841 else if (token->type == CPP_KEYWORD)
21842 attr_id = ridpointers[(int) token->keyword];
21843 else if (token->flags & NAMED_OP)
21844 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21845
21846 if (attr_id == NULL_TREE)
21847 return NULL_TREE;
21848
21849 cp_lexer_consume_token (parser->lexer);
21850
21851 token = cp_lexer_peek_token (parser->lexer);
21852 if (token->type == CPP_SCOPE)
21853 {
21854 /* We are seeing a scoped attribute token. */
21855
21856 cp_lexer_consume_token (parser->lexer);
21857 attr_ns = attr_id;
21858
21859 token = cp_lexer_consume_token (parser->lexer);
21860 if (token->type == CPP_NAME)
21861 attr_id = token->u.value;
21862 else if (token->type == CPP_KEYWORD)
21863 attr_id = ridpointers[(int) token->keyword];
21864 else
21865 {
21866 error_at (token->location,
21867 "expected an identifier for the attribute name");
21868 return error_mark_node;
21869 }
21870 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21871 NULL_TREE);
21872 token = cp_lexer_peek_token (parser->lexer);
21873 }
21874 else
21875 {
21876 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21877 NULL_TREE);
21878 /* C++11 noreturn attribute is equivalent to GNU's. */
21879 if (is_attribute_p ("noreturn", attr_id))
21880 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21881 /* C++14 deprecated attribute is equivalent to GNU's. */
21882 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21883 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21884 }
21885
21886 /* Now parse the optional argument clause of the attribute. */
21887
21888 if (token->type != CPP_OPEN_PAREN)
21889 return attribute;
21890
21891 {
21892 vec<tree, va_gc> *vec;
21893 int attr_flag = normal_attr;
21894
21895 if (attr_ns == get_identifier ("gnu")
21896 && attribute_takes_identifier_p (attr_id))
21897 /* A GNU attribute that takes an identifier in parameter. */
21898 attr_flag = id_attr;
21899
21900 vec = cp_parser_parenthesized_expression_list
21901 (parser, attr_flag, /*cast_p=*/false,
21902 /*allow_expansion_p=*/true,
21903 /*non_constant_p=*/NULL);
21904 if (vec == NULL)
21905 arguments = error_mark_node;
21906 else
21907 {
21908 arguments = build_tree_list_vec (vec);
21909 release_tree_vector (vec);
21910 }
21911
21912 if (arguments == error_mark_node)
21913 attribute = error_mark_node;
21914 else
21915 TREE_VALUE (attribute) = arguments;
21916 }
21917
21918 return attribute;
21919 }
21920
21921 /* Parse a list of standard C++-11 attributes.
21922
21923 attribute-list:
21924 attribute [opt]
21925 attribute-list , attribute[opt]
21926 attribute ...
21927 attribute-list , attribute ...
21928 */
21929
21930 static tree
21931 cp_parser_std_attribute_list (cp_parser *parser)
21932 {
21933 tree attributes = NULL_TREE, attribute = NULL_TREE;
21934 cp_token *token = NULL;
21935
21936 while (true)
21937 {
21938 attribute = cp_parser_std_attribute (parser);
21939 if (attribute == error_mark_node)
21940 break;
21941 if (attribute != NULL_TREE)
21942 {
21943 TREE_CHAIN (attribute) = attributes;
21944 attributes = attribute;
21945 }
21946 token = cp_lexer_peek_token (parser->lexer);
21947 if (token->type != CPP_COMMA)
21948 break;
21949 cp_lexer_consume_token (parser->lexer);
21950 }
21951 attributes = nreverse (attributes);
21952 return attributes;
21953 }
21954
21955 /* Parse a standard C++-11 attribute specifier.
21956
21957 attribute-specifier:
21958 [ [ attribute-list ] ]
21959 alignment-specifier
21960
21961 alignment-specifier:
21962 alignas ( type-id ... [opt] )
21963 alignas ( alignment-expression ... [opt] ). */
21964
21965 static tree
21966 cp_parser_std_attribute_spec (cp_parser *parser)
21967 {
21968 tree attributes = NULL_TREE;
21969 cp_token *token = cp_lexer_peek_token (parser->lexer);
21970
21971 if (token->type == CPP_OPEN_SQUARE
21972 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21973 {
21974 cp_lexer_consume_token (parser->lexer);
21975 cp_lexer_consume_token (parser->lexer);
21976
21977 attributes = cp_parser_std_attribute_list (parser);
21978
21979 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21980 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21981 cp_parser_skip_to_end_of_statement (parser);
21982 else
21983 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21984 when we are sure that we have actually parsed them. */
21985 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21986 }
21987 else
21988 {
21989 tree alignas_expr;
21990
21991 /* Look for an alignment-specifier. */
21992
21993 token = cp_lexer_peek_token (parser->lexer);
21994
21995 if (token->type != CPP_KEYWORD
21996 || token->keyword != RID_ALIGNAS)
21997 return NULL_TREE;
21998
21999 cp_lexer_consume_token (parser->lexer);
22000 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22001
22002 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22003 {
22004 cp_parser_error (parser, "expected %<(%>");
22005 return error_mark_node;
22006 }
22007
22008 cp_parser_parse_tentatively (parser);
22009 alignas_expr = cp_parser_type_id (parser);
22010
22011 if (!cp_parser_parse_definitely (parser))
22012 {
22013 gcc_assert (alignas_expr == error_mark_node
22014 || alignas_expr == NULL_TREE);
22015
22016 alignas_expr =
22017 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22018 /**cp_id_kind=*/NULL);
22019 if (alignas_expr == error_mark_node)
22020 cp_parser_skip_to_end_of_statement (parser);
22021 if (alignas_expr == NULL_TREE
22022 || alignas_expr == error_mark_node)
22023 return alignas_expr;
22024 }
22025
22026 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22027 {
22028 cp_parser_error (parser, "expected %<)%>");
22029 return error_mark_node;
22030 }
22031
22032 alignas_expr = cxx_alignas_expr (alignas_expr);
22033
22034 /* Build the C++-11 representation of an 'aligned'
22035 attribute. */
22036 attributes =
22037 build_tree_list (build_tree_list (get_identifier ("gnu"),
22038 get_identifier ("aligned")),
22039 build_tree_list (NULL_TREE, alignas_expr));
22040 }
22041
22042 return attributes;
22043 }
22044
22045 /* Parse a standard C++-11 attribute-specifier-seq.
22046
22047 attribute-specifier-seq:
22048 attribute-specifier-seq [opt] attribute-specifier
22049 */
22050
22051 static tree
22052 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22053 {
22054 tree attr_specs = NULL;
22055
22056 while (true)
22057 {
22058 tree attr_spec = cp_parser_std_attribute_spec (parser);
22059 if (attr_spec == NULL_TREE)
22060 break;
22061 if (attr_spec == error_mark_node)
22062 return error_mark_node;
22063
22064 TREE_CHAIN (attr_spec) = attr_specs;
22065 attr_specs = attr_spec;
22066 }
22067
22068 attr_specs = nreverse (attr_specs);
22069 return attr_specs;
22070 }
22071
22072 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22073 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22074 current value of the PEDANTIC flag, regardless of whether or not
22075 the `__extension__' keyword is present. The caller is responsible
22076 for restoring the value of the PEDANTIC flag. */
22077
22078 static bool
22079 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22080 {
22081 /* Save the old value of the PEDANTIC flag. */
22082 *saved_pedantic = pedantic;
22083
22084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22085 {
22086 /* Consume the `__extension__' token. */
22087 cp_lexer_consume_token (parser->lexer);
22088 /* We're not being pedantic while the `__extension__' keyword is
22089 in effect. */
22090 pedantic = 0;
22091
22092 return true;
22093 }
22094
22095 return false;
22096 }
22097
22098 /* Parse a label declaration.
22099
22100 label-declaration:
22101 __label__ label-declarator-seq ;
22102
22103 label-declarator-seq:
22104 identifier , label-declarator-seq
22105 identifier */
22106
22107 static void
22108 cp_parser_label_declaration (cp_parser* parser)
22109 {
22110 /* Look for the `__label__' keyword. */
22111 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22112
22113 while (true)
22114 {
22115 tree identifier;
22116
22117 /* Look for an identifier. */
22118 identifier = cp_parser_identifier (parser);
22119 /* If we failed, stop. */
22120 if (identifier == error_mark_node)
22121 break;
22122 /* Declare it as a label. */
22123 finish_label_decl (identifier);
22124 /* If the next token is a `;', stop. */
22125 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22126 break;
22127 /* Look for the `,' separating the label declarations. */
22128 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22129 }
22130
22131 /* Look for the final `;'. */
22132 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22133 }
22134
22135 /* Support Functions */
22136
22137 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22138 NAME should have one of the representations used for an
22139 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22140 is returned. If PARSER->SCOPE is a dependent type, then a
22141 SCOPE_REF is returned.
22142
22143 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22144 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22145 was formed. Abstractly, such entities should not be passed to this
22146 function, because they do not need to be looked up, but it is
22147 simpler to check for this special case here, rather than at the
22148 call-sites.
22149
22150 In cases not explicitly covered above, this function returns a
22151 DECL, OVERLOAD, or baselink representing the result of the lookup.
22152 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22153 is returned.
22154
22155 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22156 (e.g., "struct") that was used. In that case bindings that do not
22157 refer to types are ignored.
22158
22159 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22160 ignored.
22161
22162 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22163 are ignored.
22164
22165 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22166 types.
22167
22168 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22169 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22170 NULL_TREE otherwise. */
22171
22172 static tree
22173 cp_parser_lookup_name (cp_parser *parser, tree name,
22174 enum tag_types tag_type,
22175 bool is_template,
22176 bool is_namespace,
22177 bool check_dependency,
22178 tree *ambiguous_decls,
22179 location_t name_location)
22180 {
22181 tree decl;
22182 tree object_type = parser->context->object_type;
22183
22184 /* Assume that the lookup will be unambiguous. */
22185 if (ambiguous_decls)
22186 *ambiguous_decls = NULL_TREE;
22187
22188 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22189 no longer valid. Note that if we are parsing tentatively, and
22190 the parse fails, OBJECT_TYPE will be automatically restored. */
22191 parser->context->object_type = NULL_TREE;
22192
22193 if (name == error_mark_node)
22194 return error_mark_node;
22195
22196 /* A template-id has already been resolved; there is no lookup to
22197 do. */
22198 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22199 return name;
22200 if (BASELINK_P (name))
22201 {
22202 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22203 == TEMPLATE_ID_EXPR);
22204 return name;
22205 }
22206
22207 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22208 it should already have been checked to make sure that the name
22209 used matches the type being destroyed. */
22210 if (TREE_CODE (name) == BIT_NOT_EXPR)
22211 {
22212 tree type;
22213
22214 /* Figure out to which type this destructor applies. */
22215 if (parser->scope)
22216 type = parser->scope;
22217 else if (object_type)
22218 type = object_type;
22219 else
22220 type = current_class_type;
22221 /* If that's not a class type, there is no destructor. */
22222 if (!type || !CLASS_TYPE_P (type))
22223 return error_mark_node;
22224 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22225 lazily_declare_fn (sfk_destructor, type);
22226 if (!CLASSTYPE_DESTRUCTORS (type))
22227 return error_mark_node;
22228 /* If it was a class type, return the destructor. */
22229 return CLASSTYPE_DESTRUCTORS (type);
22230 }
22231
22232 /* By this point, the NAME should be an ordinary identifier. If
22233 the id-expression was a qualified name, the qualifying scope is
22234 stored in PARSER->SCOPE at this point. */
22235 gcc_assert (identifier_p (name));
22236
22237 /* Perform the lookup. */
22238 if (parser->scope)
22239 {
22240 bool dependent_p;
22241
22242 if (parser->scope == error_mark_node)
22243 return error_mark_node;
22244
22245 /* If the SCOPE is dependent, the lookup must be deferred until
22246 the template is instantiated -- unless we are explicitly
22247 looking up names in uninstantiated templates. Even then, we
22248 cannot look up the name if the scope is not a class type; it
22249 might, for example, be a template type parameter. */
22250 dependent_p = (TYPE_P (parser->scope)
22251 && dependent_scope_p (parser->scope));
22252 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22253 && dependent_p)
22254 /* Defer lookup. */
22255 decl = error_mark_node;
22256 else
22257 {
22258 tree pushed_scope = NULL_TREE;
22259
22260 /* If PARSER->SCOPE is a dependent type, then it must be a
22261 class type, and we must not be checking dependencies;
22262 otherwise, we would have processed this lookup above. So
22263 that PARSER->SCOPE is not considered a dependent base by
22264 lookup_member, we must enter the scope here. */
22265 if (dependent_p)
22266 pushed_scope = push_scope (parser->scope);
22267
22268 /* If the PARSER->SCOPE is a template specialization, it
22269 may be instantiated during name lookup. In that case,
22270 errors may be issued. Even if we rollback the current
22271 tentative parse, those errors are valid. */
22272 decl = lookup_qualified_name (parser->scope, name,
22273 tag_type != none_type,
22274 /*complain=*/true);
22275
22276 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22277 lookup result and the nested-name-specifier nominates a class C:
22278 * if the name specified after the nested-name-specifier, when
22279 looked up in C, is the injected-class-name of C (Clause 9), or
22280 * if the name specified after the nested-name-specifier is the
22281 same as the identifier or the simple-template-id's template-
22282 name in the last component of the nested-name-specifier,
22283 the name is instead considered to name the constructor of
22284 class C. [ Note: for example, the constructor is not an
22285 acceptable lookup result in an elaborated-type-specifier so
22286 the constructor would not be used in place of the
22287 injected-class-name. --end note ] Such a constructor name
22288 shall be used only in the declarator-id of a declaration that
22289 names a constructor or in a using-declaration. */
22290 if (tag_type == none_type
22291 && DECL_SELF_REFERENCE_P (decl)
22292 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22293 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22294 tag_type != none_type,
22295 /*complain=*/true);
22296
22297 /* If we have a single function from a using decl, pull it out. */
22298 if (TREE_CODE (decl) == OVERLOAD
22299 && !really_overloaded_fn (decl))
22300 decl = OVL_FUNCTION (decl);
22301
22302 if (pushed_scope)
22303 pop_scope (pushed_scope);
22304 }
22305
22306 /* If the scope is a dependent type and either we deferred lookup or
22307 we did lookup but didn't find the name, rememeber the name. */
22308 if (decl == error_mark_node && TYPE_P (parser->scope)
22309 && dependent_type_p (parser->scope))
22310 {
22311 if (tag_type)
22312 {
22313 tree type;
22314
22315 /* The resolution to Core Issue 180 says that `struct
22316 A::B' should be considered a type-name, even if `A'
22317 is dependent. */
22318 type = make_typename_type (parser->scope, name, tag_type,
22319 /*complain=*/tf_error);
22320 if (type != error_mark_node)
22321 decl = TYPE_NAME (type);
22322 }
22323 else if (is_template
22324 && (cp_parser_next_token_ends_template_argument_p (parser)
22325 || cp_lexer_next_token_is (parser->lexer,
22326 CPP_CLOSE_PAREN)))
22327 decl = make_unbound_class_template (parser->scope,
22328 name, NULL_TREE,
22329 /*complain=*/tf_error);
22330 else
22331 decl = build_qualified_name (/*type=*/NULL_TREE,
22332 parser->scope, name,
22333 is_template);
22334 }
22335 parser->qualifying_scope = parser->scope;
22336 parser->object_scope = NULL_TREE;
22337 }
22338 else if (object_type)
22339 {
22340 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22341 OBJECT_TYPE is not a class. */
22342 if (CLASS_TYPE_P (object_type))
22343 /* If the OBJECT_TYPE is a template specialization, it may
22344 be instantiated during name lookup. In that case, errors
22345 may be issued. Even if we rollback the current tentative
22346 parse, those errors are valid. */
22347 decl = lookup_member (object_type,
22348 name,
22349 /*protect=*/0,
22350 tag_type != none_type,
22351 tf_warning_or_error);
22352 else
22353 decl = NULL_TREE;
22354
22355 if (!decl)
22356 /* Look it up in the enclosing context. */
22357 decl = lookup_name_real (name, tag_type != none_type,
22358 /*nonclass=*/0,
22359 /*block_p=*/true, is_namespace, 0);
22360 parser->object_scope = object_type;
22361 parser->qualifying_scope = NULL_TREE;
22362 }
22363 else
22364 {
22365 decl = lookup_name_real (name, tag_type != none_type,
22366 /*nonclass=*/0,
22367 /*block_p=*/true, is_namespace, 0);
22368 parser->qualifying_scope = NULL_TREE;
22369 parser->object_scope = NULL_TREE;
22370 }
22371
22372 /* If the lookup failed, let our caller know. */
22373 if (!decl || decl == error_mark_node)
22374 return error_mark_node;
22375
22376 /* Pull out the template from an injected-class-name (or multiple). */
22377 if (is_template)
22378 decl = maybe_get_template_decl_from_type_decl (decl);
22379
22380 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22381 if (TREE_CODE (decl) == TREE_LIST)
22382 {
22383 if (ambiguous_decls)
22384 *ambiguous_decls = decl;
22385 /* The error message we have to print is too complicated for
22386 cp_parser_error, so we incorporate its actions directly. */
22387 if (!cp_parser_simulate_error (parser))
22388 {
22389 error_at (name_location, "reference to %qD is ambiguous",
22390 name);
22391 print_candidates (decl);
22392 }
22393 return error_mark_node;
22394 }
22395
22396 gcc_assert (DECL_P (decl)
22397 || TREE_CODE (decl) == OVERLOAD
22398 || TREE_CODE (decl) == SCOPE_REF
22399 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22400 || BASELINK_P (decl));
22401
22402 /* If we have resolved the name of a member declaration, check to
22403 see if the declaration is accessible. When the name resolves to
22404 set of overloaded functions, accessibility is checked when
22405 overload resolution is done.
22406
22407 During an explicit instantiation, access is not checked at all,
22408 as per [temp.explicit]. */
22409 if (DECL_P (decl))
22410 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22411
22412 maybe_record_typedef_use (decl);
22413
22414 return decl;
22415 }
22416
22417 /* Like cp_parser_lookup_name, but for use in the typical case where
22418 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22419 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22420
22421 static tree
22422 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22423 {
22424 return cp_parser_lookup_name (parser, name,
22425 none_type,
22426 /*is_template=*/false,
22427 /*is_namespace=*/false,
22428 /*check_dependency=*/true,
22429 /*ambiguous_decls=*/NULL,
22430 location);
22431 }
22432
22433 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22434 the current context, return the TYPE_DECL. If TAG_NAME_P is
22435 true, the DECL indicates the class being defined in a class-head,
22436 or declared in an elaborated-type-specifier.
22437
22438 Otherwise, return DECL. */
22439
22440 static tree
22441 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22442 {
22443 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22444 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22445
22446 struct A {
22447 template <typename T> struct B;
22448 };
22449
22450 template <typename T> struct A::B {};
22451
22452 Similarly, in an elaborated-type-specifier:
22453
22454 namespace N { struct X{}; }
22455
22456 struct A {
22457 template <typename T> friend struct N::X;
22458 };
22459
22460 However, if the DECL refers to a class type, and we are in
22461 the scope of the class, then the name lookup automatically
22462 finds the TYPE_DECL created by build_self_reference rather
22463 than a TEMPLATE_DECL. For example, in:
22464
22465 template <class T> struct S {
22466 S s;
22467 };
22468
22469 there is no need to handle such case. */
22470
22471 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22472 return DECL_TEMPLATE_RESULT (decl);
22473
22474 return decl;
22475 }
22476
22477 /* If too many, or too few, template-parameter lists apply to the
22478 declarator, issue an error message. Returns TRUE if all went well,
22479 and FALSE otherwise. */
22480
22481 static bool
22482 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22483 cp_declarator *declarator,
22484 location_t declarator_location)
22485 {
22486 switch (declarator->kind)
22487 {
22488 case cdk_id:
22489 {
22490 unsigned num_templates = 0;
22491 tree scope = declarator->u.id.qualifying_scope;
22492
22493 if (scope)
22494 num_templates = num_template_headers_for_class (scope);
22495 else if (TREE_CODE (declarator->u.id.unqualified_name)
22496 == TEMPLATE_ID_EXPR)
22497 /* If the DECLARATOR has the form `X<y>' then it uses one
22498 additional level of template parameters. */
22499 ++num_templates;
22500
22501 return cp_parser_check_template_parameters
22502 (parser, num_templates, declarator_location, declarator);
22503 }
22504
22505 case cdk_function:
22506 case cdk_array:
22507 case cdk_pointer:
22508 case cdk_reference:
22509 case cdk_ptrmem:
22510 return (cp_parser_check_declarator_template_parameters
22511 (parser, declarator->declarator, declarator_location));
22512
22513 case cdk_error:
22514 return true;
22515
22516 default:
22517 gcc_unreachable ();
22518 }
22519 return false;
22520 }
22521
22522 /* NUM_TEMPLATES were used in the current declaration. If that is
22523 invalid, return FALSE and issue an error messages. Otherwise,
22524 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22525 declarator and we can print more accurate diagnostics. */
22526
22527 static bool
22528 cp_parser_check_template_parameters (cp_parser* parser,
22529 unsigned num_templates,
22530 location_t location,
22531 cp_declarator *declarator)
22532 {
22533 /* If there are the same number of template classes and parameter
22534 lists, that's OK. */
22535 if (parser->num_template_parameter_lists == num_templates)
22536 return true;
22537 /* If there are more, but only one more, then we are referring to a
22538 member template. That's OK too. */
22539 if (parser->num_template_parameter_lists == num_templates + 1)
22540 return true;
22541 /* If there are more template classes than parameter lists, we have
22542 something like:
22543
22544 template <class T> void S<T>::R<T>::f (); */
22545 if (parser->num_template_parameter_lists < num_templates)
22546 {
22547 if (declarator && !current_function_decl)
22548 error_at (location, "specializing member %<%T::%E%> "
22549 "requires %<template<>%> syntax",
22550 declarator->u.id.qualifying_scope,
22551 declarator->u.id.unqualified_name);
22552 else if (declarator)
22553 error_at (location, "invalid declaration of %<%T::%E%>",
22554 declarator->u.id.qualifying_scope,
22555 declarator->u.id.unqualified_name);
22556 else
22557 error_at (location, "too few template-parameter-lists");
22558 return false;
22559 }
22560 /* Otherwise, there are too many template parameter lists. We have
22561 something like:
22562
22563 template <class T> template <class U> void S::f(); */
22564 error_at (location, "too many template-parameter-lists");
22565 return false;
22566 }
22567
22568 /* Parse an optional `::' token indicating that the following name is
22569 from the global namespace. If so, PARSER->SCOPE is set to the
22570 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22571 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22572 Returns the new value of PARSER->SCOPE, if the `::' token is
22573 present, and NULL_TREE otherwise. */
22574
22575 static tree
22576 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22577 {
22578 cp_token *token;
22579
22580 /* Peek at the next token. */
22581 token = cp_lexer_peek_token (parser->lexer);
22582 /* If we're looking at a `::' token then we're starting from the
22583 global namespace, not our current location. */
22584 if (token->type == CPP_SCOPE)
22585 {
22586 /* Consume the `::' token. */
22587 cp_lexer_consume_token (parser->lexer);
22588 /* Set the SCOPE so that we know where to start the lookup. */
22589 parser->scope = global_namespace;
22590 parser->qualifying_scope = global_namespace;
22591 parser->object_scope = NULL_TREE;
22592
22593 return parser->scope;
22594 }
22595 else if (!current_scope_valid_p)
22596 {
22597 parser->scope = NULL_TREE;
22598 parser->qualifying_scope = NULL_TREE;
22599 parser->object_scope = NULL_TREE;
22600 }
22601
22602 return NULL_TREE;
22603 }
22604
22605 /* Returns TRUE if the upcoming token sequence is the start of a
22606 constructor declarator. If FRIEND_P is true, the declarator is
22607 preceded by the `friend' specifier. */
22608
22609 static bool
22610 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22611 {
22612 bool constructor_p;
22613 bool outside_class_specifier_p;
22614 tree nested_name_specifier;
22615 cp_token *next_token;
22616
22617 /* The common case is that this is not a constructor declarator, so
22618 try to avoid doing lots of work if at all possible. It's not
22619 valid declare a constructor at function scope. */
22620 if (parser->in_function_body)
22621 return false;
22622 /* And only certain tokens can begin a constructor declarator. */
22623 next_token = cp_lexer_peek_token (parser->lexer);
22624 if (next_token->type != CPP_NAME
22625 && next_token->type != CPP_SCOPE
22626 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22627 && next_token->type != CPP_TEMPLATE_ID)
22628 return false;
22629
22630 /* Parse tentatively; we are going to roll back all of the tokens
22631 consumed here. */
22632 cp_parser_parse_tentatively (parser);
22633 /* Assume that we are looking at a constructor declarator. */
22634 constructor_p = true;
22635
22636 /* Look for the optional `::' operator. */
22637 cp_parser_global_scope_opt (parser,
22638 /*current_scope_valid_p=*/false);
22639 /* Look for the nested-name-specifier. */
22640 nested_name_specifier
22641 = (cp_parser_nested_name_specifier_opt (parser,
22642 /*typename_keyword_p=*/false,
22643 /*check_dependency_p=*/false,
22644 /*type_p=*/false,
22645 /*is_declaration=*/false));
22646
22647 outside_class_specifier_p = (!at_class_scope_p ()
22648 || !TYPE_BEING_DEFINED (current_class_type)
22649 || friend_p);
22650
22651 /* Outside of a class-specifier, there must be a
22652 nested-name-specifier. */
22653 if (!nested_name_specifier && outside_class_specifier_p)
22654 constructor_p = false;
22655 else if (nested_name_specifier == error_mark_node)
22656 constructor_p = false;
22657
22658 /* If we have a class scope, this is easy; DR 147 says that S::S always
22659 names the constructor, and no other qualified name could. */
22660 if (constructor_p && nested_name_specifier
22661 && CLASS_TYPE_P (nested_name_specifier))
22662 {
22663 tree id = cp_parser_unqualified_id (parser,
22664 /*template_keyword_p=*/false,
22665 /*check_dependency_p=*/false,
22666 /*declarator_p=*/true,
22667 /*optional_p=*/false);
22668 if (is_overloaded_fn (id))
22669 id = DECL_NAME (get_first_fn (id));
22670 if (!constructor_name_p (id, nested_name_specifier))
22671 constructor_p = false;
22672 }
22673 /* If we still think that this might be a constructor-declarator,
22674 look for a class-name. */
22675 else if (constructor_p)
22676 {
22677 /* If we have:
22678
22679 template <typename T> struct S {
22680 S();
22681 };
22682
22683 we must recognize that the nested `S' names a class. */
22684 tree type_decl;
22685 type_decl = cp_parser_class_name (parser,
22686 /*typename_keyword_p=*/false,
22687 /*template_keyword_p=*/false,
22688 none_type,
22689 /*check_dependency_p=*/false,
22690 /*class_head_p=*/false,
22691 /*is_declaration=*/false);
22692 /* If there was no class-name, then this is not a constructor.
22693 Otherwise, if we are in a class-specifier and we aren't
22694 handling a friend declaration, check that its type matches
22695 current_class_type (c++/38313). Note: error_mark_node
22696 is left alone for error recovery purposes. */
22697 constructor_p = (!cp_parser_error_occurred (parser)
22698 && (outside_class_specifier_p
22699 || type_decl == error_mark_node
22700 || same_type_p (current_class_type,
22701 TREE_TYPE (type_decl))));
22702
22703 /* If we're still considering a constructor, we have to see a `(',
22704 to begin the parameter-declaration-clause, followed by either a
22705 `)', an `...', or a decl-specifier. We need to check for a
22706 type-specifier to avoid being fooled into thinking that:
22707
22708 S (f) (int);
22709
22710 is a constructor. (It is actually a function named `f' that
22711 takes one parameter (of type `int') and returns a value of type
22712 `S'. */
22713 if (constructor_p
22714 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22715 constructor_p = false;
22716
22717 if (constructor_p
22718 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22719 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22720 /* A parameter declaration begins with a decl-specifier,
22721 which is either the "attribute" keyword, a storage class
22722 specifier, or (usually) a type-specifier. */
22723 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22724 {
22725 tree type;
22726 tree pushed_scope = NULL_TREE;
22727 unsigned saved_num_template_parameter_lists;
22728
22729 /* Names appearing in the type-specifier should be looked up
22730 in the scope of the class. */
22731 if (current_class_type)
22732 type = NULL_TREE;
22733 else
22734 {
22735 type = TREE_TYPE (type_decl);
22736 if (TREE_CODE (type) == TYPENAME_TYPE)
22737 {
22738 type = resolve_typename_type (type,
22739 /*only_current_p=*/false);
22740 if (TREE_CODE (type) == TYPENAME_TYPE)
22741 {
22742 cp_parser_abort_tentative_parse (parser);
22743 return false;
22744 }
22745 }
22746 pushed_scope = push_scope (type);
22747 }
22748
22749 /* Inside the constructor parameter list, surrounding
22750 template-parameter-lists do not apply. */
22751 saved_num_template_parameter_lists
22752 = parser->num_template_parameter_lists;
22753 parser->num_template_parameter_lists = 0;
22754
22755 /* Look for the type-specifier. */
22756 cp_parser_type_specifier (parser,
22757 CP_PARSER_FLAGS_NONE,
22758 /*decl_specs=*/NULL,
22759 /*is_declarator=*/true,
22760 /*declares_class_or_enum=*/NULL,
22761 /*is_cv_qualifier=*/NULL);
22762
22763 parser->num_template_parameter_lists
22764 = saved_num_template_parameter_lists;
22765
22766 /* Leave the scope of the class. */
22767 if (pushed_scope)
22768 pop_scope (pushed_scope);
22769
22770 constructor_p = !cp_parser_error_occurred (parser);
22771 }
22772 }
22773
22774 /* We did not really want to consume any tokens. */
22775 cp_parser_abort_tentative_parse (parser);
22776
22777 return constructor_p;
22778 }
22779
22780 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22781 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22782 they must be performed once we are in the scope of the function.
22783
22784 Returns the function defined. */
22785
22786 static tree
22787 cp_parser_function_definition_from_specifiers_and_declarator
22788 (cp_parser* parser,
22789 cp_decl_specifier_seq *decl_specifiers,
22790 tree attributes,
22791 const cp_declarator *declarator)
22792 {
22793 tree fn;
22794 bool success_p;
22795
22796 /* Begin the function-definition. */
22797 success_p = start_function (decl_specifiers, declarator, attributes);
22798
22799 /* The things we're about to see are not directly qualified by any
22800 template headers we've seen thus far. */
22801 reset_specialization ();
22802
22803 /* If there were names looked up in the decl-specifier-seq that we
22804 did not check, check them now. We must wait until we are in the
22805 scope of the function to perform the checks, since the function
22806 might be a friend. */
22807 perform_deferred_access_checks (tf_warning_or_error);
22808
22809 if (success_p)
22810 {
22811 cp_finalize_omp_declare_simd (parser, current_function_decl);
22812 parser->omp_declare_simd = NULL;
22813 }
22814
22815 if (!success_p)
22816 {
22817 /* Skip the entire function. */
22818 cp_parser_skip_to_end_of_block_or_statement (parser);
22819 fn = error_mark_node;
22820 }
22821 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22822 {
22823 /* Seen already, skip it. An error message has already been output. */
22824 cp_parser_skip_to_end_of_block_or_statement (parser);
22825 fn = current_function_decl;
22826 current_function_decl = NULL_TREE;
22827 /* If this is a function from a class, pop the nested class. */
22828 if (current_class_name)
22829 pop_nested_class ();
22830 }
22831 else
22832 {
22833 timevar_id_t tv;
22834 if (DECL_DECLARED_INLINE_P (current_function_decl))
22835 tv = TV_PARSE_INLINE;
22836 else
22837 tv = TV_PARSE_FUNC;
22838 timevar_push (tv);
22839 fn = cp_parser_function_definition_after_declarator (parser,
22840 /*inline_p=*/false);
22841 timevar_pop (tv);
22842 }
22843
22844 return fn;
22845 }
22846
22847 /* Parse the part of a function-definition that follows the
22848 declarator. INLINE_P is TRUE iff this function is an inline
22849 function defined within a class-specifier.
22850
22851 Returns the function defined. */
22852
22853 static tree
22854 cp_parser_function_definition_after_declarator (cp_parser* parser,
22855 bool inline_p)
22856 {
22857 tree fn;
22858 bool ctor_initializer_p = false;
22859 bool saved_in_unbraced_linkage_specification_p;
22860 bool saved_in_function_body;
22861 unsigned saved_num_template_parameter_lists;
22862 cp_token *token;
22863 bool fully_implicit_function_template_p
22864 = parser->fully_implicit_function_template_p;
22865 parser->fully_implicit_function_template_p = false;
22866 tree implicit_template_parms
22867 = parser->implicit_template_parms;
22868 parser->implicit_template_parms = 0;
22869 cp_binding_level* implicit_template_scope
22870 = parser->implicit_template_scope;
22871 parser->implicit_template_scope = 0;
22872
22873 saved_in_function_body = parser->in_function_body;
22874 parser->in_function_body = true;
22875 /* If the next token is `return', then the code may be trying to
22876 make use of the "named return value" extension that G++ used to
22877 support. */
22878 token = cp_lexer_peek_token (parser->lexer);
22879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22880 {
22881 /* Consume the `return' keyword. */
22882 cp_lexer_consume_token (parser->lexer);
22883 /* Look for the identifier that indicates what value is to be
22884 returned. */
22885 cp_parser_identifier (parser);
22886 /* Issue an error message. */
22887 error_at (token->location,
22888 "named return values are no longer supported");
22889 /* Skip tokens until we reach the start of the function body. */
22890 while (true)
22891 {
22892 cp_token *token = cp_lexer_peek_token (parser->lexer);
22893 if (token->type == CPP_OPEN_BRACE
22894 || token->type == CPP_EOF
22895 || token->type == CPP_PRAGMA_EOL)
22896 break;
22897 cp_lexer_consume_token (parser->lexer);
22898 }
22899 }
22900 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22901 anything declared inside `f'. */
22902 saved_in_unbraced_linkage_specification_p
22903 = parser->in_unbraced_linkage_specification_p;
22904 parser->in_unbraced_linkage_specification_p = false;
22905 /* Inside the function, surrounding template-parameter-lists do not
22906 apply. */
22907 saved_num_template_parameter_lists
22908 = parser->num_template_parameter_lists;
22909 parser->num_template_parameter_lists = 0;
22910
22911 start_lambda_scope (current_function_decl);
22912
22913 /* If the next token is `try', `__transaction_atomic', or
22914 `__transaction_relaxed`, then we are looking at either function-try-block
22915 or function-transaction-block. Note that all of these include the
22916 function-body. */
22917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22918 ctor_initializer_p = cp_parser_function_transaction (parser,
22919 RID_TRANSACTION_ATOMIC);
22920 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22921 RID_TRANSACTION_RELAXED))
22922 ctor_initializer_p = cp_parser_function_transaction (parser,
22923 RID_TRANSACTION_RELAXED);
22924 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22925 ctor_initializer_p = cp_parser_function_try_block (parser);
22926 else
22927 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22928 (parser, /*in_function_try_block=*/false);
22929
22930 finish_lambda_scope ();
22931
22932 /* Finish the function. */
22933 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22934 (inline_p ? 2 : 0));
22935 /* Generate code for it, if necessary. */
22936 expand_or_defer_fn (fn);
22937 /* Restore the saved values. */
22938 parser->in_unbraced_linkage_specification_p
22939 = saved_in_unbraced_linkage_specification_p;
22940 parser->num_template_parameter_lists
22941 = saved_num_template_parameter_lists;
22942 parser->in_function_body = saved_in_function_body;
22943
22944 parser->fully_implicit_function_template_p
22945 = fully_implicit_function_template_p;
22946 parser->implicit_template_parms
22947 = implicit_template_parms;
22948 parser->implicit_template_scope
22949 = implicit_template_scope;
22950
22951 if (parser->fully_implicit_function_template_p)
22952 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22953
22954 return fn;
22955 }
22956
22957 /* Parse a template-declaration, assuming that the `export' (and
22958 `extern') keywords, if present, has already been scanned. MEMBER_P
22959 is as for cp_parser_template_declaration. */
22960
22961 static void
22962 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22963 {
22964 tree decl = NULL_TREE;
22965 vec<deferred_access_check, va_gc> *checks;
22966 tree parameter_list;
22967 bool friend_p = false;
22968 bool need_lang_pop;
22969 cp_token *token;
22970
22971 /* Look for the `template' keyword. */
22972 token = cp_lexer_peek_token (parser->lexer);
22973 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22974 return;
22975
22976 /* And the `<'. */
22977 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22978 return;
22979 if (at_class_scope_p () && current_function_decl)
22980 {
22981 /* 14.5.2.2 [temp.mem]
22982
22983 A local class shall not have member templates. */
22984 error_at (token->location,
22985 "invalid declaration of member template in local class");
22986 cp_parser_skip_to_end_of_block_or_statement (parser);
22987 return;
22988 }
22989 /* [temp]
22990
22991 A template ... shall not have C linkage. */
22992 if (current_lang_name == lang_name_c)
22993 {
22994 error_at (token->location, "template with C linkage");
22995 /* Give it C++ linkage to avoid confusing other parts of the
22996 front end. */
22997 push_lang_context (lang_name_cplusplus);
22998 need_lang_pop = true;
22999 }
23000 else
23001 need_lang_pop = false;
23002
23003 /* We cannot perform access checks on the template parameter
23004 declarations until we know what is being declared, just as we
23005 cannot check the decl-specifier list. */
23006 push_deferring_access_checks (dk_deferred);
23007
23008 /* If the next token is `>', then we have an invalid
23009 specialization. Rather than complain about an invalid template
23010 parameter, issue an error message here. */
23011 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23012 {
23013 cp_parser_error (parser, "invalid explicit specialization");
23014 begin_specialization ();
23015 parameter_list = NULL_TREE;
23016 }
23017 else
23018 {
23019 /* Parse the template parameters. */
23020 parameter_list = cp_parser_template_parameter_list (parser);
23021 }
23022
23023 /* Get the deferred access checks from the parameter list. These
23024 will be checked once we know what is being declared, as for a
23025 member template the checks must be performed in the scope of the
23026 class containing the member. */
23027 checks = get_deferred_access_checks ();
23028
23029 /* Look for the `>'. */
23030 cp_parser_skip_to_end_of_template_parameter_list (parser);
23031 /* We just processed one more parameter list. */
23032 ++parser->num_template_parameter_lists;
23033 /* If the next token is `template', there are more template
23034 parameters. */
23035 if (cp_lexer_next_token_is_keyword (parser->lexer,
23036 RID_TEMPLATE))
23037 cp_parser_template_declaration_after_export (parser, member_p);
23038 else if (cxx_dialect >= cxx11
23039 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23040 decl = cp_parser_alias_declaration (parser);
23041 else
23042 {
23043 /* There are no access checks when parsing a template, as we do not
23044 know if a specialization will be a friend. */
23045 push_deferring_access_checks (dk_no_check);
23046 token = cp_lexer_peek_token (parser->lexer);
23047 decl = cp_parser_single_declaration (parser,
23048 checks,
23049 member_p,
23050 /*explicit_specialization_p=*/false,
23051 &friend_p);
23052 pop_deferring_access_checks ();
23053
23054 /* If this is a member template declaration, let the front
23055 end know. */
23056 if (member_p && !friend_p && decl)
23057 {
23058 if (TREE_CODE (decl) == TYPE_DECL)
23059 cp_parser_check_access_in_redeclaration (decl, token->location);
23060
23061 decl = finish_member_template_decl (decl);
23062 }
23063 else if (friend_p && decl
23064 && DECL_DECLARES_TYPE_P (decl))
23065 make_friend_class (current_class_type, TREE_TYPE (decl),
23066 /*complain=*/true);
23067 }
23068 /* We are done with the current parameter list. */
23069 --parser->num_template_parameter_lists;
23070
23071 pop_deferring_access_checks ();
23072
23073 /* Finish up. */
23074 finish_template_decl (parameter_list);
23075
23076 /* Check the template arguments for a literal operator template. */
23077 if (decl
23078 && DECL_DECLARES_FUNCTION_P (decl)
23079 && UDLIT_OPER_P (DECL_NAME (decl)))
23080 {
23081 bool ok = true;
23082 if (parameter_list == NULL_TREE)
23083 ok = false;
23084 else
23085 {
23086 int num_parms = TREE_VEC_LENGTH (parameter_list);
23087 if (num_parms == 1)
23088 {
23089 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23090 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23091 if (TREE_TYPE (parm) != char_type_node
23092 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23093 ok = false;
23094 }
23095 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23096 {
23097 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23098 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23099 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23100 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23101 if (TREE_TYPE (parm) != TREE_TYPE (type)
23102 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23103 ok = false;
23104 }
23105 else
23106 ok = false;
23107 }
23108 if (!ok)
23109 error ("literal operator template %qD has invalid parameter list."
23110 " Expected non-type template argument pack <char...>"
23111 " or <typename CharT, CharT...>",
23112 decl);
23113 }
23114 /* Register member declarations. */
23115 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23116 finish_member_declaration (decl);
23117 /* For the erroneous case of a template with C linkage, we pushed an
23118 implicit C++ linkage scope; exit that scope now. */
23119 if (need_lang_pop)
23120 pop_lang_context ();
23121 /* If DECL is a function template, we must return to parse it later.
23122 (Even though there is no definition, there might be default
23123 arguments that need handling.) */
23124 if (member_p && decl
23125 && DECL_DECLARES_FUNCTION_P (decl))
23126 vec_safe_push (unparsed_funs_with_definitions, decl);
23127 }
23128
23129 /* Perform the deferred access checks from a template-parameter-list.
23130 CHECKS is a TREE_LIST of access checks, as returned by
23131 get_deferred_access_checks. */
23132
23133 static void
23134 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23135 {
23136 ++processing_template_parmlist;
23137 perform_access_checks (checks, tf_warning_or_error);
23138 --processing_template_parmlist;
23139 }
23140
23141 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23142 `function-definition' sequence that follows a template header.
23143 If MEMBER_P is true, this declaration appears in a class scope.
23144
23145 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23146 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23147
23148 static tree
23149 cp_parser_single_declaration (cp_parser* parser,
23150 vec<deferred_access_check, va_gc> *checks,
23151 bool member_p,
23152 bool explicit_specialization_p,
23153 bool* friend_p)
23154 {
23155 int declares_class_or_enum;
23156 tree decl = NULL_TREE;
23157 cp_decl_specifier_seq decl_specifiers;
23158 bool function_definition_p = false;
23159 cp_token *decl_spec_token_start;
23160
23161 /* This function is only used when processing a template
23162 declaration. */
23163 gcc_assert (innermost_scope_kind () == sk_template_parms
23164 || innermost_scope_kind () == sk_template_spec);
23165
23166 /* Defer access checks until we know what is being declared. */
23167 push_deferring_access_checks (dk_deferred);
23168
23169 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23170 alternative. */
23171 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23172 cp_parser_decl_specifier_seq (parser,
23173 CP_PARSER_FLAGS_OPTIONAL,
23174 &decl_specifiers,
23175 &declares_class_or_enum);
23176 if (friend_p)
23177 *friend_p = cp_parser_friend_p (&decl_specifiers);
23178
23179 /* There are no template typedefs. */
23180 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23181 {
23182 error_at (decl_spec_token_start->location,
23183 "template declaration of %<typedef%>");
23184 decl = error_mark_node;
23185 }
23186
23187 /* Gather up the access checks that occurred the
23188 decl-specifier-seq. */
23189 stop_deferring_access_checks ();
23190
23191 /* Check for the declaration of a template class. */
23192 if (declares_class_or_enum)
23193 {
23194 if (cp_parser_declares_only_class_p (parser))
23195 {
23196 decl = shadow_tag (&decl_specifiers);
23197
23198 /* In this case:
23199
23200 struct C {
23201 friend template <typename T> struct A<T>::B;
23202 };
23203
23204 A<T>::B will be represented by a TYPENAME_TYPE, and
23205 therefore not recognized by shadow_tag. */
23206 if (friend_p && *friend_p
23207 && !decl
23208 && decl_specifiers.type
23209 && TYPE_P (decl_specifiers.type))
23210 decl = decl_specifiers.type;
23211
23212 if (decl && decl != error_mark_node)
23213 decl = TYPE_NAME (decl);
23214 else
23215 decl = error_mark_node;
23216
23217 /* Perform access checks for template parameters. */
23218 cp_parser_perform_template_parameter_access_checks (checks);
23219 }
23220 }
23221
23222 /* Complain about missing 'typename' or other invalid type names. */
23223 if (!decl_specifiers.any_type_specifiers_p
23224 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23225 {
23226 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23227 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23228 the rest of this declaration. */
23229 decl = error_mark_node;
23230 goto out;
23231 }
23232
23233 /* If it's not a template class, try for a template function. If
23234 the next token is a `;', then this declaration does not declare
23235 anything. But, if there were errors in the decl-specifiers, then
23236 the error might well have come from an attempted class-specifier.
23237 In that case, there's no need to warn about a missing declarator. */
23238 if (!decl
23239 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23240 || decl_specifiers.type != error_mark_node))
23241 {
23242 decl = cp_parser_init_declarator (parser,
23243 &decl_specifiers,
23244 checks,
23245 /*function_definition_allowed_p=*/true,
23246 member_p,
23247 declares_class_or_enum,
23248 &function_definition_p,
23249 NULL);
23250
23251 /* 7.1.1-1 [dcl.stc]
23252
23253 A storage-class-specifier shall not be specified in an explicit
23254 specialization... */
23255 if (decl
23256 && explicit_specialization_p
23257 && decl_specifiers.storage_class != sc_none)
23258 {
23259 error_at (decl_spec_token_start->location,
23260 "explicit template specialization cannot have a storage class");
23261 decl = error_mark_node;
23262 }
23263
23264 if (decl && VAR_P (decl))
23265 check_template_variable (decl);
23266 }
23267
23268 /* Look for a trailing `;' after the declaration. */
23269 if (!function_definition_p
23270 && (decl == error_mark_node
23271 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23272 cp_parser_skip_to_end_of_block_or_statement (parser);
23273
23274 out:
23275 pop_deferring_access_checks ();
23276
23277 /* Clear any current qualification; whatever comes next is the start
23278 of something new. */
23279 parser->scope = NULL_TREE;
23280 parser->qualifying_scope = NULL_TREE;
23281 parser->object_scope = NULL_TREE;
23282
23283 return decl;
23284 }
23285
23286 /* Parse a cast-expression that is not the operand of a unary "&". */
23287
23288 static tree
23289 cp_parser_simple_cast_expression (cp_parser *parser)
23290 {
23291 return cp_parser_cast_expression (parser, /*address_p=*/false,
23292 /*cast_p=*/false, /*decltype*/false, NULL);
23293 }
23294
23295 /* Parse a functional cast to TYPE. Returns an expression
23296 representing the cast. */
23297
23298 static tree
23299 cp_parser_functional_cast (cp_parser* parser, tree type)
23300 {
23301 vec<tree, va_gc> *vec;
23302 tree expression_list;
23303 tree cast;
23304 bool nonconst_p;
23305
23306 if (!type)
23307 type = error_mark_node;
23308
23309 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23310 {
23311 cp_lexer_set_source_position (parser->lexer);
23312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23313 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23314 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23315 if (TREE_CODE (type) == TYPE_DECL)
23316 type = TREE_TYPE (type);
23317 return finish_compound_literal (type, expression_list,
23318 tf_warning_or_error);
23319 }
23320
23321
23322 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23323 /*cast_p=*/true,
23324 /*allow_expansion_p=*/true,
23325 /*non_constant_p=*/NULL);
23326 if (vec == NULL)
23327 expression_list = error_mark_node;
23328 else
23329 {
23330 expression_list = build_tree_list_vec (vec);
23331 release_tree_vector (vec);
23332 }
23333
23334 cast = build_functional_cast (type, expression_list,
23335 tf_warning_or_error);
23336 /* [expr.const]/1: In an integral constant expression "only type
23337 conversions to integral or enumeration type can be used". */
23338 if (TREE_CODE (type) == TYPE_DECL)
23339 type = TREE_TYPE (type);
23340 if (cast != error_mark_node
23341 && !cast_valid_in_integral_constant_expression_p (type)
23342 && cp_parser_non_integral_constant_expression (parser,
23343 NIC_CONSTRUCTOR))
23344 return error_mark_node;
23345 return cast;
23346 }
23347
23348 /* Save the tokens that make up the body of a member function defined
23349 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23350 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23351 specifiers applied to the declaration. Returns the FUNCTION_DECL
23352 for the member function. */
23353
23354 static tree
23355 cp_parser_save_member_function_body (cp_parser* parser,
23356 cp_decl_specifier_seq *decl_specifiers,
23357 cp_declarator *declarator,
23358 tree attributes)
23359 {
23360 cp_token *first;
23361 cp_token *last;
23362 tree fn;
23363
23364 /* Create the FUNCTION_DECL. */
23365 fn = grokmethod (decl_specifiers, declarator, attributes);
23366 cp_finalize_omp_declare_simd (parser, fn);
23367 /* If something went badly wrong, bail out now. */
23368 if (fn == error_mark_node)
23369 {
23370 /* If there's a function-body, skip it. */
23371 if (cp_parser_token_starts_function_definition_p
23372 (cp_lexer_peek_token (parser->lexer)))
23373 cp_parser_skip_to_end_of_block_or_statement (parser);
23374 return error_mark_node;
23375 }
23376
23377 /* Remember it, if there default args to post process. */
23378 cp_parser_save_default_args (parser, fn);
23379
23380 /* Save away the tokens that make up the body of the
23381 function. */
23382 first = parser->lexer->next_token;
23383 /* Handle function try blocks. */
23384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23385 cp_lexer_consume_token (parser->lexer);
23386 /* We can have braced-init-list mem-initializers before the fn body. */
23387 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23388 {
23389 cp_lexer_consume_token (parser->lexer);
23390 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23391 {
23392 /* cache_group will stop after an un-nested { } pair, too. */
23393 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23394 break;
23395
23396 /* variadic mem-inits have ... after the ')'. */
23397 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23398 cp_lexer_consume_token (parser->lexer);
23399 }
23400 }
23401 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23402 /* Handle function try blocks. */
23403 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23404 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23405 last = parser->lexer->next_token;
23406
23407 /* Save away the inline definition; we will process it when the
23408 class is complete. */
23409 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23410 DECL_PENDING_INLINE_P (fn) = 1;
23411
23412 /* We need to know that this was defined in the class, so that
23413 friend templates are handled correctly. */
23414 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23415
23416 /* Add FN to the queue of functions to be parsed later. */
23417 vec_safe_push (unparsed_funs_with_definitions, fn);
23418
23419 return fn;
23420 }
23421
23422 /* Save the tokens that make up the in-class initializer for a non-static
23423 data member. Returns a DEFAULT_ARG. */
23424
23425 static tree
23426 cp_parser_save_nsdmi (cp_parser* parser)
23427 {
23428 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23429 }
23430
23431 /* Parse a template-argument-list, as well as the trailing ">" (but
23432 not the opening "<"). See cp_parser_template_argument_list for the
23433 return value. */
23434
23435 static tree
23436 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23437 {
23438 tree arguments;
23439 tree saved_scope;
23440 tree saved_qualifying_scope;
23441 tree saved_object_scope;
23442 bool saved_greater_than_is_operator_p;
23443 int saved_unevaluated_operand;
23444 int saved_inhibit_evaluation_warnings;
23445
23446 /* [temp.names]
23447
23448 When parsing a template-id, the first non-nested `>' is taken as
23449 the end of the template-argument-list rather than a greater-than
23450 operator. */
23451 saved_greater_than_is_operator_p
23452 = parser->greater_than_is_operator_p;
23453 parser->greater_than_is_operator_p = false;
23454 /* Parsing the argument list may modify SCOPE, so we save it
23455 here. */
23456 saved_scope = parser->scope;
23457 saved_qualifying_scope = parser->qualifying_scope;
23458 saved_object_scope = parser->object_scope;
23459 /* We need to evaluate the template arguments, even though this
23460 template-id may be nested within a "sizeof". */
23461 saved_unevaluated_operand = cp_unevaluated_operand;
23462 cp_unevaluated_operand = 0;
23463 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23464 c_inhibit_evaluation_warnings = 0;
23465 /* Parse the template-argument-list itself. */
23466 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23467 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23468 arguments = NULL_TREE;
23469 else
23470 arguments = cp_parser_template_argument_list (parser);
23471 /* Look for the `>' that ends the template-argument-list. If we find
23472 a '>>' instead, it's probably just a typo. */
23473 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23474 {
23475 if (cxx_dialect != cxx98)
23476 {
23477 /* In C++0x, a `>>' in a template argument list or cast
23478 expression is considered to be two separate `>'
23479 tokens. So, change the current token to a `>', but don't
23480 consume it: it will be consumed later when the outer
23481 template argument list (or cast expression) is parsed.
23482 Note that this replacement of `>' for `>>' is necessary
23483 even if we are parsing tentatively: in the tentative
23484 case, after calling
23485 cp_parser_enclosed_template_argument_list we will always
23486 throw away all of the template arguments and the first
23487 closing `>', either because the template argument list
23488 was erroneous or because we are replacing those tokens
23489 with a CPP_TEMPLATE_ID token. The second `>' (which will
23490 not have been thrown away) is needed either to close an
23491 outer template argument list or to complete a new-style
23492 cast. */
23493 cp_token *token = cp_lexer_peek_token (parser->lexer);
23494 token->type = CPP_GREATER;
23495 }
23496 else if (!saved_greater_than_is_operator_p)
23497 {
23498 /* If we're in a nested template argument list, the '>>' has
23499 to be a typo for '> >'. We emit the error message, but we
23500 continue parsing and we push a '>' as next token, so that
23501 the argument list will be parsed correctly. Note that the
23502 global source location is still on the token before the
23503 '>>', so we need to say explicitly where we want it. */
23504 cp_token *token = cp_lexer_peek_token (parser->lexer);
23505 error_at (token->location, "%<>>%> should be %<> >%> "
23506 "within a nested template argument list");
23507
23508 token->type = CPP_GREATER;
23509 }
23510 else
23511 {
23512 /* If this is not a nested template argument list, the '>>'
23513 is a typo for '>'. Emit an error message and continue.
23514 Same deal about the token location, but here we can get it
23515 right by consuming the '>>' before issuing the diagnostic. */
23516 cp_token *token = cp_lexer_consume_token (parser->lexer);
23517 error_at (token->location,
23518 "spurious %<>>%>, use %<>%> to terminate "
23519 "a template argument list");
23520 }
23521 }
23522 else
23523 cp_parser_skip_to_end_of_template_parameter_list (parser);
23524 /* The `>' token might be a greater-than operator again now. */
23525 parser->greater_than_is_operator_p
23526 = saved_greater_than_is_operator_p;
23527 /* Restore the SAVED_SCOPE. */
23528 parser->scope = saved_scope;
23529 parser->qualifying_scope = saved_qualifying_scope;
23530 parser->object_scope = saved_object_scope;
23531 cp_unevaluated_operand = saved_unevaluated_operand;
23532 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23533
23534 return arguments;
23535 }
23536
23537 /* MEMBER_FUNCTION is a member function, or a friend. If default
23538 arguments, or the body of the function have not yet been parsed,
23539 parse them now. */
23540
23541 static void
23542 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23543 {
23544 timevar_push (TV_PARSE_INMETH);
23545 /* If this member is a template, get the underlying
23546 FUNCTION_DECL. */
23547 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23548 member_function = DECL_TEMPLATE_RESULT (member_function);
23549
23550 /* There should not be any class definitions in progress at this
23551 point; the bodies of members are only parsed outside of all class
23552 definitions. */
23553 gcc_assert (parser->num_classes_being_defined == 0);
23554 /* While we're parsing the member functions we might encounter more
23555 classes. We want to handle them right away, but we don't want
23556 them getting mixed up with functions that are currently in the
23557 queue. */
23558 push_unparsed_function_queues (parser);
23559
23560 /* Make sure that any template parameters are in scope. */
23561 maybe_begin_member_template_processing (member_function);
23562
23563 /* If the body of the function has not yet been parsed, parse it
23564 now. */
23565 if (DECL_PENDING_INLINE_P (member_function))
23566 {
23567 tree function_scope;
23568 cp_token_cache *tokens;
23569
23570 /* The function is no longer pending; we are processing it. */
23571 tokens = DECL_PENDING_INLINE_INFO (member_function);
23572 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23573 DECL_PENDING_INLINE_P (member_function) = 0;
23574
23575 /* If this is a local class, enter the scope of the containing
23576 function. */
23577 function_scope = current_function_decl;
23578 if (function_scope)
23579 push_function_context ();
23580
23581 /* Push the body of the function onto the lexer stack. */
23582 cp_parser_push_lexer_for_tokens (parser, tokens);
23583
23584 /* Let the front end know that we going to be defining this
23585 function. */
23586 start_preparsed_function (member_function, NULL_TREE,
23587 SF_PRE_PARSED | SF_INCLASS_INLINE);
23588
23589 /* Don't do access checking if it is a templated function. */
23590 if (processing_template_decl)
23591 push_deferring_access_checks (dk_no_check);
23592
23593 /* #pragma omp declare reduction needs special parsing. */
23594 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23595 {
23596 parser->lexer->in_pragma = true;
23597 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23598 finish_function (/*inline*/2);
23599 cp_check_omp_declare_reduction (member_function);
23600 }
23601 else
23602 /* Now, parse the body of the function. */
23603 cp_parser_function_definition_after_declarator (parser,
23604 /*inline_p=*/true);
23605
23606 if (processing_template_decl)
23607 pop_deferring_access_checks ();
23608
23609 /* Leave the scope of the containing function. */
23610 if (function_scope)
23611 pop_function_context ();
23612 cp_parser_pop_lexer (parser);
23613 }
23614
23615 /* Remove any template parameters from the symbol table. */
23616 maybe_end_member_template_processing ();
23617
23618 /* Restore the queue. */
23619 pop_unparsed_function_queues (parser);
23620 timevar_pop (TV_PARSE_INMETH);
23621 }
23622
23623 /* If DECL contains any default args, remember it on the unparsed
23624 functions queue. */
23625
23626 static void
23627 cp_parser_save_default_args (cp_parser* parser, tree decl)
23628 {
23629 tree probe;
23630
23631 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23632 probe;
23633 probe = TREE_CHAIN (probe))
23634 if (TREE_PURPOSE (probe))
23635 {
23636 cp_default_arg_entry entry = {current_class_type, decl};
23637 vec_safe_push (unparsed_funs_with_default_args, entry);
23638 break;
23639 }
23640 }
23641
23642 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23643 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23644 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23645 from the parameter-type-list. */
23646
23647 static tree
23648 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23649 tree default_arg, tree parmtype)
23650 {
23651 cp_token_cache *tokens;
23652 tree parsed_arg;
23653 bool dummy;
23654
23655 if (default_arg == error_mark_node)
23656 return error_mark_node;
23657
23658 /* Push the saved tokens for the default argument onto the parser's
23659 lexer stack. */
23660 tokens = DEFARG_TOKENS (default_arg);
23661 cp_parser_push_lexer_for_tokens (parser, tokens);
23662
23663 start_lambda_scope (decl);
23664
23665 /* Parse the default argument. */
23666 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23667 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23668 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23669
23670 finish_lambda_scope ();
23671
23672 if (parsed_arg == error_mark_node)
23673 cp_parser_skip_to_end_of_statement (parser);
23674
23675 if (!processing_template_decl)
23676 {
23677 /* In a non-template class, check conversions now. In a template,
23678 we'll wait and instantiate these as needed. */
23679 if (TREE_CODE (decl) == PARM_DECL)
23680 parsed_arg = check_default_argument (parmtype, parsed_arg,
23681 tf_warning_or_error);
23682 else
23683 {
23684 int flags = LOOKUP_IMPLICIT;
23685 if (DIRECT_LIST_INIT_P (parsed_arg))
23686 flags = LOOKUP_NORMAL;
23687 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23688 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23689 /* This represents the whole initialization. */
23690 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23691 }
23692 }
23693
23694 /* If the token stream has not been completely used up, then
23695 there was extra junk after the end of the default
23696 argument. */
23697 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23698 {
23699 if (TREE_CODE (decl) == PARM_DECL)
23700 cp_parser_error (parser, "expected %<,%>");
23701 else
23702 cp_parser_error (parser, "expected %<;%>");
23703 }
23704
23705 /* Revert to the main lexer. */
23706 cp_parser_pop_lexer (parser);
23707
23708 return parsed_arg;
23709 }
23710
23711 /* FIELD is a non-static data member with an initializer which we saved for
23712 later; parse it now. */
23713
23714 static void
23715 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23716 {
23717 tree def;
23718
23719 maybe_begin_member_template_processing (field);
23720
23721 push_unparsed_function_queues (parser);
23722 def = cp_parser_late_parse_one_default_arg (parser, field,
23723 DECL_INITIAL (field),
23724 NULL_TREE);
23725 pop_unparsed_function_queues (parser);
23726
23727 maybe_end_member_template_processing ();
23728
23729 DECL_INITIAL (field) = def;
23730 }
23731
23732 /* FN is a FUNCTION_DECL which may contains a parameter with an
23733 unparsed DEFAULT_ARG. Parse the default args now. This function
23734 assumes that the current scope is the scope in which the default
23735 argument should be processed. */
23736
23737 static void
23738 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23739 {
23740 bool saved_local_variables_forbidden_p;
23741 tree parm, parmdecl;
23742
23743 /* While we're parsing the default args, we might (due to the
23744 statement expression extension) encounter more classes. We want
23745 to handle them right away, but we don't want them getting mixed
23746 up with default args that are currently in the queue. */
23747 push_unparsed_function_queues (parser);
23748
23749 /* Local variable names (and the `this' keyword) may not appear
23750 in a default argument. */
23751 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23752 parser->local_variables_forbidden_p = true;
23753
23754 push_defarg_context (fn);
23755
23756 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23757 parmdecl = DECL_ARGUMENTS (fn);
23758 parm && parm != void_list_node;
23759 parm = TREE_CHAIN (parm),
23760 parmdecl = DECL_CHAIN (parmdecl))
23761 {
23762 tree default_arg = TREE_PURPOSE (parm);
23763 tree parsed_arg;
23764 vec<tree, va_gc> *insts;
23765 tree copy;
23766 unsigned ix;
23767
23768 if (!default_arg)
23769 continue;
23770
23771 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23772 /* This can happen for a friend declaration for a function
23773 already declared with default arguments. */
23774 continue;
23775
23776 parsed_arg
23777 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23778 default_arg,
23779 TREE_VALUE (parm));
23780 if (parsed_arg == error_mark_node)
23781 {
23782 continue;
23783 }
23784
23785 TREE_PURPOSE (parm) = parsed_arg;
23786
23787 /* Update any instantiations we've already created. */
23788 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23789 vec_safe_iterate (insts, ix, &copy); ix++)
23790 TREE_PURPOSE (copy) = parsed_arg;
23791 }
23792
23793 pop_defarg_context ();
23794
23795 /* Make sure no default arg is missing. */
23796 check_default_args (fn);
23797
23798 /* Restore the state of local_variables_forbidden_p. */
23799 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23800
23801 /* Restore the queue. */
23802 pop_unparsed_function_queues (parser);
23803 }
23804
23805 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23806
23807 sizeof ... ( identifier )
23808
23809 where the 'sizeof' token has already been consumed. */
23810
23811 static tree
23812 cp_parser_sizeof_pack (cp_parser *parser)
23813 {
23814 /* Consume the `...'. */
23815 cp_lexer_consume_token (parser->lexer);
23816 maybe_warn_variadic_templates ();
23817
23818 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23819 if (paren)
23820 cp_lexer_consume_token (parser->lexer);
23821 else
23822 permerror (cp_lexer_peek_token (parser->lexer)->location,
23823 "%<sizeof...%> argument must be surrounded by parentheses");
23824
23825 cp_token *token = cp_lexer_peek_token (parser->lexer);
23826 tree name = cp_parser_identifier (parser);
23827 if (name == error_mark_node)
23828 return error_mark_node;
23829 /* The name is not qualified. */
23830 parser->scope = NULL_TREE;
23831 parser->qualifying_scope = NULL_TREE;
23832 parser->object_scope = NULL_TREE;
23833 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23834 if (expr == error_mark_node)
23835 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23836 token->location);
23837 if (TREE_CODE (expr) == TYPE_DECL)
23838 expr = TREE_TYPE (expr);
23839 else if (TREE_CODE (expr) == CONST_DECL)
23840 expr = DECL_INITIAL (expr);
23841 expr = make_pack_expansion (expr);
23842
23843 if (paren)
23844 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23845
23846 return expr;
23847 }
23848
23849 /* Parse the operand of `sizeof' (or a similar operator). Returns
23850 either a TYPE or an expression, depending on the form of the
23851 input. The KEYWORD indicates which kind of expression we have
23852 encountered. */
23853
23854 static tree
23855 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23856 {
23857 tree expr = NULL_TREE;
23858 const char *saved_message;
23859 char *tmp;
23860 bool saved_integral_constant_expression_p;
23861 bool saved_non_integral_constant_expression_p;
23862
23863 /* If it's a `...', then we are computing the length of a parameter
23864 pack. */
23865 if (keyword == RID_SIZEOF
23866 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23867 return cp_parser_sizeof_pack (parser);
23868
23869 /* Types cannot be defined in a `sizeof' expression. Save away the
23870 old message. */
23871 saved_message = parser->type_definition_forbidden_message;
23872 /* And create the new one. */
23873 tmp = concat ("types may not be defined in %<",
23874 IDENTIFIER_POINTER (ridpointers[keyword]),
23875 "%> expressions", NULL);
23876 parser->type_definition_forbidden_message = tmp;
23877
23878 /* The restrictions on constant-expressions do not apply inside
23879 sizeof expressions. */
23880 saved_integral_constant_expression_p
23881 = parser->integral_constant_expression_p;
23882 saved_non_integral_constant_expression_p
23883 = parser->non_integral_constant_expression_p;
23884 parser->integral_constant_expression_p = false;
23885
23886 /* Do not actually evaluate the expression. */
23887 ++cp_unevaluated_operand;
23888 ++c_inhibit_evaluation_warnings;
23889 /* If it's a `(', then we might be looking at the type-id
23890 construction. */
23891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23892 {
23893 tree type = NULL_TREE;
23894 bool compound_literal_p;
23895
23896 /* We can't be sure yet whether we're looking at a type-id or an
23897 expression. */
23898 cp_parser_parse_tentatively (parser);
23899 /* Consume the `('. */
23900 cp_lexer_consume_token (parser->lexer);
23901 /* Note: as a GNU Extension, compound literals are considered
23902 postfix-expressions as they are in C99, so they are valid
23903 arguments to sizeof. See comment in cp_parser_cast_expression
23904 for details. */
23905 cp_lexer_save_tokens (parser->lexer);
23906 /* Skip tokens until the next token is a closing parenthesis.
23907 If we find the closing `)', and the next token is a `{', then
23908 we are looking at a compound-literal. */
23909 compound_literal_p
23910 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23911 /*consume_paren=*/true)
23912 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23913 /* Roll back the tokens we skipped. */
23914 cp_lexer_rollback_tokens (parser->lexer);
23915 /* If we were looking at a compound-literal, simulate an error
23916 so that the call to cp_parser_parse_definitely below will
23917 fail. */
23918 if (compound_literal_p)
23919 cp_parser_simulate_error (parser);
23920 else
23921 {
23922 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23923 parser->in_type_id_in_expr_p = true;
23924 /* Look for the type-id. */
23925 type = cp_parser_type_id (parser);
23926 /* Look for the closing `)'. */
23927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23928 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23929 }
23930
23931 /* If all went well, then we're done. */
23932 if (cp_parser_parse_definitely (parser))
23933 {
23934 cp_decl_specifier_seq decl_specs;
23935
23936 /* Build a trivial decl-specifier-seq. */
23937 clear_decl_specs (&decl_specs);
23938 decl_specs.type = type;
23939
23940 /* Call grokdeclarator to figure out what type this is. */
23941 expr = grokdeclarator (NULL,
23942 &decl_specs,
23943 TYPENAME,
23944 /*initialized=*/0,
23945 /*attrlist=*/NULL);
23946 }
23947 }
23948
23949 /* If the type-id production did not work out, then we must be
23950 looking at the unary-expression production. */
23951 if (!expr)
23952 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23953 /*cast_p=*/false, NULL);
23954
23955 /* Go back to evaluating expressions. */
23956 --cp_unevaluated_operand;
23957 --c_inhibit_evaluation_warnings;
23958
23959 /* Free the message we created. */
23960 free (tmp);
23961 /* And restore the old one. */
23962 parser->type_definition_forbidden_message = saved_message;
23963 parser->integral_constant_expression_p
23964 = saved_integral_constant_expression_p;
23965 parser->non_integral_constant_expression_p
23966 = saved_non_integral_constant_expression_p;
23967
23968 return expr;
23969 }
23970
23971 /* If the current declaration has no declarator, return true. */
23972
23973 static bool
23974 cp_parser_declares_only_class_p (cp_parser *parser)
23975 {
23976 /* If the next token is a `;' or a `,' then there is no
23977 declarator. */
23978 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23979 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23980 }
23981
23982 /* Update the DECL_SPECS to reflect the storage class indicated by
23983 KEYWORD. */
23984
23985 static void
23986 cp_parser_set_storage_class (cp_parser *parser,
23987 cp_decl_specifier_seq *decl_specs,
23988 enum rid keyword,
23989 cp_token *token)
23990 {
23991 cp_storage_class storage_class;
23992
23993 if (parser->in_unbraced_linkage_specification_p)
23994 {
23995 error_at (token->location, "invalid use of %qD in linkage specification",
23996 ridpointers[keyword]);
23997 return;
23998 }
23999 else if (decl_specs->storage_class != sc_none)
24000 {
24001 decl_specs->conflicting_specifiers_p = true;
24002 return;
24003 }
24004
24005 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24006 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24007 && decl_specs->gnu_thread_keyword_p)
24008 {
24009 pedwarn (decl_specs->locations[ds_thread], 0,
24010 "%<__thread%> before %qD", ridpointers[keyword]);
24011 }
24012
24013 switch (keyword)
24014 {
24015 case RID_AUTO:
24016 storage_class = sc_auto;
24017 break;
24018 case RID_REGISTER:
24019 storage_class = sc_register;
24020 break;
24021 case RID_STATIC:
24022 storage_class = sc_static;
24023 break;
24024 case RID_EXTERN:
24025 storage_class = sc_extern;
24026 break;
24027 case RID_MUTABLE:
24028 storage_class = sc_mutable;
24029 break;
24030 default:
24031 gcc_unreachable ();
24032 }
24033 decl_specs->storage_class = storage_class;
24034 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24035
24036 /* A storage class specifier cannot be applied alongside a typedef
24037 specifier. If there is a typedef specifier present then set
24038 conflicting_specifiers_p which will trigger an error later
24039 on in grokdeclarator. */
24040 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24041 decl_specs->conflicting_specifiers_p = true;
24042 }
24043
24044 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24045 is true, the type is a class or enum definition. */
24046
24047 static void
24048 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24049 tree type_spec,
24050 cp_token *token,
24051 bool type_definition_p)
24052 {
24053 decl_specs->any_specifiers_p = true;
24054
24055 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24056 (with, for example, in "typedef int wchar_t;") we remember that
24057 this is what happened. In system headers, we ignore these
24058 declarations so that G++ can work with system headers that are not
24059 C++-safe. */
24060 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24061 && !type_definition_p
24062 && (type_spec == boolean_type_node
24063 || type_spec == char16_type_node
24064 || type_spec == char32_type_node
24065 || type_spec == wchar_type_node)
24066 && (decl_specs->type
24067 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24068 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24069 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24070 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24071 {
24072 decl_specs->redefined_builtin_type = type_spec;
24073 set_and_check_decl_spec_loc (decl_specs,
24074 ds_redefined_builtin_type_spec,
24075 token);
24076 if (!decl_specs->type)
24077 {
24078 decl_specs->type = type_spec;
24079 decl_specs->type_definition_p = false;
24080 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24081 }
24082 }
24083 else if (decl_specs->type)
24084 decl_specs->multiple_types_p = true;
24085 else
24086 {
24087 decl_specs->type = type_spec;
24088 decl_specs->type_definition_p = type_definition_p;
24089 decl_specs->redefined_builtin_type = NULL_TREE;
24090 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24091 }
24092 }
24093
24094 /* True iff TOKEN is the GNU keyword __thread. */
24095
24096 static bool
24097 token_is__thread (cp_token *token)
24098 {
24099 gcc_assert (token->keyword == RID_THREAD);
24100 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24101 }
24102
24103 /* Set the location for a declarator specifier and check if it is
24104 duplicated.
24105
24106 DECL_SPECS is the sequence of declarator specifiers onto which to
24107 set the location.
24108
24109 DS is the single declarator specifier to set which location is to
24110 be set onto the existing sequence of declarators.
24111
24112 LOCATION is the location for the declarator specifier to
24113 consider. */
24114
24115 static void
24116 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24117 cp_decl_spec ds, cp_token *token)
24118 {
24119 gcc_assert (ds < ds_last);
24120
24121 if (decl_specs == NULL)
24122 return;
24123
24124 source_location location = token->location;
24125
24126 if (decl_specs->locations[ds] == 0)
24127 {
24128 decl_specs->locations[ds] = location;
24129 if (ds == ds_thread)
24130 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24131 }
24132 else
24133 {
24134 if (ds == ds_long)
24135 {
24136 if (decl_specs->locations[ds_long_long] != 0)
24137 error_at (location,
24138 "%<long long long%> is too long for GCC");
24139 else
24140 {
24141 decl_specs->locations[ds_long_long] = location;
24142 pedwarn_cxx98 (location,
24143 OPT_Wlong_long,
24144 "ISO C++ 1998 does not support %<long long%>");
24145 }
24146 }
24147 else if (ds == ds_thread)
24148 {
24149 bool gnu = token_is__thread (token);
24150 if (gnu != decl_specs->gnu_thread_keyword_p)
24151 error_at (location,
24152 "both %<__thread%> and %<thread_local%> specified");
24153 else
24154 error_at (location, "duplicate %qD", token->u.value);
24155 }
24156 else
24157 {
24158 static const char *const decl_spec_names[] = {
24159 "signed",
24160 "unsigned",
24161 "short",
24162 "long",
24163 "const",
24164 "volatile",
24165 "restrict",
24166 "inline",
24167 "virtual",
24168 "explicit",
24169 "friend",
24170 "typedef",
24171 "using",
24172 "constexpr",
24173 "__complex"
24174 };
24175 error_at (location,
24176 "duplicate %qs", decl_spec_names[ds]);
24177 }
24178 }
24179 }
24180
24181 /* Return true iff the declarator specifier DS is present in the
24182 sequence of declarator specifiers DECL_SPECS. */
24183
24184 bool
24185 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24186 cp_decl_spec ds)
24187 {
24188 gcc_assert (ds < ds_last);
24189
24190 if (decl_specs == NULL)
24191 return false;
24192
24193 return decl_specs->locations[ds] != 0;
24194 }
24195
24196 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24197 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24198
24199 static bool
24200 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24201 {
24202 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24203 }
24204
24205 /* Issue an error message indicating that TOKEN_DESC was expected.
24206 If KEYWORD is true, it indicated this function is called by
24207 cp_parser_require_keword and the required token can only be
24208 a indicated keyword. */
24209
24210 static void
24211 cp_parser_required_error (cp_parser *parser,
24212 required_token token_desc,
24213 bool keyword)
24214 {
24215 switch (token_desc)
24216 {
24217 case RT_NEW:
24218 cp_parser_error (parser, "expected %<new%>");
24219 return;
24220 case RT_DELETE:
24221 cp_parser_error (parser, "expected %<delete%>");
24222 return;
24223 case RT_RETURN:
24224 cp_parser_error (parser, "expected %<return%>");
24225 return;
24226 case RT_WHILE:
24227 cp_parser_error (parser, "expected %<while%>");
24228 return;
24229 case RT_EXTERN:
24230 cp_parser_error (parser, "expected %<extern%>");
24231 return;
24232 case RT_STATIC_ASSERT:
24233 cp_parser_error (parser, "expected %<static_assert%>");
24234 return;
24235 case RT_DECLTYPE:
24236 cp_parser_error (parser, "expected %<decltype%>");
24237 return;
24238 case RT_OPERATOR:
24239 cp_parser_error (parser, "expected %<operator%>");
24240 return;
24241 case RT_CLASS:
24242 cp_parser_error (parser, "expected %<class%>");
24243 return;
24244 case RT_TEMPLATE:
24245 cp_parser_error (parser, "expected %<template%>");
24246 return;
24247 case RT_NAMESPACE:
24248 cp_parser_error (parser, "expected %<namespace%>");
24249 return;
24250 case RT_USING:
24251 cp_parser_error (parser, "expected %<using%>");
24252 return;
24253 case RT_ASM:
24254 cp_parser_error (parser, "expected %<asm%>");
24255 return;
24256 case RT_TRY:
24257 cp_parser_error (parser, "expected %<try%>");
24258 return;
24259 case RT_CATCH:
24260 cp_parser_error (parser, "expected %<catch%>");
24261 return;
24262 case RT_THROW:
24263 cp_parser_error (parser, "expected %<throw%>");
24264 return;
24265 case RT_LABEL:
24266 cp_parser_error (parser, "expected %<__label__%>");
24267 return;
24268 case RT_AT_TRY:
24269 cp_parser_error (parser, "expected %<@try%>");
24270 return;
24271 case RT_AT_SYNCHRONIZED:
24272 cp_parser_error (parser, "expected %<@synchronized%>");
24273 return;
24274 case RT_AT_THROW:
24275 cp_parser_error (parser, "expected %<@throw%>");
24276 return;
24277 case RT_TRANSACTION_ATOMIC:
24278 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24279 return;
24280 case RT_TRANSACTION_RELAXED:
24281 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24282 return;
24283 default:
24284 break;
24285 }
24286 if (!keyword)
24287 {
24288 switch (token_desc)
24289 {
24290 case RT_SEMICOLON:
24291 cp_parser_error (parser, "expected %<;%>");
24292 return;
24293 case RT_OPEN_PAREN:
24294 cp_parser_error (parser, "expected %<(%>");
24295 return;
24296 case RT_CLOSE_BRACE:
24297 cp_parser_error (parser, "expected %<}%>");
24298 return;
24299 case RT_OPEN_BRACE:
24300 cp_parser_error (parser, "expected %<{%>");
24301 return;
24302 case RT_CLOSE_SQUARE:
24303 cp_parser_error (parser, "expected %<]%>");
24304 return;
24305 case RT_OPEN_SQUARE:
24306 cp_parser_error (parser, "expected %<[%>");
24307 return;
24308 case RT_COMMA:
24309 cp_parser_error (parser, "expected %<,%>");
24310 return;
24311 case RT_SCOPE:
24312 cp_parser_error (parser, "expected %<::%>");
24313 return;
24314 case RT_LESS:
24315 cp_parser_error (parser, "expected %<<%>");
24316 return;
24317 case RT_GREATER:
24318 cp_parser_error (parser, "expected %<>%>");
24319 return;
24320 case RT_EQ:
24321 cp_parser_error (parser, "expected %<=%>");
24322 return;
24323 case RT_ELLIPSIS:
24324 cp_parser_error (parser, "expected %<...%>");
24325 return;
24326 case RT_MULT:
24327 cp_parser_error (parser, "expected %<*%>");
24328 return;
24329 case RT_COMPL:
24330 cp_parser_error (parser, "expected %<~%>");
24331 return;
24332 case RT_COLON:
24333 cp_parser_error (parser, "expected %<:%>");
24334 return;
24335 case RT_COLON_SCOPE:
24336 cp_parser_error (parser, "expected %<:%> or %<::%>");
24337 return;
24338 case RT_CLOSE_PAREN:
24339 cp_parser_error (parser, "expected %<)%>");
24340 return;
24341 case RT_COMMA_CLOSE_PAREN:
24342 cp_parser_error (parser, "expected %<,%> or %<)%>");
24343 return;
24344 case RT_PRAGMA_EOL:
24345 cp_parser_error (parser, "expected end of line");
24346 return;
24347 case RT_NAME:
24348 cp_parser_error (parser, "expected identifier");
24349 return;
24350 case RT_SELECT:
24351 cp_parser_error (parser, "expected selection-statement");
24352 return;
24353 case RT_INTERATION:
24354 cp_parser_error (parser, "expected iteration-statement");
24355 return;
24356 case RT_JUMP:
24357 cp_parser_error (parser, "expected jump-statement");
24358 return;
24359 case RT_CLASS_KEY:
24360 cp_parser_error (parser, "expected class-key");
24361 return;
24362 case RT_CLASS_TYPENAME_TEMPLATE:
24363 cp_parser_error (parser,
24364 "expected %<class%>, %<typename%>, or %<template%>");
24365 return;
24366 default:
24367 gcc_unreachable ();
24368 }
24369 }
24370 else
24371 gcc_unreachable ();
24372 }
24373
24374
24375
24376 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24377 issue an error message indicating that TOKEN_DESC was expected.
24378
24379 Returns the token consumed, if the token had the appropriate type.
24380 Otherwise, returns NULL. */
24381
24382 static cp_token *
24383 cp_parser_require (cp_parser* parser,
24384 enum cpp_ttype type,
24385 required_token token_desc)
24386 {
24387 if (cp_lexer_next_token_is (parser->lexer, type))
24388 return cp_lexer_consume_token (parser->lexer);
24389 else
24390 {
24391 /* Output the MESSAGE -- unless we're parsing tentatively. */
24392 if (!cp_parser_simulate_error (parser))
24393 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24394 return NULL;
24395 }
24396 }
24397
24398 /* An error message is produced if the next token is not '>'.
24399 All further tokens are skipped until the desired token is
24400 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24401
24402 static void
24403 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24404 {
24405 /* Current level of '< ... >'. */
24406 unsigned level = 0;
24407 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24408 unsigned nesting_depth = 0;
24409
24410 /* Are we ready, yet? If not, issue error message. */
24411 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24412 return;
24413
24414 /* Skip tokens until the desired token is found. */
24415 while (true)
24416 {
24417 /* Peek at the next token. */
24418 switch (cp_lexer_peek_token (parser->lexer)->type)
24419 {
24420 case CPP_LESS:
24421 if (!nesting_depth)
24422 ++level;
24423 break;
24424
24425 case CPP_RSHIFT:
24426 if (cxx_dialect == cxx98)
24427 /* C++0x views the `>>' operator as two `>' tokens, but
24428 C++98 does not. */
24429 break;
24430 else if (!nesting_depth && level-- == 0)
24431 {
24432 /* We've hit a `>>' where the first `>' closes the
24433 template argument list, and the second `>' is
24434 spurious. Just consume the `>>' and stop; we've
24435 already produced at least one error. */
24436 cp_lexer_consume_token (parser->lexer);
24437 return;
24438 }
24439 /* Fall through for C++0x, so we handle the second `>' in
24440 the `>>'. */
24441
24442 case CPP_GREATER:
24443 if (!nesting_depth && level-- == 0)
24444 {
24445 /* We've reached the token we want, consume it and stop. */
24446 cp_lexer_consume_token (parser->lexer);
24447 return;
24448 }
24449 break;
24450
24451 case CPP_OPEN_PAREN:
24452 case CPP_OPEN_SQUARE:
24453 ++nesting_depth;
24454 break;
24455
24456 case CPP_CLOSE_PAREN:
24457 case CPP_CLOSE_SQUARE:
24458 if (nesting_depth-- == 0)
24459 return;
24460 break;
24461
24462 case CPP_EOF:
24463 case CPP_PRAGMA_EOL:
24464 case CPP_SEMICOLON:
24465 case CPP_OPEN_BRACE:
24466 case CPP_CLOSE_BRACE:
24467 /* The '>' was probably forgotten, don't look further. */
24468 return;
24469
24470 default:
24471 break;
24472 }
24473
24474 /* Consume this token. */
24475 cp_lexer_consume_token (parser->lexer);
24476 }
24477 }
24478
24479 /* If the next token is the indicated keyword, consume it. Otherwise,
24480 issue an error message indicating that TOKEN_DESC was expected.
24481
24482 Returns the token consumed, if the token had the appropriate type.
24483 Otherwise, returns NULL. */
24484
24485 static cp_token *
24486 cp_parser_require_keyword (cp_parser* parser,
24487 enum rid keyword,
24488 required_token token_desc)
24489 {
24490 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24491
24492 if (token && token->keyword != keyword)
24493 {
24494 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24495 return NULL;
24496 }
24497
24498 return token;
24499 }
24500
24501 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24502 function-definition. */
24503
24504 static bool
24505 cp_parser_token_starts_function_definition_p (cp_token* token)
24506 {
24507 return (/* An ordinary function-body begins with an `{'. */
24508 token->type == CPP_OPEN_BRACE
24509 /* A ctor-initializer begins with a `:'. */
24510 || token->type == CPP_COLON
24511 /* A function-try-block begins with `try'. */
24512 || token->keyword == RID_TRY
24513 /* A function-transaction-block begins with `__transaction_atomic'
24514 or `__transaction_relaxed'. */
24515 || token->keyword == RID_TRANSACTION_ATOMIC
24516 || token->keyword == RID_TRANSACTION_RELAXED
24517 /* The named return value extension begins with `return'. */
24518 || token->keyword == RID_RETURN);
24519 }
24520
24521 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24522 definition. */
24523
24524 static bool
24525 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24526 {
24527 cp_token *token;
24528
24529 token = cp_lexer_peek_token (parser->lexer);
24530 return (token->type == CPP_OPEN_BRACE
24531 || (token->type == CPP_COLON
24532 && !parser->colon_doesnt_start_class_def_p));
24533 }
24534
24535 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24536 C++0x) ending a template-argument. */
24537
24538 static bool
24539 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24540 {
24541 cp_token *token;
24542
24543 token = cp_lexer_peek_token (parser->lexer);
24544 return (token->type == CPP_COMMA
24545 || token->type == CPP_GREATER
24546 || token->type == CPP_ELLIPSIS
24547 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24548 }
24549
24550 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24551 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24552
24553 static bool
24554 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24555 size_t n)
24556 {
24557 cp_token *token;
24558
24559 token = cp_lexer_peek_nth_token (parser->lexer, n);
24560 if (token->type == CPP_LESS)
24561 return true;
24562 /* Check for the sequence `<::' in the original code. It would be lexed as
24563 `[:', where `[' is a digraph, and there is no whitespace before
24564 `:'. */
24565 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24566 {
24567 cp_token *token2;
24568 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24569 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24570 return true;
24571 }
24572 return false;
24573 }
24574
24575 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24576 or none_type otherwise. */
24577
24578 static enum tag_types
24579 cp_parser_token_is_class_key (cp_token* token)
24580 {
24581 switch (token->keyword)
24582 {
24583 case RID_CLASS:
24584 return class_type;
24585 case RID_STRUCT:
24586 return record_type;
24587 case RID_UNION:
24588 return union_type;
24589
24590 default:
24591 return none_type;
24592 }
24593 }
24594
24595 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24596
24597 static void
24598 cp_parser_check_class_key (enum tag_types class_key, tree type)
24599 {
24600 if (type == error_mark_node)
24601 return;
24602 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24603 {
24604 if (permerror (input_location, "%qs tag used in naming %q#T",
24605 class_key == union_type ? "union"
24606 : class_key == record_type ? "struct" : "class",
24607 type))
24608 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24609 "%q#T was previously declared here", type);
24610 }
24611 }
24612
24613 /* Issue an error message if DECL is redeclared with different
24614 access than its original declaration [class.access.spec/3].
24615 This applies to nested classes and nested class templates.
24616 [class.mem/1]. */
24617
24618 static void
24619 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24620 {
24621 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24622 return;
24623
24624 if ((TREE_PRIVATE (decl)
24625 != (current_access_specifier == access_private_node))
24626 || (TREE_PROTECTED (decl)
24627 != (current_access_specifier == access_protected_node)))
24628 error_at (location, "%qD redeclared with different access", decl);
24629 }
24630
24631 /* Look for the `template' keyword, as a syntactic disambiguator.
24632 Return TRUE iff it is present, in which case it will be
24633 consumed. */
24634
24635 static bool
24636 cp_parser_optional_template_keyword (cp_parser *parser)
24637 {
24638 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24639 {
24640 /* In C++98 the `template' keyword can only be used within templates;
24641 outside templates the parser can always figure out what is a
24642 template and what is not. In C++11, per the resolution of DR 468,
24643 `template' is allowed in cases where it is not strictly necessary. */
24644 if (!processing_template_decl
24645 && pedantic && cxx_dialect == cxx98)
24646 {
24647 cp_token *token = cp_lexer_peek_token (parser->lexer);
24648 pedwarn (token->location, OPT_Wpedantic,
24649 "in C++98 %<template%> (as a disambiguator) is only "
24650 "allowed within templates");
24651 /* If this part of the token stream is rescanned, the same
24652 error message would be generated. So, we purge the token
24653 from the stream. */
24654 cp_lexer_purge_token (parser->lexer);
24655 return false;
24656 }
24657 else
24658 {
24659 /* Consume the `template' keyword. */
24660 cp_lexer_consume_token (parser->lexer);
24661 return true;
24662 }
24663 }
24664 return false;
24665 }
24666
24667 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24668 set PARSER->SCOPE, and perform other related actions. */
24669
24670 static void
24671 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24672 {
24673 int i;
24674 struct tree_check *check_value;
24675 deferred_access_check *chk;
24676 vec<deferred_access_check, va_gc> *checks;
24677
24678 /* Get the stored value. */
24679 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24680 /* Perform any access checks that were deferred. */
24681 checks = check_value->checks;
24682 if (checks)
24683 {
24684 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24685 perform_or_defer_access_check (chk->binfo,
24686 chk->decl,
24687 chk->diag_decl, tf_warning_or_error);
24688 }
24689 /* Set the scope from the stored value. */
24690 parser->scope = check_value->value;
24691 parser->qualifying_scope = check_value->qualifying_scope;
24692 parser->object_scope = NULL_TREE;
24693 }
24694
24695 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24696 encounter the end of a block before what we were looking for. */
24697
24698 static bool
24699 cp_parser_cache_group (cp_parser *parser,
24700 enum cpp_ttype end,
24701 unsigned depth)
24702 {
24703 while (true)
24704 {
24705 cp_token *token = cp_lexer_peek_token (parser->lexer);
24706
24707 /* Abort a parenthesized expression if we encounter a semicolon. */
24708 if ((end == CPP_CLOSE_PAREN || depth == 0)
24709 && token->type == CPP_SEMICOLON)
24710 return true;
24711 /* If we've reached the end of the file, stop. */
24712 if (token->type == CPP_EOF
24713 || (end != CPP_PRAGMA_EOL
24714 && token->type == CPP_PRAGMA_EOL))
24715 return true;
24716 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24717 /* We've hit the end of an enclosing block, so there's been some
24718 kind of syntax error. */
24719 return true;
24720
24721 /* Consume the token. */
24722 cp_lexer_consume_token (parser->lexer);
24723 /* See if it starts a new group. */
24724 if (token->type == CPP_OPEN_BRACE)
24725 {
24726 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24727 /* In theory this should probably check end == '}', but
24728 cp_parser_save_member_function_body needs it to exit
24729 after either '}' or ')' when called with ')'. */
24730 if (depth == 0)
24731 return false;
24732 }
24733 else if (token->type == CPP_OPEN_PAREN)
24734 {
24735 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24736 if (depth == 0 && end == CPP_CLOSE_PAREN)
24737 return false;
24738 }
24739 else if (token->type == CPP_PRAGMA)
24740 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24741 else if (token->type == end)
24742 return false;
24743 }
24744 }
24745
24746 /* Like above, for caching a default argument or NSDMI. Both of these are
24747 terminated by a non-nested comma, but it can be unclear whether or not a
24748 comma is nested in a template argument list unless we do more parsing.
24749 In order to handle this ambiguity, when we encounter a ',' after a '<'
24750 we try to parse what follows as a parameter-declaration-list (in the
24751 case of a default argument) or a member-declarator (in the case of an
24752 NSDMI). If that succeeds, then we stop caching. */
24753
24754 static tree
24755 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24756 {
24757 unsigned depth = 0;
24758 int maybe_template_id = 0;
24759 cp_token *first_token;
24760 cp_token *token;
24761 tree default_argument;
24762
24763 /* Add tokens until we have processed the entire default
24764 argument. We add the range [first_token, token). */
24765 first_token = cp_lexer_peek_token (parser->lexer);
24766 if (first_token->type == CPP_OPEN_BRACE)
24767 {
24768 /* For list-initialization, this is straightforward. */
24769 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24770 token = cp_lexer_peek_token (parser->lexer);
24771 }
24772 else while (true)
24773 {
24774 bool done = false;
24775
24776 /* Peek at the next token. */
24777 token = cp_lexer_peek_token (parser->lexer);
24778 /* What we do depends on what token we have. */
24779 switch (token->type)
24780 {
24781 /* In valid code, a default argument must be
24782 immediately followed by a `,' `)', or `...'. */
24783 case CPP_COMMA:
24784 if (depth == 0 && maybe_template_id)
24785 {
24786 /* If we've seen a '<', we might be in a
24787 template-argument-list. Until Core issue 325 is
24788 resolved, we don't know how this situation ought
24789 to be handled, so try to DTRT. We check whether
24790 what comes after the comma is a valid parameter
24791 declaration list. If it is, then the comma ends
24792 the default argument; otherwise the default
24793 argument continues. */
24794 bool error = false;
24795
24796 /* Set ITALP so cp_parser_parameter_declaration_list
24797 doesn't decide to commit to this parse. */
24798 bool saved_italp = parser->in_template_argument_list_p;
24799 parser->in_template_argument_list_p = true;
24800
24801 cp_parser_parse_tentatively (parser);
24802 cp_lexer_consume_token (parser->lexer);
24803
24804 if (nsdmi)
24805 {
24806 int ctor_dtor_or_conv_p;
24807 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24808 &ctor_dtor_or_conv_p,
24809 /*parenthesized_p=*/NULL,
24810 /*member_p=*/true);
24811 }
24812 else
24813 {
24814 begin_scope (sk_function_parms, NULL_TREE);
24815 cp_parser_parameter_declaration_list (parser, &error);
24816 pop_bindings_and_leave_scope ();
24817 }
24818 if (!cp_parser_error_occurred (parser) && !error)
24819 done = true;
24820 cp_parser_abort_tentative_parse (parser);
24821
24822 parser->in_template_argument_list_p = saved_italp;
24823 break;
24824 }
24825 case CPP_CLOSE_PAREN:
24826 case CPP_ELLIPSIS:
24827 /* If we run into a non-nested `;', `}', or `]',
24828 then the code is invalid -- but the default
24829 argument is certainly over. */
24830 case CPP_SEMICOLON:
24831 case CPP_CLOSE_BRACE:
24832 case CPP_CLOSE_SQUARE:
24833 if (depth == 0
24834 /* Handle correctly int n = sizeof ... ( p ); */
24835 && token->type != CPP_ELLIPSIS)
24836 done = true;
24837 /* Update DEPTH, if necessary. */
24838 else if (token->type == CPP_CLOSE_PAREN
24839 || token->type == CPP_CLOSE_BRACE
24840 || token->type == CPP_CLOSE_SQUARE)
24841 --depth;
24842 break;
24843
24844 case CPP_OPEN_PAREN:
24845 case CPP_OPEN_SQUARE:
24846 case CPP_OPEN_BRACE:
24847 ++depth;
24848 break;
24849
24850 case CPP_LESS:
24851 if (depth == 0)
24852 /* This might be the comparison operator, or it might
24853 start a template argument list. */
24854 ++maybe_template_id;
24855 break;
24856
24857 case CPP_RSHIFT:
24858 if (cxx_dialect == cxx98)
24859 break;
24860 /* Fall through for C++0x, which treats the `>>'
24861 operator like two `>' tokens in certain
24862 cases. */
24863
24864 case CPP_GREATER:
24865 if (depth == 0)
24866 {
24867 /* This might be an operator, or it might close a
24868 template argument list. But if a previous '<'
24869 started a template argument list, this will have
24870 closed it, so we can't be in one anymore. */
24871 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24872 if (maybe_template_id < 0)
24873 maybe_template_id = 0;
24874 }
24875 break;
24876
24877 /* If we run out of tokens, issue an error message. */
24878 case CPP_EOF:
24879 case CPP_PRAGMA_EOL:
24880 error_at (token->location, "file ends in default argument");
24881 done = true;
24882 break;
24883
24884 case CPP_NAME:
24885 case CPP_SCOPE:
24886 /* In these cases, we should look for template-ids.
24887 For example, if the default argument is
24888 `X<int, double>()', we need to do name lookup to
24889 figure out whether or not `X' is a template; if
24890 so, the `,' does not end the default argument.
24891
24892 That is not yet done. */
24893 break;
24894
24895 default:
24896 break;
24897 }
24898
24899 /* If we've reached the end, stop. */
24900 if (done)
24901 break;
24902
24903 /* Add the token to the token block. */
24904 token = cp_lexer_consume_token (parser->lexer);
24905 }
24906
24907 /* Create a DEFAULT_ARG to represent the unparsed default
24908 argument. */
24909 default_argument = make_node (DEFAULT_ARG);
24910 DEFARG_TOKENS (default_argument)
24911 = cp_token_cache_new (first_token, token);
24912 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24913
24914 return default_argument;
24915 }
24916
24917 /* Begin parsing tentatively. We always save tokens while parsing
24918 tentatively so that if the tentative parsing fails we can restore the
24919 tokens. */
24920
24921 static void
24922 cp_parser_parse_tentatively (cp_parser* parser)
24923 {
24924 /* Enter a new parsing context. */
24925 parser->context = cp_parser_context_new (parser->context);
24926 /* Begin saving tokens. */
24927 cp_lexer_save_tokens (parser->lexer);
24928 /* In order to avoid repetitive access control error messages,
24929 access checks are queued up until we are no longer parsing
24930 tentatively. */
24931 push_deferring_access_checks (dk_deferred);
24932 }
24933
24934 /* Commit to the currently active tentative parse. */
24935
24936 static void
24937 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24938 {
24939 cp_parser_context *context;
24940 cp_lexer *lexer;
24941
24942 /* Mark all of the levels as committed. */
24943 lexer = parser->lexer;
24944 for (context = parser->context; context->next; context = context->next)
24945 {
24946 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24947 break;
24948 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24949 while (!cp_lexer_saving_tokens (lexer))
24950 lexer = lexer->next;
24951 cp_lexer_commit_tokens (lexer);
24952 }
24953 }
24954
24955 /* Commit to the topmost currently active tentative parse.
24956
24957 Note that this function shouldn't be called when there are
24958 irreversible side-effects while in a tentative state. For
24959 example, we shouldn't create a permanent entry in the symbol
24960 table, or issue an error message that might not apply if the
24961 tentative parse is aborted. */
24962
24963 static void
24964 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24965 {
24966 cp_parser_context *context = parser->context;
24967 cp_lexer *lexer = parser->lexer;
24968
24969 if (context)
24970 {
24971 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24972 return;
24973 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24974
24975 while (!cp_lexer_saving_tokens (lexer))
24976 lexer = lexer->next;
24977 cp_lexer_commit_tokens (lexer);
24978 }
24979 }
24980
24981 /* Abort the currently active tentative parse. All consumed tokens
24982 will be rolled back, and no diagnostics will be issued. */
24983
24984 static void
24985 cp_parser_abort_tentative_parse (cp_parser* parser)
24986 {
24987 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24988 || errorcount > 0);
24989 cp_parser_simulate_error (parser);
24990 /* Now, pretend that we want to see if the construct was
24991 successfully parsed. */
24992 cp_parser_parse_definitely (parser);
24993 }
24994
24995 /* Stop parsing tentatively. If a parse error has occurred, restore the
24996 token stream. Otherwise, commit to the tokens we have consumed.
24997 Returns true if no error occurred; false otherwise. */
24998
24999 static bool
25000 cp_parser_parse_definitely (cp_parser* parser)
25001 {
25002 bool error_occurred;
25003 cp_parser_context *context;
25004
25005 /* Remember whether or not an error occurred, since we are about to
25006 destroy that information. */
25007 error_occurred = cp_parser_error_occurred (parser);
25008 /* Remove the topmost context from the stack. */
25009 context = parser->context;
25010 parser->context = context->next;
25011 /* If no parse errors occurred, commit to the tentative parse. */
25012 if (!error_occurred)
25013 {
25014 /* Commit to the tokens read tentatively, unless that was
25015 already done. */
25016 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25017 cp_lexer_commit_tokens (parser->lexer);
25018
25019 pop_to_parent_deferring_access_checks ();
25020 }
25021 /* Otherwise, if errors occurred, roll back our state so that things
25022 are just as they were before we began the tentative parse. */
25023 else
25024 {
25025 cp_lexer_rollback_tokens (parser->lexer);
25026 pop_deferring_access_checks ();
25027 }
25028 /* Add the context to the front of the free list. */
25029 context->next = cp_parser_context_free_list;
25030 cp_parser_context_free_list = context;
25031
25032 return !error_occurred;
25033 }
25034
25035 /* Returns true if we are parsing tentatively and are not committed to
25036 this tentative parse. */
25037
25038 static bool
25039 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25040 {
25041 return (cp_parser_parsing_tentatively (parser)
25042 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25043 }
25044
25045 /* Returns nonzero iff an error has occurred during the most recent
25046 tentative parse. */
25047
25048 static bool
25049 cp_parser_error_occurred (cp_parser* parser)
25050 {
25051 return (cp_parser_parsing_tentatively (parser)
25052 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25053 }
25054
25055 /* Returns nonzero if GNU extensions are allowed. */
25056
25057 static bool
25058 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25059 {
25060 return parser->allow_gnu_extensions_p;
25061 }
25062 \f
25063 /* Objective-C++ Productions */
25064
25065
25066 /* Parse an Objective-C expression, which feeds into a primary-expression
25067 above.
25068
25069 objc-expression:
25070 objc-message-expression
25071 objc-string-literal
25072 objc-encode-expression
25073 objc-protocol-expression
25074 objc-selector-expression
25075
25076 Returns a tree representation of the expression. */
25077
25078 static tree
25079 cp_parser_objc_expression (cp_parser* parser)
25080 {
25081 /* Try to figure out what kind of declaration is present. */
25082 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25083
25084 switch (kwd->type)
25085 {
25086 case CPP_OPEN_SQUARE:
25087 return cp_parser_objc_message_expression (parser);
25088
25089 case CPP_OBJC_STRING:
25090 kwd = cp_lexer_consume_token (parser->lexer);
25091 return objc_build_string_object (kwd->u.value);
25092
25093 case CPP_KEYWORD:
25094 switch (kwd->keyword)
25095 {
25096 case RID_AT_ENCODE:
25097 return cp_parser_objc_encode_expression (parser);
25098
25099 case RID_AT_PROTOCOL:
25100 return cp_parser_objc_protocol_expression (parser);
25101
25102 case RID_AT_SELECTOR:
25103 return cp_parser_objc_selector_expression (parser);
25104
25105 default:
25106 break;
25107 }
25108 default:
25109 error_at (kwd->location,
25110 "misplaced %<@%D%> Objective-C++ construct",
25111 kwd->u.value);
25112 cp_parser_skip_to_end_of_block_or_statement (parser);
25113 }
25114
25115 return error_mark_node;
25116 }
25117
25118 /* Parse an Objective-C message expression.
25119
25120 objc-message-expression:
25121 [ objc-message-receiver objc-message-args ]
25122
25123 Returns a representation of an Objective-C message. */
25124
25125 static tree
25126 cp_parser_objc_message_expression (cp_parser* parser)
25127 {
25128 tree receiver, messageargs;
25129
25130 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25131 receiver = cp_parser_objc_message_receiver (parser);
25132 messageargs = cp_parser_objc_message_args (parser);
25133 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25134
25135 return objc_build_message_expr (receiver, messageargs);
25136 }
25137
25138 /* Parse an objc-message-receiver.
25139
25140 objc-message-receiver:
25141 expression
25142 simple-type-specifier
25143
25144 Returns a representation of the type or expression. */
25145
25146 static tree
25147 cp_parser_objc_message_receiver (cp_parser* parser)
25148 {
25149 tree rcv;
25150
25151 /* An Objective-C message receiver may be either (1) a type
25152 or (2) an expression. */
25153 cp_parser_parse_tentatively (parser);
25154 rcv = cp_parser_expression (parser, false, NULL);
25155
25156 if (cp_parser_parse_definitely (parser))
25157 return rcv;
25158
25159 rcv = cp_parser_simple_type_specifier (parser,
25160 /*decl_specs=*/NULL,
25161 CP_PARSER_FLAGS_NONE);
25162
25163 return objc_get_class_reference (rcv);
25164 }
25165
25166 /* Parse the arguments and selectors comprising an Objective-C message.
25167
25168 objc-message-args:
25169 objc-selector
25170 objc-selector-args
25171 objc-selector-args , objc-comma-args
25172
25173 objc-selector-args:
25174 objc-selector [opt] : assignment-expression
25175 objc-selector-args objc-selector [opt] : assignment-expression
25176
25177 objc-comma-args:
25178 assignment-expression
25179 objc-comma-args , assignment-expression
25180
25181 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25182 selector arguments and TREE_VALUE containing a list of comma
25183 arguments. */
25184
25185 static tree
25186 cp_parser_objc_message_args (cp_parser* parser)
25187 {
25188 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25189 bool maybe_unary_selector_p = true;
25190 cp_token *token = cp_lexer_peek_token (parser->lexer);
25191
25192 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25193 {
25194 tree selector = NULL_TREE, arg;
25195
25196 if (token->type != CPP_COLON)
25197 selector = cp_parser_objc_selector (parser);
25198
25199 /* Detect if we have a unary selector. */
25200 if (maybe_unary_selector_p
25201 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25202 return build_tree_list (selector, NULL_TREE);
25203
25204 maybe_unary_selector_p = false;
25205 cp_parser_require (parser, CPP_COLON, RT_COLON);
25206 arg = cp_parser_assignment_expression (parser, false, NULL);
25207
25208 sel_args
25209 = chainon (sel_args,
25210 build_tree_list (selector, arg));
25211
25212 token = cp_lexer_peek_token (parser->lexer);
25213 }
25214
25215 /* Handle non-selector arguments, if any. */
25216 while (token->type == CPP_COMMA)
25217 {
25218 tree arg;
25219
25220 cp_lexer_consume_token (parser->lexer);
25221 arg = cp_parser_assignment_expression (parser, false, NULL);
25222
25223 addl_args
25224 = chainon (addl_args,
25225 build_tree_list (NULL_TREE, arg));
25226
25227 token = cp_lexer_peek_token (parser->lexer);
25228 }
25229
25230 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25231 {
25232 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25233 return build_tree_list (error_mark_node, error_mark_node);
25234 }
25235
25236 return build_tree_list (sel_args, addl_args);
25237 }
25238
25239 /* Parse an Objective-C encode expression.
25240
25241 objc-encode-expression:
25242 @encode objc-typename
25243
25244 Returns an encoded representation of the type argument. */
25245
25246 static tree
25247 cp_parser_objc_encode_expression (cp_parser* parser)
25248 {
25249 tree type;
25250 cp_token *token;
25251
25252 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25253 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25254 token = cp_lexer_peek_token (parser->lexer);
25255 type = complete_type (cp_parser_type_id (parser));
25256 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25257
25258 if (!type)
25259 {
25260 error_at (token->location,
25261 "%<@encode%> must specify a type as an argument");
25262 return error_mark_node;
25263 }
25264
25265 /* This happens if we find @encode(T) (where T is a template
25266 typename or something dependent on a template typename) when
25267 parsing a template. In that case, we can't compile it
25268 immediately, but we rather create an AT_ENCODE_EXPR which will
25269 need to be instantiated when the template is used.
25270 */
25271 if (dependent_type_p (type))
25272 {
25273 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25274 TREE_READONLY (value) = 1;
25275 return value;
25276 }
25277
25278 return objc_build_encode_expr (type);
25279 }
25280
25281 /* Parse an Objective-C @defs expression. */
25282
25283 static tree
25284 cp_parser_objc_defs_expression (cp_parser *parser)
25285 {
25286 tree name;
25287
25288 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25289 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25290 name = cp_parser_identifier (parser);
25291 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25292
25293 return objc_get_class_ivars (name);
25294 }
25295
25296 /* Parse an Objective-C protocol expression.
25297
25298 objc-protocol-expression:
25299 @protocol ( identifier )
25300
25301 Returns a representation of the protocol expression. */
25302
25303 static tree
25304 cp_parser_objc_protocol_expression (cp_parser* parser)
25305 {
25306 tree proto;
25307
25308 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25309 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25310 proto = cp_parser_identifier (parser);
25311 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25312
25313 return objc_build_protocol_expr (proto);
25314 }
25315
25316 /* Parse an Objective-C selector expression.
25317
25318 objc-selector-expression:
25319 @selector ( objc-method-signature )
25320
25321 objc-method-signature:
25322 objc-selector
25323 objc-selector-seq
25324
25325 objc-selector-seq:
25326 objc-selector :
25327 objc-selector-seq objc-selector :
25328
25329 Returns a representation of the method selector. */
25330
25331 static tree
25332 cp_parser_objc_selector_expression (cp_parser* parser)
25333 {
25334 tree sel_seq = NULL_TREE;
25335 bool maybe_unary_selector_p = true;
25336 cp_token *token;
25337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25338
25339 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25340 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25341 token = cp_lexer_peek_token (parser->lexer);
25342
25343 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25344 || token->type == CPP_SCOPE)
25345 {
25346 tree selector = NULL_TREE;
25347
25348 if (token->type != CPP_COLON
25349 || token->type == CPP_SCOPE)
25350 selector = cp_parser_objc_selector (parser);
25351
25352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25353 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25354 {
25355 /* Detect if we have a unary selector. */
25356 if (maybe_unary_selector_p)
25357 {
25358 sel_seq = selector;
25359 goto finish_selector;
25360 }
25361 else
25362 {
25363 cp_parser_error (parser, "expected %<:%>");
25364 }
25365 }
25366 maybe_unary_selector_p = false;
25367 token = cp_lexer_consume_token (parser->lexer);
25368
25369 if (token->type == CPP_SCOPE)
25370 {
25371 sel_seq
25372 = chainon (sel_seq,
25373 build_tree_list (selector, NULL_TREE));
25374 sel_seq
25375 = chainon (sel_seq,
25376 build_tree_list (NULL_TREE, NULL_TREE));
25377 }
25378 else
25379 sel_seq
25380 = chainon (sel_seq,
25381 build_tree_list (selector, NULL_TREE));
25382
25383 token = cp_lexer_peek_token (parser->lexer);
25384 }
25385
25386 finish_selector:
25387 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25388
25389 return objc_build_selector_expr (loc, sel_seq);
25390 }
25391
25392 /* Parse a list of identifiers.
25393
25394 objc-identifier-list:
25395 identifier
25396 objc-identifier-list , identifier
25397
25398 Returns a TREE_LIST of identifier nodes. */
25399
25400 static tree
25401 cp_parser_objc_identifier_list (cp_parser* parser)
25402 {
25403 tree identifier;
25404 tree list;
25405 cp_token *sep;
25406
25407 identifier = cp_parser_identifier (parser);
25408 if (identifier == error_mark_node)
25409 return error_mark_node;
25410
25411 list = build_tree_list (NULL_TREE, identifier);
25412 sep = cp_lexer_peek_token (parser->lexer);
25413
25414 while (sep->type == CPP_COMMA)
25415 {
25416 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25417 identifier = cp_parser_identifier (parser);
25418 if (identifier == error_mark_node)
25419 return list;
25420
25421 list = chainon (list, build_tree_list (NULL_TREE,
25422 identifier));
25423 sep = cp_lexer_peek_token (parser->lexer);
25424 }
25425
25426 return list;
25427 }
25428
25429 /* Parse an Objective-C alias declaration.
25430
25431 objc-alias-declaration:
25432 @compatibility_alias identifier identifier ;
25433
25434 This function registers the alias mapping with the Objective-C front end.
25435 It returns nothing. */
25436
25437 static void
25438 cp_parser_objc_alias_declaration (cp_parser* parser)
25439 {
25440 tree alias, orig;
25441
25442 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25443 alias = cp_parser_identifier (parser);
25444 orig = cp_parser_identifier (parser);
25445 objc_declare_alias (alias, orig);
25446 cp_parser_consume_semicolon_at_end_of_statement (parser);
25447 }
25448
25449 /* Parse an Objective-C class forward-declaration.
25450
25451 objc-class-declaration:
25452 @class objc-identifier-list ;
25453
25454 The function registers the forward declarations with the Objective-C
25455 front end. It returns nothing. */
25456
25457 static void
25458 cp_parser_objc_class_declaration (cp_parser* parser)
25459 {
25460 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25461 while (true)
25462 {
25463 tree id;
25464
25465 id = cp_parser_identifier (parser);
25466 if (id == error_mark_node)
25467 break;
25468
25469 objc_declare_class (id);
25470
25471 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25472 cp_lexer_consume_token (parser->lexer);
25473 else
25474 break;
25475 }
25476 cp_parser_consume_semicolon_at_end_of_statement (parser);
25477 }
25478
25479 /* Parse a list of Objective-C protocol references.
25480
25481 objc-protocol-refs-opt:
25482 objc-protocol-refs [opt]
25483
25484 objc-protocol-refs:
25485 < objc-identifier-list >
25486
25487 Returns a TREE_LIST of identifiers, if any. */
25488
25489 static tree
25490 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25491 {
25492 tree protorefs = NULL_TREE;
25493
25494 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25495 {
25496 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25497 protorefs = cp_parser_objc_identifier_list (parser);
25498 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25499 }
25500
25501 return protorefs;
25502 }
25503
25504 /* Parse a Objective-C visibility specification. */
25505
25506 static void
25507 cp_parser_objc_visibility_spec (cp_parser* parser)
25508 {
25509 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25510
25511 switch (vis->keyword)
25512 {
25513 case RID_AT_PRIVATE:
25514 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25515 break;
25516 case RID_AT_PROTECTED:
25517 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25518 break;
25519 case RID_AT_PUBLIC:
25520 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25521 break;
25522 case RID_AT_PACKAGE:
25523 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25524 break;
25525 default:
25526 return;
25527 }
25528
25529 /* Eat '@private'/'@protected'/'@public'. */
25530 cp_lexer_consume_token (parser->lexer);
25531 }
25532
25533 /* Parse an Objective-C method type. Return 'true' if it is a class
25534 (+) method, and 'false' if it is an instance (-) method. */
25535
25536 static inline bool
25537 cp_parser_objc_method_type (cp_parser* parser)
25538 {
25539 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25540 return true;
25541 else
25542 return false;
25543 }
25544
25545 /* Parse an Objective-C protocol qualifier. */
25546
25547 static tree
25548 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25549 {
25550 tree quals = NULL_TREE, node;
25551 cp_token *token = cp_lexer_peek_token (parser->lexer);
25552
25553 node = token->u.value;
25554
25555 while (node && identifier_p (node)
25556 && (node == ridpointers [(int) RID_IN]
25557 || node == ridpointers [(int) RID_OUT]
25558 || node == ridpointers [(int) RID_INOUT]
25559 || node == ridpointers [(int) RID_BYCOPY]
25560 || node == ridpointers [(int) RID_BYREF]
25561 || node == ridpointers [(int) RID_ONEWAY]))
25562 {
25563 quals = tree_cons (NULL_TREE, node, quals);
25564 cp_lexer_consume_token (parser->lexer);
25565 token = cp_lexer_peek_token (parser->lexer);
25566 node = token->u.value;
25567 }
25568
25569 return quals;
25570 }
25571
25572 /* Parse an Objective-C typename. */
25573
25574 static tree
25575 cp_parser_objc_typename (cp_parser* parser)
25576 {
25577 tree type_name = NULL_TREE;
25578
25579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25580 {
25581 tree proto_quals, cp_type = NULL_TREE;
25582
25583 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25584 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25585
25586 /* An ObjC type name may consist of just protocol qualifiers, in which
25587 case the type shall default to 'id'. */
25588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25589 {
25590 cp_type = cp_parser_type_id (parser);
25591
25592 /* If the type could not be parsed, an error has already
25593 been produced. For error recovery, behave as if it had
25594 not been specified, which will use the default type
25595 'id'. */
25596 if (cp_type == error_mark_node)
25597 {
25598 cp_type = NULL_TREE;
25599 /* We need to skip to the closing parenthesis as
25600 cp_parser_type_id() does not seem to do it for
25601 us. */
25602 cp_parser_skip_to_closing_parenthesis (parser,
25603 /*recovering=*/true,
25604 /*or_comma=*/false,
25605 /*consume_paren=*/false);
25606 }
25607 }
25608
25609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25610 type_name = build_tree_list (proto_quals, cp_type);
25611 }
25612
25613 return type_name;
25614 }
25615
25616 /* Check to see if TYPE refers to an Objective-C selector name. */
25617
25618 static bool
25619 cp_parser_objc_selector_p (enum cpp_ttype type)
25620 {
25621 return (type == CPP_NAME || type == CPP_KEYWORD
25622 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25623 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25624 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25625 || type == CPP_XOR || type == CPP_XOR_EQ);
25626 }
25627
25628 /* Parse an Objective-C selector. */
25629
25630 static tree
25631 cp_parser_objc_selector (cp_parser* parser)
25632 {
25633 cp_token *token = cp_lexer_consume_token (parser->lexer);
25634
25635 if (!cp_parser_objc_selector_p (token->type))
25636 {
25637 error_at (token->location, "invalid Objective-C++ selector name");
25638 return error_mark_node;
25639 }
25640
25641 /* C++ operator names are allowed to appear in ObjC selectors. */
25642 switch (token->type)
25643 {
25644 case CPP_AND_AND: return get_identifier ("and");
25645 case CPP_AND_EQ: return get_identifier ("and_eq");
25646 case CPP_AND: return get_identifier ("bitand");
25647 case CPP_OR: return get_identifier ("bitor");
25648 case CPP_COMPL: return get_identifier ("compl");
25649 case CPP_NOT: return get_identifier ("not");
25650 case CPP_NOT_EQ: return get_identifier ("not_eq");
25651 case CPP_OR_OR: return get_identifier ("or");
25652 case CPP_OR_EQ: return get_identifier ("or_eq");
25653 case CPP_XOR: return get_identifier ("xor");
25654 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25655 default: return token->u.value;
25656 }
25657 }
25658
25659 /* Parse an Objective-C params list. */
25660
25661 static tree
25662 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25663 {
25664 tree params = NULL_TREE;
25665 bool maybe_unary_selector_p = true;
25666 cp_token *token = cp_lexer_peek_token (parser->lexer);
25667
25668 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25669 {
25670 tree selector = NULL_TREE, type_name, identifier;
25671 tree parm_attr = NULL_TREE;
25672
25673 if (token->keyword == RID_ATTRIBUTE)
25674 break;
25675
25676 if (token->type != CPP_COLON)
25677 selector = cp_parser_objc_selector (parser);
25678
25679 /* Detect if we have a unary selector. */
25680 if (maybe_unary_selector_p
25681 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25682 {
25683 params = selector; /* Might be followed by attributes. */
25684 break;
25685 }
25686
25687 maybe_unary_selector_p = false;
25688 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25689 {
25690 /* Something went quite wrong. There should be a colon
25691 here, but there is not. Stop parsing parameters. */
25692 break;
25693 }
25694 type_name = cp_parser_objc_typename (parser);
25695 /* New ObjC allows attributes on parameters too. */
25696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25697 parm_attr = cp_parser_attributes_opt (parser);
25698 identifier = cp_parser_identifier (parser);
25699
25700 params
25701 = chainon (params,
25702 objc_build_keyword_decl (selector,
25703 type_name,
25704 identifier,
25705 parm_attr));
25706
25707 token = cp_lexer_peek_token (parser->lexer);
25708 }
25709
25710 if (params == NULL_TREE)
25711 {
25712 cp_parser_error (parser, "objective-c++ method declaration is expected");
25713 return error_mark_node;
25714 }
25715
25716 /* We allow tail attributes for the method. */
25717 if (token->keyword == RID_ATTRIBUTE)
25718 {
25719 *attributes = cp_parser_attributes_opt (parser);
25720 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25721 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25722 return params;
25723 cp_parser_error (parser,
25724 "method attributes must be specified at the end");
25725 return error_mark_node;
25726 }
25727
25728 if (params == NULL_TREE)
25729 {
25730 cp_parser_error (parser, "objective-c++ method declaration is expected");
25731 return error_mark_node;
25732 }
25733 return params;
25734 }
25735
25736 /* Parse the non-keyword Objective-C params. */
25737
25738 static tree
25739 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25740 tree* attributes)
25741 {
25742 tree params = make_node (TREE_LIST);
25743 cp_token *token = cp_lexer_peek_token (parser->lexer);
25744 *ellipsisp = false; /* Initially, assume no ellipsis. */
25745
25746 while (token->type == CPP_COMMA)
25747 {
25748 cp_parameter_declarator *parmdecl;
25749 tree parm;
25750
25751 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25752 token = cp_lexer_peek_token (parser->lexer);
25753
25754 if (token->type == CPP_ELLIPSIS)
25755 {
25756 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25757 *ellipsisp = true;
25758 token = cp_lexer_peek_token (parser->lexer);
25759 break;
25760 }
25761
25762 /* TODO: parse attributes for tail parameters. */
25763 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25764 parm = grokdeclarator (parmdecl->declarator,
25765 &parmdecl->decl_specifiers,
25766 PARM, /*initialized=*/0,
25767 /*attrlist=*/NULL);
25768
25769 chainon (params, build_tree_list (NULL_TREE, parm));
25770 token = cp_lexer_peek_token (parser->lexer);
25771 }
25772
25773 /* We allow tail attributes for the method. */
25774 if (token->keyword == RID_ATTRIBUTE)
25775 {
25776 if (*attributes == NULL_TREE)
25777 {
25778 *attributes = cp_parser_attributes_opt (parser);
25779 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25780 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25781 return params;
25782 }
25783 else
25784 /* We have an error, but parse the attributes, so that we can
25785 carry on. */
25786 *attributes = cp_parser_attributes_opt (parser);
25787
25788 cp_parser_error (parser,
25789 "method attributes must be specified at the end");
25790 return error_mark_node;
25791 }
25792
25793 return params;
25794 }
25795
25796 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25797
25798 static void
25799 cp_parser_objc_interstitial_code (cp_parser* parser)
25800 {
25801 cp_token *token = cp_lexer_peek_token (parser->lexer);
25802
25803 /* If the next token is `extern' and the following token is a string
25804 literal, then we have a linkage specification. */
25805 if (token->keyword == RID_EXTERN
25806 && cp_parser_is_pure_string_literal
25807 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25808 cp_parser_linkage_specification (parser);
25809 /* Handle #pragma, if any. */
25810 else if (token->type == CPP_PRAGMA)
25811 cp_parser_pragma (parser, pragma_objc_icode);
25812 /* Allow stray semicolons. */
25813 else if (token->type == CPP_SEMICOLON)
25814 cp_lexer_consume_token (parser->lexer);
25815 /* Mark methods as optional or required, when building protocols. */
25816 else if (token->keyword == RID_AT_OPTIONAL)
25817 {
25818 cp_lexer_consume_token (parser->lexer);
25819 objc_set_method_opt (true);
25820 }
25821 else if (token->keyword == RID_AT_REQUIRED)
25822 {
25823 cp_lexer_consume_token (parser->lexer);
25824 objc_set_method_opt (false);
25825 }
25826 else if (token->keyword == RID_NAMESPACE)
25827 cp_parser_namespace_definition (parser);
25828 /* Other stray characters must generate errors. */
25829 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25830 {
25831 cp_lexer_consume_token (parser->lexer);
25832 error ("stray %qs between Objective-C++ methods",
25833 token->type == CPP_OPEN_BRACE ? "{" : "}");
25834 }
25835 /* Finally, try to parse a block-declaration, or a function-definition. */
25836 else
25837 cp_parser_block_declaration (parser, /*statement_p=*/false);
25838 }
25839
25840 /* Parse a method signature. */
25841
25842 static tree
25843 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25844 {
25845 tree rettype, kwdparms, optparms;
25846 bool ellipsis = false;
25847 bool is_class_method;
25848
25849 is_class_method = cp_parser_objc_method_type (parser);
25850 rettype = cp_parser_objc_typename (parser);
25851 *attributes = NULL_TREE;
25852 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25853 if (kwdparms == error_mark_node)
25854 return error_mark_node;
25855 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25856 if (optparms == error_mark_node)
25857 return error_mark_node;
25858
25859 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25860 }
25861
25862 static bool
25863 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25864 {
25865 tree tattr;
25866 cp_lexer_save_tokens (parser->lexer);
25867 tattr = cp_parser_attributes_opt (parser);
25868 gcc_assert (tattr) ;
25869
25870 /* If the attributes are followed by a method introducer, this is not allowed.
25871 Dump the attributes and flag the situation. */
25872 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25873 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25874 return true;
25875
25876 /* Otherwise, the attributes introduce some interstitial code, possibly so
25877 rewind to allow that check. */
25878 cp_lexer_rollback_tokens (parser->lexer);
25879 return false;
25880 }
25881
25882 /* Parse an Objective-C method prototype list. */
25883
25884 static void
25885 cp_parser_objc_method_prototype_list (cp_parser* parser)
25886 {
25887 cp_token *token = cp_lexer_peek_token (parser->lexer);
25888
25889 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25890 {
25891 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25892 {
25893 tree attributes, sig;
25894 bool is_class_method;
25895 if (token->type == CPP_PLUS)
25896 is_class_method = true;
25897 else
25898 is_class_method = false;
25899 sig = cp_parser_objc_method_signature (parser, &attributes);
25900 if (sig == error_mark_node)
25901 {
25902 cp_parser_skip_to_end_of_block_or_statement (parser);
25903 token = cp_lexer_peek_token (parser->lexer);
25904 continue;
25905 }
25906 objc_add_method_declaration (is_class_method, sig, attributes);
25907 cp_parser_consume_semicolon_at_end_of_statement (parser);
25908 }
25909 else if (token->keyword == RID_AT_PROPERTY)
25910 cp_parser_objc_at_property_declaration (parser);
25911 else if (token->keyword == RID_ATTRIBUTE
25912 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25913 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25914 OPT_Wattributes,
25915 "prefix attributes are ignored for methods");
25916 else
25917 /* Allow for interspersed non-ObjC++ code. */
25918 cp_parser_objc_interstitial_code (parser);
25919
25920 token = cp_lexer_peek_token (parser->lexer);
25921 }
25922
25923 if (token->type != CPP_EOF)
25924 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25925 else
25926 cp_parser_error (parser, "expected %<@end%>");
25927
25928 objc_finish_interface ();
25929 }
25930
25931 /* Parse an Objective-C method definition list. */
25932
25933 static void
25934 cp_parser_objc_method_definition_list (cp_parser* parser)
25935 {
25936 cp_token *token = cp_lexer_peek_token (parser->lexer);
25937
25938 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25939 {
25940 tree meth;
25941
25942 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25943 {
25944 cp_token *ptk;
25945 tree sig, attribute;
25946 bool is_class_method;
25947 if (token->type == CPP_PLUS)
25948 is_class_method = true;
25949 else
25950 is_class_method = false;
25951 push_deferring_access_checks (dk_deferred);
25952 sig = cp_parser_objc_method_signature (parser, &attribute);
25953 if (sig == error_mark_node)
25954 {
25955 cp_parser_skip_to_end_of_block_or_statement (parser);
25956 token = cp_lexer_peek_token (parser->lexer);
25957 continue;
25958 }
25959 objc_start_method_definition (is_class_method, sig, attribute,
25960 NULL_TREE);
25961
25962 /* For historical reasons, we accept an optional semicolon. */
25963 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25964 cp_lexer_consume_token (parser->lexer);
25965
25966 ptk = cp_lexer_peek_token (parser->lexer);
25967 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25968 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25969 {
25970 perform_deferred_access_checks (tf_warning_or_error);
25971 stop_deferring_access_checks ();
25972 meth = cp_parser_function_definition_after_declarator (parser,
25973 false);
25974 pop_deferring_access_checks ();
25975 objc_finish_method_definition (meth);
25976 }
25977 }
25978 /* The following case will be removed once @synthesize is
25979 completely implemented. */
25980 else if (token->keyword == RID_AT_PROPERTY)
25981 cp_parser_objc_at_property_declaration (parser);
25982 else if (token->keyword == RID_AT_SYNTHESIZE)
25983 cp_parser_objc_at_synthesize_declaration (parser);
25984 else if (token->keyword == RID_AT_DYNAMIC)
25985 cp_parser_objc_at_dynamic_declaration (parser);
25986 else if (token->keyword == RID_ATTRIBUTE
25987 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25988 warning_at (token->location, OPT_Wattributes,
25989 "prefix attributes are ignored for methods");
25990 else
25991 /* Allow for interspersed non-ObjC++ code. */
25992 cp_parser_objc_interstitial_code (parser);
25993
25994 token = cp_lexer_peek_token (parser->lexer);
25995 }
25996
25997 if (token->type != CPP_EOF)
25998 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25999 else
26000 cp_parser_error (parser, "expected %<@end%>");
26001
26002 objc_finish_implementation ();
26003 }
26004
26005 /* Parse Objective-C ivars. */
26006
26007 static void
26008 cp_parser_objc_class_ivars (cp_parser* parser)
26009 {
26010 cp_token *token = cp_lexer_peek_token (parser->lexer);
26011
26012 if (token->type != CPP_OPEN_BRACE)
26013 return; /* No ivars specified. */
26014
26015 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26016 token = cp_lexer_peek_token (parser->lexer);
26017
26018 while (token->type != CPP_CLOSE_BRACE
26019 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26020 {
26021 cp_decl_specifier_seq declspecs;
26022 int decl_class_or_enum_p;
26023 tree prefix_attributes;
26024
26025 cp_parser_objc_visibility_spec (parser);
26026
26027 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26028 break;
26029
26030 cp_parser_decl_specifier_seq (parser,
26031 CP_PARSER_FLAGS_OPTIONAL,
26032 &declspecs,
26033 &decl_class_or_enum_p);
26034
26035 /* auto, register, static, extern, mutable. */
26036 if (declspecs.storage_class != sc_none)
26037 {
26038 cp_parser_error (parser, "invalid type for instance variable");
26039 declspecs.storage_class = sc_none;
26040 }
26041
26042 /* thread_local. */
26043 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26044 {
26045 cp_parser_error (parser, "invalid type for instance variable");
26046 declspecs.locations[ds_thread] = 0;
26047 }
26048
26049 /* typedef. */
26050 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26051 {
26052 cp_parser_error (parser, "invalid type for instance variable");
26053 declspecs.locations[ds_typedef] = 0;
26054 }
26055
26056 prefix_attributes = declspecs.attributes;
26057 declspecs.attributes = NULL_TREE;
26058
26059 /* Keep going until we hit the `;' at the end of the
26060 declaration. */
26061 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26062 {
26063 tree width = NULL_TREE, attributes, first_attribute, decl;
26064 cp_declarator *declarator = NULL;
26065 int ctor_dtor_or_conv_p;
26066
26067 /* Check for a (possibly unnamed) bitfield declaration. */
26068 token = cp_lexer_peek_token (parser->lexer);
26069 if (token->type == CPP_COLON)
26070 goto eat_colon;
26071
26072 if (token->type == CPP_NAME
26073 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26074 == CPP_COLON))
26075 {
26076 /* Get the name of the bitfield. */
26077 declarator = make_id_declarator (NULL_TREE,
26078 cp_parser_identifier (parser),
26079 sfk_none);
26080
26081 eat_colon:
26082 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26083 /* Get the width of the bitfield. */
26084 width
26085 = cp_parser_constant_expression (parser,
26086 /*allow_non_constant=*/false,
26087 NULL);
26088 }
26089 else
26090 {
26091 /* Parse the declarator. */
26092 declarator
26093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26094 &ctor_dtor_or_conv_p,
26095 /*parenthesized_p=*/NULL,
26096 /*member_p=*/false);
26097 }
26098
26099 /* Look for attributes that apply to the ivar. */
26100 attributes = cp_parser_attributes_opt (parser);
26101 /* Remember which attributes are prefix attributes and
26102 which are not. */
26103 first_attribute = attributes;
26104 /* Combine the attributes. */
26105 attributes = chainon (prefix_attributes, attributes);
26106
26107 if (width)
26108 /* Create the bitfield declaration. */
26109 decl = grokbitfield (declarator, &declspecs,
26110 width,
26111 attributes);
26112 else
26113 decl = grokfield (declarator, &declspecs,
26114 NULL_TREE, /*init_const_expr_p=*/false,
26115 NULL_TREE, attributes);
26116
26117 /* Add the instance variable. */
26118 if (decl != error_mark_node && decl != NULL_TREE)
26119 objc_add_instance_variable (decl);
26120
26121 /* Reset PREFIX_ATTRIBUTES. */
26122 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26123 attributes = TREE_CHAIN (attributes);
26124 if (attributes)
26125 TREE_CHAIN (attributes) = NULL_TREE;
26126
26127 token = cp_lexer_peek_token (parser->lexer);
26128
26129 if (token->type == CPP_COMMA)
26130 {
26131 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26132 continue;
26133 }
26134 break;
26135 }
26136
26137 cp_parser_consume_semicolon_at_end_of_statement (parser);
26138 token = cp_lexer_peek_token (parser->lexer);
26139 }
26140
26141 if (token->keyword == RID_AT_END)
26142 cp_parser_error (parser, "expected %<}%>");
26143
26144 /* Do not consume the RID_AT_END, so it will be read again as terminating
26145 the @interface of @implementation. */
26146 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26147 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26148
26149 /* For historical reasons, we accept an optional semicolon. */
26150 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26151 cp_lexer_consume_token (parser->lexer);
26152 }
26153
26154 /* Parse an Objective-C protocol declaration. */
26155
26156 static void
26157 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26158 {
26159 tree proto, protorefs;
26160 cp_token *tok;
26161
26162 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26164 {
26165 tok = cp_lexer_peek_token (parser->lexer);
26166 error_at (tok->location, "identifier expected after %<@protocol%>");
26167 cp_parser_consume_semicolon_at_end_of_statement (parser);
26168 return;
26169 }
26170
26171 /* See if we have a forward declaration or a definition. */
26172 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26173
26174 /* Try a forward declaration first. */
26175 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26176 {
26177 while (true)
26178 {
26179 tree id;
26180
26181 id = cp_parser_identifier (parser);
26182 if (id == error_mark_node)
26183 break;
26184
26185 objc_declare_protocol (id, attributes);
26186
26187 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26188 cp_lexer_consume_token (parser->lexer);
26189 else
26190 break;
26191 }
26192 cp_parser_consume_semicolon_at_end_of_statement (parser);
26193 }
26194
26195 /* Ok, we got a full-fledged definition (or at least should). */
26196 else
26197 {
26198 proto = cp_parser_identifier (parser);
26199 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26200 objc_start_protocol (proto, protorefs, attributes);
26201 cp_parser_objc_method_prototype_list (parser);
26202 }
26203 }
26204
26205 /* Parse an Objective-C superclass or category. */
26206
26207 static void
26208 cp_parser_objc_superclass_or_category (cp_parser *parser,
26209 bool iface_p,
26210 tree *super,
26211 tree *categ, bool *is_class_extension)
26212 {
26213 cp_token *next = cp_lexer_peek_token (parser->lexer);
26214
26215 *super = *categ = NULL_TREE;
26216 *is_class_extension = false;
26217 if (next->type == CPP_COLON)
26218 {
26219 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26220 *super = cp_parser_identifier (parser);
26221 }
26222 else if (next->type == CPP_OPEN_PAREN)
26223 {
26224 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26225
26226 /* If there is no category name, and this is an @interface, we
26227 have a class extension. */
26228 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26229 {
26230 *categ = NULL_TREE;
26231 *is_class_extension = true;
26232 }
26233 else
26234 *categ = cp_parser_identifier (parser);
26235
26236 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26237 }
26238 }
26239
26240 /* Parse an Objective-C class interface. */
26241
26242 static void
26243 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26244 {
26245 tree name, super, categ, protos;
26246 bool is_class_extension;
26247
26248 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26249 name = cp_parser_identifier (parser);
26250 if (name == error_mark_node)
26251 {
26252 /* It's hard to recover because even if valid @interface stuff
26253 is to follow, we can't compile it (or validate it) if we
26254 don't even know which class it refers to. Let's assume this
26255 was a stray '@interface' token in the stream and skip it.
26256 */
26257 return;
26258 }
26259 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26260 &is_class_extension);
26261 protos = cp_parser_objc_protocol_refs_opt (parser);
26262
26263 /* We have either a class or a category on our hands. */
26264 if (categ || is_class_extension)
26265 objc_start_category_interface (name, categ, protos, attributes);
26266 else
26267 {
26268 objc_start_class_interface (name, super, protos, attributes);
26269 /* Handle instance variable declarations, if any. */
26270 cp_parser_objc_class_ivars (parser);
26271 objc_continue_interface ();
26272 }
26273
26274 cp_parser_objc_method_prototype_list (parser);
26275 }
26276
26277 /* Parse an Objective-C class implementation. */
26278
26279 static void
26280 cp_parser_objc_class_implementation (cp_parser* parser)
26281 {
26282 tree name, super, categ;
26283 bool is_class_extension;
26284
26285 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26286 name = cp_parser_identifier (parser);
26287 if (name == error_mark_node)
26288 {
26289 /* It's hard to recover because even if valid @implementation
26290 stuff is to follow, we can't compile it (or validate it) if
26291 we don't even know which class it refers to. Let's assume
26292 this was a stray '@implementation' token in the stream and
26293 skip it.
26294 */
26295 return;
26296 }
26297 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26298 &is_class_extension);
26299
26300 /* We have either a class or a category on our hands. */
26301 if (categ)
26302 objc_start_category_implementation (name, categ);
26303 else
26304 {
26305 objc_start_class_implementation (name, super);
26306 /* Handle instance variable declarations, if any. */
26307 cp_parser_objc_class_ivars (parser);
26308 objc_continue_implementation ();
26309 }
26310
26311 cp_parser_objc_method_definition_list (parser);
26312 }
26313
26314 /* Consume the @end token and finish off the implementation. */
26315
26316 static void
26317 cp_parser_objc_end_implementation (cp_parser* parser)
26318 {
26319 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26320 objc_finish_implementation ();
26321 }
26322
26323 /* Parse an Objective-C declaration. */
26324
26325 static void
26326 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26327 {
26328 /* Try to figure out what kind of declaration is present. */
26329 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26330
26331 if (attributes)
26332 switch (kwd->keyword)
26333 {
26334 case RID_AT_ALIAS:
26335 case RID_AT_CLASS:
26336 case RID_AT_END:
26337 error_at (kwd->location, "attributes may not be specified before"
26338 " the %<@%D%> Objective-C++ keyword",
26339 kwd->u.value);
26340 attributes = NULL;
26341 break;
26342 case RID_AT_IMPLEMENTATION:
26343 warning_at (kwd->location, OPT_Wattributes,
26344 "prefix attributes are ignored before %<@%D%>",
26345 kwd->u.value);
26346 attributes = NULL;
26347 default:
26348 break;
26349 }
26350
26351 switch (kwd->keyword)
26352 {
26353 case RID_AT_ALIAS:
26354 cp_parser_objc_alias_declaration (parser);
26355 break;
26356 case RID_AT_CLASS:
26357 cp_parser_objc_class_declaration (parser);
26358 break;
26359 case RID_AT_PROTOCOL:
26360 cp_parser_objc_protocol_declaration (parser, attributes);
26361 break;
26362 case RID_AT_INTERFACE:
26363 cp_parser_objc_class_interface (parser, attributes);
26364 break;
26365 case RID_AT_IMPLEMENTATION:
26366 cp_parser_objc_class_implementation (parser);
26367 break;
26368 case RID_AT_END:
26369 cp_parser_objc_end_implementation (parser);
26370 break;
26371 default:
26372 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26373 kwd->u.value);
26374 cp_parser_skip_to_end_of_block_or_statement (parser);
26375 }
26376 }
26377
26378 /* Parse an Objective-C try-catch-finally statement.
26379
26380 objc-try-catch-finally-stmt:
26381 @try compound-statement objc-catch-clause-seq [opt]
26382 objc-finally-clause [opt]
26383
26384 objc-catch-clause-seq:
26385 objc-catch-clause objc-catch-clause-seq [opt]
26386
26387 objc-catch-clause:
26388 @catch ( objc-exception-declaration ) compound-statement
26389
26390 objc-finally-clause:
26391 @finally compound-statement
26392
26393 objc-exception-declaration:
26394 parameter-declaration
26395 '...'
26396
26397 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26398
26399 Returns NULL_TREE.
26400
26401 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26402 for C. Keep them in sync. */
26403
26404 static tree
26405 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26406 {
26407 location_t location;
26408 tree stmt;
26409
26410 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26411 location = cp_lexer_peek_token (parser->lexer)->location;
26412 objc_maybe_warn_exceptions (location);
26413 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26414 node, lest it get absorbed into the surrounding block. */
26415 stmt = push_stmt_list ();
26416 cp_parser_compound_statement (parser, NULL, false, false);
26417 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26418
26419 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26420 {
26421 cp_parameter_declarator *parm;
26422 tree parameter_declaration = error_mark_node;
26423 bool seen_open_paren = false;
26424
26425 cp_lexer_consume_token (parser->lexer);
26426 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26427 seen_open_paren = true;
26428 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26429 {
26430 /* We have "@catch (...)" (where the '...' are literally
26431 what is in the code). Skip the '...'.
26432 parameter_declaration is set to NULL_TREE, and
26433 objc_being_catch_clauses() knows that that means
26434 '...'. */
26435 cp_lexer_consume_token (parser->lexer);
26436 parameter_declaration = NULL_TREE;
26437 }
26438 else
26439 {
26440 /* We have "@catch (NSException *exception)" or something
26441 like that. Parse the parameter declaration. */
26442 parm = cp_parser_parameter_declaration (parser, false, NULL);
26443 if (parm == NULL)
26444 parameter_declaration = error_mark_node;
26445 else
26446 parameter_declaration = grokdeclarator (parm->declarator,
26447 &parm->decl_specifiers,
26448 PARM, /*initialized=*/0,
26449 /*attrlist=*/NULL);
26450 }
26451 if (seen_open_paren)
26452 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26453 else
26454 {
26455 /* If there was no open parenthesis, we are recovering from
26456 an error, and we are trying to figure out what mistake
26457 the user has made. */
26458
26459 /* If there is an immediate closing parenthesis, the user
26460 probably forgot the opening one (ie, they typed "@catch
26461 NSException *e)". Parse the closing parenthesis and keep
26462 going. */
26463 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26464 cp_lexer_consume_token (parser->lexer);
26465
26466 /* If these is no immediate closing parenthesis, the user
26467 probably doesn't know that parenthesis are required at
26468 all (ie, they typed "@catch NSException *e"). So, just
26469 forget about the closing parenthesis and keep going. */
26470 }
26471 objc_begin_catch_clause (parameter_declaration);
26472 cp_parser_compound_statement (parser, NULL, false, false);
26473 objc_finish_catch_clause ();
26474 }
26475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26476 {
26477 cp_lexer_consume_token (parser->lexer);
26478 location = cp_lexer_peek_token (parser->lexer)->location;
26479 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26480 node, lest it get absorbed into the surrounding block. */
26481 stmt = push_stmt_list ();
26482 cp_parser_compound_statement (parser, NULL, false, false);
26483 objc_build_finally_clause (location, pop_stmt_list (stmt));
26484 }
26485
26486 return objc_finish_try_stmt ();
26487 }
26488
26489 /* Parse an Objective-C synchronized statement.
26490
26491 objc-synchronized-stmt:
26492 @synchronized ( expression ) compound-statement
26493
26494 Returns NULL_TREE. */
26495
26496 static tree
26497 cp_parser_objc_synchronized_statement (cp_parser *parser)
26498 {
26499 location_t location;
26500 tree lock, stmt;
26501
26502 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26503
26504 location = cp_lexer_peek_token (parser->lexer)->location;
26505 objc_maybe_warn_exceptions (location);
26506 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26507 lock = cp_parser_expression (parser, false, NULL);
26508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26509
26510 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26511 node, lest it get absorbed into the surrounding block. */
26512 stmt = push_stmt_list ();
26513 cp_parser_compound_statement (parser, NULL, false, false);
26514
26515 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26516 }
26517
26518 /* Parse an Objective-C throw statement.
26519
26520 objc-throw-stmt:
26521 @throw assignment-expression [opt] ;
26522
26523 Returns a constructed '@throw' statement. */
26524
26525 static tree
26526 cp_parser_objc_throw_statement (cp_parser *parser)
26527 {
26528 tree expr = NULL_TREE;
26529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26530
26531 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26532
26533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26534 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26535
26536 cp_parser_consume_semicolon_at_end_of_statement (parser);
26537
26538 return objc_build_throw_stmt (loc, expr);
26539 }
26540
26541 /* Parse an Objective-C statement. */
26542
26543 static tree
26544 cp_parser_objc_statement (cp_parser * parser)
26545 {
26546 /* Try to figure out what kind of declaration is present. */
26547 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26548
26549 switch (kwd->keyword)
26550 {
26551 case RID_AT_TRY:
26552 return cp_parser_objc_try_catch_finally_statement (parser);
26553 case RID_AT_SYNCHRONIZED:
26554 return cp_parser_objc_synchronized_statement (parser);
26555 case RID_AT_THROW:
26556 return cp_parser_objc_throw_statement (parser);
26557 default:
26558 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26559 kwd->u.value);
26560 cp_parser_skip_to_end_of_block_or_statement (parser);
26561 }
26562
26563 return error_mark_node;
26564 }
26565
26566 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26567 look ahead to see if an objc keyword follows the attributes. This
26568 is to detect the use of prefix attributes on ObjC @interface and
26569 @protocol. */
26570
26571 static bool
26572 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26573 {
26574 cp_lexer_save_tokens (parser->lexer);
26575 *attrib = cp_parser_attributes_opt (parser);
26576 gcc_assert (*attrib);
26577 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26578 {
26579 cp_lexer_commit_tokens (parser->lexer);
26580 return true;
26581 }
26582 cp_lexer_rollback_tokens (parser->lexer);
26583 return false;
26584 }
26585
26586 /* This routine is a minimal replacement for
26587 c_parser_struct_declaration () used when parsing the list of
26588 types/names or ObjC++ properties. For example, when parsing the
26589 code
26590
26591 @property (readonly) int a, b, c;
26592
26593 this function is responsible for parsing "int a, int b, int c" and
26594 returning the declarations as CHAIN of DECLs.
26595
26596 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26597 similar parsing. */
26598 static tree
26599 cp_parser_objc_struct_declaration (cp_parser *parser)
26600 {
26601 tree decls = NULL_TREE;
26602 cp_decl_specifier_seq declspecs;
26603 int decl_class_or_enum_p;
26604 tree prefix_attributes;
26605
26606 cp_parser_decl_specifier_seq (parser,
26607 CP_PARSER_FLAGS_NONE,
26608 &declspecs,
26609 &decl_class_or_enum_p);
26610
26611 if (declspecs.type == error_mark_node)
26612 return error_mark_node;
26613
26614 /* auto, register, static, extern, mutable. */
26615 if (declspecs.storage_class != sc_none)
26616 {
26617 cp_parser_error (parser, "invalid type for property");
26618 declspecs.storage_class = sc_none;
26619 }
26620
26621 /* thread_local. */
26622 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26623 {
26624 cp_parser_error (parser, "invalid type for property");
26625 declspecs.locations[ds_thread] = 0;
26626 }
26627
26628 /* typedef. */
26629 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26630 {
26631 cp_parser_error (parser, "invalid type for property");
26632 declspecs.locations[ds_typedef] = 0;
26633 }
26634
26635 prefix_attributes = declspecs.attributes;
26636 declspecs.attributes = NULL_TREE;
26637
26638 /* Keep going until we hit the `;' at the end of the declaration. */
26639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26640 {
26641 tree attributes, first_attribute, decl;
26642 cp_declarator *declarator;
26643 cp_token *token;
26644
26645 /* Parse the declarator. */
26646 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26647 NULL, NULL, false);
26648
26649 /* Look for attributes that apply to the ivar. */
26650 attributes = cp_parser_attributes_opt (parser);
26651 /* Remember which attributes are prefix attributes and
26652 which are not. */
26653 first_attribute = attributes;
26654 /* Combine the attributes. */
26655 attributes = chainon (prefix_attributes, attributes);
26656
26657 decl = grokfield (declarator, &declspecs,
26658 NULL_TREE, /*init_const_expr_p=*/false,
26659 NULL_TREE, attributes);
26660
26661 if (decl == error_mark_node || decl == NULL_TREE)
26662 return error_mark_node;
26663
26664 /* Reset PREFIX_ATTRIBUTES. */
26665 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26666 attributes = TREE_CHAIN (attributes);
26667 if (attributes)
26668 TREE_CHAIN (attributes) = NULL_TREE;
26669
26670 DECL_CHAIN (decl) = decls;
26671 decls = decl;
26672
26673 token = cp_lexer_peek_token (parser->lexer);
26674 if (token->type == CPP_COMMA)
26675 {
26676 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26677 continue;
26678 }
26679 else
26680 break;
26681 }
26682 return decls;
26683 }
26684
26685 /* Parse an Objective-C @property declaration. The syntax is:
26686
26687 objc-property-declaration:
26688 '@property' objc-property-attributes[opt] struct-declaration ;
26689
26690 objc-property-attributes:
26691 '(' objc-property-attribute-list ')'
26692
26693 objc-property-attribute-list:
26694 objc-property-attribute
26695 objc-property-attribute-list, objc-property-attribute
26696
26697 objc-property-attribute
26698 'getter' = identifier
26699 'setter' = identifier
26700 'readonly'
26701 'readwrite'
26702 'assign'
26703 'retain'
26704 'copy'
26705 'nonatomic'
26706
26707 For example:
26708 @property NSString *name;
26709 @property (readonly) id object;
26710 @property (retain, nonatomic, getter=getTheName) id name;
26711 @property int a, b, c;
26712
26713 PS: This function is identical to
26714 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26715 static void
26716 cp_parser_objc_at_property_declaration (cp_parser *parser)
26717 {
26718 /* The following variables hold the attributes of the properties as
26719 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26720 seen. When we see an attribute, we set them to 'true' (if they
26721 are boolean properties) or to the identifier (if they have an
26722 argument, ie, for getter and setter). Note that here we only
26723 parse the list of attributes, check the syntax and accumulate the
26724 attributes that we find. objc_add_property_declaration() will
26725 then process the information. */
26726 bool property_assign = false;
26727 bool property_copy = false;
26728 tree property_getter_ident = NULL_TREE;
26729 bool property_nonatomic = false;
26730 bool property_readonly = false;
26731 bool property_readwrite = false;
26732 bool property_retain = false;
26733 tree property_setter_ident = NULL_TREE;
26734
26735 /* 'properties' is the list of properties that we read. Usually a
26736 single one, but maybe more (eg, in "@property int a, b, c;" there
26737 are three). */
26738 tree properties;
26739 location_t loc;
26740
26741 loc = cp_lexer_peek_token (parser->lexer)->location;
26742
26743 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26744
26745 /* Parse the optional attribute list... */
26746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26747 {
26748 /* Eat the '('. */
26749 cp_lexer_consume_token (parser->lexer);
26750
26751 while (true)
26752 {
26753 bool syntax_error = false;
26754 cp_token *token = cp_lexer_peek_token (parser->lexer);
26755 enum rid keyword;
26756
26757 if (token->type != CPP_NAME)
26758 {
26759 cp_parser_error (parser, "expected identifier");
26760 break;
26761 }
26762 keyword = C_RID_CODE (token->u.value);
26763 cp_lexer_consume_token (parser->lexer);
26764 switch (keyword)
26765 {
26766 case RID_ASSIGN: property_assign = true; break;
26767 case RID_COPY: property_copy = true; break;
26768 case RID_NONATOMIC: property_nonatomic = true; break;
26769 case RID_READONLY: property_readonly = true; break;
26770 case RID_READWRITE: property_readwrite = true; break;
26771 case RID_RETAIN: property_retain = true; break;
26772
26773 case RID_GETTER:
26774 case RID_SETTER:
26775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26776 {
26777 if (keyword == RID_GETTER)
26778 cp_parser_error (parser,
26779 "missing %<=%> (after %<getter%> attribute)");
26780 else
26781 cp_parser_error (parser,
26782 "missing %<=%> (after %<setter%> attribute)");
26783 syntax_error = true;
26784 break;
26785 }
26786 cp_lexer_consume_token (parser->lexer); /* eat the = */
26787 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26788 {
26789 cp_parser_error (parser, "expected identifier");
26790 syntax_error = true;
26791 break;
26792 }
26793 if (keyword == RID_SETTER)
26794 {
26795 if (property_setter_ident != NULL_TREE)
26796 {
26797 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26798 cp_lexer_consume_token (parser->lexer);
26799 }
26800 else
26801 property_setter_ident = cp_parser_objc_selector (parser);
26802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26803 cp_parser_error (parser, "setter name must terminate with %<:%>");
26804 else
26805 cp_lexer_consume_token (parser->lexer);
26806 }
26807 else
26808 {
26809 if (property_getter_ident != NULL_TREE)
26810 {
26811 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26812 cp_lexer_consume_token (parser->lexer);
26813 }
26814 else
26815 property_getter_ident = cp_parser_objc_selector (parser);
26816 }
26817 break;
26818 default:
26819 cp_parser_error (parser, "unknown property attribute");
26820 syntax_error = true;
26821 break;
26822 }
26823
26824 if (syntax_error)
26825 break;
26826
26827 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26828 cp_lexer_consume_token (parser->lexer);
26829 else
26830 break;
26831 }
26832
26833 /* FIXME: "@property (setter, assign);" will generate a spurious
26834 "error: expected ‘)’ before ‘,’ token". This is because
26835 cp_parser_require, unlike the C counterpart, will produce an
26836 error even if we are in error recovery. */
26837 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26838 {
26839 cp_parser_skip_to_closing_parenthesis (parser,
26840 /*recovering=*/true,
26841 /*or_comma=*/false,
26842 /*consume_paren=*/true);
26843 }
26844 }
26845
26846 /* ... and the property declaration(s). */
26847 properties = cp_parser_objc_struct_declaration (parser);
26848
26849 if (properties == error_mark_node)
26850 {
26851 cp_parser_skip_to_end_of_statement (parser);
26852 /* If the next token is now a `;', consume it. */
26853 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26854 cp_lexer_consume_token (parser->lexer);
26855 return;
26856 }
26857
26858 if (properties == NULL_TREE)
26859 cp_parser_error (parser, "expected identifier");
26860 else
26861 {
26862 /* Comma-separated properties are chained together in
26863 reverse order; add them one by one. */
26864 properties = nreverse (properties);
26865
26866 for (; properties; properties = TREE_CHAIN (properties))
26867 objc_add_property_declaration (loc, copy_node (properties),
26868 property_readonly, property_readwrite,
26869 property_assign, property_retain,
26870 property_copy, property_nonatomic,
26871 property_getter_ident, property_setter_ident);
26872 }
26873
26874 cp_parser_consume_semicolon_at_end_of_statement (parser);
26875 }
26876
26877 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26878
26879 objc-synthesize-declaration:
26880 @synthesize objc-synthesize-identifier-list ;
26881
26882 objc-synthesize-identifier-list:
26883 objc-synthesize-identifier
26884 objc-synthesize-identifier-list, objc-synthesize-identifier
26885
26886 objc-synthesize-identifier
26887 identifier
26888 identifier = identifier
26889
26890 For example:
26891 @synthesize MyProperty;
26892 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26893
26894 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26895 for C. Keep them in sync.
26896 */
26897 static void
26898 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26899 {
26900 tree list = NULL_TREE;
26901 location_t loc;
26902 loc = cp_lexer_peek_token (parser->lexer)->location;
26903
26904 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26905 while (true)
26906 {
26907 tree property, ivar;
26908 property = cp_parser_identifier (parser);
26909 if (property == error_mark_node)
26910 {
26911 cp_parser_consume_semicolon_at_end_of_statement (parser);
26912 return;
26913 }
26914 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26915 {
26916 cp_lexer_consume_token (parser->lexer);
26917 ivar = cp_parser_identifier (parser);
26918 if (ivar == error_mark_node)
26919 {
26920 cp_parser_consume_semicolon_at_end_of_statement (parser);
26921 return;
26922 }
26923 }
26924 else
26925 ivar = NULL_TREE;
26926 list = chainon (list, build_tree_list (ivar, property));
26927 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26928 cp_lexer_consume_token (parser->lexer);
26929 else
26930 break;
26931 }
26932 cp_parser_consume_semicolon_at_end_of_statement (parser);
26933 objc_add_synthesize_declaration (loc, list);
26934 }
26935
26936 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26937
26938 objc-dynamic-declaration:
26939 @dynamic identifier-list ;
26940
26941 For example:
26942 @dynamic MyProperty;
26943 @dynamic MyProperty, AnotherProperty;
26944
26945 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26946 for C. Keep them in sync.
26947 */
26948 static void
26949 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26950 {
26951 tree list = NULL_TREE;
26952 location_t loc;
26953 loc = cp_lexer_peek_token (parser->lexer)->location;
26954
26955 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26956 while (true)
26957 {
26958 tree property;
26959 property = cp_parser_identifier (parser);
26960 if (property == error_mark_node)
26961 {
26962 cp_parser_consume_semicolon_at_end_of_statement (parser);
26963 return;
26964 }
26965 list = chainon (list, build_tree_list (NULL, property));
26966 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26967 cp_lexer_consume_token (parser->lexer);
26968 else
26969 break;
26970 }
26971 cp_parser_consume_semicolon_at_end_of_statement (parser);
26972 objc_add_dynamic_declaration (loc, list);
26973 }
26974
26975 \f
26976 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26977
26978 /* Returns name of the next clause.
26979 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26980 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26981 returned and the token is consumed. */
26982
26983 static pragma_omp_clause
26984 cp_parser_omp_clause_name (cp_parser *parser)
26985 {
26986 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26987
26988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26989 result = PRAGMA_OMP_CLAUSE_IF;
26990 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26991 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26992 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26993 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26994 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26995 result = PRAGMA_OMP_CLAUSE_FOR;
26996 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26997 {
26998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26999 const char *p = IDENTIFIER_POINTER (id);
27000
27001 switch (p[0])
27002 {
27003 case 'a':
27004 if (!strcmp ("aligned", p))
27005 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27006 break;
27007 case 'c':
27008 if (!strcmp ("collapse", p))
27009 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27010 else if (!strcmp ("copyin", p))
27011 result = PRAGMA_OMP_CLAUSE_COPYIN;
27012 else if (!strcmp ("copyprivate", p))
27013 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27014 break;
27015 case 'd':
27016 if (!strcmp ("depend", p))
27017 result = PRAGMA_OMP_CLAUSE_DEPEND;
27018 else if (!strcmp ("device", p))
27019 result = PRAGMA_OMP_CLAUSE_DEVICE;
27020 else if (!strcmp ("dist_schedule", p))
27021 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27022 break;
27023 case 'f':
27024 if (!strcmp ("final", p))
27025 result = PRAGMA_OMP_CLAUSE_FINAL;
27026 else if (!strcmp ("firstprivate", p))
27027 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27028 else if (!strcmp ("from", p))
27029 result = PRAGMA_OMP_CLAUSE_FROM;
27030 break;
27031 case 'i':
27032 if (!strcmp ("inbranch", p))
27033 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27034 break;
27035 case 'l':
27036 if (!strcmp ("lastprivate", p))
27037 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27038 else if (!strcmp ("linear", p))
27039 result = PRAGMA_OMP_CLAUSE_LINEAR;
27040 break;
27041 case 'm':
27042 if (!strcmp ("map", p))
27043 result = PRAGMA_OMP_CLAUSE_MAP;
27044 else if (!strcmp ("mergeable", p))
27045 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27046 else if (flag_cilkplus && !strcmp ("mask", p))
27047 result = PRAGMA_CILK_CLAUSE_MASK;
27048 break;
27049 case 'n':
27050 if (!strcmp ("notinbranch", p))
27051 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27052 else if (!strcmp ("nowait", p))
27053 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27054 else if (flag_cilkplus && !strcmp ("nomask", p))
27055 result = PRAGMA_CILK_CLAUSE_NOMASK;
27056 else if (!strcmp ("num_teams", p))
27057 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27058 else if (!strcmp ("num_threads", p))
27059 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27060 break;
27061 case 'o':
27062 if (!strcmp ("ordered", p))
27063 result = PRAGMA_OMP_CLAUSE_ORDERED;
27064 break;
27065 case 'p':
27066 if (!strcmp ("parallel", p))
27067 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27068 else if (!strcmp ("proc_bind", p))
27069 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27070 break;
27071 case 'r':
27072 if (!strcmp ("reduction", p))
27073 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27074 break;
27075 case 's':
27076 if (!strcmp ("safelen", p))
27077 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27078 else if (!strcmp ("schedule", p))
27079 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27080 else if (!strcmp ("sections", p))
27081 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27082 else if (!strcmp ("shared", p))
27083 result = PRAGMA_OMP_CLAUSE_SHARED;
27084 else if (!strcmp ("simdlen", p))
27085 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27086 break;
27087 case 't':
27088 if (!strcmp ("taskgroup", p))
27089 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27090 else if (!strcmp ("thread_limit", p))
27091 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27092 else if (!strcmp ("to", p))
27093 result = PRAGMA_OMP_CLAUSE_TO;
27094 break;
27095 case 'u':
27096 if (!strcmp ("uniform", p))
27097 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27098 else if (!strcmp ("untied", p))
27099 result = PRAGMA_OMP_CLAUSE_UNTIED;
27100 break;
27101 case 'v':
27102 if (flag_cilkplus && !strcmp ("vectorlength", p))
27103 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27104 break;
27105 }
27106 }
27107
27108 if (result != PRAGMA_OMP_CLAUSE_NONE)
27109 cp_lexer_consume_token (parser->lexer);
27110
27111 return result;
27112 }
27113
27114 /* Validate that a clause of the given type does not already exist. */
27115
27116 static void
27117 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27118 const char *name, location_t location)
27119 {
27120 tree c;
27121
27122 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27123 if (OMP_CLAUSE_CODE (c) == code)
27124 {
27125 error_at (location, "too many %qs clauses", name);
27126 break;
27127 }
27128 }
27129
27130 /* OpenMP 2.5:
27131 variable-list:
27132 identifier
27133 variable-list , identifier
27134
27135 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27136 colon). An opening parenthesis will have been consumed by the caller.
27137
27138 If KIND is nonzero, create the appropriate node and install the decl
27139 in OMP_CLAUSE_DECL and add the node to the head of the list.
27140
27141 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27142 return the list created.
27143
27144 COLON can be NULL if only closing parenthesis should end the list,
27145 or pointer to bool which will receive false if the list is terminated
27146 by closing parenthesis or true if the list is terminated by colon. */
27147
27148 static tree
27149 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27150 tree list, bool *colon)
27151 {
27152 cp_token *token;
27153 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27154 if (colon)
27155 {
27156 parser->colon_corrects_to_scope_p = false;
27157 *colon = false;
27158 }
27159 while (1)
27160 {
27161 tree name, decl;
27162
27163 token = cp_lexer_peek_token (parser->lexer);
27164 name = cp_parser_id_expression (parser, /*template_p=*/false,
27165 /*check_dependency_p=*/true,
27166 /*template_p=*/NULL,
27167 /*declarator_p=*/false,
27168 /*optional_p=*/false);
27169 if (name == error_mark_node)
27170 goto skip_comma;
27171
27172 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27173 if (decl == error_mark_node)
27174 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27175 token->location);
27176 else if (kind != 0)
27177 {
27178 switch (kind)
27179 {
27180 case OMP_CLAUSE_MAP:
27181 case OMP_CLAUSE_FROM:
27182 case OMP_CLAUSE_TO:
27183 case OMP_CLAUSE_DEPEND:
27184 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27185 {
27186 tree low_bound = NULL_TREE, length = NULL_TREE;
27187
27188 parser->colon_corrects_to_scope_p = false;
27189 cp_lexer_consume_token (parser->lexer);
27190 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27191 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27192 NULL);
27193 if (!colon)
27194 parser->colon_corrects_to_scope_p
27195 = saved_colon_corrects_to_scope_p;
27196 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27197 length = integer_one_node;
27198 else
27199 {
27200 /* Look for `:'. */
27201 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27202 goto skip_comma;
27203 if (!cp_lexer_next_token_is (parser->lexer,
27204 CPP_CLOSE_SQUARE))
27205 length = cp_parser_expression (parser,
27206 /*cast_p=*/false,
27207 NULL);
27208 }
27209 /* Look for the closing `]'. */
27210 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27211 RT_CLOSE_SQUARE))
27212 goto skip_comma;
27213 decl = tree_cons (low_bound, length, decl);
27214 }
27215 break;
27216 default:
27217 break;
27218 }
27219
27220 tree u = build_omp_clause (token->location, kind);
27221 OMP_CLAUSE_DECL (u) = decl;
27222 OMP_CLAUSE_CHAIN (u) = list;
27223 list = u;
27224 }
27225 else
27226 list = tree_cons (decl, NULL_TREE, list);
27227
27228 get_comma:
27229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27230 break;
27231 cp_lexer_consume_token (parser->lexer);
27232 }
27233
27234 if (colon)
27235 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27236
27237 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27238 {
27239 *colon = true;
27240 cp_parser_require (parser, CPP_COLON, RT_COLON);
27241 return list;
27242 }
27243
27244 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27245 {
27246 int ending;
27247
27248 /* Try to resync to an unnested comma. Copied from
27249 cp_parser_parenthesized_expression_list. */
27250 skip_comma:
27251 if (colon)
27252 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27253 ending = cp_parser_skip_to_closing_parenthesis (parser,
27254 /*recovering=*/true,
27255 /*or_comma=*/true,
27256 /*consume_paren=*/true);
27257 if (ending < 0)
27258 goto get_comma;
27259 }
27260
27261 return list;
27262 }
27263
27264 /* Similarly, but expect leading and trailing parenthesis. This is a very
27265 common case for omp clauses. */
27266
27267 static tree
27268 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27269 {
27270 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27271 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27272 return list;
27273 }
27274
27275 /* OpenMP 3.0:
27276 collapse ( constant-expression ) */
27277
27278 static tree
27279 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27280 {
27281 tree c, num;
27282 location_t loc;
27283 HOST_WIDE_INT n;
27284
27285 loc = cp_lexer_peek_token (parser->lexer)->location;
27286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27287 return list;
27288
27289 num = cp_parser_constant_expression (parser, false, NULL);
27290
27291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27293 /*or_comma=*/false,
27294 /*consume_paren=*/true);
27295
27296 if (num == error_mark_node)
27297 return list;
27298 num = fold_non_dependent_expr (num);
27299 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27300 || !tree_fits_shwi_p (num)
27301 || (n = tree_to_shwi (num)) <= 0
27302 || (int) n != n)
27303 {
27304 error_at (loc, "collapse argument needs positive constant integer expression");
27305 return list;
27306 }
27307
27308 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27309 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27310 OMP_CLAUSE_CHAIN (c) = list;
27311 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27312
27313 return c;
27314 }
27315
27316 /* OpenMP 2.5:
27317 default ( shared | none ) */
27318
27319 static tree
27320 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27321 {
27322 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27323 tree c;
27324
27325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27326 return list;
27327 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27328 {
27329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27330 const char *p = IDENTIFIER_POINTER (id);
27331
27332 switch (p[0])
27333 {
27334 case 'n':
27335 if (strcmp ("none", p) != 0)
27336 goto invalid_kind;
27337 kind = OMP_CLAUSE_DEFAULT_NONE;
27338 break;
27339
27340 case 's':
27341 if (strcmp ("shared", p) != 0)
27342 goto invalid_kind;
27343 kind = OMP_CLAUSE_DEFAULT_SHARED;
27344 break;
27345
27346 default:
27347 goto invalid_kind;
27348 }
27349
27350 cp_lexer_consume_token (parser->lexer);
27351 }
27352 else
27353 {
27354 invalid_kind:
27355 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27356 }
27357
27358 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27359 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27360 /*or_comma=*/false,
27361 /*consume_paren=*/true);
27362
27363 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27364 return list;
27365
27366 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27367 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27368 OMP_CLAUSE_CHAIN (c) = list;
27369 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27370
27371 return c;
27372 }
27373
27374 /* OpenMP 3.1:
27375 final ( expression ) */
27376
27377 static tree
27378 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27379 {
27380 tree t, c;
27381
27382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27383 return list;
27384
27385 t = cp_parser_condition (parser);
27386
27387 if (t == error_mark_node
27388 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27390 /*or_comma=*/false,
27391 /*consume_paren=*/true);
27392
27393 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27394
27395 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27396 OMP_CLAUSE_FINAL_EXPR (c) = t;
27397 OMP_CLAUSE_CHAIN (c) = list;
27398
27399 return c;
27400 }
27401
27402 /* OpenMP 2.5:
27403 if ( expression ) */
27404
27405 static tree
27406 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27407 {
27408 tree t, c;
27409
27410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27411 return list;
27412
27413 t = cp_parser_condition (parser);
27414
27415 if (t == error_mark_node
27416 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27417 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27418 /*or_comma=*/false,
27419 /*consume_paren=*/true);
27420
27421 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27422
27423 c = build_omp_clause (location, OMP_CLAUSE_IF);
27424 OMP_CLAUSE_IF_EXPR (c) = t;
27425 OMP_CLAUSE_CHAIN (c) = list;
27426
27427 return c;
27428 }
27429
27430 /* OpenMP 3.1:
27431 mergeable */
27432
27433 static tree
27434 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27435 tree list, location_t location)
27436 {
27437 tree c;
27438
27439 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27440 location);
27441
27442 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27443 OMP_CLAUSE_CHAIN (c) = list;
27444 return c;
27445 }
27446
27447 /* OpenMP 2.5:
27448 nowait */
27449
27450 static tree
27451 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27452 tree list, location_t location)
27453 {
27454 tree c;
27455
27456 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27457
27458 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27459 OMP_CLAUSE_CHAIN (c) = list;
27460 return c;
27461 }
27462
27463 /* OpenMP 2.5:
27464 num_threads ( expression ) */
27465
27466 static tree
27467 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27468 location_t location)
27469 {
27470 tree t, c;
27471
27472 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27473 return list;
27474
27475 t = cp_parser_expression (parser, false, NULL);
27476
27477 if (t == error_mark_node
27478 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27480 /*or_comma=*/false,
27481 /*consume_paren=*/true);
27482
27483 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27484 "num_threads", location);
27485
27486 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27487 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27488 OMP_CLAUSE_CHAIN (c) = list;
27489
27490 return c;
27491 }
27492
27493 /* OpenMP 2.5:
27494 ordered */
27495
27496 static tree
27497 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27498 tree list, location_t location)
27499 {
27500 tree c;
27501
27502 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27503 "ordered", location);
27504
27505 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27506 OMP_CLAUSE_CHAIN (c) = list;
27507 return c;
27508 }
27509
27510 /* OpenMP 2.5:
27511 reduction ( reduction-operator : variable-list )
27512
27513 reduction-operator:
27514 One of: + * - & ^ | && ||
27515
27516 OpenMP 3.1:
27517
27518 reduction-operator:
27519 One of: + * - & ^ | && || min max
27520
27521 OpenMP 4.0:
27522
27523 reduction-operator:
27524 One of: + * - & ^ | && ||
27525 id-expression */
27526
27527 static tree
27528 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27529 {
27530 enum tree_code code = ERROR_MARK;
27531 tree nlist, c, id = NULL_TREE;
27532
27533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27534 return list;
27535
27536 switch (cp_lexer_peek_token (parser->lexer)->type)
27537 {
27538 case CPP_PLUS: code = PLUS_EXPR; break;
27539 case CPP_MULT: code = MULT_EXPR; break;
27540 case CPP_MINUS: code = MINUS_EXPR; break;
27541 case CPP_AND: code = BIT_AND_EXPR; break;
27542 case CPP_XOR: code = BIT_XOR_EXPR; break;
27543 case CPP_OR: code = BIT_IOR_EXPR; break;
27544 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27545 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27546 default: break;
27547 }
27548
27549 if (code != ERROR_MARK)
27550 cp_lexer_consume_token (parser->lexer);
27551 else
27552 {
27553 bool saved_colon_corrects_to_scope_p;
27554 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27555 parser->colon_corrects_to_scope_p = false;
27556 id = cp_parser_id_expression (parser, /*template_p=*/false,
27557 /*check_dependency_p=*/true,
27558 /*template_p=*/NULL,
27559 /*declarator_p=*/false,
27560 /*optional_p=*/false);
27561 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27562 if (identifier_p (id))
27563 {
27564 const char *p = IDENTIFIER_POINTER (id);
27565
27566 if (strcmp (p, "min") == 0)
27567 code = MIN_EXPR;
27568 else if (strcmp (p, "max") == 0)
27569 code = MAX_EXPR;
27570 else if (id == ansi_opname (PLUS_EXPR))
27571 code = PLUS_EXPR;
27572 else if (id == ansi_opname (MULT_EXPR))
27573 code = MULT_EXPR;
27574 else if (id == ansi_opname (MINUS_EXPR))
27575 code = MINUS_EXPR;
27576 else if (id == ansi_opname (BIT_AND_EXPR))
27577 code = BIT_AND_EXPR;
27578 else if (id == ansi_opname (BIT_IOR_EXPR))
27579 code = BIT_IOR_EXPR;
27580 else if (id == ansi_opname (BIT_XOR_EXPR))
27581 code = BIT_XOR_EXPR;
27582 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27583 code = TRUTH_ANDIF_EXPR;
27584 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27585 code = TRUTH_ORIF_EXPR;
27586 id = omp_reduction_id (code, id, NULL_TREE);
27587 tree scope = parser->scope;
27588 if (scope)
27589 id = build_qualified_name (NULL_TREE, scope, id, false);
27590 parser->scope = NULL_TREE;
27591 parser->qualifying_scope = NULL_TREE;
27592 parser->object_scope = NULL_TREE;
27593 }
27594 else
27595 {
27596 error ("invalid reduction-identifier");
27597 resync_fail:
27598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27599 /*or_comma=*/false,
27600 /*consume_paren=*/true);
27601 return list;
27602 }
27603 }
27604
27605 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27606 goto resync_fail;
27607
27608 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27609 NULL);
27610 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27611 {
27612 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27613 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27614 }
27615
27616 return nlist;
27617 }
27618
27619 /* OpenMP 2.5:
27620 schedule ( schedule-kind )
27621 schedule ( schedule-kind , expression )
27622
27623 schedule-kind:
27624 static | dynamic | guided | runtime | auto */
27625
27626 static tree
27627 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27628 {
27629 tree c, t;
27630
27631 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27632 return list;
27633
27634 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27635
27636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27637 {
27638 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27639 const char *p = IDENTIFIER_POINTER (id);
27640
27641 switch (p[0])
27642 {
27643 case 'd':
27644 if (strcmp ("dynamic", p) != 0)
27645 goto invalid_kind;
27646 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27647 break;
27648
27649 case 'g':
27650 if (strcmp ("guided", p) != 0)
27651 goto invalid_kind;
27652 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27653 break;
27654
27655 case 'r':
27656 if (strcmp ("runtime", p) != 0)
27657 goto invalid_kind;
27658 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27659 break;
27660
27661 default:
27662 goto invalid_kind;
27663 }
27664 }
27665 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27666 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27667 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27668 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27669 else
27670 goto invalid_kind;
27671 cp_lexer_consume_token (parser->lexer);
27672
27673 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27674 {
27675 cp_token *token;
27676 cp_lexer_consume_token (parser->lexer);
27677
27678 token = cp_lexer_peek_token (parser->lexer);
27679 t = cp_parser_assignment_expression (parser, false, NULL);
27680
27681 if (t == error_mark_node)
27682 goto resync_fail;
27683 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27684 error_at (token->location, "schedule %<runtime%> does not take "
27685 "a %<chunk_size%> parameter");
27686 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27687 error_at (token->location, "schedule %<auto%> does not take "
27688 "a %<chunk_size%> parameter");
27689 else
27690 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27691
27692 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27693 goto resync_fail;
27694 }
27695 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27696 goto resync_fail;
27697
27698 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27699 OMP_CLAUSE_CHAIN (c) = list;
27700 return c;
27701
27702 invalid_kind:
27703 cp_parser_error (parser, "invalid schedule kind");
27704 resync_fail:
27705 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27706 /*or_comma=*/false,
27707 /*consume_paren=*/true);
27708 return list;
27709 }
27710
27711 /* OpenMP 3.0:
27712 untied */
27713
27714 static tree
27715 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27716 tree list, location_t location)
27717 {
27718 tree c;
27719
27720 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27721
27722 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27723 OMP_CLAUSE_CHAIN (c) = list;
27724 return c;
27725 }
27726
27727 /* OpenMP 4.0:
27728 inbranch
27729 notinbranch */
27730
27731 static tree
27732 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27733 tree list, location_t location)
27734 {
27735 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27736 tree c = build_omp_clause (location, code);
27737 OMP_CLAUSE_CHAIN (c) = list;
27738 return c;
27739 }
27740
27741 /* OpenMP 4.0:
27742 parallel
27743 for
27744 sections
27745 taskgroup */
27746
27747 static tree
27748 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27749 enum omp_clause_code code,
27750 tree list, location_t location)
27751 {
27752 tree c = build_omp_clause (location, code);
27753 OMP_CLAUSE_CHAIN (c) = list;
27754 return c;
27755 }
27756
27757 /* OpenMP 4.0:
27758 num_teams ( expression ) */
27759
27760 static tree
27761 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27762 location_t location)
27763 {
27764 tree t, c;
27765
27766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27767 return list;
27768
27769 t = cp_parser_expression (parser, false, NULL);
27770
27771 if (t == error_mark_node
27772 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27774 /*or_comma=*/false,
27775 /*consume_paren=*/true);
27776
27777 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27778 "num_teams", location);
27779
27780 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27781 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27782 OMP_CLAUSE_CHAIN (c) = list;
27783
27784 return c;
27785 }
27786
27787 /* OpenMP 4.0:
27788 thread_limit ( expression ) */
27789
27790 static tree
27791 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27792 location_t location)
27793 {
27794 tree t, c;
27795
27796 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27797 return list;
27798
27799 t = cp_parser_expression (parser, false, NULL);
27800
27801 if (t == error_mark_node
27802 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27804 /*or_comma=*/false,
27805 /*consume_paren=*/true);
27806
27807 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27808 "thread_limit", location);
27809
27810 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27811 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27812 OMP_CLAUSE_CHAIN (c) = list;
27813
27814 return c;
27815 }
27816
27817 /* OpenMP 4.0:
27818 aligned ( variable-list )
27819 aligned ( variable-list : constant-expression ) */
27820
27821 static tree
27822 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27823 {
27824 tree nlist, c, alignment = NULL_TREE;
27825 bool colon;
27826
27827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27828 return list;
27829
27830 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27831 &colon);
27832
27833 if (colon)
27834 {
27835 alignment = cp_parser_constant_expression (parser, false, NULL);
27836
27837 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27839 /*or_comma=*/false,
27840 /*consume_paren=*/true);
27841
27842 if (alignment == error_mark_node)
27843 alignment = NULL_TREE;
27844 }
27845
27846 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27847 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27848
27849 return nlist;
27850 }
27851
27852 /* OpenMP 4.0:
27853 linear ( variable-list )
27854 linear ( variable-list : expression ) */
27855
27856 static tree
27857 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27858 bool is_cilk_simd_fn)
27859 {
27860 tree nlist, c, step = integer_one_node;
27861 bool colon;
27862
27863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27864 return list;
27865
27866 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27867 &colon);
27868
27869 if (colon)
27870 {
27871 step = cp_parser_expression (parser, false, NULL);
27872
27873 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27874 {
27875 sorry ("using parameters for %<linear%> step is not supported yet");
27876 step = integer_one_node;
27877 }
27878 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27879 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27880 /*or_comma=*/false,
27881 /*consume_paren=*/true);
27882
27883 if (step == error_mark_node)
27884 return list;
27885 }
27886
27887 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27888 OMP_CLAUSE_LINEAR_STEP (c) = step;
27889
27890 return nlist;
27891 }
27892
27893 /* OpenMP 4.0:
27894 safelen ( constant-expression ) */
27895
27896 static tree
27897 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27898 location_t location)
27899 {
27900 tree t, c;
27901
27902 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27903 return list;
27904
27905 t = cp_parser_constant_expression (parser, false, NULL);
27906
27907 if (t == error_mark_node
27908 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27909 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27910 /*or_comma=*/false,
27911 /*consume_paren=*/true);
27912
27913 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27914
27915 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27916 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27917 OMP_CLAUSE_CHAIN (c) = list;
27918
27919 return c;
27920 }
27921
27922 /* OpenMP 4.0:
27923 simdlen ( constant-expression ) */
27924
27925 static tree
27926 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27927 location_t location)
27928 {
27929 tree t, c;
27930
27931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27932 return list;
27933
27934 t = cp_parser_constant_expression (parser, false, NULL);
27935
27936 if (t == error_mark_node
27937 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27939 /*or_comma=*/false,
27940 /*consume_paren=*/true);
27941
27942 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27943
27944 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27945 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27946 OMP_CLAUSE_CHAIN (c) = list;
27947
27948 return c;
27949 }
27950
27951 /* OpenMP 4.0:
27952 depend ( depend-kind : variable-list )
27953
27954 depend-kind:
27955 in | out | inout */
27956
27957 static tree
27958 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27959 {
27960 tree nlist, c;
27961 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27962
27963 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27964 return list;
27965
27966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27967 {
27968 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27969 const char *p = IDENTIFIER_POINTER (id);
27970
27971 if (strcmp ("in", p) == 0)
27972 kind = OMP_CLAUSE_DEPEND_IN;
27973 else if (strcmp ("inout", p) == 0)
27974 kind = OMP_CLAUSE_DEPEND_INOUT;
27975 else if (strcmp ("out", p) == 0)
27976 kind = OMP_CLAUSE_DEPEND_OUT;
27977 else
27978 goto invalid_kind;
27979 }
27980 else
27981 goto invalid_kind;
27982
27983 cp_lexer_consume_token (parser->lexer);
27984 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27985 goto resync_fail;
27986
27987 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27988 NULL);
27989
27990 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27991 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27992
27993 return nlist;
27994
27995 invalid_kind:
27996 cp_parser_error (parser, "invalid depend kind");
27997 resync_fail:
27998 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27999 /*or_comma=*/false,
28000 /*consume_paren=*/true);
28001 return list;
28002 }
28003
28004 /* OpenMP 4.0:
28005 map ( map-kind : variable-list )
28006 map ( variable-list )
28007
28008 map-kind:
28009 alloc | to | from | tofrom */
28010
28011 static tree
28012 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28013 {
28014 tree nlist, c;
28015 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28016
28017 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28018 return list;
28019
28020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28021 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28022 {
28023 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28024 const char *p = IDENTIFIER_POINTER (id);
28025
28026 if (strcmp ("alloc", p) == 0)
28027 kind = OMP_CLAUSE_MAP_ALLOC;
28028 else if (strcmp ("to", p) == 0)
28029 kind = OMP_CLAUSE_MAP_TO;
28030 else if (strcmp ("from", p) == 0)
28031 kind = OMP_CLAUSE_MAP_FROM;
28032 else if (strcmp ("tofrom", p) == 0)
28033 kind = OMP_CLAUSE_MAP_TOFROM;
28034 else
28035 {
28036 cp_parser_error (parser, "invalid map kind");
28037 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28038 /*or_comma=*/false,
28039 /*consume_paren=*/true);
28040 return list;
28041 }
28042 cp_lexer_consume_token (parser->lexer);
28043 cp_lexer_consume_token (parser->lexer);
28044 }
28045
28046 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28047 NULL);
28048
28049 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28050 OMP_CLAUSE_MAP_KIND (c) = kind;
28051
28052 return nlist;
28053 }
28054
28055 /* OpenMP 4.0:
28056 device ( expression ) */
28057
28058 static tree
28059 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28060 location_t location)
28061 {
28062 tree t, c;
28063
28064 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28065 return list;
28066
28067 t = cp_parser_expression (parser, false, NULL);
28068
28069 if (t == error_mark_node
28070 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28072 /*or_comma=*/false,
28073 /*consume_paren=*/true);
28074
28075 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28076 "device", location);
28077
28078 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28079 OMP_CLAUSE_DEVICE_ID (c) = t;
28080 OMP_CLAUSE_CHAIN (c) = list;
28081
28082 return c;
28083 }
28084
28085 /* OpenMP 4.0:
28086 dist_schedule ( static )
28087 dist_schedule ( static , expression ) */
28088
28089 static tree
28090 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28091 location_t location)
28092 {
28093 tree c, t;
28094
28095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28096 return list;
28097
28098 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28099
28100 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28101 goto invalid_kind;
28102 cp_lexer_consume_token (parser->lexer);
28103
28104 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28105 {
28106 cp_lexer_consume_token (parser->lexer);
28107
28108 t = cp_parser_assignment_expression (parser, false, NULL);
28109
28110 if (t == error_mark_node)
28111 goto resync_fail;
28112 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28113
28114 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28115 goto resync_fail;
28116 }
28117 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28118 goto resync_fail;
28119
28120 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28121 location);
28122 OMP_CLAUSE_CHAIN (c) = list;
28123 return c;
28124
28125 invalid_kind:
28126 cp_parser_error (parser, "invalid dist_schedule kind");
28127 resync_fail:
28128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28129 /*or_comma=*/false,
28130 /*consume_paren=*/true);
28131 return list;
28132 }
28133
28134 /* OpenMP 4.0:
28135 proc_bind ( proc-bind-kind )
28136
28137 proc-bind-kind:
28138 master | close | spread */
28139
28140 static tree
28141 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28142 location_t location)
28143 {
28144 tree c;
28145 enum omp_clause_proc_bind_kind kind;
28146
28147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28148 return list;
28149
28150 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28151 {
28152 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28153 const char *p = IDENTIFIER_POINTER (id);
28154
28155 if (strcmp ("master", p) == 0)
28156 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28157 else if (strcmp ("close", p) == 0)
28158 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28159 else if (strcmp ("spread", p) == 0)
28160 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28161 else
28162 goto invalid_kind;
28163 }
28164 else
28165 goto invalid_kind;
28166
28167 cp_lexer_consume_token (parser->lexer);
28168 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28169 goto resync_fail;
28170
28171 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28172 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28173 location);
28174 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28175 OMP_CLAUSE_CHAIN (c) = list;
28176 return c;
28177
28178 invalid_kind:
28179 cp_parser_error (parser, "invalid depend kind");
28180 resync_fail:
28181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28182 /*or_comma=*/false,
28183 /*consume_paren=*/true);
28184 return list;
28185 }
28186
28187 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28188 is a bitmask in MASK. Return the list of clauses found; the result
28189 of clause default goes in *pdefault. */
28190
28191 static tree
28192 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28193 const char *where, cp_token *pragma_tok,
28194 bool finish_p = true)
28195 {
28196 tree clauses = NULL;
28197 bool first = true;
28198 cp_token *token = NULL;
28199 bool cilk_simd_fn = false;
28200
28201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28202 {
28203 pragma_omp_clause c_kind;
28204 const char *c_name;
28205 tree prev = clauses;
28206
28207 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28208 cp_lexer_consume_token (parser->lexer);
28209
28210 token = cp_lexer_peek_token (parser->lexer);
28211 c_kind = cp_parser_omp_clause_name (parser);
28212
28213 switch (c_kind)
28214 {
28215 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28216 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28217 token->location);
28218 c_name = "collapse";
28219 break;
28220 case PRAGMA_OMP_CLAUSE_COPYIN:
28221 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28222 c_name = "copyin";
28223 break;
28224 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28225 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28226 clauses);
28227 c_name = "copyprivate";
28228 break;
28229 case PRAGMA_OMP_CLAUSE_DEFAULT:
28230 clauses = cp_parser_omp_clause_default (parser, clauses,
28231 token->location);
28232 c_name = "default";
28233 break;
28234 case PRAGMA_OMP_CLAUSE_FINAL:
28235 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28236 c_name = "final";
28237 break;
28238 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28239 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28240 clauses);
28241 c_name = "firstprivate";
28242 break;
28243 case PRAGMA_OMP_CLAUSE_IF:
28244 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28245 c_name = "if";
28246 break;
28247 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28248 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28249 clauses);
28250 c_name = "lastprivate";
28251 break;
28252 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28253 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28254 token->location);
28255 c_name = "mergeable";
28256 break;
28257 case PRAGMA_OMP_CLAUSE_NOWAIT:
28258 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28259 c_name = "nowait";
28260 break;
28261 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28262 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28263 token->location);
28264 c_name = "num_threads";
28265 break;
28266 case PRAGMA_OMP_CLAUSE_ORDERED:
28267 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28268 token->location);
28269 c_name = "ordered";
28270 break;
28271 case PRAGMA_OMP_CLAUSE_PRIVATE:
28272 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28273 clauses);
28274 c_name = "private";
28275 break;
28276 case PRAGMA_OMP_CLAUSE_REDUCTION:
28277 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28278 c_name = "reduction";
28279 break;
28280 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28281 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28282 token->location);
28283 c_name = "schedule";
28284 break;
28285 case PRAGMA_OMP_CLAUSE_SHARED:
28286 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28287 clauses);
28288 c_name = "shared";
28289 break;
28290 case PRAGMA_OMP_CLAUSE_UNTIED:
28291 clauses = cp_parser_omp_clause_untied (parser, clauses,
28292 token->location);
28293 c_name = "untied";
28294 break;
28295 case PRAGMA_OMP_CLAUSE_INBRANCH:
28296 case PRAGMA_CILK_CLAUSE_MASK:
28297 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28298 clauses, token->location);
28299 c_name = "inbranch";
28300 break;
28301 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28302 case PRAGMA_CILK_CLAUSE_NOMASK:
28303 clauses = cp_parser_omp_clause_branch (parser,
28304 OMP_CLAUSE_NOTINBRANCH,
28305 clauses, token->location);
28306 c_name = "notinbranch";
28307 break;
28308 case PRAGMA_OMP_CLAUSE_PARALLEL:
28309 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28310 clauses, token->location);
28311 c_name = "parallel";
28312 if (!first)
28313 {
28314 clause_not_first:
28315 error_at (token->location, "%qs must be the first clause of %qs",
28316 c_name, where);
28317 clauses = prev;
28318 }
28319 break;
28320 case PRAGMA_OMP_CLAUSE_FOR:
28321 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28322 clauses, token->location);
28323 c_name = "for";
28324 if (!first)
28325 goto clause_not_first;
28326 break;
28327 case PRAGMA_OMP_CLAUSE_SECTIONS:
28328 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28329 clauses, token->location);
28330 c_name = "sections";
28331 if (!first)
28332 goto clause_not_first;
28333 break;
28334 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28335 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28336 clauses, token->location);
28337 c_name = "taskgroup";
28338 if (!first)
28339 goto clause_not_first;
28340 break;
28341 case PRAGMA_OMP_CLAUSE_TO:
28342 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28343 clauses);
28344 c_name = "to";
28345 break;
28346 case PRAGMA_OMP_CLAUSE_FROM:
28347 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28348 clauses);
28349 c_name = "from";
28350 break;
28351 case PRAGMA_OMP_CLAUSE_UNIFORM:
28352 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28353 clauses);
28354 c_name = "uniform";
28355 break;
28356 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28357 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28358 token->location);
28359 c_name = "num_teams";
28360 break;
28361 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28362 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28363 token->location);
28364 c_name = "thread_limit";
28365 break;
28366 case PRAGMA_OMP_CLAUSE_ALIGNED:
28367 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28368 c_name = "aligned";
28369 break;
28370 case PRAGMA_OMP_CLAUSE_LINEAR:
28371 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28372 cilk_simd_fn = true;
28373 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28374 c_name = "linear";
28375 break;
28376 case PRAGMA_OMP_CLAUSE_DEPEND:
28377 clauses = cp_parser_omp_clause_depend (parser, clauses);
28378 c_name = "depend";
28379 break;
28380 case PRAGMA_OMP_CLAUSE_MAP:
28381 clauses = cp_parser_omp_clause_map (parser, clauses);
28382 c_name = "map";
28383 break;
28384 case PRAGMA_OMP_CLAUSE_DEVICE:
28385 clauses = cp_parser_omp_clause_device (parser, clauses,
28386 token->location);
28387 c_name = "device";
28388 break;
28389 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28390 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28391 token->location);
28392 c_name = "dist_schedule";
28393 break;
28394 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28395 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28396 token->location);
28397 c_name = "proc_bind";
28398 break;
28399 case PRAGMA_OMP_CLAUSE_SAFELEN:
28400 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28401 token->location);
28402 c_name = "safelen";
28403 break;
28404 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28405 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28406 token->location);
28407 c_name = "simdlen";
28408 break;
28409 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28410 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28411 c_name = "simdlen";
28412 break;
28413 default:
28414 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28415 goto saw_error;
28416 }
28417
28418 first = false;
28419
28420 if (((mask >> c_kind) & 1) == 0)
28421 {
28422 /* Remove the invalid clause(s) from the list to avoid
28423 confusing the rest of the compiler. */
28424 clauses = prev;
28425 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28426 }
28427 }
28428 saw_error:
28429 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28430 no reason to skip to the end. */
28431 if (!(flag_cilkplus && pragma_tok == NULL))
28432 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28433 if (finish_p)
28434 return finish_omp_clauses (clauses);
28435 return clauses;
28436 }
28437
28438 /* OpenMP 2.5:
28439 structured-block:
28440 statement
28441
28442 In practice, we're also interested in adding the statement to an
28443 outer node. So it is convenient if we work around the fact that
28444 cp_parser_statement calls add_stmt. */
28445
28446 static unsigned
28447 cp_parser_begin_omp_structured_block (cp_parser *parser)
28448 {
28449 unsigned save = parser->in_statement;
28450
28451 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28452 This preserves the "not within loop or switch" style error messages
28453 for nonsense cases like
28454 void foo() {
28455 #pragma omp single
28456 break;
28457 }
28458 */
28459 if (parser->in_statement)
28460 parser->in_statement = IN_OMP_BLOCK;
28461
28462 return save;
28463 }
28464
28465 static void
28466 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28467 {
28468 parser->in_statement = save;
28469 }
28470
28471 static tree
28472 cp_parser_omp_structured_block (cp_parser *parser)
28473 {
28474 tree stmt = begin_omp_structured_block ();
28475 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28476
28477 cp_parser_statement (parser, NULL_TREE, false, NULL);
28478
28479 cp_parser_end_omp_structured_block (parser, save);
28480 return finish_omp_structured_block (stmt);
28481 }
28482
28483 /* OpenMP 2.5:
28484 # pragma omp atomic new-line
28485 expression-stmt
28486
28487 expression-stmt:
28488 x binop= expr | x++ | ++x | x-- | --x
28489 binop:
28490 +, *, -, /, &, ^, |, <<, >>
28491
28492 where x is an lvalue expression with scalar type.
28493
28494 OpenMP 3.1:
28495 # pragma omp atomic new-line
28496 update-stmt
28497
28498 # pragma omp atomic read new-line
28499 read-stmt
28500
28501 # pragma omp atomic write new-line
28502 write-stmt
28503
28504 # pragma omp atomic update new-line
28505 update-stmt
28506
28507 # pragma omp atomic capture new-line
28508 capture-stmt
28509
28510 # pragma omp atomic capture new-line
28511 capture-block
28512
28513 read-stmt:
28514 v = x
28515 write-stmt:
28516 x = expr
28517 update-stmt:
28518 expression-stmt | x = x binop expr
28519 capture-stmt:
28520 v = expression-stmt
28521 capture-block:
28522 { v = x; update-stmt; } | { update-stmt; v = x; }
28523
28524 OpenMP 4.0:
28525 update-stmt:
28526 expression-stmt | x = x binop expr | x = expr binop x
28527 capture-stmt:
28528 v = update-stmt
28529 capture-block:
28530 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28531
28532 where x and v are lvalue expressions with scalar type. */
28533
28534 static void
28535 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28536 {
28537 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28538 tree rhs1 = NULL_TREE, orig_lhs;
28539 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28540 bool structured_block = false;
28541 bool seq_cst = false;
28542
28543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28544 {
28545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28546 const char *p = IDENTIFIER_POINTER (id);
28547
28548 if (!strcmp (p, "seq_cst"))
28549 {
28550 seq_cst = true;
28551 cp_lexer_consume_token (parser->lexer);
28552 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28553 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28554 cp_lexer_consume_token (parser->lexer);
28555 }
28556 }
28557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28558 {
28559 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28560 const char *p = IDENTIFIER_POINTER (id);
28561
28562 if (!strcmp (p, "read"))
28563 code = OMP_ATOMIC_READ;
28564 else if (!strcmp (p, "write"))
28565 code = NOP_EXPR;
28566 else if (!strcmp (p, "update"))
28567 code = OMP_ATOMIC;
28568 else if (!strcmp (p, "capture"))
28569 code = OMP_ATOMIC_CAPTURE_NEW;
28570 else
28571 p = NULL;
28572 if (p)
28573 cp_lexer_consume_token (parser->lexer);
28574 }
28575 if (!seq_cst)
28576 {
28577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28578 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28579 cp_lexer_consume_token (parser->lexer);
28580
28581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28582 {
28583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28584 const char *p = IDENTIFIER_POINTER (id);
28585
28586 if (!strcmp (p, "seq_cst"))
28587 {
28588 seq_cst = true;
28589 cp_lexer_consume_token (parser->lexer);
28590 }
28591 }
28592 }
28593 cp_parser_require_pragma_eol (parser, pragma_tok);
28594
28595 switch (code)
28596 {
28597 case OMP_ATOMIC_READ:
28598 case NOP_EXPR: /* atomic write */
28599 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28600 /*cast_p=*/false, NULL);
28601 if (v == error_mark_node)
28602 goto saw_error;
28603 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28604 goto saw_error;
28605 if (code == NOP_EXPR)
28606 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28607 else
28608 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28609 /*cast_p=*/false, NULL);
28610 if (lhs == error_mark_node)
28611 goto saw_error;
28612 if (code == NOP_EXPR)
28613 {
28614 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28615 opcode. */
28616 code = OMP_ATOMIC;
28617 rhs = lhs;
28618 lhs = v;
28619 v = NULL_TREE;
28620 }
28621 goto done;
28622 case OMP_ATOMIC_CAPTURE_NEW:
28623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28624 {
28625 cp_lexer_consume_token (parser->lexer);
28626 structured_block = true;
28627 }
28628 else
28629 {
28630 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28631 /*cast_p=*/false, NULL);
28632 if (v == error_mark_node)
28633 goto saw_error;
28634 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28635 goto saw_error;
28636 }
28637 default:
28638 break;
28639 }
28640
28641 restart:
28642 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28643 /*cast_p=*/false, NULL);
28644 orig_lhs = lhs;
28645 switch (TREE_CODE (lhs))
28646 {
28647 case ERROR_MARK:
28648 goto saw_error;
28649
28650 case POSTINCREMENT_EXPR:
28651 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28652 code = OMP_ATOMIC_CAPTURE_OLD;
28653 /* FALLTHROUGH */
28654 case PREINCREMENT_EXPR:
28655 lhs = TREE_OPERAND (lhs, 0);
28656 opcode = PLUS_EXPR;
28657 rhs = integer_one_node;
28658 break;
28659
28660 case POSTDECREMENT_EXPR:
28661 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28662 code = OMP_ATOMIC_CAPTURE_OLD;
28663 /* FALLTHROUGH */
28664 case PREDECREMENT_EXPR:
28665 lhs = TREE_OPERAND (lhs, 0);
28666 opcode = MINUS_EXPR;
28667 rhs = integer_one_node;
28668 break;
28669
28670 case COMPOUND_EXPR:
28671 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28672 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28673 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28674 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28675 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28676 (TREE_OPERAND (lhs, 1), 0), 0)))
28677 == BOOLEAN_TYPE)
28678 /* Undo effects of boolean_increment for post {in,de}crement. */
28679 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28680 /* FALLTHRU */
28681 case MODIFY_EXPR:
28682 if (TREE_CODE (lhs) == MODIFY_EXPR
28683 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28684 {
28685 /* Undo effects of boolean_increment. */
28686 if (integer_onep (TREE_OPERAND (lhs, 1)))
28687 {
28688 /* This is pre or post increment. */
28689 rhs = TREE_OPERAND (lhs, 1);
28690 lhs = TREE_OPERAND (lhs, 0);
28691 opcode = NOP_EXPR;
28692 if (code == OMP_ATOMIC_CAPTURE_NEW
28693 && !structured_block
28694 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28695 code = OMP_ATOMIC_CAPTURE_OLD;
28696 break;
28697 }
28698 }
28699 /* FALLTHRU */
28700 default:
28701 switch (cp_lexer_peek_token (parser->lexer)->type)
28702 {
28703 case CPP_MULT_EQ:
28704 opcode = MULT_EXPR;
28705 break;
28706 case CPP_DIV_EQ:
28707 opcode = TRUNC_DIV_EXPR;
28708 break;
28709 case CPP_PLUS_EQ:
28710 opcode = PLUS_EXPR;
28711 break;
28712 case CPP_MINUS_EQ:
28713 opcode = MINUS_EXPR;
28714 break;
28715 case CPP_LSHIFT_EQ:
28716 opcode = LSHIFT_EXPR;
28717 break;
28718 case CPP_RSHIFT_EQ:
28719 opcode = RSHIFT_EXPR;
28720 break;
28721 case CPP_AND_EQ:
28722 opcode = BIT_AND_EXPR;
28723 break;
28724 case CPP_OR_EQ:
28725 opcode = BIT_IOR_EXPR;
28726 break;
28727 case CPP_XOR_EQ:
28728 opcode = BIT_XOR_EXPR;
28729 break;
28730 case CPP_EQ:
28731 enum cp_parser_prec oprec;
28732 cp_token *token;
28733 cp_lexer_consume_token (parser->lexer);
28734 cp_parser_parse_tentatively (parser);
28735 rhs1 = cp_parser_simple_cast_expression (parser);
28736 if (rhs1 == error_mark_node)
28737 {
28738 cp_parser_abort_tentative_parse (parser);
28739 cp_parser_simple_cast_expression (parser);
28740 goto saw_error;
28741 }
28742 token = cp_lexer_peek_token (parser->lexer);
28743 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28744 {
28745 cp_parser_abort_tentative_parse (parser);
28746 cp_parser_parse_tentatively (parser);
28747 rhs = cp_parser_binary_expression (parser, false, true,
28748 PREC_NOT_OPERATOR, NULL);
28749 if (rhs == error_mark_node)
28750 {
28751 cp_parser_abort_tentative_parse (parser);
28752 cp_parser_binary_expression (parser, false, true,
28753 PREC_NOT_OPERATOR, NULL);
28754 goto saw_error;
28755 }
28756 switch (TREE_CODE (rhs))
28757 {
28758 case MULT_EXPR:
28759 case TRUNC_DIV_EXPR:
28760 case PLUS_EXPR:
28761 case MINUS_EXPR:
28762 case LSHIFT_EXPR:
28763 case RSHIFT_EXPR:
28764 case BIT_AND_EXPR:
28765 case BIT_IOR_EXPR:
28766 case BIT_XOR_EXPR:
28767 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28768 {
28769 if (cp_parser_parse_definitely (parser))
28770 {
28771 opcode = TREE_CODE (rhs);
28772 rhs1 = TREE_OPERAND (rhs, 0);
28773 rhs = TREE_OPERAND (rhs, 1);
28774 goto stmt_done;
28775 }
28776 else
28777 goto saw_error;
28778 }
28779 break;
28780 default:
28781 break;
28782 }
28783 cp_parser_abort_tentative_parse (parser);
28784 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28785 {
28786 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28787 if (rhs == error_mark_node)
28788 goto saw_error;
28789 opcode = NOP_EXPR;
28790 rhs1 = NULL_TREE;
28791 goto stmt_done;
28792 }
28793 cp_parser_error (parser,
28794 "invalid form of %<#pragma omp atomic%>");
28795 goto saw_error;
28796 }
28797 if (!cp_parser_parse_definitely (parser))
28798 goto saw_error;
28799 switch (token->type)
28800 {
28801 case CPP_SEMICOLON:
28802 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28803 {
28804 code = OMP_ATOMIC_CAPTURE_OLD;
28805 v = lhs;
28806 lhs = NULL_TREE;
28807 lhs1 = rhs1;
28808 rhs1 = NULL_TREE;
28809 cp_lexer_consume_token (parser->lexer);
28810 goto restart;
28811 }
28812 else if (structured_block)
28813 {
28814 opcode = NOP_EXPR;
28815 rhs = rhs1;
28816 rhs1 = NULL_TREE;
28817 goto stmt_done;
28818 }
28819 cp_parser_error (parser,
28820 "invalid form of %<#pragma omp atomic%>");
28821 goto saw_error;
28822 case CPP_MULT:
28823 opcode = MULT_EXPR;
28824 break;
28825 case CPP_DIV:
28826 opcode = TRUNC_DIV_EXPR;
28827 break;
28828 case CPP_PLUS:
28829 opcode = PLUS_EXPR;
28830 break;
28831 case CPP_MINUS:
28832 opcode = MINUS_EXPR;
28833 break;
28834 case CPP_LSHIFT:
28835 opcode = LSHIFT_EXPR;
28836 break;
28837 case CPP_RSHIFT:
28838 opcode = RSHIFT_EXPR;
28839 break;
28840 case CPP_AND:
28841 opcode = BIT_AND_EXPR;
28842 break;
28843 case CPP_OR:
28844 opcode = BIT_IOR_EXPR;
28845 break;
28846 case CPP_XOR:
28847 opcode = BIT_XOR_EXPR;
28848 break;
28849 default:
28850 cp_parser_error (parser,
28851 "invalid operator for %<#pragma omp atomic%>");
28852 goto saw_error;
28853 }
28854 oprec = TOKEN_PRECEDENCE (token);
28855 gcc_assert (oprec != PREC_NOT_OPERATOR);
28856 if (commutative_tree_code (opcode))
28857 oprec = (enum cp_parser_prec) (oprec - 1);
28858 cp_lexer_consume_token (parser->lexer);
28859 rhs = cp_parser_binary_expression (parser, false, false,
28860 oprec, NULL);
28861 if (rhs == error_mark_node)
28862 goto saw_error;
28863 goto stmt_done;
28864 /* FALLTHROUGH */
28865 default:
28866 cp_parser_error (parser,
28867 "invalid operator for %<#pragma omp atomic%>");
28868 goto saw_error;
28869 }
28870 cp_lexer_consume_token (parser->lexer);
28871
28872 rhs = cp_parser_expression (parser, false, NULL);
28873 if (rhs == error_mark_node)
28874 goto saw_error;
28875 break;
28876 }
28877 stmt_done:
28878 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28879 {
28880 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28881 goto saw_error;
28882 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28883 /*cast_p=*/false, NULL);
28884 if (v == error_mark_node)
28885 goto saw_error;
28886 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28887 goto saw_error;
28888 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28889 /*cast_p=*/false, NULL);
28890 if (lhs1 == error_mark_node)
28891 goto saw_error;
28892 }
28893 if (structured_block)
28894 {
28895 cp_parser_consume_semicolon_at_end_of_statement (parser);
28896 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28897 }
28898 done:
28899 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28900 if (!structured_block)
28901 cp_parser_consume_semicolon_at_end_of_statement (parser);
28902 return;
28903
28904 saw_error:
28905 cp_parser_skip_to_end_of_block_or_statement (parser);
28906 if (structured_block)
28907 {
28908 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28909 cp_lexer_consume_token (parser->lexer);
28910 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28911 {
28912 cp_parser_skip_to_end_of_block_or_statement (parser);
28913 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28914 cp_lexer_consume_token (parser->lexer);
28915 }
28916 }
28917 }
28918
28919
28920 /* OpenMP 2.5:
28921 # pragma omp barrier new-line */
28922
28923 static void
28924 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28925 {
28926 cp_parser_require_pragma_eol (parser, pragma_tok);
28927 finish_omp_barrier ();
28928 }
28929
28930 /* OpenMP 2.5:
28931 # pragma omp critical [(name)] new-line
28932 structured-block */
28933
28934 static tree
28935 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28936 {
28937 tree stmt, name = NULL;
28938
28939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28940 {
28941 cp_lexer_consume_token (parser->lexer);
28942
28943 name = cp_parser_identifier (parser);
28944
28945 if (name == error_mark_node
28946 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28948 /*or_comma=*/false,
28949 /*consume_paren=*/true);
28950 if (name == error_mark_node)
28951 name = NULL;
28952 }
28953 cp_parser_require_pragma_eol (parser, pragma_tok);
28954
28955 stmt = cp_parser_omp_structured_block (parser);
28956 return c_finish_omp_critical (input_location, stmt, name);
28957 }
28958
28959 /* OpenMP 2.5:
28960 # pragma omp flush flush-vars[opt] new-line
28961
28962 flush-vars:
28963 ( variable-list ) */
28964
28965 static void
28966 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28967 {
28968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28969 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28970 cp_parser_require_pragma_eol (parser, pragma_tok);
28971
28972 finish_omp_flush ();
28973 }
28974
28975 /* Helper function, to parse omp for increment expression. */
28976
28977 static tree
28978 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28979 {
28980 tree cond = cp_parser_binary_expression (parser, false, true,
28981 PREC_NOT_OPERATOR, NULL);
28982 if (cond == error_mark_node
28983 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28984 {
28985 cp_parser_skip_to_end_of_statement (parser);
28986 return error_mark_node;
28987 }
28988
28989 switch (TREE_CODE (cond))
28990 {
28991 case GT_EXPR:
28992 case GE_EXPR:
28993 case LT_EXPR:
28994 case LE_EXPR:
28995 break;
28996 case NE_EXPR:
28997 if (code == CILK_SIMD)
28998 break;
28999 /* Fall through: OpenMP disallows NE_EXPR. */
29000 default:
29001 return error_mark_node;
29002 }
29003
29004 /* If decl is an iterator, preserve LHS and RHS of the relational
29005 expr until finish_omp_for. */
29006 if (decl
29007 && (type_dependent_expression_p (decl)
29008 || CLASS_TYPE_P (TREE_TYPE (decl))))
29009 return cond;
29010
29011 return build_x_binary_op (input_location, TREE_CODE (cond),
29012 TREE_OPERAND (cond, 0), ERROR_MARK,
29013 TREE_OPERAND (cond, 1), ERROR_MARK,
29014 /*overload=*/NULL, tf_warning_or_error);
29015 }
29016
29017 /* Helper function, to parse omp for increment expression. */
29018
29019 static tree
29020 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29021 {
29022 cp_token *token = cp_lexer_peek_token (parser->lexer);
29023 enum tree_code op;
29024 tree lhs, rhs;
29025 cp_id_kind idk;
29026 bool decl_first;
29027
29028 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29029 {
29030 op = (token->type == CPP_PLUS_PLUS
29031 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29032 cp_lexer_consume_token (parser->lexer);
29033 lhs = cp_parser_simple_cast_expression (parser);
29034 if (lhs != decl)
29035 return error_mark_node;
29036 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29037 }
29038
29039 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29040 if (lhs != decl)
29041 return error_mark_node;
29042
29043 token = cp_lexer_peek_token (parser->lexer);
29044 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29045 {
29046 op = (token->type == CPP_PLUS_PLUS
29047 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29048 cp_lexer_consume_token (parser->lexer);
29049 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29050 }
29051
29052 op = cp_parser_assignment_operator_opt (parser);
29053 if (op == ERROR_MARK)
29054 return error_mark_node;
29055
29056 if (op != NOP_EXPR)
29057 {
29058 rhs = cp_parser_assignment_expression (parser, false, NULL);
29059 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29060 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29061 }
29062
29063 lhs = cp_parser_binary_expression (parser, false, false,
29064 PREC_ADDITIVE_EXPRESSION, NULL);
29065 token = cp_lexer_peek_token (parser->lexer);
29066 decl_first = lhs == decl;
29067 if (decl_first)
29068 lhs = NULL_TREE;
29069 if (token->type != CPP_PLUS
29070 && token->type != CPP_MINUS)
29071 return error_mark_node;
29072
29073 do
29074 {
29075 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29076 cp_lexer_consume_token (parser->lexer);
29077 rhs = cp_parser_binary_expression (parser, false, false,
29078 PREC_ADDITIVE_EXPRESSION, NULL);
29079 token = cp_lexer_peek_token (parser->lexer);
29080 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29081 {
29082 if (lhs == NULL_TREE)
29083 {
29084 if (op == PLUS_EXPR)
29085 lhs = rhs;
29086 else
29087 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29088 tf_warning_or_error);
29089 }
29090 else
29091 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29092 ERROR_MARK, NULL, tf_warning_or_error);
29093 }
29094 }
29095 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29096
29097 if (!decl_first)
29098 {
29099 if (rhs != decl || op == MINUS_EXPR)
29100 return error_mark_node;
29101 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29102 }
29103 else
29104 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29105
29106 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29107 }
29108
29109 /* Parse the initialization statement of either an OpenMP for loop or
29110 a Cilk Plus for loop.
29111
29112 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29113 Plus.
29114
29115 Return true if the resulting construct should have an
29116 OMP_CLAUSE_PRIVATE added to it. */
29117
29118 static bool
29119 cp_parser_omp_for_loop_init (cp_parser *parser,
29120 bool parsing_openmp,
29121 tree &this_pre_body,
29122 vec<tree, va_gc> *for_block,
29123 tree &init,
29124 tree &decl,
29125 tree &real_decl)
29126 {
29127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29128 return false;
29129
29130 bool add_private_clause = false;
29131
29132 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29133
29134 init-expr:
29135 var = lb
29136 integer-type var = lb
29137 random-access-iterator-type var = lb
29138 pointer-type var = lb
29139 */
29140 cp_decl_specifier_seq type_specifiers;
29141
29142 /* First, try to parse as an initialized declaration. See
29143 cp_parser_condition, from whence the bulk of this is copied. */
29144
29145 cp_parser_parse_tentatively (parser);
29146 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29147 /*is_trailing_return=*/false,
29148 &type_specifiers);
29149 if (cp_parser_parse_definitely (parser))
29150 {
29151 /* If parsing a type specifier seq succeeded, then this
29152 MUST be a initialized declaration. */
29153 tree asm_specification, attributes;
29154 cp_declarator *declarator;
29155
29156 declarator = cp_parser_declarator (parser,
29157 CP_PARSER_DECLARATOR_NAMED,
29158 /*ctor_dtor_or_conv_p=*/NULL,
29159 /*parenthesized_p=*/NULL,
29160 /*member_p=*/false);
29161 attributes = cp_parser_attributes_opt (parser);
29162 asm_specification = cp_parser_asm_specification_opt (parser);
29163
29164 if (declarator == cp_error_declarator)
29165 cp_parser_skip_to_end_of_statement (parser);
29166
29167 else
29168 {
29169 tree pushed_scope, auto_node;
29170
29171 decl = start_decl (declarator, &type_specifiers,
29172 SD_INITIALIZED, attributes,
29173 /*prefix_attributes=*/NULL_TREE,
29174 &pushed_scope);
29175
29176 auto_node = type_uses_auto (TREE_TYPE (decl));
29177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29178 {
29179 if (cp_lexer_next_token_is (parser->lexer,
29180 CPP_OPEN_PAREN))
29181 {
29182 if (parsing_openmp)
29183 error ("parenthesized initialization is not allowed in "
29184 "OpenMP %<for%> loop");
29185 else
29186 error ("parenthesized initialization is "
29187 "not allowed in for-loop");
29188 }
29189 else
29190 /* Trigger an error. */
29191 cp_parser_require (parser, CPP_EQ, RT_EQ);
29192
29193 init = error_mark_node;
29194 cp_parser_skip_to_end_of_statement (parser);
29195 }
29196 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29197 || type_dependent_expression_p (decl)
29198 || auto_node)
29199 {
29200 bool is_direct_init, is_non_constant_init;
29201
29202 init = cp_parser_initializer (parser,
29203 &is_direct_init,
29204 &is_non_constant_init);
29205
29206 if (auto_node)
29207 {
29208 TREE_TYPE (decl)
29209 = do_auto_deduction (TREE_TYPE (decl), init,
29210 auto_node);
29211
29212 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29213 && !type_dependent_expression_p (decl))
29214 goto non_class;
29215 }
29216
29217 cp_finish_decl (decl, init, !is_non_constant_init,
29218 asm_specification,
29219 LOOKUP_ONLYCONVERTING);
29220 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29221 {
29222 vec_safe_push (for_block, this_pre_body);
29223 init = NULL_TREE;
29224 }
29225 else
29226 init = pop_stmt_list (this_pre_body);
29227 this_pre_body = NULL_TREE;
29228 }
29229 else
29230 {
29231 /* Consume '='. */
29232 cp_lexer_consume_token (parser->lexer);
29233 init = cp_parser_assignment_expression (parser, false, NULL);
29234
29235 non_class:
29236 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29237 init = error_mark_node;
29238 else
29239 cp_finish_decl (decl, NULL_TREE,
29240 /*init_const_expr_p=*/false,
29241 asm_specification,
29242 LOOKUP_ONLYCONVERTING);
29243 }
29244
29245 if (pushed_scope)
29246 pop_scope (pushed_scope);
29247 }
29248 }
29249 else
29250 {
29251 cp_id_kind idk;
29252 /* If parsing a type specifier sequence failed, then
29253 this MUST be a simple expression. */
29254 cp_parser_parse_tentatively (parser);
29255 decl = cp_parser_primary_expression (parser, false, false,
29256 false, &idk);
29257 if (!cp_parser_error_occurred (parser)
29258 && decl
29259 && DECL_P (decl)
29260 && CLASS_TYPE_P (TREE_TYPE (decl)))
29261 {
29262 tree rhs;
29263
29264 cp_parser_parse_definitely (parser);
29265 cp_parser_require (parser, CPP_EQ, RT_EQ);
29266 rhs = cp_parser_assignment_expression (parser, false, NULL);
29267 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29268 decl, NOP_EXPR,
29269 rhs,
29270 tf_warning_or_error));
29271 add_private_clause = true;
29272 }
29273 else
29274 {
29275 decl = NULL;
29276 cp_parser_abort_tentative_parse (parser);
29277 init = cp_parser_expression (parser, false, NULL);
29278 if (init)
29279 {
29280 if (TREE_CODE (init) == MODIFY_EXPR
29281 || TREE_CODE (init) == MODOP_EXPR)
29282 real_decl = TREE_OPERAND (init, 0);
29283 }
29284 }
29285 }
29286 return add_private_clause;
29287 }
29288
29289 /* Parse the restricted form of the for statement allowed by OpenMP. */
29290
29291 static tree
29292 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29293 tree *cclauses)
29294 {
29295 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29296 tree real_decl, initv, condv, incrv, declv;
29297 tree this_pre_body, cl;
29298 location_t loc_first;
29299 bool collapse_err = false;
29300 int i, collapse = 1, nbraces = 0;
29301 vec<tree, va_gc> *for_block = make_tree_vector ();
29302
29303 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29304 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29305 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29306
29307 gcc_assert (collapse >= 1);
29308
29309 declv = make_tree_vec (collapse);
29310 initv = make_tree_vec (collapse);
29311 condv = make_tree_vec (collapse);
29312 incrv = make_tree_vec (collapse);
29313
29314 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29315
29316 for (i = 0; i < collapse; i++)
29317 {
29318 int bracecount = 0;
29319 bool add_private_clause = false;
29320 location_t loc;
29321
29322 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29323 {
29324 cp_parser_error (parser, "for statement expected");
29325 return NULL;
29326 }
29327 loc = cp_lexer_consume_token (parser->lexer)->location;
29328
29329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29330 return NULL;
29331
29332 init = decl = real_decl = NULL;
29333 this_pre_body = push_stmt_list ();
29334
29335 add_private_clause
29336 |= cp_parser_omp_for_loop_init (parser,
29337 /*parsing_openmp=*/code != CILK_SIMD,
29338 this_pre_body, for_block,
29339 init, decl, real_decl);
29340
29341 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29342 if (this_pre_body)
29343 {
29344 this_pre_body = pop_stmt_list (this_pre_body);
29345 if (pre_body)
29346 {
29347 tree t = pre_body;
29348 pre_body = push_stmt_list ();
29349 add_stmt (t);
29350 add_stmt (this_pre_body);
29351 pre_body = pop_stmt_list (pre_body);
29352 }
29353 else
29354 pre_body = this_pre_body;
29355 }
29356
29357 if (decl)
29358 real_decl = decl;
29359 if (cclauses != NULL
29360 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29361 && real_decl != NULL_TREE)
29362 {
29363 tree *c;
29364 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29365 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29366 && OMP_CLAUSE_DECL (*c) == real_decl)
29367 {
29368 error_at (loc, "iteration variable %qD"
29369 " should not be firstprivate", real_decl);
29370 *c = OMP_CLAUSE_CHAIN (*c);
29371 }
29372 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29373 && OMP_CLAUSE_DECL (*c) == real_decl)
29374 {
29375 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29376 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29377 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29378 OMP_CLAUSE_DECL (l) = real_decl;
29379 OMP_CLAUSE_CHAIN (l) = clauses;
29380 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29381 clauses = l;
29382 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29383 CP_OMP_CLAUSE_INFO (*c) = NULL;
29384 add_private_clause = false;
29385 }
29386 else
29387 {
29388 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29389 && OMP_CLAUSE_DECL (*c) == real_decl)
29390 add_private_clause = false;
29391 c = &OMP_CLAUSE_CHAIN (*c);
29392 }
29393 }
29394
29395 if (add_private_clause)
29396 {
29397 tree c;
29398 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29399 {
29400 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29401 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29402 && OMP_CLAUSE_DECL (c) == decl)
29403 break;
29404 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29405 && OMP_CLAUSE_DECL (c) == decl)
29406 error_at (loc, "iteration variable %qD "
29407 "should not be firstprivate",
29408 decl);
29409 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29410 && OMP_CLAUSE_DECL (c) == decl)
29411 error_at (loc, "iteration variable %qD should not be reduction",
29412 decl);
29413 }
29414 if (c == NULL)
29415 {
29416 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29417 OMP_CLAUSE_DECL (c) = decl;
29418 c = finish_omp_clauses (c);
29419 if (c)
29420 {
29421 OMP_CLAUSE_CHAIN (c) = clauses;
29422 clauses = c;
29423 }
29424 }
29425 }
29426
29427 cond = NULL;
29428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29429 cond = cp_parser_omp_for_cond (parser, decl, code);
29430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29431
29432 incr = NULL;
29433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29434 {
29435 /* If decl is an iterator, preserve the operator on decl
29436 until finish_omp_for. */
29437 if (real_decl
29438 && ((processing_template_decl
29439 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29440 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29441 incr = cp_parser_omp_for_incr (parser, real_decl);
29442 else
29443 incr = cp_parser_expression (parser, false, NULL);
29444 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29445 SET_EXPR_LOCATION (incr, input_location);
29446 }
29447
29448 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29450 /*or_comma=*/false,
29451 /*consume_paren=*/true);
29452
29453 TREE_VEC_ELT (declv, i) = decl;
29454 TREE_VEC_ELT (initv, i) = init;
29455 TREE_VEC_ELT (condv, i) = cond;
29456 TREE_VEC_ELT (incrv, i) = incr;
29457
29458 if (i == collapse - 1)
29459 break;
29460
29461 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29462 in between the collapsed for loops to be still considered perfectly
29463 nested. Hopefully the final version clarifies this.
29464 For now handle (multiple) {'s and empty statements. */
29465 cp_parser_parse_tentatively (parser);
29466 do
29467 {
29468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29469 break;
29470 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29471 {
29472 cp_lexer_consume_token (parser->lexer);
29473 bracecount++;
29474 }
29475 else if (bracecount
29476 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29477 cp_lexer_consume_token (parser->lexer);
29478 else
29479 {
29480 loc = cp_lexer_peek_token (parser->lexer)->location;
29481 error_at (loc, "not enough collapsed for loops");
29482 collapse_err = true;
29483 cp_parser_abort_tentative_parse (parser);
29484 declv = NULL_TREE;
29485 break;
29486 }
29487 }
29488 while (1);
29489
29490 if (declv)
29491 {
29492 cp_parser_parse_definitely (parser);
29493 nbraces += bracecount;
29494 }
29495 }
29496
29497 /* Note that we saved the original contents of this flag when we entered
29498 the structured block, and so we don't need to re-save it here. */
29499 if (code == CILK_SIMD)
29500 parser->in_statement = IN_CILK_SIMD_FOR;
29501 else
29502 parser->in_statement = IN_OMP_FOR;
29503
29504 /* Note that the grammar doesn't call for a structured block here,
29505 though the loop as a whole is a structured block. */
29506 body = push_stmt_list ();
29507 cp_parser_statement (parser, NULL_TREE, false, NULL);
29508 body = pop_stmt_list (body);
29509
29510 if (declv == NULL_TREE)
29511 ret = NULL_TREE;
29512 else
29513 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29514 pre_body, clauses);
29515
29516 while (nbraces)
29517 {
29518 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29519 {
29520 cp_lexer_consume_token (parser->lexer);
29521 nbraces--;
29522 }
29523 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29524 cp_lexer_consume_token (parser->lexer);
29525 else
29526 {
29527 if (!collapse_err)
29528 {
29529 error_at (cp_lexer_peek_token (parser->lexer)->location,
29530 "collapsed loops not perfectly nested");
29531 }
29532 collapse_err = true;
29533 cp_parser_statement_seq_opt (parser, NULL);
29534 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29535 break;
29536 }
29537 }
29538
29539 while (!for_block->is_empty ())
29540 add_stmt (pop_stmt_list (for_block->pop ()));
29541 release_tree_vector (for_block);
29542
29543 return ret;
29544 }
29545
29546 /* Helper function for OpenMP parsing, split clauses and call
29547 finish_omp_clauses on each of the set of clauses afterwards. */
29548
29549 static void
29550 cp_omp_split_clauses (location_t loc, enum tree_code code,
29551 omp_clause_mask mask, tree clauses, tree *cclauses)
29552 {
29553 int i;
29554 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29555 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29556 if (cclauses[i])
29557 cclauses[i] = finish_omp_clauses (cclauses[i]);
29558 }
29559
29560 /* OpenMP 4.0:
29561 #pragma omp simd simd-clause[optseq] new-line
29562 for-loop */
29563
29564 #define OMP_SIMD_CLAUSE_MASK \
29565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29572
29573 static tree
29574 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29575 char *p_name, omp_clause_mask mask, tree *cclauses)
29576 {
29577 tree clauses, sb, ret;
29578 unsigned int save;
29579 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29580
29581 strcat (p_name, " simd");
29582 mask |= OMP_SIMD_CLAUSE_MASK;
29583 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29584
29585 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29586 cclauses == NULL);
29587 if (cclauses)
29588 {
29589 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29590 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29591 }
29592
29593 sb = begin_omp_structured_block ();
29594 save = cp_parser_begin_omp_structured_block (parser);
29595
29596 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29597
29598 cp_parser_end_omp_structured_block (parser, save);
29599 add_stmt (finish_omp_structured_block (sb));
29600
29601 return ret;
29602 }
29603
29604 /* OpenMP 2.5:
29605 #pragma omp for for-clause[optseq] new-line
29606 for-loop
29607
29608 OpenMP 4.0:
29609 #pragma omp for simd for-simd-clause[optseq] new-line
29610 for-loop */
29611
29612 #define OMP_FOR_CLAUSE_MASK \
29613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29621
29622 static tree
29623 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29624 char *p_name, omp_clause_mask mask, tree *cclauses)
29625 {
29626 tree clauses, sb, ret;
29627 unsigned int save;
29628 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29629
29630 strcat (p_name, " for");
29631 mask |= OMP_FOR_CLAUSE_MASK;
29632 if (cclauses)
29633 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29634
29635 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29636 {
29637 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29638 const char *p = IDENTIFIER_POINTER (id);
29639
29640 if (strcmp (p, "simd") == 0)
29641 {
29642 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29643 if (cclauses == NULL)
29644 cclauses = cclauses_buf;
29645
29646 cp_lexer_consume_token (parser->lexer);
29647 if (!flag_openmp) /* flag_openmp_simd */
29648 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29649 cclauses);
29650 sb = begin_omp_structured_block ();
29651 save = cp_parser_begin_omp_structured_block (parser);
29652 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29653 cclauses);
29654 cp_parser_end_omp_structured_block (parser, save);
29655 tree body = finish_omp_structured_block (sb);
29656 if (ret == NULL)
29657 return ret;
29658 ret = make_node (OMP_FOR);
29659 TREE_TYPE (ret) = void_type_node;
29660 OMP_FOR_BODY (ret) = body;
29661 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29662 SET_EXPR_LOCATION (ret, loc);
29663 add_stmt (ret);
29664 return ret;
29665 }
29666 }
29667 if (!flag_openmp) /* flag_openmp_simd */
29668 {
29669 cp_parser_require_pragma_eol (parser, pragma_tok);
29670 return NULL_TREE;
29671 }
29672
29673 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29674 cclauses == NULL);
29675 if (cclauses)
29676 {
29677 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29678 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29679 }
29680
29681 sb = begin_omp_structured_block ();
29682 save = cp_parser_begin_omp_structured_block (parser);
29683
29684 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29685
29686 cp_parser_end_omp_structured_block (parser, save);
29687 add_stmt (finish_omp_structured_block (sb));
29688
29689 return ret;
29690 }
29691
29692 /* OpenMP 2.5:
29693 # pragma omp master new-line
29694 structured-block */
29695
29696 static tree
29697 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29698 {
29699 cp_parser_require_pragma_eol (parser, pragma_tok);
29700 return c_finish_omp_master (input_location,
29701 cp_parser_omp_structured_block (parser));
29702 }
29703
29704 /* OpenMP 2.5:
29705 # pragma omp ordered new-line
29706 structured-block */
29707
29708 static tree
29709 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29710 {
29711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29712 cp_parser_require_pragma_eol (parser, pragma_tok);
29713 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29714 }
29715
29716 /* OpenMP 2.5:
29717
29718 section-scope:
29719 { section-sequence }
29720
29721 section-sequence:
29722 section-directive[opt] structured-block
29723 section-sequence section-directive structured-block */
29724
29725 static tree
29726 cp_parser_omp_sections_scope (cp_parser *parser)
29727 {
29728 tree stmt, substmt;
29729 bool error_suppress = false;
29730 cp_token *tok;
29731
29732 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29733 return NULL_TREE;
29734
29735 stmt = push_stmt_list ();
29736
29737 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29738 {
29739 substmt = cp_parser_omp_structured_block (parser);
29740 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29741 add_stmt (substmt);
29742 }
29743
29744 while (1)
29745 {
29746 tok = cp_lexer_peek_token (parser->lexer);
29747 if (tok->type == CPP_CLOSE_BRACE)
29748 break;
29749 if (tok->type == CPP_EOF)
29750 break;
29751
29752 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29753 {
29754 cp_lexer_consume_token (parser->lexer);
29755 cp_parser_require_pragma_eol (parser, tok);
29756 error_suppress = false;
29757 }
29758 else if (!error_suppress)
29759 {
29760 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29761 error_suppress = true;
29762 }
29763
29764 substmt = cp_parser_omp_structured_block (parser);
29765 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29766 add_stmt (substmt);
29767 }
29768 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29769
29770 substmt = pop_stmt_list (stmt);
29771
29772 stmt = make_node (OMP_SECTIONS);
29773 TREE_TYPE (stmt) = void_type_node;
29774 OMP_SECTIONS_BODY (stmt) = substmt;
29775
29776 add_stmt (stmt);
29777 return stmt;
29778 }
29779
29780 /* OpenMP 2.5:
29781 # pragma omp sections sections-clause[optseq] newline
29782 sections-scope */
29783
29784 #define OMP_SECTIONS_CLAUSE_MASK \
29785 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29790
29791 static tree
29792 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29793 char *p_name, omp_clause_mask mask, tree *cclauses)
29794 {
29795 tree clauses, ret;
29796 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29797
29798 strcat (p_name, " sections");
29799 mask |= OMP_SECTIONS_CLAUSE_MASK;
29800 if (cclauses)
29801 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29802
29803 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29804 cclauses == NULL);
29805 if (cclauses)
29806 {
29807 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29808 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29809 }
29810
29811 ret = cp_parser_omp_sections_scope (parser);
29812 if (ret)
29813 OMP_SECTIONS_CLAUSES (ret) = clauses;
29814
29815 return ret;
29816 }
29817
29818 /* OpenMP 2.5:
29819 # pragma omp parallel parallel-clause[optseq] new-line
29820 structured-block
29821 # pragma omp parallel for parallel-for-clause[optseq] new-line
29822 structured-block
29823 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29824 structured-block
29825
29826 OpenMP 4.0:
29827 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29828 structured-block */
29829
29830 #define OMP_PARALLEL_CLAUSE_MASK \
29831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29840
29841 static tree
29842 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29843 char *p_name, omp_clause_mask mask, tree *cclauses)
29844 {
29845 tree stmt, clauses, block;
29846 unsigned int save;
29847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29848
29849 strcat (p_name, " parallel");
29850 mask |= OMP_PARALLEL_CLAUSE_MASK;
29851
29852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29853 {
29854 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29855 if (cclauses == NULL)
29856 cclauses = cclauses_buf;
29857
29858 cp_lexer_consume_token (parser->lexer);
29859 if (!flag_openmp) /* flag_openmp_simd */
29860 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29861 block = begin_omp_parallel ();
29862 save = cp_parser_begin_omp_structured_block (parser);
29863 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29864 cp_parser_end_omp_structured_block (parser, save);
29865 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29866 block);
29867 if (ret == NULL_TREE)
29868 return ret;
29869 OMP_PARALLEL_COMBINED (stmt) = 1;
29870 return stmt;
29871 }
29872 else if (cclauses)
29873 {
29874 error_at (loc, "expected %<for%> after %qs", p_name);
29875 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29876 return NULL_TREE;
29877 }
29878 else if (!flag_openmp) /* flag_openmp_simd */
29879 {
29880 cp_parser_require_pragma_eol (parser, pragma_tok);
29881 return NULL_TREE;
29882 }
29883 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29884 {
29885 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29886 const char *p = IDENTIFIER_POINTER (id);
29887 if (strcmp (p, "sections") == 0)
29888 {
29889 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29890 cclauses = cclauses_buf;
29891
29892 cp_lexer_consume_token (parser->lexer);
29893 block = begin_omp_parallel ();
29894 save = cp_parser_begin_omp_structured_block (parser);
29895 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29896 cp_parser_end_omp_structured_block (parser, save);
29897 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29898 block);
29899 OMP_PARALLEL_COMBINED (stmt) = 1;
29900 return stmt;
29901 }
29902 }
29903
29904 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29905
29906 block = begin_omp_parallel ();
29907 save = cp_parser_begin_omp_structured_block (parser);
29908 cp_parser_statement (parser, NULL_TREE, false, NULL);
29909 cp_parser_end_omp_structured_block (parser, save);
29910 stmt = finish_omp_parallel (clauses, block);
29911 return stmt;
29912 }
29913
29914 /* OpenMP 2.5:
29915 # pragma omp single single-clause[optseq] new-line
29916 structured-block */
29917
29918 #define OMP_SINGLE_CLAUSE_MASK \
29919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29923
29924 static tree
29925 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29926 {
29927 tree stmt = make_node (OMP_SINGLE);
29928 TREE_TYPE (stmt) = void_type_node;
29929
29930 OMP_SINGLE_CLAUSES (stmt)
29931 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29932 "#pragma omp single", pragma_tok);
29933 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29934
29935 return add_stmt (stmt);
29936 }
29937
29938 /* OpenMP 3.0:
29939 # pragma omp task task-clause[optseq] new-line
29940 structured-block */
29941
29942 #define OMP_TASK_CLAUSE_MASK \
29943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29952
29953 static tree
29954 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29955 {
29956 tree clauses, block;
29957 unsigned int save;
29958
29959 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29960 "#pragma omp task", pragma_tok);
29961 block = begin_omp_task ();
29962 save = cp_parser_begin_omp_structured_block (parser);
29963 cp_parser_statement (parser, NULL_TREE, false, NULL);
29964 cp_parser_end_omp_structured_block (parser, save);
29965 return finish_omp_task (clauses, block);
29966 }
29967
29968 /* OpenMP 3.0:
29969 # pragma omp taskwait new-line */
29970
29971 static void
29972 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29973 {
29974 cp_parser_require_pragma_eol (parser, pragma_tok);
29975 finish_omp_taskwait ();
29976 }
29977
29978 /* OpenMP 3.1:
29979 # pragma omp taskyield new-line */
29980
29981 static void
29982 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29983 {
29984 cp_parser_require_pragma_eol (parser, pragma_tok);
29985 finish_omp_taskyield ();
29986 }
29987
29988 /* OpenMP 4.0:
29989 # pragma omp taskgroup new-line
29990 structured-block */
29991
29992 static tree
29993 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29994 {
29995 cp_parser_require_pragma_eol (parser, pragma_tok);
29996 return c_finish_omp_taskgroup (input_location,
29997 cp_parser_omp_structured_block (parser));
29998 }
29999
30000
30001 /* OpenMP 2.5:
30002 # pragma omp threadprivate (variable-list) */
30003
30004 static void
30005 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30006 {
30007 tree vars;
30008
30009 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30010 cp_parser_require_pragma_eol (parser, pragma_tok);
30011
30012 finish_omp_threadprivate (vars);
30013 }
30014
30015 /* OpenMP 4.0:
30016 # pragma omp cancel cancel-clause[optseq] new-line */
30017
30018 #define OMP_CANCEL_CLAUSE_MASK \
30019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30024
30025 static void
30026 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30027 {
30028 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30029 "#pragma omp cancel", pragma_tok);
30030 finish_omp_cancel (clauses);
30031 }
30032
30033 /* OpenMP 4.0:
30034 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30035
30036 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30037 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30041
30042 static void
30043 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30044 {
30045 tree clauses;
30046 bool point_seen = false;
30047
30048 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30049 {
30050 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30051 const char *p = IDENTIFIER_POINTER (id);
30052
30053 if (strcmp (p, "point") == 0)
30054 {
30055 cp_lexer_consume_token (parser->lexer);
30056 point_seen = true;
30057 }
30058 }
30059 if (!point_seen)
30060 {
30061 cp_parser_error (parser, "expected %<point%>");
30062 cp_parser_require_pragma_eol (parser, pragma_tok);
30063 return;
30064 }
30065
30066 clauses = cp_parser_omp_all_clauses (parser,
30067 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30068 "#pragma omp cancellation point",
30069 pragma_tok);
30070 finish_omp_cancellation_point (clauses);
30071 }
30072
30073 /* OpenMP 4.0:
30074 #pragma omp distribute distribute-clause[optseq] new-line
30075 for-loop */
30076
30077 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30078 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30082
30083 static tree
30084 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30085 char *p_name, omp_clause_mask mask, tree *cclauses)
30086 {
30087 tree clauses, sb, ret;
30088 unsigned int save;
30089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30090
30091 strcat (p_name, " distribute");
30092 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30093
30094 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30095 {
30096 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30097 const char *p = IDENTIFIER_POINTER (id);
30098 bool simd = false;
30099 bool parallel = false;
30100
30101 if (strcmp (p, "simd") == 0)
30102 simd = true;
30103 else
30104 parallel = strcmp (p, "parallel") == 0;
30105 if (parallel || simd)
30106 {
30107 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30108 if (cclauses == NULL)
30109 cclauses = cclauses_buf;
30110 cp_lexer_consume_token (parser->lexer);
30111 if (!flag_openmp) /* flag_openmp_simd */
30112 {
30113 if (simd)
30114 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30115 cclauses);
30116 else
30117 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30118 cclauses);
30119 }
30120 sb = begin_omp_structured_block ();
30121 save = cp_parser_begin_omp_structured_block (parser);
30122 if (simd)
30123 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30124 cclauses);
30125 else
30126 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30127 cclauses);
30128 cp_parser_end_omp_structured_block (parser, save);
30129 tree body = finish_omp_structured_block (sb);
30130 if (ret == NULL)
30131 return ret;
30132 ret = make_node (OMP_DISTRIBUTE);
30133 TREE_TYPE (ret) = void_type_node;
30134 OMP_FOR_BODY (ret) = body;
30135 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30136 SET_EXPR_LOCATION (ret, loc);
30137 add_stmt (ret);
30138 return ret;
30139 }
30140 }
30141 if (!flag_openmp) /* flag_openmp_simd */
30142 {
30143 cp_parser_require_pragma_eol (parser, pragma_tok);
30144 return NULL_TREE;
30145 }
30146
30147 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30148 cclauses == NULL);
30149 if (cclauses)
30150 {
30151 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30152 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30153 }
30154
30155 sb = begin_omp_structured_block ();
30156 save = cp_parser_begin_omp_structured_block (parser);
30157
30158 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30159
30160 cp_parser_end_omp_structured_block (parser, save);
30161 add_stmt (finish_omp_structured_block (sb));
30162
30163 return ret;
30164 }
30165
30166 /* OpenMP 4.0:
30167 # pragma omp teams teams-clause[optseq] new-line
30168 structured-block */
30169
30170 #define OMP_TEAMS_CLAUSE_MASK \
30171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30178
30179 static tree
30180 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30181 char *p_name, omp_clause_mask mask, tree *cclauses)
30182 {
30183 tree clauses, sb, ret;
30184 unsigned int save;
30185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30186
30187 strcat (p_name, " teams");
30188 mask |= OMP_TEAMS_CLAUSE_MASK;
30189
30190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30191 {
30192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30193 const char *p = IDENTIFIER_POINTER (id);
30194 if (strcmp (p, "distribute") == 0)
30195 {
30196 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30197 if (cclauses == NULL)
30198 cclauses = cclauses_buf;
30199
30200 cp_lexer_consume_token (parser->lexer);
30201 if (!flag_openmp) /* flag_openmp_simd */
30202 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30203 cclauses);
30204 sb = begin_omp_structured_block ();
30205 save = cp_parser_begin_omp_structured_block (parser);
30206 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30207 cclauses);
30208 cp_parser_end_omp_structured_block (parser, save);
30209 tree body = finish_omp_structured_block (sb);
30210 if (ret == NULL)
30211 return ret;
30212 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30213 ret = make_node (OMP_TEAMS);
30214 TREE_TYPE (ret) = void_type_node;
30215 OMP_TEAMS_CLAUSES (ret) = clauses;
30216 OMP_TEAMS_BODY (ret) = body;
30217 return add_stmt (ret);
30218 }
30219 }
30220 if (!flag_openmp) /* flag_openmp_simd */
30221 {
30222 cp_parser_require_pragma_eol (parser, pragma_tok);
30223 return NULL_TREE;
30224 }
30225
30226 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30227 cclauses == NULL);
30228 if (cclauses)
30229 {
30230 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30231 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30232 }
30233
30234 tree stmt = make_node (OMP_TEAMS);
30235 TREE_TYPE (stmt) = void_type_node;
30236 OMP_TEAMS_CLAUSES (stmt) = clauses;
30237 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30238
30239 return add_stmt (stmt);
30240 }
30241
30242 /* OpenMP 4.0:
30243 # pragma omp target data target-data-clause[optseq] new-line
30244 structured-block */
30245
30246 #define OMP_TARGET_DATA_CLAUSE_MASK \
30247 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30250
30251 static tree
30252 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30253 {
30254 tree stmt = make_node (OMP_TARGET_DATA);
30255 TREE_TYPE (stmt) = void_type_node;
30256
30257 OMP_TARGET_DATA_CLAUSES (stmt)
30258 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30259 "#pragma omp target data", pragma_tok);
30260 keep_next_level (true);
30261 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30262
30263 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30264 return add_stmt (stmt);
30265 }
30266
30267 /* OpenMP 4.0:
30268 # pragma omp target update target-update-clause[optseq] new-line */
30269
30270 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30275
30276 static bool
30277 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30278 enum pragma_context context)
30279 {
30280 if (context == pragma_stmt)
30281 {
30282 error_at (pragma_tok->location,
30283 "%<#pragma omp target update%> may only be "
30284 "used in compound statements");
30285 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30286 return false;
30287 }
30288
30289 tree clauses
30290 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30291 "#pragma omp target update", pragma_tok);
30292 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30293 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30294 {
30295 error_at (pragma_tok->location,
30296 "%<#pragma omp target update must contain at least one "
30297 "%<from%> or %<to%> clauses");
30298 return false;
30299 }
30300
30301 tree stmt = make_node (OMP_TARGET_UPDATE);
30302 TREE_TYPE (stmt) = void_type_node;
30303 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30304 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30305 add_stmt (stmt);
30306 return false;
30307 }
30308
30309 /* OpenMP 4.0:
30310 # pragma omp target target-clause[optseq] new-line
30311 structured-block */
30312
30313 #define OMP_TARGET_CLAUSE_MASK \
30314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30317
30318 static bool
30319 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30320 enum pragma_context context)
30321 {
30322 if (context != pragma_stmt && context != pragma_compound)
30323 {
30324 cp_parser_error (parser, "expected declaration specifiers");
30325 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30326 return false;
30327 }
30328
30329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30330 {
30331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30332 const char *p = IDENTIFIER_POINTER (id);
30333
30334 if (strcmp (p, "teams") == 0)
30335 {
30336 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30337 char p_name[sizeof ("#pragma omp target teams distribute "
30338 "parallel for simd")];
30339
30340 cp_lexer_consume_token (parser->lexer);
30341 strcpy (p_name, "#pragma omp target");
30342 if (!flag_openmp) /* flag_openmp_simd */
30343 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30344 OMP_TARGET_CLAUSE_MASK, cclauses);
30345 keep_next_level (true);
30346 tree sb = begin_omp_structured_block ();
30347 unsigned save = cp_parser_begin_omp_structured_block (parser);
30348 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30349 OMP_TARGET_CLAUSE_MASK, cclauses);
30350 cp_parser_end_omp_structured_block (parser, save);
30351 tree body = finish_omp_structured_block (sb);
30352 if (ret == NULL)
30353 return ret;
30354 tree stmt = make_node (OMP_TARGET);
30355 TREE_TYPE (stmt) = void_type_node;
30356 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30357 OMP_TARGET_BODY (stmt) = body;
30358 add_stmt (stmt);
30359 return true;
30360 }
30361 else if (!flag_openmp) /* flag_openmp_simd */
30362 {
30363 cp_parser_require_pragma_eol (parser, pragma_tok);
30364 return NULL_TREE;
30365 }
30366 else if (strcmp (p, "data") == 0)
30367 {
30368 cp_lexer_consume_token (parser->lexer);
30369 cp_parser_omp_target_data (parser, pragma_tok);
30370 return true;
30371 }
30372 else if (strcmp (p, "update") == 0)
30373 {
30374 cp_lexer_consume_token (parser->lexer);
30375 return cp_parser_omp_target_update (parser, pragma_tok, context);
30376 }
30377 }
30378
30379 tree stmt = make_node (OMP_TARGET);
30380 TREE_TYPE (stmt) = void_type_node;
30381
30382 OMP_TARGET_CLAUSES (stmt)
30383 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30384 "#pragma omp target", pragma_tok);
30385 keep_next_level (true);
30386 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30387
30388 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30389 add_stmt (stmt);
30390 return true;
30391 }
30392
30393 /* OpenMP 4.0:
30394 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30395
30396 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30403
30404 static void
30405 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30406 enum pragma_context context)
30407 {
30408 bool first_p = parser->omp_declare_simd == NULL;
30409 cp_omp_declare_simd_data data;
30410 if (first_p)
30411 {
30412 data.error_seen = false;
30413 data.fndecl_seen = false;
30414 data.tokens = vNULL;
30415 parser->omp_declare_simd = &data;
30416 }
30417 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30418 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30419 cp_lexer_consume_token (parser->lexer);
30420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30421 parser->omp_declare_simd->error_seen = true;
30422 cp_parser_require_pragma_eol (parser, pragma_tok);
30423 struct cp_token_cache *cp
30424 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30425 parser->omp_declare_simd->tokens.safe_push (cp);
30426 if (first_p)
30427 {
30428 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30429 cp_parser_pragma (parser, context);
30430 switch (context)
30431 {
30432 case pragma_external:
30433 cp_parser_declaration (parser);
30434 break;
30435 case pragma_member:
30436 cp_parser_member_declaration (parser);
30437 break;
30438 case pragma_objc_icode:
30439 cp_parser_block_declaration (parser, /*statement_p=*/false);
30440 break;
30441 default:
30442 cp_parser_declaration_statement (parser);
30443 break;
30444 }
30445 if (parser->omp_declare_simd
30446 && !parser->omp_declare_simd->error_seen
30447 && !parser->omp_declare_simd->fndecl_seen)
30448 error_at (pragma_tok->location,
30449 "%<#pragma omp declare simd%> not immediately followed by "
30450 "function declaration or definition");
30451 data.tokens.release ();
30452 parser->omp_declare_simd = NULL;
30453 }
30454 }
30455
30456 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30457 This function is modelled similar to the late parsing of omp declare
30458 simd. */
30459
30460 static tree
30461 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30462 {
30463 struct cp_token_cache *ce;
30464 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30465 int ii = 0;
30466
30467 if (parser->omp_declare_simd != NULL)
30468 {
30469 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30470 " marked as a Cilk Plus SIMD-enabled function");
30471 XDELETE (parser->cilk_simd_fn_info);
30472 parser->cilk_simd_fn_info = NULL;
30473 return attrs;
30474 }
30475 if (!info->error_seen && info->fndecl_seen)
30476 {
30477 error ("vector attribute not immediately followed by a single function"
30478 " declaration or definition");
30479 info->error_seen = true;
30480 }
30481 if (info->error_seen)
30482 return attrs;
30483
30484 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30485 {
30486 tree c, cl;
30487
30488 cp_parser_push_lexer_for_tokens (parser, ce);
30489 parser->lexer->in_pragma = true;
30490 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30491 "SIMD-enabled functions attribute",
30492 NULL);
30493 cp_parser_pop_lexer (parser);
30494 if (cl)
30495 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30496
30497 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30498 TREE_CHAIN (c) = attrs;
30499 attrs = c;
30500
30501 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30502 TREE_CHAIN (c) = attrs;
30503 if (processing_template_decl)
30504 ATTR_IS_DEPENDENT (c) = 1;
30505 attrs = c;
30506 }
30507 info->fndecl_seen = true;
30508 XDELETE (parser->cilk_simd_fn_info);
30509 parser->cilk_simd_fn_info = NULL;
30510 return attrs;
30511 }
30512
30513 /* Finalize #pragma omp declare simd clauses after direct declarator has
30514 been parsed, and put that into "omp declare simd" attribute. */
30515
30516 static tree
30517 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30518 {
30519 struct cp_token_cache *ce;
30520 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30521 int i;
30522
30523 if (!data->error_seen && data->fndecl_seen)
30524 {
30525 error ("%<#pragma omp declare simd%> not immediately followed by "
30526 "a single function declaration or definition");
30527 data->error_seen = true;
30528 return attrs;
30529 }
30530 if (data->error_seen)
30531 return attrs;
30532
30533 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30534 {
30535 tree c, cl;
30536
30537 cp_parser_push_lexer_for_tokens (parser, ce);
30538 parser->lexer->in_pragma = true;
30539 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30540 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30541 cp_lexer_consume_token (parser->lexer);
30542 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30543 "#pragma omp declare simd", pragma_tok);
30544 cp_parser_pop_lexer (parser);
30545 if (cl)
30546 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30547 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30548 TREE_CHAIN (c) = attrs;
30549 if (processing_template_decl)
30550 ATTR_IS_DEPENDENT (c) = 1;
30551 attrs = c;
30552 }
30553
30554 data->fndecl_seen = true;
30555 return attrs;
30556 }
30557
30558
30559 /* OpenMP 4.0:
30560 # pragma omp declare target new-line
30561 declarations and definitions
30562 # pragma omp end declare target new-line */
30563
30564 static void
30565 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30566 {
30567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30568 scope_chain->omp_declare_target_attribute++;
30569 }
30570
30571 static void
30572 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30573 {
30574 const char *p = "";
30575 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30576 {
30577 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30578 p = IDENTIFIER_POINTER (id);
30579 }
30580 if (strcmp (p, "declare") == 0)
30581 {
30582 cp_lexer_consume_token (parser->lexer);
30583 p = "";
30584 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30585 {
30586 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30587 p = IDENTIFIER_POINTER (id);
30588 }
30589 if (strcmp (p, "target") == 0)
30590 cp_lexer_consume_token (parser->lexer);
30591 else
30592 {
30593 cp_parser_error (parser, "expected %<target%>");
30594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30595 return;
30596 }
30597 }
30598 else
30599 {
30600 cp_parser_error (parser, "expected %<declare%>");
30601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30602 return;
30603 }
30604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30605 if (!scope_chain->omp_declare_target_attribute)
30606 error_at (pragma_tok->location,
30607 "%<#pragma omp end declare target%> without corresponding "
30608 "%<#pragma omp declare target%>");
30609 else
30610 scope_chain->omp_declare_target_attribute--;
30611 }
30612
30613 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30614 expression and optional initializer clause of
30615 #pragma omp declare reduction. We store the expression(s) as
30616 either 3, 6 or 7 special statements inside of the artificial function's
30617 body. The first two statements are DECL_EXPRs for the artificial
30618 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30619 expression that uses those variables.
30620 If there was any INITIALIZER clause, this is followed by further statements,
30621 the fourth and fifth statements are DECL_EXPRs for the artificial
30622 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30623 constructor variant (first token after open paren is not omp_priv),
30624 then the sixth statement is a statement with the function call expression
30625 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30626 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30627 to initialize the OMP_PRIV artificial variable and there is seventh
30628 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30629
30630 static bool
30631 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30632 {
30633 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30634 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30635 type = TREE_TYPE (type);
30636 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30637 DECL_ARTIFICIAL (omp_out) = 1;
30638 pushdecl (omp_out);
30639 add_decl_expr (omp_out);
30640 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30641 DECL_ARTIFICIAL (omp_in) = 1;
30642 pushdecl (omp_in);
30643 add_decl_expr (omp_in);
30644 tree combiner;
30645 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30646
30647 keep_next_level (true);
30648 tree block = begin_omp_structured_block ();
30649 combiner = cp_parser_expression (parser, false, NULL);
30650 finish_expr_stmt (combiner);
30651 block = finish_omp_structured_block (block);
30652 add_stmt (block);
30653
30654 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30655 return false;
30656
30657 const char *p = "";
30658 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30659 {
30660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30661 p = IDENTIFIER_POINTER (id);
30662 }
30663
30664 if (strcmp (p, "initializer") == 0)
30665 {
30666 cp_lexer_consume_token (parser->lexer);
30667 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30668 return false;
30669
30670 p = "";
30671 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30672 {
30673 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30674 p = IDENTIFIER_POINTER (id);
30675 }
30676
30677 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30678 DECL_ARTIFICIAL (omp_priv) = 1;
30679 pushdecl (omp_priv);
30680 add_decl_expr (omp_priv);
30681 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30682 DECL_ARTIFICIAL (omp_orig) = 1;
30683 pushdecl (omp_orig);
30684 add_decl_expr (omp_orig);
30685
30686 keep_next_level (true);
30687 block = begin_omp_structured_block ();
30688
30689 bool ctor = false;
30690 if (strcmp (p, "omp_priv") == 0)
30691 {
30692 bool is_direct_init, is_non_constant_init;
30693 ctor = true;
30694 cp_lexer_consume_token (parser->lexer);
30695 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30696 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30697 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30698 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30699 == CPP_CLOSE_PAREN
30700 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30701 == CPP_CLOSE_PAREN))
30702 {
30703 finish_omp_structured_block (block);
30704 error ("invalid initializer clause");
30705 return false;
30706 }
30707 initializer = cp_parser_initializer (parser, &is_direct_init,
30708 &is_non_constant_init);
30709 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30710 NULL_TREE, LOOKUP_ONLYCONVERTING);
30711 }
30712 else
30713 {
30714 cp_parser_parse_tentatively (parser);
30715 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30716 /*check_dependency_p=*/true,
30717 /*template_p=*/NULL,
30718 /*declarator_p=*/false,
30719 /*optional_p=*/false);
30720 vec<tree, va_gc> *args;
30721 if (fn_name == error_mark_node
30722 || cp_parser_error_occurred (parser)
30723 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30724 || ((args = cp_parser_parenthesized_expression_list
30725 (parser, non_attr, /*cast_p=*/false,
30726 /*allow_expansion_p=*/true,
30727 /*non_constant_p=*/NULL)),
30728 cp_parser_error_occurred (parser)))
30729 {
30730 finish_omp_structured_block (block);
30731 cp_parser_abort_tentative_parse (parser);
30732 cp_parser_error (parser, "expected id-expression (arguments)");
30733 return false;
30734 }
30735 unsigned int i;
30736 tree arg;
30737 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30738 if (arg == omp_priv
30739 || (TREE_CODE (arg) == ADDR_EXPR
30740 && TREE_OPERAND (arg, 0) == omp_priv))
30741 break;
30742 cp_parser_abort_tentative_parse (parser);
30743 if (arg == NULL_TREE)
30744 error ("one of the initializer call arguments should be %<omp_priv%>"
30745 " or %<&omp_priv%>");
30746 initializer = cp_parser_postfix_expression (parser, false, false, false,
30747 false, NULL);
30748 finish_expr_stmt (initializer);
30749 }
30750
30751 block = finish_omp_structured_block (block);
30752 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30753 finish_expr_stmt (block);
30754
30755 if (ctor)
30756 add_decl_expr (omp_orig);
30757
30758 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30759 return false;
30760 }
30761
30762 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30763 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30764
30765 return true;
30766 }
30767
30768 /* OpenMP 4.0
30769 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30770 initializer-clause[opt] new-line
30771
30772 initializer-clause:
30773 initializer (omp_priv initializer)
30774 initializer (function-name (argument-list)) */
30775
30776 static void
30777 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30778 enum pragma_context)
30779 {
30780 auto_vec<tree> types;
30781 enum tree_code reduc_code = ERROR_MARK;
30782 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30783 unsigned int i;
30784 cp_token *first_token;
30785 cp_token_cache *cp;
30786 int errs;
30787 void *p;
30788
30789 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30790 p = obstack_alloc (&declarator_obstack, 0);
30791
30792 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30793 goto fail;
30794
30795 switch (cp_lexer_peek_token (parser->lexer)->type)
30796 {
30797 case CPP_PLUS:
30798 reduc_code = PLUS_EXPR;
30799 break;
30800 case CPP_MULT:
30801 reduc_code = MULT_EXPR;
30802 break;
30803 case CPP_MINUS:
30804 reduc_code = MINUS_EXPR;
30805 break;
30806 case CPP_AND:
30807 reduc_code = BIT_AND_EXPR;
30808 break;
30809 case CPP_XOR:
30810 reduc_code = BIT_XOR_EXPR;
30811 break;
30812 case CPP_OR:
30813 reduc_code = BIT_IOR_EXPR;
30814 break;
30815 case CPP_AND_AND:
30816 reduc_code = TRUTH_ANDIF_EXPR;
30817 break;
30818 case CPP_OR_OR:
30819 reduc_code = TRUTH_ORIF_EXPR;
30820 break;
30821 case CPP_NAME:
30822 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30823 break;
30824 default:
30825 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30826 "%<|%>, %<&&%>, %<||%> or identifier");
30827 goto fail;
30828 }
30829
30830 if (reduc_code != ERROR_MARK)
30831 cp_lexer_consume_token (parser->lexer);
30832
30833 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30834 if (reduc_id == error_mark_node)
30835 goto fail;
30836
30837 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30838 goto fail;
30839
30840 /* Types may not be defined in declare reduction type list. */
30841 const char *saved_message;
30842 saved_message = parser->type_definition_forbidden_message;
30843 parser->type_definition_forbidden_message
30844 = G_("types may not be defined in declare reduction type list");
30845 bool saved_colon_corrects_to_scope_p;
30846 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30847 parser->colon_corrects_to_scope_p = false;
30848 bool saved_colon_doesnt_start_class_def_p;
30849 saved_colon_doesnt_start_class_def_p
30850 = parser->colon_doesnt_start_class_def_p;
30851 parser->colon_doesnt_start_class_def_p = true;
30852
30853 while (true)
30854 {
30855 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30856 type = cp_parser_type_id (parser);
30857 if (type == error_mark_node)
30858 ;
30859 else if (ARITHMETIC_TYPE_P (type)
30860 && (orig_reduc_id == NULL_TREE
30861 || (TREE_CODE (type) != COMPLEX_TYPE
30862 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30863 "min") == 0
30864 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30865 "max") == 0))))
30866 error_at (loc, "predeclared arithmetic type %qT in "
30867 "%<#pragma omp declare reduction%>", type);
30868 else if (TREE_CODE (type) == FUNCTION_TYPE
30869 || TREE_CODE (type) == METHOD_TYPE
30870 || TREE_CODE (type) == ARRAY_TYPE)
30871 error_at (loc, "function or array type %qT in "
30872 "%<#pragma omp declare reduction%>", type);
30873 else if (TREE_CODE (type) == REFERENCE_TYPE)
30874 error_at (loc, "reference type %qT in "
30875 "%<#pragma omp declare reduction%>", type);
30876 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30877 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30878 "%<#pragma omp declare reduction%>", type);
30879 else
30880 types.safe_push (type);
30881
30882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30883 cp_lexer_consume_token (parser->lexer);
30884 else
30885 break;
30886 }
30887
30888 /* Restore the saved message. */
30889 parser->type_definition_forbidden_message = saved_message;
30890 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30891 parser->colon_doesnt_start_class_def_p
30892 = saved_colon_doesnt_start_class_def_p;
30893
30894 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30895 || types.is_empty ())
30896 {
30897 fail:
30898 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30899 goto done;
30900 }
30901
30902 first_token = cp_lexer_peek_token (parser->lexer);
30903 cp = NULL;
30904 errs = errorcount;
30905 FOR_EACH_VEC_ELT (types, i, type)
30906 {
30907 tree fntype
30908 = build_function_type_list (void_type_node,
30909 cp_build_reference_type (type, false),
30910 NULL_TREE);
30911 tree this_reduc_id = reduc_id;
30912 if (!dependent_type_p (type))
30913 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30914 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30915 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30916 DECL_ARTIFICIAL (fndecl) = 1;
30917 DECL_EXTERNAL (fndecl) = 1;
30918 DECL_DECLARED_INLINE_P (fndecl) = 1;
30919 DECL_IGNORED_P (fndecl) = 1;
30920 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30921 DECL_ATTRIBUTES (fndecl)
30922 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30923 DECL_ATTRIBUTES (fndecl));
30924 if (processing_template_decl)
30925 fndecl = push_template_decl (fndecl);
30926 bool block_scope = false;
30927 tree block = NULL_TREE;
30928 if (current_function_decl)
30929 {
30930 block_scope = true;
30931 DECL_CONTEXT (fndecl) = global_namespace;
30932 if (!processing_template_decl)
30933 pushdecl (fndecl);
30934 }
30935 else if (current_class_type)
30936 {
30937 if (cp == NULL)
30938 {
30939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30940 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30941 cp_lexer_consume_token (parser->lexer);
30942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30943 goto fail;
30944 cp = cp_token_cache_new (first_token,
30945 cp_lexer_peek_nth_token (parser->lexer,
30946 2));
30947 }
30948 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30949 finish_member_declaration (fndecl);
30950 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30951 DECL_PENDING_INLINE_P (fndecl) = 1;
30952 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30953 continue;
30954 }
30955 else
30956 {
30957 DECL_CONTEXT (fndecl) = current_namespace;
30958 pushdecl (fndecl);
30959 }
30960 if (!block_scope)
30961 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30962 else
30963 block = begin_omp_structured_block ();
30964 if (cp)
30965 {
30966 cp_parser_push_lexer_for_tokens (parser, cp);
30967 parser->lexer->in_pragma = true;
30968 }
30969 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30970 {
30971 if (!block_scope)
30972 finish_function (0);
30973 else
30974 DECL_CONTEXT (fndecl) = current_function_decl;
30975 if (cp)
30976 cp_parser_pop_lexer (parser);
30977 goto fail;
30978 }
30979 if (cp)
30980 cp_parser_pop_lexer (parser);
30981 if (!block_scope)
30982 finish_function (0);
30983 else
30984 {
30985 DECL_CONTEXT (fndecl) = current_function_decl;
30986 block = finish_omp_structured_block (block);
30987 if (TREE_CODE (block) == BIND_EXPR)
30988 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30989 else if (TREE_CODE (block) == STATEMENT_LIST)
30990 DECL_SAVED_TREE (fndecl) = block;
30991 if (processing_template_decl)
30992 add_decl_expr (fndecl);
30993 }
30994 cp_check_omp_declare_reduction (fndecl);
30995 if (cp == NULL && types.length () > 1)
30996 cp = cp_token_cache_new (first_token,
30997 cp_lexer_peek_nth_token (parser->lexer, 2));
30998 if (errs != errorcount)
30999 break;
31000 }
31001
31002 cp_parser_require_pragma_eol (parser, pragma_tok);
31003
31004 done:
31005 /* Free any declarators allocated. */
31006 obstack_free (&declarator_obstack, p);
31007 }
31008
31009 /* OpenMP 4.0
31010 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31011 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31012 initializer-clause[opt] new-line
31013 #pragma omp declare target new-line */
31014
31015 static void
31016 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31017 enum pragma_context context)
31018 {
31019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31020 {
31021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31022 const char *p = IDENTIFIER_POINTER (id);
31023
31024 if (strcmp (p, "simd") == 0)
31025 {
31026 cp_lexer_consume_token (parser->lexer);
31027 cp_parser_omp_declare_simd (parser, pragma_tok,
31028 context);
31029 return;
31030 }
31031 cp_ensure_no_omp_declare_simd (parser);
31032 if (strcmp (p, "reduction") == 0)
31033 {
31034 cp_lexer_consume_token (parser->lexer);
31035 cp_parser_omp_declare_reduction (parser, pragma_tok,
31036 context);
31037 return;
31038 }
31039 if (!flag_openmp) /* flag_openmp_simd */
31040 {
31041 cp_parser_require_pragma_eol (parser, pragma_tok);
31042 return;
31043 }
31044 if (strcmp (p, "target") == 0)
31045 {
31046 cp_lexer_consume_token (parser->lexer);
31047 cp_parser_omp_declare_target (parser, pragma_tok);
31048 return;
31049 }
31050 }
31051 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31052 "or %<target%>");
31053 cp_parser_require_pragma_eol (parser, pragma_tok);
31054 }
31055
31056 /* Main entry point to OpenMP statement pragmas. */
31057
31058 static void
31059 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31060 {
31061 tree stmt;
31062 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31063 omp_clause_mask mask (0);
31064
31065 switch (pragma_tok->pragma_kind)
31066 {
31067 case PRAGMA_OMP_ATOMIC:
31068 cp_parser_omp_atomic (parser, pragma_tok);
31069 return;
31070 case PRAGMA_OMP_CRITICAL:
31071 stmt = cp_parser_omp_critical (parser, pragma_tok);
31072 break;
31073 case PRAGMA_OMP_DISTRIBUTE:
31074 strcpy (p_name, "#pragma omp");
31075 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31076 break;
31077 case PRAGMA_OMP_FOR:
31078 strcpy (p_name, "#pragma omp");
31079 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31080 break;
31081 case PRAGMA_OMP_MASTER:
31082 stmt = cp_parser_omp_master (parser, pragma_tok);
31083 break;
31084 case PRAGMA_OMP_ORDERED:
31085 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31086 break;
31087 case PRAGMA_OMP_PARALLEL:
31088 strcpy (p_name, "#pragma omp");
31089 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31090 break;
31091 case PRAGMA_OMP_SECTIONS:
31092 strcpy (p_name, "#pragma omp");
31093 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31094 break;
31095 case PRAGMA_OMP_SIMD:
31096 strcpy (p_name, "#pragma omp");
31097 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31098 break;
31099 case PRAGMA_OMP_SINGLE:
31100 stmt = cp_parser_omp_single (parser, pragma_tok);
31101 break;
31102 case PRAGMA_OMP_TASK:
31103 stmt = cp_parser_omp_task (parser, pragma_tok);
31104 break;
31105 case PRAGMA_OMP_TASKGROUP:
31106 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31107 break;
31108 case PRAGMA_OMP_TEAMS:
31109 strcpy (p_name, "#pragma omp");
31110 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31111 break;
31112 default:
31113 gcc_unreachable ();
31114 }
31115
31116 if (stmt)
31117 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31118 }
31119 \f
31120 /* Transactional Memory parsing routines. */
31121
31122 /* Parse a transaction attribute.
31123
31124 txn-attribute:
31125 attribute
31126 [ [ identifier ] ]
31127
31128 ??? Simplify this when C++0x bracket attributes are
31129 implemented properly. */
31130
31131 static tree
31132 cp_parser_txn_attribute_opt (cp_parser *parser)
31133 {
31134 cp_token *token;
31135 tree attr_name, attr = NULL;
31136
31137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31138 return cp_parser_attributes_opt (parser);
31139
31140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31141 return NULL_TREE;
31142 cp_lexer_consume_token (parser->lexer);
31143 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31144 goto error1;
31145
31146 token = cp_lexer_peek_token (parser->lexer);
31147 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31148 {
31149 token = cp_lexer_consume_token (parser->lexer);
31150
31151 attr_name = (token->type == CPP_KEYWORD
31152 /* For keywords, use the canonical spelling,
31153 not the parsed identifier. */
31154 ? ridpointers[(int) token->keyword]
31155 : token->u.value);
31156 attr = build_tree_list (attr_name, NULL_TREE);
31157 }
31158 else
31159 cp_parser_error (parser, "expected identifier");
31160
31161 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31162 error1:
31163 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31164 return attr;
31165 }
31166
31167 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31168
31169 transaction-statement:
31170 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31171 compound-statement
31172 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31173 */
31174
31175 static tree
31176 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31177 {
31178 unsigned char old_in = parser->in_transaction;
31179 unsigned char this_in = 1, new_in;
31180 cp_token *token;
31181 tree stmt, attrs, noex;
31182
31183 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31184 || keyword == RID_TRANSACTION_RELAXED);
31185 token = cp_parser_require_keyword (parser, keyword,
31186 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31187 : RT_TRANSACTION_RELAXED));
31188 gcc_assert (token != NULL);
31189
31190 if (keyword == RID_TRANSACTION_RELAXED)
31191 this_in |= TM_STMT_ATTR_RELAXED;
31192 else
31193 {
31194 attrs = cp_parser_txn_attribute_opt (parser);
31195 if (attrs)
31196 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31197 }
31198
31199 /* Parse a noexcept specification. */
31200 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31201
31202 /* Keep track if we're in the lexical scope of an outer transaction. */
31203 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31204
31205 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31206
31207 parser->in_transaction = new_in;
31208 cp_parser_compound_statement (parser, NULL, false, false);
31209 parser->in_transaction = old_in;
31210
31211 finish_transaction_stmt (stmt, NULL, this_in, noex);
31212
31213 return stmt;
31214 }
31215
31216 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31217
31218 transaction-expression:
31219 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31220 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31221 */
31222
31223 static tree
31224 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31225 {
31226 unsigned char old_in = parser->in_transaction;
31227 unsigned char this_in = 1;
31228 cp_token *token;
31229 tree expr, noex;
31230 bool noex_expr;
31231
31232 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31233 || keyword == RID_TRANSACTION_RELAXED);
31234
31235 if (!flag_tm)
31236 error (keyword == RID_TRANSACTION_RELAXED
31237 ? G_("%<__transaction_relaxed%> without transactional memory "
31238 "support enabled")
31239 : G_("%<__transaction_atomic%> without transactional memory "
31240 "support enabled"));
31241
31242 token = cp_parser_require_keyword (parser, keyword,
31243 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31244 : RT_TRANSACTION_RELAXED));
31245 gcc_assert (token != NULL);
31246
31247 if (keyword == RID_TRANSACTION_RELAXED)
31248 this_in |= TM_STMT_ATTR_RELAXED;
31249
31250 /* Set this early. This might mean that we allow transaction_cancel in
31251 an expression that we find out later actually has to be a constexpr.
31252 However, we expect that cxx_constant_value will be able to deal with
31253 this; also, if the noexcept has no constexpr, then what we parse next
31254 really is a transaction's body. */
31255 parser->in_transaction = this_in;
31256
31257 /* Parse a noexcept specification. */
31258 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31259 true);
31260
31261 if (!noex || !noex_expr
31262 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31263 {
31264 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31265
31266 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31267 expr = finish_parenthesized_expr (expr);
31268
31269 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31270 }
31271 else
31272 {
31273 /* The only expression that is available got parsed for the noexcept
31274 already. noexcept is true then. */
31275 expr = noex;
31276 noex = boolean_true_node;
31277 }
31278
31279 expr = build_transaction_expr (token->location, expr, this_in, noex);
31280 parser->in_transaction = old_in;
31281
31282 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31283 return error_mark_node;
31284
31285 return (flag_tm ? expr : error_mark_node);
31286 }
31287
31288 /* Parse a function-transaction-block.
31289
31290 function-transaction-block:
31291 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31292 function-body
31293 __transaction_atomic txn-attribute[opt] function-try-block
31294 __transaction_relaxed ctor-initializer[opt] function-body
31295 __transaction_relaxed function-try-block
31296 */
31297
31298 static bool
31299 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31300 {
31301 unsigned char old_in = parser->in_transaction;
31302 unsigned char new_in = 1;
31303 tree compound_stmt, stmt, attrs;
31304 bool ctor_initializer_p;
31305 cp_token *token;
31306
31307 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31308 || keyword == RID_TRANSACTION_RELAXED);
31309 token = cp_parser_require_keyword (parser, keyword,
31310 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31311 : RT_TRANSACTION_RELAXED));
31312 gcc_assert (token != NULL);
31313
31314 if (keyword == RID_TRANSACTION_RELAXED)
31315 new_in |= TM_STMT_ATTR_RELAXED;
31316 else
31317 {
31318 attrs = cp_parser_txn_attribute_opt (parser);
31319 if (attrs)
31320 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31321 }
31322
31323 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31324
31325 parser->in_transaction = new_in;
31326
31327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31328 ctor_initializer_p = cp_parser_function_try_block (parser);
31329 else
31330 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31331 (parser, /*in_function_try_block=*/false);
31332
31333 parser->in_transaction = old_in;
31334
31335 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31336
31337 return ctor_initializer_p;
31338 }
31339
31340 /* Parse a __transaction_cancel statement.
31341
31342 cancel-statement:
31343 __transaction_cancel txn-attribute[opt] ;
31344 __transaction_cancel txn-attribute[opt] throw-expression ;
31345
31346 ??? Cancel and throw is not yet implemented. */
31347
31348 static tree
31349 cp_parser_transaction_cancel (cp_parser *parser)
31350 {
31351 cp_token *token;
31352 bool is_outer = false;
31353 tree stmt, attrs;
31354
31355 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31356 RT_TRANSACTION_CANCEL);
31357 gcc_assert (token != NULL);
31358
31359 attrs = cp_parser_txn_attribute_opt (parser);
31360 if (attrs)
31361 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31362
31363 /* ??? Parse cancel-and-throw here. */
31364
31365 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31366
31367 if (!flag_tm)
31368 {
31369 error_at (token->location, "%<__transaction_cancel%> without "
31370 "transactional memory support enabled");
31371 return error_mark_node;
31372 }
31373 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31374 {
31375 error_at (token->location, "%<__transaction_cancel%> within a "
31376 "%<__transaction_relaxed%>");
31377 return error_mark_node;
31378 }
31379 else if (is_outer)
31380 {
31381 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31382 && !is_tm_may_cancel_outer (current_function_decl))
31383 {
31384 error_at (token->location, "outer %<__transaction_cancel%> not "
31385 "within outer %<__transaction_atomic%>");
31386 error_at (token->location,
31387 " or a %<transaction_may_cancel_outer%> function");
31388 return error_mark_node;
31389 }
31390 }
31391 else if (parser->in_transaction == 0)
31392 {
31393 error_at (token->location, "%<__transaction_cancel%> not within "
31394 "%<__transaction_atomic%>");
31395 return error_mark_node;
31396 }
31397
31398 stmt = build_tm_abort_call (token->location, is_outer);
31399 add_stmt (stmt);
31400
31401 return stmt;
31402 }
31403 \f
31404 /* The parser. */
31405
31406 static GTY (()) cp_parser *the_parser;
31407
31408 \f
31409 /* Special handling for the first token or line in the file. The first
31410 thing in the file might be #pragma GCC pch_preprocess, which loads a
31411 PCH file, which is a GC collection point. So we need to handle this
31412 first pragma without benefit of an existing lexer structure.
31413
31414 Always returns one token to the caller in *FIRST_TOKEN. This is
31415 either the true first token of the file, or the first token after
31416 the initial pragma. */
31417
31418 static void
31419 cp_parser_initial_pragma (cp_token *first_token)
31420 {
31421 tree name = NULL;
31422
31423 cp_lexer_get_preprocessor_token (NULL, first_token);
31424 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31425 return;
31426
31427 cp_lexer_get_preprocessor_token (NULL, first_token);
31428 if (first_token->type == CPP_STRING)
31429 {
31430 name = first_token->u.value;
31431
31432 cp_lexer_get_preprocessor_token (NULL, first_token);
31433 if (first_token->type != CPP_PRAGMA_EOL)
31434 error_at (first_token->location,
31435 "junk at end of %<#pragma GCC pch_preprocess%>");
31436 }
31437 else
31438 error_at (first_token->location, "expected string literal");
31439
31440 /* Skip to the end of the pragma. */
31441 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31442 cp_lexer_get_preprocessor_token (NULL, first_token);
31443
31444 /* Now actually load the PCH file. */
31445 if (name)
31446 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31447
31448 /* Read one more token to return to our caller. We have to do this
31449 after reading the PCH file in, since its pointers have to be
31450 live. */
31451 cp_lexer_get_preprocessor_token (NULL, first_token);
31452 }
31453
31454 /* Normal parsing of a pragma token. Here we can (and must) use the
31455 regular lexer. */
31456
31457 static bool
31458 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31459 {
31460 cp_token *pragma_tok;
31461 unsigned int id;
31462
31463 pragma_tok = cp_lexer_consume_token (parser->lexer);
31464 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31465 parser->lexer->in_pragma = true;
31466
31467 id = pragma_tok->pragma_kind;
31468 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31469 cp_ensure_no_omp_declare_simd (parser);
31470 switch (id)
31471 {
31472 case PRAGMA_GCC_PCH_PREPROCESS:
31473 error_at (pragma_tok->location,
31474 "%<#pragma GCC pch_preprocess%> must be first");
31475 break;
31476
31477 case PRAGMA_OMP_BARRIER:
31478 switch (context)
31479 {
31480 case pragma_compound:
31481 cp_parser_omp_barrier (parser, pragma_tok);
31482 return false;
31483 case pragma_stmt:
31484 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31485 "used in compound statements");
31486 break;
31487 default:
31488 goto bad_stmt;
31489 }
31490 break;
31491
31492 case PRAGMA_OMP_FLUSH:
31493 switch (context)
31494 {
31495 case pragma_compound:
31496 cp_parser_omp_flush (parser, pragma_tok);
31497 return false;
31498 case pragma_stmt:
31499 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31500 "used in compound statements");
31501 break;
31502 default:
31503 goto bad_stmt;
31504 }
31505 break;
31506
31507 case PRAGMA_OMP_TASKWAIT:
31508 switch (context)
31509 {
31510 case pragma_compound:
31511 cp_parser_omp_taskwait (parser, pragma_tok);
31512 return false;
31513 case pragma_stmt:
31514 error_at (pragma_tok->location,
31515 "%<#pragma omp taskwait%> may only be "
31516 "used in compound statements");
31517 break;
31518 default:
31519 goto bad_stmt;
31520 }
31521 break;
31522
31523 case PRAGMA_OMP_TASKYIELD:
31524 switch (context)
31525 {
31526 case pragma_compound:
31527 cp_parser_omp_taskyield (parser, pragma_tok);
31528 return false;
31529 case pragma_stmt:
31530 error_at (pragma_tok->location,
31531 "%<#pragma omp taskyield%> may only be "
31532 "used in compound statements");
31533 break;
31534 default:
31535 goto bad_stmt;
31536 }
31537 break;
31538
31539 case PRAGMA_OMP_CANCEL:
31540 switch (context)
31541 {
31542 case pragma_compound:
31543 cp_parser_omp_cancel (parser, pragma_tok);
31544 return false;
31545 case pragma_stmt:
31546 error_at (pragma_tok->location,
31547 "%<#pragma omp cancel%> may only be "
31548 "used in compound statements");
31549 break;
31550 default:
31551 goto bad_stmt;
31552 }
31553 break;
31554
31555 case PRAGMA_OMP_CANCELLATION_POINT:
31556 switch (context)
31557 {
31558 case pragma_compound:
31559 cp_parser_omp_cancellation_point (parser, pragma_tok);
31560 return false;
31561 case pragma_stmt:
31562 error_at (pragma_tok->location,
31563 "%<#pragma omp cancellation point%> may only be "
31564 "used in compound statements");
31565 break;
31566 default:
31567 goto bad_stmt;
31568 }
31569 break;
31570
31571 case PRAGMA_OMP_THREADPRIVATE:
31572 cp_parser_omp_threadprivate (parser, pragma_tok);
31573 return false;
31574
31575 case PRAGMA_OMP_DECLARE_REDUCTION:
31576 cp_parser_omp_declare (parser, pragma_tok, context);
31577 return false;
31578
31579 case PRAGMA_OMP_ATOMIC:
31580 case PRAGMA_OMP_CRITICAL:
31581 case PRAGMA_OMP_DISTRIBUTE:
31582 case PRAGMA_OMP_FOR:
31583 case PRAGMA_OMP_MASTER:
31584 case PRAGMA_OMP_ORDERED:
31585 case PRAGMA_OMP_PARALLEL:
31586 case PRAGMA_OMP_SECTIONS:
31587 case PRAGMA_OMP_SIMD:
31588 case PRAGMA_OMP_SINGLE:
31589 case PRAGMA_OMP_TASK:
31590 case PRAGMA_OMP_TASKGROUP:
31591 case PRAGMA_OMP_TEAMS:
31592 if (context != pragma_stmt && context != pragma_compound)
31593 goto bad_stmt;
31594 cp_parser_omp_construct (parser, pragma_tok);
31595 return true;
31596
31597 case PRAGMA_OMP_TARGET:
31598 return cp_parser_omp_target (parser, pragma_tok, context);
31599
31600 case PRAGMA_OMP_END_DECLARE_TARGET:
31601 cp_parser_omp_end_declare_target (parser, pragma_tok);
31602 return false;
31603
31604 case PRAGMA_OMP_SECTION:
31605 error_at (pragma_tok->location,
31606 "%<#pragma omp section%> may only be used in "
31607 "%<#pragma omp sections%> construct");
31608 break;
31609
31610 case PRAGMA_IVDEP:
31611 {
31612 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31613 cp_token *tok;
31614 tok = cp_lexer_peek_token (the_parser->lexer);
31615 if (tok->type != CPP_KEYWORD
31616 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31617 && tok->keyword != RID_DO))
31618 {
31619 cp_parser_error (parser, "for, while or do statement expected");
31620 return false;
31621 }
31622 cp_parser_iteration_statement (parser, true);
31623 return true;
31624 }
31625
31626 case PRAGMA_CILK_SIMD:
31627 if (context == pragma_external)
31628 {
31629 error_at (pragma_tok->location,
31630 "%<#pragma simd%> must be inside a function");
31631 break;
31632 }
31633 cp_parser_cilk_simd (parser, pragma_tok);
31634 return true;
31635
31636 default:
31637 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31638 c_invoke_pragma_handler (id);
31639 break;
31640
31641 bad_stmt:
31642 cp_parser_error (parser, "expected declaration specifiers");
31643 break;
31644 }
31645
31646 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31647 return false;
31648 }
31649
31650 /* The interface the pragma parsers have to the lexer. */
31651
31652 enum cpp_ttype
31653 pragma_lex (tree *value)
31654 {
31655 cp_token *tok;
31656 enum cpp_ttype ret;
31657
31658 tok = cp_lexer_peek_token (the_parser->lexer);
31659
31660 ret = tok->type;
31661 *value = tok->u.value;
31662
31663 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31664 ret = CPP_EOF;
31665 else if (ret == CPP_STRING)
31666 *value = cp_parser_string_literal (the_parser, false, false);
31667 else
31668 {
31669 cp_lexer_consume_token (the_parser->lexer);
31670 if (ret == CPP_KEYWORD)
31671 ret = CPP_NAME;
31672 }
31673
31674 return ret;
31675 }
31676
31677 \f
31678 /* External interface. */
31679
31680 /* Parse one entire translation unit. */
31681
31682 void
31683 c_parse_file (void)
31684 {
31685 static bool already_called = false;
31686
31687 if (already_called)
31688 {
31689 sorry ("inter-module optimizations not implemented for C++");
31690 return;
31691 }
31692 already_called = true;
31693
31694 the_parser = cp_parser_new ();
31695 push_deferring_access_checks (flag_access_control
31696 ? dk_no_deferred : dk_no_check);
31697 cp_parser_translation_unit (the_parser);
31698 the_parser = NULL;
31699 }
31700
31701 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31702 vectorlength clause:
31703 Syntax:
31704 vectorlength ( constant-expression ) */
31705
31706 static tree
31707 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31708 bool is_simd_fn)
31709 {
31710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31711 tree expr;
31712 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31713 safelen clause. Thus, vectorlength is represented as OMP 4.0
31714 safelen. For SIMD-enabled function it is represented by OMP 4.0
31715 simdlen. */
31716 if (!is_simd_fn)
31717 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31718 loc);
31719 else
31720 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31721 loc);
31722
31723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31724 return error_mark_node;
31725
31726 expr = cp_parser_constant_expression (parser, false, NULL);
31727 expr = maybe_constant_value (expr);
31728
31729 /* If expr == error_mark_node, then don't emit any errors nor
31730 create a clause. if any of the above functions returns
31731 error mark node then they would have emitted an error message. */
31732 if (expr == error_mark_node)
31733 ;
31734 else if (!TREE_TYPE (expr)
31735 || !TREE_CONSTANT (expr)
31736 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31737 error_at (loc, "vectorlength must be an integer constant");
31738 else if (TREE_CONSTANT (expr)
31739 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31740 error_at (loc, "vectorlength must be a power of 2");
31741 else
31742 {
31743 tree c;
31744 if (!is_simd_fn)
31745 {
31746 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31747 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31748 OMP_CLAUSE_CHAIN (c) = clauses;
31749 clauses = c;
31750 }
31751 else
31752 {
31753 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31754 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31755 OMP_CLAUSE_CHAIN (c) = clauses;
31756 clauses = c;
31757 }
31758 }
31759
31760 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31761 return error_mark_node;
31762 return clauses;
31763 }
31764
31765 /* Handles the Cilk Plus #pragma simd linear clause.
31766 Syntax:
31767 linear ( simd-linear-variable-list )
31768
31769 simd-linear-variable-list:
31770 simd-linear-variable
31771 simd-linear-variable-list , simd-linear-variable
31772
31773 simd-linear-variable:
31774 id-expression
31775 id-expression : simd-linear-step
31776
31777 simd-linear-step:
31778 conditional-expression */
31779
31780 static tree
31781 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31782 {
31783 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31784
31785 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31786 return clauses;
31787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31788 {
31789 cp_parser_error (parser, "expected identifier");
31790 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31791 return error_mark_node;
31792 }
31793
31794 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31795 parser->colon_corrects_to_scope_p = false;
31796 while (1)
31797 {
31798 cp_token *token = cp_lexer_peek_token (parser->lexer);
31799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31800 {
31801 cp_parser_error (parser, "expected variable-name");
31802 clauses = error_mark_node;
31803 break;
31804 }
31805
31806 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31807 false, false);
31808 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31809 token->location);
31810 if (decl == error_mark_node)
31811 {
31812 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31813 token->location);
31814 clauses = error_mark_node;
31815 }
31816 else
31817 {
31818 tree e = NULL_TREE;
31819 tree step_size = integer_one_node;
31820
31821 /* If present, parse the linear step. Otherwise, assume the default
31822 value of 1. */
31823 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31824 {
31825 cp_lexer_consume_token (parser->lexer);
31826
31827 e = cp_parser_assignment_expression (parser, false, NULL);
31828 e = maybe_constant_value (e);
31829
31830 if (e == error_mark_node)
31831 {
31832 /* If an error has occurred, then the whole pragma is
31833 considered ill-formed. Thus, no reason to keep
31834 parsing. */
31835 clauses = error_mark_node;
31836 break;
31837 }
31838 else if (type_dependent_expression_p (e)
31839 || value_dependent_expression_p (e)
31840 || (TREE_TYPE (e)
31841 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31842 && (TREE_CONSTANT (e)
31843 || DECL_P (e))))
31844 step_size = e;
31845 else
31846 cp_parser_error (parser,
31847 "step size must be an integer constant "
31848 "expression or an integer variable");
31849 }
31850
31851 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31852 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31853 OMP_CLAUSE_DECL (l) = decl;
31854 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31855 OMP_CLAUSE_CHAIN (l) = clauses;
31856 clauses = l;
31857 }
31858 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31859 cp_lexer_consume_token (parser->lexer);
31860 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31861 break;
31862 else
31863 {
31864 error_at (cp_lexer_peek_token (parser->lexer)->location,
31865 "expected %<,%> or %<)%> after %qE", decl);
31866 clauses = error_mark_node;
31867 break;
31868 }
31869 }
31870 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31871 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31872 return clauses;
31873 }
31874
31875 /* Returns the name of the next clause. If the clause is not
31876 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31877 token is not consumed. Otherwise, the appropriate enum from the
31878 pragma_simd_clause is returned and the token is consumed. */
31879
31880 static pragma_omp_clause
31881 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31882 {
31883 pragma_omp_clause clause_type;
31884 cp_token *token = cp_lexer_peek_token (parser->lexer);
31885
31886 if (token->keyword == RID_PRIVATE)
31887 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31888 else if (!token->u.value || token->type != CPP_NAME)
31889 return PRAGMA_CILK_CLAUSE_NONE;
31890 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31891 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31892 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31893 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31894 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31895 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31896 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31897 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31898 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31899 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31900 else
31901 return PRAGMA_CILK_CLAUSE_NONE;
31902
31903 cp_lexer_consume_token (parser->lexer);
31904 return clause_type;
31905 }
31906
31907 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31908
31909 static tree
31910 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31911 {
31912 tree clauses = NULL_TREE;
31913
31914 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31915 && clauses != error_mark_node)
31916 {
31917 pragma_omp_clause c_kind;
31918 c_kind = cp_parser_cilk_simd_clause_name (parser);
31919 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31920 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31921 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31922 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31923 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31924 /* Use the OpenMP 4.0 equivalent function. */
31925 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31926 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31927 /* Use the OpenMP 4.0 equivalent function. */
31928 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31929 clauses);
31930 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31931 /* Use the OMP 4.0 equivalent function. */
31932 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31933 clauses);
31934 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31935 /* Use the OMP 4.0 equivalent function. */
31936 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31937 else
31938 {
31939 clauses = error_mark_node;
31940 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31941 break;
31942 }
31943 }
31944
31945 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31946
31947 if (clauses == error_mark_node)
31948 return error_mark_node;
31949 else
31950 return c_finish_cilk_clauses (clauses);
31951 }
31952
31953 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31954
31955 static void
31956 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31957 {
31958 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31959
31960 if (clauses == error_mark_node)
31961 return;
31962
31963 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31964 {
31965 error_at (cp_lexer_peek_token (parser->lexer)->location,
31966 "for statement expected");
31967 return;
31968 }
31969
31970 tree sb = begin_omp_structured_block ();
31971 int save = cp_parser_begin_omp_structured_block (parser);
31972 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31973 if (ret)
31974 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31975 cp_parser_end_omp_structured_block (parser, save);
31976 add_stmt (finish_omp_structured_block (sb));
31977 return;
31978 }
31979
31980 /* Create an identifier for a generic parameter type (a synthesized
31981 template parameter implied by `auto' or a concept identifier). */
31982
31983 static GTY(()) int generic_parm_count;
31984 static tree
31985 make_generic_type_name ()
31986 {
31987 char buf[32];
31988 sprintf (buf, "auto:%d", ++generic_parm_count);
31989 return get_identifier (buf);
31990 }
31991
31992 /* Predicate that behaves as is_auto_or_concept but matches the parent
31993 node of the generic type rather than the generic type itself. This
31994 allows for type transformation in add_implicit_template_parms. */
31995
31996 static inline bool
31997 tree_type_is_auto_or_concept (const_tree t)
31998 {
31999 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32000 }
32001
32002 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32003 (creating a new template parameter list if necessary). Returns the newly
32004 created template type parm. */
32005
32006 tree
32007 synthesize_implicit_template_parm (cp_parser *parser)
32008 {
32009 gcc_assert (current_binding_level->kind == sk_function_parms);
32010
32011 /* We are either continuing a function template that already contains implicit
32012 template parameters, creating a new fully-implicit function template, or
32013 extending an existing explicit function template with implicit template
32014 parameters. */
32015
32016 cp_binding_level *const entry_scope = current_binding_level;
32017
32018 bool become_template = false;
32019 cp_binding_level *parent_scope = 0;
32020
32021 if (parser->implicit_template_scope)
32022 {
32023 gcc_assert (parser->implicit_template_parms);
32024
32025 current_binding_level = parser->implicit_template_scope;
32026 }
32027 else
32028 {
32029 /* Roll back to the existing template parameter scope (in the case of
32030 extending an explicit function template) or introduce a new template
32031 parameter scope ahead of the function parameter scope (or class scope
32032 in the case of out-of-line member definitions). The function scope is
32033 added back after template parameter synthesis below. */
32034
32035 cp_binding_level *scope = entry_scope;
32036
32037 while (scope->kind == sk_function_parms)
32038 {
32039 parent_scope = scope;
32040 scope = scope->level_chain;
32041 }
32042 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32043 {
32044 /* If not defining a class, then any class scope is a scope level in
32045 an out-of-line member definition. In this case simply wind back
32046 beyond the first such scope to inject the template parameter list.
32047 Otherwise wind back to the class being defined. The latter can
32048 occur in class member friend declarations such as:
32049
32050 class A {
32051 void foo (auto);
32052 };
32053 class B {
32054 friend void A::foo (auto);
32055 };
32056
32057 The template parameter list synthesized for the friend declaration
32058 must be injected in the scope of 'B'. This can also occur in
32059 erroneous cases such as:
32060
32061 struct A {
32062 struct B {
32063 void foo (auto);
32064 };
32065 void B::foo (auto) {}
32066 };
32067
32068 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32069 but, nevertheless, the template parameter list synthesized for the
32070 declarator should be injected into the scope of 'A' as if the
32071 ill-formed template was specified explicitly. */
32072
32073 while (scope->kind == sk_class && !scope->defining_class_p)
32074 {
32075 parent_scope = scope;
32076 scope = scope->level_chain;
32077 }
32078 }
32079
32080 current_binding_level = scope;
32081
32082 if (scope->kind != sk_template_parms
32083 || !function_being_declared_is_template_p (parser))
32084 {
32085 /* Introduce a new template parameter list for implicit template
32086 parameters. */
32087
32088 become_template = true;
32089
32090 parser->implicit_template_scope
32091 = begin_scope (sk_template_parms, NULL);
32092
32093 ++processing_template_decl;
32094
32095 parser->fully_implicit_function_template_p = true;
32096 ++parser->num_template_parameter_lists;
32097 }
32098 else
32099 {
32100 /* Synthesize implicit template parameters at the end of the explicit
32101 template parameter list. */
32102
32103 gcc_assert (current_template_parms);
32104
32105 parser->implicit_template_scope = scope;
32106
32107 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32108 parser->implicit_template_parms
32109 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32110 }
32111 }
32112
32113 /* Synthesize a new template parameter and track the current template
32114 parameter chain with implicit_template_parms. */
32115
32116 tree synth_id = make_generic_type_name ();
32117 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32118 synth_id);
32119 tree new_parm
32120 = process_template_parm (parser->implicit_template_parms,
32121 input_location,
32122 build_tree_list (NULL_TREE, synth_tmpl_parm),
32123 /*non_type=*/false,
32124 /*param_pack=*/false);
32125
32126
32127 if (parser->implicit_template_parms)
32128 parser->implicit_template_parms
32129 = TREE_CHAIN (parser->implicit_template_parms);
32130 else
32131 parser->implicit_template_parms = new_parm;
32132
32133 tree new_type = TREE_TYPE (getdecls ());
32134
32135 /* If creating a fully implicit function template, start the new implicit
32136 template parameter list with this synthesized type, otherwise grow the
32137 current template parameter list. */
32138
32139 if (become_template)
32140 {
32141 parent_scope->level_chain = current_binding_level;
32142
32143 tree new_parms = make_tree_vec (1);
32144 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32145 current_template_parms = tree_cons (size_int (processing_template_decl),
32146 new_parms, current_template_parms);
32147 }
32148 else
32149 {
32150 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32151 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32152 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32153 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32154 }
32155
32156 current_binding_level = entry_scope;
32157
32158 return new_type;
32159 }
32160
32161 /* Finish the declaration of a fully implicit function template. Such a
32162 template has no explicit template parameter list so has not been through the
32163 normal template head and tail processing. synthesize_implicit_template_parm
32164 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32165 provided if the declaration is a class member such that its template
32166 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32167 form is returned. Otherwise NULL_TREE is returned. */
32168
32169 tree
32170 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32171 {
32172 gcc_assert (parser->fully_implicit_function_template_p);
32173
32174 if (member_decl_opt && member_decl_opt != error_mark_node
32175 && DECL_VIRTUAL_P (member_decl_opt))
32176 {
32177 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32178 "implicit templates may not be %<virtual%>");
32179 DECL_VIRTUAL_P (member_decl_opt) = false;
32180 }
32181
32182 if (member_decl_opt)
32183 member_decl_opt = finish_member_template_decl (member_decl_opt);
32184 end_template_decl ();
32185
32186 parser->fully_implicit_function_template_p = false;
32187 --parser->num_template_parameter_lists;
32188
32189 return member_decl_opt;
32190 }
32191
32192 #include "gt-cp-parser.h"