]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
genattrtab.c (write_header): Include hash-set.h...
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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 "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "trans-mem.h"
41 #include "cp-tree.h"
42 #include "intl.h"
43 #include "c-family/c-pragma.h"
44 #include "decl.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "target.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "tree-pretty-print.h"
60 #include "parser.h"
61 #include "type-utils.h"
62 #include "omp-low.h"
63
64 \f
65 /* The lexer. */
66
67 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
68 and c-lex.c) and the C++ parser. */
69
70 static cp_token eof_token =
71 {
72 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
73 };
74
75 /* The various kinds of non integral constant we encounter. */
76 typedef enum non_integral_constant {
77 NIC_NONE,
78 /* floating-point literal */
79 NIC_FLOAT,
80 /* %<this%> */
81 NIC_THIS,
82 /* %<__FUNCTION__%> */
83 NIC_FUNC_NAME,
84 /* %<__PRETTY_FUNCTION__%> */
85 NIC_PRETTY_FUNC,
86 /* %<__func__%> */
87 NIC_C99_FUNC,
88 /* "%<va_arg%> */
89 NIC_VA_ARG,
90 /* a cast */
91 NIC_CAST,
92 /* %<typeid%> operator */
93 NIC_TYPEID,
94 /* non-constant compound literals */
95 NIC_NCC,
96 /* a function call */
97 NIC_FUNC_CALL,
98 /* an increment */
99 NIC_INC,
100 /* an decrement */
101 NIC_DEC,
102 /* an array reference */
103 NIC_ARRAY_REF,
104 /* %<->%> */
105 NIC_ARROW,
106 /* %<.%> */
107 NIC_POINT,
108 /* the address of a label */
109 NIC_ADDR_LABEL,
110 /* %<*%> */
111 NIC_STAR,
112 /* %<&%> */
113 NIC_ADDR,
114 /* %<++%> */
115 NIC_PREINCREMENT,
116 /* %<--%> */
117 NIC_PREDECREMENT,
118 /* %<new%> */
119 NIC_NEW,
120 /* %<delete%> */
121 NIC_DEL,
122 /* calls to overloaded operators */
123 NIC_OVERLOADED,
124 /* an assignment */
125 NIC_ASSIGNMENT,
126 /* a comma operator */
127 NIC_COMMA,
128 /* a call to a constructor */
129 NIC_CONSTRUCTOR,
130 /* a transaction expression */
131 NIC_TRANSACTION
132 } non_integral_constant;
133
134 /* The various kinds of errors about name-lookup failing. */
135 typedef enum name_lookup_error {
136 /* NULL */
137 NLE_NULL,
138 /* is not a type */
139 NLE_TYPE,
140 /* is not a class or namespace */
141 NLE_CXX98,
142 /* is not a class, namespace, or enumeration */
143 NLE_NOT_CXX98
144 } name_lookup_error;
145
146 /* The various kinds of required token */
147 typedef enum required_token {
148 RT_NONE,
149 RT_SEMICOLON, /* ';' */
150 RT_OPEN_PAREN, /* '(' */
151 RT_CLOSE_BRACE, /* '}' */
152 RT_OPEN_BRACE, /* '{' */
153 RT_CLOSE_SQUARE, /* ']' */
154 RT_OPEN_SQUARE, /* '[' */
155 RT_COMMA, /* ',' */
156 RT_SCOPE, /* '::' */
157 RT_LESS, /* '<' */
158 RT_GREATER, /* '>' */
159 RT_EQ, /* '=' */
160 RT_ELLIPSIS, /* '...' */
161 RT_MULT, /* '*' */
162 RT_COMPL, /* '~' */
163 RT_COLON, /* ':' */
164 RT_COLON_SCOPE, /* ':' or '::' */
165 RT_CLOSE_PAREN, /* ')' */
166 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
167 RT_PRAGMA_EOL, /* end of line */
168 RT_NAME, /* identifier */
169
170 /* The type is CPP_KEYWORD */
171 RT_NEW, /* new */
172 RT_DELETE, /* delete */
173 RT_RETURN, /* return */
174 RT_WHILE, /* while */
175 RT_EXTERN, /* extern */
176 RT_STATIC_ASSERT, /* static_assert */
177 RT_DECLTYPE, /* decltype */
178 RT_OPERATOR, /* operator */
179 RT_CLASS, /* class */
180 RT_TEMPLATE, /* template */
181 RT_NAMESPACE, /* namespace */
182 RT_USING, /* using */
183 RT_ASM, /* asm */
184 RT_TRY, /* try */
185 RT_CATCH, /* catch */
186 RT_THROW, /* throw */
187 RT_LABEL, /* __label__ */
188 RT_AT_TRY, /* @try */
189 RT_AT_SYNCHRONIZED, /* @synchronized */
190 RT_AT_THROW, /* @throw */
191
192 RT_SELECT, /* selection-statement */
193 RT_INTERATION, /* iteration-statement */
194 RT_JUMP, /* jump-statement */
195 RT_CLASS_KEY, /* class-key */
196 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
197 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
198 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
199 RT_TRANSACTION_CANCEL /* __transaction_cancel */
200 } required_token;
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249
250 static void cp_parser_initial_pragma
251 (cp_token *);
252
253 static tree cp_literal_operator_id
254 (const char *);
255
256 static void cp_parser_cilk_simd
257 (cp_parser *, cp_token *);
258 static tree cp_parser_cilk_for
259 (cp_parser *, tree);
260 static bool cp_parser_omp_declare_reduction_exprs
261 (tree, cp_parser *);
262 static tree cp_parser_cilk_simd_vectorlength
263 (cp_parser *, tree, bool);
264
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
268
269 /* Variables. */
270
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
273
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
277
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
283
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
288 {
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
292
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
294
295 if (buffer == NULL)
296 return;
297
298 if (num == 0)
299 num = buffer->length ();
300
301 if (start_token == NULL)
302 start_token = buffer->address ();
303
304 if (start_token > buffer->address ())
305 {
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
308 }
309
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
313 {
314 if (token == start_token)
315 do_print = true;
316
317 if (!do_print)
318 continue;
319
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
323
324 cp_lexer_print_token (file, token);
325
326 if (token == curr_token)
327 fprintf (file, "]]");
328
329 switch (token->type)
330 {
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
337
338 default:
339 fputc (' ', file);
340 }
341 }
342
343 if (i == num && i < buffer->length ())
344 {
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
347 }
348
349 fprintf (file, "\n");
350 }
351
352
353 /* Dump all tokens in BUFFER to stderr. */
354
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
357 {
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
363 {
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 }
366
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
369 {
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
374 }
375
376
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
379
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 {
383 if (t)
384 {
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
387 }
388 }
389
390
391 /* Dump parser context C to FILE. */
392
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
395 {
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
400 }
401
402
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
407 {
408 unsigned i;
409 cp_parser_context *c;
410
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
413 {
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
416 }
417 }
418
419
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
424 {
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
427 }
428
429
430 /* Print an unparsed function entry UF to FILE. */
431
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
434 {
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
438
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
443 {
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
449 }
450
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
454 {
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
457 }
458 fprintf (file, "\n");
459
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
463 {
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
466 }
467 fprintf (file, "\n");
468 }
469
470
471 /* Print the stack of unparsed member functions S to FILE. */
472
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
476 {
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
479
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
482 {
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
485 }
486 }
487
488
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
494 {
495 cp_token *next_token, *first_token, *start_token;
496
497 if (file == NULL)
498 file = stderr;
499
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
507 }
508
509
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
512
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
515 {
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
519
520 if (file == NULL)
521 file = stderr;
522
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
596 }
597
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
600 {
601 cp_debug_parser (stderr, &ref);
602 }
603
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
606 {
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
611 }
612
613 /* Allocate memory for a new lexer object and return it. */
614
615 static cp_lexer *
616 cp_lexer_alloc (void)
617 {
618 cp_lexer *lexer;
619
620 c_common_no_more_pch ();
621
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
624
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
627
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
629
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
632
633 return lexer;
634 }
635
636
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
639
640 static cp_lexer *
641 cp_lexer_new_main (void)
642 {
643 cp_lexer *lexer;
644 cp_token token;
645
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
650
651 lexer = cp_lexer_alloc ();
652
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
655
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
658 {
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
661 }
662
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
669
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
673
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
676 }
677
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
680
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
683 {
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
687
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
692
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
694
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
697
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
700 }
701
702 /* Frees all resources associated with LEXER. */
703
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
706 {
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
710 }
711
712 /* Returns nonzero if debugging information should be output. */
713
714 static inline bool
715 cp_lexer_debugging_p (cp_lexer *lexer)
716 {
717 return lexer->debugging_p;
718 }
719
720
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
723 {
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
725
726 return lexer->next_token - previous_p;
727 }
728
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
731 {
732 return pos;
733 }
734
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
737 {
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
739 }
740
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
743 {
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
748 }
749
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
752 {
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
754
755 return cp_lexer_token_at (lexer, tp);
756 }
757
758 /* nonzero if we are presently saving tokens. */
759
760 static inline int
761 cp_lexer_saving_tokens (const cp_lexer* lexer)
762 {
763 return lexer->saved_tokens.length () != 0;
764 }
765
766 /* Store the next token from the preprocessor in *TOKEN. Return true
767 if we reach EOF. If LEXER is NULL, assume we are handling an
768 initial #pragma pch_preprocess, and thus want the lexer to return
769 processed strings. */
770
771 static void
772 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
773 {
774 static int is_extern_c = 0;
775
776 /* Get a new token from the preprocessor. */
777 token->type
778 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
779 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
780 token->keyword = RID_MAX;
781 token->pragma_kind = PRAGMA_NONE;
782 token->purged_p = false;
783 token->error_reported = false;
784
785 /* On some systems, some header files are surrounded by an
786 implicit extern "C" block. Set a flag in the token if it
787 comes from such a header. */
788 is_extern_c += pending_lang_change;
789 pending_lang_change = 0;
790 token->implicit_extern_c = is_extern_c > 0;
791
792 /* Check to see if this token is a keyword. */
793 if (token->type == CPP_NAME)
794 {
795 if (C_IS_RESERVED_WORD (token->u.value))
796 {
797 /* Mark this token as a keyword. */
798 token->type = CPP_KEYWORD;
799 /* Record which keyword. */
800 token->keyword = C_RID_CODE (token->u.value);
801 }
802 else
803 {
804 if (warn_cxx0x_compat
805 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
806 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
807 {
808 /* Warn about the C++0x keyword (but still treat it as
809 an identifier). */
810 warning (OPT_Wc__0x_compat,
811 "identifier %qE is a keyword in C++11",
812 token->u.value);
813
814 /* Clear out the C_RID_CODE so we don't warn about this
815 particular identifier-turned-keyword again. */
816 C_SET_RID_CODE (token->u.value, RID_MAX);
817 }
818
819 token->keyword = RID_MAX;
820 }
821 }
822 else if (token->type == CPP_AT_NAME)
823 {
824 /* This only happens in Objective-C++; it must be a keyword. */
825 token->type = CPP_KEYWORD;
826 switch (C_RID_CODE (token->u.value))
827 {
828 /* Replace 'class' with '@class', 'private' with '@private',
829 etc. This prevents confusion with the C++ keyword
830 'class', and makes the tokens consistent with other
831 Objective-C 'AT' keywords. For example '@class' is
832 reported as RID_AT_CLASS which is consistent with
833 '@synchronized', which is reported as
834 RID_AT_SYNCHRONIZED.
835 */
836 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
837 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
838 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
839 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
840 case RID_THROW: token->keyword = RID_AT_THROW; break;
841 case RID_TRY: token->keyword = RID_AT_TRY; break;
842 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
843 default: token->keyword = C_RID_CODE (token->u.value);
844 }
845 }
846 else if (token->type == CPP_PRAGMA)
847 {
848 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
849 token->pragma_kind = ((enum pragma_kind)
850 TREE_INT_CST_LOW (token->u.value));
851 token->u.value = NULL_TREE;
852 }
853 }
854
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
858 {
859 if (token->type != CPP_EOF)
860 {
861 input_location = token->location;
862 }
863 }
864
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
868 {
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
871 }
872
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
875
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
878 {
879 if (cp_lexer_debugging_p (lexer))
880 {
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
884 }
885 return lexer->next_token;
886 }
887
888 /* Return true if the next token has the indicated TYPE. */
889
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
892 {
893 return cp_lexer_peek_token (lexer)->type == type;
894 }
895
896 /* Return true if the next token does not have the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return !cp_lexer_next_token_is (lexer, type);
902 }
903
904 /* Return true if the next token is the indicated KEYWORD. */
905
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
908 {
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
910 }
911
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
914 {
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
916 }
917
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
920 {
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
922 }
923
924 /* Return true if the next token is not the indicated KEYWORD. */
925
926 static inline bool
927 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
928 {
929 return cp_lexer_peek_token (lexer)->keyword != keyword;
930 }
931
932 /* Return true if the next token is a keyword for a decl-specifier. */
933
934 static bool
935 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
936 {
937 cp_token *token;
938
939 token = cp_lexer_peek_token (lexer);
940 switch (token->keyword)
941 {
942 /* auto specifier: storage-class-specifier in C++,
943 simple-type-specifier in C++0x. */
944 case RID_AUTO:
945 /* Storage classes. */
946 case RID_REGISTER:
947 case RID_STATIC:
948 case RID_EXTERN:
949 case RID_MUTABLE:
950 case RID_THREAD:
951 /* Elaborated type specifiers. */
952 case RID_ENUM:
953 case RID_CLASS:
954 case RID_STRUCT:
955 case RID_UNION:
956 case RID_TYPENAME:
957 /* Simple type specifiers. */
958 case RID_CHAR:
959 case RID_CHAR16:
960 case RID_CHAR32:
961 case RID_WCHAR:
962 case RID_BOOL:
963 case RID_SHORT:
964 case RID_INT:
965 case RID_LONG:
966 case RID_SIGNED:
967 case RID_UNSIGNED:
968 case RID_FLOAT:
969 case RID_DOUBLE:
970 case RID_VOID:
971 /* GNU extensions. */
972 case RID_ATTRIBUTE:
973 case RID_TYPEOF:
974 /* C++0x extensions. */
975 case RID_DECLTYPE:
976 case RID_UNDERLYING_TYPE:
977 return true;
978
979 default:
980 if (token->keyword >= RID_FIRST_INT_N
981 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
982 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
983 return true;
984 return false;
985 }
986 }
987
988 /* Returns TRUE iff the token T begins a decltype type. */
989
990 static bool
991 token_is_decltype (cp_token *t)
992 {
993 return (t->keyword == RID_DECLTYPE
994 || t->type == CPP_DECLTYPE);
995 }
996
997 /* Returns TRUE iff the next token begins a decltype type. */
998
999 static bool
1000 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1001 {
1002 cp_token *t = cp_lexer_peek_token (lexer);
1003 return token_is_decltype (t);
1004 }
1005
1006 /* Return a pointer to the Nth token in the token stream. If N is 1,
1007 then this is precisely equivalent to cp_lexer_peek_token (except
1008 that it is not inline). One would like to disallow that case, but
1009 there is one case (cp_parser_nth_token_starts_template_id) where
1010 the caller passes a variable for N and it might be 1. */
1011
1012 static cp_token *
1013 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1014 {
1015 cp_token *token;
1016
1017 /* N is 1-based, not zero-based. */
1018 gcc_assert (n > 0);
1019
1020 if (cp_lexer_debugging_p (lexer))
1021 fprintf (cp_lexer_debug_stream,
1022 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1023
1024 --n;
1025 token = lexer->next_token;
1026 gcc_assert (!n || token != &eof_token);
1027 while (n != 0)
1028 {
1029 ++token;
1030 if (token == lexer->last_token)
1031 {
1032 token = &eof_token;
1033 break;
1034 }
1035
1036 if (!token->purged_p)
1037 --n;
1038 }
1039
1040 if (cp_lexer_debugging_p (lexer))
1041 {
1042 cp_lexer_print_token (cp_lexer_debug_stream, token);
1043 putc ('\n', cp_lexer_debug_stream);
1044 }
1045
1046 return token;
1047 }
1048
1049 /* Return the next token, and advance the lexer's next_token pointer
1050 to point to the next non-purged token. */
1051
1052 static cp_token *
1053 cp_lexer_consume_token (cp_lexer* lexer)
1054 {
1055 cp_token *token = lexer->next_token;
1056
1057 gcc_assert (token != &eof_token);
1058 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1059
1060 do
1061 {
1062 lexer->next_token++;
1063 if (lexer->next_token == lexer->last_token)
1064 {
1065 lexer->next_token = &eof_token;
1066 break;
1067 }
1068
1069 }
1070 while (lexer->next_token->purged_p);
1071
1072 cp_lexer_set_source_position_from_token (token);
1073
1074 /* Provide debugging output. */
1075 if (cp_lexer_debugging_p (lexer))
1076 {
1077 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1078 cp_lexer_print_token (cp_lexer_debug_stream, token);
1079 putc ('\n', cp_lexer_debug_stream);
1080 }
1081
1082 return token;
1083 }
1084
1085 /* Permanently remove the next token from the token stream, and
1086 advance the next_token pointer to refer to the next non-purged
1087 token. */
1088
1089 static void
1090 cp_lexer_purge_token (cp_lexer *lexer)
1091 {
1092 cp_token *tok = lexer->next_token;
1093
1094 gcc_assert (tok != &eof_token);
1095 tok->purged_p = true;
1096 tok->location = UNKNOWN_LOCATION;
1097 tok->u.value = NULL_TREE;
1098 tok->keyword = RID_MAX;
1099
1100 do
1101 {
1102 tok++;
1103 if (tok == lexer->last_token)
1104 {
1105 tok = &eof_token;
1106 break;
1107 }
1108 }
1109 while (tok->purged_p);
1110 lexer->next_token = tok;
1111 }
1112
1113 /* Permanently remove all tokens after TOK, up to, but not
1114 including, the token that will be returned next by
1115 cp_lexer_peek_token. */
1116
1117 static void
1118 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1119 {
1120 cp_token *peek = lexer->next_token;
1121
1122 if (peek == &eof_token)
1123 peek = lexer->last_token;
1124
1125 gcc_assert (tok < peek);
1126
1127 for ( tok += 1; tok != peek; tok += 1)
1128 {
1129 tok->purged_p = true;
1130 tok->location = UNKNOWN_LOCATION;
1131 tok->u.value = NULL_TREE;
1132 tok->keyword = RID_MAX;
1133 }
1134 }
1135
1136 /* Begin saving tokens. All tokens consumed after this point will be
1137 preserved. */
1138
1139 static void
1140 cp_lexer_save_tokens (cp_lexer* lexer)
1141 {
1142 /* Provide debugging output. */
1143 if (cp_lexer_debugging_p (lexer))
1144 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1145
1146 lexer->saved_tokens.safe_push (lexer->next_token);
1147 }
1148
1149 /* Commit to the portion of the token stream most recently saved. */
1150
1151 static void
1152 cp_lexer_commit_tokens (cp_lexer* lexer)
1153 {
1154 /* Provide debugging output. */
1155 if (cp_lexer_debugging_p (lexer))
1156 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1157
1158 lexer->saved_tokens.pop ();
1159 }
1160
1161 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1162 to the token stream. Stop saving tokens. */
1163
1164 static void
1165 cp_lexer_rollback_tokens (cp_lexer* lexer)
1166 {
1167 /* Provide debugging output. */
1168 if (cp_lexer_debugging_p (lexer))
1169 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1170
1171 lexer->next_token = lexer->saved_tokens.pop ();
1172 }
1173
1174 /* RAII wrapper around the above functions, with sanity checking. Creating
1175 a variable saves tokens, which are committed when the variable is
1176 destroyed unless they are explicitly rolled back by calling the rollback
1177 member function. */
1178
1179 struct saved_token_sentinel
1180 {
1181 cp_lexer *lexer;
1182 unsigned len;
1183 bool commit;
1184 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1185 {
1186 len = lexer->saved_tokens.length ();
1187 cp_lexer_save_tokens (lexer);
1188 }
1189 void rollback ()
1190 {
1191 cp_lexer_rollback_tokens (lexer);
1192 commit = false;
1193 }
1194 ~saved_token_sentinel()
1195 {
1196 if (commit)
1197 cp_lexer_commit_tokens (lexer);
1198 gcc_assert (lexer->saved_tokens.length () == len);
1199 }
1200 };
1201
1202 /* Print a representation of the TOKEN on the STREAM. */
1203
1204 static void
1205 cp_lexer_print_token (FILE * stream, cp_token *token)
1206 {
1207 /* We don't use cpp_type2name here because the parser defines
1208 a few tokens of its own. */
1209 static const char *const token_names[] = {
1210 /* cpplib-defined token types */
1211 #define OP(e, s) #e,
1212 #define TK(e, s) #e,
1213 TTYPE_TABLE
1214 #undef OP
1215 #undef TK
1216 /* C++ parser token types - see "Manifest constants", above. */
1217 "KEYWORD",
1218 "TEMPLATE_ID",
1219 "NESTED_NAME_SPECIFIER",
1220 };
1221
1222 /* For some tokens, print the associated data. */
1223 switch (token->type)
1224 {
1225 case CPP_KEYWORD:
1226 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1227 For example, `struct' is mapped to an INTEGER_CST. */
1228 if (!identifier_p (token->u.value))
1229 break;
1230 /* else fall through */
1231 case CPP_NAME:
1232 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1233 break;
1234
1235 case CPP_STRING:
1236 case CPP_STRING16:
1237 case CPP_STRING32:
1238 case CPP_WSTRING:
1239 case CPP_UTF8STRING:
1240 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1241 break;
1242
1243 case CPP_NUMBER:
1244 print_generic_expr (stream, token->u.value, 0);
1245 break;
1246
1247 default:
1248 /* If we have a name for the token, print it out. Otherwise, we
1249 simply give the numeric code. */
1250 if (token->type < ARRAY_SIZE(token_names))
1251 fputs (token_names[token->type], stream);
1252 else
1253 fprintf (stream, "[%d]", token->type);
1254 break;
1255 }
1256 }
1257
1258 DEBUG_FUNCTION void
1259 debug (cp_token &ref)
1260 {
1261 cp_lexer_print_token (stderr, &ref);
1262 fprintf (stderr, "\n");
1263 }
1264
1265 DEBUG_FUNCTION void
1266 debug (cp_token *ptr)
1267 {
1268 if (ptr)
1269 debug (*ptr);
1270 else
1271 fprintf (stderr, "<nil>\n");
1272 }
1273
1274
1275 /* Start emitting debugging information. */
1276
1277 static void
1278 cp_lexer_start_debugging (cp_lexer* lexer)
1279 {
1280 lexer->debugging_p = true;
1281 cp_lexer_debug_stream = stderr;
1282 }
1283
1284 /* Stop emitting debugging information. */
1285
1286 static void
1287 cp_lexer_stop_debugging (cp_lexer* lexer)
1288 {
1289 lexer->debugging_p = false;
1290 cp_lexer_debug_stream = NULL;
1291 }
1292
1293 /* Create a new cp_token_cache, representing a range of tokens. */
1294
1295 static cp_token_cache *
1296 cp_token_cache_new (cp_token *first, cp_token *last)
1297 {
1298 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1299 cache->first = first;
1300 cache->last = last;
1301 return cache;
1302 }
1303
1304 /* Diagnose if #pragma omp declare simd isn't followed immediately
1305 by function declaration or definition. */
1306
1307 static inline void
1308 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1309 {
1310 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1311 {
1312 error ("%<#pragma omp declare simd%> not immediately followed by "
1313 "function declaration or definition");
1314 parser->omp_declare_simd = NULL;
1315 }
1316 }
1317
1318 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1319 and put that into "omp declare simd" attribute. */
1320
1321 static inline void
1322 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1323 {
1324 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1325 {
1326 if (fndecl == error_mark_node)
1327 {
1328 parser->omp_declare_simd = NULL;
1329 return;
1330 }
1331 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1332 {
1333 cp_ensure_no_omp_declare_simd (parser);
1334 return;
1335 }
1336 }
1337 }
1338 \f
1339 /* Decl-specifiers. */
1340
1341 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1342
1343 static void
1344 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1345 {
1346 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1347 }
1348
1349 /* Declarators. */
1350
1351 /* Nothing other than the parser should be creating declarators;
1352 declarators are a semi-syntactic representation of C++ entities.
1353 Other parts of the front end that need to create entities (like
1354 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1355
1356 static cp_declarator *make_call_declarator
1357 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1358 static cp_declarator *make_array_declarator
1359 (cp_declarator *, tree);
1360 static cp_declarator *make_pointer_declarator
1361 (cp_cv_quals, cp_declarator *, tree);
1362 static cp_declarator *make_reference_declarator
1363 (cp_cv_quals, cp_declarator *, bool, tree);
1364 static cp_parameter_declarator *make_parameter_declarator
1365 (cp_decl_specifier_seq *, cp_declarator *, tree);
1366 static cp_declarator *make_ptrmem_declarator
1367 (cp_cv_quals, tree, cp_declarator *, tree);
1368
1369 /* An erroneous declarator. */
1370 static cp_declarator *cp_error_declarator;
1371
1372 /* The obstack on which declarators and related data structures are
1373 allocated. */
1374 static struct obstack declarator_obstack;
1375
1376 /* Alloc BYTES from the declarator memory pool. */
1377
1378 static inline void *
1379 alloc_declarator (size_t bytes)
1380 {
1381 return obstack_alloc (&declarator_obstack, bytes);
1382 }
1383
1384 /* Allocate a declarator of the indicated KIND. Clear fields that are
1385 common to all declarators. */
1386
1387 static cp_declarator *
1388 make_declarator (cp_declarator_kind kind)
1389 {
1390 cp_declarator *declarator;
1391
1392 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1393 declarator->kind = kind;
1394 declarator->attributes = NULL_TREE;
1395 declarator->std_attributes = NULL_TREE;
1396 declarator->declarator = NULL;
1397 declarator->parameter_pack_p = false;
1398 declarator->id_loc = UNKNOWN_LOCATION;
1399
1400 return declarator;
1401 }
1402
1403 /* Make a declarator for a generalized identifier. If
1404 QUALIFYING_SCOPE is non-NULL, the identifier is
1405 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1406 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1407 is, if any. */
1408
1409 static cp_declarator *
1410 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1411 special_function_kind sfk)
1412 {
1413 cp_declarator *declarator;
1414
1415 /* It is valid to write:
1416
1417 class C { void f(); };
1418 typedef C D;
1419 void D::f();
1420
1421 The standard is not clear about whether `typedef const C D' is
1422 legal; as of 2002-09-15 the committee is considering that
1423 question. EDG 3.0 allows that syntax. Therefore, we do as
1424 well. */
1425 if (qualifying_scope && TYPE_P (qualifying_scope))
1426 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1427
1428 gcc_assert (identifier_p (unqualified_name)
1429 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1430 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1431
1432 declarator = make_declarator (cdk_id);
1433 declarator->u.id.qualifying_scope = qualifying_scope;
1434 declarator->u.id.unqualified_name = unqualified_name;
1435 declarator->u.id.sfk = sfk;
1436
1437 return declarator;
1438 }
1439
1440 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1441 of modifiers such as const or volatile to apply to the pointer
1442 type, represented as identifiers. ATTRIBUTES represent the attributes that
1443 appertain to the pointer or reference. */
1444
1445 cp_declarator *
1446 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1447 tree attributes)
1448 {
1449 cp_declarator *declarator;
1450
1451 declarator = make_declarator (cdk_pointer);
1452 declarator->declarator = target;
1453 declarator->u.pointer.qualifiers = cv_qualifiers;
1454 declarator->u.pointer.class_type = NULL_TREE;
1455 if (target)
1456 {
1457 declarator->id_loc = target->id_loc;
1458 declarator->parameter_pack_p = target->parameter_pack_p;
1459 target->parameter_pack_p = false;
1460 }
1461 else
1462 declarator->parameter_pack_p = false;
1463
1464 declarator->std_attributes = attributes;
1465
1466 return declarator;
1467 }
1468
1469 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1470 represent the attributes that appertain to the pointer or
1471 reference. */
1472
1473 cp_declarator *
1474 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1475 bool rvalue_ref, tree attributes)
1476 {
1477 cp_declarator *declarator;
1478
1479 declarator = make_declarator (cdk_reference);
1480 declarator->declarator = target;
1481 declarator->u.reference.qualifiers = cv_qualifiers;
1482 declarator->u.reference.rvalue_ref = rvalue_ref;
1483 if (target)
1484 {
1485 declarator->id_loc = target->id_loc;
1486 declarator->parameter_pack_p = target->parameter_pack_p;
1487 target->parameter_pack_p = false;
1488 }
1489 else
1490 declarator->parameter_pack_p = false;
1491
1492 declarator->std_attributes = attributes;
1493
1494 return declarator;
1495 }
1496
1497 /* Like make_pointer_declarator -- but for a pointer to a non-static
1498 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1499 appertain to the pointer or reference. */
1500
1501 cp_declarator *
1502 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1503 cp_declarator *pointee,
1504 tree attributes)
1505 {
1506 cp_declarator *declarator;
1507
1508 declarator = make_declarator (cdk_ptrmem);
1509 declarator->declarator = pointee;
1510 declarator->u.pointer.qualifiers = cv_qualifiers;
1511 declarator->u.pointer.class_type = class_type;
1512
1513 if (pointee)
1514 {
1515 declarator->parameter_pack_p = pointee->parameter_pack_p;
1516 pointee->parameter_pack_p = false;
1517 }
1518 else
1519 declarator->parameter_pack_p = false;
1520
1521 declarator->std_attributes = attributes;
1522
1523 return declarator;
1524 }
1525
1526 /* Make a declarator for the function given by TARGET, with the
1527 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1528 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1529 indicates what exceptions can be thrown. */
1530
1531 cp_declarator *
1532 make_call_declarator (cp_declarator *target,
1533 tree parms,
1534 cp_cv_quals cv_qualifiers,
1535 cp_virt_specifiers virt_specifiers,
1536 cp_ref_qualifier ref_qualifier,
1537 tree exception_specification,
1538 tree late_return_type)
1539 {
1540 cp_declarator *declarator;
1541
1542 declarator = make_declarator (cdk_function);
1543 declarator->declarator = target;
1544 declarator->u.function.parameters = parms;
1545 declarator->u.function.qualifiers = cv_qualifiers;
1546 declarator->u.function.virt_specifiers = virt_specifiers;
1547 declarator->u.function.ref_qualifier = ref_qualifier;
1548 declarator->u.function.exception_specification = exception_specification;
1549 declarator->u.function.late_return_type = late_return_type;
1550 if (target)
1551 {
1552 declarator->id_loc = target->id_loc;
1553 declarator->parameter_pack_p = target->parameter_pack_p;
1554 target->parameter_pack_p = false;
1555 }
1556 else
1557 declarator->parameter_pack_p = false;
1558
1559 return declarator;
1560 }
1561
1562 /* Make a declarator for an array of BOUNDS elements, each of which is
1563 defined by ELEMENT. */
1564
1565 cp_declarator *
1566 make_array_declarator (cp_declarator *element, tree bounds)
1567 {
1568 cp_declarator *declarator;
1569
1570 declarator = make_declarator (cdk_array);
1571 declarator->declarator = element;
1572 declarator->u.array.bounds = bounds;
1573 if (element)
1574 {
1575 declarator->id_loc = element->id_loc;
1576 declarator->parameter_pack_p = element->parameter_pack_p;
1577 element->parameter_pack_p = false;
1578 }
1579 else
1580 declarator->parameter_pack_p = false;
1581
1582 return declarator;
1583 }
1584
1585 /* Determine whether the declarator we've seen so far can be a
1586 parameter pack, when followed by an ellipsis. */
1587 static bool
1588 declarator_can_be_parameter_pack (cp_declarator *declarator)
1589 {
1590 /* Search for a declarator name, or any other declarator that goes
1591 after the point where the ellipsis could appear in a parameter
1592 pack. If we find any of these, then this declarator can not be
1593 made into a parameter pack. */
1594 bool found = false;
1595 while (declarator && !found)
1596 {
1597 switch ((int)declarator->kind)
1598 {
1599 case cdk_id:
1600 case cdk_array:
1601 found = true;
1602 break;
1603
1604 case cdk_error:
1605 return true;
1606
1607 default:
1608 declarator = declarator->declarator;
1609 break;
1610 }
1611 }
1612
1613 return !found;
1614 }
1615
1616 cp_parameter_declarator *no_parameters;
1617
1618 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1619 DECLARATOR and DEFAULT_ARGUMENT. */
1620
1621 cp_parameter_declarator *
1622 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1623 cp_declarator *declarator,
1624 tree default_argument)
1625 {
1626 cp_parameter_declarator *parameter;
1627
1628 parameter = ((cp_parameter_declarator *)
1629 alloc_declarator (sizeof (cp_parameter_declarator)));
1630 parameter->next = NULL;
1631 if (decl_specifiers)
1632 parameter->decl_specifiers = *decl_specifiers;
1633 else
1634 clear_decl_specs (&parameter->decl_specifiers);
1635 parameter->declarator = declarator;
1636 parameter->default_argument = default_argument;
1637 parameter->ellipsis_p = false;
1638
1639 return parameter;
1640 }
1641
1642 /* Returns true iff DECLARATOR is a declaration for a function. */
1643
1644 static bool
1645 function_declarator_p (const cp_declarator *declarator)
1646 {
1647 while (declarator)
1648 {
1649 if (declarator->kind == cdk_function
1650 && declarator->declarator->kind == cdk_id)
1651 return true;
1652 if (declarator->kind == cdk_id
1653 || declarator->kind == cdk_error)
1654 return false;
1655 declarator = declarator->declarator;
1656 }
1657 return false;
1658 }
1659
1660 /* The parser. */
1661
1662 /* Overview
1663 --------
1664
1665 A cp_parser parses the token stream as specified by the C++
1666 grammar. Its job is purely parsing, not semantic analysis. For
1667 example, the parser breaks the token stream into declarators,
1668 expressions, statements, and other similar syntactic constructs.
1669 It does not check that the types of the expressions on either side
1670 of an assignment-statement are compatible, or that a function is
1671 not declared with a parameter of type `void'.
1672
1673 The parser invokes routines elsewhere in the compiler to perform
1674 semantic analysis and to build up the abstract syntax tree for the
1675 code processed.
1676
1677 The parser (and the template instantiation code, which is, in a
1678 way, a close relative of parsing) are the only parts of the
1679 compiler that should be calling push_scope and pop_scope, or
1680 related functions. The parser (and template instantiation code)
1681 keeps track of what scope is presently active; everything else
1682 should simply honor that. (The code that generates static
1683 initializers may also need to set the scope, in order to check
1684 access control correctly when emitting the initializers.)
1685
1686 Methodology
1687 -----------
1688
1689 The parser is of the standard recursive-descent variety. Upcoming
1690 tokens in the token stream are examined in order to determine which
1691 production to use when parsing a non-terminal. Some C++ constructs
1692 require arbitrary look ahead to disambiguate. For example, it is
1693 impossible, in the general case, to tell whether a statement is an
1694 expression or declaration without scanning the entire statement.
1695 Therefore, the parser is capable of "parsing tentatively." When the
1696 parser is not sure what construct comes next, it enters this mode.
1697 Then, while we attempt to parse the construct, the parser queues up
1698 error messages, rather than issuing them immediately, and saves the
1699 tokens it consumes. If the construct is parsed successfully, the
1700 parser "commits", i.e., it issues any queued error messages and
1701 the tokens that were being preserved are permanently discarded.
1702 If, however, the construct is not parsed successfully, the parser
1703 rolls back its state completely so that it can resume parsing using
1704 a different alternative.
1705
1706 Future Improvements
1707 -------------------
1708
1709 The performance of the parser could probably be improved substantially.
1710 We could often eliminate the need to parse tentatively by looking ahead
1711 a little bit. In some places, this approach might not entirely eliminate
1712 the need to parse tentatively, but it might still speed up the average
1713 case. */
1714
1715 /* Flags that are passed to some parsing functions. These values can
1716 be bitwise-ored together. */
1717
1718 enum
1719 {
1720 /* No flags. */
1721 CP_PARSER_FLAGS_NONE = 0x0,
1722 /* The construct is optional. If it is not present, then no error
1723 should be issued. */
1724 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1725 /* When parsing a type-specifier, treat user-defined type-names
1726 as non-type identifiers. */
1727 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1728 /* When parsing a type-specifier, do not try to parse a class-specifier
1729 or enum-specifier. */
1730 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1731 /* When parsing a decl-specifier-seq, only allow type-specifier or
1732 constexpr. */
1733 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1734 };
1735
1736 /* This type is used for parameters and variables which hold
1737 combinations of the above flags. */
1738 typedef int cp_parser_flags;
1739
1740 /* The different kinds of declarators we want to parse. */
1741
1742 typedef enum cp_parser_declarator_kind
1743 {
1744 /* We want an abstract declarator. */
1745 CP_PARSER_DECLARATOR_ABSTRACT,
1746 /* We want a named declarator. */
1747 CP_PARSER_DECLARATOR_NAMED,
1748 /* We don't mind, but the name must be an unqualified-id. */
1749 CP_PARSER_DECLARATOR_EITHER
1750 } cp_parser_declarator_kind;
1751
1752 /* The precedence values used to parse binary expressions. The minimum value
1753 of PREC must be 1, because zero is reserved to quickly discriminate
1754 binary operators from other tokens. */
1755
1756 enum cp_parser_prec
1757 {
1758 PREC_NOT_OPERATOR,
1759 PREC_LOGICAL_OR_EXPRESSION,
1760 PREC_LOGICAL_AND_EXPRESSION,
1761 PREC_INCLUSIVE_OR_EXPRESSION,
1762 PREC_EXCLUSIVE_OR_EXPRESSION,
1763 PREC_AND_EXPRESSION,
1764 PREC_EQUALITY_EXPRESSION,
1765 PREC_RELATIONAL_EXPRESSION,
1766 PREC_SHIFT_EXPRESSION,
1767 PREC_ADDITIVE_EXPRESSION,
1768 PREC_MULTIPLICATIVE_EXPRESSION,
1769 PREC_PM_EXPRESSION,
1770 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1771 };
1772
1773 /* A mapping from a token type to a corresponding tree node type, with a
1774 precedence value. */
1775
1776 typedef struct cp_parser_binary_operations_map_node
1777 {
1778 /* The token type. */
1779 enum cpp_ttype token_type;
1780 /* The corresponding tree code. */
1781 enum tree_code tree_type;
1782 /* The precedence of this operator. */
1783 enum cp_parser_prec prec;
1784 } cp_parser_binary_operations_map_node;
1785
1786 typedef struct cp_parser_expression_stack_entry
1787 {
1788 /* Left hand side of the binary operation we are currently
1789 parsing. */
1790 tree lhs;
1791 /* Original tree code for left hand side, if it was a binary
1792 expression itself (used for -Wparentheses). */
1793 enum tree_code lhs_type;
1794 /* Tree code for the binary operation we are parsing. */
1795 enum tree_code tree_type;
1796 /* Precedence of the binary operation we are parsing. */
1797 enum cp_parser_prec prec;
1798 /* Location of the binary operation we are parsing. */
1799 location_t loc;
1800 } cp_parser_expression_stack_entry;
1801
1802 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1803 entries because precedence levels on the stack are monotonically
1804 increasing. */
1805 typedef struct cp_parser_expression_stack_entry
1806 cp_parser_expression_stack[NUM_PREC_VALUES];
1807
1808 /* Prototypes. */
1809
1810 /* Constructors and destructors. */
1811
1812 static cp_parser_context *cp_parser_context_new
1813 (cp_parser_context *);
1814
1815 /* Class variables. */
1816
1817 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1818
1819 /* The operator-precedence table used by cp_parser_binary_expression.
1820 Transformed into an associative array (binops_by_token) by
1821 cp_parser_new. */
1822
1823 static const cp_parser_binary_operations_map_node binops[] = {
1824 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1825 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1826
1827 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1828 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1829 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1830
1831 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1832 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1833
1834 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1835 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1836
1837 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1838 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1839 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1840 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1841
1842 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1843 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1844
1845 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1846
1847 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1848
1849 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1850
1851 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1852
1853 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1854 };
1855
1856 /* The same as binops, but initialized by cp_parser_new so that
1857 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1858 for speed. */
1859 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1860
1861 /* Constructors and destructors. */
1862
1863 /* Construct a new context. The context below this one on the stack
1864 is given by NEXT. */
1865
1866 static cp_parser_context *
1867 cp_parser_context_new (cp_parser_context* next)
1868 {
1869 cp_parser_context *context;
1870
1871 /* Allocate the storage. */
1872 if (cp_parser_context_free_list != NULL)
1873 {
1874 /* Pull the first entry from the free list. */
1875 context = cp_parser_context_free_list;
1876 cp_parser_context_free_list = context->next;
1877 memset (context, 0, sizeof (*context));
1878 }
1879 else
1880 context = ggc_cleared_alloc<cp_parser_context> ();
1881
1882 /* No errors have occurred yet in this context. */
1883 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1884 /* If this is not the bottommost context, copy information that we
1885 need from the previous context. */
1886 if (next)
1887 {
1888 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1889 expression, then we are parsing one in this context, too. */
1890 context->object_type = next->object_type;
1891 /* Thread the stack. */
1892 context->next = next;
1893 }
1894
1895 return context;
1896 }
1897
1898 /* Managing the unparsed function queues. */
1899
1900 #define unparsed_funs_with_default_args \
1901 parser->unparsed_queues->last ().funs_with_default_args
1902 #define unparsed_funs_with_definitions \
1903 parser->unparsed_queues->last ().funs_with_definitions
1904 #define unparsed_nsdmis \
1905 parser->unparsed_queues->last ().nsdmis
1906 #define unparsed_classes \
1907 parser->unparsed_queues->last ().classes
1908
1909 static void
1910 push_unparsed_function_queues (cp_parser *parser)
1911 {
1912 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1913 vec_safe_push (parser->unparsed_queues, e);
1914 }
1915
1916 static void
1917 pop_unparsed_function_queues (cp_parser *parser)
1918 {
1919 release_tree_vector (unparsed_funs_with_definitions);
1920 parser->unparsed_queues->pop ();
1921 }
1922
1923 /* Prototypes. */
1924
1925 /* Constructors and destructors. */
1926
1927 static cp_parser *cp_parser_new
1928 (void);
1929
1930 /* Routines to parse various constructs.
1931
1932 Those that return `tree' will return the error_mark_node (rather
1933 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1934 Sometimes, they will return an ordinary node if error-recovery was
1935 attempted, even though a parse error occurred. So, to check
1936 whether or not a parse error occurred, you should always use
1937 cp_parser_error_occurred. If the construct is optional (indicated
1938 either by an `_opt' in the name of the function that does the
1939 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1940 the construct is not present. */
1941
1942 /* Lexical conventions [gram.lex] */
1943
1944 static tree cp_parser_identifier
1945 (cp_parser *);
1946 static tree cp_parser_string_literal
1947 (cp_parser *, bool, bool, bool);
1948 static tree cp_parser_userdef_char_literal
1949 (cp_parser *);
1950 static tree cp_parser_userdef_string_literal
1951 (tree);
1952 static tree cp_parser_userdef_numeric_literal
1953 (cp_parser *);
1954
1955 /* Basic concepts [gram.basic] */
1956
1957 static bool cp_parser_translation_unit
1958 (cp_parser *);
1959
1960 /* Expressions [gram.expr] */
1961
1962 static tree cp_parser_primary_expression
1963 (cp_parser *, bool, bool, bool, cp_id_kind *);
1964 static tree cp_parser_id_expression
1965 (cp_parser *, bool, bool, bool *, bool, bool);
1966 static tree cp_parser_unqualified_id
1967 (cp_parser *, bool, bool, bool, bool);
1968 static tree cp_parser_nested_name_specifier_opt
1969 (cp_parser *, bool, bool, bool, bool);
1970 static tree cp_parser_nested_name_specifier
1971 (cp_parser *, bool, bool, bool, bool);
1972 static tree cp_parser_qualifying_entity
1973 (cp_parser *, bool, bool, bool, bool, bool);
1974 static tree cp_parser_postfix_expression
1975 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1976 static tree cp_parser_postfix_open_square_expression
1977 (cp_parser *, tree, bool, bool);
1978 static tree cp_parser_postfix_dot_deref_expression
1979 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1980 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1981 (cp_parser *, int, bool, bool, bool *, bool = false);
1982 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1983 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1984 static void cp_parser_pseudo_destructor_name
1985 (cp_parser *, tree, tree *, tree *);
1986 static tree cp_parser_unary_expression
1987 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1988 static enum tree_code cp_parser_unary_operator
1989 (cp_token *);
1990 static tree cp_parser_new_expression
1991 (cp_parser *);
1992 static vec<tree, va_gc> *cp_parser_new_placement
1993 (cp_parser *);
1994 static tree cp_parser_new_type_id
1995 (cp_parser *, tree *);
1996 static cp_declarator *cp_parser_new_declarator_opt
1997 (cp_parser *);
1998 static cp_declarator *cp_parser_direct_new_declarator
1999 (cp_parser *);
2000 static vec<tree, va_gc> *cp_parser_new_initializer
2001 (cp_parser *);
2002 static tree cp_parser_delete_expression
2003 (cp_parser *);
2004 static tree cp_parser_cast_expression
2005 (cp_parser *, bool, bool, bool, cp_id_kind *);
2006 static tree cp_parser_binary_expression
2007 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2008 static tree cp_parser_question_colon_clause
2009 (cp_parser *, tree);
2010 static tree cp_parser_assignment_expression
2011 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2012 static enum tree_code cp_parser_assignment_operator_opt
2013 (cp_parser *);
2014 static tree cp_parser_expression
2015 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2016 static tree cp_parser_constant_expression
2017 (cp_parser *, bool = false, bool * = NULL);
2018 static tree cp_parser_builtin_offsetof
2019 (cp_parser *);
2020 static tree cp_parser_lambda_expression
2021 (cp_parser *);
2022 static void cp_parser_lambda_introducer
2023 (cp_parser *, tree);
2024 static bool cp_parser_lambda_declarator_opt
2025 (cp_parser *, tree);
2026 static void cp_parser_lambda_body
2027 (cp_parser *, tree);
2028
2029 /* Statements [gram.stmt.stmt] */
2030
2031 static void cp_parser_statement
2032 (cp_parser *, tree, bool, bool *);
2033 static void cp_parser_label_for_labeled_statement
2034 (cp_parser *, tree);
2035 static tree cp_parser_expression_statement
2036 (cp_parser *, tree);
2037 static tree cp_parser_compound_statement
2038 (cp_parser *, tree, bool, bool);
2039 static void cp_parser_statement_seq_opt
2040 (cp_parser *, tree);
2041 static tree cp_parser_selection_statement
2042 (cp_parser *, bool *);
2043 static tree cp_parser_condition
2044 (cp_parser *);
2045 static tree cp_parser_iteration_statement
2046 (cp_parser *, bool);
2047 static bool cp_parser_for_init_statement
2048 (cp_parser *, tree *decl);
2049 static tree cp_parser_for
2050 (cp_parser *, bool);
2051 static tree cp_parser_c_for
2052 (cp_parser *, tree, tree, bool);
2053 static tree cp_parser_range_for
2054 (cp_parser *, tree, tree, tree, bool);
2055 static void do_range_for_auto_deduction
2056 (tree, tree);
2057 static tree cp_parser_perform_range_for_lookup
2058 (tree, tree *, tree *);
2059 static tree cp_parser_range_for_member_function
2060 (tree, tree);
2061 static tree cp_parser_jump_statement
2062 (cp_parser *);
2063 static void cp_parser_declaration_statement
2064 (cp_parser *);
2065
2066 static tree cp_parser_implicitly_scoped_statement
2067 (cp_parser *, bool *);
2068 static void cp_parser_already_scoped_statement
2069 (cp_parser *);
2070
2071 /* Declarations [gram.dcl.dcl] */
2072
2073 static void cp_parser_declaration_seq_opt
2074 (cp_parser *);
2075 static void cp_parser_declaration
2076 (cp_parser *);
2077 static void cp_parser_block_declaration
2078 (cp_parser *, bool);
2079 static void cp_parser_simple_declaration
2080 (cp_parser *, bool, tree *);
2081 static void cp_parser_decl_specifier_seq
2082 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2083 static tree cp_parser_storage_class_specifier_opt
2084 (cp_parser *);
2085 static tree cp_parser_function_specifier_opt
2086 (cp_parser *, cp_decl_specifier_seq *);
2087 static tree cp_parser_type_specifier
2088 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2089 int *, bool *);
2090 static tree cp_parser_simple_type_specifier
2091 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2092 static tree cp_parser_type_name
2093 (cp_parser *);
2094 static tree cp_parser_nonclass_name
2095 (cp_parser* parser);
2096 static tree cp_parser_elaborated_type_specifier
2097 (cp_parser *, bool, bool);
2098 static tree cp_parser_enum_specifier
2099 (cp_parser *);
2100 static void cp_parser_enumerator_list
2101 (cp_parser *, tree);
2102 static void cp_parser_enumerator_definition
2103 (cp_parser *, tree);
2104 static tree cp_parser_namespace_name
2105 (cp_parser *);
2106 static void cp_parser_namespace_definition
2107 (cp_parser *);
2108 static void cp_parser_namespace_body
2109 (cp_parser *);
2110 static tree cp_parser_qualified_namespace_specifier
2111 (cp_parser *);
2112 static void cp_parser_namespace_alias_definition
2113 (cp_parser *);
2114 static bool cp_parser_using_declaration
2115 (cp_parser *, bool);
2116 static void cp_parser_using_directive
2117 (cp_parser *);
2118 static tree cp_parser_alias_declaration
2119 (cp_parser *);
2120 static void cp_parser_asm_definition
2121 (cp_parser *);
2122 static void cp_parser_linkage_specification
2123 (cp_parser *);
2124 static void cp_parser_static_assert
2125 (cp_parser *, bool);
2126 static tree cp_parser_decltype
2127 (cp_parser *);
2128
2129 /* Declarators [gram.dcl.decl] */
2130
2131 static tree cp_parser_init_declarator
2132 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2133 bool, bool, int, bool *, tree *, location_t *);
2134 static cp_declarator *cp_parser_declarator
2135 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2136 static cp_declarator *cp_parser_direct_declarator
2137 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2138 static enum tree_code cp_parser_ptr_operator
2139 (cp_parser *, tree *, cp_cv_quals *, tree *);
2140 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2141 (cp_parser *);
2142 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2143 (cp_parser *);
2144 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_late_return_type_opt
2147 (cp_parser *, cp_declarator *, cp_cv_quals);
2148 static tree cp_parser_declarator_id
2149 (cp_parser *, bool);
2150 static tree cp_parser_type_id
2151 (cp_parser *);
2152 static tree cp_parser_template_type_arg
2153 (cp_parser *);
2154 static tree cp_parser_trailing_type_id (cp_parser *);
2155 static tree cp_parser_type_id_1
2156 (cp_parser *, bool, bool);
2157 static void cp_parser_type_specifier_seq
2158 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2159 static tree cp_parser_parameter_declaration_clause
2160 (cp_parser *);
2161 static tree cp_parser_parameter_declaration_list
2162 (cp_parser *, bool *);
2163 static cp_parameter_declarator *cp_parser_parameter_declaration
2164 (cp_parser *, bool, bool *);
2165 static tree cp_parser_default_argument
2166 (cp_parser *, bool);
2167 static void cp_parser_function_body
2168 (cp_parser *, bool);
2169 static tree cp_parser_initializer
2170 (cp_parser *, bool *, bool *);
2171 static tree cp_parser_initializer_clause
2172 (cp_parser *, bool *);
2173 static tree cp_parser_braced_list
2174 (cp_parser*, bool*);
2175 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2176 (cp_parser *, bool *);
2177
2178 static bool cp_parser_ctor_initializer_opt_and_function_body
2179 (cp_parser *, bool);
2180
2181 static tree cp_parser_late_parsing_omp_declare_simd
2182 (cp_parser *, tree);
2183
2184 static tree cp_parser_late_parsing_cilk_simd_fn_info
2185 (cp_parser *, tree);
2186
2187 static tree synthesize_implicit_template_parm
2188 (cp_parser *);
2189 static tree finish_fully_implicit_template
2190 (cp_parser *, tree);
2191
2192 /* Classes [gram.class] */
2193
2194 static tree cp_parser_class_name
2195 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2196 static tree cp_parser_class_specifier
2197 (cp_parser *);
2198 static tree cp_parser_class_head
2199 (cp_parser *, bool *);
2200 static enum tag_types cp_parser_class_key
2201 (cp_parser *);
2202 static void cp_parser_type_parameter_key
2203 (cp_parser* parser);
2204 static void cp_parser_member_specification_opt
2205 (cp_parser *);
2206 static void cp_parser_member_declaration
2207 (cp_parser *);
2208 static tree cp_parser_pure_specifier
2209 (cp_parser *);
2210 static tree cp_parser_constant_initializer
2211 (cp_parser *);
2212
2213 /* Derived classes [gram.class.derived] */
2214
2215 static tree cp_parser_base_clause
2216 (cp_parser *);
2217 static tree cp_parser_base_specifier
2218 (cp_parser *);
2219
2220 /* Special member functions [gram.special] */
2221
2222 static tree cp_parser_conversion_function_id
2223 (cp_parser *);
2224 static tree cp_parser_conversion_type_id
2225 (cp_parser *);
2226 static cp_declarator *cp_parser_conversion_declarator_opt
2227 (cp_parser *);
2228 static bool cp_parser_ctor_initializer_opt
2229 (cp_parser *);
2230 static void cp_parser_mem_initializer_list
2231 (cp_parser *);
2232 static tree cp_parser_mem_initializer
2233 (cp_parser *);
2234 static tree cp_parser_mem_initializer_id
2235 (cp_parser *);
2236
2237 /* Overloading [gram.over] */
2238
2239 static tree cp_parser_operator_function_id
2240 (cp_parser *);
2241 static tree cp_parser_operator
2242 (cp_parser *);
2243
2244 /* Templates [gram.temp] */
2245
2246 static void cp_parser_template_declaration
2247 (cp_parser *, bool);
2248 static tree cp_parser_template_parameter_list
2249 (cp_parser *);
2250 static tree cp_parser_template_parameter
2251 (cp_parser *, bool *, bool *);
2252 static tree cp_parser_type_parameter
2253 (cp_parser *, bool *);
2254 static tree cp_parser_template_id
2255 (cp_parser *, bool, bool, enum tag_types, bool);
2256 static tree cp_parser_template_name
2257 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2258 static tree cp_parser_template_argument_list
2259 (cp_parser *);
2260 static tree cp_parser_template_argument
2261 (cp_parser *);
2262 static void cp_parser_explicit_instantiation
2263 (cp_parser *);
2264 static void cp_parser_explicit_specialization
2265 (cp_parser *);
2266
2267 /* Exception handling [gram.exception] */
2268
2269 static tree cp_parser_try_block
2270 (cp_parser *);
2271 static bool cp_parser_function_try_block
2272 (cp_parser *);
2273 static void cp_parser_handler_seq
2274 (cp_parser *);
2275 static void cp_parser_handler
2276 (cp_parser *);
2277 static tree cp_parser_exception_declaration
2278 (cp_parser *);
2279 static tree cp_parser_throw_expression
2280 (cp_parser *);
2281 static tree cp_parser_exception_specification_opt
2282 (cp_parser *);
2283 static tree cp_parser_type_id_list
2284 (cp_parser *);
2285
2286 /* GNU Extensions */
2287
2288 static tree cp_parser_asm_specification_opt
2289 (cp_parser *);
2290 static tree cp_parser_asm_operand_list
2291 (cp_parser *);
2292 static tree cp_parser_asm_clobber_list
2293 (cp_parser *);
2294 static tree cp_parser_asm_label_list
2295 (cp_parser *);
2296 static bool cp_next_tokens_can_be_attribute_p
2297 (cp_parser *);
2298 static bool cp_next_tokens_can_be_gnu_attribute_p
2299 (cp_parser *);
2300 static bool cp_next_tokens_can_be_std_attribute_p
2301 (cp_parser *);
2302 static bool cp_nth_tokens_can_be_std_attribute_p
2303 (cp_parser *, size_t);
2304 static bool cp_nth_tokens_can_be_gnu_attribute_p
2305 (cp_parser *, size_t);
2306 static bool cp_nth_tokens_can_be_attribute_p
2307 (cp_parser *, size_t);
2308 static tree cp_parser_attributes_opt
2309 (cp_parser *);
2310 static tree cp_parser_gnu_attributes_opt
2311 (cp_parser *);
2312 static tree cp_parser_gnu_attribute_list
2313 (cp_parser *);
2314 static tree cp_parser_std_attribute
2315 (cp_parser *);
2316 static tree cp_parser_std_attribute_spec
2317 (cp_parser *);
2318 static tree cp_parser_std_attribute_spec_seq
2319 (cp_parser *);
2320 static bool cp_parser_extension_opt
2321 (cp_parser *, int *);
2322 static void cp_parser_label_declaration
2323 (cp_parser *);
2324
2325 /* Transactional Memory Extensions */
2326
2327 static tree cp_parser_transaction
2328 (cp_parser *, enum rid);
2329 static tree cp_parser_transaction_expression
2330 (cp_parser *, enum rid);
2331 static bool cp_parser_function_transaction
2332 (cp_parser *, enum rid);
2333 static tree cp_parser_transaction_cancel
2334 (cp_parser *);
2335
2336 enum pragma_context {
2337 pragma_external,
2338 pragma_member,
2339 pragma_objc_icode,
2340 pragma_stmt,
2341 pragma_compound
2342 };
2343 static bool cp_parser_pragma
2344 (cp_parser *, enum pragma_context);
2345
2346 /* Objective-C++ Productions */
2347
2348 static tree cp_parser_objc_message_receiver
2349 (cp_parser *);
2350 static tree cp_parser_objc_message_args
2351 (cp_parser *);
2352 static tree cp_parser_objc_message_expression
2353 (cp_parser *);
2354 static tree cp_parser_objc_encode_expression
2355 (cp_parser *);
2356 static tree cp_parser_objc_defs_expression
2357 (cp_parser *);
2358 static tree cp_parser_objc_protocol_expression
2359 (cp_parser *);
2360 static tree cp_parser_objc_selector_expression
2361 (cp_parser *);
2362 static tree cp_parser_objc_expression
2363 (cp_parser *);
2364 static bool cp_parser_objc_selector_p
2365 (enum cpp_ttype);
2366 static tree cp_parser_objc_selector
2367 (cp_parser *);
2368 static tree cp_parser_objc_protocol_refs_opt
2369 (cp_parser *);
2370 static void cp_parser_objc_declaration
2371 (cp_parser *, tree);
2372 static tree cp_parser_objc_statement
2373 (cp_parser *);
2374 static bool cp_parser_objc_valid_prefix_attributes
2375 (cp_parser *, tree *);
2376 static void cp_parser_objc_at_property_declaration
2377 (cp_parser *) ;
2378 static void cp_parser_objc_at_synthesize_declaration
2379 (cp_parser *) ;
2380 static void cp_parser_objc_at_dynamic_declaration
2381 (cp_parser *) ;
2382 static tree cp_parser_objc_struct_declaration
2383 (cp_parser *) ;
2384
2385 /* Utility Routines */
2386
2387 static tree cp_parser_lookup_name
2388 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2389 static tree cp_parser_lookup_name_simple
2390 (cp_parser *, tree, location_t);
2391 static tree cp_parser_maybe_treat_template_as_class
2392 (tree, bool);
2393 static bool cp_parser_check_declarator_template_parameters
2394 (cp_parser *, cp_declarator *, location_t);
2395 static bool cp_parser_check_template_parameters
2396 (cp_parser *, unsigned, location_t, cp_declarator *);
2397 static tree cp_parser_simple_cast_expression
2398 (cp_parser *);
2399 static tree cp_parser_global_scope_opt
2400 (cp_parser *, bool);
2401 static bool cp_parser_constructor_declarator_p
2402 (cp_parser *, bool);
2403 static tree cp_parser_function_definition_from_specifiers_and_declarator
2404 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2405 static tree cp_parser_function_definition_after_declarator
2406 (cp_parser *, bool);
2407 static void cp_parser_template_declaration_after_export
2408 (cp_parser *, bool);
2409 static void cp_parser_perform_template_parameter_access_checks
2410 (vec<deferred_access_check, va_gc> *);
2411 static tree cp_parser_single_declaration
2412 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2413 static tree cp_parser_functional_cast
2414 (cp_parser *, tree);
2415 static tree cp_parser_save_member_function_body
2416 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2417 static tree cp_parser_save_nsdmi
2418 (cp_parser *);
2419 static tree cp_parser_enclosed_template_argument_list
2420 (cp_parser *);
2421 static void cp_parser_save_default_args
2422 (cp_parser *, tree);
2423 static void cp_parser_late_parsing_for_member
2424 (cp_parser *, tree);
2425 static tree cp_parser_late_parse_one_default_arg
2426 (cp_parser *, tree, tree, tree);
2427 static void cp_parser_late_parsing_nsdmi
2428 (cp_parser *, tree);
2429 static void cp_parser_late_parsing_default_args
2430 (cp_parser *, tree);
2431 static tree cp_parser_sizeof_operand
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_trait_expr
2434 (cp_parser *, enum rid);
2435 static bool cp_parser_declares_only_class_p
2436 (cp_parser *);
2437 static void cp_parser_set_storage_class
2438 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2439 static void cp_parser_set_decl_spec_type
2440 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2441 static void set_and_check_decl_spec_loc
2442 (cp_decl_specifier_seq *decl_specs,
2443 cp_decl_spec ds, cp_token *);
2444 static bool cp_parser_friend_p
2445 (const cp_decl_specifier_seq *);
2446 static void cp_parser_required_error
2447 (cp_parser *, required_token, bool);
2448 static cp_token *cp_parser_require
2449 (cp_parser *, enum cpp_ttype, required_token);
2450 static cp_token *cp_parser_require_keyword
2451 (cp_parser *, enum rid, required_token);
2452 static bool cp_parser_token_starts_function_definition_p
2453 (cp_token *);
2454 static bool cp_parser_next_token_starts_class_definition_p
2455 (cp_parser *);
2456 static bool cp_parser_next_token_ends_template_argument_p
2457 (cp_parser *);
2458 static bool cp_parser_nth_token_starts_template_argument_list_p
2459 (cp_parser *, size_t);
2460 static enum tag_types cp_parser_token_is_class_key
2461 (cp_token *);
2462 static enum tag_types cp_parser_token_is_type_parameter_key
2463 (cp_token *);
2464 static void cp_parser_check_class_key
2465 (enum tag_types, tree type);
2466 static void cp_parser_check_access_in_redeclaration
2467 (tree type, location_t location);
2468 static bool cp_parser_optional_template_keyword
2469 (cp_parser *);
2470 static void cp_parser_pre_parsed_nested_name_specifier
2471 (cp_parser *);
2472 static bool cp_parser_cache_group
2473 (cp_parser *, enum cpp_ttype, unsigned);
2474 static tree cp_parser_cache_defarg
2475 (cp_parser *parser, bool nsdmi);
2476 static void cp_parser_parse_tentatively
2477 (cp_parser *);
2478 static void cp_parser_commit_to_tentative_parse
2479 (cp_parser *);
2480 static void cp_parser_commit_to_topmost_tentative_parse
2481 (cp_parser *);
2482 static void cp_parser_abort_tentative_parse
2483 (cp_parser *);
2484 static bool cp_parser_parse_definitely
2485 (cp_parser *);
2486 static inline bool cp_parser_parsing_tentatively
2487 (cp_parser *);
2488 static bool cp_parser_uncommitted_to_tentative_parse_p
2489 (cp_parser *);
2490 static void cp_parser_error
2491 (cp_parser *, const char *);
2492 static void cp_parser_name_lookup_error
2493 (cp_parser *, tree, tree, name_lookup_error, location_t);
2494 static bool cp_parser_simulate_error
2495 (cp_parser *);
2496 static bool cp_parser_check_type_definition
2497 (cp_parser *);
2498 static void cp_parser_check_for_definition_in_return_type
2499 (cp_declarator *, tree, location_t type_location);
2500 static void cp_parser_check_for_invalid_template_id
2501 (cp_parser *, tree, enum tag_types, location_t location);
2502 static bool cp_parser_non_integral_constant_expression
2503 (cp_parser *, non_integral_constant);
2504 static void cp_parser_diagnose_invalid_type_name
2505 (cp_parser *, tree, location_t);
2506 static bool cp_parser_parse_and_diagnose_invalid_type_name
2507 (cp_parser *);
2508 static int cp_parser_skip_to_closing_parenthesis
2509 (cp_parser *, bool, bool, bool);
2510 static void cp_parser_skip_to_end_of_statement
2511 (cp_parser *);
2512 static void cp_parser_consume_semicolon_at_end_of_statement
2513 (cp_parser *);
2514 static void cp_parser_skip_to_end_of_block_or_statement
2515 (cp_parser *);
2516 static bool cp_parser_skip_to_closing_brace
2517 (cp_parser *);
2518 static void cp_parser_skip_to_end_of_template_parameter_list
2519 (cp_parser *);
2520 static void cp_parser_skip_to_pragma_eol
2521 (cp_parser*, cp_token *);
2522 static bool cp_parser_error_occurred
2523 (cp_parser *);
2524 static bool cp_parser_allow_gnu_extensions_p
2525 (cp_parser *);
2526 static bool cp_parser_is_pure_string_literal
2527 (cp_token *);
2528 static bool cp_parser_is_string_literal
2529 (cp_token *);
2530 static bool cp_parser_is_keyword
2531 (cp_token *, enum rid);
2532 static tree cp_parser_make_typename_type
2533 (cp_parser *, tree, location_t location);
2534 static cp_declarator * cp_parser_make_indirect_declarator
2535 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2536 static bool cp_parser_compound_literal_p
2537 (cp_parser *);
2538 static bool cp_parser_array_designator_p
2539 (cp_parser *);
2540 static bool cp_parser_skip_to_closing_square_bracket
2541 (cp_parser *);
2542
2543 /* Returns nonzero if we are parsing tentatively. */
2544
2545 static inline bool
2546 cp_parser_parsing_tentatively (cp_parser* parser)
2547 {
2548 return parser->context->next != NULL;
2549 }
2550
2551 /* Returns nonzero if TOKEN is a string literal. */
2552
2553 static bool
2554 cp_parser_is_pure_string_literal (cp_token* token)
2555 {
2556 return (token->type == CPP_STRING ||
2557 token->type == CPP_STRING16 ||
2558 token->type == CPP_STRING32 ||
2559 token->type == CPP_WSTRING ||
2560 token->type == CPP_UTF8STRING);
2561 }
2562
2563 /* Returns nonzero if TOKEN is a string literal
2564 of a user-defined string literal. */
2565
2566 static bool
2567 cp_parser_is_string_literal (cp_token* token)
2568 {
2569 return (cp_parser_is_pure_string_literal (token) ||
2570 token->type == CPP_STRING_USERDEF ||
2571 token->type == CPP_STRING16_USERDEF ||
2572 token->type == CPP_STRING32_USERDEF ||
2573 token->type == CPP_WSTRING_USERDEF ||
2574 token->type == CPP_UTF8STRING_USERDEF);
2575 }
2576
2577 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2578
2579 static bool
2580 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2581 {
2582 return token->keyword == keyword;
2583 }
2584
2585 /* If not parsing tentatively, issue a diagnostic of the form
2586 FILE:LINE: MESSAGE before TOKEN
2587 where TOKEN is the next token in the input stream. MESSAGE
2588 (specified by the caller) is usually of the form "expected
2589 OTHER-TOKEN". */
2590
2591 static void
2592 cp_parser_error (cp_parser* parser, const char* gmsgid)
2593 {
2594 if (!cp_parser_simulate_error (parser))
2595 {
2596 cp_token *token = cp_lexer_peek_token (parser->lexer);
2597 /* This diagnostic makes more sense if it is tagged to the line
2598 of the token we just peeked at. */
2599 cp_lexer_set_source_position_from_token (token);
2600
2601 if (token->type == CPP_PRAGMA)
2602 {
2603 error_at (token->location,
2604 "%<#pragma%> is not allowed here");
2605 cp_parser_skip_to_pragma_eol (parser, token);
2606 return;
2607 }
2608
2609 c_parse_error (gmsgid,
2610 /* Because c_parser_error does not understand
2611 CPP_KEYWORD, keywords are treated like
2612 identifiers. */
2613 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2614 token->u.value, token->flags);
2615 }
2616 }
2617
2618 /* Issue an error about name-lookup failing. NAME is the
2619 IDENTIFIER_NODE DECL is the result of
2620 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2621 the thing that we hoped to find. */
2622
2623 static void
2624 cp_parser_name_lookup_error (cp_parser* parser,
2625 tree name,
2626 tree decl,
2627 name_lookup_error desired,
2628 location_t location)
2629 {
2630 /* If name lookup completely failed, tell the user that NAME was not
2631 declared. */
2632 if (decl == error_mark_node)
2633 {
2634 if (parser->scope && parser->scope != global_namespace)
2635 error_at (location, "%<%E::%E%> has not been declared",
2636 parser->scope, name);
2637 else if (parser->scope == global_namespace)
2638 error_at (location, "%<::%E%> has not been declared", name);
2639 else if (parser->object_scope
2640 && !CLASS_TYPE_P (parser->object_scope))
2641 error_at (location, "request for member %qE in non-class type %qT",
2642 name, parser->object_scope);
2643 else if (parser->object_scope)
2644 error_at (location, "%<%T::%E%> has not been declared",
2645 parser->object_scope, name);
2646 else
2647 error_at (location, "%qE has not been declared", name);
2648 }
2649 else if (parser->scope && parser->scope != global_namespace)
2650 {
2651 switch (desired)
2652 {
2653 case NLE_TYPE:
2654 error_at (location, "%<%E::%E%> is not a type",
2655 parser->scope, name);
2656 break;
2657 case NLE_CXX98:
2658 error_at (location, "%<%E::%E%> is not a class or namespace",
2659 parser->scope, name);
2660 break;
2661 case NLE_NOT_CXX98:
2662 error_at (location,
2663 "%<%E::%E%> is not a class, namespace, or enumeration",
2664 parser->scope, name);
2665 break;
2666 default:
2667 gcc_unreachable ();
2668
2669 }
2670 }
2671 else if (parser->scope == global_namespace)
2672 {
2673 switch (desired)
2674 {
2675 case NLE_TYPE:
2676 error_at (location, "%<::%E%> is not a type", name);
2677 break;
2678 case NLE_CXX98:
2679 error_at (location, "%<::%E%> is not a class or namespace", name);
2680 break;
2681 case NLE_NOT_CXX98:
2682 error_at (location,
2683 "%<::%E%> is not a class, namespace, or enumeration",
2684 name);
2685 break;
2686 default:
2687 gcc_unreachable ();
2688 }
2689 }
2690 else
2691 {
2692 switch (desired)
2693 {
2694 case NLE_TYPE:
2695 error_at (location, "%qE is not a type", name);
2696 break;
2697 case NLE_CXX98:
2698 error_at (location, "%qE is not a class or namespace", name);
2699 break;
2700 case NLE_NOT_CXX98:
2701 error_at (location,
2702 "%qE is not a class, namespace, or enumeration", name);
2703 break;
2704 default:
2705 gcc_unreachable ();
2706 }
2707 }
2708 }
2709
2710 /* If we are parsing tentatively, remember that an error has occurred
2711 during this tentative parse. Returns true if the error was
2712 simulated; false if a message should be issued by the caller. */
2713
2714 static bool
2715 cp_parser_simulate_error (cp_parser* parser)
2716 {
2717 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2718 {
2719 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2720 return true;
2721 }
2722 return false;
2723 }
2724
2725 /* This function is called when a type is defined. If type
2726 definitions are forbidden at this point, an error message is
2727 issued. */
2728
2729 static bool
2730 cp_parser_check_type_definition (cp_parser* parser)
2731 {
2732 /* If types are forbidden here, issue a message. */
2733 if (parser->type_definition_forbidden_message)
2734 {
2735 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2736 in the message need to be interpreted. */
2737 error (parser->type_definition_forbidden_message);
2738 return false;
2739 }
2740 return true;
2741 }
2742
2743 /* This function is called when the DECLARATOR is processed. The TYPE
2744 was a type defined in the decl-specifiers. If it is invalid to
2745 define a type in the decl-specifiers for DECLARATOR, an error is
2746 issued. TYPE_LOCATION is the location of TYPE and is used
2747 for error reporting. */
2748
2749 static void
2750 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2751 tree type, location_t type_location)
2752 {
2753 /* [dcl.fct] forbids type definitions in return types.
2754 Unfortunately, it's not easy to know whether or not we are
2755 processing a return type until after the fact. */
2756 while (declarator
2757 && (declarator->kind == cdk_pointer
2758 || declarator->kind == cdk_reference
2759 || declarator->kind == cdk_ptrmem))
2760 declarator = declarator->declarator;
2761 if (declarator
2762 && declarator->kind == cdk_function)
2763 {
2764 error_at (type_location,
2765 "new types may not be defined in a return type");
2766 inform (type_location,
2767 "(perhaps a semicolon is missing after the definition of %qT)",
2768 type);
2769 }
2770 }
2771
2772 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2773 "<" in any valid C++ program. If the next token is indeed "<",
2774 issue a message warning the user about what appears to be an
2775 invalid attempt to form a template-id. LOCATION is the location
2776 of the type-specifier (TYPE) */
2777
2778 static void
2779 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2780 tree type,
2781 enum tag_types tag_type,
2782 location_t location)
2783 {
2784 cp_token_position start = 0;
2785
2786 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2787 {
2788 if (TYPE_P (type))
2789 error_at (location, "%qT is not a template", type);
2790 else if (identifier_p (type))
2791 {
2792 if (tag_type != none_type)
2793 error_at (location, "%qE is not a class template", type);
2794 else
2795 error_at (location, "%qE is not a template", type);
2796 }
2797 else
2798 error_at (location, "invalid template-id");
2799 /* Remember the location of the invalid "<". */
2800 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2801 start = cp_lexer_token_position (parser->lexer, true);
2802 /* Consume the "<". */
2803 cp_lexer_consume_token (parser->lexer);
2804 /* Parse the template arguments. */
2805 cp_parser_enclosed_template_argument_list (parser);
2806 /* Permanently remove the invalid template arguments so that
2807 this error message is not issued again. */
2808 if (start)
2809 cp_lexer_purge_tokens_after (parser->lexer, start);
2810 }
2811 }
2812
2813 /* If parsing an integral constant-expression, issue an error message
2814 about the fact that THING appeared and return true. Otherwise,
2815 return false. In either case, set
2816 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2817
2818 static bool
2819 cp_parser_non_integral_constant_expression (cp_parser *parser,
2820 non_integral_constant thing)
2821 {
2822 parser->non_integral_constant_expression_p = true;
2823 if (parser->integral_constant_expression_p)
2824 {
2825 if (!parser->allow_non_integral_constant_expression_p)
2826 {
2827 const char *msg = NULL;
2828 switch (thing)
2829 {
2830 case NIC_FLOAT:
2831 error ("floating-point literal "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_CAST:
2835 error ("a cast to a type other than an integral or "
2836 "enumeration type cannot appear in a "
2837 "constant-expression");
2838 return true;
2839 case NIC_TYPEID:
2840 error ("%<typeid%> operator "
2841 "cannot appear in a constant-expression");
2842 return true;
2843 case NIC_NCC:
2844 error ("non-constant compound literals "
2845 "cannot appear in a constant-expression");
2846 return true;
2847 case NIC_FUNC_CALL:
2848 error ("a function call "
2849 "cannot appear in a constant-expression");
2850 return true;
2851 case NIC_INC:
2852 error ("an increment "
2853 "cannot appear in a constant-expression");
2854 return true;
2855 case NIC_DEC:
2856 error ("an decrement "
2857 "cannot appear in a constant-expression");
2858 return true;
2859 case NIC_ARRAY_REF:
2860 error ("an array reference "
2861 "cannot appear in a constant-expression");
2862 return true;
2863 case NIC_ADDR_LABEL:
2864 error ("the address of a label "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_OVERLOADED:
2868 error ("calls to overloaded operators "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_ASSIGNMENT:
2872 error ("an assignment cannot appear in a constant-expression");
2873 return true;
2874 case NIC_COMMA:
2875 error ("a comma operator "
2876 "cannot appear in a constant-expression");
2877 return true;
2878 case NIC_CONSTRUCTOR:
2879 error ("a call to a constructor "
2880 "cannot appear in a constant-expression");
2881 return true;
2882 case NIC_TRANSACTION:
2883 error ("a transaction expression "
2884 "cannot appear in a constant-expression");
2885 return true;
2886 case NIC_THIS:
2887 msg = "this";
2888 break;
2889 case NIC_FUNC_NAME:
2890 msg = "__FUNCTION__";
2891 break;
2892 case NIC_PRETTY_FUNC:
2893 msg = "__PRETTY_FUNCTION__";
2894 break;
2895 case NIC_C99_FUNC:
2896 msg = "__func__";
2897 break;
2898 case NIC_VA_ARG:
2899 msg = "va_arg";
2900 break;
2901 case NIC_ARROW:
2902 msg = "->";
2903 break;
2904 case NIC_POINT:
2905 msg = ".";
2906 break;
2907 case NIC_STAR:
2908 msg = "*";
2909 break;
2910 case NIC_ADDR:
2911 msg = "&";
2912 break;
2913 case NIC_PREINCREMENT:
2914 msg = "++";
2915 break;
2916 case NIC_PREDECREMENT:
2917 msg = "--";
2918 break;
2919 case NIC_NEW:
2920 msg = "new";
2921 break;
2922 case NIC_DEL:
2923 msg = "delete";
2924 break;
2925 default:
2926 gcc_unreachable ();
2927 }
2928 if (msg)
2929 error ("%qs cannot appear in a constant-expression", msg);
2930 return true;
2931 }
2932 }
2933 return false;
2934 }
2935
2936 /* Emit a diagnostic for an invalid type name. This function commits
2937 to the current active tentative parse, if any. (Otherwise, the
2938 problematic construct might be encountered again later, resulting
2939 in duplicate error messages.) LOCATION is the location of ID. */
2940
2941 static void
2942 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2943 location_t location)
2944 {
2945 tree decl, ambiguous_decls;
2946 cp_parser_commit_to_tentative_parse (parser);
2947 /* Try to lookup the identifier. */
2948 decl = cp_parser_lookup_name (parser, id, none_type,
2949 /*is_template=*/false,
2950 /*is_namespace=*/false,
2951 /*check_dependency=*/true,
2952 &ambiguous_decls, location);
2953 if (ambiguous_decls)
2954 /* If the lookup was ambiguous, an error will already have
2955 been issued. */
2956 return;
2957 /* If the lookup found a template-name, it means that the user forgot
2958 to specify an argument list. Emit a useful error message. */
2959 if (TREE_CODE (decl) == TEMPLATE_DECL)
2960 error_at (location,
2961 "invalid use of template-name %qE without an argument list",
2962 decl);
2963 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2964 error_at (location, "invalid use of destructor %qD as a type", id);
2965 else if (TREE_CODE (decl) == TYPE_DECL)
2966 /* Something like 'unsigned A a;' */
2967 error_at (location, "invalid combination of multiple type-specifiers");
2968 else if (!parser->scope)
2969 {
2970 /* Issue an error message. */
2971 error_at (location, "%qE does not name a type", id);
2972 /* If we're in a template class, it's possible that the user was
2973 referring to a type from a base class. For example:
2974
2975 template <typename T> struct A { typedef T X; };
2976 template <typename T> struct B : public A<T> { X x; };
2977
2978 The user should have said "typename A<T>::X". */
2979 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2980 inform (location, "C++11 %<constexpr%> only available with "
2981 "-std=c++11 or -std=gnu++11");
2982 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2983 inform (location, "C++11 %<noexcept%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (cxx_dialect < cxx11
2986 && TREE_CODE (id) == IDENTIFIER_NODE
2987 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2988 inform (location, "C++11 %<thread_local%> only available with "
2989 "-std=c++11 or -std=gnu++11");
2990 else if (processing_template_decl && current_class_type
2991 && TYPE_BINFO (current_class_type))
2992 {
2993 tree b;
2994
2995 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2996 b;
2997 b = TREE_CHAIN (b))
2998 {
2999 tree base_type = BINFO_TYPE (b);
3000 if (CLASS_TYPE_P (base_type)
3001 && dependent_type_p (base_type))
3002 {
3003 tree field;
3004 /* Go from a particular instantiation of the
3005 template (which will have an empty TYPE_FIELDs),
3006 to the main version. */
3007 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3008 for (field = TYPE_FIELDS (base_type);
3009 field;
3010 field = DECL_CHAIN (field))
3011 if (TREE_CODE (field) == TYPE_DECL
3012 && DECL_NAME (field) == id)
3013 {
3014 inform (location,
3015 "(perhaps %<typename %T::%E%> was intended)",
3016 BINFO_TYPE (b), id);
3017 break;
3018 }
3019 if (field)
3020 break;
3021 }
3022 }
3023 }
3024 }
3025 /* Here we diagnose qualified-ids where the scope is actually correct,
3026 but the identifier does not resolve to a valid type name. */
3027 else if (parser->scope != error_mark_node)
3028 {
3029 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3030 {
3031 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3032 error_at (location_of (id),
3033 "%qE in namespace %qE does not name a template type",
3034 id, parser->scope);
3035 else
3036 error_at (location_of (id),
3037 "%qE in namespace %qE does not name a type",
3038 id, parser->scope);
3039 }
3040 else if (CLASS_TYPE_P (parser->scope)
3041 && constructor_name_p (id, parser->scope))
3042 {
3043 /* A<T>::A<T>() */
3044 error_at (location, "%<%T::%E%> names the constructor, not"
3045 " the type", parser->scope, id);
3046 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3047 error_at (location, "and %qT has no template constructors",
3048 parser->scope);
3049 }
3050 else if (TYPE_P (parser->scope)
3051 && dependent_scope_p (parser->scope))
3052 error_at (location, "need %<typename%> before %<%T::%E%> because "
3053 "%qT is a dependent scope",
3054 parser->scope, id, parser->scope);
3055 else if (TYPE_P (parser->scope))
3056 {
3057 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3058 error_at (location_of (id),
3059 "%qE in %q#T does not name a template type",
3060 id, parser->scope);
3061 else
3062 error_at (location_of (id),
3063 "%qE in %q#T does not name a type",
3064 id, parser->scope);
3065 }
3066 else
3067 gcc_unreachable ();
3068 }
3069 }
3070
3071 /* Check for a common situation where a type-name should be present,
3072 but is not, and issue a sensible error message. Returns true if an
3073 invalid type-name was detected.
3074
3075 The situation handled by this function are variable declarations of the
3076 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3077 Usually, `ID' should name a type, but if we got here it means that it
3078 does not. We try to emit the best possible error message depending on
3079 how exactly the id-expression looks like. */
3080
3081 static bool
3082 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3083 {
3084 tree id;
3085 cp_token *token = cp_lexer_peek_token (parser->lexer);
3086
3087 /* Avoid duplicate error about ambiguous lookup. */
3088 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3089 {
3090 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3091 if (next->type == CPP_NAME && next->error_reported)
3092 goto out;
3093 }
3094
3095 cp_parser_parse_tentatively (parser);
3096 id = cp_parser_id_expression (parser,
3097 /*template_keyword_p=*/false,
3098 /*check_dependency_p=*/true,
3099 /*template_p=*/NULL,
3100 /*declarator_p=*/true,
3101 /*optional_p=*/false);
3102 /* If the next token is a (, this is a function with no explicit return
3103 type, i.e. constructor, destructor or conversion op. */
3104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3105 || TREE_CODE (id) == TYPE_DECL)
3106 {
3107 cp_parser_abort_tentative_parse (parser);
3108 return false;
3109 }
3110 if (!cp_parser_parse_definitely (parser))
3111 return false;
3112
3113 /* Emit a diagnostic for the invalid type. */
3114 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3115 out:
3116 /* If we aren't in the middle of a declarator (i.e. in a
3117 parameter-declaration-clause), skip to the end of the declaration;
3118 there's no point in trying to process it. */
3119 if (!parser->in_declarator_p)
3120 cp_parser_skip_to_end_of_block_or_statement (parser);
3121 return true;
3122 }
3123
3124 /* Consume tokens up to, and including, the next non-nested closing `)'.
3125 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3126 are doing error recovery. Returns -1 if OR_COMMA is true and we
3127 found an unnested comma. */
3128
3129 static int
3130 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3131 bool recovering,
3132 bool or_comma,
3133 bool consume_paren)
3134 {
3135 unsigned paren_depth = 0;
3136 unsigned brace_depth = 0;
3137 unsigned square_depth = 0;
3138
3139 if (recovering && !or_comma
3140 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3141 return 0;
3142
3143 while (true)
3144 {
3145 cp_token * token = cp_lexer_peek_token (parser->lexer);
3146
3147 switch (token->type)
3148 {
3149 case CPP_EOF:
3150 case CPP_PRAGMA_EOL:
3151 /* If we've run out of tokens, then there is no closing `)'. */
3152 return 0;
3153
3154 /* This is good for lambda expression capture-lists. */
3155 case CPP_OPEN_SQUARE:
3156 ++square_depth;
3157 break;
3158 case CPP_CLOSE_SQUARE:
3159 if (!square_depth--)
3160 return 0;
3161 break;
3162
3163 case CPP_SEMICOLON:
3164 /* This matches the processing in skip_to_end_of_statement. */
3165 if (!brace_depth)
3166 return 0;
3167 break;
3168
3169 case CPP_OPEN_BRACE:
3170 ++brace_depth;
3171 break;
3172 case CPP_CLOSE_BRACE:
3173 if (!brace_depth--)
3174 return 0;
3175 break;
3176
3177 case CPP_COMMA:
3178 if (recovering && or_comma && !brace_depth && !paren_depth
3179 && !square_depth)
3180 return -1;
3181 break;
3182
3183 case CPP_OPEN_PAREN:
3184 if (!brace_depth)
3185 ++paren_depth;
3186 break;
3187
3188 case CPP_CLOSE_PAREN:
3189 if (!brace_depth && !paren_depth--)
3190 {
3191 if (consume_paren)
3192 cp_lexer_consume_token (parser->lexer);
3193 return 1;
3194 }
3195 break;
3196
3197 default:
3198 break;
3199 }
3200
3201 /* Consume the token. */
3202 cp_lexer_consume_token (parser->lexer);
3203 }
3204 }
3205
3206 /* Consume tokens until we reach the end of the current statement.
3207 Normally, that will be just before consuming a `;'. However, if a
3208 non-nested `}' comes first, then we stop before consuming that. */
3209
3210 static void
3211 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3212 {
3213 unsigned nesting_depth = 0;
3214
3215 /* Unwind generic function template scope if necessary. */
3216 if (parser->fully_implicit_function_template_p)
3217 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3218
3219 while (true)
3220 {
3221 cp_token *token = cp_lexer_peek_token (parser->lexer);
3222
3223 switch (token->type)
3224 {
3225 case CPP_EOF:
3226 case CPP_PRAGMA_EOL:
3227 /* If we've run out of tokens, stop. */
3228 return;
3229
3230 case CPP_SEMICOLON:
3231 /* If the next token is a `;', we have reached the end of the
3232 statement. */
3233 if (!nesting_depth)
3234 return;
3235 break;
3236
3237 case CPP_CLOSE_BRACE:
3238 /* If this is a non-nested '}', stop before consuming it.
3239 That way, when confronted with something like:
3240
3241 { 3 + }
3242
3243 we stop before consuming the closing '}', even though we
3244 have not yet reached a `;'. */
3245 if (nesting_depth == 0)
3246 return;
3247
3248 /* If it is the closing '}' for a block that we have
3249 scanned, stop -- but only after consuming the token.
3250 That way given:
3251
3252 void f g () { ... }
3253 typedef int I;
3254
3255 we will stop after the body of the erroneously declared
3256 function, but before consuming the following `typedef'
3257 declaration. */
3258 if (--nesting_depth == 0)
3259 {
3260 cp_lexer_consume_token (parser->lexer);
3261 return;
3262 }
3263
3264 case CPP_OPEN_BRACE:
3265 ++nesting_depth;
3266 break;
3267
3268 default:
3269 break;
3270 }
3271
3272 /* Consume the token. */
3273 cp_lexer_consume_token (parser->lexer);
3274 }
3275 }
3276
3277 /* This function is called at the end of a statement or declaration.
3278 If the next token is a semicolon, it is consumed; otherwise, error
3279 recovery is attempted. */
3280
3281 static void
3282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3283 {
3284 /* Look for the trailing `;'. */
3285 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3286 {
3287 /* If there is additional (erroneous) input, skip to the end of
3288 the statement. */
3289 cp_parser_skip_to_end_of_statement (parser);
3290 /* If the next token is now a `;', consume it. */
3291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3292 cp_lexer_consume_token (parser->lexer);
3293 }
3294 }
3295
3296 /* Skip tokens until we have consumed an entire block, or until we
3297 have consumed a non-nested `;'. */
3298
3299 static void
3300 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3301 {
3302 int nesting_depth = 0;
3303
3304 /* Unwind generic function template scope if necessary. */
3305 if (parser->fully_implicit_function_template_p)
3306 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3307
3308 while (nesting_depth >= 0)
3309 {
3310 cp_token *token = cp_lexer_peek_token (parser->lexer);
3311
3312 switch (token->type)
3313 {
3314 case CPP_EOF:
3315 case CPP_PRAGMA_EOL:
3316 /* If we've run out of tokens, stop. */
3317 return;
3318
3319 case CPP_SEMICOLON:
3320 /* Stop if this is an unnested ';'. */
3321 if (!nesting_depth)
3322 nesting_depth = -1;
3323 break;
3324
3325 case CPP_CLOSE_BRACE:
3326 /* Stop if this is an unnested '}', or closes the outermost
3327 nesting level. */
3328 nesting_depth--;
3329 if (nesting_depth < 0)
3330 return;
3331 if (!nesting_depth)
3332 nesting_depth = -1;
3333 break;
3334
3335 case CPP_OPEN_BRACE:
3336 /* Nest. */
3337 nesting_depth++;
3338 break;
3339
3340 default:
3341 break;
3342 }
3343
3344 /* Consume the token. */
3345 cp_lexer_consume_token (parser->lexer);
3346 }
3347 }
3348
3349 /* Skip tokens until a non-nested closing curly brace is the next
3350 token, or there are no more tokens. Return true in the first case,
3351 false otherwise. */
3352
3353 static bool
3354 cp_parser_skip_to_closing_brace (cp_parser *parser)
3355 {
3356 unsigned nesting_depth = 0;
3357
3358 while (true)
3359 {
3360 cp_token *token = cp_lexer_peek_token (parser->lexer);
3361
3362 switch (token->type)
3363 {
3364 case CPP_EOF:
3365 case CPP_PRAGMA_EOL:
3366 /* If we've run out of tokens, stop. */
3367 return false;
3368
3369 case CPP_CLOSE_BRACE:
3370 /* If the next token is a non-nested `}', then we have reached
3371 the end of the current block. */
3372 if (nesting_depth-- == 0)
3373 return true;
3374 break;
3375
3376 case CPP_OPEN_BRACE:
3377 /* If it the next token is a `{', then we are entering a new
3378 block. Consume the entire block. */
3379 ++nesting_depth;
3380 break;
3381
3382 default:
3383 break;
3384 }
3385
3386 /* Consume the token. */
3387 cp_lexer_consume_token (parser->lexer);
3388 }
3389 }
3390
3391 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3392 parameter is the PRAGMA token, allowing us to purge the entire pragma
3393 sequence. */
3394
3395 static void
3396 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3397 {
3398 cp_token *token;
3399
3400 parser->lexer->in_pragma = false;
3401
3402 do
3403 token = cp_lexer_consume_token (parser->lexer);
3404 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3405
3406 /* Ensure that the pragma is not parsed again. */
3407 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3408 }
3409
3410 /* Require pragma end of line, resyncing with it as necessary. The
3411 arguments are as for cp_parser_skip_to_pragma_eol. */
3412
3413 static void
3414 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3415 {
3416 parser->lexer->in_pragma = false;
3417 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3418 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3419 }
3420
3421 /* This is a simple wrapper around make_typename_type. When the id is
3422 an unresolved identifier node, we can provide a superior diagnostic
3423 using cp_parser_diagnose_invalid_type_name. */
3424
3425 static tree
3426 cp_parser_make_typename_type (cp_parser *parser, tree id,
3427 location_t id_location)
3428 {
3429 tree result;
3430 if (identifier_p (id))
3431 {
3432 result = make_typename_type (parser->scope, id, typename_type,
3433 /*complain=*/tf_none);
3434 if (result == error_mark_node)
3435 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3436 return result;
3437 }
3438 return make_typename_type (parser->scope, id, typename_type, tf_error);
3439 }
3440
3441 /* This is a wrapper around the
3442 make_{pointer,ptrmem,reference}_declarator functions that decides
3443 which one to call based on the CODE and CLASS_TYPE arguments. The
3444 CODE argument should be one of the values returned by
3445 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3446 appertain to the pointer or reference. */
3447
3448 static cp_declarator *
3449 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3450 cp_cv_quals cv_qualifiers,
3451 cp_declarator *target,
3452 tree attributes)
3453 {
3454 if (code == ERROR_MARK)
3455 return cp_error_declarator;
3456
3457 if (code == INDIRECT_REF)
3458 if (class_type == NULL_TREE)
3459 return make_pointer_declarator (cv_qualifiers, target, attributes);
3460 else
3461 return make_ptrmem_declarator (cv_qualifiers, class_type,
3462 target, attributes);
3463 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3464 return make_reference_declarator (cv_qualifiers, target,
3465 false, attributes);
3466 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3467 return make_reference_declarator (cv_qualifiers, target,
3468 true, attributes);
3469 gcc_unreachable ();
3470 }
3471
3472 /* Create a new C++ parser. */
3473
3474 static cp_parser *
3475 cp_parser_new (void)
3476 {
3477 cp_parser *parser;
3478 cp_lexer *lexer;
3479 unsigned i;
3480
3481 /* cp_lexer_new_main is called before doing GC allocation because
3482 cp_lexer_new_main might load a PCH file. */
3483 lexer = cp_lexer_new_main ();
3484
3485 /* Initialize the binops_by_token so that we can get the tree
3486 directly from the token. */
3487 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3488 binops_by_token[binops[i].token_type] = binops[i];
3489
3490 parser = ggc_cleared_alloc<cp_parser> ();
3491 parser->lexer = lexer;
3492 parser->context = cp_parser_context_new (NULL);
3493
3494 /* For now, we always accept GNU extensions. */
3495 parser->allow_gnu_extensions_p = 1;
3496
3497 /* The `>' token is a greater-than operator, not the end of a
3498 template-id. */
3499 parser->greater_than_is_operator_p = true;
3500
3501 parser->default_arg_ok_p = true;
3502
3503 /* We are not parsing a constant-expression. */
3504 parser->integral_constant_expression_p = false;
3505 parser->allow_non_integral_constant_expression_p = false;
3506 parser->non_integral_constant_expression_p = false;
3507
3508 /* Local variable names are not forbidden. */
3509 parser->local_variables_forbidden_p = false;
3510
3511 /* We are not processing an `extern "C"' declaration. */
3512 parser->in_unbraced_linkage_specification_p = false;
3513
3514 /* We are not processing a declarator. */
3515 parser->in_declarator_p = false;
3516
3517 /* We are not processing a template-argument-list. */
3518 parser->in_template_argument_list_p = false;
3519
3520 /* We are not in an iteration statement. */
3521 parser->in_statement = 0;
3522
3523 /* We are not in a switch statement. */
3524 parser->in_switch_statement_p = false;
3525
3526 /* We are not parsing a type-id inside an expression. */
3527 parser->in_type_id_in_expr_p = false;
3528
3529 /* Declarations aren't implicitly extern "C". */
3530 parser->implicit_extern_c = false;
3531
3532 /* String literals should be translated to the execution character set. */
3533 parser->translate_strings_p = true;
3534
3535 /* We are not parsing a function body. */
3536 parser->in_function_body = false;
3537
3538 /* We can correct until told otherwise. */
3539 parser->colon_corrects_to_scope_p = true;
3540
3541 /* The unparsed function queue is empty. */
3542 push_unparsed_function_queues (parser);
3543
3544 /* There are no classes being defined. */
3545 parser->num_classes_being_defined = 0;
3546
3547 /* No template parameters apply. */
3548 parser->num_template_parameter_lists = 0;
3549
3550 /* Not declaring an implicit function template. */
3551 parser->auto_is_implicit_function_template_parm_p = false;
3552 parser->fully_implicit_function_template_p = false;
3553 parser->implicit_template_parms = 0;
3554 parser->implicit_template_scope = 0;
3555
3556 return parser;
3557 }
3558
3559 /* Create a cp_lexer structure which will emit the tokens in CACHE
3560 and push it onto the parser's lexer stack. This is used for delayed
3561 parsing of in-class method bodies and default arguments, and should
3562 not be confused with tentative parsing. */
3563 static void
3564 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3565 {
3566 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3567 lexer->next = parser->lexer;
3568 parser->lexer = lexer;
3569
3570 /* Move the current source position to that of the first token in the
3571 new lexer. */
3572 cp_lexer_set_source_position_from_token (lexer->next_token);
3573 }
3574
3575 /* Pop the top lexer off the parser stack. This is never used for the
3576 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3577 static void
3578 cp_parser_pop_lexer (cp_parser *parser)
3579 {
3580 cp_lexer *lexer = parser->lexer;
3581 parser->lexer = lexer->next;
3582 cp_lexer_destroy (lexer);
3583
3584 /* Put the current source position back where it was before this
3585 lexer was pushed. */
3586 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3587 }
3588
3589 /* Lexical conventions [gram.lex] */
3590
3591 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3592 identifier. */
3593
3594 static tree
3595 cp_parser_identifier (cp_parser* parser)
3596 {
3597 cp_token *token;
3598
3599 /* Look for the identifier. */
3600 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3601 /* Return the value. */
3602 return token ? token->u.value : error_mark_node;
3603 }
3604
3605 /* Parse a sequence of adjacent string constants. Returns a
3606 TREE_STRING representing the combined, nul-terminated string
3607 constant. If TRANSLATE is true, translate the string to the
3608 execution character set. If WIDE_OK is true, a wide string is
3609 invalid here.
3610
3611 C++98 [lex.string] says that if a narrow string literal token is
3612 adjacent to a wide string literal token, the behavior is undefined.
3613 However, C99 6.4.5p4 says that this results in a wide string literal.
3614 We follow C99 here, for consistency with the C front end.
3615
3616 This code is largely lifted from lex_string() in c-lex.c.
3617
3618 FUTURE: ObjC++ will need to handle @-strings here. */
3619 static tree
3620 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3621 bool lookup_udlit = true)
3622 {
3623 tree value;
3624 size_t count;
3625 struct obstack str_ob;
3626 cpp_string str, istr, *strs;
3627 cp_token *tok;
3628 enum cpp_ttype type, curr_type;
3629 int have_suffix_p = 0;
3630 tree string_tree;
3631 tree suffix_id = NULL_TREE;
3632 bool curr_tok_is_userdef_p = false;
3633
3634 tok = cp_lexer_peek_token (parser->lexer);
3635 if (!cp_parser_is_string_literal (tok))
3636 {
3637 cp_parser_error (parser, "expected string-literal");
3638 return error_mark_node;
3639 }
3640
3641 if (cpp_userdef_string_p (tok->type))
3642 {
3643 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3644 curr_type = cpp_userdef_string_remove_type (tok->type);
3645 curr_tok_is_userdef_p = true;
3646 }
3647 else
3648 {
3649 string_tree = tok->u.value;
3650 curr_type = tok->type;
3651 }
3652 type = curr_type;
3653
3654 /* Try to avoid the overhead of creating and destroying an obstack
3655 for the common case of just one string. */
3656 if (!cp_parser_is_string_literal
3657 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3658 {
3659 cp_lexer_consume_token (parser->lexer);
3660
3661 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3662 str.len = TREE_STRING_LENGTH (string_tree);
3663 count = 1;
3664
3665 if (curr_tok_is_userdef_p)
3666 {
3667 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3668 have_suffix_p = 1;
3669 curr_type = cpp_userdef_string_remove_type (tok->type);
3670 }
3671 else
3672 curr_type = tok->type;
3673
3674 strs = &str;
3675 }
3676 else
3677 {
3678 gcc_obstack_init (&str_ob);
3679 count = 0;
3680
3681 do
3682 {
3683 cp_lexer_consume_token (parser->lexer);
3684 count++;
3685 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3686 str.len = TREE_STRING_LENGTH (string_tree);
3687
3688 if (curr_tok_is_userdef_p)
3689 {
3690 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3691 if (have_suffix_p == 0)
3692 {
3693 suffix_id = curr_suffix_id;
3694 have_suffix_p = 1;
3695 }
3696 else if (have_suffix_p == 1
3697 && curr_suffix_id != suffix_id)
3698 {
3699 error ("inconsistent user-defined literal suffixes"
3700 " %qD and %qD in string literal",
3701 suffix_id, curr_suffix_id);
3702 have_suffix_p = -1;
3703 }
3704 curr_type = cpp_userdef_string_remove_type (tok->type);
3705 }
3706 else
3707 curr_type = tok->type;
3708
3709 if (type != curr_type)
3710 {
3711 if (type == CPP_STRING)
3712 type = curr_type;
3713 else if (curr_type != CPP_STRING)
3714 error_at (tok->location,
3715 "unsupported non-standard concatenation "
3716 "of string literals");
3717 }
3718
3719 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3720
3721 tok = cp_lexer_peek_token (parser->lexer);
3722 if (cpp_userdef_string_p (tok->type))
3723 {
3724 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3725 curr_type = cpp_userdef_string_remove_type (tok->type);
3726 curr_tok_is_userdef_p = true;
3727 }
3728 else
3729 {
3730 string_tree = tok->u.value;
3731 curr_type = tok->type;
3732 curr_tok_is_userdef_p = false;
3733 }
3734 }
3735 while (cp_parser_is_string_literal (tok));
3736
3737 strs = (cpp_string *) obstack_finish (&str_ob);
3738 }
3739
3740 if (type != CPP_STRING && !wide_ok)
3741 {
3742 cp_parser_error (parser, "a wide string is invalid in this context");
3743 type = CPP_STRING;
3744 }
3745
3746 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3747 (parse_in, strs, count, &istr, type))
3748 {
3749 value = build_string (istr.len, (const char *)istr.text);
3750 free (CONST_CAST (unsigned char *, istr.text));
3751
3752 switch (type)
3753 {
3754 default:
3755 case CPP_STRING:
3756 case CPP_UTF8STRING:
3757 TREE_TYPE (value) = char_array_type_node;
3758 break;
3759 case CPP_STRING16:
3760 TREE_TYPE (value) = char16_array_type_node;
3761 break;
3762 case CPP_STRING32:
3763 TREE_TYPE (value) = char32_array_type_node;
3764 break;
3765 case CPP_WSTRING:
3766 TREE_TYPE (value) = wchar_array_type_node;
3767 break;
3768 }
3769
3770 value = fix_string_type (value);
3771
3772 if (have_suffix_p)
3773 {
3774 tree literal = build_userdef_literal (suffix_id, value,
3775 OT_NONE, NULL_TREE);
3776 if (lookup_udlit)
3777 value = cp_parser_userdef_string_literal (literal);
3778 else
3779 value = literal;
3780 }
3781 }
3782 else
3783 /* cpp_interpret_string has issued an error. */
3784 value = error_mark_node;
3785
3786 if (count > 1)
3787 obstack_free (&str_ob, 0);
3788
3789 return value;
3790 }
3791
3792 /* Look up a literal operator with the name and the exact arguments. */
3793
3794 static tree
3795 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3796 {
3797 tree decl, fns;
3798 decl = lookup_name (name);
3799 if (!decl || !is_overloaded_fn (decl))
3800 return error_mark_node;
3801
3802 for (fns = decl; fns; fns = OVL_NEXT (fns))
3803 {
3804 unsigned int ix;
3805 bool found = true;
3806 tree fn = OVL_CURRENT (fns);
3807 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3808 if (parmtypes != NULL_TREE)
3809 {
3810 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3811 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3812 {
3813 tree tparm = TREE_VALUE (parmtypes);
3814 tree targ = TREE_TYPE ((*args)[ix]);
3815 bool ptr = TYPE_PTR_P (tparm);
3816 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3817 if ((ptr || arr || !same_type_p (tparm, targ))
3818 && (!ptr || !arr
3819 || !same_type_p (TREE_TYPE (tparm),
3820 TREE_TYPE (targ))))
3821 found = false;
3822 }
3823 if (found
3824 && ix == vec_safe_length (args)
3825 /* May be this should be sufficient_parms_p instead,
3826 depending on how exactly should user-defined literals
3827 work in presence of default arguments on the literal
3828 operator parameters. */
3829 && parmtypes == void_list_node)
3830 return fn;
3831 }
3832 }
3833
3834 return error_mark_node;
3835 }
3836
3837 /* Parse a user-defined char constant. Returns a call to a user-defined
3838 literal operator taking the character as an argument. */
3839
3840 static tree
3841 cp_parser_userdef_char_literal (cp_parser *parser)
3842 {
3843 cp_token *token = cp_lexer_consume_token (parser->lexer);
3844 tree literal = token->u.value;
3845 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3846 tree value = USERDEF_LITERAL_VALUE (literal);
3847 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3848 tree decl, result;
3849
3850 /* Build up a call to the user-defined operator */
3851 /* Lookup the name we got back from the id-expression. */
3852 vec<tree, va_gc> *args = make_tree_vector ();
3853 vec_safe_push (args, value);
3854 decl = lookup_literal_operator (name, args);
3855 if (!decl || decl == error_mark_node)
3856 {
3857 error ("unable to find character literal operator %qD with %qT argument",
3858 name, TREE_TYPE (value));
3859 release_tree_vector (args);
3860 return error_mark_node;
3861 }
3862 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3863 release_tree_vector (args);
3864 if (result != error_mark_node)
3865 return result;
3866
3867 error ("unable to find character literal operator %qD with %qT argument",
3868 name, TREE_TYPE (value));
3869 return error_mark_node;
3870 }
3871
3872 /* A subroutine of cp_parser_userdef_numeric_literal to
3873 create a char... template parameter pack from a string node. */
3874
3875 static tree
3876 make_char_string_pack (tree value)
3877 {
3878 tree charvec;
3879 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3880 const char *str = TREE_STRING_POINTER (value);
3881 int i, len = TREE_STRING_LENGTH (value) - 1;
3882 tree argvec = make_tree_vec (1);
3883
3884 /* Fill in CHARVEC with all of the parameters. */
3885 charvec = make_tree_vec (len);
3886 for (i = 0; i < len; ++i)
3887 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3888
3889 /* Build the argument packs. */
3890 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3891 TREE_TYPE (argpack) = char_type_node;
3892
3893 TREE_VEC_ELT (argvec, 0) = argpack;
3894
3895 return argvec;
3896 }
3897
3898 /* A subroutine of cp_parser_userdef_numeric_literal to
3899 create a char... template parameter pack from a string node. */
3900
3901 static tree
3902 make_string_pack (tree value)
3903 {
3904 tree charvec;
3905 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3906 const unsigned char *str
3907 = (const unsigned char *) TREE_STRING_POINTER (value);
3908 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3909 int len = TREE_STRING_LENGTH (value) / sz - 1;
3910 tree argvec = make_tree_vec (2);
3911
3912 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3913 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3914
3915 /* First template parm is character type. */
3916 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3917
3918 /* Fill in CHARVEC with all of the parameters. */
3919 charvec = make_tree_vec (len);
3920 for (int i = 0; i < len; ++i)
3921 TREE_VEC_ELT (charvec, i)
3922 = double_int_to_tree (str_char_type_node,
3923 double_int::from_buffer (str + i * sz, sz));
3924
3925 /* Build the argument packs. */
3926 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3927 TREE_TYPE (argpack) = str_char_type_node;
3928
3929 TREE_VEC_ELT (argvec, 1) = argpack;
3930
3931 return argvec;
3932 }
3933
3934 /* Parse a user-defined numeric constant. returns a call to a user-defined
3935 literal operator. */
3936
3937 static tree
3938 cp_parser_userdef_numeric_literal (cp_parser *parser)
3939 {
3940 cp_token *token = cp_lexer_consume_token (parser->lexer);
3941 tree literal = token->u.value;
3942 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3943 tree value = USERDEF_LITERAL_VALUE (literal);
3944 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3945 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3946 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3947 tree decl, result;
3948 vec<tree, va_gc> *args;
3949
3950 /* Look for a literal operator taking the exact type of numeric argument
3951 as the literal value. */
3952 args = make_tree_vector ();
3953 vec_safe_push (args, value);
3954 decl = lookup_literal_operator (name, args);
3955 if (decl && decl != error_mark_node)
3956 {
3957 result = finish_call_expr (decl, &args, false, true, tf_none);
3958 if (result != error_mark_node)
3959 {
3960 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3961 warning_at (token->location, OPT_Woverflow,
3962 "integer literal exceeds range of %qT type",
3963 long_long_unsigned_type_node);
3964 else
3965 {
3966 if (overflow > 0)
3967 warning_at (token->location, OPT_Woverflow,
3968 "floating literal exceeds range of %qT type",
3969 long_double_type_node);
3970 else if (overflow < 0)
3971 warning_at (token->location, OPT_Woverflow,
3972 "floating literal truncated to zero");
3973 }
3974 release_tree_vector (args);
3975 return result;
3976 }
3977 }
3978 release_tree_vector (args);
3979
3980 /* If the numeric argument didn't work, look for a raw literal
3981 operator taking a const char* argument consisting of the number
3982 in string format. */
3983 args = make_tree_vector ();
3984 vec_safe_push (args, num_string);
3985 decl = lookup_literal_operator (name, args);
3986 if (decl && decl != error_mark_node)
3987 {
3988 result = finish_call_expr (decl, &args, false, true, tf_none);
3989 if (result != error_mark_node)
3990 {
3991 release_tree_vector (args);
3992 return result;
3993 }
3994 }
3995 release_tree_vector (args);
3996
3997 /* If the raw literal didn't work, look for a non-type template
3998 function with parameter pack char.... Call the function with
3999 template parameter characters representing the number. */
4000 args = make_tree_vector ();
4001 decl = lookup_literal_operator (name, args);
4002 if (decl && decl != error_mark_node)
4003 {
4004 tree tmpl_args = make_char_string_pack (num_string);
4005 decl = lookup_template_function (decl, tmpl_args);
4006 result = finish_call_expr (decl, &args, false, true, tf_none);
4007 if (result != error_mark_node)
4008 {
4009 release_tree_vector (args);
4010 return result;
4011 }
4012 }
4013 release_tree_vector (args);
4014
4015 error ("unable to find numeric literal operator %qD", name);
4016 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4017 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4018 "to enable more built-in suffixes");
4019 return error_mark_node;
4020 }
4021
4022 /* Parse a user-defined string constant. Returns a call to a user-defined
4023 literal operator taking a character pointer and the length of the string
4024 as arguments. */
4025
4026 static tree
4027 cp_parser_userdef_string_literal (tree literal)
4028 {
4029 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4030 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4031 tree value = USERDEF_LITERAL_VALUE (literal);
4032 int len = TREE_STRING_LENGTH (value)
4033 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4034 tree decl, result;
4035 vec<tree, va_gc> *args;
4036
4037 /* Look for a template function with typename parameter CharT
4038 and parameter pack CharT... Call the function with
4039 template parameter characters representing the string. */
4040 args = make_tree_vector ();
4041 decl = lookup_literal_operator (name, args);
4042 if (decl && decl != error_mark_node)
4043 {
4044 tree tmpl_args = make_string_pack (value);
4045 decl = lookup_template_function (decl, tmpl_args);
4046 result = finish_call_expr (decl, &args, false, true, tf_none);
4047 if (result != error_mark_node)
4048 {
4049 release_tree_vector (args);
4050 return result;
4051 }
4052 }
4053 release_tree_vector (args);
4054
4055 /* Build up a call to the user-defined operator */
4056 /* Lookup the name we got back from the id-expression. */
4057 args = make_tree_vector ();
4058 vec_safe_push (args, value);
4059 vec_safe_push (args, build_int_cst (size_type_node, len));
4060 decl = lookup_name (name);
4061 if (!decl || decl == error_mark_node)
4062 {
4063 error ("unable to find string literal operator %qD", name);
4064 release_tree_vector (args);
4065 return error_mark_node;
4066 }
4067 result = finish_call_expr (decl, &args, false, true, tf_none);
4068 release_tree_vector (args);
4069 if (result != error_mark_node)
4070 return result;
4071
4072 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4073 name, TREE_TYPE (value), size_type_node);
4074 return error_mark_node;
4075 }
4076
4077
4078 /* Basic concepts [gram.basic] */
4079
4080 /* Parse a translation-unit.
4081
4082 translation-unit:
4083 declaration-seq [opt]
4084
4085 Returns TRUE if all went well. */
4086
4087 static bool
4088 cp_parser_translation_unit (cp_parser* parser)
4089 {
4090 /* The address of the first non-permanent object on the declarator
4091 obstack. */
4092 static void *declarator_obstack_base;
4093
4094 bool success;
4095
4096 /* Create the declarator obstack, if necessary. */
4097 if (!cp_error_declarator)
4098 {
4099 gcc_obstack_init (&declarator_obstack);
4100 /* Create the error declarator. */
4101 cp_error_declarator = make_declarator (cdk_error);
4102 /* Create the empty parameter list. */
4103 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4104 /* Remember where the base of the declarator obstack lies. */
4105 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4106 }
4107
4108 cp_parser_declaration_seq_opt (parser);
4109
4110 /* If there are no tokens left then all went well. */
4111 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4112 {
4113 /* Get rid of the token array; we don't need it any more. */
4114 cp_lexer_destroy (parser->lexer);
4115 parser->lexer = NULL;
4116
4117 /* This file might have been a context that's implicitly extern
4118 "C". If so, pop the lang context. (Only relevant for PCH.) */
4119 if (parser->implicit_extern_c)
4120 {
4121 pop_lang_context ();
4122 parser->implicit_extern_c = false;
4123 }
4124
4125 /* Finish up. */
4126 finish_translation_unit ();
4127
4128 success = true;
4129 }
4130 else
4131 {
4132 cp_parser_error (parser, "expected declaration");
4133 success = false;
4134 }
4135
4136 /* Make sure the declarator obstack was fully cleaned up. */
4137 gcc_assert (obstack_next_free (&declarator_obstack)
4138 == declarator_obstack_base);
4139
4140 /* All went well. */
4141 return success;
4142 }
4143
4144 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4145 decltype context. */
4146
4147 static inline tsubst_flags_t
4148 complain_flags (bool decltype_p)
4149 {
4150 tsubst_flags_t complain = tf_warning_or_error;
4151 if (decltype_p)
4152 complain |= tf_decltype;
4153 return complain;
4154 }
4155
4156 /* We're about to parse a collection of statements. If we're currently
4157 parsing tentatively, set up a firewall so that any nested
4158 cp_parser_commit_to_tentative_parse won't affect the current context. */
4159
4160 static cp_token_position
4161 cp_parser_start_tentative_firewall (cp_parser *parser)
4162 {
4163 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4164 return 0;
4165
4166 cp_parser_parse_tentatively (parser);
4167 cp_parser_commit_to_topmost_tentative_parse (parser);
4168 return cp_lexer_token_position (parser->lexer, false);
4169 }
4170
4171 /* We've finished parsing the collection of statements. Wrap up the
4172 firewall and replace the relevant tokens with the parsed form. */
4173
4174 static void
4175 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4176 tree expr)
4177 {
4178 if (!start)
4179 return;
4180
4181 /* Finish the firewall level. */
4182 cp_parser_parse_definitely (parser);
4183 /* And remember the result of the parse for when we try again. */
4184 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4185 token->type = CPP_PREPARSED_EXPR;
4186 token->u.value = expr;
4187 token->keyword = RID_MAX;
4188 cp_lexer_purge_tokens_after (parser->lexer, start);
4189 }
4190
4191 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4192 enclosing parentheses. */
4193
4194 static tree
4195 cp_parser_statement_expr (cp_parser *parser)
4196 {
4197 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4198
4199 /* Consume the '('. */
4200 cp_lexer_consume_token (parser->lexer);
4201 /* Start the statement-expression. */
4202 tree expr = begin_stmt_expr ();
4203 /* Parse the compound-statement. */
4204 cp_parser_compound_statement (parser, expr, false, false);
4205 /* Finish up. */
4206 expr = finish_stmt_expr (expr, false);
4207 /* Consume the ')'. */
4208 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4209 cp_parser_skip_to_end_of_statement (parser);
4210
4211 cp_parser_end_tentative_firewall (parser, start, expr);
4212 return expr;
4213 }
4214
4215 /* Expressions [gram.expr] */
4216
4217 /* Parse a primary-expression.
4218
4219 primary-expression:
4220 literal
4221 this
4222 ( expression )
4223 id-expression
4224 lambda-expression (C++11)
4225
4226 GNU Extensions:
4227
4228 primary-expression:
4229 ( compound-statement )
4230 __builtin_va_arg ( assignment-expression , type-id )
4231 __builtin_offsetof ( type-id , offsetof-expression )
4232
4233 C++ Extensions:
4234 __has_nothrow_assign ( type-id )
4235 __has_nothrow_constructor ( type-id )
4236 __has_nothrow_copy ( type-id )
4237 __has_trivial_assign ( type-id )
4238 __has_trivial_constructor ( type-id )
4239 __has_trivial_copy ( type-id )
4240 __has_trivial_destructor ( type-id )
4241 __has_virtual_destructor ( type-id )
4242 __is_abstract ( type-id )
4243 __is_base_of ( type-id , type-id )
4244 __is_class ( type-id )
4245 __is_empty ( type-id )
4246 __is_enum ( type-id )
4247 __is_final ( type-id )
4248 __is_literal_type ( type-id )
4249 __is_pod ( type-id )
4250 __is_polymorphic ( type-id )
4251 __is_std_layout ( type-id )
4252 __is_trivial ( type-id )
4253 __is_union ( type-id )
4254
4255 Objective-C++ Extension:
4256
4257 primary-expression:
4258 objc-expression
4259
4260 literal:
4261 __null
4262
4263 ADDRESS_P is true iff this expression was immediately preceded by
4264 "&" and therefore might denote a pointer-to-member. CAST_P is true
4265 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4266 true iff this expression is a template argument.
4267
4268 Returns a representation of the expression. Upon return, *IDK
4269 indicates what kind of id-expression (if any) was present. */
4270
4271 static tree
4272 cp_parser_primary_expression (cp_parser *parser,
4273 bool address_p,
4274 bool cast_p,
4275 bool template_arg_p,
4276 bool decltype_p,
4277 cp_id_kind *idk)
4278 {
4279 cp_token *token = NULL;
4280
4281 /* Assume the primary expression is not an id-expression. */
4282 *idk = CP_ID_KIND_NONE;
4283
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 switch ((int) token->type)
4287 {
4288 /* literal:
4289 integer-literal
4290 character-literal
4291 floating-literal
4292 string-literal
4293 boolean-literal
4294 pointer-literal
4295 user-defined-literal */
4296 case CPP_CHAR:
4297 case CPP_CHAR16:
4298 case CPP_CHAR32:
4299 case CPP_WCHAR:
4300 case CPP_NUMBER:
4301 case CPP_PREPARSED_EXPR:
4302 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4303 return cp_parser_userdef_numeric_literal (parser);
4304 token = cp_lexer_consume_token (parser->lexer);
4305 if (TREE_CODE (token->u.value) == FIXED_CST)
4306 {
4307 error_at (token->location,
4308 "fixed-point types not supported in C++");
4309 return error_mark_node;
4310 }
4311 /* Floating-point literals are only allowed in an integral
4312 constant expression if they are cast to an integral or
4313 enumeration type. */
4314 if (TREE_CODE (token->u.value) == REAL_CST
4315 && parser->integral_constant_expression_p
4316 && pedantic)
4317 {
4318 /* CAST_P will be set even in invalid code like "int(2.7 +
4319 ...)". Therefore, we have to check that the next token
4320 is sure to end the cast. */
4321 if (cast_p)
4322 {
4323 cp_token *next_token;
4324
4325 next_token = cp_lexer_peek_token (parser->lexer);
4326 if (/* The comma at the end of an
4327 enumerator-definition. */
4328 next_token->type != CPP_COMMA
4329 /* The curly brace at the end of an enum-specifier. */
4330 && next_token->type != CPP_CLOSE_BRACE
4331 /* The end of a statement. */
4332 && next_token->type != CPP_SEMICOLON
4333 /* The end of the cast-expression. */
4334 && next_token->type != CPP_CLOSE_PAREN
4335 /* The end of an array bound. */
4336 && next_token->type != CPP_CLOSE_SQUARE
4337 /* The closing ">" in a template-argument-list. */
4338 && (next_token->type != CPP_GREATER
4339 || parser->greater_than_is_operator_p)
4340 /* C++0x only: A ">>" treated like two ">" tokens,
4341 in a template-argument-list. */
4342 && (next_token->type != CPP_RSHIFT
4343 || (cxx_dialect == cxx98)
4344 || parser->greater_than_is_operator_p))
4345 cast_p = false;
4346 }
4347
4348 /* If we are within a cast, then the constraint that the
4349 cast is to an integral or enumeration type will be
4350 checked at that point. If we are not within a cast, then
4351 this code is invalid. */
4352 if (!cast_p)
4353 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4354 }
4355 return token->u.value;
4356
4357 case CPP_CHAR_USERDEF:
4358 case CPP_CHAR16_USERDEF:
4359 case CPP_CHAR32_USERDEF:
4360 case CPP_WCHAR_USERDEF:
4361 return cp_parser_userdef_char_literal (parser);
4362
4363 case CPP_STRING:
4364 case CPP_STRING16:
4365 case CPP_STRING32:
4366 case CPP_WSTRING:
4367 case CPP_UTF8STRING:
4368 case CPP_STRING_USERDEF:
4369 case CPP_STRING16_USERDEF:
4370 case CPP_STRING32_USERDEF:
4371 case CPP_WSTRING_USERDEF:
4372 case CPP_UTF8STRING_USERDEF:
4373 /* ??? Should wide strings be allowed when parser->translate_strings_p
4374 is false (i.e. in attributes)? If not, we can kill the third
4375 argument to cp_parser_string_literal. */
4376 return cp_parser_string_literal (parser,
4377 parser->translate_strings_p,
4378 true);
4379
4380 case CPP_OPEN_PAREN:
4381 /* If we see `( { ' then we are looking at the beginning of
4382 a GNU statement-expression. */
4383 if (cp_parser_allow_gnu_extensions_p (parser)
4384 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4385 {
4386 /* Statement-expressions are not allowed by the standard. */
4387 pedwarn (token->location, OPT_Wpedantic,
4388 "ISO C++ forbids braced-groups within expressions");
4389
4390 /* And they're not allowed outside of a function-body; you
4391 cannot, for example, write:
4392
4393 int i = ({ int j = 3; j + 1; });
4394
4395 at class or namespace scope. */
4396 if (!parser->in_function_body
4397 || parser->in_template_argument_list_p)
4398 {
4399 error_at (token->location,
4400 "statement-expressions are not allowed outside "
4401 "functions nor in template-argument lists");
4402 cp_parser_skip_to_end_of_block_or_statement (parser);
4403 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4404 cp_lexer_consume_token (parser->lexer);
4405 return error_mark_node;
4406 }
4407 else
4408 return cp_parser_statement_expr (parser);
4409 }
4410 /* Otherwise it's a normal parenthesized expression. */
4411 {
4412 tree expr;
4413 bool saved_greater_than_is_operator_p;
4414
4415 /* Consume the `('. */
4416 cp_lexer_consume_token (parser->lexer);
4417 /* Within a parenthesized expression, a `>' token is always
4418 the greater-than operator. */
4419 saved_greater_than_is_operator_p
4420 = parser->greater_than_is_operator_p;
4421 parser->greater_than_is_operator_p = true;
4422
4423 /* Parse the parenthesized expression. */
4424 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4425 /* Let the front end know that this expression was
4426 enclosed in parentheses. This matters in case, for
4427 example, the expression is of the form `A::B', since
4428 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4429 not. */
4430 expr = finish_parenthesized_expr (expr);
4431 /* DR 705: Wrapping an unqualified name in parentheses
4432 suppresses arg-dependent lookup. We want to pass back
4433 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4434 (c++/37862), but none of the others. */
4435 if (*idk != CP_ID_KIND_QUALIFIED)
4436 *idk = CP_ID_KIND_NONE;
4437
4438 /* The `>' token might be the end of a template-id or
4439 template-parameter-list now. */
4440 parser->greater_than_is_operator_p
4441 = saved_greater_than_is_operator_p;
4442 /* Consume the `)'. */
4443 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4444 cp_parser_skip_to_end_of_statement (parser);
4445
4446 return expr;
4447 }
4448
4449 case CPP_OPEN_SQUARE:
4450 {
4451 if (c_dialect_objc ())
4452 {
4453 /* We might have an Objective-C++ message. */
4454 cp_parser_parse_tentatively (parser);
4455 tree msg = cp_parser_objc_message_expression (parser);
4456 /* If that works out, we're done ... */
4457 if (cp_parser_parse_definitely (parser))
4458 return msg;
4459 /* ... else, fall though to see if it's a lambda. */
4460 }
4461 tree lam = cp_parser_lambda_expression (parser);
4462 /* Don't warn about a failed tentative parse. */
4463 if (cp_parser_error_occurred (parser))
4464 return error_mark_node;
4465 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4466 return lam;
4467 }
4468
4469 case CPP_OBJC_STRING:
4470 if (c_dialect_objc ())
4471 /* We have an Objective-C++ string literal. */
4472 return cp_parser_objc_expression (parser);
4473 cp_parser_error (parser, "expected primary-expression");
4474 return error_mark_node;
4475
4476 case CPP_KEYWORD:
4477 switch (token->keyword)
4478 {
4479 /* These two are the boolean literals. */
4480 case RID_TRUE:
4481 cp_lexer_consume_token (parser->lexer);
4482 return boolean_true_node;
4483 case RID_FALSE:
4484 cp_lexer_consume_token (parser->lexer);
4485 return boolean_false_node;
4486
4487 /* The `__null' literal. */
4488 case RID_NULL:
4489 cp_lexer_consume_token (parser->lexer);
4490 return null_node;
4491
4492 /* The `nullptr' literal. */
4493 case RID_NULLPTR:
4494 cp_lexer_consume_token (parser->lexer);
4495 return nullptr_node;
4496
4497 /* Recognize the `this' keyword. */
4498 case RID_THIS:
4499 cp_lexer_consume_token (parser->lexer);
4500 if (parser->local_variables_forbidden_p)
4501 {
4502 error_at (token->location,
4503 "%<this%> may not be used in this context");
4504 return error_mark_node;
4505 }
4506 /* Pointers cannot appear in constant-expressions. */
4507 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4508 return error_mark_node;
4509 return finish_this_expr ();
4510
4511 /* The `operator' keyword can be the beginning of an
4512 id-expression. */
4513 case RID_OPERATOR:
4514 goto id_expression;
4515
4516 case RID_FUNCTION_NAME:
4517 case RID_PRETTY_FUNCTION_NAME:
4518 case RID_C99_FUNCTION_NAME:
4519 {
4520 non_integral_constant name;
4521
4522 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4523 __func__ are the names of variables -- but they are
4524 treated specially. Therefore, they are handled here,
4525 rather than relying on the generic id-expression logic
4526 below. Grammatically, these names are id-expressions.
4527
4528 Consume the token. */
4529 token = cp_lexer_consume_token (parser->lexer);
4530
4531 switch (token->keyword)
4532 {
4533 case RID_FUNCTION_NAME:
4534 name = NIC_FUNC_NAME;
4535 break;
4536 case RID_PRETTY_FUNCTION_NAME:
4537 name = NIC_PRETTY_FUNC;
4538 break;
4539 case RID_C99_FUNCTION_NAME:
4540 name = NIC_C99_FUNC;
4541 break;
4542 default:
4543 gcc_unreachable ();
4544 }
4545
4546 if (cp_parser_non_integral_constant_expression (parser, name))
4547 return error_mark_node;
4548
4549 /* Look up the name. */
4550 return finish_fname (token->u.value);
4551 }
4552
4553 case RID_VA_ARG:
4554 {
4555 tree expression;
4556 tree type;
4557 source_location type_location;
4558
4559 /* The `__builtin_va_arg' construct is used to handle
4560 `va_arg'. Consume the `__builtin_va_arg' token. */
4561 cp_lexer_consume_token (parser->lexer);
4562 /* Look for the opening `('. */
4563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4564 /* Now, parse the assignment-expression. */
4565 expression = cp_parser_assignment_expression (parser);
4566 /* Look for the `,'. */
4567 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4568 type_location = cp_lexer_peek_token (parser->lexer)->location;
4569 /* Parse the type-id. */
4570 type = cp_parser_type_id (parser);
4571 /* Look for the closing `)'. */
4572 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4573 /* Using `va_arg' in a constant-expression is not
4574 allowed. */
4575 if (cp_parser_non_integral_constant_expression (parser,
4576 NIC_VA_ARG))
4577 return error_mark_node;
4578 return build_x_va_arg (type_location, expression, type);
4579 }
4580
4581 case RID_OFFSETOF:
4582 return cp_parser_builtin_offsetof (parser);
4583
4584 case RID_HAS_NOTHROW_ASSIGN:
4585 case RID_HAS_NOTHROW_CONSTRUCTOR:
4586 case RID_HAS_NOTHROW_COPY:
4587 case RID_HAS_TRIVIAL_ASSIGN:
4588 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4589 case RID_HAS_TRIVIAL_COPY:
4590 case RID_HAS_TRIVIAL_DESTRUCTOR:
4591 case RID_HAS_VIRTUAL_DESTRUCTOR:
4592 case RID_IS_ABSTRACT:
4593 case RID_IS_BASE_OF:
4594 case RID_IS_CLASS:
4595 case RID_IS_EMPTY:
4596 case RID_IS_ENUM:
4597 case RID_IS_FINAL:
4598 case RID_IS_LITERAL_TYPE:
4599 case RID_IS_POD:
4600 case RID_IS_POLYMORPHIC:
4601 case RID_IS_STD_LAYOUT:
4602 case RID_IS_TRIVIAL:
4603 case RID_IS_TRIVIALLY_ASSIGNABLE:
4604 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4605 case RID_IS_TRIVIALLY_COPYABLE:
4606 case RID_IS_UNION:
4607 return cp_parser_trait_expr (parser, token->keyword);
4608
4609 /* Objective-C++ expressions. */
4610 case RID_AT_ENCODE:
4611 case RID_AT_PROTOCOL:
4612 case RID_AT_SELECTOR:
4613 return cp_parser_objc_expression (parser);
4614
4615 case RID_TEMPLATE:
4616 if (parser->in_function_body
4617 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4618 == CPP_LESS))
4619 {
4620 error_at (token->location,
4621 "a template declaration cannot appear at block scope");
4622 cp_parser_skip_to_end_of_block_or_statement (parser);
4623 return error_mark_node;
4624 }
4625 default:
4626 cp_parser_error (parser, "expected primary-expression");
4627 return error_mark_node;
4628 }
4629
4630 /* An id-expression can start with either an identifier, a
4631 `::' as the beginning of a qualified-id, or the "operator"
4632 keyword. */
4633 case CPP_NAME:
4634 case CPP_SCOPE:
4635 case CPP_TEMPLATE_ID:
4636 case CPP_NESTED_NAME_SPECIFIER:
4637 {
4638 tree id_expression;
4639 tree decl;
4640 const char *error_msg;
4641 bool template_p;
4642 bool done;
4643 cp_token *id_expr_token;
4644
4645 id_expression:
4646 /* Parse the id-expression. */
4647 id_expression
4648 = cp_parser_id_expression (parser,
4649 /*template_keyword_p=*/false,
4650 /*check_dependency_p=*/true,
4651 &template_p,
4652 /*declarator_p=*/false,
4653 /*optional_p=*/false);
4654 if (id_expression == error_mark_node)
4655 return error_mark_node;
4656 id_expr_token = token;
4657 token = cp_lexer_peek_token (parser->lexer);
4658 done = (token->type != CPP_OPEN_SQUARE
4659 && token->type != CPP_OPEN_PAREN
4660 && token->type != CPP_DOT
4661 && token->type != CPP_DEREF
4662 && token->type != CPP_PLUS_PLUS
4663 && token->type != CPP_MINUS_MINUS);
4664 /* If we have a template-id, then no further lookup is
4665 required. If the template-id was for a template-class, we
4666 will sometimes have a TYPE_DECL at this point. */
4667 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4668 || TREE_CODE (id_expression) == TYPE_DECL)
4669 decl = id_expression;
4670 /* Look up the name. */
4671 else
4672 {
4673 tree ambiguous_decls;
4674
4675 /* If we already know that this lookup is ambiguous, then
4676 we've already issued an error message; there's no reason
4677 to check again. */
4678 if (id_expr_token->type == CPP_NAME
4679 && id_expr_token->error_reported)
4680 {
4681 cp_parser_simulate_error (parser);
4682 return error_mark_node;
4683 }
4684
4685 decl = cp_parser_lookup_name (parser, id_expression,
4686 none_type,
4687 template_p,
4688 /*is_namespace=*/false,
4689 /*check_dependency=*/true,
4690 &ambiguous_decls,
4691 id_expr_token->location);
4692 /* If the lookup was ambiguous, an error will already have
4693 been issued. */
4694 if (ambiguous_decls)
4695 return error_mark_node;
4696
4697 /* In Objective-C++, we may have an Objective-C 2.0
4698 dot-syntax for classes here. */
4699 if (c_dialect_objc ()
4700 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4701 && TREE_CODE (decl) == TYPE_DECL
4702 && objc_is_class_name (decl))
4703 {
4704 tree component;
4705 cp_lexer_consume_token (parser->lexer);
4706 component = cp_parser_identifier (parser);
4707 if (component == error_mark_node)
4708 return error_mark_node;
4709
4710 return objc_build_class_component_ref (id_expression, component);
4711 }
4712
4713 /* In Objective-C++, an instance variable (ivar) may be preferred
4714 to whatever cp_parser_lookup_name() found. */
4715 decl = objc_lookup_ivar (decl, id_expression);
4716
4717 /* If name lookup gives us a SCOPE_REF, then the
4718 qualifying scope was dependent. */
4719 if (TREE_CODE (decl) == SCOPE_REF)
4720 {
4721 /* At this point, we do not know if DECL is a valid
4722 integral constant expression. We assume that it is
4723 in fact such an expression, so that code like:
4724
4725 template <int N> struct A {
4726 int a[B<N>::i];
4727 };
4728
4729 is accepted. At template-instantiation time, we
4730 will check that B<N>::i is actually a constant. */
4731 return decl;
4732 }
4733 /* Check to see if DECL is a local variable in a context
4734 where that is forbidden. */
4735 if (parser->local_variables_forbidden_p
4736 && local_variable_p (decl))
4737 {
4738 /* It might be that we only found DECL because we are
4739 trying to be generous with pre-ISO scoping rules.
4740 For example, consider:
4741
4742 int i;
4743 void g() {
4744 for (int i = 0; i < 10; ++i) {}
4745 extern void f(int j = i);
4746 }
4747
4748 Here, name look up will originally find the out
4749 of scope `i'. We need to issue a warning message,
4750 but then use the global `i'. */
4751 decl = check_for_out_of_scope_variable (decl);
4752 if (local_variable_p (decl))
4753 {
4754 error_at (id_expr_token->location,
4755 "local variable %qD may not appear in this context",
4756 decl);
4757 return error_mark_node;
4758 }
4759 }
4760 }
4761
4762 decl = (finish_id_expression
4763 (id_expression, decl, parser->scope,
4764 idk,
4765 parser->integral_constant_expression_p,
4766 parser->allow_non_integral_constant_expression_p,
4767 &parser->non_integral_constant_expression_p,
4768 template_p, done, address_p,
4769 template_arg_p,
4770 &error_msg,
4771 id_expr_token->location));
4772 if (error_msg)
4773 cp_parser_error (parser, error_msg);
4774 return decl;
4775 }
4776
4777 /* Anything else is an error. */
4778 default:
4779 cp_parser_error (parser, "expected primary-expression");
4780 return error_mark_node;
4781 }
4782 }
4783
4784 static inline tree
4785 cp_parser_primary_expression (cp_parser *parser,
4786 bool address_p,
4787 bool cast_p,
4788 bool template_arg_p,
4789 cp_id_kind *idk)
4790 {
4791 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4792 /*decltype*/false, idk);
4793 }
4794
4795 /* Parse an id-expression.
4796
4797 id-expression:
4798 unqualified-id
4799 qualified-id
4800
4801 qualified-id:
4802 :: [opt] nested-name-specifier template [opt] unqualified-id
4803 :: identifier
4804 :: operator-function-id
4805 :: template-id
4806
4807 Return a representation of the unqualified portion of the
4808 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4809 a `::' or nested-name-specifier.
4810
4811 Often, if the id-expression was a qualified-id, the caller will
4812 want to make a SCOPE_REF to represent the qualified-id. This
4813 function does not do this in order to avoid wastefully creating
4814 SCOPE_REFs when they are not required.
4815
4816 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4817 `template' keyword.
4818
4819 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4820 uninstantiated templates.
4821
4822 If *TEMPLATE_P is non-NULL, it is set to true iff the
4823 `template' keyword is used to explicitly indicate that the entity
4824 named is a template.
4825
4826 If DECLARATOR_P is true, the id-expression is appearing as part of
4827 a declarator, rather than as part of an expression. */
4828
4829 static tree
4830 cp_parser_id_expression (cp_parser *parser,
4831 bool template_keyword_p,
4832 bool check_dependency_p,
4833 bool *template_p,
4834 bool declarator_p,
4835 bool optional_p)
4836 {
4837 bool global_scope_p;
4838 bool nested_name_specifier_p;
4839
4840 /* Assume the `template' keyword was not used. */
4841 if (template_p)
4842 *template_p = template_keyword_p;
4843
4844 /* Look for the optional `::' operator. */
4845 global_scope_p
4846 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4847 != NULL_TREE);
4848 /* Look for the optional nested-name-specifier. */
4849 nested_name_specifier_p
4850 = (cp_parser_nested_name_specifier_opt (parser,
4851 /*typename_keyword_p=*/false,
4852 check_dependency_p,
4853 /*type_p=*/false,
4854 declarator_p)
4855 != NULL_TREE);
4856 /* If there is a nested-name-specifier, then we are looking at
4857 the first qualified-id production. */
4858 if (nested_name_specifier_p)
4859 {
4860 tree saved_scope;
4861 tree saved_object_scope;
4862 tree saved_qualifying_scope;
4863 tree unqualified_id;
4864 bool is_template;
4865
4866 /* See if the next token is the `template' keyword. */
4867 if (!template_p)
4868 template_p = &is_template;
4869 *template_p = cp_parser_optional_template_keyword (parser);
4870 /* Name lookup we do during the processing of the
4871 unqualified-id might obliterate SCOPE. */
4872 saved_scope = parser->scope;
4873 saved_object_scope = parser->object_scope;
4874 saved_qualifying_scope = parser->qualifying_scope;
4875 /* Process the final unqualified-id. */
4876 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4877 check_dependency_p,
4878 declarator_p,
4879 /*optional_p=*/false);
4880 /* Restore the SAVED_SCOPE for our caller. */
4881 parser->scope = saved_scope;
4882 parser->object_scope = saved_object_scope;
4883 parser->qualifying_scope = saved_qualifying_scope;
4884
4885 return unqualified_id;
4886 }
4887 /* Otherwise, if we are in global scope, then we are looking at one
4888 of the other qualified-id productions. */
4889 else if (global_scope_p)
4890 {
4891 cp_token *token;
4892 tree id;
4893
4894 /* Peek at the next token. */
4895 token = cp_lexer_peek_token (parser->lexer);
4896
4897 /* If it's an identifier, and the next token is not a "<", then
4898 we can avoid the template-id case. This is an optimization
4899 for this common case. */
4900 if (token->type == CPP_NAME
4901 && !cp_parser_nth_token_starts_template_argument_list_p
4902 (parser, 2))
4903 return cp_parser_identifier (parser);
4904
4905 cp_parser_parse_tentatively (parser);
4906 /* Try a template-id. */
4907 id = cp_parser_template_id (parser,
4908 /*template_keyword_p=*/false,
4909 /*check_dependency_p=*/true,
4910 none_type,
4911 declarator_p);
4912 /* If that worked, we're done. */
4913 if (cp_parser_parse_definitely (parser))
4914 return id;
4915
4916 /* Peek at the next token. (Changes in the token buffer may
4917 have invalidated the pointer obtained above.) */
4918 token = cp_lexer_peek_token (parser->lexer);
4919
4920 switch (token->type)
4921 {
4922 case CPP_NAME:
4923 return cp_parser_identifier (parser);
4924
4925 case CPP_KEYWORD:
4926 if (token->keyword == RID_OPERATOR)
4927 return cp_parser_operator_function_id (parser);
4928 /* Fall through. */
4929
4930 default:
4931 cp_parser_error (parser, "expected id-expression");
4932 return error_mark_node;
4933 }
4934 }
4935 else
4936 return cp_parser_unqualified_id (parser, template_keyword_p,
4937 /*check_dependency_p=*/true,
4938 declarator_p,
4939 optional_p);
4940 }
4941
4942 /* Parse an unqualified-id.
4943
4944 unqualified-id:
4945 identifier
4946 operator-function-id
4947 conversion-function-id
4948 ~ class-name
4949 template-id
4950
4951 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4952 keyword, in a construct like `A::template ...'.
4953
4954 Returns a representation of unqualified-id. For the `identifier'
4955 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4956 production a BIT_NOT_EXPR is returned; the operand of the
4957 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4958 other productions, see the documentation accompanying the
4959 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4960 names are looked up in uninstantiated templates. If DECLARATOR_P
4961 is true, the unqualified-id is appearing as part of a declarator,
4962 rather than as part of an expression. */
4963
4964 static tree
4965 cp_parser_unqualified_id (cp_parser* parser,
4966 bool template_keyword_p,
4967 bool check_dependency_p,
4968 bool declarator_p,
4969 bool optional_p)
4970 {
4971 cp_token *token;
4972
4973 /* Peek at the next token. */
4974 token = cp_lexer_peek_token (parser->lexer);
4975
4976 switch ((int) token->type)
4977 {
4978 case CPP_NAME:
4979 {
4980 tree id;
4981
4982 /* We don't know yet whether or not this will be a
4983 template-id. */
4984 cp_parser_parse_tentatively (parser);
4985 /* Try a template-id. */
4986 id = cp_parser_template_id (parser, template_keyword_p,
4987 check_dependency_p,
4988 none_type,
4989 declarator_p);
4990 /* If it worked, we're done. */
4991 if (cp_parser_parse_definitely (parser))
4992 return id;
4993 /* Otherwise, it's an ordinary identifier. */
4994 return cp_parser_identifier (parser);
4995 }
4996
4997 case CPP_TEMPLATE_ID:
4998 return cp_parser_template_id (parser, template_keyword_p,
4999 check_dependency_p,
5000 none_type,
5001 declarator_p);
5002
5003 case CPP_COMPL:
5004 {
5005 tree type_decl;
5006 tree qualifying_scope;
5007 tree object_scope;
5008 tree scope;
5009 bool done;
5010
5011 /* Consume the `~' token. */
5012 cp_lexer_consume_token (parser->lexer);
5013 /* Parse the class-name. The standard, as written, seems to
5014 say that:
5015
5016 template <typename T> struct S { ~S (); };
5017 template <typename T> S<T>::~S() {}
5018
5019 is invalid, since `~' must be followed by a class-name, but
5020 `S<T>' is dependent, and so not known to be a class.
5021 That's not right; we need to look in uninstantiated
5022 templates. A further complication arises from:
5023
5024 template <typename T> void f(T t) {
5025 t.T::~T();
5026 }
5027
5028 Here, it is not possible to look up `T' in the scope of `T'
5029 itself. We must look in both the current scope, and the
5030 scope of the containing complete expression.
5031
5032 Yet another issue is:
5033
5034 struct S {
5035 int S;
5036 ~S();
5037 };
5038
5039 S::~S() {}
5040
5041 The standard does not seem to say that the `S' in `~S'
5042 should refer to the type `S' and not the data member
5043 `S::S'. */
5044
5045 /* DR 244 says that we look up the name after the "~" in the
5046 same scope as we looked up the qualifying name. That idea
5047 isn't fully worked out; it's more complicated than that. */
5048 scope = parser->scope;
5049 object_scope = parser->object_scope;
5050 qualifying_scope = parser->qualifying_scope;
5051
5052 /* Check for invalid scopes. */
5053 if (scope == error_mark_node)
5054 {
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5056 cp_lexer_consume_token (parser->lexer);
5057 return error_mark_node;
5058 }
5059 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5060 {
5061 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5062 error_at (token->location,
5063 "scope %qT before %<~%> is not a class-name",
5064 scope);
5065 cp_parser_simulate_error (parser);
5066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5067 cp_lexer_consume_token (parser->lexer);
5068 return error_mark_node;
5069 }
5070 gcc_assert (!scope || TYPE_P (scope));
5071
5072 /* If the name is of the form "X::~X" it's OK even if X is a
5073 typedef. */
5074 token = cp_lexer_peek_token (parser->lexer);
5075 if (scope
5076 && token->type == CPP_NAME
5077 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5078 != CPP_LESS)
5079 && (token->u.value == TYPE_IDENTIFIER (scope)
5080 || (CLASS_TYPE_P (scope)
5081 && constructor_name_p (token->u.value, scope))))
5082 {
5083 cp_lexer_consume_token (parser->lexer);
5084 return build_nt (BIT_NOT_EXPR, scope);
5085 }
5086
5087 /* ~auto means the destructor of whatever the object is. */
5088 if (cp_parser_is_keyword (token, RID_AUTO))
5089 {
5090 if (cxx_dialect < cxx14)
5091 pedwarn (input_location, 0,
5092 "%<~auto%> only available with "
5093 "-std=c++14 or -std=gnu++14");
5094 cp_lexer_consume_token (parser->lexer);
5095 return build_nt (BIT_NOT_EXPR, make_auto ());
5096 }
5097
5098 /* If there was an explicit qualification (S::~T), first look
5099 in the scope given by the qualification (i.e., S).
5100
5101 Note: in the calls to cp_parser_class_name below we pass
5102 typename_type so that lookup finds the injected-class-name
5103 rather than the constructor. */
5104 done = false;
5105 type_decl = NULL_TREE;
5106 if (scope)
5107 {
5108 cp_parser_parse_tentatively (parser);
5109 type_decl = cp_parser_class_name (parser,
5110 /*typename_keyword_p=*/false,
5111 /*template_keyword_p=*/false,
5112 typename_type,
5113 /*check_dependency=*/false,
5114 /*class_head_p=*/false,
5115 declarator_p);
5116 if (cp_parser_parse_definitely (parser))
5117 done = true;
5118 }
5119 /* In "N::S::~S", look in "N" as well. */
5120 if (!done && scope && qualifying_scope)
5121 {
5122 cp_parser_parse_tentatively (parser);
5123 parser->scope = qualifying_scope;
5124 parser->object_scope = NULL_TREE;
5125 parser->qualifying_scope = NULL_TREE;
5126 type_decl
5127 = cp_parser_class_name (parser,
5128 /*typename_keyword_p=*/false,
5129 /*template_keyword_p=*/false,
5130 typename_type,
5131 /*check_dependency=*/false,
5132 /*class_head_p=*/false,
5133 declarator_p);
5134 if (cp_parser_parse_definitely (parser))
5135 done = true;
5136 }
5137 /* In "p->S::~T", look in the scope given by "*p" as well. */
5138 else if (!done && object_scope)
5139 {
5140 cp_parser_parse_tentatively (parser);
5141 parser->scope = object_scope;
5142 parser->object_scope = NULL_TREE;
5143 parser->qualifying_scope = NULL_TREE;
5144 type_decl
5145 = cp_parser_class_name (parser,
5146 /*typename_keyword_p=*/false,
5147 /*template_keyword_p=*/false,
5148 typename_type,
5149 /*check_dependency=*/false,
5150 /*class_head_p=*/false,
5151 declarator_p);
5152 if (cp_parser_parse_definitely (parser))
5153 done = true;
5154 }
5155 /* Look in the surrounding context. */
5156 if (!done)
5157 {
5158 parser->scope = NULL_TREE;
5159 parser->object_scope = NULL_TREE;
5160 parser->qualifying_scope = NULL_TREE;
5161 if (processing_template_decl)
5162 cp_parser_parse_tentatively (parser);
5163 type_decl
5164 = cp_parser_class_name (parser,
5165 /*typename_keyword_p=*/false,
5166 /*template_keyword_p=*/false,
5167 typename_type,
5168 /*check_dependency=*/false,
5169 /*class_head_p=*/false,
5170 declarator_p);
5171 if (processing_template_decl
5172 && ! cp_parser_parse_definitely (parser))
5173 {
5174 /* We couldn't find a type with this name, so just accept
5175 it and check for a match at instantiation time. */
5176 type_decl = cp_parser_identifier (parser);
5177 if (type_decl != error_mark_node)
5178 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5179 return type_decl;
5180 }
5181 }
5182 /* If an error occurred, assume that the name of the
5183 destructor is the same as the name of the qualifying
5184 class. That allows us to keep parsing after running
5185 into ill-formed destructor names. */
5186 if (type_decl == error_mark_node && scope)
5187 return build_nt (BIT_NOT_EXPR, scope);
5188 else if (type_decl == error_mark_node)
5189 return error_mark_node;
5190
5191 /* Check that destructor name and scope match. */
5192 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5193 {
5194 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5195 error_at (token->location,
5196 "declaration of %<~%T%> as member of %qT",
5197 type_decl, scope);
5198 cp_parser_simulate_error (parser);
5199 return error_mark_node;
5200 }
5201
5202 /* [class.dtor]
5203
5204 A typedef-name that names a class shall not be used as the
5205 identifier in the declarator for a destructor declaration. */
5206 if (declarator_p
5207 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5208 && !DECL_SELF_REFERENCE_P (type_decl)
5209 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5210 error_at (token->location,
5211 "typedef-name %qD used as destructor declarator",
5212 type_decl);
5213
5214 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5215 }
5216
5217 case CPP_KEYWORD:
5218 if (token->keyword == RID_OPERATOR)
5219 {
5220 tree id;
5221
5222 /* This could be a template-id, so we try that first. */
5223 cp_parser_parse_tentatively (parser);
5224 /* Try a template-id. */
5225 id = cp_parser_template_id (parser, template_keyword_p,
5226 /*check_dependency_p=*/true,
5227 none_type,
5228 declarator_p);
5229 /* If that worked, we're done. */
5230 if (cp_parser_parse_definitely (parser))
5231 return id;
5232 /* We still don't know whether we're looking at an
5233 operator-function-id or a conversion-function-id. */
5234 cp_parser_parse_tentatively (parser);
5235 /* Try an operator-function-id. */
5236 id = cp_parser_operator_function_id (parser);
5237 /* If that didn't work, try a conversion-function-id. */
5238 if (!cp_parser_parse_definitely (parser))
5239 id = cp_parser_conversion_function_id (parser);
5240 else if (UDLIT_OPER_P (id))
5241 {
5242 /* 17.6.3.3.5 */
5243 const char *name = UDLIT_OP_SUFFIX (id);
5244 if (name[0] != '_' && !in_system_header_at (input_location)
5245 && declarator_p)
5246 warning (0, "literal operator suffixes not preceded by %<_%>"
5247 " are reserved for future standardization");
5248 }
5249
5250 return id;
5251 }
5252 /* Fall through. */
5253
5254 default:
5255 if (optional_p)
5256 return NULL_TREE;
5257 cp_parser_error (parser, "expected unqualified-id");
5258 return error_mark_node;
5259 }
5260 }
5261
5262 /* Parse an (optional) nested-name-specifier.
5263
5264 nested-name-specifier: [C++98]
5265 class-or-namespace-name :: nested-name-specifier [opt]
5266 class-or-namespace-name :: template nested-name-specifier [opt]
5267
5268 nested-name-specifier: [C++0x]
5269 type-name ::
5270 namespace-name ::
5271 nested-name-specifier identifier ::
5272 nested-name-specifier template [opt] simple-template-id ::
5273
5274 PARSER->SCOPE should be set appropriately before this function is
5275 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5276 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5277 in name lookups.
5278
5279 Sets PARSER->SCOPE to the class (TYPE) or namespace
5280 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5281 it unchanged if there is no nested-name-specifier. Returns the new
5282 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5283
5284 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5285 part of a declaration and/or decl-specifier. */
5286
5287 static tree
5288 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5289 bool typename_keyword_p,
5290 bool check_dependency_p,
5291 bool type_p,
5292 bool is_declaration)
5293 {
5294 bool success = false;
5295 cp_token_position start = 0;
5296 cp_token *token;
5297
5298 /* Remember where the nested-name-specifier starts. */
5299 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5300 {
5301 start = cp_lexer_token_position (parser->lexer, false);
5302 push_deferring_access_checks (dk_deferred);
5303 }
5304
5305 while (true)
5306 {
5307 tree new_scope;
5308 tree old_scope;
5309 tree saved_qualifying_scope;
5310 bool template_keyword_p;
5311
5312 /* Spot cases that cannot be the beginning of a
5313 nested-name-specifier. */
5314 token = cp_lexer_peek_token (parser->lexer);
5315
5316 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5317 the already parsed nested-name-specifier. */
5318 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5319 {
5320 /* Grab the nested-name-specifier and continue the loop. */
5321 cp_parser_pre_parsed_nested_name_specifier (parser);
5322 /* If we originally encountered this nested-name-specifier
5323 with IS_DECLARATION set to false, we will not have
5324 resolved TYPENAME_TYPEs, so we must do so here. */
5325 if (is_declaration
5326 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5327 {
5328 new_scope = resolve_typename_type (parser->scope,
5329 /*only_current_p=*/false);
5330 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5331 parser->scope = new_scope;
5332 }
5333 success = true;
5334 continue;
5335 }
5336
5337 /* Spot cases that cannot be the beginning of a
5338 nested-name-specifier. On the second and subsequent times
5339 through the loop, we look for the `template' keyword. */
5340 if (success && token->keyword == RID_TEMPLATE)
5341 ;
5342 /* A template-id can start a nested-name-specifier. */
5343 else if (token->type == CPP_TEMPLATE_ID)
5344 ;
5345 /* DR 743: decltype can be used in a nested-name-specifier. */
5346 else if (token_is_decltype (token))
5347 ;
5348 else
5349 {
5350 /* If the next token is not an identifier, then it is
5351 definitely not a type-name or namespace-name. */
5352 if (token->type != CPP_NAME)
5353 break;
5354 /* If the following token is neither a `<' (to begin a
5355 template-id), nor a `::', then we are not looking at a
5356 nested-name-specifier. */
5357 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5358
5359 if (token->type == CPP_COLON
5360 && parser->colon_corrects_to_scope_p
5361 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5362 {
5363 error_at (token->location,
5364 "found %<:%> in nested-name-specifier, expected %<::%>");
5365 token->type = CPP_SCOPE;
5366 }
5367
5368 if (token->type != CPP_SCOPE
5369 && !cp_parser_nth_token_starts_template_argument_list_p
5370 (parser, 2))
5371 break;
5372 }
5373
5374 /* The nested-name-specifier is optional, so we parse
5375 tentatively. */
5376 cp_parser_parse_tentatively (parser);
5377
5378 /* Look for the optional `template' keyword, if this isn't the
5379 first time through the loop. */
5380 if (success)
5381 template_keyword_p = cp_parser_optional_template_keyword (parser);
5382 else
5383 template_keyword_p = false;
5384
5385 /* Save the old scope since the name lookup we are about to do
5386 might destroy it. */
5387 old_scope = parser->scope;
5388 saved_qualifying_scope = parser->qualifying_scope;
5389 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5390 look up names in "X<T>::I" in order to determine that "Y" is
5391 a template. So, if we have a typename at this point, we make
5392 an effort to look through it. */
5393 if (is_declaration
5394 && !typename_keyword_p
5395 && parser->scope
5396 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5397 parser->scope = resolve_typename_type (parser->scope,
5398 /*only_current_p=*/false);
5399 /* Parse the qualifying entity. */
5400 new_scope
5401 = cp_parser_qualifying_entity (parser,
5402 typename_keyword_p,
5403 template_keyword_p,
5404 check_dependency_p,
5405 type_p,
5406 is_declaration);
5407 /* Look for the `::' token. */
5408 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5409
5410 /* If we found what we wanted, we keep going; otherwise, we're
5411 done. */
5412 if (!cp_parser_parse_definitely (parser))
5413 {
5414 bool error_p = false;
5415
5416 /* Restore the OLD_SCOPE since it was valid before the
5417 failed attempt at finding the last
5418 class-or-namespace-name. */
5419 parser->scope = old_scope;
5420 parser->qualifying_scope = saved_qualifying_scope;
5421
5422 /* If the next token is a decltype, and the one after that is a
5423 `::', then the decltype has failed to resolve to a class or
5424 enumeration type. Give this error even when parsing
5425 tentatively since it can't possibly be valid--and we're going
5426 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5427 won't get another chance.*/
5428 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5429 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5430 == CPP_SCOPE))
5431 {
5432 token = cp_lexer_consume_token (parser->lexer);
5433 error_at (token->location, "decltype evaluates to %qT, "
5434 "which is not a class or enumeration type",
5435 token->u.value);
5436 parser->scope = error_mark_node;
5437 error_p = true;
5438 /* As below. */
5439 success = true;
5440 cp_lexer_consume_token (parser->lexer);
5441 }
5442
5443 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5444 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5445 {
5446 /* If we have a non-type template-id followed by ::, it can't
5447 possibly be valid. */
5448 token = cp_lexer_peek_token (parser->lexer);
5449 tree tid = token->u.tree_check_value->value;
5450 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5451 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5452 {
5453 tree tmpl = NULL_TREE;
5454 if (is_overloaded_fn (tid))
5455 {
5456 tree fns = get_fns (tid);
5457 if (!OVL_CHAIN (fns))
5458 tmpl = OVL_CURRENT (fns);
5459 error_at (token->location, "function template-id %qD "
5460 "in nested-name-specifier", tid);
5461 }
5462 else
5463 {
5464 /* Variable template. */
5465 tmpl = TREE_OPERAND (tid, 0);
5466 gcc_assert (variable_template_p (tmpl));
5467 error_at (token->location, "variable template-id %qD "
5468 "in nested-name-specifier", tid);
5469 }
5470 if (tmpl)
5471 inform (DECL_SOURCE_LOCATION (tmpl),
5472 "%qD declared here", tmpl);
5473
5474 parser->scope = error_mark_node;
5475 error_p = true;
5476 /* As below. */
5477 success = true;
5478 cp_lexer_consume_token (parser->lexer);
5479 cp_lexer_consume_token (parser->lexer);
5480 }
5481 }
5482
5483 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5484 break;
5485 /* If the next token is an identifier, and the one after
5486 that is a `::', then any valid interpretation would have
5487 found a class-or-namespace-name. */
5488 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5489 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5490 == CPP_SCOPE)
5491 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5492 != CPP_COMPL))
5493 {
5494 token = cp_lexer_consume_token (parser->lexer);
5495 if (!error_p)
5496 {
5497 if (!token->error_reported)
5498 {
5499 tree decl;
5500 tree ambiguous_decls;
5501
5502 decl = cp_parser_lookup_name (parser, token->u.value,
5503 none_type,
5504 /*is_template=*/false,
5505 /*is_namespace=*/false,
5506 /*check_dependency=*/true,
5507 &ambiguous_decls,
5508 token->location);
5509 if (TREE_CODE (decl) == TEMPLATE_DECL)
5510 error_at (token->location,
5511 "%qD used without template parameters",
5512 decl);
5513 else if (ambiguous_decls)
5514 {
5515 // cp_parser_lookup_name has the same diagnostic,
5516 // thus make sure to emit it at most once.
5517 if (cp_parser_uncommitted_to_tentative_parse_p
5518 (parser))
5519 {
5520 error_at (token->location,
5521 "reference to %qD is ambiguous",
5522 token->u.value);
5523 print_candidates (ambiguous_decls);
5524 }
5525 decl = error_mark_node;
5526 }
5527 else
5528 {
5529 if (cxx_dialect != cxx98)
5530 cp_parser_name_lookup_error
5531 (parser, token->u.value, decl, NLE_NOT_CXX98,
5532 token->location);
5533 else
5534 cp_parser_name_lookup_error
5535 (parser, token->u.value, decl, NLE_CXX98,
5536 token->location);
5537 }
5538 }
5539 parser->scope = error_mark_node;
5540 error_p = true;
5541 /* Treat this as a successful nested-name-specifier
5542 due to:
5543
5544 [basic.lookup.qual]
5545
5546 If the name found is not a class-name (clause
5547 _class_) or namespace-name (_namespace.def_), the
5548 program is ill-formed. */
5549 success = true;
5550 }
5551 cp_lexer_consume_token (parser->lexer);
5552 }
5553 break;
5554 }
5555 /* We've found one valid nested-name-specifier. */
5556 success = true;
5557 /* Name lookup always gives us a DECL. */
5558 if (TREE_CODE (new_scope) == TYPE_DECL)
5559 new_scope = TREE_TYPE (new_scope);
5560 /* Uses of "template" must be followed by actual templates. */
5561 if (template_keyword_p
5562 && !(CLASS_TYPE_P (new_scope)
5563 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5564 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5565 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5566 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5567 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5568 == TEMPLATE_ID_EXPR)))
5569 permerror (input_location, TYPE_P (new_scope)
5570 ? G_("%qT is not a template")
5571 : G_("%qD is not a template"),
5572 new_scope);
5573 /* If it is a class scope, try to complete it; we are about to
5574 be looking up names inside the class. */
5575 if (TYPE_P (new_scope)
5576 /* Since checking types for dependency can be expensive,
5577 avoid doing it if the type is already complete. */
5578 && !COMPLETE_TYPE_P (new_scope)
5579 /* Do not try to complete dependent types. */
5580 && !dependent_type_p (new_scope))
5581 {
5582 new_scope = complete_type (new_scope);
5583 /* If it is a typedef to current class, use the current
5584 class instead, as the typedef won't have any names inside
5585 it yet. */
5586 if (!COMPLETE_TYPE_P (new_scope)
5587 && currently_open_class (new_scope))
5588 new_scope = TYPE_MAIN_VARIANT (new_scope);
5589 }
5590 /* Make sure we look in the right scope the next time through
5591 the loop. */
5592 parser->scope = new_scope;
5593 }
5594
5595 /* If parsing tentatively, replace the sequence of tokens that makes
5596 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5597 token. That way, should we re-parse the token stream, we will
5598 not have to repeat the effort required to do the parse, nor will
5599 we issue duplicate error messages. */
5600 if (success && start)
5601 {
5602 cp_token *token;
5603
5604 token = cp_lexer_token_at (parser->lexer, start);
5605 /* Reset the contents of the START token. */
5606 token->type = CPP_NESTED_NAME_SPECIFIER;
5607 /* Retrieve any deferred checks. Do not pop this access checks yet
5608 so the memory will not be reclaimed during token replacing below. */
5609 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5610 token->u.tree_check_value->value = parser->scope;
5611 token->u.tree_check_value->checks = get_deferred_access_checks ();
5612 token->u.tree_check_value->qualifying_scope =
5613 parser->qualifying_scope;
5614 token->keyword = RID_MAX;
5615
5616 /* Purge all subsequent tokens. */
5617 cp_lexer_purge_tokens_after (parser->lexer, start);
5618 }
5619
5620 if (start)
5621 pop_to_parent_deferring_access_checks ();
5622
5623 return success ? parser->scope : NULL_TREE;
5624 }
5625
5626 /* Parse a nested-name-specifier. See
5627 cp_parser_nested_name_specifier_opt for details. This function
5628 behaves identically, except that it will an issue an error if no
5629 nested-name-specifier is present. */
5630
5631 static tree
5632 cp_parser_nested_name_specifier (cp_parser *parser,
5633 bool typename_keyword_p,
5634 bool check_dependency_p,
5635 bool type_p,
5636 bool is_declaration)
5637 {
5638 tree scope;
5639
5640 /* Look for the nested-name-specifier. */
5641 scope = cp_parser_nested_name_specifier_opt (parser,
5642 typename_keyword_p,
5643 check_dependency_p,
5644 type_p,
5645 is_declaration);
5646 /* If it was not present, issue an error message. */
5647 if (!scope)
5648 {
5649 cp_parser_error (parser, "expected nested-name-specifier");
5650 parser->scope = NULL_TREE;
5651 }
5652
5653 return scope;
5654 }
5655
5656 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5657 this is either a class-name or a namespace-name (which corresponds
5658 to the class-or-namespace-name production in the grammar). For
5659 C++0x, it can also be a type-name that refers to an enumeration
5660 type or a simple-template-id.
5661
5662 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5663 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5664 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5665 TYPE_P is TRUE iff the next name should be taken as a class-name,
5666 even the same name is declared to be another entity in the same
5667 scope.
5668
5669 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5670 specified by the class-or-namespace-name. If neither is found the
5671 ERROR_MARK_NODE is returned. */
5672
5673 static tree
5674 cp_parser_qualifying_entity (cp_parser *parser,
5675 bool typename_keyword_p,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool type_p,
5679 bool is_declaration)
5680 {
5681 tree saved_scope;
5682 tree saved_qualifying_scope;
5683 tree saved_object_scope;
5684 tree scope;
5685 bool only_class_p;
5686 bool successful_parse_p;
5687
5688 /* DR 743: decltype can appear in a nested-name-specifier. */
5689 if (cp_lexer_next_token_is_decltype (parser->lexer))
5690 {
5691 scope = cp_parser_decltype (parser);
5692 if (TREE_CODE (scope) != ENUMERAL_TYPE
5693 && !MAYBE_CLASS_TYPE_P (scope))
5694 {
5695 cp_parser_simulate_error (parser);
5696 return error_mark_node;
5697 }
5698 if (TYPE_NAME (scope))
5699 scope = TYPE_NAME (scope);
5700 return scope;
5701 }
5702
5703 /* Before we try to parse the class-name, we must save away the
5704 current PARSER->SCOPE since cp_parser_class_name will destroy
5705 it. */
5706 saved_scope = parser->scope;
5707 saved_qualifying_scope = parser->qualifying_scope;
5708 saved_object_scope = parser->object_scope;
5709 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5710 there is no need to look for a namespace-name. */
5711 only_class_p = template_keyword_p
5712 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5713 if (!only_class_p)
5714 cp_parser_parse_tentatively (parser);
5715 scope = cp_parser_class_name (parser,
5716 typename_keyword_p,
5717 template_keyword_p,
5718 type_p ? class_type : none_type,
5719 check_dependency_p,
5720 /*class_head_p=*/false,
5721 is_declaration);
5722 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5723 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5724 if (!only_class_p
5725 && cxx_dialect != cxx98
5726 && !successful_parse_p)
5727 {
5728 /* Restore the saved scope. */
5729 parser->scope = saved_scope;
5730 parser->qualifying_scope = saved_qualifying_scope;
5731 parser->object_scope = saved_object_scope;
5732
5733 /* Parse tentatively. */
5734 cp_parser_parse_tentatively (parser);
5735
5736 /* Parse a type-name */
5737 scope = cp_parser_type_name (parser);
5738
5739 /* "If the name found does not designate a namespace or a class,
5740 enumeration, or dependent type, the program is ill-formed."
5741
5742 We cover classes and dependent types above and namespaces below,
5743 so this code is only looking for enums. */
5744 if (!scope || TREE_CODE (scope) != TYPE_DECL
5745 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5746 cp_parser_simulate_error (parser);
5747
5748 successful_parse_p = cp_parser_parse_definitely (parser);
5749 }
5750 /* If that didn't work, try for a namespace-name. */
5751 if (!only_class_p && !successful_parse_p)
5752 {
5753 /* Restore the saved scope. */
5754 parser->scope = saved_scope;
5755 parser->qualifying_scope = saved_qualifying_scope;
5756 parser->object_scope = saved_object_scope;
5757 /* If we are not looking at an identifier followed by the scope
5758 resolution operator, then this is not part of a
5759 nested-name-specifier. (Note that this function is only used
5760 to parse the components of a nested-name-specifier.) */
5761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5762 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5763 return error_mark_node;
5764 scope = cp_parser_namespace_name (parser);
5765 }
5766
5767 return scope;
5768 }
5769
5770 /* Return true if we are looking at a compound-literal, false otherwise. */
5771
5772 static bool
5773 cp_parser_compound_literal_p (cp_parser *parser)
5774 {
5775 /* Consume the `('. */
5776 cp_lexer_consume_token (parser->lexer);
5777
5778 cp_lexer_save_tokens (parser->lexer);
5779
5780 /* Skip tokens until the next token is a closing parenthesis.
5781 If we find the closing `)', and the next token is a `{', then
5782 we are looking at a compound-literal. */
5783 bool compound_literal_p
5784 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5785 /*consume_paren=*/true)
5786 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5787
5788 /* Roll back the tokens we skipped. */
5789 cp_lexer_rollback_tokens (parser->lexer);
5790
5791 return compound_literal_p;
5792 }
5793
5794 /* Parse a postfix-expression.
5795
5796 postfix-expression:
5797 primary-expression
5798 postfix-expression [ expression ]
5799 postfix-expression ( expression-list [opt] )
5800 simple-type-specifier ( expression-list [opt] )
5801 typename :: [opt] nested-name-specifier identifier
5802 ( expression-list [opt] )
5803 typename :: [opt] nested-name-specifier template [opt] template-id
5804 ( expression-list [opt] )
5805 postfix-expression . template [opt] id-expression
5806 postfix-expression -> template [opt] id-expression
5807 postfix-expression . pseudo-destructor-name
5808 postfix-expression -> pseudo-destructor-name
5809 postfix-expression ++
5810 postfix-expression --
5811 dynamic_cast < type-id > ( expression )
5812 static_cast < type-id > ( expression )
5813 reinterpret_cast < type-id > ( expression )
5814 const_cast < type-id > ( expression )
5815 typeid ( expression )
5816 typeid ( type-id )
5817
5818 GNU Extension:
5819
5820 postfix-expression:
5821 ( type-id ) { initializer-list , [opt] }
5822
5823 This extension is a GNU version of the C99 compound-literal
5824 construct. (The C99 grammar uses `type-name' instead of `type-id',
5825 but they are essentially the same concept.)
5826
5827 If ADDRESS_P is true, the postfix expression is the operand of the
5828 `&' operator. CAST_P is true if this expression is the target of a
5829 cast.
5830
5831 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5832 class member access expressions [expr.ref].
5833
5834 Returns a representation of the expression. */
5835
5836 static tree
5837 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5838 bool member_access_only_p, bool decltype_p,
5839 cp_id_kind * pidk_return)
5840 {
5841 cp_token *token;
5842 location_t loc;
5843 enum rid keyword;
5844 cp_id_kind idk = CP_ID_KIND_NONE;
5845 tree postfix_expression = NULL_TREE;
5846 bool is_member_access = false;
5847 int saved_in_statement = -1;
5848
5849 /* Peek at the next token. */
5850 token = cp_lexer_peek_token (parser->lexer);
5851 loc = token->location;
5852 /* Some of the productions are determined by keywords. */
5853 keyword = token->keyword;
5854 switch (keyword)
5855 {
5856 case RID_DYNCAST:
5857 case RID_STATCAST:
5858 case RID_REINTCAST:
5859 case RID_CONSTCAST:
5860 {
5861 tree type;
5862 tree expression;
5863 const char *saved_message;
5864 bool saved_in_type_id_in_expr_p;
5865
5866 /* All of these can be handled in the same way from the point
5867 of view of parsing. Begin by consuming the token
5868 identifying the cast. */
5869 cp_lexer_consume_token (parser->lexer);
5870
5871 /* New types cannot be defined in the cast. */
5872 saved_message = parser->type_definition_forbidden_message;
5873 parser->type_definition_forbidden_message
5874 = G_("types may not be defined in casts");
5875
5876 /* Look for the opening `<'. */
5877 cp_parser_require (parser, CPP_LESS, RT_LESS);
5878 /* Parse the type to which we are casting. */
5879 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5880 parser->in_type_id_in_expr_p = true;
5881 type = cp_parser_type_id (parser);
5882 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5883 /* Look for the closing `>'. */
5884 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5885 /* Restore the old message. */
5886 parser->type_definition_forbidden_message = saved_message;
5887
5888 bool saved_greater_than_is_operator_p
5889 = parser->greater_than_is_operator_p;
5890 parser->greater_than_is_operator_p = true;
5891
5892 /* And the expression which is being cast. */
5893 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5894 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5895 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5896
5897 parser->greater_than_is_operator_p
5898 = saved_greater_than_is_operator_p;
5899
5900 /* Only type conversions to integral or enumeration types
5901 can be used in constant-expressions. */
5902 if (!cast_valid_in_integral_constant_expression_p (type)
5903 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5904 return error_mark_node;
5905
5906 switch (keyword)
5907 {
5908 case RID_DYNCAST:
5909 postfix_expression
5910 = build_dynamic_cast (type, expression, tf_warning_or_error);
5911 break;
5912 case RID_STATCAST:
5913 postfix_expression
5914 = build_static_cast (type, expression, tf_warning_or_error);
5915 break;
5916 case RID_REINTCAST:
5917 postfix_expression
5918 = build_reinterpret_cast (type, expression,
5919 tf_warning_or_error);
5920 break;
5921 case RID_CONSTCAST:
5922 postfix_expression
5923 = build_const_cast (type, expression, tf_warning_or_error);
5924 break;
5925 default:
5926 gcc_unreachable ();
5927 }
5928 }
5929 break;
5930
5931 case RID_TYPEID:
5932 {
5933 tree type;
5934 const char *saved_message;
5935 bool saved_in_type_id_in_expr_p;
5936
5937 /* Consume the `typeid' token. */
5938 cp_lexer_consume_token (parser->lexer);
5939 /* Look for the `(' token. */
5940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5941 /* Types cannot be defined in a `typeid' expression. */
5942 saved_message = parser->type_definition_forbidden_message;
5943 parser->type_definition_forbidden_message
5944 = G_("types may not be defined in a %<typeid%> expression");
5945 /* We can't be sure yet whether we're looking at a type-id or an
5946 expression. */
5947 cp_parser_parse_tentatively (parser);
5948 /* Try a type-id first. */
5949 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5950 parser->in_type_id_in_expr_p = true;
5951 type = cp_parser_type_id (parser);
5952 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5953 /* Look for the `)' token. Otherwise, we can't be sure that
5954 we're not looking at an expression: consider `typeid (int
5955 (3))', for example. */
5956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5957 /* If all went well, simply lookup the type-id. */
5958 if (cp_parser_parse_definitely (parser))
5959 postfix_expression = get_typeid (type, tf_warning_or_error);
5960 /* Otherwise, fall back to the expression variant. */
5961 else
5962 {
5963 tree expression;
5964
5965 /* Look for an expression. */
5966 expression = cp_parser_expression (parser, & idk);
5967 /* Compute its typeid. */
5968 postfix_expression = build_typeid (expression, tf_warning_or_error);
5969 /* Look for the `)' token. */
5970 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5971 }
5972 /* Restore the saved message. */
5973 parser->type_definition_forbidden_message = saved_message;
5974 /* `typeid' may not appear in an integral constant expression. */
5975 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5976 return error_mark_node;
5977 }
5978 break;
5979
5980 case RID_TYPENAME:
5981 {
5982 tree type;
5983 /* The syntax permitted here is the same permitted for an
5984 elaborated-type-specifier. */
5985 type = cp_parser_elaborated_type_specifier (parser,
5986 /*is_friend=*/false,
5987 /*is_declaration=*/false);
5988 postfix_expression = cp_parser_functional_cast (parser, type);
5989 }
5990 break;
5991
5992 case RID_CILK_SPAWN:
5993 {
5994 cp_lexer_consume_token (parser->lexer);
5995 token = cp_lexer_peek_token (parser->lexer);
5996 if (token->type == CPP_SEMICOLON)
5997 {
5998 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5999 "an expression");
6000 postfix_expression = error_mark_node;
6001 break;
6002 }
6003 else if (!current_function_decl)
6004 {
6005 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6006 "inside a function");
6007 postfix_expression = error_mark_node;
6008 break;
6009 }
6010 else
6011 {
6012 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6013 saved_in_statement = parser->in_statement;
6014 parser->in_statement |= IN_CILK_SPAWN;
6015 }
6016 cfun->calls_cilk_spawn = 1;
6017 postfix_expression =
6018 cp_parser_postfix_expression (parser, false, false,
6019 false, false, &idk);
6020 if (!flag_cilkplus)
6021 {
6022 error_at (token->location, "-fcilkplus must be enabled to use"
6023 " %<_Cilk_spawn%>");
6024 cfun->calls_cilk_spawn = 0;
6025 }
6026 else if (saved_in_statement & IN_CILK_SPAWN)
6027 {
6028 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6029 "are not permitted");
6030 postfix_expression = error_mark_node;
6031 cfun->calls_cilk_spawn = 0;
6032 }
6033 else
6034 {
6035 postfix_expression = build_cilk_spawn (token->location,
6036 postfix_expression);
6037 if (postfix_expression != error_mark_node)
6038 SET_EXPR_LOCATION (postfix_expression, input_location);
6039 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6040 }
6041 break;
6042 }
6043
6044 case RID_BUILTIN_SHUFFLE:
6045 {
6046 vec<tree, va_gc> *vec;
6047 unsigned int i;
6048 tree p;
6049
6050 cp_lexer_consume_token (parser->lexer);
6051 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6052 /*cast_p=*/false, /*allow_expansion_p=*/true,
6053 /*non_constant_p=*/NULL);
6054 if (vec == NULL)
6055 return error_mark_node;
6056
6057 FOR_EACH_VEC_ELT (*vec, i, p)
6058 mark_exp_read (p);
6059
6060 if (vec->length () == 2)
6061 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6062 tf_warning_or_error);
6063 else if (vec->length () == 3)
6064 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6065 tf_warning_or_error);
6066 else
6067 {
6068 error_at (loc, "wrong number of arguments to "
6069 "%<__builtin_shuffle%>");
6070 return error_mark_node;
6071 }
6072 break;
6073 }
6074
6075 default:
6076 {
6077 tree type;
6078
6079 /* If the next thing is a simple-type-specifier, we may be
6080 looking at a functional cast. We could also be looking at
6081 an id-expression. So, we try the functional cast, and if
6082 that doesn't work we fall back to the primary-expression. */
6083 cp_parser_parse_tentatively (parser);
6084 /* Look for the simple-type-specifier. */
6085 type = cp_parser_simple_type_specifier (parser,
6086 /*decl_specs=*/NULL,
6087 CP_PARSER_FLAGS_NONE);
6088 /* Parse the cast itself. */
6089 if (!cp_parser_error_occurred (parser))
6090 postfix_expression
6091 = cp_parser_functional_cast (parser, type);
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser))
6094 break;
6095
6096 /* If the functional-cast didn't work out, try a
6097 compound-literal. */
6098 if (cp_parser_allow_gnu_extensions_p (parser)
6099 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6100 {
6101 tree initializer = NULL_TREE;
6102
6103 cp_parser_parse_tentatively (parser);
6104
6105 /* Avoid calling cp_parser_type_id pointlessly, see comment
6106 in cp_parser_cast_expression about c++/29234. */
6107 if (!cp_parser_compound_literal_p (parser))
6108 cp_parser_simulate_error (parser);
6109 else
6110 {
6111 /* Parse the type. */
6112 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6113 parser->in_type_id_in_expr_p = true;
6114 type = cp_parser_type_id (parser);
6115 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6116 /* Look for the `)'. */
6117 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6118 }
6119
6120 /* If things aren't going well, there's no need to
6121 keep going. */
6122 if (!cp_parser_error_occurred (parser))
6123 {
6124 bool non_constant_p;
6125 /* Parse the brace-enclosed initializer list. */
6126 initializer = cp_parser_braced_list (parser,
6127 &non_constant_p);
6128 }
6129 /* If that worked, we're definitely looking at a
6130 compound-literal expression. */
6131 if (cp_parser_parse_definitely (parser))
6132 {
6133 /* Warn the user that a compound literal is not
6134 allowed in standard C++. */
6135 pedwarn (input_location, OPT_Wpedantic,
6136 "ISO C++ forbids compound-literals");
6137 /* For simplicity, we disallow compound literals in
6138 constant-expressions. We could
6139 allow compound literals of integer type, whose
6140 initializer was a constant, in constant
6141 expressions. Permitting that usage, as a further
6142 extension, would not change the meaning of any
6143 currently accepted programs. (Of course, as
6144 compound literals are not part of ISO C++, the
6145 standard has nothing to say.) */
6146 if (cp_parser_non_integral_constant_expression (parser,
6147 NIC_NCC))
6148 {
6149 postfix_expression = error_mark_node;
6150 break;
6151 }
6152 /* Form the representation of the compound-literal. */
6153 postfix_expression
6154 = finish_compound_literal (type, initializer,
6155 tf_warning_or_error);
6156 break;
6157 }
6158 }
6159
6160 /* It must be a primary-expression. */
6161 postfix_expression
6162 = cp_parser_primary_expression (parser, address_p, cast_p,
6163 /*template_arg_p=*/false,
6164 decltype_p,
6165 &idk);
6166 }
6167 break;
6168 }
6169
6170 /* Note that we don't need to worry about calling build_cplus_new on a
6171 class-valued CALL_EXPR in decltype when it isn't the end of the
6172 postfix-expression; unary_complex_lvalue will take care of that for
6173 all these cases. */
6174
6175 /* Keep looping until the postfix-expression is complete. */
6176 while (true)
6177 {
6178 if (idk == CP_ID_KIND_UNQUALIFIED
6179 && identifier_p (postfix_expression)
6180 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6181 /* It is not a Koenig lookup function call. */
6182 postfix_expression
6183 = unqualified_name_lookup_error (postfix_expression);
6184
6185 /* Peek at the next token. */
6186 token = cp_lexer_peek_token (parser->lexer);
6187
6188 switch (token->type)
6189 {
6190 case CPP_OPEN_SQUARE:
6191 if (cp_next_tokens_can_be_std_attribute_p (parser))
6192 {
6193 cp_parser_error (parser,
6194 "two consecutive %<[%> shall "
6195 "only introduce an attribute");
6196 return error_mark_node;
6197 }
6198 postfix_expression
6199 = cp_parser_postfix_open_square_expression (parser,
6200 postfix_expression,
6201 false,
6202 decltype_p);
6203 idk = CP_ID_KIND_NONE;
6204 is_member_access = false;
6205 break;
6206
6207 case CPP_OPEN_PAREN:
6208 /* postfix-expression ( expression-list [opt] ) */
6209 {
6210 bool koenig_p;
6211 bool is_builtin_constant_p;
6212 bool saved_integral_constant_expression_p = false;
6213 bool saved_non_integral_constant_expression_p = false;
6214 tsubst_flags_t complain = complain_flags (decltype_p);
6215 vec<tree, va_gc> *args;
6216
6217 is_member_access = false;
6218
6219 is_builtin_constant_p
6220 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6221 if (is_builtin_constant_p)
6222 {
6223 /* The whole point of __builtin_constant_p is to allow
6224 non-constant expressions to appear as arguments. */
6225 saved_integral_constant_expression_p
6226 = parser->integral_constant_expression_p;
6227 saved_non_integral_constant_expression_p
6228 = parser->non_integral_constant_expression_p;
6229 parser->integral_constant_expression_p = false;
6230 }
6231 args = (cp_parser_parenthesized_expression_list
6232 (parser, non_attr,
6233 /*cast_p=*/false, /*allow_expansion_p=*/true,
6234 /*non_constant_p=*/NULL,
6235 /*want_literal_zero_p=*/warn_memset_transposed_args));
6236 if (is_builtin_constant_p)
6237 {
6238 parser->integral_constant_expression_p
6239 = saved_integral_constant_expression_p;
6240 parser->non_integral_constant_expression_p
6241 = saved_non_integral_constant_expression_p;
6242 }
6243
6244 if (args == NULL)
6245 {
6246 postfix_expression = error_mark_node;
6247 break;
6248 }
6249
6250 /* Function calls are not permitted in
6251 constant-expressions. */
6252 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6253 && cp_parser_non_integral_constant_expression (parser,
6254 NIC_FUNC_CALL))
6255 {
6256 postfix_expression = error_mark_node;
6257 release_tree_vector (args);
6258 break;
6259 }
6260
6261 koenig_p = false;
6262 if (idk == CP_ID_KIND_UNQUALIFIED
6263 || idk == CP_ID_KIND_TEMPLATE_ID)
6264 {
6265 if (identifier_p (postfix_expression))
6266 {
6267 if (!args->is_empty ())
6268 {
6269 koenig_p = true;
6270 if (!any_type_dependent_arguments_p (args))
6271 postfix_expression
6272 = perform_koenig_lookup (postfix_expression, args,
6273 complain);
6274 }
6275 else
6276 postfix_expression
6277 = unqualified_fn_lookup_error (postfix_expression);
6278 }
6279 /* We do not perform argument-dependent lookup if
6280 normal lookup finds a non-function, in accordance
6281 with the expected resolution of DR 218. */
6282 else if (!args->is_empty ()
6283 && is_overloaded_fn (postfix_expression))
6284 {
6285 tree fn = get_first_fn (postfix_expression);
6286 fn = STRIP_TEMPLATE (fn);
6287
6288 /* Do not do argument dependent lookup if regular
6289 lookup finds a member function or a block-scope
6290 function declaration. [basic.lookup.argdep]/3 */
6291 if (!DECL_FUNCTION_MEMBER_P (fn)
6292 && !DECL_LOCAL_FUNCTION_P (fn))
6293 {
6294 koenig_p = true;
6295 if (!any_type_dependent_arguments_p (args))
6296 postfix_expression
6297 = perform_koenig_lookup (postfix_expression, args,
6298 complain);
6299 }
6300 }
6301 }
6302
6303 if (warn_memset_transposed_args)
6304 {
6305 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6306 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6307 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6308 && vec_safe_length (args) == 3
6309 && integer_zerop ((*args)[2])
6310 && LITERAL_ZERO_P ((*args)[2])
6311 && !(integer_zerop ((*args)[1])
6312 && LITERAL_ZERO_P ((*args)[1])))
6313 warning (OPT_Wmemset_transposed_args,
6314 "%<memset%> used with constant zero length "
6315 "parameter; this could be due to transposed "
6316 "parameters");
6317
6318 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6319 to avoid leaking those into folder and middle-end. */
6320 unsigned int i;
6321 tree arg;
6322 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6323 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6324 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6325 }
6326
6327 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6328 {
6329 tree instance = TREE_OPERAND (postfix_expression, 0);
6330 tree fn = TREE_OPERAND (postfix_expression, 1);
6331
6332 if (processing_template_decl
6333 && (type_dependent_expression_p (instance)
6334 || (!BASELINK_P (fn)
6335 && TREE_CODE (fn) != FIELD_DECL)
6336 || type_dependent_expression_p (fn)
6337 || any_type_dependent_arguments_p (args)))
6338 {
6339 postfix_expression
6340 = build_nt_call_vec (postfix_expression, args);
6341 release_tree_vector (args);
6342 break;
6343 }
6344
6345 if (BASELINK_P (fn))
6346 {
6347 postfix_expression
6348 = (build_new_method_call
6349 (instance, fn, &args, NULL_TREE,
6350 (idk == CP_ID_KIND_QUALIFIED
6351 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6352 : LOOKUP_NORMAL),
6353 /*fn_p=*/NULL,
6354 complain));
6355 }
6356 else
6357 postfix_expression
6358 = finish_call_expr (postfix_expression, &args,
6359 /*disallow_virtual=*/false,
6360 /*koenig_p=*/false,
6361 complain);
6362 }
6363 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6364 || TREE_CODE (postfix_expression) == MEMBER_REF
6365 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6366 postfix_expression = (build_offset_ref_call_from_tree
6367 (postfix_expression, &args,
6368 complain));
6369 else if (idk == CP_ID_KIND_QUALIFIED)
6370 /* A call to a static class member, or a namespace-scope
6371 function. */
6372 postfix_expression
6373 = finish_call_expr (postfix_expression, &args,
6374 /*disallow_virtual=*/true,
6375 koenig_p,
6376 complain);
6377 else
6378 /* All other function calls. */
6379 postfix_expression
6380 = finish_call_expr (postfix_expression, &args,
6381 /*disallow_virtual=*/false,
6382 koenig_p,
6383 complain);
6384
6385 protected_set_expr_location (postfix_expression, token->location);
6386
6387 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6388 idk = CP_ID_KIND_NONE;
6389
6390 release_tree_vector (args);
6391 }
6392 break;
6393
6394 case CPP_DOT:
6395 case CPP_DEREF:
6396 /* postfix-expression . template [opt] id-expression
6397 postfix-expression . pseudo-destructor-name
6398 postfix-expression -> template [opt] id-expression
6399 postfix-expression -> pseudo-destructor-name */
6400
6401 /* Consume the `.' or `->' operator. */
6402 cp_lexer_consume_token (parser->lexer);
6403
6404 postfix_expression
6405 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6406 postfix_expression,
6407 false, &idk, loc);
6408
6409 is_member_access = true;
6410 break;
6411
6412 case CPP_PLUS_PLUS:
6413 /* postfix-expression ++ */
6414 /* Consume the `++' token. */
6415 cp_lexer_consume_token (parser->lexer);
6416 /* Generate a representation for the complete expression. */
6417 postfix_expression
6418 = finish_increment_expr (postfix_expression,
6419 POSTINCREMENT_EXPR);
6420 /* Increments may not appear in constant-expressions. */
6421 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6422 postfix_expression = error_mark_node;
6423 idk = CP_ID_KIND_NONE;
6424 is_member_access = false;
6425 break;
6426
6427 case CPP_MINUS_MINUS:
6428 /* postfix-expression -- */
6429 /* Consume the `--' token. */
6430 cp_lexer_consume_token (parser->lexer);
6431 /* Generate a representation for the complete expression. */
6432 postfix_expression
6433 = finish_increment_expr (postfix_expression,
6434 POSTDECREMENT_EXPR);
6435 /* Decrements may not appear in constant-expressions. */
6436 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6437 postfix_expression = error_mark_node;
6438 idk = CP_ID_KIND_NONE;
6439 is_member_access = false;
6440 break;
6441
6442 default:
6443 if (pidk_return != NULL)
6444 * pidk_return = idk;
6445 if (member_access_only_p)
6446 return is_member_access? postfix_expression : error_mark_node;
6447 else
6448 return postfix_expression;
6449 }
6450 }
6451
6452 /* We should never get here. */
6453 gcc_unreachable ();
6454 return error_mark_node;
6455 }
6456
6457 /* This function parses Cilk Plus array notations. If a normal array expr. is
6458 parsed then the array index is passed back to the caller through *INIT_INDEX
6459 and the function returns a NULL_TREE. If array notation expr. is parsed,
6460 then *INIT_INDEX is ignored by the caller and the function returns
6461 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6462 error_mark_node. */
6463
6464 static tree
6465 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6466 tree array_value)
6467 {
6468 cp_token *token = NULL;
6469 tree length_index, stride = NULL_TREE, value_tree, array_type;
6470 if (!array_value || array_value == error_mark_node)
6471 {
6472 cp_parser_skip_to_end_of_statement (parser);
6473 return error_mark_node;
6474 }
6475
6476 array_type = TREE_TYPE (array_value);
6477
6478 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6479 parser->colon_corrects_to_scope_p = false;
6480 token = cp_lexer_peek_token (parser->lexer);
6481
6482 if (!token)
6483 {
6484 cp_parser_error (parser, "expected %<:%> or numeral");
6485 return error_mark_node;
6486 }
6487 else if (token->type == CPP_COLON)
6488 {
6489 /* Consume the ':'. */
6490 cp_lexer_consume_token (parser->lexer);
6491
6492 /* If we are here, then we have a case like this A[:]. */
6493 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6494 {
6495 cp_parser_error (parser, "expected %<]%>");
6496 cp_parser_skip_to_end_of_statement (parser);
6497 return error_mark_node;
6498 }
6499 *init_index = NULL_TREE;
6500 stride = NULL_TREE;
6501 length_index = NULL_TREE;
6502 }
6503 else
6504 {
6505 /* If we are here, then there are three valid possibilities:
6506 1. ARRAY [ EXP ]
6507 2. ARRAY [ EXP : EXP ]
6508 3. ARRAY [ EXP : EXP : EXP ] */
6509
6510 *init_index = cp_parser_expression (parser);
6511 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6512 {
6513 /* This indicates that we have a normal array expression. */
6514 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6515 return NULL_TREE;
6516 }
6517
6518 /* Consume the ':'. */
6519 cp_lexer_consume_token (parser->lexer);
6520 length_index = cp_parser_expression (parser);
6521 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6522 {
6523 cp_lexer_consume_token (parser->lexer);
6524 stride = cp_parser_expression (parser);
6525 }
6526 }
6527 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6528
6529 if (*init_index == error_mark_node || length_index == error_mark_node
6530 || stride == error_mark_node || array_type == error_mark_node)
6531 {
6532 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6533 cp_lexer_consume_token (parser->lexer);
6534 return error_mark_node;
6535 }
6536 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6537
6538 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6539 length_index, stride, array_type);
6540 return value_tree;
6541 }
6542
6543 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6544 by cp_parser_builtin_offsetof. We're looking for
6545
6546 postfix-expression [ expression ]
6547 postfix-expression [ braced-init-list ] (C++11)
6548
6549 FOR_OFFSETOF is set if we're being called in that context, which
6550 changes how we deal with integer constant expressions. */
6551
6552 static tree
6553 cp_parser_postfix_open_square_expression (cp_parser *parser,
6554 tree postfix_expression,
6555 bool for_offsetof,
6556 bool decltype_p)
6557 {
6558 tree index = NULL_TREE;
6559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6560 bool saved_greater_than_is_operator_p;
6561
6562 /* Consume the `[' token. */
6563 cp_lexer_consume_token (parser->lexer);
6564
6565 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6566 parser->greater_than_is_operator_p = true;
6567
6568 /* Parse the index expression. */
6569 /* ??? For offsetof, there is a question of what to allow here. If
6570 offsetof is not being used in an integral constant expression context,
6571 then we *could* get the right answer by computing the value at runtime.
6572 If we are in an integral constant expression context, then we might
6573 could accept any constant expression; hard to say without analysis.
6574 Rather than open the barn door too wide right away, allow only integer
6575 constant expressions here. */
6576 if (for_offsetof)
6577 index = cp_parser_constant_expression (parser);
6578 else
6579 {
6580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6581 {
6582 bool expr_nonconst_p;
6583 cp_lexer_set_source_position (parser->lexer);
6584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6585 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6586 if (flag_cilkplus
6587 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6588 {
6589 error_at (cp_lexer_peek_token (parser->lexer)->location,
6590 "braced list index is not allowed with array "
6591 "notation");
6592 cp_parser_skip_to_end_of_statement (parser);
6593 return error_mark_node;
6594 }
6595 }
6596 else if (flag_cilkplus)
6597 {
6598 /* Here are have these two options:
6599 ARRAY[EXP : EXP] - Array notation expr with default
6600 stride of 1.
6601 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6602 stride. */
6603 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6604 postfix_expression);
6605 if (an_exp)
6606 return an_exp;
6607 }
6608 else
6609 index = cp_parser_expression (parser);
6610 }
6611
6612 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6613
6614 /* Look for the closing `]'. */
6615 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6616
6617 /* Build the ARRAY_REF. */
6618 postfix_expression = grok_array_decl (loc, postfix_expression,
6619 index, decltype_p);
6620
6621 /* When not doing offsetof, array references are not permitted in
6622 constant-expressions. */
6623 if (!for_offsetof
6624 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6625 postfix_expression = error_mark_node;
6626
6627 return postfix_expression;
6628 }
6629
6630 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6631 by cp_parser_builtin_offsetof. We're looking for
6632
6633 postfix-expression . template [opt] id-expression
6634 postfix-expression . pseudo-destructor-name
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression -> pseudo-destructor-name
6637
6638 FOR_OFFSETOF is set if we're being called in that context. That sorta
6639 limits what of the above we'll actually accept, but nevermind.
6640 TOKEN_TYPE is the "." or "->" token, which will already have been
6641 removed from the stream. */
6642
6643 static tree
6644 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6645 enum cpp_ttype token_type,
6646 tree postfix_expression,
6647 bool for_offsetof, cp_id_kind *idk,
6648 location_t location)
6649 {
6650 tree name;
6651 bool dependent_p;
6652 bool pseudo_destructor_p;
6653 tree scope = NULL_TREE;
6654
6655 /* If this is a `->' operator, dereference the pointer. */
6656 if (token_type == CPP_DEREF)
6657 postfix_expression = build_x_arrow (location, postfix_expression,
6658 tf_warning_or_error);
6659 /* Check to see whether or not the expression is type-dependent. */
6660 dependent_p = type_dependent_expression_p (postfix_expression);
6661 /* The identifier following the `->' or `.' is not qualified. */
6662 parser->scope = NULL_TREE;
6663 parser->qualifying_scope = NULL_TREE;
6664 parser->object_scope = NULL_TREE;
6665 *idk = CP_ID_KIND_NONE;
6666
6667 /* Enter the scope corresponding to the type of the object
6668 given by the POSTFIX_EXPRESSION. */
6669 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6670 {
6671 scope = TREE_TYPE (postfix_expression);
6672 /* According to the standard, no expression should ever have
6673 reference type. Unfortunately, we do not currently match
6674 the standard in this respect in that our internal representation
6675 of an expression may have reference type even when the standard
6676 says it does not. Therefore, we have to manually obtain the
6677 underlying type here. */
6678 scope = non_reference (scope);
6679 /* The type of the POSTFIX_EXPRESSION must be complete. */
6680 if (scope == unknown_type_node)
6681 {
6682 error_at (location, "%qE does not have class type",
6683 postfix_expression);
6684 scope = NULL_TREE;
6685 }
6686 /* Unlike the object expression in other contexts, *this is not
6687 required to be of complete type for purposes of class member
6688 access (5.2.5) outside the member function body. */
6689 else if (postfix_expression != current_class_ref
6690 && !(processing_template_decl && scope == current_class_type))
6691 scope = complete_type_or_else (scope, NULL_TREE);
6692 /* Let the name lookup machinery know that we are processing a
6693 class member access expression. */
6694 parser->context->object_type = scope;
6695 /* If something went wrong, we want to be able to discern that case,
6696 as opposed to the case where there was no SCOPE due to the type
6697 of expression being dependent. */
6698 if (!scope)
6699 scope = error_mark_node;
6700 /* If the SCOPE was erroneous, make the various semantic analysis
6701 functions exit quickly -- and without issuing additional error
6702 messages. */
6703 if (scope == error_mark_node)
6704 postfix_expression = error_mark_node;
6705 }
6706
6707 /* Assume this expression is not a pseudo-destructor access. */
6708 pseudo_destructor_p = false;
6709
6710 /* If the SCOPE is a scalar type, then, if this is a valid program,
6711 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6712 is type dependent, it can be pseudo-destructor-name or something else.
6713 Try to parse it as pseudo-destructor-name first. */
6714 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6715 {
6716 tree s;
6717 tree type;
6718
6719 cp_parser_parse_tentatively (parser);
6720 /* Parse the pseudo-destructor-name. */
6721 s = NULL_TREE;
6722 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6723 &s, &type);
6724 if (dependent_p
6725 && (cp_parser_error_occurred (parser)
6726 || !SCALAR_TYPE_P (type)))
6727 cp_parser_abort_tentative_parse (parser);
6728 else if (cp_parser_parse_definitely (parser))
6729 {
6730 pseudo_destructor_p = true;
6731 postfix_expression
6732 = finish_pseudo_destructor_expr (postfix_expression,
6733 s, type, location);
6734 }
6735 }
6736
6737 if (!pseudo_destructor_p)
6738 {
6739 /* If the SCOPE is not a scalar type, we are looking at an
6740 ordinary class member access expression, rather than a
6741 pseudo-destructor-name. */
6742 bool template_p;
6743 cp_token *token = cp_lexer_peek_token (parser->lexer);
6744 /* Parse the id-expression. */
6745 name = (cp_parser_id_expression
6746 (parser,
6747 cp_parser_optional_template_keyword (parser),
6748 /*check_dependency_p=*/true,
6749 &template_p,
6750 /*declarator_p=*/false,
6751 /*optional_p=*/false));
6752 /* In general, build a SCOPE_REF if the member name is qualified.
6753 However, if the name was not dependent and has already been
6754 resolved; there is no need to build the SCOPE_REF. For example;
6755
6756 struct X { void f(); };
6757 template <typename T> void f(T* t) { t->X::f(); }
6758
6759 Even though "t" is dependent, "X::f" is not and has been resolved
6760 to a BASELINK; there is no need to include scope information. */
6761
6762 /* But we do need to remember that there was an explicit scope for
6763 virtual function calls. */
6764 if (parser->scope)
6765 *idk = CP_ID_KIND_QUALIFIED;
6766
6767 /* If the name is a template-id that names a type, we will get a
6768 TYPE_DECL here. That is invalid code. */
6769 if (TREE_CODE (name) == TYPE_DECL)
6770 {
6771 error_at (token->location, "invalid use of %qD", name);
6772 postfix_expression = error_mark_node;
6773 }
6774 else
6775 {
6776 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6777 {
6778 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6779 {
6780 error_at (token->location, "%<%D::%D%> is not a class member",
6781 parser->scope, name);
6782 postfix_expression = error_mark_node;
6783 }
6784 else
6785 name = build_qualified_name (/*type=*/NULL_TREE,
6786 parser->scope,
6787 name,
6788 template_p);
6789 parser->scope = NULL_TREE;
6790 parser->qualifying_scope = NULL_TREE;
6791 parser->object_scope = NULL_TREE;
6792 }
6793 if (parser->scope && name && BASELINK_P (name))
6794 adjust_result_of_qualified_name_lookup
6795 (name, parser->scope, scope);
6796 postfix_expression
6797 = finish_class_member_access_expr (postfix_expression, name,
6798 template_p,
6799 tf_warning_or_error);
6800 }
6801 }
6802
6803 /* We no longer need to look up names in the scope of the object on
6804 the left-hand side of the `.' or `->' operator. */
6805 parser->context->object_type = NULL_TREE;
6806
6807 /* Outside of offsetof, these operators may not appear in
6808 constant-expressions. */
6809 if (!for_offsetof
6810 && (cp_parser_non_integral_constant_expression
6811 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6812 postfix_expression = error_mark_node;
6813
6814 return postfix_expression;
6815 }
6816
6817 /* Cache of LITERAL_ZERO_P constants. */
6818
6819 static GTY(()) tree literal_zeros[itk_none];
6820
6821 /* Parse a parenthesized expression-list.
6822
6823 expression-list:
6824 assignment-expression
6825 expression-list, assignment-expression
6826
6827 attribute-list:
6828 expression-list
6829 identifier
6830 identifier, expression-list
6831
6832 CAST_P is true if this expression is the target of a cast.
6833
6834 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6835 argument pack.
6836
6837 Returns a vector of trees. Each element is a representation of an
6838 assignment-expression. NULL is returned if the ( and or ) are
6839 missing. An empty, but allocated, vector is returned on no
6840 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6841 if we are parsing an attribute list for an attribute that wants a
6842 plain identifier argument, normal_attr for an attribute that wants
6843 an expression, or non_attr if we aren't parsing an attribute list. If
6844 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6845 not all of the expressions in the list were constant.
6846 WANT_LITERAL_ZERO_P is true if the caller is interested in
6847 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6848 immediately, this can be removed. */
6849
6850 static vec<tree, va_gc> *
6851 cp_parser_parenthesized_expression_list (cp_parser* parser,
6852 int is_attribute_list,
6853 bool cast_p,
6854 bool allow_expansion_p,
6855 bool *non_constant_p,
6856 bool want_literal_zero_p)
6857 {
6858 vec<tree, va_gc> *expression_list;
6859 bool fold_expr_p = is_attribute_list != non_attr;
6860 tree identifier = NULL_TREE;
6861 bool saved_greater_than_is_operator_p;
6862
6863 /* Assume all the expressions will be constant. */
6864 if (non_constant_p)
6865 *non_constant_p = false;
6866
6867 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6868 return NULL;
6869
6870 expression_list = make_tree_vector ();
6871
6872 /* Within a parenthesized expression, a `>' token is always
6873 the greater-than operator. */
6874 saved_greater_than_is_operator_p
6875 = parser->greater_than_is_operator_p;
6876 parser->greater_than_is_operator_p = true;
6877
6878 /* Consume expressions until there are no more. */
6879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6880 while (true)
6881 {
6882 tree expr;
6883
6884 /* At the beginning of attribute lists, check to see if the
6885 next token is an identifier. */
6886 if (is_attribute_list == id_attr
6887 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6888 {
6889 cp_token *token;
6890
6891 /* Consume the identifier. */
6892 token = cp_lexer_consume_token (parser->lexer);
6893 /* Save the identifier. */
6894 identifier = token->u.value;
6895 }
6896 else
6897 {
6898 bool expr_non_constant_p;
6899
6900 /* Parse the next assignment-expression. */
6901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6902 {
6903 /* A braced-init-list. */
6904 cp_lexer_set_source_position (parser->lexer);
6905 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6906 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6907 if (non_constant_p && expr_non_constant_p)
6908 *non_constant_p = true;
6909 }
6910 else if (non_constant_p)
6911 {
6912 expr = (cp_parser_constant_expression
6913 (parser, /*allow_non_constant_p=*/true,
6914 &expr_non_constant_p));
6915 if (expr_non_constant_p)
6916 *non_constant_p = true;
6917 }
6918 else
6919 {
6920 expr = NULL_TREE;
6921 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6922 switch (tok->type)
6923 {
6924 case CPP_NUMBER:
6925 case CPP_CHAR:
6926 case CPP_WCHAR:
6927 case CPP_CHAR16:
6928 case CPP_CHAR32:
6929 /* If a parameter is literal zero alone, remember it
6930 for -Wmemset-transposed-args warning. */
6931 if (integer_zerop (tok->u.value)
6932 && !TREE_OVERFLOW (tok->u.value)
6933 && want_literal_zero_p
6934 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6935 == CPP_COMMA
6936 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6937 == CPP_CLOSE_PAREN))
6938 {
6939 unsigned int i;
6940 for (i = 0; i < itk_none; ++i)
6941 if (TREE_TYPE (tok->u.value) == integer_types[i])
6942 break;
6943 if (i < itk_none && literal_zeros[i])
6944 expr = literal_zeros[i];
6945 else
6946 {
6947 expr = copy_node (tok->u.value);
6948 LITERAL_ZERO_P (expr) = 1;
6949 if (i < itk_none)
6950 literal_zeros[i] = expr;
6951 }
6952 /* Consume the 0 token (or '\0', 0LL etc.). */
6953 cp_lexer_consume_token (parser->lexer);
6954 }
6955 break;
6956 default:
6957 break;
6958 }
6959 if (expr == NULL_TREE)
6960 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6961 cast_p);
6962 }
6963
6964 if (fold_expr_p)
6965 expr = instantiate_non_dependent_expr (expr);
6966
6967 /* If we have an ellipsis, then this is an expression
6968 expansion. */
6969 if (allow_expansion_p
6970 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6971 {
6972 /* Consume the `...'. */
6973 cp_lexer_consume_token (parser->lexer);
6974
6975 /* Build the argument pack. */
6976 expr = make_pack_expansion (expr);
6977 }
6978
6979 /* Add it to the list. We add error_mark_node
6980 expressions to the list, so that we can still tell if
6981 the correct form for a parenthesized expression-list
6982 is found. That gives better errors. */
6983 vec_safe_push (expression_list, expr);
6984
6985 if (expr == error_mark_node)
6986 goto skip_comma;
6987 }
6988
6989 /* After the first item, attribute lists look the same as
6990 expression lists. */
6991 is_attribute_list = non_attr;
6992
6993 get_comma:;
6994 /* If the next token isn't a `,', then we are done. */
6995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6996 break;
6997
6998 /* Otherwise, consume the `,' and keep going. */
6999 cp_lexer_consume_token (parser->lexer);
7000 }
7001
7002 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7003 {
7004 int ending;
7005
7006 skip_comma:;
7007 /* We try and resync to an unnested comma, as that will give the
7008 user better diagnostics. */
7009 ending = cp_parser_skip_to_closing_parenthesis (parser,
7010 /*recovering=*/true,
7011 /*or_comma=*/true,
7012 /*consume_paren=*/true);
7013 if (ending < 0)
7014 goto get_comma;
7015 if (!ending)
7016 {
7017 parser->greater_than_is_operator_p
7018 = saved_greater_than_is_operator_p;
7019 return NULL;
7020 }
7021 }
7022
7023 parser->greater_than_is_operator_p
7024 = saved_greater_than_is_operator_p;
7025
7026 if (identifier)
7027 vec_safe_insert (expression_list, 0, identifier);
7028
7029 return expression_list;
7030 }
7031
7032 /* Parse a pseudo-destructor-name.
7033
7034 pseudo-destructor-name:
7035 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7036 :: [opt] nested-name-specifier template template-id :: ~ type-name
7037 :: [opt] nested-name-specifier [opt] ~ type-name
7038
7039 If either of the first two productions is used, sets *SCOPE to the
7040 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7041 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7042 or ERROR_MARK_NODE if the parse fails. */
7043
7044 static void
7045 cp_parser_pseudo_destructor_name (cp_parser* parser,
7046 tree object,
7047 tree* scope,
7048 tree* type)
7049 {
7050 bool nested_name_specifier_p;
7051
7052 /* Handle ~auto. */
7053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7054 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7055 && !type_dependent_expression_p (object))
7056 {
7057 if (cxx_dialect < cxx14)
7058 pedwarn (input_location, 0,
7059 "%<~auto%> only available with "
7060 "-std=c++14 or -std=gnu++14");
7061 cp_lexer_consume_token (parser->lexer);
7062 cp_lexer_consume_token (parser->lexer);
7063 *scope = NULL_TREE;
7064 *type = TREE_TYPE (object);
7065 return;
7066 }
7067
7068 /* Assume that things will not work out. */
7069 *type = error_mark_node;
7070
7071 /* Look for the optional `::' operator. */
7072 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7073 /* Look for the optional nested-name-specifier. */
7074 nested_name_specifier_p
7075 = (cp_parser_nested_name_specifier_opt (parser,
7076 /*typename_keyword_p=*/false,
7077 /*check_dependency_p=*/true,
7078 /*type_p=*/false,
7079 /*is_declaration=*/false)
7080 != NULL_TREE);
7081 /* Now, if we saw a nested-name-specifier, we might be doing the
7082 second production. */
7083 if (nested_name_specifier_p
7084 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7085 {
7086 /* Consume the `template' keyword. */
7087 cp_lexer_consume_token (parser->lexer);
7088 /* Parse the template-id. */
7089 cp_parser_template_id (parser,
7090 /*template_keyword_p=*/true,
7091 /*check_dependency_p=*/false,
7092 class_type,
7093 /*is_declaration=*/true);
7094 /* Look for the `::' token. */
7095 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7096 }
7097 /* If the next token is not a `~', then there might be some
7098 additional qualification. */
7099 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7100 {
7101 /* At this point, we're looking for "type-name :: ~". The type-name
7102 must not be a class-name, since this is a pseudo-destructor. So,
7103 it must be either an enum-name, or a typedef-name -- both of which
7104 are just identifiers. So, we peek ahead to check that the "::"
7105 and "~" tokens are present; if they are not, then we can avoid
7106 calling type_name. */
7107 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7108 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7109 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7110 {
7111 cp_parser_error (parser, "non-scalar type");
7112 return;
7113 }
7114
7115 /* Look for the type-name. */
7116 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7117 if (*scope == error_mark_node)
7118 return;
7119
7120 /* Look for the `::' token. */
7121 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7122 }
7123 else
7124 *scope = NULL_TREE;
7125
7126 /* Look for the `~'. */
7127 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7128
7129 /* Once we see the ~, this has to be a pseudo-destructor. */
7130 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7131 cp_parser_commit_to_topmost_tentative_parse (parser);
7132
7133 /* Look for the type-name again. We are not responsible for
7134 checking that it matches the first type-name. */
7135 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7136 }
7137
7138 /* Parse a unary-expression.
7139
7140 unary-expression:
7141 postfix-expression
7142 ++ cast-expression
7143 -- cast-expression
7144 unary-operator cast-expression
7145 sizeof unary-expression
7146 sizeof ( type-id )
7147 alignof ( type-id ) [C++0x]
7148 new-expression
7149 delete-expression
7150
7151 GNU Extensions:
7152
7153 unary-expression:
7154 __extension__ cast-expression
7155 __alignof__ unary-expression
7156 __alignof__ ( type-id )
7157 alignof unary-expression [C++0x]
7158 __real__ cast-expression
7159 __imag__ cast-expression
7160 && identifier
7161 sizeof ( type-id ) { initializer-list , [opt] }
7162 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7163 __alignof__ ( type-id ) { initializer-list , [opt] }
7164
7165 ADDRESS_P is true iff the unary-expression is appearing as the
7166 operand of the `&' operator. CAST_P is true if this expression is
7167 the target of a cast.
7168
7169 Returns a representation of the expression. */
7170
7171 static tree
7172 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7173 bool address_p, bool cast_p, bool decltype_p)
7174 {
7175 cp_token *token;
7176 enum tree_code unary_operator;
7177
7178 /* Peek at the next token. */
7179 token = cp_lexer_peek_token (parser->lexer);
7180 /* Some keywords give away the kind of expression. */
7181 if (token->type == CPP_KEYWORD)
7182 {
7183 enum rid keyword = token->keyword;
7184
7185 switch (keyword)
7186 {
7187 case RID_ALIGNOF:
7188 case RID_SIZEOF:
7189 {
7190 tree operand, ret;
7191 enum tree_code op;
7192 location_t first_loc;
7193
7194 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7195 /* Consume the token. */
7196 cp_lexer_consume_token (parser->lexer);
7197 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7198 /* Parse the operand. */
7199 operand = cp_parser_sizeof_operand (parser, keyword);
7200
7201 if (TYPE_P (operand))
7202 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7203 else
7204 {
7205 /* ISO C++ defines alignof only with types, not with
7206 expressions. So pedwarn if alignof is used with a non-
7207 type expression. However, __alignof__ is ok. */
7208 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7209 pedwarn (token->location, OPT_Wpedantic,
7210 "ISO C++ does not allow %<alignof%> "
7211 "with a non-type");
7212
7213 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7214 }
7215 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7216 SIZEOF_EXPR with the original operand. */
7217 if (op == SIZEOF_EXPR && ret != error_mark_node)
7218 {
7219 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7220 {
7221 if (!processing_template_decl && TYPE_P (operand))
7222 {
7223 ret = build_min (SIZEOF_EXPR, size_type_node,
7224 build1 (NOP_EXPR, operand,
7225 error_mark_node));
7226 SIZEOF_EXPR_TYPE_P (ret) = 1;
7227 }
7228 else
7229 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7230 TREE_SIDE_EFFECTS (ret) = 0;
7231 TREE_READONLY (ret) = 1;
7232 }
7233 SET_EXPR_LOCATION (ret, first_loc);
7234 }
7235 return ret;
7236 }
7237
7238 case RID_NEW:
7239 return cp_parser_new_expression (parser);
7240
7241 case RID_DELETE:
7242 return cp_parser_delete_expression (parser);
7243
7244 case RID_EXTENSION:
7245 {
7246 /* The saved value of the PEDANTIC flag. */
7247 int saved_pedantic;
7248 tree expr;
7249
7250 /* Save away the PEDANTIC flag. */
7251 cp_parser_extension_opt (parser, &saved_pedantic);
7252 /* Parse the cast-expression. */
7253 expr = cp_parser_simple_cast_expression (parser);
7254 /* Restore the PEDANTIC flag. */
7255 pedantic = saved_pedantic;
7256
7257 return expr;
7258 }
7259
7260 case RID_REALPART:
7261 case RID_IMAGPART:
7262 {
7263 tree expression;
7264
7265 /* Consume the `__real__' or `__imag__' token. */
7266 cp_lexer_consume_token (parser->lexer);
7267 /* Parse the cast-expression. */
7268 expression = cp_parser_simple_cast_expression (parser);
7269 /* Create the complete representation. */
7270 return build_x_unary_op (token->location,
7271 (keyword == RID_REALPART
7272 ? REALPART_EXPR : IMAGPART_EXPR),
7273 expression,
7274 tf_warning_or_error);
7275 }
7276 break;
7277
7278 case RID_TRANSACTION_ATOMIC:
7279 case RID_TRANSACTION_RELAXED:
7280 return cp_parser_transaction_expression (parser, keyword);
7281
7282 case RID_NOEXCEPT:
7283 {
7284 tree expr;
7285 const char *saved_message;
7286 bool saved_integral_constant_expression_p;
7287 bool saved_non_integral_constant_expression_p;
7288 bool saved_greater_than_is_operator_p;
7289
7290 cp_lexer_consume_token (parser->lexer);
7291 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7292
7293 saved_message = parser->type_definition_forbidden_message;
7294 parser->type_definition_forbidden_message
7295 = G_("types may not be defined in %<noexcept%> expressions");
7296
7297 saved_integral_constant_expression_p
7298 = parser->integral_constant_expression_p;
7299 saved_non_integral_constant_expression_p
7300 = parser->non_integral_constant_expression_p;
7301 parser->integral_constant_expression_p = false;
7302
7303 saved_greater_than_is_operator_p
7304 = parser->greater_than_is_operator_p;
7305 parser->greater_than_is_operator_p = true;
7306
7307 ++cp_unevaluated_operand;
7308 ++c_inhibit_evaluation_warnings;
7309 ++cp_noexcept_operand;
7310 expr = cp_parser_expression (parser);
7311 --cp_noexcept_operand;
7312 --c_inhibit_evaluation_warnings;
7313 --cp_unevaluated_operand;
7314
7315 parser->greater_than_is_operator_p
7316 = saved_greater_than_is_operator_p;
7317
7318 parser->integral_constant_expression_p
7319 = saved_integral_constant_expression_p;
7320 parser->non_integral_constant_expression_p
7321 = saved_non_integral_constant_expression_p;
7322
7323 parser->type_definition_forbidden_message = saved_message;
7324
7325 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7326 return finish_noexcept_expr (expr, tf_warning_or_error);
7327 }
7328
7329 default:
7330 break;
7331 }
7332 }
7333
7334 /* Look for the `:: new' and `:: delete', which also signal the
7335 beginning of a new-expression, or delete-expression,
7336 respectively. If the next token is `::', then it might be one of
7337 these. */
7338 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7339 {
7340 enum rid keyword;
7341
7342 /* See if the token after the `::' is one of the keywords in
7343 which we're interested. */
7344 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7345 /* If it's `new', we have a new-expression. */
7346 if (keyword == RID_NEW)
7347 return cp_parser_new_expression (parser);
7348 /* Similarly, for `delete'. */
7349 else if (keyword == RID_DELETE)
7350 return cp_parser_delete_expression (parser);
7351 }
7352
7353 /* Look for a unary operator. */
7354 unary_operator = cp_parser_unary_operator (token);
7355 /* The `++' and `--' operators can be handled similarly, even though
7356 they are not technically unary-operators in the grammar. */
7357 if (unary_operator == ERROR_MARK)
7358 {
7359 if (token->type == CPP_PLUS_PLUS)
7360 unary_operator = PREINCREMENT_EXPR;
7361 else if (token->type == CPP_MINUS_MINUS)
7362 unary_operator = PREDECREMENT_EXPR;
7363 /* Handle the GNU address-of-label extension. */
7364 else if (cp_parser_allow_gnu_extensions_p (parser)
7365 && token->type == CPP_AND_AND)
7366 {
7367 tree identifier;
7368 tree expression;
7369 location_t loc = token->location;
7370
7371 /* Consume the '&&' token. */
7372 cp_lexer_consume_token (parser->lexer);
7373 /* Look for the identifier. */
7374 identifier = cp_parser_identifier (parser);
7375 /* Create an expression representing the address. */
7376 expression = finish_label_address_expr (identifier, loc);
7377 if (cp_parser_non_integral_constant_expression (parser,
7378 NIC_ADDR_LABEL))
7379 expression = error_mark_node;
7380 return expression;
7381 }
7382 }
7383 if (unary_operator != ERROR_MARK)
7384 {
7385 tree cast_expression;
7386 tree expression = error_mark_node;
7387 non_integral_constant non_constant_p = NIC_NONE;
7388 location_t loc = token->location;
7389 tsubst_flags_t complain = complain_flags (decltype_p);
7390
7391 /* Consume the operator token. */
7392 token = cp_lexer_consume_token (parser->lexer);
7393 /* Parse the cast-expression. */
7394 cast_expression
7395 = cp_parser_cast_expression (parser,
7396 unary_operator == ADDR_EXPR,
7397 /*cast_p=*/false,
7398 /*decltype*/false,
7399 pidk);
7400 /* Now, build an appropriate representation. */
7401 switch (unary_operator)
7402 {
7403 case INDIRECT_REF:
7404 non_constant_p = NIC_STAR;
7405 expression = build_x_indirect_ref (loc, cast_expression,
7406 RO_UNARY_STAR,
7407 complain);
7408 break;
7409
7410 case ADDR_EXPR:
7411 non_constant_p = NIC_ADDR;
7412 /* Fall through. */
7413 case BIT_NOT_EXPR:
7414 expression = build_x_unary_op (loc, unary_operator,
7415 cast_expression,
7416 complain);
7417 break;
7418
7419 case PREINCREMENT_EXPR:
7420 case PREDECREMENT_EXPR:
7421 non_constant_p = unary_operator == PREINCREMENT_EXPR
7422 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7423 /* Fall through. */
7424 case UNARY_PLUS_EXPR:
7425 case NEGATE_EXPR:
7426 case TRUTH_NOT_EXPR:
7427 expression = finish_unary_op_expr (loc, unary_operator,
7428 cast_expression, complain);
7429 break;
7430
7431 default:
7432 gcc_unreachable ();
7433 }
7434
7435 if (non_constant_p != NIC_NONE
7436 && cp_parser_non_integral_constant_expression (parser,
7437 non_constant_p))
7438 expression = error_mark_node;
7439
7440 return expression;
7441 }
7442
7443 return cp_parser_postfix_expression (parser, address_p, cast_p,
7444 /*member_access_only_p=*/false,
7445 decltype_p,
7446 pidk);
7447 }
7448
7449 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7450 unary-operator, the corresponding tree code is returned. */
7451
7452 static enum tree_code
7453 cp_parser_unary_operator (cp_token* token)
7454 {
7455 switch (token->type)
7456 {
7457 case CPP_MULT:
7458 return INDIRECT_REF;
7459
7460 case CPP_AND:
7461 return ADDR_EXPR;
7462
7463 case CPP_PLUS:
7464 return UNARY_PLUS_EXPR;
7465
7466 case CPP_MINUS:
7467 return NEGATE_EXPR;
7468
7469 case CPP_NOT:
7470 return TRUTH_NOT_EXPR;
7471
7472 case CPP_COMPL:
7473 return BIT_NOT_EXPR;
7474
7475 default:
7476 return ERROR_MARK;
7477 }
7478 }
7479
7480 /* Parse a new-expression.
7481
7482 new-expression:
7483 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7484 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7485
7486 Returns a representation of the expression. */
7487
7488 static tree
7489 cp_parser_new_expression (cp_parser* parser)
7490 {
7491 bool global_scope_p;
7492 vec<tree, va_gc> *placement;
7493 tree type;
7494 vec<tree, va_gc> *initializer;
7495 tree nelts = NULL_TREE;
7496 tree ret;
7497
7498 /* Look for the optional `::' operator. */
7499 global_scope_p
7500 = (cp_parser_global_scope_opt (parser,
7501 /*current_scope_valid_p=*/false)
7502 != NULL_TREE);
7503 /* Look for the `new' operator. */
7504 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7505 /* There's no easy way to tell a new-placement from the
7506 `( type-id )' construct. */
7507 cp_parser_parse_tentatively (parser);
7508 /* Look for a new-placement. */
7509 placement = cp_parser_new_placement (parser);
7510 /* If that didn't work out, there's no new-placement. */
7511 if (!cp_parser_parse_definitely (parser))
7512 {
7513 if (placement != NULL)
7514 release_tree_vector (placement);
7515 placement = NULL;
7516 }
7517
7518 /* If the next token is a `(', then we have a parenthesized
7519 type-id. */
7520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7521 {
7522 cp_token *token;
7523 const char *saved_message = parser->type_definition_forbidden_message;
7524
7525 /* Consume the `('. */
7526 cp_lexer_consume_token (parser->lexer);
7527
7528 /* Parse the type-id. */
7529 parser->type_definition_forbidden_message
7530 = G_("types may not be defined in a new-expression");
7531 type = cp_parser_type_id (parser);
7532 parser->type_definition_forbidden_message = saved_message;
7533
7534 /* Look for the closing `)'. */
7535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7536 token = cp_lexer_peek_token (parser->lexer);
7537 /* There should not be a direct-new-declarator in this production,
7538 but GCC used to allowed this, so we check and emit a sensible error
7539 message for this case. */
7540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7541 {
7542 error_at (token->location,
7543 "array bound forbidden after parenthesized type-id");
7544 inform (token->location,
7545 "try removing the parentheses around the type-id");
7546 cp_parser_direct_new_declarator (parser);
7547 }
7548 }
7549 /* Otherwise, there must be a new-type-id. */
7550 else
7551 type = cp_parser_new_type_id (parser, &nelts);
7552
7553 /* If the next token is a `(' or '{', then we have a new-initializer. */
7554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7555 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7556 initializer = cp_parser_new_initializer (parser);
7557 else
7558 initializer = NULL;
7559
7560 /* A new-expression may not appear in an integral constant
7561 expression. */
7562 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7563 ret = error_mark_node;
7564 else
7565 {
7566 /* Create a representation of the new-expression. */
7567 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7568 tf_warning_or_error);
7569 }
7570
7571 if (placement != NULL)
7572 release_tree_vector (placement);
7573 if (initializer != NULL)
7574 release_tree_vector (initializer);
7575
7576 return ret;
7577 }
7578
7579 /* Parse a new-placement.
7580
7581 new-placement:
7582 ( expression-list )
7583
7584 Returns the same representation as for an expression-list. */
7585
7586 static vec<tree, va_gc> *
7587 cp_parser_new_placement (cp_parser* parser)
7588 {
7589 vec<tree, va_gc> *expression_list;
7590
7591 /* Parse the expression-list. */
7592 expression_list = (cp_parser_parenthesized_expression_list
7593 (parser, non_attr, /*cast_p=*/false,
7594 /*allow_expansion_p=*/true,
7595 /*non_constant_p=*/NULL));
7596
7597 return expression_list;
7598 }
7599
7600 /* Parse a new-type-id.
7601
7602 new-type-id:
7603 type-specifier-seq new-declarator [opt]
7604
7605 Returns the TYPE allocated. If the new-type-id indicates an array
7606 type, *NELTS is set to the number of elements in the last array
7607 bound; the TYPE will not include the last array bound. */
7608
7609 static tree
7610 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7611 {
7612 cp_decl_specifier_seq type_specifier_seq;
7613 cp_declarator *new_declarator;
7614 cp_declarator *declarator;
7615 cp_declarator *outer_declarator;
7616 const char *saved_message;
7617
7618 /* The type-specifier sequence must not contain type definitions.
7619 (It cannot contain declarations of new types either, but if they
7620 are not definitions we will catch that because they are not
7621 complete.) */
7622 saved_message = parser->type_definition_forbidden_message;
7623 parser->type_definition_forbidden_message
7624 = G_("types may not be defined in a new-type-id");
7625 /* Parse the type-specifier-seq. */
7626 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7627 /*is_trailing_return=*/false,
7628 &type_specifier_seq);
7629 /* Restore the old message. */
7630 parser->type_definition_forbidden_message = saved_message;
7631
7632 if (type_specifier_seq.type == error_mark_node)
7633 return error_mark_node;
7634
7635 /* Parse the new-declarator. */
7636 new_declarator = cp_parser_new_declarator_opt (parser);
7637
7638 /* Determine the number of elements in the last array dimension, if
7639 any. */
7640 *nelts = NULL_TREE;
7641 /* Skip down to the last array dimension. */
7642 declarator = new_declarator;
7643 outer_declarator = NULL;
7644 while (declarator && (declarator->kind == cdk_pointer
7645 || declarator->kind == cdk_ptrmem))
7646 {
7647 outer_declarator = declarator;
7648 declarator = declarator->declarator;
7649 }
7650 while (declarator
7651 && declarator->kind == cdk_array
7652 && declarator->declarator
7653 && declarator->declarator->kind == cdk_array)
7654 {
7655 outer_declarator = declarator;
7656 declarator = declarator->declarator;
7657 }
7658
7659 if (declarator && declarator->kind == cdk_array)
7660 {
7661 *nelts = declarator->u.array.bounds;
7662 if (*nelts == error_mark_node)
7663 *nelts = integer_one_node;
7664
7665 if (outer_declarator)
7666 outer_declarator->declarator = declarator->declarator;
7667 else
7668 new_declarator = NULL;
7669 }
7670
7671 return groktypename (&type_specifier_seq, new_declarator, false);
7672 }
7673
7674 /* Parse an (optional) new-declarator.
7675
7676 new-declarator:
7677 ptr-operator new-declarator [opt]
7678 direct-new-declarator
7679
7680 Returns the declarator. */
7681
7682 static cp_declarator *
7683 cp_parser_new_declarator_opt (cp_parser* parser)
7684 {
7685 enum tree_code code;
7686 tree type, std_attributes = NULL_TREE;
7687 cp_cv_quals cv_quals;
7688
7689 /* We don't know if there's a ptr-operator next, or not. */
7690 cp_parser_parse_tentatively (parser);
7691 /* Look for a ptr-operator. */
7692 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7693 /* If that worked, look for more new-declarators. */
7694 if (cp_parser_parse_definitely (parser))
7695 {
7696 cp_declarator *declarator;
7697
7698 /* Parse another optional declarator. */
7699 declarator = cp_parser_new_declarator_opt (parser);
7700
7701 declarator = cp_parser_make_indirect_declarator
7702 (code, type, cv_quals, declarator, std_attributes);
7703
7704 return declarator;
7705 }
7706
7707 /* If the next token is a `[', there is a direct-new-declarator. */
7708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7709 return cp_parser_direct_new_declarator (parser);
7710
7711 return NULL;
7712 }
7713
7714 /* Parse a direct-new-declarator.
7715
7716 direct-new-declarator:
7717 [ expression ]
7718 direct-new-declarator [constant-expression]
7719
7720 */
7721
7722 static cp_declarator *
7723 cp_parser_direct_new_declarator (cp_parser* parser)
7724 {
7725 cp_declarator *declarator = NULL;
7726
7727 while (true)
7728 {
7729 tree expression;
7730 cp_token *token;
7731
7732 /* Look for the opening `['. */
7733 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7734
7735 token = cp_lexer_peek_token (parser->lexer);
7736 expression = cp_parser_expression (parser);
7737 /* The standard requires that the expression have integral
7738 type. DR 74 adds enumeration types. We believe that the
7739 real intent is that these expressions be handled like the
7740 expression in a `switch' condition, which also allows
7741 classes with a single conversion to integral or
7742 enumeration type. */
7743 if (!processing_template_decl)
7744 {
7745 expression
7746 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7747 expression,
7748 /*complain=*/true);
7749 if (!expression)
7750 {
7751 error_at (token->location,
7752 "expression in new-declarator must have integral "
7753 "or enumeration type");
7754 expression = error_mark_node;
7755 }
7756 }
7757
7758 /* Look for the closing `]'. */
7759 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7760
7761 /* Add this bound to the declarator. */
7762 declarator = make_array_declarator (declarator, expression);
7763
7764 /* If the next token is not a `[', then there are no more
7765 bounds. */
7766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7767 break;
7768 }
7769
7770 return declarator;
7771 }
7772
7773 /* Parse a new-initializer.
7774
7775 new-initializer:
7776 ( expression-list [opt] )
7777 braced-init-list
7778
7779 Returns a representation of the expression-list. */
7780
7781 static vec<tree, va_gc> *
7782 cp_parser_new_initializer (cp_parser* parser)
7783 {
7784 vec<tree, va_gc> *expression_list;
7785
7786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7787 {
7788 tree t;
7789 bool expr_non_constant_p;
7790 cp_lexer_set_source_position (parser->lexer);
7791 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7792 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7793 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7794 expression_list = make_tree_vector_single (t);
7795 }
7796 else
7797 expression_list = (cp_parser_parenthesized_expression_list
7798 (parser, non_attr, /*cast_p=*/false,
7799 /*allow_expansion_p=*/true,
7800 /*non_constant_p=*/NULL));
7801
7802 return expression_list;
7803 }
7804
7805 /* Parse a delete-expression.
7806
7807 delete-expression:
7808 :: [opt] delete cast-expression
7809 :: [opt] delete [ ] cast-expression
7810
7811 Returns a representation of the expression. */
7812
7813 static tree
7814 cp_parser_delete_expression (cp_parser* parser)
7815 {
7816 bool global_scope_p;
7817 bool array_p;
7818 tree expression;
7819
7820 /* Look for the optional `::' operator. */
7821 global_scope_p
7822 = (cp_parser_global_scope_opt (parser,
7823 /*current_scope_valid_p=*/false)
7824 != NULL_TREE);
7825 /* Look for the `delete' keyword. */
7826 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7827 /* See if the array syntax is in use. */
7828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7829 {
7830 /* Consume the `[' token. */
7831 cp_lexer_consume_token (parser->lexer);
7832 /* Look for the `]' token. */
7833 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7834 /* Remember that this is the `[]' construct. */
7835 array_p = true;
7836 }
7837 else
7838 array_p = false;
7839
7840 /* Parse the cast-expression. */
7841 expression = cp_parser_simple_cast_expression (parser);
7842
7843 /* A delete-expression may not appear in an integral constant
7844 expression. */
7845 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7846 return error_mark_node;
7847
7848 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7849 tf_warning_or_error);
7850 }
7851
7852 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7853 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7854 0 otherwise. */
7855
7856 static int
7857 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7858 {
7859 cp_token *token = cp_lexer_peek_token (parser->lexer);
7860 switch (token->type)
7861 {
7862 case CPP_COMMA:
7863 case CPP_SEMICOLON:
7864 case CPP_QUERY:
7865 case CPP_COLON:
7866 case CPP_CLOSE_SQUARE:
7867 case CPP_CLOSE_PAREN:
7868 case CPP_CLOSE_BRACE:
7869 case CPP_OPEN_BRACE:
7870 case CPP_DOT:
7871 case CPP_DOT_STAR:
7872 case CPP_DEREF:
7873 case CPP_DEREF_STAR:
7874 case CPP_DIV:
7875 case CPP_MOD:
7876 case CPP_LSHIFT:
7877 case CPP_RSHIFT:
7878 case CPP_LESS:
7879 case CPP_GREATER:
7880 case CPP_LESS_EQ:
7881 case CPP_GREATER_EQ:
7882 case CPP_EQ_EQ:
7883 case CPP_NOT_EQ:
7884 case CPP_EQ:
7885 case CPP_MULT_EQ:
7886 case CPP_DIV_EQ:
7887 case CPP_MOD_EQ:
7888 case CPP_PLUS_EQ:
7889 case CPP_MINUS_EQ:
7890 case CPP_RSHIFT_EQ:
7891 case CPP_LSHIFT_EQ:
7892 case CPP_AND_EQ:
7893 case CPP_XOR_EQ:
7894 case CPP_OR_EQ:
7895 case CPP_XOR:
7896 case CPP_OR:
7897 case CPP_OR_OR:
7898 case CPP_EOF:
7899 case CPP_ELLIPSIS:
7900 return 0;
7901
7902 case CPP_OPEN_PAREN:
7903 /* In ((type ()) () the last () isn't a valid cast-expression,
7904 so the whole must be parsed as postfix-expression. */
7905 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7906 != CPP_CLOSE_PAREN;
7907
7908 case CPP_OPEN_SQUARE:
7909 /* '[' may start a primary-expression in obj-c++ and in C++11,
7910 as a lambda-expression, eg, '(void)[]{}'. */
7911 if (cxx_dialect >= cxx11)
7912 return -1;
7913 return c_dialect_objc ();
7914
7915 case CPP_PLUS_PLUS:
7916 case CPP_MINUS_MINUS:
7917 /* '++' and '--' may or may not start a cast-expression:
7918
7919 struct T { void operator++(int); };
7920 void f() { (T())++; }
7921
7922 vs
7923
7924 int a;
7925 (int)++a; */
7926 return -1;
7927
7928 default:
7929 return 1;
7930 }
7931 }
7932
7933 /* Parse a cast-expression.
7934
7935 cast-expression:
7936 unary-expression
7937 ( type-id ) cast-expression
7938
7939 ADDRESS_P is true iff the unary-expression is appearing as the
7940 operand of the `&' operator. CAST_P is true if this expression is
7941 the target of a cast.
7942
7943 Returns a representation of the expression. */
7944
7945 static tree
7946 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7947 bool decltype_p, cp_id_kind * pidk)
7948 {
7949 /* If it's a `(', then we might be looking at a cast. */
7950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7951 {
7952 tree type = NULL_TREE;
7953 tree expr = NULL_TREE;
7954 int cast_expression = 0;
7955 const char *saved_message;
7956
7957 /* There's no way to know yet whether or not this is a cast.
7958 For example, `(int (3))' is a unary-expression, while `(int)
7959 3' is a cast. So, we resort to parsing tentatively. */
7960 cp_parser_parse_tentatively (parser);
7961 /* Types may not be defined in a cast. */
7962 saved_message = parser->type_definition_forbidden_message;
7963 parser->type_definition_forbidden_message
7964 = G_("types may not be defined in casts");
7965 /* Consume the `('. */
7966 cp_lexer_consume_token (parser->lexer);
7967 /* A very tricky bit is that `(struct S) { 3 }' is a
7968 compound-literal (which we permit in C++ as an extension).
7969 But, that construct is not a cast-expression -- it is a
7970 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7971 is legal; if the compound-literal were a cast-expression,
7972 you'd need an extra set of parentheses.) But, if we parse
7973 the type-id, and it happens to be a class-specifier, then we
7974 will commit to the parse at that point, because we cannot
7975 undo the action that is done when creating a new class. So,
7976 then we cannot back up and do a postfix-expression.
7977
7978 Another tricky case is the following (c++/29234):
7979
7980 struct S { void operator () (); };
7981
7982 void foo ()
7983 {
7984 ( S()() );
7985 }
7986
7987 As a type-id we parse the parenthesized S()() as a function
7988 returning a function, groktypename complains and we cannot
7989 back up in this case either.
7990
7991 Therefore, we scan ahead to the closing `)', and check to see
7992 if the tokens after the `)' can start a cast-expression. Otherwise
7993 we are dealing with an unary-expression, a postfix-expression
7994 or something else.
7995
7996 Yet another tricky case, in C++11, is the following (c++/54891):
7997
7998 (void)[]{};
7999
8000 The issue is that usually, besides the case of lambda-expressions,
8001 the parenthesized type-id cannot be followed by '[', and, eg, we
8002 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8003 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8004 we don't commit, we try a cast-expression, then an unary-expression.
8005
8006 Save tokens so that we can put them back. */
8007 cp_lexer_save_tokens (parser->lexer);
8008
8009 /* We may be looking at a cast-expression. */
8010 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8011 /*consume_paren=*/true))
8012 cast_expression
8013 = cp_parser_tokens_start_cast_expression (parser);
8014
8015 /* Roll back the tokens we skipped. */
8016 cp_lexer_rollback_tokens (parser->lexer);
8017 /* If we aren't looking at a cast-expression, simulate an error so
8018 that the call to cp_parser_error_occurred below returns true. */
8019 if (!cast_expression)
8020 cp_parser_simulate_error (parser);
8021 else
8022 {
8023 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8024 parser->in_type_id_in_expr_p = true;
8025 /* Look for the type-id. */
8026 type = cp_parser_type_id (parser);
8027 /* Look for the closing `)'. */
8028 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8029 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8030 }
8031
8032 /* Restore the saved message. */
8033 parser->type_definition_forbidden_message = saved_message;
8034
8035 /* At this point this can only be either a cast or a
8036 parenthesized ctor such as `(T ())' that looks like a cast to
8037 function returning T. */
8038 if (!cp_parser_error_occurred (parser))
8039 {
8040 /* Only commit if the cast-expression doesn't start with
8041 '++', '--', or '[' in C++11. */
8042 if (cast_expression > 0)
8043 cp_parser_commit_to_topmost_tentative_parse (parser);
8044
8045 expr = cp_parser_cast_expression (parser,
8046 /*address_p=*/false,
8047 /*cast_p=*/true,
8048 /*decltype_p=*/false,
8049 pidk);
8050
8051 if (cp_parser_parse_definitely (parser))
8052 {
8053 /* Warn about old-style casts, if so requested. */
8054 if (warn_old_style_cast
8055 && !in_system_header_at (input_location)
8056 && !VOID_TYPE_P (type)
8057 && current_lang_name != lang_name_c)
8058 warning (OPT_Wold_style_cast, "use of old-style cast");
8059
8060 /* Only type conversions to integral or enumeration types
8061 can be used in constant-expressions. */
8062 if (!cast_valid_in_integral_constant_expression_p (type)
8063 && cp_parser_non_integral_constant_expression (parser,
8064 NIC_CAST))
8065 return error_mark_node;
8066
8067 /* Perform the cast. */
8068 expr = build_c_cast (input_location, type, expr);
8069 return expr;
8070 }
8071 }
8072 else
8073 cp_parser_abort_tentative_parse (parser);
8074 }
8075
8076 /* If we get here, then it's not a cast, so it must be a
8077 unary-expression. */
8078 return cp_parser_unary_expression (parser, pidk, address_p,
8079 cast_p, decltype_p);
8080 }
8081
8082 /* Parse a binary expression of the general form:
8083
8084 pm-expression:
8085 cast-expression
8086 pm-expression .* cast-expression
8087 pm-expression ->* cast-expression
8088
8089 multiplicative-expression:
8090 pm-expression
8091 multiplicative-expression * pm-expression
8092 multiplicative-expression / pm-expression
8093 multiplicative-expression % pm-expression
8094
8095 additive-expression:
8096 multiplicative-expression
8097 additive-expression + multiplicative-expression
8098 additive-expression - multiplicative-expression
8099
8100 shift-expression:
8101 additive-expression
8102 shift-expression << additive-expression
8103 shift-expression >> additive-expression
8104
8105 relational-expression:
8106 shift-expression
8107 relational-expression < shift-expression
8108 relational-expression > shift-expression
8109 relational-expression <= shift-expression
8110 relational-expression >= shift-expression
8111
8112 GNU Extension:
8113
8114 relational-expression:
8115 relational-expression <? shift-expression
8116 relational-expression >? shift-expression
8117
8118 equality-expression:
8119 relational-expression
8120 equality-expression == relational-expression
8121 equality-expression != relational-expression
8122
8123 and-expression:
8124 equality-expression
8125 and-expression & equality-expression
8126
8127 exclusive-or-expression:
8128 and-expression
8129 exclusive-or-expression ^ and-expression
8130
8131 inclusive-or-expression:
8132 exclusive-or-expression
8133 inclusive-or-expression | exclusive-or-expression
8134
8135 logical-and-expression:
8136 inclusive-or-expression
8137 logical-and-expression && inclusive-or-expression
8138
8139 logical-or-expression:
8140 logical-and-expression
8141 logical-or-expression || logical-and-expression
8142
8143 All these are implemented with a single function like:
8144
8145 binary-expression:
8146 simple-cast-expression
8147 binary-expression <token> binary-expression
8148
8149 CAST_P is true if this expression is the target of a cast.
8150
8151 The binops_by_token map is used to get the tree codes for each <token> type.
8152 binary-expressions are associated according to a precedence table. */
8153
8154 #define TOKEN_PRECEDENCE(token) \
8155 (((token->type == CPP_GREATER \
8156 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8157 && !parser->greater_than_is_operator_p) \
8158 ? PREC_NOT_OPERATOR \
8159 : binops_by_token[token->type].prec)
8160
8161 static tree
8162 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8163 bool no_toplevel_fold_p,
8164 bool decltype_p,
8165 enum cp_parser_prec prec,
8166 cp_id_kind * pidk)
8167 {
8168 cp_parser_expression_stack stack;
8169 cp_parser_expression_stack_entry *sp = &stack[0];
8170 cp_parser_expression_stack_entry current;
8171 tree rhs;
8172 cp_token *token;
8173 enum tree_code rhs_type;
8174 enum cp_parser_prec new_prec, lookahead_prec;
8175 tree overload;
8176
8177 /* Parse the first expression. */
8178 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8179 ? TRUTH_NOT_EXPR : ERROR_MARK);
8180 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8181 cast_p, decltype_p, pidk);
8182 current.prec = prec;
8183
8184 if (cp_parser_error_occurred (parser))
8185 return error_mark_node;
8186
8187 for (;;)
8188 {
8189 /* Get an operator token. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191
8192 if (warn_cxx0x_compat
8193 && token->type == CPP_RSHIFT
8194 && !parser->greater_than_is_operator_p)
8195 {
8196 if (warning_at (token->location, OPT_Wc__0x_compat,
8197 "%<>>%> operator is treated"
8198 " as two right angle brackets in C++11"))
8199 inform (token->location,
8200 "suggest parentheses around %<>>%> expression");
8201 }
8202
8203 new_prec = TOKEN_PRECEDENCE (token);
8204
8205 /* Popping an entry off the stack means we completed a subexpression:
8206 - either we found a token which is not an operator (`>' where it is not
8207 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8208 will happen repeatedly;
8209 - or, we found an operator which has lower priority. This is the case
8210 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8211 parsing `3 * 4'. */
8212 if (new_prec <= current.prec)
8213 {
8214 if (sp == stack)
8215 break;
8216 else
8217 goto pop;
8218 }
8219
8220 get_rhs:
8221 current.tree_type = binops_by_token[token->type].tree_type;
8222 current.loc = token->location;
8223
8224 /* We used the operator token. */
8225 cp_lexer_consume_token (parser->lexer);
8226
8227 /* For "false && x" or "true || x", x will never be executed;
8228 disable warnings while evaluating it. */
8229 if (current.tree_type == TRUTH_ANDIF_EXPR)
8230 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8231 else if (current.tree_type == TRUTH_ORIF_EXPR)
8232 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8233
8234 /* Extract another operand. It may be the RHS of this expression
8235 or the LHS of a new, higher priority expression. */
8236 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8237 ? TRUTH_NOT_EXPR : ERROR_MARK);
8238 rhs = cp_parser_simple_cast_expression (parser);
8239
8240 /* Get another operator token. Look up its precedence to avoid
8241 building a useless (immediately popped) stack entry for common
8242 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8243 token = cp_lexer_peek_token (parser->lexer);
8244 lookahead_prec = TOKEN_PRECEDENCE (token);
8245 if (lookahead_prec > new_prec)
8246 {
8247 /* ... and prepare to parse the RHS of the new, higher priority
8248 expression. Since precedence levels on the stack are
8249 monotonically increasing, we do not have to care about
8250 stack overflows. */
8251 *sp = current;
8252 ++sp;
8253 current.lhs = rhs;
8254 current.lhs_type = rhs_type;
8255 current.prec = new_prec;
8256 new_prec = lookahead_prec;
8257 goto get_rhs;
8258
8259 pop:
8260 lookahead_prec = new_prec;
8261 /* If the stack is not empty, we have parsed into LHS the right side
8262 (`4' in the example above) of an expression we had suspended.
8263 We can use the information on the stack to recover the LHS (`3')
8264 from the stack together with the tree code (`MULT_EXPR'), and
8265 the precedence of the higher level subexpression
8266 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8267 which will be used to actually build the additive expression. */
8268 rhs = current.lhs;
8269 rhs_type = current.lhs_type;
8270 --sp;
8271 current = *sp;
8272 }
8273
8274 /* Undo the disabling of warnings done above. */
8275 if (current.tree_type == TRUTH_ANDIF_EXPR)
8276 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8277 else if (current.tree_type == TRUTH_ORIF_EXPR)
8278 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8279
8280 if (warn_logical_not_paren
8281 && current.lhs_type == TRUTH_NOT_EXPR)
8282 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8283
8284 overload = NULL;
8285 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8286 ERROR_MARK for everything that is not a binary expression.
8287 This makes warn_about_parentheses miss some warnings that
8288 involve unary operators. For unary expressions we should
8289 pass the correct tree_code unless the unary expression was
8290 surrounded by parentheses.
8291 */
8292 if (no_toplevel_fold_p
8293 && lookahead_prec <= current.prec
8294 && sp == stack)
8295 current.lhs = build2 (current.tree_type,
8296 TREE_CODE_CLASS (current.tree_type)
8297 == tcc_comparison
8298 ? boolean_type_node : TREE_TYPE (current.lhs),
8299 current.lhs, rhs);
8300 else
8301 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8302 current.lhs, current.lhs_type,
8303 rhs, rhs_type, &overload,
8304 complain_flags (decltype_p));
8305 current.lhs_type = current.tree_type;
8306 if (EXPR_P (current.lhs))
8307 SET_EXPR_LOCATION (current.lhs, current.loc);
8308
8309 /* If the binary operator required the use of an overloaded operator,
8310 then this expression cannot be an integral constant-expression.
8311 An overloaded operator can be used even if both operands are
8312 otherwise permissible in an integral constant-expression if at
8313 least one of the operands is of enumeration type. */
8314
8315 if (overload
8316 && cp_parser_non_integral_constant_expression (parser,
8317 NIC_OVERLOADED))
8318 return error_mark_node;
8319 }
8320
8321 return current.lhs;
8322 }
8323
8324 static tree
8325 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8326 bool no_toplevel_fold_p,
8327 enum cp_parser_prec prec,
8328 cp_id_kind * pidk)
8329 {
8330 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8331 /*decltype*/false, prec, pidk);
8332 }
8333
8334 /* Parse the `? expression : assignment-expression' part of a
8335 conditional-expression. The LOGICAL_OR_EXPR is the
8336 logical-or-expression that started the conditional-expression.
8337 Returns a representation of the entire conditional-expression.
8338
8339 This routine is used by cp_parser_assignment_expression.
8340
8341 ? expression : assignment-expression
8342
8343 GNU Extensions:
8344
8345 ? : assignment-expression */
8346
8347 static tree
8348 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8349 {
8350 tree expr;
8351 tree assignment_expr;
8352 struct cp_token *token;
8353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8354
8355 /* Consume the `?' token. */
8356 cp_lexer_consume_token (parser->lexer);
8357 token = cp_lexer_peek_token (parser->lexer);
8358 if (cp_parser_allow_gnu_extensions_p (parser)
8359 && token->type == CPP_COLON)
8360 {
8361 pedwarn (token->location, OPT_Wpedantic,
8362 "ISO C++ does not allow ?: with omitted middle operand");
8363 /* Implicit true clause. */
8364 expr = NULL_TREE;
8365 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8366 warn_for_omitted_condop (token->location, logical_or_expr);
8367 }
8368 else
8369 {
8370 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8371 parser->colon_corrects_to_scope_p = false;
8372 /* Parse the expression. */
8373 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8374 expr = cp_parser_expression (parser);
8375 c_inhibit_evaluation_warnings +=
8376 ((logical_or_expr == truthvalue_true_node)
8377 - (logical_or_expr == truthvalue_false_node));
8378 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8379 }
8380
8381 /* The next token should be a `:'. */
8382 cp_parser_require (parser, CPP_COLON, RT_COLON);
8383 /* Parse the assignment-expression. */
8384 assignment_expr = cp_parser_assignment_expression (parser);
8385 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8386
8387 /* Build the conditional-expression. */
8388 return build_x_conditional_expr (loc, logical_or_expr,
8389 expr,
8390 assignment_expr,
8391 tf_warning_or_error);
8392 }
8393
8394 /* Parse an assignment-expression.
8395
8396 assignment-expression:
8397 conditional-expression
8398 logical-or-expression assignment-operator assignment_expression
8399 throw-expression
8400
8401 CAST_P is true if this expression is the target of a cast.
8402 DECLTYPE_P is true if this expression is the operand of decltype.
8403
8404 Returns a representation for the expression. */
8405
8406 static tree
8407 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8408 bool cast_p, bool decltype_p)
8409 {
8410 tree expr;
8411
8412 /* If the next token is the `throw' keyword, then we're looking at
8413 a throw-expression. */
8414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8415 expr = cp_parser_throw_expression (parser);
8416 /* Otherwise, it must be that we are looking at a
8417 logical-or-expression. */
8418 else
8419 {
8420 /* Parse the binary expressions (logical-or-expression). */
8421 expr = cp_parser_binary_expression (parser, cast_p, false,
8422 decltype_p,
8423 PREC_NOT_OPERATOR, pidk);
8424 /* If the next token is a `?' then we're actually looking at a
8425 conditional-expression. */
8426 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8427 return cp_parser_question_colon_clause (parser, expr);
8428 else
8429 {
8430 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8431
8432 /* If it's an assignment-operator, we're using the second
8433 production. */
8434 enum tree_code assignment_operator
8435 = cp_parser_assignment_operator_opt (parser);
8436 if (assignment_operator != ERROR_MARK)
8437 {
8438 bool non_constant_p;
8439 location_t saved_input_location;
8440
8441 /* Parse the right-hand side of the assignment. */
8442 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8443
8444 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8446
8447 /* An assignment may not appear in a
8448 constant-expression. */
8449 if (cp_parser_non_integral_constant_expression (parser,
8450 NIC_ASSIGNMENT))
8451 return error_mark_node;
8452 /* Build the assignment expression. Its default
8453 location is the location of the '=' token. */
8454 saved_input_location = input_location;
8455 input_location = loc;
8456 expr = build_x_modify_expr (loc, expr,
8457 assignment_operator,
8458 rhs,
8459 complain_flags (decltype_p));
8460 input_location = saved_input_location;
8461 }
8462 }
8463 }
8464
8465 return expr;
8466 }
8467
8468 /* Parse an (optional) assignment-operator.
8469
8470 assignment-operator: one of
8471 = *= /= %= += -= >>= <<= &= ^= |=
8472
8473 GNU Extension:
8474
8475 assignment-operator: one of
8476 <?= >?=
8477
8478 If the next token is an assignment operator, the corresponding tree
8479 code is returned, and the token is consumed. For example, for
8480 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8481 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8482 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8483 operator, ERROR_MARK is returned. */
8484
8485 static enum tree_code
8486 cp_parser_assignment_operator_opt (cp_parser* parser)
8487 {
8488 enum tree_code op;
8489 cp_token *token;
8490
8491 /* Peek at the next token. */
8492 token = cp_lexer_peek_token (parser->lexer);
8493
8494 switch (token->type)
8495 {
8496 case CPP_EQ:
8497 op = NOP_EXPR;
8498 break;
8499
8500 case CPP_MULT_EQ:
8501 op = MULT_EXPR;
8502 break;
8503
8504 case CPP_DIV_EQ:
8505 op = TRUNC_DIV_EXPR;
8506 break;
8507
8508 case CPP_MOD_EQ:
8509 op = TRUNC_MOD_EXPR;
8510 break;
8511
8512 case CPP_PLUS_EQ:
8513 op = PLUS_EXPR;
8514 break;
8515
8516 case CPP_MINUS_EQ:
8517 op = MINUS_EXPR;
8518 break;
8519
8520 case CPP_RSHIFT_EQ:
8521 op = RSHIFT_EXPR;
8522 break;
8523
8524 case CPP_LSHIFT_EQ:
8525 op = LSHIFT_EXPR;
8526 break;
8527
8528 case CPP_AND_EQ:
8529 op = BIT_AND_EXPR;
8530 break;
8531
8532 case CPP_XOR_EQ:
8533 op = BIT_XOR_EXPR;
8534 break;
8535
8536 case CPP_OR_EQ:
8537 op = BIT_IOR_EXPR;
8538 break;
8539
8540 default:
8541 /* Nothing else is an assignment operator. */
8542 op = ERROR_MARK;
8543 }
8544
8545 /* If it was an assignment operator, consume it. */
8546 if (op != ERROR_MARK)
8547 cp_lexer_consume_token (parser->lexer);
8548
8549 return op;
8550 }
8551
8552 /* Parse an expression.
8553
8554 expression:
8555 assignment-expression
8556 expression , assignment-expression
8557
8558 CAST_P is true if this expression is the target of a cast.
8559 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8560 except possibly parenthesized or on the RHS of a comma (N3276).
8561
8562 Returns a representation of the expression. */
8563
8564 static tree
8565 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8566 bool cast_p, bool decltype_p)
8567 {
8568 tree expression = NULL_TREE;
8569 location_t loc = UNKNOWN_LOCATION;
8570
8571 while (true)
8572 {
8573 tree assignment_expression;
8574
8575 /* Parse the next assignment-expression. */
8576 assignment_expression
8577 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8578
8579 /* We don't create a temporary for a call that is the immediate operand
8580 of decltype or on the RHS of a comma. But when we see a comma, we
8581 need to create a temporary for a call on the LHS. */
8582 if (decltype_p && !processing_template_decl
8583 && TREE_CODE (assignment_expression) == CALL_EXPR
8584 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8585 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8586 assignment_expression
8587 = build_cplus_new (TREE_TYPE (assignment_expression),
8588 assignment_expression, tf_warning_or_error);
8589
8590 /* If this is the first assignment-expression, we can just
8591 save it away. */
8592 if (!expression)
8593 expression = assignment_expression;
8594 else
8595 expression = build_x_compound_expr (loc, expression,
8596 assignment_expression,
8597 complain_flags (decltype_p));
8598 /* If the next token is not a comma, then we are done with the
8599 expression. */
8600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8601 break;
8602 /* Consume the `,'. */
8603 loc = cp_lexer_peek_token (parser->lexer)->location;
8604 cp_lexer_consume_token (parser->lexer);
8605 /* A comma operator cannot appear in a constant-expression. */
8606 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8607 expression = error_mark_node;
8608 }
8609
8610 return expression;
8611 }
8612
8613 /* Parse a constant-expression.
8614
8615 constant-expression:
8616 conditional-expression
8617
8618 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8619 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8620 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8621 is false, NON_CONSTANT_P should be NULL. */
8622
8623 static tree
8624 cp_parser_constant_expression (cp_parser* parser,
8625 bool allow_non_constant_p,
8626 bool *non_constant_p)
8627 {
8628 bool saved_integral_constant_expression_p;
8629 bool saved_allow_non_integral_constant_expression_p;
8630 bool saved_non_integral_constant_expression_p;
8631 tree expression;
8632
8633 /* It might seem that we could simply parse the
8634 conditional-expression, and then check to see if it were
8635 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8636 one that the compiler can figure out is constant, possibly after
8637 doing some simplifications or optimizations. The standard has a
8638 precise definition of constant-expression, and we must honor
8639 that, even though it is somewhat more restrictive.
8640
8641 For example:
8642
8643 int i[(2, 3)];
8644
8645 is not a legal declaration, because `(2, 3)' is not a
8646 constant-expression. The `,' operator is forbidden in a
8647 constant-expression. However, GCC's constant-folding machinery
8648 will fold this operation to an INTEGER_CST for `3'. */
8649
8650 /* Save the old settings. */
8651 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8652 saved_allow_non_integral_constant_expression_p
8653 = parser->allow_non_integral_constant_expression_p;
8654 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8655 /* We are now parsing a constant-expression. */
8656 parser->integral_constant_expression_p = true;
8657 parser->allow_non_integral_constant_expression_p
8658 = (allow_non_constant_p || cxx_dialect >= cxx11);
8659 parser->non_integral_constant_expression_p = false;
8660 /* Although the grammar says "conditional-expression", we parse an
8661 "assignment-expression", which also permits "throw-expression"
8662 and the use of assignment operators. In the case that
8663 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8664 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8665 actually essential that we look for an assignment-expression.
8666 For example, cp_parser_initializer_clauses uses this function to
8667 determine whether a particular assignment-expression is in fact
8668 constant. */
8669 expression = cp_parser_assignment_expression (parser);
8670 /* Restore the old settings. */
8671 parser->integral_constant_expression_p
8672 = saved_integral_constant_expression_p;
8673 parser->allow_non_integral_constant_expression_p
8674 = saved_allow_non_integral_constant_expression_p;
8675 if (cxx_dialect >= cxx11)
8676 {
8677 /* Require an rvalue constant expression here; that's what our
8678 callers expect. Reference constant expressions are handled
8679 separately in e.g. cp_parser_template_argument. */
8680 bool is_const = potential_rvalue_constant_expression (expression);
8681 parser->non_integral_constant_expression_p = !is_const;
8682 if (!is_const && !allow_non_constant_p)
8683 require_potential_rvalue_constant_expression (expression);
8684 }
8685 if (allow_non_constant_p)
8686 *non_constant_p = parser->non_integral_constant_expression_p;
8687 parser->non_integral_constant_expression_p
8688 = saved_non_integral_constant_expression_p;
8689
8690 return expression;
8691 }
8692
8693 /* Parse __builtin_offsetof.
8694
8695 offsetof-expression:
8696 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8697
8698 offsetof-member-designator:
8699 id-expression
8700 | offsetof-member-designator "." id-expression
8701 | offsetof-member-designator "[" expression "]"
8702 | offsetof-member-designator "->" id-expression */
8703
8704 static tree
8705 cp_parser_builtin_offsetof (cp_parser *parser)
8706 {
8707 int save_ice_p, save_non_ice_p;
8708 tree type, expr;
8709 cp_id_kind dummy;
8710 cp_token *token;
8711
8712 /* We're about to accept non-integral-constant things, but will
8713 definitely yield an integral constant expression. Save and
8714 restore these values around our local parsing. */
8715 save_ice_p = parser->integral_constant_expression_p;
8716 save_non_ice_p = parser->non_integral_constant_expression_p;
8717
8718 /* Consume the "__builtin_offsetof" token. */
8719 cp_lexer_consume_token (parser->lexer);
8720 /* Consume the opening `('. */
8721 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8722 /* Parse the type-id. */
8723 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8724 type = cp_parser_type_id (parser);
8725 /* Look for the `,'. */
8726 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8727 token = cp_lexer_peek_token (parser->lexer);
8728
8729 /* Build the (type *)null that begins the traditional offsetof macro. */
8730 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8731 tf_warning_or_error);
8732
8733 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8734 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8735 true, &dummy, token->location);
8736 while (true)
8737 {
8738 token = cp_lexer_peek_token (parser->lexer);
8739 switch (token->type)
8740 {
8741 case CPP_OPEN_SQUARE:
8742 /* offsetof-member-designator "[" expression "]" */
8743 expr = cp_parser_postfix_open_square_expression (parser, expr,
8744 true, false);
8745 break;
8746
8747 case CPP_DEREF:
8748 /* offsetof-member-designator "->" identifier */
8749 expr = grok_array_decl (token->location, expr,
8750 integer_zero_node, false);
8751 /* FALLTHRU */
8752
8753 case CPP_DOT:
8754 /* offsetof-member-designator "." identifier */
8755 cp_lexer_consume_token (parser->lexer);
8756 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8757 expr, true, &dummy,
8758 token->location);
8759 break;
8760
8761 case CPP_CLOSE_PAREN:
8762 /* Consume the ")" token. */
8763 cp_lexer_consume_token (parser->lexer);
8764 goto success;
8765
8766 default:
8767 /* Error. We know the following require will fail, but
8768 that gives the proper error message. */
8769 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8770 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8771 expr = error_mark_node;
8772 goto failure;
8773 }
8774 }
8775
8776 success:
8777 expr = finish_offsetof (expr, loc);
8778
8779 failure:
8780 parser->integral_constant_expression_p = save_ice_p;
8781 parser->non_integral_constant_expression_p = save_non_ice_p;
8782
8783 return expr;
8784 }
8785
8786 /* Parse a trait expression.
8787
8788 Returns a representation of the expression, the underlying type
8789 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8790
8791 static tree
8792 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8793 {
8794 cp_trait_kind kind;
8795 tree type1, type2 = NULL_TREE;
8796 bool binary = false;
8797 bool variadic = false;
8798
8799 switch (keyword)
8800 {
8801 case RID_HAS_NOTHROW_ASSIGN:
8802 kind = CPTK_HAS_NOTHROW_ASSIGN;
8803 break;
8804 case RID_HAS_NOTHROW_CONSTRUCTOR:
8805 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8806 break;
8807 case RID_HAS_NOTHROW_COPY:
8808 kind = CPTK_HAS_NOTHROW_COPY;
8809 break;
8810 case RID_HAS_TRIVIAL_ASSIGN:
8811 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8812 break;
8813 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8814 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8815 break;
8816 case RID_HAS_TRIVIAL_COPY:
8817 kind = CPTK_HAS_TRIVIAL_COPY;
8818 break;
8819 case RID_HAS_TRIVIAL_DESTRUCTOR:
8820 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8821 break;
8822 case RID_HAS_VIRTUAL_DESTRUCTOR:
8823 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8824 break;
8825 case RID_IS_ABSTRACT:
8826 kind = CPTK_IS_ABSTRACT;
8827 break;
8828 case RID_IS_BASE_OF:
8829 kind = CPTK_IS_BASE_OF;
8830 binary = true;
8831 break;
8832 case RID_IS_CLASS:
8833 kind = CPTK_IS_CLASS;
8834 break;
8835 case RID_IS_EMPTY:
8836 kind = CPTK_IS_EMPTY;
8837 break;
8838 case RID_IS_ENUM:
8839 kind = CPTK_IS_ENUM;
8840 break;
8841 case RID_IS_FINAL:
8842 kind = CPTK_IS_FINAL;
8843 break;
8844 case RID_IS_LITERAL_TYPE:
8845 kind = CPTK_IS_LITERAL_TYPE;
8846 break;
8847 case RID_IS_POD:
8848 kind = CPTK_IS_POD;
8849 break;
8850 case RID_IS_POLYMORPHIC:
8851 kind = CPTK_IS_POLYMORPHIC;
8852 break;
8853 case RID_IS_STD_LAYOUT:
8854 kind = CPTK_IS_STD_LAYOUT;
8855 break;
8856 case RID_IS_TRIVIAL:
8857 kind = CPTK_IS_TRIVIAL;
8858 break;
8859 case RID_IS_TRIVIALLY_ASSIGNABLE:
8860 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8861 binary = true;
8862 break;
8863 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8864 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8865 variadic = true;
8866 break;
8867 case RID_IS_TRIVIALLY_COPYABLE:
8868 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8869 break;
8870 case RID_IS_UNION:
8871 kind = CPTK_IS_UNION;
8872 break;
8873 case RID_UNDERLYING_TYPE:
8874 kind = CPTK_UNDERLYING_TYPE;
8875 break;
8876 case RID_BASES:
8877 kind = CPTK_BASES;
8878 break;
8879 case RID_DIRECT_BASES:
8880 kind = CPTK_DIRECT_BASES;
8881 break;
8882 default:
8883 gcc_unreachable ();
8884 }
8885
8886 /* Consume the token. */
8887 cp_lexer_consume_token (parser->lexer);
8888
8889 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8890
8891 type1 = cp_parser_type_id (parser);
8892
8893 if (type1 == error_mark_node)
8894 return error_mark_node;
8895
8896 if (binary)
8897 {
8898 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8899
8900 type2 = cp_parser_type_id (parser);
8901
8902 if (type2 == error_mark_node)
8903 return error_mark_node;
8904 }
8905 else if (variadic)
8906 {
8907 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8908 {
8909 cp_lexer_consume_token (parser->lexer);
8910 tree elt = cp_parser_type_id (parser);
8911 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8912 {
8913 cp_lexer_consume_token (parser->lexer);
8914 elt = make_pack_expansion (elt);
8915 }
8916 if (elt == error_mark_node)
8917 return error_mark_node;
8918 type2 = tree_cons (NULL_TREE, elt, type2);
8919 }
8920 }
8921
8922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8923
8924 /* Complete the trait expression, which may mean either processing
8925 the trait expr now or saving it for template instantiation. */
8926 switch(kind)
8927 {
8928 case CPTK_UNDERLYING_TYPE:
8929 return finish_underlying_type (type1);
8930 case CPTK_BASES:
8931 return finish_bases (type1, false);
8932 case CPTK_DIRECT_BASES:
8933 return finish_bases (type1, true);
8934 default:
8935 return finish_trait_expr (kind, type1, type2);
8936 }
8937 }
8938
8939 /* Lambdas that appear in variable initializer or default argument scope
8940 get that in their mangling, so we need to record it. We might as well
8941 use the count for function and namespace scopes as well. */
8942 static GTY(()) tree lambda_scope;
8943 static GTY(()) int lambda_count;
8944 typedef struct GTY(()) tree_int
8945 {
8946 tree t;
8947 int i;
8948 } tree_int;
8949 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8950
8951 static void
8952 start_lambda_scope (tree decl)
8953 {
8954 tree_int ti;
8955 gcc_assert (decl);
8956 /* Once we're inside a function, we ignore other scopes and just push
8957 the function again so that popping works properly. */
8958 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8959 decl = current_function_decl;
8960 ti.t = lambda_scope;
8961 ti.i = lambda_count;
8962 vec_safe_push (lambda_scope_stack, ti);
8963 if (lambda_scope != decl)
8964 {
8965 /* Don't reset the count if we're still in the same function. */
8966 lambda_scope = decl;
8967 lambda_count = 0;
8968 }
8969 }
8970
8971 static void
8972 record_lambda_scope (tree lambda)
8973 {
8974 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8975 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8976 }
8977
8978 static void
8979 finish_lambda_scope (void)
8980 {
8981 tree_int *p = &lambda_scope_stack->last ();
8982 if (lambda_scope != p->t)
8983 {
8984 lambda_scope = p->t;
8985 lambda_count = p->i;
8986 }
8987 lambda_scope_stack->pop ();
8988 }
8989
8990 /* Parse a lambda expression.
8991
8992 lambda-expression:
8993 lambda-introducer lambda-declarator [opt] compound-statement
8994
8995 Returns a representation of the expression. */
8996
8997 static tree
8998 cp_parser_lambda_expression (cp_parser* parser)
8999 {
9000 tree lambda_expr = build_lambda_expr ();
9001 tree type;
9002 bool ok = true;
9003 cp_token *token = cp_lexer_peek_token (parser->lexer);
9004 cp_token_position start = 0;
9005
9006 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9007
9008 if (cp_unevaluated_operand)
9009 {
9010 if (!token->error_reported)
9011 {
9012 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9013 "lambda-expression in unevaluated context");
9014 token->error_reported = true;
9015 }
9016 ok = false;
9017 }
9018 else if (parser->in_template_argument_list_p)
9019 {
9020 if (!token->error_reported)
9021 {
9022 error_at (token->location, "lambda-expression in template-argument");
9023 token->error_reported = true;
9024 }
9025 ok = false;
9026 }
9027
9028 /* We may be in the middle of deferred access check. Disable
9029 it now. */
9030 push_deferring_access_checks (dk_no_deferred);
9031
9032 cp_parser_lambda_introducer (parser, lambda_expr);
9033
9034 type = begin_lambda_type (lambda_expr);
9035 if (type == error_mark_node)
9036 return error_mark_node;
9037
9038 record_lambda_scope (lambda_expr);
9039
9040 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9041 determine_visibility (TYPE_NAME (type));
9042
9043 /* Now that we've started the type, add the capture fields for any
9044 explicit captures. */
9045 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9046
9047 {
9048 /* Inside the class, surrounding template-parameter-lists do not apply. */
9049 unsigned int saved_num_template_parameter_lists
9050 = parser->num_template_parameter_lists;
9051 unsigned char in_statement = parser->in_statement;
9052 bool in_switch_statement_p = parser->in_switch_statement_p;
9053 bool fully_implicit_function_template_p
9054 = parser->fully_implicit_function_template_p;
9055 tree implicit_template_parms = parser->implicit_template_parms;
9056 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9057 bool auto_is_implicit_function_template_parm_p
9058 = parser->auto_is_implicit_function_template_parm_p;
9059
9060 parser->num_template_parameter_lists = 0;
9061 parser->in_statement = 0;
9062 parser->in_switch_statement_p = false;
9063 parser->fully_implicit_function_template_p = false;
9064 parser->implicit_template_parms = 0;
9065 parser->implicit_template_scope = 0;
9066 parser->auto_is_implicit_function_template_parm_p = false;
9067
9068 /* By virtue of defining a local class, a lambda expression has access to
9069 the private variables of enclosing classes. */
9070
9071 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9072
9073 if (ok)
9074 {
9075 if (!cp_parser_error_occurred (parser)
9076 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9077 && cp_parser_start_tentative_firewall (parser))
9078 start = token;
9079 cp_parser_lambda_body (parser, lambda_expr);
9080 }
9081 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9082 {
9083 if (cp_parser_skip_to_closing_brace (parser))
9084 cp_lexer_consume_token (parser->lexer);
9085 }
9086
9087 /* The capture list was built up in reverse order; fix that now. */
9088 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9089 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9090
9091 if (ok)
9092 maybe_add_lambda_conv_op (type);
9093
9094 type = finish_struct (type, /*attributes=*/NULL_TREE);
9095
9096 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9097 parser->in_statement = in_statement;
9098 parser->in_switch_statement_p = in_switch_statement_p;
9099 parser->fully_implicit_function_template_p
9100 = fully_implicit_function_template_p;
9101 parser->implicit_template_parms = implicit_template_parms;
9102 parser->implicit_template_scope = implicit_template_scope;
9103 parser->auto_is_implicit_function_template_parm_p
9104 = auto_is_implicit_function_template_parm_p;
9105 }
9106
9107 pop_deferring_access_checks ();
9108
9109 /* This field is only used during parsing of the lambda. */
9110 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9111
9112 /* This lambda shouldn't have any proxies left at this point. */
9113 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9114 /* And now that we're done, push proxies for an enclosing lambda. */
9115 insert_pending_capture_proxies ();
9116
9117 if (ok)
9118 lambda_expr = build_lambda_object (lambda_expr);
9119 else
9120 lambda_expr = error_mark_node;
9121
9122 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9123
9124 return lambda_expr;
9125 }
9126
9127 /* Parse the beginning of a lambda expression.
9128
9129 lambda-introducer:
9130 [ lambda-capture [opt] ]
9131
9132 LAMBDA_EXPR is the current representation of the lambda expression. */
9133
9134 static void
9135 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9136 {
9137 /* Need commas after the first capture. */
9138 bool first = true;
9139
9140 /* Eat the leading `['. */
9141 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9142
9143 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9144 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9145 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9146 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9147 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9148 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9149
9150 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9151 {
9152 cp_lexer_consume_token (parser->lexer);
9153 first = false;
9154 }
9155
9156 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9157 {
9158 cp_token* capture_token;
9159 tree capture_id;
9160 tree capture_init_expr;
9161 cp_id_kind idk = CP_ID_KIND_NONE;
9162 bool explicit_init_p = false;
9163
9164 enum capture_kind_type
9165 {
9166 BY_COPY,
9167 BY_REFERENCE
9168 };
9169 enum capture_kind_type capture_kind = BY_COPY;
9170
9171 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9172 {
9173 error ("expected end of capture-list");
9174 return;
9175 }
9176
9177 if (first)
9178 first = false;
9179 else
9180 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9181
9182 /* Possibly capture `this'. */
9183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9184 {
9185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9186 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9187 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9188 "with by-copy capture default");
9189 cp_lexer_consume_token (parser->lexer);
9190 add_capture (lambda_expr,
9191 /*id=*/this_identifier,
9192 /*initializer=*/finish_this_expr(),
9193 /*by_reference_p=*/false,
9194 explicit_init_p);
9195 continue;
9196 }
9197
9198 /* Remember whether we want to capture as a reference or not. */
9199 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9200 {
9201 capture_kind = BY_REFERENCE;
9202 cp_lexer_consume_token (parser->lexer);
9203 }
9204
9205 /* Get the identifier. */
9206 capture_token = cp_lexer_peek_token (parser->lexer);
9207 capture_id = cp_parser_identifier (parser);
9208
9209 if (capture_id == error_mark_node)
9210 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9211 delimiters, but I modified this to stop on unnested ']' as well. It
9212 was already changed to stop on unnested '}', so the
9213 "closing_parenthesis" name is no more misleading with my change. */
9214 {
9215 cp_parser_skip_to_closing_parenthesis (parser,
9216 /*recovering=*/true,
9217 /*or_comma=*/true,
9218 /*consume_paren=*/true);
9219 break;
9220 }
9221
9222 /* Find the initializer for this capture. */
9223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9224 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9225 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9226 {
9227 bool direct, non_constant;
9228 /* An explicit initializer exists. */
9229 if (cxx_dialect < cxx14)
9230 pedwarn (input_location, 0,
9231 "lambda capture initializers "
9232 "only available with -std=c++14 or -std=gnu++14");
9233 capture_init_expr = cp_parser_initializer (parser, &direct,
9234 &non_constant);
9235 explicit_init_p = true;
9236 if (capture_init_expr == NULL_TREE)
9237 {
9238 error ("empty initializer for lambda init-capture");
9239 capture_init_expr = error_mark_node;
9240 }
9241 }
9242 else
9243 {
9244 const char* error_msg;
9245
9246 /* Turn the identifier into an id-expression. */
9247 capture_init_expr
9248 = cp_parser_lookup_name_simple (parser, capture_id,
9249 capture_token->location);
9250
9251 if (capture_init_expr == error_mark_node)
9252 {
9253 unqualified_name_lookup_error (capture_id);
9254 continue;
9255 }
9256 else if (DECL_P (capture_init_expr)
9257 && (!VAR_P (capture_init_expr)
9258 && TREE_CODE (capture_init_expr) != PARM_DECL))
9259 {
9260 error_at (capture_token->location,
9261 "capture of non-variable %qD ",
9262 capture_init_expr);
9263 inform (0, "%q+#D declared here", capture_init_expr);
9264 continue;
9265 }
9266 if (VAR_P (capture_init_expr)
9267 && decl_storage_duration (capture_init_expr) != dk_auto)
9268 {
9269 if (pedwarn (capture_token->location, 0, "capture of variable "
9270 "%qD with non-automatic storage duration",
9271 capture_init_expr))
9272 inform (0, "%q+#D declared here", capture_init_expr);
9273 continue;
9274 }
9275
9276 capture_init_expr
9277 = finish_id_expression
9278 (capture_id,
9279 capture_init_expr,
9280 parser->scope,
9281 &idk,
9282 /*integral_constant_expression_p=*/false,
9283 /*allow_non_integral_constant_expression_p=*/false,
9284 /*non_integral_constant_expression_p=*/NULL,
9285 /*template_p=*/false,
9286 /*done=*/true,
9287 /*address_p=*/false,
9288 /*template_arg_p=*/false,
9289 &error_msg,
9290 capture_token->location);
9291
9292 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9293 {
9294 cp_lexer_consume_token (parser->lexer);
9295 capture_init_expr = make_pack_expansion (capture_init_expr);
9296 }
9297 else
9298 check_for_bare_parameter_packs (capture_init_expr);
9299 }
9300
9301 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9302 && !explicit_init_p)
9303 {
9304 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9305 && capture_kind == BY_COPY)
9306 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9307 "of %qD redundant with by-copy capture default",
9308 capture_id);
9309 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9310 && capture_kind == BY_REFERENCE)
9311 pedwarn (capture_token->location, 0, "explicit by-reference "
9312 "capture of %qD redundant with by-reference capture "
9313 "default", capture_id);
9314 }
9315
9316 add_capture (lambda_expr,
9317 capture_id,
9318 capture_init_expr,
9319 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9320 explicit_init_p);
9321 }
9322
9323 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9324 }
9325
9326 /* Parse the (optional) middle of a lambda expression.
9327
9328 lambda-declarator:
9329 < template-parameter-list [opt] >
9330 ( parameter-declaration-clause [opt] )
9331 attribute-specifier [opt]
9332 mutable [opt]
9333 exception-specification [opt]
9334 lambda-return-type-clause [opt]
9335
9336 LAMBDA_EXPR is the current representation of the lambda expression. */
9337
9338 static bool
9339 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9340 {
9341 /* 5.1.1.4 of the standard says:
9342 If a lambda-expression does not include a lambda-declarator, it is as if
9343 the lambda-declarator were ().
9344 This means an empty parameter list, no attributes, and no exception
9345 specification. */
9346 tree param_list = void_list_node;
9347 tree attributes = NULL_TREE;
9348 tree exception_spec = NULL_TREE;
9349 tree template_param_list = NULL_TREE;
9350
9351 /* The template-parameter-list is optional, but must begin with
9352 an opening angle if present. */
9353 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9354 {
9355 if (cxx_dialect < cxx14)
9356 pedwarn (parser->lexer->next_token->location, 0,
9357 "lambda templates are only available with "
9358 "-std=c++14 or -std=gnu++14");
9359
9360 cp_lexer_consume_token (parser->lexer);
9361
9362 template_param_list = cp_parser_template_parameter_list (parser);
9363
9364 cp_parser_skip_to_end_of_template_parameter_list (parser);
9365
9366 /* We just processed one more parameter list. */
9367 ++parser->num_template_parameter_lists;
9368 }
9369
9370 /* The parameter-declaration-clause is optional (unless
9371 template-parameter-list was given), but must begin with an
9372 opening parenthesis if present. */
9373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9374 {
9375 cp_lexer_consume_token (parser->lexer);
9376
9377 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9378
9379 /* Parse parameters. */
9380 param_list = cp_parser_parameter_declaration_clause (parser);
9381
9382 /* Default arguments shall not be specified in the
9383 parameter-declaration-clause of a lambda-declarator. */
9384 for (tree t = param_list; t; t = TREE_CHAIN (t))
9385 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9386 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9387 "default argument specified for lambda parameter");
9388
9389 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9390
9391 attributes = cp_parser_attributes_opt (parser);
9392
9393 /* Parse optional `mutable' keyword. */
9394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9395 {
9396 cp_lexer_consume_token (parser->lexer);
9397 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9398 }
9399
9400 /* Parse optional exception specification. */
9401 exception_spec = cp_parser_exception_specification_opt (parser);
9402
9403 /* Parse optional trailing return type. */
9404 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9405 {
9406 cp_lexer_consume_token (parser->lexer);
9407 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9408 = cp_parser_trailing_type_id (parser);
9409 }
9410
9411 /* The function parameters must be in scope all the way until after the
9412 trailing-return-type in case of decltype. */
9413 pop_bindings_and_leave_scope ();
9414 }
9415 else if (template_param_list != NULL_TREE) // generate diagnostic
9416 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9417
9418 /* Create the function call operator.
9419
9420 Messing with declarators like this is no uglier than building up the
9421 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9422 other code. */
9423 {
9424 cp_decl_specifier_seq return_type_specs;
9425 cp_declarator* declarator;
9426 tree fco;
9427 int quals;
9428 void *p;
9429
9430 clear_decl_specs (&return_type_specs);
9431 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9432 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9433 else
9434 /* Maybe we will deduce the return type later. */
9435 return_type_specs.type = make_auto ();
9436
9437 p = obstack_alloc (&declarator_obstack, 0);
9438
9439 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9440 sfk_none);
9441
9442 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9443 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9444 declarator = make_call_declarator (declarator, param_list, quals,
9445 VIRT_SPEC_UNSPECIFIED,
9446 REF_QUAL_NONE,
9447 exception_spec,
9448 /*late_return_type=*/NULL_TREE);
9449 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9450
9451 fco = grokmethod (&return_type_specs,
9452 declarator,
9453 attributes);
9454 if (fco != error_mark_node)
9455 {
9456 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9457 DECL_ARTIFICIAL (fco) = 1;
9458 /* Give the object parameter a different name. */
9459 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9460 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9461 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9462 }
9463 if (template_param_list)
9464 {
9465 fco = finish_member_template_decl (fco);
9466 finish_template_decl (template_param_list);
9467 --parser->num_template_parameter_lists;
9468 }
9469 else if (parser->fully_implicit_function_template_p)
9470 fco = finish_fully_implicit_template (parser, fco);
9471
9472 finish_member_declaration (fco);
9473
9474 obstack_free (&declarator_obstack, p);
9475
9476 return (fco != error_mark_node);
9477 }
9478 }
9479
9480 /* Parse the body of a lambda expression, which is simply
9481
9482 compound-statement
9483
9484 but which requires special handling.
9485 LAMBDA_EXPR is the current representation of the lambda expression. */
9486
9487 static void
9488 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9489 {
9490 bool nested = (current_function_decl != NULL_TREE);
9491 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9492 if (nested)
9493 push_function_context ();
9494 else
9495 /* Still increment function_depth so that we don't GC in the
9496 middle of an expression. */
9497 ++function_depth;
9498 /* Clear this in case we're in the middle of a default argument. */
9499 parser->local_variables_forbidden_p = false;
9500
9501 /* Finish the function call operator
9502 - class_specifier
9503 + late_parsing_for_member
9504 + function_definition_after_declarator
9505 + ctor_initializer_opt_and_function_body */
9506 {
9507 tree fco = lambda_function (lambda_expr);
9508 tree body;
9509 bool done = false;
9510 tree compound_stmt;
9511 tree cap;
9512
9513 /* Let the front end know that we are going to be defining this
9514 function. */
9515 start_preparsed_function (fco,
9516 NULL_TREE,
9517 SF_PRE_PARSED | SF_INCLASS_INLINE);
9518
9519 start_lambda_scope (fco);
9520 body = begin_function_body ();
9521
9522 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9523 goto out;
9524
9525 /* Push the proxies for any explicit captures. */
9526 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9527 cap = TREE_CHAIN (cap))
9528 build_capture_proxy (TREE_PURPOSE (cap));
9529
9530 compound_stmt = begin_compound_stmt (0);
9531
9532 /* 5.1.1.4 of the standard says:
9533 If a lambda-expression does not include a trailing-return-type, it
9534 is as if the trailing-return-type denotes the following type:
9535 * if the compound-statement is of the form
9536 { return attribute-specifier [opt] expression ; }
9537 the type of the returned expression after lvalue-to-rvalue
9538 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9539 (_conv.array_ 4.2), and function-to-pointer conversion
9540 (_conv.func_ 4.3);
9541 * otherwise, void. */
9542
9543 /* In a lambda that has neither a lambda-return-type-clause
9544 nor a deducible form, errors should be reported for return statements
9545 in the body. Since we used void as the placeholder return type, parsing
9546 the body as usual will give such desired behavior. */
9547 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9548 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9549 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9550 {
9551 tree expr = NULL_TREE;
9552 cp_id_kind idk = CP_ID_KIND_NONE;
9553
9554 /* Parse tentatively in case there's more after the initial return
9555 statement. */
9556 cp_parser_parse_tentatively (parser);
9557
9558 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9559
9560 expr = cp_parser_expression (parser, &idk);
9561
9562 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9563 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9564
9565 if (cp_parser_parse_definitely (parser))
9566 {
9567 if (!processing_template_decl)
9568 apply_deduced_return_type (fco, lambda_return_type (expr));
9569
9570 /* Will get error here if type not deduced yet. */
9571 finish_return_stmt (expr);
9572
9573 done = true;
9574 }
9575 }
9576
9577 if (!done)
9578 {
9579 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9580 cp_parser_label_declaration (parser);
9581 cp_parser_statement_seq_opt (parser, NULL_TREE);
9582 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9583 }
9584
9585 finish_compound_stmt (compound_stmt);
9586
9587 out:
9588 finish_function_body (body);
9589 finish_lambda_scope ();
9590
9591 /* Finish the function and generate code for it if necessary. */
9592 tree fn = finish_function (/*inline*/2);
9593
9594 /* Only expand if the call op is not a template. */
9595 if (!DECL_TEMPLATE_INFO (fco))
9596 expand_or_defer_fn (fn);
9597 }
9598
9599 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9600 if (nested)
9601 pop_function_context();
9602 else
9603 --function_depth;
9604 }
9605
9606 /* Statements [gram.stmt.stmt] */
9607
9608 /* Parse a statement.
9609
9610 statement:
9611 labeled-statement
9612 expression-statement
9613 compound-statement
9614 selection-statement
9615 iteration-statement
9616 jump-statement
9617 declaration-statement
9618 try-block
9619
9620 C++11:
9621
9622 statement:
9623 labeled-statement
9624 attribute-specifier-seq (opt) expression-statement
9625 attribute-specifier-seq (opt) compound-statement
9626 attribute-specifier-seq (opt) selection-statement
9627 attribute-specifier-seq (opt) iteration-statement
9628 attribute-specifier-seq (opt) jump-statement
9629 declaration-statement
9630 attribute-specifier-seq (opt) try-block
9631
9632 TM Extension:
9633
9634 statement:
9635 atomic-statement
9636
9637 IN_COMPOUND is true when the statement is nested inside a
9638 cp_parser_compound_statement; this matters for certain pragmas.
9639
9640 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9641 is a (possibly labeled) if statement which is not enclosed in braces
9642 and has an else clause. This is used to implement -Wparentheses. */
9643
9644 static void
9645 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9646 bool in_compound, bool *if_p)
9647 {
9648 tree statement, std_attrs = NULL_TREE;
9649 cp_token *token;
9650 location_t statement_location, attrs_location;
9651
9652 restart:
9653 if (if_p != NULL)
9654 *if_p = false;
9655 /* There is no statement yet. */
9656 statement = NULL_TREE;
9657
9658 saved_token_sentinel saved_tokens (parser->lexer);
9659 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9660 if (c_dialect_objc ())
9661 /* In obj-c++, seeing '[[' might be the either the beginning of
9662 c++11 attributes, or a nested objc-message-expression. So
9663 let's parse the c++11 attributes tentatively. */
9664 cp_parser_parse_tentatively (parser);
9665 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9666 if (c_dialect_objc ())
9667 {
9668 if (!cp_parser_parse_definitely (parser))
9669 std_attrs = NULL_TREE;
9670 }
9671
9672 /* Peek at the next token. */
9673 token = cp_lexer_peek_token (parser->lexer);
9674 /* Remember the location of the first token in the statement. */
9675 statement_location = token->location;
9676 /* If this is a keyword, then that will often determine what kind of
9677 statement we have. */
9678 if (token->type == CPP_KEYWORD)
9679 {
9680 enum rid keyword = token->keyword;
9681
9682 switch (keyword)
9683 {
9684 case RID_CASE:
9685 case RID_DEFAULT:
9686 /* Looks like a labeled-statement with a case label.
9687 Parse the label, and then use tail recursion to parse
9688 the statement. */
9689 cp_parser_label_for_labeled_statement (parser, std_attrs);
9690 goto restart;
9691
9692 case RID_IF:
9693 case RID_SWITCH:
9694 statement = cp_parser_selection_statement (parser, if_p);
9695 break;
9696
9697 case RID_WHILE:
9698 case RID_DO:
9699 case RID_FOR:
9700 statement = cp_parser_iteration_statement (parser, false);
9701 break;
9702
9703 case RID_CILK_FOR:
9704 if (!flag_cilkplus)
9705 {
9706 error_at (cp_lexer_peek_token (parser->lexer)->location,
9707 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9708 cp_lexer_consume_token (parser->lexer);
9709 statement = error_mark_node;
9710 }
9711 else
9712 statement = cp_parser_cilk_for (parser, integer_zero_node);
9713 break;
9714
9715 case RID_BREAK:
9716 case RID_CONTINUE:
9717 case RID_RETURN:
9718 case RID_GOTO:
9719 statement = cp_parser_jump_statement (parser);
9720 break;
9721
9722 case RID_CILK_SYNC:
9723 cp_lexer_consume_token (parser->lexer);
9724 if (flag_cilkplus)
9725 {
9726 tree sync_expr = build_cilk_sync ();
9727 SET_EXPR_LOCATION (sync_expr,
9728 token->location);
9729 statement = finish_expr_stmt (sync_expr);
9730 }
9731 else
9732 {
9733 error_at (token->location, "-fcilkplus must be enabled to use"
9734 " %<_Cilk_sync%>");
9735 statement = error_mark_node;
9736 }
9737 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9738 break;
9739
9740 /* Objective-C++ exception-handling constructs. */
9741 case RID_AT_TRY:
9742 case RID_AT_CATCH:
9743 case RID_AT_FINALLY:
9744 case RID_AT_SYNCHRONIZED:
9745 case RID_AT_THROW:
9746 statement = cp_parser_objc_statement (parser);
9747 break;
9748
9749 case RID_TRY:
9750 statement = cp_parser_try_block (parser);
9751 break;
9752
9753 case RID_NAMESPACE:
9754 /* This must be a namespace alias definition. */
9755 cp_parser_declaration_statement (parser);
9756 return;
9757
9758 case RID_TRANSACTION_ATOMIC:
9759 case RID_TRANSACTION_RELAXED:
9760 statement = cp_parser_transaction (parser, keyword);
9761 break;
9762 case RID_TRANSACTION_CANCEL:
9763 statement = cp_parser_transaction_cancel (parser);
9764 break;
9765
9766 default:
9767 /* It might be a keyword like `int' that can start a
9768 declaration-statement. */
9769 break;
9770 }
9771 }
9772 else if (token->type == CPP_NAME)
9773 {
9774 /* If the next token is a `:', then we are looking at a
9775 labeled-statement. */
9776 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9777 if (token->type == CPP_COLON)
9778 {
9779 /* Looks like a labeled-statement with an ordinary label.
9780 Parse the label, and then use tail recursion to parse
9781 the statement. */
9782
9783 cp_parser_label_for_labeled_statement (parser, std_attrs);
9784 goto restart;
9785 }
9786 }
9787 /* Anything that starts with a `{' must be a compound-statement. */
9788 else if (token->type == CPP_OPEN_BRACE)
9789 statement = cp_parser_compound_statement (parser, NULL, false, false);
9790 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9791 a statement all its own. */
9792 else if (token->type == CPP_PRAGMA)
9793 {
9794 /* Only certain OpenMP pragmas are attached to statements, and thus
9795 are considered statements themselves. All others are not. In
9796 the context of a compound, accept the pragma as a "statement" and
9797 return so that we can check for a close brace. Otherwise we
9798 require a real statement and must go back and read one. */
9799 if (in_compound)
9800 cp_parser_pragma (parser, pragma_compound);
9801 else if (!cp_parser_pragma (parser, pragma_stmt))
9802 goto restart;
9803 return;
9804 }
9805 else if (token->type == CPP_EOF)
9806 {
9807 cp_parser_error (parser, "expected statement");
9808 return;
9809 }
9810
9811 /* Everything else must be a declaration-statement or an
9812 expression-statement. Try for the declaration-statement
9813 first, unless we are looking at a `;', in which case we know that
9814 we have an expression-statement. */
9815 if (!statement)
9816 {
9817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9818 {
9819 if (std_attrs != NULL_TREE)
9820 {
9821 /* Attributes should be parsed as part of the the
9822 declaration, so let's un-parse them. */
9823 saved_tokens.rollback();
9824 std_attrs = NULL_TREE;
9825 }
9826
9827 cp_parser_parse_tentatively (parser);
9828 /* Try to parse the declaration-statement. */
9829 cp_parser_declaration_statement (parser);
9830 /* If that worked, we're done. */
9831 if (cp_parser_parse_definitely (parser))
9832 return;
9833 }
9834 /* Look for an expression-statement instead. */
9835 statement = cp_parser_expression_statement (parser, in_statement_expr);
9836 }
9837
9838 /* Set the line number for the statement. */
9839 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9840 SET_EXPR_LOCATION (statement, statement_location);
9841
9842 /* Note that for now, we don't do anything with c++11 statements
9843 parsed at this level. */
9844 if (std_attrs != NULL_TREE)
9845 warning_at (attrs_location,
9846 OPT_Wattributes,
9847 "attributes at the beginning of statement are ignored");
9848 }
9849
9850 /* Parse the label for a labeled-statement, i.e.
9851
9852 identifier :
9853 case constant-expression :
9854 default :
9855
9856 GNU Extension:
9857 case constant-expression ... constant-expression : statement
9858
9859 When a label is parsed without errors, the label is added to the
9860 parse tree by the finish_* functions, so this function doesn't
9861 have to return the label. */
9862
9863 static void
9864 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9865 {
9866 cp_token *token;
9867 tree label = NULL_TREE;
9868 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9869
9870 /* The next token should be an identifier. */
9871 token = cp_lexer_peek_token (parser->lexer);
9872 if (token->type != CPP_NAME
9873 && token->type != CPP_KEYWORD)
9874 {
9875 cp_parser_error (parser, "expected labeled-statement");
9876 return;
9877 }
9878
9879 parser->colon_corrects_to_scope_p = false;
9880 switch (token->keyword)
9881 {
9882 case RID_CASE:
9883 {
9884 tree expr, expr_hi;
9885 cp_token *ellipsis;
9886
9887 /* Consume the `case' token. */
9888 cp_lexer_consume_token (parser->lexer);
9889 /* Parse the constant-expression. */
9890 expr = cp_parser_constant_expression (parser);
9891 if (check_for_bare_parameter_packs (expr))
9892 expr = error_mark_node;
9893
9894 ellipsis = cp_lexer_peek_token (parser->lexer);
9895 if (ellipsis->type == CPP_ELLIPSIS)
9896 {
9897 /* Consume the `...' token. */
9898 cp_lexer_consume_token (parser->lexer);
9899 expr_hi = cp_parser_constant_expression (parser);
9900 if (check_for_bare_parameter_packs (expr_hi))
9901 expr_hi = error_mark_node;
9902
9903 /* We don't need to emit warnings here, as the common code
9904 will do this for us. */
9905 }
9906 else
9907 expr_hi = NULL_TREE;
9908
9909 if (parser->in_switch_statement_p)
9910 finish_case_label (token->location, expr, expr_hi);
9911 else
9912 error_at (token->location,
9913 "case label %qE not within a switch statement",
9914 expr);
9915 }
9916 break;
9917
9918 case RID_DEFAULT:
9919 /* Consume the `default' token. */
9920 cp_lexer_consume_token (parser->lexer);
9921
9922 if (parser->in_switch_statement_p)
9923 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9924 else
9925 error_at (token->location, "case label not within a switch statement");
9926 break;
9927
9928 default:
9929 /* Anything else must be an ordinary label. */
9930 label = finish_label_stmt (cp_parser_identifier (parser));
9931 break;
9932 }
9933
9934 /* Require the `:' token. */
9935 cp_parser_require (parser, CPP_COLON, RT_COLON);
9936
9937 /* An ordinary label may optionally be followed by attributes.
9938 However, this is only permitted if the attributes are then
9939 followed by a semicolon. This is because, for backward
9940 compatibility, when parsing
9941 lab: __attribute__ ((unused)) int i;
9942 we want the attribute to attach to "i", not "lab". */
9943 if (label != NULL_TREE
9944 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9945 {
9946 tree attrs;
9947 cp_parser_parse_tentatively (parser);
9948 attrs = cp_parser_gnu_attributes_opt (parser);
9949 if (attrs == NULL_TREE
9950 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9951 cp_parser_abort_tentative_parse (parser);
9952 else if (!cp_parser_parse_definitely (parser))
9953 ;
9954 else
9955 attributes = chainon (attributes, attrs);
9956 }
9957
9958 if (attributes != NULL_TREE)
9959 cplus_decl_attributes (&label, attributes, 0);
9960
9961 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9962 }
9963
9964 /* Parse an expression-statement.
9965
9966 expression-statement:
9967 expression [opt] ;
9968
9969 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9970 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9971 indicates whether this expression-statement is part of an
9972 expression statement. */
9973
9974 static tree
9975 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9976 {
9977 tree statement = NULL_TREE;
9978 cp_token *token = cp_lexer_peek_token (parser->lexer);
9979
9980 /* If the next token is a ';', then there is no expression
9981 statement. */
9982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9983 {
9984 statement = cp_parser_expression (parser);
9985 if (statement == error_mark_node
9986 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9987 {
9988 cp_parser_skip_to_end_of_block_or_statement (parser);
9989 return error_mark_node;
9990 }
9991 }
9992
9993 /* Give a helpful message for "A<T>::type t;" and the like. */
9994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9995 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9996 {
9997 if (TREE_CODE (statement) == SCOPE_REF)
9998 error_at (token->location, "need %<typename%> before %qE because "
9999 "%qT is a dependent scope",
10000 statement, TREE_OPERAND (statement, 0));
10001 else if (is_overloaded_fn (statement)
10002 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10003 {
10004 /* A::A a; */
10005 tree fn = get_first_fn (statement);
10006 error_at (token->location,
10007 "%<%T::%D%> names the constructor, not the type",
10008 DECL_CONTEXT (fn), DECL_NAME (fn));
10009 }
10010 }
10011
10012 /* Consume the final `;'. */
10013 cp_parser_consume_semicolon_at_end_of_statement (parser);
10014
10015 if (in_statement_expr
10016 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10017 /* This is the final expression statement of a statement
10018 expression. */
10019 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10020 else if (statement)
10021 statement = finish_expr_stmt (statement);
10022
10023 return statement;
10024 }
10025
10026 /* Parse a compound-statement.
10027
10028 compound-statement:
10029 { statement-seq [opt] }
10030
10031 GNU extension:
10032
10033 compound-statement:
10034 { label-declaration-seq [opt] statement-seq [opt] }
10035
10036 label-declaration-seq:
10037 label-declaration
10038 label-declaration-seq label-declaration
10039
10040 Returns a tree representing the statement. */
10041
10042 static tree
10043 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10044 bool in_try, bool function_body)
10045 {
10046 tree compound_stmt;
10047
10048 /* Consume the `{'. */
10049 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10050 return error_mark_node;
10051 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10052 && !function_body && cxx_dialect < cxx14)
10053 pedwarn (input_location, OPT_Wpedantic,
10054 "compound-statement in constexpr function");
10055 /* Begin the compound-statement. */
10056 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10057 /* If the next keyword is `__label__' we have a label declaration. */
10058 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10059 cp_parser_label_declaration (parser);
10060 /* Parse an (optional) statement-seq. */
10061 cp_parser_statement_seq_opt (parser, in_statement_expr);
10062 /* Finish the compound-statement. */
10063 finish_compound_stmt (compound_stmt);
10064 /* Consume the `}'. */
10065 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10066
10067 return compound_stmt;
10068 }
10069
10070 /* Parse an (optional) statement-seq.
10071
10072 statement-seq:
10073 statement
10074 statement-seq [opt] statement */
10075
10076 static void
10077 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10078 {
10079 /* Scan statements until there aren't any more. */
10080 while (true)
10081 {
10082 cp_token *token = cp_lexer_peek_token (parser->lexer);
10083
10084 /* If we are looking at a `}', then we have run out of
10085 statements; the same is true if we have reached the end
10086 of file, or have stumbled upon a stray '@end'. */
10087 if (token->type == CPP_CLOSE_BRACE
10088 || token->type == CPP_EOF
10089 || token->type == CPP_PRAGMA_EOL
10090 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10091 break;
10092
10093 /* If we are in a compound statement and find 'else' then
10094 something went wrong. */
10095 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10096 {
10097 if (parser->in_statement & IN_IF_STMT)
10098 break;
10099 else
10100 {
10101 token = cp_lexer_consume_token (parser->lexer);
10102 error_at (token->location, "%<else%> without a previous %<if%>");
10103 }
10104 }
10105
10106 /* Parse the statement. */
10107 cp_parser_statement (parser, in_statement_expr, true, NULL);
10108 }
10109 }
10110
10111 /* Parse a selection-statement.
10112
10113 selection-statement:
10114 if ( condition ) statement
10115 if ( condition ) statement else statement
10116 switch ( condition ) statement
10117
10118 Returns the new IF_STMT or SWITCH_STMT.
10119
10120 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10121 is a (possibly labeled) if statement which is not enclosed in
10122 braces and has an else clause. This is used to implement
10123 -Wparentheses. */
10124
10125 static tree
10126 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10127 {
10128 cp_token *token;
10129 enum rid keyword;
10130
10131 if (if_p != NULL)
10132 *if_p = false;
10133
10134 /* Peek at the next token. */
10135 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10136
10137 /* See what kind of keyword it is. */
10138 keyword = token->keyword;
10139 switch (keyword)
10140 {
10141 case RID_IF:
10142 case RID_SWITCH:
10143 {
10144 tree statement;
10145 tree condition;
10146
10147 /* Look for the `('. */
10148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10149 {
10150 cp_parser_skip_to_end_of_statement (parser);
10151 return error_mark_node;
10152 }
10153
10154 /* Begin the selection-statement. */
10155 if (keyword == RID_IF)
10156 statement = begin_if_stmt ();
10157 else
10158 statement = begin_switch_stmt ();
10159
10160 /* Parse the condition. */
10161 condition = cp_parser_condition (parser);
10162 /* Look for the `)'. */
10163 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10164 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10165 /*consume_paren=*/true);
10166
10167 if (keyword == RID_IF)
10168 {
10169 bool nested_if;
10170 unsigned char in_statement;
10171
10172 /* Add the condition. */
10173 finish_if_stmt_cond (condition, statement);
10174
10175 /* Parse the then-clause. */
10176 in_statement = parser->in_statement;
10177 parser->in_statement |= IN_IF_STMT;
10178 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10179 {
10180 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10181 add_stmt (build_empty_stmt (loc));
10182 cp_lexer_consume_token (parser->lexer);
10183 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10184 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10185 "empty body in an %<if%> statement");
10186 nested_if = false;
10187 }
10188 else
10189 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10190 parser->in_statement = in_statement;
10191
10192 finish_then_clause (statement);
10193
10194 /* If the next token is `else', parse the else-clause. */
10195 if (cp_lexer_next_token_is_keyword (parser->lexer,
10196 RID_ELSE))
10197 {
10198 /* Consume the `else' keyword. */
10199 cp_lexer_consume_token (parser->lexer);
10200 begin_else_clause (statement);
10201 /* Parse the else-clause. */
10202 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10203 {
10204 location_t loc;
10205 loc = cp_lexer_peek_token (parser->lexer)->location;
10206 warning_at (loc,
10207 OPT_Wempty_body, "suggest braces around "
10208 "empty body in an %<else%> statement");
10209 add_stmt (build_empty_stmt (loc));
10210 cp_lexer_consume_token (parser->lexer);
10211 }
10212 else
10213 cp_parser_implicitly_scoped_statement (parser, NULL);
10214
10215 finish_else_clause (statement);
10216
10217 /* If we are currently parsing a then-clause, then
10218 IF_P will not be NULL. We set it to true to
10219 indicate that this if statement has an else clause.
10220 This may trigger the Wparentheses warning below
10221 when we get back up to the parent if statement. */
10222 if (if_p != NULL)
10223 *if_p = true;
10224 }
10225 else
10226 {
10227 /* This if statement does not have an else clause. If
10228 NESTED_IF is true, then the then-clause is an if
10229 statement which does have an else clause. We warn
10230 about the potential ambiguity. */
10231 if (nested_if)
10232 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10233 "suggest explicit braces to avoid ambiguous"
10234 " %<else%>");
10235 }
10236
10237 /* Now we're all done with the if-statement. */
10238 finish_if_stmt (statement);
10239 }
10240 else
10241 {
10242 bool in_switch_statement_p;
10243 unsigned char in_statement;
10244
10245 /* Add the condition. */
10246 finish_switch_cond (condition, statement);
10247
10248 /* Parse the body of the switch-statement. */
10249 in_switch_statement_p = parser->in_switch_statement_p;
10250 in_statement = parser->in_statement;
10251 parser->in_switch_statement_p = true;
10252 parser->in_statement |= IN_SWITCH_STMT;
10253 cp_parser_implicitly_scoped_statement (parser, NULL);
10254 parser->in_switch_statement_p = in_switch_statement_p;
10255 parser->in_statement = in_statement;
10256
10257 /* Now we're all done with the switch-statement. */
10258 finish_switch_stmt (statement);
10259 }
10260
10261 return statement;
10262 }
10263 break;
10264
10265 default:
10266 cp_parser_error (parser, "expected selection-statement");
10267 return error_mark_node;
10268 }
10269 }
10270
10271 /* Parse a condition.
10272
10273 condition:
10274 expression
10275 type-specifier-seq declarator = initializer-clause
10276 type-specifier-seq declarator braced-init-list
10277
10278 GNU Extension:
10279
10280 condition:
10281 type-specifier-seq declarator asm-specification [opt]
10282 attributes [opt] = assignment-expression
10283
10284 Returns the expression that should be tested. */
10285
10286 static tree
10287 cp_parser_condition (cp_parser* parser)
10288 {
10289 cp_decl_specifier_seq type_specifiers;
10290 const char *saved_message;
10291 int declares_class_or_enum;
10292
10293 /* Try the declaration first. */
10294 cp_parser_parse_tentatively (parser);
10295 /* New types are not allowed in the type-specifier-seq for a
10296 condition. */
10297 saved_message = parser->type_definition_forbidden_message;
10298 parser->type_definition_forbidden_message
10299 = G_("types may not be defined in conditions");
10300 /* Parse the type-specifier-seq. */
10301 cp_parser_decl_specifier_seq (parser,
10302 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10303 &type_specifiers,
10304 &declares_class_or_enum);
10305 /* Restore the saved message. */
10306 parser->type_definition_forbidden_message = saved_message;
10307 /* If all is well, we might be looking at a declaration. */
10308 if (!cp_parser_error_occurred (parser))
10309 {
10310 tree decl;
10311 tree asm_specification;
10312 tree attributes;
10313 cp_declarator *declarator;
10314 tree initializer = NULL_TREE;
10315
10316 /* Parse the declarator. */
10317 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10318 /*ctor_dtor_or_conv_p=*/NULL,
10319 /*parenthesized_p=*/NULL,
10320 /*member_p=*/false,
10321 /*friend_p=*/false);
10322 /* Parse the attributes. */
10323 attributes = cp_parser_attributes_opt (parser);
10324 /* Parse the asm-specification. */
10325 asm_specification = cp_parser_asm_specification_opt (parser);
10326 /* If the next token is not an `=' or '{', then we might still be
10327 looking at an expression. For example:
10328
10329 if (A(a).x)
10330
10331 looks like a decl-specifier-seq and a declarator -- but then
10332 there is no `=', so this is an expression. */
10333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10334 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10335 cp_parser_simulate_error (parser);
10336
10337 /* If we did see an `=' or '{', then we are looking at a declaration
10338 for sure. */
10339 if (cp_parser_parse_definitely (parser))
10340 {
10341 tree pushed_scope;
10342 bool non_constant_p;
10343 bool flags = LOOKUP_ONLYCONVERTING;
10344
10345 /* Create the declaration. */
10346 decl = start_decl (declarator, &type_specifiers,
10347 /*initialized_p=*/true,
10348 attributes, /*prefix_attributes=*/NULL_TREE,
10349 &pushed_scope);
10350
10351 /* Parse the initializer. */
10352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10353 {
10354 initializer = cp_parser_braced_list (parser, &non_constant_p);
10355 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10356 flags = 0;
10357 }
10358 else
10359 {
10360 /* Consume the `='. */
10361 cp_parser_require (parser, CPP_EQ, RT_EQ);
10362 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10363 }
10364 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10366
10367 /* Process the initializer. */
10368 cp_finish_decl (decl,
10369 initializer, !non_constant_p,
10370 asm_specification,
10371 flags);
10372
10373 if (pushed_scope)
10374 pop_scope (pushed_scope);
10375
10376 return convert_from_reference (decl);
10377 }
10378 }
10379 /* If we didn't even get past the declarator successfully, we are
10380 definitely not looking at a declaration. */
10381 else
10382 cp_parser_abort_tentative_parse (parser);
10383
10384 /* Otherwise, we are looking at an expression. */
10385 return cp_parser_expression (parser);
10386 }
10387
10388 /* Parses a for-statement or range-for-statement until the closing ')',
10389 not included. */
10390
10391 static tree
10392 cp_parser_for (cp_parser *parser, bool ivdep)
10393 {
10394 tree init, scope, decl;
10395 bool is_range_for;
10396
10397 /* Begin the for-statement. */
10398 scope = begin_for_scope (&init);
10399
10400 /* Parse the initialization. */
10401 is_range_for = cp_parser_for_init_statement (parser, &decl);
10402
10403 if (is_range_for)
10404 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10405 else
10406 return cp_parser_c_for (parser, scope, init, ivdep);
10407 }
10408
10409 static tree
10410 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10411 {
10412 /* Normal for loop */
10413 tree condition = NULL_TREE;
10414 tree expression = NULL_TREE;
10415 tree stmt;
10416
10417 stmt = begin_for_stmt (scope, init);
10418 /* The for-init-statement has already been parsed in
10419 cp_parser_for_init_statement, so no work is needed here. */
10420 finish_for_init_stmt (stmt);
10421
10422 /* If there's a condition, process it. */
10423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10424 condition = cp_parser_condition (parser);
10425 else if (ivdep)
10426 {
10427 cp_parser_error (parser, "missing loop condition in loop with "
10428 "%<GCC ivdep%> pragma");
10429 condition = error_mark_node;
10430 }
10431 finish_for_cond (condition, stmt, ivdep);
10432 /* Look for the `;'. */
10433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10434
10435 /* If there's an expression, process it. */
10436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10437 expression = cp_parser_expression (parser);
10438 finish_for_expr (expression, stmt);
10439
10440 return stmt;
10441 }
10442
10443 /* Tries to parse a range-based for-statement:
10444
10445 range-based-for:
10446 decl-specifier-seq declarator : expression
10447
10448 The decl-specifier-seq declarator and the `:' are already parsed by
10449 cp_parser_for_init_statement. If processing_template_decl it returns a
10450 newly created RANGE_FOR_STMT; if not, it is converted to a
10451 regular FOR_STMT. */
10452
10453 static tree
10454 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10455 bool ivdep)
10456 {
10457 tree stmt, range_expr;
10458
10459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10460 {
10461 bool expr_non_constant_p;
10462 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10463 }
10464 else
10465 range_expr = cp_parser_expression (parser);
10466
10467 /* If in template, STMT is converted to a normal for-statement
10468 at instantiation. If not, it is done just ahead. */
10469 if (processing_template_decl)
10470 {
10471 if (check_for_bare_parameter_packs (range_expr))
10472 range_expr = error_mark_node;
10473 stmt = begin_range_for_stmt (scope, init);
10474 if (ivdep)
10475 RANGE_FOR_IVDEP (stmt) = 1;
10476 finish_range_for_decl (stmt, range_decl, range_expr);
10477 if (!type_dependent_expression_p (range_expr)
10478 /* do_auto_deduction doesn't mess with template init-lists. */
10479 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10480 do_range_for_auto_deduction (range_decl, range_expr);
10481 }
10482 else
10483 {
10484 stmt = begin_for_stmt (scope, init);
10485 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10486 }
10487 return stmt;
10488 }
10489
10490 /* Subroutine of cp_convert_range_for: given the initializer expression,
10491 builds up the range temporary. */
10492
10493 static tree
10494 build_range_temp (tree range_expr)
10495 {
10496 tree range_type, range_temp;
10497
10498 /* Find out the type deduced by the declaration
10499 `auto &&__range = range_expr'. */
10500 range_type = cp_build_reference_type (make_auto (), true);
10501 range_type = do_auto_deduction (range_type, range_expr,
10502 type_uses_auto (range_type));
10503
10504 /* Create the __range variable. */
10505 range_temp = build_decl (input_location, VAR_DECL,
10506 get_identifier ("__for_range"), range_type);
10507 TREE_USED (range_temp) = 1;
10508 DECL_ARTIFICIAL (range_temp) = 1;
10509
10510 return range_temp;
10511 }
10512
10513 /* Used by cp_parser_range_for in template context: we aren't going to
10514 do a full conversion yet, but we still need to resolve auto in the
10515 type of the for-range-declaration if present. This is basically
10516 a shortcut version of cp_convert_range_for. */
10517
10518 static void
10519 do_range_for_auto_deduction (tree decl, tree range_expr)
10520 {
10521 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10522 if (auto_node)
10523 {
10524 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10525 range_temp = convert_from_reference (build_range_temp (range_expr));
10526 iter_type = (cp_parser_perform_range_for_lookup
10527 (range_temp, &begin_dummy, &end_dummy));
10528 if (iter_type)
10529 {
10530 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10531 iter_type);
10532 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10533 tf_warning_or_error);
10534 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10535 iter_decl, auto_node);
10536 }
10537 }
10538 }
10539
10540 /* Converts a range-based for-statement into a normal
10541 for-statement, as per the definition.
10542
10543 for (RANGE_DECL : RANGE_EXPR)
10544 BLOCK
10545
10546 should be equivalent to:
10547
10548 {
10549 auto &&__range = RANGE_EXPR;
10550 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10551 __begin != __end;
10552 ++__begin)
10553 {
10554 RANGE_DECL = *__begin;
10555 BLOCK
10556 }
10557 }
10558
10559 If RANGE_EXPR is an array:
10560 BEGIN_EXPR = __range
10561 END_EXPR = __range + ARRAY_SIZE(__range)
10562 Else if RANGE_EXPR has a member 'begin' or 'end':
10563 BEGIN_EXPR = __range.begin()
10564 END_EXPR = __range.end()
10565 Else:
10566 BEGIN_EXPR = begin(__range)
10567 END_EXPR = end(__range);
10568
10569 If __range has a member 'begin' but not 'end', or vice versa, we must
10570 still use the second alternative (it will surely fail, however).
10571 When calling begin()/end() in the third alternative we must use
10572 argument dependent lookup, but always considering 'std' as an associated
10573 namespace. */
10574
10575 tree
10576 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10577 bool ivdep)
10578 {
10579 tree begin, end;
10580 tree iter_type, begin_expr, end_expr;
10581 tree condition, expression;
10582
10583 if (range_decl == error_mark_node || range_expr == error_mark_node)
10584 /* If an error happened previously do nothing or else a lot of
10585 unhelpful errors would be issued. */
10586 begin_expr = end_expr = iter_type = error_mark_node;
10587 else
10588 {
10589 tree range_temp;
10590
10591 if (TREE_CODE (range_expr) == VAR_DECL
10592 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10593 /* Can't bind a reference to an array of runtime bound. */
10594 range_temp = range_expr;
10595 else
10596 {
10597 range_temp = build_range_temp (range_expr);
10598 pushdecl (range_temp);
10599 cp_finish_decl (range_temp, range_expr,
10600 /*is_constant_init*/false, NULL_TREE,
10601 LOOKUP_ONLYCONVERTING);
10602 range_temp = convert_from_reference (range_temp);
10603 }
10604 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10605 &begin_expr, &end_expr);
10606 }
10607
10608 /* The new for initialization statement. */
10609 begin = build_decl (input_location, VAR_DECL,
10610 get_identifier ("__for_begin"), iter_type);
10611 TREE_USED (begin) = 1;
10612 DECL_ARTIFICIAL (begin) = 1;
10613 pushdecl (begin);
10614 cp_finish_decl (begin, begin_expr,
10615 /*is_constant_init*/false, NULL_TREE,
10616 LOOKUP_ONLYCONVERTING);
10617
10618 end = build_decl (input_location, VAR_DECL,
10619 get_identifier ("__for_end"), iter_type);
10620 TREE_USED (end) = 1;
10621 DECL_ARTIFICIAL (end) = 1;
10622 pushdecl (end);
10623 cp_finish_decl (end, end_expr,
10624 /*is_constant_init*/false, NULL_TREE,
10625 LOOKUP_ONLYCONVERTING);
10626
10627 finish_for_init_stmt (statement);
10628
10629 /* The new for condition. */
10630 condition = build_x_binary_op (input_location, NE_EXPR,
10631 begin, ERROR_MARK,
10632 end, ERROR_MARK,
10633 NULL, tf_warning_or_error);
10634 finish_for_cond (condition, statement, ivdep);
10635
10636 /* The new increment expression. */
10637 expression = finish_unary_op_expr (input_location,
10638 PREINCREMENT_EXPR, begin,
10639 tf_warning_or_error);
10640 finish_for_expr (expression, statement);
10641
10642 /* The declaration is initialized with *__begin inside the loop body. */
10643 cp_finish_decl (range_decl,
10644 build_x_indirect_ref (input_location, begin, RO_NULL,
10645 tf_warning_or_error),
10646 /*is_constant_init*/false, NULL_TREE,
10647 LOOKUP_ONLYCONVERTING);
10648
10649 return statement;
10650 }
10651
10652 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10653 We need to solve both at the same time because the method used
10654 depends on the existence of members begin or end.
10655 Returns the type deduced for the iterator expression. */
10656
10657 static tree
10658 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10659 {
10660 if (error_operand_p (range))
10661 {
10662 *begin = *end = error_mark_node;
10663 return error_mark_node;
10664 }
10665
10666 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10667 {
10668 error ("range-based %<for%> expression of type %qT "
10669 "has incomplete type", TREE_TYPE (range));
10670 *begin = *end = error_mark_node;
10671 return error_mark_node;
10672 }
10673 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10674 {
10675 /* If RANGE is an array, we will use pointer arithmetic. */
10676 *begin = range;
10677 *end = build_binary_op (input_location, PLUS_EXPR,
10678 range,
10679 array_type_nelts_top (TREE_TYPE (range)),
10680 0);
10681 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10682 }
10683 else
10684 {
10685 /* If it is not an array, we must do a bit of magic. */
10686 tree id_begin, id_end;
10687 tree member_begin, member_end;
10688
10689 *begin = *end = error_mark_node;
10690
10691 id_begin = get_identifier ("begin");
10692 id_end = get_identifier ("end");
10693 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10694 /*protect=*/2, /*want_type=*/false,
10695 tf_warning_or_error);
10696 member_end = lookup_member (TREE_TYPE (range), id_end,
10697 /*protect=*/2, /*want_type=*/false,
10698 tf_warning_or_error);
10699
10700 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10701 {
10702 /* Use the member functions. */
10703 if (member_begin != NULL_TREE)
10704 *begin = cp_parser_range_for_member_function (range, id_begin);
10705 else
10706 error ("range-based %<for%> expression of type %qT has an "
10707 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10708
10709 if (member_end != NULL_TREE)
10710 *end = cp_parser_range_for_member_function (range, id_end);
10711 else
10712 error ("range-based %<for%> expression of type %qT has a "
10713 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10714 }
10715 else
10716 {
10717 /* Use global functions with ADL. */
10718 vec<tree, va_gc> *vec;
10719 vec = make_tree_vector ();
10720
10721 vec_safe_push (vec, range);
10722
10723 member_begin = perform_koenig_lookup (id_begin, vec,
10724 tf_warning_or_error);
10725 *begin = finish_call_expr (member_begin, &vec, false, true,
10726 tf_warning_or_error);
10727 member_end = perform_koenig_lookup (id_end, vec,
10728 tf_warning_or_error);
10729 *end = finish_call_expr (member_end, &vec, false, true,
10730 tf_warning_or_error);
10731
10732 release_tree_vector (vec);
10733 }
10734
10735 /* Last common checks. */
10736 if (*begin == error_mark_node || *end == error_mark_node)
10737 {
10738 /* If one of the expressions is an error do no more checks. */
10739 *begin = *end = error_mark_node;
10740 return error_mark_node;
10741 }
10742 else if (type_dependent_expression_p (*begin)
10743 || type_dependent_expression_p (*end))
10744 /* Can happen, when, eg, in a template context, Koenig lookup
10745 can't resolve begin/end (c++/58503). */
10746 return NULL_TREE;
10747 else
10748 {
10749 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10750 /* The unqualified type of the __begin and __end temporaries should
10751 be the same, as required by the multiple auto declaration. */
10752 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10753 error ("inconsistent begin/end types in range-based %<for%> "
10754 "statement: %qT and %qT",
10755 TREE_TYPE (*begin), TREE_TYPE (*end));
10756 return iter_type;
10757 }
10758 }
10759 }
10760
10761 /* Helper function for cp_parser_perform_range_for_lookup.
10762 Builds a tree for RANGE.IDENTIFIER(). */
10763
10764 static tree
10765 cp_parser_range_for_member_function (tree range, tree identifier)
10766 {
10767 tree member, res;
10768 vec<tree, va_gc> *vec;
10769
10770 member = finish_class_member_access_expr (range, identifier,
10771 false, tf_warning_or_error);
10772 if (member == error_mark_node)
10773 return error_mark_node;
10774
10775 vec = make_tree_vector ();
10776 res = finish_call_expr (member, &vec,
10777 /*disallow_virtual=*/false,
10778 /*koenig_p=*/false,
10779 tf_warning_or_error);
10780 release_tree_vector (vec);
10781 return res;
10782 }
10783
10784 /* Parse an iteration-statement.
10785
10786 iteration-statement:
10787 while ( condition ) statement
10788 do statement while ( expression ) ;
10789 for ( for-init-statement condition [opt] ; expression [opt] )
10790 statement
10791
10792 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10793
10794 static tree
10795 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10796 {
10797 cp_token *token;
10798 enum rid keyword;
10799 tree statement;
10800 unsigned char in_statement;
10801
10802 /* Peek at the next token. */
10803 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10804 if (!token)
10805 return error_mark_node;
10806
10807 /* Remember whether or not we are already within an iteration
10808 statement. */
10809 in_statement = parser->in_statement;
10810
10811 /* See what kind of keyword it is. */
10812 keyword = token->keyword;
10813 switch (keyword)
10814 {
10815 case RID_WHILE:
10816 {
10817 tree condition;
10818
10819 /* Begin the while-statement. */
10820 statement = begin_while_stmt ();
10821 /* Look for the `('. */
10822 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10823 /* Parse the condition. */
10824 condition = cp_parser_condition (parser);
10825 finish_while_stmt_cond (condition, statement, ivdep);
10826 /* Look for the `)'. */
10827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10828 /* Parse the dependent statement. */
10829 parser->in_statement = IN_ITERATION_STMT;
10830 cp_parser_already_scoped_statement (parser);
10831 parser->in_statement = in_statement;
10832 /* We're done with the while-statement. */
10833 finish_while_stmt (statement);
10834 }
10835 break;
10836
10837 case RID_DO:
10838 {
10839 tree expression;
10840
10841 /* Begin the do-statement. */
10842 statement = begin_do_stmt ();
10843 /* Parse the body of the do-statement. */
10844 parser->in_statement = IN_ITERATION_STMT;
10845 cp_parser_implicitly_scoped_statement (parser, NULL);
10846 parser->in_statement = in_statement;
10847 finish_do_body (statement);
10848 /* Look for the `while' keyword. */
10849 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10850 /* Look for the `('. */
10851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10852 /* Parse the expression. */
10853 expression = cp_parser_expression (parser);
10854 /* We're done with the do-statement. */
10855 finish_do_stmt (expression, statement, ivdep);
10856 /* Look for the `)'. */
10857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10858 /* Look for the `;'. */
10859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10860 }
10861 break;
10862
10863 case RID_FOR:
10864 {
10865 /* Look for the `('. */
10866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10867
10868 statement = cp_parser_for (parser, ivdep);
10869
10870 /* Look for the `)'. */
10871 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10872
10873 /* Parse the body of the for-statement. */
10874 parser->in_statement = IN_ITERATION_STMT;
10875 cp_parser_already_scoped_statement (parser);
10876 parser->in_statement = in_statement;
10877
10878 /* We're done with the for-statement. */
10879 finish_for_stmt (statement);
10880 }
10881 break;
10882
10883 default:
10884 cp_parser_error (parser, "expected iteration-statement");
10885 statement = error_mark_node;
10886 break;
10887 }
10888
10889 return statement;
10890 }
10891
10892 /* Parse a for-init-statement or the declarator of a range-based-for.
10893 Returns true if a range-based-for declaration is seen.
10894
10895 for-init-statement:
10896 expression-statement
10897 simple-declaration */
10898
10899 static bool
10900 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10901 {
10902 /* If the next token is a `;', then we have an empty
10903 expression-statement. Grammatically, this is also a
10904 simple-declaration, but an invalid one, because it does not
10905 declare anything. Therefore, if we did not handle this case
10906 specially, we would issue an error message about an invalid
10907 declaration. */
10908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10909 {
10910 bool is_range_for = false;
10911 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10912
10913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10914 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10915 {
10916 /* N3994 -- for (id : init) ... */
10917 if (cxx_dialect < cxx1z)
10918 pedwarn (input_location, 0, "range-based for loop without a "
10919 "type-specifier only available with "
10920 "-std=c++1z or -std=gnu++1z");
10921 tree name = cp_parser_identifier (parser);
10922 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10923 *decl = build_decl (input_location, VAR_DECL, name, type);
10924 pushdecl (*decl);
10925 cp_lexer_consume_token (parser->lexer);
10926 return true;
10927 }
10928
10929 /* A colon is used in range-based for. */
10930 parser->colon_corrects_to_scope_p = false;
10931
10932 /* We're going to speculatively look for a declaration, falling back
10933 to an expression, if necessary. */
10934 cp_parser_parse_tentatively (parser);
10935 /* Parse the declaration. */
10936 cp_parser_simple_declaration (parser,
10937 /*function_definition_allowed_p=*/false,
10938 decl);
10939 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10940 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10941 {
10942 /* It is a range-for, consume the ':' */
10943 cp_lexer_consume_token (parser->lexer);
10944 is_range_for = true;
10945 if (cxx_dialect < cxx11)
10946 {
10947 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10948 "range-based %<for%> loops only available with "
10949 "-std=c++11 or -std=gnu++11");
10950 *decl = error_mark_node;
10951 }
10952 }
10953 else
10954 /* The ';' is not consumed yet because we told
10955 cp_parser_simple_declaration not to. */
10956 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10957
10958 if (cp_parser_parse_definitely (parser))
10959 return is_range_for;
10960 /* If the tentative parse failed, then we shall need to look for an
10961 expression-statement. */
10962 }
10963 /* If we are here, it is an expression-statement. */
10964 cp_parser_expression_statement (parser, NULL_TREE);
10965 return false;
10966 }
10967
10968 /* Parse a jump-statement.
10969
10970 jump-statement:
10971 break ;
10972 continue ;
10973 return expression [opt] ;
10974 return braced-init-list ;
10975 goto identifier ;
10976
10977 GNU extension:
10978
10979 jump-statement:
10980 goto * expression ;
10981
10982 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10983
10984 static tree
10985 cp_parser_jump_statement (cp_parser* parser)
10986 {
10987 tree statement = error_mark_node;
10988 cp_token *token;
10989 enum rid keyword;
10990 unsigned char in_statement;
10991
10992 /* Peek at the next token. */
10993 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10994 if (!token)
10995 return error_mark_node;
10996
10997 /* See what kind of keyword it is. */
10998 keyword = token->keyword;
10999 switch (keyword)
11000 {
11001 case RID_BREAK:
11002 in_statement = parser->in_statement & ~IN_IF_STMT;
11003 switch (in_statement)
11004 {
11005 case 0:
11006 error_at (token->location, "break statement not within loop or switch");
11007 break;
11008 default:
11009 gcc_assert ((in_statement & IN_SWITCH_STMT)
11010 || in_statement == IN_ITERATION_STMT);
11011 statement = finish_break_stmt ();
11012 if (in_statement == IN_ITERATION_STMT)
11013 break_maybe_infinite_loop ();
11014 break;
11015 case IN_OMP_BLOCK:
11016 error_at (token->location, "invalid exit from OpenMP structured block");
11017 break;
11018 case IN_OMP_FOR:
11019 error_at (token->location, "break statement used with OpenMP for loop");
11020 break;
11021 case IN_CILK_SIMD_FOR:
11022 error_at (token->location, "break statement used with Cilk Plus for loop");
11023 break;
11024 }
11025 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11026 break;
11027
11028 case RID_CONTINUE:
11029 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11030 {
11031 case 0:
11032 error_at (token->location, "continue statement not within a loop");
11033 break;
11034 case IN_CILK_SIMD_FOR:
11035 error_at (token->location,
11036 "continue statement within %<#pragma simd%> loop body");
11037 /* Fall through. */
11038 case IN_ITERATION_STMT:
11039 case IN_OMP_FOR:
11040 statement = finish_continue_stmt ();
11041 break;
11042 case IN_OMP_BLOCK:
11043 error_at (token->location, "invalid exit from OpenMP structured block");
11044 break;
11045 default:
11046 gcc_unreachable ();
11047 }
11048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11049 break;
11050
11051 case RID_RETURN:
11052 {
11053 tree expr;
11054 bool expr_non_constant_p;
11055
11056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11057 {
11058 cp_lexer_set_source_position (parser->lexer);
11059 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11060 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11061 }
11062 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11063 expr = cp_parser_expression (parser);
11064 else
11065 /* If the next token is a `;', then there is no
11066 expression. */
11067 expr = NULL_TREE;
11068 /* Build the return-statement. */
11069 statement = finish_return_stmt (expr);
11070 /* Look for the final `;'. */
11071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11072 }
11073 break;
11074
11075 case RID_GOTO:
11076 if (parser->in_function_body
11077 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11078 {
11079 error ("%<goto%> in %<constexpr%> function");
11080 cp_function_chain->invalid_constexpr = true;
11081 }
11082
11083 /* Create the goto-statement. */
11084 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11085 {
11086 /* Issue a warning about this use of a GNU extension. */
11087 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11088 /* Consume the '*' token. */
11089 cp_lexer_consume_token (parser->lexer);
11090 /* Parse the dependent expression. */
11091 finish_goto_stmt (cp_parser_expression (parser));
11092 }
11093 else
11094 finish_goto_stmt (cp_parser_identifier (parser));
11095 /* Look for the final `;'. */
11096 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11097 break;
11098
11099 default:
11100 cp_parser_error (parser, "expected jump-statement");
11101 break;
11102 }
11103
11104 return statement;
11105 }
11106
11107 /* Parse a declaration-statement.
11108
11109 declaration-statement:
11110 block-declaration */
11111
11112 static void
11113 cp_parser_declaration_statement (cp_parser* parser)
11114 {
11115 void *p;
11116
11117 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11118 p = obstack_alloc (&declarator_obstack, 0);
11119
11120 /* Parse the block-declaration. */
11121 cp_parser_block_declaration (parser, /*statement_p=*/true);
11122
11123 /* Free any declarators allocated. */
11124 obstack_free (&declarator_obstack, p);
11125 }
11126
11127 /* Some dependent statements (like `if (cond) statement'), are
11128 implicitly in their own scope. In other words, if the statement is
11129 a single statement (as opposed to a compound-statement), it is
11130 none-the-less treated as if it were enclosed in braces. Any
11131 declarations appearing in the dependent statement are out of scope
11132 after control passes that point. This function parses a statement,
11133 but ensures that is in its own scope, even if it is not a
11134 compound-statement.
11135
11136 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11137 is a (possibly labeled) if statement which is not enclosed in
11138 braces and has an else clause. This is used to implement
11139 -Wparentheses.
11140
11141 Returns the new statement. */
11142
11143 static tree
11144 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11145 {
11146 tree statement;
11147
11148 if (if_p != NULL)
11149 *if_p = false;
11150
11151 /* Mark if () ; with a special NOP_EXPR. */
11152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11153 {
11154 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11155 cp_lexer_consume_token (parser->lexer);
11156 statement = add_stmt (build_empty_stmt (loc));
11157 }
11158 /* if a compound is opened, we simply parse the statement directly. */
11159 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11160 statement = cp_parser_compound_statement (parser, NULL, false, false);
11161 /* If the token is not a `{', then we must take special action. */
11162 else
11163 {
11164 /* Create a compound-statement. */
11165 statement = begin_compound_stmt (0);
11166 /* Parse the dependent-statement. */
11167 cp_parser_statement (parser, NULL_TREE, false, if_p);
11168 /* Finish the dummy compound-statement. */
11169 finish_compound_stmt (statement);
11170 }
11171
11172 /* Return the statement. */
11173 return statement;
11174 }
11175
11176 /* For some dependent statements (like `while (cond) statement'), we
11177 have already created a scope. Therefore, even if the dependent
11178 statement is a compound-statement, we do not want to create another
11179 scope. */
11180
11181 static void
11182 cp_parser_already_scoped_statement (cp_parser* parser)
11183 {
11184 /* If the token is a `{', then we must take special action. */
11185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11186 cp_parser_statement (parser, NULL_TREE, false, NULL);
11187 else
11188 {
11189 /* Avoid calling cp_parser_compound_statement, so that we
11190 don't create a new scope. Do everything else by hand. */
11191 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11192 /* If the next keyword is `__label__' we have a label declaration. */
11193 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11194 cp_parser_label_declaration (parser);
11195 /* Parse an (optional) statement-seq. */
11196 cp_parser_statement_seq_opt (parser, NULL_TREE);
11197 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11198 }
11199 }
11200
11201 /* Declarations [gram.dcl.dcl] */
11202
11203 /* Parse an optional declaration-sequence.
11204
11205 declaration-seq:
11206 declaration
11207 declaration-seq declaration */
11208
11209 static void
11210 cp_parser_declaration_seq_opt (cp_parser* parser)
11211 {
11212 while (true)
11213 {
11214 cp_token *token;
11215
11216 token = cp_lexer_peek_token (parser->lexer);
11217
11218 if (token->type == CPP_CLOSE_BRACE
11219 || token->type == CPP_EOF
11220 || token->type == CPP_PRAGMA_EOL)
11221 break;
11222
11223 if (token->type == CPP_SEMICOLON)
11224 {
11225 /* A declaration consisting of a single semicolon is
11226 invalid. Allow it unless we're being pedantic. */
11227 cp_lexer_consume_token (parser->lexer);
11228 if (!in_system_header_at (input_location))
11229 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11230 continue;
11231 }
11232
11233 /* If we're entering or exiting a region that's implicitly
11234 extern "C", modify the lang context appropriately. */
11235 if (!parser->implicit_extern_c && token->implicit_extern_c)
11236 {
11237 push_lang_context (lang_name_c);
11238 parser->implicit_extern_c = true;
11239 }
11240 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11241 {
11242 pop_lang_context ();
11243 parser->implicit_extern_c = false;
11244 }
11245
11246 if (token->type == CPP_PRAGMA)
11247 {
11248 /* A top-level declaration can consist solely of a #pragma.
11249 A nested declaration cannot, so this is done here and not
11250 in cp_parser_declaration. (A #pragma at block scope is
11251 handled in cp_parser_statement.) */
11252 cp_parser_pragma (parser, pragma_external);
11253 continue;
11254 }
11255
11256 /* Parse the declaration itself. */
11257 cp_parser_declaration (parser);
11258 }
11259 }
11260
11261 /* Parse a declaration.
11262
11263 declaration:
11264 block-declaration
11265 function-definition
11266 template-declaration
11267 explicit-instantiation
11268 explicit-specialization
11269 linkage-specification
11270 namespace-definition
11271
11272 GNU extension:
11273
11274 declaration:
11275 __extension__ declaration */
11276
11277 static void
11278 cp_parser_declaration (cp_parser* parser)
11279 {
11280 cp_token token1;
11281 cp_token token2;
11282 int saved_pedantic;
11283 void *p;
11284 tree attributes = NULL_TREE;
11285
11286 /* Check for the `__extension__' keyword. */
11287 if (cp_parser_extension_opt (parser, &saved_pedantic))
11288 {
11289 /* Parse the qualified declaration. */
11290 cp_parser_declaration (parser);
11291 /* Restore the PEDANTIC flag. */
11292 pedantic = saved_pedantic;
11293
11294 return;
11295 }
11296
11297 /* Try to figure out what kind of declaration is present. */
11298 token1 = *cp_lexer_peek_token (parser->lexer);
11299
11300 if (token1.type != CPP_EOF)
11301 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11302 else
11303 {
11304 token2.type = CPP_EOF;
11305 token2.keyword = RID_MAX;
11306 }
11307
11308 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11309 p = obstack_alloc (&declarator_obstack, 0);
11310
11311 /* If the next token is `extern' and the following token is a string
11312 literal, then we have a linkage specification. */
11313 if (token1.keyword == RID_EXTERN
11314 && cp_parser_is_pure_string_literal (&token2))
11315 cp_parser_linkage_specification (parser);
11316 /* If the next token is `template', then we have either a template
11317 declaration, an explicit instantiation, or an explicit
11318 specialization. */
11319 else if (token1.keyword == RID_TEMPLATE)
11320 {
11321 /* `template <>' indicates a template specialization. */
11322 if (token2.type == CPP_LESS
11323 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11324 cp_parser_explicit_specialization (parser);
11325 /* `template <' indicates a template declaration. */
11326 else if (token2.type == CPP_LESS)
11327 cp_parser_template_declaration (parser, /*member_p=*/false);
11328 /* Anything else must be an explicit instantiation. */
11329 else
11330 cp_parser_explicit_instantiation (parser);
11331 }
11332 /* If the next token is `export', then we have a template
11333 declaration. */
11334 else if (token1.keyword == RID_EXPORT)
11335 cp_parser_template_declaration (parser, /*member_p=*/false);
11336 /* If the next token is `extern', 'static' or 'inline' and the one
11337 after that is `template', we have a GNU extended explicit
11338 instantiation directive. */
11339 else if (cp_parser_allow_gnu_extensions_p (parser)
11340 && (token1.keyword == RID_EXTERN
11341 || token1.keyword == RID_STATIC
11342 || token1.keyword == RID_INLINE)
11343 && token2.keyword == RID_TEMPLATE)
11344 cp_parser_explicit_instantiation (parser);
11345 /* If the next token is `namespace', check for a named or unnamed
11346 namespace definition. */
11347 else if (token1.keyword == RID_NAMESPACE
11348 && (/* A named namespace definition. */
11349 (token2.type == CPP_NAME
11350 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11351 != CPP_EQ))
11352 /* An unnamed namespace definition. */
11353 || token2.type == CPP_OPEN_BRACE
11354 || token2.keyword == RID_ATTRIBUTE))
11355 cp_parser_namespace_definition (parser);
11356 /* An inline (associated) namespace definition. */
11357 else if (token1.keyword == RID_INLINE
11358 && token2.keyword == RID_NAMESPACE)
11359 cp_parser_namespace_definition (parser);
11360 /* Objective-C++ declaration/definition. */
11361 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11362 cp_parser_objc_declaration (parser, NULL_TREE);
11363 else if (c_dialect_objc ()
11364 && token1.keyword == RID_ATTRIBUTE
11365 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11366 cp_parser_objc_declaration (parser, attributes);
11367 /* We must have either a block declaration or a function
11368 definition. */
11369 else
11370 /* Try to parse a block-declaration, or a function-definition. */
11371 cp_parser_block_declaration (parser, /*statement_p=*/false);
11372
11373 /* Free any declarators allocated. */
11374 obstack_free (&declarator_obstack, p);
11375 }
11376
11377 /* Parse a block-declaration.
11378
11379 block-declaration:
11380 simple-declaration
11381 asm-definition
11382 namespace-alias-definition
11383 using-declaration
11384 using-directive
11385
11386 GNU Extension:
11387
11388 block-declaration:
11389 __extension__ block-declaration
11390
11391 C++0x Extension:
11392
11393 block-declaration:
11394 static_assert-declaration
11395
11396 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11397 part of a declaration-statement. */
11398
11399 static void
11400 cp_parser_block_declaration (cp_parser *parser,
11401 bool statement_p)
11402 {
11403 cp_token *token1;
11404 int saved_pedantic;
11405
11406 /* Check for the `__extension__' keyword. */
11407 if (cp_parser_extension_opt (parser, &saved_pedantic))
11408 {
11409 /* Parse the qualified declaration. */
11410 cp_parser_block_declaration (parser, statement_p);
11411 /* Restore the PEDANTIC flag. */
11412 pedantic = saved_pedantic;
11413
11414 return;
11415 }
11416
11417 /* Peek at the next token to figure out which kind of declaration is
11418 present. */
11419 token1 = cp_lexer_peek_token (parser->lexer);
11420
11421 /* If the next keyword is `asm', we have an asm-definition. */
11422 if (token1->keyword == RID_ASM)
11423 {
11424 if (statement_p)
11425 cp_parser_commit_to_tentative_parse (parser);
11426 cp_parser_asm_definition (parser);
11427 }
11428 /* If the next keyword is `namespace', we have a
11429 namespace-alias-definition. */
11430 else if (token1->keyword == RID_NAMESPACE)
11431 cp_parser_namespace_alias_definition (parser);
11432 /* If the next keyword is `using', we have a
11433 using-declaration, a using-directive, or an alias-declaration. */
11434 else if (token1->keyword == RID_USING)
11435 {
11436 cp_token *token2;
11437
11438 if (statement_p)
11439 cp_parser_commit_to_tentative_parse (parser);
11440 /* If the token after `using' is `namespace', then we have a
11441 using-directive. */
11442 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11443 if (token2->keyword == RID_NAMESPACE)
11444 cp_parser_using_directive (parser);
11445 /* If the second token after 'using' is '=', then we have an
11446 alias-declaration. */
11447 else if (cxx_dialect >= cxx11
11448 && token2->type == CPP_NAME
11449 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11450 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11451 cp_parser_alias_declaration (parser);
11452 /* Otherwise, it's a using-declaration. */
11453 else
11454 cp_parser_using_declaration (parser,
11455 /*access_declaration_p=*/false);
11456 }
11457 /* If the next keyword is `__label__' we have a misplaced label
11458 declaration. */
11459 else if (token1->keyword == RID_LABEL)
11460 {
11461 cp_lexer_consume_token (parser->lexer);
11462 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11463 cp_parser_skip_to_end_of_statement (parser);
11464 /* If the next token is now a `;', consume it. */
11465 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11466 cp_lexer_consume_token (parser->lexer);
11467 }
11468 /* If the next token is `static_assert' we have a static assertion. */
11469 else if (token1->keyword == RID_STATIC_ASSERT)
11470 cp_parser_static_assert (parser, /*member_p=*/false);
11471 /* Anything else must be a simple-declaration. */
11472 else
11473 cp_parser_simple_declaration (parser, !statement_p,
11474 /*maybe_range_for_decl*/NULL);
11475 }
11476
11477 /* Parse a simple-declaration.
11478
11479 simple-declaration:
11480 decl-specifier-seq [opt] init-declarator-list [opt] ;
11481
11482 init-declarator-list:
11483 init-declarator
11484 init-declarator-list , init-declarator
11485
11486 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11487 function-definition as a simple-declaration.
11488
11489 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11490 parsed declaration if it is an uninitialized single declarator not followed
11491 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11492 if present, will not be consumed. */
11493
11494 static void
11495 cp_parser_simple_declaration (cp_parser* parser,
11496 bool function_definition_allowed_p,
11497 tree *maybe_range_for_decl)
11498 {
11499 cp_decl_specifier_seq decl_specifiers;
11500 int declares_class_or_enum;
11501 bool saw_declarator;
11502 location_t comma_loc = UNKNOWN_LOCATION;
11503 location_t init_loc = UNKNOWN_LOCATION;
11504
11505 if (maybe_range_for_decl)
11506 *maybe_range_for_decl = NULL_TREE;
11507
11508 /* Defer access checks until we know what is being declared; the
11509 checks for names appearing in the decl-specifier-seq should be
11510 done as if we were in the scope of the thing being declared. */
11511 push_deferring_access_checks (dk_deferred);
11512
11513 /* Parse the decl-specifier-seq. We have to keep track of whether
11514 or not the decl-specifier-seq declares a named class or
11515 enumeration type, since that is the only case in which the
11516 init-declarator-list is allowed to be empty.
11517
11518 [dcl.dcl]
11519
11520 In a simple-declaration, the optional init-declarator-list can be
11521 omitted only when declaring a class or enumeration, that is when
11522 the decl-specifier-seq contains either a class-specifier, an
11523 elaborated-type-specifier, or an enum-specifier. */
11524 cp_parser_decl_specifier_seq (parser,
11525 CP_PARSER_FLAGS_OPTIONAL,
11526 &decl_specifiers,
11527 &declares_class_or_enum);
11528 /* We no longer need to defer access checks. */
11529 stop_deferring_access_checks ();
11530
11531 /* In a block scope, a valid declaration must always have a
11532 decl-specifier-seq. By not trying to parse declarators, we can
11533 resolve the declaration/expression ambiguity more quickly. */
11534 if (!function_definition_allowed_p
11535 && !decl_specifiers.any_specifiers_p)
11536 {
11537 cp_parser_error (parser, "expected declaration");
11538 goto done;
11539 }
11540
11541 /* If the next two tokens are both identifiers, the code is
11542 erroneous. The usual cause of this situation is code like:
11543
11544 T t;
11545
11546 where "T" should name a type -- but does not. */
11547 if (!decl_specifiers.any_type_specifiers_p
11548 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11549 {
11550 /* If parsing tentatively, we should commit; we really are
11551 looking at a declaration. */
11552 cp_parser_commit_to_tentative_parse (parser);
11553 /* Give up. */
11554 goto done;
11555 }
11556
11557 /* If we have seen at least one decl-specifier, and the next token
11558 is not a parenthesis, then we must be looking at a declaration.
11559 (After "int (" we might be looking at a functional cast.) */
11560 if (decl_specifiers.any_specifiers_p
11561 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11562 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11563 && !cp_parser_error_occurred (parser))
11564 cp_parser_commit_to_tentative_parse (parser);
11565
11566 /* Keep going until we hit the `;' at the end of the simple
11567 declaration. */
11568 saw_declarator = false;
11569 while (cp_lexer_next_token_is_not (parser->lexer,
11570 CPP_SEMICOLON))
11571 {
11572 cp_token *token;
11573 bool function_definition_p;
11574 tree decl;
11575
11576 if (saw_declarator)
11577 {
11578 /* If we are processing next declarator, comma is expected */
11579 token = cp_lexer_peek_token (parser->lexer);
11580 gcc_assert (token->type == CPP_COMMA);
11581 cp_lexer_consume_token (parser->lexer);
11582 if (maybe_range_for_decl)
11583 {
11584 *maybe_range_for_decl = error_mark_node;
11585 if (comma_loc == UNKNOWN_LOCATION)
11586 comma_loc = token->location;
11587 }
11588 }
11589 else
11590 saw_declarator = true;
11591
11592 /* Parse the init-declarator. */
11593 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11594 /*checks=*/NULL,
11595 function_definition_allowed_p,
11596 /*member_p=*/false,
11597 declares_class_or_enum,
11598 &function_definition_p,
11599 maybe_range_for_decl,
11600 &init_loc);
11601 /* If an error occurred while parsing tentatively, exit quickly.
11602 (That usually happens when in the body of a function; each
11603 statement is treated as a declaration-statement until proven
11604 otherwise.) */
11605 if (cp_parser_error_occurred (parser))
11606 goto done;
11607 /* Handle function definitions specially. */
11608 if (function_definition_p)
11609 {
11610 /* If the next token is a `,', then we are probably
11611 processing something like:
11612
11613 void f() {}, *p;
11614
11615 which is erroneous. */
11616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11617 {
11618 cp_token *token = cp_lexer_peek_token (parser->lexer);
11619 error_at (token->location,
11620 "mixing"
11621 " declarations and function-definitions is forbidden");
11622 }
11623 /* Otherwise, we're done with the list of declarators. */
11624 else
11625 {
11626 pop_deferring_access_checks ();
11627 return;
11628 }
11629 }
11630 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11631 *maybe_range_for_decl = decl;
11632 /* The next token should be either a `,' or a `;'. */
11633 token = cp_lexer_peek_token (parser->lexer);
11634 /* If it's a `,', there are more declarators to come. */
11635 if (token->type == CPP_COMMA)
11636 /* will be consumed next time around */;
11637 /* If it's a `;', we are done. */
11638 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11639 break;
11640 /* Anything else is an error. */
11641 else
11642 {
11643 /* If we have already issued an error message we don't need
11644 to issue another one. */
11645 if (decl != error_mark_node
11646 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11647 cp_parser_error (parser, "expected %<,%> or %<;%>");
11648 /* Skip tokens until we reach the end of the statement. */
11649 cp_parser_skip_to_end_of_statement (parser);
11650 /* If the next token is now a `;', consume it. */
11651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11652 cp_lexer_consume_token (parser->lexer);
11653 goto done;
11654 }
11655 /* After the first time around, a function-definition is not
11656 allowed -- even if it was OK at first. For example:
11657
11658 int i, f() {}
11659
11660 is not valid. */
11661 function_definition_allowed_p = false;
11662 }
11663
11664 /* Issue an error message if no declarators are present, and the
11665 decl-specifier-seq does not itself declare a class or
11666 enumeration: [dcl.dcl]/3. */
11667 if (!saw_declarator)
11668 {
11669 if (cp_parser_declares_only_class_p (parser))
11670 {
11671 if (!declares_class_or_enum
11672 && decl_specifiers.type
11673 && OVERLOAD_TYPE_P (decl_specifiers.type))
11674 /* Ensure an error is issued anyway when finish_decltype_type,
11675 called via cp_parser_decl_specifier_seq, returns a class or
11676 an enumeration (c++/51786). */
11677 decl_specifiers.type = NULL_TREE;
11678 shadow_tag (&decl_specifiers);
11679 }
11680 /* Perform any deferred access checks. */
11681 perform_deferred_access_checks (tf_warning_or_error);
11682 }
11683
11684 /* Consume the `;'. */
11685 if (!maybe_range_for_decl)
11686 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11687 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11688 {
11689 if (init_loc != UNKNOWN_LOCATION)
11690 error_at (init_loc, "initializer in range-based %<for%> loop");
11691 if (comma_loc != UNKNOWN_LOCATION)
11692 error_at (comma_loc,
11693 "multiple declarations in range-based %<for%> loop");
11694 }
11695
11696 done:
11697 pop_deferring_access_checks ();
11698 }
11699
11700 /* Parse a decl-specifier-seq.
11701
11702 decl-specifier-seq:
11703 decl-specifier-seq [opt] decl-specifier
11704 decl-specifier attribute-specifier-seq [opt] (C++11)
11705
11706 decl-specifier:
11707 storage-class-specifier
11708 type-specifier
11709 function-specifier
11710 friend
11711 typedef
11712
11713 GNU Extension:
11714
11715 decl-specifier:
11716 attributes
11717
11718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11719
11720 The parser flags FLAGS is used to control type-specifier parsing.
11721
11722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11723 flags:
11724
11725 1: one of the decl-specifiers is an elaborated-type-specifier
11726 (i.e., a type declaration)
11727 2: one of the decl-specifiers is an enum-specifier or a
11728 class-specifier (i.e., a type definition)
11729
11730 */
11731
11732 static void
11733 cp_parser_decl_specifier_seq (cp_parser* parser,
11734 cp_parser_flags flags,
11735 cp_decl_specifier_seq *decl_specs,
11736 int* declares_class_or_enum)
11737 {
11738 bool constructor_possible_p = !parser->in_declarator_p;
11739 bool found_decl_spec = false;
11740 cp_token *start_token = NULL;
11741 cp_decl_spec ds;
11742
11743 /* Clear DECL_SPECS. */
11744 clear_decl_specs (decl_specs);
11745
11746 /* Assume no class or enumeration type is declared. */
11747 *declares_class_or_enum = 0;
11748
11749 /* Keep reading specifiers until there are no more to read. */
11750 while (true)
11751 {
11752 bool constructor_p;
11753 cp_token *token;
11754 ds = ds_last;
11755
11756 /* Peek at the next token. */
11757 token = cp_lexer_peek_token (parser->lexer);
11758
11759 /* Save the first token of the decl spec list for error
11760 reporting. */
11761 if (!start_token)
11762 start_token = token;
11763 /* Handle attributes. */
11764 if (cp_next_tokens_can_be_attribute_p (parser))
11765 {
11766 /* Parse the attributes. */
11767 tree attrs = cp_parser_attributes_opt (parser);
11768
11769 /* In a sequence of declaration specifiers, c++11 attributes
11770 appertain to the type that precede them. In that case
11771 [dcl.spec]/1 says:
11772
11773 The attribute-specifier-seq affects the type only for
11774 the declaration it appears in, not other declarations
11775 involving the same type.
11776
11777 But for now let's force the user to position the
11778 attribute either at the beginning of the declaration or
11779 after the declarator-id, which would clearly mean that it
11780 applies to the declarator. */
11781 if (cxx11_attribute_p (attrs))
11782 {
11783 if (!found_decl_spec)
11784 /* The c++11 attribute is at the beginning of the
11785 declaration. It appertains to the entity being
11786 declared. */;
11787 else
11788 {
11789 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11790 {
11791 /* This is an attribute following a
11792 class-specifier. */
11793 if (decl_specs->type_definition_p)
11794 warn_misplaced_attr_for_class_type (token->location,
11795 decl_specs->type);
11796 attrs = NULL_TREE;
11797 }
11798 else
11799 {
11800 decl_specs->std_attributes
11801 = chainon (decl_specs->std_attributes,
11802 attrs);
11803 if (decl_specs->locations[ds_std_attribute] == 0)
11804 decl_specs->locations[ds_std_attribute] = token->location;
11805 }
11806 continue;
11807 }
11808 }
11809
11810 decl_specs->attributes
11811 = chainon (decl_specs->attributes,
11812 attrs);
11813 if (decl_specs->locations[ds_attribute] == 0)
11814 decl_specs->locations[ds_attribute] = token->location;
11815 continue;
11816 }
11817 /* Assume we will find a decl-specifier keyword. */
11818 found_decl_spec = true;
11819 /* If the next token is an appropriate keyword, we can simply
11820 add it to the list. */
11821 switch (token->keyword)
11822 {
11823 /* decl-specifier:
11824 friend
11825 constexpr */
11826 case RID_FRIEND:
11827 if (!at_class_scope_p ())
11828 {
11829 error_at (token->location, "%<friend%> used outside of class");
11830 cp_lexer_purge_token (parser->lexer);
11831 }
11832 else
11833 {
11834 ds = ds_friend;
11835 /* Consume the token. */
11836 cp_lexer_consume_token (parser->lexer);
11837 }
11838 break;
11839
11840 case RID_CONSTEXPR:
11841 ds = ds_constexpr;
11842 cp_lexer_consume_token (parser->lexer);
11843 break;
11844
11845 /* function-specifier:
11846 inline
11847 virtual
11848 explicit */
11849 case RID_INLINE:
11850 case RID_VIRTUAL:
11851 case RID_EXPLICIT:
11852 cp_parser_function_specifier_opt (parser, decl_specs);
11853 break;
11854
11855 /* decl-specifier:
11856 typedef */
11857 case RID_TYPEDEF:
11858 ds = ds_typedef;
11859 /* Consume the token. */
11860 cp_lexer_consume_token (parser->lexer);
11861 /* A constructor declarator cannot appear in a typedef. */
11862 constructor_possible_p = false;
11863 /* The "typedef" keyword can only occur in a declaration; we
11864 may as well commit at this point. */
11865 cp_parser_commit_to_tentative_parse (parser);
11866
11867 if (decl_specs->storage_class != sc_none)
11868 decl_specs->conflicting_specifiers_p = true;
11869 break;
11870
11871 /* storage-class-specifier:
11872 auto
11873 register
11874 static
11875 extern
11876 mutable
11877
11878 GNU Extension:
11879 thread */
11880 case RID_AUTO:
11881 if (cxx_dialect == cxx98)
11882 {
11883 /* Consume the token. */
11884 cp_lexer_consume_token (parser->lexer);
11885
11886 /* Complain about `auto' as a storage specifier, if
11887 we're complaining about C++0x compatibility. */
11888 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11889 " changes meaning in C++11; please remove it");
11890
11891 /* Set the storage class anyway. */
11892 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11893 token);
11894 }
11895 else
11896 /* C++0x auto type-specifier. */
11897 found_decl_spec = false;
11898 break;
11899
11900 case RID_REGISTER:
11901 case RID_STATIC:
11902 case RID_EXTERN:
11903 case RID_MUTABLE:
11904 /* Consume the token. */
11905 cp_lexer_consume_token (parser->lexer);
11906 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11907 token);
11908 break;
11909 case RID_THREAD:
11910 /* Consume the token. */
11911 ds = ds_thread;
11912 cp_lexer_consume_token (parser->lexer);
11913 break;
11914
11915 default:
11916 /* We did not yet find a decl-specifier yet. */
11917 found_decl_spec = false;
11918 break;
11919 }
11920
11921 if (found_decl_spec
11922 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11923 && token->keyword != RID_CONSTEXPR)
11924 error ("decl-specifier invalid in condition");
11925
11926 if (ds != ds_last)
11927 set_and_check_decl_spec_loc (decl_specs, ds, token);
11928
11929 /* Constructors are a special case. The `S' in `S()' is not a
11930 decl-specifier; it is the beginning of the declarator. */
11931 constructor_p
11932 = (!found_decl_spec
11933 && constructor_possible_p
11934 && (cp_parser_constructor_declarator_p
11935 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11936
11937 /* If we don't have a DECL_SPEC yet, then we must be looking at
11938 a type-specifier. */
11939 if (!found_decl_spec && !constructor_p)
11940 {
11941 int decl_spec_declares_class_or_enum;
11942 bool is_cv_qualifier;
11943 tree type_spec;
11944
11945 type_spec
11946 = cp_parser_type_specifier (parser, flags,
11947 decl_specs,
11948 /*is_declaration=*/true,
11949 &decl_spec_declares_class_or_enum,
11950 &is_cv_qualifier);
11951 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11952
11953 /* If this type-specifier referenced a user-defined type
11954 (a typedef, class-name, etc.), then we can't allow any
11955 more such type-specifiers henceforth.
11956
11957 [dcl.spec]
11958
11959 The longest sequence of decl-specifiers that could
11960 possibly be a type name is taken as the
11961 decl-specifier-seq of a declaration. The sequence shall
11962 be self-consistent as described below.
11963
11964 [dcl.type]
11965
11966 As a general rule, at most one type-specifier is allowed
11967 in the complete decl-specifier-seq of a declaration. The
11968 only exceptions are the following:
11969
11970 -- const or volatile can be combined with any other
11971 type-specifier.
11972
11973 -- signed or unsigned can be combined with char, long,
11974 short, or int.
11975
11976 -- ..
11977
11978 Example:
11979
11980 typedef char* Pc;
11981 void g (const int Pc);
11982
11983 Here, Pc is *not* part of the decl-specifier seq; it's
11984 the declarator. Therefore, once we see a type-specifier
11985 (other than a cv-qualifier), we forbid any additional
11986 user-defined types. We *do* still allow things like `int
11987 int' to be considered a decl-specifier-seq, and issue the
11988 error message later. */
11989 if (type_spec && !is_cv_qualifier)
11990 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11991 /* A constructor declarator cannot follow a type-specifier. */
11992 if (type_spec)
11993 {
11994 constructor_possible_p = false;
11995 found_decl_spec = true;
11996 if (!is_cv_qualifier)
11997 decl_specs->any_type_specifiers_p = true;
11998 }
11999 }
12000
12001 /* If we still do not have a DECL_SPEC, then there are no more
12002 decl-specifiers. */
12003 if (!found_decl_spec)
12004 break;
12005
12006 decl_specs->any_specifiers_p = true;
12007 /* After we see one decl-specifier, further decl-specifiers are
12008 always optional. */
12009 flags |= CP_PARSER_FLAGS_OPTIONAL;
12010 }
12011
12012 /* Don't allow a friend specifier with a class definition. */
12013 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12014 && (*declares_class_or_enum & 2))
12015 error_at (decl_specs->locations[ds_friend],
12016 "class definition may not be declared a friend");
12017 }
12018
12019 /* Parse an (optional) storage-class-specifier.
12020
12021 storage-class-specifier:
12022 auto
12023 register
12024 static
12025 extern
12026 mutable
12027
12028 GNU Extension:
12029
12030 storage-class-specifier:
12031 thread
12032
12033 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12034
12035 static tree
12036 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12037 {
12038 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12039 {
12040 case RID_AUTO:
12041 if (cxx_dialect != cxx98)
12042 return NULL_TREE;
12043 /* Fall through for C++98. */
12044
12045 case RID_REGISTER:
12046 case RID_STATIC:
12047 case RID_EXTERN:
12048 case RID_MUTABLE:
12049 case RID_THREAD:
12050 /* Consume the token. */
12051 return cp_lexer_consume_token (parser->lexer)->u.value;
12052
12053 default:
12054 return NULL_TREE;
12055 }
12056 }
12057
12058 /* Parse an (optional) function-specifier.
12059
12060 function-specifier:
12061 inline
12062 virtual
12063 explicit
12064
12065 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12066 Updates DECL_SPECS, if it is non-NULL. */
12067
12068 static tree
12069 cp_parser_function_specifier_opt (cp_parser* parser,
12070 cp_decl_specifier_seq *decl_specs)
12071 {
12072 cp_token *token = cp_lexer_peek_token (parser->lexer);
12073 switch (token->keyword)
12074 {
12075 case RID_INLINE:
12076 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12077 break;
12078
12079 case RID_VIRTUAL:
12080 /* 14.5.2.3 [temp.mem]
12081
12082 A member function template shall not be virtual. */
12083 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12084 error_at (token->location, "templates may not be %<virtual%>");
12085 else
12086 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12087 break;
12088
12089 case RID_EXPLICIT:
12090 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12091 break;
12092
12093 default:
12094 return NULL_TREE;
12095 }
12096
12097 /* Consume the token. */
12098 return cp_lexer_consume_token (parser->lexer)->u.value;
12099 }
12100
12101 /* Parse a linkage-specification.
12102
12103 linkage-specification:
12104 extern string-literal { declaration-seq [opt] }
12105 extern string-literal declaration */
12106
12107 static void
12108 cp_parser_linkage_specification (cp_parser* parser)
12109 {
12110 tree linkage;
12111
12112 /* Look for the `extern' keyword. */
12113 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12114
12115 /* Look for the string-literal. */
12116 linkage = cp_parser_string_literal (parser, false, false);
12117
12118 /* Transform the literal into an identifier. If the literal is a
12119 wide-character string, or contains embedded NULs, then we can't
12120 handle it as the user wants. */
12121 if (strlen (TREE_STRING_POINTER (linkage))
12122 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12123 {
12124 cp_parser_error (parser, "invalid linkage-specification");
12125 /* Assume C++ linkage. */
12126 linkage = lang_name_cplusplus;
12127 }
12128 else
12129 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12130
12131 /* We're now using the new linkage. */
12132 push_lang_context (linkage);
12133
12134 /* If the next token is a `{', then we're using the first
12135 production. */
12136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12137 {
12138 cp_ensure_no_omp_declare_simd (parser);
12139
12140 /* Consume the `{' token. */
12141 cp_lexer_consume_token (parser->lexer);
12142 /* Parse the declarations. */
12143 cp_parser_declaration_seq_opt (parser);
12144 /* Look for the closing `}'. */
12145 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12146 }
12147 /* Otherwise, there's just one declaration. */
12148 else
12149 {
12150 bool saved_in_unbraced_linkage_specification_p;
12151
12152 saved_in_unbraced_linkage_specification_p
12153 = parser->in_unbraced_linkage_specification_p;
12154 parser->in_unbraced_linkage_specification_p = true;
12155 cp_parser_declaration (parser);
12156 parser->in_unbraced_linkage_specification_p
12157 = saved_in_unbraced_linkage_specification_p;
12158 }
12159
12160 /* We're done with the linkage-specification. */
12161 pop_lang_context ();
12162 }
12163
12164 /* Parse a static_assert-declaration.
12165
12166 static_assert-declaration:
12167 static_assert ( constant-expression , string-literal ) ;
12168
12169 If MEMBER_P, this static_assert is a class member. */
12170
12171 static void
12172 cp_parser_static_assert(cp_parser *parser, bool member_p)
12173 {
12174 tree condition;
12175 tree message;
12176 cp_token *token;
12177 location_t saved_loc;
12178 bool dummy;
12179
12180 /* Peek at the `static_assert' token so we can keep track of exactly
12181 where the static assertion started. */
12182 token = cp_lexer_peek_token (parser->lexer);
12183 saved_loc = token->location;
12184
12185 /* Look for the `static_assert' keyword. */
12186 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12187 RT_STATIC_ASSERT))
12188 return;
12189
12190 /* We know we are in a static assertion; commit to any tentative
12191 parse. */
12192 if (cp_parser_parsing_tentatively (parser))
12193 cp_parser_commit_to_tentative_parse (parser);
12194
12195 /* Parse the `(' starting the static assertion condition. */
12196 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12197
12198 /* Parse the constant-expression. Allow a non-constant expression
12199 here in order to give better diagnostics in finish_static_assert. */
12200 condition =
12201 cp_parser_constant_expression (parser,
12202 /*allow_non_constant_p=*/true,
12203 /*non_constant_p=*/&dummy);
12204
12205 /* Parse the separating `,'. */
12206 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12207
12208 /* Parse the string-literal message. */
12209 message = cp_parser_string_literal (parser,
12210 /*translate=*/false,
12211 /*wide_ok=*/true);
12212
12213 /* A `)' completes the static assertion. */
12214 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12215 cp_parser_skip_to_closing_parenthesis (parser,
12216 /*recovering=*/true,
12217 /*or_comma=*/false,
12218 /*consume_paren=*/true);
12219
12220 /* A semicolon terminates the declaration. */
12221 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12222
12223 /* Complete the static assertion, which may mean either processing
12224 the static assert now or saving it for template instantiation. */
12225 finish_static_assert (condition, message, saved_loc, member_p);
12226 }
12227
12228 /* Parse the expression in decltype ( expression ). */
12229
12230 static tree
12231 cp_parser_decltype_expr (cp_parser *parser,
12232 bool &id_expression_or_member_access_p)
12233 {
12234 cp_token *id_expr_start_token;
12235 tree expr;
12236
12237 /* First, try parsing an id-expression. */
12238 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12239 cp_parser_parse_tentatively (parser);
12240 expr = cp_parser_id_expression (parser,
12241 /*template_keyword_p=*/false,
12242 /*check_dependency_p=*/true,
12243 /*template_p=*/NULL,
12244 /*declarator_p=*/false,
12245 /*optional_p=*/false);
12246
12247 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12248 {
12249 bool non_integral_constant_expression_p = false;
12250 tree id_expression = expr;
12251 cp_id_kind idk;
12252 const char *error_msg;
12253
12254 if (identifier_p (expr))
12255 /* Lookup the name we got back from the id-expression. */
12256 expr = cp_parser_lookup_name_simple (parser, expr,
12257 id_expr_start_token->location);
12258
12259 if (expr
12260 && expr != error_mark_node
12261 && TREE_CODE (expr) != TYPE_DECL
12262 && (TREE_CODE (expr) != BIT_NOT_EXPR
12263 || !TYPE_P (TREE_OPERAND (expr, 0)))
12264 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12265 {
12266 /* Complete lookup of the id-expression. */
12267 expr = (finish_id_expression
12268 (id_expression, expr, parser->scope, &idk,
12269 /*integral_constant_expression_p=*/false,
12270 /*allow_non_integral_constant_expression_p=*/true,
12271 &non_integral_constant_expression_p,
12272 /*template_p=*/false,
12273 /*done=*/true,
12274 /*address_p=*/false,
12275 /*template_arg_p=*/false,
12276 &error_msg,
12277 id_expr_start_token->location));
12278
12279 if (expr == error_mark_node)
12280 /* We found an id-expression, but it was something that we
12281 should not have found. This is an error, not something
12282 we can recover from, so note that we found an
12283 id-expression and we'll recover as gracefully as
12284 possible. */
12285 id_expression_or_member_access_p = true;
12286 }
12287
12288 if (expr
12289 && expr != error_mark_node
12290 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12291 /* We have an id-expression. */
12292 id_expression_or_member_access_p = true;
12293 }
12294
12295 if (!id_expression_or_member_access_p)
12296 {
12297 /* Abort the id-expression parse. */
12298 cp_parser_abort_tentative_parse (parser);
12299
12300 /* Parsing tentatively, again. */
12301 cp_parser_parse_tentatively (parser);
12302
12303 /* Parse a class member access. */
12304 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12305 /*cast_p=*/false, /*decltype*/true,
12306 /*member_access_only_p=*/true, NULL);
12307
12308 if (expr
12309 && expr != error_mark_node
12310 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12311 /* We have an id-expression. */
12312 id_expression_or_member_access_p = true;
12313 }
12314
12315 if (id_expression_or_member_access_p)
12316 /* We have parsed the complete id-expression or member access. */
12317 cp_parser_parse_definitely (parser);
12318 else
12319 {
12320 /* Abort our attempt to parse an id-expression or member access
12321 expression. */
12322 cp_parser_abort_tentative_parse (parser);
12323
12324 /* Parse a full expression. */
12325 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12326 /*decltype_p=*/true);
12327 }
12328
12329 return expr;
12330 }
12331
12332 /* Parse a `decltype' type. Returns the type.
12333
12334 simple-type-specifier:
12335 decltype ( expression )
12336 C++14 proposal:
12337 decltype ( auto ) */
12338
12339 static tree
12340 cp_parser_decltype (cp_parser *parser)
12341 {
12342 tree expr;
12343 bool id_expression_or_member_access_p = false;
12344 const char *saved_message;
12345 bool saved_integral_constant_expression_p;
12346 bool saved_non_integral_constant_expression_p;
12347 bool saved_greater_than_is_operator_p;
12348 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12349
12350 if (start_token->type == CPP_DECLTYPE)
12351 {
12352 /* Already parsed. */
12353 cp_lexer_consume_token (parser->lexer);
12354 return start_token->u.value;
12355 }
12356
12357 /* Look for the `decltype' token. */
12358 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12359 return error_mark_node;
12360
12361 /* Parse the opening `('. */
12362 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12363 return error_mark_node;
12364
12365 /* decltype (auto) */
12366 if (cxx_dialect >= cxx14
12367 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12368 {
12369 cp_lexer_consume_token (parser->lexer);
12370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12371 return error_mark_node;
12372 expr = make_decltype_auto ();
12373 AUTO_IS_DECLTYPE (expr) = true;
12374 goto rewrite;
12375 }
12376
12377 /* Types cannot be defined in a `decltype' expression. Save away the
12378 old message. */
12379 saved_message = parser->type_definition_forbidden_message;
12380
12381 /* And create the new one. */
12382 parser->type_definition_forbidden_message
12383 = G_("types may not be defined in %<decltype%> expressions");
12384
12385 /* The restrictions on constant-expressions do not apply inside
12386 decltype expressions. */
12387 saved_integral_constant_expression_p
12388 = parser->integral_constant_expression_p;
12389 saved_non_integral_constant_expression_p
12390 = parser->non_integral_constant_expression_p;
12391 parser->integral_constant_expression_p = false;
12392
12393 /* Within a parenthesized expression, a `>' token is always
12394 the greater-than operator. */
12395 saved_greater_than_is_operator_p
12396 = parser->greater_than_is_operator_p;
12397 parser->greater_than_is_operator_p = true;
12398
12399 /* Do not actually evaluate the expression. */
12400 ++cp_unevaluated_operand;
12401
12402 /* Do not warn about problems with the expression. */
12403 ++c_inhibit_evaluation_warnings;
12404
12405 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12406
12407 /* Go back to evaluating expressions. */
12408 --cp_unevaluated_operand;
12409 --c_inhibit_evaluation_warnings;
12410
12411 /* The `>' token might be the end of a template-id or
12412 template-parameter-list now. */
12413 parser->greater_than_is_operator_p
12414 = saved_greater_than_is_operator_p;
12415
12416 /* Restore the old message and the integral constant expression
12417 flags. */
12418 parser->type_definition_forbidden_message = saved_message;
12419 parser->integral_constant_expression_p
12420 = saved_integral_constant_expression_p;
12421 parser->non_integral_constant_expression_p
12422 = saved_non_integral_constant_expression_p;
12423
12424 /* Parse to the closing `)'. */
12425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12426 {
12427 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12428 /*consume_paren=*/true);
12429 return error_mark_node;
12430 }
12431
12432 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12433 tf_warning_or_error);
12434
12435 rewrite:
12436 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12437 it again. */
12438 start_token->type = CPP_DECLTYPE;
12439 start_token->u.value = expr;
12440 start_token->keyword = RID_MAX;
12441 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12442
12443 return expr;
12444 }
12445
12446 /* Special member functions [gram.special] */
12447
12448 /* Parse a conversion-function-id.
12449
12450 conversion-function-id:
12451 operator conversion-type-id
12452
12453 Returns an IDENTIFIER_NODE representing the operator. */
12454
12455 static tree
12456 cp_parser_conversion_function_id (cp_parser* parser)
12457 {
12458 tree type;
12459 tree saved_scope;
12460 tree saved_qualifying_scope;
12461 tree saved_object_scope;
12462 tree pushed_scope = NULL_TREE;
12463
12464 /* Look for the `operator' token. */
12465 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12466 return error_mark_node;
12467 /* When we parse the conversion-type-id, the current scope will be
12468 reset. However, we need that information in able to look up the
12469 conversion function later, so we save it here. */
12470 saved_scope = parser->scope;
12471 saved_qualifying_scope = parser->qualifying_scope;
12472 saved_object_scope = parser->object_scope;
12473 /* We must enter the scope of the class so that the names of
12474 entities declared within the class are available in the
12475 conversion-type-id. For example, consider:
12476
12477 struct S {
12478 typedef int I;
12479 operator I();
12480 };
12481
12482 S::operator I() { ... }
12483
12484 In order to see that `I' is a type-name in the definition, we
12485 must be in the scope of `S'. */
12486 if (saved_scope)
12487 pushed_scope = push_scope (saved_scope);
12488 /* Parse the conversion-type-id. */
12489 type = cp_parser_conversion_type_id (parser);
12490 /* Leave the scope of the class, if any. */
12491 if (pushed_scope)
12492 pop_scope (pushed_scope);
12493 /* Restore the saved scope. */
12494 parser->scope = saved_scope;
12495 parser->qualifying_scope = saved_qualifying_scope;
12496 parser->object_scope = saved_object_scope;
12497 /* If the TYPE is invalid, indicate failure. */
12498 if (type == error_mark_node)
12499 return error_mark_node;
12500 return mangle_conv_op_name_for_type (type);
12501 }
12502
12503 /* Parse a conversion-type-id:
12504
12505 conversion-type-id:
12506 type-specifier-seq conversion-declarator [opt]
12507
12508 Returns the TYPE specified. */
12509
12510 static tree
12511 cp_parser_conversion_type_id (cp_parser* parser)
12512 {
12513 tree attributes;
12514 cp_decl_specifier_seq type_specifiers;
12515 cp_declarator *declarator;
12516 tree type_specified;
12517 const char *saved_message;
12518
12519 /* Parse the attributes. */
12520 attributes = cp_parser_attributes_opt (parser);
12521
12522 saved_message = parser->type_definition_forbidden_message;
12523 parser->type_definition_forbidden_message
12524 = G_("types may not be defined in a conversion-type-id");
12525
12526 /* Parse the type-specifiers. */
12527 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12528 /*is_trailing_return=*/false,
12529 &type_specifiers);
12530
12531 parser->type_definition_forbidden_message = saved_message;
12532
12533 /* If that didn't work, stop. */
12534 if (type_specifiers.type == error_mark_node)
12535 return error_mark_node;
12536 /* Parse the conversion-declarator. */
12537 declarator = cp_parser_conversion_declarator_opt (parser);
12538
12539 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12540 /*initialized=*/0, &attributes);
12541 if (attributes)
12542 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12543
12544 /* Don't give this error when parsing tentatively. This happens to
12545 work because we always parse this definitively once. */
12546 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12547 && type_uses_auto (type_specified))
12548 {
12549 if (cxx_dialect < cxx14)
12550 {
12551 error ("invalid use of %<auto%> in conversion operator");
12552 return error_mark_node;
12553 }
12554 else if (template_parm_scope_p ())
12555 warning (0, "use of %<auto%> in member template "
12556 "conversion operator can never be deduced");
12557 }
12558
12559 return type_specified;
12560 }
12561
12562 /* Parse an (optional) conversion-declarator.
12563
12564 conversion-declarator:
12565 ptr-operator conversion-declarator [opt]
12566
12567 */
12568
12569 static cp_declarator *
12570 cp_parser_conversion_declarator_opt (cp_parser* parser)
12571 {
12572 enum tree_code code;
12573 tree class_type, std_attributes = NULL_TREE;
12574 cp_cv_quals cv_quals;
12575
12576 /* We don't know if there's a ptr-operator next, or not. */
12577 cp_parser_parse_tentatively (parser);
12578 /* Try the ptr-operator. */
12579 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12580 &std_attributes);
12581 /* If it worked, look for more conversion-declarators. */
12582 if (cp_parser_parse_definitely (parser))
12583 {
12584 cp_declarator *declarator;
12585
12586 /* Parse another optional declarator. */
12587 declarator = cp_parser_conversion_declarator_opt (parser);
12588
12589 declarator = cp_parser_make_indirect_declarator
12590 (code, class_type, cv_quals, declarator, std_attributes);
12591
12592 return declarator;
12593 }
12594
12595 return NULL;
12596 }
12597
12598 /* Parse an (optional) ctor-initializer.
12599
12600 ctor-initializer:
12601 : mem-initializer-list
12602
12603 Returns TRUE iff the ctor-initializer was actually present. */
12604
12605 static bool
12606 cp_parser_ctor_initializer_opt (cp_parser* parser)
12607 {
12608 /* If the next token is not a `:', then there is no
12609 ctor-initializer. */
12610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12611 {
12612 /* Do default initialization of any bases and members. */
12613 if (DECL_CONSTRUCTOR_P (current_function_decl))
12614 finish_mem_initializers (NULL_TREE);
12615
12616 return false;
12617 }
12618
12619 /* Consume the `:' token. */
12620 cp_lexer_consume_token (parser->lexer);
12621 /* And the mem-initializer-list. */
12622 cp_parser_mem_initializer_list (parser);
12623
12624 return true;
12625 }
12626
12627 /* Parse a mem-initializer-list.
12628
12629 mem-initializer-list:
12630 mem-initializer ... [opt]
12631 mem-initializer ... [opt] , mem-initializer-list */
12632
12633 static void
12634 cp_parser_mem_initializer_list (cp_parser* parser)
12635 {
12636 tree mem_initializer_list = NULL_TREE;
12637 tree target_ctor = error_mark_node;
12638 cp_token *token = cp_lexer_peek_token (parser->lexer);
12639
12640 /* Let the semantic analysis code know that we are starting the
12641 mem-initializer-list. */
12642 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12643 error_at (token->location,
12644 "only constructors take member initializers");
12645
12646 /* Loop through the list. */
12647 while (true)
12648 {
12649 tree mem_initializer;
12650
12651 token = cp_lexer_peek_token (parser->lexer);
12652 /* Parse the mem-initializer. */
12653 mem_initializer = cp_parser_mem_initializer (parser);
12654 /* If the next token is a `...', we're expanding member initializers. */
12655 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12656 {
12657 /* Consume the `...'. */
12658 cp_lexer_consume_token (parser->lexer);
12659
12660 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12661 can be expanded but members cannot. */
12662 if (mem_initializer != error_mark_node
12663 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12664 {
12665 error_at (token->location,
12666 "cannot expand initializer for member %<%D%>",
12667 TREE_PURPOSE (mem_initializer));
12668 mem_initializer = error_mark_node;
12669 }
12670
12671 /* Construct the pack expansion type. */
12672 if (mem_initializer != error_mark_node)
12673 mem_initializer = make_pack_expansion (mem_initializer);
12674 }
12675 if (target_ctor != error_mark_node
12676 && mem_initializer != error_mark_node)
12677 {
12678 error ("mem-initializer for %qD follows constructor delegation",
12679 TREE_PURPOSE (mem_initializer));
12680 mem_initializer = error_mark_node;
12681 }
12682 /* Look for a target constructor. */
12683 if (mem_initializer != error_mark_node
12684 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12685 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12686 {
12687 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12688 if (mem_initializer_list)
12689 {
12690 error ("constructor delegation follows mem-initializer for %qD",
12691 TREE_PURPOSE (mem_initializer_list));
12692 mem_initializer = error_mark_node;
12693 }
12694 target_ctor = mem_initializer;
12695 }
12696 /* Add it to the list, unless it was erroneous. */
12697 if (mem_initializer != error_mark_node)
12698 {
12699 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12700 mem_initializer_list = mem_initializer;
12701 }
12702 /* If the next token is not a `,', we're done. */
12703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12704 break;
12705 /* Consume the `,' token. */
12706 cp_lexer_consume_token (parser->lexer);
12707 }
12708
12709 /* Perform semantic analysis. */
12710 if (DECL_CONSTRUCTOR_P (current_function_decl))
12711 finish_mem_initializers (mem_initializer_list);
12712 }
12713
12714 /* Parse a mem-initializer.
12715
12716 mem-initializer:
12717 mem-initializer-id ( expression-list [opt] )
12718 mem-initializer-id braced-init-list
12719
12720 GNU extension:
12721
12722 mem-initializer:
12723 ( expression-list [opt] )
12724
12725 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12726 class) or FIELD_DECL (for a non-static data member) to initialize;
12727 the TREE_VALUE is the expression-list. An empty initialization
12728 list is represented by void_list_node. */
12729
12730 static tree
12731 cp_parser_mem_initializer (cp_parser* parser)
12732 {
12733 tree mem_initializer_id;
12734 tree expression_list;
12735 tree member;
12736 cp_token *token = cp_lexer_peek_token (parser->lexer);
12737
12738 /* Find out what is being initialized. */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12740 {
12741 permerror (token->location,
12742 "anachronistic old-style base class initializer");
12743 mem_initializer_id = NULL_TREE;
12744 }
12745 else
12746 {
12747 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12748 if (mem_initializer_id == error_mark_node)
12749 return mem_initializer_id;
12750 }
12751 member = expand_member_init (mem_initializer_id);
12752 if (member && !DECL_P (member))
12753 in_base_initializer = 1;
12754
12755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12756 {
12757 bool expr_non_constant_p;
12758 cp_lexer_set_source_position (parser->lexer);
12759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12760 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12761 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12762 expression_list = build_tree_list (NULL_TREE, expression_list);
12763 }
12764 else
12765 {
12766 vec<tree, va_gc> *vec;
12767 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12768 /*cast_p=*/false,
12769 /*allow_expansion_p=*/true,
12770 /*non_constant_p=*/NULL);
12771 if (vec == NULL)
12772 return error_mark_node;
12773 expression_list = build_tree_list_vec (vec);
12774 release_tree_vector (vec);
12775 }
12776
12777 if (expression_list == error_mark_node)
12778 return error_mark_node;
12779 if (!expression_list)
12780 expression_list = void_type_node;
12781
12782 in_base_initializer = 0;
12783
12784 return member ? build_tree_list (member, expression_list) : error_mark_node;
12785 }
12786
12787 /* Parse a mem-initializer-id.
12788
12789 mem-initializer-id:
12790 :: [opt] nested-name-specifier [opt] class-name
12791 identifier
12792
12793 Returns a TYPE indicating the class to be initializer for the first
12794 production. Returns an IDENTIFIER_NODE indicating the data member
12795 to be initialized for the second production. */
12796
12797 static tree
12798 cp_parser_mem_initializer_id (cp_parser* parser)
12799 {
12800 bool global_scope_p;
12801 bool nested_name_specifier_p;
12802 bool template_p = false;
12803 tree id;
12804
12805 cp_token *token = cp_lexer_peek_token (parser->lexer);
12806
12807 /* `typename' is not allowed in this context ([temp.res]). */
12808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12809 {
12810 error_at (token->location,
12811 "keyword %<typename%> not allowed in this context (a qualified "
12812 "member initializer is implicitly a type)");
12813 cp_lexer_consume_token (parser->lexer);
12814 }
12815 /* Look for the optional `::' operator. */
12816 global_scope_p
12817 = (cp_parser_global_scope_opt (parser,
12818 /*current_scope_valid_p=*/false)
12819 != NULL_TREE);
12820 /* Look for the optional nested-name-specifier. The simplest way to
12821 implement:
12822
12823 [temp.res]
12824
12825 The keyword `typename' is not permitted in a base-specifier or
12826 mem-initializer; in these contexts a qualified name that
12827 depends on a template-parameter is implicitly assumed to be a
12828 type name.
12829
12830 is to assume that we have seen the `typename' keyword at this
12831 point. */
12832 nested_name_specifier_p
12833 = (cp_parser_nested_name_specifier_opt (parser,
12834 /*typename_keyword_p=*/true,
12835 /*check_dependency_p=*/true,
12836 /*type_p=*/true,
12837 /*is_declaration=*/true)
12838 != NULL_TREE);
12839 if (nested_name_specifier_p)
12840 template_p = cp_parser_optional_template_keyword (parser);
12841 /* If there is a `::' operator or a nested-name-specifier, then we
12842 are definitely looking for a class-name. */
12843 if (global_scope_p || nested_name_specifier_p)
12844 return cp_parser_class_name (parser,
12845 /*typename_keyword_p=*/true,
12846 /*template_keyword_p=*/template_p,
12847 typename_type,
12848 /*check_dependency_p=*/true,
12849 /*class_head_p=*/false,
12850 /*is_declaration=*/true);
12851 /* Otherwise, we could also be looking for an ordinary identifier. */
12852 cp_parser_parse_tentatively (parser);
12853 /* Try a class-name. */
12854 id = cp_parser_class_name (parser,
12855 /*typename_keyword_p=*/true,
12856 /*template_keyword_p=*/false,
12857 none_type,
12858 /*check_dependency_p=*/true,
12859 /*class_head_p=*/false,
12860 /*is_declaration=*/true);
12861 /* If we found one, we're done. */
12862 if (cp_parser_parse_definitely (parser))
12863 return id;
12864 /* Otherwise, look for an ordinary identifier. */
12865 return cp_parser_identifier (parser);
12866 }
12867
12868 /* Overloading [gram.over] */
12869
12870 /* Parse an operator-function-id.
12871
12872 operator-function-id:
12873 operator operator
12874
12875 Returns an IDENTIFIER_NODE for the operator which is a
12876 human-readable spelling of the identifier, e.g., `operator +'. */
12877
12878 static tree
12879 cp_parser_operator_function_id (cp_parser* parser)
12880 {
12881 /* Look for the `operator' keyword. */
12882 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12883 return error_mark_node;
12884 /* And then the name of the operator itself. */
12885 return cp_parser_operator (parser);
12886 }
12887
12888 /* Return an identifier node for a user-defined literal operator.
12889 The suffix identifier is chained to the operator name identifier. */
12890
12891 static tree
12892 cp_literal_operator_id (const char* name)
12893 {
12894 tree identifier;
12895 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12896 + strlen (name) + 10);
12897 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12898 identifier = get_identifier (buffer);
12899
12900 return identifier;
12901 }
12902
12903 /* Parse an operator.
12904
12905 operator:
12906 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12907 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12908 || ++ -- , ->* -> () []
12909
12910 GNU Extensions:
12911
12912 operator:
12913 <? >? <?= >?=
12914
12915 Returns an IDENTIFIER_NODE for the operator which is a
12916 human-readable spelling of the identifier, e.g., `operator +'. */
12917
12918 static tree
12919 cp_parser_operator (cp_parser* parser)
12920 {
12921 tree id = NULL_TREE;
12922 cp_token *token;
12923 bool utf8 = false;
12924
12925 /* Peek at the next token. */
12926 token = cp_lexer_peek_token (parser->lexer);
12927 /* Figure out which operator we have. */
12928 switch (token->type)
12929 {
12930 case CPP_KEYWORD:
12931 {
12932 enum tree_code op;
12933
12934 /* The keyword should be either `new' or `delete'. */
12935 if (token->keyword == RID_NEW)
12936 op = NEW_EXPR;
12937 else if (token->keyword == RID_DELETE)
12938 op = DELETE_EXPR;
12939 else
12940 break;
12941
12942 /* Consume the `new' or `delete' token. */
12943 cp_lexer_consume_token (parser->lexer);
12944
12945 /* Peek at the next token. */
12946 token = cp_lexer_peek_token (parser->lexer);
12947 /* If it's a `[' token then this is the array variant of the
12948 operator. */
12949 if (token->type == CPP_OPEN_SQUARE)
12950 {
12951 /* Consume the `[' token. */
12952 cp_lexer_consume_token (parser->lexer);
12953 /* Look for the `]' token. */
12954 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12955 id = ansi_opname (op == NEW_EXPR
12956 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12957 }
12958 /* Otherwise, we have the non-array variant. */
12959 else
12960 id = ansi_opname (op);
12961
12962 return id;
12963 }
12964
12965 case CPP_PLUS:
12966 id = ansi_opname (PLUS_EXPR);
12967 break;
12968
12969 case CPP_MINUS:
12970 id = ansi_opname (MINUS_EXPR);
12971 break;
12972
12973 case CPP_MULT:
12974 id = ansi_opname (MULT_EXPR);
12975 break;
12976
12977 case CPP_DIV:
12978 id = ansi_opname (TRUNC_DIV_EXPR);
12979 break;
12980
12981 case CPP_MOD:
12982 id = ansi_opname (TRUNC_MOD_EXPR);
12983 break;
12984
12985 case CPP_XOR:
12986 id = ansi_opname (BIT_XOR_EXPR);
12987 break;
12988
12989 case CPP_AND:
12990 id = ansi_opname (BIT_AND_EXPR);
12991 break;
12992
12993 case CPP_OR:
12994 id = ansi_opname (BIT_IOR_EXPR);
12995 break;
12996
12997 case CPP_COMPL:
12998 id = ansi_opname (BIT_NOT_EXPR);
12999 break;
13000
13001 case CPP_NOT:
13002 id = ansi_opname (TRUTH_NOT_EXPR);
13003 break;
13004
13005 case CPP_EQ:
13006 id = ansi_assopname (NOP_EXPR);
13007 break;
13008
13009 case CPP_LESS:
13010 id = ansi_opname (LT_EXPR);
13011 break;
13012
13013 case CPP_GREATER:
13014 id = ansi_opname (GT_EXPR);
13015 break;
13016
13017 case CPP_PLUS_EQ:
13018 id = ansi_assopname (PLUS_EXPR);
13019 break;
13020
13021 case CPP_MINUS_EQ:
13022 id = ansi_assopname (MINUS_EXPR);
13023 break;
13024
13025 case CPP_MULT_EQ:
13026 id = ansi_assopname (MULT_EXPR);
13027 break;
13028
13029 case CPP_DIV_EQ:
13030 id = ansi_assopname (TRUNC_DIV_EXPR);
13031 break;
13032
13033 case CPP_MOD_EQ:
13034 id = ansi_assopname (TRUNC_MOD_EXPR);
13035 break;
13036
13037 case CPP_XOR_EQ:
13038 id = ansi_assopname (BIT_XOR_EXPR);
13039 break;
13040
13041 case CPP_AND_EQ:
13042 id = ansi_assopname (BIT_AND_EXPR);
13043 break;
13044
13045 case CPP_OR_EQ:
13046 id = ansi_assopname (BIT_IOR_EXPR);
13047 break;
13048
13049 case CPP_LSHIFT:
13050 id = ansi_opname (LSHIFT_EXPR);
13051 break;
13052
13053 case CPP_RSHIFT:
13054 id = ansi_opname (RSHIFT_EXPR);
13055 break;
13056
13057 case CPP_LSHIFT_EQ:
13058 id = ansi_assopname (LSHIFT_EXPR);
13059 break;
13060
13061 case CPP_RSHIFT_EQ:
13062 id = ansi_assopname (RSHIFT_EXPR);
13063 break;
13064
13065 case CPP_EQ_EQ:
13066 id = ansi_opname (EQ_EXPR);
13067 break;
13068
13069 case CPP_NOT_EQ:
13070 id = ansi_opname (NE_EXPR);
13071 break;
13072
13073 case CPP_LESS_EQ:
13074 id = ansi_opname (LE_EXPR);
13075 break;
13076
13077 case CPP_GREATER_EQ:
13078 id = ansi_opname (GE_EXPR);
13079 break;
13080
13081 case CPP_AND_AND:
13082 id = ansi_opname (TRUTH_ANDIF_EXPR);
13083 break;
13084
13085 case CPP_OR_OR:
13086 id = ansi_opname (TRUTH_ORIF_EXPR);
13087 break;
13088
13089 case CPP_PLUS_PLUS:
13090 id = ansi_opname (POSTINCREMENT_EXPR);
13091 break;
13092
13093 case CPP_MINUS_MINUS:
13094 id = ansi_opname (PREDECREMENT_EXPR);
13095 break;
13096
13097 case CPP_COMMA:
13098 id = ansi_opname (COMPOUND_EXPR);
13099 break;
13100
13101 case CPP_DEREF_STAR:
13102 id = ansi_opname (MEMBER_REF);
13103 break;
13104
13105 case CPP_DEREF:
13106 id = ansi_opname (COMPONENT_REF);
13107 break;
13108
13109 case CPP_OPEN_PAREN:
13110 /* Consume the `('. */
13111 cp_lexer_consume_token (parser->lexer);
13112 /* Look for the matching `)'. */
13113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13114 return ansi_opname (CALL_EXPR);
13115
13116 case CPP_OPEN_SQUARE:
13117 /* Consume the `['. */
13118 cp_lexer_consume_token (parser->lexer);
13119 /* Look for the matching `]'. */
13120 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13121 return ansi_opname (ARRAY_REF);
13122
13123 case CPP_UTF8STRING:
13124 case CPP_UTF8STRING_USERDEF:
13125 utf8 = true;
13126 case CPP_STRING:
13127 case CPP_WSTRING:
13128 case CPP_STRING16:
13129 case CPP_STRING32:
13130 case CPP_STRING_USERDEF:
13131 case CPP_WSTRING_USERDEF:
13132 case CPP_STRING16_USERDEF:
13133 case CPP_STRING32_USERDEF:
13134 {
13135 tree str, string_tree;
13136 int sz, len;
13137
13138 if (cxx_dialect == cxx98)
13139 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13140
13141 /* Consume the string. */
13142 str = cp_parser_string_literal (parser, /*translate=*/true,
13143 /*wide_ok=*/true, /*lookup_udlit=*/false);
13144 if (str == error_mark_node)
13145 return error_mark_node;
13146 else if (TREE_CODE (str) == USERDEF_LITERAL)
13147 {
13148 string_tree = USERDEF_LITERAL_VALUE (str);
13149 id = USERDEF_LITERAL_SUFFIX_ID (str);
13150 }
13151 else
13152 {
13153 string_tree = str;
13154 /* Look for the suffix identifier. */
13155 token = cp_lexer_peek_token (parser->lexer);
13156 if (token->type == CPP_NAME)
13157 id = cp_parser_identifier (parser);
13158 else if (token->type == CPP_KEYWORD)
13159 {
13160 error ("unexpected keyword;"
13161 " remove space between quotes and suffix identifier");
13162 return error_mark_node;
13163 }
13164 else
13165 {
13166 error ("expected suffix identifier");
13167 return error_mark_node;
13168 }
13169 }
13170 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13171 (TREE_TYPE (TREE_TYPE (string_tree))));
13172 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13173 if (len != 0)
13174 {
13175 error ("expected empty string after %<operator%> keyword");
13176 return error_mark_node;
13177 }
13178 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13179 != char_type_node)
13180 {
13181 error ("invalid encoding prefix in literal operator");
13182 return error_mark_node;
13183 }
13184 if (id != error_mark_node)
13185 {
13186 const char *name = IDENTIFIER_POINTER (id);
13187 id = cp_literal_operator_id (name);
13188 }
13189 return id;
13190 }
13191
13192 default:
13193 /* Anything else is an error. */
13194 break;
13195 }
13196
13197 /* If we have selected an identifier, we need to consume the
13198 operator token. */
13199 if (id)
13200 cp_lexer_consume_token (parser->lexer);
13201 /* Otherwise, no valid operator name was present. */
13202 else
13203 {
13204 cp_parser_error (parser, "expected operator");
13205 id = error_mark_node;
13206 }
13207
13208 return id;
13209 }
13210
13211 /* Parse a template-declaration.
13212
13213 template-declaration:
13214 export [opt] template < template-parameter-list > declaration
13215
13216 If MEMBER_P is TRUE, this template-declaration occurs within a
13217 class-specifier.
13218
13219 The grammar rule given by the standard isn't correct. What
13220 is really meant is:
13221
13222 template-declaration:
13223 export [opt] template-parameter-list-seq
13224 decl-specifier-seq [opt] init-declarator [opt] ;
13225 export [opt] template-parameter-list-seq
13226 function-definition
13227
13228 template-parameter-list-seq:
13229 template-parameter-list-seq [opt]
13230 template < template-parameter-list > */
13231
13232 static void
13233 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13234 {
13235 /* Check for `export'. */
13236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13237 {
13238 /* Consume the `export' token. */
13239 cp_lexer_consume_token (parser->lexer);
13240 /* Warn that we do not support `export'. */
13241 warning (0, "keyword %<export%> not implemented, and will be ignored");
13242 }
13243
13244 cp_parser_template_declaration_after_export (parser, member_p);
13245 }
13246
13247 /* Parse a template-parameter-list.
13248
13249 template-parameter-list:
13250 template-parameter
13251 template-parameter-list , template-parameter
13252
13253 Returns a TREE_LIST. Each node represents a template parameter.
13254 The nodes are connected via their TREE_CHAINs. */
13255
13256 static tree
13257 cp_parser_template_parameter_list (cp_parser* parser)
13258 {
13259 tree parameter_list = NULL_TREE;
13260
13261 begin_template_parm_list ();
13262
13263 /* The loop below parses the template parms. We first need to know
13264 the total number of template parms to be able to compute proper
13265 canonical types of each dependent type. So after the loop, when
13266 we know the total number of template parms,
13267 end_template_parm_list computes the proper canonical types and
13268 fixes up the dependent types accordingly. */
13269 while (true)
13270 {
13271 tree parameter;
13272 bool is_non_type;
13273 bool is_parameter_pack;
13274 location_t parm_loc;
13275
13276 /* Parse the template-parameter. */
13277 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13278 parameter = cp_parser_template_parameter (parser,
13279 &is_non_type,
13280 &is_parameter_pack);
13281 /* Add it to the list. */
13282 if (parameter != error_mark_node)
13283 parameter_list = process_template_parm (parameter_list,
13284 parm_loc,
13285 parameter,
13286 is_non_type,
13287 is_parameter_pack);
13288 else
13289 {
13290 tree err_parm = build_tree_list (parameter, parameter);
13291 parameter_list = chainon (parameter_list, err_parm);
13292 }
13293
13294 /* If the next token is not a `,', we're done. */
13295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13296 break;
13297 /* Otherwise, consume the `,' token. */
13298 cp_lexer_consume_token (parser->lexer);
13299 }
13300
13301 return end_template_parm_list (parameter_list);
13302 }
13303
13304 /* Parse a template-parameter.
13305
13306 template-parameter:
13307 type-parameter
13308 parameter-declaration
13309
13310 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13311 the parameter. The TREE_PURPOSE is the default value, if any.
13312 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13313 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13314 set to true iff this parameter is a parameter pack. */
13315
13316 static tree
13317 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13318 bool *is_parameter_pack)
13319 {
13320 cp_token *token;
13321 cp_parameter_declarator *parameter_declarator;
13322 cp_declarator *id_declarator;
13323 tree parm;
13324
13325 /* Assume it is a type parameter or a template parameter. */
13326 *is_non_type = false;
13327 /* Assume it not a parameter pack. */
13328 *is_parameter_pack = false;
13329 /* Peek at the next token. */
13330 token = cp_lexer_peek_token (parser->lexer);
13331 /* If it is `class' or `template', we have a type-parameter. */
13332 if (token->keyword == RID_TEMPLATE)
13333 return cp_parser_type_parameter (parser, is_parameter_pack);
13334 /* If it is `class' or `typename' we do not know yet whether it is a
13335 type parameter or a non-type parameter. Consider:
13336
13337 template <typename T, typename T::X X> ...
13338
13339 or:
13340
13341 template <class C, class D*> ...
13342
13343 Here, the first parameter is a type parameter, and the second is
13344 a non-type parameter. We can tell by looking at the token after
13345 the identifier -- if it is a `,', `=', or `>' then we have a type
13346 parameter. */
13347 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13348 {
13349 /* Peek at the token after `class' or `typename'. */
13350 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13351 /* If it's an ellipsis, we have a template type parameter
13352 pack. */
13353 if (token->type == CPP_ELLIPSIS)
13354 return cp_parser_type_parameter (parser, is_parameter_pack);
13355 /* If it's an identifier, skip it. */
13356 if (token->type == CPP_NAME)
13357 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13358 /* Now, see if the token looks like the end of a template
13359 parameter. */
13360 if (token->type == CPP_COMMA
13361 || token->type == CPP_EQ
13362 || token->type == CPP_GREATER)
13363 return cp_parser_type_parameter (parser, is_parameter_pack);
13364 }
13365
13366 /* Otherwise, it is a non-type parameter.
13367
13368 [temp.param]
13369
13370 When parsing a default template-argument for a non-type
13371 template-parameter, the first non-nested `>' is taken as the end
13372 of the template parameter-list rather than a greater-than
13373 operator. */
13374 *is_non_type = true;
13375 parameter_declarator
13376 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13377 /*parenthesized_p=*/NULL);
13378
13379 if (!parameter_declarator)
13380 return error_mark_node;
13381
13382 /* If the parameter declaration is marked as a parameter pack, set
13383 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13384 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13385 grokdeclarator. */
13386 if (parameter_declarator->declarator
13387 && parameter_declarator->declarator->parameter_pack_p)
13388 {
13389 *is_parameter_pack = true;
13390 parameter_declarator->declarator->parameter_pack_p = false;
13391 }
13392
13393 if (parameter_declarator->default_argument)
13394 {
13395 /* Can happen in some cases of erroneous input (c++/34892). */
13396 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13397 /* Consume the `...' for better error recovery. */
13398 cp_lexer_consume_token (parser->lexer);
13399 }
13400 /* If the next token is an ellipsis, and we don't already have it
13401 marked as a parameter pack, then we have a parameter pack (that
13402 has no declarator). */
13403 else if (!*is_parameter_pack
13404 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13405 && (declarator_can_be_parameter_pack
13406 (parameter_declarator->declarator)))
13407 {
13408 /* Consume the `...'. */
13409 cp_lexer_consume_token (parser->lexer);
13410 maybe_warn_variadic_templates ();
13411
13412 *is_parameter_pack = true;
13413 }
13414 /* We might end up with a pack expansion as the type of the non-type
13415 template parameter, in which case this is a non-type template
13416 parameter pack. */
13417 else if (parameter_declarator->decl_specifiers.type
13418 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13419 {
13420 *is_parameter_pack = true;
13421 parameter_declarator->decl_specifiers.type =
13422 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13423 }
13424
13425 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13426 {
13427 /* Parameter packs cannot have default arguments. However, a
13428 user may try to do so, so we'll parse them and give an
13429 appropriate diagnostic here. */
13430
13431 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13432
13433 /* Find the name of the parameter pack. */
13434 id_declarator = parameter_declarator->declarator;
13435 while (id_declarator && id_declarator->kind != cdk_id)
13436 id_declarator = id_declarator->declarator;
13437
13438 if (id_declarator && id_declarator->kind == cdk_id)
13439 error_at (start_token->location,
13440 "template parameter pack %qD cannot have a default argument",
13441 id_declarator->u.id.unqualified_name);
13442 else
13443 error_at (start_token->location,
13444 "template parameter pack cannot have a default argument");
13445
13446 /* Parse the default argument, but throw away the result. */
13447 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13448 }
13449
13450 parm = grokdeclarator (parameter_declarator->declarator,
13451 &parameter_declarator->decl_specifiers,
13452 TPARM, /*initialized=*/0,
13453 /*attrlist=*/NULL);
13454 if (parm == error_mark_node)
13455 return error_mark_node;
13456
13457 return build_tree_list (parameter_declarator->default_argument, parm);
13458 }
13459
13460 /* Parse a type-parameter.
13461
13462 type-parameter:
13463 class identifier [opt]
13464 class identifier [opt] = type-id
13465 typename identifier [opt]
13466 typename identifier [opt] = type-id
13467 template < template-parameter-list > class identifier [opt]
13468 template < template-parameter-list > class identifier [opt]
13469 = id-expression
13470
13471 GNU Extension (variadic templates):
13472
13473 type-parameter:
13474 class ... identifier [opt]
13475 typename ... identifier [opt]
13476
13477 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13478 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13479 the declaration of the parameter.
13480
13481 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13482
13483 static tree
13484 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13485 {
13486 cp_token *token;
13487 tree parameter;
13488
13489 /* Look for a keyword to tell us what kind of parameter this is. */
13490 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13491 if (!token)
13492 return error_mark_node;
13493
13494 switch (token->keyword)
13495 {
13496 case RID_CLASS:
13497 case RID_TYPENAME:
13498 {
13499 tree identifier;
13500 tree default_argument;
13501
13502 /* If the next token is an ellipsis, we have a template
13503 argument pack. */
13504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13505 {
13506 /* Consume the `...' token. */
13507 cp_lexer_consume_token (parser->lexer);
13508 maybe_warn_variadic_templates ();
13509
13510 *is_parameter_pack = true;
13511 }
13512
13513 /* If the next token is an identifier, then it names the
13514 parameter. */
13515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13516 identifier = cp_parser_identifier (parser);
13517 else
13518 identifier = NULL_TREE;
13519
13520 /* Create the parameter. */
13521 parameter = finish_template_type_parm (class_type_node, identifier);
13522
13523 /* If the next token is an `=', we have a default argument. */
13524 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13525 {
13526 /* Consume the `=' token. */
13527 cp_lexer_consume_token (parser->lexer);
13528 /* Parse the default-argument. */
13529 push_deferring_access_checks (dk_no_deferred);
13530 default_argument = cp_parser_type_id (parser);
13531
13532 /* Template parameter packs cannot have default
13533 arguments. */
13534 if (*is_parameter_pack)
13535 {
13536 if (identifier)
13537 error_at (token->location,
13538 "template parameter pack %qD cannot have a "
13539 "default argument", identifier);
13540 else
13541 error_at (token->location,
13542 "template parameter packs cannot have "
13543 "default arguments");
13544 default_argument = NULL_TREE;
13545 }
13546 else if (check_for_bare_parameter_packs (default_argument))
13547 default_argument = error_mark_node;
13548 pop_deferring_access_checks ();
13549 }
13550 else
13551 default_argument = NULL_TREE;
13552
13553 /* Create the combined representation of the parameter and the
13554 default argument. */
13555 parameter = build_tree_list (default_argument, parameter);
13556 }
13557 break;
13558
13559 case RID_TEMPLATE:
13560 {
13561 tree identifier;
13562 tree default_argument;
13563
13564 /* Look for the `<'. */
13565 cp_parser_require (parser, CPP_LESS, RT_LESS);
13566 /* Parse the template-parameter-list. */
13567 cp_parser_template_parameter_list (parser);
13568 /* Look for the `>'. */
13569 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13570 /* Look for the `class' or 'typename' keywords. */
13571 cp_parser_type_parameter_key (parser);
13572 /* If the next token is an ellipsis, we have a template
13573 argument pack. */
13574 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13575 {
13576 /* Consume the `...' token. */
13577 cp_lexer_consume_token (parser->lexer);
13578 maybe_warn_variadic_templates ();
13579
13580 *is_parameter_pack = true;
13581 }
13582 /* If the next token is an `=', then there is a
13583 default-argument. If the next token is a `>', we are at
13584 the end of the parameter-list. If the next token is a `,',
13585 then we are at the end of this parameter. */
13586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13587 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13588 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13589 {
13590 identifier = cp_parser_identifier (parser);
13591 /* Treat invalid names as if the parameter were nameless. */
13592 if (identifier == error_mark_node)
13593 identifier = NULL_TREE;
13594 }
13595 else
13596 identifier = NULL_TREE;
13597
13598 /* Create the template parameter. */
13599 parameter = finish_template_template_parm (class_type_node,
13600 identifier);
13601
13602 /* If the next token is an `=', then there is a
13603 default-argument. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13605 {
13606 bool is_template;
13607
13608 /* Consume the `='. */
13609 cp_lexer_consume_token (parser->lexer);
13610 /* Parse the id-expression. */
13611 push_deferring_access_checks (dk_no_deferred);
13612 /* save token before parsing the id-expression, for error
13613 reporting */
13614 token = cp_lexer_peek_token (parser->lexer);
13615 default_argument
13616 = cp_parser_id_expression (parser,
13617 /*template_keyword_p=*/false,
13618 /*check_dependency_p=*/true,
13619 /*template_p=*/&is_template,
13620 /*declarator_p=*/false,
13621 /*optional_p=*/false);
13622 if (TREE_CODE (default_argument) == TYPE_DECL)
13623 /* If the id-expression was a template-id that refers to
13624 a template-class, we already have the declaration here,
13625 so no further lookup is needed. */
13626 ;
13627 else
13628 /* Look up the name. */
13629 default_argument
13630 = cp_parser_lookup_name (parser, default_argument,
13631 none_type,
13632 /*is_template=*/is_template,
13633 /*is_namespace=*/false,
13634 /*check_dependency=*/true,
13635 /*ambiguous_decls=*/NULL,
13636 token->location);
13637 /* See if the default argument is valid. */
13638 default_argument
13639 = check_template_template_default_arg (default_argument);
13640
13641 /* Template parameter packs cannot have default
13642 arguments. */
13643 if (*is_parameter_pack)
13644 {
13645 if (identifier)
13646 error_at (token->location,
13647 "template parameter pack %qD cannot "
13648 "have a default argument",
13649 identifier);
13650 else
13651 error_at (token->location, "template parameter packs cannot "
13652 "have default arguments");
13653 default_argument = NULL_TREE;
13654 }
13655 pop_deferring_access_checks ();
13656 }
13657 else
13658 default_argument = NULL_TREE;
13659
13660 /* Create the combined representation of the parameter and the
13661 default argument. */
13662 parameter = build_tree_list (default_argument, parameter);
13663 }
13664 break;
13665
13666 default:
13667 gcc_unreachable ();
13668 break;
13669 }
13670
13671 return parameter;
13672 }
13673
13674 /* Parse a template-id.
13675
13676 template-id:
13677 template-name < template-argument-list [opt] >
13678
13679 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13680 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13681 returned. Otherwise, if the template-name names a function, or set
13682 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13683 names a class, returns a TYPE_DECL for the specialization.
13684
13685 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13686 uninstantiated templates. */
13687
13688 static tree
13689 cp_parser_template_id (cp_parser *parser,
13690 bool template_keyword_p,
13691 bool check_dependency_p,
13692 enum tag_types tag_type,
13693 bool is_declaration)
13694 {
13695 int i;
13696 tree templ;
13697 tree arguments;
13698 tree template_id;
13699 cp_token_position start_of_id = 0;
13700 deferred_access_check *chk;
13701 vec<deferred_access_check, va_gc> *access_check;
13702 cp_token *next_token = NULL, *next_token_2 = NULL;
13703 bool is_identifier;
13704
13705 /* If the next token corresponds to a template-id, there is no need
13706 to reparse it. */
13707 next_token = cp_lexer_peek_token (parser->lexer);
13708 if (next_token->type == CPP_TEMPLATE_ID)
13709 {
13710 struct tree_check *check_value;
13711
13712 /* Get the stored value. */
13713 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13714 /* Perform any access checks that were deferred. */
13715 access_check = check_value->checks;
13716 if (access_check)
13717 {
13718 FOR_EACH_VEC_ELT (*access_check, i, chk)
13719 perform_or_defer_access_check (chk->binfo,
13720 chk->decl,
13721 chk->diag_decl,
13722 tf_warning_or_error);
13723 }
13724 /* Return the stored value. */
13725 return check_value->value;
13726 }
13727
13728 /* Avoid performing name lookup if there is no possibility of
13729 finding a template-id. */
13730 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13731 || (next_token->type == CPP_NAME
13732 && !cp_parser_nth_token_starts_template_argument_list_p
13733 (parser, 2)))
13734 {
13735 cp_parser_error (parser, "expected template-id");
13736 return error_mark_node;
13737 }
13738
13739 /* Remember where the template-id starts. */
13740 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13741 start_of_id = cp_lexer_token_position (parser->lexer, false);
13742
13743 push_deferring_access_checks (dk_deferred);
13744
13745 /* Parse the template-name. */
13746 is_identifier = false;
13747 templ = cp_parser_template_name (parser, template_keyword_p,
13748 check_dependency_p,
13749 is_declaration,
13750 tag_type,
13751 &is_identifier);
13752 if (templ == error_mark_node || is_identifier)
13753 {
13754 pop_deferring_access_checks ();
13755 return templ;
13756 }
13757
13758 /* If we find the sequence `[:' after a template-name, it's probably
13759 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13760 parse correctly the argument list. */
13761 next_token = cp_lexer_peek_token (parser->lexer);
13762 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13763 if (next_token->type == CPP_OPEN_SQUARE
13764 && next_token->flags & DIGRAPH
13765 && next_token_2->type == CPP_COLON
13766 && !(next_token_2->flags & PREV_WHITE))
13767 {
13768 cp_parser_parse_tentatively (parser);
13769 /* Change `:' into `::'. */
13770 next_token_2->type = CPP_SCOPE;
13771 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13772 CPP_LESS. */
13773 cp_lexer_consume_token (parser->lexer);
13774
13775 /* Parse the arguments. */
13776 arguments = cp_parser_enclosed_template_argument_list (parser);
13777 if (!cp_parser_parse_definitely (parser))
13778 {
13779 /* If we couldn't parse an argument list, then we revert our changes
13780 and return simply an error. Maybe this is not a template-id
13781 after all. */
13782 next_token_2->type = CPP_COLON;
13783 cp_parser_error (parser, "expected %<<%>");
13784 pop_deferring_access_checks ();
13785 return error_mark_node;
13786 }
13787 /* Otherwise, emit an error about the invalid digraph, but continue
13788 parsing because we got our argument list. */
13789 if (permerror (next_token->location,
13790 "%<<::%> cannot begin a template-argument list"))
13791 {
13792 static bool hint = false;
13793 inform (next_token->location,
13794 "%<<:%> is an alternate spelling for %<[%>."
13795 " Insert whitespace between %<<%> and %<::%>");
13796 if (!hint && !flag_permissive)
13797 {
13798 inform (next_token->location, "(if you use %<-fpermissive%> "
13799 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13800 "accept your code)");
13801 hint = true;
13802 }
13803 }
13804 }
13805 else
13806 {
13807 /* Look for the `<' that starts the template-argument-list. */
13808 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13809 {
13810 pop_deferring_access_checks ();
13811 return error_mark_node;
13812 }
13813 /* Parse the arguments. */
13814 arguments = cp_parser_enclosed_template_argument_list (parser);
13815 }
13816
13817 /* Build a representation of the specialization. */
13818 if (identifier_p (templ))
13819 template_id = build_min_nt_loc (next_token->location,
13820 TEMPLATE_ID_EXPR,
13821 templ, arguments);
13822 else if (DECL_TYPE_TEMPLATE_P (templ)
13823 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13824 {
13825 bool entering_scope;
13826 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13827 template (rather than some instantiation thereof) only if
13828 is not nested within some other construct. For example, in
13829 "template <typename T> void f(T) { A<T>::", A<T> is just an
13830 instantiation of A. */
13831 entering_scope = (template_parm_scope_p ()
13832 && cp_lexer_next_token_is (parser->lexer,
13833 CPP_SCOPE));
13834 template_id
13835 = finish_template_type (templ, arguments, entering_scope);
13836 }
13837 else if (variable_template_p (templ))
13838 {
13839 template_id = lookup_template_variable (templ, arguments);
13840 }
13841 else
13842 {
13843 /* If it's not a class-template or a template-template, it should be
13844 a function-template. */
13845 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13846 || TREE_CODE (templ) == OVERLOAD
13847 || BASELINK_P (templ)));
13848
13849 template_id = lookup_template_function (templ, arguments);
13850 }
13851
13852 /* If parsing tentatively, replace the sequence of tokens that makes
13853 up the template-id with a CPP_TEMPLATE_ID token. That way,
13854 should we re-parse the token stream, we will not have to repeat
13855 the effort required to do the parse, nor will we issue duplicate
13856 error messages about problems during instantiation of the
13857 template. */
13858 if (start_of_id
13859 /* Don't do this if we had a parse error in a declarator; re-parsing
13860 might succeed if a name changes meaning (60361). */
13861 && !(cp_parser_error_occurred (parser)
13862 && cp_parser_parsing_tentatively (parser)
13863 && parser->in_declarator_p))
13864 {
13865 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13866
13867 /* Reset the contents of the START_OF_ID token. */
13868 token->type = CPP_TEMPLATE_ID;
13869 /* Retrieve any deferred checks. Do not pop this access checks yet
13870 so the memory will not be reclaimed during token replacing below. */
13871 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13872 token->u.tree_check_value->value = template_id;
13873 token->u.tree_check_value->checks = get_deferred_access_checks ();
13874 token->keyword = RID_MAX;
13875
13876 /* Purge all subsequent tokens. */
13877 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13878
13879 /* ??? Can we actually assume that, if template_id ==
13880 error_mark_node, we will have issued a diagnostic to the
13881 user, as opposed to simply marking the tentative parse as
13882 failed? */
13883 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13884 error_at (token->location, "parse error in template argument list");
13885 }
13886
13887 pop_to_parent_deferring_access_checks ();
13888 return template_id;
13889 }
13890
13891 /* Parse a template-name.
13892
13893 template-name:
13894 identifier
13895
13896 The standard should actually say:
13897
13898 template-name:
13899 identifier
13900 operator-function-id
13901
13902 A defect report has been filed about this issue.
13903
13904 A conversion-function-id cannot be a template name because they cannot
13905 be part of a template-id. In fact, looking at this code:
13906
13907 a.operator K<int>()
13908
13909 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13910 It is impossible to call a templated conversion-function-id with an
13911 explicit argument list, since the only allowed template parameter is
13912 the type to which it is converting.
13913
13914 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13915 `template' keyword, in a construction like:
13916
13917 T::template f<3>()
13918
13919 In that case `f' is taken to be a template-name, even though there
13920 is no way of knowing for sure.
13921
13922 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13923 name refers to a set of overloaded functions, at least one of which
13924 is a template, or an IDENTIFIER_NODE with the name of the template,
13925 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13926 names are looked up inside uninstantiated templates. */
13927
13928 static tree
13929 cp_parser_template_name (cp_parser* parser,
13930 bool template_keyword_p,
13931 bool check_dependency_p,
13932 bool is_declaration,
13933 enum tag_types tag_type,
13934 bool *is_identifier)
13935 {
13936 tree identifier;
13937 tree decl;
13938 tree fns;
13939 cp_token *token = cp_lexer_peek_token (parser->lexer);
13940
13941 /* If the next token is `operator', then we have either an
13942 operator-function-id or a conversion-function-id. */
13943 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13944 {
13945 /* We don't know whether we're looking at an
13946 operator-function-id or a conversion-function-id. */
13947 cp_parser_parse_tentatively (parser);
13948 /* Try an operator-function-id. */
13949 identifier = cp_parser_operator_function_id (parser);
13950 /* If that didn't work, try a conversion-function-id. */
13951 if (!cp_parser_parse_definitely (parser))
13952 {
13953 cp_parser_error (parser, "expected template-name");
13954 return error_mark_node;
13955 }
13956 }
13957 /* Look for the identifier. */
13958 else
13959 identifier = cp_parser_identifier (parser);
13960
13961 /* If we didn't find an identifier, we don't have a template-id. */
13962 if (identifier == error_mark_node)
13963 return error_mark_node;
13964
13965 /* If the name immediately followed the `template' keyword, then it
13966 is a template-name. However, if the next token is not `<', then
13967 we do not treat it as a template-name, since it is not being used
13968 as part of a template-id. This enables us to handle constructs
13969 like:
13970
13971 template <typename T> struct S { S(); };
13972 template <typename T> S<T>::S();
13973
13974 correctly. We would treat `S' as a template -- if it were `S<T>'
13975 -- but we do not if there is no `<'. */
13976
13977 if (processing_template_decl
13978 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13979 {
13980 /* In a declaration, in a dependent context, we pretend that the
13981 "template" keyword was present in order to improve error
13982 recovery. For example, given:
13983
13984 template <typename T> void f(T::X<int>);
13985
13986 we want to treat "X<int>" as a template-id. */
13987 if (is_declaration
13988 && !template_keyword_p
13989 && parser->scope && TYPE_P (parser->scope)
13990 && check_dependency_p
13991 && dependent_scope_p (parser->scope)
13992 /* Do not do this for dtors (or ctors), since they never
13993 need the template keyword before their name. */
13994 && !constructor_name_p (identifier, parser->scope))
13995 {
13996 cp_token_position start = 0;
13997
13998 /* Explain what went wrong. */
13999 error_at (token->location, "non-template %qD used as template",
14000 identifier);
14001 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14002 parser->scope, identifier);
14003 /* If parsing tentatively, find the location of the "<" token. */
14004 if (cp_parser_simulate_error (parser))
14005 start = cp_lexer_token_position (parser->lexer, true);
14006 /* Parse the template arguments so that we can issue error
14007 messages about them. */
14008 cp_lexer_consume_token (parser->lexer);
14009 cp_parser_enclosed_template_argument_list (parser);
14010 /* Skip tokens until we find a good place from which to
14011 continue parsing. */
14012 cp_parser_skip_to_closing_parenthesis (parser,
14013 /*recovering=*/true,
14014 /*or_comma=*/true,
14015 /*consume_paren=*/false);
14016 /* If parsing tentatively, permanently remove the
14017 template argument list. That will prevent duplicate
14018 error messages from being issued about the missing
14019 "template" keyword. */
14020 if (start)
14021 cp_lexer_purge_tokens_after (parser->lexer, start);
14022 if (is_identifier)
14023 *is_identifier = true;
14024 return identifier;
14025 }
14026
14027 /* If the "template" keyword is present, then there is generally
14028 no point in doing name-lookup, so we just return IDENTIFIER.
14029 But, if the qualifying scope is non-dependent then we can
14030 (and must) do name-lookup normally. */
14031 if (template_keyword_p
14032 && (!parser->scope
14033 || (TYPE_P (parser->scope)
14034 && dependent_type_p (parser->scope))))
14035 return identifier;
14036 }
14037
14038 /* Look up the name. */
14039 decl = cp_parser_lookup_name (parser, identifier,
14040 tag_type,
14041 /*is_template=*/true,
14042 /*is_namespace=*/false,
14043 check_dependency_p,
14044 /*ambiguous_decls=*/NULL,
14045 token->location);
14046
14047 /* If DECL is a template, then the name was a template-name. */
14048 if (TREE_CODE (decl) == TEMPLATE_DECL)
14049 {
14050 if (TREE_DEPRECATED (decl)
14051 && deprecated_state != DEPRECATED_SUPPRESS)
14052 warn_deprecated_use (decl, NULL_TREE);
14053 }
14054 else
14055 {
14056 tree fn = NULL_TREE;
14057
14058 /* The standard does not explicitly indicate whether a name that
14059 names a set of overloaded declarations, some of which are
14060 templates, is a template-name. However, such a name should
14061 be a template-name; otherwise, there is no way to form a
14062 template-id for the overloaded templates. */
14063 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14064 if (TREE_CODE (fns) == OVERLOAD)
14065 for (fn = fns; fn; fn = OVL_NEXT (fn))
14066 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14067 break;
14068
14069 if (!fn)
14070 {
14071 /* The name does not name a template. */
14072 cp_parser_error (parser, "expected template-name");
14073 return error_mark_node;
14074 }
14075 }
14076
14077 /* If DECL is dependent, and refers to a function, then just return
14078 its name; we will look it up again during template instantiation. */
14079 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14080 {
14081 tree scope = ovl_scope (decl);
14082 if (TYPE_P (scope) && dependent_type_p (scope))
14083 return identifier;
14084 }
14085
14086 return decl;
14087 }
14088
14089 /* Parse a template-argument-list.
14090
14091 template-argument-list:
14092 template-argument ... [opt]
14093 template-argument-list , template-argument ... [opt]
14094
14095 Returns a TREE_VEC containing the arguments. */
14096
14097 static tree
14098 cp_parser_template_argument_list (cp_parser* parser)
14099 {
14100 tree fixed_args[10];
14101 unsigned n_args = 0;
14102 unsigned alloced = 10;
14103 tree *arg_ary = fixed_args;
14104 tree vec;
14105 bool saved_in_template_argument_list_p;
14106 bool saved_ice_p;
14107 bool saved_non_ice_p;
14108
14109 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14110 parser->in_template_argument_list_p = true;
14111 /* Even if the template-id appears in an integral
14112 constant-expression, the contents of the argument list do
14113 not. */
14114 saved_ice_p = parser->integral_constant_expression_p;
14115 parser->integral_constant_expression_p = false;
14116 saved_non_ice_p = parser->non_integral_constant_expression_p;
14117 parser->non_integral_constant_expression_p = false;
14118
14119 /* Parse the arguments. */
14120 do
14121 {
14122 tree argument;
14123
14124 if (n_args)
14125 /* Consume the comma. */
14126 cp_lexer_consume_token (parser->lexer);
14127
14128 /* Parse the template-argument. */
14129 argument = cp_parser_template_argument (parser);
14130
14131 /* If the next token is an ellipsis, we're expanding a template
14132 argument pack. */
14133 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14134 {
14135 if (argument == error_mark_node)
14136 {
14137 cp_token *token = cp_lexer_peek_token (parser->lexer);
14138 error_at (token->location,
14139 "expected parameter pack before %<...%>");
14140 }
14141 /* Consume the `...' token. */
14142 cp_lexer_consume_token (parser->lexer);
14143
14144 /* Make the argument into a TYPE_PACK_EXPANSION or
14145 EXPR_PACK_EXPANSION. */
14146 argument = make_pack_expansion (argument);
14147 }
14148
14149 if (n_args == alloced)
14150 {
14151 alloced *= 2;
14152
14153 if (arg_ary == fixed_args)
14154 {
14155 arg_ary = XNEWVEC (tree, alloced);
14156 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14157 }
14158 else
14159 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14160 }
14161 arg_ary[n_args++] = argument;
14162 }
14163 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14164
14165 vec = make_tree_vec (n_args);
14166
14167 while (n_args--)
14168 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14169
14170 if (arg_ary != fixed_args)
14171 free (arg_ary);
14172 parser->non_integral_constant_expression_p = saved_non_ice_p;
14173 parser->integral_constant_expression_p = saved_ice_p;
14174 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14175 #ifdef ENABLE_CHECKING
14176 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14177 #endif
14178 return vec;
14179 }
14180
14181 /* Parse a template-argument.
14182
14183 template-argument:
14184 assignment-expression
14185 type-id
14186 id-expression
14187
14188 The representation is that of an assignment-expression, type-id, or
14189 id-expression -- except that the qualified id-expression is
14190 evaluated, so that the value returned is either a DECL or an
14191 OVERLOAD.
14192
14193 Although the standard says "assignment-expression", it forbids
14194 throw-expressions or assignments in the template argument.
14195 Therefore, we use "conditional-expression" instead. */
14196
14197 static tree
14198 cp_parser_template_argument (cp_parser* parser)
14199 {
14200 tree argument;
14201 bool template_p;
14202 bool address_p;
14203 bool maybe_type_id = false;
14204 cp_token *token = NULL, *argument_start_token = NULL;
14205 location_t loc = 0;
14206 cp_id_kind idk;
14207
14208 /* There's really no way to know what we're looking at, so we just
14209 try each alternative in order.
14210
14211 [temp.arg]
14212
14213 In a template-argument, an ambiguity between a type-id and an
14214 expression is resolved to a type-id, regardless of the form of
14215 the corresponding template-parameter.
14216
14217 Therefore, we try a type-id first. */
14218 cp_parser_parse_tentatively (parser);
14219 argument = cp_parser_template_type_arg (parser);
14220 /* If there was no error parsing the type-id but the next token is a
14221 '>>', our behavior depends on which dialect of C++ we're
14222 parsing. In C++98, we probably found a typo for '> >'. But there
14223 are type-id which are also valid expressions. For instance:
14224
14225 struct X { int operator >> (int); };
14226 template <int V> struct Foo {};
14227 Foo<X () >> 5> r;
14228
14229 Here 'X()' is a valid type-id of a function type, but the user just
14230 wanted to write the expression "X() >> 5". Thus, we remember that we
14231 found a valid type-id, but we still try to parse the argument as an
14232 expression to see what happens.
14233
14234 In C++0x, the '>>' will be considered two separate '>'
14235 tokens. */
14236 if (!cp_parser_error_occurred (parser)
14237 && cxx_dialect == cxx98
14238 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14239 {
14240 maybe_type_id = true;
14241 cp_parser_abort_tentative_parse (parser);
14242 }
14243 else
14244 {
14245 /* If the next token isn't a `,' or a `>', then this argument wasn't
14246 really finished. This means that the argument is not a valid
14247 type-id. */
14248 if (!cp_parser_next_token_ends_template_argument_p (parser))
14249 cp_parser_error (parser, "expected template-argument");
14250 /* If that worked, we're done. */
14251 if (cp_parser_parse_definitely (parser))
14252 return argument;
14253 }
14254 /* We're still not sure what the argument will be. */
14255 cp_parser_parse_tentatively (parser);
14256 /* Try a template. */
14257 argument_start_token = cp_lexer_peek_token (parser->lexer);
14258 argument = cp_parser_id_expression (parser,
14259 /*template_keyword_p=*/false,
14260 /*check_dependency_p=*/true,
14261 &template_p,
14262 /*declarator_p=*/false,
14263 /*optional_p=*/false);
14264 /* If the next token isn't a `,' or a `>', then this argument wasn't
14265 really finished. */
14266 if (!cp_parser_next_token_ends_template_argument_p (parser))
14267 cp_parser_error (parser, "expected template-argument");
14268 if (!cp_parser_error_occurred (parser))
14269 {
14270 /* Figure out what is being referred to. If the id-expression
14271 was for a class template specialization, then we will have a
14272 TYPE_DECL at this point. There is no need to do name lookup
14273 at this point in that case. */
14274 if (TREE_CODE (argument) != TYPE_DECL)
14275 argument = cp_parser_lookup_name (parser, argument,
14276 none_type,
14277 /*is_template=*/template_p,
14278 /*is_namespace=*/false,
14279 /*check_dependency=*/true,
14280 /*ambiguous_decls=*/NULL,
14281 argument_start_token->location);
14282 if (TREE_CODE (argument) != TEMPLATE_DECL
14283 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14284 cp_parser_error (parser, "expected template-name");
14285 }
14286 if (cp_parser_parse_definitely (parser))
14287 {
14288 if (TREE_DEPRECATED (argument))
14289 warn_deprecated_use (argument, NULL_TREE);
14290 return argument;
14291 }
14292 /* It must be a non-type argument. There permitted cases are given
14293 in [temp.arg.nontype]:
14294
14295 -- an integral constant-expression of integral or enumeration
14296 type; or
14297
14298 -- the name of a non-type template-parameter; or
14299
14300 -- the name of an object or function with external linkage...
14301
14302 -- the address of an object or function with external linkage...
14303
14304 -- a pointer to member... */
14305 /* Look for a non-type template parameter. */
14306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14307 {
14308 cp_parser_parse_tentatively (parser);
14309 argument = cp_parser_primary_expression (parser,
14310 /*address_p=*/false,
14311 /*cast_p=*/false,
14312 /*template_arg_p=*/true,
14313 &idk);
14314 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14315 || !cp_parser_next_token_ends_template_argument_p (parser))
14316 cp_parser_simulate_error (parser);
14317 if (cp_parser_parse_definitely (parser))
14318 return argument;
14319 }
14320
14321 /* If the next token is "&", the argument must be the address of an
14322 object or function with external linkage. */
14323 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14324 if (address_p)
14325 {
14326 loc = cp_lexer_peek_token (parser->lexer)->location;
14327 cp_lexer_consume_token (parser->lexer);
14328 }
14329 /* See if we might have an id-expression. */
14330 token = cp_lexer_peek_token (parser->lexer);
14331 if (token->type == CPP_NAME
14332 || token->keyword == RID_OPERATOR
14333 || token->type == CPP_SCOPE
14334 || token->type == CPP_TEMPLATE_ID
14335 || token->type == CPP_NESTED_NAME_SPECIFIER)
14336 {
14337 cp_parser_parse_tentatively (parser);
14338 argument = cp_parser_primary_expression (parser,
14339 address_p,
14340 /*cast_p=*/false,
14341 /*template_arg_p=*/true,
14342 &idk);
14343 if (cp_parser_error_occurred (parser)
14344 || !cp_parser_next_token_ends_template_argument_p (parser))
14345 cp_parser_abort_tentative_parse (parser);
14346 else
14347 {
14348 tree probe;
14349
14350 if (INDIRECT_REF_P (argument))
14351 {
14352 /* Strip the dereference temporarily. */
14353 gcc_assert (REFERENCE_REF_P (argument));
14354 argument = TREE_OPERAND (argument, 0);
14355 }
14356
14357 /* If we're in a template, we represent a qualified-id referring
14358 to a static data member as a SCOPE_REF even if the scope isn't
14359 dependent so that we can check access control later. */
14360 probe = argument;
14361 if (TREE_CODE (probe) == SCOPE_REF)
14362 probe = TREE_OPERAND (probe, 1);
14363 if (VAR_P (probe))
14364 {
14365 /* A variable without external linkage might still be a
14366 valid constant-expression, so no error is issued here
14367 if the external-linkage check fails. */
14368 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14369 cp_parser_simulate_error (parser);
14370 }
14371 else if (is_overloaded_fn (argument))
14372 /* All overloaded functions are allowed; if the external
14373 linkage test does not pass, an error will be issued
14374 later. */
14375 ;
14376 else if (address_p
14377 && (TREE_CODE (argument) == OFFSET_REF
14378 || TREE_CODE (argument) == SCOPE_REF))
14379 /* A pointer-to-member. */
14380 ;
14381 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14382 ;
14383 else
14384 cp_parser_simulate_error (parser);
14385
14386 if (cp_parser_parse_definitely (parser))
14387 {
14388 if (address_p)
14389 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14390 tf_warning_or_error);
14391 else
14392 argument = convert_from_reference (argument);
14393 return argument;
14394 }
14395 }
14396 }
14397 /* If the argument started with "&", there are no other valid
14398 alternatives at this point. */
14399 if (address_p)
14400 {
14401 cp_parser_error (parser, "invalid non-type template argument");
14402 return error_mark_node;
14403 }
14404
14405 /* If the argument wasn't successfully parsed as a type-id followed
14406 by '>>', the argument can only be a constant expression now.
14407 Otherwise, we try parsing the constant-expression tentatively,
14408 because the argument could really be a type-id. */
14409 if (maybe_type_id)
14410 cp_parser_parse_tentatively (parser);
14411 argument = cp_parser_constant_expression (parser);
14412
14413 if (!maybe_type_id)
14414 return argument;
14415 if (!cp_parser_next_token_ends_template_argument_p (parser))
14416 cp_parser_error (parser, "expected template-argument");
14417 if (cp_parser_parse_definitely (parser))
14418 return argument;
14419 /* We did our best to parse the argument as a non type-id, but that
14420 was the only alternative that matched (albeit with a '>' after
14421 it). We can assume it's just a typo from the user, and a
14422 diagnostic will then be issued. */
14423 return cp_parser_template_type_arg (parser);
14424 }
14425
14426 /* Parse an explicit-instantiation.
14427
14428 explicit-instantiation:
14429 template declaration
14430
14431 Although the standard says `declaration', what it really means is:
14432
14433 explicit-instantiation:
14434 template decl-specifier-seq [opt] declarator [opt] ;
14435
14436 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14437 supposed to be allowed. A defect report has been filed about this
14438 issue.
14439
14440 GNU Extension:
14441
14442 explicit-instantiation:
14443 storage-class-specifier template
14444 decl-specifier-seq [opt] declarator [opt] ;
14445 function-specifier template
14446 decl-specifier-seq [opt] declarator [opt] ; */
14447
14448 static void
14449 cp_parser_explicit_instantiation (cp_parser* parser)
14450 {
14451 int declares_class_or_enum;
14452 cp_decl_specifier_seq decl_specifiers;
14453 tree extension_specifier = NULL_TREE;
14454
14455 timevar_push (TV_TEMPLATE_INST);
14456
14457 /* Look for an (optional) storage-class-specifier or
14458 function-specifier. */
14459 if (cp_parser_allow_gnu_extensions_p (parser))
14460 {
14461 extension_specifier
14462 = cp_parser_storage_class_specifier_opt (parser);
14463 if (!extension_specifier)
14464 extension_specifier
14465 = cp_parser_function_specifier_opt (parser,
14466 /*decl_specs=*/NULL);
14467 }
14468
14469 /* Look for the `template' keyword. */
14470 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14471 /* Let the front end know that we are processing an explicit
14472 instantiation. */
14473 begin_explicit_instantiation ();
14474 /* [temp.explicit] says that we are supposed to ignore access
14475 control while processing explicit instantiation directives. */
14476 push_deferring_access_checks (dk_no_check);
14477 /* Parse a decl-specifier-seq. */
14478 cp_parser_decl_specifier_seq (parser,
14479 CP_PARSER_FLAGS_OPTIONAL,
14480 &decl_specifiers,
14481 &declares_class_or_enum);
14482 /* If there was exactly one decl-specifier, and it declared a class,
14483 and there's no declarator, then we have an explicit type
14484 instantiation. */
14485 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14486 {
14487 tree type;
14488
14489 type = check_tag_decl (&decl_specifiers,
14490 /*explicit_type_instantiation_p=*/true);
14491 /* Turn access control back on for names used during
14492 template instantiation. */
14493 pop_deferring_access_checks ();
14494 if (type)
14495 do_type_instantiation (type, extension_specifier,
14496 /*complain=*/tf_error);
14497 }
14498 else
14499 {
14500 cp_declarator *declarator;
14501 tree decl;
14502
14503 /* Parse the declarator. */
14504 declarator
14505 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14506 /*ctor_dtor_or_conv_p=*/NULL,
14507 /*parenthesized_p=*/NULL,
14508 /*member_p=*/false,
14509 /*friend_p=*/false);
14510 if (declares_class_or_enum & 2)
14511 cp_parser_check_for_definition_in_return_type (declarator,
14512 decl_specifiers.type,
14513 decl_specifiers.locations[ds_type_spec]);
14514 if (declarator != cp_error_declarator)
14515 {
14516 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14517 permerror (decl_specifiers.locations[ds_inline],
14518 "explicit instantiation shall not use"
14519 " %<inline%> specifier");
14520 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14521 permerror (decl_specifiers.locations[ds_constexpr],
14522 "explicit instantiation shall not use"
14523 " %<constexpr%> specifier");
14524
14525 decl = grokdeclarator (declarator, &decl_specifiers,
14526 NORMAL, 0, &decl_specifiers.attributes);
14527 /* Turn access control back on for names used during
14528 template instantiation. */
14529 pop_deferring_access_checks ();
14530 /* Do the explicit instantiation. */
14531 do_decl_instantiation (decl, extension_specifier);
14532 }
14533 else
14534 {
14535 pop_deferring_access_checks ();
14536 /* Skip the body of the explicit instantiation. */
14537 cp_parser_skip_to_end_of_statement (parser);
14538 }
14539 }
14540 /* We're done with the instantiation. */
14541 end_explicit_instantiation ();
14542
14543 cp_parser_consume_semicolon_at_end_of_statement (parser);
14544
14545 timevar_pop (TV_TEMPLATE_INST);
14546 }
14547
14548 /* Parse an explicit-specialization.
14549
14550 explicit-specialization:
14551 template < > declaration
14552
14553 Although the standard says `declaration', what it really means is:
14554
14555 explicit-specialization:
14556 template <> decl-specifier [opt] init-declarator [opt] ;
14557 template <> function-definition
14558 template <> explicit-specialization
14559 template <> template-declaration */
14560
14561 static void
14562 cp_parser_explicit_specialization (cp_parser* parser)
14563 {
14564 bool need_lang_pop;
14565 cp_token *token = cp_lexer_peek_token (parser->lexer);
14566
14567 /* Look for the `template' keyword. */
14568 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14569 /* Look for the `<'. */
14570 cp_parser_require (parser, CPP_LESS, RT_LESS);
14571 /* Look for the `>'. */
14572 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14573 /* We have processed another parameter list. */
14574 ++parser->num_template_parameter_lists;
14575 /* [temp]
14576
14577 A template ... explicit specialization ... shall not have C
14578 linkage. */
14579 if (current_lang_name == lang_name_c)
14580 {
14581 error_at (token->location, "template specialization with C linkage");
14582 /* Give it C++ linkage to avoid confusing other parts of the
14583 front end. */
14584 push_lang_context (lang_name_cplusplus);
14585 need_lang_pop = true;
14586 }
14587 else
14588 need_lang_pop = false;
14589 /* Let the front end know that we are beginning a specialization. */
14590 if (!begin_specialization ())
14591 {
14592 end_specialization ();
14593 return;
14594 }
14595
14596 /* If the next keyword is `template', we need to figure out whether
14597 or not we're looking a template-declaration. */
14598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14599 {
14600 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14601 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14602 cp_parser_template_declaration_after_export (parser,
14603 /*member_p=*/false);
14604 else
14605 cp_parser_explicit_specialization (parser);
14606 }
14607 else
14608 /* Parse the dependent declaration. */
14609 cp_parser_single_declaration (parser,
14610 /*checks=*/NULL,
14611 /*member_p=*/false,
14612 /*explicit_specialization_p=*/true,
14613 /*friend_p=*/NULL);
14614 /* We're done with the specialization. */
14615 end_specialization ();
14616 /* For the erroneous case of a template with C linkage, we pushed an
14617 implicit C++ linkage scope; exit that scope now. */
14618 if (need_lang_pop)
14619 pop_lang_context ();
14620 /* We're done with this parameter list. */
14621 --parser->num_template_parameter_lists;
14622 }
14623
14624 /* Parse a type-specifier.
14625
14626 type-specifier:
14627 simple-type-specifier
14628 class-specifier
14629 enum-specifier
14630 elaborated-type-specifier
14631 cv-qualifier
14632
14633 GNU Extension:
14634
14635 type-specifier:
14636 __complex__
14637
14638 Returns a representation of the type-specifier. For a
14639 class-specifier, enum-specifier, or elaborated-type-specifier, a
14640 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14641
14642 The parser flags FLAGS is used to control type-specifier parsing.
14643
14644 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14645 in a decl-specifier-seq.
14646
14647 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14648 class-specifier, enum-specifier, or elaborated-type-specifier, then
14649 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14650 if a type is declared; 2 if it is defined. Otherwise, it is set to
14651 zero.
14652
14653 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14654 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14655 is set to FALSE. */
14656
14657 static tree
14658 cp_parser_type_specifier (cp_parser* parser,
14659 cp_parser_flags flags,
14660 cp_decl_specifier_seq *decl_specs,
14661 bool is_declaration,
14662 int* declares_class_or_enum,
14663 bool* is_cv_qualifier)
14664 {
14665 tree type_spec = NULL_TREE;
14666 cp_token *token;
14667 enum rid keyword;
14668 cp_decl_spec ds = ds_last;
14669
14670 /* Assume this type-specifier does not declare a new type. */
14671 if (declares_class_or_enum)
14672 *declares_class_or_enum = 0;
14673 /* And that it does not specify a cv-qualifier. */
14674 if (is_cv_qualifier)
14675 *is_cv_qualifier = false;
14676 /* Peek at the next token. */
14677 token = cp_lexer_peek_token (parser->lexer);
14678
14679 /* If we're looking at a keyword, we can use that to guide the
14680 production we choose. */
14681 keyword = token->keyword;
14682 switch (keyword)
14683 {
14684 case RID_ENUM:
14685 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14686 goto elaborated_type_specifier;
14687
14688 /* Look for the enum-specifier. */
14689 type_spec = cp_parser_enum_specifier (parser);
14690 /* If that worked, we're done. */
14691 if (type_spec)
14692 {
14693 if (declares_class_or_enum)
14694 *declares_class_or_enum = 2;
14695 if (decl_specs)
14696 cp_parser_set_decl_spec_type (decl_specs,
14697 type_spec,
14698 token,
14699 /*type_definition_p=*/true);
14700 return type_spec;
14701 }
14702 else
14703 goto elaborated_type_specifier;
14704
14705 /* Any of these indicate either a class-specifier, or an
14706 elaborated-type-specifier. */
14707 case RID_CLASS:
14708 case RID_STRUCT:
14709 case RID_UNION:
14710 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14711 goto elaborated_type_specifier;
14712
14713 /* Parse tentatively so that we can back up if we don't find a
14714 class-specifier. */
14715 cp_parser_parse_tentatively (parser);
14716 /* Look for the class-specifier. */
14717 type_spec = cp_parser_class_specifier (parser);
14718 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14719 /* If that worked, we're done. */
14720 if (cp_parser_parse_definitely (parser))
14721 {
14722 if (declares_class_or_enum)
14723 *declares_class_or_enum = 2;
14724 if (decl_specs)
14725 cp_parser_set_decl_spec_type (decl_specs,
14726 type_spec,
14727 token,
14728 /*type_definition_p=*/true);
14729 return type_spec;
14730 }
14731
14732 /* Fall through. */
14733 elaborated_type_specifier:
14734 /* We're declaring (not defining) a class or enum. */
14735 if (declares_class_or_enum)
14736 *declares_class_or_enum = 1;
14737
14738 /* Fall through. */
14739 case RID_TYPENAME:
14740 /* Look for an elaborated-type-specifier. */
14741 type_spec
14742 = (cp_parser_elaborated_type_specifier
14743 (parser,
14744 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14745 is_declaration));
14746 if (decl_specs)
14747 cp_parser_set_decl_spec_type (decl_specs,
14748 type_spec,
14749 token,
14750 /*type_definition_p=*/false);
14751 return type_spec;
14752
14753 case RID_CONST:
14754 ds = ds_const;
14755 if (is_cv_qualifier)
14756 *is_cv_qualifier = true;
14757 break;
14758
14759 case RID_VOLATILE:
14760 ds = ds_volatile;
14761 if (is_cv_qualifier)
14762 *is_cv_qualifier = true;
14763 break;
14764
14765 case RID_RESTRICT:
14766 ds = ds_restrict;
14767 if (is_cv_qualifier)
14768 *is_cv_qualifier = true;
14769 break;
14770
14771 case RID_COMPLEX:
14772 /* The `__complex__' keyword is a GNU extension. */
14773 ds = ds_complex;
14774 break;
14775
14776 default:
14777 break;
14778 }
14779
14780 /* Handle simple keywords. */
14781 if (ds != ds_last)
14782 {
14783 if (decl_specs)
14784 {
14785 set_and_check_decl_spec_loc (decl_specs, ds, token);
14786 decl_specs->any_specifiers_p = true;
14787 }
14788 return cp_lexer_consume_token (parser->lexer)->u.value;
14789 }
14790
14791 /* If we do not already have a type-specifier, assume we are looking
14792 at a simple-type-specifier. */
14793 type_spec = cp_parser_simple_type_specifier (parser,
14794 decl_specs,
14795 flags);
14796
14797 /* If we didn't find a type-specifier, and a type-specifier was not
14798 optional in this context, issue an error message. */
14799 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14800 {
14801 cp_parser_error (parser, "expected type specifier");
14802 return error_mark_node;
14803 }
14804
14805 return type_spec;
14806 }
14807
14808 /* Parse a simple-type-specifier.
14809
14810 simple-type-specifier:
14811 :: [opt] nested-name-specifier [opt] type-name
14812 :: [opt] nested-name-specifier template template-id
14813 char
14814 wchar_t
14815 bool
14816 short
14817 int
14818 long
14819 signed
14820 unsigned
14821 float
14822 double
14823 void
14824
14825 C++0x Extension:
14826
14827 simple-type-specifier:
14828 auto
14829 decltype ( expression )
14830 char16_t
14831 char32_t
14832 __underlying_type ( type-id )
14833
14834 GNU Extension:
14835
14836 simple-type-specifier:
14837 __int128
14838 __typeof__ unary-expression
14839 __typeof__ ( type-id )
14840 __typeof__ ( type-id ) { initializer-list , [opt] }
14841
14842 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14843 appropriately updated. */
14844
14845 static tree
14846 cp_parser_simple_type_specifier (cp_parser* parser,
14847 cp_decl_specifier_seq *decl_specs,
14848 cp_parser_flags flags)
14849 {
14850 tree type = NULL_TREE;
14851 cp_token *token;
14852 int idx;
14853
14854 /* Peek at the next token. */
14855 token = cp_lexer_peek_token (parser->lexer);
14856
14857 /* If we're looking at a keyword, things are easy. */
14858 switch (token->keyword)
14859 {
14860 case RID_CHAR:
14861 if (decl_specs)
14862 decl_specs->explicit_char_p = true;
14863 type = char_type_node;
14864 break;
14865 case RID_CHAR16:
14866 type = char16_type_node;
14867 break;
14868 case RID_CHAR32:
14869 type = char32_type_node;
14870 break;
14871 case RID_WCHAR:
14872 type = wchar_type_node;
14873 break;
14874 case RID_BOOL:
14875 type = boolean_type_node;
14876 break;
14877 case RID_SHORT:
14878 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14879 type = short_integer_type_node;
14880 break;
14881 case RID_INT:
14882 if (decl_specs)
14883 decl_specs->explicit_int_p = true;
14884 type = integer_type_node;
14885 break;
14886 case RID_INT_N_0:
14887 case RID_INT_N_1:
14888 case RID_INT_N_2:
14889 case RID_INT_N_3:
14890 idx = token->keyword - RID_INT_N_0;
14891 if (! int_n_enabled_p [idx])
14892 break;
14893 if (decl_specs)
14894 {
14895 decl_specs->explicit_intN_p = true;
14896 decl_specs->int_n_idx = idx;
14897 }
14898 type = int_n_trees [idx].signed_type;
14899 break;
14900 case RID_LONG:
14901 if (decl_specs)
14902 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14903 type = long_integer_type_node;
14904 break;
14905 case RID_SIGNED:
14906 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14907 type = integer_type_node;
14908 break;
14909 case RID_UNSIGNED:
14910 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14911 type = unsigned_type_node;
14912 break;
14913 case RID_FLOAT:
14914 type = float_type_node;
14915 break;
14916 case RID_DOUBLE:
14917 type = double_type_node;
14918 break;
14919 case RID_VOID:
14920 type = void_type_node;
14921 break;
14922
14923 case RID_AUTO:
14924 maybe_warn_cpp0x (CPP0X_AUTO);
14925 if (parser->auto_is_implicit_function_template_parm_p)
14926 {
14927 if (cxx_dialect >= cxx14)
14928 type = synthesize_implicit_template_parm (parser);
14929 else
14930 type = error_mark_node;
14931
14932 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14933 {
14934 if (cxx_dialect < cxx14)
14935 error_at (token->location,
14936 "use of %<auto%> in lambda parameter declaration "
14937 "only available with "
14938 "-std=c++14 or -std=gnu++14");
14939 }
14940 else if (cxx_dialect < cxx14)
14941 error_at (token->location,
14942 "use of %<auto%> in parameter declaration "
14943 "only available with "
14944 "-std=c++14 or -std=gnu++14");
14945 else
14946 pedwarn (token->location, OPT_Wpedantic,
14947 "ISO C++ forbids use of %<auto%> in parameter "
14948 "declaration");
14949 }
14950 else
14951 type = make_auto ();
14952 break;
14953
14954 case RID_DECLTYPE:
14955 /* Since DR 743, decltype can either be a simple-type-specifier by
14956 itself or begin a nested-name-specifier. Parsing it will replace
14957 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14958 handling below decide what to do. */
14959 cp_parser_decltype (parser);
14960 cp_lexer_set_token_position (parser->lexer, token);
14961 break;
14962
14963 case RID_TYPEOF:
14964 /* Consume the `typeof' token. */
14965 cp_lexer_consume_token (parser->lexer);
14966 /* Parse the operand to `typeof'. */
14967 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14968 /* If it is not already a TYPE, take its type. */
14969 if (!TYPE_P (type))
14970 type = finish_typeof (type);
14971
14972 if (decl_specs)
14973 cp_parser_set_decl_spec_type (decl_specs, type,
14974 token,
14975 /*type_definition_p=*/false);
14976
14977 return type;
14978
14979 case RID_UNDERLYING_TYPE:
14980 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14981 if (decl_specs)
14982 cp_parser_set_decl_spec_type (decl_specs, type,
14983 token,
14984 /*type_definition_p=*/false);
14985
14986 return type;
14987
14988 case RID_BASES:
14989 case RID_DIRECT_BASES:
14990 type = cp_parser_trait_expr (parser, token->keyword);
14991 if (decl_specs)
14992 cp_parser_set_decl_spec_type (decl_specs, type,
14993 token,
14994 /*type_definition_p=*/false);
14995 return type;
14996 default:
14997 break;
14998 }
14999
15000 /* If token is an already-parsed decltype not followed by ::,
15001 it's a simple-type-specifier. */
15002 if (token->type == CPP_DECLTYPE
15003 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15004 {
15005 type = token->u.value;
15006 if (decl_specs)
15007 {
15008 cp_parser_set_decl_spec_type (decl_specs, type,
15009 token,
15010 /*type_definition_p=*/false);
15011 /* Remember that we are handling a decltype in order to
15012 implement the resolution of DR 1510 when the argument
15013 isn't instantiation dependent. */
15014 decl_specs->decltype_p = true;
15015 }
15016 cp_lexer_consume_token (parser->lexer);
15017 return type;
15018 }
15019
15020 /* If the type-specifier was for a built-in type, we're done. */
15021 if (type)
15022 {
15023 /* Record the type. */
15024 if (decl_specs
15025 && (token->keyword != RID_SIGNED
15026 && token->keyword != RID_UNSIGNED
15027 && token->keyword != RID_SHORT
15028 && token->keyword != RID_LONG))
15029 cp_parser_set_decl_spec_type (decl_specs,
15030 type,
15031 token,
15032 /*type_definition_p=*/false);
15033 if (decl_specs)
15034 decl_specs->any_specifiers_p = true;
15035
15036 /* Consume the token. */
15037 cp_lexer_consume_token (parser->lexer);
15038
15039 if (type == error_mark_node)
15040 return error_mark_node;
15041
15042 /* There is no valid C++ program where a non-template type is
15043 followed by a "<". That usually indicates that the user thought
15044 that the type was a template. */
15045 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15046 token->location);
15047
15048 return TYPE_NAME (type);
15049 }
15050
15051 /* The type-specifier must be a user-defined type. */
15052 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15053 {
15054 bool qualified_p;
15055 bool global_p;
15056
15057 /* Don't gobble tokens or issue error messages if this is an
15058 optional type-specifier. */
15059 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15060 cp_parser_parse_tentatively (parser);
15061
15062 /* Look for the optional `::' operator. */
15063 global_p
15064 = (cp_parser_global_scope_opt (parser,
15065 /*current_scope_valid_p=*/false)
15066 != NULL_TREE);
15067 /* Look for the nested-name specifier. */
15068 qualified_p
15069 = (cp_parser_nested_name_specifier_opt (parser,
15070 /*typename_keyword_p=*/false,
15071 /*check_dependency_p=*/true,
15072 /*type_p=*/false,
15073 /*is_declaration=*/false)
15074 != NULL_TREE);
15075 token = cp_lexer_peek_token (parser->lexer);
15076 /* If we have seen a nested-name-specifier, and the next token
15077 is `template', then we are using the template-id production. */
15078 if (parser->scope
15079 && cp_parser_optional_template_keyword (parser))
15080 {
15081 /* Look for the template-id. */
15082 type = cp_parser_template_id (parser,
15083 /*template_keyword_p=*/true,
15084 /*check_dependency_p=*/true,
15085 none_type,
15086 /*is_declaration=*/false);
15087 /* If the template-id did not name a type, we are out of
15088 luck. */
15089 if (TREE_CODE (type) != TYPE_DECL)
15090 {
15091 cp_parser_error (parser, "expected template-id for type");
15092 type = NULL_TREE;
15093 }
15094 }
15095 /* Otherwise, look for a type-name. */
15096 else
15097 type = cp_parser_type_name (parser);
15098 /* Keep track of all name-lookups performed in class scopes. */
15099 if (type
15100 && !global_p
15101 && !qualified_p
15102 && TREE_CODE (type) == TYPE_DECL
15103 && identifier_p (DECL_NAME (type)))
15104 maybe_note_name_used_in_class (DECL_NAME (type), type);
15105 /* If it didn't work out, we don't have a TYPE. */
15106 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15107 && !cp_parser_parse_definitely (parser))
15108 type = NULL_TREE;
15109 if (type && decl_specs)
15110 cp_parser_set_decl_spec_type (decl_specs, type,
15111 token,
15112 /*type_definition_p=*/false);
15113 }
15114
15115 /* If we didn't get a type-name, issue an error message. */
15116 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15117 {
15118 cp_parser_error (parser, "expected type-name");
15119 return error_mark_node;
15120 }
15121
15122 if (type && type != error_mark_node)
15123 {
15124 /* See if TYPE is an Objective-C type, and if so, parse and
15125 accept any protocol references following it. Do this before
15126 the cp_parser_check_for_invalid_template_id() call, because
15127 Objective-C types can be followed by '<...>' which would
15128 enclose protocol names rather than template arguments, and so
15129 everything is fine. */
15130 if (c_dialect_objc () && !parser->scope
15131 && (objc_is_id (type) || objc_is_class_name (type)))
15132 {
15133 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15134 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15135
15136 /* Clobber the "unqualified" type previously entered into
15137 DECL_SPECS with the new, improved protocol-qualified version. */
15138 if (decl_specs)
15139 decl_specs->type = qual_type;
15140
15141 return qual_type;
15142 }
15143
15144 /* There is no valid C++ program where a non-template type is
15145 followed by a "<". That usually indicates that the user
15146 thought that the type was a template. */
15147 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15148 none_type,
15149 token->location);
15150 }
15151
15152 return type;
15153 }
15154
15155 /* Parse a type-name.
15156
15157 type-name:
15158 class-name
15159 enum-name
15160 typedef-name
15161 simple-template-id [in c++0x]
15162
15163 enum-name:
15164 identifier
15165
15166 typedef-name:
15167 identifier
15168
15169 Returns a TYPE_DECL for the type. */
15170
15171 static tree
15172 cp_parser_type_name (cp_parser* parser)
15173 {
15174 tree type_decl;
15175
15176 /* We can't know yet whether it is a class-name or not. */
15177 cp_parser_parse_tentatively (parser);
15178 /* Try a class-name. */
15179 type_decl = cp_parser_class_name (parser,
15180 /*typename_keyword_p=*/false,
15181 /*template_keyword_p=*/false,
15182 none_type,
15183 /*check_dependency_p=*/true,
15184 /*class_head_p=*/false,
15185 /*is_declaration=*/false);
15186 /* If it's not a class-name, keep looking. */
15187 if (!cp_parser_parse_definitely (parser))
15188 {
15189 if (cxx_dialect < cxx11)
15190 /* It must be a typedef-name or an enum-name. */
15191 return cp_parser_nonclass_name (parser);
15192
15193 cp_parser_parse_tentatively (parser);
15194 /* It is either a simple-template-id representing an
15195 instantiation of an alias template... */
15196 type_decl = cp_parser_template_id (parser,
15197 /*template_keyword_p=*/false,
15198 /*check_dependency_p=*/true,
15199 none_type,
15200 /*is_declaration=*/false);
15201 /* Note that this must be an instantiation of an alias template
15202 because [temp.names]/6 says:
15203
15204 A template-id that names an alias template specialization
15205 is a type-name.
15206
15207 Whereas [temp.names]/7 says:
15208
15209 A simple-template-id that names a class template
15210 specialization is a class-name. */
15211 if (type_decl != NULL_TREE
15212 && TREE_CODE (type_decl) == TYPE_DECL
15213 && TYPE_DECL_ALIAS_P (type_decl))
15214 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15215 else
15216 cp_parser_simulate_error (parser);
15217
15218 if (!cp_parser_parse_definitely (parser))
15219 /* ... Or a typedef-name or an enum-name. */
15220 return cp_parser_nonclass_name (parser);
15221 }
15222
15223 return type_decl;
15224 }
15225
15226 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15227
15228 enum-name:
15229 identifier
15230
15231 typedef-name:
15232 identifier
15233
15234 Returns a TYPE_DECL for the type. */
15235
15236 static tree
15237 cp_parser_nonclass_name (cp_parser* parser)
15238 {
15239 tree type_decl;
15240 tree identifier;
15241
15242 cp_token *token = cp_lexer_peek_token (parser->lexer);
15243 identifier = cp_parser_identifier (parser);
15244 if (identifier == error_mark_node)
15245 return error_mark_node;
15246
15247 /* Look up the type-name. */
15248 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15249
15250 type_decl = strip_using_decl (type_decl);
15251
15252 if (TREE_CODE (type_decl) != TYPE_DECL
15253 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15254 {
15255 /* See if this is an Objective-C type. */
15256 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15257 tree type = objc_get_protocol_qualified_type (identifier, protos);
15258 if (type)
15259 type_decl = TYPE_NAME (type);
15260 }
15261
15262 /* Issue an error if we did not find a type-name. */
15263 if (TREE_CODE (type_decl) != TYPE_DECL
15264 /* In Objective-C, we have the complication that class names are
15265 normally type names and start declarations (eg, the
15266 "NSObject" in "NSObject *object;"), but can be used in an
15267 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15268 is an expression. So, a classname followed by a dot is not a
15269 valid type-name. */
15270 || (objc_is_class_name (TREE_TYPE (type_decl))
15271 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15272 {
15273 if (!cp_parser_simulate_error (parser))
15274 cp_parser_name_lookup_error (parser, identifier, type_decl,
15275 NLE_TYPE, token->location);
15276 return error_mark_node;
15277 }
15278 /* Remember that the name was used in the definition of the
15279 current class so that we can check later to see if the
15280 meaning would have been different after the class was
15281 entirely defined. */
15282 else if (type_decl != error_mark_node
15283 && !parser->scope)
15284 maybe_note_name_used_in_class (identifier, type_decl);
15285
15286 return type_decl;
15287 }
15288
15289 /* Parse an elaborated-type-specifier. Note that the grammar given
15290 here incorporates the resolution to DR68.
15291
15292 elaborated-type-specifier:
15293 class-key :: [opt] nested-name-specifier [opt] identifier
15294 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15295 enum-key :: [opt] nested-name-specifier [opt] identifier
15296 typename :: [opt] nested-name-specifier identifier
15297 typename :: [opt] nested-name-specifier template [opt]
15298 template-id
15299
15300 GNU extension:
15301
15302 elaborated-type-specifier:
15303 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15304 class-key attributes :: [opt] nested-name-specifier [opt]
15305 template [opt] template-id
15306 enum attributes :: [opt] nested-name-specifier [opt] identifier
15307
15308 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15309 declared `friend'. If IS_DECLARATION is TRUE, then this
15310 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15311 something is being declared.
15312
15313 Returns the TYPE specified. */
15314
15315 static tree
15316 cp_parser_elaborated_type_specifier (cp_parser* parser,
15317 bool is_friend,
15318 bool is_declaration)
15319 {
15320 enum tag_types tag_type;
15321 tree identifier;
15322 tree type = NULL_TREE;
15323 tree attributes = NULL_TREE;
15324 tree globalscope;
15325 cp_token *token = NULL;
15326
15327 /* See if we're looking at the `enum' keyword. */
15328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15329 {
15330 /* Consume the `enum' token. */
15331 cp_lexer_consume_token (parser->lexer);
15332 /* Remember that it's an enumeration type. */
15333 tag_type = enum_type;
15334 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15335 enums) is used here. */
15336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15337 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15338 {
15339 pedwarn (input_location, 0, "elaborated-type-specifier "
15340 "for a scoped enum must not use the %<%D%> keyword",
15341 cp_lexer_peek_token (parser->lexer)->u.value);
15342 /* Consume the `struct' or `class' and parse it anyway. */
15343 cp_lexer_consume_token (parser->lexer);
15344 }
15345 /* Parse the attributes. */
15346 attributes = cp_parser_attributes_opt (parser);
15347 }
15348 /* Or, it might be `typename'. */
15349 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15350 RID_TYPENAME))
15351 {
15352 /* Consume the `typename' token. */
15353 cp_lexer_consume_token (parser->lexer);
15354 /* Remember that it's a `typename' type. */
15355 tag_type = typename_type;
15356 }
15357 /* Otherwise it must be a class-key. */
15358 else
15359 {
15360 tag_type = cp_parser_class_key (parser);
15361 if (tag_type == none_type)
15362 return error_mark_node;
15363 /* Parse the attributes. */
15364 attributes = cp_parser_attributes_opt (parser);
15365 }
15366
15367 /* Look for the `::' operator. */
15368 globalscope = cp_parser_global_scope_opt (parser,
15369 /*current_scope_valid_p=*/false);
15370 /* Look for the nested-name-specifier. */
15371 if (tag_type == typename_type && !globalscope)
15372 {
15373 if (!cp_parser_nested_name_specifier (parser,
15374 /*typename_keyword_p=*/true,
15375 /*check_dependency_p=*/true,
15376 /*type_p=*/true,
15377 is_declaration))
15378 return error_mark_node;
15379 }
15380 else
15381 /* Even though `typename' is not present, the proposed resolution
15382 to Core Issue 180 says that in `class A<T>::B', `B' should be
15383 considered a type-name, even if `A<T>' is dependent. */
15384 cp_parser_nested_name_specifier_opt (parser,
15385 /*typename_keyword_p=*/true,
15386 /*check_dependency_p=*/true,
15387 /*type_p=*/true,
15388 is_declaration);
15389 /* For everything but enumeration types, consider a template-id.
15390 For an enumeration type, consider only a plain identifier. */
15391 if (tag_type != enum_type)
15392 {
15393 bool template_p = false;
15394 tree decl;
15395
15396 /* Allow the `template' keyword. */
15397 template_p = cp_parser_optional_template_keyword (parser);
15398 /* If we didn't see `template', we don't know if there's a
15399 template-id or not. */
15400 if (!template_p)
15401 cp_parser_parse_tentatively (parser);
15402 /* Parse the template-id. */
15403 token = cp_lexer_peek_token (parser->lexer);
15404 decl = cp_parser_template_id (parser, template_p,
15405 /*check_dependency_p=*/true,
15406 tag_type,
15407 is_declaration);
15408 /* If we didn't find a template-id, look for an ordinary
15409 identifier. */
15410 if (!template_p && !cp_parser_parse_definitely (parser))
15411 ;
15412 /* We can get here when cp_parser_template_id, called by
15413 cp_parser_class_name with tag_type == none_type, succeeds
15414 and caches a BASELINK. Then, when called again here,
15415 instead of failing and returning an error_mark_node
15416 returns it (see template/typename17.C in C++11).
15417 ??? Could we diagnose this earlier? */
15418 else if (tag_type == typename_type && BASELINK_P (decl))
15419 {
15420 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15421 type = error_mark_node;
15422 }
15423 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15424 in effect, then we must assume that, upon instantiation, the
15425 template will correspond to a class. */
15426 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15427 && tag_type == typename_type)
15428 type = make_typename_type (parser->scope, decl,
15429 typename_type,
15430 /*complain=*/tf_error);
15431 /* If the `typename' keyword is in effect and DECL is not a type
15432 decl, then type is non existent. */
15433 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15434 ;
15435 else if (TREE_CODE (decl) == TYPE_DECL)
15436 type = check_elaborated_type_specifier (tag_type, decl,
15437 /*allow_template_p=*/true);
15438 else if (decl == error_mark_node)
15439 type = error_mark_node;
15440 }
15441
15442 if (!type)
15443 {
15444 token = cp_lexer_peek_token (parser->lexer);
15445 identifier = cp_parser_identifier (parser);
15446
15447 if (identifier == error_mark_node)
15448 {
15449 parser->scope = NULL_TREE;
15450 return error_mark_node;
15451 }
15452
15453 /* For a `typename', we needn't call xref_tag. */
15454 if (tag_type == typename_type
15455 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15456 return cp_parser_make_typename_type (parser, identifier,
15457 token->location);
15458
15459 /* Template parameter lists apply only if we are not within a
15460 function parameter list. */
15461 bool template_parm_lists_apply
15462 = parser->num_template_parameter_lists;
15463 if (template_parm_lists_apply)
15464 for (cp_binding_level *s = current_binding_level;
15465 s && s->kind != sk_template_parms;
15466 s = s->level_chain)
15467 if (s->kind == sk_function_parms)
15468 template_parm_lists_apply = false;
15469
15470 /* Look up a qualified name in the usual way. */
15471 if (parser->scope)
15472 {
15473 tree decl;
15474 tree ambiguous_decls;
15475
15476 decl = cp_parser_lookup_name (parser, identifier,
15477 tag_type,
15478 /*is_template=*/false,
15479 /*is_namespace=*/false,
15480 /*check_dependency=*/true,
15481 &ambiguous_decls,
15482 token->location);
15483
15484 /* If the lookup was ambiguous, an error will already have been
15485 issued. */
15486 if (ambiguous_decls)
15487 return error_mark_node;
15488
15489 /* If we are parsing friend declaration, DECL may be a
15490 TEMPLATE_DECL tree node here. However, we need to check
15491 whether this TEMPLATE_DECL results in valid code. Consider
15492 the following example:
15493
15494 namespace N {
15495 template <class T> class C {};
15496 }
15497 class X {
15498 template <class T> friend class N::C; // #1, valid code
15499 };
15500 template <class T> class Y {
15501 friend class N::C; // #2, invalid code
15502 };
15503
15504 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15505 name lookup of `N::C'. We see that friend declaration must
15506 be template for the code to be valid. Note that
15507 processing_template_decl does not work here since it is
15508 always 1 for the above two cases. */
15509
15510 decl = (cp_parser_maybe_treat_template_as_class
15511 (decl, /*tag_name_p=*/is_friend
15512 && template_parm_lists_apply));
15513
15514 if (TREE_CODE (decl) != TYPE_DECL)
15515 {
15516 cp_parser_diagnose_invalid_type_name (parser,
15517 identifier,
15518 token->location);
15519 return error_mark_node;
15520 }
15521
15522 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15523 {
15524 bool allow_template = (template_parm_lists_apply
15525 || DECL_SELF_REFERENCE_P (decl));
15526 type = check_elaborated_type_specifier (tag_type, decl,
15527 allow_template);
15528
15529 if (type == error_mark_node)
15530 return error_mark_node;
15531 }
15532
15533 /* Forward declarations of nested types, such as
15534
15535 class C1::C2;
15536 class C1::C2::C3;
15537
15538 are invalid unless all components preceding the final '::'
15539 are complete. If all enclosing types are complete, these
15540 declarations become merely pointless.
15541
15542 Invalid forward declarations of nested types are errors
15543 caught elsewhere in parsing. Those that are pointless arrive
15544 here. */
15545
15546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15547 && !is_friend && !processing_explicit_instantiation)
15548 warning (0, "declaration %qD does not declare anything", decl);
15549
15550 type = TREE_TYPE (decl);
15551 }
15552 else
15553 {
15554 /* An elaborated-type-specifier sometimes introduces a new type and
15555 sometimes names an existing type. Normally, the rule is that it
15556 introduces a new type only if there is not an existing type of
15557 the same name already in scope. For example, given:
15558
15559 struct S {};
15560 void f() { struct S s; }
15561
15562 the `struct S' in the body of `f' is the same `struct S' as in
15563 the global scope; the existing definition is used. However, if
15564 there were no global declaration, this would introduce a new
15565 local class named `S'.
15566
15567 An exception to this rule applies to the following code:
15568
15569 namespace N { struct S; }
15570
15571 Here, the elaborated-type-specifier names a new type
15572 unconditionally; even if there is already an `S' in the
15573 containing scope this declaration names a new type.
15574 This exception only applies if the elaborated-type-specifier
15575 forms the complete declaration:
15576
15577 [class.name]
15578
15579 A declaration consisting solely of `class-key identifier ;' is
15580 either a redeclaration of the name in the current scope or a
15581 forward declaration of the identifier as a class name. It
15582 introduces the name into the current scope.
15583
15584 We are in this situation precisely when the next token is a `;'.
15585
15586 An exception to the exception is that a `friend' declaration does
15587 *not* name a new type; i.e., given:
15588
15589 struct S { friend struct T; };
15590
15591 `T' is not a new type in the scope of `S'.
15592
15593 Also, `new struct S' or `sizeof (struct S)' never results in the
15594 definition of a new type; a new type can only be declared in a
15595 declaration context. */
15596
15597 tag_scope ts;
15598 bool template_p;
15599
15600 if (is_friend)
15601 /* Friends have special name lookup rules. */
15602 ts = ts_within_enclosing_non_class;
15603 else if (is_declaration
15604 && cp_lexer_next_token_is (parser->lexer,
15605 CPP_SEMICOLON))
15606 /* This is a `class-key identifier ;' */
15607 ts = ts_current;
15608 else
15609 ts = ts_global;
15610
15611 template_p =
15612 (template_parm_lists_apply
15613 && (cp_parser_next_token_starts_class_definition_p (parser)
15614 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15615 /* An unqualified name was used to reference this type, so
15616 there were no qualifying templates. */
15617 if (template_parm_lists_apply
15618 && !cp_parser_check_template_parameters (parser,
15619 /*num_templates=*/0,
15620 token->location,
15621 /*declarator=*/NULL))
15622 return error_mark_node;
15623 type = xref_tag (tag_type, identifier, ts, template_p);
15624 }
15625 }
15626
15627 if (type == error_mark_node)
15628 return error_mark_node;
15629
15630 /* Allow attributes on forward declarations of classes. */
15631 if (attributes)
15632 {
15633 if (TREE_CODE (type) == TYPENAME_TYPE)
15634 warning (OPT_Wattributes,
15635 "attributes ignored on uninstantiated type");
15636 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15637 && ! processing_explicit_instantiation)
15638 warning (OPT_Wattributes,
15639 "attributes ignored on template instantiation");
15640 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15641 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15642 else
15643 warning (OPT_Wattributes,
15644 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15645 }
15646
15647 if (tag_type != enum_type)
15648 {
15649 /* Indicate whether this class was declared as a `class' or as a
15650 `struct'. */
15651 if (TREE_CODE (type) == RECORD_TYPE)
15652 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15653 cp_parser_check_class_key (tag_type, type);
15654 }
15655
15656 /* A "<" cannot follow an elaborated type specifier. If that
15657 happens, the user was probably trying to form a template-id. */
15658 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15659 token->location);
15660
15661 return type;
15662 }
15663
15664 /* Parse an enum-specifier.
15665
15666 enum-specifier:
15667 enum-head { enumerator-list [opt] }
15668 enum-head { enumerator-list , } [C++0x]
15669
15670 enum-head:
15671 enum-key identifier [opt] enum-base [opt]
15672 enum-key nested-name-specifier identifier enum-base [opt]
15673
15674 enum-key:
15675 enum
15676 enum class [C++0x]
15677 enum struct [C++0x]
15678
15679 enum-base: [C++0x]
15680 : type-specifier-seq
15681
15682 opaque-enum-specifier:
15683 enum-key identifier enum-base [opt] ;
15684
15685 GNU Extensions:
15686 enum-key attributes[opt] identifier [opt] enum-base [opt]
15687 { enumerator-list [opt] }attributes[opt]
15688 enum-key attributes[opt] identifier [opt] enum-base [opt]
15689 { enumerator-list, }attributes[opt] [C++0x]
15690
15691 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15692 if the token stream isn't an enum-specifier after all. */
15693
15694 static tree
15695 cp_parser_enum_specifier (cp_parser* parser)
15696 {
15697 tree identifier;
15698 tree type = NULL_TREE;
15699 tree prev_scope;
15700 tree nested_name_specifier = NULL_TREE;
15701 tree attributes;
15702 bool scoped_enum_p = false;
15703 bool has_underlying_type = false;
15704 bool nested_being_defined = false;
15705 bool new_value_list = false;
15706 bool is_new_type = false;
15707 bool is_anonymous = false;
15708 tree underlying_type = NULL_TREE;
15709 cp_token *type_start_token = NULL;
15710 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15711
15712 parser->colon_corrects_to_scope_p = false;
15713
15714 /* Parse tentatively so that we can back up if we don't find a
15715 enum-specifier. */
15716 cp_parser_parse_tentatively (parser);
15717
15718 /* Caller guarantees that the current token is 'enum', an identifier
15719 possibly follows, and the token after that is an opening brace.
15720 If we don't have an identifier, fabricate an anonymous name for
15721 the enumeration being defined. */
15722 cp_lexer_consume_token (parser->lexer);
15723
15724 /* Parse the "class" or "struct", which indicates a scoped
15725 enumeration type in C++0x. */
15726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15727 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15728 {
15729 if (cxx_dialect < cxx11)
15730 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15731
15732 /* Consume the `struct' or `class' token. */
15733 cp_lexer_consume_token (parser->lexer);
15734
15735 scoped_enum_p = true;
15736 }
15737
15738 attributes = cp_parser_attributes_opt (parser);
15739
15740 /* Clear the qualification. */
15741 parser->scope = NULL_TREE;
15742 parser->qualifying_scope = NULL_TREE;
15743 parser->object_scope = NULL_TREE;
15744
15745 /* Figure out in what scope the declaration is being placed. */
15746 prev_scope = current_scope ();
15747
15748 type_start_token = cp_lexer_peek_token (parser->lexer);
15749
15750 push_deferring_access_checks (dk_no_check);
15751 nested_name_specifier
15752 = cp_parser_nested_name_specifier_opt (parser,
15753 /*typename_keyword_p=*/true,
15754 /*check_dependency_p=*/false,
15755 /*type_p=*/false,
15756 /*is_declaration=*/false);
15757
15758 if (nested_name_specifier)
15759 {
15760 tree name;
15761
15762 identifier = cp_parser_identifier (parser);
15763 name = cp_parser_lookup_name (parser, identifier,
15764 enum_type,
15765 /*is_template=*/false,
15766 /*is_namespace=*/false,
15767 /*check_dependency=*/true,
15768 /*ambiguous_decls=*/NULL,
15769 input_location);
15770 if (name && name != error_mark_node)
15771 {
15772 type = TREE_TYPE (name);
15773 if (TREE_CODE (type) == TYPENAME_TYPE)
15774 {
15775 /* Are template enums allowed in ISO? */
15776 if (template_parm_scope_p ())
15777 pedwarn (type_start_token->location, OPT_Wpedantic,
15778 "%qD is an enumeration template", name);
15779 /* ignore a typename reference, for it will be solved by name
15780 in start_enum. */
15781 type = NULL_TREE;
15782 }
15783 }
15784 else if (nested_name_specifier == error_mark_node)
15785 /* We already issued an error. */;
15786 else
15787 error_at (type_start_token->location,
15788 "%qD is not an enumerator-name", identifier);
15789 }
15790 else
15791 {
15792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15793 identifier = cp_parser_identifier (parser);
15794 else
15795 {
15796 identifier = make_anon_name ();
15797 is_anonymous = true;
15798 if (scoped_enum_p)
15799 error_at (type_start_token->location,
15800 "anonymous scoped enum is not allowed");
15801 }
15802 }
15803 pop_deferring_access_checks ();
15804
15805 /* Check for the `:' that denotes a specified underlying type in C++0x.
15806 Note that a ':' could also indicate a bitfield width, however. */
15807 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15808 {
15809 cp_decl_specifier_seq type_specifiers;
15810
15811 /* Consume the `:'. */
15812 cp_lexer_consume_token (parser->lexer);
15813
15814 /* Parse the type-specifier-seq. */
15815 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15816 /*is_trailing_return=*/false,
15817 &type_specifiers);
15818
15819 /* At this point this is surely not elaborated type specifier. */
15820 if (!cp_parser_parse_definitely (parser))
15821 return NULL_TREE;
15822
15823 if (cxx_dialect < cxx11)
15824 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15825
15826 has_underlying_type = true;
15827
15828 /* If that didn't work, stop. */
15829 if (type_specifiers.type != error_mark_node)
15830 {
15831 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15832 /*initialized=*/0, NULL);
15833 if (underlying_type == error_mark_node
15834 || check_for_bare_parameter_packs (underlying_type))
15835 underlying_type = NULL_TREE;
15836 }
15837 }
15838
15839 /* Look for the `{' but don't consume it yet. */
15840 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15841 {
15842 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15843 {
15844 cp_parser_error (parser, "expected %<{%>");
15845 if (has_underlying_type)
15846 {
15847 type = NULL_TREE;
15848 goto out;
15849 }
15850 }
15851 /* An opaque-enum-specifier must have a ';' here. */
15852 if ((scoped_enum_p || underlying_type)
15853 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15854 {
15855 cp_parser_error (parser, "expected %<;%> or %<{%>");
15856 if (has_underlying_type)
15857 {
15858 type = NULL_TREE;
15859 goto out;
15860 }
15861 }
15862 }
15863
15864 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15865 return NULL_TREE;
15866
15867 if (nested_name_specifier)
15868 {
15869 if (CLASS_TYPE_P (nested_name_specifier))
15870 {
15871 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15872 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15873 push_scope (nested_name_specifier);
15874 }
15875 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15876 {
15877 push_nested_namespace (nested_name_specifier);
15878 }
15879 }
15880
15881 /* Issue an error message if type-definitions are forbidden here. */
15882 if (!cp_parser_check_type_definition (parser))
15883 type = error_mark_node;
15884 else
15885 /* Create the new type. We do this before consuming the opening
15886 brace so the enum will be recorded as being on the line of its
15887 tag (or the 'enum' keyword, if there is no tag). */
15888 type = start_enum (identifier, type, underlying_type,
15889 scoped_enum_p, &is_new_type);
15890
15891 /* If the next token is not '{' it is an opaque-enum-specifier or an
15892 elaborated-type-specifier. */
15893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15894 {
15895 timevar_push (TV_PARSE_ENUM);
15896 if (nested_name_specifier
15897 && nested_name_specifier != error_mark_node)
15898 {
15899 /* The following catches invalid code such as:
15900 enum class S<int>::E { A, B, C }; */
15901 if (!processing_specialization
15902 && CLASS_TYPE_P (nested_name_specifier)
15903 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15904 error_at (type_start_token->location, "cannot add an enumerator "
15905 "list to a template instantiation");
15906
15907 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15908 {
15909 error_at (type_start_token->location,
15910 "%<%T::%E%> has not been declared",
15911 TYPE_CONTEXT (nested_name_specifier),
15912 nested_name_specifier);
15913 type = error_mark_node;
15914 }
15915 /* If that scope does not contain the scope in which the
15916 class was originally declared, the program is invalid. */
15917 else if (prev_scope && !is_ancestor (prev_scope,
15918 nested_name_specifier))
15919 {
15920 if (at_namespace_scope_p ())
15921 error_at (type_start_token->location,
15922 "declaration of %qD in namespace %qD which does not "
15923 "enclose %qD",
15924 type, prev_scope, nested_name_specifier);
15925 else
15926 error_at (type_start_token->location,
15927 "declaration of %qD in %qD which does not "
15928 "enclose %qD",
15929 type, prev_scope, nested_name_specifier);
15930 type = error_mark_node;
15931 }
15932 }
15933
15934 if (scoped_enum_p)
15935 begin_scope (sk_scoped_enum, type);
15936
15937 /* Consume the opening brace. */
15938 cp_lexer_consume_token (parser->lexer);
15939
15940 if (type == error_mark_node)
15941 ; /* Nothing to add */
15942 else if (OPAQUE_ENUM_P (type)
15943 || (cxx_dialect > cxx98 && processing_specialization))
15944 {
15945 new_value_list = true;
15946 SET_OPAQUE_ENUM_P (type, false);
15947 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15948 }
15949 else
15950 {
15951 error_at (type_start_token->location,
15952 "multiple definition of %q#T", type);
15953 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15954 "previous definition here");
15955 type = error_mark_node;
15956 }
15957
15958 if (type == error_mark_node)
15959 cp_parser_skip_to_end_of_block_or_statement (parser);
15960 /* If the next token is not '}', then there are some enumerators. */
15961 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15962 {
15963 if (is_anonymous && !scoped_enum_p)
15964 pedwarn (type_start_token->location, OPT_Wpedantic,
15965 "ISO C++ forbids empty anonymous enum");
15966 }
15967 else
15968 cp_parser_enumerator_list (parser, type);
15969
15970 /* Consume the final '}'. */
15971 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15972
15973 if (scoped_enum_p)
15974 finish_scope ();
15975 timevar_pop (TV_PARSE_ENUM);
15976 }
15977 else
15978 {
15979 /* If a ';' follows, then it is an opaque-enum-specifier
15980 and additional restrictions apply. */
15981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15982 {
15983 if (is_anonymous)
15984 error_at (type_start_token->location,
15985 "opaque-enum-specifier without name");
15986 else if (nested_name_specifier)
15987 error_at (type_start_token->location,
15988 "opaque-enum-specifier must use a simple identifier");
15989 }
15990 }
15991
15992 /* Look for trailing attributes to apply to this enumeration, and
15993 apply them if appropriate. */
15994 if (cp_parser_allow_gnu_extensions_p (parser))
15995 {
15996 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15997 trailing_attr = chainon (trailing_attr, attributes);
15998 cplus_decl_attributes (&type,
15999 trailing_attr,
16000 (int) ATTR_FLAG_TYPE_IN_PLACE);
16001 }
16002
16003 /* Finish up the enumeration. */
16004 if (type != error_mark_node)
16005 {
16006 if (new_value_list)
16007 finish_enum_value_list (type);
16008 if (is_new_type)
16009 finish_enum (type);
16010 }
16011
16012 if (nested_name_specifier)
16013 {
16014 if (CLASS_TYPE_P (nested_name_specifier))
16015 {
16016 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16017 pop_scope (nested_name_specifier);
16018 }
16019 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16020 {
16021 pop_nested_namespace (nested_name_specifier);
16022 }
16023 }
16024 out:
16025 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16026 return type;
16027 }
16028
16029 /* Parse an enumerator-list. The enumerators all have the indicated
16030 TYPE.
16031
16032 enumerator-list:
16033 enumerator-definition
16034 enumerator-list , enumerator-definition */
16035
16036 static void
16037 cp_parser_enumerator_list (cp_parser* parser, tree type)
16038 {
16039 while (true)
16040 {
16041 /* Parse an enumerator-definition. */
16042 cp_parser_enumerator_definition (parser, type);
16043
16044 /* If the next token is not a ',', we've reached the end of
16045 the list. */
16046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16047 break;
16048 /* Otherwise, consume the `,' and keep going. */
16049 cp_lexer_consume_token (parser->lexer);
16050 /* If the next token is a `}', there is a trailing comma. */
16051 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16052 {
16053 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16054 pedwarn (input_location, OPT_Wpedantic,
16055 "comma at end of enumerator list");
16056 break;
16057 }
16058 }
16059 }
16060
16061 /* Parse an enumerator-definition. The enumerator has the indicated
16062 TYPE.
16063
16064 enumerator-definition:
16065 enumerator
16066 enumerator = constant-expression
16067
16068 enumerator:
16069 identifier */
16070
16071 static void
16072 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16073 {
16074 tree identifier;
16075 tree value;
16076 location_t loc;
16077
16078 /* Save the input location because we are interested in the location
16079 of the identifier and not the location of the explicit value. */
16080 loc = cp_lexer_peek_token (parser->lexer)->location;
16081
16082 /* Look for the identifier. */
16083 identifier = cp_parser_identifier (parser);
16084 if (identifier == error_mark_node)
16085 return;
16086
16087 /* If the next token is an '=', then there is an explicit value. */
16088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16089 {
16090 /* Consume the `=' token. */
16091 cp_lexer_consume_token (parser->lexer);
16092 /* Parse the value. */
16093 value = cp_parser_constant_expression (parser);
16094 }
16095 else
16096 value = NULL_TREE;
16097
16098 /* If we are processing a template, make sure the initializer of the
16099 enumerator doesn't contain any bare template parameter pack. */
16100 if (check_for_bare_parameter_packs (value))
16101 value = error_mark_node;
16102
16103 /* Create the enumerator. */
16104 build_enumerator (identifier, value, type, loc);
16105 }
16106
16107 /* Parse a namespace-name.
16108
16109 namespace-name:
16110 original-namespace-name
16111 namespace-alias
16112
16113 Returns the NAMESPACE_DECL for the namespace. */
16114
16115 static tree
16116 cp_parser_namespace_name (cp_parser* parser)
16117 {
16118 tree identifier;
16119 tree namespace_decl;
16120
16121 cp_token *token = cp_lexer_peek_token (parser->lexer);
16122
16123 /* Get the name of the namespace. */
16124 identifier = cp_parser_identifier (parser);
16125 if (identifier == error_mark_node)
16126 return error_mark_node;
16127
16128 /* Look up the identifier in the currently active scope. Look only
16129 for namespaces, due to:
16130
16131 [basic.lookup.udir]
16132
16133 When looking up a namespace-name in a using-directive or alias
16134 definition, only namespace names are considered.
16135
16136 And:
16137
16138 [basic.lookup.qual]
16139
16140 During the lookup of a name preceding the :: scope resolution
16141 operator, object, function, and enumerator names are ignored.
16142
16143 (Note that cp_parser_qualifying_entity only calls this
16144 function if the token after the name is the scope resolution
16145 operator.) */
16146 namespace_decl = cp_parser_lookup_name (parser, identifier,
16147 none_type,
16148 /*is_template=*/false,
16149 /*is_namespace=*/true,
16150 /*check_dependency=*/true,
16151 /*ambiguous_decls=*/NULL,
16152 token->location);
16153 /* If it's not a namespace, issue an error. */
16154 if (namespace_decl == error_mark_node
16155 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16156 {
16157 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16158 error_at (token->location, "%qD is not a namespace-name", identifier);
16159 cp_parser_error (parser, "expected namespace-name");
16160 namespace_decl = error_mark_node;
16161 }
16162
16163 return namespace_decl;
16164 }
16165
16166 /* Parse a namespace-definition.
16167
16168 namespace-definition:
16169 named-namespace-definition
16170 unnamed-namespace-definition
16171
16172 named-namespace-definition:
16173 original-namespace-definition
16174 extension-namespace-definition
16175
16176 original-namespace-definition:
16177 namespace identifier { namespace-body }
16178
16179 extension-namespace-definition:
16180 namespace original-namespace-name { namespace-body }
16181
16182 unnamed-namespace-definition:
16183 namespace { namespace-body } */
16184
16185 static void
16186 cp_parser_namespace_definition (cp_parser* parser)
16187 {
16188 tree identifier, attribs;
16189 bool has_visibility;
16190 bool is_inline;
16191
16192 cp_ensure_no_omp_declare_simd (parser);
16193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16194 {
16195 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16196 is_inline = true;
16197 cp_lexer_consume_token (parser->lexer);
16198 }
16199 else
16200 is_inline = false;
16201
16202 /* Look for the `namespace' keyword. */
16203 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16204
16205 /* Get the name of the namespace. We do not attempt to distinguish
16206 between an original-namespace-definition and an
16207 extension-namespace-definition at this point. The semantic
16208 analysis routines are responsible for that. */
16209 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16210 identifier = cp_parser_identifier (parser);
16211 else
16212 identifier = NULL_TREE;
16213
16214 /* Parse any specified attributes. */
16215 attribs = cp_parser_attributes_opt (parser);
16216
16217 /* Look for the `{' to start the namespace. */
16218 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16219 /* Start the namespace. */
16220 push_namespace (identifier);
16221
16222 /* "inline namespace" is equivalent to a stub namespace definition
16223 followed by a strong using directive. */
16224 if (is_inline)
16225 {
16226 tree name_space = current_namespace;
16227 /* Set up namespace association. */
16228 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16229 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16230 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16231 /* Import the contents of the inline namespace. */
16232 pop_namespace ();
16233 do_using_directive (name_space);
16234 push_namespace (identifier);
16235 }
16236
16237 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16238
16239 /* Parse the body of the namespace. */
16240 cp_parser_namespace_body (parser);
16241
16242 if (has_visibility)
16243 pop_visibility (1);
16244
16245 /* Finish the namespace. */
16246 pop_namespace ();
16247 /* Look for the final `}'. */
16248 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16249 }
16250
16251 /* Parse a namespace-body.
16252
16253 namespace-body:
16254 declaration-seq [opt] */
16255
16256 static void
16257 cp_parser_namespace_body (cp_parser* parser)
16258 {
16259 cp_parser_declaration_seq_opt (parser);
16260 }
16261
16262 /* Parse a namespace-alias-definition.
16263
16264 namespace-alias-definition:
16265 namespace identifier = qualified-namespace-specifier ; */
16266
16267 static void
16268 cp_parser_namespace_alias_definition (cp_parser* parser)
16269 {
16270 tree identifier;
16271 tree namespace_specifier;
16272
16273 cp_token *token = cp_lexer_peek_token (parser->lexer);
16274
16275 /* Look for the `namespace' keyword. */
16276 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16277 /* Look for the identifier. */
16278 identifier = cp_parser_identifier (parser);
16279 if (identifier == error_mark_node)
16280 return;
16281 /* Look for the `=' token. */
16282 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16283 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16284 {
16285 error_at (token->location, "%<namespace%> definition is not allowed here");
16286 /* Skip the definition. */
16287 cp_lexer_consume_token (parser->lexer);
16288 if (cp_parser_skip_to_closing_brace (parser))
16289 cp_lexer_consume_token (parser->lexer);
16290 return;
16291 }
16292 cp_parser_require (parser, CPP_EQ, RT_EQ);
16293 /* Look for the qualified-namespace-specifier. */
16294 namespace_specifier
16295 = cp_parser_qualified_namespace_specifier (parser);
16296 /* Look for the `;' token. */
16297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16298
16299 /* Register the alias in the symbol table. */
16300 do_namespace_alias (identifier, namespace_specifier);
16301 }
16302
16303 /* Parse a qualified-namespace-specifier.
16304
16305 qualified-namespace-specifier:
16306 :: [opt] nested-name-specifier [opt] namespace-name
16307
16308 Returns a NAMESPACE_DECL corresponding to the specified
16309 namespace. */
16310
16311 static tree
16312 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16313 {
16314 /* Look for the optional `::'. */
16315 cp_parser_global_scope_opt (parser,
16316 /*current_scope_valid_p=*/false);
16317
16318 /* Look for the optional nested-name-specifier. */
16319 cp_parser_nested_name_specifier_opt (parser,
16320 /*typename_keyword_p=*/false,
16321 /*check_dependency_p=*/true,
16322 /*type_p=*/false,
16323 /*is_declaration=*/true);
16324
16325 return cp_parser_namespace_name (parser);
16326 }
16327
16328 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16329 access declaration.
16330
16331 using-declaration:
16332 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16333 using :: unqualified-id ;
16334
16335 access-declaration:
16336 qualified-id ;
16337
16338 */
16339
16340 static bool
16341 cp_parser_using_declaration (cp_parser* parser,
16342 bool access_declaration_p)
16343 {
16344 cp_token *token;
16345 bool typename_p = false;
16346 bool global_scope_p;
16347 tree decl;
16348 tree identifier;
16349 tree qscope;
16350 int oldcount = errorcount;
16351 cp_token *diag_token = NULL;
16352
16353 if (access_declaration_p)
16354 {
16355 diag_token = cp_lexer_peek_token (parser->lexer);
16356 cp_parser_parse_tentatively (parser);
16357 }
16358 else
16359 {
16360 /* Look for the `using' keyword. */
16361 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16362
16363 /* Peek at the next token. */
16364 token = cp_lexer_peek_token (parser->lexer);
16365 /* See if it's `typename'. */
16366 if (token->keyword == RID_TYPENAME)
16367 {
16368 /* Remember that we've seen it. */
16369 typename_p = true;
16370 /* Consume the `typename' token. */
16371 cp_lexer_consume_token (parser->lexer);
16372 }
16373 }
16374
16375 /* Look for the optional global scope qualification. */
16376 global_scope_p
16377 = (cp_parser_global_scope_opt (parser,
16378 /*current_scope_valid_p=*/false)
16379 != NULL_TREE);
16380
16381 /* If we saw `typename', or didn't see `::', then there must be a
16382 nested-name-specifier present. */
16383 if (typename_p || !global_scope_p)
16384 {
16385 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16386 /*check_dependency_p=*/true,
16387 /*type_p=*/false,
16388 /*is_declaration=*/true);
16389 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16390 {
16391 cp_parser_skip_to_end_of_block_or_statement (parser);
16392 return false;
16393 }
16394 }
16395 /* Otherwise, we could be in either of the two productions. In that
16396 case, treat the nested-name-specifier as optional. */
16397 else
16398 qscope = cp_parser_nested_name_specifier_opt (parser,
16399 /*typename_keyword_p=*/false,
16400 /*check_dependency_p=*/true,
16401 /*type_p=*/false,
16402 /*is_declaration=*/true);
16403 if (!qscope)
16404 qscope = global_namespace;
16405 else if (UNSCOPED_ENUM_P (qscope))
16406 qscope = CP_TYPE_CONTEXT (qscope);
16407
16408 if (access_declaration_p && cp_parser_error_occurred (parser))
16409 /* Something has already gone wrong; there's no need to parse
16410 further. Since an error has occurred, the return value of
16411 cp_parser_parse_definitely will be false, as required. */
16412 return cp_parser_parse_definitely (parser);
16413
16414 token = cp_lexer_peek_token (parser->lexer);
16415 /* Parse the unqualified-id. */
16416 identifier = cp_parser_unqualified_id (parser,
16417 /*template_keyword_p=*/false,
16418 /*check_dependency_p=*/true,
16419 /*declarator_p=*/true,
16420 /*optional_p=*/false);
16421
16422 if (access_declaration_p)
16423 {
16424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16425 cp_parser_simulate_error (parser);
16426 if (!cp_parser_parse_definitely (parser))
16427 return false;
16428 }
16429
16430 /* The function we call to handle a using-declaration is different
16431 depending on what scope we are in. */
16432 if (qscope == error_mark_node || identifier == error_mark_node)
16433 ;
16434 else if (!identifier_p (identifier)
16435 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16436 /* [namespace.udecl]
16437
16438 A using declaration shall not name a template-id. */
16439 error_at (token->location,
16440 "a template-id may not appear in a using-declaration");
16441 else
16442 {
16443 if (at_class_scope_p ())
16444 {
16445 /* Create the USING_DECL. */
16446 decl = do_class_using_decl (parser->scope, identifier);
16447
16448 if (decl && typename_p)
16449 USING_DECL_TYPENAME_P (decl) = 1;
16450
16451 if (check_for_bare_parameter_packs (decl))
16452 {
16453 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16454 return false;
16455 }
16456 else
16457 /* Add it to the list of members in this class. */
16458 finish_member_declaration (decl);
16459 }
16460 else
16461 {
16462 decl = cp_parser_lookup_name_simple (parser,
16463 identifier,
16464 token->location);
16465 if (decl == error_mark_node)
16466 cp_parser_name_lookup_error (parser, identifier,
16467 decl, NLE_NULL,
16468 token->location);
16469 else if (check_for_bare_parameter_packs (decl))
16470 {
16471 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16472 return false;
16473 }
16474 else if (!at_namespace_scope_p ())
16475 do_local_using_decl (decl, qscope, identifier);
16476 else
16477 do_toplevel_using_decl (decl, qscope, identifier);
16478 }
16479 }
16480
16481 /* Look for the final `;'. */
16482 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16483
16484 if (access_declaration_p && errorcount == oldcount)
16485 warning_at (diag_token->location, OPT_Wdeprecated,
16486 "access declarations are deprecated "
16487 "in favour of using-declarations; "
16488 "suggestion: add the %<using%> keyword");
16489
16490 return true;
16491 }
16492
16493 /* Parse an alias-declaration.
16494
16495 alias-declaration:
16496 using identifier attribute-specifier-seq [opt] = type-id */
16497
16498 static tree
16499 cp_parser_alias_declaration (cp_parser* parser)
16500 {
16501 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16502 location_t id_location;
16503 cp_declarator *declarator;
16504 cp_decl_specifier_seq decl_specs;
16505 bool member_p;
16506 const char *saved_message = NULL;
16507
16508 /* Look for the `using' keyword. */
16509 cp_token *using_token
16510 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16511 if (using_token == NULL)
16512 return error_mark_node;
16513
16514 id_location = cp_lexer_peek_token (parser->lexer)->location;
16515 id = cp_parser_identifier (parser);
16516 if (id == error_mark_node)
16517 return error_mark_node;
16518
16519 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16520 attributes = cp_parser_attributes_opt (parser);
16521 if (attributes == error_mark_node)
16522 return error_mark_node;
16523
16524 cp_parser_require (parser, CPP_EQ, RT_EQ);
16525
16526 if (cp_parser_error_occurred (parser))
16527 return error_mark_node;
16528
16529 cp_parser_commit_to_tentative_parse (parser);
16530
16531 /* Now we are going to parse the type-id of the declaration. */
16532
16533 /*
16534 [dcl.type]/3 says:
16535
16536 "A type-specifier-seq shall not define a class or enumeration
16537 unless it appears in the type-id of an alias-declaration (7.1.3) that
16538 is not the declaration of a template-declaration."
16539
16540 In other words, if we currently are in an alias template, the
16541 type-id should not define a type.
16542
16543 So let's set parser->type_definition_forbidden_message in that
16544 case; cp_parser_check_type_definition (called by
16545 cp_parser_class_specifier) will then emit an error if a type is
16546 defined in the type-id. */
16547 if (parser->num_template_parameter_lists)
16548 {
16549 saved_message = parser->type_definition_forbidden_message;
16550 parser->type_definition_forbidden_message =
16551 G_("types may not be defined in alias template declarations");
16552 }
16553
16554 type = cp_parser_type_id (parser);
16555
16556 /* Restore the error message if need be. */
16557 if (parser->num_template_parameter_lists)
16558 parser->type_definition_forbidden_message = saved_message;
16559
16560 if (type == error_mark_node
16561 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16562 {
16563 cp_parser_skip_to_end_of_block_or_statement (parser);
16564 return error_mark_node;
16565 }
16566
16567 /* A typedef-name can also be introduced by an alias-declaration. The
16568 identifier following the using keyword becomes a typedef-name. It has
16569 the same semantics as if it were introduced by the typedef
16570 specifier. In particular, it does not define a new type and it shall
16571 not appear in the type-id. */
16572
16573 clear_decl_specs (&decl_specs);
16574 decl_specs.type = type;
16575 if (attributes != NULL_TREE)
16576 {
16577 decl_specs.attributes = attributes;
16578 set_and_check_decl_spec_loc (&decl_specs,
16579 ds_attribute,
16580 attrs_token);
16581 }
16582 set_and_check_decl_spec_loc (&decl_specs,
16583 ds_typedef,
16584 using_token);
16585 set_and_check_decl_spec_loc (&decl_specs,
16586 ds_alias,
16587 using_token);
16588
16589 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16590 declarator->id_loc = id_location;
16591
16592 member_p = at_class_scope_p ();
16593 if (member_p)
16594 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16595 NULL_TREE, attributes);
16596 else
16597 decl = start_decl (declarator, &decl_specs, 0,
16598 attributes, NULL_TREE, &pushed_scope);
16599 if (decl == error_mark_node)
16600 return decl;
16601
16602 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16603
16604 if (pushed_scope)
16605 pop_scope (pushed_scope);
16606
16607 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16608 added into the symbol table; otherwise, return the TYPE_DECL. */
16609 if (DECL_LANG_SPECIFIC (decl)
16610 && DECL_TEMPLATE_INFO (decl)
16611 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16612 {
16613 decl = DECL_TI_TEMPLATE (decl);
16614 if (member_p)
16615 check_member_template (decl);
16616 }
16617
16618 return decl;
16619 }
16620
16621 /* Parse a using-directive.
16622
16623 using-directive:
16624 using namespace :: [opt] nested-name-specifier [opt]
16625 namespace-name ; */
16626
16627 static void
16628 cp_parser_using_directive (cp_parser* parser)
16629 {
16630 tree namespace_decl;
16631 tree attribs;
16632
16633 /* Look for the `using' keyword. */
16634 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16635 /* And the `namespace' keyword. */
16636 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16637 /* Look for the optional `::' operator. */
16638 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16639 /* And the optional nested-name-specifier. */
16640 cp_parser_nested_name_specifier_opt (parser,
16641 /*typename_keyword_p=*/false,
16642 /*check_dependency_p=*/true,
16643 /*type_p=*/false,
16644 /*is_declaration=*/true);
16645 /* Get the namespace being used. */
16646 namespace_decl = cp_parser_namespace_name (parser);
16647 /* And any specified attributes. */
16648 attribs = cp_parser_attributes_opt (parser);
16649 /* Update the symbol table. */
16650 parse_using_directive (namespace_decl, attribs);
16651 /* Look for the final `;'. */
16652 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16653 }
16654
16655 /* Parse an asm-definition.
16656
16657 asm-definition:
16658 asm ( string-literal ) ;
16659
16660 GNU Extension:
16661
16662 asm-definition:
16663 asm volatile [opt] ( string-literal ) ;
16664 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16665 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16666 : asm-operand-list [opt] ) ;
16667 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16668 : asm-operand-list [opt]
16669 : asm-clobber-list [opt] ) ;
16670 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16671 : asm-clobber-list [opt]
16672 : asm-goto-list ) ; */
16673
16674 static void
16675 cp_parser_asm_definition (cp_parser* parser)
16676 {
16677 tree string;
16678 tree outputs = NULL_TREE;
16679 tree inputs = NULL_TREE;
16680 tree clobbers = NULL_TREE;
16681 tree labels = NULL_TREE;
16682 tree asm_stmt;
16683 bool volatile_p = false;
16684 bool extended_p = false;
16685 bool invalid_inputs_p = false;
16686 bool invalid_outputs_p = false;
16687 bool goto_p = false;
16688 required_token missing = RT_NONE;
16689
16690 /* Look for the `asm' keyword. */
16691 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16692
16693 if (parser->in_function_body
16694 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16695 {
16696 error ("%<asm%> in %<constexpr%> function");
16697 cp_function_chain->invalid_constexpr = true;
16698 }
16699
16700 /* See if the next token is `volatile'. */
16701 if (cp_parser_allow_gnu_extensions_p (parser)
16702 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16703 {
16704 /* Remember that we saw the `volatile' keyword. */
16705 volatile_p = true;
16706 /* Consume the token. */
16707 cp_lexer_consume_token (parser->lexer);
16708 }
16709 if (cp_parser_allow_gnu_extensions_p (parser)
16710 && parser->in_function_body
16711 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16712 {
16713 /* Remember that we saw the `goto' keyword. */
16714 goto_p = true;
16715 /* Consume the token. */
16716 cp_lexer_consume_token (parser->lexer);
16717 }
16718 /* Look for the opening `('. */
16719 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16720 return;
16721 /* Look for the string. */
16722 string = cp_parser_string_literal (parser, false, false);
16723 if (string == error_mark_node)
16724 {
16725 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16726 /*consume_paren=*/true);
16727 return;
16728 }
16729
16730 /* If we're allowing GNU extensions, check for the extended assembly
16731 syntax. Unfortunately, the `:' tokens need not be separated by
16732 a space in C, and so, for compatibility, we tolerate that here
16733 too. Doing that means that we have to treat the `::' operator as
16734 two `:' tokens. */
16735 if (cp_parser_allow_gnu_extensions_p (parser)
16736 && parser->in_function_body
16737 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16738 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16739 {
16740 bool inputs_p = false;
16741 bool clobbers_p = false;
16742 bool labels_p = false;
16743
16744 /* The extended syntax was used. */
16745 extended_p = true;
16746
16747 /* Look for outputs. */
16748 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16749 {
16750 /* Consume the `:'. */
16751 cp_lexer_consume_token (parser->lexer);
16752 /* Parse the output-operands. */
16753 if (cp_lexer_next_token_is_not (parser->lexer,
16754 CPP_COLON)
16755 && cp_lexer_next_token_is_not (parser->lexer,
16756 CPP_SCOPE)
16757 && cp_lexer_next_token_is_not (parser->lexer,
16758 CPP_CLOSE_PAREN)
16759 && !goto_p)
16760 outputs = cp_parser_asm_operand_list (parser);
16761
16762 if (outputs == error_mark_node)
16763 invalid_outputs_p = true;
16764 }
16765 /* If the next token is `::', there are no outputs, and the
16766 next token is the beginning of the inputs. */
16767 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16768 /* The inputs are coming next. */
16769 inputs_p = true;
16770
16771 /* Look for inputs. */
16772 if (inputs_p
16773 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16774 {
16775 /* Consume the `:' or `::'. */
16776 cp_lexer_consume_token (parser->lexer);
16777 /* Parse the output-operands. */
16778 if (cp_lexer_next_token_is_not (parser->lexer,
16779 CPP_COLON)
16780 && cp_lexer_next_token_is_not (parser->lexer,
16781 CPP_SCOPE)
16782 && cp_lexer_next_token_is_not (parser->lexer,
16783 CPP_CLOSE_PAREN))
16784 inputs = cp_parser_asm_operand_list (parser);
16785
16786 if (inputs == error_mark_node)
16787 invalid_inputs_p = true;
16788 }
16789 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16790 /* The clobbers are coming next. */
16791 clobbers_p = true;
16792
16793 /* Look for clobbers. */
16794 if (clobbers_p
16795 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16796 {
16797 clobbers_p = true;
16798 /* Consume the `:' or `::'. */
16799 cp_lexer_consume_token (parser->lexer);
16800 /* Parse the clobbers. */
16801 if (cp_lexer_next_token_is_not (parser->lexer,
16802 CPP_COLON)
16803 && cp_lexer_next_token_is_not (parser->lexer,
16804 CPP_CLOSE_PAREN))
16805 clobbers = cp_parser_asm_clobber_list (parser);
16806 }
16807 else if (goto_p
16808 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16809 /* The labels are coming next. */
16810 labels_p = true;
16811
16812 /* Look for labels. */
16813 if (labels_p
16814 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16815 {
16816 labels_p = true;
16817 /* Consume the `:' or `::'. */
16818 cp_lexer_consume_token (parser->lexer);
16819 /* Parse the labels. */
16820 labels = cp_parser_asm_label_list (parser);
16821 }
16822
16823 if (goto_p && !labels_p)
16824 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16825 }
16826 else if (goto_p)
16827 missing = RT_COLON_SCOPE;
16828
16829 /* Look for the closing `)'. */
16830 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16831 missing ? missing : RT_CLOSE_PAREN))
16832 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16833 /*consume_paren=*/true);
16834 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16835
16836 if (!invalid_inputs_p && !invalid_outputs_p)
16837 {
16838 /* Create the ASM_EXPR. */
16839 if (parser->in_function_body)
16840 {
16841 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16842 inputs, clobbers, labels);
16843 /* If the extended syntax was not used, mark the ASM_EXPR. */
16844 if (!extended_p)
16845 {
16846 tree temp = asm_stmt;
16847 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16848 temp = TREE_OPERAND (temp, 0);
16849
16850 ASM_INPUT_P (temp) = 1;
16851 }
16852 }
16853 else
16854 symtab->finalize_toplevel_asm (string);
16855 }
16856 }
16857
16858 /* Declarators [gram.dcl.decl] */
16859
16860 /* Parse an init-declarator.
16861
16862 init-declarator:
16863 declarator initializer [opt]
16864
16865 GNU Extension:
16866
16867 init-declarator:
16868 declarator asm-specification [opt] attributes [opt] initializer [opt]
16869
16870 function-definition:
16871 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16872 function-body
16873 decl-specifier-seq [opt] declarator function-try-block
16874
16875 GNU Extension:
16876
16877 function-definition:
16878 __extension__ function-definition
16879
16880 TM Extension:
16881
16882 function-definition:
16883 decl-specifier-seq [opt] declarator function-transaction-block
16884
16885 The DECL_SPECIFIERS apply to this declarator. Returns a
16886 representation of the entity declared. If MEMBER_P is TRUE, then
16887 this declarator appears in a class scope. The new DECL created by
16888 this declarator is returned.
16889
16890 The CHECKS are access checks that should be performed once we know
16891 what entity is being declared (and, therefore, what classes have
16892 befriended it).
16893
16894 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16895 for a function-definition here as well. If the declarator is a
16896 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16897 be TRUE upon return. By that point, the function-definition will
16898 have been completely parsed.
16899
16900 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16901 is FALSE.
16902
16903 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16904 parsed declaration if it is an uninitialized single declarator not followed
16905 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16906 if present, will not be consumed. If returned, this declarator will be
16907 created with SD_INITIALIZED but will not call cp_finish_decl.
16908
16909 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16910 and there is an initializer, the pointed location_t is set to the
16911 location of the '=' or `(', or '{' in C++11 token introducing the
16912 initializer. */
16913
16914 static tree
16915 cp_parser_init_declarator (cp_parser* parser,
16916 cp_decl_specifier_seq *decl_specifiers,
16917 vec<deferred_access_check, va_gc> *checks,
16918 bool function_definition_allowed_p,
16919 bool member_p,
16920 int declares_class_or_enum,
16921 bool* function_definition_p,
16922 tree* maybe_range_for_decl,
16923 location_t* init_loc)
16924 {
16925 cp_token *token = NULL, *asm_spec_start_token = NULL,
16926 *attributes_start_token = NULL;
16927 cp_declarator *declarator;
16928 tree prefix_attributes;
16929 tree attributes = NULL;
16930 tree asm_specification;
16931 tree initializer;
16932 tree decl = NULL_TREE;
16933 tree scope;
16934 int is_initialized;
16935 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16936 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16937 "(...)". */
16938 enum cpp_ttype initialization_kind;
16939 bool is_direct_init = false;
16940 bool is_non_constant_init;
16941 int ctor_dtor_or_conv_p;
16942 bool friend_p = cp_parser_friend_p (decl_specifiers);
16943 tree pushed_scope = NULL_TREE;
16944 bool range_for_decl_p = false;
16945 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16946 location_t tmp_init_loc = UNKNOWN_LOCATION;
16947
16948 /* Gather the attributes that were provided with the
16949 decl-specifiers. */
16950 prefix_attributes = decl_specifiers->attributes;
16951
16952 /* Assume that this is not the declarator for a function
16953 definition. */
16954 if (function_definition_p)
16955 *function_definition_p = false;
16956
16957 /* Default arguments are only permitted for function parameters. */
16958 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16959 parser->default_arg_ok_p = false;
16960
16961 /* Defer access checks while parsing the declarator; we cannot know
16962 what names are accessible until we know what is being
16963 declared. */
16964 resume_deferring_access_checks ();
16965
16966 /* Parse the declarator. */
16967 token = cp_lexer_peek_token (parser->lexer);
16968 declarator
16969 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16970 &ctor_dtor_or_conv_p,
16971 /*parenthesized_p=*/NULL,
16972 member_p, friend_p);
16973 /* Gather up the deferred checks. */
16974 stop_deferring_access_checks ();
16975
16976 parser->default_arg_ok_p = saved_default_arg_ok_p;
16977
16978 /* If the DECLARATOR was erroneous, there's no need to go
16979 further. */
16980 if (declarator == cp_error_declarator)
16981 return error_mark_node;
16982
16983 /* Check that the number of template-parameter-lists is OK. */
16984 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16985 token->location))
16986 return error_mark_node;
16987
16988 if (declares_class_or_enum & 2)
16989 cp_parser_check_for_definition_in_return_type (declarator,
16990 decl_specifiers->type,
16991 decl_specifiers->locations[ds_type_spec]);
16992
16993 /* Figure out what scope the entity declared by the DECLARATOR is
16994 located in. `grokdeclarator' sometimes changes the scope, so
16995 we compute it now. */
16996 scope = get_scope_of_declarator (declarator);
16997
16998 /* Perform any lookups in the declared type which were thought to be
16999 dependent, but are not in the scope of the declarator. */
17000 decl_specifiers->type
17001 = maybe_update_decl_type (decl_specifiers->type, scope);
17002
17003 /* If we're allowing GNU extensions, look for an
17004 asm-specification. */
17005 if (cp_parser_allow_gnu_extensions_p (parser))
17006 {
17007 /* Look for an asm-specification. */
17008 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17009 asm_specification = cp_parser_asm_specification_opt (parser);
17010 }
17011 else
17012 asm_specification = NULL_TREE;
17013
17014 /* Look for attributes. */
17015 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17016 attributes = cp_parser_attributes_opt (parser);
17017
17018 /* Peek at the next token. */
17019 token = cp_lexer_peek_token (parser->lexer);
17020
17021 bool bogus_implicit_tmpl = false;
17022
17023 if (function_declarator_p (declarator))
17024 {
17025 /* Check to see if the token indicates the start of a
17026 function-definition. */
17027 if (cp_parser_token_starts_function_definition_p (token))
17028 {
17029 if (!function_definition_allowed_p)
17030 {
17031 /* If a function-definition should not appear here, issue an
17032 error message. */
17033 cp_parser_error (parser,
17034 "a function-definition is not allowed here");
17035 return error_mark_node;
17036 }
17037
17038 location_t func_brace_location
17039 = cp_lexer_peek_token (parser->lexer)->location;
17040
17041 /* Neither attributes nor an asm-specification are allowed
17042 on a function-definition. */
17043 if (asm_specification)
17044 error_at (asm_spec_start_token->location,
17045 "an asm-specification is not allowed "
17046 "on a function-definition");
17047 if (attributes)
17048 error_at (attributes_start_token->location,
17049 "attributes are not allowed "
17050 "on a function-definition");
17051 /* This is a function-definition. */
17052 *function_definition_p = true;
17053
17054 /* Parse the function definition. */
17055 if (member_p)
17056 decl = cp_parser_save_member_function_body (parser,
17057 decl_specifiers,
17058 declarator,
17059 prefix_attributes);
17060 else
17061 decl =
17062 (cp_parser_function_definition_from_specifiers_and_declarator
17063 (parser, decl_specifiers, prefix_attributes, declarator));
17064
17065 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17066 {
17067 /* This is where the prologue starts... */
17068 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17069 = func_brace_location;
17070 }
17071
17072 return decl;
17073 }
17074 }
17075 else if (parser->fully_implicit_function_template_p)
17076 {
17077 /* A non-template declaration involving a function parameter list
17078 containing an implicit template parameter will be made into a
17079 template. If the resulting declaration is not going to be an
17080 actual function then finish the template scope here to prevent it.
17081 An error message will be issued once we have a decl to talk about.
17082
17083 FIXME probably we should do type deduction rather than create an
17084 implicit template, but the standard currently doesn't allow it. */
17085 bogus_implicit_tmpl = true;
17086 finish_fully_implicit_template (parser, NULL_TREE);
17087 }
17088
17089 /* [dcl.dcl]
17090
17091 Only in function declarations for constructors, destructors, and
17092 type conversions can the decl-specifier-seq be omitted.
17093
17094 We explicitly postpone this check past the point where we handle
17095 function-definitions because we tolerate function-definitions
17096 that are missing their return types in some modes. */
17097 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17098 {
17099 cp_parser_error (parser,
17100 "expected constructor, destructor, or type conversion");
17101 return error_mark_node;
17102 }
17103
17104 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17105 if (token->type == CPP_EQ
17106 || token->type == CPP_OPEN_PAREN
17107 || token->type == CPP_OPEN_BRACE)
17108 {
17109 is_initialized = SD_INITIALIZED;
17110 initialization_kind = token->type;
17111 if (maybe_range_for_decl)
17112 *maybe_range_for_decl = error_mark_node;
17113 tmp_init_loc = token->location;
17114 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17115 *init_loc = tmp_init_loc;
17116
17117 if (token->type == CPP_EQ
17118 && function_declarator_p (declarator))
17119 {
17120 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17121 if (t2->keyword == RID_DEFAULT)
17122 is_initialized = SD_DEFAULTED;
17123 else if (t2->keyword == RID_DELETE)
17124 is_initialized = SD_DELETED;
17125 }
17126 }
17127 else
17128 {
17129 /* If the init-declarator isn't initialized and isn't followed by a
17130 `,' or `;', it's not a valid init-declarator. */
17131 if (token->type != CPP_COMMA
17132 && token->type != CPP_SEMICOLON)
17133 {
17134 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17135 range_for_decl_p = true;
17136 else
17137 {
17138 if (!maybe_range_for_decl)
17139 cp_parser_error (parser, "expected initializer");
17140 return error_mark_node;
17141 }
17142 }
17143 is_initialized = SD_UNINITIALIZED;
17144 initialization_kind = CPP_EOF;
17145 }
17146
17147 /* Because start_decl has side-effects, we should only call it if we
17148 know we're going ahead. By this point, we know that we cannot
17149 possibly be looking at any other construct. */
17150 cp_parser_commit_to_tentative_parse (parser);
17151
17152 /* Enter the newly declared entry in the symbol table. If we're
17153 processing a declaration in a class-specifier, we wait until
17154 after processing the initializer. */
17155 if (!member_p)
17156 {
17157 if (parser->in_unbraced_linkage_specification_p)
17158 decl_specifiers->storage_class = sc_extern;
17159 decl = start_decl (declarator, decl_specifiers,
17160 range_for_decl_p? SD_INITIALIZED : is_initialized,
17161 attributes, prefix_attributes, &pushed_scope);
17162 cp_finalize_omp_declare_simd (parser, decl);
17163 /* Adjust location of decl if declarator->id_loc is more appropriate:
17164 set, and decl wasn't merged with another decl, in which case its
17165 location would be different from input_location, and more accurate. */
17166 if (DECL_P (decl)
17167 && declarator->id_loc != UNKNOWN_LOCATION
17168 && DECL_SOURCE_LOCATION (decl) == input_location)
17169 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17170 }
17171 else if (scope)
17172 /* Enter the SCOPE. That way unqualified names appearing in the
17173 initializer will be looked up in SCOPE. */
17174 pushed_scope = push_scope (scope);
17175
17176 /* Perform deferred access control checks, now that we know in which
17177 SCOPE the declared entity resides. */
17178 if (!member_p && decl)
17179 {
17180 tree saved_current_function_decl = NULL_TREE;
17181
17182 /* If the entity being declared is a function, pretend that we
17183 are in its scope. If it is a `friend', it may have access to
17184 things that would not otherwise be accessible. */
17185 if (TREE_CODE (decl) == FUNCTION_DECL)
17186 {
17187 saved_current_function_decl = current_function_decl;
17188 current_function_decl = decl;
17189 }
17190
17191 /* Perform access checks for template parameters. */
17192 cp_parser_perform_template_parameter_access_checks (checks);
17193
17194 /* Perform the access control checks for the declarator and the
17195 decl-specifiers. */
17196 perform_deferred_access_checks (tf_warning_or_error);
17197
17198 /* Restore the saved value. */
17199 if (TREE_CODE (decl) == FUNCTION_DECL)
17200 current_function_decl = saved_current_function_decl;
17201 }
17202
17203 /* Parse the initializer. */
17204 initializer = NULL_TREE;
17205 is_direct_init = false;
17206 is_non_constant_init = true;
17207 if (is_initialized)
17208 {
17209 if (function_declarator_p (declarator))
17210 {
17211 if (initialization_kind == CPP_EQ)
17212 initializer = cp_parser_pure_specifier (parser);
17213 else
17214 {
17215 /* If the declaration was erroneous, we don't really
17216 know what the user intended, so just silently
17217 consume the initializer. */
17218 if (decl != error_mark_node)
17219 error_at (tmp_init_loc, "initializer provided for function");
17220 cp_parser_skip_to_closing_parenthesis (parser,
17221 /*recovering=*/true,
17222 /*or_comma=*/false,
17223 /*consume_paren=*/true);
17224 }
17225 }
17226 else
17227 {
17228 /* We want to record the extra mangling scope for in-class
17229 initializers of class members and initializers of static data
17230 member templates. The former involves deferring
17231 parsing of the initializer until end of class as with default
17232 arguments. So right here we only handle the latter. */
17233 if (!member_p && processing_template_decl)
17234 start_lambda_scope (decl);
17235 initializer = cp_parser_initializer (parser,
17236 &is_direct_init,
17237 &is_non_constant_init);
17238 if (!member_p && processing_template_decl)
17239 finish_lambda_scope ();
17240 if (initializer == error_mark_node)
17241 cp_parser_skip_to_end_of_statement (parser);
17242 }
17243 }
17244
17245 /* The old parser allows attributes to appear after a parenthesized
17246 initializer. Mark Mitchell proposed removing this functionality
17247 on the GCC mailing lists on 2002-08-13. This parser accepts the
17248 attributes -- but ignores them. */
17249 if (cp_parser_allow_gnu_extensions_p (parser)
17250 && initialization_kind == CPP_OPEN_PAREN)
17251 if (cp_parser_attributes_opt (parser))
17252 warning (OPT_Wattributes,
17253 "attributes after parenthesized initializer ignored");
17254
17255 /* And now complain about a non-function implicit template. */
17256 if (bogus_implicit_tmpl)
17257 error_at (DECL_SOURCE_LOCATION (decl),
17258 "non-function %qD declared as implicit template", decl);
17259
17260 /* For an in-class declaration, use `grokfield' to create the
17261 declaration. */
17262 if (member_p)
17263 {
17264 if (pushed_scope)
17265 {
17266 pop_scope (pushed_scope);
17267 pushed_scope = NULL_TREE;
17268 }
17269 decl = grokfield (declarator, decl_specifiers,
17270 initializer, !is_non_constant_init,
17271 /*asmspec=*/NULL_TREE,
17272 chainon (attributes, prefix_attributes));
17273 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17274 cp_parser_save_default_args (parser, decl);
17275 cp_finalize_omp_declare_simd (parser, decl);
17276 }
17277
17278 /* Finish processing the declaration. But, skip member
17279 declarations. */
17280 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17281 {
17282 cp_finish_decl (decl,
17283 initializer, !is_non_constant_init,
17284 asm_specification,
17285 /* If the initializer is in parentheses, then this is
17286 a direct-initialization, which means that an
17287 `explicit' constructor is OK. Otherwise, an
17288 `explicit' constructor cannot be used. */
17289 ((is_direct_init || !is_initialized)
17290 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17291 }
17292 else if ((cxx_dialect != cxx98) && friend_p
17293 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17294 /* Core issue #226 (C++0x only): A default template-argument
17295 shall not be specified in a friend class template
17296 declaration. */
17297 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17298 /*is_partial=*/false, /*is_friend_decl=*/1);
17299
17300 if (!friend_p && pushed_scope)
17301 pop_scope (pushed_scope);
17302
17303 if (function_declarator_p (declarator)
17304 && parser->fully_implicit_function_template_p)
17305 {
17306 if (member_p)
17307 decl = finish_fully_implicit_template (parser, decl);
17308 else
17309 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17310 }
17311
17312 return decl;
17313 }
17314
17315 /* Parse a declarator.
17316
17317 declarator:
17318 direct-declarator
17319 ptr-operator declarator
17320
17321 abstract-declarator:
17322 ptr-operator abstract-declarator [opt]
17323 direct-abstract-declarator
17324
17325 GNU Extensions:
17326
17327 declarator:
17328 attributes [opt] direct-declarator
17329 attributes [opt] ptr-operator declarator
17330
17331 abstract-declarator:
17332 attributes [opt] ptr-operator abstract-declarator [opt]
17333 attributes [opt] direct-abstract-declarator
17334
17335 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17336 detect constructor, destructor or conversion operators. It is set
17337 to -1 if the declarator is a name, and +1 if it is a
17338 function. Otherwise it is set to zero. Usually you just want to
17339 test for >0, but internally the negative value is used.
17340
17341 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17342 a decl-specifier-seq unless it declares a constructor, destructor,
17343 or conversion. It might seem that we could check this condition in
17344 semantic analysis, rather than parsing, but that makes it difficult
17345 to handle something like `f()'. We want to notice that there are
17346 no decl-specifiers, and therefore realize that this is an
17347 expression, not a declaration.)
17348
17349 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17350 the declarator is a direct-declarator of the form "(...)".
17351
17352 MEMBER_P is true iff this declarator is a member-declarator.
17353
17354 FRIEND_P is true iff this declarator is a friend. */
17355
17356 static cp_declarator *
17357 cp_parser_declarator (cp_parser* parser,
17358 cp_parser_declarator_kind dcl_kind,
17359 int* ctor_dtor_or_conv_p,
17360 bool* parenthesized_p,
17361 bool member_p, bool friend_p)
17362 {
17363 cp_declarator *declarator;
17364 enum tree_code code;
17365 cp_cv_quals cv_quals;
17366 tree class_type;
17367 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17368
17369 /* Assume this is not a constructor, destructor, or type-conversion
17370 operator. */
17371 if (ctor_dtor_or_conv_p)
17372 *ctor_dtor_or_conv_p = 0;
17373
17374 if (cp_parser_allow_gnu_extensions_p (parser))
17375 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17376
17377 /* Check for the ptr-operator production. */
17378 cp_parser_parse_tentatively (parser);
17379 /* Parse the ptr-operator. */
17380 code = cp_parser_ptr_operator (parser,
17381 &class_type,
17382 &cv_quals,
17383 &std_attributes);
17384
17385 /* If that worked, then we have a ptr-operator. */
17386 if (cp_parser_parse_definitely (parser))
17387 {
17388 /* If a ptr-operator was found, then this declarator was not
17389 parenthesized. */
17390 if (parenthesized_p)
17391 *parenthesized_p = true;
17392 /* The dependent declarator is optional if we are parsing an
17393 abstract-declarator. */
17394 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17395 cp_parser_parse_tentatively (parser);
17396
17397 /* Parse the dependent declarator. */
17398 declarator = cp_parser_declarator (parser, dcl_kind,
17399 /*ctor_dtor_or_conv_p=*/NULL,
17400 /*parenthesized_p=*/NULL,
17401 /*member_p=*/false,
17402 friend_p);
17403
17404 /* If we are parsing an abstract-declarator, we must handle the
17405 case where the dependent declarator is absent. */
17406 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17407 && !cp_parser_parse_definitely (parser))
17408 declarator = NULL;
17409
17410 declarator = cp_parser_make_indirect_declarator
17411 (code, class_type, cv_quals, declarator, std_attributes);
17412 }
17413 /* Everything else is a direct-declarator. */
17414 else
17415 {
17416 if (parenthesized_p)
17417 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17418 CPP_OPEN_PAREN);
17419 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17420 ctor_dtor_or_conv_p,
17421 member_p, friend_p);
17422 }
17423
17424 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17425 declarator->attributes = gnu_attributes;
17426 return declarator;
17427 }
17428
17429 /* Parse a direct-declarator or direct-abstract-declarator.
17430
17431 direct-declarator:
17432 declarator-id
17433 direct-declarator ( parameter-declaration-clause )
17434 cv-qualifier-seq [opt]
17435 ref-qualifier [opt]
17436 exception-specification [opt]
17437 direct-declarator [ constant-expression [opt] ]
17438 ( declarator )
17439
17440 direct-abstract-declarator:
17441 direct-abstract-declarator [opt]
17442 ( parameter-declaration-clause )
17443 cv-qualifier-seq [opt]
17444 ref-qualifier [opt]
17445 exception-specification [opt]
17446 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17447 ( abstract-declarator )
17448
17449 Returns a representation of the declarator. DCL_KIND is
17450 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17451 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17452 we are parsing a direct-declarator. It is
17453 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17454 of ambiguity we prefer an abstract declarator, as per
17455 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17456 as for cp_parser_declarator. */
17457
17458 static cp_declarator *
17459 cp_parser_direct_declarator (cp_parser* parser,
17460 cp_parser_declarator_kind dcl_kind,
17461 int* ctor_dtor_or_conv_p,
17462 bool member_p, bool friend_p)
17463 {
17464 cp_token *token;
17465 cp_declarator *declarator = NULL;
17466 tree scope = NULL_TREE;
17467 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17468 bool saved_in_declarator_p = parser->in_declarator_p;
17469 bool first = true;
17470 tree pushed_scope = NULL_TREE;
17471
17472 while (true)
17473 {
17474 /* Peek at the next token. */
17475 token = cp_lexer_peek_token (parser->lexer);
17476 if (token->type == CPP_OPEN_PAREN)
17477 {
17478 /* This is either a parameter-declaration-clause, or a
17479 parenthesized declarator. When we know we are parsing a
17480 named declarator, it must be a parenthesized declarator
17481 if FIRST is true. For instance, `(int)' is a
17482 parameter-declaration-clause, with an omitted
17483 direct-abstract-declarator. But `((*))', is a
17484 parenthesized abstract declarator. Finally, when T is a
17485 template parameter `(T)' is a
17486 parameter-declaration-clause, and not a parenthesized
17487 named declarator.
17488
17489 We first try and parse a parameter-declaration-clause,
17490 and then try a nested declarator (if FIRST is true).
17491
17492 It is not an error for it not to be a
17493 parameter-declaration-clause, even when FIRST is
17494 false. Consider,
17495
17496 int i (int);
17497 int i (3);
17498
17499 The first is the declaration of a function while the
17500 second is the definition of a variable, including its
17501 initializer.
17502
17503 Having seen only the parenthesis, we cannot know which of
17504 these two alternatives should be selected. Even more
17505 complex are examples like:
17506
17507 int i (int (a));
17508 int i (int (3));
17509
17510 The former is a function-declaration; the latter is a
17511 variable initialization.
17512
17513 Thus again, we try a parameter-declaration-clause, and if
17514 that fails, we back out and return. */
17515
17516 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17517 {
17518 tree params;
17519 bool is_declarator = false;
17520
17521 /* In a member-declarator, the only valid interpretation
17522 of a parenthesis is the start of a
17523 parameter-declaration-clause. (It is invalid to
17524 initialize a static data member with a parenthesized
17525 initializer; only the "=" form of initialization is
17526 permitted.) */
17527 if (!member_p)
17528 cp_parser_parse_tentatively (parser);
17529
17530 /* Consume the `('. */
17531 cp_lexer_consume_token (parser->lexer);
17532 if (first)
17533 {
17534 /* If this is going to be an abstract declarator, we're
17535 in a declarator and we can't have default args. */
17536 parser->default_arg_ok_p = false;
17537 parser->in_declarator_p = true;
17538 }
17539
17540 begin_scope (sk_function_parms, NULL_TREE);
17541
17542 /* Parse the parameter-declaration-clause. */
17543 params = cp_parser_parameter_declaration_clause (parser);
17544
17545 /* Consume the `)'. */
17546 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17547
17548 /* If all went well, parse the cv-qualifier-seq,
17549 ref-qualifier and the exception-specification. */
17550 if (member_p || cp_parser_parse_definitely (parser))
17551 {
17552 cp_cv_quals cv_quals;
17553 cp_virt_specifiers virt_specifiers;
17554 cp_ref_qualifier ref_qual;
17555 tree exception_specification;
17556 tree late_return;
17557 tree attrs;
17558 bool memfn = (member_p || (pushed_scope
17559 && CLASS_TYPE_P (pushed_scope)));
17560
17561 is_declarator = true;
17562
17563 if (ctor_dtor_or_conv_p)
17564 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17565 first = false;
17566
17567 /* Parse the cv-qualifier-seq. */
17568 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17569 /* Parse the ref-qualifier. */
17570 ref_qual = cp_parser_ref_qualifier_opt (parser);
17571 /* And the exception-specification. */
17572 exception_specification
17573 = cp_parser_exception_specification_opt (parser);
17574
17575 attrs = cp_parser_std_attribute_spec_seq (parser);
17576
17577 /* In here, we handle cases where attribute is used after
17578 the function declaration. For example:
17579 void func (int x) __attribute__((vector(..))); */
17580 if (flag_cilkplus
17581 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17582 {
17583 cp_parser_parse_tentatively (parser);
17584 tree attr = cp_parser_gnu_attributes_opt (parser);
17585 if (cp_lexer_next_token_is_not (parser->lexer,
17586 CPP_SEMICOLON)
17587 && cp_lexer_next_token_is_not (parser->lexer,
17588 CPP_OPEN_BRACE))
17589 cp_parser_abort_tentative_parse (parser);
17590 else if (!cp_parser_parse_definitely (parser))
17591 ;
17592 else
17593 attrs = chainon (attr, attrs);
17594 }
17595 late_return = (cp_parser_late_return_type_opt
17596 (parser, declarator,
17597 memfn ? cv_quals : -1));
17598
17599
17600 /* Parse the virt-specifier-seq. */
17601 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17602
17603 /* Create the function-declarator. */
17604 declarator = make_call_declarator (declarator,
17605 params,
17606 cv_quals,
17607 virt_specifiers,
17608 ref_qual,
17609 exception_specification,
17610 late_return);
17611 declarator->std_attributes = attrs;
17612 /* Any subsequent parameter lists are to do with
17613 return type, so are not those of the declared
17614 function. */
17615 parser->default_arg_ok_p = false;
17616 }
17617
17618 /* Remove the function parms from scope. */
17619 pop_bindings_and_leave_scope ();
17620
17621 if (is_declarator)
17622 /* Repeat the main loop. */
17623 continue;
17624 }
17625
17626 /* If this is the first, we can try a parenthesized
17627 declarator. */
17628 if (first)
17629 {
17630 bool saved_in_type_id_in_expr_p;
17631
17632 parser->default_arg_ok_p = saved_default_arg_ok_p;
17633 parser->in_declarator_p = saved_in_declarator_p;
17634
17635 /* Consume the `('. */
17636 cp_lexer_consume_token (parser->lexer);
17637 /* Parse the nested declarator. */
17638 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17639 parser->in_type_id_in_expr_p = true;
17640 declarator
17641 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17642 /*parenthesized_p=*/NULL,
17643 member_p, friend_p);
17644 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17645 first = false;
17646 /* Expect a `)'. */
17647 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17648 declarator = cp_error_declarator;
17649 if (declarator == cp_error_declarator)
17650 break;
17651
17652 goto handle_declarator;
17653 }
17654 /* Otherwise, we must be done. */
17655 else
17656 break;
17657 }
17658 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17659 && token->type == CPP_OPEN_SQUARE
17660 && !cp_next_tokens_can_be_attribute_p (parser))
17661 {
17662 /* Parse an array-declarator. */
17663 tree bounds, attrs;
17664
17665 if (ctor_dtor_or_conv_p)
17666 *ctor_dtor_or_conv_p = 0;
17667
17668 first = false;
17669 parser->default_arg_ok_p = false;
17670 parser->in_declarator_p = true;
17671 /* Consume the `['. */
17672 cp_lexer_consume_token (parser->lexer);
17673 /* Peek at the next token. */
17674 token = cp_lexer_peek_token (parser->lexer);
17675 /* If the next token is `]', then there is no
17676 constant-expression. */
17677 if (token->type != CPP_CLOSE_SQUARE)
17678 {
17679 bool non_constant_p;
17680 bounds
17681 = cp_parser_constant_expression (parser,
17682 /*allow_non_constant=*/true,
17683 &non_constant_p);
17684 if (!non_constant_p)
17685 /* OK */;
17686 else if (error_operand_p (bounds))
17687 /* Already gave an error. */;
17688 else if (!parser->in_function_body
17689 || current_binding_level->kind == sk_function_parms)
17690 {
17691 /* Normally, the array bound must be an integral constant
17692 expression. However, as an extension, we allow VLAs
17693 in function scopes as long as they aren't part of a
17694 parameter declaration. */
17695 cp_parser_error (parser,
17696 "array bound is not an integer constant");
17697 bounds = error_mark_node;
17698 }
17699 else if (processing_template_decl
17700 && !type_dependent_expression_p (bounds))
17701 {
17702 /* Remember this wasn't a constant-expression. */
17703 bounds = build_nop (TREE_TYPE (bounds), bounds);
17704 TREE_SIDE_EFFECTS (bounds) = 1;
17705 }
17706 }
17707 else
17708 bounds = NULL_TREE;
17709 /* Look for the closing `]'. */
17710 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17711 {
17712 declarator = cp_error_declarator;
17713 break;
17714 }
17715
17716 attrs = cp_parser_std_attribute_spec_seq (parser);
17717 declarator = make_array_declarator (declarator, bounds);
17718 declarator->std_attributes = attrs;
17719 }
17720 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17721 {
17722 {
17723 tree qualifying_scope;
17724 tree unqualified_name;
17725 tree attrs;
17726 special_function_kind sfk;
17727 bool abstract_ok;
17728 bool pack_expansion_p = false;
17729 cp_token *declarator_id_start_token;
17730
17731 /* Parse a declarator-id */
17732 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17733 if (abstract_ok)
17734 {
17735 cp_parser_parse_tentatively (parser);
17736
17737 /* If we see an ellipsis, we should be looking at a
17738 parameter pack. */
17739 if (token->type == CPP_ELLIPSIS)
17740 {
17741 /* Consume the `...' */
17742 cp_lexer_consume_token (parser->lexer);
17743
17744 pack_expansion_p = true;
17745 }
17746 }
17747
17748 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17749 unqualified_name
17750 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17751 qualifying_scope = parser->scope;
17752 if (abstract_ok)
17753 {
17754 bool okay = false;
17755
17756 if (!unqualified_name && pack_expansion_p)
17757 {
17758 /* Check whether an error occurred. */
17759 okay = !cp_parser_error_occurred (parser);
17760
17761 /* We already consumed the ellipsis to mark a
17762 parameter pack, but we have no way to report it,
17763 so abort the tentative parse. We will be exiting
17764 immediately anyway. */
17765 cp_parser_abort_tentative_parse (parser);
17766 }
17767 else
17768 okay = cp_parser_parse_definitely (parser);
17769
17770 if (!okay)
17771 unqualified_name = error_mark_node;
17772 else if (unqualified_name
17773 && (qualifying_scope
17774 || (!identifier_p (unqualified_name))))
17775 {
17776 cp_parser_error (parser, "expected unqualified-id");
17777 unqualified_name = error_mark_node;
17778 }
17779 }
17780
17781 if (!unqualified_name)
17782 return NULL;
17783 if (unqualified_name == error_mark_node)
17784 {
17785 declarator = cp_error_declarator;
17786 pack_expansion_p = false;
17787 declarator->parameter_pack_p = false;
17788 break;
17789 }
17790
17791 attrs = cp_parser_std_attribute_spec_seq (parser);
17792
17793 if (qualifying_scope && at_namespace_scope_p ()
17794 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17795 {
17796 /* In the declaration of a member of a template class
17797 outside of the class itself, the SCOPE will sometimes
17798 be a TYPENAME_TYPE. For example, given:
17799
17800 template <typename T>
17801 int S<T>::R::i = 3;
17802
17803 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17804 this context, we must resolve S<T>::R to an ordinary
17805 type, rather than a typename type.
17806
17807 The reason we normally avoid resolving TYPENAME_TYPEs
17808 is that a specialization of `S' might render
17809 `S<T>::R' not a type. However, if `S' is
17810 specialized, then this `i' will not be used, so there
17811 is no harm in resolving the types here. */
17812 tree type;
17813
17814 /* Resolve the TYPENAME_TYPE. */
17815 type = resolve_typename_type (qualifying_scope,
17816 /*only_current_p=*/false);
17817 /* If that failed, the declarator is invalid. */
17818 if (TREE_CODE (type) == TYPENAME_TYPE)
17819 {
17820 if (typedef_variant_p (type))
17821 error_at (declarator_id_start_token->location,
17822 "cannot define member of dependent typedef "
17823 "%qT", type);
17824 else
17825 error_at (declarator_id_start_token->location,
17826 "%<%T::%E%> is not a type",
17827 TYPE_CONTEXT (qualifying_scope),
17828 TYPE_IDENTIFIER (qualifying_scope));
17829 }
17830 qualifying_scope = type;
17831 }
17832
17833 sfk = sfk_none;
17834
17835 if (unqualified_name)
17836 {
17837 tree class_type;
17838
17839 if (qualifying_scope
17840 && CLASS_TYPE_P (qualifying_scope))
17841 class_type = qualifying_scope;
17842 else
17843 class_type = current_class_type;
17844
17845 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17846 {
17847 tree name_type = TREE_TYPE (unqualified_name);
17848 if (class_type && same_type_p (name_type, class_type))
17849 {
17850 if (qualifying_scope
17851 && CLASSTYPE_USE_TEMPLATE (name_type))
17852 {
17853 error_at (declarator_id_start_token->location,
17854 "invalid use of constructor as a template");
17855 inform (declarator_id_start_token->location,
17856 "use %<%T::%D%> instead of %<%T::%D%> to "
17857 "name the constructor in a qualified name",
17858 class_type,
17859 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17860 class_type, name_type);
17861 declarator = cp_error_declarator;
17862 break;
17863 }
17864 else
17865 unqualified_name = constructor_name (class_type);
17866 }
17867 else
17868 {
17869 /* We do not attempt to print the declarator
17870 here because we do not have enough
17871 information about its original syntactic
17872 form. */
17873 cp_parser_error (parser, "invalid declarator");
17874 declarator = cp_error_declarator;
17875 break;
17876 }
17877 }
17878
17879 if (class_type)
17880 {
17881 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17882 sfk = sfk_destructor;
17883 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17884 sfk = sfk_conversion;
17885 else if (/* There's no way to declare a constructor
17886 for an anonymous type, even if the type
17887 got a name for linkage purposes. */
17888 !TYPE_WAS_ANONYMOUS (class_type)
17889 /* Handle correctly (c++/19200):
17890
17891 struct S {
17892 struct T{};
17893 friend void S(T);
17894 };
17895
17896 and also:
17897
17898 namespace N {
17899 void S();
17900 }
17901
17902 struct S {
17903 friend void N::S();
17904 }; */
17905 && !(friend_p
17906 && class_type != qualifying_scope)
17907 && constructor_name_p (unqualified_name,
17908 class_type))
17909 {
17910 unqualified_name = constructor_name (class_type);
17911 sfk = sfk_constructor;
17912 }
17913 else if (is_overloaded_fn (unqualified_name)
17914 && DECL_CONSTRUCTOR_P (get_first_fn
17915 (unqualified_name)))
17916 sfk = sfk_constructor;
17917
17918 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17919 *ctor_dtor_or_conv_p = -1;
17920 }
17921 }
17922 declarator = make_id_declarator (qualifying_scope,
17923 unqualified_name,
17924 sfk);
17925 declarator->std_attributes = attrs;
17926 declarator->id_loc = token->location;
17927 declarator->parameter_pack_p = pack_expansion_p;
17928
17929 if (pack_expansion_p)
17930 maybe_warn_variadic_templates ();
17931 }
17932
17933 handle_declarator:;
17934 scope = get_scope_of_declarator (declarator);
17935 if (scope)
17936 {
17937 /* Any names that appear after the declarator-id for a
17938 member are looked up in the containing scope. */
17939 if (at_function_scope_p ())
17940 {
17941 /* But declarations with qualified-ids can't appear in a
17942 function. */
17943 cp_parser_error (parser, "qualified-id in declaration");
17944 declarator = cp_error_declarator;
17945 break;
17946 }
17947 pushed_scope = push_scope (scope);
17948 }
17949 parser->in_declarator_p = true;
17950 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17951 || (declarator && declarator->kind == cdk_id))
17952 /* Default args are only allowed on function
17953 declarations. */
17954 parser->default_arg_ok_p = saved_default_arg_ok_p;
17955 else
17956 parser->default_arg_ok_p = false;
17957
17958 first = false;
17959 }
17960 /* We're done. */
17961 else
17962 break;
17963 }
17964
17965 /* For an abstract declarator, we might wind up with nothing at this
17966 point. That's an error; the declarator is not optional. */
17967 if (!declarator)
17968 cp_parser_error (parser, "expected declarator");
17969
17970 /* If we entered a scope, we must exit it now. */
17971 if (pushed_scope)
17972 pop_scope (pushed_scope);
17973
17974 parser->default_arg_ok_p = saved_default_arg_ok_p;
17975 parser->in_declarator_p = saved_in_declarator_p;
17976
17977 return declarator;
17978 }
17979
17980 /* Parse a ptr-operator.
17981
17982 ptr-operator:
17983 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17984 * cv-qualifier-seq [opt]
17985 &
17986 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17987 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17988
17989 GNU Extension:
17990
17991 ptr-operator:
17992 & cv-qualifier-seq [opt]
17993
17994 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17995 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17996 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17997 filled in with the TYPE containing the member. *CV_QUALS is
17998 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17999 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18000 Note that the tree codes returned by this function have nothing
18001 to do with the types of trees that will be eventually be created
18002 to represent the pointer or reference type being parsed. They are
18003 just constants with suggestive names. */
18004 static enum tree_code
18005 cp_parser_ptr_operator (cp_parser* parser,
18006 tree* type,
18007 cp_cv_quals *cv_quals,
18008 tree *attributes)
18009 {
18010 enum tree_code code = ERROR_MARK;
18011 cp_token *token;
18012 tree attrs = NULL_TREE;
18013
18014 /* Assume that it's not a pointer-to-member. */
18015 *type = NULL_TREE;
18016 /* And that there are no cv-qualifiers. */
18017 *cv_quals = TYPE_UNQUALIFIED;
18018
18019 /* Peek at the next token. */
18020 token = cp_lexer_peek_token (parser->lexer);
18021
18022 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18023 if (token->type == CPP_MULT)
18024 code = INDIRECT_REF;
18025 else if (token->type == CPP_AND)
18026 code = ADDR_EXPR;
18027 else if ((cxx_dialect != cxx98) &&
18028 token->type == CPP_AND_AND) /* C++0x only */
18029 code = NON_LVALUE_EXPR;
18030
18031 if (code != ERROR_MARK)
18032 {
18033 /* Consume the `*', `&' or `&&'. */
18034 cp_lexer_consume_token (parser->lexer);
18035
18036 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18037 `&', if we are allowing GNU extensions. (The only qualifier
18038 that can legally appear after `&' is `restrict', but that is
18039 enforced during semantic analysis. */
18040 if (code == INDIRECT_REF
18041 || cp_parser_allow_gnu_extensions_p (parser))
18042 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18043
18044 attrs = cp_parser_std_attribute_spec_seq (parser);
18045 if (attributes != NULL)
18046 *attributes = attrs;
18047 }
18048 else
18049 {
18050 /* Try the pointer-to-member case. */
18051 cp_parser_parse_tentatively (parser);
18052 /* Look for the optional `::' operator. */
18053 cp_parser_global_scope_opt (parser,
18054 /*current_scope_valid_p=*/false);
18055 /* Look for the nested-name specifier. */
18056 token = cp_lexer_peek_token (parser->lexer);
18057 cp_parser_nested_name_specifier (parser,
18058 /*typename_keyword_p=*/false,
18059 /*check_dependency_p=*/true,
18060 /*type_p=*/false,
18061 /*is_declaration=*/false);
18062 /* If we found it, and the next token is a `*', then we are
18063 indeed looking at a pointer-to-member operator. */
18064 if (!cp_parser_error_occurred (parser)
18065 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18066 {
18067 /* Indicate that the `*' operator was used. */
18068 code = INDIRECT_REF;
18069
18070 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18071 error_at (token->location, "%qD is a namespace", parser->scope);
18072 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18073 error_at (token->location, "cannot form pointer to member of "
18074 "non-class %q#T", parser->scope);
18075 else
18076 {
18077 /* The type of which the member is a member is given by the
18078 current SCOPE. */
18079 *type = parser->scope;
18080 /* The next name will not be qualified. */
18081 parser->scope = NULL_TREE;
18082 parser->qualifying_scope = NULL_TREE;
18083 parser->object_scope = NULL_TREE;
18084 /* Look for optional c++11 attributes. */
18085 attrs = cp_parser_std_attribute_spec_seq (parser);
18086 if (attributes != NULL)
18087 *attributes = attrs;
18088 /* Look for the optional cv-qualifier-seq. */
18089 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18090 }
18091 }
18092 /* If that didn't work we don't have a ptr-operator. */
18093 if (!cp_parser_parse_definitely (parser))
18094 cp_parser_error (parser, "expected ptr-operator");
18095 }
18096
18097 return code;
18098 }
18099
18100 /* Parse an (optional) cv-qualifier-seq.
18101
18102 cv-qualifier-seq:
18103 cv-qualifier cv-qualifier-seq [opt]
18104
18105 cv-qualifier:
18106 const
18107 volatile
18108
18109 GNU Extension:
18110
18111 cv-qualifier:
18112 __restrict__
18113
18114 Returns a bitmask representing the cv-qualifiers. */
18115
18116 static cp_cv_quals
18117 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18118 {
18119 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18120
18121 while (true)
18122 {
18123 cp_token *token;
18124 cp_cv_quals cv_qualifier;
18125
18126 /* Peek at the next token. */
18127 token = cp_lexer_peek_token (parser->lexer);
18128 /* See if it's a cv-qualifier. */
18129 switch (token->keyword)
18130 {
18131 case RID_CONST:
18132 cv_qualifier = TYPE_QUAL_CONST;
18133 break;
18134
18135 case RID_VOLATILE:
18136 cv_qualifier = TYPE_QUAL_VOLATILE;
18137 break;
18138
18139 case RID_RESTRICT:
18140 cv_qualifier = TYPE_QUAL_RESTRICT;
18141 break;
18142
18143 default:
18144 cv_qualifier = TYPE_UNQUALIFIED;
18145 break;
18146 }
18147
18148 if (!cv_qualifier)
18149 break;
18150
18151 if (cv_quals & cv_qualifier)
18152 {
18153 error_at (token->location, "duplicate cv-qualifier");
18154 cp_lexer_purge_token (parser->lexer);
18155 }
18156 else
18157 {
18158 cp_lexer_consume_token (parser->lexer);
18159 cv_quals |= cv_qualifier;
18160 }
18161 }
18162
18163 return cv_quals;
18164 }
18165
18166 /* Parse an (optional) ref-qualifier
18167
18168 ref-qualifier:
18169 &
18170 &&
18171
18172 Returns cp_ref_qualifier representing ref-qualifier. */
18173
18174 static cp_ref_qualifier
18175 cp_parser_ref_qualifier_opt (cp_parser* parser)
18176 {
18177 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18178
18179 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18180 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18181 return ref_qual;
18182
18183 while (true)
18184 {
18185 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18186 cp_token *token = cp_lexer_peek_token (parser->lexer);
18187
18188 switch (token->type)
18189 {
18190 case CPP_AND:
18191 curr_ref_qual = REF_QUAL_LVALUE;
18192 break;
18193
18194 case CPP_AND_AND:
18195 curr_ref_qual = REF_QUAL_RVALUE;
18196 break;
18197
18198 default:
18199 curr_ref_qual = REF_QUAL_NONE;
18200 break;
18201 }
18202
18203 if (!curr_ref_qual)
18204 break;
18205 else if (ref_qual)
18206 {
18207 error_at (token->location, "multiple ref-qualifiers");
18208 cp_lexer_purge_token (parser->lexer);
18209 }
18210 else
18211 {
18212 ref_qual = curr_ref_qual;
18213 cp_lexer_consume_token (parser->lexer);
18214 }
18215 }
18216
18217 return ref_qual;
18218 }
18219
18220 /* Parse an (optional) virt-specifier-seq.
18221
18222 virt-specifier-seq:
18223 virt-specifier virt-specifier-seq [opt]
18224
18225 virt-specifier:
18226 override
18227 final
18228
18229 Returns a bitmask representing the virt-specifiers. */
18230
18231 static cp_virt_specifiers
18232 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18233 {
18234 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18235
18236 while (true)
18237 {
18238 cp_token *token;
18239 cp_virt_specifiers virt_specifier;
18240
18241 /* Peek at the next token. */
18242 token = cp_lexer_peek_token (parser->lexer);
18243 /* See if it's a virt-specifier-qualifier. */
18244 if (token->type != CPP_NAME)
18245 break;
18246 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18247 {
18248 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18249 virt_specifier = VIRT_SPEC_OVERRIDE;
18250 }
18251 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18252 {
18253 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18254 virt_specifier = VIRT_SPEC_FINAL;
18255 }
18256 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18257 {
18258 virt_specifier = VIRT_SPEC_FINAL;
18259 }
18260 else
18261 break;
18262
18263 if (virt_specifiers & virt_specifier)
18264 {
18265 error_at (token->location, "duplicate virt-specifier");
18266 cp_lexer_purge_token (parser->lexer);
18267 }
18268 else
18269 {
18270 cp_lexer_consume_token (parser->lexer);
18271 virt_specifiers |= virt_specifier;
18272 }
18273 }
18274 return virt_specifiers;
18275 }
18276
18277 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18278 is in scope even though it isn't real. */
18279
18280 void
18281 inject_this_parameter (tree ctype, cp_cv_quals quals)
18282 {
18283 tree this_parm;
18284
18285 if (current_class_ptr)
18286 {
18287 /* We don't clear this between NSDMIs. Is it already what we want? */
18288 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18289 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18290 && cp_type_quals (type) == quals)
18291 return;
18292 }
18293
18294 this_parm = build_this_parm (ctype, quals);
18295 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18296 current_class_ptr = NULL_TREE;
18297 current_class_ref
18298 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18299 current_class_ptr = this_parm;
18300 }
18301
18302 /* Return true iff our current scope is a non-static data member
18303 initializer. */
18304
18305 bool
18306 parsing_nsdmi (void)
18307 {
18308 /* We recognize NSDMI context by the context-less 'this' pointer set up
18309 by the function above. */
18310 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18311 return true;
18312 return false;
18313 }
18314
18315 /* Parse a late-specified return type, if any. This is not a separate
18316 non-terminal, but part of a function declarator, which looks like
18317
18318 -> trailing-type-specifier-seq abstract-declarator(opt)
18319
18320 Returns the type indicated by the type-id.
18321
18322 In addition to this this parses any queued up omp declare simd
18323 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18324
18325 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18326 function. */
18327
18328 static tree
18329 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18330 cp_cv_quals quals)
18331 {
18332 cp_token *token;
18333 tree type = NULL_TREE;
18334 bool declare_simd_p = (parser->omp_declare_simd
18335 && declarator
18336 && declarator->kind == cdk_id);
18337
18338 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18339 && declarator && declarator->kind == cdk_id);
18340
18341 /* Peek at the next token. */
18342 token = cp_lexer_peek_token (parser->lexer);
18343 /* A late-specified return type is indicated by an initial '->'. */
18344 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18345 return NULL_TREE;
18346
18347 tree save_ccp = current_class_ptr;
18348 tree save_ccr = current_class_ref;
18349 if (quals >= 0)
18350 {
18351 /* DR 1207: 'this' is in scope in the trailing return type. */
18352 inject_this_parameter (current_class_type, quals);
18353 }
18354
18355 if (token->type == CPP_DEREF)
18356 {
18357 /* Consume the ->. */
18358 cp_lexer_consume_token (parser->lexer);
18359
18360 type = cp_parser_trailing_type_id (parser);
18361 }
18362
18363 if (cilk_simd_fn_vector_p)
18364 declarator->std_attributes
18365 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18366 declarator->std_attributes);
18367 if (declare_simd_p)
18368 declarator->std_attributes
18369 = cp_parser_late_parsing_omp_declare_simd (parser,
18370 declarator->std_attributes);
18371
18372 if (quals >= 0)
18373 {
18374 current_class_ptr = save_ccp;
18375 current_class_ref = save_ccr;
18376 }
18377
18378 return type;
18379 }
18380
18381 /* Parse a declarator-id.
18382
18383 declarator-id:
18384 id-expression
18385 :: [opt] nested-name-specifier [opt] type-name
18386
18387 In the `id-expression' case, the value returned is as for
18388 cp_parser_id_expression if the id-expression was an unqualified-id.
18389 If the id-expression was a qualified-id, then a SCOPE_REF is
18390 returned. The first operand is the scope (either a NAMESPACE_DECL
18391 or TREE_TYPE), but the second is still just a representation of an
18392 unqualified-id. */
18393
18394 static tree
18395 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18396 {
18397 tree id;
18398 /* The expression must be an id-expression. Assume that qualified
18399 names are the names of types so that:
18400
18401 template <class T>
18402 int S<T>::R::i = 3;
18403
18404 will work; we must treat `S<T>::R' as the name of a type.
18405 Similarly, assume that qualified names are templates, where
18406 required, so that:
18407
18408 template <class T>
18409 int S<T>::R<T>::i = 3;
18410
18411 will work, too. */
18412 id = cp_parser_id_expression (parser,
18413 /*template_keyword_p=*/false,
18414 /*check_dependency_p=*/false,
18415 /*template_p=*/NULL,
18416 /*declarator_p=*/true,
18417 optional_p);
18418 if (id && BASELINK_P (id))
18419 id = BASELINK_FUNCTIONS (id);
18420 return id;
18421 }
18422
18423 /* Parse a type-id.
18424
18425 type-id:
18426 type-specifier-seq abstract-declarator [opt]
18427
18428 Returns the TYPE specified. */
18429
18430 static tree
18431 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18432 bool is_trailing_return)
18433 {
18434 cp_decl_specifier_seq type_specifier_seq;
18435 cp_declarator *abstract_declarator;
18436
18437 /* Parse the type-specifier-seq. */
18438 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18439 is_trailing_return,
18440 &type_specifier_seq);
18441 if (type_specifier_seq.type == error_mark_node)
18442 return error_mark_node;
18443
18444 /* There might or might not be an abstract declarator. */
18445 cp_parser_parse_tentatively (parser);
18446 /* Look for the declarator. */
18447 abstract_declarator
18448 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18449 /*parenthesized_p=*/NULL,
18450 /*member_p=*/false,
18451 /*friend_p=*/false);
18452 /* Check to see if there really was a declarator. */
18453 if (!cp_parser_parse_definitely (parser))
18454 abstract_declarator = NULL;
18455
18456 if (type_specifier_seq.type
18457 /* None of the valid uses of 'auto' in C++14 involve the type-id
18458 nonterminal, but it is valid in a trailing-return-type. */
18459 && !(cxx_dialect >= cxx14 && is_trailing_return)
18460 && type_uses_auto (type_specifier_seq.type))
18461 {
18462 /* A type-id with type 'auto' is only ok if the abstract declarator
18463 is a function declarator with a late-specified return type. */
18464 if (abstract_declarator
18465 && abstract_declarator->kind == cdk_function
18466 && abstract_declarator->u.function.late_return_type)
18467 /* OK */;
18468 else
18469 {
18470 error ("invalid use of %<auto%>");
18471 return error_mark_node;
18472 }
18473 }
18474
18475 return groktypename (&type_specifier_seq, abstract_declarator,
18476 is_template_arg);
18477 }
18478
18479 static tree cp_parser_type_id (cp_parser *parser)
18480 {
18481 return cp_parser_type_id_1 (parser, false, false);
18482 }
18483
18484 static tree cp_parser_template_type_arg (cp_parser *parser)
18485 {
18486 tree r;
18487 const char *saved_message = parser->type_definition_forbidden_message;
18488 parser->type_definition_forbidden_message
18489 = G_("types may not be defined in template arguments");
18490 r = cp_parser_type_id_1 (parser, true, false);
18491 parser->type_definition_forbidden_message = saved_message;
18492 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18493 {
18494 error ("invalid use of %<auto%> in template argument");
18495 r = error_mark_node;
18496 }
18497 return r;
18498 }
18499
18500 static tree cp_parser_trailing_type_id (cp_parser *parser)
18501 {
18502 return cp_parser_type_id_1 (parser, false, true);
18503 }
18504
18505 /* Parse a type-specifier-seq.
18506
18507 type-specifier-seq:
18508 type-specifier type-specifier-seq [opt]
18509
18510 GNU extension:
18511
18512 type-specifier-seq:
18513 attributes type-specifier-seq [opt]
18514
18515 If IS_DECLARATION is true, we are at the start of a "condition" or
18516 exception-declaration, so we might be followed by a declarator-id.
18517
18518 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18519 i.e. we've just seen "->".
18520
18521 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18522
18523 static void
18524 cp_parser_type_specifier_seq (cp_parser* parser,
18525 bool is_declaration,
18526 bool is_trailing_return,
18527 cp_decl_specifier_seq *type_specifier_seq)
18528 {
18529 bool seen_type_specifier = false;
18530 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18531 cp_token *start_token = NULL;
18532
18533 /* Clear the TYPE_SPECIFIER_SEQ. */
18534 clear_decl_specs (type_specifier_seq);
18535
18536 /* In the context of a trailing return type, enum E { } is an
18537 elaborated-type-specifier followed by a function-body, not an
18538 enum-specifier. */
18539 if (is_trailing_return)
18540 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18541
18542 /* Parse the type-specifiers and attributes. */
18543 while (true)
18544 {
18545 tree type_specifier;
18546 bool is_cv_qualifier;
18547
18548 /* Check for attributes first. */
18549 if (cp_next_tokens_can_be_attribute_p (parser))
18550 {
18551 type_specifier_seq->attributes =
18552 chainon (type_specifier_seq->attributes,
18553 cp_parser_attributes_opt (parser));
18554 continue;
18555 }
18556
18557 /* record the token of the beginning of the type specifier seq,
18558 for error reporting purposes*/
18559 if (!start_token)
18560 start_token = cp_lexer_peek_token (parser->lexer);
18561
18562 /* Look for the type-specifier. */
18563 type_specifier = cp_parser_type_specifier (parser,
18564 flags,
18565 type_specifier_seq,
18566 /*is_declaration=*/false,
18567 NULL,
18568 &is_cv_qualifier);
18569 if (!type_specifier)
18570 {
18571 /* If the first type-specifier could not be found, this is not a
18572 type-specifier-seq at all. */
18573 if (!seen_type_specifier)
18574 {
18575 /* Set in_declarator_p to avoid skipping to the semicolon. */
18576 int in_decl = parser->in_declarator_p;
18577 parser->in_declarator_p = true;
18578
18579 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18580 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18581 cp_parser_error (parser, "expected type-specifier");
18582
18583 parser->in_declarator_p = in_decl;
18584
18585 type_specifier_seq->type = error_mark_node;
18586 return;
18587 }
18588 /* If subsequent type-specifiers could not be found, the
18589 type-specifier-seq is complete. */
18590 break;
18591 }
18592
18593 seen_type_specifier = true;
18594 /* The standard says that a condition can be:
18595
18596 type-specifier-seq declarator = assignment-expression
18597
18598 However, given:
18599
18600 struct S {};
18601 if (int S = ...)
18602
18603 we should treat the "S" as a declarator, not as a
18604 type-specifier. The standard doesn't say that explicitly for
18605 type-specifier-seq, but it does say that for
18606 decl-specifier-seq in an ordinary declaration. Perhaps it
18607 would be clearer just to allow a decl-specifier-seq here, and
18608 then add a semantic restriction that if any decl-specifiers
18609 that are not type-specifiers appear, the program is invalid. */
18610 if (is_declaration && !is_cv_qualifier)
18611 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18612 }
18613 }
18614
18615 /* Return whether the function currently being declared has an associated
18616 template parameter list. */
18617
18618 static bool
18619 function_being_declared_is_template_p (cp_parser* parser)
18620 {
18621 if (!current_template_parms || processing_template_parmlist)
18622 return false;
18623
18624 if (parser->implicit_template_scope)
18625 return true;
18626
18627 if (at_class_scope_p ()
18628 && TYPE_BEING_DEFINED (current_class_type))
18629 return parser->num_template_parameter_lists != 0;
18630
18631 return ((int) parser->num_template_parameter_lists > template_class_depth
18632 (current_class_type));
18633 }
18634
18635 /* Parse a parameter-declaration-clause.
18636
18637 parameter-declaration-clause:
18638 parameter-declaration-list [opt] ... [opt]
18639 parameter-declaration-list , ...
18640
18641 Returns a representation for the parameter declarations. A return
18642 value of NULL indicates a parameter-declaration-clause consisting
18643 only of an ellipsis. */
18644
18645 static tree
18646 cp_parser_parameter_declaration_clause (cp_parser* parser)
18647 {
18648 tree parameters;
18649 cp_token *token;
18650 bool ellipsis_p;
18651 bool is_error;
18652
18653 struct cleanup {
18654 cp_parser* parser;
18655 int auto_is_implicit_function_template_parm_p;
18656 ~cleanup() {
18657 parser->auto_is_implicit_function_template_parm_p
18658 = auto_is_implicit_function_template_parm_p;
18659 }
18660 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18661
18662 (void) cleanup;
18663
18664 if (!processing_specialization
18665 && !processing_template_parmlist
18666 && !processing_explicit_instantiation)
18667 if (!current_function_decl
18668 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18669 parser->auto_is_implicit_function_template_parm_p = true;
18670
18671 /* Peek at the next token. */
18672 token = cp_lexer_peek_token (parser->lexer);
18673 /* Check for trivial parameter-declaration-clauses. */
18674 if (token->type == CPP_ELLIPSIS)
18675 {
18676 /* Consume the `...' token. */
18677 cp_lexer_consume_token (parser->lexer);
18678 return NULL_TREE;
18679 }
18680 else if (token->type == CPP_CLOSE_PAREN)
18681 /* There are no parameters. */
18682 {
18683 #ifndef NO_IMPLICIT_EXTERN_C
18684 if (in_system_header_at (input_location)
18685 && current_class_type == NULL
18686 && current_lang_name == lang_name_c)
18687 return NULL_TREE;
18688 else
18689 #endif
18690 return void_list_node;
18691 }
18692 /* Check for `(void)', too, which is a special case. */
18693 else if (token->keyword == RID_VOID
18694 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18695 == CPP_CLOSE_PAREN))
18696 {
18697 /* Consume the `void' token. */
18698 cp_lexer_consume_token (parser->lexer);
18699 /* There are no parameters. */
18700 return void_list_node;
18701 }
18702
18703 /* Parse the parameter-declaration-list. */
18704 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18705 /* If a parse error occurred while parsing the
18706 parameter-declaration-list, then the entire
18707 parameter-declaration-clause is erroneous. */
18708 if (is_error)
18709 return NULL;
18710
18711 /* Peek at the next token. */
18712 token = cp_lexer_peek_token (parser->lexer);
18713 /* If it's a `,', the clause should terminate with an ellipsis. */
18714 if (token->type == CPP_COMMA)
18715 {
18716 /* Consume the `,'. */
18717 cp_lexer_consume_token (parser->lexer);
18718 /* Expect an ellipsis. */
18719 ellipsis_p
18720 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18721 }
18722 /* It might also be `...' if the optional trailing `,' was
18723 omitted. */
18724 else if (token->type == CPP_ELLIPSIS)
18725 {
18726 /* Consume the `...' token. */
18727 cp_lexer_consume_token (parser->lexer);
18728 /* And remember that we saw it. */
18729 ellipsis_p = true;
18730 }
18731 else
18732 ellipsis_p = false;
18733
18734 /* Finish the parameter list. */
18735 if (!ellipsis_p)
18736 parameters = chainon (parameters, void_list_node);
18737
18738 return parameters;
18739 }
18740
18741 /* Parse a parameter-declaration-list.
18742
18743 parameter-declaration-list:
18744 parameter-declaration
18745 parameter-declaration-list , parameter-declaration
18746
18747 Returns a representation of the parameter-declaration-list, as for
18748 cp_parser_parameter_declaration_clause. However, the
18749 `void_list_node' is never appended to the list. Upon return,
18750 *IS_ERROR will be true iff an error occurred. */
18751
18752 static tree
18753 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18754 {
18755 tree parameters = NULL_TREE;
18756 tree *tail = &parameters;
18757 bool saved_in_unbraced_linkage_specification_p;
18758 int index = 0;
18759
18760 /* Assume all will go well. */
18761 *is_error = false;
18762 /* The special considerations that apply to a function within an
18763 unbraced linkage specifications do not apply to the parameters
18764 to the function. */
18765 saved_in_unbraced_linkage_specification_p
18766 = parser->in_unbraced_linkage_specification_p;
18767 parser->in_unbraced_linkage_specification_p = false;
18768
18769 /* Look for more parameters. */
18770 while (true)
18771 {
18772 cp_parameter_declarator *parameter;
18773 tree decl = error_mark_node;
18774 bool parenthesized_p = false;
18775 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18776 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18777 (current_template_parms)) : 0);
18778
18779 /* Parse the parameter. */
18780 parameter
18781 = cp_parser_parameter_declaration (parser,
18782 /*template_parm_p=*/false,
18783 &parenthesized_p);
18784
18785 /* We don't know yet if the enclosing context is deprecated, so wait
18786 and warn in grokparms if appropriate. */
18787 deprecated_state = DEPRECATED_SUPPRESS;
18788
18789 if (parameter)
18790 {
18791 /* If a function parameter pack was specified and an implicit template
18792 parameter was introduced during cp_parser_parameter_declaration,
18793 change any implicit parameters introduced into packs. */
18794 if (parser->implicit_template_parms
18795 && parameter->declarator
18796 && parameter->declarator->parameter_pack_p)
18797 {
18798 int latest_template_parm_idx = TREE_VEC_LENGTH
18799 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18800
18801 if (latest_template_parm_idx != template_parm_idx)
18802 parameter->decl_specifiers.type = convert_generic_types_to_packs
18803 (parameter->decl_specifiers.type,
18804 template_parm_idx, latest_template_parm_idx);
18805 }
18806
18807 decl = grokdeclarator (parameter->declarator,
18808 &parameter->decl_specifiers,
18809 PARM,
18810 parameter->default_argument != NULL_TREE,
18811 &parameter->decl_specifiers.attributes);
18812 }
18813
18814 deprecated_state = DEPRECATED_NORMAL;
18815
18816 /* If a parse error occurred parsing the parameter declaration,
18817 then the entire parameter-declaration-list is erroneous. */
18818 if (decl == error_mark_node)
18819 {
18820 *is_error = true;
18821 parameters = error_mark_node;
18822 break;
18823 }
18824
18825 if (parameter->decl_specifiers.attributes)
18826 cplus_decl_attributes (&decl,
18827 parameter->decl_specifiers.attributes,
18828 0);
18829 if (DECL_NAME (decl))
18830 decl = pushdecl (decl);
18831
18832 if (decl != error_mark_node)
18833 {
18834 retrofit_lang_decl (decl);
18835 DECL_PARM_INDEX (decl) = ++index;
18836 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18837 }
18838
18839 /* Add the new parameter to the list. */
18840 *tail = build_tree_list (parameter->default_argument, decl);
18841 tail = &TREE_CHAIN (*tail);
18842
18843 /* Peek at the next token. */
18844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18845 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18846 /* These are for Objective-C++ */
18847 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18848 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18849 /* The parameter-declaration-list is complete. */
18850 break;
18851 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18852 {
18853 cp_token *token;
18854
18855 /* Peek at the next token. */
18856 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18857 /* If it's an ellipsis, then the list is complete. */
18858 if (token->type == CPP_ELLIPSIS)
18859 break;
18860 /* Otherwise, there must be more parameters. Consume the
18861 `,'. */
18862 cp_lexer_consume_token (parser->lexer);
18863 /* When parsing something like:
18864
18865 int i(float f, double d)
18866
18867 we can tell after seeing the declaration for "f" that we
18868 are not looking at an initialization of a variable "i",
18869 but rather at the declaration of a function "i".
18870
18871 Due to the fact that the parsing of template arguments
18872 (as specified to a template-id) requires backtracking we
18873 cannot use this technique when inside a template argument
18874 list. */
18875 if (!parser->in_template_argument_list_p
18876 && !parser->in_type_id_in_expr_p
18877 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18878 /* However, a parameter-declaration of the form
18879 "float(f)" (which is a valid declaration of a
18880 parameter "f") can also be interpreted as an
18881 expression (the conversion of "f" to "float"). */
18882 && !parenthesized_p)
18883 cp_parser_commit_to_tentative_parse (parser);
18884 }
18885 else
18886 {
18887 cp_parser_error (parser, "expected %<,%> or %<...%>");
18888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18889 cp_parser_skip_to_closing_parenthesis (parser,
18890 /*recovering=*/true,
18891 /*or_comma=*/false,
18892 /*consume_paren=*/false);
18893 break;
18894 }
18895 }
18896
18897 parser->in_unbraced_linkage_specification_p
18898 = saved_in_unbraced_linkage_specification_p;
18899
18900 /* Reset implicit_template_scope if we are about to leave the function
18901 parameter list that introduced it. Note that for out-of-line member
18902 definitions, there will be one or more class scopes before we get to
18903 the template parameter scope. */
18904
18905 if (cp_binding_level *its = parser->implicit_template_scope)
18906 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18907 {
18908 while (maybe_its->kind == sk_class)
18909 maybe_its = maybe_its->level_chain;
18910 if (maybe_its == its)
18911 {
18912 parser->implicit_template_parms = 0;
18913 parser->implicit_template_scope = 0;
18914 }
18915 }
18916
18917 return parameters;
18918 }
18919
18920 /* Parse a parameter declaration.
18921
18922 parameter-declaration:
18923 decl-specifier-seq ... [opt] declarator
18924 decl-specifier-seq declarator = assignment-expression
18925 decl-specifier-seq ... [opt] abstract-declarator [opt]
18926 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18927
18928 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18929 declares a template parameter. (In that case, a non-nested `>'
18930 token encountered during the parsing of the assignment-expression
18931 is not interpreted as a greater-than operator.)
18932
18933 Returns a representation of the parameter, or NULL if an error
18934 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18935 true iff the declarator is of the form "(p)". */
18936
18937 static cp_parameter_declarator *
18938 cp_parser_parameter_declaration (cp_parser *parser,
18939 bool template_parm_p,
18940 bool *parenthesized_p)
18941 {
18942 int declares_class_or_enum;
18943 cp_decl_specifier_seq decl_specifiers;
18944 cp_declarator *declarator;
18945 tree default_argument;
18946 cp_token *token = NULL, *declarator_token_start = NULL;
18947 const char *saved_message;
18948
18949 /* In a template parameter, `>' is not an operator.
18950
18951 [temp.param]
18952
18953 When parsing a default template-argument for a non-type
18954 template-parameter, the first non-nested `>' is taken as the end
18955 of the template parameter-list rather than a greater-than
18956 operator. */
18957
18958 /* Type definitions may not appear in parameter types. */
18959 saved_message = parser->type_definition_forbidden_message;
18960 parser->type_definition_forbidden_message
18961 = G_("types may not be defined in parameter types");
18962
18963 /* Parse the declaration-specifiers. */
18964 cp_parser_decl_specifier_seq (parser,
18965 CP_PARSER_FLAGS_NONE,
18966 &decl_specifiers,
18967 &declares_class_or_enum);
18968
18969 /* Complain about missing 'typename' or other invalid type names. */
18970 if (!decl_specifiers.any_type_specifiers_p
18971 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18972 decl_specifiers.type = error_mark_node;
18973
18974 /* If an error occurred, there's no reason to attempt to parse the
18975 rest of the declaration. */
18976 if (cp_parser_error_occurred (parser))
18977 {
18978 parser->type_definition_forbidden_message = saved_message;
18979 return NULL;
18980 }
18981
18982 /* Peek at the next token. */
18983 token = cp_lexer_peek_token (parser->lexer);
18984
18985 /* If the next token is a `)', `,', `=', `>', or `...', then there
18986 is no declarator. However, when variadic templates are enabled,
18987 there may be a declarator following `...'. */
18988 if (token->type == CPP_CLOSE_PAREN
18989 || token->type == CPP_COMMA
18990 || token->type == CPP_EQ
18991 || token->type == CPP_GREATER)
18992 {
18993 declarator = NULL;
18994 if (parenthesized_p)
18995 *parenthesized_p = false;
18996 }
18997 /* Otherwise, there should be a declarator. */
18998 else
18999 {
19000 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19001 parser->default_arg_ok_p = false;
19002
19003 /* After seeing a decl-specifier-seq, if the next token is not a
19004 "(", there is no possibility that the code is a valid
19005 expression. Therefore, if parsing tentatively, we commit at
19006 this point. */
19007 if (!parser->in_template_argument_list_p
19008 /* In an expression context, having seen:
19009
19010 (int((char ...
19011
19012 we cannot be sure whether we are looking at a
19013 function-type (taking a "char" as a parameter) or a cast
19014 of some object of type "char" to "int". */
19015 && !parser->in_type_id_in_expr_p
19016 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19017 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19018 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19019 cp_parser_commit_to_tentative_parse (parser);
19020 /* Parse the declarator. */
19021 declarator_token_start = token;
19022 declarator = cp_parser_declarator (parser,
19023 CP_PARSER_DECLARATOR_EITHER,
19024 /*ctor_dtor_or_conv_p=*/NULL,
19025 parenthesized_p,
19026 /*member_p=*/false,
19027 /*friend_p=*/false);
19028 parser->default_arg_ok_p = saved_default_arg_ok_p;
19029 /* After the declarator, allow more attributes. */
19030 decl_specifiers.attributes
19031 = chainon (decl_specifiers.attributes,
19032 cp_parser_attributes_opt (parser));
19033 }
19034
19035 /* If the next token is an ellipsis, and we have not seen a
19036 declarator name, and the type of the declarator contains parameter
19037 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19038 a parameter pack expansion expression. Otherwise, leave the
19039 ellipsis for a C-style variadic function. */
19040 token = cp_lexer_peek_token (parser->lexer);
19041 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19042 {
19043 tree type = decl_specifiers.type;
19044
19045 if (type && DECL_P (type))
19046 type = TREE_TYPE (type);
19047
19048 if (type
19049 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19050 && declarator_can_be_parameter_pack (declarator)
19051 && (!declarator || !declarator->parameter_pack_p)
19052 && uses_parameter_packs (type))
19053 {
19054 /* Consume the `...'. */
19055 cp_lexer_consume_token (parser->lexer);
19056 maybe_warn_variadic_templates ();
19057
19058 /* Build a pack expansion type */
19059 if (declarator)
19060 declarator->parameter_pack_p = true;
19061 else
19062 decl_specifiers.type = make_pack_expansion (type);
19063 }
19064 }
19065
19066 /* The restriction on defining new types applies only to the type
19067 of the parameter, not to the default argument. */
19068 parser->type_definition_forbidden_message = saved_message;
19069
19070 /* If the next token is `=', then process a default argument. */
19071 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19072 {
19073 token = cp_lexer_peek_token (parser->lexer);
19074 /* If we are defining a class, then the tokens that make up the
19075 default argument must be saved and processed later. */
19076 if (!template_parm_p && at_class_scope_p ()
19077 && TYPE_BEING_DEFINED (current_class_type)
19078 && !LAMBDA_TYPE_P (current_class_type))
19079 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19080 /* Outside of a class definition, we can just parse the
19081 assignment-expression. */
19082 else
19083 default_argument
19084 = cp_parser_default_argument (parser, template_parm_p);
19085
19086 if (!parser->default_arg_ok_p)
19087 {
19088 if (flag_permissive)
19089 warning (0, "deprecated use of default argument for parameter of non-function");
19090 else
19091 {
19092 error_at (token->location,
19093 "default arguments are only "
19094 "permitted for function parameters");
19095 default_argument = NULL_TREE;
19096 }
19097 }
19098 else if ((declarator && declarator->parameter_pack_p)
19099 || (decl_specifiers.type
19100 && PACK_EXPANSION_P (decl_specifiers.type)))
19101 {
19102 /* Find the name of the parameter pack. */
19103 cp_declarator *id_declarator = declarator;
19104 while (id_declarator && id_declarator->kind != cdk_id)
19105 id_declarator = id_declarator->declarator;
19106
19107 if (id_declarator && id_declarator->kind == cdk_id)
19108 error_at (declarator_token_start->location,
19109 template_parm_p
19110 ? G_("template parameter pack %qD "
19111 "cannot have a default argument")
19112 : G_("parameter pack %qD cannot have "
19113 "a default argument"),
19114 id_declarator->u.id.unqualified_name);
19115 else
19116 error_at (declarator_token_start->location,
19117 template_parm_p
19118 ? G_("template parameter pack cannot have "
19119 "a default argument")
19120 : G_("parameter pack cannot have a "
19121 "default argument"));
19122
19123 default_argument = NULL_TREE;
19124 }
19125 }
19126 else
19127 default_argument = NULL_TREE;
19128
19129 return make_parameter_declarator (&decl_specifiers,
19130 declarator,
19131 default_argument);
19132 }
19133
19134 /* Parse a default argument and return it.
19135
19136 TEMPLATE_PARM_P is true if this is a default argument for a
19137 non-type template parameter. */
19138 static tree
19139 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19140 {
19141 tree default_argument = NULL_TREE;
19142 bool saved_greater_than_is_operator_p;
19143 bool saved_local_variables_forbidden_p;
19144 bool non_constant_p, is_direct_init;
19145
19146 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19147 set correctly. */
19148 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19149 parser->greater_than_is_operator_p = !template_parm_p;
19150 /* Local variable names (and the `this' keyword) may not
19151 appear in a default argument. */
19152 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19153 parser->local_variables_forbidden_p = true;
19154 /* Parse the assignment-expression. */
19155 if (template_parm_p)
19156 push_deferring_access_checks (dk_no_deferred);
19157 tree saved_class_ptr = NULL_TREE;
19158 tree saved_class_ref = NULL_TREE;
19159 /* The "this" pointer is not valid in a default argument. */
19160 if (cfun)
19161 {
19162 saved_class_ptr = current_class_ptr;
19163 cp_function_chain->x_current_class_ptr = NULL_TREE;
19164 saved_class_ref = current_class_ref;
19165 cp_function_chain->x_current_class_ref = NULL_TREE;
19166 }
19167 default_argument
19168 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19169 /* Restore the "this" pointer. */
19170 if (cfun)
19171 {
19172 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19173 cp_function_chain->x_current_class_ref = saved_class_ref;
19174 }
19175 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19177 if (template_parm_p)
19178 pop_deferring_access_checks ();
19179 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19180 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19181
19182 return default_argument;
19183 }
19184
19185 /* Parse a function-body.
19186
19187 function-body:
19188 compound_statement */
19189
19190 static void
19191 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19192 {
19193 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19194 }
19195
19196 /* Parse a ctor-initializer-opt followed by a function-body. Return
19197 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19198 is true we are parsing a function-try-block. */
19199
19200 static bool
19201 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19202 bool in_function_try_block)
19203 {
19204 tree body, list;
19205 bool ctor_initializer_p;
19206 const bool check_body_p =
19207 DECL_CONSTRUCTOR_P (current_function_decl)
19208 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19209 tree last = NULL;
19210
19211 /* Begin the function body. */
19212 body = begin_function_body ();
19213 /* Parse the optional ctor-initializer. */
19214 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19215
19216 /* If we're parsing a constexpr constructor definition, we need
19217 to check that the constructor body is indeed empty. However,
19218 before we get to cp_parser_function_body lot of junk has been
19219 generated, so we can't just check that we have an empty block.
19220 Rather we take a snapshot of the outermost block, and check whether
19221 cp_parser_function_body changed its state. */
19222 if (check_body_p)
19223 {
19224 list = cur_stmt_list;
19225 if (STATEMENT_LIST_TAIL (list))
19226 last = STATEMENT_LIST_TAIL (list)->stmt;
19227 }
19228 /* Parse the function-body. */
19229 cp_parser_function_body (parser, in_function_try_block);
19230 if (check_body_p)
19231 check_constexpr_ctor_body (last, list, /*complain=*/true);
19232 /* Finish the function body. */
19233 finish_function_body (body);
19234
19235 return ctor_initializer_p;
19236 }
19237
19238 /* Parse an initializer.
19239
19240 initializer:
19241 = initializer-clause
19242 ( expression-list )
19243
19244 Returns an expression representing the initializer. If no
19245 initializer is present, NULL_TREE is returned.
19246
19247 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19248 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19249 set to TRUE if there is no initializer present. If there is an
19250 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19251 is set to true; otherwise it is set to false. */
19252
19253 static tree
19254 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19255 bool* non_constant_p)
19256 {
19257 cp_token *token;
19258 tree init;
19259
19260 /* Peek at the next token. */
19261 token = cp_lexer_peek_token (parser->lexer);
19262
19263 /* Let our caller know whether or not this initializer was
19264 parenthesized. */
19265 *is_direct_init = (token->type != CPP_EQ);
19266 /* Assume that the initializer is constant. */
19267 *non_constant_p = false;
19268
19269 if (token->type == CPP_EQ)
19270 {
19271 /* Consume the `='. */
19272 cp_lexer_consume_token (parser->lexer);
19273 /* Parse the initializer-clause. */
19274 init = cp_parser_initializer_clause (parser, non_constant_p);
19275 }
19276 else if (token->type == CPP_OPEN_PAREN)
19277 {
19278 vec<tree, va_gc> *vec;
19279 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19280 /*cast_p=*/false,
19281 /*allow_expansion_p=*/true,
19282 non_constant_p);
19283 if (vec == NULL)
19284 return error_mark_node;
19285 init = build_tree_list_vec (vec);
19286 release_tree_vector (vec);
19287 }
19288 else if (token->type == CPP_OPEN_BRACE)
19289 {
19290 cp_lexer_set_source_position (parser->lexer);
19291 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19292 init = cp_parser_braced_list (parser, non_constant_p);
19293 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19294 }
19295 else
19296 {
19297 /* Anything else is an error. */
19298 cp_parser_error (parser, "expected initializer");
19299 init = error_mark_node;
19300 }
19301
19302 return init;
19303 }
19304
19305 /* Parse an initializer-clause.
19306
19307 initializer-clause:
19308 assignment-expression
19309 braced-init-list
19310
19311 Returns an expression representing the initializer.
19312
19313 If the `assignment-expression' production is used the value
19314 returned is simply a representation for the expression.
19315
19316 Otherwise, calls cp_parser_braced_list. */
19317
19318 static tree
19319 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19320 {
19321 tree initializer;
19322
19323 /* Assume the expression is constant. */
19324 *non_constant_p = false;
19325
19326 /* If it is not a `{', then we are looking at an
19327 assignment-expression. */
19328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19329 {
19330 initializer
19331 = cp_parser_constant_expression (parser,
19332 /*allow_non_constant_p=*/true,
19333 non_constant_p);
19334 }
19335 else
19336 initializer = cp_parser_braced_list (parser, non_constant_p);
19337
19338 return initializer;
19339 }
19340
19341 /* Parse a brace-enclosed initializer list.
19342
19343 braced-init-list:
19344 { initializer-list , [opt] }
19345 { }
19346
19347 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19348 the elements of the initializer-list (or NULL, if the last
19349 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19350 NULL_TREE. There is no way to detect whether or not the optional
19351 trailing `,' was provided. NON_CONSTANT_P is as for
19352 cp_parser_initializer. */
19353
19354 static tree
19355 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19356 {
19357 tree initializer;
19358
19359 /* Consume the `{' token. */
19360 cp_lexer_consume_token (parser->lexer);
19361 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19362 initializer = make_node (CONSTRUCTOR);
19363 /* If it's not a `}', then there is a non-trivial initializer. */
19364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19365 {
19366 /* Parse the initializer list. */
19367 CONSTRUCTOR_ELTS (initializer)
19368 = cp_parser_initializer_list (parser, non_constant_p);
19369 /* A trailing `,' token is allowed. */
19370 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19371 cp_lexer_consume_token (parser->lexer);
19372 }
19373 else
19374 *non_constant_p = false;
19375 /* Now, there should be a trailing `}'. */
19376 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19377 TREE_TYPE (initializer) = init_list_type_node;
19378 return initializer;
19379 }
19380
19381 /* Consume tokens up to, and including, the next non-nested closing `]'.
19382 Returns true iff we found a closing `]'. */
19383
19384 static bool
19385 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19386 {
19387 unsigned square_depth = 0;
19388
19389 while (true)
19390 {
19391 cp_token * token = cp_lexer_peek_token (parser->lexer);
19392
19393 switch (token->type)
19394 {
19395 case CPP_EOF:
19396 case CPP_PRAGMA_EOL:
19397 /* If we've run out of tokens, then there is no closing `]'. */
19398 return false;
19399
19400 case CPP_OPEN_SQUARE:
19401 ++square_depth;
19402 break;
19403
19404 case CPP_CLOSE_SQUARE:
19405 if (!square_depth--)
19406 {
19407 cp_lexer_consume_token (parser->lexer);
19408 return true;
19409 }
19410 break;
19411
19412 default:
19413 break;
19414 }
19415
19416 /* Consume the token. */
19417 cp_lexer_consume_token (parser->lexer);
19418 }
19419 }
19420
19421 /* Return true if we are looking at an array-designator, false otherwise. */
19422
19423 static bool
19424 cp_parser_array_designator_p (cp_parser *parser)
19425 {
19426 /* Consume the `['. */
19427 cp_lexer_consume_token (parser->lexer);
19428
19429 cp_lexer_save_tokens (parser->lexer);
19430
19431 /* Skip tokens until the next token is a closing square bracket.
19432 If we find the closing `]', and the next token is a `=', then
19433 we are looking at an array designator. */
19434 bool array_designator_p
19435 = (cp_parser_skip_to_closing_square_bracket (parser)
19436 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19437
19438 /* Roll back the tokens we skipped. */
19439 cp_lexer_rollback_tokens (parser->lexer);
19440
19441 return array_designator_p;
19442 }
19443
19444 /* Parse an initializer-list.
19445
19446 initializer-list:
19447 initializer-clause ... [opt]
19448 initializer-list , initializer-clause ... [opt]
19449
19450 GNU Extension:
19451
19452 initializer-list:
19453 designation initializer-clause ...[opt]
19454 initializer-list , designation initializer-clause ...[opt]
19455
19456 designation:
19457 . identifier =
19458 identifier :
19459 [ constant-expression ] =
19460
19461 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19462 for the initializer. If the INDEX of the elt is non-NULL, it is the
19463 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19464 as for cp_parser_initializer. */
19465
19466 static vec<constructor_elt, va_gc> *
19467 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19468 {
19469 vec<constructor_elt, va_gc> *v = NULL;
19470
19471 /* Assume all of the expressions are constant. */
19472 *non_constant_p = false;
19473
19474 /* Parse the rest of the list. */
19475 while (true)
19476 {
19477 cp_token *token;
19478 tree designator;
19479 tree initializer;
19480 bool clause_non_constant_p;
19481
19482 /* If the next token is an identifier and the following one is a
19483 colon, we are looking at the GNU designated-initializer
19484 syntax. */
19485 if (cp_parser_allow_gnu_extensions_p (parser)
19486 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19487 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19488 {
19489 /* Warn the user that they are using an extension. */
19490 pedwarn (input_location, OPT_Wpedantic,
19491 "ISO C++ does not allow designated initializers");
19492 /* Consume the identifier. */
19493 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19494 /* Consume the `:'. */
19495 cp_lexer_consume_token (parser->lexer);
19496 }
19497 /* Also handle the C99 syntax, '. id ='. */
19498 else if (cp_parser_allow_gnu_extensions_p (parser)
19499 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19500 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19502 {
19503 /* Warn the user that they are using an extension. */
19504 pedwarn (input_location, OPT_Wpedantic,
19505 "ISO C++ does not allow C99 designated initializers");
19506 /* Consume the `.'. */
19507 cp_lexer_consume_token (parser->lexer);
19508 /* Consume the identifier. */
19509 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19510 /* Consume the `='. */
19511 cp_lexer_consume_token (parser->lexer);
19512 }
19513 /* Also handle C99 array designators, '[ const ] ='. */
19514 else if (cp_parser_allow_gnu_extensions_p (parser)
19515 && !c_dialect_objc ()
19516 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19517 {
19518 /* In C++11, [ could start a lambda-introducer. */
19519 bool non_const = false;
19520
19521 cp_parser_parse_tentatively (parser);
19522
19523 if (!cp_parser_array_designator_p (parser))
19524 {
19525 cp_parser_simulate_error (parser);
19526 designator = NULL_TREE;
19527 }
19528 else
19529 {
19530 designator = cp_parser_constant_expression (parser, true,
19531 &non_const);
19532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19533 cp_parser_require (parser, CPP_EQ, RT_EQ);
19534 }
19535
19536 if (!cp_parser_parse_definitely (parser))
19537 designator = NULL_TREE;
19538 else if (non_const)
19539 require_potential_rvalue_constant_expression (designator);
19540 }
19541 else
19542 designator = NULL_TREE;
19543
19544 /* Parse the initializer. */
19545 initializer = cp_parser_initializer_clause (parser,
19546 &clause_non_constant_p);
19547 /* If any clause is non-constant, so is the entire initializer. */
19548 if (clause_non_constant_p)
19549 *non_constant_p = true;
19550
19551 /* If we have an ellipsis, this is an initializer pack
19552 expansion. */
19553 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19554 {
19555 /* Consume the `...'. */
19556 cp_lexer_consume_token (parser->lexer);
19557
19558 /* Turn the initializer into an initializer expansion. */
19559 initializer = make_pack_expansion (initializer);
19560 }
19561
19562 /* Add it to the vector. */
19563 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19564
19565 /* If the next token is not a comma, we have reached the end of
19566 the list. */
19567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19568 break;
19569
19570 /* Peek at the next token. */
19571 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19572 /* If the next token is a `}', then we're still done. An
19573 initializer-clause can have a trailing `,' after the
19574 initializer-list and before the closing `}'. */
19575 if (token->type == CPP_CLOSE_BRACE)
19576 break;
19577
19578 /* Consume the `,' token. */
19579 cp_lexer_consume_token (parser->lexer);
19580 }
19581
19582 return v;
19583 }
19584
19585 /* Classes [gram.class] */
19586
19587 /* Parse a class-name.
19588
19589 class-name:
19590 identifier
19591 template-id
19592
19593 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19594 to indicate that names looked up in dependent types should be
19595 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19596 keyword has been used to indicate that the name that appears next
19597 is a template. TAG_TYPE indicates the explicit tag given before
19598 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19599 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19600 is the class being defined in a class-head.
19601
19602 Returns the TYPE_DECL representing the class. */
19603
19604 static tree
19605 cp_parser_class_name (cp_parser *parser,
19606 bool typename_keyword_p,
19607 bool template_keyword_p,
19608 enum tag_types tag_type,
19609 bool check_dependency_p,
19610 bool class_head_p,
19611 bool is_declaration)
19612 {
19613 tree decl;
19614 tree scope;
19615 bool typename_p;
19616 cp_token *token;
19617 tree identifier = NULL_TREE;
19618
19619 /* All class-names start with an identifier. */
19620 token = cp_lexer_peek_token (parser->lexer);
19621 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19622 {
19623 cp_parser_error (parser, "expected class-name");
19624 return error_mark_node;
19625 }
19626
19627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19628 to a template-id, so we save it here. */
19629 scope = parser->scope;
19630 if (scope == error_mark_node)
19631 return error_mark_node;
19632
19633 /* Any name names a type if we're following the `typename' keyword
19634 in a qualified name where the enclosing scope is type-dependent. */
19635 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19636 && dependent_type_p (scope));
19637 /* Handle the common case (an identifier, but not a template-id)
19638 efficiently. */
19639 if (token->type == CPP_NAME
19640 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19641 {
19642 cp_token *identifier_token;
19643 bool ambiguous_p;
19644
19645 /* Look for the identifier. */
19646 identifier_token = cp_lexer_peek_token (parser->lexer);
19647 ambiguous_p = identifier_token->error_reported;
19648 identifier = cp_parser_identifier (parser);
19649 /* If the next token isn't an identifier, we are certainly not
19650 looking at a class-name. */
19651 if (identifier == error_mark_node)
19652 decl = error_mark_node;
19653 /* If we know this is a type-name, there's no need to look it
19654 up. */
19655 else if (typename_p)
19656 decl = identifier;
19657 else
19658 {
19659 tree ambiguous_decls;
19660 /* If we already know that this lookup is ambiguous, then
19661 we've already issued an error message; there's no reason
19662 to check again. */
19663 if (ambiguous_p)
19664 {
19665 cp_parser_simulate_error (parser);
19666 return error_mark_node;
19667 }
19668 /* If the next token is a `::', then the name must be a type
19669 name.
19670
19671 [basic.lookup.qual]
19672
19673 During the lookup for a name preceding the :: scope
19674 resolution operator, object, function, and enumerator
19675 names are ignored. */
19676 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19677 tag_type = typename_type;
19678 /* Look up the name. */
19679 decl = cp_parser_lookup_name (parser, identifier,
19680 tag_type,
19681 /*is_template=*/false,
19682 /*is_namespace=*/false,
19683 check_dependency_p,
19684 &ambiguous_decls,
19685 identifier_token->location);
19686 if (ambiguous_decls)
19687 {
19688 if (cp_parser_parsing_tentatively (parser))
19689 cp_parser_simulate_error (parser);
19690 return error_mark_node;
19691 }
19692 }
19693 }
19694 else
19695 {
19696 /* Try a template-id. */
19697 decl = cp_parser_template_id (parser, template_keyword_p,
19698 check_dependency_p,
19699 tag_type,
19700 is_declaration);
19701 if (decl == error_mark_node)
19702 return error_mark_node;
19703 }
19704
19705 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19706
19707 /* If this is a typename, create a TYPENAME_TYPE. */
19708 if (typename_p && decl != error_mark_node)
19709 {
19710 decl = make_typename_type (scope, decl, typename_type,
19711 /*complain=*/tf_error);
19712 if (decl != error_mark_node)
19713 decl = TYPE_NAME (decl);
19714 }
19715
19716 decl = strip_using_decl (decl);
19717
19718 /* Check to see that it is really the name of a class. */
19719 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19720 && identifier_p (TREE_OPERAND (decl, 0))
19721 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19722 /* Situations like this:
19723
19724 template <typename T> struct A {
19725 typename T::template X<int>::I i;
19726 };
19727
19728 are problematic. Is `T::template X<int>' a class-name? The
19729 standard does not seem to be definitive, but there is no other
19730 valid interpretation of the following `::'. Therefore, those
19731 names are considered class-names. */
19732 {
19733 decl = make_typename_type (scope, decl, tag_type, tf_error);
19734 if (decl != error_mark_node)
19735 decl = TYPE_NAME (decl);
19736 }
19737 else if (TREE_CODE (decl) != TYPE_DECL
19738 || TREE_TYPE (decl) == error_mark_node
19739 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19740 /* In Objective-C 2.0, a classname followed by '.' starts a
19741 dot-syntax expression, and it's not a type-name. */
19742 || (c_dialect_objc ()
19743 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19744 && objc_is_class_name (decl)))
19745 decl = error_mark_node;
19746
19747 if (decl == error_mark_node)
19748 cp_parser_error (parser, "expected class-name");
19749 else if (identifier && !parser->scope)
19750 maybe_note_name_used_in_class (identifier, decl);
19751
19752 return decl;
19753 }
19754
19755 /* Parse a class-specifier.
19756
19757 class-specifier:
19758 class-head { member-specification [opt] }
19759
19760 Returns the TREE_TYPE representing the class. */
19761
19762 static tree
19763 cp_parser_class_specifier_1 (cp_parser* parser)
19764 {
19765 tree type;
19766 tree attributes = NULL_TREE;
19767 bool nested_name_specifier_p;
19768 unsigned saved_num_template_parameter_lists;
19769 bool saved_in_function_body;
19770 unsigned char in_statement;
19771 bool in_switch_statement_p;
19772 bool saved_in_unbraced_linkage_specification_p;
19773 tree old_scope = NULL_TREE;
19774 tree scope = NULL_TREE;
19775 cp_token *closing_brace;
19776
19777 push_deferring_access_checks (dk_no_deferred);
19778
19779 /* Parse the class-head. */
19780 type = cp_parser_class_head (parser,
19781 &nested_name_specifier_p);
19782 /* If the class-head was a semantic disaster, skip the entire body
19783 of the class. */
19784 if (!type)
19785 {
19786 cp_parser_skip_to_end_of_block_or_statement (parser);
19787 pop_deferring_access_checks ();
19788 return error_mark_node;
19789 }
19790
19791 /* Look for the `{'. */
19792 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19793 {
19794 pop_deferring_access_checks ();
19795 return error_mark_node;
19796 }
19797
19798 cp_ensure_no_omp_declare_simd (parser);
19799
19800 /* Issue an error message if type-definitions are forbidden here. */
19801 cp_parser_check_type_definition (parser);
19802 /* Remember that we are defining one more class. */
19803 ++parser->num_classes_being_defined;
19804 /* Inside the class, surrounding template-parameter-lists do not
19805 apply. */
19806 saved_num_template_parameter_lists
19807 = parser->num_template_parameter_lists;
19808 parser->num_template_parameter_lists = 0;
19809 /* We are not in a function body. */
19810 saved_in_function_body = parser->in_function_body;
19811 parser->in_function_body = false;
19812 /* Or in a loop. */
19813 in_statement = parser->in_statement;
19814 parser->in_statement = 0;
19815 /* Or in a switch. */
19816 in_switch_statement_p = parser->in_switch_statement_p;
19817 parser->in_switch_statement_p = false;
19818 /* We are not immediately inside an extern "lang" block. */
19819 saved_in_unbraced_linkage_specification_p
19820 = parser->in_unbraced_linkage_specification_p;
19821 parser->in_unbraced_linkage_specification_p = false;
19822
19823 /* Start the class. */
19824 if (nested_name_specifier_p)
19825 {
19826 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19827 old_scope = push_inner_scope (scope);
19828 }
19829 type = begin_class_definition (type);
19830
19831 if (type == error_mark_node)
19832 /* If the type is erroneous, skip the entire body of the class. */
19833 cp_parser_skip_to_closing_brace (parser);
19834 else
19835 /* Parse the member-specification. */
19836 cp_parser_member_specification_opt (parser);
19837
19838 /* Look for the trailing `}'. */
19839 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19840 /* Look for trailing attributes to apply to this class. */
19841 if (cp_parser_allow_gnu_extensions_p (parser))
19842 attributes = cp_parser_gnu_attributes_opt (parser);
19843 if (type != error_mark_node)
19844 type = finish_struct (type, attributes);
19845 if (nested_name_specifier_p)
19846 pop_inner_scope (old_scope, scope);
19847
19848 /* We've finished a type definition. Check for the common syntax
19849 error of forgetting a semicolon after the definition. We need to
19850 be careful, as we can't just check for not-a-semicolon and be done
19851 with it; the user might have typed:
19852
19853 class X { } c = ...;
19854 class X { } *p = ...;
19855
19856 and so forth. Instead, enumerate all the possible tokens that
19857 might follow this production; if we don't see one of them, then
19858 complain and silently insert the semicolon. */
19859 {
19860 cp_token *token = cp_lexer_peek_token (parser->lexer);
19861 bool want_semicolon = true;
19862
19863 if (cp_next_tokens_can_be_std_attribute_p (parser))
19864 /* Don't try to parse c++11 attributes here. As per the
19865 grammar, that should be a task for
19866 cp_parser_decl_specifier_seq. */
19867 want_semicolon = false;
19868
19869 switch (token->type)
19870 {
19871 case CPP_NAME:
19872 case CPP_SEMICOLON:
19873 case CPP_MULT:
19874 case CPP_AND:
19875 case CPP_OPEN_PAREN:
19876 case CPP_CLOSE_PAREN:
19877 case CPP_COMMA:
19878 want_semicolon = false;
19879 break;
19880
19881 /* While it's legal for type qualifiers and storage class
19882 specifiers to follow type definitions in the grammar, only
19883 compiler testsuites contain code like that. Assume that if
19884 we see such code, then what we're really seeing is a case
19885 like:
19886
19887 class X { }
19888 const <type> var = ...;
19889
19890 or
19891
19892 class Y { }
19893 static <type> func (...) ...
19894
19895 i.e. the qualifier or specifier applies to the next
19896 declaration. To do so, however, we need to look ahead one
19897 more token to see if *that* token is a type specifier.
19898
19899 This code could be improved to handle:
19900
19901 class Z { }
19902 static const <type> var = ...; */
19903 case CPP_KEYWORD:
19904 if (keyword_is_decl_specifier (token->keyword))
19905 {
19906 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19907
19908 /* Handling user-defined types here would be nice, but very
19909 tricky. */
19910 want_semicolon
19911 = (lookahead->type == CPP_KEYWORD
19912 && keyword_begins_type_specifier (lookahead->keyword));
19913 }
19914 break;
19915 default:
19916 break;
19917 }
19918
19919 /* If we don't have a type, then something is very wrong and we
19920 shouldn't try to do anything clever. Likewise for not seeing the
19921 closing brace. */
19922 if (closing_brace && TYPE_P (type) && want_semicolon)
19923 {
19924 cp_token_position prev
19925 = cp_lexer_previous_token_position (parser->lexer);
19926 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19927 location_t loc = prev_token->location;
19928
19929 if (CLASSTYPE_DECLARED_CLASS (type))
19930 error_at (loc, "expected %<;%> after class definition");
19931 else if (TREE_CODE (type) == RECORD_TYPE)
19932 error_at (loc, "expected %<;%> after struct definition");
19933 else if (TREE_CODE (type) == UNION_TYPE)
19934 error_at (loc, "expected %<;%> after union definition");
19935 else
19936 gcc_unreachable ();
19937
19938 /* Unget one token and smash it to look as though we encountered
19939 a semicolon in the input stream. */
19940 cp_lexer_set_token_position (parser->lexer, prev);
19941 token = cp_lexer_peek_token (parser->lexer);
19942 token->type = CPP_SEMICOLON;
19943 token->keyword = RID_MAX;
19944 }
19945 }
19946
19947 /* If this class is not itself within the scope of another class,
19948 then we need to parse the bodies of all of the queued function
19949 definitions. Note that the queued functions defined in a class
19950 are not always processed immediately following the
19951 class-specifier for that class. Consider:
19952
19953 struct A {
19954 struct B { void f() { sizeof (A); } };
19955 };
19956
19957 If `f' were processed before the processing of `A' were
19958 completed, there would be no way to compute the size of `A'.
19959 Note that the nesting we are interested in here is lexical --
19960 not the semantic nesting given by TYPE_CONTEXT. In particular,
19961 for:
19962
19963 struct A { struct B; };
19964 struct A::B { void f() { } };
19965
19966 there is no need to delay the parsing of `A::B::f'. */
19967 if (--parser->num_classes_being_defined == 0)
19968 {
19969 tree decl;
19970 tree class_type = NULL_TREE;
19971 tree pushed_scope = NULL_TREE;
19972 unsigned ix;
19973 cp_default_arg_entry *e;
19974 tree save_ccp, save_ccr;
19975
19976 /* In a first pass, parse default arguments to the functions.
19977 Then, in a second pass, parse the bodies of the functions.
19978 This two-phased approach handles cases like:
19979
19980 struct S {
19981 void f() { g(); }
19982 void g(int i = 3);
19983 };
19984
19985 */
19986 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19987 {
19988 decl = e->decl;
19989 /* If there are default arguments that have not yet been processed,
19990 take care of them now. */
19991 if (class_type != e->class_type)
19992 {
19993 if (pushed_scope)
19994 pop_scope (pushed_scope);
19995 class_type = e->class_type;
19996 pushed_scope = push_scope (class_type);
19997 }
19998 /* Make sure that any template parameters are in scope. */
19999 maybe_begin_member_template_processing (decl);
20000 /* Parse the default argument expressions. */
20001 cp_parser_late_parsing_default_args (parser, decl);
20002 /* Remove any template parameters from the symbol table. */
20003 maybe_end_member_template_processing ();
20004 }
20005 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20006 /* Now parse any NSDMIs. */
20007 save_ccp = current_class_ptr;
20008 save_ccr = current_class_ref;
20009 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20010 {
20011 if (class_type != DECL_CONTEXT (decl))
20012 {
20013 if (pushed_scope)
20014 pop_scope (pushed_scope);
20015 class_type = DECL_CONTEXT (decl);
20016 pushed_scope = push_scope (class_type);
20017 }
20018 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20019 cp_parser_late_parsing_nsdmi (parser, decl);
20020 }
20021 vec_safe_truncate (unparsed_nsdmis, 0);
20022 current_class_ptr = save_ccp;
20023 current_class_ref = save_ccr;
20024 if (pushed_scope)
20025 pop_scope (pushed_scope);
20026
20027 /* Now do some post-NSDMI bookkeeping. */
20028 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20029 after_nsdmi_defaulted_late_checks (class_type);
20030 vec_safe_truncate (unparsed_classes, 0);
20031 after_nsdmi_defaulted_late_checks (type);
20032
20033 /* Now parse the body of the functions. */
20034 if (flag_openmp)
20035 {
20036 /* OpenMP UDRs need to be parsed before all other functions. */
20037 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20038 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20039 cp_parser_late_parsing_for_member (parser, decl);
20040 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20041 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20042 cp_parser_late_parsing_for_member (parser, decl);
20043 }
20044 else
20045 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20046 cp_parser_late_parsing_for_member (parser, decl);
20047 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20048 }
20049 else
20050 vec_safe_push (unparsed_classes, type);
20051
20052 /* Put back any saved access checks. */
20053 pop_deferring_access_checks ();
20054
20055 /* Restore saved state. */
20056 parser->in_switch_statement_p = in_switch_statement_p;
20057 parser->in_statement = in_statement;
20058 parser->in_function_body = saved_in_function_body;
20059 parser->num_template_parameter_lists
20060 = saved_num_template_parameter_lists;
20061 parser->in_unbraced_linkage_specification_p
20062 = saved_in_unbraced_linkage_specification_p;
20063
20064 return type;
20065 }
20066
20067 static tree
20068 cp_parser_class_specifier (cp_parser* parser)
20069 {
20070 tree ret;
20071 timevar_push (TV_PARSE_STRUCT);
20072 ret = cp_parser_class_specifier_1 (parser);
20073 timevar_pop (TV_PARSE_STRUCT);
20074 return ret;
20075 }
20076
20077 /* Parse a class-head.
20078
20079 class-head:
20080 class-key identifier [opt] base-clause [opt]
20081 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20082 class-key nested-name-specifier [opt] template-id
20083 base-clause [opt]
20084
20085 class-virt-specifier:
20086 final
20087
20088 GNU Extensions:
20089 class-key attributes identifier [opt] base-clause [opt]
20090 class-key attributes nested-name-specifier identifier base-clause [opt]
20091 class-key attributes nested-name-specifier [opt] template-id
20092 base-clause [opt]
20093
20094 Upon return BASES is initialized to the list of base classes (or
20095 NULL, if there are none) in the same form returned by
20096 cp_parser_base_clause.
20097
20098 Returns the TYPE of the indicated class. Sets
20099 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20100 involving a nested-name-specifier was used, and FALSE otherwise.
20101
20102 Returns error_mark_node if this is not a class-head.
20103
20104 Returns NULL_TREE if the class-head is syntactically valid, but
20105 semantically invalid in a way that means we should skip the entire
20106 body of the class. */
20107
20108 static tree
20109 cp_parser_class_head (cp_parser* parser,
20110 bool* nested_name_specifier_p)
20111 {
20112 tree nested_name_specifier;
20113 enum tag_types class_key;
20114 tree id = NULL_TREE;
20115 tree type = NULL_TREE;
20116 tree attributes;
20117 tree bases;
20118 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20119 bool template_id_p = false;
20120 bool qualified_p = false;
20121 bool invalid_nested_name_p = false;
20122 bool invalid_explicit_specialization_p = false;
20123 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20124 tree pushed_scope = NULL_TREE;
20125 unsigned num_templates;
20126 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20127 /* Assume no nested-name-specifier will be present. */
20128 *nested_name_specifier_p = false;
20129 /* Assume no template parameter lists will be used in defining the
20130 type. */
20131 num_templates = 0;
20132 parser->colon_corrects_to_scope_p = false;
20133
20134 /* Look for the class-key. */
20135 class_key = cp_parser_class_key (parser);
20136 if (class_key == none_type)
20137 return error_mark_node;
20138
20139 /* Parse the attributes. */
20140 attributes = cp_parser_attributes_opt (parser);
20141
20142 /* If the next token is `::', that is invalid -- but sometimes
20143 people do try to write:
20144
20145 struct ::S {};
20146
20147 Handle this gracefully by accepting the extra qualifier, and then
20148 issuing an error about it later if this really is a
20149 class-head. If it turns out just to be an elaborated type
20150 specifier, remain silent. */
20151 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20152 qualified_p = true;
20153
20154 push_deferring_access_checks (dk_no_check);
20155
20156 /* Determine the name of the class. Begin by looking for an
20157 optional nested-name-specifier. */
20158 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20159 nested_name_specifier
20160 = cp_parser_nested_name_specifier_opt (parser,
20161 /*typename_keyword_p=*/false,
20162 /*check_dependency_p=*/false,
20163 /*type_p=*/true,
20164 /*is_declaration=*/false);
20165 /* If there was a nested-name-specifier, then there *must* be an
20166 identifier. */
20167 if (nested_name_specifier)
20168 {
20169 type_start_token = cp_lexer_peek_token (parser->lexer);
20170 /* Although the grammar says `identifier', it really means
20171 `class-name' or `template-name'. You are only allowed to
20172 define a class that has already been declared with this
20173 syntax.
20174
20175 The proposed resolution for Core Issue 180 says that wherever
20176 you see `class T::X' you should treat `X' as a type-name.
20177
20178 It is OK to define an inaccessible class; for example:
20179
20180 class A { class B; };
20181 class A::B {};
20182
20183 We do not know if we will see a class-name, or a
20184 template-name. We look for a class-name first, in case the
20185 class-name is a template-id; if we looked for the
20186 template-name first we would stop after the template-name. */
20187 cp_parser_parse_tentatively (parser);
20188 type = cp_parser_class_name (parser,
20189 /*typename_keyword_p=*/false,
20190 /*template_keyword_p=*/false,
20191 class_type,
20192 /*check_dependency_p=*/false,
20193 /*class_head_p=*/true,
20194 /*is_declaration=*/false);
20195 /* If that didn't work, ignore the nested-name-specifier. */
20196 if (!cp_parser_parse_definitely (parser))
20197 {
20198 invalid_nested_name_p = true;
20199 type_start_token = cp_lexer_peek_token (parser->lexer);
20200 id = cp_parser_identifier (parser);
20201 if (id == error_mark_node)
20202 id = NULL_TREE;
20203 }
20204 /* If we could not find a corresponding TYPE, treat this
20205 declaration like an unqualified declaration. */
20206 if (type == error_mark_node)
20207 nested_name_specifier = NULL_TREE;
20208 /* Otherwise, count the number of templates used in TYPE and its
20209 containing scopes. */
20210 else
20211 {
20212 tree scope;
20213
20214 for (scope = TREE_TYPE (type);
20215 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20216 scope = get_containing_scope (scope))
20217 if (TYPE_P (scope)
20218 && CLASS_TYPE_P (scope)
20219 && CLASSTYPE_TEMPLATE_INFO (scope)
20220 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20221 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20222 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20223 ++num_templates;
20224 }
20225 }
20226 /* Otherwise, the identifier is optional. */
20227 else
20228 {
20229 /* We don't know whether what comes next is a template-id,
20230 an identifier, or nothing at all. */
20231 cp_parser_parse_tentatively (parser);
20232 /* Check for a template-id. */
20233 type_start_token = cp_lexer_peek_token (parser->lexer);
20234 id = cp_parser_template_id (parser,
20235 /*template_keyword_p=*/false,
20236 /*check_dependency_p=*/true,
20237 class_key,
20238 /*is_declaration=*/true);
20239 /* If that didn't work, it could still be an identifier. */
20240 if (!cp_parser_parse_definitely (parser))
20241 {
20242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20243 {
20244 type_start_token = cp_lexer_peek_token (parser->lexer);
20245 id = cp_parser_identifier (parser);
20246 }
20247 else
20248 id = NULL_TREE;
20249 }
20250 else
20251 {
20252 template_id_p = true;
20253 ++num_templates;
20254 }
20255 }
20256
20257 pop_deferring_access_checks ();
20258
20259 if (id)
20260 {
20261 cp_parser_check_for_invalid_template_id (parser, id,
20262 class_key,
20263 type_start_token->location);
20264 }
20265 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20266
20267 /* If it's not a `:' or a `{' then we can't really be looking at a
20268 class-head, since a class-head only appears as part of a
20269 class-specifier. We have to detect this situation before calling
20270 xref_tag, since that has irreversible side-effects. */
20271 if (!cp_parser_next_token_starts_class_definition_p (parser))
20272 {
20273 cp_parser_error (parser, "expected %<{%> or %<:%>");
20274 type = error_mark_node;
20275 goto out;
20276 }
20277
20278 /* At this point, we're going ahead with the class-specifier, even
20279 if some other problem occurs. */
20280 cp_parser_commit_to_tentative_parse (parser);
20281 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20282 {
20283 cp_parser_error (parser,
20284 "cannot specify %<override%> for a class");
20285 type = error_mark_node;
20286 goto out;
20287 }
20288 /* Issue the error about the overly-qualified name now. */
20289 if (qualified_p)
20290 {
20291 cp_parser_error (parser,
20292 "global qualification of class name is invalid");
20293 type = error_mark_node;
20294 goto out;
20295 }
20296 else if (invalid_nested_name_p)
20297 {
20298 cp_parser_error (parser,
20299 "qualified name does not name a class");
20300 type = error_mark_node;
20301 goto out;
20302 }
20303 else if (nested_name_specifier)
20304 {
20305 tree scope;
20306
20307 /* Reject typedef-names in class heads. */
20308 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20309 {
20310 error_at (type_start_token->location,
20311 "invalid class name in declaration of %qD",
20312 type);
20313 type = NULL_TREE;
20314 goto done;
20315 }
20316
20317 /* Figure out in what scope the declaration is being placed. */
20318 scope = current_scope ();
20319 /* If that scope does not contain the scope in which the
20320 class was originally declared, the program is invalid. */
20321 if (scope && !is_ancestor (scope, nested_name_specifier))
20322 {
20323 if (at_namespace_scope_p ())
20324 error_at (type_start_token->location,
20325 "declaration of %qD in namespace %qD which does not "
20326 "enclose %qD",
20327 type, scope, nested_name_specifier);
20328 else
20329 error_at (type_start_token->location,
20330 "declaration of %qD in %qD which does not enclose %qD",
20331 type, scope, nested_name_specifier);
20332 type = NULL_TREE;
20333 goto done;
20334 }
20335 /* [dcl.meaning]
20336
20337 A declarator-id shall not be qualified except for the
20338 definition of a ... nested class outside of its class
20339 ... [or] the definition or explicit instantiation of a
20340 class member of a namespace outside of its namespace. */
20341 if (scope == nested_name_specifier)
20342 {
20343 permerror (nested_name_specifier_token_start->location,
20344 "extra qualification not allowed");
20345 nested_name_specifier = NULL_TREE;
20346 num_templates = 0;
20347 }
20348 }
20349 /* An explicit-specialization must be preceded by "template <>". If
20350 it is not, try to recover gracefully. */
20351 if (at_namespace_scope_p ()
20352 && parser->num_template_parameter_lists == 0
20353 && template_id_p)
20354 {
20355 error_at (type_start_token->location,
20356 "an explicit specialization must be preceded by %<template <>%>");
20357 invalid_explicit_specialization_p = true;
20358 /* Take the same action that would have been taken by
20359 cp_parser_explicit_specialization. */
20360 ++parser->num_template_parameter_lists;
20361 begin_specialization ();
20362 }
20363 /* There must be no "return" statements between this point and the
20364 end of this function; set "type "to the correct return value and
20365 use "goto done;" to return. */
20366 /* Make sure that the right number of template parameters were
20367 present. */
20368 if (!cp_parser_check_template_parameters (parser, num_templates,
20369 type_start_token->location,
20370 /*declarator=*/NULL))
20371 {
20372 /* If something went wrong, there is no point in even trying to
20373 process the class-definition. */
20374 type = NULL_TREE;
20375 goto done;
20376 }
20377
20378 /* Look up the type. */
20379 if (template_id_p)
20380 {
20381 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20382 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20383 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20384 {
20385 error_at (type_start_token->location,
20386 "function template %qD redeclared as a class template", id);
20387 type = error_mark_node;
20388 }
20389 else
20390 {
20391 type = TREE_TYPE (id);
20392 type = maybe_process_partial_specialization (type);
20393 }
20394 if (nested_name_specifier)
20395 pushed_scope = push_scope (nested_name_specifier);
20396 }
20397 else if (nested_name_specifier)
20398 {
20399 tree class_type;
20400
20401 /* Given:
20402
20403 template <typename T> struct S { struct T };
20404 template <typename T> struct S<T>::T { };
20405
20406 we will get a TYPENAME_TYPE when processing the definition of
20407 `S::T'. We need to resolve it to the actual type before we
20408 try to define it. */
20409 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20410 {
20411 class_type = resolve_typename_type (TREE_TYPE (type),
20412 /*only_current_p=*/false);
20413 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20414 type = TYPE_NAME (class_type);
20415 else
20416 {
20417 cp_parser_error (parser, "could not resolve typename type");
20418 type = error_mark_node;
20419 }
20420 }
20421
20422 if (maybe_process_partial_specialization (TREE_TYPE (type))
20423 == error_mark_node)
20424 {
20425 type = NULL_TREE;
20426 goto done;
20427 }
20428
20429 class_type = current_class_type;
20430 /* Enter the scope indicated by the nested-name-specifier. */
20431 pushed_scope = push_scope (nested_name_specifier);
20432 /* Get the canonical version of this type. */
20433 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20434 /* Call push_template_decl if it seems like we should be defining a
20435 template either from the template headers or the type we're
20436 defining, so that we diagnose both extra and missing headers. */
20437 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20438 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20439 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20440 {
20441 type = push_template_decl (type);
20442 if (type == error_mark_node)
20443 {
20444 type = NULL_TREE;
20445 goto done;
20446 }
20447 }
20448
20449 type = TREE_TYPE (type);
20450 *nested_name_specifier_p = true;
20451 }
20452 else /* The name is not a nested name. */
20453 {
20454 /* If the class was unnamed, create a dummy name. */
20455 if (!id)
20456 id = make_anon_name ();
20457 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20458 parser->num_template_parameter_lists);
20459 }
20460
20461 /* Indicate whether this class was declared as a `class' or as a
20462 `struct'. */
20463 if (TREE_CODE (type) == RECORD_TYPE)
20464 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20465 cp_parser_check_class_key (class_key, type);
20466
20467 /* If this type was already complete, and we see another definition,
20468 that's an error. */
20469 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20470 {
20471 error_at (type_start_token->location, "redefinition of %q#T",
20472 type);
20473 error_at (type_start_token->location, "previous definition of %q+#T",
20474 type);
20475 type = NULL_TREE;
20476 goto done;
20477 }
20478 else if (type == error_mark_node)
20479 type = NULL_TREE;
20480
20481 if (type)
20482 {
20483 /* Apply attributes now, before any use of the class as a template
20484 argument in its base list. */
20485 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20486 fixup_attribute_variants (type);
20487 }
20488
20489 /* We will have entered the scope containing the class; the names of
20490 base classes should be looked up in that context. For example:
20491
20492 struct A { struct B {}; struct C; };
20493 struct A::C : B {};
20494
20495 is valid. */
20496
20497 /* Get the list of base-classes, if there is one. */
20498 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20499 {
20500 /* PR59482: enter the class scope so that base-specifiers are looked
20501 up correctly. */
20502 if (type)
20503 pushclass (type);
20504 bases = cp_parser_base_clause (parser);
20505 /* PR59482: get out of the previously pushed class scope so that the
20506 subsequent pops pop the right thing. */
20507 if (type)
20508 popclass ();
20509 }
20510 else
20511 bases = NULL_TREE;
20512
20513 /* If we're really defining a class, process the base classes.
20514 If they're invalid, fail. */
20515 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20516 && !xref_basetypes (type, bases))
20517 type = NULL_TREE;
20518
20519 done:
20520 /* Leave the scope given by the nested-name-specifier. We will
20521 enter the class scope itself while processing the members. */
20522 if (pushed_scope)
20523 pop_scope (pushed_scope);
20524
20525 if (invalid_explicit_specialization_p)
20526 {
20527 end_specialization ();
20528 --parser->num_template_parameter_lists;
20529 }
20530
20531 if (type)
20532 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20533 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20534 CLASSTYPE_FINAL (type) = 1;
20535 out:
20536 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20537 return type;
20538 }
20539
20540 /* Parse a class-key.
20541
20542 class-key:
20543 class
20544 struct
20545 union
20546
20547 Returns the kind of class-key specified, or none_type to indicate
20548 error. */
20549
20550 static enum tag_types
20551 cp_parser_class_key (cp_parser* parser)
20552 {
20553 cp_token *token;
20554 enum tag_types tag_type;
20555
20556 /* Look for the class-key. */
20557 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20558 if (!token)
20559 return none_type;
20560
20561 /* Check to see if the TOKEN is a class-key. */
20562 tag_type = cp_parser_token_is_class_key (token);
20563 if (!tag_type)
20564 cp_parser_error (parser, "expected class-key");
20565 return tag_type;
20566 }
20567
20568 /* Parse a type-parameter-key.
20569
20570 type-parameter-key:
20571 class
20572 typename
20573 */
20574
20575 static void
20576 cp_parser_type_parameter_key (cp_parser* parser)
20577 {
20578 /* Look for the type-parameter-key. */
20579 enum tag_types tag_type = none_type;
20580 cp_token *token = cp_lexer_peek_token (parser->lexer);
20581 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20582 {
20583 cp_lexer_consume_token (parser->lexer);
20584 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20585 /* typename is not allowed in a template template parameter
20586 by the standard until C++1Z. */
20587 pedwarn (token->location, OPT_Wpedantic,
20588 "ISO C++ forbids typename key in template template parameter;"
20589 " use -std=c++1z or -std=gnu++1z");
20590 }
20591 else
20592 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20593
20594 return;
20595 }
20596
20597 /* Parse an (optional) member-specification.
20598
20599 member-specification:
20600 member-declaration member-specification [opt]
20601 access-specifier : member-specification [opt] */
20602
20603 static void
20604 cp_parser_member_specification_opt (cp_parser* parser)
20605 {
20606 while (true)
20607 {
20608 cp_token *token;
20609 enum rid keyword;
20610
20611 /* Peek at the next token. */
20612 token = cp_lexer_peek_token (parser->lexer);
20613 /* If it's a `}', or EOF then we've seen all the members. */
20614 if (token->type == CPP_CLOSE_BRACE
20615 || token->type == CPP_EOF
20616 || token->type == CPP_PRAGMA_EOL)
20617 break;
20618
20619 /* See if this token is a keyword. */
20620 keyword = token->keyword;
20621 switch (keyword)
20622 {
20623 case RID_PUBLIC:
20624 case RID_PROTECTED:
20625 case RID_PRIVATE:
20626 /* Consume the access-specifier. */
20627 cp_lexer_consume_token (parser->lexer);
20628 /* Remember which access-specifier is active. */
20629 current_access_specifier = token->u.value;
20630 /* Look for the `:'. */
20631 cp_parser_require (parser, CPP_COLON, RT_COLON);
20632 break;
20633
20634 default:
20635 /* Accept #pragmas at class scope. */
20636 if (token->type == CPP_PRAGMA)
20637 {
20638 cp_parser_pragma (parser, pragma_member);
20639 break;
20640 }
20641
20642 /* Otherwise, the next construction must be a
20643 member-declaration. */
20644 cp_parser_member_declaration (parser);
20645 }
20646 }
20647 }
20648
20649 /* Parse a member-declaration.
20650
20651 member-declaration:
20652 decl-specifier-seq [opt] member-declarator-list [opt] ;
20653 function-definition ; [opt]
20654 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20655 using-declaration
20656 template-declaration
20657 alias-declaration
20658
20659 member-declarator-list:
20660 member-declarator
20661 member-declarator-list , member-declarator
20662
20663 member-declarator:
20664 declarator pure-specifier [opt]
20665 declarator constant-initializer [opt]
20666 identifier [opt] : constant-expression
20667
20668 GNU Extensions:
20669
20670 member-declaration:
20671 __extension__ member-declaration
20672
20673 member-declarator:
20674 declarator attributes [opt] pure-specifier [opt]
20675 declarator attributes [opt] constant-initializer [opt]
20676 identifier [opt] attributes [opt] : constant-expression
20677
20678 C++0x Extensions:
20679
20680 member-declaration:
20681 static_assert-declaration */
20682
20683 static void
20684 cp_parser_member_declaration (cp_parser* parser)
20685 {
20686 cp_decl_specifier_seq decl_specifiers;
20687 tree prefix_attributes;
20688 tree decl;
20689 int declares_class_or_enum;
20690 bool friend_p;
20691 cp_token *token = NULL;
20692 cp_token *decl_spec_token_start = NULL;
20693 cp_token *initializer_token_start = NULL;
20694 int saved_pedantic;
20695 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20696
20697 /* Check for the `__extension__' keyword. */
20698 if (cp_parser_extension_opt (parser, &saved_pedantic))
20699 {
20700 /* Recurse. */
20701 cp_parser_member_declaration (parser);
20702 /* Restore the old value of the PEDANTIC flag. */
20703 pedantic = saved_pedantic;
20704
20705 return;
20706 }
20707
20708 /* Check for a template-declaration. */
20709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20710 {
20711 /* An explicit specialization here is an error condition, and we
20712 expect the specialization handler to detect and report this. */
20713 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20714 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20715 cp_parser_explicit_specialization (parser);
20716 else
20717 cp_parser_template_declaration (parser, /*member_p=*/true);
20718
20719 return;
20720 }
20721
20722 /* Check for a using-declaration. */
20723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20724 {
20725 if (cxx_dialect < cxx11)
20726 {
20727 /* Parse the using-declaration. */
20728 cp_parser_using_declaration (parser,
20729 /*access_declaration_p=*/false);
20730 return;
20731 }
20732 else
20733 {
20734 tree decl;
20735 bool alias_decl_expected;
20736 cp_parser_parse_tentatively (parser);
20737 decl = cp_parser_alias_declaration (parser);
20738 /* Note that if we actually see the '=' token after the
20739 identifier, cp_parser_alias_declaration commits the
20740 tentative parse. In that case, we really expects an
20741 alias-declaration. Otherwise, we expect a using
20742 declaration. */
20743 alias_decl_expected =
20744 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20745 cp_parser_parse_definitely (parser);
20746
20747 if (alias_decl_expected)
20748 finish_member_declaration (decl);
20749 else
20750 cp_parser_using_declaration (parser,
20751 /*access_declaration_p=*/false);
20752 return;
20753 }
20754 }
20755
20756 /* Check for @defs. */
20757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20758 {
20759 tree ivar, member;
20760 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20761 ivar = ivar_chains;
20762 while (ivar)
20763 {
20764 member = ivar;
20765 ivar = TREE_CHAIN (member);
20766 TREE_CHAIN (member) = NULL_TREE;
20767 finish_member_declaration (member);
20768 }
20769 return;
20770 }
20771
20772 /* If the next token is `static_assert' we have a static assertion. */
20773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20774 {
20775 cp_parser_static_assert (parser, /*member_p=*/true);
20776 return;
20777 }
20778
20779 parser->colon_corrects_to_scope_p = false;
20780
20781 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20782 goto out;
20783
20784 /* Parse the decl-specifier-seq. */
20785 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20786 cp_parser_decl_specifier_seq (parser,
20787 CP_PARSER_FLAGS_OPTIONAL,
20788 &decl_specifiers,
20789 &declares_class_or_enum);
20790 /* Check for an invalid type-name. */
20791 if (!decl_specifiers.any_type_specifiers_p
20792 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20793 goto out;
20794 /* If there is no declarator, then the decl-specifier-seq should
20795 specify a type. */
20796 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20797 {
20798 /* If there was no decl-specifier-seq, and the next token is a
20799 `;', then we have something like:
20800
20801 struct S { ; };
20802
20803 [class.mem]
20804
20805 Each member-declaration shall declare at least one member
20806 name of the class. */
20807 if (!decl_specifiers.any_specifiers_p)
20808 {
20809 cp_token *token = cp_lexer_peek_token (parser->lexer);
20810 if (!in_system_header_at (token->location))
20811 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20812 }
20813 else
20814 {
20815 tree type;
20816
20817 /* See if this declaration is a friend. */
20818 friend_p = cp_parser_friend_p (&decl_specifiers);
20819 /* If there were decl-specifiers, check to see if there was
20820 a class-declaration. */
20821 type = check_tag_decl (&decl_specifiers,
20822 /*explicit_type_instantiation_p=*/false);
20823 /* Nested classes have already been added to the class, but
20824 a `friend' needs to be explicitly registered. */
20825 if (friend_p)
20826 {
20827 /* If the `friend' keyword was present, the friend must
20828 be introduced with a class-key. */
20829 if (!declares_class_or_enum && cxx_dialect < cxx11)
20830 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20831 "in C++03 a class-key must be used "
20832 "when declaring a friend");
20833 /* In this case:
20834
20835 template <typename T> struct A {
20836 friend struct A<T>::B;
20837 };
20838
20839 A<T>::B will be represented by a TYPENAME_TYPE, and
20840 therefore not recognized by check_tag_decl. */
20841 if (!type)
20842 {
20843 type = decl_specifiers.type;
20844 if (type && TREE_CODE (type) == TYPE_DECL)
20845 type = TREE_TYPE (type);
20846 }
20847 if (!type || !TYPE_P (type))
20848 error_at (decl_spec_token_start->location,
20849 "friend declaration does not name a class or "
20850 "function");
20851 else
20852 make_friend_class (current_class_type, type,
20853 /*complain=*/true);
20854 }
20855 /* If there is no TYPE, an error message will already have
20856 been issued. */
20857 else if (!type || type == error_mark_node)
20858 ;
20859 /* An anonymous aggregate has to be handled specially; such
20860 a declaration really declares a data member (with a
20861 particular type), as opposed to a nested class. */
20862 else if (ANON_AGGR_TYPE_P (type))
20863 {
20864 /* C++11 9.5/6. */
20865 if (decl_specifiers.storage_class != sc_none)
20866 error_at (decl_spec_token_start->location,
20867 "a storage class on an anonymous aggregate "
20868 "in class scope is not allowed");
20869
20870 /* Remove constructors and such from TYPE, now that we
20871 know it is an anonymous aggregate. */
20872 fixup_anonymous_aggr (type);
20873 /* And make the corresponding data member. */
20874 decl = build_decl (decl_spec_token_start->location,
20875 FIELD_DECL, NULL_TREE, type);
20876 /* Add it to the class. */
20877 finish_member_declaration (decl);
20878 }
20879 else
20880 cp_parser_check_access_in_redeclaration
20881 (TYPE_NAME (type),
20882 decl_spec_token_start->location);
20883 }
20884 }
20885 else
20886 {
20887 bool assume_semicolon = false;
20888
20889 /* Clear attributes from the decl_specifiers but keep them
20890 around as prefix attributes that apply them to the entity
20891 being declared. */
20892 prefix_attributes = decl_specifiers.attributes;
20893 decl_specifiers.attributes = NULL_TREE;
20894
20895 /* See if these declarations will be friends. */
20896 friend_p = cp_parser_friend_p (&decl_specifiers);
20897
20898 /* Keep going until we hit the `;' at the end of the
20899 declaration. */
20900 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20901 {
20902 tree attributes = NULL_TREE;
20903 tree first_attribute;
20904
20905 /* Peek at the next token. */
20906 token = cp_lexer_peek_token (parser->lexer);
20907
20908 /* Check for a bitfield declaration. */
20909 if (token->type == CPP_COLON
20910 || (token->type == CPP_NAME
20911 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20912 == CPP_COLON))
20913 {
20914 tree identifier;
20915 tree width;
20916
20917 /* Get the name of the bitfield. Note that we cannot just
20918 check TOKEN here because it may have been invalidated by
20919 the call to cp_lexer_peek_nth_token above. */
20920 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20921 identifier = cp_parser_identifier (parser);
20922 else
20923 identifier = NULL_TREE;
20924
20925 /* Consume the `:' token. */
20926 cp_lexer_consume_token (parser->lexer);
20927 /* Get the width of the bitfield. */
20928 width
20929 = cp_parser_constant_expression (parser);
20930
20931 /* Look for attributes that apply to the bitfield. */
20932 attributes = cp_parser_attributes_opt (parser);
20933 /* Remember which attributes are prefix attributes and
20934 which are not. */
20935 first_attribute = attributes;
20936 /* Combine the attributes. */
20937 attributes = chainon (prefix_attributes, attributes);
20938
20939 /* Create the bitfield declaration. */
20940 decl = grokbitfield (identifier
20941 ? make_id_declarator (NULL_TREE,
20942 identifier,
20943 sfk_none)
20944 : NULL,
20945 &decl_specifiers,
20946 width,
20947 attributes);
20948 }
20949 else
20950 {
20951 cp_declarator *declarator;
20952 tree initializer;
20953 tree asm_specification;
20954 int ctor_dtor_or_conv_p;
20955
20956 /* Parse the declarator. */
20957 declarator
20958 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20959 &ctor_dtor_or_conv_p,
20960 /*parenthesized_p=*/NULL,
20961 /*member_p=*/true,
20962 friend_p);
20963
20964 /* If something went wrong parsing the declarator, make sure
20965 that we at least consume some tokens. */
20966 if (declarator == cp_error_declarator)
20967 {
20968 /* Skip to the end of the statement. */
20969 cp_parser_skip_to_end_of_statement (parser);
20970 /* If the next token is not a semicolon, that is
20971 probably because we just skipped over the body of
20972 a function. So, we consume a semicolon if
20973 present, but do not issue an error message if it
20974 is not present. */
20975 if (cp_lexer_next_token_is (parser->lexer,
20976 CPP_SEMICOLON))
20977 cp_lexer_consume_token (parser->lexer);
20978 goto out;
20979 }
20980
20981 if (declares_class_or_enum & 2)
20982 cp_parser_check_for_definition_in_return_type
20983 (declarator, decl_specifiers.type,
20984 decl_specifiers.locations[ds_type_spec]);
20985
20986 /* Look for an asm-specification. */
20987 asm_specification = cp_parser_asm_specification_opt (parser);
20988 /* Look for attributes that apply to the declaration. */
20989 attributes = cp_parser_attributes_opt (parser);
20990 /* Remember which attributes are prefix attributes and
20991 which are not. */
20992 first_attribute = attributes;
20993 /* Combine the attributes. */
20994 attributes = chainon (prefix_attributes, attributes);
20995
20996 /* If it's an `=', then we have a constant-initializer or a
20997 pure-specifier. It is not correct to parse the
20998 initializer before registering the member declaration
20999 since the member declaration should be in scope while
21000 its initializer is processed. However, the rest of the
21001 front end does not yet provide an interface that allows
21002 us to handle this correctly. */
21003 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21004 {
21005 /* In [class.mem]:
21006
21007 A pure-specifier shall be used only in the declaration of
21008 a virtual function.
21009
21010 A member-declarator can contain a constant-initializer
21011 only if it declares a static member of integral or
21012 enumeration type.
21013
21014 Therefore, if the DECLARATOR is for a function, we look
21015 for a pure-specifier; otherwise, we look for a
21016 constant-initializer. When we call `grokfield', it will
21017 perform more stringent semantics checks. */
21018 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21019 if (function_declarator_p (declarator)
21020 || (decl_specifiers.type
21021 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21022 && declarator->kind == cdk_id
21023 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21024 == FUNCTION_TYPE)))
21025 initializer = cp_parser_pure_specifier (parser);
21026 else if (decl_specifiers.storage_class != sc_static)
21027 initializer = cp_parser_save_nsdmi (parser);
21028 else if (cxx_dialect >= cxx11)
21029 {
21030 bool nonconst;
21031 /* Don't require a constant rvalue in C++11, since we
21032 might want a reference constant. We'll enforce
21033 constancy later. */
21034 cp_lexer_consume_token (parser->lexer);
21035 /* Parse the initializer. */
21036 initializer = cp_parser_initializer_clause (parser,
21037 &nonconst);
21038 }
21039 else
21040 /* Parse the initializer. */
21041 initializer = cp_parser_constant_initializer (parser);
21042 }
21043 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21044 && !function_declarator_p (declarator))
21045 {
21046 bool x;
21047 if (decl_specifiers.storage_class != sc_static)
21048 initializer = cp_parser_save_nsdmi (parser);
21049 else
21050 initializer = cp_parser_initializer (parser, &x, &x);
21051 }
21052 /* Otherwise, there is no initializer. */
21053 else
21054 initializer = NULL_TREE;
21055
21056 /* See if we are probably looking at a function
21057 definition. We are certainly not looking at a
21058 member-declarator. Calling `grokfield' has
21059 side-effects, so we must not do it unless we are sure
21060 that we are looking at a member-declarator. */
21061 if (cp_parser_token_starts_function_definition_p
21062 (cp_lexer_peek_token (parser->lexer)))
21063 {
21064 /* The grammar does not allow a pure-specifier to be
21065 used when a member function is defined. (It is
21066 possible that this fact is an oversight in the
21067 standard, since a pure function may be defined
21068 outside of the class-specifier. */
21069 if (initializer && initializer_token_start)
21070 error_at (initializer_token_start->location,
21071 "pure-specifier on function-definition");
21072 decl = cp_parser_save_member_function_body (parser,
21073 &decl_specifiers,
21074 declarator,
21075 attributes);
21076 if (parser->fully_implicit_function_template_p)
21077 decl = finish_fully_implicit_template (parser, decl);
21078 /* If the member was not a friend, declare it here. */
21079 if (!friend_p)
21080 finish_member_declaration (decl);
21081 /* Peek at the next token. */
21082 token = cp_lexer_peek_token (parser->lexer);
21083 /* If the next token is a semicolon, consume it. */
21084 if (token->type == CPP_SEMICOLON)
21085 cp_lexer_consume_token (parser->lexer);
21086 goto out;
21087 }
21088 else
21089 if (declarator->kind == cdk_function)
21090 declarator->id_loc = token->location;
21091 /* Create the declaration. */
21092 decl = grokfield (declarator, &decl_specifiers,
21093 initializer, /*init_const_expr_p=*/true,
21094 asm_specification, attributes);
21095 if (parser->fully_implicit_function_template_p)
21096 {
21097 if (friend_p)
21098 finish_fully_implicit_template (parser, 0);
21099 else
21100 decl = finish_fully_implicit_template (parser, decl);
21101 }
21102 }
21103
21104 cp_finalize_omp_declare_simd (parser, decl);
21105
21106 /* Reset PREFIX_ATTRIBUTES. */
21107 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21108 attributes = TREE_CHAIN (attributes);
21109 if (attributes)
21110 TREE_CHAIN (attributes) = NULL_TREE;
21111
21112 /* If there is any qualification still in effect, clear it
21113 now; we will be starting fresh with the next declarator. */
21114 parser->scope = NULL_TREE;
21115 parser->qualifying_scope = NULL_TREE;
21116 parser->object_scope = NULL_TREE;
21117 /* If it's a `,', then there are more declarators. */
21118 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21119 {
21120 cp_lexer_consume_token (parser->lexer);
21121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21122 {
21123 cp_token *token = cp_lexer_previous_token (parser->lexer);
21124 error_at (token->location,
21125 "stray %<,%> at end of member declaration");
21126 }
21127 }
21128 /* If the next token isn't a `;', then we have a parse error. */
21129 else if (cp_lexer_next_token_is_not (parser->lexer,
21130 CPP_SEMICOLON))
21131 {
21132 /* The next token might be a ways away from where the
21133 actual semicolon is missing. Find the previous token
21134 and use that for our error position. */
21135 cp_token *token = cp_lexer_previous_token (parser->lexer);
21136 error_at (token->location,
21137 "expected %<;%> at end of member declaration");
21138
21139 /* Assume that the user meant to provide a semicolon. If
21140 we were to cp_parser_skip_to_end_of_statement, we might
21141 skip to a semicolon inside a member function definition
21142 and issue nonsensical error messages. */
21143 assume_semicolon = true;
21144 }
21145
21146 if (decl)
21147 {
21148 /* Add DECL to the list of members. */
21149 if (!friend_p
21150 /* Explicitly include, eg, NSDMIs, for better error
21151 recovery (c++/58650). */
21152 || !DECL_DECLARES_FUNCTION_P (decl))
21153 finish_member_declaration (decl);
21154
21155 if (TREE_CODE (decl) == FUNCTION_DECL)
21156 cp_parser_save_default_args (parser, decl);
21157 else if (TREE_CODE (decl) == FIELD_DECL
21158 && !DECL_C_BIT_FIELD (decl)
21159 && DECL_INITIAL (decl))
21160 /* Add DECL to the queue of NSDMI to be parsed later. */
21161 vec_safe_push (unparsed_nsdmis, decl);
21162 }
21163
21164 if (assume_semicolon)
21165 goto out;
21166 }
21167 }
21168
21169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21170 out:
21171 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21172 }
21173
21174 /* Parse a pure-specifier.
21175
21176 pure-specifier:
21177 = 0
21178
21179 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21180 Otherwise, ERROR_MARK_NODE is returned. */
21181
21182 static tree
21183 cp_parser_pure_specifier (cp_parser* parser)
21184 {
21185 cp_token *token;
21186
21187 /* Look for the `=' token. */
21188 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21189 return error_mark_node;
21190 /* Look for the `0' token. */
21191 token = cp_lexer_peek_token (parser->lexer);
21192
21193 if (token->type == CPP_EOF
21194 || token->type == CPP_PRAGMA_EOL)
21195 return error_mark_node;
21196
21197 cp_lexer_consume_token (parser->lexer);
21198
21199 /* Accept = default or = delete in c++0x mode. */
21200 if (token->keyword == RID_DEFAULT
21201 || token->keyword == RID_DELETE)
21202 {
21203 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21204 return token->u.value;
21205 }
21206
21207 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21208 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21209 {
21210 cp_parser_error (parser,
21211 "invalid pure specifier (only %<= 0%> is allowed)");
21212 cp_parser_skip_to_end_of_statement (parser);
21213 return error_mark_node;
21214 }
21215 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21216 {
21217 error_at (token->location, "templates may not be %<virtual%>");
21218 return error_mark_node;
21219 }
21220
21221 return integer_zero_node;
21222 }
21223
21224 /* Parse a constant-initializer.
21225
21226 constant-initializer:
21227 = constant-expression
21228
21229 Returns a representation of the constant-expression. */
21230
21231 static tree
21232 cp_parser_constant_initializer (cp_parser* parser)
21233 {
21234 /* Look for the `=' token. */
21235 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21236 return error_mark_node;
21237
21238 /* It is invalid to write:
21239
21240 struct S { static const int i = { 7 }; };
21241
21242 */
21243 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21244 {
21245 cp_parser_error (parser,
21246 "a brace-enclosed initializer is not allowed here");
21247 /* Consume the opening brace. */
21248 cp_lexer_consume_token (parser->lexer);
21249 /* Skip the initializer. */
21250 cp_parser_skip_to_closing_brace (parser);
21251 /* Look for the trailing `}'. */
21252 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21253
21254 return error_mark_node;
21255 }
21256
21257 return cp_parser_constant_expression (parser);
21258 }
21259
21260 /* Derived classes [gram.class.derived] */
21261
21262 /* Parse a base-clause.
21263
21264 base-clause:
21265 : base-specifier-list
21266
21267 base-specifier-list:
21268 base-specifier ... [opt]
21269 base-specifier-list , base-specifier ... [opt]
21270
21271 Returns a TREE_LIST representing the base-classes, in the order in
21272 which they were declared. The representation of each node is as
21273 described by cp_parser_base_specifier.
21274
21275 In the case that no bases are specified, this function will return
21276 NULL_TREE, not ERROR_MARK_NODE. */
21277
21278 static tree
21279 cp_parser_base_clause (cp_parser* parser)
21280 {
21281 tree bases = NULL_TREE;
21282
21283 /* Look for the `:' that begins the list. */
21284 cp_parser_require (parser, CPP_COLON, RT_COLON);
21285
21286 /* Scan the base-specifier-list. */
21287 while (true)
21288 {
21289 cp_token *token;
21290 tree base;
21291 bool pack_expansion_p = false;
21292
21293 /* Look for the base-specifier. */
21294 base = cp_parser_base_specifier (parser);
21295 /* Look for the (optional) ellipsis. */
21296 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21297 {
21298 /* Consume the `...'. */
21299 cp_lexer_consume_token (parser->lexer);
21300
21301 pack_expansion_p = true;
21302 }
21303
21304 /* Add BASE to the front of the list. */
21305 if (base && base != error_mark_node)
21306 {
21307 if (pack_expansion_p)
21308 /* Make this a pack expansion type. */
21309 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21310
21311 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21312 {
21313 TREE_CHAIN (base) = bases;
21314 bases = base;
21315 }
21316 }
21317 /* Peek at the next token. */
21318 token = cp_lexer_peek_token (parser->lexer);
21319 /* If it's not a comma, then the list is complete. */
21320 if (token->type != CPP_COMMA)
21321 break;
21322 /* Consume the `,'. */
21323 cp_lexer_consume_token (parser->lexer);
21324 }
21325
21326 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21327 base class had a qualified name. However, the next name that
21328 appears is certainly not qualified. */
21329 parser->scope = NULL_TREE;
21330 parser->qualifying_scope = NULL_TREE;
21331 parser->object_scope = NULL_TREE;
21332
21333 return nreverse (bases);
21334 }
21335
21336 /* Parse a base-specifier.
21337
21338 base-specifier:
21339 :: [opt] nested-name-specifier [opt] class-name
21340 virtual access-specifier [opt] :: [opt] nested-name-specifier
21341 [opt] class-name
21342 access-specifier virtual [opt] :: [opt] nested-name-specifier
21343 [opt] class-name
21344
21345 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21346 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21347 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21348 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21349
21350 static tree
21351 cp_parser_base_specifier (cp_parser* parser)
21352 {
21353 cp_token *token;
21354 bool done = false;
21355 bool virtual_p = false;
21356 bool duplicate_virtual_error_issued_p = false;
21357 bool duplicate_access_error_issued_p = false;
21358 bool class_scope_p, template_p;
21359 tree access = access_default_node;
21360 tree type;
21361
21362 /* Process the optional `virtual' and `access-specifier'. */
21363 while (!done)
21364 {
21365 /* Peek at the next token. */
21366 token = cp_lexer_peek_token (parser->lexer);
21367 /* Process `virtual'. */
21368 switch (token->keyword)
21369 {
21370 case RID_VIRTUAL:
21371 /* If `virtual' appears more than once, issue an error. */
21372 if (virtual_p && !duplicate_virtual_error_issued_p)
21373 {
21374 cp_parser_error (parser,
21375 "%<virtual%> specified more than once in base-specified");
21376 duplicate_virtual_error_issued_p = true;
21377 }
21378
21379 virtual_p = true;
21380
21381 /* Consume the `virtual' token. */
21382 cp_lexer_consume_token (parser->lexer);
21383
21384 break;
21385
21386 case RID_PUBLIC:
21387 case RID_PROTECTED:
21388 case RID_PRIVATE:
21389 /* If more than one access specifier appears, issue an
21390 error. */
21391 if (access != access_default_node
21392 && !duplicate_access_error_issued_p)
21393 {
21394 cp_parser_error (parser,
21395 "more than one access specifier in base-specified");
21396 duplicate_access_error_issued_p = true;
21397 }
21398
21399 access = ridpointers[(int) token->keyword];
21400
21401 /* Consume the access-specifier. */
21402 cp_lexer_consume_token (parser->lexer);
21403
21404 break;
21405
21406 default:
21407 done = true;
21408 break;
21409 }
21410 }
21411 /* It is not uncommon to see programs mechanically, erroneously, use
21412 the 'typename' keyword to denote (dependent) qualified types
21413 as base classes. */
21414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21415 {
21416 token = cp_lexer_peek_token (parser->lexer);
21417 if (!processing_template_decl)
21418 error_at (token->location,
21419 "keyword %<typename%> not allowed outside of templates");
21420 else
21421 error_at (token->location,
21422 "keyword %<typename%> not allowed in this context "
21423 "(the base class is implicitly a type)");
21424 cp_lexer_consume_token (parser->lexer);
21425 }
21426
21427 /* Look for the optional `::' operator. */
21428 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21429 /* Look for the nested-name-specifier. The simplest way to
21430 implement:
21431
21432 [temp.res]
21433
21434 The keyword `typename' is not permitted in a base-specifier or
21435 mem-initializer; in these contexts a qualified name that
21436 depends on a template-parameter is implicitly assumed to be a
21437 type name.
21438
21439 is to pretend that we have seen the `typename' keyword at this
21440 point. */
21441 cp_parser_nested_name_specifier_opt (parser,
21442 /*typename_keyword_p=*/true,
21443 /*check_dependency_p=*/true,
21444 typename_type,
21445 /*is_declaration=*/true);
21446 /* If the base class is given by a qualified name, assume that names
21447 we see are type names or templates, as appropriate. */
21448 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21449 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21450
21451 if (!parser->scope
21452 && cp_lexer_next_token_is_decltype (parser->lexer))
21453 /* DR 950 allows decltype as a base-specifier. */
21454 type = cp_parser_decltype (parser);
21455 else
21456 {
21457 /* Otherwise, look for the class-name. */
21458 type = cp_parser_class_name (parser,
21459 class_scope_p,
21460 template_p,
21461 typename_type,
21462 /*check_dependency_p=*/true,
21463 /*class_head_p=*/false,
21464 /*is_declaration=*/true);
21465 type = TREE_TYPE (type);
21466 }
21467
21468 if (type == error_mark_node)
21469 return error_mark_node;
21470
21471 return finish_base_specifier (type, access, virtual_p);
21472 }
21473
21474 /* Exception handling [gram.exception] */
21475
21476 /* Parse an (optional) noexcept-specification.
21477
21478 noexcept-specification:
21479 noexcept ( constant-expression ) [opt]
21480
21481 If no noexcept-specification is present, returns NULL_TREE.
21482 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21483 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21484 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21485 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21486 in which case a boolean condition is returned instead. */
21487
21488 static tree
21489 cp_parser_noexcept_specification_opt (cp_parser* parser,
21490 bool require_constexpr,
21491 bool* consumed_expr,
21492 bool return_cond)
21493 {
21494 cp_token *token;
21495 const char *saved_message;
21496
21497 /* Peek at the next token. */
21498 token = cp_lexer_peek_token (parser->lexer);
21499
21500 /* Is it a noexcept-specification? */
21501 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21502 {
21503 tree expr;
21504 cp_lexer_consume_token (parser->lexer);
21505
21506 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21507 {
21508 cp_lexer_consume_token (parser->lexer);
21509
21510 if (require_constexpr)
21511 {
21512 /* Types may not be defined in an exception-specification. */
21513 saved_message = parser->type_definition_forbidden_message;
21514 parser->type_definition_forbidden_message
21515 = G_("types may not be defined in an exception-specification");
21516
21517 expr = cp_parser_constant_expression (parser);
21518
21519 /* Restore the saved message. */
21520 parser->type_definition_forbidden_message = saved_message;
21521 }
21522 else
21523 {
21524 expr = cp_parser_expression (parser);
21525 *consumed_expr = true;
21526 }
21527
21528 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21529 }
21530 else
21531 {
21532 expr = boolean_true_node;
21533 if (!require_constexpr)
21534 *consumed_expr = false;
21535 }
21536
21537 /* We cannot build a noexcept-spec right away because this will check
21538 that expr is a constexpr. */
21539 if (!return_cond)
21540 return build_noexcept_spec (expr, tf_warning_or_error);
21541 else
21542 return expr;
21543 }
21544 else
21545 return NULL_TREE;
21546 }
21547
21548 /* Parse an (optional) exception-specification.
21549
21550 exception-specification:
21551 throw ( type-id-list [opt] )
21552
21553 Returns a TREE_LIST representing the exception-specification. The
21554 TREE_VALUE of each node is a type. */
21555
21556 static tree
21557 cp_parser_exception_specification_opt (cp_parser* parser)
21558 {
21559 cp_token *token;
21560 tree type_id_list;
21561 const char *saved_message;
21562
21563 /* Peek at the next token. */
21564 token = cp_lexer_peek_token (parser->lexer);
21565
21566 /* Is it a noexcept-specification? */
21567 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21568 false);
21569 if (type_id_list != NULL_TREE)
21570 return type_id_list;
21571
21572 /* If it's not `throw', then there's no exception-specification. */
21573 if (!cp_parser_is_keyword (token, RID_THROW))
21574 return NULL_TREE;
21575
21576 #if 0
21577 /* Enable this once a lot of code has transitioned to noexcept? */
21578 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21579 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21580 "deprecated in C++0x; use %<noexcept%> instead");
21581 #endif
21582
21583 /* Consume the `throw'. */
21584 cp_lexer_consume_token (parser->lexer);
21585
21586 /* Look for the `('. */
21587 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21588
21589 /* Peek at the next token. */
21590 token = cp_lexer_peek_token (parser->lexer);
21591 /* If it's not a `)', then there is a type-id-list. */
21592 if (token->type != CPP_CLOSE_PAREN)
21593 {
21594 /* Types may not be defined in an exception-specification. */
21595 saved_message = parser->type_definition_forbidden_message;
21596 parser->type_definition_forbidden_message
21597 = G_("types may not be defined in an exception-specification");
21598 /* Parse the type-id-list. */
21599 type_id_list = cp_parser_type_id_list (parser);
21600 /* Restore the saved message. */
21601 parser->type_definition_forbidden_message = saved_message;
21602 }
21603 else
21604 type_id_list = empty_except_spec;
21605
21606 /* Look for the `)'. */
21607 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21608
21609 return type_id_list;
21610 }
21611
21612 /* Parse an (optional) type-id-list.
21613
21614 type-id-list:
21615 type-id ... [opt]
21616 type-id-list , type-id ... [opt]
21617
21618 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21619 in the order that the types were presented. */
21620
21621 static tree
21622 cp_parser_type_id_list (cp_parser* parser)
21623 {
21624 tree types = NULL_TREE;
21625
21626 while (true)
21627 {
21628 cp_token *token;
21629 tree type;
21630
21631 /* Get the next type-id. */
21632 type = cp_parser_type_id (parser);
21633 /* Parse the optional ellipsis. */
21634 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21635 {
21636 /* Consume the `...'. */
21637 cp_lexer_consume_token (parser->lexer);
21638
21639 /* Turn the type into a pack expansion expression. */
21640 type = make_pack_expansion (type);
21641 }
21642 /* Add it to the list. */
21643 types = add_exception_specifier (types, type, /*complain=*/1);
21644 /* Peek at the next token. */
21645 token = cp_lexer_peek_token (parser->lexer);
21646 /* If it is not a `,', we are done. */
21647 if (token->type != CPP_COMMA)
21648 break;
21649 /* Consume the `,'. */
21650 cp_lexer_consume_token (parser->lexer);
21651 }
21652
21653 return nreverse (types);
21654 }
21655
21656 /* Parse a try-block.
21657
21658 try-block:
21659 try compound-statement handler-seq */
21660
21661 static tree
21662 cp_parser_try_block (cp_parser* parser)
21663 {
21664 tree try_block;
21665
21666 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21667 if (parser->in_function_body
21668 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21669 error ("%<try%> in %<constexpr%> function");
21670
21671 try_block = begin_try_block ();
21672 cp_parser_compound_statement (parser, NULL, true, false);
21673 finish_try_block (try_block);
21674 cp_parser_handler_seq (parser);
21675 finish_handler_sequence (try_block);
21676
21677 return try_block;
21678 }
21679
21680 /* Parse a function-try-block.
21681
21682 function-try-block:
21683 try ctor-initializer [opt] function-body handler-seq */
21684
21685 static bool
21686 cp_parser_function_try_block (cp_parser* parser)
21687 {
21688 tree compound_stmt;
21689 tree try_block;
21690 bool ctor_initializer_p;
21691
21692 /* Look for the `try' keyword. */
21693 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21694 return false;
21695 /* Let the rest of the front end know where we are. */
21696 try_block = begin_function_try_block (&compound_stmt);
21697 /* Parse the function-body. */
21698 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21699 (parser, /*in_function_try_block=*/true);
21700 /* We're done with the `try' part. */
21701 finish_function_try_block (try_block);
21702 /* Parse the handlers. */
21703 cp_parser_handler_seq (parser);
21704 /* We're done with the handlers. */
21705 finish_function_handler_sequence (try_block, compound_stmt);
21706
21707 return ctor_initializer_p;
21708 }
21709
21710 /* Parse a handler-seq.
21711
21712 handler-seq:
21713 handler handler-seq [opt] */
21714
21715 static void
21716 cp_parser_handler_seq (cp_parser* parser)
21717 {
21718 while (true)
21719 {
21720 cp_token *token;
21721
21722 /* Parse the handler. */
21723 cp_parser_handler (parser);
21724 /* Peek at the next token. */
21725 token = cp_lexer_peek_token (parser->lexer);
21726 /* If it's not `catch' then there are no more handlers. */
21727 if (!cp_parser_is_keyword (token, RID_CATCH))
21728 break;
21729 }
21730 }
21731
21732 /* Parse a handler.
21733
21734 handler:
21735 catch ( exception-declaration ) compound-statement */
21736
21737 static void
21738 cp_parser_handler (cp_parser* parser)
21739 {
21740 tree handler;
21741 tree declaration;
21742
21743 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21744 handler = begin_handler ();
21745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21746 declaration = cp_parser_exception_declaration (parser);
21747 finish_handler_parms (declaration, handler);
21748 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21749 cp_parser_compound_statement (parser, NULL, false, false);
21750 finish_handler (handler);
21751 }
21752
21753 /* Parse an exception-declaration.
21754
21755 exception-declaration:
21756 type-specifier-seq declarator
21757 type-specifier-seq abstract-declarator
21758 type-specifier-seq
21759 ...
21760
21761 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21762 ellipsis variant is used. */
21763
21764 static tree
21765 cp_parser_exception_declaration (cp_parser* parser)
21766 {
21767 cp_decl_specifier_seq type_specifiers;
21768 cp_declarator *declarator;
21769 const char *saved_message;
21770
21771 /* If it's an ellipsis, it's easy to handle. */
21772 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21773 {
21774 /* Consume the `...' token. */
21775 cp_lexer_consume_token (parser->lexer);
21776 return NULL_TREE;
21777 }
21778
21779 /* Types may not be defined in exception-declarations. */
21780 saved_message = parser->type_definition_forbidden_message;
21781 parser->type_definition_forbidden_message
21782 = G_("types may not be defined in exception-declarations");
21783
21784 /* Parse the type-specifier-seq. */
21785 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21786 /*is_trailing_return=*/false,
21787 &type_specifiers);
21788 /* If it's a `)', then there is no declarator. */
21789 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21790 declarator = NULL;
21791 else
21792 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21793 /*ctor_dtor_or_conv_p=*/NULL,
21794 /*parenthesized_p=*/NULL,
21795 /*member_p=*/false,
21796 /*friend_p=*/false);
21797
21798 /* Restore the saved message. */
21799 parser->type_definition_forbidden_message = saved_message;
21800
21801 if (!type_specifiers.any_specifiers_p)
21802 return error_mark_node;
21803
21804 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21805 }
21806
21807 /* Parse a throw-expression.
21808
21809 throw-expression:
21810 throw assignment-expression [opt]
21811
21812 Returns a THROW_EXPR representing the throw-expression. */
21813
21814 static tree
21815 cp_parser_throw_expression (cp_parser* parser)
21816 {
21817 tree expression;
21818 cp_token* token;
21819
21820 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21821 token = cp_lexer_peek_token (parser->lexer);
21822 /* Figure out whether or not there is an assignment-expression
21823 following the "throw" keyword. */
21824 if (token->type == CPP_COMMA
21825 || token->type == CPP_SEMICOLON
21826 || token->type == CPP_CLOSE_PAREN
21827 || token->type == CPP_CLOSE_SQUARE
21828 || token->type == CPP_CLOSE_BRACE
21829 || token->type == CPP_COLON)
21830 expression = NULL_TREE;
21831 else
21832 expression = cp_parser_assignment_expression (parser);
21833
21834 return build_throw (expression);
21835 }
21836
21837 /* GNU Extensions */
21838
21839 /* Parse an (optional) asm-specification.
21840
21841 asm-specification:
21842 asm ( string-literal )
21843
21844 If the asm-specification is present, returns a STRING_CST
21845 corresponding to the string-literal. Otherwise, returns
21846 NULL_TREE. */
21847
21848 static tree
21849 cp_parser_asm_specification_opt (cp_parser* parser)
21850 {
21851 cp_token *token;
21852 tree asm_specification;
21853
21854 /* Peek at the next token. */
21855 token = cp_lexer_peek_token (parser->lexer);
21856 /* If the next token isn't the `asm' keyword, then there's no
21857 asm-specification. */
21858 if (!cp_parser_is_keyword (token, RID_ASM))
21859 return NULL_TREE;
21860
21861 /* Consume the `asm' token. */
21862 cp_lexer_consume_token (parser->lexer);
21863 /* Look for the `('. */
21864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21865
21866 /* Look for the string-literal. */
21867 asm_specification = cp_parser_string_literal (parser, false, false);
21868
21869 /* Look for the `)'. */
21870 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21871
21872 return asm_specification;
21873 }
21874
21875 /* Parse an asm-operand-list.
21876
21877 asm-operand-list:
21878 asm-operand
21879 asm-operand-list , asm-operand
21880
21881 asm-operand:
21882 string-literal ( expression )
21883 [ string-literal ] string-literal ( expression )
21884
21885 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21886 each node is the expression. The TREE_PURPOSE is itself a
21887 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21888 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21889 is a STRING_CST for the string literal before the parenthesis. Returns
21890 ERROR_MARK_NODE if any of the operands are invalid. */
21891
21892 static tree
21893 cp_parser_asm_operand_list (cp_parser* parser)
21894 {
21895 tree asm_operands = NULL_TREE;
21896 bool invalid_operands = false;
21897
21898 while (true)
21899 {
21900 tree string_literal;
21901 tree expression;
21902 tree name;
21903
21904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21905 {
21906 /* Consume the `[' token. */
21907 cp_lexer_consume_token (parser->lexer);
21908 /* Read the operand name. */
21909 name = cp_parser_identifier (parser);
21910 if (name != error_mark_node)
21911 name = build_string (IDENTIFIER_LENGTH (name),
21912 IDENTIFIER_POINTER (name));
21913 /* Look for the closing `]'. */
21914 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21915 }
21916 else
21917 name = NULL_TREE;
21918 /* Look for the string-literal. */
21919 string_literal = cp_parser_string_literal (parser, false, false);
21920
21921 /* Look for the `('. */
21922 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21923 /* Parse the expression. */
21924 expression = cp_parser_expression (parser);
21925 /* Look for the `)'. */
21926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21927
21928 if (name == error_mark_node
21929 || string_literal == error_mark_node
21930 || expression == error_mark_node)
21931 invalid_operands = true;
21932
21933 /* Add this operand to the list. */
21934 asm_operands = tree_cons (build_tree_list (name, string_literal),
21935 expression,
21936 asm_operands);
21937 /* If the next token is not a `,', there are no more
21938 operands. */
21939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21940 break;
21941 /* Consume the `,'. */
21942 cp_lexer_consume_token (parser->lexer);
21943 }
21944
21945 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21946 }
21947
21948 /* Parse an asm-clobber-list.
21949
21950 asm-clobber-list:
21951 string-literal
21952 asm-clobber-list , string-literal
21953
21954 Returns a TREE_LIST, indicating the clobbers in the order that they
21955 appeared. The TREE_VALUE of each node is a STRING_CST. */
21956
21957 static tree
21958 cp_parser_asm_clobber_list (cp_parser* parser)
21959 {
21960 tree clobbers = NULL_TREE;
21961
21962 while (true)
21963 {
21964 tree string_literal;
21965
21966 /* Look for the string literal. */
21967 string_literal = cp_parser_string_literal (parser, false, false);
21968 /* Add it to the list. */
21969 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21970 /* If the next token is not a `,', then the list is
21971 complete. */
21972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21973 break;
21974 /* Consume the `,' token. */
21975 cp_lexer_consume_token (parser->lexer);
21976 }
21977
21978 return clobbers;
21979 }
21980
21981 /* Parse an asm-label-list.
21982
21983 asm-label-list:
21984 identifier
21985 asm-label-list , identifier
21986
21987 Returns a TREE_LIST, indicating the labels in the order that they
21988 appeared. The TREE_VALUE of each node is a label. */
21989
21990 static tree
21991 cp_parser_asm_label_list (cp_parser* parser)
21992 {
21993 tree labels = NULL_TREE;
21994
21995 while (true)
21996 {
21997 tree identifier, label, name;
21998
21999 /* Look for the identifier. */
22000 identifier = cp_parser_identifier (parser);
22001 if (!error_operand_p (identifier))
22002 {
22003 label = lookup_label (identifier);
22004 if (TREE_CODE (label) == LABEL_DECL)
22005 {
22006 TREE_USED (label) = 1;
22007 check_goto (label);
22008 name = build_string (IDENTIFIER_LENGTH (identifier),
22009 IDENTIFIER_POINTER (identifier));
22010 labels = tree_cons (name, label, labels);
22011 }
22012 }
22013 /* If the next token is not a `,', then the list is
22014 complete. */
22015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22016 break;
22017 /* Consume the `,' token. */
22018 cp_lexer_consume_token (parser->lexer);
22019 }
22020
22021 return nreverse (labels);
22022 }
22023
22024 /* Return TRUE iff the next tokens in the stream are possibly the
22025 beginning of a GNU extension attribute. */
22026
22027 static bool
22028 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22029 {
22030 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22031 }
22032
22033 /* Return TRUE iff the next tokens in the stream are possibly the
22034 beginning of a standard C++-11 attribute specifier. */
22035
22036 static bool
22037 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22038 {
22039 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22040 }
22041
22042 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22043 beginning of a standard C++-11 attribute specifier. */
22044
22045 static bool
22046 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22047 {
22048 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22049
22050 return (cxx_dialect >= cxx11
22051 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22052 || (token->type == CPP_OPEN_SQUARE
22053 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22054 && token->type == CPP_OPEN_SQUARE)));
22055 }
22056
22057 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22058 beginning of a GNU extension attribute. */
22059
22060 static bool
22061 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22062 {
22063 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22064
22065 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22066 }
22067
22068 /* Return true iff the next tokens can be the beginning of either a
22069 GNU attribute list, or a standard C++11 attribute sequence. */
22070
22071 static bool
22072 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22073 {
22074 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22075 || cp_next_tokens_can_be_std_attribute_p (parser));
22076 }
22077
22078 /* Return true iff the next Nth tokens can be the beginning of either
22079 a GNU attribute list, or a standard C++11 attribute sequence. */
22080
22081 static bool
22082 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22083 {
22084 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22085 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22086 }
22087
22088 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22089 of GNU attributes, or return NULL. */
22090
22091 static tree
22092 cp_parser_attributes_opt (cp_parser *parser)
22093 {
22094 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22095 return cp_parser_gnu_attributes_opt (parser);
22096 return cp_parser_std_attribute_spec_seq (parser);
22097 }
22098
22099 #define CILK_SIMD_FN_CLAUSE_MASK \
22100 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22101 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22102 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22103 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22104 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22105
22106 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22107 vector [(<clauses>)] */
22108
22109 static void
22110 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22111 {
22112 bool first_p = parser->cilk_simd_fn_info == NULL;
22113 cp_token *token = v_token;
22114 if (first_p)
22115 {
22116 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22117 parser->cilk_simd_fn_info->error_seen = false;
22118 parser->cilk_simd_fn_info->fndecl_seen = false;
22119 parser->cilk_simd_fn_info->tokens = vNULL;
22120 }
22121 int paren_scope = 0;
22122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22123 {
22124 cp_lexer_consume_token (parser->lexer);
22125 v_token = cp_lexer_peek_token (parser->lexer);
22126 paren_scope++;
22127 }
22128 while (paren_scope > 0)
22129 {
22130 token = cp_lexer_peek_token (parser->lexer);
22131 if (token->type == CPP_OPEN_PAREN)
22132 paren_scope++;
22133 else if (token->type == CPP_CLOSE_PAREN)
22134 paren_scope--;
22135 /* Do not push the last ')' */
22136 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22137 cp_lexer_consume_token (parser->lexer);
22138 }
22139
22140 token->type = CPP_PRAGMA_EOL;
22141 parser->lexer->next_token = token;
22142 cp_lexer_consume_token (parser->lexer);
22143
22144 struct cp_token_cache *cp
22145 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22146 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22147 }
22148
22149 /* Parse an (optional) series of attributes.
22150
22151 attributes:
22152 attributes attribute
22153
22154 attribute:
22155 __attribute__ (( attribute-list [opt] ))
22156
22157 The return value is as for cp_parser_gnu_attribute_list. */
22158
22159 static tree
22160 cp_parser_gnu_attributes_opt (cp_parser* parser)
22161 {
22162 tree attributes = NULL_TREE;
22163
22164 while (true)
22165 {
22166 cp_token *token;
22167 tree attribute_list;
22168 bool ok = true;
22169
22170 /* Peek at the next token. */
22171 token = cp_lexer_peek_token (parser->lexer);
22172 /* If it's not `__attribute__', then we're done. */
22173 if (token->keyword != RID_ATTRIBUTE)
22174 break;
22175
22176 /* Consume the `__attribute__' keyword. */
22177 cp_lexer_consume_token (parser->lexer);
22178 /* Look for the two `(' tokens. */
22179 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22180 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22181
22182 /* Peek at the next token. */
22183 token = cp_lexer_peek_token (parser->lexer);
22184 if (token->type != CPP_CLOSE_PAREN)
22185 /* Parse the attribute-list. */
22186 attribute_list = cp_parser_gnu_attribute_list (parser);
22187 else
22188 /* If the next token is a `)', then there is no attribute
22189 list. */
22190 attribute_list = NULL;
22191
22192 /* Look for the two `)' tokens. */
22193 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22194 ok = false;
22195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22196 ok = false;
22197 if (!ok)
22198 cp_parser_skip_to_end_of_statement (parser);
22199
22200 /* Add these new attributes to the list. */
22201 attributes = chainon (attributes, attribute_list);
22202 }
22203
22204 return attributes;
22205 }
22206
22207 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22208 "__vector" or "__vector__." */
22209
22210 static inline bool
22211 is_cilkplus_vector_p (tree name)
22212 {
22213 if (flag_cilkplus && is_attribute_p ("vector", name))
22214 return true;
22215 return false;
22216 }
22217
22218 /* Parse a GNU attribute-list.
22219
22220 attribute-list:
22221 attribute
22222 attribute-list , attribute
22223
22224 attribute:
22225 identifier
22226 identifier ( identifier )
22227 identifier ( identifier , expression-list )
22228 identifier ( expression-list )
22229
22230 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22231 to an attribute. The TREE_PURPOSE of each node is the identifier
22232 indicating which attribute is in use. The TREE_VALUE represents
22233 the arguments, if any. */
22234
22235 static tree
22236 cp_parser_gnu_attribute_list (cp_parser* parser)
22237 {
22238 tree attribute_list = NULL_TREE;
22239 bool save_translate_strings_p = parser->translate_strings_p;
22240
22241 parser->translate_strings_p = false;
22242 while (true)
22243 {
22244 cp_token *token;
22245 tree identifier;
22246 tree attribute;
22247
22248 /* Look for the identifier. We also allow keywords here; for
22249 example `__attribute__ ((const))' is legal. */
22250 token = cp_lexer_peek_token (parser->lexer);
22251 if (token->type == CPP_NAME
22252 || token->type == CPP_KEYWORD)
22253 {
22254 tree arguments = NULL_TREE;
22255
22256 /* Consume the token, but save it since we need it for the
22257 SIMD enabled function parsing. */
22258 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22259
22260 /* Save away the identifier that indicates which attribute
22261 this is. */
22262 identifier = (token->type == CPP_KEYWORD)
22263 /* For keywords, use the canonical spelling, not the
22264 parsed identifier. */
22265 ? ridpointers[(int) token->keyword]
22266 : id_token->u.value;
22267
22268 attribute = build_tree_list (identifier, NULL_TREE);
22269
22270 /* Peek at the next token. */
22271 token = cp_lexer_peek_token (parser->lexer);
22272 /* If it's an `(', then parse the attribute arguments. */
22273 if (token->type == CPP_OPEN_PAREN)
22274 {
22275 vec<tree, va_gc> *vec;
22276 int attr_flag = (attribute_takes_identifier_p (identifier)
22277 ? id_attr : normal_attr);
22278 if (is_cilkplus_vector_p (identifier))
22279 {
22280 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22281 continue;
22282 }
22283 else
22284 vec = cp_parser_parenthesized_expression_list
22285 (parser, attr_flag, /*cast_p=*/false,
22286 /*allow_expansion_p=*/false,
22287 /*non_constant_p=*/NULL);
22288 if (vec == NULL)
22289 arguments = error_mark_node;
22290 else
22291 {
22292 arguments = build_tree_list_vec (vec);
22293 release_tree_vector (vec);
22294 }
22295 /* Save the arguments away. */
22296 TREE_VALUE (attribute) = arguments;
22297 }
22298 else if (is_cilkplus_vector_p (identifier))
22299 {
22300 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22301 continue;
22302 }
22303
22304 if (arguments != error_mark_node)
22305 {
22306 /* Add this attribute to the list. */
22307 TREE_CHAIN (attribute) = attribute_list;
22308 attribute_list = attribute;
22309 }
22310
22311 token = cp_lexer_peek_token (parser->lexer);
22312 }
22313 /* Now, look for more attributes. If the next token isn't a
22314 `,', we're done. */
22315 if (token->type != CPP_COMMA)
22316 break;
22317
22318 /* Consume the comma and keep going. */
22319 cp_lexer_consume_token (parser->lexer);
22320 }
22321 parser->translate_strings_p = save_translate_strings_p;
22322
22323 /* We built up the list in reverse order. */
22324 return nreverse (attribute_list);
22325 }
22326
22327 /* Parse a standard C++11 attribute.
22328
22329 The returned representation is a TREE_LIST which TREE_PURPOSE is
22330 the scoped name of the attribute, and the TREE_VALUE is its
22331 arguments list.
22332
22333 Note that the scoped name of the attribute is itself a TREE_LIST
22334 which TREE_PURPOSE is the namespace of the attribute, and
22335 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22336 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22337 and which TREE_PURPOSE is directly the attribute name.
22338
22339 Clients of the attribute code should use get_attribute_namespace
22340 and get_attribute_name to get the actual namespace and name of
22341 attributes, regardless of their being GNU or C++11 attributes.
22342
22343 attribute:
22344 attribute-token attribute-argument-clause [opt]
22345
22346 attribute-token:
22347 identifier
22348 attribute-scoped-token
22349
22350 attribute-scoped-token:
22351 attribute-namespace :: identifier
22352
22353 attribute-namespace:
22354 identifier
22355
22356 attribute-argument-clause:
22357 ( balanced-token-seq )
22358
22359 balanced-token-seq:
22360 balanced-token [opt]
22361 balanced-token-seq balanced-token
22362
22363 balanced-token:
22364 ( balanced-token-seq )
22365 [ balanced-token-seq ]
22366 { balanced-token-seq }. */
22367
22368 static tree
22369 cp_parser_std_attribute (cp_parser *parser)
22370 {
22371 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22372 cp_token *token;
22373
22374 /* First, parse name of the the attribute, a.k.a
22375 attribute-token. */
22376
22377 token = cp_lexer_peek_token (parser->lexer);
22378 if (token->type == CPP_NAME)
22379 attr_id = token->u.value;
22380 else if (token->type == CPP_KEYWORD)
22381 attr_id = ridpointers[(int) token->keyword];
22382 else if (token->flags & NAMED_OP)
22383 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22384
22385 if (attr_id == NULL_TREE)
22386 return NULL_TREE;
22387
22388 cp_lexer_consume_token (parser->lexer);
22389
22390 token = cp_lexer_peek_token (parser->lexer);
22391 if (token->type == CPP_SCOPE)
22392 {
22393 /* We are seeing a scoped attribute token. */
22394
22395 cp_lexer_consume_token (parser->lexer);
22396 attr_ns = attr_id;
22397
22398 token = cp_lexer_consume_token (parser->lexer);
22399 if (token->type == CPP_NAME)
22400 attr_id = token->u.value;
22401 else if (token->type == CPP_KEYWORD)
22402 attr_id = ridpointers[(int) token->keyword];
22403 else
22404 {
22405 error_at (token->location,
22406 "expected an identifier for the attribute name");
22407 return error_mark_node;
22408 }
22409 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22410 NULL_TREE);
22411 token = cp_lexer_peek_token (parser->lexer);
22412 }
22413 else
22414 {
22415 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22416 NULL_TREE);
22417 /* C++11 noreturn attribute is equivalent to GNU's. */
22418 if (is_attribute_p ("noreturn", attr_id))
22419 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22420 /* C++14 deprecated attribute is equivalent to GNU's. */
22421 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22422 {
22423 if (cxx_dialect == cxx11)
22424 pedwarn (token->location, OPT_Wpedantic,
22425 "%<deprecated%> is a C++14 feature;"
22426 " use %<gnu::deprecated%>");
22427 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22428 }
22429 }
22430
22431 /* Now parse the optional argument clause of the attribute. */
22432
22433 if (token->type != CPP_OPEN_PAREN)
22434 return attribute;
22435
22436 {
22437 vec<tree, va_gc> *vec;
22438 int attr_flag = normal_attr;
22439
22440 if (attr_ns == get_identifier ("gnu")
22441 && attribute_takes_identifier_p (attr_id))
22442 /* A GNU attribute that takes an identifier in parameter. */
22443 attr_flag = id_attr;
22444
22445 vec = cp_parser_parenthesized_expression_list
22446 (parser, attr_flag, /*cast_p=*/false,
22447 /*allow_expansion_p=*/true,
22448 /*non_constant_p=*/NULL);
22449 if (vec == NULL)
22450 arguments = error_mark_node;
22451 else
22452 {
22453 arguments = build_tree_list_vec (vec);
22454 release_tree_vector (vec);
22455 }
22456
22457 if (arguments == error_mark_node)
22458 attribute = error_mark_node;
22459 else
22460 TREE_VALUE (attribute) = arguments;
22461 }
22462
22463 return attribute;
22464 }
22465
22466 /* Parse a list of standard C++-11 attributes.
22467
22468 attribute-list:
22469 attribute [opt]
22470 attribute-list , attribute[opt]
22471 attribute ...
22472 attribute-list , attribute ...
22473 */
22474
22475 static tree
22476 cp_parser_std_attribute_list (cp_parser *parser)
22477 {
22478 tree attributes = NULL_TREE, attribute = NULL_TREE;
22479 cp_token *token = NULL;
22480
22481 while (true)
22482 {
22483 attribute = cp_parser_std_attribute (parser);
22484 if (attribute == error_mark_node)
22485 break;
22486 if (attribute != NULL_TREE)
22487 {
22488 TREE_CHAIN (attribute) = attributes;
22489 attributes = attribute;
22490 }
22491 token = cp_lexer_peek_token (parser->lexer);
22492 if (token->type != CPP_COMMA)
22493 break;
22494 cp_lexer_consume_token (parser->lexer);
22495 }
22496 attributes = nreverse (attributes);
22497 return attributes;
22498 }
22499
22500 /* Parse a standard C++-11 attribute specifier.
22501
22502 attribute-specifier:
22503 [ [ attribute-list ] ]
22504 alignment-specifier
22505
22506 alignment-specifier:
22507 alignas ( type-id ... [opt] )
22508 alignas ( alignment-expression ... [opt] ). */
22509
22510 static tree
22511 cp_parser_std_attribute_spec (cp_parser *parser)
22512 {
22513 tree attributes = NULL_TREE;
22514 cp_token *token = cp_lexer_peek_token (parser->lexer);
22515
22516 if (token->type == CPP_OPEN_SQUARE
22517 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22518 {
22519 cp_lexer_consume_token (parser->lexer);
22520 cp_lexer_consume_token (parser->lexer);
22521
22522 attributes = cp_parser_std_attribute_list (parser);
22523
22524 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22525 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22526 cp_parser_skip_to_end_of_statement (parser);
22527 else
22528 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22529 when we are sure that we have actually parsed them. */
22530 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22531 }
22532 else
22533 {
22534 tree alignas_expr;
22535
22536 /* Look for an alignment-specifier. */
22537
22538 token = cp_lexer_peek_token (parser->lexer);
22539
22540 if (token->type != CPP_KEYWORD
22541 || token->keyword != RID_ALIGNAS)
22542 return NULL_TREE;
22543
22544 cp_lexer_consume_token (parser->lexer);
22545 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22546
22547 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22548 {
22549 cp_parser_error (parser, "expected %<(%>");
22550 return error_mark_node;
22551 }
22552
22553 cp_parser_parse_tentatively (parser);
22554 alignas_expr = cp_parser_type_id (parser);
22555
22556 if (!cp_parser_parse_definitely (parser))
22557 {
22558 gcc_assert (alignas_expr == error_mark_node
22559 || alignas_expr == NULL_TREE);
22560
22561 alignas_expr =
22562 cp_parser_assignment_expression (parser);
22563 if (alignas_expr == error_mark_node)
22564 cp_parser_skip_to_end_of_statement (parser);
22565 if (alignas_expr == NULL_TREE
22566 || alignas_expr == error_mark_node)
22567 return alignas_expr;
22568 }
22569
22570 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22571 {
22572 cp_parser_error (parser, "expected %<)%>");
22573 return error_mark_node;
22574 }
22575
22576 alignas_expr = cxx_alignas_expr (alignas_expr);
22577
22578 /* Build the C++-11 representation of an 'aligned'
22579 attribute. */
22580 attributes =
22581 build_tree_list (build_tree_list (get_identifier ("gnu"),
22582 get_identifier ("aligned")),
22583 build_tree_list (NULL_TREE, alignas_expr));
22584 }
22585
22586 return attributes;
22587 }
22588
22589 /* Parse a standard C++-11 attribute-specifier-seq.
22590
22591 attribute-specifier-seq:
22592 attribute-specifier-seq [opt] attribute-specifier
22593 */
22594
22595 static tree
22596 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22597 {
22598 tree attr_specs = NULL;
22599
22600 while (true)
22601 {
22602 tree attr_spec = cp_parser_std_attribute_spec (parser);
22603 if (attr_spec == NULL_TREE)
22604 break;
22605 if (attr_spec == error_mark_node)
22606 return error_mark_node;
22607
22608 TREE_CHAIN (attr_spec) = attr_specs;
22609 attr_specs = attr_spec;
22610 }
22611
22612 attr_specs = nreverse (attr_specs);
22613 return attr_specs;
22614 }
22615
22616 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22617 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22618 current value of the PEDANTIC flag, regardless of whether or not
22619 the `__extension__' keyword is present. The caller is responsible
22620 for restoring the value of the PEDANTIC flag. */
22621
22622 static bool
22623 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22624 {
22625 /* Save the old value of the PEDANTIC flag. */
22626 *saved_pedantic = pedantic;
22627
22628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22629 {
22630 /* Consume the `__extension__' token. */
22631 cp_lexer_consume_token (parser->lexer);
22632 /* We're not being pedantic while the `__extension__' keyword is
22633 in effect. */
22634 pedantic = 0;
22635
22636 return true;
22637 }
22638
22639 return false;
22640 }
22641
22642 /* Parse a label declaration.
22643
22644 label-declaration:
22645 __label__ label-declarator-seq ;
22646
22647 label-declarator-seq:
22648 identifier , label-declarator-seq
22649 identifier */
22650
22651 static void
22652 cp_parser_label_declaration (cp_parser* parser)
22653 {
22654 /* Look for the `__label__' keyword. */
22655 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22656
22657 while (true)
22658 {
22659 tree identifier;
22660
22661 /* Look for an identifier. */
22662 identifier = cp_parser_identifier (parser);
22663 /* If we failed, stop. */
22664 if (identifier == error_mark_node)
22665 break;
22666 /* Declare it as a label. */
22667 finish_label_decl (identifier);
22668 /* If the next token is a `;', stop. */
22669 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22670 break;
22671 /* Look for the `,' separating the label declarations. */
22672 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22673 }
22674
22675 /* Look for the final `;'. */
22676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22677 }
22678
22679 /* Support Functions */
22680
22681 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22682 NAME should have one of the representations used for an
22683 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22684 is returned. If PARSER->SCOPE is a dependent type, then a
22685 SCOPE_REF is returned.
22686
22687 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22688 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22689 was formed. Abstractly, such entities should not be passed to this
22690 function, because they do not need to be looked up, but it is
22691 simpler to check for this special case here, rather than at the
22692 call-sites.
22693
22694 In cases not explicitly covered above, this function returns a
22695 DECL, OVERLOAD, or baselink representing the result of the lookup.
22696 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22697 is returned.
22698
22699 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22700 (e.g., "struct") that was used. In that case bindings that do not
22701 refer to types are ignored.
22702
22703 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22704 ignored.
22705
22706 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22707 are ignored.
22708
22709 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22710 types.
22711
22712 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22713 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22714 NULL_TREE otherwise. */
22715
22716 static tree
22717 cp_parser_lookup_name (cp_parser *parser, tree name,
22718 enum tag_types tag_type,
22719 bool is_template,
22720 bool is_namespace,
22721 bool check_dependency,
22722 tree *ambiguous_decls,
22723 location_t name_location)
22724 {
22725 tree decl;
22726 tree object_type = parser->context->object_type;
22727
22728 /* Assume that the lookup will be unambiguous. */
22729 if (ambiguous_decls)
22730 *ambiguous_decls = NULL_TREE;
22731
22732 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22733 no longer valid. Note that if we are parsing tentatively, and
22734 the parse fails, OBJECT_TYPE will be automatically restored. */
22735 parser->context->object_type = NULL_TREE;
22736
22737 if (name == error_mark_node)
22738 return error_mark_node;
22739
22740 /* A template-id has already been resolved; there is no lookup to
22741 do. */
22742 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22743 return name;
22744 if (BASELINK_P (name))
22745 {
22746 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22747 == TEMPLATE_ID_EXPR);
22748 return name;
22749 }
22750
22751 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22752 it should already have been checked to make sure that the name
22753 used matches the type being destroyed. */
22754 if (TREE_CODE (name) == BIT_NOT_EXPR)
22755 {
22756 tree type;
22757
22758 /* Figure out to which type this destructor applies. */
22759 if (parser->scope)
22760 type = parser->scope;
22761 else if (object_type)
22762 type = object_type;
22763 else
22764 type = current_class_type;
22765 /* If that's not a class type, there is no destructor. */
22766 if (!type || !CLASS_TYPE_P (type))
22767 return error_mark_node;
22768 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22769 lazily_declare_fn (sfk_destructor, type);
22770 if (!CLASSTYPE_DESTRUCTORS (type))
22771 return error_mark_node;
22772 /* If it was a class type, return the destructor. */
22773 return CLASSTYPE_DESTRUCTORS (type);
22774 }
22775
22776 /* By this point, the NAME should be an ordinary identifier. If
22777 the id-expression was a qualified name, the qualifying scope is
22778 stored in PARSER->SCOPE at this point. */
22779 gcc_assert (identifier_p (name));
22780
22781 /* Perform the lookup. */
22782 if (parser->scope)
22783 {
22784 bool dependent_p;
22785
22786 if (parser->scope == error_mark_node)
22787 return error_mark_node;
22788
22789 /* If the SCOPE is dependent, the lookup must be deferred until
22790 the template is instantiated -- unless we are explicitly
22791 looking up names in uninstantiated templates. Even then, we
22792 cannot look up the name if the scope is not a class type; it
22793 might, for example, be a template type parameter. */
22794 dependent_p = (TYPE_P (parser->scope)
22795 && dependent_scope_p (parser->scope));
22796 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22797 && dependent_p)
22798 /* Defer lookup. */
22799 decl = error_mark_node;
22800 else
22801 {
22802 tree pushed_scope = NULL_TREE;
22803
22804 /* If PARSER->SCOPE is a dependent type, then it must be a
22805 class type, and we must not be checking dependencies;
22806 otherwise, we would have processed this lookup above. So
22807 that PARSER->SCOPE is not considered a dependent base by
22808 lookup_member, we must enter the scope here. */
22809 if (dependent_p)
22810 pushed_scope = push_scope (parser->scope);
22811
22812 /* If the PARSER->SCOPE is a template specialization, it
22813 may be instantiated during name lookup. In that case,
22814 errors may be issued. Even if we rollback the current
22815 tentative parse, those errors are valid. */
22816 decl = lookup_qualified_name (parser->scope, name,
22817 tag_type != none_type,
22818 /*complain=*/true);
22819
22820 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22821 lookup result and the nested-name-specifier nominates a class C:
22822 * if the name specified after the nested-name-specifier, when
22823 looked up in C, is the injected-class-name of C (Clause 9), or
22824 * if the name specified after the nested-name-specifier is the
22825 same as the identifier or the simple-template-id's template-
22826 name in the last component of the nested-name-specifier,
22827 the name is instead considered to name the constructor of
22828 class C. [ Note: for example, the constructor is not an
22829 acceptable lookup result in an elaborated-type-specifier so
22830 the constructor would not be used in place of the
22831 injected-class-name. --end note ] Such a constructor name
22832 shall be used only in the declarator-id of a declaration that
22833 names a constructor or in a using-declaration. */
22834 if (tag_type == none_type
22835 && DECL_SELF_REFERENCE_P (decl)
22836 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22837 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22838 tag_type != none_type,
22839 /*complain=*/true);
22840
22841 /* If we have a single function from a using decl, pull it out. */
22842 if (TREE_CODE (decl) == OVERLOAD
22843 && !really_overloaded_fn (decl))
22844 decl = OVL_FUNCTION (decl);
22845
22846 if (pushed_scope)
22847 pop_scope (pushed_scope);
22848 }
22849
22850 /* If the scope is a dependent type and either we deferred lookup or
22851 we did lookup but didn't find the name, rememeber the name. */
22852 if (decl == error_mark_node && TYPE_P (parser->scope)
22853 && dependent_type_p (parser->scope))
22854 {
22855 if (tag_type)
22856 {
22857 tree type;
22858
22859 /* The resolution to Core Issue 180 says that `struct
22860 A::B' should be considered a type-name, even if `A'
22861 is dependent. */
22862 type = make_typename_type (parser->scope, name, tag_type,
22863 /*complain=*/tf_error);
22864 if (type != error_mark_node)
22865 decl = TYPE_NAME (type);
22866 }
22867 else if (is_template
22868 && (cp_parser_next_token_ends_template_argument_p (parser)
22869 || cp_lexer_next_token_is (parser->lexer,
22870 CPP_CLOSE_PAREN)))
22871 decl = make_unbound_class_template (parser->scope,
22872 name, NULL_TREE,
22873 /*complain=*/tf_error);
22874 else
22875 decl = build_qualified_name (/*type=*/NULL_TREE,
22876 parser->scope, name,
22877 is_template);
22878 }
22879 parser->qualifying_scope = parser->scope;
22880 parser->object_scope = NULL_TREE;
22881 }
22882 else if (object_type)
22883 {
22884 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22885 OBJECT_TYPE is not a class. */
22886 if (CLASS_TYPE_P (object_type))
22887 /* If the OBJECT_TYPE is a template specialization, it may
22888 be instantiated during name lookup. In that case, errors
22889 may be issued. Even if we rollback the current tentative
22890 parse, those errors are valid. */
22891 decl = lookup_member (object_type,
22892 name,
22893 /*protect=*/0,
22894 tag_type != none_type,
22895 tf_warning_or_error);
22896 else
22897 decl = NULL_TREE;
22898
22899 if (!decl)
22900 /* Look it up in the enclosing context. */
22901 decl = lookup_name_real (name, tag_type != none_type,
22902 /*nonclass=*/0,
22903 /*block_p=*/true, is_namespace, 0);
22904 parser->object_scope = object_type;
22905 parser->qualifying_scope = NULL_TREE;
22906 }
22907 else
22908 {
22909 decl = lookup_name_real (name, tag_type != none_type,
22910 /*nonclass=*/0,
22911 /*block_p=*/true, is_namespace, 0);
22912 parser->qualifying_scope = NULL_TREE;
22913 parser->object_scope = NULL_TREE;
22914 }
22915
22916 /* If the lookup failed, let our caller know. */
22917 if (!decl || decl == error_mark_node)
22918 return error_mark_node;
22919
22920 /* Pull out the template from an injected-class-name (or multiple). */
22921 if (is_template)
22922 decl = maybe_get_template_decl_from_type_decl (decl);
22923
22924 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22925 if (TREE_CODE (decl) == TREE_LIST)
22926 {
22927 if (ambiguous_decls)
22928 *ambiguous_decls = decl;
22929 /* The error message we have to print is too complicated for
22930 cp_parser_error, so we incorporate its actions directly. */
22931 if (!cp_parser_simulate_error (parser))
22932 {
22933 error_at (name_location, "reference to %qD is ambiguous",
22934 name);
22935 print_candidates (decl);
22936 }
22937 return error_mark_node;
22938 }
22939
22940 gcc_assert (DECL_P (decl)
22941 || TREE_CODE (decl) == OVERLOAD
22942 || TREE_CODE (decl) == SCOPE_REF
22943 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22944 || BASELINK_P (decl));
22945
22946 /* If we have resolved the name of a member declaration, check to
22947 see if the declaration is accessible. When the name resolves to
22948 set of overloaded functions, accessibility is checked when
22949 overload resolution is done.
22950
22951 During an explicit instantiation, access is not checked at all,
22952 as per [temp.explicit]. */
22953 if (DECL_P (decl))
22954 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22955
22956 maybe_record_typedef_use (decl);
22957
22958 return decl;
22959 }
22960
22961 /* Like cp_parser_lookup_name, but for use in the typical case where
22962 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22963 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22964
22965 static tree
22966 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22967 {
22968 return cp_parser_lookup_name (parser, name,
22969 none_type,
22970 /*is_template=*/false,
22971 /*is_namespace=*/false,
22972 /*check_dependency=*/true,
22973 /*ambiguous_decls=*/NULL,
22974 location);
22975 }
22976
22977 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22978 the current context, return the TYPE_DECL. If TAG_NAME_P is
22979 true, the DECL indicates the class being defined in a class-head,
22980 or declared in an elaborated-type-specifier.
22981
22982 Otherwise, return DECL. */
22983
22984 static tree
22985 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22986 {
22987 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22988 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22989
22990 struct A {
22991 template <typename T> struct B;
22992 };
22993
22994 template <typename T> struct A::B {};
22995
22996 Similarly, in an elaborated-type-specifier:
22997
22998 namespace N { struct X{}; }
22999
23000 struct A {
23001 template <typename T> friend struct N::X;
23002 };
23003
23004 However, if the DECL refers to a class type, and we are in
23005 the scope of the class, then the name lookup automatically
23006 finds the TYPE_DECL created by build_self_reference rather
23007 than a TEMPLATE_DECL. For example, in:
23008
23009 template <class T> struct S {
23010 S s;
23011 };
23012
23013 there is no need to handle such case. */
23014
23015 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23016 return DECL_TEMPLATE_RESULT (decl);
23017
23018 return decl;
23019 }
23020
23021 /* If too many, or too few, template-parameter lists apply to the
23022 declarator, issue an error message. Returns TRUE if all went well,
23023 and FALSE otherwise. */
23024
23025 static bool
23026 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23027 cp_declarator *declarator,
23028 location_t declarator_location)
23029 {
23030 switch (declarator->kind)
23031 {
23032 case cdk_id:
23033 {
23034 unsigned num_templates = 0;
23035 tree scope = declarator->u.id.qualifying_scope;
23036
23037 if (scope)
23038 num_templates = num_template_headers_for_class (scope);
23039 else if (TREE_CODE (declarator->u.id.unqualified_name)
23040 == TEMPLATE_ID_EXPR)
23041 /* If the DECLARATOR has the form `X<y>' then it uses one
23042 additional level of template parameters. */
23043 ++num_templates;
23044
23045 return cp_parser_check_template_parameters
23046 (parser, num_templates, declarator_location, declarator);
23047 }
23048
23049 case cdk_function:
23050 case cdk_array:
23051 case cdk_pointer:
23052 case cdk_reference:
23053 case cdk_ptrmem:
23054 return (cp_parser_check_declarator_template_parameters
23055 (parser, declarator->declarator, declarator_location));
23056
23057 case cdk_error:
23058 return true;
23059
23060 default:
23061 gcc_unreachable ();
23062 }
23063 return false;
23064 }
23065
23066 /* NUM_TEMPLATES were used in the current declaration. If that is
23067 invalid, return FALSE and issue an error messages. Otherwise,
23068 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23069 declarator and we can print more accurate diagnostics. */
23070
23071 static bool
23072 cp_parser_check_template_parameters (cp_parser* parser,
23073 unsigned num_templates,
23074 location_t location,
23075 cp_declarator *declarator)
23076 {
23077 /* If there are the same number of template classes and parameter
23078 lists, that's OK. */
23079 if (parser->num_template_parameter_lists == num_templates)
23080 return true;
23081 /* If there are more, but only one more, then we are referring to a
23082 member template. That's OK too. */
23083 if (parser->num_template_parameter_lists == num_templates + 1)
23084 return true;
23085 /* If there are more template classes than parameter lists, we have
23086 something like:
23087
23088 template <class T> void S<T>::R<T>::f (); */
23089 if (parser->num_template_parameter_lists < num_templates)
23090 {
23091 if (declarator && !current_function_decl)
23092 error_at (location, "specializing member %<%T::%E%> "
23093 "requires %<template<>%> syntax",
23094 declarator->u.id.qualifying_scope,
23095 declarator->u.id.unqualified_name);
23096 else if (declarator)
23097 error_at (location, "invalid declaration of %<%T::%E%>",
23098 declarator->u.id.qualifying_scope,
23099 declarator->u.id.unqualified_name);
23100 else
23101 error_at (location, "too few template-parameter-lists");
23102 return false;
23103 }
23104 /* Otherwise, there are too many template parameter lists. We have
23105 something like:
23106
23107 template <class T> template <class U> void S::f(); */
23108 error_at (location, "too many template-parameter-lists");
23109 return false;
23110 }
23111
23112 /* Parse an optional `::' token indicating that the following name is
23113 from the global namespace. If so, PARSER->SCOPE is set to the
23114 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23115 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23116 Returns the new value of PARSER->SCOPE, if the `::' token is
23117 present, and NULL_TREE otherwise. */
23118
23119 static tree
23120 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23121 {
23122 cp_token *token;
23123
23124 /* Peek at the next token. */
23125 token = cp_lexer_peek_token (parser->lexer);
23126 /* If we're looking at a `::' token then we're starting from the
23127 global namespace, not our current location. */
23128 if (token->type == CPP_SCOPE)
23129 {
23130 /* Consume the `::' token. */
23131 cp_lexer_consume_token (parser->lexer);
23132 /* Set the SCOPE so that we know where to start the lookup. */
23133 parser->scope = global_namespace;
23134 parser->qualifying_scope = global_namespace;
23135 parser->object_scope = NULL_TREE;
23136
23137 return parser->scope;
23138 }
23139 else if (!current_scope_valid_p)
23140 {
23141 parser->scope = NULL_TREE;
23142 parser->qualifying_scope = NULL_TREE;
23143 parser->object_scope = NULL_TREE;
23144 }
23145
23146 return NULL_TREE;
23147 }
23148
23149 /* Returns TRUE if the upcoming token sequence is the start of a
23150 constructor declarator. If FRIEND_P is true, the declarator is
23151 preceded by the `friend' specifier. */
23152
23153 static bool
23154 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23155 {
23156 bool constructor_p;
23157 bool outside_class_specifier_p;
23158 tree nested_name_specifier;
23159 cp_token *next_token;
23160
23161 /* The common case is that this is not a constructor declarator, so
23162 try to avoid doing lots of work if at all possible. It's not
23163 valid declare a constructor at function scope. */
23164 if (parser->in_function_body)
23165 return false;
23166 /* And only certain tokens can begin a constructor declarator. */
23167 next_token = cp_lexer_peek_token (parser->lexer);
23168 if (next_token->type != CPP_NAME
23169 && next_token->type != CPP_SCOPE
23170 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23171 && next_token->type != CPP_TEMPLATE_ID)
23172 return false;
23173
23174 /* Parse tentatively; we are going to roll back all of the tokens
23175 consumed here. */
23176 cp_parser_parse_tentatively (parser);
23177 /* Assume that we are looking at a constructor declarator. */
23178 constructor_p = true;
23179
23180 /* Look for the optional `::' operator. */
23181 cp_parser_global_scope_opt (parser,
23182 /*current_scope_valid_p=*/false);
23183 /* Look for the nested-name-specifier. */
23184 nested_name_specifier
23185 = (cp_parser_nested_name_specifier_opt (parser,
23186 /*typename_keyword_p=*/false,
23187 /*check_dependency_p=*/false,
23188 /*type_p=*/false,
23189 /*is_declaration=*/false));
23190
23191 outside_class_specifier_p = (!at_class_scope_p ()
23192 || !TYPE_BEING_DEFINED (current_class_type)
23193 || friend_p);
23194
23195 /* Outside of a class-specifier, there must be a
23196 nested-name-specifier. */
23197 if (!nested_name_specifier && outside_class_specifier_p)
23198 constructor_p = false;
23199 else if (nested_name_specifier == error_mark_node)
23200 constructor_p = false;
23201
23202 /* If we have a class scope, this is easy; DR 147 says that S::S always
23203 names the constructor, and no other qualified name could. */
23204 if (constructor_p && nested_name_specifier
23205 && CLASS_TYPE_P (nested_name_specifier))
23206 {
23207 tree id = cp_parser_unqualified_id (parser,
23208 /*template_keyword_p=*/false,
23209 /*check_dependency_p=*/false,
23210 /*declarator_p=*/true,
23211 /*optional_p=*/false);
23212 if (is_overloaded_fn (id))
23213 id = DECL_NAME (get_first_fn (id));
23214 if (!constructor_name_p (id, nested_name_specifier))
23215 constructor_p = false;
23216 }
23217 /* If we still think that this might be a constructor-declarator,
23218 look for a class-name. */
23219 else if (constructor_p)
23220 {
23221 /* If we have:
23222
23223 template <typename T> struct S {
23224 S();
23225 };
23226
23227 we must recognize that the nested `S' names a class. */
23228 tree type_decl;
23229 type_decl = cp_parser_class_name (parser,
23230 /*typename_keyword_p=*/false,
23231 /*template_keyword_p=*/false,
23232 none_type,
23233 /*check_dependency_p=*/false,
23234 /*class_head_p=*/false,
23235 /*is_declaration=*/false);
23236 /* If there was no class-name, then this is not a constructor.
23237 Otherwise, if we are in a class-specifier and we aren't
23238 handling a friend declaration, check that its type matches
23239 current_class_type (c++/38313). Note: error_mark_node
23240 is left alone for error recovery purposes. */
23241 constructor_p = (!cp_parser_error_occurred (parser)
23242 && (outside_class_specifier_p
23243 || type_decl == error_mark_node
23244 || same_type_p (current_class_type,
23245 TREE_TYPE (type_decl))));
23246
23247 /* If we're still considering a constructor, we have to see a `(',
23248 to begin the parameter-declaration-clause, followed by either a
23249 `)', an `...', or a decl-specifier. We need to check for a
23250 type-specifier to avoid being fooled into thinking that:
23251
23252 S (f) (int);
23253
23254 is a constructor. (It is actually a function named `f' that
23255 takes one parameter (of type `int') and returns a value of type
23256 `S'. */
23257 if (constructor_p
23258 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23259 constructor_p = false;
23260
23261 if (constructor_p
23262 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23263 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23264 /* A parameter declaration begins with a decl-specifier,
23265 which is either the "attribute" keyword, a storage class
23266 specifier, or (usually) a type-specifier. */
23267 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23268 {
23269 tree type;
23270 tree pushed_scope = NULL_TREE;
23271 unsigned saved_num_template_parameter_lists;
23272
23273 /* Names appearing in the type-specifier should be looked up
23274 in the scope of the class. */
23275 if (current_class_type)
23276 type = NULL_TREE;
23277 else
23278 {
23279 type = TREE_TYPE (type_decl);
23280 if (TREE_CODE (type) == TYPENAME_TYPE)
23281 {
23282 type = resolve_typename_type (type,
23283 /*only_current_p=*/false);
23284 if (TREE_CODE (type) == TYPENAME_TYPE)
23285 {
23286 cp_parser_abort_tentative_parse (parser);
23287 return false;
23288 }
23289 }
23290 pushed_scope = push_scope (type);
23291 }
23292
23293 /* Inside the constructor parameter list, surrounding
23294 template-parameter-lists do not apply. */
23295 saved_num_template_parameter_lists
23296 = parser->num_template_parameter_lists;
23297 parser->num_template_parameter_lists = 0;
23298
23299 /* Look for the type-specifier. */
23300 cp_parser_type_specifier (parser,
23301 CP_PARSER_FLAGS_NONE,
23302 /*decl_specs=*/NULL,
23303 /*is_declarator=*/true,
23304 /*declares_class_or_enum=*/NULL,
23305 /*is_cv_qualifier=*/NULL);
23306
23307 parser->num_template_parameter_lists
23308 = saved_num_template_parameter_lists;
23309
23310 /* Leave the scope of the class. */
23311 if (pushed_scope)
23312 pop_scope (pushed_scope);
23313
23314 constructor_p = !cp_parser_error_occurred (parser);
23315 }
23316 }
23317
23318 /* We did not really want to consume any tokens. */
23319 cp_parser_abort_tentative_parse (parser);
23320
23321 return constructor_p;
23322 }
23323
23324 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23325 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23326 they must be performed once we are in the scope of the function.
23327
23328 Returns the function defined. */
23329
23330 static tree
23331 cp_parser_function_definition_from_specifiers_and_declarator
23332 (cp_parser* parser,
23333 cp_decl_specifier_seq *decl_specifiers,
23334 tree attributes,
23335 const cp_declarator *declarator)
23336 {
23337 tree fn;
23338 bool success_p;
23339
23340 /* Begin the function-definition. */
23341 success_p = start_function (decl_specifiers, declarator, attributes);
23342
23343 /* The things we're about to see are not directly qualified by any
23344 template headers we've seen thus far. */
23345 reset_specialization ();
23346
23347 /* If there were names looked up in the decl-specifier-seq that we
23348 did not check, check them now. We must wait until we are in the
23349 scope of the function to perform the checks, since the function
23350 might be a friend. */
23351 perform_deferred_access_checks (tf_warning_or_error);
23352
23353 if (success_p)
23354 {
23355 cp_finalize_omp_declare_simd (parser, current_function_decl);
23356 parser->omp_declare_simd = NULL;
23357 }
23358
23359 if (!success_p)
23360 {
23361 /* Skip the entire function. */
23362 cp_parser_skip_to_end_of_block_or_statement (parser);
23363 fn = error_mark_node;
23364 }
23365 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23366 {
23367 /* Seen already, skip it. An error message has already been output. */
23368 cp_parser_skip_to_end_of_block_or_statement (parser);
23369 fn = current_function_decl;
23370 current_function_decl = NULL_TREE;
23371 /* If this is a function from a class, pop the nested class. */
23372 if (current_class_name)
23373 pop_nested_class ();
23374 }
23375 else
23376 {
23377 timevar_id_t tv;
23378 if (DECL_DECLARED_INLINE_P (current_function_decl))
23379 tv = TV_PARSE_INLINE;
23380 else
23381 tv = TV_PARSE_FUNC;
23382 timevar_push (tv);
23383 fn = cp_parser_function_definition_after_declarator (parser,
23384 /*inline_p=*/false);
23385 timevar_pop (tv);
23386 }
23387
23388 return fn;
23389 }
23390
23391 /* Parse the part of a function-definition that follows the
23392 declarator. INLINE_P is TRUE iff this function is an inline
23393 function defined within a class-specifier.
23394
23395 Returns the function defined. */
23396
23397 static tree
23398 cp_parser_function_definition_after_declarator (cp_parser* parser,
23399 bool inline_p)
23400 {
23401 tree fn;
23402 bool ctor_initializer_p = false;
23403 bool saved_in_unbraced_linkage_specification_p;
23404 bool saved_in_function_body;
23405 unsigned saved_num_template_parameter_lists;
23406 cp_token *token;
23407 bool fully_implicit_function_template_p
23408 = parser->fully_implicit_function_template_p;
23409 parser->fully_implicit_function_template_p = false;
23410 tree implicit_template_parms
23411 = parser->implicit_template_parms;
23412 parser->implicit_template_parms = 0;
23413 cp_binding_level* implicit_template_scope
23414 = parser->implicit_template_scope;
23415 parser->implicit_template_scope = 0;
23416
23417 saved_in_function_body = parser->in_function_body;
23418 parser->in_function_body = true;
23419 /* If the next token is `return', then the code may be trying to
23420 make use of the "named return value" extension that G++ used to
23421 support. */
23422 token = cp_lexer_peek_token (parser->lexer);
23423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23424 {
23425 /* Consume the `return' keyword. */
23426 cp_lexer_consume_token (parser->lexer);
23427 /* Look for the identifier that indicates what value is to be
23428 returned. */
23429 cp_parser_identifier (parser);
23430 /* Issue an error message. */
23431 error_at (token->location,
23432 "named return values are no longer supported");
23433 /* Skip tokens until we reach the start of the function body. */
23434 while (true)
23435 {
23436 cp_token *token = cp_lexer_peek_token (parser->lexer);
23437 if (token->type == CPP_OPEN_BRACE
23438 || token->type == CPP_EOF
23439 || token->type == CPP_PRAGMA_EOL)
23440 break;
23441 cp_lexer_consume_token (parser->lexer);
23442 }
23443 }
23444 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23445 anything declared inside `f'. */
23446 saved_in_unbraced_linkage_specification_p
23447 = parser->in_unbraced_linkage_specification_p;
23448 parser->in_unbraced_linkage_specification_p = false;
23449 /* Inside the function, surrounding template-parameter-lists do not
23450 apply. */
23451 saved_num_template_parameter_lists
23452 = parser->num_template_parameter_lists;
23453 parser->num_template_parameter_lists = 0;
23454
23455 start_lambda_scope (current_function_decl);
23456
23457 /* If the next token is `try', `__transaction_atomic', or
23458 `__transaction_relaxed`, then we are looking at either function-try-block
23459 or function-transaction-block. Note that all of these include the
23460 function-body. */
23461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23462 ctor_initializer_p = cp_parser_function_transaction (parser,
23463 RID_TRANSACTION_ATOMIC);
23464 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23465 RID_TRANSACTION_RELAXED))
23466 ctor_initializer_p = cp_parser_function_transaction (parser,
23467 RID_TRANSACTION_RELAXED);
23468 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23469 ctor_initializer_p = cp_parser_function_try_block (parser);
23470 else
23471 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23472 (parser, /*in_function_try_block=*/false);
23473
23474 finish_lambda_scope ();
23475
23476 /* Finish the function. */
23477 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23478 (inline_p ? 2 : 0));
23479 /* Generate code for it, if necessary. */
23480 expand_or_defer_fn (fn);
23481 /* Restore the saved values. */
23482 parser->in_unbraced_linkage_specification_p
23483 = saved_in_unbraced_linkage_specification_p;
23484 parser->num_template_parameter_lists
23485 = saved_num_template_parameter_lists;
23486 parser->in_function_body = saved_in_function_body;
23487
23488 parser->fully_implicit_function_template_p
23489 = fully_implicit_function_template_p;
23490 parser->implicit_template_parms
23491 = implicit_template_parms;
23492 parser->implicit_template_scope
23493 = implicit_template_scope;
23494
23495 if (parser->fully_implicit_function_template_p)
23496 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23497
23498 return fn;
23499 }
23500
23501 /* Parse a template-declaration, assuming that the `export' (and
23502 `extern') keywords, if present, has already been scanned. MEMBER_P
23503 is as for cp_parser_template_declaration. */
23504
23505 static void
23506 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23507 {
23508 tree decl = NULL_TREE;
23509 vec<deferred_access_check, va_gc> *checks;
23510 tree parameter_list;
23511 bool friend_p = false;
23512 bool need_lang_pop;
23513 cp_token *token;
23514
23515 /* Look for the `template' keyword. */
23516 token = cp_lexer_peek_token (parser->lexer);
23517 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23518 return;
23519
23520 /* And the `<'. */
23521 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23522 return;
23523 if (at_class_scope_p () && current_function_decl)
23524 {
23525 /* 14.5.2.2 [temp.mem]
23526
23527 A local class shall not have member templates. */
23528 error_at (token->location,
23529 "invalid declaration of member template in local class");
23530 cp_parser_skip_to_end_of_block_or_statement (parser);
23531 return;
23532 }
23533 /* [temp]
23534
23535 A template ... shall not have C linkage. */
23536 if (current_lang_name == lang_name_c)
23537 {
23538 error_at (token->location, "template with C linkage");
23539 /* Give it C++ linkage to avoid confusing other parts of the
23540 front end. */
23541 push_lang_context (lang_name_cplusplus);
23542 need_lang_pop = true;
23543 }
23544 else
23545 need_lang_pop = false;
23546
23547 /* We cannot perform access checks on the template parameter
23548 declarations until we know what is being declared, just as we
23549 cannot check the decl-specifier list. */
23550 push_deferring_access_checks (dk_deferred);
23551
23552 /* If the next token is `>', then we have an invalid
23553 specialization. Rather than complain about an invalid template
23554 parameter, issue an error message here. */
23555 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23556 {
23557 cp_parser_error (parser, "invalid explicit specialization");
23558 begin_specialization ();
23559 parameter_list = NULL_TREE;
23560 }
23561 else
23562 {
23563 /* Parse the template parameters. */
23564 parameter_list = cp_parser_template_parameter_list (parser);
23565 }
23566
23567 /* Get the deferred access checks from the parameter list. These
23568 will be checked once we know what is being declared, as for a
23569 member template the checks must be performed in the scope of the
23570 class containing the member. */
23571 checks = get_deferred_access_checks ();
23572
23573 /* Look for the `>'. */
23574 cp_parser_skip_to_end_of_template_parameter_list (parser);
23575 /* We just processed one more parameter list. */
23576 ++parser->num_template_parameter_lists;
23577 /* If the next token is `template', there are more template
23578 parameters. */
23579 if (cp_lexer_next_token_is_keyword (parser->lexer,
23580 RID_TEMPLATE))
23581 cp_parser_template_declaration_after_export (parser, member_p);
23582 else if (cxx_dialect >= cxx11
23583 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23584 decl = cp_parser_alias_declaration (parser);
23585 else
23586 {
23587 /* There are no access checks when parsing a template, as we do not
23588 know if a specialization will be a friend. */
23589 push_deferring_access_checks (dk_no_check);
23590 token = cp_lexer_peek_token (parser->lexer);
23591 decl = cp_parser_single_declaration (parser,
23592 checks,
23593 member_p,
23594 /*explicit_specialization_p=*/false,
23595 &friend_p);
23596 pop_deferring_access_checks ();
23597
23598 /* If this is a member template declaration, let the front
23599 end know. */
23600 if (member_p && !friend_p && decl)
23601 {
23602 if (TREE_CODE (decl) == TYPE_DECL)
23603 cp_parser_check_access_in_redeclaration (decl, token->location);
23604
23605 decl = finish_member_template_decl (decl);
23606 }
23607 else if (friend_p && decl
23608 && DECL_DECLARES_TYPE_P (decl))
23609 make_friend_class (current_class_type, TREE_TYPE (decl),
23610 /*complain=*/true);
23611 }
23612 /* We are done with the current parameter list. */
23613 --parser->num_template_parameter_lists;
23614
23615 pop_deferring_access_checks ();
23616
23617 /* Finish up. */
23618 finish_template_decl (parameter_list);
23619
23620 /* Check the template arguments for a literal operator template. */
23621 if (decl
23622 && DECL_DECLARES_FUNCTION_P (decl)
23623 && UDLIT_OPER_P (DECL_NAME (decl)))
23624 {
23625 bool ok = true;
23626 if (parameter_list == NULL_TREE)
23627 ok = false;
23628 else
23629 {
23630 int num_parms = TREE_VEC_LENGTH (parameter_list);
23631 if (num_parms == 1)
23632 {
23633 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23634 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23635 if (TREE_TYPE (parm) != char_type_node
23636 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23637 ok = false;
23638 }
23639 else if (num_parms == 2 && cxx_dialect >= cxx14)
23640 {
23641 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23642 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23643 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23644 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23645 if (TREE_TYPE (parm) != TREE_TYPE (type)
23646 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23647 ok = false;
23648 }
23649 else
23650 ok = false;
23651 }
23652 if (!ok)
23653 {
23654 if (cxx_dialect >= cxx14)
23655 error ("literal operator template %qD has invalid parameter list."
23656 " Expected non-type template argument pack <char...>"
23657 " or <typename CharT, CharT...>",
23658 decl);
23659 else
23660 error ("literal operator template %qD has invalid parameter list."
23661 " Expected non-type template argument pack <char...>",
23662 decl);
23663 }
23664 }
23665 /* Register member declarations. */
23666 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23667 finish_member_declaration (decl);
23668 /* For the erroneous case of a template with C linkage, we pushed an
23669 implicit C++ linkage scope; exit that scope now. */
23670 if (need_lang_pop)
23671 pop_lang_context ();
23672 /* If DECL is a function template, we must return to parse it later.
23673 (Even though there is no definition, there might be default
23674 arguments that need handling.) */
23675 if (member_p && decl
23676 && DECL_DECLARES_FUNCTION_P (decl))
23677 vec_safe_push (unparsed_funs_with_definitions, decl);
23678 }
23679
23680 /* Perform the deferred access checks from a template-parameter-list.
23681 CHECKS is a TREE_LIST of access checks, as returned by
23682 get_deferred_access_checks. */
23683
23684 static void
23685 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23686 {
23687 ++processing_template_parmlist;
23688 perform_access_checks (checks, tf_warning_or_error);
23689 --processing_template_parmlist;
23690 }
23691
23692 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23693 `function-definition' sequence that follows a template header.
23694 If MEMBER_P is true, this declaration appears in a class scope.
23695
23696 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23697 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23698
23699 static tree
23700 cp_parser_single_declaration (cp_parser* parser,
23701 vec<deferred_access_check, va_gc> *checks,
23702 bool member_p,
23703 bool explicit_specialization_p,
23704 bool* friend_p)
23705 {
23706 int declares_class_or_enum;
23707 tree decl = NULL_TREE;
23708 cp_decl_specifier_seq decl_specifiers;
23709 bool function_definition_p = false;
23710 cp_token *decl_spec_token_start;
23711
23712 /* This function is only used when processing a template
23713 declaration. */
23714 gcc_assert (innermost_scope_kind () == sk_template_parms
23715 || innermost_scope_kind () == sk_template_spec);
23716
23717 /* Defer access checks until we know what is being declared. */
23718 push_deferring_access_checks (dk_deferred);
23719
23720 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23721 alternative. */
23722 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23723 cp_parser_decl_specifier_seq (parser,
23724 CP_PARSER_FLAGS_OPTIONAL,
23725 &decl_specifiers,
23726 &declares_class_or_enum);
23727 if (friend_p)
23728 *friend_p = cp_parser_friend_p (&decl_specifiers);
23729
23730 /* There are no template typedefs. */
23731 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23732 {
23733 error_at (decl_spec_token_start->location,
23734 "template declaration of %<typedef%>");
23735 decl = error_mark_node;
23736 }
23737
23738 /* Gather up the access checks that occurred the
23739 decl-specifier-seq. */
23740 stop_deferring_access_checks ();
23741
23742 /* Check for the declaration of a template class. */
23743 if (declares_class_or_enum)
23744 {
23745 if (cp_parser_declares_only_class_p (parser))
23746 {
23747 decl = shadow_tag (&decl_specifiers);
23748
23749 /* In this case:
23750
23751 struct C {
23752 friend template <typename T> struct A<T>::B;
23753 };
23754
23755 A<T>::B will be represented by a TYPENAME_TYPE, and
23756 therefore not recognized by shadow_tag. */
23757 if (friend_p && *friend_p
23758 && !decl
23759 && decl_specifiers.type
23760 && TYPE_P (decl_specifiers.type))
23761 decl = decl_specifiers.type;
23762
23763 if (decl && decl != error_mark_node)
23764 decl = TYPE_NAME (decl);
23765 else
23766 decl = error_mark_node;
23767
23768 /* Perform access checks for template parameters. */
23769 cp_parser_perform_template_parameter_access_checks (checks);
23770 }
23771 }
23772
23773 /* Complain about missing 'typename' or other invalid type names. */
23774 if (!decl_specifiers.any_type_specifiers_p
23775 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23776 {
23777 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23778 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23779 the rest of this declaration. */
23780 decl = error_mark_node;
23781 goto out;
23782 }
23783
23784 /* If it's not a template class, try for a template function. If
23785 the next token is a `;', then this declaration does not declare
23786 anything. But, if there were errors in the decl-specifiers, then
23787 the error might well have come from an attempted class-specifier.
23788 In that case, there's no need to warn about a missing declarator. */
23789 if (!decl
23790 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23791 || decl_specifiers.type != error_mark_node))
23792 {
23793 decl = cp_parser_init_declarator (parser,
23794 &decl_specifiers,
23795 checks,
23796 /*function_definition_allowed_p=*/true,
23797 member_p,
23798 declares_class_or_enum,
23799 &function_definition_p,
23800 NULL, NULL);
23801
23802 /* 7.1.1-1 [dcl.stc]
23803
23804 A storage-class-specifier shall not be specified in an explicit
23805 specialization... */
23806 if (decl
23807 && explicit_specialization_p
23808 && decl_specifiers.storage_class != sc_none)
23809 {
23810 error_at (decl_spec_token_start->location,
23811 "explicit template specialization cannot have a storage class");
23812 decl = error_mark_node;
23813 }
23814
23815 if (decl && VAR_P (decl))
23816 check_template_variable (decl);
23817 }
23818
23819 /* Look for a trailing `;' after the declaration. */
23820 if (!function_definition_p
23821 && (decl == error_mark_node
23822 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23823 cp_parser_skip_to_end_of_block_or_statement (parser);
23824
23825 out:
23826 pop_deferring_access_checks ();
23827
23828 /* Clear any current qualification; whatever comes next is the start
23829 of something new. */
23830 parser->scope = NULL_TREE;
23831 parser->qualifying_scope = NULL_TREE;
23832 parser->object_scope = NULL_TREE;
23833
23834 return decl;
23835 }
23836
23837 /* Parse a cast-expression that is not the operand of a unary "&". */
23838
23839 static tree
23840 cp_parser_simple_cast_expression (cp_parser *parser)
23841 {
23842 return cp_parser_cast_expression (parser, /*address_p=*/false,
23843 /*cast_p=*/false, /*decltype*/false, NULL);
23844 }
23845
23846 /* Parse a functional cast to TYPE. Returns an expression
23847 representing the cast. */
23848
23849 static tree
23850 cp_parser_functional_cast (cp_parser* parser, tree type)
23851 {
23852 vec<tree, va_gc> *vec;
23853 tree expression_list;
23854 tree cast;
23855 bool nonconst_p;
23856
23857 if (!type)
23858 type = error_mark_node;
23859
23860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23861 {
23862 cp_lexer_set_source_position (parser->lexer);
23863 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23864 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23865 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23866 if (TREE_CODE (type) == TYPE_DECL)
23867 type = TREE_TYPE (type);
23868 return finish_compound_literal (type, expression_list,
23869 tf_warning_or_error);
23870 }
23871
23872
23873 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23874 /*cast_p=*/true,
23875 /*allow_expansion_p=*/true,
23876 /*non_constant_p=*/NULL);
23877 if (vec == NULL)
23878 expression_list = error_mark_node;
23879 else
23880 {
23881 expression_list = build_tree_list_vec (vec);
23882 release_tree_vector (vec);
23883 }
23884
23885 cast = build_functional_cast (type, expression_list,
23886 tf_warning_or_error);
23887 /* [expr.const]/1: In an integral constant expression "only type
23888 conversions to integral or enumeration type can be used". */
23889 if (TREE_CODE (type) == TYPE_DECL)
23890 type = TREE_TYPE (type);
23891 if (cast != error_mark_node
23892 && !cast_valid_in_integral_constant_expression_p (type)
23893 && cp_parser_non_integral_constant_expression (parser,
23894 NIC_CONSTRUCTOR))
23895 return error_mark_node;
23896 return cast;
23897 }
23898
23899 /* Save the tokens that make up the body of a member function defined
23900 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23901 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23902 specifiers applied to the declaration. Returns the FUNCTION_DECL
23903 for the member function. */
23904
23905 static tree
23906 cp_parser_save_member_function_body (cp_parser* parser,
23907 cp_decl_specifier_seq *decl_specifiers,
23908 cp_declarator *declarator,
23909 tree attributes)
23910 {
23911 cp_token *first;
23912 cp_token *last;
23913 tree fn;
23914
23915 /* Create the FUNCTION_DECL. */
23916 fn = grokmethod (decl_specifiers, declarator, attributes);
23917 cp_finalize_omp_declare_simd (parser, fn);
23918 /* If something went badly wrong, bail out now. */
23919 if (fn == error_mark_node)
23920 {
23921 /* If there's a function-body, skip it. */
23922 if (cp_parser_token_starts_function_definition_p
23923 (cp_lexer_peek_token (parser->lexer)))
23924 cp_parser_skip_to_end_of_block_or_statement (parser);
23925 return error_mark_node;
23926 }
23927
23928 /* Remember it, if there default args to post process. */
23929 cp_parser_save_default_args (parser, fn);
23930
23931 /* Save away the tokens that make up the body of the
23932 function. */
23933 first = parser->lexer->next_token;
23934 /* Handle function try blocks. */
23935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23936 cp_lexer_consume_token (parser->lexer);
23937 /* We can have braced-init-list mem-initializers before the fn body. */
23938 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23939 {
23940 cp_lexer_consume_token (parser->lexer);
23941 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23942 {
23943 /* cache_group will stop after an un-nested { } pair, too. */
23944 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23945 break;
23946
23947 /* variadic mem-inits have ... after the ')'. */
23948 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23949 cp_lexer_consume_token (parser->lexer);
23950 }
23951 }
23952 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23953 /* Handle function try blocks. */
23954 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23955 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23956 last = parser->lexer->next_token;
23957
23958 /* Save away the inline definition; we will process it when the
23959 class is complete. */
23960 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23961 DECL_PENDING_INLINE_P (fn) = 1;
23962
23963 /* We need to know that this was defined in the class, so that
23964 friend templates are handled correctly. */
23965 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23966
23967 /* Add FN to the queue of functions to be parsed later. */
23968 vec_safe_push (unparsed_funs_with_definitions, fn);
23969
23970 return fn;
23971 }
23972
23973 /* Save the tokens that make up the in-class initializer for a non-static
23974 data member. Returns a DEFAULT_ARG. */
23975
23976 static tree
23977 cp_parser_save_nsdmi (cp_parser* parser)
23978 {
23979 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23980 }
23981
23982 /* Parse a template-argument-list, as well as the trailing ">" (but
23983 not the opening "<"). See cp_parser_template_argument_list for the
23984 return value. */
23985
23986 static tree
23987 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23988 {
23989 tree arguments;
23990 tree saved_scope;
23991 tree saved_qualifying_scope;
23992 tree saved_object_scope;
23993 bool saved_greater_than_is_operator_p;
23994 int saved_unevaluated_operand;
23995 int saved_inhibit_evaluation_warnings;
23996
23997 /* [temp.names]
23998
23999 When parsing a template-id, the first non-nested `>' is taken as
24000 the end of the template-argument-list rather than a greater-than
24001 operator. */
24002 saved_greater_than_is_operator_p
24003 = parser->greater_than_is_operator_p;
24004 parser->greater_than_is_operator_p = false;
24005 /* Parsing the argument list may modify SCOPE, so we save it
24006 here. */
24007 saved_scope = parser->scope;
24008 saved_qualifying_scope = parser->qualifying_scope;
24009 saved_object_scope = parser->object_scope;
24010 /* We need to evaluate the template arguments, even though this
24011 template-id may be nested within a "sizeof". */
24012 saved_unevaluated_operand = cp_unevaluated_operand;
24013 cp_unevaluated_operand = 0;
24014 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24015 c_inhibit_evaluation_warnings = 0;
24016 /* Parse the template-argument-list itself. */
24017 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24018 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24019 arguments = NULL_TREE;
24020 else
24021 arguments = cp_parser_template_argument_list (parser);
24022 /* Look for the `>' that ends the template-argument-list. If we find
24023 a '>>' instead, it's probably just a typo. */
24024 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24025 {
24026 if (cxx_dialect != cxx98)
24027 {
24028 /* In C++0x, a `>>' in a template argument list or cast
24029 expression is considered to be two separate `>'
24030 tokens. So, change the current token to a `>', but don't
24031 consume it: it will be consumed later when the outer
24032 template argument list (or cast expression) is parsed.
24033 Note that this replacement of `>' for `>>' is necessary
24034 even if we are parsing tentatively: in the tentative
24035 case, after calling
24036 cp_parser_enclosed_template_argument_list we will always
24037 throw away all of the template arguments and the first
24038 closing `>', either because the template argument list
24039 was erroneous or because we are replacing those tokens
24040 with a CPP_TEMPLATE_ID token. The second `>' (which will
24041 not have been thrown away) is needed either to close an
24042 outer template argument list or to complete a new-style
24043 cast. */
24044 cp_token *token = cp_lexer_peek_token (parser->lexer);
24045 token->type = CPP_GREATER;
24046 }
24047 else if (!saved_greater_than_is_operator_p)
24048 {
24049 /* If we're in a nested template argument list, the '>>' has
24050 to be a typo for '> >'. We emit the error message, but we
24051 continue parsing and we push a '>' as next token, so that
24052 the argument list will be parsed correctly. Note that the
24053 global source location is still on the token before the
24054 '>>', so we need to say explicitly where we want it. */
24055 cp_token *token = cp_lexer_peek_token (parser->lexer);
24056 error_at (token->location, "%<>>%> should be %<> >%> "
24057 "within a nested template argument list");
24058
24059 token->type = CPP_GREATER;
24060 }
24061 else
24062 {
24063 /* If this is not a nested template argument list, the '>>'
24064 is a typo for '>'. Emit an error message and continue.
24065 Same deal about the token location, but here we can get it
24066 right by consuming the '>>' before issuing the diagnostic. */
24067 cp_token *token = cp_lexer_consume_token (parser->lexer);
24068 error_at (token->location,
24069 "spurious %<>>%>, use %<>%> to terminate "
24070 "a template argument list");
24071 }
24072 }
24073 else
24074 cp_parser_skip_to_end_of_template_parameter_list (parser);
24075 /* The `>' token might be a greater-than operator again now. */
24076 parser->greater_than_is_operator_p
24077 = saved_greater_than_is_operator_p;
24078 /* Restore the SAVED_SCOPE. */
24079 parser->scope = saved_scope;
24080 parser->qualifying_scope = saved_qualifying_scope;
24081 parser->object_scope = saved_object_scope;
24082 cp_unevaluated_operand = saved_unevaluated_operand;
24083 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24084
24085 return arguments;
24086 }
24087
24088 /* MEMBER_FUNCTION is a member function, or a friend. If default
24089 arguments, or the body of the function have not yet been parsed,
24090 parse them now. */
24091
24092 static void
24093 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24094 {
24095 timevar_push (TV_PARSE_INMETH);
24096 /* If this member is a template, get the underlying
24097 FUNCTION_DECL. */
24098 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24099 member_function = DECL_TEMPLATE_RESULT (member_function);
24100
24101 /* There should not be any class definitions in progress at this
24102 point; the bodies of members are only parsed outside of all class
24103 definitions. */
24104 gcc_assert (parser->num_classes_being_defined == 0);
24105 /* While we're parsing the member functions we might encounter more
24106 classes. We want to handle them right away, but we don't want
24107 them getting mixed up with functions that are currently in the
24108 queue. */
24109 push_unparsed_function_queues (parser);
24110
24111 /* Make sure that any template parameters are in scope. */
24112 maybe_begin_member_template_processing (member_function);
24113
24114 /* If the body of the function has not yet been parsed, parse it
24115 now. */
24116 if (DECL_PENDING_INLINE_P (member_function))
24117 {
24118 tree function_scope;
24119 cp_token_cache *tokens;
24120
24121 /* The function is no longer pending; we are processing it. */
24122 tokens = DECL_PENDING_INLINE_INFO (member_function);
24123 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24124 DECL_PENDING_INLINE_P (member_function) = 0;
24125
24126 /* If this is a local class, enter the scope of the containing
24127 function. */
24128 function_scope = current_function_decl;
24129 if (function_scope)
24130 push_function_context ();
24131
24132 /* Push the body of the function onto the lexer stack. */
24133 cp_parser_push_lexer_for_tokens (parser, tokens);
24134
24135 /* Let the front end know that we going to be defining this
24136 function. */
24137 start_preparsed_function (member_function, NULL_TREE,
24138 SF_PRE_PARSED | SF_INCLASS_INLINE);
24139
24140 /* Don't do access checking if it is a templated function. */
24141 if (processing_template_decl)
24142 push_deferring_access_checks (dk_no_check);
24143
24144 /* #pragma omp declare reduction needs special parsing. */
24145 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24146 {
24147 parser->lexer->in_pragma = true;
24148 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24149 finish_function (/*inline*/2);
24150 cp_check_omp_declare_reduction (member_function);
24151 }
24152 else
24153 /* Now, parse the body of the function. */
24154 cp_parser_function_definition_after_declarator (parser,
24155 /*inline_p=*/true);
24156
24157 if (processing_template_decl)
24158 pop_deferring_access_checks ();
24159
24160 /* Leave the scope of the containing function. */
24161 if (function_scope)
24162 pop_function_context ();
24163 cp_parser_pop_lexer (parser);
24164 }
24165
24166 /* Remove any template parameters from the symbol table. */
24167 maybe_end_member_template_processing ();
24168
24169 /* Restore the queue. */
24170 pop_unparsed_function_queues (parser);
24171 timevar_pop (TV_PARSE_INMETH);
24172 }
24173
24174 /* If DECL contains any default args, remember it on the unparsed
24175 functions queue. */
24176
24177 static void
24178 cp_parser_save_default_args (cp_parser* parser, tree decl)
24179 {
24180 tree probe;
24181
24182 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24183 probe;
24184 probe = TREE_CHAIN (probe))
24185 if (TREE_PURPOSE (probe))
24186 {
24187 cp_default_arg_entry entry = {current_class_type, decl};
24188 vec_safe_push (unparsed_funs_with_default_args, entry);
24189 break;
24190 }
24191 }
24192
24193 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24194 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24195 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24196 from the parameter-type-list. */
24197
24198 static tree
24199 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24200 tree default_arg, tree parmtype)
24201 {
24202 cp_token_cache *tokens;
24203 tree parsed_arg;
24204 bool dummy;
24205
24206 if (default_arg == error_mark_node)
24207 return error_mark_node;
24208
24209 /* Push the saved tokens for the default argument onto the parser's
24210 lexer stack. */
24211 tokens = DEFARG_TOKENS (default_arg);
24212 cp_parser_push_lexer_for_tokens (parser, tokens);
24213
24214 start_lambda_scope (decl);
24215
24216 /* Parse the default argument. */
24217 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24218 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24219 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24220
24221 finish_lambda_scope ();
24222
24223 if (parsed_arg == error_mark_node)
24224 cp_parser_skip_to_end_of_statement (parser);
24225
24226 if (!processing_template_decl)
24227 {
24228 /* In a non-template class, check conversions now. In a template,
24229 we'll wait and instantiate these as needed. */
24230 if (TREE_CODE (decl) == PARM_DECL)
24231 parsed_arg = check_default_argument (parmtype, parsed_arg,
24232 tf_warning_or_error);
24233 else
24234 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24235 }
24236
24237 /* If the token stream has not been completely used up, then
24238 there was extra junk after the end of the default
24239 argument. */
24240 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24241 {
24242 if (TREE_CODE (decl) == PARM_DECL)
24243 cp_parser_error (parser, "expected %<,%>");
24244 else
24245 cp_parser_error (parser, "expected %<;%>");
24246 }
24247
24248 /* Revert to the main lexer. */
24249 cp_parser_pop_lexer (parser);
24250
24251 return parsed_arg;
24252 }
24253
24254 /* FIELD is a non-static data member with an initializer which we saved for
24255 later; parse it now. */
24256
24257 static void
24258 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24259 {
24260 tree def;
24261
24262 maybe_begin_member_template_processing (field);
24263
24264 push_unparsed_function_queues (parser);
24265 def = cp_parser_late_parse_one_default_arg (parser, field,
24266 DECL_INITIAL (field),
24267 NULL_TREE);
24268 pop_unparsed_function_queues (parser);
24269
24270 maybe_end_member_template_processing ();
24271
24272 DECL_INITIAL (field) = def;
24273 }
24274
24275 /* FN is a FUNCTION_DECL which may contains a parameter with an
24276 unparsed DEFAULT_ARG. Parse the default args now. This function
24277 assumes that the current scope is the scope in which the default
24278 argument should be processed. */
24279
24280 static void
24281 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24282 {
24283 bool saved_local_variables_forbidden_p;
24284 tree parm, parmdecl;
24285
24286 /* While we're parsing the default args, we might (due to the
24287 statement expression extension) encounter more classes. We want
24288 to handle them right away, but we don't want them getting mixed
24289 up with default args that are currently in the queue. */
24290 push_unparsed_function_queues (parser);
24291
24292 /* Local variable names (and the `this' keyword) may not appear
24293 in a default argument. */
24294 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24295 parser->local_variables_forbidden_p = true;
24296
24297 push_defarg_context (fn);
24298
24299 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24300 parmdecl = DECL_ARGUMENTS (fn);
24301 parm && parm != void_list_node;
24302 parm = TREE_CHAIN (parm),
24303 parmdecl = DECL_CHAIN (parmdecl))
24304 {
24305 tree default_arg = TREE_PURPOSE (parm);
24306 tree parsed_arg;
24307 vec<tree, va_gc> *insts;
24308 tree copy;
24309 unsigned ix;
24310
24311 if (!default_arg)
24312 continue;
24313
24314 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24315 /* This can happen for a friend declaration for a function
24316 already declared with default arguments. */
24317 continue;
24318
24319 parsed_arg
24320 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24321 default_arg,
24322 TREE_VALUE (parm));
24323 if (parsed_arg == error_mark_node)
24324 {
24325 continue;
24326 }
24327
24328 TREE_PURPOSE (parm) = parsed_arg;
24329
24330 /* Update any instantiations we've already created. */
24331 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24332 vec_safe_iterate (insts, ix, &copy); ix++)
24333 TREE_PURPOSE (copy) = parsed_arg;
24334 }
24335
24336 pop_defarg_context ();
24337
24338 /* Make sure no default arg is missing. */
24339 check_default_args (fn);
24340
24341 /* Restore the state of local_variables_forbidden_p. */
24342 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24343
24344 /* Restore the queue. */
24345 pop_unparsed_function_queues (parser);
24346 }
24347
24348 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24349
24350 sizeof ... ( identifier )
24351
24352 where the 'sizeof' token has already been consumed. */
24353
24354 static tree
24355 cp_parser_sizeof_pack (cp_parser *parser)
24356 {
24357 /* Consume the `...'. */
24358 cp_lexer_consume_token (parser->lexer);
24359 maybe_warn_variadic_templates ();
24360
24361 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24362 if (paren)
24363 cp_lexer_consume_token (parser->lexer);
24364 else
24365 permerror (cp_lexer_peek_token (parser->lexer)->location,
24366 "%<sizeof...%> argument must be surrounded by parentheses");
24367
24368 cp_token *token = cp_lexer_peek_token (parser->lexer);
24369 tree name = cp_parser_identifier (parser);
24370 if (name == error_mark_node)
24371 return error_mark_node;
24372 /* The name is not qualified. */
24373 parser->scope = NULL_TREE;
24374 parser->qualifying_scope = NULL_TREE;
24375 parser->object_scope = NULL_TREE;
24376 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24377 if (expr == error_mark_node)
24378 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24379 token->location);
24380 if (TREE_CODE (expr) == TYPE_DECL)
24381 expr = TREE_TYPE (expr);
24382 else if (TREE_CODE (expr) == CONST_DECL)
24383 expr = DECL_INITIAL (expr);
24384 expr = make_pack_expansion (expr);
24385
24386 if (paren)
24387 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24388
24389 return expr;
24390 }
24391
24392 /* Parse the operand of `sizeof' (or a similar operator). Returns
24393 either a TYPE or an expression, depending on the form of the
24394 input. The KEYWORD indicates which kind of expression we have
24395 encountered. */
24396
24397 static tree
24398 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24399 {
24400 tree expr = NULL_TREE;
24401 const char *saved_message;
24402 char *tmp;
24403 bool saved_integral_constant_expression_p;
24404 bool saved_non_integral_constant_expression_p;
24405
24406 /* If it's a `...', then we are computing the length of a parameter
24407 pack. */
24408 if (keyword == RID_SIZEOF
24409 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24410 return cp_parser_sizeof_pack (parser);
24411
24412 /* Types cannot be defined in a `sizeof' expression. Save away the
24413 old message. */
24414 saved_message = parser->type_definition_forbidden_message;
24415 /* And create the new one. */
24416 tmp = concat ("types may not be defined in %<",
24417 IDENTIFIER_POINTER (ridpointers[keyword]),
24418 "%> expressions", NULL);
24419 parser->type_definition_forbidden_message = tmp;
24420
24421 /* The restrictions on constant-expressions do not apply inside
24422 sizeof expressions. */
24423 saved_integral_constant_expression_p
24424 = parser->integral_constant_expression_p;
24425 saved_non_integral_constant_expression_p
24426 = parser->non_integral_constant_expression_p;
24427 parser->integral_constant_expression_p = false;
24428
24429 /* Do not actually evaluate the expression. */
24430 ++cp_unevaluated_operand;
24431 ++c_inhibit_evaluation_warnings;
24432 /* If it's a `(', then we might be looking at the type-id
24433 construction. */
24434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24435 {
24436 tree type = NULL_TREE;
24437
24438 /* We can't be sure yet whether we're looking at a type-id or an
24439 expression. */
24440 cp_parser_parse_tentatively (parser);
24441 /* Note: as a GNU Extension, compound literals are considered
24442 postfix-expressions as they are in C99, so they are valid
24443 arguments to sizeof. See comment in cp_parser_cast_expression
24444 for details. */
24445 if (cp_parser_compound_literal_p (parser))
24446 cp_parser_simulate_error (parser);
24447 else
24448 {
24449 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24450 parser->in_type_id_in_expr_p = true;
24451 /* Look for the type-id. */
24452 type = cp_parser_type_id (parser);
24453 /* Look for the closing `)'. */
24454 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24455 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24456 }
24457
24458 /* If all went well, then we're done. */
24459 if (cp_parser_parse_definitely (parser))
24460 {
24461 cp_decl_specifier_seq decl_specs;
24462
24463 /* Build a trivial decl-specifier-seq. */
24464 clear_decl_specs (&decl_specs);
24465 decl_specs.type = type;
24466
24467 /* Call grokdeclarator to figure out what type this is. */
24468 expr = grokdeclarator (NULL,
24469 &decl_specs,
24470 TYPENAME,
24471 /*initialized=*/0,
24472 /*attrlist=*/NULL);
24473 }
24474 }
24475
24476 /* If the type-id production did not work out, then we must be
24477 looking at the unary-expression production. */
24478 if (!expr)
24479 expr = cp_parser_unary_expression (parser);
24480
24481 /* Go back to evaluating expressions. */
24482 --cp_unevaluated_operand;
24483 --c_inhibit_evaluation_warnings;
24484
24485 /* Free the message we created. */
24486 free (tmp);
24487 /* And restore the old one. */
24488 parser->type_definition_forbidden_message = saved_message;
24489 parser->integral_constant_expression_p
24490 = saved_integral_constant_expression_p;
24491 parser->non_integral_constant_expression_p
24492 = saved_non_integral_constant_expression_p;
24493
24494 return expr;
24495 }
24496
24497 /* If the current declaration has no declarator, return true. */
24498
24499 static bool
24500 cp_parser_declares_only_class_p (cp_parser *parser)
24501 {
24502 /* If the next token is a `;' or a `,' then there is no
24503 declarator. */
24504 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24505 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24506 }
24507
24508 /* Update the DECL_SPECS to reflect the storage class indicated by
24509 KEYWORD. */
24510
24511 static void
24512 cp_parser_set_storage_class (cp_parser *parser,
24513 cp_decl_specifier_seq *decl_specs,
24514 enum rid keyword,
24515 cp_token *token)
24516 {
24517 cp_storage_class storage_class;
24518
24519 if (parser->in_unbraced_linkage_specification_p)
24520 {
24521 error_at (token->location, "invalid use of %qD in linkage specification",
24522 ridpointers[keyword]);
24523 return;
24524 }
24525 else if (decl_specs->storage_class != sc_none)
24526 {
24527 decl_specs->conflicting_specifiers_p = true;
24528 return;
24529 }
24530
24531 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24532 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24533 && decl_specs->gnu_thread_keyword_p)
24534 {
24535 pedwarn (decl_specs->locations[ds_thread], 0,
24536 "%<__thread%> before %qD", ridpointers[keyword]);
24537 }
24538
24539 switch (keyword)
24540 {
24541 case RID_AUTO:
24542 storage_class = sc_auto;
24543 break;
24544 case RID_REGISTER:
24545 storage_class = sc_register;
24546 break;
24547 case RID_STATIC:
24548 storage_class = sc_static;
24549 break;
24550 case RID_EXTERN:
24551 storage_class = sc_extern;
24552 break;
24553 case RID_MUTABLE:
24554 storage_class = sc_mutable;
24555 break;
24556 default:
24557 gcc_unreachable ();
24558 }
24559 decl_specs->storage_class = storage_class;
24560 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24561
24562 /* A storage class specifier cannot be applied alongside a typedef
24563 specifier. If there is a typedef specifier present then set
24564 conflicting_specifiers_p which will trigger an error later
24565 on in grokdeclarator. */
24566 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24567 decl_specs->conflicting_specifiers_p = true;
24568 }
24569
24570 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24571 is true, the type is a class or enum definition. */
24572
24573 static void
24574 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24575 tree type_spec,
24576 cp_token *token,
24577 bool type_definition_p)
24578 {
24579 decl_specs->any_specifiers_p = true;
24580
24581 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24582 (with, for example, in "typedef int wchar_t;") we remember that
24583 this is what happened. In system headers, we ignore these
24584 declarations so that G++ can work with system headers that are not
24585 C++-safe. */
24586 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24587 && !type_definition_p
24588 && (type_spec == boolean_type_node
24589 || type_spec == char16_type_node
24590 || type_spec == char32_type_node
24591 || type_spec == wchar_type_node)
24592 && (decl_specs->type
24593 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24594 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24595 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24596 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24597 {
24598 decl_specs->redefined_builtin_type = type_spec;
24599 set_and_check_decl_spec_loc (decl_specs,
24600 ds_redefined_builtin_type_spec,
24601 token);
24602 if (!decl_specs->type)
24603 {
24604 decl_specs->type = type_spec;
24605 decl_specs->type_definition_p = false;
24606 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24607 }
24608 }
24609 else if (decl_specs->type)
24610 decl_specs->multiple_types_p = true;
24611 else
24612 {
24613 decl_specs->type = type_spec;
24614 decl_specs->type_definition_p = type_definition_p;
24615 decl_specs->redefined_builtin_type = NULL_TREE;
24616 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24617 }
24618 }
24619
24620 /* True iff TOKEN is the GNU keyword __thread. */
24621
24622 static bool
24623 token_is__thread (cp_token *token)
24624 {
24625 gcc_assert (token->keyword == RID_THREAD);
24626 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24627 }
24628
24629 /* Set the location for a declarator specifier and check if it is
24630 duplicated.
24631
24632 DECL_SPECS is the sequence of declarator specifiers onto which to
24633 set the location.
24634
24635 DS is the single declarator specifier to set which location is to
24636 be set onto the existing sequence of declarators.
24637
24638 LOCATION is the location for the declarator specifier to
24639 consider. */
24640
24641 static void
24642 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24643 cp_decl_spec ds, cp_token *token)
24644 {
24645 gcc_assert (ds < ds_last);
24646
24647 if (decl_specs == NULL)
24648 return;
24649
24650 source_location location = token->location;
24651
24652 if (decl_specs->locations[ds] == 0)
24653 {
24654 decl_specs->locations[ds] = location;
24655 if (ds == ds_thread)
24656 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24657 }
24658 else
24659 {
24660 if (ds == ds_long)
24661 {
24662 if (decl_specs->locations[ds_long_long] != 0)
24663 error_at (location,
24664 "%<long long long%> is too long for GCC");
24665 else
24666 {
24667 decl_specs->locations[ds_long_long] = location;
24668 pedwarn_cxx98 (location,
24669 OPT_Wlong_long,
24670 "ISO C++ 1998 does not support %<long long%>");
24671 }
24672 }
24673 else if (ds == ds_thread)
24674 {
24675 bool gnu = token_is__thread (token);
24676 if (gnu != decl_specs->gnu_thread_keyword_p)
24677 error_at (location,
24678 "both %<__thread%> and %<thread_local%> specified");
24679 else
24680 error_at (location, "duplicate %qD", token->u.value);
24681 }
24682 else
24683 {
24684 static const char *const decl_spec_names[] = {
24685 "signed",
24686 "unsigned",
24687 "short",
24688 "long",
24689 "const",
24690 "volatile",
24691 "restrict",
24692 "inline",
24693 "virtual",
24694 "explicit",
24695 "friend",
24696 "typedef",
24697 "using",
24698 "constexpr",
24699 "__complex"
24700 };
24701 error_at (location,
24702 "duplicate %qs", decl_spec_names[ds]);
24703 }
24704 }
24705 }
24706
24707 /* Return true iff the declarator specifier DS is present in the
24708 sequence of declarator specifiers DECL_SPECS. */
24709
24710 bool
24711 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24712 cp_decl_spec ds)
24713 {
24714 gcc_assert (ds < ds_last);
24715
24716 if (decl_specs == NULL)
24717 return false;
24718
24719 return decl_specs->locations[ds] != 0;
24720 }
24721
24722 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24723 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24724
24725 static bool
24726 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24727 {
24728 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24729 }
24730
24731 /* Issue an error message indicating that TOKEN_DESC was expected.
24732 If KEYWORD is true, it indicated this function is called by
24733 cp_parser_require_keword and the required token can only be
24734 a indicated keyword. */
24735
24736 static void
24737 cp_parser_required_error (cp_parser *parser,
24738 required_token token_desc,
24739 bool keyword)
24740 {
24741 switch (token_desc)
24742 {
24743 case RT_NEW:
24744 cp_parser_error (parser, "expected %<new%>");
24745 return;
24746 case RT_DELETE:
24747 cp_parser_error (parser, "expected %<delete%>");
24748 return;
24749 case RT_RETURN:
24750 cp_parser_error (parser, "expected %<return%>");
24751 return;
24752 case RT_WHILE:
24753 cp_parser_error (parser, "expected %<while%>");
24754 return;
24755 case RT_EXTERN:
24756 cp_parser_error (parser, "expected %<extern%>");
24757 return;
24758 case RT_STATIC_ASSERT:
24759 cp_parser_error (parser, "expected %<static_assert%>");
24760 return;
24761 case RT_DECLTYPE:
24762 cp_parser_error (parser, "expected %<decltype%>");
24763 return;
24764 case RT_OPERATOR:
24765 cp_parser_error (parser, "expected %<operator%>");
24766 return;
24767 case RT_CLASS:
24768 cp_parser_error (parser, "expected %<class%>");
24769 return;
24770 case RT_TEMPLATE:
24771 cp_parser_error (parser, "expected %<template%>");
24772 return;
24773 case RT_NAMESPACE:
24774 cp_parser_error (parser, "expected %<namespace%>");
24775 return;
24776 case RT_USING:
24777 cp_parser_error (parser, "expected %<using%>");
24778 return;
24779 case RT_ASM:
24780 cp_parser_error (parser, "expected %<asm%>");
24781 return;
24782 case RT_TRY:
24783 cp_parser_error (parser, "expected %<try%>");
24784 return;
24785 case RT_CATCH:
24786 cp_parser_error (parser, "expected %<catch%>");
24787 return;
24788 case RT_THROW:
24789 cp_parser_error (parser, "expected %<throw%>");
24790 return;
24791 case RT_LABEL:
24792 cp_parser_error (parser, "expected %<__label__%>");
24793 return;
24794 case RT_AT_TRY:
24795 cp_parser_error (parser, "expected %<@try%>");
24796 return;
24797 case RT_AT_SYNCHRONIZED:
24798 cp_parser_error (parser, "expected %<@synchronized%>");
24799 return;
24800 case RT_AT_THROW:
24801 cp_parser_error (parser, "expected %<@throw%>");
24802 return;
24803 case RT_TRANSACTION_ATOMIC:
24804 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24805 return;
24806 case RT_TRANSACTION_RELAXED:
24807 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24808 return;
24809 default:
24810 break;
24811 }
24812 if (!keyword)
24813 {
24814 switch (token_desc)
24815 {
24816 case RT_SEMICOLON:
24817 cp_parser_error (parser, "expected %<;%>");
24818 return;
24819 case RT_OPEN_PAREN:
24820 cp_parser_error (parser, "expected %<(%>");
24821 return;
24822 case RT_CLOSE_BRACE:
24823 cp_parser_error (parser, "expected %<}%>");
24824 return;
24825 case RT_OPEN_BRACE:
24826 cp_parser_error (parser, "expected %<{%>");
24827 return;
24828 case RT_CLOSE_SQUARE:
24829 cp_parser_error (parser, "expected %<]%>");
24830 return;
24831 case RT_OPEN_SQUARE:
24832 cp_parser_error (parser, "expected %<[%>");
24833 return;
24834 case RT_COMMA:
24835 cp_parser_error (parser, "expected %<,%>");
24836 return;
24837 case RT_SCOPE:
24838 cp_parser_error (parser, "expected %<::%>");
24839 return;
24840 case RT_LESS:
24841 cp_parser_error (parser, "expected %<<%>");
24842 return;
24843 case RT_GREATER:
24844 cp_parser_error (parser, "expected %<>%>");
24845 return;
24846 case RT_EQ:
24847 cp_parser_error (parser, "expected %<=%>");
24848 return;
24849 case RT_ELLIPSIS:
24850 cp_parser_error (parser, "expected %<...%>");
24851 return;
24852 case RT_MULT:
24853 cp_parser_error (parser, "expected %<*%>");
24854 return;
24855 case RT_COMPL:
24856 cp_parser_error (parser, "expected %<~%>");
24857 return;
24858 case RT_COLON:
24859 cp_parser_error (parser, "expected %<:%>");
24860 return;
24861 case RT_COLON_SCOPE:
24862 cp_parser_error (parser, "expected %<:%> or %<::%>");
24863 return;
24864 case RT_CLOSE_PAREN:
24865 cp_parser_error (parser, "expected %<)%>");
24866 return;
24867 case RT_COMMA_CLOSE_PAREN:
24868 cp_parser_error (parser, "expected %<,%> or %<)%>");
24869 return;
24870 case RT_PRAGMA_EOL:
24871 cp_parser_error (parser, "expected end of line");
24872 return;
24873 case RT_NAME:
24874 cp_parser_error (parser, "expected identifier");
24875 return;
24876 case RT_SELECT:
24877 cp_parser_error (parser, "expected selection-statement");
24878 return;
24879 case RT_INTERATION:
24880 cp_parser_error (parser, "expected iteration-statement");
24881 return;
24882 case RT_JUMP:
24883 cp_parser_error (parser, "expected jump-statement");
24884 return;
24885 case RT_CLASS_KEY:
24886 cp_parser_error (parser, "expected class-key");
24887 return;
24888 case RT_CLASS_TYPENAME_TEMPLATE:
24889 cp_parser_error (parser,
24890 "expected %<class%>, %<typename%>, or %<template%>");
24891 return;
24892 default:
24893 gcc_unreachable ();
24894 }
24895 }
24896 else
24897 gcc_unreachable ();
24898 }
24899
24900
24901
24902 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24903 issue an error message indicating that TOKEN_DESC was expected.
24904
24905 Returns the token consumed, if the token had the appropriate type.
24906 Otherwise, returns NULL. */
24907
24908 static cp_token *
24909 cp_parser_require (cp_parser* parser,
24910 enum cpp_ttype type,
24911 required_token token_desc)
24912 {
24913 if (cp_lexer_next_token_is (parser->lexer, type))
24914 return cp_lexer_consume_token (parser->lexer);
24915 else
24916 {
24917 /* Output the MESSAGE -- unless we're parsing tentatively. */
24918 if (!cp_parser_simulate_error (parser))
24919 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24920 return NULL;
24921 }
24922 }
24923
24924 /* An error message is produced if the next token is not '>'.
24925 All further tokens are skipped until the desired token is
24926 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24927
24928 static void
24929 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24930 {
24931 /* Current level of '< ... >'. */
24932 unsigned level = 0;
24933 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24934 unsigned nesting_depth = 0;
24935
24936 /* Are we ready, yet? If not, issue error message. */
24937 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24938 return;
24939
24940 /* Skip tokens until the desired token is found. */
24941 while (true)
24942 {
24943 /* Peek at the next token. */
24944 switch (cp_lexer_peek_token (parser->lexer)->type)
24945 {
24946 case CPP_LESS:
24947 if (!nesting_depth)
24948 ++level;
24949 break;
24950
24951 case CPP_RSHIFT:
24952 if (cxx_dialect == cxx98)
24953 /* C++0x views the `>>' operator as two `>' tokens, but
24954 C++98 does not. */
24955 break;
24956 else if (!nesting_depth && level-- == 0)
24957 {
24958 /* We've hit a `>>' where the first `>' closes the
24959 template argument list, and the second `>' is
24960 spurious. Just consume the `>>' and stop; we've
24961 already produced at least one error. */
24962 cp_lexer_consume_token (parser->lexer);
24963 return;
24964 }
24965 /* Fall through for C++0x, so we handle the second `>' in
24966 the `>>'. */
24967
24968 case CPP_GREATER:
24969 if (!nesting_depth && level-- == 0)
24970 {
24971 /* We've reached the token we want, consume it and stop. */
24972 cp_lexer_consume_token (parser->lexer);
24973 return;
24974 }
24975 break;
24976
24977 case CPP_OPEN_PAREN:
24978 case CPP_OPEN_SQUARE:
24979 ++nesting_depth;
24980 break;
24981
24982 case CPP_CLOSE_PAREN:
24983 case CPP_CLOSE_SQUARE:
24984 if (nesting_depth-- == 0)
24985 return;
24986 break;
24987
24988 case CPP_EOF:
24989 case CPP_PRAGMA_EOL:
24990 case CPP_SEMICOLON:
24991 case CPP_OPEN_BRACE:
24992 case CPP_CLOSE_BRACE:
24993 /* The '>' was probably forgotten, don't look further. */
24994 return;
24995
24996 default:
24997 break;
24998 }
24999
25000 /* Consume this token. */
25001 cp_lexer_consume_token (parser->lexer);
25002 }
25003 }
25004
25005 /* If the next token is the indicated keyword, consume it. Otherwise,
25006 issue an error message indicating that TOKEN_DESC was expected.
25007
25008 Returns the token consumed, if the token had the appropriate type.
25009 Otherwise, returns NULL. */
25010
25011 static cp_token *
25012 cp_parser_require_keyword (cp_parser* parser,
25013 enum rid keyword,
25014 required_token token_desc)
25015 {
25016 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25017
25018 if (token && token->keyword != keyword)
25019 {
25020 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25021 return NULL;
25022 }
25023
25024 return token;
25025 }
25026
25027 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25028 function-definition. */
25029
25030 static bool
25031 cp_parser_token_starts_function_definition_p (cp_token* token)
25032 {
25033 return (/* An ordinary function-body begins with an `{'. */
25034 token->type == CPP_OPEN_BRACE
25035 /* A ctor-initializer begins with a `:'. */
25036 || token->type == CPP_COLON
25037 /* A function-try-block begins with `try'. */
25038 || token->keyword == RID_TRY
25039 /* A function-transaction-block begins with `__transaction_atomic'
25040 or `__transaction_relaxed'. */
25041 || token->keyword == RID_TRANSACTION_ATOMIC
25042 || token->keyword == RID_TRANSACTION_RELAXED
25043 /* The named return value extension begins with `return'. */
25044 || token->keyword == RID_RETURN);
25045 }
25046
25047 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25048 definition. */
25049
25050 static bool
25051 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25052 {
25053 cp_token *token;
25054
25055 token = cp_lexer_peek_token (parser->lexer);
25056 return (token->type == CPP_OPEN_BRACE
25057 || (token->type == CPP_COLON
25058 && !parser->colon_doesnt_start_class_def_p));
25059 }
25060
25061 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25062 C++0x) ending a template-argument. */
25063
25064 static bool
25065 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25066 {
25067 cp_token *token;
25068
25069 token = cp_lexer_peek_token (parser->lexer);
25070 return (token->type == CPP_COMMA
25071 || token->type == CPP_GREATER
25072 || token->type == CPP_ELLIPSIS
25073 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25074 }
25075
25076 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25077 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25078
25079 static bool
25080 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25081 size_t n)
25082 {
25083 cp_token *token;
25084
25085 token = cp_lexer_peek_nth_token (parser->lexer, n);
25086 if (token->type == CPP_LESS)
25087 return true;
25088 /* Check for the sequence `<::' in the original code. It would be lexed as
25089 `[:', where `[' is a digraph, and there is no whitespace before
25090 `:'. */
25091 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25092 {
25093 cp_token *token2;
25094 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25095 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25096 return true;
25097 }
25098 return false;
25099 }
25100
25101 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25102 or none_type otherwise. */
25103
25104 static enum tag_types
25105 cp_parser_token_is_class_key (cp_token* token)
25106 {
25107 switch (token->keyword)
25108 {
25109 case RID_CLASS:
25110 return class_type;
25111 case RID_STRUCT:
25112 return record_type;
25113 case RID_UNION:
25114 return union_type;
25115
25116 default:
25117 return none_type;
25118 }
25119 }
25120
25121 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25122 or none_type otherwise or if the token is null. */
25123
25124 static enum tag_types
25125 cp_parser_token_is_type_parameter_key (cp_token* token)
25126 {
25127 if (!token)
25128 return none_type;
25129
25130 switch (token->keyword)
25131 {
25132 case RID_CLASS:
25133 return class_type;
25134 case RID_TYPENAME:
25135 return typename_type;
25136
25137 default:
25138 return none_type;
25139 }
25140 }
25141
25142 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25143
25144 static void
25145 cp_parser_check_class_key (enum tag_types class_key, tree type)
25146 {
25147 if (type == error_mark_node)
25148 return;
25149 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25150 {
25151 if (permerror (input_location, "%qs tag used in naming %q#T",
25152 class_key == union_type ? "union"
25153 : class_key == record_type ? "struct" : "class",
25154 type))
25155 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25156 "%q#T was previously declared here", type);
25157 }
25158 }
25159
25160 /* Issue an error message if DECL is redeclared with different
25161 access than its original declaration [class.access.spec/3].
25162 This applies to nested classes and nested class templates.
25163 [class.mem/1]. */
25164
25165 static void
25166 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25167 {
25168 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25169 return;
25170
25171 if ((TREE_PRIVATE (decl)
25172 != (current_access_specifier == access_private_node))
25173 || (TREE_PROTECTED (decl)
25174 != (current_access_specifier == access_protected_node)))
25175 error_at (location, "%qD redeclared with different access", decl);
25176 }
25177
25178 /* Look for the `template' keyword, as a syntactic disambiguator.
25179 Return TRUE iff it is present, in which case it will be
25180 consumed. */
25181
25182 static bool
25183 cp_parser_optional_template_keyword (cp_parser *parser)
25184 {
25185 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25186 {
25187 /* In C++98 the `template' keyword can only be used within templates;
25188 outside templates the parser can always figure out what is a
25189 template and what is not. In C++11, per the resolution of DR 468,
25190 `template' is allowed in cases where it is not strictly necessary. */
25191 if (!processing_template_decl
25192 && pedantic && cxx_dialect == cxx98)
25193 {
25194 cp_token *token = cp_lexer_peek_token (parser->lexer);
25195 pedwarn (token->location, OPT_Wpedantic,
25196 "in C++98 %<template%> (as a disambiguator) is only "
25197 "allowed within templates");
25198 /* If this part of the token stream is rescanned, the same
25199 error message would be generated. So, we purge the token
25200 from the stream. */
25201 cp_lexer_purge_token (parser->lexer);
25202 return false;
25203 }
25204 else
25205 {
25206 /* Consume the `template' keyword. */
25207 cp_lexer_consume_token (parser->lexer);
25208 return true;
25209 }
25210 }
25211 return false;
25212 }
25213
25214 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25215 set PARSER->SCOPE, and perform other related actions. */
25216
25217 static void
25218 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25219 {
25220 int i;
25221 struct tree_check *check_value;
25222 deferred_access_check *chk;
25223 vec<deferred_access_check, va_gc> *checks;
25224
25225 /* Get the stored value. */
25226 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25227 /* Perform any access checks that were deferred. */
25228 checks = check_value->checks;
25229 if (checks)
25230 {
25231 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25232 perform_or_defer_access_check (chk->binfo,
25233 chk->decl,
25234 chk->diag_decl, tf_warning_or_error);
25235 }
25236 /* Set the scope from the stored value. */
25237 parser->scope = check_value->value;
25238 parser->qualifying_scope = check_value->qualifying_scope;
25239 parser->object_scope = NULL_TREE;
25240 }
25241
25242 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25243 encounter the end of a block before what we were looking for. */
25244
25245 static bool
25246 cp_parser_cache_group (cp_parser *parser,
25247 enum cpp_ttype end,
25248 unsigned depth)
25249 {
25250 while (true)
25251 {
25252 cp_token *token = cp_lexer_peek_token (parser->lexer);
25253
25254 /* Abort a parenthesized expression if we encounter a semicolon. */
25255 if ((end == CPP_CLOSE_PAREN || depth == 0)
25256 && token->type == CPP_SEMICOLON)
25257 return true;
25258 /* If we've reached the end of the file, stop. */
25259 if (token->type == CPP_EOF
25260 || (end != CPP_PRAGMA_EOL
25261 && token->type == CPP_PRAGMA_EOL))
25262 return true;
25263 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25264 /* We've hit the end of an enclosing block, so there's been some
25265 kind of syntax error. */
25266 return true;
25267
25268 /* Consume the token. */
25269 cp_lexer_consume_token (parser->lexer);
25270 /* See if it starts a new group. */
25271 if (token->type == CPP_OPEN_BRACE)
25272 {
25273 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25274 /* In theory this should probably check end == '}', but
25275 cp_parser_save_member_function_body needs it to exit
25276 after either '}' or ')' when called with ')'. */
25277 if (depth == 0)
25278 return false;
25279 }
25280 else if (token->type == CPP_OPEN_PAREN)
25281 {
25282 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25283 if (depth == 0 && end == CPP_CLOSE_PAREN)
25284 return false;
25285 }
25286 else if (token->type == CPP_PRAGMA)
25287 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25288 else if (token->type == end)
25289 return false;
25290 }
25291 }
25292
25293 /* Like above, for caching a default argument or NSDMI. Both of these are
25294 terminated by a non-nested comma, but it can be unclear whether or not a
25295 comma is nested in a template argument list unless we do more parsing.
25296 In order to handle this ambiguity, when we encounter a ',' after a '<'
25297 we try to parse what follows as a parameter-declaration-list (in the
25298 case of a default argument) or a member-declarator (in the case of an
25299 NSDMI). If that succeeds, then we stop caching. */
25300
25301 static tree
25302 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25303 {
25304 unsigned depth = 0;
25305 int maybe_template_id = 0;
25306 cp_token *first_token;
25307 cp_token *token;
25308 tree default_argument;
25309
25310 /* Add tokens until we have processed the entire default
25311 argument. We add the range [first_token, token). */
25312 first_token = cp_lexer_peek_token (parser->lexer);
25313 if (first_token->type == CPP_OPEN_BRACE)
25314 {
25315 /* For list-initialization, this is straightforward. */
25316 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25317 token = cp_lexer_peek_token (parser->lexer);
25318 }
25319 else while (true)
25320 {
25321 bool done = false;
25322
25323 /* Peek at the next token. */
25324 token = cp_lexer_peek_token (parser->lexer);
25325 /* What we do depends on what token we have. */
25326 switch (token->type)
25327 {
25328 /* In valid code, a default argument must be
25329 immediately followed by a `,' `)', or `...'. */
25330 case CPP_COMMA:
25331 if (depth == 0 && maybe_template_id)
25332 {
25333 /* If we've seen a '<', we might be in a
25334 template-argument-list. Until Core issue 325 is
25335 resolved, we don't know how this situation ought
25336 to be handled, so try to DTRT. We check whether
25337 what comes after the comma is a valid parameter
25338 declaration list. If it is, then the comma ends
25339 the default argument; otherwise the default
25340 argument continues. */
25341 bool error = false;
25342
25343 /* Set ITALP so cp_parser_parameter_declaration_list
25344 doesn't decide to commit to this parse. */
25345 bool saved_italp = parser->in_template_argument_list_p;
25346 parser->in_template_argument_list_p = true;
25347
25348 cp_parser_parse_tentatively (parser);
25349 cp_lexer_consume_token (parser->lexer);
25350
25351 if (nsdmi)
25352 {
25353 int ctor_dtor_or_conv_p;
25354 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25355 &ctor_dtor_or_conv_p,
25356 /*parenthesized_p=*/NULL,
25357 /*member_p=*/true,
25358 /*friend_p=*/false);
25359 }
25360 else
25361 {
25362 begin_scope (sk_function_parms, NULL_TREE);
25363 cp_parser_parameter_declaration_list (parser, &error);
25364 pop_bindings_and_leave_scope ();
25365 }
25366 if (!cp_parser_error_occurred (parser) && !error)
25367 done = true;
25368 cp_parser_abort_tentative_parse (parser);
25369
25370 parser->in_template_argument_list_p = saved_italp;
25371 break;
25372 }
25373 case CPP_CLOSE_PAREN:
25374 case CPP_ELLIPSIS:
25375 /* If we run into a non-nested `;', `}', or `]',
25376 then the code is invalid -- but the default
25377 argument is certainly over. */
25378 case CPP_SEMICOLON:
25379 case CPP_CLOSE_BRACE:
25380 case CPP_CLOSE_SQUARE:
25381 if (depth == 0
25382 /* Handle correctly int n = sizeof ... ( p ); */
25383 && token->type != CPP_ELLIPSIS)
25384 done = true;
25385 /* Update DEPTH, if necessary. */
25386 else if (token->type == CPP_CLOSE_PAREN
25387 || token->type == CPP_CLOSE_BRACE
25388 || token->type == CPP_CLOSE_SQUARE)
25389 --depth;
25390 break;
25391
25392 case CPP_OPEN_PAREN:
25393 case CPP_OPEN_SQUARE:
25394 case CPP_OPEN_BRACE:
25395 ++depth;
25396 break;
25397
25398 case CPP_LESS:
25399 if (depth == 0)
25400 /* This might be the comparison operator, or it might
25401 start a template argument list. */
25402 ++maybe_template_id;
25403 break;
25404
25405 case CPP_RSHIFT:
25406 if (cxx_dialect == cxx98)
25407 break;
25408 /* Fall through for C++0x, which treats the `>>'
25409 operator like two `>' tokens in certain
25410 cases. */
25411
25412 case CPP_GREATER:
25413 if (depth == 0)
25414 {
25415 /* This might be an operator, or it might close a
25416 template argument list. But if a previous '<'
25417 started a template argument list, this will have
25418 closed it, so we can't be in one anymore. */
25419 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25420 if (maybe_template_id < 0)
25421 maybe_template_id = 0;
25422 }
25423 break;
25424
25425 /* If we run out of tokens, issue an error message. */
25426 case CPP_EOF:
25427 case CPP_PRAGMA_EOL:
25428 error_at (token->location, "file ends in default argument");
25429 done = true;
25430 break;
25431
25432 case CPP_NAME:
25433 case CPP_SCOPE:
25434 /* In these cases, we should look for template-ids.
25435 For example, if the default argument is
25436 `X<int, double>()', we need to do name lookup to
25437 figure out whether or not `X' is a template; if
25438 so, the `,' does not end the default argument.
25439
25440 That is not yet done. */
25441 break;
25442
25443 default:
25444 break;
25445 }
25446
25447 /* If we've reached the end, stop. */
25448 if (done)
25449 break;
25450
25451 /* Add the token to the token block. */
25452 token = cp_lexer_consume_token (parser->lexer);
25453 }
25454
25455 /* Create a DEFAULT_ARG to represent the unparsed default
25456 argument. */
25457 default_argument = make_node (DEFAULT_ARG);
25458 DEFARG_TOKENS (default_argument)
25459 = cp_token_cache_new (first_token, token);
25460 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25461
25462 return default_argument;
25463 }
25464
25465 /* Begin parsing tentatively. We always save tokens while parsing
25466 tentatively so that if the tentative parsing fails we can restore the
25467 tokens. */
25468
25469 static void
25470 cp_parser_parse_tentatively (cp_parser* parser)
25471 {
25472 /* Enter a new parsing context. */
25473 parser->context = cp_parser_context_new (parser->context);
25474 /* Begin saving tokens. */
25475 cp_lexer_save_tokens (parser->lexer);
25476 /* In order to avoid repetitive access control error messages,
25477 access checks are queued up until we are no longer parsing
25478 tentatively. */
25479 push_deferring_access_checks (dk_deferred);
25480 }
25481
25482 /* Commit to the currently active tentative parse. */
25483
25484 static void
25485 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25486 {
25487 cp_parser_context *context;
25488 cp_lexer *lexer;
25489
25490 /* Mark all of the levels as committed. */
25491 lexer = parser->lexer;
25492 for (context = parser->context; context->next; context = context->next)
25493 {
25494 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25495 break;
25496 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25497 while (!cp_lexer_saving_tokens (lexer))
25498 lexer = lexer->next;
25499 cp_lexer_commit_tokens (lexer);
25500 }
25501 }
25502
25503 /* Commit to the topmost currently active tentative parse.
25504
25505 Note that this function shouldn't be called when there are
25506 irreversible side-effects while in a tentative state. For
25507 example, we shouldn't create a permanent entry in the symbol
25508 table, or issue an error message that might not apply if the
25509 tentative parse is aborted. */
25510
25511 static void
25512 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25513 {
25514 cp_parser_context *context = parser->context;
25515 cp_lexer *lexer = parser->lexer;
25516
25517 if (context)
25518 {
25519 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25520 return;
25521 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25522
25523 while (!cp_lexer_saving_tokens (lexer))
25524 lexer = lexer->next;
25525 cp_lexer_commit_tokens (lexer);
25526 }
25527 }
25528
25529 /* Abort the currently active tentative parse. All consumed tokens
25530 will be rolled back, and no diagnostics will be issued. */
25531
25532 static void
25533 cp_parser_abort_tentative_parse (cp_parser* parser)
25534 {
25535 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25536 || errorcount > 0);
25537 cp_parser_simulate_error (parser);
25538 /* Now, pretend that we want to see if the construct was
25539 successfully parsed. */
25540 cp_parser_parse_definitely (parser);
25541 }
25542
25543 /* Stop parsing tentatively. If a parse error has occurred, restore the
25544 token stream. Otherwise, commit to the tokens we have consumed.
25545 Returns true if no error occurred; false otherwise. */
25546
25547 static bool
25548 cp_parser_parse_definitely (cp_parser* parser)
25549 {
25550 bool error_occurred;
25551 cp_parser_context *context;
25552
25553 /* Remember whether or not an error occurred, since we are about to
25554 destroy that information. */
25555 error_occurred = cp_parser_error_occurred (parser);
25556 /* Remove the topmost context from the stack. */
25557 context = parser->context;
25558 parser->context = context->next;
25559 /* If no parse errors occurred, commit to the tentative parse. */
25560 if (!error_occurred)
25561 {
25562 /* Commit to the tokens read tentatively, unless that was
25563 already done. */
25564 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25565 cp_lexer_commit_tokens (parser->lexer);
25566
25567 pop_to_parent_deferring_access_checks ();
25568 }
25569 /* Otherwise, if errors occurred, roll back our state so that things
25570 are just as they were before we began the tentative parse. */
25571 else
25572 {
25573 cp_lexer_rollback_tokens (parser->lexer);
25574 pop_deferring_access_checks ();
25575 }
25576 /* Add the context to the front of the free list. */
25577 context->next = cp_parser_context_free_list;
25578 cp_parser_context_free_list = context;
25579
25580 return !error_occurred;
25581 }
25582
25583 /* Returns true if we are parsing tentatively and are not committed to
25584 this tentative parse. */
25585
25586 static bool
25587 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25588 {
25589 return (cp_parser_parsing_tentatively (parser)
25590 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25591 }
25592
25593 /* Returns nonzero iff an error has occurred during the most recent
25594 tentative parse. */
25595
25596 static bool
25597 cp_parser_error_occurred (cp_parser* parser)
25598 {
25599 return (cp_parser_parsing_tentatively (parser)
25600 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25601 }
25602
25603 /* Returns nonzero if GNU extensions are allowed. */
25604
25605 static bool
25606 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25607 {
25608 return parser->allow_gnu_extensions_p;
25609 }
25610 \f
25611 /* Objective-C++ Productions */
25612
25613
25614 /* Parse an Objective-C expression, which feeds into a primary-expression
25615 above.
25616
25617 objc-expression:
25618 objc-message-expression
25619 objc-string-literal
25620 objc-encode-expression
25621 objc-protocol-expression
25622 objc-selector-expression
25623
25624 Returns a tree representation of the expression. */
25625
25626 static tree
25627 cp_parser_objc_expression (cp_parser* parser)
25628 {
25629 /* Try to figure out what kind of declaration is present. */
25630 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25631
25632 switch (kwd->type)
25633 {
25634 case CPP_OPEN_SQUARE:
25635 return cp_parser_objc_message_expression (parser);
25636
25637 case CPP_OBJC_STRING:
25638 kwd = cp_lexer_consume_token (parser->lexer);
25639 return objc_build_string_object (kwd->u.value);
25640
25641 case CPP_KEYWORD:
25642 switch (kwd->keyword)
25643 {
25644 case RID_AT_ENCODE:
25645 return cp_parser_objc_encode_expression (parser);
25646
25647 case RID_AT_PROTOCOL:
25648 return cp_parser_objc_protocol_expression (parser);
25649
25650 case RID_AT_SELECTOR:
25651 return cp_parser_objc_selector_expression (parser);
25652
25653 default:
25654 break;
25655 }
25656 default:
25657 error_at (kwd->location,
25658 "misplaced %<@%D%> Objective-C++ construct",
25659 kwd->u.value);
25660 cp_parser_skip_to_end_of_block_or_statement (parser);
25661 }
25662
25663 return error_mark_node;
25664 }
25665
25666 /* Parse an Objective-C message expression.
25667
25668 objc-message-expression:
25669 [ objc-message-receiver objc-message-args ]
25670
25671 Returns a representation of an Objective-C message. */
25672
25673 static tree
25674 cp_parser_objc_message_expression (cp_parser* parser)
25675 {
25676 tree receiver, messageargs;
25677
25678 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25679 receiver = cp_parser_objc_message_receiver (parser);
25680 messageargs = cp_parser_objc_message_args (parser);
25681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25682
25683 return objc_build_message_expr (receiver, messageargs);
25684 }
25685
25686 /* Parse an objc-message-receiver.
25687
25688 objc-message-receiver:
25689 expression
25690 simple-type-specifier
25691
25692 Returns a representation of the type or expression. */
25693
25694 static tree
25695 cp_parser_objc_message_receiver (cp_parser* parser)
25696 {
25697 tree rcv;
25698
25699 /* An Objective-C message receiver may be either (1) a type
25700 or (2) an expression. */
25701 cp_parser_parse_tentatively (parser);
25702 rcv = cp_parser_expression (parser);
25703
25704 /* If that worked out, fine. */
25705 if (cp_parser_parse_definitely (parser))
25706 return rcv;
25707
25708 cp_parser_parse_tentatively (parser);
25709 rcv = cp_parser_simple_type_specifier (parser,
25710 /*decl_specs=*/NULL,
25711 CP_PARSER_FLAGS_NONE);
25712
25713 if (cp_parser_parse_definitely (parser))
25714 return objc_get_class_reference (rcv);
25715
25716 cp_parser_error (parser, "objective-c++ message receiver expected");
25717 return error_mark_node;
25718 }
25719
25720 /* Parse the arguments and selectors comprising an Objective-C message.
25721
25722 objc-message-args:
25723 objc-selector
25724 objc-selector-args
25725 objc-selector-args , objc-comma-args
25726
25727 objc-selector-args:
25728 objc-selector [opt] : assignment-expression
25729 objc-selector-args objc-selector [opt] : assignment-expression
25730
25731 objc-comma-args:
25732 assignment-expression
25733 objc-comma-args , assignment-expression
25734
25735 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25736 selector arguments and TREE_VALUE containing a list of comma
25737 arguments. */
25738
25739 static tree
25740 cp_parser_objc_message_args (cp_parser* parser)
25741 {
25742 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25743 bool maybe_unary_selector_p = true;
25744 cp_token *token = cp_lexer_peek_token (parser->lexer);
25745
25746 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25747 {
25748 tree selector = NULL_TREE, arg;
25749
25750 if (token->type != CPP_COLON)
25751 selector = cp_parser_objc_selector (parser);
25752
25753 /* Detect if we have a unary selector. */
25754 if (maybe_unary_selector_p
25755 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25756 return build_tree_list (selector, NULL_TREE);
25757
25758 maybe_unary_selector_p = false;
25759 cp_parser_require (parser, CPP_COLON, RT_COLON);
25760 arg = cp_parser_assignment_expression (parser);
25761
25762 sel_args
25763 = chainon (sel_args,
25764 build_tree_list (selector, arg));
25765
25766 token = cp_lexer_peek_token (parser->lexer);
25767 }
25768
25769 /* Handle non-selector arguments, if any. */
25770 while (token->type == CPP_COMMA)
25771 {
25772 tree arg;
25773
25774 cp_lexer_consume_token (parser->lexer);
25775 arg = cp_parser_assignment_expression (parser);
25776
25777 addl_args
25778 = chainon (addl_args,
25779 build_tree_list (NULL_TREE, arg));
25780
25781 token = cp_lexer_peek_token (parser->lexer);
25782 }
25783
25784 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25785 {
25786 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25787 return build_tree_list (error_mark_node, error_mark_node);
25788 }
25789
25790 return build_tree_list (sel_args, addl_args);
25791 }
25792
25793 /* Parse an Objective-C encode expression.
25794
25795 objc-encode-expression:
25796 @encode objc-typename
25797
25798 Returns an encoded representation of the type argument. */
25799
25800 static tree
25801 cp_parser_objc_encode_expression (cp_parser* parser)
25802 {
25803 tree type;
25804 cp_token *token;
25805
25806 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25807 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25808 token = cp_lexer_peek_token (parser->lexer);
25809 type = complete_type (cp_parser_type_id (parser));
25810 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25811
25812 if (!type)
25813 {
25814 error_at (token->location,
25815 "%<@encode%> must specify a type as an argument");
25816 return error_mark_node;
25817 }
25818
25819 /* This happens if we find @encode(T) (where T is a template
25820 typename or something dependent on a template typename) when
25821 parsing a template. In that case, we can't compile it
25822 immediately, but we rather create an AT_ENCODE_EXPR which will
25823 need to be instantiated when the template is used.
25824 */
25825 if (dependent_type_p (type))
25826 {
25827 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25828 TREE_READONLY (value) = 1;
25829 return value;
25830 }
25831
25832 return objc_build_encode_expr (type);
25833 }
25834
25835 /* Parse an Objective-C @defs expression. */
25836
25837 static tree
25838 cp_parser_objc_defs_expression (cp_parser *parser)
25839 {
25840 tree name;
25841
25842 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25843 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25844 name = cp_parser_identifier (parser);
25845 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25846
25847 return objc_get_class_ivars (name);
25848 }
25849
25850 /* Parse an Objective-C protocol expression.
25851
25852 objc-protocol-expression:
25853 @protocol ( identifier )
25854
25855 Returns a representation of the protocol expression. */
25856
25857 static tree
25858 cp_parser_objc_protocol_expression (cp_parser* parser)
25859 {
25860 tree proto;
25861
25862 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25863 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25864 proto = cp_parser_identifier (parser);
25865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25866
25867 return objc_build_protocol_expr (proto);
25868 }
25869
25870 /* Parse an Objective-C selector expression.
25871
25872 objc-selector-expression:
25873 @selector ( objc-method-signature )
25874
25875 objc-method-signature:
25876 objc-selector
25877 objc-selector-seq
25878
25879 objc-selector-seq:
25880 objc-selector :
25881 objc-selector-seq objc-selector :
25882
25883 Returns a representation of the method selector. */
25884
25885 static tree
25886 cp_parser_objc_selector_expression (cp_parser* parser)
25887 {
25888 tree sel_seq = NULL_TREE;
25889 bool maybe_unary_selector_p = true;
25890 cp_token *token;
25891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25892
25893 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25894 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25895 token = cp_lexer_peek_token (parser->lexer);
25896
25897 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25898 || token->type == CPP_SCOPE)
25899 {
25900 tree selector = NULL_TREE;
25901
25902 if (token->type != CPP_COLON
25903 || token->type == CPP_SCOPE)
25904 selector = cp_parser_objc_selector (parser);
25905
25906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25907 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25908 {
25909 /* Detect if we have a unary selector. */
25910 if (maybe_unary_selector_p)
25911 {
25912 sel_seq = selector;
25913 goto finish_selector;
25914 }
25915 else
25916 {
25917 cp_parser_error (parser, "expected %<:%>");
25918 }
25919 }
25920 maybe_unary_selector_p = false;
25921 token = cp_lexer_consume_token (parser->lexer);
25922
25923 if (token->type == CPP_SCOPE)
25924 {
25925 sel_seq
25926 = chainon (sel_seq,
25927 build_tree_list (selector, NULL_TREE));
25928 sel_seq
25929 = chainon (sel_seq,
25930 build_tree_list (NULL_TREE, NULL_TREE));
25931 }
25932 else
25933 sel_seq
25934 = chainon (sel_seq,
25935 build_tree_list (selector, NULL_TREE));
25936
25937 token = cp_lexer_peek_token (parser->lexer);
25938 }
25939
25940 finish_selector:
25941 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25942
25943 return objc_build_selector_expr (loc, sel_seq);
25944 }
25945
25946 /* Parse a list of identifiers.
25947
25948 objc-identifier-list:
25949 identifier
25950 objc-identifier-list , identifier
25951
25952 Returns a TREE_LIST of identifier nodes. */
25953
25954 static tree
25955 cp_parser_objc_identifier_list (cp_parser* parser)
25956 {
25957 tree identifier;
25958 tree list;
25959 cp_token *sep;
25960
25961 identifier = cp_parser_identifier (parser);
25962 if (identifier == error_mark_node)
25963 return error_mark_node;
25964
25965 list = build_tree_list (NULL_TREE, identifier);
25966 sep = cp_lexer_peek_token (parser->lexer);
25967
25968 while (sep->type == CPP_COMMA)
25969 {
25970 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25971 identifier = cp_parser_identifier (parser);
25972 if (identifier == error_mark_node)
25973 return list;
25974
25975 list = chainon (list, build_tree_list (NULL_TREE,
25976 identifier));
25977 sep = cp_lexer_peek_token (parser->lexer);
25978 }
25979
25980 return list;
25981 }
25982
25983 /* Parse an Objective-C alias declaration.
25984
25985 objc-alias-declaration:
25986 @compatibility_alias identifier identifier ;
25987
25988 This function registers the alias mapping with the Objective-C front end.
25989 It returns nothing. */
25990
25991 static void
25992 cp_parser_objc_alias_declaration (cp_parser* parser)
25993 {
25994 tree alias, orig;
25995
25996 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25997 alias = cp_parser_identifier (parser);
25998 orig = cp_parser_identifier (parser);
25999 objc_declare_alias (alias, orig);
26000 cp_parser_consume_semicolon_at_end_of_statement (parser);
26001 }
26002
26003 /* Parse an Objective-C class forward-declaration.
26004
26005 objc-class-declaration:
26006 @class objc-identifier-list ;
26007
26008 The function registers the forward declarations with the Objective-C
26009 front end. It returns nothing. */
26010
26011 static void
26012 cp_parser_objc_class_declaration (cp_parser* parser)
26013 {
26014 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26015 while (true)
26016 {
26017 tree id;
26018
26019 id = cp_parser_identifier (parser);
26020 if (id == error_mark_node)
26021 break;
26022
26023 objc_declare_class (id);
26024
26025 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26026 cp_lexer_consume_token (parser->lexer);
26027 else
26028 break;
26029 }
26030 cp_parser_consume_semicolon_at_end_of_statement (parser);
26031 }
26032
26033 /* Parse a list of Objective-C protocol references.
26034
26035 objc-protocol-refs-opt:
26036 objc-protocol-refs [opt]
26037
26038 objc-protocol-refs:
26039 < objc-identifier-list >
26040
26041 Returns a TREE_LIST of identifiers, if any. */
26042
26043 static tree
26044 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26045 {
26046 tree protorefs = NULL_TREE;
26047
26048 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26049 {
26050 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26051 protorefs = cp_parser_objc_identifier_list (parser);
26052 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26053 }
26054
26055 return protorefs;
26056 }
26057
26058 /* Parse a Objective-C visibility specification. */
26059
26060 static void
26061 cp_parser_objc_visibility_spec (cp_parser* parser)
26062 {
26063 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26064
26065 switch (vis->keyword)
26066 {
26067 case RID_AT_PRIVATE:
26068 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26069 break;
26070 case RID_AT_PROTECTED:
26071 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26072 break;
26073 case RID_AT_PUBLIC:
26074 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26075 break;
26076 case RID_AT_PACKAGE:
26077 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26078 break;
26079 default:
26080 return;
26081 }
26082
26083 /* Eat '@private'/'@protected'/'@public'. */
26084 cp_lexer_consume_token (parser->lexer);
26085 }
26086
26087 /* Parse an Objective-C method type. Return 'true' if it is a class
26088 (+) method, and 'false' if it is an instance (-) method. */
26089
26090 static inline bool
26091 cp_parser_objc_method_type (cp_parser* parser)
26092 {
26093 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26094 return true;
26095 else
26096 return false;
26097 }
26098
26099 /* Parse an Objective-C protocol qualifier. */
26100
26101 static tree
26102 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26103 {
26104 tree quals = NULL_TREE, node;
26105 cp_token *token = cp_lexer_peek_token (parser->lexer);
26106
26107 node = token->u.value;
26108
26109 while (node && identifier_p (node)
26110 && (node == ridpointers [(int) RID_IN]
26111 || node == ridpointers [(int) RID_OUT]
26112 || node == ridpointers [(int) RID_INOUT]
26113 || node == ridpointers [(int) RID_BYCOPY]
26114 || node == ridpointers [(int) RID_BYREF]
26115 || node == ridpointers [(int) RID_ONEWAY]))
26116 {
26117 quals = tree_cons (NULL_TREE, node, quals);
26118 cp_lexer_consume_token (parser->lexer);
26119 token = cp_lexer_peek_token (parser->lexer);
26120 node = token->u.value;
26121 }
26122
26123 return quals;
26124 }
26125
26126 /* Parse an Objective-C typename. */
26127
26128 static tree
26129 cp_parser_objc_typename (cp_parser* parser)
26130 {
26131 tree type_name = NULL_TREE;
26132
26133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26134 {
26135 tree proto_quals, cp_type = NULL_TREE;
26136
26137 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26138 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26139
26140 /* An ObjC type name may consist of just protocol qualifiers, in which
26141 case the type shall default to 'id'. */
26142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26143 {
26144 cp_type = cp_parser_type_id (parser);
26145
26146 /* If the type could not be parsed, an error has already
26147 been produced. For error recovery, behave as if it had
26148 not been specified, which will use the default type
26149 'id'. */
26150 if (cp_type == error_mark_node)
26151 {
26152 cp_type = NULL_TREE;
26153 /* We need to skip to the closing parenthesis as
26154 cp_parser_type_id() does not seem to do it for
26155 us. */
26156 cp_parser_skip_to_closing_parenthesis (parser,
26157 /*recovering=*/true,
26158 /*or_comma=*/false,
26159 /*consume_paren=*/false);
26160 }
26161 }
26162
26163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26164 type_name = build_tree_list (proto_quals, cp_type);
26165 }
26166
26167 return type_name;
26168 }
26169
26170 /* Check to see if TYPE refers to an Objective-C selector name. */
26171
26172 static bool
26173 cp_parser_objc_selector_p (enum cpp_ttype type)
26174 {
26175 return (type == CPP_NAME || type == CPP_KEYWORD
26176 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26177 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26178 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26179 || type == CPP_XOR || type == CPP_XOR_EQ);
26180 }
26181
26182 /* Parse an Objective-C selector. */
26183
26184 static tree
26185 cp_parser_objc_selector (cp_parser* parser)
26186 {
26187 cp_token *token = cp_lexer_consume_token (parser->lexer);
26188
26189 if (!cp_parser_objc_selector_p (token->type))
26190 {
26191 error_at (token->location, "invalid Objective-C++ selector name");
26192 return error_mark_node;
26193 }
26194
26195 /* C++ operator names are allowed to appear in ObjC selectors. */
26196 switch (token->type)
26197 {
26198 case CPP_AND_AND: return get_identifier ("and");
26199 case CPP_AND_EQ: return get_identifier ("and_eq");
26200 case CPP_AND: return get_identifier ("bitand");
26201 case CPP_OR: return get_identifier ("bitor");
26202 case CPP_COMPL: return get_identifier ("compl");
26203 case CPP_NOT: return get_identifier ("not");
26204 case CPP_NOT_EQ: return get_identifier ("not_eq");
26205 case CPP_OR_OR: return get_identifier ("or");
26206 case CPP_OR_EQ: return get_identifier ("or_eq");
26207 case CPP_XOR: return get_identifier ("xor");
26208 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26209 default: return token->u.value;
26210 }
26211 }
26212
26213 /* Parse an Objective-C params list. */
26214
26215 static tree
26216 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26217 {
26218 tree params = NULL_TREE;
26219 bool maybe_unary_selector_p = true;
26220 cp_token *token = cp_lexer_peek_token (parser->lexer);
26221
26222 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26223 {
26224 tree selector = NULL_TREE, type_name, identifier;
26225 tree parm_attr = NULL_TREE;
26226
26227 if (token->keyword == RID_ATTRIBUTE)
26228 break;
26229
26230 if (token->type != CPP_COLON)
26231 selector = cp_parser_objc_selector (parser);
26232
26233 /* Detect if we have a unary selector. */
26234 if (maybe_unary_selector_p
26235 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26236 {
26237 params = selector; /* Might be followed by attributes. */
26238 break;
26239 }
26240
26241 maybe_unary_selector_p = false;
26242 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26243 {
26244 /* Something went quite wrong. There should be a colon
26245 here, but there is not. Stop parsing parameters. */
26246 break;
26247 }
26248 type_name = cp_parser_objc_typename (parser);
26249 /* New ObjC allows attributes on parameters too. */
26250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26251 parm_attr = cp_parser_attributes_opt (parser);
26252 identifier = cp_parser_identifier (parser);
26253
26254 params
26255 = chainon (params,
26256 objc_build_keyword_decl (selector,
26257 type_name,
26258 identifier,
26259 parm_attr));
26260
26261 token = cp_lexer_peek_token (parser->lexer);
26262 }
26263
26264 if (params == NULL_TREE)
26265 {
26266 cp_parser_error (parser, "objective-c++ method declaration is expected");
26267 return error_mark_node;
26268 }
26269
26270 /* We allow tail attributes for the method. */
26271 if (token->keyword == RID_ATTRIBUTE)
26272 {
26273 *attributes = cp_parser_attributes_opt (parser);
26274 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26275 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26276 return params;
26277 cp_parser_error (parser,
26278 "method attributes must be specified at the end");
26279 return error_mark_node;
26280 }
26281
26282 if (params == NULL_TREE)
26283 {
26284 cp_parser_error (parser, "objective-c++ method declaration is expected");
26285 return error_mark_node;
26286 }
26287 return params;
26288 }
26289
26290 /* Parse the non-keyword Objective-C params. */
26291
26292 static tree
26293 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26294 tree* attributes)
26295 {
26296 tree params = make_node (TREE_LIST);
26297 cp_token *token = cp_lexer_peek_token (parser->lexer);
26298 *ellipsisp = false; /* Initially, assume no ellipsis. */
26299
26300 while (token->type == CPP_COMMA)
26301 {
26302 cp_parameter_declarator *parmdecl;
26303 tree parm;
26304
26305 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26306 token = cp_lexer_peek_token (parser->lexer);
26307
26308 if (token->type == CPP_ELLIPSIS)
26309 {
26310 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26311 *ellipsisp = true;
26312 token = cp_lexer_peek_token (parser->lexer);
26313 break;
26314 }
26315
26316 /* TODO: parse attributes for tail parameters. */
26317 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26318 parm = grokdeclarator (parmdecl->declarator,
26319 &parmdecl->decl_specifiers,
26320 PARM, /*initialized=*/0,
26321 /*attrlist=*/NULL);
26322
26323 chainon (params, build_tree_list (NULL_TREE, parm));
26324 token = cp_lexer_peek_token (parser->lexer);
26325 }
26326
26327 /* We allow tail attributes for the method. */
26328 if (token->keyword == RID_ATTRIBUTE)
26329 {
26330 if (*attributes == NULL_TREE)
26331 {
26332 *attributes = cp_parser_attributes_opt (parser);
26333 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26334 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26335 return params;
26336 }
26337 else
26338 /* We have an error, but parse the attributes, so that we can
26339 carry on. */
26340 *attributes = cp_parser_attributes_opt (parser);
26341
26342 cp_parser_error (parser,
26343 "method attributes must be specified at the end");
26344 return error_mark_node;
26345 }
26346
26347 return params;
26348 }
26349
26350 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26351
26352 static void
26353 cp_parser_objc_interstitial_code (cp_parser* parser)
26354 {
26355 cp_token *token = cp_lexer_peek_token (parser->lexer);
26356
26357 /* If the next token is `extern' and the following token is a string
26358 literal, then we have a linkage specification. */
26359 if (token->keyword == RID_EXTERN
26360 && cp_parser_is_pure_string_literal
26361 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26362 cp_parser_linkage_specification (parser);
26363 /* Handle #pragma, if any. */
26364 else if (token->type == CPP_PRAGMA)
26365 cp_parser_pragma (parser, pragma_objc_icode);
26366 /* Allow stray semicolons. */
26367 else if (token->type == CPP_SEMICOLON)
26368 cp_lexer_consume_token (parser->lexer);
26369 /* Mark methods as optional or required, when building protocols. */
26370 else if (token->keyword == RID_AT_OPTIONAL)
26371 {
26372 cp_lexer_consume_token (parser->lexer);
26373 objc_set_method_opt (true);
26374 }
26375 else if (token->keyword == RID_AT_REQUIRED)
26376 {
26377 cp_lexer_consume_token (parser->lexer);
26378 objc_set_method_opt (false);
26379 }
26380 else if (token->keyword == RID_NAMESPACE)
26381 cp_parser_namespace_definition (parser);
26382 /* Other stray characters must generate errors. */
26383 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26384 {
26385 cp_lexer_consume_token (parser->lexer);
26386 error ("stray %qs between Objective-C++ methods",
26387 token->type == CPP_OPEN_BRACE ? "{" : "}");
26388 }
26389 /* Finally, try to parse a block-declaration, or a function-definition. */
26390 else
26391 cp_parser_block_declaration (parser, /*statement_p=*/false);
26392 }
26393
26394 /* Parse a method signature. */
26395
26396 static tree
26397 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26398 {
26399 tree rettype, kwdparms, optparms;
26400 bool ellipsis = false;
26401 bool is_class_method;
26402
26403 is_class_method = cp_parser_objc_method_type (parser);
26404 rettype = cp_parser_objc_typename (parser);
26405 *attributes = NULL_TREE;
26406 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26407 if (kwdparms == error_mark_node)
26408 return error_mark_node;
26409 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26410 if (optparms == error_mark_node)
26411 return error_mark_node;
26412
26413 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26414 }
26415
26416 static bool
26417 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26418 {
26419 tree tattr;
26420 cp_lexer_save_tokens (parser->lexer);
26421 tattr = cp_parser_attributes_opt (parser);
26422 gcc_assert (tattr) ;
26423
26424 /* If the attributes are followed by a method introducer, this is not allowed.
26425 Dump the attributes and flag the situation. */
26426 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26427 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26428 return true;
26429
26430 /* Otherwise, the attributes introduce some interstitial code, possibly so
26431 rewind to allow that check. */
26432 cp_lexer_rollback_tokens (parser->lexer);
26433 return false;
26434 }
26435
26436 /* Parse an Objective-C method prototype list. */
26437
26438 static void
26439 cp_parser_objc_method_prototype_list (cp_parser* parser)
26440 {
26441 cp_token *token = cp_lexer_peek_token (parser->lexer);
26442
26443 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26444 {
26445 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26446 {
26447 tree attributes, sig;
26448 bool is_class_method;
26449 if (token->type == CPP_PLUS)
26450 is_class_method = true;
26451 else
26452 is_class_method = false;
26453 sig = cp_parser_objc_method_signature (parser, &attributes);
26454 if (sig == error_mark_node)
26455 {
26456 cp_parser_skip_to_end_of_block_or_statement (parser);
26457 token = cp_lexer_peek_token (parser->lexer);
26458 continue;
26459 }
26460 objc_add_method_declaration (is_class_method, sig, attributes);
26461 cp_parser_consume_semicolon_at_end_of_statement (parser);
26462 }
26463 else if (token->keyword == RID_AT_PROPERTY)
26464 cp_parser_objc_at_property_declaration (parser);
26465 else if (token->keyword == RID_ATTRIBUTE
26466 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26467 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26468 OPT_Wattributes,
26469 "prefix attributes are ignored for methods");
26470 else
26471 /* Allow for interspersed non-ObjC++ code. */
26472 cp_parser_objc_interstitial_code (parser);
26473
26474 token = cp_lexer_peek_token (parser->lexer);
26475 }
26476
26477 if (token->type != CPP_EOF)
26478 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26479 else
26480 cp_parser_error (parser, "expected %<@end%>");
26481
26482 objc_finish_interface ();
26483 }
26484
26485 /* Parse an Objective-C method definition list. */
26486
26487 static void
26488 cp_parser_objc_method_definition_list (cp_parser* parser)
26489 {
26490 cp_token *token = cp_lexer_peek_token (parser->lexer);
26491
26492 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26493 {
26494 tree meth;
26495
26496 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26497 {
26498 cp_token *ptk;
26499 tree sig, attribute;
26500 bool is_class_method;
26501 if (token->type == CPP_PLUS)
26502 is_class_method = true;
26503 else
26504 is_class_method = false;
26505 push_deferring_access_checks (dk_deferred);
26506 sig = cp_parser_objc_method_signature (parser, &attribute);
26507 if (sig == error_mark_node)
26508 {
26509 cp_parser_skip_to_end_of_block_or_statement (parser);
26510 token = cp_lexer_peek_token (parser->lexer);
26511 continue;
26512 }
26513 objc_start_method_definition (is_class_method, sig, attribute,
26514 NULL_TREE);
26515
26516 /* For historical reasons, we accept an optional semicolon. */
26517 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26518 cp_lexer_consume_token (parser->lexer);
26519
26520 ptk = cp_lexer_peek_token (parser->lexer);
26521 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26522 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26523 {
26524 perform_deferred_access_checks (tf_warning_or_error);
26525 stop_deferring_access_checks ();
26526 meth = cp_parser_function_definition_after_declarator (parser,
26527 false);
26528 pop_deferring_access_checks ();
26529 objc_finish_method_definition (meth);
26530 }
26531 }
26532 /* The following case will be removed once @synthesize is
26533 completely implemented. */
26534 else if (token->keyword == RID_AT_PROPERTY)
26535 cp_parser_objc_at_property_declaration (parser);
26536 else if (token->keyword == RID_AT_SYNTHESIZE)
26537 cp_parser_objc_at_synthesize_declaration (parser);
26538 else if (token->keyword == RID_AT_DYNAMIC)
26539 cp_parser_objc_at_dynamic_declaration (parser);
26540 else if (token->keyword == RID_ATTRIBUTE
26541 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26542 warning_at (token->location, OPT_Wattributes,
26543 "prefix attributes are ignored for methods");
26544 else
26545 /* Allow for interspersed non-ObjC++ code. */
26546 cp_parser_objc_interstitial_code (parser);
26547
26548 token = cp_lexer_peek_token (parser->lexer);
26549 }
26550
26551 if (token->type != CPP_EOF)
26552 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26553 else
26554 cp_parser_error (parser, "expected %<@end%>");
26555
26556 objc_finish_implementation ();
26557 }
26558
26559 /* Parse Objective-C ivars. */
26560
26561 static void
26562 cp_parser_objc_class_ivars (cp_parser* parser)
26563 {
26564 cp_token *token = cp_lexer_peek_token (parser->lexer);
26565
26566 if (token->type != CPP_OPEN_BRACE)
26567 return; /* No ivars specified. */
26568
26569 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26570 token = cp_lexer_peek_token (parser->lexer);
26571
26572 while (token->type != CPP_CLOSE_BRACE
26573 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26574 {
26575 cp_decl_specifier_seq declspecs;
26576 int decl_class_or_enum_p;
26577 tree prefix_attributes;
26578
26579 cp_parser_objc_visibility_spec (parser);
26580
26581 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26582 break;
26583
26584 cp_parser_decl_specifier_seq (parser,
26585 CP_PARSER_FLAGS_OPTIONAL,
26586 &declspecs,
26587 &decl_class_or_enum_p);
26588
26589 /* auto, register, static, extern, mutable. */
26590 if (declspecs.storage_class != sc_none)
26591 {
26592 cp_parser_error (parser, "invalid type for instance variable");
26593 declspecs.storage_class = sc_none;
26594 }
26595
26596 /* thread_local. */
26597 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26598 {
26599 cp_parser_error (parser, "invalid type for instance variable");
26600 declspecs.locations[ds_thread] = 0;
26601 }
26602
26603 /* typedef. */
26604 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26605 {
26606 cp_parser_error (parser, "invalid type for instance variable");
26607 declspecs.locations[ds_typedef] = 0;
26608 }
26609
26610 prefix_attributes = declspecs.attributes;
26611 declspecs.attributes = NULL_TREE;
26612
26613 /* Keep going until we hit the `;' at the end of the
26614 declaration. */
26615 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26616 {
26617 tree width = NULL_TREE, attributes, first_attribute, decl;
26618 cp_declarator *declarator = NULL;
26619 int ctor_dtor_or_conv_p;
26620
26621 /* Check for a (possibly unnamed) bitfield declaration. */
26622 token = cp_lexer_peek_token (parser->lexer);
26623 if (token->type == CPP_COLON)
26624 goto eat_colon;
26625
26626 if (token->type == CPP_NAME
26627 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26628 == CPP_COLON))
26629 {
26630 /* Get the name of the bitfield. */
26631 declarator = make_id_declarator (NULL_TREE,
26632 cp_parser_identifier (parser),
26633 sfk_none);
26634
26635 eat_colon:
26636 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26637 /* Get the width of the bitfield. */
26638 width
26639 = cp_parser_constant_expression (parser);
26640 }
26641 else
26642 {
26643 /* Parse the declarator. */
26644 declarator
26645 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26646 &ctor_dtor_or_conv_p,
26647 /*parenthesized_p=*/NULL,
26648 /*member_p=*/false,
26649 /*friend_p=*/false);
26650 }
26651
26652 /* Look for attributes that apply to the ivar. */
26653 attributes = cp_parser_attributes_opt (parser);
26654 /* Remember which attributes are prefix attributes and
26655 which are not. */
26656 first_attribute = attributes;
26657 /* Combine the attributes. */
26658 attributes = chainon (prefix_attributes, attributes);
26659
26660 if (width)
26661 /* Create the bitfield declaration. */
26662 decl = grokbitfield (declarator, &declspecs,
26663 width,
26664 attributes);
26665 else
26666 decl = grokfield (declarator, &declspecs,
26667 NULL_TREE, /*init_const_expr_p=*/false,
26668 NULL_TREE, attributes);
26669
26670 /* Add the instance variable. */
26671 if (decl != error_mark_node && decl != NULL_TREE)
26672 objc_add_instance_variable (decl);
26673
26674 /* Reset PREFIX_ATTRIBUTES. */
26675 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26676 attributes = TREE_CHAIN (attributes);
26677 if (attributes)
26678 TREE_CHAIN (attributes) = NULL_TREE;
26679
26680 token = cp_lexer_peek_token (parser->lexer);
26681
26682 if (token->type == CPP_COMMA)
26683 {
26684 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26685 continue;
26686 }
26687 break;
26688 }
26689
26690 cp_parser_consume_semicolon_at_end_of_statement (parser);
26691 token = cp_lexer_peek_token (parser->lexer);
26692 }
26693
26694 if (token->keyword == RID_AT_END)
26695 cp_parser_error (parser, "expected %<}%>");
26696
26697 /* Do not consume the RID_AT_END, so it will be read again as terminating
26698 the @interface of @implementation. */
26699 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26700 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26701
26702 /* For historical reasons, we accept an optional semicolon. */
26703 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26704 cp_lexer_consume_token (parser->lexer);
26705 }
26706
26707 /* Parse an Objective-C protocol declaration. */
26708
26709 static void
26710 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26711 {
26712 tree proto, protorefs;
26713 cp_token *tok;
26714
26715 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26717 {
26718 tok = cp_lexer_peek_token (parser->lexer);
26719 error_at (tok->location, "identifier expected after %<@protocol%>");
26720 cp_parser_consume_semicolon_at_end_of_statement (parser);
26721 return;
26722 }
26723
26724 /* See if we have a forward declaration or a definition. */
26725 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26726
26727 /* Try a forward declaration first. */
26728 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26729 {
26730 while (true)
26731 {
26732 tree id;
26733
26734 id = cp_parser_identifier (parser);
26735 if (id == error_mark_node)
26736 break;
26737
26738 objc_declare_protocol (id, attributes);
26739
26740 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26741 cp_lexer_consume_token (parser->lexer);
26742 else
26743 break;
26744 }
26745 cp_parser_consume_semicolon_at_end_of_statement (parser);
26746 }
26747
26748 /* Ok, we got a full-fledged definition (or at least should). */
26749 else
26750 {
26751 proto = cp_parser_identifier (parser);
26752 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26753 objc_start_protocol (proto, protorefs, attributes);
26754 cp_parser_objc_method_prototype_list (parser);
26755 }
26756 }
26757
26758 /* Parse an Objective-C superclass or category. */
26759
26760 static void
26761 cp_parser_objc_superclass_or_category (cp_parser *parser,
26762 bool iface_p,
26763 tree *super,
26764 tree *categ, bool *is_class_extension)
26765 {
26766 cp_token *next = cp_lexer_peek_token (parser->lexer);
26767
26768 *super = *categ = NULL_TREE;
26769 *is_class_extension = false;
26770 if (next->type == CPP_COLON)
26771 {
26772 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26773 *super = cp_parser_identifier (parser);
26774 }
26775 else if (next->type == CPP_OPEN_PAREN)
26776 {
26777 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26778
26779 /* If there is no category name, and this is an @interface, we
26780 have a class extension. */
26781 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26782 {
26783 *categ = NULL_TREE;
26784 *is_class_extension = true;
26785 }
26786 else
26787 *categ = cp_parser_identifier (parser);
26788
26789 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26790 }
26791 }
26792
26793 /* Parse an Objective-C class interface. */
26794
26795 static void
26796 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26797 {
26798 tree name, super, categ, protos;
26799 bool is_class_extension;
26800
26801 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26802 name = cp_parser_identifier (parser);
26803 if (name == error_mark_node)
26804 {
26805 /* It's hard to recover because even if valid @interface stuff
26806 is to follow, we can't compile it (or validate it) if we
26807 don't even know which class it refers to. Let's assume this
26808 was a stray '@interface' token in the stream and skip it.
26809 */
26810 return;
26811 }
26812 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26813 &is_class_extension);
26814 protos = cp_parser_objc_protocol_refs_opt (parser);
26815
26816 /* We have either a class or a category on our hands. */
26817 if (categ || is_class_extension)
26818 objc_start_category_interface (name, categ, protos, attributes);
26819 else
26820 {
26821 objc_start_class_interface (name, super, protos, attributes);
26822 /* Handle instance variable declarations, if any. */
26823 cp_parser_objc_class_ivars (parser);
26824 objc_continue_interface ();
26825 }
26826
26827 cp_parser_objc_method_prototype_list (parser);
26828 }
26829
26830 /* Parse an Objective-C class implementation. */
26831
26832 static void
26833 cp_parser_objc_class_implementation (cp_parser* parser)
26834 {
26835 tree name, super, categ;
26836 bool is_class_extension;
26837
26838 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26839 name = cp_parser_identifier (parser);
26840 if (name == error_mark_node)
26841 {
26842 /* It's hard to recover because even if valid @implementation
26843 stuff is to follow, we can't compile it (or validate it) if
26844 we don't even know which class it refers to. Let's assume
26845 this was a stray '@implementation' token in the stream and
26846 skip it.
26847 */
26848 return;
26849 }
26850 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26851 &is_class_extension);
26852
26853 /* We have either a class or a category on our hands. */
26854 if (categ)
26855 objc_start_category_implementation (name, categ);
26856 else
26857 {
26858 objc_start_class_implementation (name, super);
26859 /* Handle instance variable declarations, if any. */
26860 cp_parser_objc_class_ivars (parser);
26861 objc_continue_implementation ();
26862 }
26863
26864 cp_parser_objc_method_definition_list (parser);
26865 }
26866
26867 /* Consume the @end token and finish off the implementation. */
26868
26869 static void
26870 cp_parser_objc_end_implementation (cp_parser* parser)
26871 {
26872 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26873 objc_finish_implementation ();
26874 }
26875
26876 /* Parse an Objective-C declaration. */
26877
26878 static void
26879 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26880 {
26881 /* Try to figure out what kind of declaration is present. */
26882 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26883
26884 if (attributes)
26885 switch (kwd->keyword)
26886 {
26887 case RID_AT_ALIAS:
26888 case RID_AT_CLASS:
26889 case RID_AT_END:
26890 error_at (kwd->location, "attributes may not be specified before"
26891 " the %<@%D%> Objective-C++ keyword",
26892 kwd->u.value);
26893 attributes = NULL;
26894 break;
26895 case RID_AT_IMPLEMENTATION:
26896 warning_at (kwd->location, OPT_Wattributes,
26897 "prefix attributes are ignored before %<@%D%>",
26898 kwd->u.value);
26899 attributes = NULL;
26900 default:
26901 break;
26902 }
26903
26904 switch (kwd->keyword)
26905 {
26906 case RID_AT_ALIAS:
26907 cp_parser_objc_alias_declaration (parser);
26908 break;
26909 case RID_AT_CLASS:
26910 cp_parser_objc_class_declaration (parser);
26911 break;
26912 case RID_AT_PROTOCOL:
26913 cp_parser_objc_protocol_declaration (parser, attributes);
26914 break;
26915 case RID_AT_INTERFACE:
26916 cp_parser_objc_class_interface (parser, attributes);
26917 break;
26918 case RID_AT_IMPLEMENTATION:
26919 cp_parser_objc_class_implementation (parser);
26920 break;
26921 case RID_AT_END:
26922 cp_parser_objc_end_implementation (parser);
26923 break;
26924 default:
26925 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26926 kwd->u.value);
26927 cp_parser_skip_to_end_of_block_or_statement (parser);
26928 }
26929 }
26930
26931 /* Parse an Objective-C try-catch-finally statement.
26932
26933 objc-try-catch-finally-stmt:
26934 @try compound-statement objc-catch-clause-seq [opt]
26935 objc-finally-clause [opt]
26936
26937 objc-catch-clause-seq:
26938 objc-catch-clause objc-catch-clause-seq [opt]
26939
26940 objc-catch-clause:
26941 @catch ( objc-exception-declaration ) compound-statement
26942
26943 objc-finally-clause:
26944 @finally compound-statement
26945
26946 objc-exception-declaration:
26947 parameter-declaration
26948 '...'
26949
26950 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26951
26952 Returns NULL_TREE.
26953
26954 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26955 for C. Keep them in sync. */
26956
26957 static tree
26958 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26959 {
26960 location_t location;
26961 tree stmt;
26962
26963 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26964 location = cp_lexer_peek_token (parser->lexer)->location;
26965 objc_maybe_warn_exceptions (location);
26966 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26967 node, lest it get absorbed into the surrounding block. */
26968 stmt = push_stmt_list ();
26969 cp_parser_compound_statement (parser, NULL, false, false);
26970 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26971
26972 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26973 {
26974 cp_parameter_declarator *parm;
26975 tree parameter_declaration = error_mark_node;
26976 bool seen_open_paren = false;
26977
26978 cp_lexer_consume_token (parser->lexer);
26979 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26980 seen_open_paren = true;
26981 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26982 {
26983 /* We have "@catch (...)" (where the '...' are literally
26984 what is in the code). Skip the '...'.
26985 parameter_declaration is set to NULL_TREE, and
26986 objc_being_catch_clauses() knows that that means
26987 '...'. */
26988 cp_lexer_consume_token (parser->lexer);
26989 parameter_declaration = NULL_TREE;
26990 }
26991 else
26992 {
26993 /* We have "@catch (NSException *exception)" or something
26994 like that. Parse the parameter declaration. */
26995 parm = cp_parser_parameter_declaration (parser, false, NULL);
26996 if (parm == NULL)
26997 parameter_declaration = error_mark_node;
26998 else
26999 parameter_declaration = grokdeclarator (parm->declarator,
27000 &parm->decl_specifiers,
27001 PARM, /*initialized=*/0,
27002 /*attrlist=*/NULL);
27003 }
27004 if (seen_open_paren)
27005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27006 else
27007 {
27008 /* If there was no open parenthesis, we are recovering from
27009 an error, and we are trying to figure out what mistake
27010 the user has made. */
27011
27012 /* If there is an immediate closing parenthesis, the user
27013 probably forgot the opening one (ie, they typed "@catch
27014 NSException *e)". Parse the closing parenthesis and keep
27015 going. */
27016 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27017 cp_lexer_consume_token (parser->lexer);
27018
27019 /* If these is no immediate closing parenthesis, the user
27020 probably doesn't know that parenthesis are required at
27021 all (ie, they typed "@catch NSException *e"). So, just
27022 forget about the closing parenthesis and keep going. */
27023 }
27024 objc_begin_catch_clause (parameter_declaration);
27025 cp_parser_compound_statement (parser, NULL, false, false);
27026 objc_finish_catch_clause ();
27027 }
27028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27029 {
27030 cp_lexer_consume_token (parser->lexer);
27031 location = cp_lexer_peek_token (parser->lexer)->location;
27032 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27033 node, lest it get absorbed into the surrounding block. */
27034 stmt = push_stmt_list ();
27035 cp_parser_compound_statement (parser, NULL, false, false);
27036 objc_build_finally_clause (location, pop_stmt_list (stmt));
27037 }
27038
27039 return objc_finish_try_stmt ();
27040 }
27041
27042 /* Parse an Objective-C synchronized statement.
27043
27044 objc-synchronized-stmt:
27045 @synchronized ( expression ) compound-statement
27046
27047 Returns NULL_TREE. */
27048
27049 static tree
27050 cp_parser_objc_synchronized_statement (cp_parser *parser)
27051 {
27052 location_t location;
27053 tree lock, stmt;
27054
27055 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27056
27057 location = cp_lexer_peek_token (parser->lexer)->location;
27058 objc_maybe_warn_exceptions (location);
27059 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27060 lock = cp_parser_expression (parser);
27061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27062
27063 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27064 node, lest it get absorbed into the surrounding block. */
27065 stmt = push_stmt_list ();
27066 cp_parser_compound_statement (parser, NULL, false, false);
27067
27068 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27069 }
27070
27071 /* Parse an Objective-C throw statement.
27072
27073 objc-throw-stmt:
27074 @throw assignment-expression [opt] ;
27075
27076 Returns a constructed '@throw' statement. */
27077
27078 static tree
27079 cp_parser_objc_throw_statement (cp_parser *parser)
27080 {
27081 tree expr = NULL_TREE;
27082 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27083
27084 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27085
27086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27087 expr = cp_parser_expression (parser);
27088
27089 cp_parser_consume_semicolon_at_end_of_statement (parser);
27090
27091 return objc_build_throw_stmt (loc, expr);
27092 }
27093
27094 /* Parse an Objective-C statement. */
27095
27096 static tree
27097 cp_parser_objc_statement (cp_parser * parser)
27098 {
27099 /* Try to figure out what kind of declaration is present. */
27100 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27101
27102 switch (kwd->keyword)
27103 {
27104 case RID_AT_TRY:
27105 return cp_parser_objc_try_catch_finally_statement (parser);
27106 case RID_AT_SYNCHRONIZED:
27107 return cp_parser_objc_synchronized_statement (parser);
27108 case RID_AT_THROW:
27109 return cp_parser_objc_throw_statement (parser);
27110 default:
27111 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27112 kwd->u.value);
27113 cp_parser_skip_to_end_of_block_or_statement (parser);
27114 }
27115
27116 return error_mark_node;
27117 }
27118
27119 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27120 look ahead to see if an objc keyword follows the attributes. This
27121 is to detect the use of prefix attributes on ObjC @interface and
27122 @protocol. */
27123
27124 static bool
27125 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27126 {
27127 cp_lexer_save_tokens (parser->lexer);
27128 *attrib = cp_parser_attributes_opt (parser);
27129 gcc_assert (*attrib);
27130 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27131 {
27132 cp_lexer_commit_tokens (parser->lexer);
27133 return true;
27134 }
27135 cp_lexer_rollback_tokens (parser->lexer);
27136 return false;
27137 }
27138
27139 /* This routine is a minimal replacement for
27140 c_parser_struct_declaration () used when parsing the list of
27141 types/names or ObjC++ properties. For example, when parsing the
27142 code
27143
27144 @property (readonly) int a, b, c;
27145
27146 this function is responsible for parsing "int a, int b, int c" and
27147 returning the declarations as CHAIN of DECLs.
27148
27149 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27150 similar parsing. */
27151 static tree
27152 cp_parser_objc_struct_declaration (cp_parser *parser)
27153 {
27154 tree decls = NULL_TREE;
27155 cp_decl_specifier_seq declspecs;
27156 int decl_class_or_enum_p;
27157 tree prefix_attributes;
27158
27159 cp_parser_decl_specifier_seq (parser,
27160 CP_PARSER_FLAGS_NONE,
27161 &declspecs,
27162 &decl_class_or_enum_p);
27163
27164 if (declspecs.type == error_mark_node)
27165 return error_mark_node;
27166
27167 /* auto, register, static, extern, mutable. */
27168 if (declspecs.storage_class != sc_none)
27169 {
27170 cp_parser_error (parser, "invalid type for property");
27171 declspecs.storage_class = sc_none;
27172 }
27173
27174 /* thread_local. */
27175 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27176 {
27177 cp_parser_error (parser, "invalid type for property");
27178 declspecs.locations[ds_thread] = 0;
27179 }
27180
27181 /* typedef. */
27182 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27183 {
27184 cp_parser_error (parser, "invalid type for property");
27185 declspecs.locations[ds_typedef] = 0;
27186 }
27187
27188 prefix_attributes = declspecs.attributes;
27189 declspecs.attributes = NULL_TREE;
27190
27191 /* Keep going until we hit the `;' at the end of the declaration. */
27192 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27193 {
27194 tree attributes, first_attribute, decl;
27195 cp_declarator *declarator;
27196 cp_token *token;
27197
27198 /* Parse the declarator. */
27199 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27200 NULL, NULL, false, false);
27201
27202 /* Look for attributes that apply to the ivar. */
27203 attributes = cp_parser_attributes_opt (parser);
27204 /* Remember which attributes are prefix attributes and
27205 which are not. */
27206 first_attribute = attributes;
27207 /* Combine the attributes. */
27208 attributes = chainon (prefix_attributes, attributes);
27209
27210 decl = grokfield (declarator, &declspecs,
27211 NULL_TREE, /*init_const_expr_p=*/false,
27212 NULL_TREE, attributes);
27213
27214 if (decl == error_mark_node || decl == NULL_TREE)
27215 return error_mark_node;
27216
27217 /* Reset PREFIX_ATTRIBUTES. */
27218 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27219 attributes = TREE_CHAIN (attributes);
27220 if (attributes)
27221 TREE_CHAIN (attributes) = NULL_TREE;
27222
27223 DECL_CHAIN (decl) = decls;
27224 decls = decl;
27225
27226 token = cp_lexer_peek_token (parser->lexer);
27227 if (token->type == CPP_COMMA)
27228 {
27229 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27230 continue;
27231 }
27232 else
27233 break;
27234 }
27235 return decls;
27236 }
27237
27238 /* Parse an Objective-C @property declaration. The syntax is:
27239
27240 objc-property-declaration:
27241 '@property' objc-property-attributes[opt] struct-declaration ;
27242
27243 objc-property-attributes:
27244 '(' objc-property-attribute-list ')'
27245
27246 objc-property-attribute-list:
27247 objc-property-attribute
27248 objc-property-attribute-list, objc-property-attribute
27249
27250 objc-property-attribute
27251 'getter' = identifier
27252 'setter' = identifier
27253 'readonly'
27254 'readwrite'
27255 'assign'
27256 'retain'
27257 'copy'
27258 'nonatomic'
27259
27260 For example:
27261 @property NSString *name;
27262 @property (readonly) id object;
27263 @property (retain, nonatomic, getter=getTheName) id name;
27264 @property int a, b, c;
27265
27266 PS: This function is identical to
27267 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27268 static void
27269 cp_parser_objc_at_property_declaration (cp_parser *parser)
27270 {
27271 /* The following variables hold the attributes of the properties as
27272 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27273 seen. When we see an attribute, we set them to 'true' (if they
27274 are boolean properties) or to the identifier (if they have an
27275 argument, ie, for getter and setter). Note that here we only
27276 parse the list of attributes, check the syntax and accumulate the
27277 attributes that we find. objc_add_property_declaration() will
27278 then process the information. */
27279 bool property_assign = false;
27280 bool property_copy = false;
27281 tree property_getter_ident = NULL_TREE;
27282 bool property_nonatomic = false;
27283 bool property_readonly = false;
27284 bool property_readwrite = false;
27285 bool property_retain = false;
27286 tree property_setter_ident = NULL_TREE;
27287
27288 /* 'properties' is the list of properties that we read. Usually a
27289 single one, but maybe more (eg, in "@property int a, b, c;" there
27290 are three). */
27291 tree properties;
27292 location_t loc;
27293
27294 loc = cp_lexer_peek_token (parser->lexer)->location;
27295
27296 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27297
27298 /* Parse the optional attribute list... */
27299 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27300 {
27301 /* Eat the '('. */
27302 cp_lexer_consume_token (parser->lexer);
27303
27304 while (true)
27305 {
27306 bool syntax_error = false;
27307 cp_token *token = cp_lexer_peek_token (parser->lexer);
27308 enum rid keyword;
27309
27310 if (token->type != CPP_NAME)
27311 {
27312 cp_parser_error (parser, "expected identifier");
27313 break;
27314 }
27315 keyword = C_RID_CODE (token->u.value);
27316 cp_lexer_consume_token (parser->lexer);
27317 switch (keyword)
27318 {
27319 case RID_ASSIGN: property_assign = true; break;
27320 case RID_COPY: property_copy = true; break;
27321 case RID_NONATOMIC: property_nonatomic = true; break;
27322 case RID_READONLY: property_readonly = true; break;
27323 case RID_READWRITE: property_readwrite = true; break;
27324 case RID_RETAIN: property_retain = true; break;
27325
27326 case RID_GETTER:
27327 case RID_SETTER:
27328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27329 {
27330 if (keyword == RID_GETTER)
27331 cp_parser_error (parser,
27332 "missing %<=%> (after %<getter%> attribute)");
27333 else
27334 cp_parser_error (parser,
27335 "missing %<=%> (after %<setter%> attribute)");
27336 syntax_error = true;
27337 break;
27338 }
27339 cp_lexer_consume_token (parser->lexer); /* eat the = */
27340 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27341 {
27342 cp_parser_error (parser, "expected identifier");
27343 syntax_error = true;
27344 break;
27345 }
27346 if (keyword == RID_SETTER)
27347 {
27348 if (property_setter_ident != NULL_TREE)
27349 {
27350 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27351 cp_lexer_consume_token (parser->lexer);
27352 }
27353 else
27354 property_setter_ident = cp_parser_objc_selector (parser);
27355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27356 cp_parser_error (parser, "setter name must terminate with %<:%>");
27357 else
27358 cp_lexer_consume_token (parser->lexer);
27359 }
27360 else
27361 {
27362 if (property_getter_ident != NULL_TREE)
27363 {
27364 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27365 cp_lexer_consume_token (parser->lexer);
27366 }
27367 else
27368 property_getter_ident = cp_parser_objc_selector (parser);
27369 }
27370 break;
27371 default:
27372 cp_parser_error (parser, "unknown property attribute");
27373 syntax_error = true;
27374 break;
27375 }
27376
27377 if (syntax_error)
27378 break;
27379
27380 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27381 cp_lexer_consume_token (parser->lexer);
27382 else
27383 break;
27384 }
27385
27386 /* FIXME: "@property (setter, assign);" will generate a spurious
27387 "error: expected ‘)’ before ‘,’ token". This is because
27388 cp_parser_require, unlike the C counterpart, will produce an
27389 error even if we are in error recovery. */
27390 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27391 {
27392 cp_parser_skip_to_closing_parenthesis (parser,
27393 /*recovering=*/true,
27394 /*or_comma=*/false,
27395 /*consume_paren=*/true);
27396 }
27397 }
27398
27399 /* ... and the property declaration(s). */
27400 properties = cp_parser_objc_struct_declaration (parser);
27401
27402 if (properties == error_mark_node)
27403 {
27404 cp_parser_skip_to_end_of_statement (parser);
27405 /* If the next token is now a `;', consume it. */
27406 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27407 cp_lexer_consume_token (parser->lexer);
27408 return;
27409 }
27410
27411 if (properties == NULL_TREE)
27412 cp_parser_error (parser, "expected identifier");
27413 else
27414 {
27415 /* Comma-separated properties are chained together in
27416 reverse order; add them one by one. */
27417 properties = nreverse (properties);
27418
27419 for (; properties; properties = TREE_CHAIN (properties))
27420 objc_add_property_declaration (loc, copy_node (properties),
27421 property_readonly, property_readwrite,
27422 property_assign, property_retain,
27423 property_copy, property_nonatomic,
27424 property_getter_ident, property_setter_ident);
27425 }
27426
27427 cp_parser_consume_semicolon_at_end_of_statement (parser);
27428 }
27429
27430 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27431
27432 objc-synthesize-declaration:
27433 @synthesize objc-synthesize-identifier-list ;
27434
27435 objc-synthesize-identifier-list:
27436 objc-synthesize-identifier
27437 objc-synthesize-identifier-list, objc-synthesize-identifier
27438
27439 objc-synthesize-identifier
27440 identifier
27441 identifier = identifier
27442
27443 For example:
27444 @synthesize MyProperty;
27445 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27446
27447 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27448 for C. Keep them in sync.
27449 */
27450 static void
27451 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27452 {
27453 tree list = NULL_TREE;
27454 location_t loc;
27455 loc = cp_lexer_peek_token (parser->lexer)->location;
27456
27457 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27458 while (true)
27459 {
27460 tree property, ivar;
27461 property = cp_parser_identifier (parser);
27462 if (property == error_mark_node)
27463 {
27464 cp_parser_consume_semicolon_at_end_of_statement (parser);
27465 return;
27466 }
27467 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27468 {
27469 cp_lexer_consume_token (parser->lexer);
27470 ivar = cp_parser_identifier (parser);
27471 if (ivar == error_mark_node)
27472 {
27473 cp_parser_consume_semicolon_at_end_of_statement (parser);
27474 return;
27475 }
27476 }
27477 else
27478 ivar = NULL_TREE;
27479 list = chainon (list, build_tree_list (ivar, property));
27480 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27481 cp_lexer_consume_token (parser->lexer);
27482 else
27483 break;
27484 }
27485 cp_parser_consume_semicolon_at_end_of_statement (parser);
27486 objc_add_synthesize_declaration (loc, list);
27487 }
27488
27489 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27490
27491 objc-dynamic-declaration:
27492 @dynamic identifier-list ;
27493
27494 For example:
27495 @dynamic MyProperty;
27496 @dynamic MyProperty, AnotherProperty;
27497
27498 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27499 for C. Keep them in sync.
27500 */
27501 static void
27502 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27503 {
27504 tree list = NULL_TREE;
27505 location_t loc;
27506 loc = cp_lexer_peek_token (parser->lexer)->location;
27507
27508 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27509 while (true)
27510 {
27511 tree property;
27512 property = cp_parser_identifier (parser);
27513 if (property == error_mark_node)
27514 {
27515 cp_parser_consume_semicolon_at_end_of_statement (parser);
27516 return;
27517 }
27518 list = chainon (list, build_tree_list (NULL, property));
27519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27520 cp_lexer_consume_token (parser->lexer);
27521 else
27522 break;
27523 }
27524 cp_parser_consume_semicolon_at_end_of_statement (parser);
27525 objc_add_dynamic_declaration (loc, list);
27526 }
27527
27528 \f
27529 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27530
27531 /* Returns name of the next clause.
27532 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27533 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27534 returned and the token is consumed. */
27535
27536 static pragma_omp_clause
27537 cp_parser_omp_clause_name (cp_parser *parser)
27538 {
27539 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27540
27541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27542 result = PRAGMA_OMP_CLAUSE_IF;
27543 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27544 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27545 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27546 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27547 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27548 result = PRAGMA_OMP_CLAUSE_FOR;
27549 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27550 {
27551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27552 const char *p = IDENTIFIER_POINTER (id);
27553
27554 switch (p[0])
27555 {
27556 case 'a':
27557 if (!strcmp ("aligned", p))
27558 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27559 break;
27560 case 'c':
27561 if (!strcmp ("collapse", p))
27562 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27563 else if (!strcmp ("copyin", p))
27564 result = PRAGMA_OMP_CLAUSE_COPYIN;
27565 else if (!strcmp ("copyprivate", p))
27566 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27567 break;
27568 case 'd':
27569 if (!strcmp ("depend", p))
27570 result = PRAGMA_OMP_CLAUSE_DEPEND;
27571 else if (!strcmp ("device", p))
27572 result = PRAGMA_OMP_CLAUSE_DEVICE;
27573 else if (!strcmp ("dist_schedule", p))
27574 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27575 break;
27576 case 'f':
27577 if (!strcmp ("final", p))
27578 result = PRAGMA_OMP_CLAUSE_FINAL;
27579 else if (!strcmp ("firstprivate", p))
27580 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27581 else if (!strcmp ("from", p))
27582 result = PRAGMA_OMP_CLAUSE_FROM;
27583 break;
27584 case 'i':
27585 if (!strcmp ("inbranch", p))
27586 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27587 break;
27588 case 'l':
27589 if (!strcmp ("lastprivate", p))
27590 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27591 else if (!strcmp ("linear", p))
27592 result = PRAGMA_OMP_CLAUSE_LINEAR;
27593 break;
27594 case 'm':
27595 if (!strcmp ("map", p))
27596 result = PRAGMA_OMP_CLAUSE_MAP;
27597 else if (!strcmp ("mergeable", p))
27598 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27599 else if (flag_cilkplus && !strcmp ("mask", p))
27600 result = PRAGMA_CILK_CLAUSE_MASK;
27601 break;
27602 case 'n':
27603 if (!strcmp ("notinbranch", p))
27604 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27605 else if (!strcmp ("nowait", p))
27606 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27607 else if (flag_cilkplus && !strcmp ("nomask", p))
27608 result = PRAGMA_CILK_CLAUSE_NOMASK;
27609 else if (!strcmp ("num_teams", p))
27610 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27611 else if (!strcmp ("num_threads", p))
27612 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27613 break;
27614 case 'o':
27615 if (!strcmp ("ordered", p))
27616 result = PRAGMA_OMP_CLAUSE_ORDERED;
27617 break;
27618 case 'p':
27619 if (!strcmp ("parallel", p))
27620 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27621 else if (!strcmp ("proc_bind", p))
27622 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27623 break;
27624 case 'r':
27625 if (!strcmp ("reduction", p))
27626 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27627 break;
27628 case 's':
27629 if (!strcmp ("safelen", p))
27630 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27631 else if (!strcmp ("schedule", p))
27632 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27633 else if (!strcmp ("sections", p))
27634 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27635 else if (!strcmp ("shared", p))
27636 result = PRAGMA_OMP_CLAUSE_SHARED;
27637 else if (!strcmp ("simdlen", p))
27638 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27639 break;
27640 case 't':
27641 if (!strcmp ("taskgroup", p))
27642 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27643 else if (!strcmp ("thread_limit", p))
27644 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27645 else if (!strcmp ("to", p))
27646 result = PRAGMA_OMP_CLAUSE_TO;
27647 break;
27648 case 'u':
27649 if (!strcmp ("uniform", p))
27650 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27651 else if (!strcmp ("untied", p))
27652 result = PRAGMA_OMP_CLAUSE_UNTIED;
27653 break;
27654 case 'v':
27655 if (flag_cilkplus && !strcmp ("vectorlength", p))
27656 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27657 break;
27658 }
27659 }
27660
27661 if (result != PRAGMA_OMP_CLAUSE_NONE)
27662 cp_lexer_consume_token (parser->lexer);
27663
27664 return result;
27665 }
27666
27667 /* Validate that a clause of the given type does not already exist. */
27668
27669 static void
27670 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27671 const char *name, location_t location)
27672 {
27673 tree c;
27674
27675 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27676 if (OMP_CLAUSE_CODE (c) == code)
27677 {
27678 error_at (location, "too many %qs clauses", name);
27679 break;
27680 }
27681 }
27682
27683 /* OpenMP 2.5:
27684 variable-list:
27685 identifier
27686 variable-list , identifier
27687
27688 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27689 colon). An opening parenthesis will have been consumed by the caller.
27690
27691 If KIND is nonzero, create the appropriate node and install the decl
27692 in OMP_CLAUSE_DECL and add the node to the head of the list.
27693
27694 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27695 return the list created.
27696
27697 COLON can be NULL if only closing parenthesis should end the list,
27698 or pointer to bool which will receive false if the list is terminated
27699 by closing parenthesis or true if the list is terminated by colon. */
27700
27701 static tree
27702 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27703 tree list, bool *colon)
27704 {
27705 cp_token *token;
27706 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27707 if (colon)
27708 {
27709 parser->colon_corrects_to_scope_p = false;
27710 *colon = false;
27711 }
27712 while (1)
27713 {
27714 tree name, decl;
27715
27716 token = cp_lexer_peek_token (parser->lexer);
27717 name = cp_parser_id_expression (parser, /*template_p=*/false,
27718 /*check_dependency_p=*/true,
27719 /*template_p=*/NULL,
27720 /*declarator_p=*/false,
27721 /*optional_p=*/false);
27722 if (name == error_mark_node)
27723 goto skip_comma;
27724
27725 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27726 if (decl == error_mark_node)
27727 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27728 token->location);
27729 else if (kind != 0)
27730 {
27731 switch (kind)
27732 {
27733 case OMP_CLAUSE_MAP:
27734 case OMP_CLAUSE_FROM:
27735 case OMP_CLAUSE_TO:
27736 case OMP_CLAUSE_DEPEND:
27737 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27738 {
27739 tree low_bound = NULL_TREE, length = NULL_TREE;
27740
27741 parser->colon_corrects_to_scope_p = false;
27742 cp_lexer_consume_token (parser->lexer);
27743 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27744 low_bound = cp_parser_expression (parser);
27745 if (!colon)
27746 parser->colon_corrects_to_scope_p
27747 = saved_colon_corrects_to_scope_p;
27748 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27749 length = integer_one_node;
27750 else
27751 {
27752 /* Look for `:'. */
27753 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27754 goto skip_comma;
27755 if (!cp_lexer_next_token_is (parser->lexer,
27756 CPP_CLOSE_SQUARE))
27757 length = cp_parser_expression (parser);
27758 }
27759 /* Look for the closing `]'. */
27760 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27761 RT_CLOSE_SQUARE))
27762 goto skip_comma;
27763 decl = tree_cons (low_bound, length, decl);
27764 }
27765 break;
27766 default:
27767 break;
27768 }
27769
27770 tree u = build_omp_clause (token->location, kind);
27771 OMP_CLAUSE_DECL (u) = decl;
27772 OMP_CLAUSE_CHAIN (u) = list;
27773 list = u;
27774 }
27775 else
27776 list = tree_cons (decl, NULL_TREE, list);
27777
27778 get_comma:
27779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27780 break;
27781 cp_lexer_consume_token (parser->lexer);
27782 }
27783
27784 if (colon)
27785 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27786
27787 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27788 {
27789 *colon = true;
27790 cp_parser_require (parser, CPP_COLON, RT_COLON);
27791 return list;
27792 }
27793
27794 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27795 {
27796 int ending;
27797
27798 /* Try to resync to an unnested comma. Copied from
27799 cp_parser_parenthesized_expression_list. */
27800 skip_comma:
27801 if (colon)
27802 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27803 ending = cp_parser_skip_to_closing_parenthesis (parser,
27804 /*recovering=*/true,
27805 /*or_comma=*/true,
27806 /*consume_paren=*/true);
27807 if (ending < 0)
27808 goto get_comma;
27809 }
27810
27811 return list;
27812 }
27813
27814 /* Similarly, but expect leading and trailing parenthesis. This is a very
27815 common case for omp clauses. */
27816
27817 static tree
27818 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27819 {
27820 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27821 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27822 return list;
27823 }
27824
27825 /* OpenMP 3.0:
27826 collapse ( constant-expression ) */
27827
27828 static tree
27829 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27830 {
27831 tree c, num;
27832 location_t loc;
27833 HOST_WIDE_INT n;
27834
27835 loc = cp_lexer_peek_token (parser->lexer)->location;
27836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27837 return list;
27838
27839 num = cp_parser_constant_expression (parser);
27840
27841 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27843 /*or_comma=*/false,
27844 /*consume_paren=*/true);
27845
27846 if (num == error_mark_node)
27847 return list;
27848 num = fold_non_dependent_expr (num);
27849 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27850 || !tree_fits_shwi_p (num)
27851 || (n = tree_to_shwi (num)) <= 0
27852 || (int) n != n)
27853 {
27854 error_at (loc, "collapse argument needs positive constant integer expression");
27855 return list;
27856 }
27857
27858 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27859 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27860 OMP_CLAUSE_CHAIN (c) = list;
27861 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27862
27863 return c;
27864 }
27865
27866 /* OpenMP 2.5:
27867 default ( shared | none ) */
27868
27869 static tree
27870 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27871 {
27872 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27873 tree c;
27874
27875 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27876 return list;
27877 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27878 {
27879 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27880 const char *p = IDENTIFIER_POINTER (id);
27881
27882 switch (p[0])
27883 {
27884 case 'n':
27885 if (strcmp ("none", p) != 0)
27886 goto invalid_kind;
27887 kind = OMP_CLAUSE_DEFAULT_NONE;
27888 break;
27889
27890 case 's':
27891 if (strcmp ("shared", p) != 0)
27892 goto invalid_kind;
27893 kind = OMP_CLAUSE_DEFAULT_SHARED;
27894 break;
27895
27896 default:
27897 goto invalid_kind;
27898 }
27899
27900 cp_lexer_consume_token (parser->lexer);
27901 }
27902 else
27903 {
27904 invalid_kind:
27905 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27906 }
27907
27908 if (!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 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27914 return list;
27915
27916 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27917 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27918 OMP_CLAUSE_CHAIN (c) = list;
27919 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27920
27921 return c;
27922 }
27923
27924 /* OpenMP 3.1:
27925 final ( expression ) */
27926
27927 static tree
27928 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27929 {
27930 tree t, c;
27931
27932 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27933 return list;
27934
27935 t = cp_parser_condition (parser);
27936
27937 if (t == error_mark_node
27938 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27940 /*or_comma=*/false,
27941 /*consume_paren=*/true);
27942
27943 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27944
27945 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27946 OMP_CLAUSE_FINAL_EXPR (c) = t;
27947 OMP_CLAUSE_CHAIN (c) = list;
27948
27949 return c;
27950 }
27951
27952 /* OpenMP 2.5:
27953 if ( expression ) */
27954
27955 static tree
27956 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27957 {
27958 tree t, c;
27959
27960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27961 return list;
27962
27963 t = cp_parser_condition (parser);
27964
27965 if (t == error_mark_node
27966 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27967 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27968 /*or_comma=*/false,
27969 /*consume_paren=*/true);
27970
27971 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27972
27973 c = build_omp_clause (location, OMP_CLAUSE_IF);
27974 OMP_CLAUSE_IF_EXPR (c) = t;
27975 OMP_CLAUSE_CHAIN (c) = list;
27976
27977 return c;
27978 }
27979
27980 /* OpenMP 3.1:
27981 mergeable */
27982
27983 static tree
27984 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27985 tree list, location_t location)
27986 {
27987 tree c;
27988
27989 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27990 location);
27991
27992 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27993 OMP_CLAUSE_CHAIN (c) = list;
27994 return c;
27995 }
27996
27997 /* OpenMP 2.5:
27998 nowait */
27999
28000 static tree
28001 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28002 tree list, location_t location)
28003 {
28004 tree c;
28005
28006 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28007
28008 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28009 OMP_CLAUSE_CHAIN (c) = list;
28010 return c;
28011 }
28012
28013 /* OpenMP 2.5:
28014 num_threads ( expression ) */
28015
28016 static tree
28017 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28018 location_t location)
28019 {
28020 tree t, c;
28021
28022 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28023 return list;
28024
28025 t = cp_parser_expression (parser);
28026
28027 if (t == error_mark_node
28028 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28029 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28030 /*or_comma=*/false,
28031 /*consume_paren=*/true);
28032
28033 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28034 "num_threads", location);
28035
28036 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28037 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28038 OMP_CLAUSE_CHAIN (c) = list;
28039
28040 return c;
28041 }
28042
28043 /* OpenMP 2.5:
28044 ordered */
28045
28046 static tree
28047 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28048 tree list, location_t location)
28049 {
28050 tree c;
28051
28052 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28053 "ordered", location);
28054
28055 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28056 OMP_CLAUSE_CHAIN (c) = list;
28057 return c;
28058 }
28059
28060 /* OpenMP 2.5:
28061 reduction ( reduction-operator : variable-list )
28062
28063 reduction-operator:
28064 One of: + * - & ^ | && ||
28065
28066 OpenMP 3.1:
28067
28068 reduction-operator:
28069 One of: + * - & ^ | && || min max
28070
28071 OpenMP 4.0:
28072
28073 reduction-operator:
28074 One of: + * - & ^ | && ||
28075 id-expression */
28076
28077 static tree
28078 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28079 {
28080 enum tree_code code = ERROR_MARK;
28081 tree nlist, c, id = NULL_TREE;
28082
28083 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28084 return list;
28085
28086 switch (cp_lexer_peek_token (parser->lexer)->type)
28087 {
28088 case CPP_PLUS: code = PLUS_EXPR; break;
28089 case CPP_MULT: code = MULT_EXPR; break;
28090 case CPP_MINUS: code = MINUS_EXPR; break;
28091 case CPP_AND: code = BIT_AND_EXPR; break;
28092 case CPP_XOR: code = BIT_XOR_EXPR; break;
28093 case CPP_OR: code = BIT_IOR_EXPR; break;
28094 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28095 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28096 default: break;
28097 }
28098
28099 if (code != ERROR_MARK)
28100 cp_lexer_consume_token (parser->lexer);
28101 else
28102 {
28103 bool saved_colon_corrects_to_scope_p;
28104 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28105 parser->colon_corrects_to_scope_p = false;
28106 id = cp_parser_id_expression (parser, /*template_p=*/false,
28107 /*check_dependency_p=*/true,
28108 /*template_p=*/NULL,
28109 /*declarator_p=*/false,
28110 /*optional_p=*/false);
28111 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28112 if (identifier_p (id))
28113 {
28114 const char *p = IDENTIFIER_POINTER (id);
28115
28116 if (strcmp (p, "min") == 0)
28117 code = MIN_EXPR;
28118 else if (strcmp (p, "max") == 0)
28119 code = MAX_EXPR;
28120 else if (id == ansi_opname (PLUS_EXPR))
28121 code = PLUS_EXPR;
28122 else if (id == ansi_opname (MULT_EXPR))
28123 code = MULT_EXPR;
28124 else if (id == ansi_opname (MINUS_EXPR))
28125 code = MINUS_EXPR;
28126 else if (id == ansi_opname (BIT_AND_EXPR))
28127 code = BIT_AND_EXPR;
28128 else if (id == ansi_opname (BIT_IOR_EXPR))
28129 code = BIT_IOR_EXPR;
28130 else if (id == ansi_opname (BIT_XOR_EXPR))
28131 code = BIT_XOR_EXPR;
28132 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28133 code = TRUTH_ANDIF_EXPR;
28134 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28135 code = TRUTH_ORIF_EXPR;
28136 id = omp_reduction_id (code, id, NULL_TREE);
28137 tree scope = parser->scope;
28138 if (scope)
28139 id = build_qualified_name (NULL_TREE, scope, id, false);
28140 parser->scope = NULL_TREE;
28141 parser->qualifying_scope = NULL_TREE;
28142 parser->object_scope = NULL_TREE;
28143 }
28144 else
28145 {
28146 error ("invalid reduction-identifier");
28147 resync_fail:
28148 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28149 /*or_comma=*/false,
28150 /*consume_paren=*/true);
28151 return list;
28152 }
28153 }
28154
28155 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28156 goto resync_fail;
28157
28158 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28159 NULL);
28160 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28161 {
28162 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28163 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28164 }
28165
28166 return nlist;
28167 }
28168
28169 /* OpenMP 2.5:
28170 schedule ( schedule-kind )
28171 schedule ( schedule-kind , expression )
28172
28173 schedule-kind:
28174 static | dynamic | guided | runtime | auto */
28175
28176 static tree
28177 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28178 {
28179 tree c, t;
28180
28181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28182 return list;
28183
28184 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28185
28186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28187 {
28188 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28189 const char *p = IDENTIFIER_POINTER (id);
28190
28191 switch (p[0])
28192 {
28193 case 'd':
28194 if (strcmp ("dynamic", p) != 0)
28195 goto invalid_kind;
28196 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28197 break;
28198
28199 case 'g':
28200 if (strcmp ("guided", p) != 0)
28201 goto invalid_kind;
28202 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28203 break;
28204
28205 case 'r':
28206 if (strcmp ("runtime", p) != 0)
28207 goto invalid_kind;
28208 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28209 break;
28210
28211 default:
28212 goto invalid_kind;
28213 }
28214 }
28215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28216 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28217 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28218 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28219 else
28220 goto invalid_kind;
28221 cp_lexer_consume_token (parser->lexer);
28222
28223 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28224 {
28225 cp_token *token;
28226 cp_lexer_consume_token (parser->lexer);
28227
28228 token = cp_lexer_peek_token (parser->lexer);
28229 t = cp_parser_assignment_expression (parser);
28230
28231 if (t == error_mark_node)
28232 goto resync_fail;
28233 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28234 error_at (token->location, "schedule %<runtime%> does not take "
28235 "a %<chunk_size%> parameter");
28236 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28237 error_at (token->location, "schedule %<auto%> does not take "
28238 "a %<chunk_size%> parameter");
28239 else
28240 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28241
28242 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28243 goto resync_fail;
28244 }
28245 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28246 goto resync_fail;
28247
28248 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28249 OMP_CLAUSE_CHAIN (c) = list;
28250 return c;
28251
28252 invalid_kind:
28253 cp_parser_error (parser, "invalid schedule kind");
28254 resync_fail:
28255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28256 /*or_comma=*/false,
28257 /*consume_paren=*/true);
28258 return list;
28259 }
28260
28261 /* OpenMP 3.0:
28262 untied */
28263
28264 static tree
28265 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28266 tree list, location_t location)
28267 {
28268 tree c;
28269
28270 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28271
28272 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28273 OMP_CLAUSE_CHAIN (c) = list;
28274 return c;
28275 }
28276
28277 /* OpenMP 4.0:
28278 inbranch
28279 notinbranch */
28280
28281 static tree
28282 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28283 tree list, location_t location)
28284 {
28285 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28286 tree c = build_omp_clause (location, code);
28287 OMP_CLAUSE_CHAIN (c) = list;
28288 return c;
28289 }
28290
28291 /* OpenMP 4.0:
28292 parallel
28293 for
28294 sections
28295 taskgroup */
28296
28297 static tree
28298 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28299 enum omp_clause_code code,
28300 tree list, location_t location)
28301 {
28302 tree c = build_omp_clause (location, code);
28303 OMP_CLAUSE_CHAIN (c) = list;
28304 return c;
28305 }
28306
28307 /* OpenMP 4.0:
28308 num_teams ( expression ) */
28309
28310 static tree
28311 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28312 location_t location)
28313 {
28314 tree t, c;
28315
28316 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28317 return list;
28318
28319 t = cp_parser_expression (parser);
28320
28321 if (t == error_mark_node
28322 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28323 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28324 /*or_comma=*/false,
28325 /*consume_paren=*/true);
28326
28327 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28328 "num_teams", location);
28329
28330 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28331 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28332 OMP_CLAUSE_CHAIN (c) = list;
28333
28334 return c;
28335 }
28336
28337 /* OpenMP 4.0:
28338 thread_limit ( expression ) */
28339
28340 static tree
28341 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28342 location_t location)
28343 {
28344 tree t, c;
28345
28346 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28347 return list;
28348
28349 t = cp_parser_expression (parser);
28350
28351 if (t == error_mark_node
28352 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28354 /*or_comma=*/false,
28355 /*consume_paren=*/true);
28356
28357 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28358 "thread_limit", location);
28359
28360 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28361 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28362 OMP_CLAUSE_CHAIN (c) = list;
28363
28364 return c;
28365 }
28366
28367 /* OpenMP 4.0:
28368 aligned ( variable-list )
28369 aligned ( variable-list : constant-expression ) */
28370
28371 static tree
28372 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28373 {
28374 tree nlist, c, alignment = NULL_TREE;
28375 bool colon;
28376
28377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28378 return list;
28379
28380 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28381 &colon);
28382
28383 if (colon)
28384 {
28385 alignment = cp_parser_constant_expression (parser);
28386
28387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28389 /*or_comma=*/false,
28390 /*consume_paren=*/true);
28391
28392 if (alignment == error_mark_node)
28393 alignment = NULL_TREE;
28394 }
28395
28396 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28397 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28398
28399 return nlist;
28400 }
28401
28402 /* OpenMP 4.0:
28403 linear ( variable-list )
28404 linear ( variable-list : expression ) */
28405
28406 static tree
28407 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28408 bool is_cilk_simd_fn)
28409 {
28410 tree nlist, c, step = integer_one_node;
28411 bool colon;
28412
28413 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28414 return list;
28415
28416 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28417 &colon);
28418
28419 if (colon)
28420 {
28421 step = cp_parser_expression (parser);
28422
28423 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28424 {
28425 sorry ("using parameters for %<linear%> step is not supported yet");
28426 step = integer_one_node;
28427 }
28428 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28429 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28430 /*or_comma=*/false,
28431 /*consume_paren=*/true);
28432
28433 if (step == error_mark_node)
28434 return list;
28435 }
28436
28437 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28438 OMP_CLAUSE_LINEAR_STEP (c) = step;
28439
28440 return nlist;
28441 }
28442
28443 /* OpenMP 4.0:
28444 safelen ( constant-expression ) */
28445
28446 static tree
28447 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28448 location_t location)
28449 {
28450 tree t, c;
28451
28452 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28453 return list;
28454
28455 t = cp_parser_constant_expression (parser);
28456
28457 if (t == error_mark_node
28458 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28460 /*or_comma=*/false,
28461 /*consume_paren=*/true);
28462
28463 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28464
28465 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28466 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28467 OMP_CLAUSE_CHAIN (c) = list;
28468
28469 return c;
28470 }
28471
28472 /* OpenMP 4.0:
28473 simdlen ( constant-expression ) */
28474
28475 static tree
28476 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28477 location_t location)
28478 {
28479 tree t, c;
28480
28481 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28482 return list;
28483
28484 t = cp_parser_constant_expression (parser);
28485
28486 if (t == error_mark_node
28487 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28489 /*or_comma=*/false,
28490 /*consume_paren=*/true);
28491
28492 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28493
28494 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28495 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28496 OMP_CLAUSE_CHAIN (c) = list;
28497
28498 return c;
28499 }
28500
28501 /* OpenMP 4.0:
28502 depend ( depend-kind : variable-list )
28503
28504 depend-kind:
28505 in | out | inout */
28506
28507 static tree
28508 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28509 {
28510 tree nlist, c;
28511 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28512
28513 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28514 return list;
28515
28516 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28517 {
28518 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28519 const char *p = IDENTIFIER_POINTER (id);
28520
28521 if (strcmp ("in", p) == 0)
28522 kind = OMP_CLAUSE_DEPEND_IN;
28523 else if (strcmp ("inout", p) == 0)
28524 kind = OMP_CLAUSE_DEPEND_INOUT;
28525 else if (strcmp ("out", p) == 0)
28526 kind = OMP_CLAUSE_DEPEND_OUT;
28527 else
28528 goto invalid_kind;
28529 }
28530 else
28531 goto invalid_kind;
28532
28533 cp_lexer_consume_token (parser->lexer);
28534 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28535 goto resync_fail;
28536
28537 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28538 NULL);
28539
28540 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28541 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28542
28543 return nlist;
28544
28545 invalid_kind:
28546 cp_parser_error (parser, "invalid depend kind");
28547 resync_fail:
28548 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28549 /*or_comma=*/false,
28550 /*consume_paren=*/true);
28551 return list;
28552 }
28553
28554 /* OpenMP 4.0:
28555 map ( map-kind : variable-list )
28556 map ( variable-list )
28557
28558 map-kind:
28559 alloc | to | from | tofrom */
28560
28561 static tree
28562 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28563 {
28564 tree nlist, c;
28565 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28566
28567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28568 return list;
28569
28570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28571 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28572 {
28573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28574 const char *p = IDENTIFIER_POINTER (id);
28575
28576 if (strcmp ("alloc", p) == 0)
28577 kind = OMP_CLAUSE_MAP_ALLOC;
28578 else if (strcmp ("to", p) == 0)
28579 kind = OMP_CLAUSE_MAP_TO;
28580 else if (strcmp ("from", p) == 0)
28581 kind = OMP_CLAUSE_MAP_FROM;
28582 else if (strcmp ("tofrom", p) == 0)
28583 kind = OMP_CLAUSE_MAP_TOFROM;
28584 else
28585 {
28586 cp_parser_error (parser, "invalid map kind");
28587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28588 /*or_comma=*/false,
28589 /*consume_paren=*/true);
28590 return list;
28591 }
28592 cp_lexer_consume_token (parser->lexer);
28593 cp_lexer_consume_token (parser->lexer);
28594 }
28595
28596 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28597 NULL);
28598
28599 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28600 OMP_CLAUSE_MAP_KIND (c) = kind;
28601
28602 return nlist;
28603 }
28604
28605 /* OpenMP 4.0:
28606 device ( expression ) */
28607
28608 static tree
28609 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28610 location_t location)
28611 {
28612 tree t, c;
28613
28614 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28615 return list;
28616
28617 t = cp_parser_expression (parser);
28618
28619 if (t == error_mark_node
28620 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28621 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28622 /*or_comma=*/false,
28623 /*consume_paren=*/true);
28624
28625 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28626 "device", location);
28627
28628 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28629 OMP_CLAUSE_DEVICE_ID (c) = t;
28630 OMP_CLAUSE_CHAIN (c) = list;
28631
28632 return c;
28633 }
28634
28635 /* OpenMP 4.0:
28636 dist_schedule ( static )
28637 dist_schedule ( static , expression ) */
28638
28639 static tree
28640 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28641 location_t location)
28642 {
28643 tree c, t;
28644
28645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28646 return list;
28647
28648 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28649
28650 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28651 goto invalid_kind;
28652 cp_lexer_consume_token (parser->lexer);
28653
28654 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28655 {
28656 cp_lexer_consume_token (parser->lexer);
28657
28658 t = cp_parser_assignment_expression (parser);
28659
28660 if (t == error_mark_node)
28661 goto resync_fail;
28662 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28663
28664 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28665 goto resync_fail;
28666 }
28667 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28668 goto resync_fail;
28669
28670 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28671 location);
28672 OMP_CLAUSE_CHAIN (c) = list;
28673 return c;
28674
28675 invalid_kind:
28676 cp_parser_error (parser, "invalid dist_schedule kind");
28677 resync_fail:
28678 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28679 /*or_comma=*/false,
28680 /*consume_paren=*/true);
28681 return list;
28682 }
28683
28684 /* OpenMP 4.0:
28685 proc_bind ( proc-bind-kind )
28686
28687 proc-bind-kind:
28688 master | close | spread */
28689
28690 static tree
28691 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28692 location_t location)
28693 {
28694 tree c;
28695 enum omp_clause_proc_bind_kind kind;
28696
28697 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28698 return list;
28699
28700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28701 {
28702 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28703 const char *p = IDENTIFIER_POINTER (id);
28704
28705 if (strcmp ("master", p) == 0)
28706 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28707 else if (strcmp ("close", p) == 0)
28708 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28709 else if (strcmp ("spread", p) == 0)
28710 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28711 else
28712 goto invalid_kind;
28713 }
28714 else
28715 goto invalid_kind;
28716
28717 cp_lexer_consume_token (parser->lexer);
28718 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28719 goto resync_fail;
28720
28721 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28722 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28723 location);
28724 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28725 OMP_CLAUSE_CHAIN (c) = list;
28726 return c;
28727
28728 invalid_kind:
28729 cp_parser_error (parser, "invalid depend kind");
28730 resync_fail:
28731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28732 /*or_comma=*/false,
28733 /*consume_paren=*/true);
28734 return list;
28735 }
28736
28737 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28738 is a bitmask in MASK. Return the list of clauses found; the result
28739 of clause default goes in *pdefault. */
28740
28741 static tree
28742 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28743 const char *where, cp_token *pragma_tok,
28744 bool finish_p = true)
28745 {
28746 tree clauses = NULL;
28747 bool first = true;
28748 cp_token *token = NULL;
28749 bool cilk_simd_fn = false;
28750
28751 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28752 {
28753 pragma_omp_clause c_kind;
28754 const char *c_name;
28755 tree prev = clauses;
28756
28757 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28758 cp_lexer_consume_token (parser->lexer);
28759
28760 token = cp_lexer_peek_token (parser->lexer);
28761 c_kind = cp_parser_omp_clause_name (parser);
28762
28763 switch (c_kind)
28764 {
28765 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28766 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28767 token->location);
28768 c_name = "collapse";
28769 break;
28770 case PRAGMA_OMP_CLAUSE_COPYIN:
28771 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28772 c_name = "copyin";
28773 break;
28774 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28775 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28776 clauses);
28777 c_name = "copyprivate";
28778 break;
28779 case PRAGMA_OMP_CLAUSE_DEFAULT:
28780 clauses = cp_parser_omp_clause_default (parser, clauses,
28781 token->location);
28782 c_name = "default";
28783 break;
28784 case PRAGMA_OMP_CLAUSE_FINAL:
28785 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28786 c_name = "final";
28787 break;
28788 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28789 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28790 clauses);
28791 c_name = "firstprivate";
28792 break;
28793 case PRAGMA_OMP_CLAUSE_IF:
28794 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28795 c_name = "if";
28796 break;
28797 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28798 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28799 clauses);
28800 c_name = "lastprivate";
28801 break;
28802 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28803 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28804 token->location);
28805 c_name = "mergeable";
28806 break;
28807 case PRAGMA_OMP_CLAUSE_NOWAIT:
28808 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28809 c_name = "nowait";
28810 break;
28811 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28812 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28813 token->location);
28814 c_name = "num_threads";
28815 break;
28816 case PRAGMA_OMP_CLAUSE_ORDERED:
28817 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28818 token->location);
28819 c_name = "ordered";
28820 break;
28821 case PRAGMA_OMP_CLAUSE_PRIVATE:
28822 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28823 clauses);
28824 c_name = "private";
28825 break;
28826 case PRAGMA_OMP_CLAUSE_REDUCTION:
28827 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28828 c_name = "reduction";
28829 break;
28830 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28831 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28832 token->location);
28833 c_name = "schedule";
28834 break;
28835 case PRAGMA_OMP_CLAUSE_SHARED:
28836 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28837 clauses);
28838 c_name = "shared";
28839 break;
28840 case PRAGMA_OMP_CLAUSE_UNTIED:
28841 clauses = cp_parser_omp_clause_untied (parser, clauses,
28842 token->location);
28843 c_name = "untied";
28844 break;
28845 case PRAGMA_OMP_CLAUSE_INBRANCH:
28846 case PRAGMA_CILK_CLAUSE_MASK:
28847 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28848 clauses, token->location);
28849 c_name = "inbranch";
28850 break;
28851 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28852 case PRAGMA_CILK_CLAUSE_NOMASK:
28853 clauses = cp_parser_omp_clause_branch (parser,
28854 OMP_CLAUSE_NOTINBRANCH,
28855 clauses, token->location);
28856 c_name = "notinbranch";
28857 break;
28858 case PRAGMA_OMP_CLAUSE_PARALLEL:
28859 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28860 clauses, token->location);
28861 c_name = "parallel";
28862 if (!first)
28863 {
28864 clause_not_first:
28865 error_at (token->location, "%qs must be the first clause of %qs",
28866 c_name, where);
28867 clauses = prev;
28868 }
28869 break;
28870 case PRAGMA_OMP_CLAUSE_FOR:
28871 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28872 clauses, token->location);
28873 c_name = "for";
28874 if (!first)
28875 goto clause_not_first;
28876 break;
28877 case PRAGMA_OMP_CLAUSE_SECTIONS:
28878 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28879 clauses, token->location);
28880 c_name = "sections";
28881 if (!first)
28882 goto clause_not_first;
28883 break;
28884 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28885 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28886 clauses, token->location);
28887 c_name = "taskgroup";
28888 if (!first)
28889 goto clause_not_first;
28890 break;
28891 case PRAGMA_OMP_CLAUSE_TO:
28892 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28893 clauses);
28894 c_name = "to";
28895 break;
28896 case PRAGMA_OMP_CLAUSE_FROM:
28897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28898 clauses);
28899 c_name = "from";
28900 break;
28901 case PRAGMA_OMP_CLAUSE_UNIFORM:
28902 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28903 clauses);
28904 c_name = "uniform";
28905 break;
28906 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28907 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28908 token->location);
28909 c_name = "num_teams";
28910 break;
28911 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28912 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28913 token->location);
28914 c_name = "thread_limit";
28915 break;
28916 case PRAGMA_OMP_CLAUSE_ALIGNED:
28917 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28918 c_name = "aligned";
28919 break;
28920 case PRAGMA_OMP_CLAUSE_LINEAR:
28921 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28922 cilk_simd_fn = true;
28923 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28924 c_name = "linear";
28925 break;
28926 case PRAGMA_OMP_CLAUSE_DEPEND:
28927 clauses = cp_parser_omp_clause_depend (parser, clauses);
28928 c_name = "depend";
28929 break;
28930 case PRAGMA_OMP_CLAUSE_MAP:
28931 clauses = cp_parser_omp_clause_map (parser, clauses);
28932 c_name = "map";
28933 break;
28934 case PRAGMA_OMP_CLAUSE_DEVICE:
28935 clauses = cp_parser_omp_clause_device (parser, clauses,
28936 token->location);
28937 c_name = "device";
28938 break;
28939 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28940 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28941 token->location);
28942 c_name = "dist_schedule";
28943 break;
28944 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28945 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28946 token->location);
28947 c_name = "proc_bind";
28948 break;
28949 case PRAGMA_OMP_CLAUSE_SAFELEN:
28950 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28951 token->location);
28952 c_name = "safelen";
28953 break;
28954 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28955 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28956 token->location);
28957 c_name = "simdlen";
28958 break;
28959 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28960 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28961 c_name = "simdlen";
28962 break;
28963 default:
28964 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28965 goto saw_error;
28966 }
28967
28968 first = false;
28969
28970 if (((mask >> c_kind) & 1) == 0)
28971 {
28972 /* Remove the invalid clause(s) from the list to avoid
28973 confusing the rest of the compiler. */
28974 clauses = prev;
28975 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28976 }
28977 }
28978 saw_error:
28979 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28980 no reason to skip to the end. */
28981 if (!(flag_cilkplus && pragma_tok == NULL))
28982 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28983 if (finish_p)
28984 return finish_omp_clauses (clauses);
28985 return clauses;
28986 }
28987
28988 /* OpenMP 2.5:
28989 structured-block:
28990 statement
28991
28992 In practice, we're also interested in adding the statement to an
28993 outer node. So it is convenient if we work around the fact that
28994 cp_parser_statement calls add_stmt. */
28995
28996 static unsigned
28997 cp_parser_begin_omp_structured_block (cp_parser *parser)
28998 {
28999 unsigned save = parser->in_statement;
29000
29001 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29002 This preserves the "not within loop or switch" style error messages
29003 for nonsense cases like
29004 void foo() {
29005 #pragma omp single
29006 break;
29007 }
29008 */
29009 if (parser->in_statement)
29010 parser->in_statement = IN_OMP_BLOCK;
29011
29012 return save;
29013 }
29014
29015 static void
29016 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29017 {
29018 parser->in_statement = save;
29019 }
29020
29021 static tree
29022 cp_parser_omp_structured_block (cp_parser *parser)
29023 {
29024 tree stmt = begin_omp_structured_block ();
29025 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29026
29027 cp_parser_statement (parser, NULL_TREE, false, NULL);
29028
29029 cp_parser_end_omp_structured_block (parser, save);
29030 return finish_omp_structured_block (stmt);
29031 }
29032
29033 /* OpenMP 2.5:
29034 # pragma omp atomic new-line
29035 expression-stmt
29036
29037 expression-stmt:
29038 x binop= expr | x++ | ++x | x-- | --x
29039 binop:
29040 +, *, -, /, &, ^, |, <<, >>
29041
29042 where x is an lvalue expression with scalar type.
29043
29044 OpenMP 3.1:
29045 # pragma omp atomic new-line
29046 update-stmt
29047
29048 # pragma omp atomic read new-line
29049 read-stmt
29050
29051 # pragma omp atomic write new-line
29052 write-stmt
29053
29054 # pragma omp atomic update new-line
29055 update-stmt
29056
29057 # pragma omp atomic capture new-line
29058 capture-stmt
29059
29060 # pragma omp atomic capture new-line
29061 capture-block
29062
29063 read-stmt:
29064 v = x
29065 write-stmt:
29066 x = expr
29067 update-stmt:
29068 expression-stmt | x = x binop expr
29069 capture-stmt:
29070 v = expression-stmt
29071 capture-block:
29072 { v = x; update-stmt; } | { update-stmt; v = x; }
29073
29074 OpenMP 4.0:
29075 update-stmt:
29076 expression-stmt | x = x binop expr | x = expr binop x
29077 capture-stmt:
29078 v = update-stmt
29079 capture-block:
29080 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29081
29082 where x and v are lvalue expressions with scalar type. */
29083
29084 static void
29085 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29086 {
29087 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29088 tree rhs1 = NULL_TREE, orig_lhs;
29089 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29090 bool structured_block = false;
29091 bool seq_cst = false;
29092
29093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29094 {
29095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29096 const char *p = IDENTIFIER_POINTER (id);
29097
29098 if (!strcmp (p, "seq_cst"))
29099 {
29100 seq_cst = true;
29101 cp_lexer_consume_token (parser->lexer);
29102 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29103 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29104 cp_lexer_consume_token (parser->lexer);
29105 }
29106 }
29107 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29108 {
29109 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29110 const char *p = IDENTIFIER_POINTER (id);
29111
29112 if (!strcmp (p, "read"))
29113 code = OMP_ATOMIC_READ;
29114 else if (!strcmp (p, "write"))
29115 code = NOP_EXPR;
29116 else if (!strcmp (p, "update"))
29117 code = OMP_ATOMIC;
29118 else if (!strcmp (p, "capture"))
29119 code = OMP_ATOMIC_CAPTURE_NEW;
29120 else
29121 p = NULL;
29122 if (p)
29123 cp_lexer_consume_token (parser->lexer);
29124 }
29125 if (!seq_cst)
29126 {
29127 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29128 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29129 cp_lexer_consume_token (parser->lexer);
29130
29131 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29132 {
29133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29134 const char *p = IDENTIFIER_POINTER (id);
29135
29136 if (!strcmp (p, "seq_cst"))
29137 {
29138 seq_cst = true;
29139 cp_lexer_consume_token (parser->lexer);
29140 }
29141 }
29142 }
29143 cp_parser_require_pragma_eol (parser, pragma_tok);
29144
29145 switch (code)
29146 {
29147 case OMP_ATOMIC_READ:
29148 case NOP_EXPR: /* atomic write */
29149 v = cp_parser_unary_expression (parser);
29150 if (v == error_mark_node)
29151 goto saw_error;
29152 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29153 goto saw_error;
29154 if (code == NOP_EXPR)
29155 lhs = cp_parser_expression (parser);
29156 else
29157 lhs = cp_parser_unary_expression (parser);
29158 if (lhs == error_mark_node)
29159 goto saw_error;
29160 if (code == NOP_EXPR)
29161 {
29162 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29163 opcode. */
29164 code = OMP_ATOMIC;
29165 rhs = lhs;
29166 lhs = v;
29167 v = NULL_TREE;
29168 }
29169 goto done;
29170 case OMP_ATOMIC_CAPTURE_NEW:
29171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29172 {
29173 cp_lexer_consume_token (parser->lexer);
29174 structured_block = true;
29175 }
29176 else
29177 {
29178 v = cp_parser_unary_expression (parser);
29179 if (v == error_mark_node)
29180 goto saw_error;
29181 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29182 goto saw_error;
29183 }
29184 default:
29185 break;
29186 }
29187
29188 restart:
29189 lhs = cp_parser_unary_expression (parser);
29190 orig_lhs = lhs;
29191 switch (TREE_CODE (lhs))
29192 {
29193 case ERROR_MARK:
29194 goto saw_error;
29195
29196 case POSTINCREMENT_EXPR:
29197 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29198 code = OMP_ATOMIC_CAPTURE_OLD;
29199 /* FALLTHROUGH */
29200 case PREINCREMENT_EXPR:
29201 lhs = TREE_OPERAND (lhs, 0);
29202 opcode = PLUS_EXPR;
29203 rhs = integer_one_node;
29204 break;
29205
29206 case POSTDECREMENT_EXPR:
29207 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29208 code = OMP_ATOMIC_CAPTURE_OLD;
29209 /* FALLTHROUGH */
29210 case PREDECREMENT_EXPR:
29211 lhs = TREE_OPERAND (lhs, 0);
29212 opcode = MINUS_EXPR;
29213 rhs = integer_one_node;
29214 break;
29215
29216 case COMPOUND_EXPR:
29217 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29218 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29219 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29220 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29221 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29222 (TREE_OPERAND (lhs, 1), 0), 0)))
29223 == BOOLEAN_TYPE)
29224 /* Undo effects of boolean_increment for post {in,de}crement. */
29225 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29226 /* FALLTHRU */
29227 case MODIFY_EXPR:
29228 if (TREE_CODE (lhs) == MODIFY_EXPR
29229 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29230 {
29231 /* Undo effects of boolean_increment. */
29232 if (integer_onep (TREE_OPERAND (lhs, 1)))
29233 {
29234 /* This is pre or post increment. */
29235 rhs = TREE_OPERAND (lhs, 1);
29236 lhs = TREE_OPERAND (lhs, 0);
29237 opcode = NOP_EXPR;
29238 if (code == OMP_ATOMIC_CAPTURE_NEW
29239 && !structured_block
29240 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29241 code = OMP_ATOMIC_CAPTURE_OLD;
29242 break;
29243 }
29244 }
29245 /* FALLTHRU */
29246 default:
29247 switch (cp_lexer_peek_token (parser->lexer)->type)
29248 {
29249 case CPP_MULT_EQ:
29250 opcode = MULT_EXPR;
29251 break;
29252 case CPP_DIV_EQ:
29253 opcode = TRUNC_DIV_EXPR;
29254 break;
29255 case CPP_PLUS_EQ:
29256 opcode = PLUS_EXPR;
29257 break;
29258 case CPP_MINUS_EQ:
29259 opcode = MINUS_EXPR;
29260 break;
29261 case CPP_LSHIFT_EQ:
29262 opcode = LSHIFT_EXPR;
29263 break;
29264 case CPP_RSHIFT_EQ:
29265 opcode = RSHIFT_EXPR;
29266 break;
29267 case CPP_AND_EQ:
29268 opcode = BIT_AND_EXPR;
29269 break;
29270 case CPP_OR_EQ:
29271 opcode = BIT_IOR_EXPR;
29272 break;
29273 case CPP_XOR_EQ:
29274 opcode = BIT_XOR_EXPR;
29275 break;
29276 case CPP_EQ:
29277 enum cp_parser_prec oprec;
29278 cp_token *token;
29279 cp_lexer_consume_token (parser->lexer);
29280 cp_parser_parse_tentatively (parser);
29281 rhs1 = cp_parser_simple_cast_expression (parser);
29282 if (rhs1 == error_mark_node)
29283 {
29284 cp_parser_abort_tentative_parse (parser);
29285 cp_parser_simple_cast_expression (parser);
29286 goto saw_error;
29287 }
29288 token = cp_lexer_peek_token (parser->lexer);
29289 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29290 {
29291 cp_parser_abort_tentative_parse (parser);
29292 cp_parser_parse_tentatively (parser);
29293 rhs = cp_parser_binary_expression (parser, false, true,
29294 PREC_NOT_OPERATOR, NULL);
29295 if (rhs == error_mark_node)
29296 {
29297 cp_parser_abort_tentative_parse (parser);
29298 cp_parser_binary_expression (parser, false, true,
29299 PREC_NOT_OPERATOR, NULL);
29300 goto saw_error;
29301 }
29302 switch (TREE_CODE (rhs))
29303 {
29304 case MULT_EXPR:
29305 case TRUNC_DIV_EXPR:
29306 case PLUS_EXPR:
29307 case MINUS_EXPR:
29308 case LSHIFT_EXPR:
29309 case RSHIFT_EXPR:
29310 case BIT_AND_EXPR:
29311 case BIT_IOR_EXPR:
29312 case BIT_XOR_EXPR:
29313 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29314 {
29315 if (cp_parser_parse_definitely (parser))
29316 {
29317 opcode = TREE_CODE (rhs);
29318 rhs1 = TREE_OPERAND (rhs, 0);
29319 rhs = TREE_OPERAND (rhs, 1);
29320 goto stmt_done;
29321 }
29322 else
29323 goto saw_error;
29324 }
29325 break;
29326 default:
29327 break;
29328 }
29329 cp_parser_abort_tentative_parse (parser);
29330 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29331 {
29332 rhs = cp_parser_expression (parser);
29333 if (rhs == error_mark_node)
29334 goto saw_error;
29335 opcode = NOP_EXPR;
29336 rhs1 = NULL_TREE;
29337 goto stmt_done;
29338 }
29339 cp_parser_error (parser,
29340 "invalid form of %<#pragma omp atomic%>");
29341 goto saw_error;
29342 }
29343 if (!cp_parser_parse_definitely (parser))
29344 goto saw_error;
29345 switch (token->type)
29346 {
29347 case CPP_SEMICOLON:
29348 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29349 {
29350 code = OMP_ATOMIC_CAPTURE_OLD;
29351 v = lhs;
29352 lhs = NULL_TREE;
29353 lhs1 = rhs1;
29354 rhs1 = NULL_TREE;
29355 cp_lexer_consume_token (parser->lexer);
29356 goto restart;
29357 }
29358 else if (structured_block)
29359 {
29360 opcode = NOP_EXPR;
29361 rhs = rhs1;
29362 rhs1 = NULL_TREE;
29363 goto stmt_done;
29364 }
29365 cp_parser_error (parser,
29366 "invalid form of %<#pragma omp atomic%>");
29367 goto saw_error;
29368 case CPP_MULT:
29369 opcode = MULT_EXPR;
29370 break;
29371 case CPP_DIV:
29372 opcode = TRUNC_DIV_EXPR;
29373 break;
29374 case CPP_PLUS:
29375 opcode = PLUS_EXPR;
29376 break;
29377 case CPP_MINUS:
29378 opcode = MINUS_EXPR;
29379 break;
29380 case CPP_LSHIFT:
29381 opcode = LSHIFT_EXPR;
29382 break;
29383 case CPP_RSHIFT:
29384 opcode = RSHIFT_EXPR;
29385 break;
29386 case CPP_AND:
29387 opcode = BIT_AND_EXPR;
29388 break;
29389 case CPP_OR:
29390 opcode = BIT_IOR_EXPR;
29391 break;
29392 case CPP_XOR:
29393 opcode = BIT_XOR_EXPR;
29394 break;
29395 default:
29396 cp_parser_error (parser,
29397 "invalid operator for %<#pragma omp atomic%>");
29398 goto saw_error;
29399 }
29400 oprec = TOKEN_PRECEDENCE (token);
29401 gcc_assert (oprec != PREC_NOT_OPERATOR);
29402 if (commutative_tree_code (opcode))
29403 oprec = (enum cp_parser_prec) (oprec - 1);
29404 cp_lexer_consume_token (parser->lexer);
29405 rhs = cp_parser_binary_expression (parser, false, false,
29406 oprec, NULL);
29407 if (rhs == error_mark_node)
29408 goto saw_error;
29409 goto stmt_done;
29410 /* FALLTHROUGH */
29411 default:
29412 cp_parser_error (parser,
29413 "invalid operator for %<#pragma omp atomic%>");
29414 goto saw_error;
29415 }
29416 cp_lexer_consume_token (parser->lexer);
29417
29418 rhs = cp_parser_expression (parser);
29419 if (rhs == error_mark_node)
29420 goto saw_error;
29421 break;
29422 }
29423 stmt_done:
29424 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29425 {
29426 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29427 goto saw_error;
29428 v = cp_parser_unary_expression (parser);
29429 if (v == error_mark_node)
29430 goto saw_error;
29431 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29432 goto saw_error;
29433 lhs1 = cp_parser_unary_expression (parser);
29434 if (lhs1 == error_mark_node)
29435 goto saw_error;
29436 }
29437 if (structured_block)
29438 {
29439 cp_parser_consume_semicolon_at_end_of_statement (parser);
29440 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29441 }
29442 done:
29443 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29444 if (!structured_block)
29445 cp_parser_consume_semicolon_at_end_of_statement (parser);
29446 return;
29447
29448 saw_error:
29449 cp_parser_skip_to_end_of_block_or_statement (parser);
29450 if (structured_block)
29451 {
29452 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29453 cp_lexer_consume_token (parser->lexer);
29454 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29455 {
29456 cp_parser_skip_to_end_of_block_or_statement (parser);
29457 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29458 cp_lexer_consume_token (parser->lexer);
29459 }
29460 }
29461 }
29462
29463
29464 /* OpenMP 2.5:
29465 # pragma omp barrier new-line */
29466
29467 static void
29468 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29469 {
29470 cp_parser_require_pragma_eol (parser, pragma_tok);
29471 finish_omp_barrier ();
29472 }
29473
29474 /* OpenMP 2.5:
29475 # pragma omp critical [(name)] new-line
29476 structured-block */
29477
29478 static tree
29479 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29480 {
29481 tree stmt, name = NULL;
29482
29483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29484 {
29485 cp_lexer_consume_token (parser->lexer);
29486
29487 name = cp_parser_identifier (parser);
29488
29489 if (name == error_mark_node
29490 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29491 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29492 /*or_comma=*/false,
29493 /*consume_paren=*/true);
29494 if (name == error_mark_node)
29495 name = NULL;
29496 }
29497 cp_parser_require_pragma_eol (parser, pragma_tok);
29498
29499 stmt = cp_parser_omp_structured_block (parser);
29500 return c_finish_omp_critical (input_location, stmt, name);
29501 }
29502
29503 /* OpenMP 2.5:
29504 # pragma omp flush flush-vars[opt] new-line
29505
29506 flush-vars:
29507 ( variable-list ) */
29508
29509 static void
29510 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29511 {
29512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29513 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29514 cp_parser_require_pragma_eol (parser, pragma_tok);
29515
29516 finish_omp_flush ();
29517 }
29518
29519 /* Helper function, to parse omp for increment expression. */
29520
29521 static tree
29522 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29523 {
29524 tree cond = cp_parser_binary_expression (parser, false, true,
29525 PREC_NOT_OPERATOR, NULL);
29526 if (cond == error_mark_node
29527 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29528 {
29529 cp_parser_skip_to_end_of_statement (parser);
29530 return error_mark_node;
29531 }
29532
29533 switch (TREE_CODE (cond))
29534 {
29535 case GT_EXPR:
29536 case GE_EXPR:
29537 case LT_EXPR:
29538 case LE_EXPR:
29539 break;
29540 case NE_EXPR:
29541 if (code == CILK_SIMD || code == CILK_FOR)
29542 break;
29543 /* Fall through: OpenMP disallows NE_EXPR. */
29544 default:
29545 return error_mark_node;
29546 }
29547
29548 /* If decl is an iterator, preserve LHS and RHS of the relational
29549 expr until finish_omp_for. */
29550 if (decl
29551 && (type_dependent_expression_p (decl)
29552 || CLASS_TYPE_P (TREE_TYPE (decl))))
29553 return cond;
29554
29555 return build_x_binary_op (input_location, TREE_CODE (cond),
29556 TREE_OPERAND (cond, 0), ERROR_MARK,
29557 TREE_OPERAND (cond, 1), ERROR_MARK,
29558 /*overload=*/NULL, tf_warning_or_error);
29559 }
29560
29561 /* Helper function, to parse omp for increment expression. */
29562
29563 static tree
29564 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29565 {
29566 cp_token *token = cp_lexer_peek_token (parser->lexer);
29567 enum tree_code op;
29568 tree lhs, rhs;
29569 cp_id_kind idk;
29570 bool decl_first;
29571
29572 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29573 {
29574 op = (token->type == CPP_PLUS_PLUS
29575 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29576 cp_lexer_consume_token (parser->lexer);
29577 lhs = cp_parser_simple_cast_expression (parser);
29578 if (lhs != decl)
29579 return error_mark_node;
29580 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29581 }
29582
29583 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29584 if (lhs != decl)
29585 return error_mark_node;
29586
29587 token = cp_lexer_peek_token (parser->lexer);
29588 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29589 {
29590 op = (token->type == CPP_PLUS_PLUS
29591 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29592 cp_lexer_consume_token (parser->lexer);
29593 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29594 }
29595
29596 op = cp_parser_assignment_operator_opt (parser);
29597 if (op == ERROR_MARK)
29598 return error_mark_node;
29599
29600 if (op != NOP_EXPR)
29601 {
29602 rhs = cp_parser_assignment_expression (parser);
29603 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29604 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29605 }
29606
29607 lhs = cp_parser_binary_expression (parser, false, false,
29608 PREC_ADDITIVE_EXPRESSION, NULL);
29609 token = cp_lexer_peek_token (parser->lexer);
29610 decl_first = lhs == decl;
29611 if (decl_first)
29612 lhs = NULL_TREE;
29613 if (token->type != CPP_PLUS
29614 && token->type != CPP_MINUS)
29615 return error_mark_node;
29616
29617 do
29618 {
29619 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29620 cp_lexer_consume_token (parser->lexer);
29621 rhs = cp_parser_binary_expression (parser, false, false,
29622 PREC_ADDITIVE_EXPRESSION, NULL);
29623 token = cp_lexer_peek_token (parser->lexer);
29624 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29625 {
29626 if (lhs == NULL_TREE)
29627 {
29628 if (op == PLUS_EXPR)
29629 lhs = rhs;
29630 else
29631 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29632 tf_warning_or_error);
29633 }
29634 else
29635 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29636 ERROR_MARK, NULL, tf_warning_or_error);
29637 }
29638 }
29639 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29640
29641 if (!decl_first)
29642 {
29643 if (rhs != decl || op == MINUS_EXPR)
29644 return error_mark_node;
29645 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29646 }
29647 else
29648 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29649
29650 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29651 }
29652
29653 /* Parse the initialization statement of either an OpenMP for loop or
29654 a Cilk Plus for loop.
29655
29656 Return true if the resulting construct should have an
29657 OMP_CLAUSE_PRIVATE added to it. */
29658
29659 static bool
29660 cp_parser_omp_for_loop_init (cp_parser *parser,
29661 enum tree_code code,
29662 tree &this_pre_body,
29663 vec<tree, va_gc> *for_block,
29664 tree &init,
29665 tree &decl,
29666 tree &real_decl)
29667 {
29668 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29669 return false;
29670
29671 bool add_private_clause = false;
29672
29673 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29674
29675 init-expr:
29676 var = lb
29677 integer-type var = lb
29678 random-access-iterator-type var = lb
29679 pointer-type var = lb
29680 */
29681 cp_decl_specifier_seq type_specifiers;
29682
29683 /* First, try to parse as an initialized declaration. See
29684 cp_parser_condition, from whence the bulk of this is copied. */
29685
29686 cp_parser_parse_tentatively (parser);
29687 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29688 /*is_trailing_return=*/false,
29689 &type_specifiers);
29690 if (cp_parser_parse_definitely (parser))
29691 {
29692 /* If parsing a type specifier seq succeeded, then this
29693 MUST be a initialized declaration. */
29694 tree asm_specification, attributes;
29695 cp_declarator *declarator;
29696
29697 declarator = cp_parser_declarator (parser,
29698 CP_PARSER_DECLARATOR_NAMED,
29699 /*ctor_dtor_or_conv_p=*/NULL,
29700 /*parenthesized_p=*/NULL,
29701 /*member_p=*/false,
29702 /*friend_p=*/false);
29703 attributes = cp_parser_attributes_opt (parser);
29704 asm_specification = cp_parser_asm_specification_opt (parser);
29705
29706 if (declarator == cp_error_declarator)
29707 cp_parser_skip_to_end_of_statement (parser);
29708
29709 else
29710 {
29711 tree pushed_scope, auto_node;
29712
29713 decl = start_decl (declarator, &type_specifiers,
29714 SD_INITIALIZED, attributes,
29715 /*prefix_attributes=*/NULL_TREE,
29716 &pushed_scope);
29717
29718 auto_node = type_uses_auto (TREE_TYPE (decl));
29719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29720 {
29721 if (cp_lexer_next_token_is (parser->lexer,
29722 CPP_OPEN_PAREN))
29723 {
29724 if (code != CILK_SIMD && code != CILK_FOR)
29725 error ("parenthesized initialization is not allowed in "
29726 "OpenMP %<for%> loop");
29727 else
29728 error ("parenthesized initialization is "
29729 "not allowed in for-loop");
29730 }
29731 else
29732 /* Trigger an error. */
29733 cp_parser_require (parser, CPP_EQ, RT_EQ);
29734
29735 init = error_mark_node;
29736 cp_parser_skip_to_end_of_statement (parser);
29737 }
29738 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29739 || type_dependent_expression_p (decl)
29740 || auto_node)
29741 {
29742 bool is_direct_init, is_non_constant_init;
29743
29744 init = cp_parser_initializer (parser,
29745 &is_direct_init,
29746 &is_non_constant_init);
29747
29748 if (auto_node)
29749 {
29750 TREE_TYPE (decl)
29751 = do_auto_deduction (TREE_TYPE (decl), init,
29752 auto_node);
29753
29754 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29755 && !type_dependent_expression_p (decl))
29756 goto non_class;
29757 }
29758
29759 cp_finish_decl (decl, init, !is_non_constant_init,
29760 asm_specification,
29761 LOOKUP_ONLYCONVERTING);
29762 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29763 {
29764 vec_safe_push (for_block, this_pre_body);
29765 init = NULL_TREE;
29766 }
29767 else
29768 init = pop_stmt_list (this_pre_body);
29769 this_pre_body = NULL_TREE;
29770 }
29771 else
29772 {
29773 /* Consume '='. */
29774 cp_lexer_consume_token (parser->lexer);
29775 init = cp_parser_assignment_expression (parser);
29776
29777 non_class:
29778 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29779 init = error_mark_node;
29780 else
29781 cp_finish_decl (decl, NULL_TREE,
29782 /*init_const_expr_p=*/false,
29783 asm_specification,
29784 LOOKUP_ONLYCONVERTING);
29785 }
29786
29787 if (pushed_scope)
29788 pop_scope (pushed_scope);
29789 }
29790 }
29791 else
29792 {
29793 cp_id_kind idk;
29794 /* If parsing a type specifier sequence failed, then
29795 this MUST be a simple expression. */
29796 if (code == CILK_FOR)
29797 error ("%<_Cilk_for%> allows expression instead of declaration only "
29798 "in C, not in C++");
29799 cp_parser_parse_tentatively (parser);
29800 decl = cp_parser_primary_expression (parser, false, false,
29801 false, &idk);
29802 if (!cp_parser_error_occurred (parser)
29803 && decl
29804 && DECL_P (decl)
29805 && CLASS_TYPE_P (TREE_TYPE (decl)))
29806 {
29807 tree rhs;
29808
29809 cp_parser_parse_definitely (parser);
29810 cp_parser_require (parser, CPP_EQ, RT_EQ);
29811 rhs = cp_parser_assignment_expression (parser);
29812 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29813 decl, NOP_EXPR,
29814 rhs,
29815 tf_warning_or_error));
29816 add_private_clause = true;
29817 }
29818 else
29819 {
29820 decl = NULL;
29821 cp_parser_abort_tentative_parse (parser);
29822 init = cp_parser_expression (parser);
29823 if (init)
29824 {
29825 if (TREE_CODE (init) == MODIFY_EXPR
29826 || TREE_CODE (init) == MODOP_EXPR)
29827 real_decl = TREE_OPERAND (init, 0);
29828 }
29829 }
29830 }
29831 return add_private_clause;
29832 }
29833
29834 /* Parse the restricted form of the for statement allowed by OpenMP. */
29835
29836 static tree
29837 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29838 tree *cclauses)
29839 {
29840 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29841 tree real_decl, initv, condv, incrv, declv;
29842 tree this_pre_body, cl;
29843 location_t loc_first;
29844 bool collapse_err = false;
29845 int i, collapse = 1, nbraces = 0;
29846 vec<tree, va_gc> *for_block = make_tree_vector ();
29847
29848 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29849 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29850 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29851
29852 gcc_assert (collapse >= 1);
29853
29854 declv = make_tree_vec (collapse);
29855 initv = make_tree_vec (collapse);
29856 condv = make_tree_vec (collapse);
29857 incrv = make_tree_vec (collapse);
29858
29859 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29860
29861 for (i = 0; i < collapse; i++)
29862 {
29863 int bracecount = 0;
29864 bool add_private_clause = false;
29865 location_t loc;
29866
29867 if (code != CILK_FOR
29868 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29869 {
29870 cp_parser_error (parser, "for statement expected");
29871 return NULL;
29872 }
29873 if (code == CILK_FOR
29874 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29875 {
29876 cp_parser_error (parser, "_Cilk_for statement expected");
29877 return NULL;
29878 }
29879 loc = cp_lexer_consume_token (parser->lexer)->location;
29880
29881 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29882 return NULL;
29883
29884 init = decl = real_decl = NULL;
29885 this_pre_body = push_stmt_list ();
29886
29887 add_private_clause
29888 |= cp_parser_omp_for_loop_init (parser, code,
29889 this_pre_body, for_block,
29890 init, decl, real_decl);
29891
29892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29893 if (this_pre_body)
29894 {
29895 this_pre_body = pop_stmt_list (this_pre_body);
29896 if (pre_body)
29897 {
29898 tree t = pre_body;
29899 pre_body = push_stmt_list ();
29900 add_stmt (t);
29901 add_stmt (this_pre_body);
29902 pre_body = pop_stmt_list (pre_body);
29903 }
29904 else
29905 pre_body = this_pre_body;
29906 }
29907
29908 if (decl)
29909 real_decl = decl;
29910 if (cclauses != NULL
29911 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29912 && real_decl != NULL_TREE)
29913 {
29914 tree *c;
29915 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29916 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29917 && OMP_CLAUSE_DECL (*c) == real_decl)
29918 {
29919 error_at (loc, "iteration variable %qD"
29920 " should not be firstprivate", real_decl);
29921 *c = OMP_CLAUSE_CHAIN (*c);
29922 }
29923 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29924 && OMP_CLAUSE_DECL (*c) == real_decl)
29925 {
29926 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29927 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29928 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29929 OMP_CLAUSE_DECL (l) = real_decl;
29930 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29931 if (code == OMP_SIMD)
29932 {
29933 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29934 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29935 }
29936 else
29937 {
29938 OMP_CLAUSE_CHAIN (l) = clauses;
29939 clauses = l;
29940 }
29941 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29942 CP_OMP_CLAUSE_INFO (*c) = NULL;
29943 add_private_clause = false;
29944 }
29945 else
29946 {
29947 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29948 && OMP_CLAUSE_DECL (*c) == real_decl)
29949 add_private_clause = false;
29950 c = &OMP_CLAUSE_CHAIN (*c);
29951 }
29952 }
29953
29954 if (add_private_clause)
29955 {
29956 tree c;
29957 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29958 {
29959 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29960 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29961 && OMP_CLAUSE_DECL (c) == decl)
29962 break;
29963 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29964 && OMP_CLAUSE_DECL (c) == decl)
29965 error_at (loc, "iteration variable %qD "
29966 "should not be firstprivate",
29967 decl);
29968 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29969 && OMP_CLAUSE_DECL (c) == decl)
29970 error_at (loc, "iteration variable %qD should not be reduction",
29971 decl);
29972 }
29973 if (c == NULL)
29974 {
29975 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29976 OMP_CLAUSE_DECL (c) = decl;
29977 c = finish_omp_clauses (c);
29978 if (c)
29979 {
29980 OMP_CLAUSE_CHAIN (c) = clauses;
29981 clauses = c;
29982 }
29983 }
29984 }
29985
29986 cond = NULL;
29987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29988 cond = cp_parser_omp_for_cond (parser, decl, code);
29989 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29990
29991 incr = NULL;
29992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29993 {
29994 /* If decl is an iterator, preserve the operator on decl
29995 until finish_omp_for. */
29996 if (real_decl
29997 && ((processing_template_decl
29998 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29999 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30000 incr = cp_parser_omp_for_incr (parser, real_decl);
30001 else
30002 incr = cp_parser_expression (parser);
30003 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30004 SET_EXPR_LOCATION (incr, input_location);
30005 }
30006
30007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30008 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30009 /*or_comma=*/false,
30010 /*consume_paren=*/true);
30011
30012 TREE_VEC_ELT (declv, i) = decl;
30013 TREE_VEC_ELT (initv, i) = init;
30014 TREE_VEC_ELT (condv, i) = cond;
30015 TREE_VEC_ELT (incrv, i) = incr;
30016
30017 if (i == collapse - 1)
30018 break;
30019
30020 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30021 in between the collapsed for loops to be still considered perfectly
30022 nested. Hopefully the final version clarifies this.
30023 For now handle (multiple) {'s and empty statements. */
30024 cp_parser_parse_tentatively (parser);
30025 do
30026 {
30027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30028 break;
30029 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30030 {
30031 cp_lexer_consume_token (parser->lexer);
30032 bracecount++;
30033 }
30034 else if (bracecount
30035 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30036 cp_lexer_consume_token (parser->lexer);
30037 else
30038 {
30039 loc = cp_lexer_peek_token (parser->lexer)->location;
30040 error_at (loc, "not enough collapsed for loops");
30041 collapse_err = true;
30042 cp_parser_abort_tentative_parse (parser);
30043 declv = NULL_TREE;
30044 break;
30045 }
30046 }
30047 while (1);
30048
30049 if (declv)
30050 {
30051 cp_parser_parse_definitely (parser);
30052 nbraces += bracecount;
30053 }
30054 }
30055
30056 /* Note that we saved the original contents of this flag when we entered
30057 the structured block, and so we don't need to re-save it here. */
30058 if (code == CILK_SIMD || code == CILK_FOR)
30059 parser->in_statement = IN_CILK_SIMD_FOR;
30060 else
30061 parser->in_statement = IN_OMP_FOR;
30062
30063 /* Note that the grammar doesn't call for a structured block here,
30064 though the loop as a whole is a structured block. */
30065 body = push_stmt_list ();
30066 cp_parser_statement (parser, NULL_TREE, false, NULL);
30067 body = pop_stmt_list (body);
30068
30069 if (declv == NULL_TREE)
30070 ret = NULL_TREE;
30071 else
30072 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30073 pre_body, clauses);
30074
30075 while (nbraces)
30076 {
30077 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30078 {
30079 cp_lexer_consume_token (parser->lexer);
30080 nbraces--;
30081 }
30082 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30083 cp_lexer_consume_token (parser->lexer);
30084 else
30085 {
30086 if (!collapse_err)
30087 {
30088 error_at (cp_lexer_peek_token (parser->lexer)->location,
30089 "collapsed loops not perfectly nested");
30090 }
30091 collapse_err = true;
30092 cp_parser_statement_seq_opt (parser, NULL);
30093 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30094 break;
30095 }
30096 }
30097
30098 while (!for_block->is_empty ())
30099 add_stmt (pop_stmt_list (for_block->pop ()));
30100 release_tree_vector (for_block);
30101
30102 return ret;
30103 }
30104
30105 /* Helper function for OpenMP parsing, split clauses and call
30106 finish_omp_clauses on each of the set of clauses afterwards. */
30107
30108 static void
30109 cp_omp_split_clauses (location_t loc, enum tree_code code,
30110 omp_clause_mask mask, tree clauses, tree *cclauses)
30111 {
30112 int i;
30113 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30114 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30115 if (cclauses[i])
30116 cclauses[i] = finish_omp_clauses (cclauses[i]);
30117 }
30118
30119 /* OpenMP 4.0:
30120 #pragma omp simd simd-clause[optseq] new-line
30121 for-loop */
30122
30123 #define OMP_SIMD_CLAUSE_MASK \
30124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30131
30132 static tree
30133 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30134 char *p_name, omp_clause_mask mask, tree *cclauses)
30135 {
30136 tree clauses, sb, ret;
30137 unsigned int save;
30138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30139
30140 strcat (p_name, " simd");
30141 mask |= OMP_SIMD_CLAUSE_MASK;
30142 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30143
30144 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30145 cclauses == NULL);
30146 if (cclauses)
30147 {
30148 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30149 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30150 }
30151
30152 sb = begin_omp_structured_block ();
30153 save = cp_parser_begin_omp_structured_block (parser);
30154
30155 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30156
30157 cp_parser_end_omp_structured_block (parser, save);
30158 add_stmt (finish_omp_structured_block (sb));
30159
30160 return ret;
30161 }
30162
30163 /* OpenMP 2.5:
30164 #pragma omp for for-clause[optseq] new-line
30165 for-loop
30166
30167 OpenMP 4.0:
30168 #pragma omp for simd for-simd-clause[optseq] new-line
30169 for-loop */
30170
30171 #define OMP_FOR_CLAUSE_MASK \
30172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30180
30181 static tree
30182 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30183 char *p_name, omp_clause_mask mask, tree *cclauses)
30184 {
30185 tree clauses, sb, ret;
30186 unsigned int save;
30187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30188
30189 strcat (p_name, " for");
30190 mask |= OMP_FOR_CLAUSE_MASK;
30191 if (cclauses)
30192 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30193
30194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30195 {
30196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30197 const char *p = IDENTIFIER_POINTER (id);
30198
30199 if (strcmp (p, "simd") == 0)
30200 {
30201 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30202 if (cclauses == NULL)
30203 cclauses = cclauses_buf;
30204
30205 cp_lexer_consume_token (parser->lexer);
30206 if (!flag_openmp) /* flag_openmp_simd */
30207 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30208 cclauses);
30209 sb = begin_omp_structured_block ();
30210 save = cp_parser_begin_omp_structured_block (parser);
30211 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30212 cclauses);
30213 cp_parser_end_omp_structured_block (parser, save);
30214 tree body = finish_omp_structured_block (sb);
30215 if (ret == NULL)
30216 return ret;
30217 ret = make_node (OMP_FOR);
30218 TREE_TYPE (ret) = void_type_node;
30219 OMP_FOR_BODY (ret) = body;
30220 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30221 SET_EXPR_LOCATION (ret, loc);
30222 add_stmt (ret);
30223 return ret;
30224 }
30225 }
30226 if (!flag_openmp) /* flag_openmp_simd */
30227 {
30228 cp_parser_require_pragma_eol (parser, pragma_tok);
30229 return NULL_TREE;
30230 }
30231
30232 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30233 cclauses == NULL);
30234 if (cclauses)
30235 {
30236 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30237 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30238 }
30239
30240 sb = begin_omp_structured_block ();
30241 save = cp_parser_begin_omp_structured_block (parser);
30242
30243 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30244
30245 cp_parser_end_omp_structured_block (parser, save);
30246 add_stmt (finish_omp_structured_block (sb));
30247
30248 return ret;
30249 }
30250
30251 /* OpenMP 2.5:
30252 # pragma omp master new-line
30253 structured-block */
30254
30255 static tree
30256 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30257 {
30258 cp_parser_require_pragma_eol (parser, pragma_tok);
30259 return c_finish_omp_master (input_location,
30260 cp_parser_omp_structured_block (parser));
30261 }
30262
30263 /* OpenMP 2.5:
30264 # pragma omp ordered new-line
30265 structured-block */
30266
30267 static tree
30268 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30269 {
30270 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30271 cp_parser_require_pragma_eol (parser, pragma_tok);
30272 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30273 }
30274
30275 /* OpenMP 2.5:
30276
30277 section-scope:
30278 { section-sequence }
30279
30280 section-sequence:
30281 section-directive[opt] structured-block
30282 section-sequence section-directive structured-block */
30283
30284 static tree
30285 cp_parser_omp_sections_scope (cp_parser *parser)
30286 {
30287 tree stmt, substmt;
30288 bool error_suppress = false;
30289 cp_token *tok;
30290
30291 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30292 return NULL_TREE;
30293
30294 stmt = push_stmt_list ();
30295
30296 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30297 {
30298 substmt = cp_parser_omp_structured_block (parser);
30299 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30300 add_stmt (substmt);
30301 }
30302
30303 while (1)
30304 {
30305 tok = cp_lexer_peek_token (parser->lexer);
30306 if (tok->type == CPP_CLOSE_BRACE)
30307 break;
30308 if (tok->type == CPP_EOF)
30309 break;
30310
30311 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30312 {
30313 cp_lexer_consume_token (parser->lexer);
30314 cp_parser_require_pragma_eol (parser, tok);
30315 error_suppress = false;
30316 }
30317 else if (!error_suppress)
30318 {
30319 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30320 error_suppress = true;
30321 }
30322
30323 substmt = cp_parser_omp_structured_block (parser);
30324 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30325 add_stmt (substmt);
30326 }
30327 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30328
30329 substmt = pop_stmt_list (stmt);
30330
30331 stmt = make_node (OMP_SECTIONS);
30332 TREE_TYPE (stmt) = void_type_node;
30333 OMP_SECTIONS_BODY (stmt) = substmt;
30334
30335 add_stmt (stmt);
30336 return stmt;
30337 }
30338
30339 /* OpenMP 2.5:
30340 # pragma omp sections sections-clause[optseq] newline
30341 sections-scope */
30342
30343 #define OMP_SECTIONS_CLAUSE_MASK \
30344 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30349
30350 static tree
30351 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30352 char *p_name, omp_clause_mask mask, tree *cclauses)
30353 {
30354 tree clauses, ret;
30355 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30356
30357 strcat (p_name, " sections");
30358 mask |= OMP_SECTIONS_CLAUSE_MASK;
30359 if (cclauses)
30360 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30361
30362 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30363 cclauses == NULL);
30364 if (cclauses)
30365 {
30366 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30367 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30368 }
30369
30370 ret = cp_parser_omp_sections_scope (parser);
30371 if (ret)
30372 OMP_SECTIONS_CLAUSES (ret) = clauses;
30373
30374 return ret;
30375 }
30376
30377 /* OpenMP 2.5:
30378 # pragma omp parallel parallel-clause[optseq] new-line
30379 structured-block
30380 # pragma omp parallel for parallel-for-clause[optseq] new-line
30381 structured-block
30382 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30383 structured-block
30384
30385 OpenMP 4.0:
30386 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30387 structured-block */
30388
30389 #define OMP_PARALLEL_CLAUSE_MASK \
30390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30399
30400 static tree
30401 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30402 char *p_name, omp_clause_mask mask, tree *cclauses)
30403 {
30404 tree stmt, clauses, block;
30405 unsigned int save;
30406 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30407
30408 strcat (p_name, " parallel");
30409 mask |= OMP_PARALLEL_CLAUSE_MASK;
30410
30411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30412 {
30413 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30414 if (cclauses == NULL)
30415 cclauses = cclauses_buf;
30416
30417 cp_lexer_consume_token (parser->lexer);
30418 if (!flag_openmp) /* flag_openmp_simd */
30419 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30420 block = begin_omp_parallel ();
30421 save = cp_parser_begin_omp_structured_block (parser);
30422 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30423 cp_parser_end_omp_structured_block (parser, save);
30424 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30425 block);
30426 if (ret == NULL_TREE)
30427 return ret;
30428 OMP_PARALLEL_COMBINED (stmt) = 1;
30429 return stmt;
30430 }
30431 else if (cclauses)
30432 {
30433 error_at (loc, "expected %<for%> after %qs", p_name);
30434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30435 return NULL_TREE;
30436 }
30437 else if (!flag_openmp) /* flag_openmp_simd */
30438 {
30439 cp_parser_require_pragma_eol (parser, pragma_tok);
30440 return NULL_TREE;
30441 }
30442 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30443 {
30444 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30445 const char *p = IDENTIFIER_POINTER (id);
30446 if (strcmp (p, "sections") == 0)
30447 {
30448 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30449 cclauses = cclauses_buf;
30450
30451 cp_lexer_consume_token (parser->lexer);
30452 block = begin_omp_parallel ();
30453 save = cp_parser_begin_omp_structured_block (parser);
30454 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30455 cp_parser_end_omp_structured_block (parser, save);
30456 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30457 block);
30458 OMP_PARALLEL_COMBINED (stmt) = 1;
30459 return stmt;
30460 }
30461 }
30462
30463 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30464
30465 block = begin_omp_parallel ();
30466 save = cp_parser_begin_omp_structured_block (parser);
30467 cp_parser_statement (parser, NULL_TREE, false, NULL);
30468 cp_parser_end_omp_structured_block (parser, save);
30469 stmt = finish_omp_parallel (clauses, block);
30470 return stmt;
30471 }
30472
30473 /* OpenMP 2.5:
30474 # pragma omp single single-clause[optseq] new-line
30475 structured-block */
30476
30477 #define OMP_SINGLE_CLAUSE_MASK \
30478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30482
30483 static tree
30484 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30485 {
30486 tree stmt = make_node (OMP_SINGLE);
30487 TREE_TYPE (stmt) = void_type_node;
30488
30489 OMP_SINGLE_CLAUSES (stmt)
30490 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30491 "#pragma omp single", pragma_tok);
30492 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30493
30494 return add_stmt (stmt);
30495 }
30496
30497 /* OpenMP 3.0:
30498 # pragma omp task task-clause[optseq] new-line
30499 structured-block */
30500
30501 #define OMP_TASK_CLAUSE_MASK \
30502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30511
30512 static tree
30513 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30514 {
30515 tree clauses, block;
30516 unsigned int save;
30517
30518 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30519 "#pragma omp task", pragma_tok);
30520 block = begin_omp_task ();
30521 save = cp_parser_begin_omp_structured_block (parser);
30522 cp_parser_statement (parser, NULL_TREE, false, NULL);
30523 cp_parser_end_omp_structured_block (parser, save);
30524 return finish_omp_task (clauses, block);
30525 }
30526
30527 /* OpenMP 3.0:
30528 # pragma omp taskwait new-line */
30529
30530 static void
30531 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30532 {
30533 cp_parser_require_pragma_eol (parser, pragma_tok);
30534 finish_omp_taskwait ();
30535 }
30536
30537 /* OpenMP 3.1:
30538 # pragma omp taskyield new-line */
30539
30540 static void
30541 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30542 {
30543 cp_parser_require_pragma_eol (parser, pragma_tok);
30544 finish_omp_taskyield ();
30545 }
30546
30547 /* OpenMP 4.0:
30548 # pragma omp taskgroup new-line
30549 structured-block */
30550
30551 static tree
30552 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30553 {
30554 cp_parser_require_pragma_eol (parser, pragma_tok);
30555 return c_finish_omp_taskgroup (input_location,
30556 cp_parser_omp_structured_block (parser));
30557 }
30558
30559
30560 /* OpenMP 2.5:
30561 # pragma omp threadprivate (variable-list) */
30562
30563 static void
30564 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30565 {
30566 tree vars;
30567
30568 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30569 cp_parser_require_pragma_eol (parser, pragma_tok);
30570
30571 finish_omp_threadprivate (vars);
30572 }
30573
30574 /* OpenMP 4.0:
30575 # pragma omp cancel cancel-clause[optseq] new-line */
30576
30577 #define OMP_CANCEL_CLAUSE_MASK \
30578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30583
30584 static void
30585 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30586 {
30587 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30588 "#pragma omp cancel", pragma_tok);
30589 finish_omp_cancel (clauses);
30590 }
30591
30592 /* OpenMP 4.0:
30593 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30594
30595 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30600
30601 static void
30602 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30603 {
30604 tree clauses;
30605 bool point_seen = false;
30606
30607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30608 {
30609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30610 const char *p = IDENTIFIER_POINTER (id);
30611
30612 if (strcmp (p, "point") == 0)
30613 {
30614 cp_lexer_consume_token (parser->lexer);
30615 point_seen = true;
30616 }
30617 }
30618 if (!point_seen)
30619 {
30620 cp_parser_error (parser, "expected %<point%>");
30621 cp_parser_require_pragma_eol (parser, pragma_tok);
30622 return;
30623 }
30624
30625 clauses = cp_parser_omp_all_clauses (parser,
30626 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30627 "#pragma omp cancellation point",
30628 pragma_tok);
30629 finish_omp_cancellation_point (clauses);
30630 }
30631
30632 /* OpenMP 4.0:
30633 #pragma omp distribute distribute-clause[optseq] new-line
30634 for-loop */
30635
30636 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30641
30642 static tree
30643 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30644 char *p_name, omp_clause_mask mask, tree *cclauses)
30645 {
30646 tree clauses, sb, ret;
30647 unsigned int save;
30648 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30649
30650 strcat (p_name, " distribute");
30651 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30652
30653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30654 {
30655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30656 const char *p = IDENTIFIER_POINTER (id);
30657 bool simd = false;
30658 bool parallel = false;
30659
30660 if (strcmp (p, "simd") == 0)
30661 simd = true;
30662 else
30663 parallel = strcmp (p, "parallel") == 0;
30664 if (parallel || simd)
30665 {
30666 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30667 if (cclauses == NULL)
30668 cclauses = cclauses_buf;
30669 cp_lexer_consume_token (parser->lexer);
30670 if (!flag_openmp) /* flag_openmp_simd */
30671 {
30672 if (simd)
30673 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30674 cclauses);
30675 else
30676 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30677 cclauses);
30678 }
30679 sb = begin_omp_structured_block ();
30680 save = cp_parser_begin_omp_structured_block (parser);
30681 if (simd)
30682 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30683 cclauses);
30684 else
30685 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30686 cclauses);
30687 cp_parser_end_omp_structured_block (parser, save);
30688 tree body = finish_omp_structured_block (sb);
30689 if (ret == NULL)
30690 return ret;
30691 ret = make_node (OMP_DISTRIBUTE);
30692 TREE_TYPE (ret) = void_type_node;
30693 OMP_FOR_BODY (ret) = body;
30694 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30695 SET_EXPR_LOCATION (ret, loc);
30696 add_stmt (ret);
30697 return ret;
30698 }
30699 }
30700 if (!flag_openmp) /* flag_openmp_simd */
30701 {
30702 cp_parser_require_pragma_eol (parser, pragma_tok);
30703 return NULL_TREE;
30704 }
30705
30706 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30707 cclauses == NULL);
30708 if (cclauses)
30709 {
30710 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30711 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30712 }
30713
30714 sb = begin_omp_structured_block ();
30715 save = cp_parser_begin_omp_structured_block (parser);
30716
30717 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30718
30719 cp_parser_end_omp_structured_block (parser, save);
30720 add_stmt (finish_omp_structured_block (sb));
30721
30722 return ret;
30723 }
30724
30725 /* OpenMP 4.0:
30726 # pragma omp teams teams-clause[optseq] new-line
30727 structured-block */
30728
30729 #define OMP_TEAMS_CLAUSE_MASK \
30730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30737
30738 static tree
30739 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30740 char *p_name, omp_clause_mask mask, tree *cclauses)
30741 {
30742 tree clauses, sb, ret;
30743 unsigned int save;
30744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30745
30746 strcat (p_name, " teams");
30747 mask |= OMP_TEAMS_CLAUSE_MASK;
30748
30749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30750 {
30751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30752 const char *p = IDENTIFIER_POINTER (id);
30753 if (strcmp (p, "distribute") == 0)
30754 {
30755 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30756 if (cclauses == NULL)
30757 cclauses = cclauses_buf;
30758
30759 cp_lexer_consume_token (parser->lexer);
30760 if (!flag_openmp) /* flag_openmp_simd */
30761 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30762 cclauses);
30763 sb = begin_omp_structured_block ();
30764 save = cp_parser_begin_omp_structured_block (parser);
30765 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30766 cclauses);
30767 cp_parser_end_omp_structured_block (parser, save);
30768 tree body = finish_omp_structured_block (sb);
30769 if (ret == NULL)
30770 return ret;
30771 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30772 ret = make_node (OMP_TEAMS);
30773 TREE_TYPE (ret) = void_type_node;
30774 OMP_TEAMS_CLAUSES (ret) = clauses;
30775 OMP_TEAMS_BODY (ret) = body;
30776 return add_stmt (ret);
30777 }
30778 }
30779 if (!flag_openmp) /* flag_openmp_simd */
30780 {
30781 cp_parser_require_pragma_eol (parser, pragma_tok);
30782 return NULL_TREE;
30783 }
30784
30785 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30786 cclauses == NULL);
30787 if (cclauses)
30788 {
30789 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30790 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30791 }
30792
30793 tree stmt = make_node (OMP_TEAMS);
30794 TREE_TYPE (stmt) = void_type_node;
30795 OMP_TEAMS_CLAUSES (stmt) = clauses;
30796 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30797
30798 return add_stmt (stmt);
30799 }
30800
30801 /* OpenMP 4.0:
30802 # pragma omp target data target-data-clause[optseq] new-line
30803 structured-block */
30804
30805 #define OMP_TARGET_DATA_CLAUSE_MASK \
30806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30809
30810 static tree
30811 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30812 {
30813 tree stmt = make_node (OMP_TARGET_DATA);
30814 TREE_TYPE (stmt) = void_type_node;
30815
30816 OMP_TARGET_DATA_CLAUSES (stmt)
30817 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30818 "#pragma omp target data", pragma_tok);
30819 keep_next_level (true);
30820 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30821
30822 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30823 return add_stmt (stmt);
30824 }
30825
30826 /* OpenMP 4.0:
30827 # pragma omp target update target-update-clause[optseq] new-line */
30828
30829 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30834
30835 static bool
30836 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30837 enum pragma_context context)
30838 {
30839 if (context == pragma_stmt)
30840 {
30841 error_at (pragma_tok->location,
30842 "%<#pragma omp target update%> may only be "
30843 "used in compound statements");
30844 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30845 return false;
30846 }
30847
30848 tree clauses
30849 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30850 "#pragma omp target update", pragma_tok);
30851 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30852 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30853 {
30854 error_at (pragma_tok->location,
30855 "%<#pragma omp target update must contain at least one "
30856 "%<from%> or %<to%> clauses");
30857 return false;
30858 }
30859
30860 tree stmt = make_node (OMP_TARGET_UPDATE);
30861 TREE_TYPE (stmt) = void_type_node;
30862 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30863 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30864 add_stmt (stmt);
30865 return false;
30866 }
30867
30868 /* OpenMP 4.0:
30869 # pragma omp target target-clause[optseq] new-line
30870 structured-block */
30871
30872 #define OMP_TARGET_CLAUSE_MASK \
30873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30876
30877 static bool
30878 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30879 enum pragma_context context)
30880 {
30881 if (context != pragma_stmt && context != pragma_compound)
30882 {
30883 cp_parser_error (parser, "expected declaration specifiers");
30884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30885 return false;
30886 }
30887
30888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30889 {
30890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30891 const char *p = IDENTIFIER_POINTER (id);
30892
30893 if (strcmp (p, "teams") == 0)
30894 {
30895 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30896 char p_name[sizeof ("#pragma omp target teams distribute "
30897 "parallel for simd")];
30898
30899 cp_lexer_consume_token (parser->lexer);
30900 strcpy (p_name, "#pragma omp target");
30901 if (!flag_openmp) /* flag_openmp_simd */
30902 {
30903 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30904 OMP_TARGET_CLAUSE_MASK,
30905 cclauses);
30906 return stmt != NULL_TREE;
30907 }
30908 keep_next_level (true);
30909 tree sb = begin_omp_structured_block ();
30910 unsigned save = cp_parser_begin_omp_structured_block (parser);
30911 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30912 OMP_TARGET_CLAUSE_MASK, cclauses);
30913 cp_parser_end_omp_structured_block (parser, save);
30914 tree body = finish_omp_structured_block (sb);
30915 if (ret == NULL_TREE)
30916 return false;
30917 tree stmt = make_node (OMP_TARGET);
30918 TREE_TYPE (stmt) = void_type_node;
30919 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30920 OMP_TARGET_BODY (stmt) = body;
30921 add_stmt (stmt);
30922 return true;
30923 }
30924 else if (!flag_openmp) /* flag_openmp_simd */
30925 {
30926 cp_parser_require_pragma_eol (parser, pragma_tok);
30927 return false;
30928 }
30929 else if (strcmp (p, "data") == 0)
30930 {
30931 cp_lexer_consume_token (parser->lexer);
30932 cp_parser_omp_target_data (parser, pragma_tok);
30933 return true;
30934 }
30935 else if (strcmp (p, "update") == 0)
30936 {
30937 cp_lexer_consume_token (parser->lexer);
30938 return cp_parser_omp_target_update (parser, pragma_tok, context);
30939 }
30940 }
30941
30942 tree stmt = make_node (OMP_TARGET);
30943 TREE_TYPE (stmt) = void_type_node;
30944
30945 OMP_TARGET_CLAUSES (stmt)
30946 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30947 "#pragma omp target", pragma_tok);
30948 keep_next_level (true);
30949 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30950
30951 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30952 add_stmt (stmt);
30953 return true;
30954 }
30955
30956 /* OpenMP 4.0:
30957 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30958
30959 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30966
30967 static void
30968 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30969 enum pragma_context context)
30970 {
30971 bool first_p = parser->omp_declare_simd == NULL;
30972 cp_omp_declare_simd_data data;
30973 if (first_p)
30974 {
30975 data.error_seen = false;
30976 data.fndecl_seen = false;
30977 data.tokens = vNULL;
30978 parser->omp_declare_simd = &data;
30979 }
30980 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30981 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30982 cp_lexer_consume_token (parser->lexer);
30983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30984 parser->omp_declare_simd->error_seen = true;
30985 cp_parser_require_pragma_eol (parser, pragma_tok);
30986 struct cp_token_cache *cp
30987 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30988 parser->omp_declare_simd->tokens.safe_push (cp);
30989 if (first_p)
30990 {
30991 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30992 cp_parser_pragma (parser, context);
30993 switch (context)
30994 {
30995 case pragma_external:
30996 cp_parser_declaration (parser);
30997 break;
30998 case pragma_member:
30999 cp_parser_member_declaration (parser);
31000 break;
31001 case pragma_objc_icode:
31002 cp_parser_block_declaration (parser, /*statement_p=*/false);
31003 break;
31004 default:
31005 cp_parser_declaration_statement (parser);
31006 break;
31007 }
31008 if (parser->omp_declare_simd
31009 && !parser->omp_declare_simd->error_seen
31010 && !parser->omp_declare_simd->fndecl_seen)
31011 error_at (pragma_tok->location,
31012 "%<#pragma omp declare simd%> not immediately followed by "
31013 "function declaration or definition");
31014 data.tokens.release ();
31015 parser->omp_declare_simd = NULL;
31016 }
31017 }
31018
31019 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31020 This function is modelled similar to the late parsing of omp declare
31021 simd. */
31022
31023 static tree
31024 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31025 {
31026 struct cp_token_cache *ce;
31027 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31028 int ii = 0;
31029
31030 if (parser->omp_declare_simd != NULL)
31031 {
31032 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31033 " marked as a Cilk Plus SIMD-enabled function");
31034 XDELETE (parser->cilk_simd_fn_info);
31035 parser->cilk_simd_fn_info = NULL;
31036 return attrs;
31037 }
31038 if (!info->error_seen && info->fndecl_seen)
31039 {
31040 error ("vector attribute not immediately followed by a single function"
31041 " declaration or definition");
31042 info->error_seen = true;
31043 }
31044 if (info->error_seen)
31045 return attrs;
31046
31047 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31048 {
31049 tree c, cl;
31050
31051 cp_parser_push_lexer_for_tokens (parser, ce);
31052 parser->lexer->in_pragma = true;
31053 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31054 "SIMD-enabled functions attribute",
31055 NULL);
31056 cp_parser_pop_lexer (parser);
31057 if (cl)
31058 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31059
31060 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31061 TREE_CHAIN (c) = attrs;
31062 attrs = c;
31063
31064 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31065 TREE_CHAIN (c) = attrs;
31066 if (processing_template_decl)
31067 ATTR_IS_DEPENDENT (c) = 1;
31068 attrs = c;
31069 }
31070 info->fndecl_seen = true;
31071 XDELETE (parser->cilk_simd_fn_info);
31072 parser->cilk_simd_fn_info = NULL;
31073 return attrs;
31074 }
31075
31076 /* Finalize #pragma omp declare simd clauses after direct declarator has
31077 been parsed, and put that into "omp declare simd" attribute. */
31078
31079 static tree
31080 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31081 {
31082 struct cp_token_cache *ce;
31083 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31084 int i;
31085
31086 if (!data->error_seen && data->fndecl_seen)
31087 {
31088 error ("%<#pragma omp declare simd%> not immediately followed by "
31089 "a single function declaration or definition");
31090 data->error_seen = true;
31091 return attrs;
31092 }
31093 if (data->error_seen)
31094 return attrs;
31095
31096 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31097 {
31098 tree c, cl;
31099
31100 cp_parser_push_lexer_for_tokens (parser, ce);
31101 parser->lexer->in_pragma = true;
31102 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31103 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31104 cp_lexer_consume_token (parser->lexer);
31105 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31106 "#pragma omp declare simd", pragma_tok);
31107 cp_parser_pop_lexer (parser);
31108 if (cl)
31109 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31110 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31111 TREE_CHAIN (c) = attrs;
31112 if (processing_template_decl)
31113 ATTR_IS_DEPENDENT (c) = 1;
31114 attrs = c;
31115 }
31116
31117 data->fndecl_seen = true;
31118 return attrs;
31119 }
31120
31121
31122 /* OpenMP 4.0:
31123 # pragma omp declare target new-line
31124 declarations and definitions
31125 # pragma omp end declare target new-line */
31126
31127 static void
31128 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31129 {
31130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31131 scope_chain->omp_declare_target_attribute++;
31132 }
31133
31134 static void
31135 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31136 {
31137 const char *p = "";
31138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31139 {
31140 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31141 p = IDENTIFIER_POINTER (id);
31142 }
31143 if (strcmp (p, "declare") == 0)
31144 {
31145 cp_lexer_consume_token (parser->lexer);
31146 p = "";
31147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31148 {
31149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31150 p = IDENTIFIER_POINTER (id);
31151 }
31152 if (strcmp (p, "target") == 0)
31153 cp_lexer_consume_token (parser->lexer);
31154 else
31155 {
31156 cp_parser_error (parser, "expected %<target%>");
31157 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31158 return;
31159 }
31160 }
31161 else
31162 {
31163 cp_parser_error (parser, "expected %<declare%>");
31164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31165 return;
31166 }
31167 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31168 if (!scope_chain->omp_declare_target_attribute)
31169 error_at (pragma_tok->location,
31170 "%<#pragma omp end declare target%> without corresponding "
31171 "%<#pragma omp declare target%>");
31172 else
31173 scope_chain->omp_declare_target_attribute--;
31174 }
31175
31176 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31177 expression and optional initializer clause of
31178 #pragma omp declare reduction. We store the expression(s) as
31179 either 3, 6 or 7 special statements inside of the artificial function's
31180 body. The first two statements are DECL_EXPRs for the artificial
31181 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31182 expression that uses those variables.
31183 If there was any INITIALIZER clause, this is followed by further statements,
31184 the fourth and fifth statements are DECL_EXPRs for the artificial
31185 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31186 constructor variant (first token after open paren is not omp_priv),
31187 then the sixth statement is a statement with the function call expression
31188 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31189 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31190 to initialize the OMP_PRIV artificial variable and there is seventh
31191 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31192
31193 static bool
31194 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31195 {
31196 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31197 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31198 type = TREE_TYPE (type);
31199 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31200 DECL_ARTIFICIAL (omp_out) = 1;
31201 pushdecl (omp_out);
31202 add_decl_expr (omp_out);
31203 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31204 DECL_ARTIFICIAL (omp_in) = 1;
31205 pushdecl (omp_in);
31206 add_decl_expr (omp_in);
31207 tree combiner;
31208 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31209
31210 keep_next_level (true);
31211 tree block = begin_omp_structured_block ();
31212 combiner = cp_parser_expression (parser);
31213 finish_expr_stmt (combiner);
31214 block = finish_omp_structured_block (block);
31215 add_stmt (block);
31216
31217 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31218 return false;
31219
31220 const char *p = "";
31221 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31222 {
31223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31224 p = IDENTIFIER_POINTER (id);
31225 }
31226
31227 if (strcmp (p, "initializer") == 0)
31228 {
31229 cp_lexer_consume_token (parser->lexer);
31230 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31231 return false;
31232
31233 p = "";
31234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31235 {
31236 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31237 p = IDENTIFIER_POINTER (id);
31238 }
31239
31240 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31241 DECL_ARTIFICIAL (omp_priv) = 1;
31242 pushdecl (omp_priv);
31243 add_decl_expr (omp_priv);
31244 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31245 DECL_ARTIFICIAL (omp_orig) = 1;
31246 pushdecl (omp_orig);
31247 add_decl_expr (omp_orig);
31248
31249 keep_next_level (true);
31250 block = begin_omp_structured_block ();
31251
31252 bool ctor = false;
31253 if (strcmp (p, "omp_priv") == 0)
31254 {
31255 bool is_direct_init, is_non_constant_init;
31256 ctor = true;
31257 cp_lexer_consume_token (parser->lexer);
31258 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31259 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31260 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31261 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31262 == CPP_CLOSE_PAREN
31263 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31264 == CPP_CLOSE_PAREN))
31265 {
31266 finish_omp_structured_block (block);
31267 error ("invalid initializer clause");
31268 return false;
31269 }
31270 initializer = cp_parser_initializer (parser, &is_direct_init,
31271 &is_non_constant_init);
31272 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31273 NULL_TREE, LOOKUP_ONLYCONVERTING);
31274 }
31275 else
31276 {
31277 cp_parser_parse_tentatively (parser);
31278 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31279 /*check_dependency_p=*/true,
31280 /*template_p=*/NULL,
31281 /*declarator_p=*/false,
31282 /*optional_p=*/false);
31283 vec<tree, va_gc> *args;
31284 if (fn_name == error_mark_node
31285 || cp_parser_error_occurred (parser)
31286 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31287 || ((args = cp_parser_parenthesized_expression_list
31288 (parser, non_attr, /*cast_p=*/false,
31289 /*allow_expansion_p=*/true,
31290 /*non_constant_p=*/NULL)),
31291 cp_parser_error_occurred (parser)))
31292 {
31293 finish_omp_structured_block (block);
31294 cp_parser_abort_tentative_parse (parser);
31295 cp_parser_error (parser, "expected id-expression (arguments)");
31296 return false;
31297 }
31298 unsigned int i;
31299 tree arg;
31300 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31301 if (arg == omp_priv
31302 || (TREE_CODE (arg) == ADDR_EXPR
31303 && TREE_OPERAND (arg, 0) == omp_priv))
31304 break;
31305 cp_parser_abort_tentative_parse (parser);
31306 if (arg == NULL_TREE)
31307 error ("one of the initializer call arguments should be %<omp_priv%>"
31308 " or %<&omp_priv%>");
31309 initializer = cp_parser_postfix_expression (parser, false, false, false,
31310 false, NULL);
31311 finish_expr_stmt (initializer);
31312 }
31313
31314 block = finish_omp_structured_block (block);
31315 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31316 add_stmt (block);
31317
31318 if (ctor)
31319 add_decl_expr (omp_orig);
31320
31321 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31322 return false;
31323 }
31324
31325 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31326 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31327
31328 return true;
31329 }
31330
31331 /* OpenMP 4.0
31332 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31333 initializer-clause[opt] new-line
31334
31335 initializer-clause:
31336 initializer (omp_priv initializer)
31337 initializer (function-name (argument-list)) */
31338
31339 static void
31340 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31341 enum pragma_context)
31342 {
31343 auto_vec<tree> types;
31344 enum tree_code reduc_code = ERROR_MARK;
31345 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31346 unsigned int i;
31347 cp_token *first_token;
31348 cp_token_cache *cp;
31349 int errs;
31350 void *p;
31351
31352 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31353 p = obstack_alloc (&declarator_obstack, 0);
31354
31355 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31356 goto fail;
31357
31358 switch (cp_lexer_peek_token (parser->lexer)->type)
31359 {
31360 case CPP_PLUS:
31361 reduc_code = PLUS_EXPR;
31362 break;
31363 case CPP_MULT:
31364 reduc_code = MULT_EXPR;
31365 break;
31366 case CPP_MINUS:
31367 reduc_code = MINUS_EXPR;
31368 break;
31369 case CPP_AND:
31370 reduc_code = BIT_AND_EXPR;
31371 break;
31372 case CPP_XOR:
31373 reduc_code = BIT_XOR_EXPR;
31374 break;
31375 case CPP_OR:
31376 reduc_code = BIT_IOR_EXPR;
31377 break;
31378 case CPP_AND_AND:
31379 reduc_code = TRUTH_ANDIF_EXPR;
31380 break;
31381 case CPP_OR_OR:
31382 reduc_code = TRUTH_ORIF_EXPR;
31383 break;
31384 case CPP_NAME:
31385 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31386 break;
31387 default:
31388 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31389 "%<|%>, %<&&%>, %<||%> or identifier");
31390 goto fail;
31391 }
31392
31393 if (reduc_code != ERROR_MARK)
31394 cp_lexer_consume_token (parser->lexer);
31395
31396 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31397 if (reduc_id == error_mark_node)
31398 goto fail;
31399
31400 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31401 goto fail;
31402
31403 /* Types may not be defined in declare reduction type list. */
31404 const char *saved_message;
31405 saved_message = parser->type_definition_forbidden_message;
31406 parser->type_definition_forbidden_message
31407 = G_("types may not be defined in declare reduction type list");
31408 bool saved_colon_corrects_to_scope_p;
31409 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31410 parser->colon_corrects_to_scope_p = false;
31411 bool saved_colon_doesnt_start_class_def_p;
31412 saved_colon_doesnt_start_class_def_p
31413 = parser->colon_doesnt_start_class_def_p;
31414 parser->colon_doesnt_start_class_def_p = true;
31415
31416 while (true)
31417 {
31418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31419 type = cp_parser_type_id (parser);
31420 if (type == error_mark_node)
31421 ;
31422 else if (ARITHMETIC_TYPE_P (type)
31423 && (orig_reduc_id == NULL_TREE
31424 || (TREE_CODE (type) != COMPLEX_TYPE
31425 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31426 "min") == 0
31427 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31428 "max") == 0))))
31429 error_at (loc, "predeclared arithmetic type %qT in "
31430 "%<#pragma omp declare reduction%>", type);
31431 else if (TREE_CODE (type) == FUNCTION_TYPE
31432 || TREE_CODE (type) == METHOD_TYPE
31433 || TREE_CODE (type) == ARRAY_TYPE)
31434 error_at (loc, "function or array type %qT in "
31435 "%<#pragma omp declare reduction%>", type);
31436 else if (TREE_CODE (type) == REFERENCE_TYPE)
31437 error_at (loc, "reference type %qT in "
31438 "%<#pragma omp declare reduction%>", type);
31439 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31440 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31441 "%<#pragma omp declare reduction%>", type);
31442 else
31443 types.safe_push (type);
31444
31445 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31446 cp_lexer_consume_token (parser->lexer);
31447 else
31448 break;
31449 }
31450
31451 /* Restore the saved message. */
31452 parser->type_definition_forbidden_message = saved_message;
31453 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31454 parser->colon_doesnt_start_class_def_p
31455 = saved_colon_doesnt_start_class_def_p;
31456
31457 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31458 || types.is_empty ())
31459 {
31460 fail:
31461 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31462 goto done;
31463 }
31464
31465 first_token = cp_lexer_peek_token (parser->lexer);
31466 cp = NULL;
31467 errs = errorcount;
31468 FOR_EACH_VEC_ELT (types, i, type)
31469 {
31470 tree fntype
31471 = build_function_type_list (void_type_node,
31472 cp_build_reference_type (type, false),
31473 NULL_TREE);
31474 tree this_reduc_id = reduc_id;
31475 if (!dependent_type_p (type))
31476 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31477 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31478 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31479 DECL_ARTIFICIAL (fndecl) = 1;
31480 DECL_EXTERNAL (fndecl) = 1;
31481 DECL_DECLARED_INLINE_P (fndecl) = 1;
31482 DECL_IGNORED_P (fndecl) = 1;
31483 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31484 DECL_ATTRIBUTES (fndecl)
31485 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31486 DECL_ATTRIBUTES (fndecl));
31487 if (processing_template_decl)
31488 fndecl = push_template_decl (fndecl);
31489 bool block_scope = false;
31490 tree block = NULL_TREE;
31491 if (current_function_decl)
31492 {
31493 block_scope = true;
31494 DECL_CONTEXT (fndecl) = global_namespace;
31495 if (!processing_template_decl)
31496 pushdecl (fndecl);
31497 }
31498 else if (current_class_type)
31499 {
31500 if (cp == NULL)
31501 {
31502 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31503 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31504 cp_lexer_consume_token (parser->lexer);
31505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31506 goto fail;
31507 cp = cp_token_cache_new (first_token,
31508 cp_lexer_peek_nth_token (parser->lexer,
31509 2));
31510 }
31511 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31512 finish_member_declaration (fndecl);
31513 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31514 DECL_PENDING_INLINE_P (fndecl) = 1;
31515 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31516 continue;
31517 }
31518 else
31519 {
31520 DECL_CONTEXT (fndecl) = current_namespace;
31521 pushdecl (fndecl);
31522 }
31523 if (!block_scope)
31524 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31525 else
31526 block = begin_omp_structured_block ();
31527 if (cp)
31528 {
31529 cp_parser_push_lexer_for_tokens (parser, cp);
31530 parser->lexer->in_pragma = true;
31531 }
31532 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31533 {
31534 if (!block_scope)
31535 finish_function (0);
31536 else
31537 DECL_CONTEXT (fndecl) = current_function_decl;
31538 if (cp)
31539 cp_parser_pop_lexer (parser);
31540 goto fail;
31541 }
31542 if (cp)
31543 cp_parser_pop_lexer (parser);
31544 if (!block_scope)
31545 finish_function (0);
31546 else
31547 {
31548 DECL_CONTEXT (fndecl) = current_function_decl;
31549 block = finish_omp_structured_block (block);
31550 if (TREE_CODE (block) == BIND_EXPR)
31551 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31552 else if (TREE_CODE (block) == STATEMENT_LIST)
31553 DECL_SAVED_TREE (fndecl) = block;
31554 if (processing_template_decl)
31555 add_decl_expr (fndecl);
31556 }
31557 cp_check_omp_declare_reduction (fndecl);
31558 if (cp == NULL && types.length () > 1)
31559 cp = cp_token_cache_new (first_token,
31560 cp_lexer_peek_nth_token (parser->lexer, 2));
31561 if (errs != errorcount)
31562 break;
31563 }
31564
31565 cp_parser_require_pragma_eol (parser, pragma_tok);
31566
31567 done:
31568 /* Free any declarators allocated. */
31569 obstack_free (&declarator_obstack, p);
31570 }
31571
31572 /* OpenMP 4.0
31573 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31574 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31575 initializer-clause[opt] new-line
31576 #pragma omp declare target new-line */
31577
31578 static void
31579 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31580 enum pragma_context context)
31581 {
31582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31583 {
31584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31585 const char *p = IDENTIFIER_POINTER (id);
31586
31587 if (strcmp (p, "simd") == 0)
31588 {
31589 cp_lexer_consume_token (parser->lexer);
31590 cp_parser_omp_declare_simd (parser, pragma_tok,
31591 context);
31592 return;
31593 }
31594 cp_ensure_no_omp_declare_simd (parser);
31595 if (strcmp (p, "reduction") == 0)
31596 {
31597 cp_lexer_consume_token (parser->lexer);
31598 cp_parser_omp_declare_reduction (parser, pragma_tok,
31599 context);
31600 return;
31601 }
31602 if (!flag_openmp) /* flag_openmp_simd */
31603 {
31604 cp_parser_require_pragma_eol (parser, pragma_tok);
31605 return;
31606 }
31607 if (strcmp (p, "target") == 0)
31608 {
31609 cp_lexer_consume_token (parser->lexer);
31610 cp_parser_omp_declare_target (parser, pragma_tok);
31611 return;
31612 }
31613 }
31614 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31615 "or %<target%>");
31616 cp_parser_require_pragma_eol (parser, pragma_tok);
31617 }
31618
31619 /* Main entry point to OpenMP statement pragmas. */
31620
31621 static void
31622 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31623 {
31624 tree stmt;
31625 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31626 omp_clause_mask mask (0);
31627
31628 switch (pragma_tok->pragma_kind)
31629 {
31630 case PRAGMA_OMP_ATOMIC:
31631 cp_parser_omp_atomic (parser, pragma_tok);
31632 return;
31633 case PRAGMA_OMP_CRITICAL:
31634 stmt = cp_parser_omp_critical (parser, pragma_tok);
31635 break;
31636 case PRAGMA_OMP_DISTRIBUTE:
31637 strcpy (p_name, "#pragma omp");
31638 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31639 break;
31640 case PRAGMA_OMP_FOR:
31641 strcpy (p_name, "#pragma omp");
31642 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31643 break;
31644 case PRAGMA_OMP_MASTER:
31645 stmt = cp_parser_omp_master (parser, pragma_tok);
31646 break;
31647 case PRAGMA_OMP_ORDERED:
31648 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31649 break;
31650 case PRAGMA_OMP_PARALLEL:
31651 strcpy (p_name, "#pragma omp");
31652 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31653 break;
31654 case PRAGMA_OMP_SECTIONS:
31655 strcpy (p_name, "#pragma omp");
31656 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31657 break;
31658 case PRAGMA_OMP_SIMD:
31659 strcpy (p_name, "#pragma omp");
31660 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31661 break;
31662 case PRAGMA_OMP_SINGLE:
31663 stmt = cp_parser_omp_single (parser, pragma_tok);
31664 break;
31665 case PRAGMA_OMP_TASK:
31666 stmt = cp_parser_omp_task (parser, pragma_tok);
31667 break;
31668 case PRAGMA_OMP_TASKGROUP:
31669 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31670 break;
31671 case PRAGMA_OMP_TEAMS:
31672 strcpy (p_name, "#pragma omp");
31673 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31674 break;
31675 default:
31676 gcc_unreachable ();
31677 }
31678
31679 if (stmt)
31680 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31681 }
31682 \f
31683 /* Transactional Memory parsing routines. */
31684
31685 /* Parse a transaction attribute.
31686
31687 txn-attribute:
31688 attribute
31689 [ [ identifier ] ]
31690
31691 ??? Simplify this when C++0x bracket attributes are
31692 implemented properly. */
31693
31694 static tree
31695 cp_parser_txn_attribute_opt (cp_parser *parser)
31696 {
31697 cp_token *token;
31698 tree attr_name, attr = NULL;
31699
31700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31701 return cp_parser_attributes_opt (parser);
31702
31703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31704 return NULL_TREE;
31705 cp_lexer_consume_token (parser->lexer);
31706 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31707 goto error1;
31708
31709 token = cp_lexer_peek_token (parser->lexer);
31710 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31711 {
31712 token = cp_lexer_consume_token (parser->lexer);
31713
31714 attr_name = (token->type == CPP_KEYWORD
31715 /* For keywords, use the canonical spelling,
31716 not the parsed identifier. */
31717 ? ridpointers[(int) token->keyword]
31718 : token->u.value);
31719 attr = build_tree_list (attr_name, NULL_TREE);
31720 }
31721 else
31722 cp_parser_error (parser, "expected identifier");
31723
31724 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31725 error1:
31726 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31727 return attr;
31728 }
31729
31730 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31731
31732 transaction-statement:
31733 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31734 compound-statement
31735 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31736 */
31737
31738 static tree
31739 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31740 {
31741 unsigned char old_in = parser->in_transaction;
31742 unsigned char this_in = 1, new_in;
31743 cp_token *token;
31744 tree stmt, attrs, noex;
31745
31746 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31747 || keyword == RID_TRANSACTION_RELAXED);
31748 token = cp_parser_require_keyword (parser, keyword,
31749 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31750 : RT_TRANSACTION_RELAXED));
31751 gcc_assert (token != NULL);
31752
31753 if (keyword == RID_TRANSACTION_RELAXED)
31754 this_in |= TM_STMT_ATTR_RELAXED;
31755 else
31756 {
31757 attrs = cp_parser_txn_attribute_opt (parser);
31758 if (attrs)
31759 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31760 }
31761
31762 /* Parse a noexcept specification. */
31763 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31764
31765 /* Keep track if we're in the lexical scope of an outer transaction. */
31766 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31767
31768 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31769
31770 parser->in_transaction = new_in;
31771 cp_parser_compound_statement (parser, NULL, false, false);
31772 parser->in_transaction = old_in;
31773
31774 finish_transaction_stmt (stmt, NULL, this_in, noex);
31775
31776 return stmt;
31777 }
31778
31779 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31780
31781 transaction-expression:
31782 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31783 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31784 */
31785
31786 static tree
31787 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31788 {
31789 unsigned char old_in = parser->in_transaction;
31790 unsigned char this_in = 1;
31791 cp_token *token;
31792 tree expr, noex;
31793 bool noex_expr;
31794
31795 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31796 || keyword == RID_TRANSACTION_RELAXED);
31797
31798 if (!flag_tm)
31799 error (keyword == RID_TRANSACTION_RELAXED
31800 ? G_("%<__transaction_relaxed%> without transactional memory "
31801 "support enabled")
31802 : G_("%<__transaction_atomic%> without transactional memory "
31803 "support enabled"));
31804
31805 token = cp_parser_require_keyword (parser, keyword,
31806 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31807 : RT_TRANSACTION_RELAXED));
31808 gcc_assert (token != NULL);
31809
31810 if (keyword == RID_TRANSACTION_RELAXED)
31811 this_in |= TM_STMT_ATTR_RELAXED;
31812
31813 /* Set this early. This might mean that we allow transaction_cancel in
31814 an expression that we find out later actually has to be a constexpr.
31815 However, we expect that cxx_constant_value will be able to deal with
31816 this; also, if the noexcept has no constexpr, then what we parse next
31817 really is a transaction's body. */
31818 parser->in_transaction = this_in;
31819
31820 /* Parse a noexcept specification. */
31821 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31822 true);
31823
31824 if (!noex || !noex_expr
31825 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31826 {
31827 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31828
31829 expr = cp_parser_expression (parser);
31830 expr = finish_parenthesized_expr (expr);
31831
31832 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31833 }
31834 else
31835 {
31836 /* The only expression that is available got parsed for the noexcept
31837 already. noexcept is true then. */
31838 expr = noex;
31839 noex = boolean_true_node;
31840 }
31841
31842 expr = build_transaction_expr (token->location, expr, this_in, noex);
31843 parser->in_transaction = old_in;
31844
31845 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31846 return error_mark_node;
31847
31848 return (flag_tm ? expr : error_mark_node);
31849 }
31850
31851 /* Parse a function-transaction-block.
31852
31853 function-transaction-block:
31854 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31855 function-body
31856 __transaction_atomic txn-attribute[opt] function-try-block
31857 __transaction_relaxed ctor-initializer[opt] function-body
31858 __transaction_relaxed function-try-block
31859 */
31860
31861 static bool
31862 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31863 {
31864 unsigned char old_in = parser->in_transaction;
31865 unsigned char new_in = 1;
31866 tree compound_stmt, stmt, attrs;
31867 bool ctor_initializer_p;
31868 cp_token *token;
31869
31870 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31871 || keyword == RID_TRANSACTION_RELAXED);
31872 token = cp_parser_require_keyword (parser, keyword,
31873 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31874 : RT_TRANSACTION_RELAXED));
31875 gcc_assert (token != NULL);
31876
31877 if (keyword == RID_TRANSACTION_RELAXED)
31878 new_in |= TM_STMT_ATTR_RELAXED;
31879 else
31880 {
31881 attrs = cp_parser_txn_attribute_opt (parser);
31882 if (attrs)
31883 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31884 }
31885
31886 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31887
31888 parser->in_transaction = new_in;
31889
31890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31891 ctor_initializer_p = cp_parser_function_try_block (parser);
31892 else
31893 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31894 (parser, /*in_function_try_block=*/false);
31895
31896 parser->in_transaction = old_in;
31897
31898 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31899
31900 return ctor_initializer_p;
31901 }
31902
31903 /* Parse a __transaction_cancel statement.
31904
31905 cancel-statement:
31906 __transaction_cancel txn-attribute[opt] ;
31907 __transaction_cancel txn-attribute[opt] throw-expression ;
31908
31909 ??? Cancel and throw is not yet implemented. */
31910
31911 static tree
31912 cp_parser_transaction_cancel (cp_parser *parser)
31913 {
31914 cp_token *token;
31915 bool is_outer = false;
31916 tree stmt, attrs;
31917
31918 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31919 RT_TRANSACTION_CANCEL);
31920 gcc_assert (token != NULL);
31921
31922 attrs = cp_parser_txn_attribute_opt (parser);
31923 if (attrs)
31924 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31925
31926 /* ??? Parse cancel-and-throw here. */
31927
31928 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31929
31930 if (!flag_tm)
31931 {
31932 error_at (token->location, "%<__transaction_cancel%> without "
31933 "transactional memory support enabled");
31934 return error_mark_node;
31935 }
31936 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31937 {
31938 error_at (token->location, "%<__transaction_cancel%> within a "
31939 "%<__transaction_relaxed%>");
31940 return error_mark_node;
31941 }
31942 else if (is_outer)
31943 {
31944 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31945 && !is_tm_may_cancel_outer (current_function_decl))
31946 {
31947 error_at (token->location, "outer %<__transaction_cancel%> not "
31948 "within outer %<__transaction_atomic%>");
31949 error_at (token->location,
31950 " or a %<transaction_may_cancel_outer%> function");
31951 return error_mark_node;
31952 }
31953 }
31954 else if (parser->in_transaction == 0)
31955 {
31956 error_at (token->location, "%<__transaction_cancel%> not within "
31957 "%<__transaction_atomic%>");
31958 return error_mark_node;
31959 }
31960
31961 stmt = build_tm_abort_call (token->location, is_outer);
31962 add_stmt (stmt);
31963
31964 return stmt;
31965 }
31966 \f
31967 /* The parser. */
31968
31969 static GTY (()) cp_parser *the_parser;
31970
31971 \f
31972 /* Special handling for the first token or line in the file. The first
31973 thing in the file might be #pragma GCC pch_preprocess, which loads a
31974 PCH file, which is a GC collection point. So we need to handle this
31975 first pragma without benefit of an existing lexer structure.
31976
31977 Always returns one token to the caller in *FIRST_TOKEN. This is
31978 either the true first token of the file, or the first token after
31979 the initial pragma. */
31980
31981 static void
31982 cp_parser_initial_pragma (cp_token *first_token)
31983 {
31984 tree name = NULL;
31985
31986 cp_lexer_get_preprocessor_token (NULL, first_token);
31987 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31988 return;
31989
31990 cp_lexer_get_preprocessor_token (NULL, first_token);
31991 if (first_token->type == CPP_STRING)
31992 {
31993 name = first_token->u.value;
31994
31995 cp_lexer_get_preprocessor_token (NULL, first_token);
31996 if (first_token->type != CPP_PRAGMA_EOL)
31997 error_at (first_token->location,
31998 "junk at end of %<#pragma GCC pch_preprocess%>");
31999 }
32000 else
32001 error_at (first_token->location, "expected string literal");
32002
32003 /* Skip to the end of the pragma. */
32004 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32005 cp_lexer_get_preprocessor_token (NULL, first_token);
32006
32007 /* Now actually load the PCH file. */
32008 if (name)
32009 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32010
32011 /* Read one more token to return to our caller. We have to do this
32012 after reading the PCH file in, since its pointers have to be
32013 live. */
32014 cp_lexer_get_preprocessor_token (NULL, first_token);
32015 }
32016
32017 /* Parses the grainsize pragma for the _Cilk_for statement.
32018 Syntax:
32019 #pragma cilk grainsize = <VALUE>. */
32020
32021 static void
32022 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32023 {
32024 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32025 {
32026 tree exp = cp_parser_binary_expression (parser, false, false,
32027 PREC_NOT_OPERATOR, NULL);
32028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32029 if (!exp || exp == error_mark_node)
32030 {
32031 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32032 return;
32033 }
32034
32035 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32037 cp_parser_cilk_for (parser, exp);
32038 else
32039 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32040 "%<#pragma cilk grainsize%> is not followed by "
32041 "%<_Cilk_for%>");
32042 return;
32043 }
32044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32045 }
32046
32047 /* Normal parsing of a pragma token. Here we can (and must) use the
32048 regular lexer. */
32049
32050 static bool
32051 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32052 {
32053 cp_token *pragma_tok;
32054 unsigned int id;
32055
32056 pragma_tok = cp_lexer_consume_token (parser->lexer);
32057 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32058 parser->lexer->in_pragma = true;
32059
32060 id = pragma_tok->pragma_kind;
32061 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32062 cp_ensure_no_omp_declare_simd (parser);
32063 switch (id)
32064 {
32065 case PRAGMA_GCC_PCH_PREPROCESS:
32066 error_at (pragma_tok->location,
32067 "%<#pragma GCC pch_preprocess%> must be first");
32068 break;
32069
32070 case PRAGMA_OMP_BARRIER:
32071 switch (context)
32072 {
32073 case pragma_compound:
32074 cp_parser_omp_barrier (parser, pragma_tok);
32075 return false;
32076 case pragma_stmt:
32077 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32078 "used in compound statements");
32079 break;
32080 default:
32081 goto bad_stmt;
32082 }
32083 break;
32084
32085 case PRAGMA_OMP_FLUSH:
32086 switch (context)
32087 {
32088 case pragma_compound:
32089 cp_parser_omp_flush (parser, pragma_tok);
32090 return false;
32091 case pragma_stmt:
32092 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32093 "used in compound statements");
32094 break;
32095 default:
32096 goto bad_stmt;
32097 }
32098 break;
32099
32100 case PRAGMA_OMP_TASKWAIT:
32101 switch (context)
32102 {
32103 case pragma_compound:
32104 cp_parser_omp_taskwait (parser, pragma_tok);
32105 return false;
32106 case pragma_stmt:
32107 error_at (pragma_tok->location,
32108 "%<#pragma omp taskwait%> may only be "
32109 "used in compound statements");
32110 break;
32111 default:
32112 goto bad_stmt;
32113 }
32114 break;
32115
32116 case PRAGMA_OMP_TASKYIELD:
32117 switch (context)
32118 {
32119 case pragma_compound:
32120 cp_parser_omp_taskyield (parser, pragma_tok);
32121 return false;
32122 case pragma_stmt:
32123 error_at (pragma_tok->location,
32124 "%<#pragma omp taskyield%> may only be "
32125 "used in compound statements");
32126 break;
32127 default:
32128 goto bad_stmt;
32129 }
32130 break;
32131
32132 case PRAGMA_OMP_CANCEL:
32133 switch (context)
32134 {
32135 case pragma_compound:
32136 cp_parser_omp_cancel (parser, pragma_tok);
32137 return false;
32138 case pragma_stmt:
32139 error_at (pragma_tok->location,
32140 "%<#pragma omp cancel%> may only be "
32141 "used in compound statements");
32142 break;
32143 default:
32144 goto bad_stmt;
32145 }
32146 break;
32147
32148 case PRAGMA_OMP_CANCELLATION_POINT:
32149 switch (context)
32150 {
32151 case pragma_compound:
32152 cp_parser_omp_cancellation_point (parser, pragma_tok);
32153 return false;
32154 case pragma_stmt:
32155 error_at (pragma_tok->location,
32156 "%<#pragma omp cancellation point%> may only be "
32157 "used in compound statements");
32158 break;
32159 default:
32160 goto bad_stmt;
32161 }
32162 break;
32163
32164 case PRAGMA_OMP_THREADPRIVATE:
32165 cp_parser_omp_threadprivate (parser, pragma_tok);
32166 return false;
32167
32168 case PRAGMA_OMP_DECLARE_REDUCTION:
32169 cp_parser_omp_declare (parser, pragma_tok, context);
32170 return false;
32171
32172 case PRAGMA_OMP_ATOMIC:
32173 case PRAGMA_OMP_CRITICAL:
32174 case PRAGMA_OMP_DISTRIBUTE:
32175 case PRAGMA_OMP_FOR:
32176 case PRAGMA_OMP_MASTER:
32177 case PRAGMA_OMP_ORDERED:
32178 case PRAGMA_OMP_PARALLEL:
32179 case PRAGMA_OMP_SECTIONS:
32180 case PRAGMA_OMP_SIMD:
32181 case PRAGMA_OMP_SINGLE:
32182 case PRAGMA_OMP_TASK:
32183 case PRAGMA_OMP_TASKGROUP:
32184 case PRAGMA_OMP_TEAMS:
32185 if (context != pragma_stmt && context != pragma_compound)
32186 goto bad_stmt;
32187 cp_parser_omp_construct (parser, pragma_tok);
32188 return true;
32189
32190 case PRAGMA_OMP_TARGET:
32191 return cp_parser_omp_target (parser, pragma_tok, context);
32192
32193 case PRAGMA_OMP_END_DECLARE_TARGET:
32194 cp_parser_omp_end_declare_target (parser, pragma_tok);
32195 return false;
32196
32197 case PRAGMA_OMP_SECTION:
32198 error_at (pragma_tok->location,
32199 "%<#pragma omp section%> may only be used in "
32200 "%<#pragma omp sections%> construct");
32201 break;
32202
32203 case PRAGMA_IVDEP:
32204 {
32205 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32206 cp_token *tok;
32207 tok = cp_lexer_peek_token (the_parser->lexer);
32208 if (tok->type != CPP_KEYWORD
32209 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32210 && tok->keyword != RID_DO))
32211 {
32212 cp_parser_error (parser, "for, while or do statement expected");
32213 return false;
32214 }
32215 cp_parser_iteration_statement (parser, true);
32216 return true;
32217 }
32218
32219 case PRAGMA_CILK_SIMD:
32220 if (context == pragma_external)
32221 {
32222 error_at (pragma_tok->location,
32223 "%<#pragma simd%> must be inside a function");
32224 break;
32225 }
32226 cp_parser_cilk_simd (parser, pragma_tok);
32227 return true;
32228
32229 case PRAGMA_CILK_GRAINSIZE:
32230 if (context == pragma_external)
32231 {
32232 error_at (pragma_tok->location,
32233 "%<#pragma cilk grainsize%> must be inside a function");
32234 break;
32235 }
32236
32237 /* Ignore the pragma if Cilk Plus is not enabled. */
32238 if (flag_cilkplus)
32239 {
32240 cp_parser_cilk_grainsize (parser, pragma_tok);
32241 return true;
32242 }
32243 else
32244 {
32245 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32246 "%<#pragma cilk grainsize%>");
32247 break;
32248 }
32249
32250 default:
32251 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32252 c_invoke_pragma_handler (id);
32253 break;
32254
32255 bad_stmt:
32256 cp_parser_error (parser, "expected declaration specifiers");
32257 break;
32258 }
32259
32260 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32261 return false;
32262 }
32263
32264 /* The interface the pragma parsers have to the lexer. */
32265
32266 enum cpp_ttype
32267 pragma_lex (tree *value)
32268 {
32269 cp_token *tok;
32270 enum cpp_ttype ret;
32271
32272 tok = cp_lexer_peek_token (the_parser->lexer);
32273
32274 ret = tok->type;
32275 *value = tok->u.value;
32276
32277 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32278 ret = CPP_EOF;
32279 else if (ret == CPP_STRING)
32280 *value = cp_parser_string_literal (the_parser, false, false);
32281 else
32282 {
32283 cp_lexer_consume_token (the_parser->lexer);
32284 if (ret == CPP_KEYWORD)
32285 ret = CPP_NAME;
32286 }
32287
32288 return ret;
32289 }
32290
32291 \f
32292 /* External interface. */
32293
32294 /* Parse one entire translation unit. */
32295
32296 void
32297 c_parse_file (void)
32298 {
32299 static bool already_called = false;
32300
32301 if (already_called)
32302 fatal_error ("inter-module optimizations not implemented for C++");
32303 already_called = true;
32304
32305 the_parser = cp_parser_new ();
32306 push_deferring_access_checks (flag_access_control
32307 ? dk_no_deferred : dk_no_check);
32308 cp_parser_translation_unit (the_parser);
32309 the_parser = NULL;
32310 }
32311
32312 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32313 vectorlength clause:
32314 Syntax:
32315 vectorlength ( constant-expression ) */
32316
32317 static tree
32318 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32319 bool is_simd_fn)
32320 {
32321 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32322 tree expr;
32323 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32324 safelen clause. Thus, vectorlength is represented as OMP 4.0
32325 safelen. For SIMD-enabled function it is represented by OMP 4.0
32326 simdlen. */
32327 if (!is_simd_fn)
32328 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32329 loc);
32330 else
32331 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32332 loc);
32333
32334 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32335 return error_mark_node;
32336
32337 expr = cp_parser_constant_expression (parser);
32338 expr = maybe_constant_value (expr);
32339
32340 /* If expr == error_mark_node, then don't emit any errors nor
32341 create a clause. if any of the above functions returns
32342 error mark node then they would have emitted an error message. */
32343 if (expr == error_mark_node)
32344 ;
32345 else if (!TREE_TYPE (expr)
32346 || !TREE_CONSTANT (expr)
32347 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32348 error_at (loc, "vectorlength must be an integer constant");
32349 else if (TREE_CONSTANT (expr)
32350 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32351 error_at (loc, "vectorlength must be a power of 2");
32352 else
32353 {
32354 tree c;
32355 if (!is_simd_fn)
32356 {
32357 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32358 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32359 OMP_CLAUSE_CHAIN (c) = clauses;
32360 clauses = c;
32361 }
32362 else
32363 {
32364 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32365 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32366 OMP_CLAUSE_CHAIN (c) = clauses;
32367 clauses = c;
32368 }
32369 }
32370
32371 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32372 return error_mark_node;
32373 return clauses;
32374 }
32375
32376 /* Handles the Cilk Plus #pragma simd linear clause.
32377 Syntax:
32378 linear ( simd-linear-variable-list )
32379
32380 simd-linear-variable-list:
32381 simd-linear-variable
32382 simd-linear-variable-list , simd-linear-variable
32383
32384 simd-linear-variable:
32385 id-expression
32386 id-expression : simd-linear-step
32387
32388 simd-linear-step:
32389 conditional-expression */
32390
32391 static tree
32392 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32393 {
32394 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32395
32396 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32397 return clauses;
32398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32399 {
32400 cp_parser_error (parser, "expected identifier");
32401 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32402 return error_mark_node;
32403 }
32404
32405 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32406 parser->colon_corrects_to_scope_p = false;
32407 while (1)
32408 {
32409 cp_token *token = cp_lexer_peek_token (parser->lexer);
32410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32411 {
32412 cp_parser_error (parser, "expected variable-name");
32413 clauses = error_mark_node;
32414 break;
32415 }
32416
32417 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32418 false, false);
32419 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32420 token->location);
32421 if (decl == error_mark_node)
32422 {
32423 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32424 token->location);
32425 clauses = error_mark_node;
32426 }
32427 else
32428 {
32429 tree e = NULL_TREE;
32430 tree step_size = integer_one_node;
32431
32432 /* If present, parse the linear step. Otherwise, assume the default
32433 value of 1. */
32434 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32435 {
32436 cp_lexer_consume_token (parser->lexer);
32437
32438 e = cp_parser_assignment_expression (parser);
32439 e = maybe_constant_value (e);
32440
32441 if (e == error_mark_node)
32442 {
32443 /* If an error has occurred, then the whole pragma is
32444 considered ill-formed. Thus, no reason to keep
32445 parsing. */
32446 clauses = error_mark_node;
32447 break;
32448 }
32449 else if (type_dependent_expression_p (e)
32450 || value_dependent_expression_p (e)
32451 || (TREE_TYPE (e)
32452 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32453 && (TREE_CONSTANT (e)
32454 || DECL_P (e))))
32455 step_size = e;
32456 else
32457 cp_parser_error (parser,
32458 "step size must be an integer constant "
32459 "expression or an integer variable");
32460 }
32461
32462 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32463 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32464 OMP_CLAUSE_DECL (l) = decl;
32465 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32466 OMP_CLAUSE_CHAIN (l) = clauses;
32467 clauses = l;
32468 }
32469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32470 cp_lexer_consume_token (parser->lexer);
32471 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32472 break;
32473 else
32474 {
32475 error_at (cp_lexer_peek_token (parser->lexer)->location,
32476 "expected %<,%> or %<)%> after %qE", decl);
32477 clauses = error_mark_node;
32478 break;
32479 }
32480 }
32481 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32482 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32483 return clauses;
32484 }
32485
32486 /* Returns the name of the next clause. If the clause is not
32487 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32488 token is not consumed. Otherwise, the appropriate enum from the
32489 pragma_simd_clause is returned and the token is consumed. */
32490
32491 static pragma_omp_clause
32492 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32493 {
32494 pragma_omp_clause clause_type;
32495 cp_token *token = cp_lexer_peek_token (parser->lexer);
32496
32497 if (token->keyword == RID_PRIVATE)
32498 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32499 else if (!token->u.value || token->type != CPP_NAME)
32500 return PRAGMA_CILK_CLAUSE_NONE;
32501 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32502 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32503 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32504 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32505 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32506 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32507 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32508 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32509 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32510 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32511 else
32512 return PRAGMA_CILK_CLAUSE_NONE;
32513
32514 cp_lexer_consume_token (parser->lexer);
32515 return clause_type;
32516 }
32517
32518 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32519
32520 static tree
32521 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32522 {
32523 tree clauses = NULL_TREE;
32524
32525 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32526 && clauses != error_mark_node)
32527 {
32528 pragma_omp_clause c_kind;
32529 c_kind = cp_parser_cilk_simd_clause_name (parser);
32530 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32531 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32532 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32533 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32534 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32535 /* Use the OpenMP 4.0 equivalent function. */
32536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32537 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32538 /* Use the OpenMP 4.0 equivalent function. */
32539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32540 clauses);
32541 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32542 /* Use the OMP 4.0 equivalent function. */
32543 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32544 clauses);
32545 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32546 /* Use the OMP 4.0 equivalent function. */
32547 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32548 else
32549 {
32550 clauses = error_mark_node;
32551 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32552 break;
32553 }
32554 }
32555
32556 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32557
32558 if (clauses == error_mark_node)
32559 return error_mark_node;
32560 else
32561 return c_finish_cilk_clauses (clauses);
32562 }
32563
32564 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32565
32566 static void
32567 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32568 {
32569 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32570
32571 if (clauses == error_mark_node)
32572 return;
32573
32574 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32575 {
32576 error_at (cp_lexer_peek_token (parser->lexer)->location,
32577 "for statement expected");
32578 return;
32579 }
32580
32581 tree sb = begin_omp_structured_block ();
32582 int save = cp_parser_begin_omp_structured_block (parser);
32583 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32584 if (ret)
32585 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32586 cp_parser_end_omp_structured_block (parser, save);
32587 add_stmt (finish_omp_structured_block (sb));
32588 }
32589
32590 /* Main entry-point for parsing Cilk Plus _Cilk_for
32591 loops. The return value is error_mark_node
32592 when errors happen and CILK_FOR tree on success. */
32593
32594 static tree
32595 cp_parser_cilk_for (cp_parser *parser, tree grain)
32596 {
32597 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32598 gcc_unreachable ();
32599
32600 tree sb = begin_omp_structured_block ();
32601 int save = cp_parser_begin_omp_structured_block (parser);
32602
32603 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32604 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32605 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32606 clauses = finish_omp_clauses (clauses);
32607
32608 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32609 if (ret)
32610 cpp_validate_cilk_plus_loop (ret);
32611 else
32612 ret = error_mark_node;
32613
32614 cp_parser_end_omp_structured_block (parser, save);
32615 add_stmt (finish_omp_structured_block (sb));
32616 return ret;
32617 }
32618
32619 /* Create an identifier for a generic parameter type (a synthesized
32620 template parameter implied by `auto' or a concept identifier). */
32621
32622 static GTY(()) int generic_parm_count;
32623 static tree
32624 make_generic_type_name ()
32625 {
32626 char buf[32];
32627 sprintf (buf, "auto:%d", ++generic_parm_count);
32628 return get_identifier (buf);
32629 }
32630
32631 /* Predicate that behaves as is_auto_or_concept but matches the parent
32632 node of the generic type rather than the generic type itself. This
32633 allows for type transformation in add_implicit_template_parms. */
32634
32635 static inline bool
32636 tree_type_is_auto_or_concept (const_tree t)
32637 {
32638 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32639 }
32640
32641 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32642 (creating a new template parameter list if necessary). Returns the newly
32643 created template type parm. */
32644
32645 tree
32646 synthesize_implicit_template_parm (cp_parser *parser)
32647 {
32648 gcc_assert (current_binding_level->kind == sk_function_parms);
32649
32650 /* We are either continuing a function template that already contains implicit
32651 template parameters, creating a new fully-implicit function template, or
32652 extending an existing explicit function template with implicit template
32653 parameters. */
32654
32655 cp_binding_level *const entry_scope = current_binding_level;
32656
32657 bool become_template = false;
32658 cp_binding_level *parent_scope = 0;
32659
32660 if (parser->implicit_template_scope)
32661 {
32662 gcc_assert (parser->implicit_template_parms);
32663
32664 current_binding_level = parser->implicit_template_scope;
32665 }
32666 else
32667 {
32668 /* Roll back to the existing template parameter scope (in the case of
32669 extending an explicit function template) or introduce a new template
32670 parameter scope ahead of the function parameter scope (or class scope
32671 in the case of out-of-line member definitions). The function scope is
32672 added back after template parameter synthesis below. */
32673
32674 cp_binding_level *scope = entry_scope;
32675
32676 while (scope->kind == sk_function_parms)
32677 {
32678 parent_scope = scope;
32679 scope = scope->level_chain;
32680 }
32681 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32682 {
32683 /* If not defining a class, then any class scope is a scope level in
32684 an out-of-line member definition. In this case simply wind back
32685 beyond the first such scope to inject the template parameter list.
32686 Otherwise wind back to the class being defined. The latter can
32687 occur in class member friend declarations such as:
32688
32689 class A {
32690 void foo (auto);
32691 };
32692 class B {
32693 friend void A::foo (auto);
32694 };
32695
32696 The template parameter list synthesized for the friend declaration
32697 must be injected in the scope of 'B'. This can also occur in
32698 erroneous cases such as:
32699
32700 struct A {
32701 struct B {
32702 void foo (auto);
32703 };
32704 void B::foo (auto) {}
32705 };
32706
32707 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32708 but, nevertheless, the template parameter list synthesized for the
32709 declarator should be injected into the scope of 'A' as if the
32710 ill-formed template was specified explicitly. */
32711
32712 while (scope->kind == sk_class && !scope->defining_class_p)
32713 {
32714 parent_scope = scope;
32715 scope = scope->level_chain;
32716 }
32717 }
32718
32719 current_binding_level = scope;
32720
32721 if (scope->kind != sk_template_parms
32722 || !function_being_declared_is_template_p (parser))
32723 {
32724 /* Introduce a new template parameter list for implicit template
32725 parameters. */
32726
32727 become_template = true;
32728
32729 parser->implicit_template_scope
32730 = begin_scope (sk_template_parms, NULL);
32731
32732 ++processing_template_decl;
32733
32734 parser->fully_implicit_function_template_p = true;
32735 ++parser->num_template_parameter_lists;
32736 }
32737 else
32738 {
32739 /* Synthesize implicit template parameters at the end of the explicit
32740 template parameter list. */
32741
32742 gcc_assert (current_template_parms);
32743
32744 parser->implicit_template_scope = scope;
32745
32746 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32747 parser->implicit_template_parms
32748 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32749 }
32750 }
32751
32752 /* Synthesize a new template parameter and track the current template
32753 parameter chain with implicit_template_parms. */
32754
32755 tree synth_id = make_generic_type_name ();
32756 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32757 synth_id);
32758 tree new_parm
32759 = process_template_parm (parser->implicit_template_parms,
32760 input_location,
32761 build_tree_list (NULL_TREE, synth_tmpl_parm),
32762 /*non_type=*/false,
32763 /*param_pack=*/false);
32764
32765
32766 if (parser->implicit_template_parms)
32767 parser->implicit_template_parms
32768 = TREE_CHAIN (parser->implicit_template_parms);
32769 else
32770 parser->implicit_template_parms = new_parm;
32771
32772 tree new_type = TREE_TYPE (getdecls ());
32773
32774 /* If creating a fully implicit function template, start the new implicit
32775 template parameter list with this synthesized type, otherwise grow the
32776 current template parameter list. */
32777
32778 if (become_template)
32779 {
32780 parent_scope->level_chain = current_binding_level;
32781
32782 tree new_parms = make_tree_vec (1);
32783 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32784 current_template_parms = tree_cons (size_int (processing_template_decl),
32785 new_parms, current_template_parms);
32786 }
32787 else
32788 {
32789 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32790 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32791 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32792 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32793 }
32794
32795 current_binding_level = entry_scope;
32796
32797 return new_type;
32798 }
32799
32800 /* Finish the declaration of a fully implicit function template. Such a
32801 template has no explicit template parameter list so has not been through the
32802 normal template head and tail processing. synthesize_implicit_template_parm
32803 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32804 provided if the declaration is a class member such that its template
32805 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32806 form is returned. Otherwise NULL_TREE is returned. */
32807
32808 tree
32809 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32810 {
32811 gcc_assert (parser->fully_implicit_function_template_p);
32812
32813 if (member_decl_opt && member_decl_opt != error_mark_node
32814 && DECL_VIRTUAL_P (member_decl_opt))
32815 {
32816 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32817 "implicit templates may not be %<virtual%>");
32818 DECL_VIRTUAL_P (member_decl_opt) = false;
32819 }
32820
32821 if (member_decl_opt)
32822 member_decl_opt = finish_member_template_decl (member_decl_opt);
32823 end_template_decl ();
32824
32825 parser->fully_implicit_function_template_p = false;
32826 --parser->num_template_parameter_lists;
32827
32828 return member_decl_opt;
32829 }
32830
32831 #include "gt-cp-parser.h"