]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Implement -Wmisleading-indentation
[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 #include "gomp-constants.h"
64
65 \f
66 /* The lexer. */
67
68 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
69 and c-lex.c) and the C++ parser. */
70
71 static cp_token eof_token =
72 {
73 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
74 };
75
76 /* The various kinds of non integral constant we encounter. */
77 typedef enum non_integral_constant {
78 NIC_NONE,
79 /* floating-point literal */
80 NIC_FLOAT,
81 /* %<this%> */
82 NIC_THIS,
83 /* %<__FUNCTION__%> */
84 NIC_FUNC_NAME,
85 /* %<__PRETTY_FUNCTION__%> */
86 NIC_PRETTY_FUNC,
87 /* %<__func__%> */
88 NIC_C99_FUNC,
89 /* "%<va_arg%> */
90 NIC_VA_ARG,
91 /* a cast */
92 NIC_CAST,
93 /* %<typeid%> operator */
94 NIC_TYPEID,
95 /* non-constant compound literals */
96 NIC_NCC,
97 /* a function call */
98 NIC_FUNC_CALL,
99 /* an increment */
100 NIC_INC,
101 /* an decrement */
102 NIC_DEC,
103 /* an array reference */
104 NIC_ARRAY_REF,
105 /* %<->%> */
106 NIC_ARROW,
107 /* %<.%> */
108 NIC_POINT,
109 /* the address of a label */
110 NIC_ADDR_LABEL,
111 /* %<*%> */
112 NIC_STAR,
113 /* %<&%> */
114 NIC_ADDR,
115 /* %<++%> */
116 NIC_PREINCREMENT,
117 /* %<--%> */
118 NIC_PREDECREMENT,
119 /* %<new%> */
120 NIC_NEW,
121 /* %<delete%> */
122 NIC_DEL,
123 /* calls to overloaded operators */
124 NIC_OVERLOADED,
125 /* an assignment */
126 NIC_ASSIGNMENT,
127 /* a comma operator */
128 NIC_COMMA,
129 /* a call to a constructor */
130 NIC_CONSTRUCTOR,
131 /* a transaction expression */
132 NIC_TRANSACTION
133 } non_integral_constant;
134
135 /* The various kinds of errors about name-lookup failing. */
136 typedef enum name_lookup_error {
137 /* NULL */
138 NLE_NULL,
139 /* is not a type */
140 NLE_TYPE,
141 /* is not a class or namespace */
142 NLE_CXX98,
143 /* is not a class, namespace, or enumeration */
144 NLE_NOT_CXX98
145 } name_lookup_error;
146
147 /* The various kinds of required token */
148 typedef enum required_token {
149 RT_NONE,
150 RT_SEMICOLON, /* ';' */
151 RT_OPEN_PAREN, /* '(' */
152 RT_CLOSE_BRACE, /* '}' */
153 RT_OPEN_BRACE, /* '{' */
154 RT_CLOSE_SQUARE, /* ']' */
155 RT_OPEN_SQUARE, /* '[' */
156 RT_COMMA, /* ',' */
157 RT_SCOPE, /* '::' */
158 RT_LESS, /* '<' */
159 RT_GREATER, /* '>' */
160 RT_EQ, /* '=' */
161 RT_ELLIPSIS, /* '...' */
162 RT_MULT, /* '*' */
163 RT_COMPL, /* '~' */
164 RT_COLON, /* ':' */
165 RT_COLON_SCOPE, /* ':' or '::' */
166 RT_CLOSE_PAREN, /* ')' */
167 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
168 RT_PRAGMA_EOL, /* end of line */
169 RT_NAME, /* identifier */
170
171 /* The type is CPP_KEYWORD */
172 RT_NEW, /* new */
173 RT_DELETE, /* delete */
174 RT_RETURN, /* return */
175 RT_WHILE, /* while */
176 RT_EXTERN, /* extern */
177 RT_STATIC_ASSERT, /* static_assert */
178 RT_DECLTYPE, /* decltype */
179 RT_OPERATOR, /* operator */
180 RT_CLASS, /* class */
181 RT_TEMPLATE, /* template */
182 RT_NAMESPACE, /* namespace */
183 RT_USING, /* using */
184 RT_ASM, /* asm */
185 RT_TRY, /* try */
186 RT_CATCH, /* catch */
187 RT_THROW, /* throw */
188 RT_LABEL, /* __label__ */
189 RT_AT_TRY, /* @try */
190 RT_AT_SYNCHRONIZED, /* @synchronized */
191 RT_AT_THROW, /* @throw */
192
193 RT_SELECT, /* selection-statement */
194 RT_INTERATION, /* iteration-statement */
195 RT_JUMP, /* jump-statement */
196 RT_CLASS_KEY, /* class-key */
197 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
198 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
199 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
200 RT_TRANSACTION_CANCEL /* __transaction_cancel */
201 } required_token;
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static tree cp_literal_operator_id
255 (const char *);
256
257 static void cp_parser_cilk_simd
258 (cp_parser *, cp_token *);
259 static tree cp_parser_cilk_for
260 (cp_parser *, tree);
261 static bool cp_parser_omp_declare_reduction_exprs
262 (tree, cp_parser *);
263 static tree cp_parser_cilk_simd_vectorlength
264 (cp_parser *, tree, bool);
265
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
269
270 /* Variables. */
271
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream;
274
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand;
278
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
284
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287 cp_token *start_token, unsigned num,
288 cp_token *curr_token)
289 {
290 unsigned i, nprinted;
291 cp_token *token;
292 bool do_print;
293
294 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295
296 if (buffer == NULL)
297 return;
298
299 if (num == 0)
300 num = buffer->length ();
301
302 if (start_token == NULL)
303 start_token = buffer->address ();
304
305 if (start_token > buffer->address ())
306 {
307 cp_lexer_print_token (file, &(*buffer)[0]);
308 fprintf (file, " ... ");
309 }
310
311 do_print = false;
312 nprinted = 0;
313 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 {
315 if (token == start_token)
316 do_print = true;
317
318 if (!do_print)
319 continue;
320
321 nprinted++;
322 if (token == curr_token)
323 fprintf (file, "[[");
324
325 cp_lexer_print_token (file, token);
326
327 if (token == curr_token)
328 fprintf (file, "]]");
329
330 switch (token->type)
331 {
332 case CPP_SEMICOLON:
333 case CPP_OPEN_BRACE:
334 case CPP_CLOSE_BRACE:
335 case CPP_EOF:
336 fputc ('\n', file);
337 break;
338
339 default:
340 fputc (' ', file);
341 }
342 }
343
344 if (i == num && i < buffer->length ())
345 {
346 fprintf (file, " ... ");
347 cp_lexer_print_token (file, &buffer->last ());
348 }
349
350 fprintf (file, "\n");
351 }
352
353
354 /* Dump all tokens in BUFFER to stderr. */
355
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 {
359 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 }
361
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
364 {
365 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 }
367
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
370 {
371 if (ptr)
372 debug (*ptr);
373 else
374 fprintf (stderr, "<nil>\n");
375 }
376
377
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
380
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 {
384 if (t)
385 {
386 fprintf (file, "%s: ", desc);
387 print_node_brief (file, "", t, 0);
388 }
389 }
390
391
392 /* Dump parser context C to FILE. */
393
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 {
397 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399 print_node_brief (file, "", c->object_type, 0);
400 fprintf (file, "}\n");
401 }
402
403
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 {
409 unsigned i;
410 cp_parser_context *c;
411
412 fprintf (file, "Parsing context stack:\n");
413 for (i = 0, c = first; c; c = c->next, i++)
414 {
415 fprintf (file, "\t#%u: ", i);
416 cp_debug_print_context (file, c);
417 }
418 }
419
420
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 {
426 if (flag)
427 fprintf (file, "%s: true\n", desc);
428 }
429
430
431 /* Print an unparsed function entry UF to FILE. */
432
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 {
436 unsigned i;
437 cp_default_arg_entry *default_arg_fn;
438 tree fn;
439
440 fprintf (file, "\tFunctions with default args:\n");
441 for (i = 0;
442 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443 i++)
444 {
445 fprintf (file, "\t\tClass type: ");
446 print_node_brief (file, "", default_arg_fn->class_type, 0);
447 fprintf (file, "\t\tDeclaration: ");
448 print_node_brief (file, "", default_arg_fn->decl, 0);
449 fprintf (file, "\n");
450 }
451
452 fprintf (file, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 {
456 print_node_brief (file, "", fn, 0);
457 fprintf (file, " ");
458 }
459 fprintf (file, "\n");
460
461 fprintf (file, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 {
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
467 }
468 fprintf (file, "\n");
469 }
470
471
472 /* Print the stack of unparsed member functions S to FILE. */
473
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476 vec<cp_unparsed_functions_entry, va_gc> *s)
477 {
478 unsigned i;
479 cp_unparsed_functions_entry *uf;
480
481 fprintf (file, "Unparsed functions\n");
482 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 {
484 fprintf (file, "#%u:\n", i);
485 cp_debug_print_unparsed_function (file, uf);
486 }
487 }
488
489
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 {
496 cp_token *next_token, *first_token, *start_token;
497
498 if (file == NULL)
499 file = stderr;
500
501 next_token = parser->lexer->next_token;
502 first_token = parser->lexer->buffer->address ();
503 start_token = (next_token > first_token + window_size / 2)
504 ? next_token - window_size / 2
505 : first_token;
506 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507 next_token);
508 }
509
510
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
513
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
516 {
517 const size_t window_size = 20;
518 cp_token *token;
519 expanded_location eloc;
520
521 if (file == NULL)
522 file = stderr;
523
524 fprintf (file, "Parser state\n\n");
525 fprintf (file, "Number of tokens: %u\n",
526 vec_safe_length (parser->lexer->buffer));
527 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528 cp_debug_print_tree_if_set (file, "Object scope",
529 parser->object_scope);
530 cp_debug_print_tree_if_set (file, "Qualifying scope",
531 parser->qualifying_scope);
532 cp_debug_print_context_stack (file, parser->context);
533 cp_debug_print_flag (file, "Allow GNU extensions",
534 parser->allow_gnu_extensions_p);
535 cp_debug_print_flag (file, "'>' token is greater-than",
536 parser->greater_than_is_operator_p);
537 cp_debug_print_flag (file, "Default args allowed in current "
538 "parameter list", parser->default_arg_ok_p);
539 cp_debug_print_flag (file, "Parsing integral constant-expression",
540 parser->integral_constant_expression_p);
541 cp_debug_print_flag (file, "Allow non-constant expression in current "
542 "constant-expression",
543 parser->allow_non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Seen non-constant expression",
545 parser->non_integral_constant_expression_p);
546 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547 "current context",
548 parser->local_variables_forbidden_p);
549 cp_debug_print_flag (file, "In unbraced linkage specification",
550 parser->in_unbraced_linkage_specification_p);
551 cp_debug_print_flag (file, "Parsing a declarator",
552 parser->in_declarator_p);
553 cp_debug_print_flag (file, "In template argument list",
554 parser->in_template_argument_list_p);
555 cp_debug_print_flag (file, "Parsing an iteration statement",
556 parser->in_statement & IN_ITERATION_STMT);
557 cp_debug_print_flag (file, "Parsing a switch statement",
558 parser->in_statement & IN_SWITCH_STMT);
559 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560 parser->in_statement & IN_OMP_BLOCK);
561 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562 parser->in_statement & IN_CILK_SIMD_FOR);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570 parser->implicit_extern_c);
571 cp_debug_print_flag (file, "String expressions should be translated "
572 "to execution character set",
573 parser->translate_strings_p);
574 cp_debug_print_flag (file, "Parsing function body outside of a "
575 "local class", parser->in_function_body);
576 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577 parser->colon_corrects_to_scope_p);
578 cp_debug_print_flag (file, "Colon doesn't start a class definition",
579 parser->colon_doesnt_start_class_def_p);
580 if (parser->type_definition_forbidden_message)
581 fprintf (file, "Error message for forbidden type definitions: %s\n",
582 parser->type_definition_forbidden_message);
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
601 {
602 cp_debug_parser (stderr, &ref);
603 }
604
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
607 {
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
612 }
613
614 /* Allocate memory for a new lexer object and return it. */
615
616 static cp_lexer *
617 cp_lexer_alloc (void)
618 {
619 cp_lexer *lexer;
620
621 c_common_no_more_pch ();
622
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
625
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
628
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633
634 return lexer;
635 }
636
637
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
640
641 static cp_lexer *
642 cp_lexer_new_main (void)
643 {
644 cp_lexer *lexer;
645 cp_token token;
646
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
651
652 lexer = cp_lexer_alloc ();
653
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
656
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
659 {
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
662 }
663
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
670
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
674
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
677 }
678
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
681
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 {
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
693
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
698
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
701 }
702
703 /* Frees all resources associated with LEXER. */
704
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
707 {
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
711 }
712
713 /* Returns nonzero if debugging information should be output. */
714
715 static inline bool
716 cp_lexer_debugging_p (cp_lexer *lexer)
717 {
718 return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727 return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733 return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756 return cp_lexer_token_at (lexer, tp);
757 }
758
759 /* nonzero if we are presently saving tokens. */
760
761 static inline int
762 cp_lexer_saving_tokens (const cp_lexer* lexer)
763 {
764 return lexer->saved_tokens.length () != 0;
765 }
766
767 /* Store the next token from the preprocessor in *TOKEN. Return true
768 if we reach EOF. If LEXER is NULL, assume we are handling an
769 initial #pragma pch_preprocess, and thus want the lexer to return
770 processed strings. */
771
772 static void
773 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
774 {
775 static int is_extern_c = 0;
776
777 /* Get a new token from the preprocessor. */
778 token->type
779 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
780 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
781 token->keyword = RID_MAX;
782 token->pragma_kind = PRAGMA_NONE;
783 token->purged_p = false;
784 token->error_reported = false;
785
786 /* On some systems, some header files are surrounded by an
787 implicit extern "C" block. Set a flag in the token if it
788 comes from such a header. */
789 is_extern_c += pending_lang_change;
790 pending_lang_change = 0;
791 token->implicit_extern_c = is_extern_c > 0;
792
793 /* Check to see if this token is a keyword. */
794 if (token->type == CPP_NAME)
795 {
796 if (C_IS_RESERVED_WORD (token->u.value))
797 {
798 /* Mark this token as a keyword. */
799 token->type = CPP_KEYWORD;
800 /* Record which keyword. */
801 token->keyword = C_RID_CODE (token->u.value);
802 }
803 else
804 {
805 if (warn_cxx11_compat
806 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
807 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
808 {
809 /* Warn about the C++0x keyword (but still treat it as
810 an identifier). */
811 warning (OPT_Wc__11_compat,
812 "identifier %qE is a keyword in C++11",
813 token->u.value);
814
815 /* Clear out the C_RID_CODE so we don't warn about this
816 particular identifier-turned-keyword again. */
817 C_SET_RID_CODE (token->u.value, RID_MAX);
818 }
819
820 token->keyword = RID_MAX;
821 }
822 }
823 else if (token->type == CPP_AT_NAME)
824 {
825 /* This only happens in Objective-C++; it must be a keyword. */
826 token->type = CPP_KEYWORD;
827 switch (C_RID_CODE (token->u.value))
828 {
829 /* Replace 'class' with '@class', 'private' with '@private',
830 etc. This prevents confusion with the C++ keyword
831 'class', and makes the tokens consistent with other
832 Objective-C 'AT' keywords. For example '@class' is
833 reported as RID_AT_CLASS which is consistent with
834 '@synchronized', which is reported as
835 RID_AT_SYNCHRONIZED.
836 */
837 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
838 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
839 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
840 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
841 case RID_THROW: token->keyword = RID_AT_THROW; break;
842 case RID_TRY: token->keyword = RID_AT_TRY; break;
843 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
844 default: token->keyword = C_RID_CODE (token->u.value);
845 }
846 }
847 else if (token->type == CPP_PRAGMA)
848 {
849 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
850 token->pragma_kind = ((enum pragma_kind)
851 TREE_INT_CST_LOW (token->u.value));
852 token->u.value = NULL_TREE;
853 }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860 if (token->type != CPP_EOF)
861 {
862 input_location = token->location;
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880 if (cp_lexer_debugging_p (lexer))
881 {
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
885 }
886 return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE. */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894 return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE. */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902 return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD. */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD. */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if the next token is a keyword for a decl-specifier. */
934
935 static bool
936 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
937 {
938 cp_token *token;
939
940 token = cp_lexer_peek_token (lexer);
941 switch (token->keyword)
942 {
943 /* auto specifier: storage-class-specifier in C++,
944 simple-type-specifier in C++0x. */
945 case RID_AUTO:
946 /* Storage classes. */
947 case RID_REGISTER:
948 case RID_STATIC:
949 case RID_EXTERN:
950 case RID_MUTABLE:
951 case RID_THREAD:
952 /* Elaborated type specifiers. */
953 case RID_ENUM:
954 case RID_CLASS:
955 case RID_STRUCT:
956 case RID_UNION:
957 case RID_TYPENAME:
958 /* Simple type specifiers. */
959 case RID_CHAR:
960 case RID_CHAR16:
961 case RID_CHAR32:
962 case RID_WCHAR:
963 case RID_BOOL:
964 case RID_SHORT:
965 case RID_INT:
966 case RID_LONG:
967 case RID_SIGNED:
968 case RID_UNSIGNED:
969 case RID_FLOAT:
970 case RID_DOUBLE:
971 case RID_VOID:
972 /* GNU extensions. */
973 case RID_ATTRIBUTE:
974 case RID_TYPEOF:
975 /* C++0x extensions. */
976 case RID_DECLTYPE:
977 case RID_UNDERLYING_TYPE:
978 return true;
979
980 default:
981 if (token->keyword >= RID_FIRST_INT_N
982 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
983 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
984 return true;
985 return false;
986 }
987 }
988
989 /* Returns TRUE iff the token T begins a decltype type. */
990
991 static bool
992 token_is_decltype (cp_token *t)
993 {
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
996 }
997
998 /* Returns TRUE iff the next token begins a decltype type. */
999
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 {
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1005 }
1006
1007 /* Return a pointer to the Nth token in the token stream. If N is 1,
1008 then this is precisely equivalent to cp_lexer_peek_token (except
1009 that it is not inline). One would like to disallow that case, but
1010 there is one case (cp_parser_nth_token_starts_template_id) where
1011 the caller passes a variable for N and it might be 1. */
1012
1013 static cp_token *
1014 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1015 {
1016 cp_token *token;
1017
1018 /* N is 1-based, not zero-based. */
1019 gcc_assert (n > 0);
1020
1021 if (cp_lexer_debugging_p (lexer))
1022 fprintf (cp_lexer_debug_stream,
1023 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1024
1025 --n;
1026 token = lexer->next_token;
1027 gcc_assert (!n || token != &eof_token);
1028 while (n != 0)
1029 {
1030 ++token;
1031 if (token == lexer->last_token)
1032 {
1033 token = &eof_token;
1034 break;
1035 }
1036
1037 if (!token->purged_p)
1038 --n;
1039 }
1040
1041 if (cp_lexer_debugging_p (lexer))
1042 {
1043 cp_lexer_print_token (cp_lexer_debug_stream, token);
1044 putc ('\n', cp_lexer_debug_stream);
1045 }
1046
1047 return token;
1048 }
1049
1050 /* Return the next token, and advance the lexer's next_token pointer
1051 to point to the next non-purged token. */
1052
1053 static cp_token *
1054 cp_lexer_consume_token (cp_lexer* lexer)
1055 {
1056 cp_token *token = lexer->next_token;
1057
1058 gcc_assert (token != &eof_token);
1059 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1060
1061 do
1062 {
1063 lexer->next_token++;
1064 if (lexer->next_token == lexer->last_token)
1065 {
1066 lexer->next_token = &eof_token;
1067 break;
1068 }
1069
1070 }
1071 while (lexer->next_token->purged_p);
1072
1073 cp_lexer_set_source_position_from_token (token);
1074
1075 /* Provide debugging output. */
1076 if (cp_lexer_debugging_p (lexer))
1077 {
1078 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1079 cp_lexer_print_token (cp_lexer_debug_stream, token);
1080 putc ('\n', cp_lexer_debug_stream);
1081 }
1082
1083 return token;
1084 }
1085
1086 /* Permanently remove the next token from the token stream, and
1087 advance the next_token pointer to refer to the next non-purged
1088 token. */
1089
1090 static void
1091 cp_lexer_purge_token (cp_lexer *lexer)
1092 {
1093 cp_token *tok = lexer->next_token;
1094
1095 gcc_assert (tok != &eof_token);
1096 tok->purged_p = true;
1097 tok->location = UNKNOWN_LOCATION;
1098 tok->u.value = NULL_TREE;
1099 tok->keyword = RID_MAX;
1100
1101 do
1102 {
1103 tok++;
1104 if (tok == lexer->last_token)
1105 {
1106 tok = &eof_token;
1107 break;
1108 }
1109 }
1110 while (tok->purged_p);
1111 lexer->next_token = tok;
1112 }
1113
1114 /* Permanently remove all tokens after TOK, up to, but not
1115 including, the token that will be returned next by
1116 cp_lexer_peek_token. */
1117
1118 static void
1119 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1120 {
1121 cp_token *peek = lexer->next_token;
1122
1123 if (peek == &eof_token)
1124 peek = lexer->last_token;
1125
1126 gcc_assert (tok < peek);
1127
1128 for ( tok += 1; tok != peek; tok += 1)
1129 {
1130 tok->purged_p = true;
1131 tok->location = UNKNOWN_LOCATION;
1132 tok->u.value = NULL_TREE;
1133 tok->keyword = RID_MAX;
1134 }
1135 }
1136
1137 /* Begin saving tokens. All tokens consumed after this point will be
1138 preserved. */
1139
1140 static void
1141 cp_lexer_save_tokens (cp_lexer* lexer)
1142 {
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1146
1147 lexer->saved_tokens.safe_push (lexer->next_token);
1148 }
1149
1150 /* Commit to the portion of the token stream most recently saved. */
1151
1152 static void
1153 cp_lexer_commit_tokens (cp_lexer* lexer)
1154 {
1155 /* Provide debugging output. */
1156 if (cp_lexer_debugging_p (lexer))
1157 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1158
1159 lexer->saved_tokens.pop ();
1160 }
1161
1162 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1163 to the token stream. Stop saving tokens. */
1164
1165 static void
1166 cp_lexer_rollback_tokens (cp_lexer* lexer)
1167 {
1168 /* Provide debugging output. */
1169 if (cp_lexer_debugging_p (lexer))
1170 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1171
1172 lexer->next_token = lexer->saved_tokens.pop ();
1173 }
1174
1175 /* RAII wrapper around the above functions, with sanity checking. Creating
1176 a variable saves tokens, which are committed when the variable is
1177 destroyed unless they are explicitly rolled back by calling the rollback
1178 member function. */
1179
1180 struct saved_token_sentinel
1181 {
1182 cp_lexer *lexer;
1183 unsigned len;
1184 bool commit;
1185 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1186 {
1187 len = lexer->saved_tokens.length ();
1188 cp_lexer_save_tokens (lexer);
1189 }
1190 void rollback ()
1191 {
1192 cp_lexer_rollback_tokens (lexer);
1193 commit = false;
1194 }
1195 ~saved_token_sentinel()
1196 {
1197 if (commit)
1198 cp_lexer_commit_tokens (lexer);
1199 gcc_assert (lexer->saved_tokens.length () == len);
1200 }
1201 };
1202
1203 /* Print a representation of the TOKEN on the STREAM. */
1204
1205 static void
1206 cp_lexer_print_token (FILE * stream, cp_token *token)
1207 {
1208 /* We don't use cpp_type2name here because the parser defines
1209 a few tokens of its own. */
1210 static const char *const token_names[] = {
1211 /* cpplib-defined token types */
1212 #define OP(e, s) #e,
1213 #define TK(e, s) #e,
1214 TTYPE_TABLE
1215 #undef OP
1216 #undef TK
1217 /* C++ parser token types - see "Manifest constants", above. */
1218 "KEYWORD",
1219 "TEMPLATE_ID",
1220 "NESTED_NAME_SPECIFIER",
1221 };
1222
1223 /* For some tokens, print the associated data. */
1224 switch (token->type)
1225 {
1226 case CPP_KEYWORD:
1227 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1228 For example, `struct' is mapped to an INTEGER_CST. */
1229 if (!identifier_p (token->u.value))
1230 break;
1231 /* else fall through */
1232 case CPP_NAME:
1233 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1234 break;
1235
1236 case CPP_STRING:
1237 case CPP_STRING16:
1238 case CPP_STRING32:
1239 case CPP_WSTRING:
1240 case CPP_UTF8STRING:
1241 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1242 break;
1243
1244 case CPP_NUMBER:
1245 print_generic_expr (stream, token->u.value, 0);
1246 break;
1247
1248 default:
1249 /* If we have a name for the token, print it out. Otherwise, we
1250 simply give the numeric code. */
1251 if (token->type < ARRAY_SIZE(token_names))
1252 fputs (token_names[token->type], stream);
1253 else
1254 fprintf (stream, "[%d]", token->type);
1255 break;
1256 }
1257 }
1258
1259 DEBUG_FUNCTION void
1260 debug (cp_token &ref)
1261 {
1262 cp_lexer_print_token (stderr, &ref);
1263 fprintf (stderr, "\n");
1264 }
1265
1266 DEBUG_FUNCTION void
1267 debug (cp_token *ptr)
1268 {
1269 if (ptr)
1270 debug (*ptr);
1271 else
1272 fprintf (stderr, "<nil>\n");
1273 }
1274
1275
1276 /* Start emitting debugging information. */
1277
1278 static void
1279 cp_lexer_start_debugging (cp_lexer* lexer)
1280 {
1281 lexer->debugging_p = true;
1282 cp_lexer_debug_stream = stderr;
1283 }
1284
1285 /* Stop emitting debugging information. */
1286
1287 static void
1288 cp_lexer_stop_debugging (cp_lexer* lexer)
1289 {
1290 lexer->debugging_p = false;
1291 cp_lexer_debug_stream = NULL;
1292 }
1293
1294 /* Create a new cp_token_cache, representing a range of tokens. */
1295
1296 static cp_token_cache *
1297 cp_token_cache_new (cp_token *first, cp_token *last)
1298 {
1299 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1300 cache->first = first;
1301 cache->last = last;
1302 return cache;
1303 }
1304
1305 /* Diagnose if #pragma omp declare simd isn't followed immediately
1306 by function declaration or definition. */
1307
1308 static inline void
1309 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1310 {
1311 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1312 {
1313 error ("%<#pragma omp declare simd%> not immediately followed by "
1314 "function declaration or definition");
1315 parser->omp_declare_simd = NULL;
1316 }
1317 }
1318
1319 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1320 and put that into "omp declare simd" attribute. */
1321
1322 static inline void
1323 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1324 {
1325 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1326 {
1327 if (fndecl == error_mark_node)
1328 {
1329 parser->omp_declare_simd = NULL;
1330 return;
1331 }
1332 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1333 {
1334 cp_ensure_no_omp_declare_simd (parser);
1335 return;
1336 }
1337 }
1338 }
1339 \f
1340 /* Decl-specifiers. */
1341
1342 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1343
1344 static void
1345 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1346 {
1347 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1348 }
1349
1350 /* Declarators. */
1351
1352 /* Nothing other than the parser should be creating declarators;
1353 declarators are a semi-syntactic representation of C++ entities.
1354 Other parts of the front end that need to create entities (like
1355 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1356
1357 static cp_declarator *make_call_declarator
1358 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1359 static cp_declarator *make_array_declarator
1360 (cp_declarator *, tree);
1361 static cp_declarator *make_pointer_declarator
1362 (cp_cv_quals, cp_declarator *, tree);
1363 static cp_declarator *make_reference_declarator
1364 (cp_cv_quals, cp_declarator *, bool, tree);
1365 static cp_parameter_declarator *make_parameter_declarator
1366 (cp_decl_specifier_seq *, cp_declarator *, tree);
1367 static cp_declarator *make_ptrmem_declarator
1368 (cp_cv_quals, tree, cp_declarator *, tree);
1369
1370 /* An erroneous declarator. */
1371 static cp_declarator *cp_error_declarator;
1372
1373 /* The obstack on which declarators and related data structures are
1374 allocated. */
1375 static struct obstack declarator_obstack;
1376
1377 /* Alloc BYTES from the declarator memory pool. */
1378
1379 static inline void *
1380 alloc_declarator (size_t bytes)
1381 {
1382 return obstack_alloc (&declarator_obstack, bytes);
1383 }
1384
1385 /* Allocate a declarator of the indicated KIND. Clear fields that are
1386 common to all declarators. */
1387
1388 static cp_declarator *
1389 make_declarator (cp_declarator_kind kind)
1390 {
1391 cp_declarator *declarator;
1392
1393 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1394 declarator->kind = kind;
1395 declarator->attributes = NULL_TREE;
1396 declarator->std_attributes = NULL_TREE;
1397 declarator->declarator = NULL;
1398 declarator->parameter_pack_p = false;
1399 declarator->id_loc = UNKNOWN_LOCATION;
1400
1401 return declarator;
1402 }
1403
1404 /* Make a declarator for a generalized identifier. If
1405 QUALIFYING_SCOPE is non-NULL, the identifier is
1406 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1407 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1408 is, if any. */
1409
1410 static cp_declarator *
1411 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1412 special_function_kind sfk)
1413 {
1414 cp_declarator *declarator;
1415
1416 /* It is valid to write:
1417
1418 class C { void f(); };
1419 typedef C D;
1420 void D::f();
1421
1422 The standard is not clear about whether `typedef const C D' is
1423 legal; as of 2002-09-15 the committee is considering that
1424 question. EDG 3.0 allows that syntax. Therefore, we do as
1425 well. */
1426 if (qualifying_scope && TYPE_P (qualifying_scope))
1427 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1428
1429 gcc_assert (identifier_p (unqualified_name)
1430 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1431 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1432
1433 declarator = make_declarator (cdk_id);
1434 declarator->u.id.qualifying_scope = qualifying_scope;
1435 declarator->u.id.unqualified_name = unqualified_name;
1436 declarator->u.id.sfk = sfk;
1437
1438 return declarator;
1439 }
1440
1441 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1442 of modifiers such as const or volatile to apply to the pointer
1443 type, represented as identifiers. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1445
1446 cp_declarator *
1447 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1448 tree attributes)
1449 {
1450 cp_declarator *declarator;
1451
1452 declarator = make_declarator (cdk_pointer);
1453 declarator->declarator = target;
1454 declarator->u.pointer.qualifiers = cv_qualifiers;
1455 declarator->u.pointer.class_type = NULL_TREE;
1456 if (target)
1457 {
1458 declarator->id_loc = target->id_loc;
1459 declarator->parameter_pack_p = target->parameter_pack_p;
1460 target->parameter_pack_p = false;
1461 }
1462 else
1463 declarator->parameter_pack_p = false;
1464
1465 declarator->std_attributes = attributes;
1466
1467 return declarator;
1468 }
1469
1470 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1471 represent the attributes that appertain to the pointer or
1472 reference. */
1473
1474 cp_declarator *
1475 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1476 bool rvalue_ref, tree attributes)
1477 {
1478 cp_declarator *declarator;
1479
1480 declarator = make_declarator (cdk_reference);
1481 declarator->declarator = target;
1482 declarator->u.reference.qualifiers = cv_qualifiers;
1483 declarator->u.reference.rvalue_ref = rvalue_ref;
1484 if (target)
1485 {
1486 declarator->id_loc = target->id_loc;
1487 declarator->parameter_pack_p = target->parameter_pack_p;
1488 target->parameter_pack_p = false;
1489 }
1490 else
1491 declarator->parameter_pack_p = false;
1492
1493 declarator->std_attributes = attributes;
1494
1495 return declarator;
1496 }
1497
1498 /* Like make_pointer_declarator -- but for a pointer to a non-static
1499 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1500 appertain to the pointer or reference. */
1501
1502 cp_declarator *
1503 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1504 cp_declarator *pointee,
1505 tree attributes)
1506 {
1507 cp_declarator *declarator;
1508
1509 declarator = make_declarator (cdk_ptrmem);
1510 declarator->declarator = pointee;
1511 declarator->u.pointer.qualifiers = cv_qualifiers;
1512 declarator->u.pointer.class_type = class_type;
1513
1514 if (pointee)
1515 {
1516 declarator->parameter_pack_p = pointee->parameter_pack_p;
1517 pointee->parameter_pack_p = false;
1518 }
1519 else
1520 declarator->parameter_pack_p = false;
1521
1522 declarator->std_attributes = attributes;
1523
1524 return declarator;
1525 }
1526
1527 /* Make a declarator for the function given by TARGET, with the
1528 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1529 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1530 indicates what exceptions can be thrown. */
1531
1532 cp_declarator *
1533 make_call_declarator (cp_declarator *target,
1534 tree parms,
1535 cp_cv_quals cv_qualifiers,
1536 cp_virt_specifiers virt_specifiers,
1537 cp_ref_qualifier ref_qualifier,
1538 tree exception_specification,
1539 tree late_return_type)
1540 {
1541 cp_declarator *declarator;
1542
1543 declarator = make_declarator (cdk_function);
1544 declarator->declarator = target;
1545 declarator->u.function.parameters = parms;
1546 declarator->u.function.qualifiers = cv_qualifiers;
1547 declarator->u.function.virt_specifiers = virt_specifiers;
1548 declarator->u.function.ref_qualifier = ref_qualifier;
1549 declarator->u.function.exception_specification = exception_specification;
1550 declarator->u.function.late_return_type = late_return_type;
1551 if (target)
1552 {
1553 declarator->id_loc = target->id_loc;
1554 declarator->parameter_pack_p = target->parameter_pack_p;
1555 target->parameter_pack_p = false;
1556 }
1557 else
1558 declarator->parameter_pack_p = false;
1559
1560 return declarator;
1561 }
1562
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1565
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1568 {
1569 cp_declarator *declarator;
1570
1571 declarator = make_declarator (cdk_array);
1572 declarator->declarator = element;
1573 declarator->u.array.bounds = bounds;
1574 if (element)
1575 {
1576 declarator->id_loc = element->id_loc;
1577 declarator->parameter_pack_p = element->parameter_pack_p;
1578 element->parameter_pack_p = false;
1579 }
1580 else
1581 declarator->parameter_pack_p = false;
1582
1583 return declarator;
1584 }
1585
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1588 static bool
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1590 {
1591 /* Search for a declarator name, or any other declarator that goes
1592 after the point where the ellipsis could appear in a parameter
1593 pack. If we find any of these, then this declarator can not be
1594 made into a parameter pack. */
1595 bool found = false;
1596 while (declarator && !found)
1597 {
1598 switch ((int)declarator->kind)
1599 {
1600 case cdk_id:
1601 case cdk_array:
1602 found = true;
1603 break;
1604
1605 case cdk_error:
1606 return true;
1607
1608 default:
1609 declarator = declarator->declarator;
1610 break;
1611 }
1612 }
1613
1614 return !found;
1615 }
1616
1617 cp_parameter_declarator *no_parameters;
1618
1619 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1620 DECLARATOR and DEFAULT_ARGUMENT. */
1621
1622 cp_parameter_declarator *
1623 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1624 cp_declarator *declarator,
1625 tree default_argument)
1626 {
1627 cp_parameter_declarator *parameter;
1628
1629 parameter = ((cp_parameter_declarator *)
1630 alloc_declarator (sizeof (cp_parameter_declarator)));
1631 parameter->next = NULL;
1632 if (decl_specifiers)
1633 parameter->decl_specifiers = *decl_specifiers;
1634 else
1635 clear_decl_specs (&parameter->decl_specifiers);
1636 parameter->declarator = declarator;
1637 parameter->default_argument = default_argument;
1638 parameter->ellipsis_p = false;
1639
1640 return parameter;
1641 }
1642
1643 /* Returns true iff DECLARATOR is a declaration for a function. */
1644
1645 static bool
1646 function_declarator_p (const cp_declarator *declarator)
1647 {
1648 while (declarator)
1649 {
1650 if (declarator->kind == cdk_function
1651 && declarator->declarator->kind == cdk_id)
1652 return true;
1653 if (declarator->kind == cdk_id
1654 || declarator->kind == cdk_error)
1655 return false;
1656 declarator = declarator->declarator;
1657 }
1658 return false;
1659 }
1660
1661 /* The parser. */
1662
1663 /* Overview
1664 --------
1665
1666 A cp_parser parses the token stream as specified by the C++
1667 grammar. Its job is purely parsing, not semantic analysis. For
1668 example, the parser breaks the token stream into declarators,
1669 expressions, statements, and other similar syntactic constructs.
1670 It does not check that the types of the expressions on either side
1671 of an assignment-statement are compatible, or that a function is
1672 not declared with a parameter of type `void'.
1673
1674 The parser invokes routines elsewhere in the compiler to perform
1675 semantic analysis and to build up the abstract syntax tree for the
1676 code processed.
1677
1678 The parser (and the template instantiation code, which is, in a
1679 way, a close relative of parsing) are the only parts of the
1680 compiler that should be calling push_scope and pop_scope, or
1681 related functions. The parser (and template instantiation code)
1682 keeps track of what scope is presently active; everything else
1683 should simply honor that. (The code that generates static
1684 initializers may also need to set the scope, in order to check
1685 access control correctly when emitting the initializers.)
1686
1687 Methodology
1688 -----------
1689
1690 The parser is of the standard recursive-descent variety. Upcoming
1691 tokens in the token stream are examined in order to determine which
1692 production to use when parsing a non-terminal. Some C++ constructs
1693 require arbitrary look ahead to disambiguate. For example, it is
1694 impossible, in the general case, to tell whether a statement is an
1695 expression or declaration without scanning the entire statement.
1696 Therefore, the parser is capable of "parsing tentatively." When the
1697 parser is not sure what construct comes next, it enters this mode.
1698 Then, while we attempt to parse the construct, the parser queues up
1699 error messages, rather than issuing them immediately, and saves the
1700 tokens it consumes. If the construct is parsed successfully, the
1701 parser "commits", i.e., it issues any queued error messages and
1702 the tokens that were being preserved are permanently discarded.
1703 If, however, the construct is not parsed successfully, the parser
1704 rolls back its state completely so that it can resume parsing using
1705 a different alternative.
1706
1707 Future Improvements
1708 -------------------
1709
1710 The performance of the parser could probably be improved substantially.
1711 We could often eliminate the need to parse tentatively by looking ahead
1712 a little bit. In some places, this approach might not entirely eliminate
1713 the need to parse tentatively, but it might still speed up the average
1714 case. */
1715
1716 /* Flags that are passed to some parsing functions. These values can
1717 be bitwise-ored together. */
1718
1719 enum
1720 {
1721 /* No flags. */
1722 CP_PARSER_FLAGS_NONE = 0x0,
1723 /* The construct is optional. If it is not present, then no error
1724 should be issued. */
1725 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1726 /* When parsing a type-specifier, treat user-defined type-names
1727 as non-type identifiers. */
1728 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1729 /* When parsing a type-specifier, do not try to parse a class-specifier
1730 or enum-specifier. */
1731 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1732 /* When parsing a decl-specifier-seq, only allow type-specifier or
1733 constexpr. */
1734 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1735 };
1736
1737 /* This type is used for parameters and variables which hold
1738 combinations of the above flags. */
1739 typedef int cp_parser_flags;
1740
1741 /* The different kinds of declarators we want to parse. */
1742
1743 typedef enum cp_parser_declarator_kind
1744 {
1745 /* We want an abstract declarator. */
1746 CP_PARSER_DECLARATOR_ABSTRACT,
1747 /* We want a named declarator. */
1748 CP_PARSER_DECLARATOR_NAMED,
1749 /* We don't mind, but the name must be an unqualified-id. */
1750 CP_PARSER_DECLARATOR_EITHER
1751 } cp_parser_declarator_kind;
1752
1753 /* The precedence values used to parse binary expressions. The minimum value
1754 of PREC must be 1, because zero is reserved to quickly discriminate
1755 binary operators from other tokens. */
1756
1757 enum cp_parser_prec
1758 {
1759 PREC_NOT_OPERATOR,
1760 PREC_LOGICAL_OR_EXPRESSION,
1761 PREC_LOGICAL_AND_EXPRESSION,
1762 PREC_INCLUSIVE_OR_EXPRESSION,
1763 PREC_EXCLUSIVE_OR_EXPRESSION,
1764 PREC_AND_EXPRESSION,
1765 PREC_EQUALITY_EXPRESSION,
1766 PREC_RELATIONAL_EXPRESSION,
1767 PREC_SHIFT_EXPRESSION,
1768 PREC_ADDITIVE_EXPRESSION,
1769 PREC_MULTIPLICATIVE_EXPRESSION,
1770 PREC_PM_EXPRESSION,
1771 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1772 };
1773
1774 /* A mapping from a token type to a corresponding tree node type, with a
1775 precedence value. */
1776
1777 typedef struct cp_parser_binary_operations_map_node
1778 {
1779 /* The token type. */
1780 enum cpp_ttype token_type;
1781 /* The corresponding tree code. */
1782 enum tree_code tree_type;
1783 /* The precedence of this operator. */
1784 enum cp_parser_prec prec;
1785 } cp_parser_binary_operations_map_node;
1786
1787 typedef struct cp_parser_expression_stack_entry
1788 {
1789 /* Left hand side of the binary operation we are currently
1790 parsing. */
1791 tree lhs;
1792 /* Original tree code for left hand side, if it was a binary
1793 expression itself (used for -Wparentheses). */
1794 enum tree_code lhs_type;
1795 /* Tree code for the binary operation we are parsing. */
1796 enum tree_code tree_type;
1797 /* Precedence of the binary operation we are parsing. */
1798 enum cp_parser_prec prec;
1799 /* Location of the binary operation we are parsing. */
1800 location_t loc;
1801 } cp_parser_expression_stack_entry;
1802
1803 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1804 entries because precedence levels on the stack are monotonically
1805 increasing. */
1806 typedef struct cp_parser_expression_stack_entry
1807 cp_parser_expression_stack[NUM_PREC_VALUES];
1808
1809 /* Prototypes. */
1810
1811 /* Constructors and destructors. */
1812
1813 static cp_parser_context *cp_parser_context_new
1814 (cp_parser_context *);
1815
1816 /* Class variables. */
1817
1818 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1819
1820 /* The operator-precedence table used by cp_parser_binary_expression.
1821 Transformed into an associative array (binops_by_token) by
1822 cp_parser_new. */
1823
1824 static const cp_parser_binary_operations_map_node binops[] = {
1825 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1826 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1827
1828 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1829 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1830 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1831
1832 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1833 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1834
1835 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1836 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1837
1838 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1839 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1840 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1841 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1842
1843 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1844 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1845
1846 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1847
1848 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1849
1850 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1851
1852 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1853
1854 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1855 };
1856
1857 /* The same as binops, but initialized by cp_parser_new so that
1858 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1859 for speed. */
1860 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1861
1862 /* Constructors and destructors. */
1863
1864 /* Construct a new context. The context below this one on the stack
1865 is given by NEXT. */
1866
1867 static cp_parser_context *
1868 cp_parser_context_new (cp_parser_context* next)
1869 {
1870 cp_parser_context *context;
1871
1872 /* Allocate the storage. */
1873 if (cp_parser_context_free_list != NULL)
1874 {
1875 /* Pull the first entry from the free list. */
1876 context = cp_parser_context_free_list;
1877 cp_parser_context_free_list = context->next;
1878 memset (context, 0, sizeof (*context));
1879 }
1880 else
1881 context = ggc_cleared_alloc<cp_parser_context> ();
1882
1883 /* No errors have occurred yet in this context. */
1884 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1885 /* If this is not the bottommost context, copy information that we
1886 need from the previous context. */
1887 if (next)
1888 {
1889 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1890 expression, then we are parsing one in this context, too. */
1891 context->object_type = next->object_type;
1892 /* Thread the stack. */
1893 context->next = next;
1894 }
1895
1896 return context;
1897 }
1898
1899 /* Managing the unparsed function queues. */
1900
1901 #define unparsed_funs_with_default_args \
1902 parser->unparsed_queues->last ().funs_with_default_args
1903 #define unparsed_funs_with_definitions \
1904 parser->unparsed_queues->last ().funs_with_definitions
1905 #define unparsed_nsdmis \
1906 parser->unparsed_queues->last ().nsdmis
1907 #define unparsed_classes \
1908 parser->unparsed_queues->last ().classes
1909
1910 static void
1911 push_unparsed_function_queues (cp_parser *parser)
1912 {
1913 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1914 vec_safe_push (parser->unparsed_queues, e);
1915 }
1916
1917 static void
1918 pop_unparsed_function_queues (cp_parser *parser)
1919 {
1920 release_tree_vector (unparsed_funs_with_definitions);
1921 parser->unparsed_queues->pop ();
1922 }
1923
1924 /* Prototypes. */
1925
1926 /* Constructors and destructors. */
1927
1928 static cp_parser *cp_parser_new
1929 (void);
1930
1931 /* Routines to parse various constructs.
1932
1933 Those that return `tree' will return the error_mark_node (rather
1934 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1935 Sometimes, they will return an ordinary node if error-recovery was
1936 attempted, even though a parse error occurred. So, to check
1937 whether or not a parse error occurred, you should always use
1938 cp_parser_error_occurred. If the construct is optional (indicated
1939 either by an `_opt' in the name of the function that does the
1940 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1941 the construct is not present. */
1942
1943 /* Lexical conventions [gram.lex] */
1944
1945 static tree cp_parser_identifier
1946 (cp_parser *);
1947 static tree cp_parser_string_literal
1948 (cp_parser *, bool, bool, bool);
1949 static tree cp_parser_userdef_char_literal
1950 (cp_parser *);
1951 static tree cp_parser_userdef_string_literal
1952 (tree);
1953 static tree cp_parser_userdef_numeric_literal
1954 (cp_parser *);
1955
1956 /* Basic concepts [gram.basic] */
1957
1958 static bool cp_parser_translation_unit
1959 (cp_parser *);
1960
1961 /* Expressions [gram.expr] */
1962
1963 static tree cp_parser_primary_expression
1964 (cp_parser *, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_id_expression
1966 (cp_parser *, bool, bool, bool *, bool, bool);
1967 static tree cp_parser_unqualified_id
1968 (cp_parser *, bool, bool, bool, bool);
1969 static tree cp_parser_nested_name_specifier_opt
1970 (cp_parser *, bool, bool, bool, bool);
1971 static tree cp_parser_nested_name_specifier
1972 (cp_parser *, bool, bool, bool, bool);
1973 static tree cp_parser_qualifying_entity
1974 (cp_parser *, bool, bool, bool, bool, bool);
1975 static tree cp_parser_postfix_expression
1976 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1977 static tree cp_parser_postfix_open_square_expression
1978 (cp_parser *, tree, bool, bool);
1979 static tree cp_parser_postfix_dot_deref_expression
1980 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1981 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1982 (cp_parser *, int, bool, bool, bool *, bool = false);
1983 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1984 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1985 static void cp_parser_pseudo_destructor_name
1986 (cp_parser *, tree, tree *, tree *);
1987 static tree cp_parser_unary_expression
1988 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1989 static enum tree_code cp_parser_unary_operator
1990 (cp_token *);
1991 static tree cp_parser_new_expression
1992 (cp_parser *);
1993 static vec<tree, va_gc> *cp_parser_new_placement
1994 (cp_parser *);
1995 static tree cp_parser_new_type_id
1996 (cp_parser *, tree *);
1997 static cp_declarator *cp_parser_new_declarator_opt
1998 (cp_parser *);
1999 static cp_declarator *cp_parser_direct_new_declarator
2000 (cp_parser *);
2001 static vec<tree, va_gc> *cp_parser_new_initializer
2002 (cp_parser *);
2003 static tree cp_parser_delete_expression
2004 (cp_parser *);
2005 static tree cp_parser_cast_expression
2006 (cp_parser *, bool, bool, bool, cp_id_kind *);
2007 static tree cp_parser_binary_expression
2008 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2009 static tree cp_parser_question_colon_clause
2010 (cp_parser *, tree);
2011 static tree cp_parser_assignment_expression
2012 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2013 static enum tree_code cp_parser_assignment_operator_opt
2014 (cp_parser *);
2015 static tree cp_parser_expression
2016 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2017 static tree cp_parser_constant_expression
2018 (cp_parser *, bool = false, bool * = NULL);
2019 static tree cp_parser_builtin_offsetof
2020 (cp_parser *);
2021 static tree cp_parser_lambda_expression
2022 (cp_parser *);
2023 static void cp_parser_lambda_introducer
2024 (cp_parser *, tree);
2025 static bool cp_parser_lambda_declarator_opt
2026 (cp_parser *, tree);
2027 static void cp_parser_lambda_body
2028 (cp_parser *, tree);
2029
2030 /* Statements [gram.stmt.stmt] */
2031
2032 static void cp_parser_statement
2033 (cp_parser *, tree, bool, bool *);
2034 static void cp_parser_label_for_labeled_statement
2035 (cp_parser *, tree);
2036 static tree cp_parser_expression_statement
2037 (cp_parser *, tree);
2038 static tree cp_parser_compound_statement
2039 (cp_parser *, tree, bool, bool);
2040 static void cp_parser_statement_seq_opt
2041 (cp_parser *, tree);
2042 static tree cp_parser_selection_statement
2043 (cp_parser *, bool *);
2044 static tree cp_parser_condition
2045 (cp_parser *);
2046 static tree cp_parser_iteration_statement
2047 (cp_parser *, bool);
2048 static bool cp_parser_for_init_statement
2049 (cp_parser *, tree *decl);
2050 static tree cp_parser_for
2051 (cp_parser *, bool);
2052 static tree cp_parser_c_for
2053 (cp_parser *, tree, tree, bool);
2054 static tree cp_parser_range_for
2055 (cp_parser *, tree, tree, tree, bool);
2056 static void do_range_for_auto_deduction
2057 (tree, tree);
2058 static tree cp_parser_perform_range_for_lookup
2059 (tree, tree *, tree *);
2060 static tree cp_parser_range_for_member_function
2061 (tree, tree);
2062 static tree cp_parser_jump_statement
2063 (cp_parser *);
2064 static void cp_parser_declaration_statement
2065 (cp_parser *);
2066
2067 static tree cp_parser_implicitly_scoped_statement
2068 (cp_parser *, bool *, location_t, const char *);
2069 static void cp_parser_already_scoped_statement
2070 (cp_parser *, location_t, const char *);
2071
2072 /* Declarations [gram.dcl.dcl] */
2073
2074 static void cp_parser_declaration_seq_opt
2075 (cp_parser *);
2076 static void cp_parser_declaration
2077 (cp_parser *);
2078 static void cp_parser_block_declaration
2079 (cp_parser *, bool);
2080 static void cp_parser_simple_declaration
2081 (cp_parser *, bool, tree *);
2082 static void cp_parser_decl_specifier_seq
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2084 static tree cp_parser_storage_class_specifier_opt
2085 (cp_parser *);
2086 static tree cp_parser_function_specifier_opt
2087 (cp_parser *, cp_decl_specifier_seq *);
2088 static tree cp_parser_type_specifier
2089 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2090 int *, bool *);
2091 static tree cp_parser_simple_type_specifier
2092 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2093 static tree cp_parser_type_name
2094 (cp_parser *);
2095 static tree cp_parser_nonclass_name
2096 (cp_parser* parser);
2097 static tree cp_parser_elaborated_type_specifier
2098 (cp_parser *, bool, bool);
2099 static tree cp_parser_enum_specifier
2100 (cp_parser *);
2101 static void cp_parser_enumerator_list
2102 (cp_parser *, tree);
2103 static void cp_parser_enumerator_definition
2104 (cp_parser *, tree);
2105 static tree cp_parser_namespace_name
2106 (cp_parser *);
2107 static void cp_parser_namespace_definition
2108 (cp_parser *);
2109 static void cp_parser_namespace_body
2110 (cp_parser *);
2111 static tree cp_parser_qualified_namespace_specifier
2112 (cp_parser *);
2113 static void cp_parser_namespace_alias_definition
2114 (cp_parser *);
2115 static bool cp_parser_using_declaration
2116 (cp_parser *, bool);
2117 static void cp_parser_using_directive
2118 (cp_parser *);
2119 static tree cp_parser_alias_declaration
2120 (cp_parser *);
2121 static void cp_parser_asm_definition
2122 (cp_parser *);
2123 static void cp_parser_linkage_specification
2124 (cp_parser *);
2125 static void cp_parser_static_assert
2126 (cp_parser *, bool);
2127 static tree cp_parser_decltype
2128 (cp_parser *);
2129
2130 /* Declarators [gram.dcl.decl] */
2131
2132 static tree cp_parser_init_declarator
2133 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2134 bool, bool, int, bool *, tree *, location_t *);
2135 static cp_declarator *cp_parser_declarator
2136 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2137 static cp_declarator *cp_parser_direct_declarator
2138 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2139 static enum tree_code cp_parser_ptr_operator
2140 (cp_parser *, tree *, cp_cv_quals *, tree *);
2141 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142 (cp_parser *);
2143 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144 (cp_parser *);
2145 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_late_return_type_opt
2148 (cp_parser *, cp_declarator *, cp_cv_quals);
2149 static tree cp_parser_declarator_id
2150 (cp_parser *, bool);
2151 static tree cp_parser_type_id
2152 (cp_parser *);
2153 static tree cp_parser_template_type_arg
2154 (cp_parser *);
2155 static tree cp_parser_trailing_type_id (cp_parser *);
2156 static tree cp_parser_type_id_1
2157 (cp_parser *, bool, bool);
2158 static void cp_parser_type_specifier_seq
2159 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2160 static tree cp_parser_parameter_declaration_clause
2161 (cp_parser *);
2162 static tree cp_parser_parameter_declaration_list
2163 (cp_parser *, bool *);
2164 static cp_parameter_declarator *cp_parser_parameter_declaration
2165 (cp_parser *, bool, bool *);
2166 static tree cp_parser_default_argument
2167 (cp_parser *, bool);
2168 static void cp_parser_function_body
2169 (cp_parser *, bool);
2170 static tree cp_parser_initializer
2171 (cp_parser *, bool *, bool *);
2172 static tree cp_parser_initializer_clause
2173 (cp_parser *, bool *);
2174 static tree cp_parser_braced_list
2175 (cp_parser*, bool*);
2176 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2177 (cp_parser *, bool *);
2178
2179 static bool cp_parser_ctor_initializer_opt_and_function_body
2180 (cp_parser *, bool);
2181
2182 static tree cp_parser_late_parsing_omp_declare_simd
2183 (cp_parser *, tree);
2184
2185 static tree cp_parser_late_parsing_cilk_simd_fn_info
2186 (cp_parser *, tree);
2187
2188 static tree synthesize_implicit_template_parm
2189 (cp_parser *);
2190 static tree finish_fully_implicit_template
2191 (cp_parser *, tree);
2192
2193 /* Classes [gram.class] */
2194
2195 static tree cp_parser_class_name
2196 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2197 static tree cp_parser_class_specifier
2198 (cp_parser *);
2199 static tree cp_parser_class_head
2200 (cp_parser *, bool *);
2201 static enum tag_types cp_parser_class_key
2202 (cp_parser *);
2203 static void cp_parser_type_parameter_key
2204 (cp_parser* parser);
2205 static void cp_parser_member_specification_opt
2206 (cp_parser *);
2207 static void cp_parser_member_declaration
2208 (cp_parser *);
2209 static tree cp_parser_pure_specifier
2210 (cp_parser *);
2211 static tree cp_parser_constant_initializer
2212 (cp_parser *);
2213
2214 /* Derived classes [gram.class.derived] */
2215
2216 static tree cp_parser_base_clause
2217 (cp_parser *);
2218 static tree cp_parser_base_specifier
2219 (cp_parser *);
2220
2221 /* Special member functions [gram.special] */
2222
2223 static tree cp_parser_conversion_function_id
2224 (cp_parser *);
2225 static tree cp_parser_conversion_type_id
2226 (cp_parser *);
2227 static cp_declarator *cp_parser_conversion_declarator_opt
2228 (cp_parser *);
2229 static bool cp_parser_ctor_initializer_opt
2230 (cp_parser *);
2231 static void cp_parser_mem_initializer_list
2232 (cp_parser *);
2233 static tree cp_parser_mem_initializer
2234 (cp_parser *);
2235 static tree cp_parser_mem_initializer_id
2236 (cp_parser *);
2237
2238 /* Overloading [gram.over] */
2239
2240 static tree cp_parser_operator_function_id
2241 (cp_parser *);
2242 static tree cp_parser_operator
2243 (cp_parser *);
2244
2245 /* Templates [gram.temp] */
2246
2247 static void cp_parser_template_declaration
2248 (cp_parser *, bool);
2249 static tree cp_parser_template_parameter_list
2250 (cp_parser *);
2251 static tree cp_parser_template_parameter
2252 (cp_parser *, bool *, bool *);
2253 static tree cp_parser_type_parameter
2254 (cp_parser *, bool *);
2255 static tree cp_parser_template_id
2256 (cp_parser *, bool, bool, enum tag_types, bool);
2257 static tree cp_parser_template_name
2258 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2259 static tree cp_parser_template_argument_list
2260 (cp_parser *);
2261 static tree cp_parser_template_argument
2262 (cp_parser *);
2263 static void cp_parser_explicit_instantiation
2264 (cp_parser *);
2265 static void cp_parser_explicit_specialization
2266 (cp_parser *);
2267
2268 /* Exception handling [gram.exception] */
2269
2270 static tree cp_parser_try_block
2271 (cp_parser *);
2272 static bool cp_parser_function_try_block
2273 (cp_parser *);
2274 static void cp_parser_handler_seq
2275 (cp_parser *);
2276 static void cp_parser_handler
2277 (cp_parser *);
2278 static tree cp_parser_exception_declaration
2279 (cp_parser *);
2280 static tree cp_parser_throw_expression
2281 (cp_parser *);
2282 static tree cp_parser_exception_specification_opt
2283 (cp_parser *);
2284 static tree cp_parser_type_id_list
2285 (cp_parser *);
2286
2287 /* GNU Extensions */
2288
2289 static tree cp_parser_asm_specification_opt
2290 (cp_parser *);
2291 static tree cp_parser_asm_operand_list
2292 (cp_parser *);
2293 static tree cp_parser_asm_clobber_list
2294 (cp_parser *);
2295 static tree cp_parser_asm_label_list
2296 (cp_parser *);
2297 static bool cp_next_tokens_can_be_attribute_p
2298 (cp_parser *);
2299 static bool cp_next_tokens_can_be_gnu_attribute_p
2300 (cp_parser *);
2301 static bool cp_next_tokens_can_be_std_attribute_p
2302 (cp_parser *);
2303 static bool cp_nth_tokens_can_be_std_attribute_p
2304 (cp_parser *, size_t);
2305 static bool cp_nth_tokens_can_be_gnu_attribute_p
2306 (cp_parser *, size_t);
2307 static bool cp_nth_tokens_can_be_attribute_p
2308 (cp_parser *, size_t);
2309 static tree cp_parser_attributes_opt
2310 (cp_parser *);
2311 static tree cp_parser_gnu_attributes_opt
2312 (cp_parser *);
2313 static tree cp_parser_gnu_attribute_list
2314 (cp_parser *);
2315 static tree cp_parser_std_attribute
2316 (cp_parser *);
2317 static tree cp_parser_std_attribute_spec
2318 (cp_parser *);
2319 static tree cp_parser_std_attribute_spec_seq
2320 (cp_parser *);
2321 static bool cp_parser_extension_opt
2322 (cp_parser *, int *);
2323 static void cp_parser_label_declaration
2324 (cp_parser *);
2325
2326 /* Transactional Memory Extensions */
2327
2328 static tree cp_parser_transaction
2329 (cp_parser *, enum rid);
2330 static tree cp_parser_transaction_expression
2331 (cp_parser *, enum rid);
2332 static bool cp_parser_function_transaction
2333 (cp_parser *, enum rid);
2334 static tree cp_parser_transaction_cancel
2335 (cp_parser *);
2336
2337 enum pragma_context {
2338 pragma_external,
2339 pragma_member,
2340 pragma_objc_icode,
2341 pragma_stmt,
2342 pragma_compound
2343 };
2344 static bool cp_parser_pragma
2345 (cp_parser *, enum pragma_context);
2346
2347 /* Objective-C++ Productions */
2348
2349 static tree cp_parser_objc_message_receiver
2350 (cp_parser *);
2351 static tree cp_parser_objc_message_args
2352 (cp_parser *);
2353 static tree cp_parser_objc_message_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_encode_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_defs_expression
2358 (cp_parser *);
2359 static tree cp_parser_objc_protocol_expression
2360 (cp_parser *);
2361 static tree cp_parser_objc_selector_expression
2362 (cp_parser *);
2363 static tree cp_parser_objc_expression
2364 (cp_parser *);
2365 static bool cp_parser_objc_selector_p
2366 (enum cpp_ttype);
2367 static tree cp_parser_objc_selector
2368 (cp_parser *);
2369 static tree cp_parser_objc_protocol_refs_opt
2370 (cp_parser *);
2371 static void cp_parser_objc_declaration
2372 (cp_parser *, tree);
2373 static tree cp_parser_objc_statement
2374 (cp_parser *);
2375 static bool cp_parser_objc_valid_prefix_attributes
2376 (cp_parser *, tree *);
2377 static void cp_parser_objc_at_property_declaration
2378 (cp_parser *) ;
2379 static void cp_parser_objc_at_synthesize_declaration
2380 (cp_parser *) ;
2381 static void cp_parser_objc_at_dynamic_declaration
2382 (cp_parser *) ;
2383 static tree cp_parser_objc_struct_declaration
2384 (cp_parser *) ;
2385
2386 /* Utility Routines */
2387
2388 static tree cp_parser_lookup_name
2389 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2390 static tree cp_parser_lookup_name_simple
2391 (cp_parser *, tree, location_t);
2392 static tree cp_parser_maybe_treat_template_as_class
2393 (tree, bool);
2394 static bool cp_parser_check_declarator_template_parameters
2395 (cp_parser *, cp_declarator *, location_t);
2396 static bool cp_parser_check_template_parameters
2397 (cp_parser *, unsigned, location_t, cp_declarator *);
2398 static tree cp_parser_simple_cast_expression
2399 (cp_parser *);
2400 static tree cp_parser_global_scope_opt
2401 (cp_parser *, bool);
2402 static bool cp_parser_constructor_declarator_p
2403 (cp_parser *, bool);
2404 static tree cp_parser_function_definition_from_specifiers_and_declarator
2405 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2406 static tree cp_parser_function_definition_after_declarator
2407 (cp_parser *, bool);
2408 static void cp_parser_template_declaration_after_export
2409 (cp_parser *, bool);
2410 static void cp_parser_perform_template_parameter_access_checks
2411 (vec<deferred_access_check, va_gc> *);
2412 static tree cp_parser_single_declaration
2413 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2414 static tree cp_parser_functional_cast
2415 (cp_parser *, tree);
2416 static tree cp_parser_save_member_function_body
2417 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2418 static tree cp_parser_save_nsdmi
2419 (cp_parser *);
2420 static tree cp_parser_enclosed_template_argument_list
2421 (cp_parser *);
2422 static void cp_parser_save_default_args
2423 (cp_parser *, tree);
2424 static void cp_parser_late_parsing_for_member
2425 (cp_parser *, tree);
2426 static tree cp_parser_late_parse_one_default_arg
2427 (cp_parser *, tree, tree, tree);
2428 static void cp_parser_late_parsing_nsdmi
2429 (cp_parser *, tree);
2430 static void cp_parser_late_parsing_default_args
2431 (cp_parser *, tree);
2432 static tree cp_parser_sizeof_operand
2433 (cp_parser *, enum rid);
2434 static tree cp_parser_trait_expr
2435 (cp_parser *, enum rid);
2436 static bool cp_parser_declares_only_class_p
2437 (cp_parser *);
2438 static void cp_parser_set_storage_class
2439 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2440 static void cp_parser_set_decl_spec_type
2441 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2442 static void set_and_check_decl_spec_loc
2443 (cp_decl_specifier_seq *decl_specs,
2444 cp_decl_spec ds, cp_token *);
2445 static bool cp_parser_friend_p
2446 (const cp_decl_specifier_seq *);
2447 static void cp_parser_required_error
2448 (cp_parser *, required_token, bool);
2449 static cp_token *cp_parser_require
2450 (cp_parser *, enum cpp_ttype, required_token);
2451 static cp_token *cp_parser_require_keyword
2452 (cp_parser *, enum rid, required_token);
2453 static bool cp_parser_token_starts_function_definition_p
2454 (cp_token *);
2455 static bool cp_parser_next_token_starts_class_definition_p
2456 (cp_parser *);
2457 static bool cp_parser_next_token_ends_template_argument_p
2458 (cp_parser *);
2459 static bool cp_parser_nth_token_starts_template_argument_list_p
2460 (cp_parser *, size_t);
2461 static enum tag_types cp_parser_token_is_class_key
2462 (cp_token *);
2463 static enum tag_types cp_parser_token_is_type_parameter_key
2464 (cp_token *);
2465 static void cp_parser_check_class_key
2466 (enum tag_types, tree type);
2467 static void cp_parser_check_access_in_redeclaration
2468 (tree type, location_t location);
2469 static bool cp_parser_optional_template_keyword
2470 (cp_parser *);
2471 static void cp_parser_pre_parsed_nested_name_specifier
2472 (cp_parser *);
2473 static bool cp_parser_cache_group
2474 (cp_parser *, enum cpp_ttype, unsigned);
2475 static tree cp_parser_cache_defarg
2476 (cp_parser *parser, bool nsdmi);
2477 static void cp_parser_parse_tentatively
2478 (cp_parser *);
2479 static void cp_parser_commit_to_tentative_parse
2480 (cp_parser *);
2481 static void cp_parser_commit_to_topmost_tentative_parse
2482 (cp_parser *);
2483 static void cp_parser_abort_tentative_parse
2484 (cp_parser *);
2485 static bool cp_parser_parse_definitely
2486 (cp_parser *);
2487 static inline bool cp_parser_parsing_tentatively
2488 (cp_parser *);
2489 static bool cp_parser_uncommitted_to_tentative_parse_p
2490 (cp_parser *);
2491 static void cp_parser_error
2492 (cp_parser *, const char *);
2493 static void cp_parser_name_lookup_error
2494 (cp_parser *, tree, tree, name_lookup_error, location_t);
2495 static bool cp_parser_simulate_error
2496 (cp_parser *);
2497 static bool cp_parser_check_type_definition
2498 (cp_parser *);
2499 static void cp_parser_check_for_definition_in_return_type
2500 (cp_declarator *, tree, location_t type_location);
2501 static void cp_parser_check_for_invalid_template_id
2502 (cp_parser *, tree, enum tag_types, location_t location);
2503 static bool cp_parser_non_integral_constant_expression
2504 (cp_parser *, non_integral_constant);
2505 static void cp_parser_diagnose_invalid_type_name
2506 (cp_parser *, tree, location_t);
2507 static bool cp_parser_parse_and_diagnose_invalid_type_name
2508 (cp_parser *);
2509 static int cp_parser_skip_to_closing_parenthesis
2510 (cp_parser *, bool, bool, bool);
2511 static void cp_parser_skip_to_end_of_statement
2512 (cp_parser *);
2513 static void cp_parser_consume_semicolon_at_end_of_statement
2514 (cp_parser *);
2515 static void cp_parser_skip_to_end_of_block_or_statement
2516 (cp_parser *);
2517 static bool cp_parser_skip_to_closing_brace
2518 (cp_parser *);
2519 static void cp_parser_skip_to_end_of_template_parameter_list
2520 (cp_parser *);
2521 static void cp_parser_skip_to_pragma_eol
2522 (cp_parser*, cp_token *);
2523 static bool cp_parser_error_occurred
2524 (cp_parser *);
2525 static bool cp_parser_allow_gnu_extensions_p
2526 (cp_parser *);
2527 static bool cp_parser_is_pure_string_literal
2528 (cp_token *);
2529 static bool cp_parser_is_string_literal
2530 (cp_token *);
2531 static bool cp_parser_is_keyword
2532 (cp_token *, enum rid);
2533 static tree cp_parser_make_typename_type
2534 (cp_parser *, tree, location_t location);
2535 static cp_declarator * cp_parser_make_indirect_declarator
2536 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2537 static bool cp_parser_compound_literal_p
2538 (cp_parser *);
2539 static bool cp_parser_array_designator_p
2540 (cp_parser *);
2541 static bool cp_parser_skip_to_closing_square_bracket
2542 (cp_parser *);
2543
2544 /* Returns nonzero if we are parsing tentatively. */
2545
2546 static inline bool
2547 cp_parser_parsing_tentatively (cp_parser* parser)
2548 {
2549 return parser->context->next != NULL;
2550 }
2551
2552 /* Returns nonzero if TOKEN is a string literal. */
2553
2554 static bool
2555 cp_parser_is_pure_string_literal (cp_token* token)
2556 {
2557 return (token->type == CPP_STRING ||
2558 token->type == CPP_STRING16 ||
2559 token->type == CPP_STRING32 ||
2560 token->type == CPP_WSTRING ||
2561 token->type == CPP_UTF8STRING);
2562 }
2563
2564 /* Returns nonzero if TOKEN is a string literal
2565 of a user-defined string literal. */
2566
2567 static bool
2568 cp_parser_is_string_literal (cp_token* token)
2569 {
2570 return (cp_parser_is_pure_string_literal (token) ||
2571 token->type == CPP_STRING_USERDEF ||
2572 token->type == CPP_STRING16_USERDEF ||
2573 token->type == CPP_STRING32_USERDEF ||
2574 token->type == CPP_WSTRING_USERDEF ||
2575 token->type == CPP_UTF8STRING_USERDEF);
2576 }
2577
2578 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2579
2580 static bool
2581 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2582 {
2583 return token->keyword == keyword;
2584 }
2585
2586 /* If not parsing tentatively, issue a diagnostic of the form
2587 FILE:LINE: MESSAGE before TOKEN
2588 where TOKEN is the next token in the input stream. MESSAGE
2589 (specified by the caller) is usually of the form "expected
2590 OTHER-TOKEN". */
2591
2592 static void
2593 cp_parser_error (cp_parser* parser, const char* gmsgid)
2594 {
2595 if (!cp_parser_simulate_error (parser))
2596 {
2597 cp_token *token = cp_lexer_peek_token (parser->lexer);
2598 /* This diagnostic makes more sense if it is tagged to the line
2599 of the token we just peeked at. */
2600 cp_lexer_set_source_position_from_token (token);
2601
2602 if (token->type == CPP_PRAGMA)
2603 {
2604 error_at (token->location,
2605 "%<#pragma%> is not allowed here");
2606 cp_parser_skip_to_pragma_eol (parser, token);
2607 return;
2608 }
2609
2610 c_parse_error (gmsgid,
2611 /* Because c_parser_error does not understand
2612 CPP_KEYWORD, keywords are treated like
2613 identifiers. */
2614 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2615 token->u.value, token->flags);
2616 }
2617 }
2618
2619 /* Issue an error about name-lookup failing. NAME is the
2620 IDENTIFIER_NODE DECL is the result of
2621 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2622 the thing that we hoped to find. */
2623
2624 static void
2625 cp_parser_name_lookup_error (cp_parser* parser,
2626 tree name,
2627 tree decl,
2628 name_lookup_error desired,
2629 location_t location)
2630 {
2631 /* If name lookup completely failed, tell the user that NAME was not
2632 declared. */
2633 if (decl == error_mark_node)
2634 {
2635 if (parser->scope && parser->scope != global_namespace)
2636 error_at (location, "%<%E::%E%> has not been declared",
2637 parser->scope, name);
2638 else if (parser->scope == global_namespace)
2639 error_at (location, "%<::%E%> has not been declared", name);
2640 else if (parser->object_scope
2641 && !CLASS_TYPE_P (parser->object_scope))
2642 error_at (location, "request for member %qE in non-class type %qT",
2643 name, parser->object_scope);
2644 else if (parser->object_scope)
2645 error_at (location, "%<%T::%E%> has not been declared",
2646 parser->object_scope, name);
2647 else
2648 error_at (location, "%qE has not been declared", name);
2649 }
2650 else if (parser->scope && parser->scope != global_namespace)
2651 {
2652 switch (desired)
2653 {
2654 case NLE_TYPE:
2655 error_at (location, "%<%E::%E%> is not a type",
2656 parser->scope, name);
2657 break;
2658 case NLE_CXX98:
2659 error_at (location, "%<%E::%E%> is not a class or namespace",
2660 parser->scope, name);
2661 break;
2662 case NLE_NOT_CXX98:
2663 error_at (location,
2664 "%<%E::%E%> is not a class, namespace, or enumeration",
2665 parser->scope, name);
2666 break;
2667 default:
2668 gcc_unreachable ();
2669
2670 }
2671 }
2672 else if (parser->scope == global_namespace)
2673 {
2674 switch (desired)
2675 {
2676 case NLE_TYPE:
2677 error_at (location, "%<::%E%> is not a type", name);
2678 break;
2679 case NLE_CXX98:
2680 error_at (location, "%<::%E%> is not a class or namespace", name);
2681 break;
2682 case NLE_NOT_CXX98:
2683 error_at (location,
2684 "%<::%E%> is not a class, namespace, or enumeration",
2685 name);
2686 break;
2687 default:
2688 gcc_unreachable ();
2689 }
2690 }
2691 else
2692 {
2693 switch (desired)
2694 {
2695 case NLE_TYPE:
2696 error_at (location, "%qE is not a type", name);
2697 break;
2698 case NLE_CXX98:
2699 error_at (location, "%qE is not a class or namespace", name);
2700 break;
2701 case NLE_NOT_CXX98:
2702 error_at (location,
2703 "%qE is not a class, namespace, or enumeration", name);
2704 break;
2705 default:
2706 gcc_unreachable ();
2707 }
2708 }
2709 }
2710
2711 /* If we are parsing tentatively, remember that an error has occurred
2712 during this tentative parse. Returns true if the error was
2713 simulated; false if a message should be issued by the caller. */
2714
2715 static bool
2716 cp_parser_simulate_error (cp_parser* parser)
2717 {
2718 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2719 {
2720 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2721 return true;
2722 }
2723 return false;
2724 }
2725
2726 /* This function is called when a type is defined. If type
2727 definitions are forbidden at this point, an error message is
2728 issued. */
2729
2730 static bool
2731 cp_parser_check_type_definition (cp_parser* parser)
2732 {
2733 /* If types are forbidden here, issue a message. */
2734 if (parser->type_definition_forbidden_message)
2735 {
2736 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2737 in the message need to be interpreted. */
2738 error (parser->type_definition_forbidden_message);
2739 return false;
2740 }
2741 return true;
2742 }
2743
2744 /* This function is called when the DECLARATOR is processed. The TYPE
2745 was a type defined in the decl-specifiers. If it is invalid to
2746 define a type in the decl-specifiers for DECLARATOR, an error is
2747 issued. TYPE_LOCATION is the location of TYPE and is used
2748 for error reporting. */
2749
2750 static void
2751 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2752 tree type, location_t type_location)
2753 {
2754 /* [dcl.fct] forbids type definitions in return types.
2755 Unfortunately, it's not easy to know whether or not we are
2756 processing a return type until after the fact. */
2757 while (declarator
2758 && (declarator->kind == cdk_pointer
2759 || declarator->kind == cdk_reference
2760 || declarator->kind == cdk_ptrmem))
2761 declarator = declarator->declarator;
2762 if (declarator
2763 && declarator->kind == cdk_function)
2764 {
2765 error_at (type_location,
2766 "new types may not be defined in a return type");
2767 inform (type_location,
2768 "(perhaps a semicolon is missing after the definition of %qT)",
2769 type);
2770 }
2771 }
2772
2773 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2774 "<" in any valid C++ program. If the next token is indeed "<",
2775 issue a message warning the user about what appears to be an
2776 invalid attempt to form a template-id. LOCATION is the location
2777 of the type-specifier (TYPE) */
2778
2779 static void
2780 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2781 tree type,
2782 enum tag_types tag_type,
2783 location_t location)
2784 {
2785 cp_token_position start = 0;
2786
2787 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2788 {
2789 if (TYPE_P (type))
2790 error_at (location, "%qT is not a template", type);
2791 else if (identifier_p (type))
2792 {
2793 if (tag_type != none_type)
2794 error_at (location, "%qE is not a class template", type);
2795 else
2796 error_at (location, "%qE is not a template", type);
2797 }
2798 else
2799 error_at (location, "invalid template-id");
2800 /* Remember the location of the invalid "<". */
2801 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2802 start = cp_lexer_token_position (parser->lexer, true);
2803 /* Consume the "<". */
2804 cp_lexer_consume_token (parser->lexer);
2805 /* Parse the template arguments. */
2806 cp_parser_enclosed_template_argument_list (parser);
2807 /* Permanently remove the invalid template arguments so that
2808 this error message is not issued again. */
2809 if (start)
2810 cp_lexer_purge_tokens_after (parser->lexer, start);
2811 }
2812 }
2813
2814 /* If parsing an integral constant-expression, issue an error message
2815 about the fact that THING appeared and return true. Otherwise,
2816 return false. In either case, set
2817 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2818
2819 static bool
2820 cp_parser_non_integral_constant_expression (cp_parser *parser,
2821 non_integral_constant thing)
2822 {
2823 parser->non_integral_constant_expression_p = true;
2824 if (parser->integral_constant_expression_p)
2825 {
2826 if (!parser->allow_non_integral_constant_expression_p)
2827 {
2828 const char *msg = NULL;
2829 switch (thing)
2830 {
2831 case NIC_FLOAT:
2832 error ("floating-point literal "
2833 "cannot appear in a constant-expression");
2834 return true;
2835 case NIC_CAST:
2836 error ("a cast to a type other than an integral or "
2837 "enumeration type cannot appear in a "
2838 "constant-expression");
2839 return true;
2840 case NIC_TYPEID:
2841 error ("%<typeid%> operator "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_NCC:
2845 error ("non-constant compound literals "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_FUNC_CALL:
2849 error ("a function call "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_INC:
2853 error ("an increment "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_DEC:
2857 error ("an decrement "
2858 "cannot appear in a constant-expression");
2859 return true;
2860 case NIC_ARRAY_REF:
2861 error ("an array reference "
2862 "cannot appear in a constant-expression");
2863 return true;
2864 case NIC_ADDR_LABEL:
2865 error ("the address of a label "
2866 "cannot appear in a constant-expression");
2867 return true;
2868 case NIC_OVERLOADED:
2869 error ("calls to overloaded operators "
2870 "cannot appear in a constant-expression");
2871 return true;
2872 case NIC_ASSIGNMENT:
2873 error ("an assignment cannot appear in a constant-expression");
2874 return true;
2875 case NIC_COMMA:
2876 error ("a comma operator "
2877 "cannot appear in a constant-expression");
2878 return true;
2879 case NIC_CONSTRUCTOR:
2880 error ("a call to a constructor "
2881 "cannot appear in a constant-expression");
2882 return true;
2883 case NIC_TRANSACTION:
2884 error ("a transaction expression "
2885 "cannot appear in a constant-expression");
2886 return true;
2887 case NIC_THIS:
2888 msg = "this";
2889 break;
2890 case NIC_FUNC_NAME:
2891 msg = "__FUNCTION__";
2892 break;
2893 case NIC_PRETTY_FUNC:
2894 msg = "__PRETTY_FUNCTION__";
2895 break;
2896 case NIC_C99_FUNC:
2897 msg = "__func__";
2898 break;
2899 case NIC_VA_ARG:
2900 msg = "va_arg";
2901 break;
2902 case NIC_ARROW:
2903 msg = "->";
2904 break;
2905 case NIC_POINT:
2906 msg = ".";
2907 break;
2908 case NIC_STAR:
2909 msg = "*";
2910 break;
2911 case NIC_ADDR:
2912 msg = "&";
2913 break;
2914 case NIC_PREINCREMENT:
2915 msg = "++";
2916 break;
2917 case NIC_PREDECREMENT:
2918 msg = "--";
2919 break;
2920 case NIC_NEW:
2921 msg = "new";
2922 break;
2923 case NIC_DEL:
2924 msg = "delete";
2925 break;
2926 default:
2927 gcc_unreachable ();
2928 }
2929 if (msg)
2930 error ("%qs cannot appear in a constant-expression", msg);
2931 return true;
2932 }
2933 }
2934 return false;
2935 }
2936
2937 /* Emit a diagnostic for an invalid type name. This function commits
2938 to the current active tentative parse, if any. (Otherwise, the
2939 problematic construct might be encountered again later, resulting
2940 in duplicate error messages.) LOCATION is the location of ID. */
2941
2942 static void
2943 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2944 location_t location)
2945 {
2946 tree decl, ambiguous_decls;
2947 cp_parser_commit_to_tentative_parse (parser);
2948 /* Try to lookup the identifier. */
2949 decl = cp_parser_lookup_name (parser, id, none_type,
2950 /*is_template=*/false,
2951 /*is_namespace=*/false,
2952 /*check_dependency=*/true,
2953 &ambiguous_decls, location);
2954 if (ambiguous_decls)
2955 /* If the lookup was ambiguous, an error will already have
2956 been issued. */
2957 return;
2958 /* If the lookup found a template-name, it means that the user forgot
2959 to specify an argument list. Emit a useful error message. */
2960 if (DECL_TYPE_TEMPLATE_P (decl))
2961 {
2962 error_at (location,
2963 "invalid use of template-name %qE without an argument list",
2964 decl);
2965 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2966 }
2967 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2968 error_at (location, "invalid use of destructor %qD as a type", id);
2969 else if (TREE_CODE (decl) == TYPE_DECL)
2970 /* Something like 'unsigned A a;' */
2971 error_at (location, "invalid combination of multiple type-specifiers");
2972 else if (!parser->scope)
2973 {
2974 /* Issue an error message. */
2975 error_at (location, "%qE does not name a type", id);
2976 /* If we're in a template class, it's possible that the user was
2977 referring to a type from a base class. For example:
2978
2979 template <typename T> struct A { typedef T X; };
2980 template <typename T> struct B : public A<T> { X x; };
2981
2982 The user should have said "typename A<T>::X". */
2983 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2984 inform (location, "C++11 %<constexpr%> only available with "
2985 "-std=c++11 or -std=gnu++11");
2986 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2987 inform (location, "C++11 %<noexcept%> only available with "
2988 "-std=c++11 or -std=gnu++11");
2989 else if (cxx_dialect < cxx11
2990 && TREE_CODE (id) == IDENTIFIER_NODE
2991 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2992 inform (location, "C++11 %<thread_local%> only available with "
2993 "-std=c++11 or -std=gnu++11");
2994 else if (processing_template_decl && current_class_type
2995 && TYPE_BINFO (current_class_type))
2996 {
2997 tree b;
2998
2999 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3000 b;
3001 b = TREE_CHAIN (b))
3002 {
3003 tree base_type = BINFO_TYPE (b);
3004 if (CLASS_TYPE_P (base_type)
3005 && dependent_type_p (base_type))
3006 {
3007 tree field;
3008 /* Go from a particular instantiation of the
3009 template (which will have an empty TYPE_FIELDs),
3010 to the main version. */
3011 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3012 for (field = TYPE_FIELDS (base_type);
3013 field;
3014 field = DECL_CHAIN (field))
3015 if (TREE_CODE (field) == TYPE_DECL
3016 && DECL_NAME (field) == id)
3017 {
3018 inform (location,
3019 "(perhaps %<typename %T::%E%> was intended)",
3020 BINFO_TYPE (b), id);
3021 break;
3022 }
3023 if (field)
3024 break;
3025 }
3026 }
3027 }
3028 }
3029 /* Here we diagnose qualified-ids where the scope is actually correct,
3030 but the identifier does not resolve to a valid type name. */
3031 else if (parser->scope != error_mark_node)
3032 {
3033 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3034 {
3035 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3036 error_at (location_of (id),
3037 "%qE in namespace %qE does not name a template type",
3038 id, parser->scope);
3039 else
3040 error_at (location_of (id),
3041 "%qE in namespace %qE does not name a type",
3042 id, parser->scope);
3043 if (DECL_P (decl))
3044 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3045 }
3046 else if (CLASS_TYPE_P (parser->scope)
3047 && constructor_name_p (id, parser->scope))
3048 {
3049 /* A<T>::A<T>() */
3050 error_at (location, "%<%T::%E%> names the constructor, not"
3051 " the type", parser->scope, id);
3052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3053 error_at (location, "and %qT has no template constructors",
3054 parser->scope);
3055 }
3056 else if (TYPE_P (parser->scope)
3057 && dependent_scope_p (parser->scope))
3058 error_at (location, "need %<typename%> before %<%T::%E%> because "
3059 "%qT is a dependent scope",
3060 parser->scope, id, parser->scope);
3061 else if (TYPE_P (parser->scope))
3062 {
3063 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3064 error_at (location_of (id),
3065 "%qE in %q#T does not name a template type",
3066 id, parser->scope);
3067 else
3068 error_at (location_of (id),
3069 "%qE in %q#T does not name a type",
3070 id, parser->scope);
3071 if (DECL_P (decl))
3072 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3073 }
3074 else
3075 gcc_unreachable ();
3076 }
3077 }
3078
3079 /* Check for a common situation where a type-name should be present,
3080 but is not, and issue a sensible error message. Returns true if an
3081 invalid type-name was detected.
3082
3083 The situation handled by this function are variable declarations of the
3084 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3085 Usually, `ID' should name a type, but if we got here it means that it
3086 does not. We try to emit the best possible error message depending on
3087 how exactly the id-expression looks like. */
3088
3089 static bool
3090 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3091 {
3092 tree id;
3093 cp_token *token = cp_lexer_peek_token (parser->lexer);
3094
3095 /* Avoid duplicate error about ambiguous lookup. */
3096 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3097 {
3098 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3099 if (next->type == CPP_NAME && next->error_reported)
3100 goto out;
3101 }
3102
3103 cp_parser_parse_tentatively (parser);
3104 id = cp_parser_id_expression (parser,
3105 /*template_keyword_p=*/false,
3106 /*check_dependency_p=*/true,
3107 /*template_p=*/NULL,
3108 /*declarator_p=*/true,
3109 /*optional_p=*/false);
3110 /* If the next token is a (, this is a function with no explicit return
3111 type, i.e. constructor, destructor or conversion op. */
3112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3113 || TREE_CODE (id) == TYPE_DECL)
3114 {
3115 cp_parser_abort_tentative_parse (parser);
3116 return false;
3117 }
3118 if (!cp_parser_parse_definitely (parser))
3119 return false;
3120
3121 /* Emit a diagnostic for the invalid type. */
3122 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3123 out:
3124 /* If we aren't in the middle of a declarator (i.e. in a
3125 parameter-declaration-clause), skip to the end of the declaration;
3126 there's no point in trying to process it. */
3127 if (!parser->in_declarator_p)
3128 cp_parser_skip_to_end_of_block_or_statement (parser);
3129 return true;
3130 }
3131
3132 /* Consume tokens up to, and including, the next non-nested closing `)'.
3133 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3134 are doing error recovery. Returns -1 if OR_COMMA is true and we
3135 found an unnested comma. */
3136
3137 static int
3138 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3139 bool recovering,
3140 bool or_comma,
3141 bool consume_paren)
3142 {
3143 unsigned paren_depth = 0;
3144 unsigned brace_depth = 0;
3145 unsigned square_depth = 0;
3146
3147 if (recovering && !or_comma
3148 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3149 return 0;
3150
3151 while (true)
3152 {
3153 cp_token * token = cp_lexer_peek_token (parser->lexer);
3154
3155 switch (token->type)
3156 {
3157 case CPP_EOF:
3158 case CPP_PRAGMA_EOL:
3159 /* If we've run out of tokens, then there is no closing `)'. */
3160 return 0;
3161
3162 /* This is good for lambda expression capture-lists. */
3163 case CPP_OPEN_SQUARE:
3164 ++square_depth;
3165 break;
3166 case CPP_CLOSE_SQUARE:
3167 if (!square_depth--)
3168 return 0;
3169 break;
3170
3171 case CPP_SEMICOLON:
3172 /* This matches the processing in skip_to_end_of_statement. */
3173 if (!brace_depth)
3174 return 0;
3175 break;
3176
3177 case CPP_OPEN_BRACE:
3178 ++brace_depth;
3179 break;
3180 case CPP_CLOSE_BRACE:
3181 if (!brace_depth--)
3182 return 0;
3183 break;
3184
3185 case CPP_COMMA:
3186 if (recovering && or_comma && !brace_depth && !paren_depth
3187 && !square_depth)
3188 return -1;
3189 break;
3190
3191 case CPP_OPEN_PAREN:
3192 if (!brace_depth)
3193 ++paren_depth;
3194 break;
3195
3196 case CPP_CLOSE_PAREN:
3197 if (!brace_depth && !paren_depth--)
3198 {
3199 if (consume_paren)
3200 cp_lexer_consume_token (parser->lexer);
3201 return 1;
3202 }
3203 break;
3204
3205 default:
3206 break;
3207 }
3208
3209 /* Consume the token. */
3210 cp_lexer_consume_token (parser->lexer);
3211 }
3212 }
3213
3214 /* Consume tokens until we reach the end of the current statement.
3215 Normally, that will be just before consuming a `;'. However, if a
3216 non-nested `}' comes first, then we stop before consuming that. */
3217
3218 static void
3219 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3220 {
3221 unsigned nesting_depth = 0;
3222
3223 /* Unwind generic function template scope if necessary. */
3224 if (parser->fully_implicit_function_template_p)
3225 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3226
3227 while (true)
3228 {
3229 cp_token *token = cp_lexer_peek_token (parser->lexer);
3230
3231 switch (token->type)
3232 {
3233 case CPP_EOF:
3234 case CPP_PRAGMA_EOL:
3235 /* If we've run out of tokens, stop. */
3236 return;
3237
3238 case CPP_SEMICOLON:
3239 /* If the next token is a `;', we have reached the end of the
3240 statement. */
3241 if (!nesting_depth)
3242 return;
3243 break;
3244
3245 case CPP_CLOSE_BRACE:
3246 /* If this is a non-nested '}', stop before consuming it.
3247 That way, when confronted with something like:
3248
3249 { 3 + }
3250
3251 we stop before consuming the closing '}', even though we
3252 have not yet reached a `;'. */
3253 if (nesting_depth == 0)
3254 return;
3255
3256 /* If it is the closing '}' for a block that we have
3257 scanned, stop -- but only after consuming the token.
3258 That way given:
3259
3260 void f g () { ... }
3261 typedef int I;
3262
3263 we will stop after the body of the erroneously declared
3264 function, but before consuming the following `typedef'
3265 declaration. */
3266 if (--nesting_depth == 0)
3267 {
3268 cp_lexer_consume_token (parser->lexer);
3269 return;
3270 }
3271
3272 case CPP_OPEN_BRACE:
3273 ++nesting_depth;
3274 break;
3275
3276 default:
3277 break;
3278 }
3279
3280 /* Consume the token. */
3281 cp_lexer_consume_token (parser->lexer);
3282 }
3283 }
3284
3285 /* This function is called at the end of a statement or declaration.
3286 If the next token is a semicolon, it is consumed; otherwise, error
3287 recovery is attempted. */
3288
3289 static void
3290 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3291 {
3292 /* Look for the trailing `;'. */
3293 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3294 {
3295 /* If there is additional (erroneous) input, skip to the end of
3296 the statement. */
3297 cp_parser_skip_to_end_of_statement (parser);
3298 /* If the next token is now a `;', consume it. */
3299 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3300 cp_lexer_consume_token (parser->lexer);
3301 }
3302 }
3303
3304 /* Skip tokens until we have consumed an entire block, or until we
3305 have consumed a non-nested `;'. */
3306
3307 static void
3308 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3309 {
3310 int nesting_depth = 0;
3311
3312 /* Unwind generic function template scope if necessary. */
3313 if (parser->fully_implicit_function_template_p)
3314 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3315
3316 while (nesting_depth >= 0)
3317 {
3318 cp_token *token = cp_lexer_peek_token (parser->lexer);
3319
3320 switch (token->type)
3321 {
3322 case CPP_EOF:
3323 case CPP_PRAGMA_EOL:
3324 /* If we've run out of tokens, stop. */
3325 return;
3326
3327 case CPP_SEMICOLON:
3328 /* Stop if this is an unnested ';'. */
3329 if (!nesting_depth)
3330 nesting_depth = -1;
3331 break;
3332
3333 case CPP_CLOSE_BRACE:
3334 /* Stop if this is an unnested '}', or closes the outermost
3335 nesting level. */
3336 nesting_depth--;
3337 if (nesting_depth < 0)
3338 return;
3339 if (!nesting_depth)
3340 nesting_depth = -1;
3341 break;
3342
3343 case CPP_OPEN_BRACE:
3344 /* Nest. */
3345 nesting_depth++;
3346 break;
3347
3348 default:
3349 break;
3350 }
3351
3352 /* Consume the token. */
3353 cp_lexer_consume_token (parser->lexer);
3354 }
3355 }
3356
3357 /* Skip tokens until a non-nested closing curly brace is the next
3358 token, or there are no more tokens. Return true in the first case,
3359 false otherwise. */
3360
3361 static bool
3362 cp_parser_skip_to_closing_brace (cp_parser *parser)
3363 {
3364 unsigned nesting_depth = 0;
3365
3366 while (true)
3367 {
3368 cp_token *token = cp_lexer_peek_token (parser->lexer);
3369
3370 switch (token->type)
3371 {
3372 case CPP_EOF:
3373 case CPP_PRAGMA_EOL:
3374 /* If we've run out of tokens, stop. */
3375 return false;
3376
3377 case CPP_CLOSE_BRACE:
3378 /* If the next token is a non-nested `}', then we have reached
3379 the end of the current block. */
3380 if (nesting_depth-- == 0)
3381 return true;
3382 break;
3383
3384 case CPP_OPEN_BRACE:
3385 /* If it the next token is a `{', then we are entering a new
3386 block. Consume the entire block. */
3387 ++nesting_depth;
3388 break;
3389
3390 default:
3391 break;
3392 }
3393
3394 /* Consume the token. */
3395 cp_lexer_consume_token (parser->lexer);
3396 }
3397 }
3398
3399 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3400 parameter is the PRAGMA token, allowing us to purge the entire pragma
3401 sequence. */
3402
3403 static void
3404 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3405 {
3406 cp_token *token;
3407
3408 parser->lexer->in_pragma = false;
3409
3410 do
3411 token = cp_lexer_consume_token (parser->lexer);
3412 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3413
3414 /* Ensure that the pragma is not parsed again. */
3415 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3416 }
3417
3418 /* Require pragma end of line, resyncing with it as necessary. The
3419 arguments are as for cp_parser_skip_to_pragma_eol. */
3420
3421 static void
3422 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3423 {
3424 parser->lexer->in_pragma = false;
3425 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3427 }
3428
3429 /* This is a simple wrapper around make_typename_type. When the id is
3430 an unresolved identifier node, we can provide a superior diagnostic
3431 using cp_parser_diagnose_invalid_type_name. */
3432
3433 static tree
3434 cp_parser_make_typename_type (cp_parser *parser, tree id,
3435 location_t id_location)
3436 {
3437 tree result;
3438 if (identifier_p (id))
3439 {
3440 result = make_typename_type (parser->scope, id, typename_type,
3441 /*complain=*/tf_none);
3442 if (result == error_mark_node)
3443 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3444 return result;
3445 }
3446 return make_typename_type (parser->scope, id, typename_type, tf_error);
3447 }
3448
3449 /* This is a wrapper around the
3450 make_{pointer,ptrmem,reference}_declarator functions that decides
3451 which one to call based on the CODE and CLASS_TYPE arguments. The
3452 CODE argument should be one of the values returned by
3453 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3454 appertain to the pointer or reference. */
3455
3456 static cp_declarator *
3457 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3458 cp_cv_quals cv_qualifiers,
3459 cp_declarator *target,
3460 tree attributes)
3461 {
3462 if (code == ERROR_MARK)
3463 return cp_error_declarator;
3464
3465 if (code == INDIRECT_REF)
3466 if (class_type == NULL_TREE)
3467 return make_pointer_declarator (cv_qualifiers, target, attributes);
3468 else
3469 return make_ptrmem_declarator (cv_qualifiers, class_type,
3470 target, attributes);
3471 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3472 return make_reference_declarator (cv_qualifiers, target,
3473 false, attributes);
3474 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3475 return make_reference_declarator (cv_qualifiers, target,
3476 true, attributes);
3477 gcc_unreachable ();
3478 }
3479
3480 /* Create a new C++ parser. */
3481
3482 static cp_parser *
3483 cp_parser_new (void)
3484 {
3485 cp_parser *parser;
3486 cp_lexer *lexer;
3487 unsigned i;
3488
3489 /* cp_lexer_new_main is called before doing GC allocation because
3490 cp_lexer_new_main might load a PCH file. */
3491 lexer = cp_lexer_new_main ();
3492
3493 /* Initialize the binops_by_token so that we can get the tree
3494 directly from the token. */
3495 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3496 binops_by_token[binops[i].token_type] = binops[i];
3497
3498 parser = ggc_cleared_alloc<cp_parser> ();
3499 parser->lexer = lexer;
3500 parser->context = cp_parser_context_new (NULL);
3501
3502 /* For now, we always accept GNU extensions. */
3503 parser->allow_gnu_extensions_p = 1;
3504
3505 /* The `>' token is a greater-than operator, not the end of a
3506 template-id. */
3507 parser->greater_than_is_operator_p = true;
3508
3509 parser->default_arg_ok_p = true;
3510
3511 /* We are not parsing a constant-expression. */
3512 parser->integral_constant_expression_p = false;
3513 parser->allow_non_integral_constant_expression_p = false;
3514 parser->non_integral_constant_expression_p = false;
3515
3516 /* Local variable names are not forbidden. */
3517 parser->local_variables_forbidden_p = false;
3518
3519 /* We are not processing an `extern "C"' declaration. */
3520 parser->in_unbraced_linkage_specification_p = false;
3521
3522 /* We are not processing a declarator. */
3523 parser->in_declarator_p = false;
3524
3525 /* We are not processing a template-argument-list. */
3526 parser->in_template_argument_list_p = false;
3527
3528 /* We are not in an iteration statement. */
3529 parser->in_statement = 0;
3530
3531 /* We are not in a switch statement. */
3532 parser->in_switch_statement_p = false;
3533
3534 /* We are not parsing a type-id inside an expression. */
3535 parser->in_type_id_in_expr_p = false;
3536
3537 /* Declarations aren't implicitly extern "C". */
3538 parser->implicit_extern_c = false;
3539
3540 /* String literals should be translated to the execution character set. */
3541 parser->translate_strings_p = true;
3542
3543 /* We are not parsing a function body. */
3544 parser->in_function_body = false;
3545
3546 /* We can correct until told otherwise. */
3547 parser->colon_corrects_to_scope_p = true;
3548
3549 /* The unparsed function queue is empty. */
3550 push_unparsed_function_queues (parser);
3551
3552 /* There are no classes being defined. */
3553 parser->num_classes_being_defined = 0;
3554
3555 /* No template parameters apply. */
3556 parser->num_template_parameter_lists = 0;
3557
3558 /* Not declaring an implicit function template. */
3559 parser->auto_is_implicit_function_template_parm_p = false;
3560 parser->fully_implicit_function_template_p = false;
3561 parser->implicit_template_parms = 0;
3562 parser->implicit_template_scope = 0;
3563
3564 return parser;
3565 }
3566
3567 /* Create a cp_lexer structure which will emit the tokens in CACHE
3568 and push it onto the parser's lexer stack. This is used for delayed
3569 parsing of in-class method bodies and default arguments, and should
3570 not be confused with tentative parsing. */
3571 static void
3572 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3573 {
3574 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3575 lexer->next = parser->lexer;
3576 parser->lexer = lexer;
3577
3578 /* Move the current source position to that of the first token in the
3579 new lexer. */
3580 cp_lexer_set_source_position_from_token (lexer->next_token);
3581 }
3582
3583 /* Pop the top lexer off the parser stack. This is never used for the
3584 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3585 static void
3586 cp_parser_pop_lexer (cp_parser *parser)
3587 {
3588 cp_lexer *lexer = parser->lexer;
3589 parser->lexer = lexer->next;
3590 cp_lexer_destroy (lexer);
3591
3592 /* Put the current source position back where it was before this
3593 lexer was pushed. */
3594 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3595 }
3596
3597 /* Lexical conventions [gram.lex] */
3598
3599 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3600 identifier. */
3601
3602 static tree
3603 cp_parser_identifier (cp_parser* parser)
3604 {
3605 cp_token *token;
3606
3607 /* Look for the identifier. */
3608 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3609 /* Return the value. */
3610 return token ? token->u.value : error_mark_node;
3611 }
3612
3613 /* Parse a sequence of adjacent string constants. Returns a
3614 TREE_STRING representing the combined, nul-terminated string
3615 constant. If TRANSLATE is true, translate the string to the
3616 execution character set. If WIDE_OK is true, a wide string is
3617 invalid here.
3618
3619 C++98 [lex.string] says that if a narrow string literal token is
3620 adjacent to a wide string literal token, the behavior is undefined.
3621 However, C99 6.4.5p4 says that this results in a wide string literal.
3622 We follow C99 here, for consistency with the C front end.
3623
3624 This code is largely lifted from lex_string() in c-lex.c.
3625
3626 FUTURE: ObjC++ will need to handle @-strings here. */
3627 static tree
3628 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3629 bool lookup_udlit = true)
3630 {
3631 tree value;
3632 size_t count;
3633 struct obstack str_ob;
3634 cpp_string str, istr, *strs;
3635 cp_token *tok;
3636 enum cpp_ttype type, curr_type;
3637 int have_suffix_p = 0;
3638 tree string_tree;
3639 tree suffix_id = NULL_TREE;
3640 bool curr_tok_is_userdef_p = false;
3641
3642 tok = cp_lexer_peek_token (parser->lexer);
3643 if (!cp_parser_is_string_literal (tok))
3644 {
3645 cp_parser_error (parser, "expected string-literal");
3646 return error_mark_node;
3647 }
3648
3649 if (cpp_userdef_string_p (tok->type))
3650 {
3651 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3652 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 curr_tok_is_userdef_p = true;
3654 }
3655 else
3656 {
3657 string_tree = tok->u.value;
3658 curr_type = tok->type;
3659 }
3660 type = curr_type;
3661
3662 /* Try to avoid the overhead of creating and destroying an obstack
3663 for the common case of just one string. */
3664 if (!cp_parser_is_string_literal
3665 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3666 {
3667 cp_lexer_consume_token (parser->lexer);
3668
3669 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3670 str.len = TREE_STRING_LENGTH (string_tree);
3671 count = 1;
3672
3673 if (curr_tok_is_userdef_p)
3674 {
3675 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3676 have_suffix_p = 1;
3677 curr_type = cpp_userdef_string_remove_type (tok->type);
3678 }
3679 else
3680 curr_type = tok->type;
3681
3682 strs = &str;
3683 }
3684 else
3685 {
3686 gcc_obstack_init (&str_ob);
3687 count = 0;
3688
3689 do
3690 {
3691 cp_lexer_consume_token (parser->lexer);
3692 count++;
3693 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3694 str.len = TREE_STRING_LENGTH (string_tree);
3695
3696 if (curr_tok_is_userdef_p)
3697 {
3698 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3699 if (have_suffix_p == 0)
3700 {
3701 suffix_id = curr_suffix_id;
3702 have_suffix_p = 1;
3703 }
3704 else if (have_suffix_p == 1
3705 && curr_suffix_id != suffix_id)
3706 {
3707 error ("inconsistent user-defined literal suffixes"
3708 " %qD and %qD in string literal",
3709 suffix_id, curr_suffix_id);
3710 have_suffix_p = -1;
3711 }
3712 curr_type = cpp_userdef_string_remove_type (tok->type);
3713 }
3714 else
3715 curr_type = tok->type;
3716
3717 if (type != curr_type)
3718 {
3719 if (type == CPP_STRING)
3720 type = curr_type;
3721 else if (curr_type != CPP_STRING)
3722 error_at (tok->location,
3723 "unsupported non-standard concatenation "
3724 "of string literals");
3725 }
3726
3727 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3728
3729 tok = cp_lexer_peek_token (parser->lexer);
3730 if (cpp_userdef_string_p (tok->type))
3731 {
3732 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3733 curr_type = cpp_userdef_string_remove_type (tok->type);
3734 curr_tok_is_userdef_p = true;
3735 }
3736 else
3737 {
3738 string_tree = tok->u.value;
3739 curr_type = tok->type;
3740 curr_tok_is_userdef_p = false;
3741 }
3742 }
3743 while (cp_parser_is_string_literal (tok));
3744
3745 strs = (cpp_string *) obstack_finish (&str_ob);
3746 }
3747
3748 if (type != CPP_STRING && !wide_ok)
3749 {
3750 cp_parser_error (parser, "a wide string is invalid in this context");
3751 type = CPP_STRING;
3752 }
3753
3754 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3755 (parse_in, strs, count, &istr, type))
3756 {
3757 value = build_string (istr.len, (const char *)istr.text);
3758 free (CONST_CAST (unsigned char *, istr.text));
3759
3760 switch (type)
3761 {
3762 default:
3763 case CPP_STRING:
3764 case CPP_UTF8STRING:
3765 TREE_TYPE (value) = char_array_type_node;
3766 break;
3767 case CPP_STRING16:
3768 TREE_TYPE (value) = char16_array_type_node;
3769 break;
3770 case CPP_STRING32:
3771 TREE_TYPE (value) = char32_array_type_node;
3772 break;
3773 case CPP_WSTRING:
3774 TREE_TYPE (value) = wchar_array_type_node;
3775 break;
3776 }
3777
3778 value = fix_string_type (value);
3779
3780 if (have_suffix_p)
3781 {
3782 tree literal = build_userdef_literal (suffix_id, value,
3783 OT_NONE, NULL_TREE);
3784 if (lookup_udlit)
3785 value = cp_parser_userdef_string_literal (literal);
3786 else
3787 value = literal;
3788 }
3789 }
3790 else
3791 /* cpp_interpret_string has issued an error. */
3792 value = error_mark_node;
3793
3794 if (count > 1)
3795 obstack_free (&str_ob, 0);
3796
3797 return value;
3798 }
3799
3800 /* Look up a literal operator with the name and the exact arguments. */
3801
3802 static tree
3803 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3804 {
3805 tree decl, fns;
3806 decl = lookup_name (name);
3807 if (!decl || !is_overloaded_fn (decl))
3808 return error_mark_node;
3809
3810 for (fns = decl; fns; fns = OVL_NEXT (fns))
3811 {
3812 unsigned int ix;
3813 bool found = true;
3814 tree fn = OVL_CURRENT (fns);
3815 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3816 if (parmtypes != NULL_TREE)
3817 {
3818 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3819 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3820 {
3821 tree tparm = TREE_VALUE (parmtypes);
3822 tree targ = TREE_TYPE ((*args)[ix]);
3823 bool ptr = TYPE_PTR_P (tparm);
3824 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3825 if ((ptr || arr || !same_type_p (tparm, targ))
3826 && (!ptr || !arr
3827 || !same_type_p (TREE_TYPE (tparm),
3828 TREE_TYPE (targ))))
3829 found = false;
3830 }
3831 if (found
3832 && ix == vec_safe_length (args)
3833 /* May be this should be sufficient_parms_p instead,
3834 depending on how exactly should user-defined literals
3835 work in presence of default arguments on the literal
3836 operator parameters. */
3837 && parmtypes == void_list_node)
3838 return decl;
3839 }
3840 }
3841
3842 return error_mark_node;
3843 }
3844
3845 /* Parse a user-defined char constant. Returns a call to a user-defined
3846 literal operator taking the character as an argument. */
3847
3848 static tree
3849 cp_parser_userdef_char_literal (cp_parser *parser)
3850 {
3851 cp_token *token = cp_lexer_consume_token (parser->lexer);
3852 tree literal = token->u.value;
3853 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3854 tree value = USERDEF_LITERAL_VALUE (literal);
3855 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3856 tree decl, result;
3857
3858 /* Build up a call to the user-defined operator */
3859 /* Lookup the name we got back from the id-expression. */
3860 vec<tree, va_gc> *args = make_tree_vector ();
3861 vec_safe_push (args, value);
3862 decl = lookup_literal_operator (name, args);
3863 if (!decl || decl == error_mark_node)
3864 {
3865 error ("unable to find character literal operator %qD with %qT argument",
3866 name, TREE_TYPE (value));
3867 release_tree_vector (args);
3868 return error_mark_node;
3869 }
3870 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3871 release_tree_vector (args);
3872 return result;
3873 }
3874
3875 /* A subroutine of cp_parser_userdef_numeric_literal to
3876 create a char... template parameter pack from a string node. */
3877
3878 static tree
3879 make_char_string_pack (tree value)
3880 {
3881 tree charvec;
3882 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3883 const char *str = TREE_STRING_POINTER (value);
3884 int i, len = TREE_STRING_LENGTH (value) - 1;
3885 tree argvec = make_tree_vec (1);
3886
3887 /* Fill in CHARVEC with all of the parameters. */
3888 charvec = make_tree_vec (len);
3889 for (i = 0; i < len; ++i)
3890 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3891
3892 /* Build the argument packs. */
3893 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3894 TREE_TYPE (argpack) = char_type_node;
3895
3896 TREE_VEC_ELT (argvec, 0) = argpack;
3897
3898 return argvec;
3899 }
3900
3901 /* A subroutine of cp_parser_userdef_numeric_literal to
3902 create a char... template parameter pack from a string node. */
3903
3904 static tree
3905 make_string_pack (tree value)
3906 {
3907 tree charvec;
3908 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3909 const unsigned char *str
3910 = (const unsigned char *) TREE_STRING_POINTER (value);
3911 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3912 int len = TREE_STRING_LENGTH (value) / sz - 1;
3913 tree argvec = make_tree_vec (2);
3914
3915 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3916 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3917
3918 /* First template parm is character type. */
3919 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3920
3921 /* Fill in CHARVEC with all of the parameters. */
3922 charvec = make_tree_vec (len);
3923 for (int i = 0; i < len; ++i)
3924 TREE_VEC_ELT (charvec, i)
3925 = double_int_to_tree (str_char_type_node,
3926 double_int::from_buffer (str + i * sz, sz));
3927
3928 /* Build the argument packs. */
3929 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3930 TREE_TYPE (argpack) = str_char_type_node;
3931
3932 TREE_VEC_ELT (argvec, 1) = argpack;
3933
3934 return argvec;
3935 }
3936
3937 /* Parse a user-defined numeric constant. returns a call to a user-defined
3938 literal operator. */
3939
3940 static tree
3941 cp_parser_userdef_numeric_literal (cp_parser *parser)
3942 {
3943 cp_token *token = cp_lexer_consume_token (parser->lexer);
3944 tree literal = token->u.value;
3945 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3946 tree value = USERDEF_LITERAL_VALUE (literal);
3947 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3948 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3949 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3950 tree decl, result;
3951 vec<tree, va_gc> *args;
3952
3953 /* Look for a literal operator taking the exact type of numeric argument
3954 as the literal value. */
3955 args = make_tree_vector ();
3956 vec_safe_push (args, value);
3957 decl = lookup_literal_operator (name, args);
3958 if (decl && decl != error_mark_node)
3959 {
3960 result = finish_call_expr (decl, &args, false, true,
3961 tf_warning_or_error);
3962
3963 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3964 {
3965 warning_at (token->location, OPT_Woverflow,
3966 "integer literal exceeds range of %qT type",
3967 long_long_unsigned_type_node);
3968 }
3969 else
3970 {
3971 if (overflow > 0)
3972 warning_at (token->location, OPT_Woverflow,
3973 "floating literal exceeds range of %qT type",
3974 long_double_type_node);
3975 else if (overflow < 0)
3976 warning_at (token->location, OPT_Woverflow,
3977 "floating literal truncated to zero");
3978 }
3979
3980 release_tree_vector (args);
3981 return result;
3982 }
3983 release_tree_vector (args);
3984
3985 /* If the numeric argument didn't work, look for a raw literal
3986 operator taking a const char* argument consisting of the number
3987 in string format. */
3988 args = make_tree_vector ();
3989 vec_safe_push (args, num_string);
3990 decl = lookup_literal_operator (name, args);
3991 if (decl && decl != error_mark_node)
3992 {
3993 result = finish_call_expr (decl, &args, false, true,
3994 tf_warning_or_error);
3995 release_tree_vector (args);
3996 return result;
3997 }
3998 release_tree_vector (args);
3999
4000 /* If the raw literal didn't work, look for a non-type template
4001 function with parameter pack char.... Call the function with
4002 template parameter characters representing the number. */
4003 args = make_tree_vector ();
4004 decl = lookup_literal_operator (name, args);
4005 if (decl && decl != error_mark_node)
4006 {
4007 tree tmpl_args = make_char_string_pack (num_string);
4008 decl = lookup_template_function (decl, tmpl_args);
4009 result = finish_call_expr (decl, &args, false, true,
4010 tf_warning_or_error);
4011 release_tree_vector (args);
4012 return result;
4013 }
4014
4015 release_tree_vector (args);
4016
4017 error ("unable to find numeric literal operator %qD", name);
4018 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4019 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4020 "to enable more built-in suffixes");
4021 return error_mark_node;
4022 }
4023
4024 /* Parse a user-defined string constant. Returns a call to a user-defined
4025 literal operator taking a character pointer and the length of the string
4026 as arguments. */
4027
4028 static tree
4029 cp_parser_userdef_string_literal (tree literal)
4030 {
4031 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4032 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4033 tree value = USERDEF_LITERAL_VALUE (literal);
4034 int len = TREE_STRING_LENGTH (value)
4035 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4036 tree decl, result;
4037 vec<tree, va_gc> *args;
4038
4039 /* Build up a call to the user-defined operator. */
4040 /* Lookup the name we got back from the id-expression. */
4041 args = make_tree_vector ();
4042 vec_safe_push (args, value);
4043 vec_safe_push (args, build_int_cst (size_type_node, len));
4044 decl = lookup_literal_operator (name, args);
4045
4046 if (decl && decl != error_mark_node)
4047 {
4048 result = finish_call_expr (decl, &args, false, true,
4049 tf_warning_or_error);
4050 release_tree_vector (args);
4051 return result;
4052 }
4053 release_tree_vector (args);
4054
4055 /* Look for a template function with typename parameter CharT
4056 and parameter pack CharT... Call the function with
4057 template parameter characters representing the string. */
4058 args = make_tree_vector ();
4059 decl = lookup_literal_operator (name, args);
4060 if (decl && decl != error_mark_node)
4061 {
4062 tree tmpl_args = make_string_pack (value);
4063 decl = lookup_template_function (decl, tmpl_args);
4064 result = finish_call_expr (decl, &args, false, true,
4065 tf_warning_or_error);
4066 release_tree_vector (args);
4067 return result;
4068 }
4069 release_tree_vector (args);
4070
4071 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4072 name, TREE_TYPE (value), size_type_node);
4073 return error_mark_node;
4074 }
4075
4076
4077 /* Basic concepts [gram.basic] */
4078
4079 /* Parse a translation-unit.
4080
4081 translation-unit:
4082 declaration-seq [opt]
4083
4084 Returns TRUE if all went well. */
4085
4086 static bool
4087 cp_parser_translation_unit (cp_parser* parser)
4088 {
4089 /* The address of the first non-permanent object on the declarator
4090 obstack. */
4091 static void *declarator_obstack_base;
4092
4093 bool success;
4094
4095 /* Create the declarator obstack, if necessary. */
4096 if (!cp_error_declarator)
4097 {
4098 gcc_obstack_init (&declarator_obstack);
4099 /* Create the error declarator. */
4100 cp_error_declarator = make_declarator (cdk_error);
4101 /* Create the empty parameter list. */
4102 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4103 /* Remember where the base of the declarator obstack lies. */
4104 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4105 }
4106
4107 cp_parser_declaration_seq_opt (parser);
4108
4109 /* If there are no tokens left then all went well. */
4110 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4111 {
4112 /* Get rid of the token array; we don't need it any more. */
4113 cp_lexer_destroy (parser->lexer);
4114 parser->lexer = NULL;
4115
4116 /* This file might have been a context that's implicitly extern
4117 "C". If so, pop the lang context. (Only relevant for PCH.) */
4118 if (parser->implicit_extern_c)
4119 {
4120 pop_lang_context ();
4121 parser->implicit_extern_c = false;
4122 }
4123
4124 /* Finish up. */
4125 finish_translation_unit ();
4126
4127 success = true;
4128 }
4129 else
4130 {
4131 cp_parser_error (parser, "expected declaration");
4132 success = false;
4133 }
4134
4135 /* Make sure the declarator obstack was fully cleaned up. */
4136 gcc_assert (obstack_next_free (&declarator_obstack)
4137 == declarator_obstack_base);
4138
4139 /* All went well. */
4140 return success;
4141 }
4142
4143 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4144 decltype context. */
4145
4146 static inline tsubst_flags_t
4147 complain_flags (bool decltype_p)
4148 {
4149 tsubst_flags_t complain = tf_warning_or_error;
4150 if (decltype_p)
4151 complain |= tf_decltype;
4152 return complain;
4153 }
4154
4155 /* We're about to parse a collection of statements. If we're currently
4156 parsing tentatively, set up a firewall so that any nested
4157 cp_parser_commit_to_tentative_parse won't affect the current context. */
4158
4159 static cp_token_position
4160 cp_parser_start_tentative_firewall (cp_parser *parser)
4161 {
4162 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4163 return 0;
4164
4165 cp_parser_parse_tentatively (parser);
4166 cp_parser_commit_to_topmost_tentative_parse (parser);
4167 return cp_lexer_token_position (parser->lexer, false);
4168 }
4169
4170 /* We've finished parsing the collection of statements. Wrap up the
4171 firewall and replace the relevant tokens with the parsed form. */
4172
4173 static void
4174 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4175 tree expr)
4176 {
4177 if (!start)
4178 return;
4179
4180 /* Finish the firewall level. */
4181 cp_parser_parse_definitely (parser);
4182 /* And remember the result of the parse for when we try again. */
4183 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4184 token->type = CPP_PREPARSED_EXPR;
4185 token->u.value = expr;
4186 token->keyword = RID_MAX;
4187 cp_lexer_purge_tokens_after (parser->lexer, start);
4188 }
4189
4190 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4191 enclosing parentheses. */
4192
4193 static tree
4194 cp_parser_statement_expr (cp_parser *parser)
4195 {
4196 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4197
4198 /* Consume the '('. */
4199 cp_lexer_consume_token (parser->lexer);
4200 /* Start the statement-expression. */
4201 tree expr = begin_stmt_expr ();
4202 /* Parse the compound-statement. */
4203 cp_parser_compound_statement (parser, expr, false, false);
4204 /* Finish up. */
4205 expr = finish_stmt_expr (expr, false);
4206 /* Consume the ')'. */
4207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4208 cp_parser_skip_to_end_of_statement (parser);
4209
4210 cp_parser_end_tentative_firewall (parser, start, expr);
4211 return expr;
4212 }
4213
4214 /* Expressions [gram.expr] */
4215
4216 /* Parse a primary-expression.
4217
4218 primary-expression:
4219 literal
4220 this
4221 ( expression )
4222 id-expression
4223 lambda-expression (C++11)
4224
4225 GNU Extensions:
4226
4227 primary-expression:
4228 ( compound-statement )
4229 __builtin_va_arg ( assignment-expression , type-id )
4230 __builtin_offsetof ( type-id , offsetof-expression )
4231
4232 C++ Extensions:
4233 __has_nothrow_assign ( type-id )
4234 __has_nothrow_constructor ( type-id )
4235 __has_nothrow_copy ( type-id )
4236 __has_trivial_assign ( type-id )
4237 __has_trivial_constructor ( type-id )
4238 __has_trivial_copy ( type-id )
4239 __has_trivial_destructor ( type-id )
4240 __has_virtual_destructor ( type-id )
4241 __is_abstract ( type-id )
4242 __is_base_of ( type-id , type-id )
4243 __is_class ( type-id )
4244 __is_empty ( type-id )
4245 __is_enum ( type-id )
4246 __is_final ( type-id )
4247 __is_literal_type ( type-id )
4248 __is_pod ( type-id )
4249 __is_polymorphic ( type-id )
4250 __is_std_layout ( type-id )
4251 __is_trivial ( type-id )
4252 __is_union ( type-id )
4253
4254 Objective-C++ Extension:
4255
4256 primary-expression:
4257 objc-expression
4258
4259 literal:
4260 __null
4261
4262 ADDRESS_P is true iff this expression was immediately preceded by
4263 "&" and therefore might denote a pointer-to-member. CAST_P is true
4264 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4265 true iff this expression is a template argument.
4266
4267 Returns a representation of the expression. Upon return, *IDK
4268 indicates what kind of id-expression (if any) was present. */
4269
4270 static tree
4271 cp_parser_primary_expression (cp_parser *parser,
4272 bool address_p,
4273 bool cast_p,
4274 bool template_arg_p,
4275 bool decltype_p,
4276 cp_id_kind *idk)
4277 {
4278 cp_token *token = NULL;
4279
4280 /* Assume the primary expression is not an id-expression. */
4281 *idk = CP_ID_KIND_NONE;
4282
4283 /* Peek at the next token. */
4284 token = cp_lexer_peek_token (parser->lexer);
4285 switch ((int) token->type)
4286 {
4287 /* literal:
4288 integer-literal
4289 character-literal
4290 floating-literal
4291 string-literal
4292 boolean-literal
4293 pointer-literal
4294 user-defined-literal */
4295 case CPP_CHAR:
4296 case CPP_CHAR16:
4297 case CPP_CHAR32:
4298 case CPP_WCHAR:
4299 case CPP_NUMBER:
4300 case CPP_PREPARSED_EXPR:
4301 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4302 return cp_parser_userdef_numeric_literal (parser);
4303 token = cp_lexer_consume_token (parser->lexer);
4304 if (TREE_CODE (token->u.value) == FIXED_CST)
4305 {
4306 error_at (token->location,
4307 "fixed-point types not supported in C++");
4308 return error_mark_node;
4309 }
4310 /* Floating-point literals are only allowed in an integral
4311 constant expression if they are cast to an integral or
4312 enumeration type. */
4313 if (TREE_CODE (token->u.value) == REAL_CST
4314 && parser->integral_constant_expression_p
4315 && pedantic)
4316 {
4317 /* CAST_P will be set even in invalid code like "int(2.7 +
4318 ...)". Therefore, we have to check that the next token
4319 is sure to end the cast. */
4320 if (cast_p)
4321 {
4322 cp_token *next_token;
4323
4324 next_token = cp_lexer_peek_token (parser->lexer);
4325 if (/* The comma at the end of an
4326 enumerator-definition. */
4327 next_token->type != CPP_COMMA
4328 /* The curly brace at the end of an enum-specifier. */
4329 && next_token->type != CPP_CLOSE_BRACE
4330 /* The end of a statement. */
4331 && next_token->type != CPP_SEMICOLON
4332 /* The end of the cast-expression. */
4333 && next_token->type != CPP_CLOSE_PAREN
4334 /* The end of an array bound. */
4335 && next_token->type != CPP_CLOSE_SQUARE
4336 /* The closing ">" in a template-argument-list. */
4337 && (next_token->type != CPP_GREATER
4338 || parser->greater_than_is_operator_p)
4339 /* C++0x only: A ">>" treated like two ">" tokens,
4340 in a template-argument-list. */
4341 && (next_token->type != CPP_RSHIFT
4342 || (cxx_dialect == cxx98)
4343 || parser->greater_than_is_operator_p))
4344 cast_p = false;
4345 }
4346
4347 /* If we are within a cast, then the constraint that the
4348 cast is to an integral or enumeration type will be
4349 checked at that point. If we are not within a cast, then
4350 this code is invalid. */
4351 if (!cast_p)
4352 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4353 }
4354 return token->u.value;
4355
4356 case CPP_CHAR_USERDEF:
4357 case CPP_CHAR16_USERDEF:
4358 case CPP_CHAR32_USERDEF:
4359 case CPP_WCHAR_USERDEF:
4360 return cp_parser_userdef_char_literal (parser);
4361
4362 case CPP_STRING:
4363 case CPP_STRING16:
4364 case CPP_STRING32:
4365 case CPP_WSTRING:
4366 case CPP_UTF8STRING:
4367 case CPP_STRING_USERDEF:
4368 case CPP_STRING16_USERDEF:
4369 case CPP_STRING32_USERDEF:
4370 case CPP_WSTRING_USERDEF:
4371 case CPP_UTF8STRING_USERDEF:
4372 /* ??? Should wide strings be allowed when parser->translate_strings_p
4373 is false (i.e. in attributes)? If not, we can kill the third
4374 argument to cp_parser_string_literal. */
4375 return cp_parser_string_literal (parser,
4376 parser->translate_strings_p,
4377 true);
4378
4379 case CPP_OPEN_PAREN:
4380 /* If we see `( { ' then we are looking at the beginning of
4381 a GNU statement-expression. */
4382 if (cp_parser_allow_gnu_extensions_p (parser)
4383 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4384 {
4385 /* Statement-expressions are not allowed by the standard. */
4386 pedwarn (token->location, OPT_Wpedantic,
4387 "ISO C++ forbids braced-groups within expressions");
4388
4389 /* And they're not allowed outside of a function-body; you
4390 cannot, for example, write:
4391
4392 int i = ({ int j = 3; j + 1; });
4393
4394 at class or namespace scope. */
4395 if (!parser->in_function_body
4396 || parser->in_template_argument_list_p)
4397 {
4398 error_at (token->location,
4399 "statement-expressions are not allowed outside "
4400 "functions nor in template-argument lists");
4401 cp_parser_skip_to_end_of_block_or_statement (parser);
4402 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4403 cp_lexer_consume_token (parser->lexer);
4404 return error_mark_node;
4405 }
4406 else
4407 return cp_parser_statement_expr (parser);
4408 }
4409 /* Otherwise it's a normal parenthesized expression. */
4410 {
4411 tree expr;
4412 bool saved_greater_than_is_operator_p;
4413
4414 /* Consume the `('. */
4415 cp_lexer_consume_token (parser->lexer);
4416 /* Within a parenthesized expression, a `>' token is always
4417 the greater-than operator. */
4418 saved_greater_than_is_operator_p
4419 = parser->greater_than_is_operator_p;
4420 parser->greater_than_is_operator_p = true;
4421
4422 /* Parse the parenthesized expression. */
4423 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4424 /* Let the front end know that this expression was
4425 enclosed in parentheses. This matters in case, for
4426 example, the expression is of the form `A::B', since
4427 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4428 not. */
4429 expr = finish_parenthesized_expr (expr);
4430 /* DR 705: Wrapping an unqualified name in parentheses
4431 suppresses arg-dependent lookup. We want to pass back
4432 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4433 (c++/37862), but none of the others. */
4434 if (*idk != CP_ID_KIND_QUALIFIED)
4435 *idk = CP_ID_KIND_NONE;
4436
4437 /* The `>' token might be the end of a template-id or
4438 template-parameter-list now. */
4439 parser->greater_than_is_operator_p
4440 = saved_greater_than_is_operator_p;
4441 /* Consume the `)'. */
4442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4443 cp_parser_skip_to_end_of_statement (parser);
4444
4445 return expr;
4446 }
4447
4448 case CPP_OPEN_SQUARE:
4449 {
4450 if (c_dialect_objc ())
4451 {
4452 /* We might have an Objective-C++ message. */
4453 cp_parser_parse_tentatively (parser);
4454 tree msg = cp_parser_objc_message_expression (parser);
4455 /* If that works out, we're done ... */
4456 if (cp_parser_parse_definitely (parser))
4457 return msg;
4458 /* ... else, fall though to see if it's a lambda. */
4459 }
4460 tree lam = cp_parser_lambda_expression (parser);
4461 /* Don't warn about a failed tentative parse. */
4462 if (cp_parser_error_occurred (parser))
4463 return error_mark_node;
4464 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4465 return lam;
4466 }
4467
4468 case CPP_OBJC_STRING:
4469 if (c_dialect_objc ())
4470 /* We have an Objective-C++ string literal. */
4471 return cp_parser_objc_expression (parser);
4472 cp_parser_error (parser, "expected primary-expression");
4473 return error_mark_node;
4474
4475 case CPP_KEYWORD:
4476 switch (token->keyword)
4477 {
4478 /* These two are the boolean literals. */
4479 case RID_TRUE:
4480 cp_lexer_consume_token (parser->lexer);
4481 return boolean_true_node;
4482 case RID_FALSE:
4483 cp_lexer_consume_token (parser->lexer);
4484 return boolean_false_node;
4485
4486 /* The `__null' literal. */
4487 case RID_NULL:
4488 cp_lexer_consume_token (parser->lexer);
4489 return null_node;
4490
4491 /* The `nullptr' literal. */
4492 case RID_NULLPTR:
4493 cp_lexer_consume_token (parser->lexer);
4494 return nullptr_node;
4495
4496 /* Recognize the `this' keyword. */
4497 case RID_THIS:
4498 cp_lexer_consume_token (parser->lexer);
4499 if (parser->local_variables_forbidden_p)
4500 {
4501 error_at (token->location,
4502 "%<this%> may not be used in this context");
4503 return error_mark_node;
4504 }
4505 /* Pointers cannot appear in constant-expressions. */
4506 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4507 return error_mark_node;
4508 return finish_this_expr ();
4509
4510 /* The `operator' keyword can be the beginning of an
4511 id-expression. */
4512 case RID_OPERATOR:
4513 goto id_expression;
4514
4515 case RID_FUNCTION_NAME:
4516 case RID_PRETTY_FUNCTION_NAME:
4517 case RID_C99_FUNCTION_NAME:
4518 {
4519 non_integral_constant name;
4520
4521 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4522 __func__ are the names of variables -- but they are
4523 treated specially. Therefore, they are handled here,
4524 rather than relying on the generic id-expression logic
4525 below. Grammatically, these names are id-expressions.
4526
4527 Consume the token. */
4528 token = cp_lexer_consume_token (parser->lexer);
4529
4530 switch (token->keyword)
4531 {
4532 case RID_FUNCTION_NAME:
4533 name = NIC_FUNC_NAME;
4534 break;
4535 case RID_PRETTY_FUNCTION_NAME:
4536 name = NIC_PRETTY_FUNC;
4537 break;
4538 case RID_C99_FUNCTION_NAME:
4539 name = NIC_C99_FUNC;
4540 break;
4541 default:
4542 gcc_unreachable ();
4543 }
4544
4545 if (cp_parser_non_integral_constant_expression (parser, name))
4546 return error_mark_node;
4547
4548 /* Look up the name. */
4549 return finish_fname (token->u.value);
4550 }
4551
4552 case RID_VA_ARG:
4553 {
4554 tree expression;
4555 tree type;
4556 source_location type_location;
4557
4558 /* The `__builtin_va_arg' construct is used to handle
4559 `va_arg'. Consume the `__builtin_va_arg' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Look for the opening `('. */
4562 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4563 /* Now, parse the assignment-expression. */
4564 expression = cp_parser_assignment_expression (parser);
4565 /* Look for the `,'. */
4566 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4567 type_location = cp_lexer_peek_token (parser->lexer)->location;
4568 /* Parse the type-id. */
4569 type = cp_parser_type_id (parser);
4570 /* Look for the closing `)'. */
4571 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4572 /* Using `va_arg' in a constant-expression is not
4573 allowed. */
4574 if (cp_parser_non_integral_constant_expression (parser,
4575 NIC_VA_ARG))
4576 return error_mark_node;
4577 return build_x_va_arg (type_location, expression, type);
4578 }
4579
4580 case RID_OFFSETOF:
4581 return cp_parser_builtin_offsetof (parser);
4582
4583 case RID_HAS_NOTHROW_ASSIGN:
4584 case RID_HAS_NOTHROW_CONSTRUCTOR:
4585 case RID_HAS_NOTHROW_COPY:
4586 case RID_HAS_TRIVIAL_ASSIGN:
4587 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4588 case RID_HAS_TRIVIAL_COPY:
4589 case RID_HAS_TRIVIAL_DESTRUCTOR:
4590 case RID_HAS_VIRTUAL_DESTRUCTOR:
4591 case RID_IS_ABSTRACT:
4592 case RID_IS_BASE_OF:
4593 case RID_IS_CLASS:
4594 case RID_IS_EMPTY:
4595 case RID_IS_ENUM:
4596 case RID_IS_FINAL:
4597 case RID_IS_LITERAL_TYPE:
4598 case RID_IS_POD:
4599 case RID_IS_POLYMORPHIC:
4600 case RID_IS_STD_LAYOUT:
4601 case RID_IS_TRIVIAL:
4602 case RID_IS_TRIVIALLY_ASSIGNABLE:
4603 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4604 case RID_IS_TRIVIALLY_COPYABLE:
4605 case RID_IS_UNION:
4606 return cp_parser_trait_expr (parser, token->keyword);
4607
4608 /* Objective-C++ expressions. */
4609 case RID_AT_ENCODE:
4610 case RID_AT_PROTOCOL:
4611 case RID_AT_SELECTOR:
4612 return cp_parser_objc_expression (parser);
4613
4614 case RID_TEMPLATE:
4615 if (parser->in_function_body
4616 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4617 == CPP_LESS))
4618 {
4619 error_at (token->location,
4620 "a template declaration cannot appear at block scope");
4621 cp_parser_skip_to_end_of_block_or_statement (parser);
4622 return error_mark_node;
4623 }
4624 default:
4625 cp_parser_error (parser, "expected primary-expression");
4626 return error_mark_node;
4627 }
4628
4629 /* An id-expression can start with either an identifier, a
4630 `::' as the beginning of a qualified-id, or the "operator"
4631 keyword. */
4632 case CPP_NAME:
4633 case CPP_SCOPE:
4634 case CPP_TEMPLATE_ID:
4635 case CPP_NESTED_NAME_SPECIFIER:
4636 {
4637 tree id_expression;
4638 tree decl;
4639 const char *error_msg;
4640 bool template_p;
4641 bool done;
4642 cp_token *id_expr_token;
4643
4644 id_expression:
4645 /* Parse the id-expression. */
4646 id_expression
4647 = cp_parser_id_expression (parser,
4648 /*template_keyword_p=*/false,
4649 /*check_dependency_p=*/true,
4650 &template_p,
4651 /*declarator_p=*/false,
4652 /*optional_p=*/false);
4653 if (id_expression == error_mark_node)
4654 return error_mark_node;
4655 id_expr_token = token;
4656 token = cp_lexer_peek_token (parser->lexer);
4657 done = (token->type != CPP_OPEN_SQUARE
4658 && token->type != CPP_OPEN_PAREN
4659 && token->type != CPP_DOT
4660 && token->type != CPP_DEREF
4661 && token->type != CPP_PLUS_PLUS
4662 && token->type != CPP_MINUS_MINUS);
4663 /* If we have a template-id, then no further lookup is
4664 required. If the template-id was for a template-class, we
4665 will sometimes have a TYPE_DECL at this point. */
4666 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4667 || TREE_CODE (id_expression) == TYPE_DECL)
4668 decl = id_expression;
4669 /* Look up the name. */
4670 else
4671 {
4672 tree ambiguous_decls;
4673
4674 /* If we already know that this lookup is ambiguous, then
4675 we've already issued an error message; there's no reason
4676 to check again. */
4677 if (id_expr_token->type == CPP_NAME
4678 && id_expr_token->error_reported)
4679 {
4680 cp_parser_simulate_error (parser);
4681 return error_mark_node;
4682 }
4683
4684 decl = cp_parser_lookup_name (parser, id_expression,
4685 none_type,
4686 template_p,
4687 /*is_namespace=*/false,
4688 /*check_dependency=*/true,
4689 &ambiguous_decls,
4690 id_expr_token->location);
4691 /* If the lookup was ambiguous, an error will already have
4692 been issued. */
4693 if (ambiguous_decls)
4694 return error_mark_node;
4695
4696 /* In Objective-C++, we may have an Objective-C 2.0
4697 dot-syntax for classes here. */
4698 if (c_dialect_objc ()
4699 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4700 && TREE_CODE (decl) == TYPE_DECL
4701 && objc_is_class_name (decl))
4702 {
4703 tree component;
4704 cp_lexer_consume_token (parser->lexer);
4705 component = cp_parser_identifier (parser);
4706 if (component == error_mark_node)
4707 return error_mark_node;
4708
4709 return objc_build_class_component_ref (id_expression, component);
4710 }
4711
4712 /* In Objective-C++, an instance variable (ivar) may be preferred
4713 to whatever cp_parser_lookup_name() found. */
4714 decl = objc_lookup_ivar (decl, id_expression);
4715
4716 /* If name lookup gives us a SCOPE_REF, then the
4717 qualifying scope was dependent. */
4718 if (TREE_CODE (decl) == SCOPE_REF)
4719 {
4720 /* At this point, we do not know if DECL is a valid
4721 integral constant expression. We assume that it is
4722 in fact such an expression, so that code like:
4723
4724 template <int N> struct A {
4725 int a[B<N>::i];
4726 };
4727
4728 is accepted. At template-instantiation time, we
4729 will check that B<N>::i is actually a constant. */
4730 return decl;
4731 }
4732 /* Check to see if DECL is a local variable in a context
4733 where that is forbidden. */
4734 if (parser->local_variables_forbidden_p
4735 && local_variable_p (decl))
4736 {
4737 /* It might be that we only found DECL because we are
4738 trying to be generous with pre-ISO scoping rules.
4739 For example, consider:
4740
4741 int i;
4742 void g() {
4743 for (int i = 0; i < 10; ++i) {}
4744 extern void f(int j = i);
4745 }
4746
4747 Here, name look up will originally find the out
4748 of scope `i'. We need to issue a warning message,
4749 but then use the global `i'. */
4750 decl = check_for_out_of_scope_variable (decl);
4751 if (local_variable_p (decl))
4752 {
4753 error_at (id_expr_token->location,
4754 "local variable %qD may not appear in this context",
4755 decl);
4756 return error_mark_node;
4757 }
4758 }
4759 }
4760
4761 decl = (finish_id_expression
4762 (id_expression, decl, parser->scope,
4763 idk,
4764 parser->integral_constant_expression_p,
4765 parser->allow_non_integral_constant_expression_p,
4766 &parser->non_integral_constant_expression_p,
4767 template_p, done, address_p,
4768 template_arg_p,
4769 &error_msg,
4770 id_expr_token->location));
4771 if (error_msg)
4772 cp_parser_error (parser, error_msg);
4773 return decl;
4774 }
4775
4776 /* Anything else is an error. */
4777 default:
4778 cp_parser_error (parser, "expected primary-expression");
4779 return error_mark_node;
4780 }
4781 }
4782
4783 static inline tree
4784 cp_parser_primary_expression (cp_parser *parser,
4785 bool address_p,
4786 bool cast_p,
4787 bool template_arg_p,
4788 cp_id_kind *idk)
4789 {
4790 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4791 /*decltype*/false, idk);
4792 }
4793
4794 /* Parse an id-expression.
4795
4796 id-expression:
4797 unqualified-id
4798 qualified-id
4799
4800 qualified-id:
4801 :: [opt] nested-name-specifier template [opt] unqualified-id
4802 :: identifier
4803 :: operator-function-id
4804 :: template-id
4805
4806 Return a representation of the unqualified portion of the
4807 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4808 a `::' or nested-name-specifier.
4809
4810 Often, if the id-expression was a qualified-id, the caller will
4811 want to make a SCOPE_REF to represent the qualified-id. This
4812 function does not do this in order to avoid wastefully creating
4813 SCOPE_REFs when they are not required.
4814
4815 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4816 `template' keyword.
4817
4818 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4819 uninstantiated templates.
4820
4821 If *TEMPLATE_P is non-NULL, it is set to true iff the
4822 `template' keyword is used to explicitly indicate that the entity
4823 named is a template.
4824
4825 If DECLARATOR_P is true, the id-expression is appearing as part of
4826 a declarator, rather than as part of an expression. */
4827
4828 static tree
4829 cp_parser_id_expression (cp_parser *parser,
4830 bool template_keyword_p,
4831 bool check_dependency_p,
4832 bool *template_p,
4833 bool declarator_p,
4834 bool optional_p)
4835 {
4836 bool global_scope_p;
4837 bool nested_name_specifier_p;
4838
4839 /* Assume the `template' keyword was not used. */
4840 if (template_p)
4841 *template_p = template_keyword_p;
4842
4843 /* Look for the optional `::' operator. */
4844 global_scope_p
4845 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4846 != NULL_TREE);
4847 /* Look for the optional nested-name-specifier. */
4848 nested_name_specifier_p
4849 = (cp_parser_nested_name_specifier_opt (parser,
4850 /*typename_keyword_p=*/false,
4851 check_dependency_p,
4852 /*type_p=*/false,
4853 declarator_p)
4854 != NULL_TREE);
4855 /* If there is a nested-name-specifier, then we are looking at
4856 the first qualified-id production. */
4857 if (nested_name_specifier_p)
4858 {
4859 tree saved_scope;
4860 tree saved_object_scope;
4861 tree saved_qualifying_scope;
4862 tree unqualified_id;
4863 bool is_template;
4864
4865 /* See if the next token is the `template' keyword. */
4866 if (!template_p)
4867 template_p = &is_template;
4868 *template_p = cp_parser_optional_template_keyword (parser);
4869 /* Name lookup we do during the processing of the
4870 unqualified-id might obliterate SCOPE. */
4871 saved_scope = parser->scope;
4872 saved_object_scope = parser->object_scope;
4873 saved_qualifying_scope = parser->qualifying_scope;
4874 /* Process the final unqualified-id. */
4875 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4876 check_dependency_p,
4877 declarator_p,
4878 /*optional_p=*/false);
4879 /* Restore the SAVED_SCOPE for our caller. */
4880 parser->scope = saved_scope;
4881 parser->object_scope = saved_object_scope;
4882 parser->qualifying_scope = saved_qualifying_scope;
4883
4884 return unqualified_id;
4885 }
4886 /* Otherwise, if we are in global scope, then we are looking at one
4887 of the other qualified-id productions. */
4888 else if (global_scope_p)
4889 {
4890 cp_token *token;
4891 tree id;
4892
4893 /* Peek at the next token. */
4894 token = cp_lexer_peek_token (parser->lexer);
4895
4896 /* If it's an identifier, and the next token is not a "<", then
4897 we can avoid the template-id case. This is an optimization
4898 for this common case. */
4899 if (token->type == CPP_NAME
4900 && !cp_parser_nth_token_starts_template_argument_list_p
4901 (parser, 2))
4902 return cp_parser_identifier (parser);
4903
4904 cp_parser_parse_tentatively (parser);
4905 /* Try a template-id. */
4906 id = cp_parser_template_id (parser,
4907 /*template_keyword_p=*/false,
4908 /*check_dependency_p=*/true,
4909 none_type,
4910 declarator_p);
4911 /* If that worked, we're done. */
4912 if (cp_parser_parse_definitely (parser))
4913 return id;
4914
4915 /* Peek at the next token. (Changes in the token buffer may
4916 have invalidated the pointer obtained above.) */
4917 token = cp_lexer_peek_token (parser->lexer);
4918
4919 switch (token->type)
4920 {
4921 case CPP_NAME:
4922 return cp_parser_identifier (parser);
4923
4924 case CPP_KEYWORD:
4925 if (token->keyword == RID_OPERATOR)
4926 return cp_parser_operator_function_id (parser);
4927 /* Fall through. */
4928
4929 default:
4930 cp_parser_error (parser, "expected id-expression");
4931 return error_mark_node;
4932 }
4933 }
4934 else
4935 return cp_parser_unqualified_id (parser, template_keyword_p,
4936 /*check_dependency_p=*/true,
4937 declarator_p,
4938 optional_p);
4939 }
4940
4941 /* Parse an unqualified-id.
4942
4943 unqualified-id:
4944 identifier
4945 operator-function-id
4946 conversion-function-id
4947 ~ class-name
4948 template-id
4949
4950 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4951 keyword, in a construct like `A::template ...'.
4952
4953 Returns a representation of unqualified-id. For the `identifier'
4954 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4955 production a BIT_NOT_EXPR is returned; the operand of the
4956 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4957 other productions, see the documentation accompanying the
4958 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4959 names are looked up in uninstantiated templates. If DECLARATOR_P
4960 is true, the unqualified-id is appearing as part of a declarator,
4961 rather than as part of an expression. */
4962
4963 static tree
4964 cp_parser_unqualified_id (cp_parser* parser,
4965 bool template_keyword_p,
4966 bool check_dependency_p,
4967 bool declarator_p,
4968 bool optional_p)
4969 {
4970 cp_token *token;
4971
4972 /* Peek at the next token. */
4973 token = cp_lexer_peek_token (parser->lexer);
4974
4975 switch ((int) token->type)
4976 {
4977 case CPP_NAME:
4978 {
4979 tree id;
4980
4981 /* We don't know yet whether or not this will be a
4982 template-id. */
4983 cp_parser_parse_tentatively (parser);
4984 /* Try a template-id. */
4985 id = cp_parser_template_id (parser, template_keyword_p,
4986 check_dependency_p,
4987 none_type,
4988 declarator_p);
4989 /* If it worked, we're done. */
4990 if (cp_parser_parse_definitely (parser))
4991 return id;
4992 /* Otherwise, it's an ordinary identifier. */
4993 return cp_parser_identifier (parser);
4994 }
4995
4996 case CPP_TEMPLATE_ID:
4997 return cp_parser_template_id (parser, template_keyword_p,
4998 check_dependency_p,
4999 none_type,
5000 declarator_p);
5001
5002 case CPP_COMPL:
5003 {
5004 tree type_decl;
5005 tree qualifying_scope;
5006 tree object_scope;
5007 tree scope;
5008 bool done;
5009
5010 /* Consume the `~' token. */
5011 cp_lexer_consume_token (parser->lexer);
5012 /* Parse the class-name. The standard, as written, seems to
5013 say that:
5014
5015 template <typename T> struct S { ~S (); };
5016 template <typename T> S<T>::~S() {}
5017
5018 is invalid, since `~' must be followed by a class-name, but
5019 `S<T>' is dependent, and so not known to be a class.
5020 That's not right; we need to look in uninstantiated
5021 templates. A further complication arises from:
5022
5023 template <typename T> void f(T t) {
5024 t.T::~T();
5025 }
5026
5027 Here, it is not possible to look up `T' in the scope of `T'
5028 itself. We must look in both the current scope, and the
5029 scope of the containing complete expression.
5030
5031 Yet another issue is:
5032
5033 struct S {
5034 int S;
5035 ~S();
5036 };
5037
5038 S::~S() {}
5039
5040 The standard does not seem to say that the `S' in `~S'
5041 should refer to the type `S' and not the data member
5042 `S::S'. */
5043
5044 /* DR 244 says that we look up the name after the "~" in the
5045 same scope as we looked up the qualifying name. That idea
5046 isn't fully worked out; it's more complicated than that. */
5047 scope = parser->scope;
5048 object_scope = parser->object_scope;
5049 qualifying_scope = parser->qualifying_scope;
5050
5051 /* Check for invalid scopes. */
5052 if (scope == error_mark_node)
5053 {
5054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5055 cp_lexer_consume_token (parser->lexer);
5056 return error_mark_node;
5057 }
5058 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5059 {
5060 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5061 error_at (token->location,
5062 "scope %qT before %<~%> is not a class-name",
5063 scope);
5064 cp_parser_simulate_error (parser);
5065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5066 cp_lexer_consume_token (parser->lexer);
5067 return error_mark_node;
5068 }
5069 gcc_assert (!scope || TYPE_P (scope));
5070
5071 /* If the name is of the form "X::~X" it's OK even if X is a
5072 typedef. */
5073 token = cp_lexer_peek_token (parser->lexer);
5074 if (scope
5075 && token->type == CPP_NAME
5076 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5077 != CPP_LESS)
5078 && (token->u.value == TYPE_IDENTIFIER (scope)
5079 || (CLASS_TYPE_P (scope)
5080 && constructor_name_p (token->u.value, scope))))
5081 {
5082 cp_lexer_consume_token (parser->lexer);
5083 return build_nt (BIT_NOT_EXPR, scope);
5084 }
5085
5086 /* ~auto means the destructor of whatever the object is. */
5087 if (cp_parser_is_keyword (token, RID_AUTO))
5088 {
5089 if (cxx_dialect < cxx14)
5090 pedwarn (input_location, 0,
5091 "%<~auto%> only available with "
5092 "-std=c++14 or -std=gnu++14");
5093 cp_lexer_consume_token (parser->lexer);
5094 return build_nt (BIT_NOT_EXPR, make_auto ());
5095 }
5096
5097 /* If there was an explicit qualification (S::~T), first look
5098 in the scope given by the qualification (i.e., S).
5099
5100 Note: in the calls to cp_parser_class_name below we pass
5101 typename_type so that lookup finds the injected-class-name
5102 rather than the constructor. */
5103 done = false;
5104 type_decl = NULL_TREE;
5105 if (scope)
5106 {
5107 cp_parser_parse_tentatively (parser);
5108 type_decl = cp_parser_class_name (parser,
5109 /*typename_keyword_p=*/false,
5110 /*template_keyword_p=*/false,
5111 typename_type,
5112 /*check_dependency=*/false,
5113 /*class_head_p=*/false,
5114 declarator_p);
5115 if (cp_parser_parse_definitely (parser))
5116 done = true;
5117 }
5118 /* In "N::S::~S", look in "N" as well. */
5119 if (!done && scope && qualifying_scope)
5120 {
5121 cp_parser_parse_tentatively (parser);
5122 parser->scope = qualifying_scope;
5123 parser->object_scope = NULL_TREE;
5124 parser->qualifying_scope = NULL_TREE;
5125 type_decl
5126 = cp_parser_class_name (parser,
5127 /*typename_keyword_p=*/false,
5128 /*template_keyword_p=*/false,
5129 typename_type,
5130 /*check_dependency=*/false,
5131 /*class_head_p=*/false,
5132 declarator_p);
5133 if (cp_parser_parse_definitely (parser))
5134 done = true;
5135 }
5136 /* In "p->S::~T", look in the scope given by "*p" as well. */
5137 else if (!done && object_scope)
5138 {
5139 cp_parser_parse_tentatively (parser);
5140 parser->scope = object_scope;
5141 parser->object_scope = NULL_TREE;
5142 parser->qualifying_scope = NULL_TREE;
5143 type_decl
5144 = cp_parser_class_name (parser,
5145 /*typename_keyword_p=*/false,
5146 /*template_keyword_p=*/false,
5147 typename_type,
5148 /*check_dependency=*/false,
5149 /*class_head_p=*/false,
5150 declarator_p);
5151 if (cp_parser_parse_definitely (parser))
5152 done = true;
5153 }
5154 /* Look in the surrounding context. */
5155 if (!done)
5156 {
5157 parser->scope = NULL_TREE;
5158 parser->object_scope = NULL_TREE;
5159 parser->qualifying_scope = NULL_TREE;
5160 if (processing_template_decl)
5161 cp_parser_parse_tentatively (parser);
5162 type_decl
5163 = cp_parser_class_name (parser,
5164 /*typename_keyword_p=*/false,
5165 /*template_keyword_p=*/false,
5166 typename_type,
5167 /*check_dependency=*/false,
5168 /*class_head_p=*/false,
5169 declarator_p);
5170 if (processing_template_decl
5171 && ! cp_parser_parse_definitely (parser))
5172 {
5173 /* We couldn't find a type with this name, so just accept
5174 it and check for a match at instantiation time. */
5175 type_decl = cp_parser_identifier (parser);
5176 if (type_decl != error_mark_node)
5177 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5178 return type_decl;
5179 }
5180 }
5181 /* If an error occurred, assume that the name of the
5182 destructor is the same as the name of the qualifying
5183 class. That allows us to keep parsing after running
5184 into ill-formed destructor names. */
5185 if (type_decl == error_mark_node && scope)
5186 return build_nt (BIT_NOT_EXPR, scope);
5187 else if (type_decl == error_mark_node)
5188 return error_mark_node;
5189
5190 /* Check that destructor name and scope match. */
5191 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5192 {
5193 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5194 error_at (token->location,
5195 "declaration of %<~%T%> as member of %qT",
5196 type_decl, scope);
5197 cp_parser_simulate_error (parser);
5198 return error_mark_node;
5199 }
5200
5201 /* [class.dtor]
5202
5203 A typedef-name that names a class shall not be used as the
5204 identifier in the declarator for a destructor declaration. */
5205 if (declarator_p
5206 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5207 && !DECL_SELF_REFERENCE_P (type_decl)
5208 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5209 error_at (token->location,
5210 "typedef-name %qD used as destructor declarator",
5211 type_decl);
5212
5213 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5214 }
5215
5216 case CPP_KEYWORD:
5217 if (token->keyword == RID_OPERATOR)
5218 {
5219 tree id;
5220
5221 /* This could be a template-id, so we try that first. */
5222 cp_parser_parse_tentatively (parser);
5223 /* Try a template-id. */
5224 id = cp_parser_template_id (parser, template_keyword_p,
5225 /*check_dependency_p=*/true,
5226 none_type,
5227 declarator_p);
5228 /* If that worked, we're done. */
5229 if (cp_parser_parse_definitely (parser))
5230 return id;
5231 /* We still don't know whether we're looking at an
5232 operator-function-id or a conversion-function-id. */
5233 cp_parser_parse_tentatively (parser);
5234 /* Try an operator-function-id. */
5235 id = cp_parser_operator_function_id (parser);
5236 /* If that didn't work, try a conversion-function-id. */
5237 if (!cp_parser_parse_definitely (parser))
5238 id = cp_parser_conversion_function_id (parser);
5239 else if (UDLIT_OPER_P (id))
5240 {
5241 /* 17.6.3.3.5 */
5242 const char *name = UDLIT_OP_SUFFIX (id);
5243 if (name[0] != '_' && !in_system_header_at (input_location)
5244 && declarator_p)
5245 warning (0, "literal operator suffixes not preceded by %<_%>"
5246 " are reserved for future standardization");
5247 }
5248
5249 return id;
5250 }
5251 /* Fall through. */
5252
5253 default:
5254 if (optional_p)
5255 return NULL_TREE;
5256 cp_parser_error (parser, "expected unqualified-id");
5257 return error_mark_node;
5258 }
5259 }
5260
5261 /* Parse an (optional) nested-name-specifier.
5262
5263 nested-name-specifier: [C++98]
5264 class-or-namespace-name :: nested-name-specifier [opt]
5265 class-or-namespace-name :: template nested-name-specifier [opt]
5266
5267 nested-name-specifier: [C++0x]
5268 type-name ::
5269 namespace-name ::
5270 nested-name-specifier identifier ::
5271 nested-name-specifier template [opt] simple-template-id ::
5272
5273 PARSER->SCOPE should be set appropriately before this function is
5274 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5275 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5276 in name lookups.
5277
5278 Sets PARSER->SCOPE to the class (TYPE) or namespace
5279 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5280 it unchanged if there is no nested-name-specifier. Returns the new
5281 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5282
5283 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5284 part of a declaration and/or decl-specifier. */
5285
5286 static tree
5287 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5288 bool typename_keyword_p,
5289 bool check_dependency_p,
5290 bool type_p,
5291 bool is_declaration)
5292 {
5293 bool success = false;
5294 cp_token_position start = 0;
5295 cp_token *token;
5296
5297 /* Remember where the nested-name-specifier starts. */
5298 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5299 {
5300 start = cp_lexer_token_position (parser->lexer, false);
5301 push_deferring_access_checks (dk_deferred);
5302 }
5303
5304 while (true)
5305 {
5306 tree new_scope;
5307 tree old_scope;
5308 tree saved_qualifying_scope;
5309 bool template_keyword_p;
5310
5311 /* Spot cases that cannot be the beginning of a
5312 nested-name-specifier. */
5313 token = cp_lexer_peek_token (parser->lexer);
5314
5315 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5316 the already parsed nested-name-specifier. */
5317 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5318 {
5319 /* Grab the nested-name-specifier and continue the loop. */
5320 cp_parser_pre_parsed_nested_name_specifier (parser);
5321 /* If we originally encountered this nested-name-specifier
5322 with IS_DECLARATION set to false, we will not have
5323 resolved TYPENAME_TYPEs, so we must do so here. */
5324 if (is_declaration
5325 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5326 {
5327 new_scope = resolve_typename_type (parser->scope,
5328 /*only_current_p=*/false);
5329 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5330 parser->scope = new_scope;
5331 }
5332 success = true;
5333 continue;
5334 }
5335
5336 /* Spot cases that cannot be the beginning of a
5337 nested-name-specifier. On the second and subsequent times
5338 through the loop, we look for the `template' keyword. */
5339 if (success && token->keyword == RID_TEMPLATE)
5340 ;
5341 /* A template-id can start a nested-name-specifier. */
5342 else if (token->type == CPP_TEMPLATE_ID)
5343 ;
5344 /* DR 743: decltype can be used in a nested-name-specifier. */
5345 else if (token_is_decltype (token))
5346 ;
5347 else
5348 {
5349 /* If the next token is not an identifier, then it is
5350 definitely not a type-name or namespace-name. */
5351 if (token->type != CPP_NAME)
5352 break;
5353 /* If the following token is neither a `<' (to begin a
5354 template-id), nor a `::', then we are not looking at a
5355 nested-name-specifier. */
5356 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5357
5358 if (token->type == CPP_COLON
5359 && parser->colon_corrects_to_scope_p
5360 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5361 {
5362 error_at (token->location,
5363 "found %<:%> in nested-name-specifier, expected %<::%>");
5364 token->type = CPP_SCOPE;
5365 }
5366
5367 if (token->type != CPP_SCOPE
5368 && !cp_parser_nth_token_starts_template_argument_list_p
5369 (parser, 2))
5370 break;
5371 }
5372
5373 /* The nested-name-specifier is optional, so we parse
5374 tentatively. */
5375 cp_parser_parse_tentatively (parser);
5376
5377 /* Look for the optional `template' keyword, if this isn't the
5378 first time through the loop. */
5379 if (success)
5380 template_keyword_p = cp_parser_optional_template_keyword (parser);
5381 else
5382 template_keyword_p = false;
5383
5384 /* Save the old scope since the name lookup we are about to do
5385 might destroy it. */
5386 old_scope = parser->scope;
5387 saved_qualifying_scope = parser->qualifying_scope;
5388 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5389 look up names in "X<T>::I" in order to determine that "Y" is
5390 a template. So, if we have a typename at this point, we make
5391 an effort to look through it. */
5392 if (is_declaration
5393 && !typename_keyword_p
5394 && parser->scope
5395 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5396 parser->scope = resolve_typename_type (parser->scope,
5397 /*only_current_p=*/false);
5398 /* Parse the qualifying entity. */
5399 new_scope
5400 = cp_parser_qualifying_entity (parser,
5401 typename_keyword_p,
5402 template_keyword_p,
5403 check_dependency_p,
5404 type_p,
5405 is_declaration);
5406 /* Look for the `::' token. */
5407 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5408
5409 /* If we found what we wanted, we keep going; otherwise, we're
5410 done. */
5411 if (!cp_parser_parse_definitely (parser))
5412 {
5413 bool error_p = false;
5414
5415 /* Restore the OLD_SCOPE since it was valid before the
5416 failed attempt at finding the last
5417 class-or-namespace-name. */
5418 parser->scope = old_scope;
5419 parser->qualifying_scope = saved_qualifying_scope;
5420
5421 /* If the next token is a decltype, and the one after that is a
5422 `::', then the decltype has failed to resolve to a class or
5423 enumeration type. Give this error even when parsing
5424 tentatively since it can't possibly be valid--and we're going
5425 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5426 won't get another chance.*/
5427 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5428 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5429 == CPP_SCOPE))
5430 {
5431 token = cp_lexer_consume_token (parser->lexer);
5432 error_at (token->location, "decltype evaluates to %qT, "
5433 "which is not a class or enumeration type",
5434 token->u.value);
5435 parser->scope = error_mark_node;
5436 error_p = true;
5437 /* As below. */
5438 success = true;
5439 cp_lexer_consume_token (parser->lexer);
5440 }
5441
5442 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5443 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5444 {
5445 /* If we have a non-type template-id followed by ::, it can't
5446 possibly be valid. */
5447 token = cp_lexer_peek_token (parser->lexer);
5448 tree tid = token->u.tree_check_value->value;
5449 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5450 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5451 {
5452 tree tmpl = NULL_TREE;
5453 if (is_overloaded_fn (tid))
5454 {
5455 tree fns = get_fns (tid);
5456 if (!OVL_CHAIN (fns))
5457 tmpl = OVL_CURRENT (fns);
5458 error_at (token->location, "function template-id %qD "
5459 "in nested-name-specifier", tid);
5460 }
5461 else
5462 {
5463 /* Variable template. */
5464 tmpl = TREE_OPERAND (tid, 0);
5465 gcc_assert (variable_template_p (tmpl));
5466 error_at (token->location, "variable template-id %qD "
5467 "in nested-name-specifier", tid);
5468 }
5469 if (tmpl)
5470 inform (DECL_SOURCE_LOCATION (tmpl),
5471 "%qD declared here", tmpl);
5472
5473 parser->scope = error_mark_node;
5474 error_p = true;
5475 /* As below. */
5476 success = true;
5477 cp_lexer_consume_token (parser->lexer);
5478 cp_lexer_consume_token (parser->lexer);
5479 }
5480 }
5481
5482 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5483 break;
5484 /* If the next token is an identifier, and the one after
5485 that is a `::', then any valid interpretation would have
5486 found a class-or-namespace-name. */
5487 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5488 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5489 == CPP_SCOPE)
5490 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5491 != CPP_COMPL))
5492 {
5493 token = cp_lexer_consume_token (parser->lexer);
5494 if (!error_p)
5495 {
5496 if (!token->error_reported)
5497 {
5498 tree decl;
5499 tree ambiguous_decls;
5500
5501 decl = cp_parser_lookup_name (parser, token->u.value,
5502 none_type,
5503 /*is_template=*/false,
5504 /*is_namespace=*/false,
5505 /*check_dependency=*/true,
5506 &ambiguous_decls,
5507 token->location);
5508 if (TREE_CODE (decl) == TEMPLATE_DECL)
5509 error_at (token->location,
5510 "%qD used without template parameters",
5511 decl);
5512 else if (ambiguous_decls)
5513 {
5514 // cp_parser_lookup_name has the same diagnostic,
5515 // thus make sure to emit it at most once.
5516 if (cp_parser_uncommitted_to_tentative_parse_p
5517 (parser))
5518 {
5519 error_at (token->location,
5520 "reference to %qD is ambiguous",
5521 token->u.value);
5522 print_candidates (ambiguous_decls);
5523 }
5524 decl = error_mark_node;
5525 }
5526 else
5527 {
5528 if (cxx_dialect != cxx98)
5529 cp_parser_name_lookup_error
5530 (parser, token->u.value, decl, NLE_NOT_CXX98,
5531 token->location);
5532 else
5533 cp_parser_name_lookup_error
5534 (parser, token->u.value, decl, NLE_CXX98,
5535 token->location);
5536 }
5537 }
5538 parser->scope = error_mark_node;
5539 error_p = true;
5540 /* Treat this as a successful nested-name-specifier
5541 due to:
5542
5543 [basic.lookup.qual]
5544
5545 If the name found is not a class-name (clause
5546 _class_) or namespace-name (_namespace.def_), the
5547 program is ill-formed. */
5548 success = true;
5549 }
5550 cp_lexer_consume_token (parser->lexer);
5551 }
5552 break;
5553 }
5554 /* We've found one valid nested-name-specifier. */
5555 success = true;
5556 /* Name lookup always gives us a DECL. */
5557 if (TREE_CODE (new_scope) == TYPE_DECL)
5558 new_scope = TREE_TYPE (new_scope);
5559 /* Uses of "template" must be followed by actual templates. */
5560 if (template_keyword_p
5561 && !(CLASS_TYPE_P (new_scope)
5562 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5563 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5564 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5565 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5566 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5567 == TEMPLATE_ID_EXPR)))
5568 permerror (input_location, TYPE_P (new_scope)
5569 ? G_("%qT is not a template")
5570 : G_("%qD is not a template"),
5571 new_scope);
5572 /* If it is a class scope, try to complete it; we are about to
5573 be looking up names inside the class. */
5574 if (TYPE_P (new_scope)
5575 /* Since checking types for dependency can be expensive,
5576 avoid doing it if the type is already complete. */
5577 && !COMPLETE_TYPE_P (new_scope)
5578 /* Do not try to complete dependent types. */
5579 && !dependent_type_p (new_scope))
5580 {
5581 new_scope = complete_type (new_scope);
5582 /* If it is a typedef to current class, use the current
5583 class instead, as the typedef won't have any names inside
5584 it yet. */
5585 if (!COMPLETE_TYPE_P (new_scope)
5586 && currently_open_class (new_scope))
5587 new_scope = TYPE_MAIN_VARIANT (new_scope);
5588 }
5589 /* Make sure we look in the right scope the next time through
5590 the loop. */
5591 parser->scope = new_scope;
5592 }
5593
5594 /* If parsing tentatively, replace the sequence of tokens that makes
5595 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5596 token. That way, should we re-parse the token stream, we will
5597 not have to repeat the effort required to do the parse, nor will
5598 we issue duplicate error messages. */
5599 if (success && start)
5600 {
5601 cp_token *token;
5602
5603 token = cp_lexer_token_at (parser->lexer, start);
5604 /* Reset the contents of the START token. */
5605 token->type = CPP_NESTED_NAME_SPECIFIER;
5606 /* Retrieve any deferred checks. Do not pop this access checks yet
5607 so the memory will not be reclaimed during token replacing below. */
5608 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5609 token->u.tree_check_value->value = parser->scope;
5610 token->u.tree_check_value->checks = get_deferred_access_checks ();
5611 token->u.tree_check_value->qualifying_scope =
5612 parser->qualifying_scope;
5613 token->keyword = RID_MAX;
5614
5615 /* Purge all subsequent tokens. */
5616 cp_lexer_purge_tokens_after (parser->lexer, start);
5617 }
5618
5619 if (start)
5620 pop_to_parent_deferring_access_checks ();
5621
5622 return success ? parser->scope : NULL_TREE;
5623 }
5624
5625 /* Parse a nested-name-specifier. See
5626 cp_parser_nested_name_specifier_opt for details. This function
5627 behaves identically, except that it will an issue an error if no
5628 nested-name-specifier is present. */
5629
5630 static tree
5631 cp_parser_nested_name_specifier (cp_parser *parser,
5632 bool typename_keyword_p,
5633 bool check_dependency_p,
5634 bool type_p,
5635 bool is_declaration)
5636 {
5637 tree scope;
5638
5639 /* Look for the nested-name-specifier. */
5640 scope = cp_parser_nested_name_specifier_opt (parser,
5641 typename_keyword_p,
5642 check_dependency_p,
5643 type_p,
5644 is_declaration);
5645 /* If it was not present, issue an error message. */
5646 if (!scope)
5647 {
5648 cp_parser_error (parser, "expected nested-name-specifier");
5649 parser->scope = NULL_TREE;
5650 }
5651
5652 return scope;
5653 }
5654
5655 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5656 this is either a class-name or a namespace-name (which corresponds
5657 to the class-or-namespace-name production in the grammar). For
5658 C++0x, it can also be a type-name that refers to an enumeration
5659 type or a simple-template-id.
5660
5661 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5662 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5663 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5664 TYPE_P is TRUE iff the next name should be taken as a class-name,
5665 even the same name is declared to be another entity in the same
5666 scope.
5667
5668 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5669 specified by the class-or-namespace-name. If neither is found the
5670 ERROR_MARK_NODE is returned. */
5671
5672 static tree
5673 cp_parser_qualifying_entity (cp_parser *parser,
5674 bool typename_keyword_p,
5675 bool template_keyword_p,
5676 bool check_dependency_p,
5677 bool type_p,
5678 bool is_declaration)
5679 {
5680 tree saved_scope;
5681 tree saved_qualifying_scope;
5682 tree saved_object_scope;
5683 tree scope;
5684 bool only_class_p;
5685 bool successful_parse_p;
5686
5687 /* DR 743: decltype can appear in a nested-name-specifier. */
5688 if (cp_lexer_next_token_is_decltype (parser->lexer))
5689 {
5690 scope = cp_parser_decltype (parser);
5691 if (TREE_CODE (scope) != ENUMERAL_TYPE
5692 && !MAYBE_CLASS_TYPE_P (scope))
5693 {
5694 cp_parser_simulate_error (parser);
5695 return error_mark_node;
5696 }
5697 if (TYPE_NAME (scope))
5698 scope = TYPE_NAME (scope);
5699 return scope;
5700 }
5701
5702 /* Before we try to parse the class-name, we must save away the
5703 current PARSER->SCOPE since cp_parser_class_name will destroy
5704 it. */
5705 saved_scope = parser->scope;
5706 saved_qualifying_scope = parser->qualifying_scope;
5707 saved_object_scope = parser->object_scope;
5708 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5709 there is no need to look for a namespace-name. */
5710 only_class_p = template_keyword_p
5711 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5712 if (!only_class_p)
5713 cp_parser_parse_tentatively (parser);
5714 scope = cp_parser_class_name (parser,
5715 typename_keyword_p,
5716 template_keyword_p,
5717 type_p ? class_type : none_type,
5718 check_dependency_p,
5719 /*class_head_p=*/false,
5720 is_declaration,
5721 /*enum_ok=*/cxx_dialect > cxx98);
5722 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5723 /* If that didn't work, try for a namespace-name. */
5724 if (!only_class_p && !successful_parse_p)
5725 {
5726 /* Restore the saved scope. */
5727 parser->scope = saved_scope;
5728 parser->qualifying_scope = saved_qualifying_scope;
5729 parser->object_scope = saved_object_scope;
5730 /* If we are not looking at an identifier followed by the scope
5731 resolution operator, then this is not part of a
5732 nested-name-specifier. (Note that this function is only used
5733 to parse the components of a nested-name-specifier.) */
5734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5735 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5736 return error_mark_node;
5737 scope = cp_parser_namespace_name (parser);
5738 }
5739
5740 return scope;
5741 }
5742
5743 /* Return true if we are looking at a compound-literal, false otherwise. */
5744
5745 static bool
5746 cp_parser_compound_literal_p (cp_parser *parser)
5747 {
5748 /* Consume the `('. */
5749 cp_lexer_consume_token (parser->lexer);
5750
5751 cp_lexer_save_tokens (parser->lexer);
5752
5753 /* Skip tokens until the next token is a closing parenthesis.
5754 If we find the closing `)', and the next token is a `{', then
5755 we are looking at a compound-literal. */
5756 bool compound_literal_p
5757 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5758 /*consume_paren=*/true)
5759 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5760
5761 /* Roll back the tokens we skipped. */
5762 cp_lexer_rollback_tokens (parser->lexer);
5763
5764 return compound_literal_p;
5765 }
5766
5767 /* Parse a postfix-expression.
5768
5769 postfix-expression:
5770 primary-expression
5771 postfix-expression [ expression ]
5772 postfix-expression ( expression-list [opt] )
5773 simple-type-specifier ( expression-list [opt] )
5774 typename :: [opt] nested-name-specifier identifier
5775 ( expression-list [opt] )
5776 typename :: [opt] nested-name-specifier template [opt] template-id
5777 ( expression-list [opt] )
5778 postfix-expression . template [opt] id-expression
5779 postfix-expression -> template [opt] id-expression
5780 postfix-expression . pseudo-destructor-name
5781 postfix-expression -> pseudo-destructor-name
5782 postfix-expression ++
5783 postfix-expression --
5784 dynamic_cast < type-id > ( expression )
5785 static_cast < type-id > ( expression )
5786 reinterpret_cast < type-id > ( expression )
5787 const_cast < type-id > ( expression )
5788 typeid ( expression )
5789 typeid ( type-id )
5790
5791 GNU Extension:
5792
5793 postfix-expression:
5794 ( type-id ) { initializer-list , [opt] }
5795
5796 This extension is a GNU version of the C99 compound-literal
5797 construct. (The C99 grammar uses `type-name' instead of `type-id',
5798 but they are essentially the same concept.)
5799
5800 If ADDRESS_P is true, the postfix expression is the operand of the
5801 `&' operator. CAST_P is true if this expression is the target of a
5802 cast.
5803
5804 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5805 class member access expressions [expr.ref].
5806
5807 Returns a representation of the expression. */
5808
5809 static tree
5810 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5811 bool member_access_only_p, bool decltype_p,
5812 cp_id_kind * pidk_return)
5813 {
5814 cp_token *token;
5815 location_t loc;
5816 enum rid keyword;
5817 cp_id_kind idk = CP_ID_KIND_NONE;
5818 tree postfix_expression = NULL_TREE;
5819 bool is_member_access = false;
5820 int saved_in_statement = -1;
5821
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5824 loc = token->location;
5825 /* Some of the productions are determined by keywords. */
5826 keyword = token->keyword;
5827 switch (keyword)
5828 {
5829 case RID_DYNCAST:
5830 case RID_STATCAST:
5831 case RID_REINTCAST:
5832 case RID_CONSTCAST:
5833 {
5834 tree type;
5835 tree expression;
5836 const char *saved_message;
5837 bool saved_in_type_id_in_expr_p;
5838
5839 /* All of these can be handled in the same way from the point
5840 of view of parsing. Begin by consuming the token
5841 identifying the cast. */
5842 cp_lexer_consume_token (parser->lexer);
5843
5844 /* New types cannot be defined in the cast. */
5845 saved_message = parser->type_definition_forbidden_message;
5846 parser->type_definition_forbidden_message
5847 = G_("types may not be defined in casts");
5848
5849 /* Look for the opening `<'. */
5850 cp_parser_require (parser, CPP_LESS, RT_LESS);
5851 /* Parse the type to which we are casting. */
5852 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5853 parser->in_type_id_in_expr_p = true;
5854 type = cp_parser_type_id (parser);
5855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5856 /* Look for the closing `>'. */
5857 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5858 /* Restore the old message. */
5859 parser->type_definition_forbidden_message = saved_message;
5860
5861 bool saved_greater_than_is_operator_p
5862 = parser->greater_than_is_operator_p;
5863 parser->greater_than_is_operator_p = true;
5864
5865 /* And the expression which is being cast. */
5866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5867 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5868 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5869
5870 parser->greater_than_is_operator_p
5871 = saved_greater_than_is_operator_p;
5872
5873 /* Only type conversions to integral or enumeration types
5874 can be used in constant-expressions. */
5875 if (!cast_valid_in_integral_constant_expression_p (type)
5876 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5877 return error_mark_node;
5878
5879 switch (keyword)
5880 {
5881 case RID_DYNCAST:
5882 postfix_expression
5883 = build_dynamic_cast (type, expression, tf_warning_or_error);
5884 break;
5885 case RID_STATCAST:
5886 postfix_expression
5887 = build_static_cast (type, expression, tf_warning_or_error);
5888 break;
5889 case RID_REINTCAST:
5890 postfix_expression
5891 = build_reinterpret_cast (type, expression,
5892 tf_warning_or_error);
5893 break;
5894 case RID_CONSTCAST:
5895 postfix_expression
5896 = build_const_cast (type, expression, tf_warning_or_error);
5897 break;
5898 default:
5899 gcc_unreachable ();
5900 }
5901 }
5902 break;
5903
5904 case RID_TYPEID:
5905 {
5906 tree type;
5907 const char *saved_message;
5908 bool saved_in_type_id_in_expr_p;
5909
5910 /* Consume the `typeid' token. */
5911 cp_lexer_consume_token (parser->lexer);
5912 /* Look for the `(' token. */
5913 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5914 /* Types cannot be defined in a `typeid' expression. */
5915 saved_message = parser->type_definition_forbidden_message;
5916 parser->type_definition_forbidden_message
5917 = G_("types may not be defined in a %<typeid%> expression");
5918 /* We can't be sure yet whether we're looking at a type-id or an
5919 expression. */
5920 cp_parser_parse_tentatively (parser);
5921 /* Try a type-id first. */
5922 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5923 parser->in_type_id_in_expr_p = true;
5924 type = cp_parser_type_id (parser);
5925 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5926 /* Look for the `)' token. Otherwise, we can't be sure that
5927 we're not looking at an expression: consider `typeid (int
5928 (3))', for example. */
5929 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5930 /* If all went well, simply lookup the type-id. */
5931 if (cp_parser_parse_definitely (parser))
5932 postfix_expression = get_typeid (type, tf_warning_or_error);
5933 /* Otherwise, fall back to the expression variant. */
5934 else
5935 {
5936 tree expression;
5937
5938 /* Look for an expression. */
5939 expression = cp_parser_expression (parser, & idk);
5940 /* Compute its typeid. */
5941 postfix_expression = build_typeid (expression, tf_warning_or_error);
5942 /* Look for the `)' token. */
5943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5944 }
5945 /* Restore the saved message. */
5946 parser->type_definition_forbidden_message = saved_message;
5947 /* `typeid' may not appear in an integral constant expression. */
5948 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5949 return error_mark_node;
5950 }
5951 break;
5952
5953 case RID_TYPENAME:
5954 {
5955 tree type;
5956 /* The syntax permitted here is the same permitted for an
5957 elaborated-type-specifier. */
5958 type = cp_parser_elaborated_type_specifier (parser,
5959 /*is_friend=*/false,
5960 /*is_declaration=*/false);
5961 postfix_expression = cp_parser_functional_cast (parser, type);
5962 }
5963 break;
5964
5965 case RID_CILK_SPAWN:
5966 {
5967 cp_lexer_consume_token (parser->lexer);
5968 token = cp_lexer_peek_token (parser->lexer);
5969 if (token->type == CPP_SEMICOLON)
5970 {
5971 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5972 "an expression");
5973 postfix_expression = error_mark_node;
5974 break;
5975 }
5976 else if (!current_function_decl)
5977 {
5978 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5979 "inside a function");
5980 postfix_expression = error_mark_node;
5981 break;
5982 }
5983 else
5984 {
5985 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5986 saved_in_statement = parser->in_statement;
5987 parser->in_statement |= IN_CILK_SPAWN;
5988 }
5989 cfun->calls_cilk_spawn = 1;
5990 postfix_expression =
5991 cp_parser_postfix_expression (parser, false, false,
5992 false, false, &idk);
5993 if (!flag_cilkplus)
5994 {
5995 error_at (token->location, "-fcilkplus must be enabled to use"
5996 " %<_Cilk_spawn%>");
5997 cfun->calls_cilk_spawn = 0;
5998 }
5999 else if (saved_in_statement & IN_CILK_SPAWN)
6000 {
6001 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6002 "are not permitted");
6003 postfix_expression = error_mark_node;
6004 cfun->calls_cilk_spawn = 0;
6005 }
6006 else
6007 {
6008 postfix_expression = build_cilk_spawn (token->location,
6009 postfix_expression);
6010 if (postfix_expression != error_mark_node)
6011 SET_EXPR_LOCATION (postfix_expression, input_location);
6012 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6013 }
6014 break;
6015 }
6016
6017 case RID_BUILTIN_SHUFFLE:
6018 {
6019 vec<tree, va_gc> *vec;
6020 unsigned int i;
6021 tree p;
6022
6023 cp_lexer_consume_token (parser->lexer);
6024 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6025 /*cast_p=*/false, /*allow_expansion_p=*/true,
6026 /*non_constant_p=*/NULL);
6027 if (vec == NULL)
6028 return error_mark_node;
6029
6030 FOR_EACH_VEC_ELT (*vec, i, p)
6031 mark_exp_read (p);
6032
6033 if (vec->length () == 2)
6034 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6035 tf_warning_or_error);
6036 else if (vec->length () == 3)
6037 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6038 tf_warning_or_error);
6039 else
6040 {
6041 error_at (loc, "wrong number of arguments to "
6042 "%<__builtin_shuffle%>");
6043 return error_mark_node;
6044 }
6045 break;
6046 }
6047
6048 default:
6049 {
6050 tree type;
6051
6052 /* If the next thing is a simple-type-specifier, we may be
6053 looking at a functional cast. We could also be looking at
6054 an id-expression. So, we try the functional cast, and if
6055 that doesn't work we fall back to the primary-expression. */
6056 cp_parser_parse_tentatively (parser);
6057 /* Look for the simple-type-specifier. */
6058 type = cp_parser_simple_type_specifier (parser,
6059 /*decl_specs=*/NULL,
6060 CP_PARSER_FLAGS_NONE);
6061 /* Parse the cast itself. */
6062 if (!cp_parser_error_occurred (parser))
6063 postfix_expression
6064 = cp_parser_functional_cast (parser, type);
6065 /* If that worked, we're done. */
6066 if (cp_parser_parse_definitely (parser))
6067 break;
6068
6069 /* If the functional-cast didn't work out, try a
6070 compound-literal. */
6071 if (cp_parser_allow_gnu_extensions_p (parser)
6072 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6073 {
6074 tree initializer = NULL_TREE;
6075
6076 cp_parser_parse_tentatively (parser);
6077
6078 /* Avoid calling cp_parser_type_id pointlessly, see comment
6079 in cp_parser_cast_expression about c++/29234. */
6080 if (!cp_parser_compound_literal_p (parser))
6081 cp_parser_simulate_error (parser);
6082 else
6083 {
6084 /* Parse the type. */
6085 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6086 parser->in_type_id_in_expr_p = true;
6087 type = cp_parser_type_id (parser);
6088 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6089 /* Look for the `)'. */
6090 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6091 }
6092
6093 /* If things aren't going well, there's no need to
6094 keep going. */
6095 if (!cp_parser_error_occurred (parser))
6096 {
6097 bool non_constant_p;
6098 /* Parse the brace-enclosed initializer list. */
6099 initializer = cp_parser_braced_list (parser,
6100 &non_constant_p);
6101 }
6102 /* If that worked, we're definitely looking at a
6103 compound-literal expression. */
6104 if (cp_parser_parse_definitely (parser))
6105 {
6106 /* Warn the user that a compound literal is not
6107 allowed in standard C++. */
6108 pedwarn (input_location, OPT_Wpedantic,
6109 "ISO C++ forbids compound-literals");
6110 /* For simplicity, we disallow compound literals in
6111 constant-expressions. We could
6112 allow compound literals of integer type, whose
6113 initializer was a constant, in constant
6114 expressions. Permitting that usage, as a further
6115 extension, would not change the meaning of any
6116 currently accepted programs. (Of course, as
6117 compound literals are not part of ISO C++, the
6118 standard has nothing to say.) */
6119 if (cp_parser_non_integral_constant_expression (parser,
6120 NIC_NCC))
6121 {
6122 postfix_expression = error_mark_node;
6123 break;
6124 }
6125 /* Form the representation of the compound-literal. */
6126 postfix_expression
6127 = finish_compound_literal (type, initializer,
6128 tf_warning_or_error);
6129 break;
6130 }
6131 }
6132
6133 /* It must be a primary-expression. */
6134 postfix_expression
6135 = cp_parser_primary_expression (parser, address_p, cast_p,
6136 /*template_arg_p=*/false,
6137 decltype_p,
6138 &idk);
6139 }
6140 break;
6141 }
6142
6143 /* Note that we don't need to worry about calling build_cplus_new on a
6144 class-valued CALL_EXPR in decltype when it isn't the end of the
6145 postfix-expression; unary_complex_lvalue will take care of that for
6146 all these cases. */
6147
6148 /* Keep looping until the postfix-expression is complete. */
6149 while (true)
6150 {
6151 if (idk == CP_ID_KIND_UNQUALIFIED
6152 && identifier_p (postfix_expression)
6153 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6154 /* It is not a Koenig lookup function call. */
6155 postfix_expression
6156 = unqualified_name_lookup_error (postfix_expression);
6157
6158 /* Peek at the next token. */
6159 token = cp_lexer_peek_token (parser->lexer);
6160
6161 switch (token->type)
6162 {
6163 case CPP_OPEN_SQUARE:
6164 if (cp_next_tokens_can_be_std_attribute_p (parser))
6165 {
6166 cp_parser_error (parser,
6167 "two consecutive %<[%> shall "
6168 "only introduce an attribute");
6169 return error_mark_node;
6170 }
6171 postfix_expression
6172 = cp_parser_postfix_open_square_expression (parser,
6173 postfix_expression,
6174 false,
6175 decltype_p);
6176 idk = CP_ID_KIND_NONE;
6177 is_member_access = false;
6178 break;
6179
6180 case CPP_OPEN_PAREN:
6181 /* postfix-expression ( expression-list [opt] ) */
6182 {
6183 bool koenig_p;
6184 bool is_builtin_constant_p;
6185 bool saved_integral_constant_expression_p = false;
6186 bool saved_non_integral_constant_expression_p = false;
6187 tsubst_flags_t complain = complain_flags (decltype_p);
6188 vec<tree, va_gc> *args;
6189
6190 is_member_access = false;
6191
6192 is_builtin_constant_p
6193 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6194 if (is_builtin_constant_p)
6195 {
6196 /* The whole point of __builtin_constant_p is to allow
6197 non-constant expressions to appear as arguments. */
6198 saved_integral_constant_expression_p
6199 = parser->integral_constant_expression_p;
6200 saved_non_integral_constant_expression_p
6201 = parser->non_integral_constant_expression_p;
6202 parser->integral_constant_expression_p = false;
6203 }
6204 args = (cp_parser_parenthesized_expression_list
6205 (parser, non_attr,
6206 /*cast_p=*/false, /*allow_expansion_p=*/true,
6207 /*non_constant_p=*/NULL,
6208 /*want_literal_zero_p=*/warn_memset_transposed_args));
6209 if (is_builtin_constant_p)
6210 {
6211 parser->integral_constant_expression_p
6212 = saved_integral_constant_expression_p;
6213 parser->non_integral_constant_expression_p
6214 = saved_non_integral_constant_expression_p;
6215 }
6216
6217 if (args == NULL)
6218 {
6219 postfix_expression = error_mark_node;
6220 break;
6221 }
6222
6223 /* Function calls are not permitted in
6224 constant-expressions. */
6225 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6226 && cp_parser_non_integral_constant_expression (parser,
6227 NIC_FUNC_CALL))
6228 {
6229 postfix_expression = error_mark_node;
6230 release_tree_vector (args);
6231 break;
6232 }
6233
6234 koenig_p = false;
6235 if (idk == CP_ID_KIND_UNQUALIFIED
6236 || idk == CP_ID_KIND_TEMPLATE_ID)
6237 {
6238 if (identifier_p (postfix_expression))
6239 {
6240 if (!args->is_empty ())
6241 {
6242 koenig_p = true;
6243 if (!any_type_dependent_arguments_p (args))
6244 postfix_expression
6245 = perform_koenig_lookup (postfix_expression, args,
6246 complain);
6247 }
6248 else
6249 postfix_expression
6250 = unqualified_fn_lookup_error (postfix_expression);
6251 }
6252 /* We do not perform argument-dependent lookup if
6253 normal lookup finds a non-function, in accordance
6254 with the expected resolution of DR 218. */
6255 else if (!args->is_empty ()
6256 && is_overloaded_fn (postfix_expression))
6257 {
6258 tree fn = get_first_fn (postfix_expression);
6259 fn = STRIP_TEMPLATE (fn);
6260
6261 /* Do not do argument dependent lookup if regular
6262 lookup finds a member function or a block-scope
6263 function declaration. [basic.lookup.argdep]/3 */
6264 if (!DECL_FUNCTION_MEMBER_P (fn)
6265 && !DECL_LOCAL_FUNCTION_P (fn))
6266 {
6267 koenig_p = true;
6268 if (!any_type_dependent_arguments_p (args))
6269 postfix_expression
6270 = perform_koenig_lookup (postfix_expression, args,
6271 complain);
6272 }
6273 }
6274 }
6275
6276 if (warn_memset_transposed_args)
6277 {
6278 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6279 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6280 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6281 && vec_safe_length (args) == 3
6282 && integer_zerop ((*args)[2])
6283 && LITERAL_ZERO_P ((*args)[2])
6284 && !(integer_zerop ((*args)[1])
6285 && LITERAL_ZERO_P ((*args)[1])))
6286 warning (OPT_Wmemset_transposed_args,
6287 "%<memset%> used with constant zero length "
6288 "parameter; this could be due to transposed "
6289 "parameters");
6290
6291 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6292 to avoid leaking those into folder and middle-end. */
6293 unsigned int i;
6294 tree arg;
6295 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6296 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6297 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6298 }
6299
6300 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6301 {
6302 tree instance = TREE_OPERAND (postfix_expression, 0);
6303 tree fn = TREE_OPERAND (postfix_expression, 1);
6304
6305 if (processing_template_decl
6306 && (type_dependent_expression_p (instance)
6307 || (!BASELINK_P (fn)
6308 && TREE_CODE (fn) != FIELD_DECL)
6309 || type_dependent_expression_p (fn)
6310 || any_type_dependent_arguments_p (args)))
6311 {
6312 postfix_expression
6313 = build_nt_call_vec (postfix_expression, args);
6314 release_tree_vector (args);
6315 break;
6316 }
6317
6318 if (BASELINK_P (fn))
6319 {
6320 postfix_expression
6321 = (build_new_method_call
6322 (instance, fn, &args, NULL_TREE,
6323 (idk == CP_ID_KIND_QUALIFIED
6324 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6325 : LOOKUP_NORMAL),
6326 /*fn_p=*/NULL,
6327 complain));
6328 }
6329 else
6330 postfix_expression
6331 = finish_call_expr (postfix_expression, &args,
6332 /*disallow_virtual=*/false,
6333 /*koenig_p=*/false,
6334 complain);
6335 }
6336 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6337 || TREE_CODE (postfix_expression) == MEMBER_REF
6338 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6339 postfix_expression = (build_offset_ref_call_from_tree
6340 (postfix_expression, &args,
6341 complain));
6342 else if (idk == CP_ID_KIND_QUALIFIED)
6343 /* A call to a static class member, or a namespace-scope
6344 function. */
6345 postfix_expression
6346 = finish_call_expr (postfix_expression, &args,
6347 /*disallow_virtual=*/true,
6348 koenig_p,
6349 complain);
6350 else
6351 /* All other function calls. */
6352 postfix_expression
6353 = finish_call_expr (postfix_expression, &args,
6354 /*disallow_virtual=*/false,
6355 koenig_p,
6356 complain);
6357
6358 protected_set_expr_location (postfix_expression, token->location);
6359
6360 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6361 idk = CP_ID_KIND_NONE;
6362
6363 release_tree_vector (args);
6364 }
6365 break;
6366
6367 case CPP_DOT:
6368 case CPP_DEREF:
6369 /* postfix-expression . template [opt] id-expression
6370 postfix-expression . pseudo-destructor-name
6371 postfix-expression -> template [opt] id-expression
6372 postfix-expression -> pseudo-destructor-name */
6373
6374 /* Consume the `.' or `->' operator. */
6375 cp_lexer_consume_token (parser->lexer);
6376
6377 postfix_expression
6378 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6379 postfix_expression,
6380 false, &idk, loc);
6381
6382 is_member_access = true;
6383 break;
6384
6385 case CPP_PLUS_PLUS:
6386 /* postfix-expression ++ */
6387 /* Consume the `++' token. */
6388 cp_lexer_consume_token (parser->lexer);
6389 /* Generate a representation for the complete expression. */
6390 postfix_expression
6391 = finish_increment_expr (postfix_expression,
6392 POSTINCREMENT_EXPR);
6393 /* Increments may not appear in constant-expressions. */
6394 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6395 postfix_expression = error_mark_node;
6396 idk = CP_ID_KIND_NONE;
6397 is_member_access = false;
6398 break;
6399
6400 case CPP_MINUS_MINUS:
6401 /* postfix-expression -- */
6402 /* Consume the `--' token. */
6403 cp_lexer_consume_token (parser->lexer);
6404 /* Generate a representation for the complete expression. */
6405 postfix_expression
6406 = finish_increment_expr (postfix_expression,
6407 POSTDECREMENT_EXPR);
6408 /* Decrements may not appear in constant-expressions. */
6409 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6410 postfix_expression = error_mark_node;
6411 idk = CP_ID_KIND_NONE;
6412 is_member_access = false;
6413 break;
6414
6415 default:
6416 if (pidk_return != NULL)
6417 * pidk_return = idk;
6418 if (member_access_only_p)
6419 return is_member_access? postfix_expression : error_mark_node;
6420 else
6421 return postfix_expression;
6422 }
6423 }
6424
6425 /* We should never get here. */
6426 gcc_unreachable ();
6427 return error_mark_node;
6428 }
6429
6430 /* This function parses Cilk Plus array notations. If a normal array expr. is
6431 parsed then the array index is passed back to the caller through *INIT_INDEX
6432 and the function returns a NULL_TREE. If array notation expr. is parsed,
6433 then *INIT_INDEX is ignored by the caller and the function returns
6434 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6435 error_mark_node. */
6436
6437 static tree
6438 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6439 tree array_value)
6440 {
6441 cp_token *token = NULL;
6442 tree length_index, stride = NULL_TREE, value_tree, array_type;
6443 if (!array_value || array_value == error_mark_node)
6444 {
6445 cp_parser_skip_to_end_of_statement (parser);
6446 return error_mark_node;
6447 }
6448
6449 array_type = TREE_TYPE (array_value);
6450
6451 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6452 parser->colon_corrects_to_scope_p = false;
6453 token = cp_lexer_peek_token (parser->lexer);
6454
6455 if (!token)
6456 {
6457 cp_parser_error (parser, "expected %<:%> or numeral");
6458 return error_mark_node;
6459 }
6460 else if (token->type == CPP_COLON)
6461 {
6462 /* Consume the ':'. */
6463 cp_lexer_consume_token (parser->lexer);
6464
6465 /* If we are here, then we have a case like this A[:]. */
6466 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6467 {
6468 cp_parser_error (parser, "expected %<]%>");
6469 cp_parser_skip_to_end_of_statement (parser);
6470 return error_mark_node;
6471 }
6472 *init_index = NULL_TREE;
6473 stride = NULL_TREE;
6474 length_index = NULL_TREE;
6475 }
6476 else
6477 {
6478 /* If we are here, then there are three valid possibilities:
6479 1. ARRAY [ EXP ]
6480 2. ARRAY [ EXP : EXP ]
6481 3. ARRAY [ EXP : EXP : EXP ] */
6482
6483 *init_index = cp_parser_expression (parser);
6484 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6485 {
6486 /* This indicates that we have a normal array expression. */
6487 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6488 return NULL_TREE;
6489 }
6490
6491 /* Consume the ':'. */
6492 cp_lexer_consume_token (parser->lexer);
6493 length_index = cp_parser_expression (parser);
6494 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6495 {
6496 cp_lexer_consume_token (parser->lexer);
6497 stride = cp_parser_expression (parser);
6498 }
6499 }
6500 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6501
6502 if (*init_index == error_mark_node || length_index == error_mark_node
6503 || stride == error_mark_node || array_type == error_mark_node)
6504 {
6505 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6506 cp_lexer_consume_token (parser->lexer);
6507 return error_mark_node;
6508 }
6509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6510
6511 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6512 length_index, stride, array_type);
6513 return value_tree;
6514 }
6515
6516 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6517 by cp_parser_builtin_offsetof. We're looking for
6518
6519 postfix-expression [ expression ]
6520 postfix-expression [ braced-init-list ] (C++11)
6521
6522 FOR_OFFSETOF is set if we're being called in that context, which
6523 changes how we deal with integer constant expressions. */
6524
6525 static tree
6526 cp_parser_postfix_open_square_expression (cp_parser *parser,
6527 tree postfix_expression,
6528 bool for_offsetof,
6529 bool decltype_p)
6530 {
6531 tree index = NULL_TREE;
6532 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6533 bool saved_greater_than_is_operator_p;
6534
6535 /* Consume the `[' token. */
6536 cp_lexer_consume_token (parser->lexer);
6537
6538 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6539 parser->greater_than_is_operator_p = true;
6540
6541 /* Parse the index expression. */
6542 /* ??? For offsetof, there is a question of what to allow here. If
6543 offsetof is not being used in an integral constant expression context,
6544 then we *could* get the right answer by computing the value at runtime.
6545 If we are in an integral constant expression context, then we might
6546 could accept any constant expression; hard to say without analysis.
6547 Rather than open the barn door too wide right away, allow only integer
6548 constant expressions here. */
6549 if (for_offsetof)
6550 index = cp_parser_constant_expression (parser);
6551 else
6552 {
6553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6554 {
6555 bool expr_nonconst_p;
6556 cp_lexer_set_source_position (parser->lexer);
6557 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6558 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6559 if (flag_cilkplus
6560 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6561 {
6562 error_at (cp_lexer_peek_token (parser->lexer)->location,
6563 "braced list index is not allowed with array "
6564 "notation");
6565 cp_parser_skip_to_end_of_statement (parser);
6566 return error_mark_node;
6567 }
6568 }
6569 else if (flag_cilkplus)
6570 {
6571 /* Here are have these two options:
6572 ARRAY[EXP : EXP] - Array notation expr with default
6573 stride of 1.
6574 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6575 stride. */
6576 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6577 postfix_expression);
6578 if (an_exp)
6579 return an_exp;
6580 }
6581 else
6582 index = cp_parser_expression (parser);
6583 }
6584
6585 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6586
6587 /* Look for the closing `]'. */
6588 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6589
6590 /* Build the ARRAY_REF. */
6591 postfix_expression = grok_array_decl (loc, postfix_expression,
6592 index, decltype_p);
6593
6594 /* When not doing offsetof, array references are not permitted in
6595 constant-expressions. */
6596 if (!for_offsetof
6597 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6598 postfix_expression = error_mark_node;
6599
6600 return postfix_expression;
6601 }
6602
6603 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6604 by cp_parser_builtin_offsetof. We're looking for
6605
6606 postfix-expression . template [opt] id-expression
6607 postfix-expression . pseudo-destructor-name
6608 postfix-expression -> template [opt] id-expression
6609 postfix-expression -> pseudo-destructor-name
6610
6611 FOR_OFFSETOF is set if we're being called in that context. That sorta
6612 limits what of the above we'll actually accept, but nevermind.
6613 TOKEN_TYPE is the "." or "->" token, which will already have been
6614 removed from the stream. */
6615
6616 static tree
6617 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6618 enum cpp_ttype token_type,
6619 tree postfix_expression,
6620 bool for_offsetof, cp_id_kind *idk,
6621 location_t location)
6622 {
6623 tree name;
6624 bool dependent_p;
6625 bool pseudo_destructor_p;
6626 tree scope = NULL_TREE;
6627
6628 /* If this is a `->' operator, dereference the pointer. */
6629 if (token_type == CPP_DEREF)
6630 postfix_expression = build_x_arrow (location, postfix_expression,
6631 tf_warning_or_error);
6632 /* Check to see whether or not the expression is type-dependent. */
6633 dependent_p = type_dependent_expression_p (postfix_expression);
6634 /* The identifier following the `->' or `.' is not qualified. */
6635 parser->scope = NULL_TREE;
6636 parser->qualifying_scope = NULL_TREE;
6637 parser->object_scope = NULL_TREE;
6638 *idk = CP_ID_KIND_NONE;
6639
6640 /* Enter the scope corresponding to the type of the object
6641 given by the POSTFIX_EXPRESSION. */
6642 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6643 {
6644 scope = TREE_TYPE (postfix_expression);
6645 /* According to the standard, no expression should ever have
6646 reference type. Unfortunately, we do not currently match
6647 the standard in this respect in that our internal representation
6648 of an expression may have reference type even when the standard
6649 says it does not. Therefore, we have to manually obtain the
6650 underlying type here. */
6651 scope = non_reference (scope);
6652 /* The type of the POSTFIX_EXPRESSION must be complete. */
6653 if (scope == unknown_type_node)
6654 {
6655 error_at (location, "%qE does not have class type",
6656 postfix_expression);
6657 scope = NULL_TREE;
6658 }
6659 /* Unlike the object expression in other contexts, *this is not
6660 required to be of complete type for purposes of class member
6661 access (5.2.5) outside the member function body. */
6662 else if (postfix_expression != current_class_ref
6663 && !(processing_template_decl && scope == current_class_type))
6664 scope = complete_type_or_else (scope, NULL_TREE);
6665 /* Let the name lookup machinery know that we are processing a
6666 class member access expression. */
6667 parser->context->object_type = scope;
6668 /* If something went wrong, we want to be able to discern that case,
6669 as opposed to the case where there was no SCOPE due to the type
6670 of expression being dependent. */
6671 if (!scope)
6672 scope = error_mark_node;
6673 /* If the SCOPE was erroneous, make the various semantic analysis
6674 functions exit quickly -- and without issuing additional error
6675 messages. */
6676 if (scope == error_mark_node)
6677 postfix_expression = error_mark_node;
6678 }
6679
6680 /* Assume this expression is not a pseudo-destructor access. */
6681 pseudo_destructor_p = false;
6682
6683 /* If the SCOPE is a scalar type, then, if this is a valid program,
6684 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6685 is type dependent, it can be pseudo-destructor-name or something else.
6686 Try to parse it as pseudo-destructor-name first. */
6687 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6688 {
6689 tree s;
6690 tree type;
6691
6692 cp_parser_parse_tentatively (parser);
6693 /* Parse the pseudo-destructor-name. */
6694 s = NULL_TREE;
6695 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6696 &s, &type);
6697 if (dependent_p
6698 && (cp_parser_error_occurred (parser)
6699 || !SCALAR_TYPE_P (type)))
6700 cp_parser_abort_tentative_parse (parser);
6701 else if (cp_parser_parse_definitely (parser))
6702 {
6703 pseudo_destructor_p = true;
6704 postfix_expression
6705 = finish_pseudo_destructor_expr (postfix_expression,
6706 s, type, location);
6707 }
6708 }
6709
6710 if (!pseudo_destructor_p)
6711 {
6712 /* If the SCOPE is not a scalar type, we are looking at an
6713 ordinary class member access expression, rather than a
6714 pseudo-destructor-name. */
6715 bool template_p;
6716 cp_token *token = cp_lexer_peek_token (parser->lexer);
6717 /* Parse the id-expression. */
6718 name = (cp_parser_id_expression
6719 (parser,
6720 cp_parser_optional_template_keyword (parser),
6721 /*check_dependency_p=*/true,
6722 &template_p,
6723 /*declarator_p=*/false,
6724 /*optional_p=*/false));
6725 /* In general, build a SCOPE_REF if the member name is qualified.
6726 However, if the name was not dependent and has already been
6727 resolved; there is no need to build the SCOPE_REF. For example;
6728
6729 struct X { void f(); };
6730 template <typename T> void f(T* t) { t->X::f(); }
6731
6732 Even though "t" is dependent, "X::f" is not and has been resolved
6733 to a BASELINK; there is no need to include scope information. */
6734
6735 /* But we do need to remember that there was an explicit scope for
6736 virtual function calls. */
6737 if (parser->scope)
6738 *idk = CP_ID_KIND_QUALIFIED;
6739
6740 /* If the name is a template-id that names a type, we will get a
6741 TYPE_DECL here. That is invalid code. */
6742 if (TREE_CODE (name) == TYPE_DECL)
6743 {
6744 error_at (token->location, "invalid use of %qD", name);
6745 postfix_expression = error_mark_node;
6746 }
6747 else
6748 {
6749 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6750 {
6751 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6752 {
6753 error_at (token->location, "%<%D::%D%> is not a class member",
6754 parser->scope, name);
6755 postfix_expression = error_mark_node;
6756 }
6757 else
6758 name = build_qualified_name (/*type=*/NULL_TREE,
6759 parser->scope,
6760 name,
6761 template_p);
6762 parser->scope = NULL_TREE;
6763 parser->qualifying_scope = NULL_TREE;
6764 parser->object_scope = NULL_TREE;
6765 }
6766 if (parser->scope && name && BASELINK_P (name))
6767 adjust_result_of_qualified_name_lookup
6768 (name, parser->scope, scope);
6769 postfix_expression
6770 = finish_class_member_access_expr (postfix_expression, name,
6771 template_p,
6772 tf_warning_or_error);
6773 }
6774 }
6775
6776 /* We no longer need to look up names in the scope of the object on
6777 the left-hand side of the `.' or `->' operator. */
6778 parser->context->object_type = NULL_TREE;
6779
6780 /* Outside of offsetof, these operators may not appear in
6781 constant-expressions. */
6782 if (!for_offsetof
6783 && (cp_parser_non_integral_constant_expression
6784 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6785 postfix_expression = error_mark_node;
6786
6787 return postfix_expression;
6788 }
6789
6790 /* Cache of LITERAL_ZERO_P constants. */
6791
6792 static GTY(()) tree literal_zeros[itk_none];
6793
6794 /* Parse a parenthesized expression-list.
6795
6796 expression-list:
6797 assignment-expression
6798 expression-list, assignment-expression
6799
6800 attribute-list:
6801 expression-list
6802 identifier
6803 identifier, expression-list
6804
6805 CAST_P is true if this expression is the target of a cast.
6806
6807 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6808 argument pack.
6809
6810 Returns a vector of trees. Each element is a representation of an
6811 assignment-expression. NULL is returned if the ( and or ) are
6812 missing. An empty, but allocated, vector is returned on no
6813 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6814 if we are parsing an attribute list for an attribute that wants a
6815 plain identifier argument, normal_attr for an attribute that wants
6816 an expression, or non_attr if we aren't parsing an attribute list. If
6817 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6818 not all of the expressions in the list were constant.
6819 WANT_LITERAL_ZERO_P is true if the caller is interested in
6820 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6821 immediately, this can be removed. */
6822
6823 static vec<tree, va_gc> *
6824 cp_parser_parenthesized_expression_list (cp_parser* parser,
6825 int is_attribute_list,
6826 bool cast_p,
6827 bool allow_expansion_p,
6828 bool *non_constant_p,
6829 bool want_literal_zero_p)
6830 {
6831 vec<tree, va_gc> *expression_list;
6832 bool fold_expr_p = is_attribute_list != non_attr;
6833 tree identifier = NULL_TREE;
6834 bool saved_greater_than_is_operator_p;
6835
6836 /* Assume all the expressions will be constant. */
6837 if (non_constant_p)
6838 *non_constant_p = false;
6839
6840 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6841 return NULL;
6842
6843 expression_list = make_tree_vector ();
6844
6845 /* Within a parenthesized expression, a `>' token is always
6846 the greater-than operator. */
6847 saved_greater_than_is_operator_p
6848 = parser->greater_than_is_operator_p;
6849 parser->greater_than_is_operator_p = true;
6850
6851 /* Consume expressions until there are no more. */
6852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6853 while (true)
6854 {
6855 tree expr;
6856
6857 /* At the beginning of attribute lists, check to see if the
6858 next token is an identifier. */
6859 if (is_attribute_list == id_attr
6860 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6861 {
6862 cp_token *token;
6863
6864 /* Consume the identifier. */
6865 token = cp_lexer_consume_token (parser->lexer);
6866 /* Save the identifier. */
6867 identifier = token->u.value;
6868 }
6869 else
6870 {
6871 bool expr_non_constant_p;
6872
6873 /* Parse the next assignment-expression. */
6874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6875 {
6876 /* A braced-init-list. */
6877 cp_lexer_set_source_position (parser->lexer);
6878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6879 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6880 if (non_constant_p && expr_non_constant_p)
6881 *non_constant_p = true;
6882 }
6883 else if (non_constant_p)
6884 {
6885 expr = (cp_parser_constant_expression
6886 (parser, /*allow_non_constant_p=*/true,
6887 &expr_non_constant_p));
6888 if (expr_non_constant_p)
6889 *non_constant_p = true;
6890 }
6891 else
6892 {
6893 expr = NULL_TREE;
6894 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6895 switch (tok->type)
6896 {
6897 case CPP_NUMBER:
6898 case CPP_CHAR:
6899 case CPP_WCHAR:
6900 case CPP_CHAR16:
6901 case CPP_CHAR32:
6902 /* If a parameter is literal zero alone, remember it
6903 for -Wmemset-transposed-args warning. */
6904 if (integer_zerop (tok->u.value)
6905 && !TREE_OVERFLOW (tok->u.value)
6906 && want_literal_zero_p
6907 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6908 == CPP_COMMA
6909 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6910 == CPP_CLOSE_PAREN))
6911 {
6912 unsigned int i;
6913 for (i = 0; i < itk_none; ++i)
6914 if (TREE_TYPE (tok->u.value) == integer_types[i])
6915 break;
6916 if (i < itk_none && literal_zeros[i])
6917 expr = literal_zeros[i];
6918 else
6919 {
6920 expr = copy_node (tok->u.value);
6921 LITERAL_ZERO_P (expr) = 1;
6922 if (i < itk_none)
6923 literal_zeros[i] = expr;
6924 }
6925 /* Consume the 0 token (or '\0', 0LL etc.). */
6926 cp_lexer_consume_token (parser->lexer);
6927 }
6928 break;
6929 default:
6930 break;
6931 }
6932 if (expr == NULL_TREE)
6933 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6934 cast_p);
6935 }
6936
6937 if (fold_expr_p)
6938 expr = instantiate_non_dependent_expr (expr);
6939
6940 /* If we have an ellipsis, then this is an expression
6941 expansion. */
6942 if (allow_expansion_p
6943 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6944 {
6945 /* Consume the `...'. */
6946 cp_lexer_consume_token (parser->lexer);
6947
6948 /* Build the argument pack. */
6949 expr = make_pack_expansion (expr);
6950 }
6951
6952 /* Add it to the list. We add error_mark_node
6953 expressions to the list, so that we can still tell if
6954 the correct form for a parenthesized expression-list
6955 is found. That gives better errors. */
6956 vec_safe_push (expression_list, expr);
6957
6958 if (expr == error_mark_node)
6959 goto skip_comma;
6960 }
6961
6962 /* After the first item, attribute lists look the same as
6963 expression lists. */
6964 is_attribute_list = non_attr;
6965
6966 get_comma:;
6967 /* If the next token isn't a `,', then we are done. */
6968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6969 break;
6970
6971 /* Otherwise, consume the `,' and keep going. */
6972 cp_lexer_consume_token (parser->lexer);
6973 }
6974
6975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6976 {
6977 int ending;
6978
6979 skip_comma:;
6980 /* We try and resync to an unnested comma, as that will give the
6981 user better diagnostics. */
6982 ending = cp_parser_skip_to_closing_parenthesis (parser,
6983 /*recovering=*/true,
6984 /*or_comma=*/true,
6985 /*consume_paren=*/true);
6986 if (ending < 0)
6987 goto get_comma;
6988 if (!ending)
6989 {
6990 parser->greater_than_is_operator_p
6991 = saved_greater_than_is_operator_p;
6992 return NULL;
6993 }
6994 }
6995
6996 parser->greater_than_is_operator_p
6997 = saved_greater_than_is_operator_p;
6998
6999 if (identifier)
7000 vec_safe_insert (expression_list, 0, identifier);
7001
7002 return expression_list;
7003 }
7004
7005 /* Parse a pseudo-destructor-name.
7006
7007 pseudo-destructor-name:
7008 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7009 :: [opt] nested-name-specifier template template-id :: ~ type-name
7010 :: [opt] nested-name-specifier [opt] ~ type-name
7011
7012 If either of the first two productions is used, sets *SCOPE to the
7013 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7014 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7015 or ERROR_MARK_NODE if the parse fails. */
7016
7017 static void
7018 cp_parser_pseudo_destructor_name (cp_parser* parser,
7019 tree object,
7020 tree* scope,
7021 tree* type)
7022 {
7023 bool nested_name_specifier_p;
7024
7025 /* Handle ~auto. */
7026 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7027 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7028 && !type_dependent_expression_p (object))
7029 {
7030 if (cxx_dialect < cxx14)
7031 pedwarn (input_location, 0,
7032 "%<~auto%> only available with "
7033 "-std=c++14 or -std=gnu++14");
7034 cp_lexer_consume_token (parser->lexer);
7035 cp_lexer_consume_token (parser->lexer);
7036 *scope = NULL_TREE;
7037 *type = TREE_TYPE (object);
7038 return;
7039 }
7040
7041 /* Assume that things will not work out. */
7042 *type = error_mark_node;
7043
7044 /* Look for the optional `::' operator. */
7045 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7046 /* Look for the optional nested-name-specifier. */
7047 nested_name_specifier_p
7048 = (cp_parser_nested_name_specifier_opt (parser,
7049 /*typename_keyword_p=*/false,
7050 /*check_dependency_p=*/true,
7051 /*type_p=*/false,
7052 /*is_declaration=*/false)
7053 != NULL_TREE);
7054 /* Now, if we saw a nested-name-specifier, we might be doing the
7055 second production. */
7056 if (nested_name_specifier_p
7057 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7058 {
7059 /* Consume the `template' keyword. */
7060 cp_lexer_consume_token (parser->lexer);
7061 /* Parse the template-id. */
7062 cp_parser_template_id (parser,
7063 /*template_keyword_p=*/true,
7064 /*check_dependency_p=*/false,
7065 class_type,
7066 /*is_declaration=*/true);
7067 /* Look for the `::' token. */
7068 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7069 }
7070 /* If the next token is not a `~', then there might be some
7071 additional qualification. */
7072 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7073 {
7074 /* At this point, we're looking for "type-name :: ~". The type-name
7075 must not be a class-name, since this is a pseudo-destructor. So,
7076 it must be either an enum-name, or a typedef-name -- both of which
7077 are just identifiers. So, we peek ahead to check that the "::"
7078 and "~" tokens are present; if they are not, then we can avoid
7079 calling type_name. */
7080 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7081 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7082 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7083 {
7084 cp_parser_error (parser, "non-scalar type");
7085 return;
7086 }
7087
7088 /* Look for the type-name. */
7089 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7090 if (*scope == error_mark_node)
7091 return;
7092
7093 /* Look for the `::' token. */
7094 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7095 }
7096 else
7097 *scope = NULL_TREE;
7098
7099 /* Look for the `~'. */
7100 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7101
7102 /* Once we see the ~, this has to be a pseudo-destructor. */
7103 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7104 cp_parser_commit_to_topmost_tentative_parse (parser);
7105
7106 /* Look for the type-name again. We are not responsible for
7107 checking that it matches the first type-name. */
7108 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7109 }
7110
7111 /* Parse a unary-expression.
7112
7113 unary-expression:
7114 postfix-expression
7115 ++ cast-expression
7116 -- cast-expression
7117 unary-operator cast-expression
7118 sizeof unary-expression
7119 sizeof ( type-id )
7120 alignof ( type-id ) [C++0x]
7121 new-expression
7122 delete-expression
7123
7124 GNU Extensions:
7125
7126 unary-expression:
7127 __extension__ cast-expression
7128 __alignof__ unary-expression
7129 __alignof__ ( type-id )
7130 alignof unary-expression [C++0x]
7131 __real__ cast-expression
7132 __imag__ cast-expression
7133 && identifier
7134 sizeof ( type-id ) { initializer-list , [opt] }
7135 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7136 __alignof__ ( type-id ) { initializer-list , [opt] }
7137
7138 ADDRESS_P is true iff the unary-expression is appearing as the
7139 operand of the `&' operator. CAST_P is true if this expression is
7140 the target of a cast.
7141
7142 Returns a representation of the expression. */
7143
7144 static tree
7145 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7146 bool address_p, bool cast_p, bool decltype_p)
7147 {
7148 cp_token *token;
7149 enum tree_code unary_operator;
7150
7151 /* Peek at the next token. */
7152 token = cp_lexer_peek_token (parser->lexer);
7153 /* Some keywords give away the kind of expression. */
7154 if (token->type == CPP_KEYWORD)
7155 {
7156 enum rid keyword = token->keyword;
7157
7158 switch (keyword)
7159 {
7160 case RID_ALIGNOF:
7161 case RID_SIZEOF:
7162 {
7163 tree operand, ret;
7164 enum tree_code op;
7165 location_t first_loc;
7166
7167 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7168 /* Consume the token. */
7169 cp_lexer_consume_token (parser->lexer);
7170 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7171 /* Parse the operand. */
7172 operand = cp_parser_sizeof_operand (parser, keyword);
7173
7174 if (TYPE_P (operand))
7175 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7176 else
7177 {
7178 /* ISO C++ defines alignof only with types, not with
7179 expressions. So pedwarn if alignof is used with a non-
7180 type expression. However, __alignof__ is ok. */
7181 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7182 pedwarn (token->location, OPT_Wpedantic,
7183 "ISO C++ does not allow %<alignof%> "
7184 "with a non-type");
7185
7186 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7187 }
7188 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7189 SIZEOF_EXPR with the original operand. */
7190 if (op == SIZEOF_EXPR && ret != error_mark_node)
7191 {
7192 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7193 {
7194 if (!processing_template_decl && TYPE_P (operand))
7195 {
7196 ret = build_min (SIZEOF_EXPR, size_type_node,
7197 build1 (NOP_EXPR, operand,
7198 error_mark_node));
7199 SIZEOF_EXPR_TYPE_P (ret) = 1;
7200 }
7201 else
7202 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7203 TREE_SIDE_EFFECTS (ret) = 0;
7204 TREE_READONLY (ret) = 1;
7205 }
7206 SET_EXPR_LOCATION (ret, first_loc);
7207 }
7208 return ret;
7209 }
7210
7211 case RID_NEW:
7212 return cp_parser_new_expression (parser);
7213
7214 case RID_DELETE:
7215 return cp_parser_delete_expression (parser);
7216
7217 case RID_EXTENSION:
7218 {
7219 /* The saved value of the PEDANTIC flag. */
7220 int saved_pedantic;
7221 tree expr;
7222
7223 /* Save away the PEDANTIC flag. */
7224 cp_parser_extension_opt (parser, &saved_pedantic);
7225 /* Parse the cast-expression. */
7226 expr = cp_parser_simple_cast_expression (parser);
7227 /* Restore the PEDANTIC flag. */
7228 pedantic = saved_pedantic;
7229
7230 return expr;
7231 }
7232
7233 case RID_REALPART:
7234 case RID_IMAGPART:
7235 {
7236 tree expression;
7237
7238 /* Consume the `__real__' or `__imag__' token. */
7239 cp_lexer_consume_token (parser->lexer);
7240 /* Parse the cast-expression. */
7241 expression = cp_parser_simple_cast_expression (parser);
7242 /* Create the complete representation. */
7243 return build_x_unary_op (token->location,
7244 (keyword == RID_REALPART
7245 ? REALPART_EXPR : IMAGPART_EXPR),
7246 expression,
7247 tf_warning_or_error);
7248 }
7249 break;
7250
7251 case RID_TRANSACTION_ATOMIC:
7252 case RID_TRANSACTION_RELAXED:
7253 return cp_parser_transaction_expression (parser, keyword);
7254
7255 case RID_NOEXCEPT:
7256 {
7257 tree expr;
7258 const char *saved_message;
7259 bool saved_integral_constant_expression_p;
7260 bool saved_non_integral_constant_expression_p;
7261 bool saved_greater_than_is_operator_p;
7262
7263 cp_lexer_consume_token (parser->lexer);
7264 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7265
7266 saved_message = parser->type_definition_forbidden_message;
7267 parser->type_definition_forbidden_message
7268 = G_("types may not be defined in %<noexcept%> expressions");
7269
7270 saved_integral_constant_expression_p
7271 = parser->integral_constant_expression_p;
7272 saved_non_integral_constant_expression_p
7273 = parser->non_integral_constant_expression_p;
7274 parser->integral_constant_expression_p = false;
7275
7276 saved_greater_than_is_operator_p
7277 = parser->greater_than_is_operator_p;
7278 parser->greater_than_is_operator_p = true;
7279
7280 ++cp_unevaluated_operand;
7281 ++c_inhibit_evaluation_warnings;
7282 ++cp_noexcept_operand;
7283 expr = cp_parser_expression (parser);
7284 --cp_noexcept_operand;
7285 --c_inhibit_evaluation_warnings;
7286 --cp_unevaluated_operand;
7287
7288 parser->greater_than_is_operator_p
7289 = saved_greater_than_is_operator_p;
7290
7291 parser->integral_constant_expression_p
7292 = saved_integral_constant_expression_p;
7293 parser->non_integral_constant_expression_p
7294 = saved_non_integral_constant_expression_p;
7295
7296 parser->type_definition_forbidden_message = saved_message;
7297
7298 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7299 return finish_noexcept_expr (expr, tf_warning_or_error);
7300 }
7301
7302 default:
7303 break;
7304 }
7305 }
7306
7307 /* Look for the `:: new' and `:: delete', which also signal the
7308 beginning of a new-expression, or delete-expression,
7309 respectively. If the next token is `::', then it might be one of
7310 these. */
7311 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7312 {
7313 enum rid keyword;
7314
7315 /* See if the token after the `::' is one of the keywords in
7316 which we're interested. */
7317 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7318 /* If it's `new', we have a new-expression. */
7319 if (keyword == RID_NEW)
7320 return cp_parser_new_expression (parser);
7321 /* Similarly, for `delete'. */
7322 else if (keyword == RID_DELETE)
7323 return cp_parser_delete_expression (parser);
7324 }
7325
7326 /* Look for a unary operator. */
7327 unary_operator = cp_parser_unary_operator (token);
7328 /* The `++' and `--' operators can be handled similarly, even though
7329 they are not technically unary-operators in the grammar. */
7330 if (unary_operator == ERROR_MARK)
7331 {
7332 if (token->type == CPP_PLUS_PLUS)
7333 unary_operator = PREINCREMENT_EXPR;
7334 else if (token->type == CPP_MINUS_MINUS)
7335 unary_operator = PREDECREMENT_EXPR;
7336 /* Handle the GNU address-of-label extension. */
7337 else if (cp_parser_allow_gnu_extensions_p (parser)
7338 && token->type == CPP_AND_AND)
7339 {
7340 tree identifier;
7341 tree expression;
7342 location_t loc = token->location;
7343
7344 /* Consume the '&&' token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 /* Look for the identifier. */
7347 identifier = cp_parser_identifier (parser);
7348 /* Create an expression representing the address. */
7349 expression = finish_label_address_expr (identifier, loc);
7350 if (cp_parser_non_integral_constant_expression (parser,
7351 NIC_ADDR_LABEL))
7352 expression = error_mark_node;
7353 return expression;
7354 }
7355 }
7356 if (unary_operator != ERROR_MARK)
7357 {
7358 tree cast_expression;
7359 tree expression = error_mark_node;
7360 non_integral_constant non_constant_p = NIC_NONE;
7361 location_t loc = token->location;
7362 tsubst_flags_t complain = complain_flags (decltype_p);
7363
7364 /* Consume the operator token. */
7365 token = cp_lexer_consume_token (parser->lexer);
7366 /* Parse the cast-expression. */
7367 cast_expression
7368 = cp_parser_cast_expression (parser,
7369 unary_operator == ADDR_EXPR,
7370 /*cast_p=*/false,
7371 /*decltype*/false,
7372 pidk);
7373 /* Now, build an appropriate representation. */
7374 switch (unary_operator)
7375 {
7376 case INDIRECT_REF:
7377 non_constant_p = NIC_STAR;
7378 expression = build_x_indirect_ref (loc, cast_expression,
7379 RO_UNARY_STAR,
7380 complain);
7381 break;
7382
7383 case ADDR_EXPR:
7384 non_constant_p = NIC_ADDR;
7385 /* Fall through. */
7386 case BIT_NOT_EXPR:
7387 expression = build_x_unary_op (loc, unary_operator,
7388 cast_expression,
7389 complain);
7390 break;
7391
7392 case PREINCREMENT_EXPR:
7393 case PREDECREMENT_EXPR:
7394 non_constant_p = unary_operator == PREINCREMENT_EXPR
7395 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7396 /* Fall through. */
7397 case UNARY_PLUS_EXPR:
7398 case NEGATE_EXPR:
7399 case TRUTH_NOT_EXPR:
7400 expression = finish_unary_op_expr (loc, unary_operator,
7401 cast_expression, complain);
7402 break;
7403
7404 default:
7405 gcc_unreachable ();
7406 }
7407
7408 if (non_constant_p != NIC_NONE
7409 && cp_parser_non_integral_constant_expression (parser,
7410 non_constant_p))
7411 expression = error_mark_node;
7412
7413 return expression;
7414 }
7415
7416 return cp_parser_postfix_expression (parser, address_p, cast_p,
7417 /*member_access_only_p=*/false,
7418 decltype_p,
7419 pidk);
7420 }
7421
7422 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7423 unary-operator, the corresponding tree code is returned. */
7424
7425 static enum tree_code
7426 cp_parser_unary_operator (cp_token* token)
7427 {
7428 switch (token->type)
7429 {
7430 case CPP_MULT:
7431 return INDIRECT_REF;
7432
7433 case CPP_AND:
7434 return ADDR_EXPR;
7435
7436 case CPP_PLUS:
7437 return UNARY_PLUS_EXPR;
7438
7439 case CPP_MINUS:
7440 return NEGATE_EXPR;
7441
7442 case CPP_NOT:
7443 return TRUTH_NOT_EXPR;
7444
7445 case CPP_COMPL:
7446 return BIT_NOT_EXPR;
7447
7448 default:
7449 return ERROR_MARK;
7450 }
7451 }
7452
7453 /* Parse a new-expression.
7454
7455 new-expression:
7456 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7457 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7458
7459 Returns a representation of the expression. */
7460
7461 static tree
7462 cp_parser_new_expression (cp_parser* parser)
7463 {
7464 bool global_scope_p;
7465 vec<tree, va_gc> *placement;
7466 tree type;
7467 vec<tree, va_gc> *initializer;
7468 tree nelts = NULL_TREE;
7469 tree ret;
7470
7471 /* Look for the optional `::' operator. */
7472 global_scope_p
7473 = (cp_parser_global_scope_opt (parser,
7474 /*current_scope_valid_p=*/false)
7475 != NULL_TREE);
7476 /* Look for the `new' operator. */
7477 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7478 /* There's no easy way to tell a new-placement from the
7479 `( type-id )' construct. */
7480 cp_parser_parse_tentatively (parser);
7481 /* Look for a new-placement. */
7482 placement = cp_parser_new_placement (parser);
7483 /* If that didn't work out, there's no new-placement. */
7484 if (!cp_parser_parse_definitely (parser))
7485 {
7486 if (placement != NULL)
7487 release_tree_vector (placement);
7488 placement = NULL;
7489 }
7490
7491 /* If the next token is a `(', then we have a parenthesized
7492 type-id. */
7493 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7494 {
7495 cp_token *token;
7496 const char *saved_message = parser->type_definition_forbidden_message;
7497
7498 /* Consume the `('. */
7499 cp_lexer_consume_token (parser->lexer);
7500
7501 /* Parse the type-id. */
7502 parser->type_definition_forbidden_message
7503 = G_("types may not be defined in a new-expression");
7504 type = cp_parser_type_id (parser);
7505 parser->type_definition_forbidden_message = saved_message;
7506
7507 /* Look for the closing `)'. */
7508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7509 token = cp_lexer_peek_token (parser->lexer);
7510 /* There should not be a direct-new-declarator in this production,
7511 but GCC used to allowed this, so we check and emit a sensible error
7512 message for this case. */
7513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7514 {
7515 error_at (token->location,
7516 "array bound forbidden after parenthesized type-id");
7517 inform (token->location,
7518 "try removing the parentheses around the type-id");
7519 cp_parser_direct_new_declarator (parser);
7520 }
7521 }
7522 /* Otherwise, there must be a new-type-id. */
7523 else
7524 type = cp_parser_new_type_id (parser, &nelts);
7525
7526 /* If the next token is a `(' or '{', then we have a new-initializer. */
7527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7528 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7529 initializer = cp_parser_new_initializer (parser);
7530 else
7531 initializer = NULL;
7532
7533 /* A new-expression may not appear in an integral constant
7534 expression. */
7535 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7536 ret = error_mark_node;
7537 else
7538 {
7539 /* Create a representation of the new-expression. */
7540 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7541 tf_warning_or_error);
7542 }
7543
7544 if (placement != NULL)
7545 release_tree_vector (placement);
7546 if (initializer != NULL)
7547 release_tree_vector (initializer);
7548
7549 return ret;
7550 }
7551
7552 /* Parse a new-placement.
7553
7554 new-placement:
7555 ( expression-list )
7556
7557 Returns the same representation as for an expression-list. */
7558
7559 static vec<tree, va_gc> *
7560 cp_parser_new_placement (cp_parser* parser)
7561 {
7562 vec<tree, va_gc> *expression_list;
7563
7564 /* Parse the expression-list. */
7565 expression_list = (cp_parser_parenthesized_expression_list
7566 (parser, non_attr, /*cast_p=*/false,
7567 /*allow_expansion_p=*/true,
7568 /*non_constant_p=*/NULL));
7569
7570 return expression_list;
7571 }
7572
7573 /* Parse a new-type-id.
7574
7575 new-type-id:
7576 type-specifier-seq new-declarator [opt]
7577
7578 Returns the TYPE allocated. If the new-type-id indicates an array
7579 type, *NELTS is set to the number of elements in the last array
7580 bound; the TYPE will not include the last array bound. */
7581
7582 static tree
7583 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7584 {
7585 cp_decl_specifier_seq type_specifier_seq;
7586 cp_declarator *new_declarator;
7587 cp_declarator *declarator;
7588 cp_declarator *outer_declarator;
7589 const char *saved_message;
7590
7591 /* The type-specifier sequence must not contain type definitions.
7592 (It cannot contain declarations of new types either, but if they
7593 are not definitions we will catch that because they are not
7594 complete.) */
7595 saved_message = parser->type_definition_forbidden_message;
7596 parser->type_definition_forbidden_message
7597 = G_("types may not be defined in a new-type-id");
7598 /* Parse the type-specifier-seq. */
7599 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7600 /*is_trailing_return=*/false,
7601 &type_specifier_seq);
7602 /* Restore the old message. */
7603 parser->type_definition_forbidden_message = saved_message;
7604
7605 if (type_specifier_seq.type == error_mark_node)
7606 return error_mark_node;
7607
7608 /* Parse the new-declarator. */
7609 new_declarator = cp_parser_new_declarator_opt (parser);
7610
7611 /* Determine the number of elements in the last array dimension, if
7612 any. */
7613 *nelts = NULL_TREE;
7614 /* Skip down to the last array dimension. */
7615 declarator = new_declarator;
7616 outer_declarator = NULL;
7617 while (declarator && (declarator->kind == cdk_pointer
7618 || declarator->kind == cdk_ptrmem))
7619 {
7620 outer_declarator = declarator;
7621 declarator = declarator->declarator;
7622 }
7623 while (declarator
7624 && declarator->kind == cdk_array
7625 && declarator->declarator
7626 && declarator->declarator->kind == cdk_array)
7627 {
7628 outer_declarator = declarator;
7629 declarator = declarator->declarator;
7630 }
7631
7632 if (declarator && declarator->kind == cdk_array)
7633 {
7634 *nelts = declarator->u.array.bounds;
7635 if (*nelts == error_mark_node)
7636 *nelts = integer_one_node;
7637
7638 if (outer_declarator)
7639 outer_declarator->declarator = declarator->declarator;
7640 else
7641 new_declarator = NULL;
7642 }
7643
7644 return groktypename (&type_specifier_seq, new_declarator, false);
7645 }
7646
7647 /* Parse an (optional) new-declarator.
7648
7649 new-declarator:
7650 ptr-operator new-declarator [opt]
7651 direct-new-declarator
7652
7653 Returns the declarator. */
7654
7655 static cp_declarator *
7656 cp_parser_new_declarator_opt (cp_parser* parser)
7657 {
7658 enum tree_code code;
7659 tree type, std_attributes = NULL_TREE;
7660 cp_cv_quals cv_quals;
7661
7662 /* We don't know if there's a ptr-operator next, or not. */
7663 cp_parser_parse_tentatively (parser);
7664 /* Look for a ptr-operator. */
7665 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7666 /* If that worked, look for more new-declarators. */
7667 if (cp_parser_parse_definitely (parser))
7668 {
7669 cp_declarator *declarator;
7670
7671 /* Parse another optional declarator. */
7672 declarator = cp_parser_new_declarator_opt (parser);
7673
7674 declarator = cp_parser_make_indirect_declarator
7675 (code, type, cv_quals, declarator, std_attributes);
7676
7677 return declarator;
7678 }
7679
7680 /* If the next token is a `[', there is a direct-new-declarator. */
7681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7682 return cp_parser_direct_new_declarator (parser);
7683
7684 return NULL;
7685 }
7686
7687 /* Parse a direct-new-declarator.
7688
7689 direct-new-declarator:
7690 [ expression ]
7691 direct-new-declarator [constant-expression]
7692
7693 */
7694
7695 static cp_declarator *
7696 cp_parser_direct_new_declarator (cp_parser* parser)
7697 {
7698 cp_declarator *declarator = NULL;
7699
7700 while (true)
7701 {
7702 tree expression;
7703 cp_token *token;
7704
7705 /* Look for the opening `['. */
7706 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7707
7708 token = cp_lexer_peek_token (parser->lexer);
7709 expression = cp_parser_expression (parser);
7710 /* The standard requires that the expression have integral
7711 type. DR 74 adds enumeration types. We believe that the
7712 real intent is that these expressions be handled like the
7713 expression in a `switch' condition, which also allows
7714 classes with a single conversion to integral or
7715 enumeration type. */
7716 if (!processing_template_decl)
7717 {
7718 expression
7719 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7720 expression,
7721 /*complain=*/true);
7722 if (!expression)
7723 {
7724 error_at (token->location,
7725 "expression in new-declarator must have integral "
7726 "or enumeration type");
7727 expression = error_mark_node;
7728 }
7729 }
7730
7731 /* Look for the closing `]'. */
7732 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7733
7734 /* Add this bound to the declarator. */
7735 declarator = make_array_declarator (declarator, expression);
7736
7737 /* If the next token is not a `[', then there are no more
7738 bounds. */
7739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7740 break;
7741 }
7742
7743 return declarator;
7744 }
7745
7746 /* Parse a new-initializer.
7747
7748 new-initializer:
7749 ( expression-list [opt] )
7750 braced-init-list
7751
7752 Returns a representation of the expression-list. */
7753
7754 static vec<tree, va_gc> *
7755 cp_parser_new_initializer (cp_parser* parser)
7756 {
7757 vec<tree, va_gc> *expression_list;
7758
7759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7760 {
7761 tree t;
7762 bool expr_non_constant_p;
7763 cp_lexer_set_source_position (parser->lexer);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7765 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7766 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7767 expression_list = make_tree_vector_single (t);
7768 }
7769 else
7770 expression_list = (cp_parser_parenthesized_expression_list
7771 (parser, non_attr, /*cast_p=*/false,
7772 /*allow_expansion_p=*/true,
7773 /*non_constant_p=*/NULL));
7774
7775 return expression_list;
7776 }
7777
7778 /* Parse a delete-expression.
7779
7780 delete-expression:
7781 :: [opt] delete cast-expression
7782 :: [opt] delete [ ] cast-expression
7783
7784 Returns a representation of the expression. */
7785
7786 static tree
7787 cp_parser_delete_expression (cp_parser* parser)
7788 {
7789 bool global_scope_p;
7790 bool array_p;
7791 tree expression;
7792
7793 /* Look for the optional `::' operator. */
7794 global_scope_p
7795 = (cp_parser_global_scope_opt (parser,
7796 /*current_scope_valid_p=*/false)
7797 != NULL_TREE);
7798 /* Look for the `delete' keyword. */
7799 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7800 /* See if the array syntax is in use. */
7801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7802 {
7803 /* Consume the `[' token. */
7804 cp_lexer_consume_token (parser->lexer);
7805 /* Look for the `]' token. */
7806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7807 /* Remember that this is the `[]' construct. */
7808 array_p = true;
7809 }
7810 else
7811 array_p = false;
7812
7813 /* Parse the cast-expression. */
7814 expression = cp_parser_simple_cast_expression (parser);
7815
7816 /* A delete-expression may not appear in an integral constant
7817 expression. */
7818 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7819 return error_mark_node;
7820
7821 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7822 tf_warning_or_error);
7823 }
7824
7825 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7826 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7827 0 otherwise. */
7828
7829 static int
7830 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7831 {
7832 cp_token *token = cp_lexer_peek_token (parser->lexer);
7833 switch (token->type)
7834 {
7835 case CPP_COMMA:
7836 case CPP_SEMICOLON:
7837 case CPP_QUERY:
7838 case CPP_COLON:
7839 case CPP_CLOSE_SQUARE:
7840 case CPP_CLOSE_PAREN:
7841 case CPP_CLOSE_BRACE:
7842 case CPP_OPEN_BRACE:
7843 case CPP_DOT:
7844 case CPP_DOT_STAR:
7845 case CPP_DEREF:
7846 case CPP_DEREF_STAR:
7847 case CPP_DIV:
7848 case CPP_MOD:
7849 case CPP_LSHIFT:
7850 case CPP_RSHIFT:
7851 case CPP_LESS:
7852 case CPP_GREATER:
7853 case CPP_LESS_EQ:
7854 case CPP_GREATER_EQ:
7855 case CPP_EQ_EQ:
7856 case CPP_NOT_EQ:
7857 case CPP_EQ:
7858 case CPP_MULT_EQ:
7859 case CPP_DIV_EQ:
7860 case CPP_MOD_EQ:
7861 case CPP_PLUS_EQ:
7862 case CPP_MINUS_EQ:
7863 case CPP_RSHIFT_EQ:
7864 case CPP_LSHIFT_EQ:
7865 case CPP_AND_EQ:
7866 case CPP_XOR_EQ:
7867 case CPP_OR_EQ:
7868 case CPP_XOR:
7869 case CPP_OR:
7870 case CPP_OR_OR:
7871 case CPP_EOF:
7872 case CPP_ELLIPSIS:
7873 return 0;
7874
7875 case CPP_OPEN_PAREN:
7876 /* In ((type ()) () the last () isn't a valid cast-expression,
7877 so the whole must be parsed as postfix-expression. */
7878 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7879 != CPP_CLOSE_PAREN;
7880
7881 case CPP_OPEN_SQUARE:
7882 /* '[' may start a primary-expression in obj-c++ and in C++11,
7883 as a lambda-expression, eg, '(void)[]{}'. */
7884 if (cxx_dialect >= cxx11)
7885 return -1;
7886 return c_dialect_objc ();
7887
7888 case CPP_PLUS_PLUS:
7889 case CPP_MINUS_MINUS:
7890 /* '++' and '--' may or may not start a cast-expression:
7891
7892 struct T { void operator++(int); };
7893 void f() { (T())++; }
7894
7895 vs
7896
7897 int a;
7898 (int)++a; */
7899 return -1;
7900
7901 default:
7902 return 1;
7903 }
7904 }
7905
7906 /* Parse a cast-expression.
7907
7908 cast-expression:
7909 unary-expression
7910 ( type-id ) cast-expression
7911
7912 ADDRESS_P is true iff the unary-expression is appearing as the
7913 operand of the `&' operator. CAST_P is true if this expression is
7914 the target of a cast.
7915
7916 Returns a representation of the expression. */
7917
7918 static tree
7919 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7920 bool decltype_p, cp_id_kind * pidk)
7921 {
7922 /* If it's a `(', then we might be looking at a cast. */
7923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7924 {
7925 tree type = NULL_TREE;
7926 tree expr = NULL_TREE;
7927 int cast_expression = 0;
7928 const char *saved_message;
7929
7930 /* There's no way to know yet whether or not this is a cast.
7931 For example, `(int (3))' is a unary-expression, while `(int)
7932 3' is a cast. So, we resort to parsing tentatively. */
7933 cp_parser_parse_tentatively (parser);
7934 /* Types may not be defined in a cast. */
7935 saved_message = parser->type_definition_forbidden_message;
7936 parser->type_definition_forbidden_message
7937 = G_("types may not be defined in casts");
7938 /* Consume the `('. */
7939 cp_lexer_consume_token (parser->lexer);
7940 /* A very tricky bit is that `(struct S) { 3 }' is a
7941 compound-literal (which we permit in C++ as an extension).
7942 But, that construct is not a cast-expression -- it is a
7943 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7944 is legal; if the compound-literal were a cast-expression,
7945 you'd need an extra set of parentheses.) But, if we parse
7946 the type-id, and it happens to be a class-specifier, then we
7947 will commit to the parse at that point, because we cannot
7948 undo the action that is done when creating a new class. So,
7949 then we cannot back up and do a postfix-expression.
7950
7951 Another tricky case is the following (c++/29234):
7952
7953 struct S { void operator () (); };
7954
7955 void foo ()
7956 {
7957 ( S()() );
7958 }
7959
7960 As a type-id we parse the parenthesized S()() as a function
7961 returning a function, groktypename complains and we cannot
7962 back up in this case either.
7963
7964 Therefore, we scan ahead to the closing `)', and check to see
7965 if the tokens after the `)' can start a cast-expression. Otherwise
7966 we are dealing with an unary-expression, a postfix-expression
7967 or something else.
7968
7969 Yet another tricky case, in C++11, is the following (c++/54891):
7970
7971 (void)[]{};
7972
7973 The issue is that usually, besides the case of lambda-expressions,
7974 the parenthesized type-id cannot be followed by '[', and, eg, we
7975 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7976 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7977 we don't commit, we try a cast-expression, then an unary-expression.
7978
7979 Save tokens so that we can put them back. */
7980 cp_lexer_save_tokens (parser->lexer);
7981
7982 /* We may be looking at a cast-expression. */
7983 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7984 /*consume_paren=*/true))
7985 cast_expression
7986 = cp_parser_tokens_start_cast_expression (parser);
7987
7988 /* Roll back the tokens we skipped. */
7989 cp_lexer_rollback_tokens (parser->lexer);
7990 /* If we aren't looking at a cast-expression, simulate an error so
7991 that the call to cp_parser_error_occurred below returns true. */
7992 if (!cast_expression)
7993 cp_parser_simulate_error (parser);
7994 else
7995 {
7996 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7997 parser->in_type_id_in_expr_p = true;
7998 /* Look for the type-id. */
7999 type = cp_parser_type_id (parser);
8000 /* Look for the closing `)'. */
8001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8002 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8003 }
8004
8005 /* Restore the saved message. */
8006 parser->type_definition_forbidden_message = saved_message;
8007
8008 /* At this point this can only be either a cast or a
8009 parenthesized ctor such as `(T ())' that looks like a cast to
8010 function returning T. */
8011 if (!cp_parser_error_occurred (parser))
8012 {
8013 /* Only commit if the cast-expression doesn't start with
8014 '++', '--', or '[' in C++11. */
8015 if (cast_expression > 0)
8016 cp_parser_commit_to_topmost_tentative_parse (parser);
8017
8018 expr = cp_parser_cast_expression (parser,
8019 /*address_p=*/false,
8020 /*cast_p=*/true,
8021 /*decltype_p=*/false,
8022 pidk);
8023
8024 if (cp_parser_parse_definitely (parser))
8025 {
8026 /* Warn about old-style casts, if so requested. */
8027 if (warn_old_style_cast
8028 && !in_system_header_at (input_location)
8029 && !VOID_TYPE_P (type)
8030 && current_lang_name != lang_name_c)
8031 warning (OPT_Wold_style_cast, "use of old-style cast");
8032
8033 /* Only type conversions to integral or enumeration types
8034 can be used in constant-expressions. */
8035 if (!cast_valid_in_integral_constant_expression_p (type)
8036 && cp_parser_non_integral_constant_expression (parser,
8037 NIC_CAST))
8038 return error_mark_node;
8039
8040 /* Perform the cast. */
8041 expr = build_c_cast (input_location, type, expr);
8042 return expr;
8043 }
8044 }
8045 else
8046 cp_parser_abort_tentative_parse (parser);
8047 }
8048
8049 /* If we get here, then it's not a cast, so it must be a
8050 unary-expression. */
8051 return cp_parser_unary_expression (parser, pidk, address_p,
8052 cast_p, decltype_p);
8053 }
8054
8055 /* Parse a binary expression of the general form:
8056
8057 pm-expression:
8058 cast-expression
8059 pm-expression .* cast-expression
8060 pm-expression ->* cast-expression
8061
8062 multiplicative-expression:
8063 pm-expression
8064 multiplicative-expression * pm-expression
8065 multiplicative-expression / pm-expression
8066 multiplicative-expression % pm-expression
8067
8068 additive-expression:
8069 multiplicative-expression
8070 additive-expression + multiplicative-expression
8071 additive-expression - multiplicative-expression
8072
8073 shift-expression:
8074 additive-expression
8075 shift-expression << additive-expression
8076 shift-expression >> additive-expression
8077
8078 relational-expression:
8079 shift-expression
8080 relational-expression < shift-expression
8081 relational-expression > shift-expression
8082 relational-expression <= shift-expression
8083 relational-expression >= shift-expression
8084
8085 GNU Extension:
8086
8087 relational-expression:
8088 relational-expression <? shift-expression
8089 relational-expression >? shift-expression
8090
8091 equality-expression:
8092 relational-expression
8093 equality-expression == relational-expression
8094 equality-expression != relational-expression
8095
8096 and-expression:
8097 equality-expression
8098 and-expression & equality-expression
8099
8100 exclusive-or-expression:
8101 and-expression
8102 exclusive-or-expression ^ and-expression
8103
8104 inclusive-or-expression:
8105 exclusive-or-expression
8106 inclusive-or-expression | exclusive-or-expression
8107
8108 logical-and-expression:
8109 inclusive-or-expression
8110 logical-and-expression && inclusive-or-expression
8111
8112 logical-or-expression:
8113 logical-and-expression
8114 logical-or-expression || logical-and-expression
8115
8116 All these are implemented with a single function like:
8117
8118 binary-expression:
8119 simple-cast-expression
8120 binary-expression <token> binary-expression
8121
8122 CAST_P is true if this expression is the target of a cast.
8123
8124 The binops_by_token map is used to get the tree codes for each <token> type.
8125 binary-expressions are associated according to a precedence table. */
8126
8127 #define TOKEN_PRECEDENCE(token) \
8128 (((token->type == CPP_GREATER \
8129 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8130 && !parser->greater_than_is_operator_p) \
8131 ? PREC_NOT_OPERATOR \
8132 : binops_by_token[token->type].prec)
8133
8134 static tree
8135 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8136 bool no_toplevel_fold_p,
8137 bool decltype_p,
8138 enum cp_parser_prec prec,
8139 cp_id_kind * pidk)
8140 {
8141 cp_parser_expression_stack stack;
8142 cp_parser_expression_stack_entry *sp = &stack[0];
8143 cp_parser_expression_stack_entry current;
8144 tree rhs;
8145 cp_token *token;
8146 enum tree_code rhs_type;
8147 enum cp_parser_prec new_prec, lookahead_prec;
8148 tree overload;
8149
8150 /* Parse the first expression. */
8151 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8152 ? TRUTH_NOT_EXPR : ERROR_MARK);
8153 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8154 cast_p, decltype_p, pidk);
8155 current.prec = prec;
8156
8157 if (cp_parser_error_occurred (parser))
8158 return error_mark_node;
8159
8160 for (;;)
8161 {
8162 /* Get an operator token. */
8163 token = cp_lexer_peek_token (parser->lexer);
8164
8165 if (warn_cxx11_compat
8166 && token->type == CPP_RSHIFT
8167 && !parser->greater_than_is_operator_p)
8168 {
8169 if (warning_at (token->location, OPT_Wc__11_compat,
8170 "%<>>%> operator is treated"
8171 " as two right angle brackets in C++11"))
8172 inform (token->location,
8173 "suggest parentheses around %<>>%> expression");
8174 }
8175
8176 new_prec = TOKEN_PRECEDENCE (token);
8177
8178 /* Popping an entry off the stack means we completed a subexpression:
8179 - either we found a token which is not an operator (`>' where it is not
8180 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8181 will happen repeatedly;
8182 - or, we found an operator which has lower priority. This is the case
8183 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8184 parsing `3 * 4'. */
8185 if (new_prec <= current.prec)
8186 {
8187 if (sp == stack)
8188 break;
8189 else
8190 goto pop;
8191 }
8192
8193 get_rhs:
8194 current.tree_type = binops_by_token[token->type].tree_type;
8195 current.loc = token->location;
8196
8197 /* We used the operator token. */
8198 cp_lexer_consume_token (parser->lexer);
8199
8200 /* For "false && x" or "true || x", x will never be executed;
8201 disable warnings while evaluating it. */
8202 if (current.tree_type == TRUTH_ANDIF_EXPR)
8203 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8204 else if (current.tree_type == TRUTH_ORIF_EXPR)
8205 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8206
8207 /* Extract another operand. It may be the RHS of this expression
8208 or the LHS of a new, higher priority expression. */
8209 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8210 ? TRUTH_NOT_EXPR : ERROR_MARK);
8211 rhs = cp_parser_simple_cast_expression (parser);
8212
8213 /* Get another operator token. Look up its precedence to avoid
8214 building a useless (immediately popped) stack entry for common
8215 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8216 token = cp_lexer_peek_token (parser->lexer);
8217 lookahead_prec = TOKEN_PRECEDENCE (token);
8218 if (lookahead_prec > new_prec)
8219 {
8220 /* ... and prepare to parse the RHS of the new, higher priority
8221 expression. Since precedence levels on the stack are
8222 monotonically increasing, we do not have to care about
8223 stack overflows. */
8224 *sp = current;
8225 ++sp;
8226 current.lhs = rhs;
8227 current.lhs_type = rhs_type;
8228 current.prec = new_prec;
8229 new_prec = lookahead_prec;
8230 goto get_rhs;
8231
8232 pop:
8233 lookahead_prec = new_prec;
8234 /* If the stack is not empty, we have parsed into LHS the right side
8235 (`4' in the example above) of an expression we had suspended.
8236 We can use the information on the stack to recover the LHS (`3')
8237 from the stack together with the tree code (`MULT_EXPR'), and
8238 the precedence of the higher level subexpression
8239 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8240 which will be used to actually build the additive expression. */
8241 rhs = current.lhs;
8242 rhs_type = current.lhs_type;
8243 --sp;
8244 current = *sp;
8245 }
8246
8247 /* Undo the disabling of warnings done above. */
8248 if (current.tree_type == TRUTH_ANDIF_EXPR)
8249 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8250 else if (current.tree_type == TRUTH_ORIF_EXPR)
8251 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8252
8253 if (warn_logical_not_paren
8254 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8255 && current.lhs_type == TRUTH_NOT_EXPR
8256 /* Avoid warning for !!x == y. */
8257 && (TREE_CODE (current.lhs) != NE_EXPR
8258 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8259 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8260 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8261 /* Avoid warning for !b == y where b is boolean. */
8262 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8263 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8264 != BOOLEAN_TYPE))))
8265 /* Avoid warning for !!b == y where b is boolean. */
8266 && (!DECL_P (current.lhs)
8267 || TREE_TYPE (current.lhs) == NULL_TREE
8268 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8269 warn_logical_not_parentheses (current.loc, current.tree_type,
8270 maybe_constant_value (rhs));
8271
8272 overload = NULL;
8273 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8274 ERROR_MARK for everything that is not a binary expression.
8275 This makes warn_about_parentheses miss some warnings that
8276 involve unary operators. For unary expressions we should
8277 pass the correct tree_code unless the unary expression was
8278 surrounded by parentheses.
8279 */
8280 if (no_toplevel_fold_p
8281 && lookahead_prec <= current.prec
8282 && sp == stack)
8283 current.lhs = build2 (current.tree_type,
8284 TREE_CODE_CLASS (current.tree_type)
8285 == tcc_comparison
8286 ? boolean_type_node : TREE_TYPE (current.lhs),
8287 current.lhs, rhs);
8288 else
8289 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8290 current.lhs, current.lhs_type,
8291 rhs, rhs_type, &overload,
8292 complain_flags (decltype_p));
8293 current.lhs_type = current.tree_type;
8294 if (EXPR_P (current.lhs))
8295 SET_EXPR_LOCATION (current.lhs, current.loc);
8296
8297 /* If the binary operator required the use of an overloaded operator,
8298 then this expression cannot be an integral constant-expression.
8299 An overloaded operator can be used even if both operands are
8300 otherwise permissible in an integral constant-expression if at
8301 least one of the operands is of enumeration type. */
8302
8303 if (overload
8304 && cp_parser_non_integral_constant_expression (parser,
8305 NIC_OVERLOADED))
8306 return error_mark_node;
8307 }
8308
8309 return current.lhs;
8310 }
8311
8312 static tree
8313 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8314 bool no_toplevel_fold_p,
8315 enum cp_parser_prec prec,
8316 cp_id_kind * pidk)
8317 {
8318 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8319 /*decltype*/false, prec, pidk);
8320 }
8321
8322 /* Parse the `? expression : assignment-expression' part of a
8323 conditional-expression. The LOGICAL_OR_EXPR is the
8324 logical-or-expression that started the conditional-expression.
8325 Returns a representation of the entire conditional-expression.
8326
8327 This routine is used by cp_parser_assignment_expression.
8328
8329 ? expression : assignment-expression
8330
8331 GNU Extensions:
8332
8333 ? : assignment-expression */
8334
8335 static tree
8336 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8337 {
8338 tree expr;
8339 tree assignment_expr;
8340 struct cp_token *token;
8341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8342
8343 /* Consume the `?' token. */
8344 cp_lexer_consume_token (parser->lexer);
8345 token = cp_lexer_peek_token (parser->lexer);
8346 if (cp_parser_allow_gnu_extensions_p (parser)
8347 && token->type == CPP_COLON)
8348 {
8349 pedwarn (token->location, OPT_Wpedantic,
8350 "ISO C++ does not allow ?: with omitted middle operand");
8351 /* Implicit true clause. */
8352 expr = NULL_TREE;
8353 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8354 warn_for_omitted_condop (token->location, logical_or_expr);
8355 }
8356 else
8357 {
8358 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8359 parser->colon_corrects_to_scope_p = false;
8360 /* Parse the expression. */
8361 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8362 expr = cp_parser_expression (parser);
8363 c_inhibit_evaluation_warnings +=
8364 ((logical_or_expr == truthvalue_true_node)
8365 - (logical_or_expr == truthvalue_false_node));
8366 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8367 }
8368
8369 /* The next token should be a `:'. */
8370 cp_parser_require (parser, CPP_COLON, RT_COLON);
8371 /* Parse the assignment-expression. */
8372 assignment_expr = cp_parser_assignment_expression (parser);
8373 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8374
8375 /* Build the conditional-expression. */
8376 return build_x_conditional_expr (loc, logical_or_expr,
8377 expr,
8378 assignment_expr,
8379 tf_warning_or_error);
8380 }
8381
8382 /* Parse an assignment-expression.
8383
8384 assignment-expression:
8385 conditional-expression
8386 logical-or-expression assignment-operator assignment_expression
8387 throw-expression
8388
8389 CAST_P is true if this expression is the target of a cast.
8390 DECLTYPE_P is true if this expression is the operand of decltype.
8391
8392 Returns a representation for the expression. */
8393
8394 static tree
8395 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8396 bool cast_p, bool decltype_p)
8397 {
8398 tree expr;
8399
8400 /* If the next token is the `throw' keyword, then we're looking at
8401 a throw-expression. */
8402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8403 expr = cp_parser_throw_expression (parser);
8404 /* Otherwise, it must be that we are looking at a
8405 logical-or-expression. */
8406 else
8407 {
8408 /* Parse the binary expressions (logical-or-expression). */
8409 expr = cp_parser_binary_expression (parser, cast_p, false,
8410 decltype_p,
8411 PREC_NOT_OPERATOR, pidk);
8412 /* If the next token is a `?' then we're actually looking at a
8413 conditional-expression. */
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8415 return cp_parser_question_colon_clause (parser, expr);
8416 else
8417 {
8418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8419
8420 /* If it's an assignment-operator, we're using the second
8421 production. */
8422 enum tree_code assignment_operator
8423 = cp_parser_assignment_operator_opt (parser);
8424 if (assignment_operator != ERROR_MARK)
8425 {
8426 bool non_constant_p;
8427 location_t saved_input_location;
8428
8429 /* Parse the right-hand side of the assignment. */
8430 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8431
8432 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8433 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8434
8435 /* An assignment may not appear in a
8436 constant-expression. */
8437 if (cp_parser_non_integral_constant_expression (parser,
8438 NIC_ASSIGNMENT))
8439 return error_mark_node;
8440 /* Build the assignment expression. Its default
8441 location is the location of the '=' token. */
8442 saved_input_location = input_location;
8443 input_location = loc;
8444 expr = build_x_modify_expr (loc, expr,
8445 assignment_operator,
8446 rhs,
8447 complain_flags (decltype_p));
8448 input_location = saved_input_location;
8449 }
8450 }
8451 }
8452
8453 return expr;
8454 }
8455
8456 /* Parse an (optional) assignment-operator.
8457
8458 assignment-operator: one of
8459 = *= /= %= += -= >>= <<= &= ^= |=
8460
8461 GNU Extension:
8462
8463 assignment-operator: one of
8464 <?= >?=
8465
8466 If the next token is an assignment operator, the corresponding tree
8467 code is returned, and the token is consumed. For example, for
8468 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8469 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8470 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8471 operator, ERROR_MARK is returned. */
8472
8473 static enum tree_code
8474 cp_parser_assignment_operator_opt (cp_parser* parser)
8475 {
8476 enum tree_code op;
8477 cp_token *token;
8478
8479 /* Peek at the next token. */
8480 token = cp_lexer_peek_token (parser->lexer);
8481
8482 switch (token->type)
8483 {
8484 case CPP_EQ:
8485 op = NOP_EXPR;
8486 break;
8487
8488 case CPP_MULT_EQ:
8489 op = MULT_EXPR;
8490 break;
8491
8492 case CPP_DIV_EQ:
8493 op = TRUNC_DIV_EXPR;
8494 break;
8495
8496 case CPP_MOD_EQ:
8497 op = TRUNC_MOD_EXPR;
8498 break;
8499
8500 case CPP_PLUS_EQ:
8501 op = PLUS_EXPR;
8502 break;
8503
8504 case CPP_MINUS_EQ:
8505 op = MINUS_EXPR;
8506 break;
8507
8508 case CPP_RSHIFT_EQ:
8509 op = RSHIFT_EXPR;
8510 break;
8511
8512 case CPP_LSHIFT_EQ:
8513 op = LSHIFT_EXPR;
8514 break;
8515
8516 case CPP_AND_EQ:
8517 op = BIT_AND_EXPR;
8518 break;
8519
8520 case CPP_XOR_EQ:
8521 op = BIT_XOR_EXPR;
8522 break;
8523
8524 case CPP_OR_EQ:
8525 op = BIT_IOR_EXPR;
8526 break;
8527
8528 default:
8529 /* Nothing else is an assignment operator. */
8530 op = ERROR_MARK;
8531 }
8532
8533 /* If it was an assignment operator, consume it. */
8534 if (op != ERROR_MARK)
8535 cp_lexer_consume_token (parser->lexer);
8536
8537 return op;
8538 }
8539
8540 /* Parse an expression.
8541
8542 expression:
8543 assignment-expression
8544 expression , assignment-expression
8545
8546 CAST_P is true if this expression is the target of a cast.
8547 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8548 except possibly parenthesized or on the RHS of a comma (N3276).
8549
8550 Returns a representation of the expression. */
8551
8552 static tree
8553 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8554 bool cast_p, bool decltype_p)
8555 {
8556 tree expression = NULL_TREE;
8557 location_t loc = UNKNOWN_LOCATION;
8558
8559 while (true)
8560 {
8561 tree assignment_expression;
8562
8563 /* Parse the next assignment-expression. */
8564 assignment_expression
8565 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8566
8567 /* We don't create a temporary for a call that is the immediate operand
8568 of decltype or on the RHS of a comma. But when we see a comma, we
8569 need to create a temporary for a call on the LHS. */
8570 if (decltype_p && !processing_template_decl
8571 && TREE_CODE (assignment_expression) == CALL_EXPR
8572 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8573 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8574 assignment_expression
8575 = build_cplus_new (TREE_TYPE (assignment_expression),
8576 assignment_expression, tf_warning_or_error);
8577
8578 /* If this is the first assignment-expression, we can just
8579 save it away. */
8580 if (!expression)
8581 expression = assignment_expression;
8582 else
8583 expression = build_x_compound_expr (loc, expression,
8584 assignment_expression,
8585 complain_flags (decltype_p));
8586 /* If the next token is not a comma, then we are done with the
8587 expression. */
8588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8589 break;
8590 /* Consume the `,'. */
8591 loc = cp_lexer_peek_token (parser->lexer)->location;
8592 cp_lexer_consume_token (parser->lexer);
8593 /* A comma operator cannot appear in a constant-expression. */
8594 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8595 expression = error_mark_node;
8596 }
8597
8598 return expression;
8599 }
8600
8601 /* Parse a constant-expression.
8602
8603 constant-expression:
8604 conditional-expression
8605
8606 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8607 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8608 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8609 is false, NON_CONSTANT_P should be NULL. */
8610
8611 static tree
8612 cp_parser_constant_expression (cp_parser* parser,
8613 bool allow_non_constant_p,
8614 bool *non_constant_p)
8615 {
8616 bool saved_integral_constant_expression_p;
8617 bool saved_allow_non_integral_constant_expression_p;
8618 bool saved_non_integral_constant_expression_p;
8619 tree expression;
8620
8621 /* It might seem that we could simply parse the
8622 conditional-expression, and then check to see if it were
8623 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8624 one that the compiler can figure out is constant, possibly after
8625 doing some simplifications or optimizations. The standard has a
8626 precise definition of constant-expression, and we must honor
8627 that, even though it is somewhat more restrictive.
8628
8629 For example:
8630
8631 int i[(2, 3)];
8632
8633 is not a legal declaration, because `(2, 3)' is not a
8634 constant-expression. The `,' operator is forbidden in a
8635 constant-expression. However, GCC's constant-folding machinery
8636 will fold this operation to an INTEGER_CST for `3'. */
8637
8638 /* Save the old settings. */
8639 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8640 saved_allow_non_integral_constant_expression_p
8641 = parser->allow_non_integral_constant_expression_p;
8642 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8643 /* We are now parsing a constant-expression. */
8644 parser->integral_constant_expression_p = true;
8645 parser->allow_non_integral_constant_expression_p
8646 = (allow_non_constant_p || cxx_dialect >= cxx11);
8647 parser->non_integral_constant_expression_p = false;
8648 /* Although the grammar says "conditional-expression", we parse an
8649 "assignment-expression", which also permits "throw-expression"
8650 and the use of assignment operators. In the case that
8651 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8652 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8653 actually essential that we look for an assignment-expression.
8654 For example, cp_parser_initializer_clauses uses this function to
8655 determine whether a particular assignment-expression is in fact
8656 constant. */
8657 expression = cp_parser_assignment_expression (parser);
8658 /* Restore the old settings. */
8659 parser->integral_constant_expression_p
8660 = saved_integral_constant_expression_p;
8661 parser->allow_non_integral_constant_expression_p
8662 = saved_allow_non_integral_constant_expression_p;
8663 if (cxx_dialect >= cxx11)
8664 {
8665 /* Require an rvalue constant expression here; that's what our
8666 callers expect. Reference constant expressions are handled
8667 separately in e.g. cp_parser_template_argument. */
8668 bool is_const = potential_rvalue_constant_expression (expression);
8669 parser->non_integral_constant_expression_p = !is_const;
8670 if (!is_const && !allow_non_constant_p)
8671 require_potential_rvalue_constant_expression (expression);
8672 }
8673 if (allow_non_constant_p)
8674 *non_constant_p = parser->non_integral_constant_expression_p;
8675 parser->non_integral_constant_expression_p
8676 = saved_non_integral_constant_expression_p;
8677
8678 return expression;
8679 }
8680
8681 /* Parse __builtin_offsetof.
8682
8683 offsetof-expression:
8684 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8685
8686 offsetof-member-designator:
8687 id-expression
8688 | offsetof-member-designator "." id-expression
8689 | offsetof-member-designator "[" expression "]"
8690 | offsetof-member-designator "->" id-expression */
8691
8692 static tree
8693 cp_parser_builtin_offsetof (cp_parser *parser)
8694 {
8695 int save_ice_p, save_non_ice_p;
8696 tree type, expr;
8697 cp_id_kind dummy;
8698 cp_token *token;
8699
8700 /* We're about to accept non-integral-constant things, but will
8701 definitely yield an integral constant expression. Save and
8702 restore these values around our local parsing. */
8703 save_ice_p = parser->integral_constant_expression_p;
8704 save_non_ice_p = parser->non_integral_constant_expression_p;
8705
8706 /* Consume the "__builtin_offsetof" token. */
8707 cp_lexer_consume_token (parser->lexer);
8708 /* Consume the opening `('. */
8709 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8710 /* Parse the type-id. */
8711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8712 type = cp_parser_type_id (parser);
8713 /* Look for the `,'. */
8714 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8715 token = cp_lexer_peek_token (parser->lexer);
8716
8717 /* Build the (type *)null that begins the traditional offsetof macro. */
8718 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8719 tf_warning_or_error);
8720
8721 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8722 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8723 true, &dummy, token->location);
8724 while (true)
8725 {
8726 token = cp_lexer_peek_token (parser->lexer);
8727 switch (token->type)
8728 {
8729 case CPP_OPEN_SQUARE:
8730 /* offsetof-member-designator "[" expression "]" */
8731 expr = cp_parser_postfix_open_square_expression (parser, expr,
8732 true, false);
8733 break;
8734
8735 case CPP_DEREF:
8736 /* offsetof-member-designator "->" identifier */
8737 expr = grok_array_decl (token->location, expr,
8738 integer_zero_node, false);
8739 /* FALLTHRU */
8740
8741 case CPP_DOT:
8742 /* offsetof-member-designator "." identifier */
8743 cp_lexer_consume_token (parser->lexer);
8744 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8745 expr, true, &dummy,
8746 token->location);
8747 break;
8748
8749 case CPP_CLOSE_PAREN:
8750 /* Consume the ")" token. */
8751 cp_lexer_consume_token (parser->lexer);
8752 goto success;
8753
8754 default:
8755 /* Error. We know the following require will fail, but
8756 that gives the proper error message. */
8757 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8758 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8759 expr = error_mark_node;
8760 goto failure;
8761 }
8762 }
8763
8764 success:
8765 expr = finish_offsetof (expr, loc);
8766
8767 failure:
8768 parser->integral_constant_expression_p = save_ice_p;
8769 parser->non_integral_constant_expression_p = save_non_ice_p;
8770
8771 return expr;
8772 }
8773
8774 /* Parse a trait expression.
8775
8776 Returns a representation of the expression, the underlying type
8777 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8778
8779 static tree
8780 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8781 {
8782 cp_trait_kind kind;
8783 tree type1, type2 = NULL_TREE;
8784 bool binary = false;
8785 bool variadic = false;
8786
8787 switch (keyword)
8788 {
8789 case RID_HAS_NOTHROW_ASSIGN:
8790 kind = CPTK_HAS_NOTHROW_ASSIGN;
8791 break;
8792 case RID_HAS_NOTHROW_CONSTRUCTOR:
8793 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8794 break;
8795 case RID_HAS_NOTHROW_COPY:
8796 kind = CPTK_HAS_NOTHROW_COPY;
8797 break;
8798 case RID_HAS_TRIVIAL_ASSIGN:
8799 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8800 break;
8801 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8802 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8803 break;
8804 case RID_HAS_TRIVIAL_COPY:
8805 kind = CPTK_HAS_TRIVIAL_COPY;
8806 break;
8807 case RID_HAS_TRIVIAL_DESTRUCTOR:
8808 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8809 break;
8810 case RID_HAS_VIRTUAL_DESTRUCTOR:
8811 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8812 break;
8813 case RID_IS_ABSTRACT:
8814 kind = CPTK_IS_ABSTRACT;
8815 break;
8816 case RID_IS_BASE_OF:
8817 kind = CPTK_IS_BASE_OF;
8818 binary = true;
8819 break;
8820 case RID_IS_CLASS:
8821 kind = CPTK_IS_CLASS;
8822 break;
8823 case RID_IS_EMPTY:
8824 kind = CPTK_IS_EMPTY;
8825 break;
8826 case RID_IS_ENUM:
8827 kind = CPTK_IS_ENUM;
8828 break;
8829 case RID_IS_FINAL:
8830 kind = CPTK_IS_FINAL;
8831 break;
8832 case RID_IS_LITERAL_TYPE:
8833 kind = CPTK_IS_LITERAL_TYPE;
8834 break;
8835 case RID_IS_POD:
8836 kind = CPTK_IS_POD;
8837 break;
8838 case RID_IS_POLYMORPHIC:
8839 kind = CPTK_IS_POLYMORPHIC;
8840 break;
8841 case RID_IS_STD_LAYOUT:
8842 kind = CPTK_IS_STD_LAYOUT;
8843 break;
8844 case RID_IS_TRIVIAL:
8845 kind = CPTK_IS_TRIVIAL;
8846 break;
8847 case RID_IS_TRIVIALLY_ASSIGNABLE:
8848 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8849 binary = true;
8850 break;
8851 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8852 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8853 variadic = true;
8854 break;
8855 case RID_IS_TRIVIALLY_COPYABLE:
8856 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8857 break;
8858 case RID_IS_UNION:
8859 kind = CPTK_IS_UNION;
8860 break;
8861 case RID_UNDERLYING_TYPE:
8862 kind = CPTK_UNDERLYING_TYPE;
8863 break;
8864 case RID_BASES:
8865 kind = CPTK_BASES;
8866 break;
8867 case RID_DIRECT_BASES:
8868 kind = CPTK_DIRECT_BASES;
8869 break;
8870 default:
8871 gcc_unreachable ();
8872 }
8873
8874 /* Consume the token. */
8875 cp_lexer_consume_token (parser->lexer);
8876
8877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8878
8879 type1 = cp_parser_type_id (parser);
8880
8881 if (type1 == error_mark_node)
8882 return error_mark_node;
8883
8884 if (binary)
8885 {
8886 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8887
8888 type2 = cp_parser_type_id (parser);
8889
8890 if (type2 == error_mark_node)
8891 return error_mark_node;
8892 }
8893 else if (variadic)
8894 {
8895 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8896 {
8897 cp_lexer_consume_token (parser->lexer);
8898 tree elt = cp_parser_type_id (parser);
8899 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8900 {
8901 cp_lexer_consume_token (parser->lexer);
8902 elt = make_pack_expansion (elt);
8903 }
8904 if (elt == error_mark_node)
8905 return error_mark_node;
8906 type2 = tree_cons (NULL_TREE, elt, type2);
8907 }
8908 }
8909
8910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8911
8912 /* Complete the trait expression, which may mean either processing
8913 the trait expr now or saving it for template instantiation. */
8914 switch(kind)
8915 {
8916 case CPTK_UNDERLYING_TYPE:
8917 return finish_underlying_type (type1);
8918 case CPTK_BASES:
8919 return finish_bases (type1, false);
8920 case CPTK_DIRECT_BASES:
8921 return finish_bases (type1, true);
8922 default:
8923 return finish_trait_expr (kind, type1, type2);
8924 }
8925 }
8926
8927 /* Lambdas that appear in variable initializer or default argument scope
8928 get that in their mangling, so we need to record it. We might as well
8929 use the count for function and namespace scopes as well. */
8930 static GTY(()) tree lambda_scope;
8931 static GTY(()) int lambda_count;
8932 typedef struct GTY(()) tree_int
8933 {
8934 tree t;
8935 int i;
8936 } tree_int;
8937 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8938
8939 static void
8940 start_lambda_scope (tree decl)
8941 {
8942 tree_int ti;
8943 gcc_assert (decl);
8944 /* Once we're inside a function, we ignore other scopes and just push
8945 the function again so that popping works properly. */
8946 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8947 decl = current_function_decl;
8948 ti.t = lambda_scope;
8949 ti.i = lambda_count;
8950 vec_safe_push (lambda_scope_stack, ti);
8951 if (lambda_scope != decl)
8952 {
8953 /* Don't reset the count if we're still in the same function. */
8954 lambda_scope = decl;
8955 lambda_count = 0;
8956 }
8957 }
8958
8959 static void
8960 record_lambda_scope (tree lambda)
8961 {
8962 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8963 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8964 }
8965
8966 static void
8967 finish_lambda_scope (void)
8968 {
8969 tree_int *p = &lambda_scope_stack->last ();
8970 if (lambda_scope != p->t)
8971 {
8972 lambda_scope = p->t;
8973 lambda_count = p->i;
8974 }
8975 lambda_scope_stack->pop ();
8976 }
8977
8978 /* Parse a lambda expression.
8979
8980 lambda-expression:
8981 lambda-introducer lambda-declarator [opt] compound-statement
8982
8983 Returns a representation of the expression. */
8984
8985 static tree
8986 cp_parser_lambda_expression (cp_parser* parser)
8987 {
8988 tree lambda_expr = build_lambda_expr ();
8989 tree type;
8990 bool ok = true;
8991 cp_token *token = cp_lexer_peek_token (parser->lexer);
8992 cp_token_position start = 0;
8993
8994 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8995
8996 if (cp_unevaluated_operand)
8997 {
8998 if (!token->error_reported)
8999 {
9000 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9001 "lambda-expression in unevaluated context");
9002 token->error_reported = true;
9003 }
9004 ok = false;
9005 }
9006 else if (parser->in_template_argument_list_p)
9007 {
9008 if (!token->error_reported)
9009 {
9010 error_at (token->location, "lambda-expression in template-argument");
9011 token->error_reported = true;
9012 }
9013 ok = false;
9014 }
9015
9016 /* We may be in the middle of deferred access check. Disable
9017 it now. */
9018 push_deferring_access_checks (dk_no_deferred);
9019
9020 cp_parser_lambda_introducer (parser, lambda_expr);
9021
9022 type = begin_lambda_type (lambda_expr);
9023 if (type == error_mark_node)
9024 return error_mark_node;
9025
9026 record_lambda_scope (lambda_expr);
9027
9028 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9029 determine_visibility (TYPE_NAME (type));
9030
9031 /* Now that we've started the type, add the capture fields for any
9032 explicit captures. */
9033 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9034
9035 {
9036 /* Inside the class, surrounding template-parameter-lists do not apply. */
9037 unsigned int saved_num_template_parameter_lists
9038 = parser->num_template_parameter_lists;
9039 unsigned char in_statement = parser->in_statement;
9040 bool in_switch_statement_p = parser->in_switch_statement_p;
9041 bool fully_implicit_function_template_p
9042 = parser->fully_implicit_function_template_p;
9043 tree implicit_template_parms = parser->implicit_template_parms;
9044 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9045 bool auto_is_implicit_function_template_parm_p
9046 = parser->auto_is_implicit_function_template_parm_p;
9047
9048 parser->num_template_parameter_lists = 0;
9049 parser->in_statement = 0;
9050 parser->in_switch_statement_p = false;
9051 parser->fully_implicit_function_template_p = false;
9052 parser->implicit_template_parms = 0;
9053 parser->implicit_template_scope = 0;
9054 parser->auto_is_implicit_function_template_parm_p = false;
9055
9056 /* By virtue of defining a local class, a lambda expression has access to
9057 the private variables of enclosing classes. */
9058
9059 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9060
9061 if (ok)
9062 {
9063 if (!cp_parser_error_occurred (parser)
9064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9065 && cp_parser_start_tentative_firewall (parser))
9066 start = token;
9067 cp_parser_lambda_body (parser, lambda_expr);
9068 }
9069 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9070 {
9071 if (cp_parser_skip_to_closing_brace (parser))
9072 cp_lexer_consume_token (parser->lexer);
9073 }
9074
9075 /* The capture list was built up in reverse order; fix that now. */
9076 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9077 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9078
9079 if (ok)
9080 maybe_add_lambda_conv_op (type);
9081
9082 type = finish_struct (type, /*attributes=*/NULL_TREE);
9083
9084 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9085 parser->in_statement = in_statement;
9086 parser->in_switch_statement_p = in_switch_statement_p;
9087 parser->fully_implicit_function_template_p
9088 = fully_implicit_function_template_p;
9089 parser->implicit_template_parms = implicit_template_parms;
9090 parser->implicit_template_scope = implicit_template_scope;
9091 parser->auto_is_implicit_function_template_parm_p
9092 = auto_is_implicit_function_template_parm_p;
9093 }
9094
9095 pop_deferring_access_checks ();
9096
9097 /* This field is only used during parsing of the lambda. */
9098 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9099
9100 /* This lambda shouldn't have any proxies left at this point. */
9101 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9102 /* And now that we're done, push proxies for an enclosing lambda. */
9103 insert_pending_capture_proxies ();
9104
9105 if (ok)
9106 lambda_expr = build_lambda_object (lambda_expr);
9107 else
9108 lambda_expr = error_mark_node;
9109
9110 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9111
9112 return lambda_expr;
9113 }
9114
9115 /* Parse the beginning of a lambda expression.
9116
9117 lambda-introducer:
9118 [ lambda-capture [opt] ]
9119
9120 LAMBDA_EXPR is the current representation of the lambda expression. */
9121
9122 static void
9123 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9124 {
9125 /* Need commas after the first capture. */
9126 bool first = true;
9127
9128 /* Eat the leading `['. */
9129 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9130
9131 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9132 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9133 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9134 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9135 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9136 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9137
9138 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9139 {
9140 cp_lexer_consume_token (parser->lexer);
9141 first = false;
9142 }
9143
9144 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9145 {
9146 cp_token* capture_token;
9147 tree capture_id;
9148 tree capture_init_expr;
9149 cp_id_kind idk = CP_ID_KIND_NONE;
9150 bool explicit_init_p = false;
9151
9152 enum capture_kind_type
9153 {
9154 BY_COPY,
9155 BY_REFERENCE
9156 };
9157 enum capture_kind_type capture_kind = BY_COPY;
9158
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9160 {
9161 error ("expected end of capture-list");
9162 return;
9163 }
9164
9165 if (first)
9166 first = false;
9167 else
9168 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9169
9170 /* Possibly capture `this'. */
9171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9172 {
9173 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9174 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9175 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9176 "with by-copy capture default");
9177 cp_lexer_consume_token (parser->lexer);
9178 add_capture (lambda_expr,
9179 /*id=*/this_identifier,
9180 /*initializer=*/finish_this_expr(),
9181 /*by_reference_p=*/false,
9182 explicit_init_p);
9183 continue;
9184 }
9185
9186 /* Remember whether we want to capture as a reference or not. */
9187 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9188 {
9189 capture_kind = BY_REFERENCE;
9190 cp_lexer_consume_token (parser->lexer);
9191 }
9192
9193 /* Get the identifier. */
9194 capture_token = cp_lexer_peek_token (parser->lexer);
9195 capture_id = cp_parser_identifier (parser);
9196
9197 if (capture_id == error_mark_node)
9198 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9199 delimiters, but I modified this to stop on unnested ']' as well. It
9200 was already changed to stop on unnested '}', so the
9201 "closing_parenthesis" name is no more misleading with my change. */
9202 {
9203 cp_parser_skip_to_closing_parenthesis (parser,
9204 /*recovering=*/true,
9205 /*or_comma=*/true,
9206 /*consume_paren=*/true);
9207 break;
9208 }
9209
9210 /* Find the initializer for this capture. */
9211 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9212 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9213 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9214 {
9215 bool direct, non_constant;
9216 /* An explicit initializer exists. */
9217 if (cxx_dialect < cxx14)
9218 pedwarn (input_location, 0,
9219 "lambda capture initializers "
9220 "only available with -std=c++14 or -std=gnu++14");
9221 capture_init_expr = cp_parser_initializer (parser, &direct,
9222 &non_constant);
9223 explicit_init_p = true;
9224 if (capture_init_expr == NULL_TREE)
9225 {
9226 error ("empty initializer for lambda init-capture");
9227 capture_init_expr = error_mark_node;
9228 }
9229 }
9230 else
9231 {
9232 const char* error_msg;
9233
9234 /* Turn the identifier into an id-expression. */
9235 capture_init_expr
9236 = cp_parser_lookup_name_simple (parser, capture_id,
9237 capture_token->location);
9238
9239 if (capture_init_expr == error_mark_node)
9240 {
9241 unqualified_name_lookup_error (capture_id);
9242 continue;
9243 }
9244 else if (DECL_P (capture_init_expr)
9245 && (!VAR_P (capture_init_expr)
9246 && TREE_CODE (capture_init_expr) != PARM_DECL))
9247 {
9248 error_at (capture_token->location,
9249 "capture of non-variable %qD ",
9250 capture_init_expr);
9251 inform (0, "%q+#D declared here", capture_init_expr);
9252 continue;
9253 }
9254 if (VAR_P (capture_init_expr)
9255 && decl_storage_duration (capture_init_expr) != dk_auto)
9256 {
9257 if (pedwarn (capture_token->location, 0, "capture of variable "
9258 "%qD with non-automatic storage duration",
9259 capture_init_expr))
9260 inform (0, "%q+#D declared here", capture_init_expr);
9261 continue;
9262 }
9263
9264 capture_init_expr
9265 = finish_id_expression
9266 (capture_id,
9267 capture_init_expr,
9268 parser->scope,
9269 &idk,
9270 /*integral_constant_expression_p=*/false,
9271 /*allow_non_integral_constant_expression_p=*/false,
9272 /*non_integral_constant_expression_p=*/NULL,
9273 /*template_p=*/false,
9274 /*done=*/true,
9275 /*address_p=*/false,
9276 /*template_arg_p=*/false,
9277 &error_msg,
9278 capture_token->location);
9279
9280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9281 {
9282 cp_lexer_consume_token (parser->lexer);
9283 capture_init_expr = make_pack_expansion (capture_init_expr);
9284 }
9285 else
9286 check_for_bare_parameter_packs (capture_init_expr);
9287 }
9288
9289 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9290 && !explicit_init_p)
9291 {
9292 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9293 && capture_kind == BY_COPY)
9294 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9295 "of %qD redundant with by-copy capture default",
9296 capture_id);
9297 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9298 && capture_kind == BY_REFERENCE)
9299 pedwarn (capture_token->location, 0, "explicit by-reference "
9300 "capture of %qD redundant with by-reference capture "
9301 "default", capture_id);
9302 }
9303
9304 add_capture (lambda_expr,
9305 capture_id,
9306 capture_init_expr,
9307 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9308 explicit_init_p);
9309 }
9310
9311 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9312 }
9313
9314 /* Parse the (optional) middle of a lambda expression.
9315
9316 lambda-declarator:
9317 < template-parameter-list [opt] >
9318 ( parameter-declaration-clause [opt] )
9319 attribute-specifier [opt]
9320 mutable [opt]
9321 exception-specification [opt]
9322 lambda-return-type-clause [opt]
9323
9324 LAMBDA_EXPR is the current representation of the lambda expression. */
9325
9326 static bool
9327 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9328 {
9329 /* 5.1.1.4 of the standard says:
9330 If a lambda-expression does not include a lambda-declarator, it is as if
9331 the lambda-declarator were ().
9332 This means an empty parameter list, no attributes, and no exception
9333 specification. */
9334 tree param_list = void_list_node;
9335 tree attributes = NULL_TREE;
9336 tree exception_spec = NULL_TREE;
9337 tree template_param_list = NULL_TREE;
9338
9339 /* The template-parameter-list is optional, but must begin with
9340 an opening angle if present. */
9341 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9342 {
9343 if (cxx_dialect < cxx14)
9344 pedwarn (parser->lexer->next_token->location, 0,
9345 "lambda templates are only available with "
9346 "-std=c++14 or -std=gnu++14");
9347
9348 cp_lexer_consume_token (parser->lexer);
9349
9350 template_param_list = cp_parser_template_parameter_list (parser);
9351
9352 cp_parser_skip_to_end_of_template_parameter_list (parser);
9353
9354 /* We just processed one more parameter list. */
9355 ++parser->num_template_parameter_lists;
9356 }
9357
9358 /* The parameter-declaration-clause is optional (unless
9359 template-parameter-list was given), but must begin with an
9360 opening parenthesis if present. */
9361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9362 {
9363 cp_lexer_consume_token (parser->lexer);
9364
9365 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9366
9367 /* Parse parameters. */
9368 param_list = cp_parser_parameter_declaration_clause (parser);
9369
9370 /* Default arguments shall not be specified in the
9371 parameter-declaration-clause of a lambda-declarator. */
9372 for (tree t = param_list; t; t = TREE_CHAIN (t))
9373 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9374 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9375 "default argument specified for lambda parameter");
9376
9377 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9378
9379 attributes = cp_parser_attributes_opt (parser);
9380
9381 /* Parse optional `mutable' keyword. */
9382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9383 {
9384 cp_lexer_consume_token (parser->lexer);
9385 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9386 }
9387
9388 /* Parse optional exception specification. */
9389 exception_spec = cp_parser_exception_specification_opt (parser);
9390
9391 /* Parse optional trailing return type. */
9392 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9393 {
9394 cp_lexer_consume_token (parser->lexer);
9395 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9396 = cp_parser_trailing_type_id (parser);
9397 }
9398
9399 /* The function parameters must be in scope all the way until after the
9400 trailing-return-type in case of decltype. */
9401 pop_bindings_and_leave_scope ();
9402 }
9403 else if (template_param_list != NULL_TREE) // generate diagnostic
9404 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9405
9406 /* Create the function call operator.
9407
9408 Messing with declarators like this is no uglier than building up the
9409 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9410 other code. */
9411 {
9412 cp_decl_specifier_seq return_type_specs;
9413 cp_declarator* declarator;
9414 tree fco;
9415 int quals;
9416 void *p;
9417
9418 clear_decl_specs (&return_type_specs);
9419 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9420 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9421 else
9422 /* Maybe we will deduce the return type later. */
9423 return_type_specs.type = make_auto ();
9424
9425 p = obstack_alloc (&declarator_obstack, 0);
9426
9427 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9428 sfk_none);
9429
9430 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9431 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9432 declarator = make_call_declarator (declarator, param_list, quals,
9433 VIRT_SPEC_UNSPECIFIED,
9434 REF_QUAL_NONE,
9435 exception_spec,
9436 /*late_return_type=*/NULL_TREE);
9437 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9438
9439 fco = grokmethod (&return_type_specs,
9440 declarator,
9441 attributes);
9442 if (fco != error_mark_node)
9443 {
9444 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9445 DECL_ARTIFICIAL (fco) = 1;
9446 /* Give the object parameter a different name. */
9447 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9448 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9449 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9450 }
9451 if (template_param_list)
9452 {
9453 fco = finish_member_template_decl (fco);
9454 finish_template_decl (template_param_list);
9455 --parser->num_template_parameter_lists;
9456 }
9457 else if (parser->fully_implicit_function_template_p)
9458 fco = finish_fully_implicit_template (parser, fco);
9459
9460 finish_member_declaration (fco);
9461
9462 obstack_free (&declarator_obstack, p);
9463
9464 return (fco != error_mark_node);
9465 }
9466 }
9467
9468 /* Parse the body of a lambda expression, which is simply
9469
9470 compound-statement
9471
9472 but which requires special handling.
9473 LAMBDA_EXPR is the current representation of the lambda expression. */
9474
9475 static void
9476 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9477 {
9478 bool nested = (current_function_decl != NULL_TREE);
9479 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9480 if (nested)
9481 push_function_context ();
9482 else
9483 /* Still increment function_depth so that we don't GC in the
9484 middle of an expression. */
9485 ++function_depth;
9486 /* Clear this in case we're in the middle of a default argument. */
9487 parser->local_variables_forbidden_p = false;
9488
9489 /* Finish the function call operator
9490 - class_specifier
9491 + late_parsing_for_member
9492 + function_definition_after_declarator
9493 + ctor_initializer_opt_and_function_body */
9494 {
9495 tree fco = lambda_function (lambda_expr);
9496 tree body;
9497 bool done = false;
9498 tree compound_stmt;
9499 tree cap;
9500
9501 /* Let the front end know that we are going to be defining this
9502 function. */
9503 start_preparsed_function (fco,
9504 NULL_TREE,
9505 SF_PRE_PARSED | SF_INCLASS_INLINE);
9506
9507 start_lambda_scope (fco);
9508 body = begin_function_body ();
9509
9510 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9511 goto out;
9512
9513 /* Push the proxies for any explicit captures. */
9514 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9515 cap = TREE_CHAIN (cap))
9516 build_capture_proxy (TREE_PURPOSE (cap));
9517
9518 compound_stmt = begin_compound_stmt (0);
9519
9520 /* 5.1.1.4 of the standard says:
9521 If a lambda-expression does not include a trailing-return-type, it
9522 is as if the trailing-return-type denotes the following type:
9523 * if the compound-statement is of the form
9524 { return attribute-specifier [opt] expression ; }
9525 the type of the returned expression after lvalue-to-rvalue
9526 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9527 (_conv.array_ 4.2), and function-to-pointer conversion
9528 (_conv.func_ 4.3);
9529 * otherwise, void. */
9530
9531 /* In a lambda that has neither a lambda-return-type-clause
9532 nor a deducible form, errors should be reported for return statements
9533 in the body. Since we used void as the placeholder return type, parsing
9534 the body as usual will give such desired behavior. */
9535 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9536 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9537 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9538 {
9539 tree expr = NULL_TREE;
9540 cp_id_kind idk = CP_ID_KIND_NONE;
9541
9542 /* Parse tentatively in case there's more after the initial return
9543 statement. */
9544 cp_parser_parse_tentatively (parser);
9545
9546 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9547
9548 expr = cp_parser_expression (parser, &idk);
9549
9550 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9551 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9552
9553 if (cp_parser_parse_definitely (parser))
9554 {
9555 if (!processing_template_decl)
9556 apply_deduced_return_type (fco, lambda_return_type (expr));
9557
9558 /* Will get error here if type not deduced yet. */
9559 finish_return_stmt (expr);
9560
9561 done = true;
9562 }
9563 }
9564
9565 if (!done)
9566 {
9567 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9568 cp_parser_label_declaration (parser);
9569 cp_parser_statement_seq_opt (parser, NULL_TREE);
9570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9571 }
9572
9573 finish_compound_stmt (compound_stmt);
9574
9575 out:
9576 finish_function_body (body);
9577 finish_lambda_scope ();
9578
9579 /* Finish the function and generate code for it if necessary. */
9580 tree fn = finish_function (/*inline*/2);
9581
9582 /* Only expand if the call op is not a template. */
9583 if (!DECL_TEMPLATE_INFO (fco))
9584 expand_or_defer_fn (fn);
9585 }
9586
9587 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9588 if (nested)
9589 pop_function_context();
9590 else
9591 --function_depth;
9592 }
9593
9594 /* Statements [gram.stmt.stmt] */
9595
9596 /* Parse a statement.
9597
9598 statement:
9599 labeled-statement
9600 expression-statement
9601 compound-statement
9602 selection-statement
9603 iteration-statement
9604 jump-statement
9605 declaration-statement
9606 try-block
9607
9608 C++11:
9609
9610 statement:
9611 labeled-statement
9612 attribute-specifier-seq (opt) expression-statement
9613 attribute-specifier-seq (opt) compound-statement
9614 attribute-specifier-seq (opt) selection-statement
9615 attribute-specifier-seq (opt) iteration-statement
9616 attribute-specifier-seq (opt) jump-statement
9617 declaration-statement
9618 attribute-specifier-seq (opt) try-block
9619
9620 TM Extension:
9621
9622 statement:
9623 atomic-statement
9624
9625 IN_COMPOUND is true when the statement is nested inside a
9626 cp_parser_compound_statement; this matters for certain pragmas.
9627
9628 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9629 is a (possibly labeled) if statement which is not enclosed in braces
9630 and has an else clause. This is used to implement -Wparentheses. */
9631
9632 static void
9633 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9634 bool in_compound, bool *if_p)
9635 {
9636 tree statement, std_attrs = NULL_TREE;
9637 cp_token *token;
9638 location_t statement_location, attrs_location;
9639
9640 restart:
9641 if (if_p != NULL)
9642 *if_p = false;
9643 /* There is no statement yet. */
9644 statement = NULL_TREE;
9645
9646 saved_token_sentinel saved_tokens (parser->lexer);
9647 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9648 if (c_dialect_objc ())
9649 /* In obj-c++, seeing '[[' might be the either the beginning of
9650 c++11 attributes, or a nested objc-message-expression. So
9651 let's parse the c++11 attributes tentatively. */
9652 cp_parser_parse_tentatively (parser);
9653 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9654 if (c_dialect_objc ())
9655 {
9656 if (!cp_parser_parse_definitely (parser))
9657 std_attrs = NULL_TREE;
9658 }
9659
9660 /* Peek at the next token. */
9661 token = cp_lexer_peek_token (parser->lexer);
9662 /* Remember the location of the first token in the statement. */
9663 statement_location = token->location;
9664 /* If this is a keyword, then that will often determine what kind of
9665 statement we have. */
9666 if (token->type == CPP_KEYWORD)
9667 {
9668 enum rid keyword = token->keyword;
9669
9670 switch (keyword)
9671 {
9672 case RID_CASE:
9673 case RID_DEFAULT:
9674 /* Looks like a labeled-statement with a case label.
9675 Parse the label, and then use tail recursion to parse
9676 the statement. */
9677 cp_parser_label_for_labeled_statement (parser, std_attrs);
9678 goto restart;
9679
9680 case RID_IF:
9681 case RID_SWITCH:
9682 statement = cp_parser_selection_statement (parser, if_p);
9683 break;
9684
9685 case RID_WHILE:
9686 case RID_DO:
9687 case RID_FOR:
9688 statement = cp_parser_iteration_statement (parser, false);
9689 break;
9690
9691 case RID_CILK_FOR:
9692 if (!flag_cilkplus)
9693 {
9694 error_at (cp_lexer_peek_token (parser->lexer)->location,
9695 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9696 cp_lexer_consume_token (parser->lexer);
9697 statement = error_mark_node;
9698 }
9699 else
9700 statement = cp_parser_cilk_for (parser, integer_zero_node);
9701 break;
9702
9703 case RID_BREAK:
9704 case RID_CONTINUE:
9705 case RID_RETURN:
9706 case RID_GOTO:
9707 statement = cp_parser_jump_statement (parser);
9708 break;
9709
9710 case RID_CILK_SYNC:
9711 cp_lexer_consume_token (parser->lexer);
9712 if (flag_cilkplus)
9713 {
9714 tree sync_expr = build_cilk_sync ();
9715 SET_EXPR_LOCATION (sync_expr,
9716 token->location);
9717 statement = finish_expr_stmt (sync_expr);
9718 }
9719 else
9720 {
9721 error_at (token->location, "-fcilkplus must be enabled to use"
9722 " %<_Cilk_sync%>");
9723 statement = error_mark_node;
9724 }
9725 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9726 break;
9727
9728 /* Objective-C++ exception-handling constructs. */
9729 case RID_AT_TRY:
9730 case RID_AT_CATCH:
9731 case RID_AT_FINALLY:
9732 case RID_AT_SYNCHRONIZED:
9733 case RID_AT_THROW:
9734 statement = cp_parser_objc_statement (parser);
9735 break;
9736
9737 case RID_TRY:
9738 statement = cp_parser_try_block (parser);
9739 break;
9740
9741 case RID_NAMESPACE:
9742 /* This must be a namespace alias definition. */
9743 cp_parser_declaration_statement (parser);
9744 return;
9745
9746 case RID_TRANSACTION_ATOMIC:
9747 case RID_TRANSACTION_RELAXED:
9748 statement = cp_parser_transaction (parser, keyword);
9749 break;
9750 case RID_TRANSACTION_CANCEL:
9751 statement = cp_parser_transaction_cancel (parser);
9752 break;
9753
9754 default:
9755 /* It might be a keyword like `int' that can start a
9756 declaration-statement. */
9757 break;
9758 }
9759 }
9760 else if (token->type == CPP_NAME)
9761 {
9762 /* If the next token is a `:', then we are looking at a
9763 labeled-statement. */
9764 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9765 if (token->type == CPP_COLON)
9766 {
9767 /* Looks like a labeled-statement with an ordinary label.
9768 Parse the label, and then use tail recursion to parse
9769 the statement. */
9770
9771 cp_parser_label_for_labeled_statement (parser, std_attrs);
9772 goto restart;
9773 }
9774 }
9775 /* Anything that starts with a `{' must be a compound-statement. */
9776 else if (token->type == CPP_OPEN_BRACE)
9777 statement = cp_parser_compound_statement (parser, NULL, false, false);
9778 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9779 a statement all its own. */
9780 else if (token->type == CPP_PRAGMA)
9781 {
9782 /* Only certain OpenMP pragmas are attached to statements, and thus
9783 are considered statements themselves. All others are not. In
9784 the context of a compound, accept the pragma as a "statement" and
9785 return so that we can check for a close brace. Otherwise we
9786 require a real statement and must go back and read one. */
9787 if (in_compound)
9788 cp_parser_pragma (parser, pragma_compound);
9789 else if (!cp_parser_pragma (parser, pragma_stmt))
9790 goto restart;
9791 return;
9792 }
9793 else if (token->type == CPP_EOF)
9794 {
9795 cp_parser_error (parser, "expected statement");
9796 return;
9797 }
9798
9799 /* Everything else must be a declaration-statement or an
9800 expression-statement. Try for the declaration-statement
9801 first, unless we are looking at a `;', in which case we know that
9802 we have an expression-statement. */
9803 if (!statement)
9804 {
9805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9806 {
9807 if (std_attrs != NULL_TREE)
9808 {
9809 /* Attributes should be parsed as part of the the
9810 declaration, so let's un-parse them. */
9811 saved_tokens.rollback();
9812 std_attrs = NULL_TREE;
9813 }
9814
9815 cp_parser_parse_tentatively (parser);
9816 /* Try to parse the declaration-statement. */
9817 cp_parser_declaration_statement (parser);
9818 /* If that worked, we're done. */
9819 if (cp_parser_parse_definitely (parser))
9820 return;
9821 }
9822 /* Look for an expression-statement instead. */
9823 statement = cp_parser_expression_statement (parser, in_statement_expr);
9824 }
9825
9826 /* Set the line number for the statement. */
9827 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9828 SET_EXPR_LOCATION (statement, statement_location);
9829
9830 /* Note that for now, we don't do anything with c++11 statements
9831 parsed at this level. */
9832 if (std_attrs != NULL_TREE)
9833 warning_at (attrs_location,
9834 OPT_Wattributes,
9835 "attributes at the beginning of statement are ignored");
9836 }
9837
9838 /* Parse the label for a labeled-statement, i.e.
9839
9840 identifier :
9841 case constant-expression :
9842 default :
9843
9844 GNU Extension:
9845 case constant-expression ... constant-expression : statement
9846
9847 When a label is parsed without errors, the label is added to the
9848 parse tree by the finish_* functions, so this function doesn't
9849 have to return the label. */
9850
9851 static void
9852 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9853 {
9854 cp_token *token;
9855 tree label = NULL_TREE;
9856 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9857
9858 /* The next token should be an identifier. */
9859 token = cp_lexer_peek_token (parser->lexer);
9860 if (token->type != CPP_NAME
9861 && token->type != CPP_KEYWORD)
9862 {
9863 cp_parser_error (parser, "expected labeled-statement");
9864 return;
9865 }
9866
9867 parser->colon_corrects_to_scope_p = false;
9868 switch (token->keyword)
9869 {
9870 case RID_CASE:
9871 {
9872 tree expr, expr_hi;
9873 cp_token *ellipsis;
9874
9875 /* Consume the `case' token. */
9876 cp_lexer_consume_token (parser->lexer);
9877 /* Parse the constant-expression. */
9878 expr = cp_parser_constant_expression (parser);
9879 if (check_for_bare_parameter_packs (expr))
9880 expr = error_mark_node;
9881
9882 ellipsis = cp_lexer_peek_token (parser->lexer);
9883 if (ellipsis->type == CPP_ELLIPSIS)
9884 {
9885 /* Consume the `...' token. */
9886 cp_lexer_consume_token (parser->lexer);
9887 expr_hi = cp_parser_constant_expression (parser);
9888 if (check_for_bare_parameter_packs (expr_hi))
9889 expr_hi = error_mark_node;
9890
9891 /* We don't need to emit warnings here, as the common code
9892 will do this for us. */
9893 }
9894 else
9895 expr_hi = NULL_TREE;
9896
9897 if (parser->in_switch_statement_p)
9898 finish_case_label (token->location, expr, expr_hi);
9899 else
9900 error_at (token->location,
9901 "case label %qE not within a switch statement",
9902 expr);
9903 }
9904 break;
9905
9906 case RID_DEFAULT:
9907 /* Consume the `default' token. */
9908 cp_lexer_consume_token (parser->lexer);
9909
9910 if (parser->in_switch_statement_p)
9911 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9912 else
9913 error_at (token->location, "case label not within a switch statement");
9914 break;
9915
9916 default:
9917 /* Anything else must be an ordinary label. */
9918 label = finish_label_stmt (cp_parser_identifier (parser));
9919 break;
9920 }
9921
9922 /* Require the `:' token. */
9923 cp_parser_require (parser, CPP_COLON, RT_COLON);
9924
9925 /* An ordinary label may optionally be followed by attributes.
9926 However, this is only permitted if the attributes are then
9927 followed by a semicolon. This is because, for backward
9928 compatibility, when parsing
9929 lab: __attribute__ ((unused)) int i;
9930 we want the attribute to attach to "i", not "lab". */
9931 if (label != NULL_TREE
9932 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9933 {
9934 tree attrs;
9935 cp_parser_parse_tentatively (parser);
9936 attrs = cp_parser_gnu_attributes_opt (parser);
9937 if (attrs == NULL_TREE
9938 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9939 cp_parser_abort_tentative_parse (parser);
9940 else if (!cp_parser_parse_definitely (parser))
9941 ;
9942 else
9943 attributes = chainon (attributes, attrs);
9944 }
9945
9946 if (attributes != NULL_TREE)
9947 cplus_decl_attributes (&label, attributes, 0);
9948
9949 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9950 }
9951
9952 /* Parse an expression-statement.
9953
9954 expression-statement:
9955 expression [opt] ;
9956
9957 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9958 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9959 indicates whether this expression-statement is part of an
9960 expression statement. */
9961
9962 static tree
9963 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9964 {
9965 tree statement = NULL_TREE;
9966 cp_token *token = cp_lexer_peek_token (parser->lexer);
9967
9968 /* If the next token is a ';', then there is no expression
9969 statement. */
9970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9971 {
9972 statement = cp_parser_expression (parser);
9973 if (statement == error_mark_node
9974 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9975 {
9976 cp_parser_skip_to_end_of_block_or_statement (parser);
9977 return error_mark_node;
9978 }
9979 }
9980
9981 /* Give a helpful message for "A<T>::type t;" and the like. */
9982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9983 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9984 {
9985 if (TREE_CODE (statement) == SCOPE_REF)
9986 error_at (token->location, "need %<typename%> before %qE because "
9987 "%qT is a dependent scope",
9988 statement, TREE_OPERAND (statement, 0));
9989 else if (is_overloaded_fn (statement)
9990 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9991 {
9992 /* A::A a; */
9993 tree fn = get_first_fn (statement);
9994 error_at (token->location,
9995 "%<%T::%D%> names the constructor, not the type",
9996 DECL_CONTEXT (fn), DECL_NAME (fn));
9997 }
9998 }
9999
10000 /* Consume the final `;'. */
10001 cp_parser_consume_semicolon_at_end_of_statement (parser);
10002
10003 if (in_statement_expr
10004 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10005 /* This is the final expression statement of a statement
10006 expression. */
10007 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10008 else if (statement)
10009 statement = finish_expr_stmt (statement);
10010
10011 return statement;
10012 }
10013
10014 /* Parse a compound-statement.
10015
10016 compound-statement:
10017 { statement-seq [opt] }
10018
10019 GNU extension:
10020
10021 compound-statement:
10022 { label-declaration-seq [opt] statement-seq [opt] }
10023
10024 label-declaration-seq:
10025 label-declaration
10026 label-declaration-seq label-declaration
10027
10028 Returns a tree representing the statement. */
10029
10030 static tree
10031 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10032 bool in_try, bool function_body)
10033 {
10034 tree compound_stmt;
10035
10036 /* Consume the `{'. */
10037 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10038 return error_mark_node;
10039 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10040 && !function_body && cxx_dialect < cxx14)
10041 pedwarn (input_location, OPT_Wpedantic,
10042 "compound-statement in constexpr function");
10043 /* Begin the compound-statement. */
10044 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10045 /* If the next keyword is `__label__' we have a label declaration. */
10046 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10047 cp_parser_label_declaration (parser);
10048 /* Parse an (optional) statement-seq. */
10049 cp_parser_statement_seq_opt (parser, in_statement_expr);
10050 /* Finish the compound-statement. */
10051 finish_compound_stmt (compound_stmt);
10052 /* Consume the `}'. */
10053 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10054
10055 return compound_stmt;
10056 }
10057
10058 /* Parse an (optional) statement-seq.
10059
10060 statement-seq:
10061 statement
10062 statement-seq [opt] statement */
10063
10064 static void
10065 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10066 {
10067 /* Scan statements until there aren't any more. */
10068 while (true)
10069 {
10070 cp_token *token = cp_lexer_peek_token (parser->lexer);
10071
10072 /* If we are looking at a `}', then we have run out of
10073 statements; the same is true if we have reached the end
10074 of file, or have stumbled upon a stray '@end'. */
10075 if (token->type == CPP_CLOSE_BRACE
10076 || token->type == CPP_EOF
10077 || token->type == CPP_PRAGMA_EOL
10078 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10079 break;
10080
10081 /* If we are in a compound statement and find 'else' then
10082 something went wrong. */
10083 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10084 {
10085 if (parser->in_statement & IN_IF_STMT)
10086 break;
10087 else
10088 {
10089 token = cp_lexer_consume_token (parser->lexer);
10090 error_at (token->location, "%<else%> without a previous %<if%>");
10091 }
10092 }
10093
10094 /* Parse the statement. */
10095 cp_parser_statement (parser, in_statement_expr, true, NULL);
10096 }
10097 }
10098
10099 /* Parse a selection-statement.
10100
10101 selection-statement:
10102 if ( condition ) statement
10103 if ( condition ) statement else statement
10104 switch ( condition ) statement
10105
10106 Returns the new IF_STMT or SWITCH_STMT.
10107
10108 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10109 is a (possibly labeled) if statement which is not enclosed in
10110 braces and has an else clause. This is used to implement
10111 -Wparentheses. */
10112
10113 static tree
10114 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10115 {
10116 cp_token *token;
10117 enum rid keyword;
10118
10119 if (if_p != NULL)
10120 *if_p = false;
10121
10122 /* Peek at the next token. */
10123 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10124
10125 /* See what kind of keyword it is. */
10126 keyword = token->keyword;
10127 switch (keyword)
10128 {
10129 case RID_IF:
10130 case RID_SWITCH:
10131 {
10132 tree statement;
10133 tree condition;
10134
10135 /* Look for the `('. */
10136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10137 {
10138 cp_parser_skip_to_end_of_statement (parser);
10139 return error_mark_node;
10140 }
10141
10142 /* Begin the selection-statement. */
10143 if (keyword == RID_IF)
10144 statement = begin_if_stmt ();
10145 else
10146 statement = begin_switch_stmt ();
10147
10148 /* Parse the condition. */
10149 condition = cp_parser_condition (parser);
10150 /* Look for the `)'. */
10151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10152 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10153 /*consume_paren=*/true);
10154
10155 if (keyword == RID_IF)
10156 {
10157 bool nested_if;
10158 unsigned char in_statement;
10159
10160 /* Add the condition. */
10161 finish_if_stmt_cond (condition, statement);
10162
10163 /* Parse the then-clause. */
10164 in_statement = parser->in_statement;
10165 parser->in_statement |= IN_IF_STMT;
10166 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10167 {
10168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10169 add_stmt (build_empty_stmt (loc));
10170 cp_lexer_consume_token (parser->lexer);
10171 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10172 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10173 "empty body in an %<if%> statement");
10174 nested_if = false;
10175 }
10176 else
10177 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10178 token->location, "if");
10179 parser->in_statement = in_statement;
10180
10181 finish_then_clause (statement);
10182
10183 /* If the next token is `else', parse the else-clause. */
10184 if (cp_lexer_next_token_is_keyword (parser->lexer,
10185 RID_ELSE))
10186 {
10187 /* Consume the `else' keyword. */
10188 location_t else_tok_loc
10189 = cp_lexer_consume_token (parser->lexer)->location;
10190 begin_else_clause (statement);
10191 /* Parse the else-clause. */
10192 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10193 {
10194 location_t loc;
10195 loc = cp_lexer_peek_token (parser->lexer)->location;
10196 warning_at (loc,
10197 OPT_Wempty_body, "suggest braces around "
10198 "empty body in an %<else%> statement");
10199 add_stmt (build_empty_stmt (loc));
10200 cp_lexer_consume_token (parser->lexer);
10201 }
10202 else
10203 cp_parser_implicitly_scoped_statement (parser, NULL,
10204 else_tok_loc, "else");
10205
10206 finish_else_clause (statement);
10207
10208 /* If we are currently parsing a then-clause, then
10209 IF_P will not be NULL. We set it to true to
10210 indicate that this if statement has an else clause.
10211 This may trigger the Wparentheses warning below
10212 when we get back up to the parent if statement. */
10213 if (if_p != NULL)
10214 *if_p = true;
10215 }
10216 else
10217 {
10218 /* This if statement does not have an else clause. If
10219 NESTED_IF is true, then the then-clause is an if
10220 statement which does have an else clause. We warn
10221 about the potential ambiguity. */
10222 if (nested_if)
10223 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10224 "suggest explicit braces to avoid ambiguous"
10225 " %<else%>");
10226 }
10227
10228 /* Now we're all done with the if-statement. */
10229 finish_if_stmt (statement);
10230 }
10231 else
10232 {
10233 bool in_switch_statement_p;
10234 unsigned char in_statement;
10235
10236 /* Add the condition. */
10237 finish_switch_cond (condition, statement);
10238
10239 /* Parse the body of the switch-statement. */
10240 in_switch_statement_p = parser->in_switch_statement_p;
10241 in_statement = parser->in_statement;
10242 parser->in_switch_statement_p = true;
10243 parser->in_statement |= IN_SWITCH_STMT;
10244 cp_parser_implicitly_scoped_statement (parser, NULL,
10245 0, "switch");
10246 parser->in_switch_statement_p = in_switch_statement_p;
10247 parser->in_statement = in_statement;
10248
10249 /* Now we're all done with the switch-statement. */
10250 finish_switch_stmt (statement);
10251 }
10252
10253 return statement;
10254 }
10255 break;
10256
10257 default:
10258 cp_parser_error (parser, "expected selection-statement");
10259 return error_mark_node;
10260 }
10261 }
10262
10263 /* Parse a condition.
10264
10265 condition:
10266 expression
10267 type-specifier-seq declarator = initializer-clause
10268 type-specifier-seq declarator braced-init-list
10269
10270 GNU Extension:
10271
10272 condition:
10273 type-specifier-seq declarator asm-specification [opt]
10274 attributes [opt] = assignment-expression
10275
10276 Returns the expression that should be tested. */
10277
10278 static tree
10279 cp_parser_condition (cp_parser* parser)
10280 {
10281 cp_decl_specifier_seq type_specifiers;
10282 const char *saved_message;
10283 int declares_class_or_enum;
10284
10285 /* Try the declaration first. */
10286 cp_parser_parse_tentatively (parser);
10287 /* New types are not allowed in the type-specifier-seq for a
10288 condition. */
10289 saved_message = parser->type_definition_forbidden_message;
10290 parser->type_definition_forbidden_message
10291 = G_("types may not be defined in conditions");
10292 /* Parse the type-specifier-seq. */
10293 cp_parser_decl_specifier_seq (parser,
10294 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10295 &type_specifiers,
10296 &declares_class_or_enum);
10297 /* Restore the saved message. */
10298 parser->type_definition_forbidden_message = saved_message;
10299 /* If all is well, we might be looking at a declaration. */
10300 if (!cp_parser_error_occurred (parser))
10301 {
10302 tree decl;
10303 tree asm_specification;
10304 tree attributes;
10305 cp_declarator *declarator;
10306 tree initializer = NULL_TREE;
10307
10308 /* Parse the declarator. */
10309 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10310 /*ctor_dtor_or_conv_p=*/NULL,
10311 /*parenthesized_p=*/NULL,
10312 /*member_p=*/false,
10313 /*friend_p=*/false);
10314 /* Parse the attributes. */
10315 attributes = cp_parser_attributes_opt (parser);
10316 /* Parse the asm-specification. */
10317 asm_specification = cp_parser_asm_specification_opt (parser);
10318 /* If the next token is not an `=' or '{', then we might still be
10319 looking at an expression. For example:
10320
10321 if (A(a).x)
10322
10323 looks like a decl-specifier-seq and a declarator -- but then
10324 there is no `=', so this is an expression. */
10325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10326 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10327 cp_parser_simulate_error (parser);
10328
10329 /* If we did see an `=' or '{', then we are looking at a declaration
10330 for sure. */
10331 if (cp_parser_parse_definitely (parser))
10332 {
10333 tree pushed_scope;
10334 bool non_constant_p;
10335 bool flags = LOOKUP_ONLYCONVERTING;
10336
10337 /* Create the declaration. */
10338 decl = start_decl (declarator, &type_specifiers,
10339 /*initialized_p=*/true,
10340 attributes, /*prefix_attributes=*/NULL_TREE,
10341 &pushed_scope);
10342
10343 /* Parse the initializer. */
10344 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10345 {
10346 initializer = cp_parser_braced_list (parser, &non_constant_p);
10347 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10348 flags = 0;
10349 }
10350 else
10351 {
10352 /* Consume the `='. */
10353 cp_parser_require (parser, CPP_EQ, RT_EQ);
10354 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10355 }
10356 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10357 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10358
10359 /* Process the initializer. */
10360 cp_finish_decl (decl,
10361 initializer, !non_constant_p,
10362 asm_specification,
10363 flags);
10364
10365 if (pushed_scope)
10366 pop_scope (pushed_scope);
10367
10368 return convert_from_reference (decl);
10369 }
10370 }
10371 /* If we didn't even get past the declarator successfully, we are
10372 definitely not looking at a declaration. */
10373 else
10374 cp_parser_abort_tentative_parse (parser);
10375
10376 /* Otherwise, we are looking at an expression. */
10377 return cp_parser_expression (parser);
10378 }
10379
10380 /* Parses a for-statement or range-for-statement until the closing ')',
10381 not included. */
10382
10383 static tree
10384 cp_parser_for (cp_parser *parser, bool ivdep)
10385 {
10386 tree init, scope, decl;
10387 bool is_range_for;
10388
10389 /* Begin the for-statement. */
10390 scope = begin_for_scope (&init);
10391
10392 /* Parse the initialization. */
10393 is_range_for = cp_parser_for_init_statement (parser, &decl);
10394
10395 if (is_range_for)
10396 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10397 else
10398 return cp_parser_c_for (parser, scope, init, ivdep);
10399 }
10400
10401 static tree
10402 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10403 {
10404 /* Normal for loop */
10405 tree condition = NULL_TREE;
10406 tree expression = NULL_TREE;
10407 tree stmt;
10408
10409 stmt = begin_for_stmt (scope, init);
10410 /* The for-init-statement has already been parsed in
10411 cp_parser_for_init_statement, so no work is needed here. */
10412 finish_for_init_stmt (stmt);
10413
10414 /* If there's a condition, process it. */
10415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10416 condition = cp_parser_condition (parser);
10417 else if (ivdep)
10418 {
10419 cp_parser_error (parser, "missing loop condition in loop with "
10420 "%<GCC ivdep%> pragma");
10421 condition = error_mark_node;
10422 }
10423 finish_for_cond (condition, stmt, ivdep);
10424 /* Look for the `;'. */
10425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10426
10427 /* If there's an expression, process it. */
10428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10429 expression = cp_parser_expression (parser);
10430 finish_for_expr (expression, stmt);
10431
10432 return stmt;
10433 }
10434
10435 /* Tries to parse a range-based for-statement:
10436
10437 range-based-for:
10438 decl-specifier-seq declarator : expression
10439
10440 The decl-specifier-seq declarator and the `:' are already parsed by
10441 cp_parser_for_init_statement. If processing_template_decl it returns a
10442 newly created RANGE_FOR_STMT; if not, it is converted to a
10443 regular FOR_STMT. */
10444
10445 static tree
10446 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10447 bool ivdep)
10448 {
10449 tree stmt, range_expr;
10450
10451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10452 {
10453 bool expr_non_constant_p;
10454 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10455 }
10456 else
10457 range_expr = cp_parser_expression (parser);
10458
10459 /* If in template, STMT is converted to a normal for-statement
10460 at instantiation. If not, it is done just ahead. */
10461 if (processing_template_decl)
10462 {
10463 if (check_for_bare_parameter_packs (range_expr))
10464 range_expr = error_mark_node;
10465 stmt = begin_range_for_stmt (scope, init);
10466 if (ivdep)
10467 RANGE_FOR_IVDEP (stmt) = 1;
10468 finish_range_for_decl (stmt, range_decl, range_expr);
10469 if (!type_dependent_expression_p (range_expr)
10470 /* do_auto_deduction doesn't mess with template init-lists. */
10471 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10472 do_range_for_auto_deduction (range_decl, range_expr);
10473 }
10474 else
10475 {
10476 stmt = begin_for_stmt (scope, init);
10477 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10478 }
10479 return stmt;
10480 }
10481
10482 /* Subroutine of cp_convert_range_for: given the initializer expression,
10483 builds up the range temporary. */
10484
10485 static tree
10486 build_range_temp (tree range_expr)
10487 {
10488 tree range_type, range_temp;
10489
10490 /* Find out the type deduced by the declaration
10491 `auto &&__range = range_expr'. */
10492 range_type = cp_build_reference_type (make_auto (), true);
10493 range_type = do_auto_deduction (range_type, range_expr,
10494 type_uses_auto (range_type));
10495
10496 /* Create the __range variable. */
10497 range_temp = build_decl (input_location, VAR_DECL,
10498 get_identifier ("__for_range"), range_type);
10499 TREE_USED (range_temp) = 1;
10500 DECL_ARTIFICIAL (range_temp) = 1;
10501
10502 return range_temp;
10503 }
10504
10505 /* Used by cp_parser_range_for in template context: we aren't going to
10506 do a full conversion yet, but we still need to resolve auto in the
10507 type of the for-range-declaration if present. This is basically
10508 a shortcut version of cp_convert_range_for. */
10509
10510 static void
10511 do_range_for_auto_deduction (tree decl, tree range_expr)
10512 {
10513 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10514 if (auto_node)
10515 {
10516 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10517 range_temp = convert_from_reference (build_range_temp (range_expr));
10518 iter_type = (cp_parser_perform_range_for_lookup
10519 (range_temp, &begin_dummy, &end_dummy));
10520 if (iter_type)
10521 {
10522 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10523 iter_type);
10524 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10525 tf_warning_or_error);
10526 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10527 iter_decl, auto_node);
10528 }
10529 }
10530 }
10531
10532 /* Converts a range-based for-statement into a normal
10533 for-statement, as per the definition.
10534
10535 for (RANGE_DECL : RANGE_EXPR)
10536 BLOCK
10537
10538 should be equivalent to:
10539
10540 {
10541 auto &&__range = RANGE_EXPR;
10542 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10543 __begin != __end;
10544 ++__begin)
10545 {
10546 RANGE_DECL = *__begin;
10547 BLOCK
10548 }
10549 }
10550
10551 If RANGE_EXPR is an array:
10552 BEGIN_EXPR = __range
10553 END_EXPR = __range + ARRAY_SIZE(__range)
10554 Else if RANGE_EXPR has a member 'begin' or 'end':
10555 BEGIN_EXPR = __range.begin()
10556 END_EXPR = __range.end()
10557 Else:
10558 BEGIN_EXPR = begin(__range)
10559 END_EXPR = end(__range);
10560
10561 If __range has a member 'begin' but not 'end', or vice versa, we must
10562 still use the second alternative (it will surely fail, however).
10563 When calling begin()/end() in the third alternative we must use
10564 argument dependent lookup, but always considering 'std' as an associated
10565 namespace. */
10566
10567 tree
10568 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10569 bool ivdep)
10570 {
10571 tree begin, end;
10572 tree iter_type, begin_expr, end_expr;
10573 tree condition, expression;
10574
10575 if (range_decl == error_mark_node || range_expr == error_mark_node)
10576 /* If an error happened previously do nothing or else a lot of
10577 unhelpful errors would be issued. */
10578 begin_expr = end_expr = iter_type = error_mark_node;
10579 else
10580 {
10581 tree range_temp;
10582
10583 if (TREE_CODE (range_expr) == VAR_DECL
10584 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10585 /* Can't bind a reference to an array of runtime bound. */
10586 range_temp = range_expr;
10587 else
10588 {
10589 range_temp = build_range_temp (range_expr);
10590 pushdecl (range_temp);
10591 cp_finish_decl (range_temp, range_expr,
10592 /*is_constant_init*/false, NULL_TREE,
10593 LOOKUP_ONLYCONVERTING);
10594 range_temp = convert_from_reference (range_temp);
10595 }
10596 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10597 &begin_expr, &end_expr);
10598 }
10599
10600 /* The new for initialization statement. */
10601 begin = build_decl (input_location, VAR_DECL,
10602 get_identifier ("__for_begin"), iter_type);
10603 TREE_USED (begin) = 1;
10604 DECL_ARTIFICIAL (begin) = 1;
10605 pushdecl (begin);
10606 cp_finish_decl (begin, begin_expr,
10607 /*is_constant_init*/false, NULL_TREE,
10608 LOOKUP_ONLYCONVERTING);
10609
10610 end = build_decl (input_location, VAR_DECL,
10611 get_identifier ("__for_end"), iter_type);
10612 TREE_USED (end) = 1;
10613 DECL_ARTIFICIAL (end) = 1;
10614 pushdecl (end);
10615 cp_finish_decl (end, end_expr,
10616 /*is_constant_init*/false, NULL_TREE,
10617 LOOKUP_ONLYCONVERTING);
10618
10619 finish_for_init_stmt (statement);
10620
10621 /* The new for condition. */
10622 condition = build_x_binary_op (input_location, NE_EXPR,
10623 begin, ERROR_MARK,
10624 end, ERROR_MARK,
10625 NULL, tf_warning_or_error);
10626 finish_for_cond (condition, statement, ivdep);
10627
10628 /* The new increment expression. */
10629 expression = finish_unary_op_expr (input_location,
10630 PREINCREMENT_EXPR, begin,
10631 tf_warning_or_error);
10632 finish_for_expr (expression, statement);
10633
10634 /* The declaration is initialized with *__begin inside the loop body. */
10635 cp_finish_decl (range_decl,
10636 build_x_indirect_ref (input_location, begin, RO_NULL,
10637 tf_warning_or_error),
10638 /*is_constant_init*/false, NULL_TREE,
10639 LOOKUP_ONLYCONVERTING);
10640
10641 return statement;
10642 }
10643
10644 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10645 We need to solve both at the same time because the method used
10646 depends on the existence of members begin or end.
10647 Returns the type deduced for the iterator expression. */
10648
10649 static tree
10650 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10651 {
10652 if (error_operand_p (range))
10653 {
10654 *begin = *end = error_mark_node;
10655 return error_mark_node;
10656 }
10657
10658 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10659 {
10660 error ("range-based %<for%> expression of type %qT "
10661 "has incomplete type", TREE_TYPE (range));
10662 *begin = *end = error_mark_node;
10663 return error_mark_node;
10664 }
10665 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10666 {
10667 /* If RANGE is an array, we will use pointer arithmetic. */
10668 *begin = range;
10669 *end = build_binary_op (input_location, PLUS_EXPR,
10670 range,
10671 array_type_nelts_top (TREE_TYPE (range)),
10672 0);
10673 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10674 }
10675 else
10676 {
10677 /* If it is not an array, we must do a bit of magic. */
10678 tree id_begin, id_end;
10679 tree member_begin, member_end;
10680
10681 *begin = *end = error_mark_node;
10682
10683 id_begin = get_identifier ("begin");
10684 id_end = get_identifier ("end");
10685 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10686 /*protect=*/2, /*want_type=*/false,
10687 tf_warning_or_error);
10688 member_end = lookup_member (TREE_TYPE (range), id_end,
10689 /*protect=*/2, /*want_type=*/false,
10690 tf_warning_or_error);
10691
10692 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10693 {
10694 /* Use the member functions. */
10695 if (member_begin != NULL_TREE)
10696 *begin = cp_parser_range_for_member_function (range, id_begin);
10697 else
10698 error ("range-based %<for%> expression of type %qT has an "
10699 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10700
10701 if (member_end != NULL_TREE)
10702 *end = cp_parser_range_for_member_function (range, id_end);
10703 else
10704 error ("range-based %<for%> expression of type %qT has a "
10705 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10706 }
10707 else
10708 {
10709 /* Use global functions with ADL. */
10710 vec<tree, va_gc> *vec;
10711 vec = make_tree_vector ();
10712
10713 vec_safe_push (vec, range);
10714
10715 member_begin = perform_koenig_lookup (id_begin, vec,
10716 tf_warning_or_error);
10717 *begin = finish_call_expr (member_begin, &vec, false, true,
10718 tf_warning_or_error);
10719 member_end = perform_koenig_lookup (id_end, vec,
10720 tf_warning_or_error);
10721 *end = finish_call_expr (member_end, &vec, false, true,
10722 tf_warning_or_error);
10723
10724 release_tree_vector (vec);
10725 }
10726
10727 /* Last common checks. */
10728 if (*begin == error_mark_node || *end == error_mark_node)
10729 {
10730 /* If one of the expressions is an error do no more checks. */
10731 *begin = *end = error_mark_node;
10732 return error_mark_node;
10733 }
10734 else if (type_dependent_expression_p (*begin)
10735 || type_dependent_expression_p (*end))
10736 /* Can happen, when, eg, in a template context, Koenig lookup
10737 can't resolve begin/end (c++/58503). */
10738 return NULL_TREE;
10739 else
10740 {
10741 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10742 /* The unqualified type of the __begin and __end temporaries should
10743 be the same, as required by the multiple auto declaration. */
10744 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10745 error ("inconsistent begin/end types in range-based %<for%> "
10746 "statement: %qT and %qT",
10747 TREE_TYPE (*begin), TREE_TYPE (*end));
10748 return iter_type;
10749 }
10750 }
10751 }
10752
10753 /* Helper function for cp_parser_perform_range_for_lookup.
10754 Builds a tree for RANGE.IDENTIFIER(). */
10755
10756 static tree
10757 cp_parser_range_for_member_function (tree range, tree identifier)
10758 {
10759 tree member, res;
10760 vec<tree, va_gc> *vec;
10761
10762 member = finish_class_member_access_expr (range, identifier,
10763 false, tf_warning_or_error);
10764 if (member == error_mark_node)
10765 return error_mark_node;
10766
10767 vec = make_tree_vector ();
10768 res = finish_call_expr (member, &vec,
10769 /*disallow_virtual=*/false,
10770 /*koenig_p=*/false,
10771 tf_warning_or_error);
10772 release_tree_vector (vec);
10773 return res;
10774 }
10775
10776 /* Parse an iteration-statement.
10777
10778 iteration-statement:
10779 while ( condition ) statement
10780 do statement while ( expression ) ;
10781 for ( for-init-statement condition [opt] ; expression [opt] )
10782 statement
10783
10784 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10785
10786 static tree
10787 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10788 {
10789 cp_token *token;
10790 location_t tok_loc;
10791 enum rid keyword;
10792 tree statement;
10793 unsigned char in_statement;
10794
10795 /* Peek at the next token. */
10796 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10797 if (!token)
10798 return error_mark_node;
10799
10800 tok_loc = token->location;
10801
10802 /* Remember whether or not we are already within an iteration
10803 statement. */
10804 in_statement = parser->in_statement;
10805
10806 /* See what kind of keyword it is. */
10807 keyword = token->keyword;
10808 switch (keyword)
10809 {
10810 case RID_WHILE:
10811 {
10812 tree condition;
10813
10814 /* Begin the while-statement. */
10815 statement = begin_while_stmt ();
10816 /* Look for the `('. */
10817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10818 /* Parse the condition. */
10819 condition = cp_parser_condition (parser);
10820 finish_while_stmt_cond (condition, statement, ivdep);
10821 /* Look for the `)'. */
10822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10823 /* Parse the dependent statement. */
10824 parser->in_statement = IN_ITERATION_STMT;
10825 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10826 parser->in_statement = in_statement;
10827 /* We're done with the while-statement. */
10828 finish_while_stmt (statement);
10829 }
10830 break;
10831
10832 case RID_DO:
10833 {
10834 tree expression;
10835
10836 /* Begin the do-statement. */
10837 statement = begin_do_stmt ();
10838 /* Parse the body of the do-statement. */
10839 parser->in_statement = IN_ITERATION_STMT;
10840 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10841 parser->in_statement = in_statement;
10842 finish_do_body (statement);
10843 /* Look for the `while' keyword. */
10844 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10845 /* Look for the `('. */
10846 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10847 /* Parse the expression. */
10848 expression = cp_parser_expression (parser);
10849 /* We're done with the do-statement. */
10850 finish_do_stmt (expression, statement, ivdep);
10851 /* Look for the `)'. */
10852 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10853 /* Look for the `;'. */
10854 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10855 }
10856 break;
10857
10858 case RID_FOR:
10859 {
10860 /* Look for the `('. */
10861 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10862
10863 statement = cp_parser_for (parser, ivdep);
10864
10865 /* Look for the `)'. */
10866 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10867
10868 /* Parse the body of the for-statement. */
10869 parser->in_statement = IN_ITERATION_STMT;
10870 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10871 parser->in_statement = in_statement;
10872
10873 /* We're done with the for-statement. */
10874 finish_for_stmt (statement);
10875 }
10876 break;
10877
10878 default:
10879 cp_parser_error (parser, "expected iteration-statement");
10880 statement = error_mark_node;
10881 break;
10882 }
10883
10884 return statement;
10885 }
10886
10887 /* Parse a for-init-statement or the declarator of a range-based-for.
10888 Returns true if a range-based-for declaration is seen.
10889
10890 for-init-statement:
10891 expression-statement
10892 simple-declaration */
10893
10894 static bool
10895 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10896 {
10897 /* If the next token is a `;', then we have an empty
10898 expression-statement. Grammatically, this is also a
10899 simple-declaration, but an invalid one, because it does not
10900 declare anything. Therefore, if we did not handle this case
10901 specially, we would issue an error message about an invalid
10902 declaration. */
10903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10904 {
10905 bool is_range_for = false;
10906 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10907
10908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10909 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10910 {
10911 /* N3994 -- for (id : init) ... */
10912 if (cxx_dialect < cxx1z)
10913 pedwarn (input_location, 0, "range-based for loop without a "
10914 "type-specifier only available with "
10915 "-std=c++1z or -std=gnu++1z");
10916 tree name = cp_parser_identifier (parser);
10917 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10918 *decl = build_decl (input_location, VAR_DECL, name, type);
10919 pushdecl (*decl);
10920 cp_lexer_consume_token (parser->lexer);
10921 return true;
10922 }
10923
10924 /* A colon is used in range-based for. */
10925 parser->colon_corrects_to_scope_p = false;
10926
10927 /* We're going to speculatively look for a declaration, falling back
10928 to an expression, if necessary. */
10929 cp_parser_parse_tentatively (parser);
10930 /* Parse the declaration. */
10931 cp_parser_simple_declaration (parser,
10932 /*function_definition_allowed_p=*/false,
10933 decl);
10934 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10935 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10936 {
10937 /* It is a range-for, consume the ':' */
10938 cp_lexer_consume_token (parser->lexer);
10939 is_range_for = true;
10940 if (cxx_dialect < cxx11)
10941 {
10942 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10943 "range-based %<for%> loops only available with "
10944 "-std=c++11 or -std=gnu++11");
10945 *decl = error_mark_node;
10946 }
10947 }
10948 else
10949 /* The ';' is not consumed yet because we told
10950 cp_parser_simple_declaration not to. */
10951 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10952
10953 if (cp_parser_parse_definitely (parser))
10954 return is_range_for;
10955 /* If the tentative parse failed, then we shall need to look for an
10956 expression-statement. */
10957 }
10958 /* If we are here, it is an expression-statement. */
10959 cp_parser_expression_statement (parser, NULL_TREE);
10960 return false;
10961 }
10962
10963 /* Parse a jump-statement.
10964
10965 jump-statement:
10966 break ;
10967 continue ;
10968 return expression [opt] ;
10969 return braced-init-list ;
10970 goto identifier ;
10971
10972 GNU extension:
10973
10974 jump-statement:
10975 goto * expression ;
10976
10977 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10978
10979 static tree
10980 cp_parser_jump_statement (cp_parser* parser)
10981 {
10982 tree statement = error_mark_node;
10983 cp_token *token;
10984 enum rid keyword;
10985 unsigned char in_statement;
10986
10987 /* Peek at the next token. */
10988 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10989 if (!token)
10990 return error_mark_node;
10991
10992 /* See what kind of keyword it is. */
10993 keyword = token->keyword;
10994 switch (keyword)
10995 {
10996 case RID_BREAK:
10997 in_statement = parser->in_statement & ~IN_IF_STMT;
10998 switch (in_statement)
10999 {
11000 case 0:
11001 error_at (token->location, "break statement not within loop or switch");
11002 break;
11003 default:
11004 gcc_assert ((in_statement & IN_SWITCH_STMT)
11005 || in_statement == IN_ITERATION_STMT);
11006 statement = finish_break_stmt ();
11007 if (in_statement == IN_ITERATION_STMT)
11008 break_maybe_infinite_loop ();
11009 break;
11010 case IN_OMP_BLOCK:
11011 error_at (token->location, "invalid exit from OpenMP structured block");
11012 break;
11013 case IN_OMP_FOR:
11014 error_at (token->location, "break statement used with OpenMP for loop");
11015 break;
11016 case IN_CILK_SIMD_FOR:
11017 error_at (token->location, "break statement used with Cilk Plus for loop");
11018 break;
11019 }
11020 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11021 break;
11022
11023 case RID_CONTINUE:
11024 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11025 {
11026 case 0:
11027 error_at (token->location, "continue statement not within a loop");
11028 break;
11029 case IN_CILK_SIMD_FOR:
11030 error_at (token->location,
11031 "continue statement within %<#pragma simd%> loop body");
11032 /* Fall through. */
11033 case IN_ITERATION_STMT:
11034 case IN_OMP_FOR:
11035 statement = finish_continue_stmt ();
11036 break;
11037 case IN_OMP_BLOCK:
11038 error_at (token->location, "invalid exit from OpenMP structured block");
11039 break;
11040 default:
11041 gcc_unreachable ();
11042 }
11043 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11044 break;
11045
11046 case RID_RETURN:
11047 {
11048 tree expr;
11049 bool expr_non_constant_p;
11050
11051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11052 {
11053 cp_lexer_set_source_position (parser->lexer);
11054 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11055 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11056 }
11057 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11058 expr = cp_parser_expression (parser);
11059 else
11060 /* If the next token is a `;', then there is no
11061 expression. */
11062 expr = NULL_TREE;
11063 /* Build the return-statement. */
11064 statement = finish_return_stmt (expr);
11065 /* Look for the final `;'. */
11066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11067 }
11068 break;
11069
11070 case RID_GOTO:
11071 if (parser->in_function_body
11072 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11073 {
11074 error ("%<goto%> in %<constexpr%> function");
11075 cp_function_chain->invalid_constexpr = true;
11076 }
11077
11078 /* Create the goto-statement. */
11079 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11080 {
11081 /* Issue a warning about this use of a GNU extension. */
11082 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11083 /* Consume the '*' token. */
11084 cp_lexer_consume_token (parser->lexer);
11085 /* Parse the dependent expression. */
11086 finish_goto_stmt (cp_parser_expression (parser));
11087 }
11088 else
11089 finish_goto_stmt (cp_parser_identifier (parser));
11090 /* Look for the final `;'. */
11091 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11092 break;
11093
11094 default:
11095 cp_parser_error (parser, "expected jump-statement");
11096 break;
11097 }
11098
11099 return statement;
11100 }
11101
11102 /* Parse a declaration-statement.
11103
11104 declaration-statement:
11105 block-declaration */
11106
11107 static void
11108 cp_parser_declaration_statement (cp_parser* parser)
11109 {
11110 void *p;
11111
11112 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11113 p = obstack_alloc (&declarator_obstack, 0);
11114
11115 /* Parse the block-declaration. */
11116 cp_parser_block_declaration (parser, /*statement_p=*/true);
11117
11118 /* Free any declarators allocated. */
11119 obstack_free (&declarator_obstack, p);
11120 }
11121
11122 /* Some dependent statements (like `if (cond) statement'), are
11123 implicitly in their own scope. In other words, if the statement is
11124 a single statement (as opposed to a compound-statement), it is
11125 none-the-less treated as if it were enclosed in braces. Any
11126 declarations appearing in the dependent statement are out of scope
11127 after control passes that point. This function parses a statement,
11128 but ensures that is in its own scope, even if it is not a
11129 compound-statement.
11130
11131 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11132 is a (possibly labeled) if statement which is not enclosed in
11133 braces and has an else clause. This is used to implement
11134 -Wparentheses.
11135
11136 Returns the new statement. */
11137
11138 static tree
11139 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11140 location_t guard_loc,
11141 const char *guard_kind)
11142 {
11143 tree statement;
11144
11145 if (if_p != NULL)
11146 *if_p = false;
11147
11148 /* Mark if () ; with a special NOP_EXPR. */
11149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11150 {
11151 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11152 cp_lexer_consume_token (parser->lexer);
11153 statement = add_stmt (build_empty_stmt (loc));
11154 }
11155 /* if a compound is opened, we simply parse the statement directly. */
11156 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11157 statement = cp_parser_compound_statement (parser, NULL, false, false);
11158 /* If the token is not a `{', then we must take special action. */
11159 else
11160 {
11161 /* Create a compound-statement. */
11162 statement = begin_compound_stmt (0);
11163 /* Parse the dependent-statement. */
11164 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11165 cp_parser_statement (parser, NULL_TREE, false, if_p);
11166 /* Finish the dummy compound-statement. */
11167 finish_compound_stmt (statement);
11168 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11169 if (next_tok->keyword != RID_ELSE)
11170 {
11171 location_t next_stmt_loc = next_tok->location;
11172 warn_for_misleading_indentation (guard_loc, body_loc,
11173 next_stmt_loc, next_tok->type,
11174 guard_kind);
11175 }
11176 }
11177
11178 /* Return the statement. */
11179 return statement;
11180 }
11181
11182 /* For some dependent statements (like `while (cond) statement'), we
11183 have already created a scope. Therefore, even if the dependent
11184 statement is a compound-statement, we do not want to create another
11185 scope. */
11186
11187 static void
11188 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11189 const char *guard_kind)
11190 {
11191 /* If the token is a `{', then we must take special action. */
11192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11193 {
11194 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11195 cp_parser_statement (parser, NULL_TREE, false, NULL);
11196 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11197 location_t next_stmt_loc = next_tok->location;
11198 warn_for_misleading_indentation (guard_loc, body_loc,
11199 next_stmt_loc, next_tok->type,
11200 guard_kind);
11201 }
11202 else
11203 {
11204 /* Avoid calling cp_parser_compound_statement, so that we
11205 don't create a new scope. Do everything else by hand. */
11206 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11207 /* If the next keyword is `__label__' we have a label declaration. */
11208 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11209 cp_parser_label_declaration (parser);
11210 /* Parse an (optional) statement-seq. */
11211 cp_parser_statement_seq_opt (parser, NULL_TREE);
11212 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11213 }
11214 }
11215
11216 /* Declarations [gram.dcl.dcl] */
11217
11218 /* Parse an optional declaration-sequence.
11219
11220 declaration-seq:
11221 declaration
11222 declaration-seq declaration */
11223
11224 static void
11225 cp_parser_declaration_seq_opt (cp_parser* parser)
11226 {
11227 while (true)
11228 {
11229 cp_token *token;
11230
11231 token = cp_lexer_peek_token (parser->lexer);
11232
11233 if (token->type == CPP_CLOSE_BRACE
11234 || token->type == CPP_EOF
11235 || token->type == CPP_PRAGMA_EOL)
11236 break;
11237
11238 if (token->type == CPP_SEMICOLON)
11239 {
11240 /* A declaration consisting of a single semicolon is
11241 invalid. Allow it unless we're being pedantic. */
11242 cp_lexer_consume_token (parser->lexer);
11243 if (!in_system_header_at (input_location))
11244 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11245 continue;
11246 }
11247
11248 /* If we're entering or exiting a region that's implicitly
11249 extern "C", modify the lang context appropriately. */
11250 if (!parser->implicit_extern_c && token->implicit_extern_c)
11251 {
11252 push_lang_context (lang_name_c);
11253 parser->implicit_extern_c = true;
11254 }
11255 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11256 {
11257 pop_lang_context ();
11258 parser->implicit_extern_c = false;
11259 }
11260
11261 if (token->type == CPP_PRAGMA)
11262 {
11263 /* A top-level declaration can consist solely of a #pragma.
11264 A nested declaration cannot, so this is done here and not
11265 in cp_parser_declaration. (A #pragma at block scope is
11266 handled in cp_parser_statement.) */
11267 cp_parser_pragma (parser, pragma_external);
11268 continue;
11269 }
11270
11271 /* Parse the declaration itself. */
11272 cp_parser_declaration (parser);
11273 }
11274 }
11275
11276 /* Parse a declaration.
11277
11278 declaration:
11279 block-declaration
11280 function-definition
11281 template-declaration
11282 explicit-instantiation
11283 explicit-specialization
11284 linkage-specification
11285 namespace-definition
11286
11287 GNU extension:
11288
11289 declaration:
11290 __extension__ declaration */
11291
11292 static void
11293 cp_parser_declaration (cp_parser* parser)
11294 {
11295 cp_token token1;
11296 cp_token token2;
11297 int saved_pedantic;
11298 void *p;
11299 tree attributes = NULL_TREE;
11300
11301 /* Check for the `__extension__' keyword. */
11302 if (cp_parser_extension_opt (parser, &saved_pedantic))
11303 {
11304 /* Parse the qualified declaration. */
11305 cp_parser_declaration (parser);
11306 /* Restore the PEDANTIC flag. */
11307 pedantic = saved_pedantic;
11308
11309 return;
11310 }
11311
11312 /* Try to figure out what kind of declaration is present. */
11313 token1 = *cp_lexer_peek_token (parser->lexer);
11314
11315 if (token1.type != CPP_EOF)
11316 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11317 else
11318 {
11319 token2.type = CPP_EOF;
11320 token2.keyword = RID_MAX;
11321 }
11322
11323 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11324 p = obstack_alloc (&declarator_obstack, 0);
11325
11326 /* If the next token is `extern' and the following token is a string
11327 literal, then we have a linkage specification. */
11328 if (token1.keyword == RID_EXTERN
11329 && cp_parser_is_pure_string_literal (&token2))
11330 cp_parser_linkage_specification (parser);
11331 /* If the next token is `template', then we have either a template
11332 declaration, an explicit instantiation, or an explicit
11333 specialization. */
11334 else if (token1.keyword == RID_TEMPLATE)
11335 {
11336 /* `template <>' indicates a template specialization. */
11337 if (token2.type == CPP_LESS
11338 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11339 cp_parser_explicit_specialization (parser);
11340 /* `template <' indicates a template declaration. */
11341 else if (token2.type == CPP_LESS)
11342 cp_parser_template_declaration (parser, /*member_p=*/false);
11343 /* Anything else must be an explicit instantiation. */
11344 else
11345 cp_parser_explicit_instantiation (parser);
11346 }
11347 /* If the next token is `export', then we have a template
11348 declaration. */
11349 else if (token1.keyword == RID_EXPORT)
11350 cp_parser_template_declaration (parser, /*member_p=*/false);
11351 /* If the next token is `extern', 'static' or 'inline' and the one
11352 after that is `template', we have a GNU extended explicit
11353 instantiation directive. */
11354 else if (cp_parser_allow_gnu_extensions_p (parser)
11355 && (token1.keyword == RID_EXTERN
11356 || token1.keyword == RID_STATIC
11357 || token1.keyword == RID_INLINE)
11358 && token2.keyword == RID_TEMPLATE)
11359 cp_parser_explicit_instantiation (parser);
11360 /* If the next token is `namespace', check for a named or unnamed
11361 namespace definition. */
11362 else if (token1.keyword == RID_NAMESPACE
11363 && (/* A named namespace definition. */
11364 (token2.type == CPP_NAME
11365 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11366 != CPP_EQ))
11367 /* An unnamed namespace definition. */
11368 || token2.type == CPP_OPEN_BRACE
11369 || token2.keyword == RID_ATTRIBUTE))
11370 cp_parser_namespace_definition (parser);
11371 /* An inline (associated) namespace definition. */
11372 else if (token1.keyword == RID_INLINE
11373 && token2.keyword == RID_NAMESPACE)
11374 cp_parser_namespace_definition (parser);
11375 /* Objective-C++ declaration/definition. */
11376 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11377 cp_parser_objc_declaration (parser, NULL_TREE);
11378 else if (c_dialect_objc ()
11379 && token1.keyword == RID_ATTRIBUTE
11380 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11381 cp_parser_objc_declaration (parser, attributes);
11382 /* We must have either a block declaration or a function
11383 definition. */
11384 else
11385 /* Try to parse a block-declaration, or a function-definition. */
11386 cp_parser_block_declaration (parser, /*statement_p=*/false);
11387
11388 /* Free any declarators allocated. */
11389 obstack_free (&declarator_obstack, p);
11390 }
11391
11392 /* Parse a block-declaration.
11393
11394 block-declaration:
11395 simple-declaration
11396 asm-definition
11397 namespace-alias-definition
11398 using-declaration
11399 using-directive
11400
11401 GNU Extension:
11402
11403 block-declaration:
11404 __extension__ block-declaration
11405
11406 C++0x Extension:
11407
11408 block-declaration:
11409 static_assert-declaration
11410
11411 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11412 part of a declaration-statement. */
11413
11414 static void
11415 cp_parser_block_declaration (cp_parser *parser,
11416 bool statement_p)
11417 {
11418 cp_token *token1;
11419 int saved_pedantic;
11420
11421 /* Check for the `__extension__' keyword. */
11422 if (cp_parser_extension_opt (parser, &saved_pedantic))
11423 {
11424 /* Parse the qualified declaration. */
11425 cp_parser_block_declaration (parser, statement_p);
11426 /* Restore the PEDANTIC flag. */
11427 pedantic = saved_pedantic;
11428
11429 return;
11430 }
11431
11432 /* Peek at the next token to figure out which kind of declaration is
11433 present. */
11434 token1 = cp_lexer_peek_token (parser->lexer);
11435
11436 /* If the next keyword is `asm', we have an asm-definition. */
11437 if (token1->keyword == RID_ASM)
11438 {
11439 if (statement_p)
11440 cp_parser_commit_to_tentative_parse (parser);
11441 cp_parser_asm_definition (parser);
11442 }
11443 /* If the next keyword is `namespace', we have a
11444 namespace-alias-definition. */
11445 else if (token1->keyword == RID_NAMESPACE)
11446 cp_parser_namespace_alias_definition (parser);
11447 /* If the next keyword is `using', we have a
11448 using-declaration, a using-directive, or an alias-declaration. */
11449 else if (token1->keyword == RID_USING)
11450 {
11451 cp_token *token2;
11452
11453 if (statement_p)
11454 cp_parser_commit_to_tentative_parse (parser);
11455 /* If the token after `using' is `namespace', then we have a
11456 using-directive. */
11457 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11458 if (token2->keyword == RID_NAMESPACE)
11459 cp_parser_using_directive (parser);
11460 /* If the second token after 'using' is '=', then we have an
11461 alias-declaration. */
11462 else if (cxx_dialect >= cxx11
11463 && token2->type == CPP_NAME
11464 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11465 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11466 cp_parser_alias_declaration (parser);
11467 /* Otherwise, it's a using-declaration. */
11468 else
11469 cp_parser_using_declaration (parser,
11470 /*access_declaration_p=*/false);
11471 }
11472 /* If the next keyword is `__label__' we have a misplaced label
11473 declaration. */
11474 else if (token1->keyword == RID_LABEL)
11475 {
11476 cp_lexer_consume_token (parser->lexer);
11477 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11478 cp_parser_skip_to_end_of_statement (parser);
11479 /* If the next token is now a `;', consume it. */
11480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11481 cp_lexer_consume_token (parser->lexer);
11482 }
11483 /* If the next token is `static_assert' we have a static assertion. */
11484 else if (token1->keyword == RID_STATIC_ASSERT)
11485 cp_parser_static_assert (parser, /*member_p=*/false);
11486 /* Anything else must be a simple-declaration. */
11487 else
11488 cp_parser_simple_declaration (parser, !statement_p,
11489 /*maybe_range_for_decl*/NULL);
11490 }
11491
11492 /* Parse a simple-declaration.
11493
11494 simple-declaration:
11495 decl-specifier-seq [opt] init-declarator-list [opt] ;
11496
11497 init-declarator-list:
11498 init-declarator
11499 init-declarator-list , init-declarator
11500
11501 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11502 function-definition as a simple-declaration.
11503
11504 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11505 parsed declaration if it is an uninitialized single declarator not followed
11506 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11507 if present, will not be consumed. */
11508
11509 static void
11510 cp_parser_simple_declaration (cp_parser* parser,
11511 bool function_definition_allowed_p,
11512 tree *maybe_range_for_decl)
11513 {
11514 cp_decl_specifier_seq decl_specifiers;
11515 int declares_class_or_enum;
11516 bool saw_declarator;
11517 location_t comma_loc = UNKNOWN_LOCATION;
11518 location_t init_loc = UNKNOWN_LOCATION;
11519
11520 if (maybe_range_for_decl)
11521 *maybe_range_for_decl = NULL_TREE;
11522
11523 /* Defer access checks until we know what is being declared; the
11524 checks for names appearing in the decl-specifier-seq should be
11525 done as if we were in the scope of the thing being declared. */
11526 push_deferring_access_checks (dk_deferred);
11527
11528 /* Parse the decl-specifier-seq. We have to keep track of whether
11529 or not the decl-specifier-seq declares a named class or
11530 enumeration type, since that is the only case in which the
11531 init-declarator-list is allowed to be empty.
11532
11533 [dcl.dcl]
11534
11535 In a simple-declaration, the optional init-declarator-list can be
11536 omitted only when declaring a class or enumeration, that is when
11537 the decl-specifier-seq contains either a class-specifier, an
11538 elaborated-type-specifier, or an enum-specifier. */
11539 cp_parser_decl_specifier_seq (parser,
11540 CP_PARSER_FLAGS_OPTIONAL,
11541 &decl_specifiers,
11542 &declares_class_or_enum);
11543 /* We no longer need to defer access checks. */
11544 stop_deferring_access_checks ();
11545
11546 /* In a block scope, a valid declaration must always have a
11547 decl-specifier-seq. By not trying to parse declarators, we can
11548 resolve the declaration/expression ambiguity more quickly. */
11549 if (!function_definition_allowed_p
11550 && !decl_specifiers.any_specifiers_p)
11551 {
11552 cp_parser_error (parser, "expected declaration");
11553 goto done;
11554 }
11555
11556 /* If the next two tokens are both identifiers, the code is
11557 erroneous. The usual cause of this situation is code like:
11558
11559 T t;
11560
11561 where "T" should name a type -- but does not. */
11562 if (!decl_specifiers.any_type_specifiers_p
11563 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11564 {
11565 /* If parsing tentatively, we should commit; we really are
11566 looking at a declaration. */
11567 cp_parser_commit_to_tentative_parse (parser);
11568 /* Give up. */
11569 goto done;
11570 }
11571
11572 /* If we have seen at least one decl-specifier, and the next token
11573 is not a parenthesis, then we must be looking at a declaration.
11574 (After "int (" we might be looking at a functional cast.) */
11575 if (decl_specifiers.any_specifiers_p
11576 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11577 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11578 && !cp_parser_error_occurred (parser))
11579 cp_parser_commit_to_tentative_parse (parser);
11580
11581 /* Keep going until we hit the `;' at the end of the simple
11582 declaration. */
11583 saw_declarator = false;
11584 while (cp_lexer_next_token_is_not (parser->lexer,
11585 CPP_SEMICOLON))
11586 {
11587 cp_token *token;
11588 bool function_definition_p;
11589 tree decl;
11590
11591 if (saw_declarator)
11592 {
11593 /* If we are processing next declarator, comma is expected */
11594 token = cp_lexer_peek_token (parser->lexer);
11595 gcc_assert (token->type == CPP_COMMA);
11596 cp_lexer_consume_token (parser->lexer);
11597 if (maybe_range_for_decl)
11598 {
11599 *maybe_range_for_decl = error_mark_node;
11600 if (comma_loc == UNKNOWN_LOCATION)
11601 comma_loc = token->location;
11602 }
11603 }
11604 else
11605 saw_declarator = true;
11606
11607 /* Parse the init-declarator. */
11608 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11609 /*checks=*/NULL,
11610 function_definition_allowed_p,
11611 /*member_p=*/false,
11612 declares_class_or_enum,
11613 &function_definition_p,
11614 maybe_range_for_decl,
11615 &init_loc);
11616 /* If an error occurred while parsing tentatively, exit quickly.
11617 (That usually happens when in the body of a function; each
11618 statement is treated as a declaration-statement until proven
11619 otherwise.) */
11620 if (cp_parser_error_occurred (parser))
11621 goto done;
11622 /* Handle function definitions specially. */
11623 if (function_definition_p)
11624 {
11625 /* If the next token is a `,', then we are probably
11626 processing something like:
11627
11628 void f() {}, *p;
11629
11630 which is erroneous. */
11631 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11632 {
11633 cp_token *token = cp_lexer_peek_token (parser->lexer);
11634 error_at (token->location,
11635 "mixing"
11636 " declarations and function-definitions is forbidden");
11637 }
11638 /* Otherwise, we're done with the list of declarators. */
11639 else
11640 {
11641 pop_deferring_access_checks ();
11642 return;
11643 }
11644 }
11645 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11646 *maybe_range_for_decl = decl;
11647 /* The next token should be either a `,' or a `;'. */
11648 token = cp_lexer_peek_token (parser->lexer);
11649 /* If it's a `,', there are more declarators to come. */
11650 if (token->type == CPP_COMMA)
11651 /* will be consumed next time around */;
11652 /* If it's a `;', we are done. */
11653 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11654 break;
11655 /* Anything else is an error. */
11656 else
11657 {
11658 /* If we have already issued an error message we don't need
11659 to issue another one. */
11660 if (decl != error_mark_node
11661 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11662 cp_parser_error (parser, "expected %<,%> or %<;%>");
11663 /* Skip tokens until we reach the end of the statement. */
11664 cp_parser_skip_to_end_of_statement (parser);
11665 /* If the next token is now a `;', consume it. */
11666 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11667 cp_lexer_consume_token (parser->lexer);
11668 goto done;
11669 }
11670 /* After the first time around, a function-definition is not
11671 allowed -- even if it was OK at first. For example:
11672
11673 int i, f() {}
11674
11675 is not valid. */
11676 function_definition_allowed_p = false;
11677 }
11678
11679 /* Issue an error message if no declarators are present, and the
11680 decl-specifier-seq does not itself declare a class or
11681 enumeration: [dcl.dcl]/3. */
11682 if (!saw_declarator)
11683 {
11684 if (cp_parser_declares_only_class_p (parser))
11685 {
11686 if (!declares_class_or_enum
11687 && decl_specifiers.type
11688 && OVERLOAD_TYPE_P (decl_specifiers.type))
11689 /* Ensure an error is issued anyway when finish_decltype_type,
11690 called via cp_parser_decl_specifier_seq, returns a class or
11691 an enumeration (c++/51786). */
11692 decl_specifiers.type = NULL_TREE;
11693 shadow_tag (&decl_specifiers);
11694 }
11695 /* Perform any deferred access checks. */
11696 perform_deferred_access_checks (tf_warning_or_error);
11697 }
11698
11699 /* Consume the `;'. */
11700 if (!maybe_range_for_decl)
11701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11702 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11703 {
11704 if (init_loc != UNKNOWN_LOCATION)
11705 error_at (init_loc, "initializer in range-based %<for%> loop");
11706 if (comma_loc != UNKNOWN_LOCATION)
11707 error_at (comma_loc,
11708 "multiple declarations in range-based %<for%> loop");
11709 }
11710
11711 done:
11712 pop_deferring_access_checks ();
11713 }
11714
11715 /* Parse a decl-specifier-seq.
11716
11717 decl-specifier-seq:
11718 decl-specifier-seq [opt] decl-specifier
11719 decl-specifier attribute-specifier-seq [opt] (C++11)
11720
11721 decl-specifier:
11722 storage-class-specifier
11723 type-specifier
11724 function-specifier
11725 friend
11726 typedef
11727
11728 GNU Extension:
11729
11730 decl-specifier:
11731 attributes
11732
11733 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11734
11735 The parser flags FLAGS is used to control type-specifier parsing.
11736
11737 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11738 flags:
11739
11740 1: one of the decl-specifiers is an elaborated-type-specifier
11741 (i.e., a type declaration)
11742 2: one of the decl-specifiers is an enum-specifier or a
11743 class-specifier (i.e., a type definition)
11744
11745 */
11746
11747 static void
11748 cp_parser_decl_specifier_seq (cp_parser* parser,
11749 cp_parser_flags flags,
11750 cp_decl_specifier_seq *decl_specs,
11751 int* declares_class_or_enum)
11752 {
11753 bool constructor_possible_p = !parser->in_declarator_p;
11754 bool found_decl_spec = false;
11755 cp_token *start_token = NULL;
11756 cp_decl_spec ds;
11757
11758 /* Clear DECL_SPECS. */
11759 clear_decl_specs (decl_specs);
11760
11761 /* Assume no class or enumeration type is declared. */
11762 *declares_class_or_enum = 0;
11763
11764 /* Keep reading specifiers until there are no more to read. */
11765 while (true)
11766 {
11767 bool constructor_p;
11768 cp_token *token;
11769 ds = ds_last;
11770
11771 /* Peek at the next token. */
11772 token = cp_lexer_peek_token (parser->lexer);
11773
11774 /* Save the first token of the decl spec list for error
11775 reporting. */
11776 if (!start_token)
11777 start_token = token;
11778 /* Handle attributes. */
11779 if (cp_next_tokens_can_be_attribute_p (parser))
11780 {
11781 /* Parse the attributes. */
11782 tree attrs = cp_parser_attributes_opt (parser);
11783
11784 /* In a sequence of declaration specifiers, c++11 attributes
11785 appertain to the type that precede them. In that case
11786 [dcl.spec]/1 says:
11787
11788 The attribute-specifier-seq affects the type only for
11789 the declaration it appears in, not other declarations
11790 involving the same type.
11791
11792 But for now let's force the user to position the
11793 attribute either at the beginning of the declaration or
11794 after the declarator-id, which would clearly mean that it
11795 applies to the declarator. */
11796 if (cxx11_attribute_p (attrs))
11797 {
11798 if (!found_decl_spec)
11799 /* The c++11 attribute is at the beginning of the
11800 declaration. It appertains to the entity being
11801 declared. */;
11802 else
11803 {
11804 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11805 {
11806 /* This is an attribute following a
11807 class-specifier. */
11808 if (decl_specs->type_definition_p)
11809 warn_misplaced_attr_for_class_type (token->location,
11810 decl_specs->type);
11811 attrs = NULL_TREE;
11812 }
11813 else
11814 {
11815 decl_specs->std_attributes
11816 = chainon (decl_specs->std_attributes,
11817 attrs);
11818 if (decl_specs->locations[ds_std_attribute] == 0)
11819 decl_specs->locations[ds_std_attribute] = token->location;
11820 }
11821 continue;
11822 }
11823 }
11824
11825 decl_specs->attributes
11826 = chainon (decl_specs->attributes,
11827 attrs);
11828 if (decl_specs->locations[ds_attribute] == 0)
11829 decl_specs->locations[ds_attribute] = token->location;
11830 continue;
11831 }
11832 /* Assume we will find a decl-specifier keyword. */
11833 found_decl_spec = true;
11834 /* If the next token is an appropriate keyword, we can simply
11835 add it to the list. */
11836 switch (token->keyword)
11837 {
11838 /* decl-specifier:
11839 friend
11840 constexpr */
11841 case RID_FRIEND:
11842 if (!at_class_scope_p ())
11843 {
11844 error_at (token->location, "%<friend%> used outside of class");
11845 cp_lexer_purge_token (parser->lexer);
11846 }
11847 else
11848 {
11849 ds = ds_friend;
11850 /* Consume the token. */
11851 cp_lexer_consume_token (parser->lexer);
11852 }
11853 break;
11854
11855 case RID_CONSTEXPR:
11856 ds = ds_constexpr;
11857 cp_lexer_consume_token (parser->lexer);
11858 break;
11859
11860 /* function-specifier:
11861 inline
11862 virtual
11863 explicit */
11864 case RID_INLINE:
11865 case RID_VIRTUAL:
11866 case RID_EXPLICIT:
11867 cp_parser_function_specifier_opt (parser, decl_specs);
11868 break;
11869
11870 /* decl-specifier:
11871 typedef */
11872 case RID_TYPEDEF:
11873 ds = ds_typedef;
11874 /* Consume the token. */
11875 cp_lexer_consume_token (parser->lexer);
11876 /* A constructor declarator cannot appear in a typedef. */
11877 constructor_possible_p = false;
11878 /* The "typedef" keyword can only occur in a declaration; we
11879 may as well commit at this point. */
11880 cp_parser_commit_to_tentative_parse (parser);
11881
11882 if (decl_specs->storage_class != sc_none)
11883 decl_specs->conflicting_specifiers_p = true;
11884 break;
11885
11886 /* storage-class-specifier:
11887 auto
11888 register
11889 static
11890 extern
11891 mutable
11892
11893 GNU Extension:
11894 thread */
11895 case RID_AUTO:
11896 if (cxx_dialect == cxx98)
11897 {
11898 /* Consume the token. */
11899 cp_lexer_consume_token (parser->lexer);
11900
11901 /* Complain about `auto' as a storage specifier, if
11902 we're complaining about C++0x compatibility. */
11903 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11904 " changes meaning in C++11; please remove it");
11905
11906 /* Set the storage class anyway. */
11907 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11908 token);
11909 }
11910 else
11911 /* C++0x auto type-specifier. */
11912 found_decl_spec = false;
11913 break;
11914
11915 case RID_REGISTER:
11916 case RID_STATIC:
11917 case RID_EXTERN:
11918 case RID_MUTABLE:
11919 /* Consume the token. */
11920 cp_lexer_consume_token (parser->lexer);
11921 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11922 token);
11923 break;
11924 case RID_THREAD:
11925 /* Consume the token. */
11926 ds = ds_thread;
11927 cp_lexer_consume_token (parser->lexer);
11928 break;
11929
11930 default:
11931 /* We did not yet find a decl-specifier yet. */
11932 found_decl_spec = false;
11933 break;
11934 }
11935
11936 if (found_decl_spec
11937 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11938 && token->keyword != RID_CONSTEXPR)
11939 error ("decl-specifier invalid in condition");
11940
11941 if (ds != ds_last)
11942 set_and_check_decl_spec_loc (decl_specs, ds, token);
11943
11944 /* Constructors are a special case. The `S' in `S()' is not a
11945 decl-specifier; it is the beginning of the declarator. */
11946 constructor_p
11947 = (!found_decl_spec
11948 && constructor_possible_p
11949 && (cp_parser_constructor_declarator_p
11950 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11951
11952 /* If we don't have a DECL_SPEC yet, then we must be looking at
11953 a type-specifier. */
11954 if (!found_decl_spec && !constructor_p)
11955 {
11956 int decl_spec_declares_class_or_enum;
11957 bool is_cv_qualifier;
11958 tree type_spec;
11959
11960 type_spec
11961 = cp_parser_type_specifier (parser, flags,
11962 decl_specs,
11963 /*is_declaration=*/true,
11964 &decl_spec_declares_class_or_enum,
11965 &is_cv_qualifier);
11966 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11967
11968 /* If this type-specifier referenced a user-defined type
11969 (a typedef, class-name, etc.), then we can't allow any
11970 more such type-specifiers henceforth.
11971
11972 [dcl.spec]
11973
11974 The longest sequence of decl-specifiers that could
11975 possibly be a type name is taken as the
11976 decl-specifier-seq of a declaration. The sequence shall
11977 be self-consistent as described below.
11978
11979 [dcl.type]
11980
11981 As a general rule, at most one type-specifier is allowed
11982 in the complete decl-specifier-seq of a declaration. The
11983 only exceptions are the following:
11984
11985 -- const or volatile can be combined with any other
11986 type-specifier.
11987
11988 -- signed or unsigned can be combined with char, long,
11989 short, or int.
11990
11991 -- ..
11992
11993 Example:
11994
11995 typedef char* Pc;
11996 void g (const int Pc);
11997
11998 Here, Pc is *not* part of the decl-specifier seq; it's
11999 the declarator. Therefore, once we see a type-specifier
12000 (other than a cv-qualifier), we forbid any additional
12001 user-defined types. We *do* still allow things like `int
12002 int' to be considered a decl-specifier-seq, and issue the
12003 error message later. */
12004 if (type_spec && !is_cv_qualifier)
12005 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12006 /* A constructor declarator cannot follow a type-specifier. */
12007 if (type_spec)
12008 {
12009 constructor_possible_p = false;
12010 found_decl_spec = true;
12011 if (!is_cv_qualifier)
12012 decl_specs->any_type_specifiers_p = true;
12013 }
12014 }
12015
12016 /* If we still do not have a DECL_SPEC, then there are no more
12017 decl-specifiers. */
12018 if (!found_decl_spec)
12019 break;
12020
12021 decl_specs->any_specifiers_p = true;
12022 /* After we see one decl-specifier, further decl-specifiers are
12023 always optional. */
12024 flags |= CP_PARSER_FLAGS_OPTIONAL;
12025 }
12026
12027 /* Don't allow a friend specifier with a class definition. */
12028 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12029 && (*declares_class_or_enum & 2))
12030 error_at (decl_specs->locations[ds_friend],
12031 "class definition may not be declared a friend");
12032 }
12033
12034 /* Parse an (optional) storage-class-specifier.
12035
12036 storage-class-specifier:
12037 auto
12038 register
12039 static
12040 extern
12041 mutable
12042
12043 GNU Extension:
12044
12045 storage-class-specifier:
12046 thread
12047
12048 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12049
12050 static tree
12051 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12052 {
12053 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12054 {
12055 case RID_AUTO:
12056 if (cxx_dialect != cxx98)
12057 return NULL_TREE;
12058 /* Fall through for C++98. */
12059
12060 case RID_REGISTER:
12061 case RID_STATIC:
12062 case RID_EXTERN:
12063 case RID_MUTABLE:
12064 case RID_THREAD:
12065 /* Consume the token. */
12066 return cp_lexer_consume_token (parser->lexer)->u.value;
12067
12068 default:
12069 return NULL_TREE;
12070 }
12071 }
12072
12073 /* Parse an (optional) function-specifier.
12074
12075 function-specifier:
12076 inline
12077 virtual
12078 explicit
12079
12080 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12081 Updates DECL_SPECS, if it is non-NULL. */
12082
12083 static tree
12084 cp_parser_function_specifier_opt (cp_parser* parser,
12085 cp_decl_specifier_seq *decl_specs)
12086 {
12087 cp_token *token = cp_lexer_peek_token (parser->lexer);
12088 switch (token->keyword)
12089 {
12090 case RID_INLINE:
12091 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12092 break;
12093
12094 case RID_VIRTUAL:
12095 /* 14.5.2.3 [temp.mem]
12096
12097 A member function template shall not be virtual. */
12098 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12099 error_at (token->location, "templates may not be %<virtual%>");
12100 else
12101 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12102 break;
12103
12104 case RID_EXPLICIT:
12105 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12106 break;
12107
12108 default:
12109 return NULL_TREE;
12110 }
12111
12112 /* Consume the token. */
12113 return cp_lexer_consume_token (parser->lexer)->u.value;
12114 }
12115
12116 /* Parse a linkage-specification.
12117
12118 linkage-specification:
12119 extern string-literal { declaration-seq [opt] }
12120 extern string-literal declaration */
12121
12122 static void
12123 cp_parser_linkage_specification (cp_parser* parser)
12124 {
12125 tree linkage;
12126
12127 /* Look for the `extern' keyword. */
12128 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12129
12130 /* Look for the string-literal. */
12131 linkage = cp_parser_string_literal (parser, false, false);
12132
12133 /* Transform the literal into an identifier. If the literal is a
12134 wide-character string, or contains embedded NULs, then we can't
12135 handle it as the user wants. */
12136 if (strlen (TREE_STRING_POINTER (linkage))
12137 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12138 {
12139 cp_parser_error (parser, "invalid linkage-specification");
12140 /* Assume C++ linkage. */
12141 linkage = lang_name_cplusplus;
12142 }
12143 else
12144 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12145
12146 /* We're now using the new linkage. */
12147 push_lang_context (linkage);
12148
12149 /* If the next token is a `{', then we're using the first
12150 production. */
12151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12152 {
12153 cp_ensure_no_omp_declare_simd (parser);
12154
12155 /* Consume the `{' token. */
12156 cp_lexer_consume_token (parser->lexer);
12157 /* Parse the declarations. */
12158 cp_parser_declaration_seq_opt (parser);
12159 /* Look for the closing `}'. */
12160 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12161 }
12162 /* Otherwise, there's just one declaration. */
12163 else
12164 {
12165 bool saved_in_unbraced_linkage_specification_p;
12166
12167 saved_in_unbraced_linkage_specification_p
12168 = parser->in_unbraced_linkage_specification_p;
12169 parser->in_unbraced_linkage_specification_p = true;
12170 cp_parser_declaration (parser);
12171 parser->in_unbraced_linkage_specification_p
12172 = saved_in_unbraced_linkage_specification_p;
12173 }
12174
12175 /* We're done with the linkage-specification. */
12176 pop_lang_context ();
12177 }
12178
12179 /* Parse a static_assert-declaration.
12180
12181 static_assert-declaration:
12182 static_assert ( constant-expression , string-literal ) ;
12183
12184 If MEMBER_P, this static_assert is a class member. */
12185
12186 static void
12187 cp_parser_static_assert(cp_parser *parser, bool member_p)
12188 {
12189 tree condition;
12190 tree message;
12191 cp_token *token;
12192 location_t saved_loc;
12193 bool dummy;
12194
12195 /* Peek at the `static_assert' token so we can keep track of exactly
12196 where the static assertion started. */
12197 token = cp_lexer_peek_token (parser->lexer);
12198 saved_loc = token->location;
12199
12200 /* Look for the `static_assert' keyword. */
12201 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12202 RT_STATIC_ASSERT))
12203 return;
12204
12205 /* We know we are in a static assertion; commit to any tentative
12206 parse. */
12207 if (cp_parser_parsing_tentatively (parser))
12208 cp_parser_commit_to_tentative_parse (parser);
12209
12210 /* Parse the `(' starting the static assertion condition. */
12211 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12212
12213 /* Parse the constant-expression. Allow a non-constant expression
12214 here in order to give better diagnostics in finish_static_assert. */
12215 condition =
12216 cp_parser_constant_expression (parser,
12217 /*allow_non_constant_p=*/true,
12218 /*non_constant_p=*/&dummy);
12219
12220 /* Parse the separating `,'. */
12221 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12222
12223 /* Parse the string-literal message. */
12224 message = cp_parser_string_literal (parser,
12225 /*translate=*/false,
12226 /*wide_ok=*/true);
12227
12228 /* A `)' completes the static assertion. */
12229 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12230 cp_parser_skip_to_closing_parenthesis (parser,
12231 /*recovering=*/true,
12232 /*or_comma=*/false,
12233 /*consume_paren=*/true);
12234
12235 /* A semicolon terminates the declaration. */
12236 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12237
12238 /* Complete the static assertion, which may mean either processing
12239 the static assert now or saving it for template instantiation. */
12240 finish_static_assert (condition, message, saved_loc, member_p);
12241 }
12242
12243 /* Parse the expression in decltype ( expression ). */
12244
12245 static tree
12246 cp_parser_decltype_expr (cp_parser *parser,
12247 bool &id_expression_or_member_access_p)
12248 {
12249 cp_token *id_expr_start_token;
12250 tree expr;
12251
12252 /* First, try parsing an id-expression. */
12253 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12254 cp_parser_parse_tentatively (parser);
12255 expr = cp_parser_id_expression (parser,
12256 /*template_keyword_p=*/false,
12257 /*check_dependency_p=*/true,
12258 /*template_p=*/NULL,
12259 /*declarator_p=*/false,
12260 /*optional_p=*/false);
12261
12262 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12263 {
12264 bool non_integral_constant_expression_p = false;
12265 tree id_expression = expr;
12266 cp_id_kind idk;
12267 const char *error_msg;
12268
12269 if (identifier_p (expr))
12270 /* Lookup the name we got back from the id-expression. */
12271 expr = cp_parser_lookup_name_simple (parser, expr,
12272 id_expr_start_token->location);
12273
12274 if (expr
12275 && expr != error_mark_node
12276 && TREE_CODE (expr) != TYPE_DECL
12277 && (TREE_CODE (expr) != BIT_NOT_EXPR
12278 || !TYPE_P (TREE_OPERAND (expr, 0)))
12279 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12280 {
12281 /* Complete lookup of the id-expression. */
12282 expr = (finish_id_expression
12283 (id_expression, expr, parser->scope, &idk,
12284 /*integral_constant_expression_p=*/false,
12285 /*allow_non_integral_constant_expression_p=*/true,
12286 &non_integral_constant_expression_p,
12287 /*template_p=*/false,
12288 /*done=*/true,
12289 /*address_p=*/false,
12290 /*template_arg_p=*/false,
12291 &error_msg,
12292 id_expr_start_token->location));
12293
12294 if (expr == error_mark_node)
12295 /* We found an id-expression, but it was something that we
12296 should not have found. This is an error, not something
12297 we can recover from, so note that we found an
12298 id-expression and we'll recover as gracefully as
12299 possible. */
12300 id_expression_or_member_access_p = true;
12301 }
12302
12303 if (expr
12304 && expr != error_mark_node
12305 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12306 /* We have an id-expression. */
12307 id_expression_or_member_access_p = true;
12308 }
12309
12310 if (!id_expression_or_member_access_p)
12311 {
12312 /* Abort the id-expression parse. */
12313 cp_parser_abort_tentative_parse (parser);
12314
12315 /* Parsing tentatively, again. */
12316 cp_parser_parse_tentatively (parser);
12317
12318 /* Parse a class member access. */
12319 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12320 /*cast_p=*/false, /*decltype*/true,
12321 /*member_access_only_p=*/true, NULL);
12322
12323 if (expr
12324 && expr != error_mark_node
12325 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12326 /* We have an id-expression. */
12327 id_expression_or_member_access_p = true;
12328 }
12329
12330 if (id_expression_or_member_access_p)
12331 /* We have parsed the complete id-expression or member access. */
12332 cp_parser_parse_definitely (parser);
12333 else
12334 {
12335 /* Abort our attempt to parse an id-expression or member access
12336 expression. */
12337 cp_parser_abort_tentative_parse (parser);
12338
12339 /* Parse a full expression. */
12340 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12341 /*decltype_p=*/true);
12342 }
12343
12344 return expr;
12345 }
12346
12347 /* Parse a `decltype' type. Returns the type.
12348
12349 simple-type-specifier:
12350 decltype ( expression )
12351 C++14 proposal:
12352 decltype ( auto ) */
12353
12354 static tree
12355 cp_parser_decltype (cp_parser *parser)
12356 {
12357 tree expr;
12358 bool id_expression_or_member_access_p = false;
12359 const char *saved_message;
12360 bool saved_integral_constant_expression_p;
12361 bool saved_non_integral_constant_expression_p;
12362 bool saved_greater_than_is_operator_p;
12363 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12364
12365 if (start_token->type == CPP_DECLTYPE)
12366 {
12367 /* Already parsed. */
12368 cp_lexer_consume_token (parser->lexer);
12369 return start_token->u.value;
12370 }
12371
12372 /* Look for the `decltype' token. */
12373 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12374 return error_mark_node;
12375
12376 /* Parse the opening `('. */
12377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12378 return error_mark_node;
12379
12380 /* decltype (auto) */
12381 if (cxx_dialect >= cxx14
12382 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12383 {
12384 cp_lexer_consume_token (parser->lexer);
12385 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12386 return error_mark_node;
12387 expr = make_decltype_auto ();
12388 AUTO_IS_DECLTYPE (expr) = true;
12389 goto rewrite;
12390 }
12391
12392 /* Types cannot be defined in a `decltype' expression. Save away the
12393 old message. */
12394 saved_message = parser->type_definition_forbidden_message;
12395
12396 /* And create the new one. */
12397 parser->type_definition_forbidden_message
12398 = G_("types may not be defined in %<decltype%> expressions");
12399
12400 /* The restrictions on constant-expressions do not apply inside
12401 decltype expressions. */
12402 saved_integral_constant_expression_p
12403 = parser->integral_constant_expression_p;
12404 saved_non_integral_constant_expression_p
12405 = parser->non_integral_constant_expression_p;
12406 parser->integral_constant_expression_p = false;
12407
12408 /* Within a parenthesized expression, a `>' token is always
12409 the greater-than operator. */
12410 saved_greater_than_is_operator_p
12411 = parser->greater_than_is_operator_p;
12412 parser->greater_than_is_operator_p = true;
12413
12414 /* Do not actually evaluate the expression. */
12415 ++cp_unevaluated_operand;
12416
12417 /* Do not warn about problems with the expression. */
12418 ++c_inhibit_evaluation_warnings;
12419
12420 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12421
12422 /* Go back to evaluating expressions. */
12423 --cp_unevaluated_operand;
12424 --c_inhibit_evaluation_warnings;
12425
12426 /* The `>' token might be the end of a template-id or
12427 template-parameter-list now. */
12428 parser->greater_than_is_operator_p
12429 = saved_greater_than_is_operator_p;
12430
12431 /* Restore the old message and the integral constant expression
12432 flags. */
12433 parser->type_definition_forbidden_message = saved_message;
12434 parser->integral_constant_expression_p
12435 = saved_integral_constant_expression_p;
12436 parser->non_integral_constant_expression_p
12437 = saved_non_integral_constant_expression_p;
12438
12439 /* Parse to the closing `)'. */
12440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12441 {
12442 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12443 /*consume_paren=*/true);
12444 return error_mark_node;
12445 }
12446
12447 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12448 tf_warning_or_error);
12449
12450 rewrite:
12451 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12452 it again. */
12453 start_token->type = CPP_DECLTYPE;
12454 start_token->u.value = expr;
12455 start_token->keyword = RID_MAX;
12456 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12457
12458 return expr;
12459 }
12460
12461 /* Special member functions [gram.special] */
12462
12463 /* Parse a conversion-function-id.
12464
12465 conversion-function-id:
12466 operator conversion-type-id
12467
12468 Returns an IDENTIFIER_NODE representing the operator. */
12469
12470 static tree
12471 cp_parser_conversion_function_id (cp_parser* parser)
12472 {
12473 tree type;
12474 tree saved_scope;
12475 tree saved_qualifying_scope;
12476 tree saved_object_scope;
12477 tree pushed_scope = NULL_TREE;
12478
12479 /* Look for the `operator' token. */
12480 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12481 return error_mark_node;
12482 /* When we parse the conversion-type-id, the current scope will be
12483 reset. However, we need that information in able to look up the
12484 conversion function later, so we save it here. */
12485 saved_scope = parser->scope;
12486 saved_qualifying_scope = parser->qualifying_scope;
12487 saved_object_scope = parser->object_scope;
12488 /* We must enter the scope of the class so that the names of
12489 entities declared within the class are available in the
12490 conversion-type-id. For example, consider:
12491
12492 struct S {
12493 typedef int I;
12494 operator I();
12495 };
12496
12497 S::operator I() { ... }
12498
12499 In order to see that `I' is a type-name in the definition, we
12500 must be in the scope of `S'. */
12501 if (saved_scope)
12502 pushed_scope = push_scope (saved_scope);
12503 /* Parse the conversion-type-id. */
12504 type = cp_parser_conversion_type_id (parser);
12505 /* Leave the scope of the class, if any. */
12506 if (pushed_scope)
12507 pop_scope (pushed_scope);
12508 /* Restore the saved scope. */
12509 parser->scope = saved_scope;
12510 parser->qualifying_scope = saved_qualifying_scope;
12511 parser->object_scope = saved_object_scope;
12512 /* If the TYPE is invalid, indicate failure. */
12513 if (type == error_mark_node)
12514 return error_mark_node;
12515 return mangle_conv_op_name_for_type (type);
12516 }
12517
12518 /* Parse a conversion-type-id:
12519
12520 conversion-type-id:
12521 type-specifier-seq conversion-declarator [opt]
12522
12523 Returns the TYPE specified. */
12524
12525 static tree
12526 cp_parser_conversion_type_id (cp_parser* parser)
12527 {
12528 tree attributes;
12529 cp_decl_specifier_seq type_specifiers;
12530 cp_declarator *declarator;
12531 tree type_specified;
12532 const char *saved_message;
12533
12534 /* Parse the attributes. */
12535 attributes = cp_parser_attributes_opt (parser);
12536
12537 saved_message = parser->type_definition_forbidden_message;
12538 parser->type_definition_forbidden_message
12539 = G_("types may not be defined in a conversion-type-id");
12540
12541 /* Parse the type-specifiers. */
12542 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12543 /*is_trailing_return=*/false,
12544 &type_specifiers);
12545
12546 parser->type_definition_forbidden_message = saved_message;
12547
12548 /* If that didn't work, stop. */
12549 if (type_specifiers.type == error_mark_node)
12550 return error_mark_node;
12551 /* Parse the conversion-declarator. */
12552 declarator = cp_parser_conversion_declarator_opt (parser);
12553
12554 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12555 /*initialized=*/0, &attributes);
12556 if (attributes)
12557 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12558
12559 /* Don't give this error when parsing tentatively. This happens to
12560 work because we always parse this definitively once. */
12561 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12562 && type_uses_auto (type_specified))
12563 {
12564 if (cxx_dialect < cxx14)
12565 {
12566 error ("invalid use of %<auto%> in conversion operator");
12567 return error_mark_node;
12568 }
12569 else if (template_parm_scope_p ())
12570 warning (0, "use of %<auto%> in member template "
12571 "conversion operator can never be deduced");
12572 }
12573
12574 return type_specified;
12575 }
12576
12577 /* Parse an (optional) conversion-declarator.
12578
12579 conversion-declarator:
12580 ptr-operator conversion-declarator [opt]
12581
12582 */
12583
12584 static cp_declarator *
12585 cp_parser_conversion_declarator_opt (cp_parser* parser)
12586 {
12587 enum tree_code code;
12588 tree class_type, std_attributes = NULL_TREE;
12589 cp_cv_quals cv_quals;
12590
12591 /* We don't know if there's a ptr-operator next, or not. */
12592 cp_parser_parse_tentatively (parser);
12593 /* Try the ptr-operator. */
12594 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12595 &std_attributes);
12596 /* If it worked, look for more conversion-declarators. */
12597 if (cp_parser_parse_definitely (parser))
12598 {
12599 cp_declarator *declarator;
12600
12601 /* Parse another optional declarator. */
12602 declarator = cp_parser_conversion_declarator_opt (parser);
12603
12604 declarator = cp_parser_make_indirect_declarator
12605 (code, class_type, cv_quals, declarator, std_attributes);
12606
12607 return declarator;
12608 }
12609
12610 return NULL;
12611 }
12612
12613 /* Parse an (optional) ctor-initializer.
12614
12615 ctor-initializer:
12616 : mem-initializer-list
12617
12618 Returns TRUE iff the ctor-initializer was actually present. */
12619
12620 static bool
12621 cp_parser_ctor_initializer_opt (cp_parser* parser)
12622 {
12623 /* If the next token is not a `:', then there is no
12624 ctor-initializer. */
12625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12626 {
12627 /* Do default initialization of any bases and members. */
12628 if (DECL_CONSTRUCTOR_P (current_function_decl))
12629 finish_mem_initializers (NULL_TREE);
12630
12631 return false;
12632 }
12633
12634 /* Consume the `:' token. */
12635 cp_lexer_consume_token (parser->lexer);
12636 /* And the mem-initializer-list. */
12637 cp_parser_mem_initializer_list (parser);
12638
12639 return true;
12640 }
12641
12642 /* Parse a mem-initializer-list.
12643
12644 mem-initializer-list:
12645 mem-initializer ... [opt]
12646 mem-initializer ... [opt] , mem-initializer-list */
12647
12648 static void
12649 cp_parser_mem_initializer_list (cp_parser* parser)
12650 {
12651 tree mem_initializer_list = NULL_TREE;
12652 tree target_ctor = error_mark_node;
12653 cp_token *token = cp_lexer_peek_token (parser->lexer);
12654
12655 /* Let the semantic analysis code know that we are starting the
12656 mem-initializer-list. */
12657 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12658 error_at (token->location,
12659 "only constructors take member initializers");
12660
12661 /* Loop through the list. */
12662 while (true)
12663 {
12664 tree mem_initializer;
12665
12666 token = cp_lexer_peek_token (parser->lexer);
12667 /* Parse the mem-initializer. */
12668 mem_initializer = cp_parser_mem_initializer (parser);
12669 /* If the next token is a `...', we're expanding member initializers. */
12670 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12671 {
12672 /* Consume the `...'. */
12673 cp_lexer_consume_token (parser->lexer);
12674
12675 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12676 can be expanded but members cannot. */
12677 if (mem_initializer != error_mark_node
12678 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12679 {
12680 error_at (token->location,
12681 "cannot expand initializer for member %<%D%>",
12682 TREE_PURPOSE (mem_initializer));
12683 mem_initializer = error_mark_node;
12684 }
12685
12686 /* Construct the pack expansion type. */
12687 if (mem_initializer != error_mark_node)
12688 mem_initializer = make_pack_expansion (mem_initializer);
12689 }
12690 if (target_ctor != error_mark_node
12691 && mem_initializer != error_mark_node)
12692 {
12693 error ("mem-initializer for %qD follows constructor delegation",
12694 TREE_PURPOSE (mem_initializer));
12695 mem_initializer = error_mark_node;
12696 }
12697 /* Look for a target constructor. */
12698 if (mem_initializer != error_mark_node
12699 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12700 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12701 {
12702 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12703 if (mem_initializer_list)
12704 {
12705 error ("constructor delegation follows mem-initializer for %qD",
12706 TREE_PURPOSE (mem_initializer_list));
12707 mem_initializer = error_mark_node;
12708 }
12709 target_ctor = mem_initializer;
12710 }
12711 /* Add it to the list, unless it was erroneous. */
12712 if (mem_initializer != error_mark_node)
12713 {
12714 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12715 mem_initializer_list = mem_initializer;
12716 }
12717 /* If the next token is not a `,', we're done. */
12718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12719 break;
12720 /* Consume the `,' token. */
12721 cp_lexer_consume_token (parser->lexer);
12722 }
12723
12724 /* Perform semantic analysis. */
12725 if (DECL_CONSTRUCTOR_P (current_function_decl))
12726 finish_mem_initializers (mem_initializer_list);
12727 }
12728
12729 /* Parse a mem-initializer.
12730
12731 mem-initializer:
12732 mem-initializer-id ( expression-list [opt] )
12733 mem-initializer-id braced-init-list
12734
12735 GNU extension:
12736
12737 mem-initializer:
12738 ( expression-list [opt] )
12739
12740 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12741 class) or FIELD_DECL (for a non-static data member) to initialize;
12742 the TREE_VALUE is the expression-list. An empty initialization
12743 list is represented by void_list_node. */
12744
12745 static tree
12746 cp_parser_mem_initializer (cp_parser* parser)
12747 {
12748 tree mem_initializer_id;
12749 tree expression_list;
12750 tree member;
12751 cp_token *token = cp_lexer_peek_token (parser->lexer);
12752
12753 /* Find out what is being initialized. */
12754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12755 {
12756 permerror (token->location,
12757 "anachronistic old-style base class initializer");
12758 mem_initializer_id = NULL_TREE;
12759 }
12760 else
12761 {
12762 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12763 if (mem_initializer_id == error_mark_node)
12764 return mem_initializer_id;
12765 }
12766 member = expand_member_init (mem_initializer_id);
12767 if (member && !DECL_P (member))
12768 in_base_initializer = 1;
12769
12770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12771 {
12772 bool expr_non_constant_p;
12773 cp_lexer_set_source_position (parser->lexer);
12774 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12775 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12776 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12777 expression_list = build_tree_list (NULL_TREE, expression_list);
12778 }
12779 else
12780 {
12781 vec<tree, va_gc> *vec;
12782 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12783 /*cast_p=*/false,
12784 /*allow_expansion_p=*/true,
12785 /*non_constant_p=*/NULL);
12786 if (vec == NULL)
12787 return error_mark_node;
12788 expression_list = build_tree_list_vec (vec);
12789 release_tree_vector (vec);
12790 }
12791
12792 if (expression_list == error_mark_node)
12793 return error_mark_node;
12794 if (!expression_list)
12795 expression_list = void_type_node;
12796
12797 in_base_initializer = 0;
12798
12799 return member ? build_tree_list (member, expression_list) : error_mark_node;
12800 }
12801
12802 /* Parse a mem-initializer-id.
12803
12804 mem-initializer-id:
12805 :: [opt] nested-name-specifier [opt] class-name
12806 identifier
12807
12808 Returns a TYPE indicating the class to be initializer for the first
12809 production. Returns an IDENTIFIER_NODE indicating the data member
12810 to be initialized for the second production. */
12811
12812 static tree
12813 cp_parser_mem_initializer_id (cp_parser* parser)
12814 {
12815 bool global_scope_p;
12816 bool nested_name_specifier_p;
12817 bool template_p = false;
12818 tree id;
12819
12820 cp_token *token = cp_lexer_peek_token (parser->lexer);
12821
12822 /* `typename' is not allowed in this context ([temp.res]). */
12823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12824 {
12825 error_at (token->location,
12826 "keyword %<typename%> not allowed in this context (a qualified "
12827 "member initializer is implicitly a type)");
12828 cp_lexer_consume_token (parser->lexer);
12829 }
12830 /* Look for the optional `::' operator. */
12831 global_scope_p
12832 = (cp_parser_global_scope_opt (parser,
12833 /*current_scope_valid_p=*/false)
12834 != NULL_TREE);
12835 /* Look for the optional nested-name-specifier. The simplest way to
12836 implement:
12837
12838 [temp.res]
12839
12840 The keyword `typename' is not permitted in a base-specifier or
12841 mem-initializer; in these contexts a qualified name that
12842 depends on a template-parameter is implicitly assumed to be a
12843 type name.
12844
12845 is to assume that we have seen the `typename' keyword at this
12846 point. */
12847 nested_name_specifier_p
12848 = (cp_parser_nested_name_specifier_opt (parser,
12849 /*typename_keyword_p=*/true,
12850 /*check_dependency_p=*/true,
12851 /*type_p=*/true,
12852 /*is_declaration=*/true)
12853 != NULL_TREE);
12854 if (nested_name_specifier_p)
12855 template_p = cp_parser_optional_template_keyword (parser);
12856 /* If there is a `::' operator or a nested-name-specifier, then we
12857 are definitely looking for a class-name. */
12858 if (global_scope_p || nested_name_specifier_p)
12859 return cp_parser_class_name (parser,
12860 /*typename_keyword_p=*/true,
12861 /*template_keyword_p=*/template_p,
12862 typename_type,
12863 /*check_dependency_p=*/true,
12864 /*class_head_p=*/false,
12865 /*is_declaration=*/true);
12866 /* Otherwise, we could also be looking for an ordinary identifier. */
12867 cp_parser_parse_tentatively (parser);
12868 /* Try a class-name. */
12869 id = cp_parser_class_name (parser,
12870 /*typename_keyword_p=*/true,
12871 /*template_keyword_p=*/false,
12872 none_type,
12873 /*check_dependency_p=*/true,
12874 /*class_head_p=*/false,
12875 /*is_declaration=*/true);
12876 /* If we found one, we're done. */
12877 if (cp_parser_parse_definitely (parser))
12878 return id;
12879 /* Otherwise, look for an ordinary identifier. */
12880 return cp_parser_identifier (parser);
12881 }
12882
12883 /* Overloading [gram.over] */
12884
12885 /* Parse an operator-function-id.
12886
12887 operator-function-id:
12888 operator operator
12889
12890 Returns an IDENTIFIER_NODE for the operator which is a
12891 human-readable spelling of the identifier, e.g., `operator +'. */
12892
12893 static tree
12894 cp_parser_operator_function_id (cp_parser* parser)
12895 {
12896 /* Look for the `operator' keyword. */
12897 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12898 return error_mark_node;
12899 /* And then the name of the operator itself. */
12900 return cp_parser_operator (parser);
12901 }
12902
12903 /* Return an identifier node for a user-defined literal operator.
12904 The suffix identifier is chained to the operator name identifier. */
12905
12906 static tree
12907 cp_literal_operator_id (const char* name)
12908 {
12909 tree identifier;
12910 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12911 + strlen (name) + 10);
12912 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12913 identifier = get_identifier (buffer);
12914
12915 return identifier;
12916 }
12917
12918 /* Parse an operator.
12919
12920 operator:
12921 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12922 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12923 || ++ -- , ->* -> () []
12924
12925 GNU Extensions:
12926
12927 operator:
12928 <? >? <?= >?=
12929
12930 Returns an IDENTIFIER_NODE for the operator which is a
12931 human-readable spelling of the identifier, e.g., `operator +'. */
12932
12933 static tree
12934 cp_parser_operator (cp_parser* parser)
12935 {
12936 tree id = NULL_TREE;
12937 cp_token *token;
12938 bool utf8 = false;
12939
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 /* Figure out which operator we have. */
12943 switch (token->type)
12944 {
12945 case CPP_KEYWORD:
12946 {
12947 enum tree_code op;
12948
12949 /* The keyword should be either `new' or `delete'. */
12950 if (token->keyword == RID_NEW)
12951 op = NEW_EXPR;
12952 else if (token->keyword == RID_DELETE)
12953 op = DELETE_EXPR;
12954 else
12955 break;
12956
12957 /* Consume the `new' or `delete' token. */
12958 cp_lexer_consume_token (parser->lexer);
12959
12960 /* Peek at the next token. */
12961 token = cp_lexer_peek_token (parser->lexer);
12962 /* If it's a `[' token then this is the array variant of the
12963 operator. */
12964 if (token->type == CPP_OPEN_SQUARE)
12965 {
12966 /* Consume the `[' token. */
12967 cp_lexer_consume_token (parser->lexer);
12968 /* Look for the `]' token. */
12969 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12970 id = ansi_opname (op == NEW_EXPR
12971 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12972 }
12973 /* Otherwise, we have the non-array variant. */
12974 else
12975 id = ansi_opname (op);
12976
12977 return id;
12978 }
12979
12980 case CPP_PLUS:
12981 id = ansi_opname (PLUS_EXPR);
12982 break;
12983
12984 case CPP_MINUS:
12985 id = ansi_opname (MINUS_EXPR);
12986 break;
12987
12988 case CPP_MULT:
12989 id = ansi_opname (MULT_EXPR);
12990 break;
12991
12992 case CPP_DIV:
12993 id = ansi_opname (TRUNC_DIV_EXPR);
12994 break;
12995
12996 case CPP_MOD:
12997 id = ansi_opname (TRUNC_MOD_EXPR);
12998 break;
12999
13000 case CPP_XOR:
13001 id = ansi_opname (BIT_XOR_EXPR);
13002 break;
13003
13004 case CPP_AND:
13005 id = ansi_opname (BIT_AND_EXPR);
13006 break;
13007
13008 case CPP_OR:
13009 id = ansi_opname (BIT_IOR_EXPR);
13010 break;
13011
13012 case CPP_COMPL:
13013 id = ansi_opname (BIT_NOT_EXPR);
13014 break;
13015
13016 case CPP_NOT:
13017 id = ansi_opname (TRUTH_NOT_EXPR);
13018 break;
13019
13020 case CPP_EQ:
13021 id = ansi_assopname (NOP_EXPR);
13022 break;
13023
13024 case CPP_LESS:
13025 id = ansi_opname (LT_EXPR);
13026 break;
13027
13028 case CPP_GREATER:
13029 id = ansi_opname (GT_EXPR);
13030 break;
13031
13032 case CPP_PLUS_EQ:
13033 id = ansi_assopname (PLUS_EXPR);
13034 break;
13035
13036 case CPP_MINUS_EQ:
13037 id = ansi_assopname (MINUS_EXPR);
13038 break;
13039
13040 case CPP_MULT_EQ:
13041 id = ansi_assopname (MULT_EXPR);
13042 break;
13043
13044 case CPP_DIV_EQ:
13045 id = ansi_assopname (TRUNC_DIV_EXPR);
13046 break;
13047
13048 case CPP_MOD_EQ:
13049 id = ansi_assopname (TRUNC_MOD_EXPR);
13050 break;
13051
13052 case CPP_XOR_EQ:
13053 id = ansi_assopname (BIT_XOR_EXPR);
13054 break;
13055
13056 case CPP_AND_EQ:
13057 id = ansi_assopname (BIT_AND_EXPR);
13058 break;
13059
13060 case CPP_OR_EQ:
13061 id = ansi_assopname (BIT_IOR_EXPR);
13062 break;
13063
13064 case CPP_LSHIFT:
13065 id = ansi_opname (LSHIFT_EXPR);
13066 break;
13067
13068 case CPP_RSHIFT:
13069 id = ansi_opname (RSHIFT_EXPR);
13070 break;
13071
13072 case CPP_LSHIFT_EQ:
13073 id = ansi_assopname (LSHIFT_EXPR);
13074 break;
13075
13076 case CPP_RSHIFT_EQ:
13077 id = ansi_assopname (RSHIFT_EXPR);
13078 break;
13079
13080 case CPP_EQ_EQ:
13081 id = ansi_opname (EQ_EXPR);
13082 break;
13083
13084 case CPP_NOT_EQ:
13085 id = ansi_opname (NE_EXPR);
13086 break;
13087
13088 case CPP_LESS_EQ:
13089 id = ansi_opname (LE_EXPR);
13090 break;
13091
13092 case CPP_GREATER_EQ:
13093 id = ansi_opname (GE_EXPR);
13094 break;
13095
13096 case CPP_AND_AND:
13097 id = ansi_opname (TRUTH_ANDIF_EXPR);
13098 break;
13099
13100 case CPP_OR_OR:
13101 id = ansi_opname (TRUTH_ORIF_EXPR);
13102 break;
13103
13104 case CPP_PLUS_PLUS:
13105 id = ansi_opname (POSTINCREMENT_EXPR);
13106 break;
13107
13108 case CPP_MINUS_MINUS:
13109 id = ansi_opname (PREDECREMENT_EXPR);
13110 break;
13111
13112 case CPP_COMMA:
13113 id = ansi_opname (COMPOUND_EXPR);
13114 break;
13115
13116 case CPP_DEREF_STAR:
13117 id = ansi_opname (MEMBER_REF);
13118 break;
13119
13120 case CPP_DEREF:
13121 id = ansi_opname (COMPONENT_REF);
13122 break;
13123
13124 case CPP_OPEN_PAREN:
13125 /* Consume the `('. */
13126 cp_lexer_consume_token (parser->lexer);
13127 /* Look for the matching `)'. */
13128 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13129 return ansi_opname (CALL_EXPR);
13130
13131 case CPP_OPEN_SQUARE:
13132 /* Consume the `['. */
13133 cp_lexer_consume_token (parser->lexer);
13134 /* Look for the matching `]'. */
13135 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13136 return ansi_opname (ARRAY_REF);
13137
13138 case CPP_UTF8STRING:
13139 case CPP_UTF8STRING_USERDEF:
13140 utf8 = true;
13141 case CPP_STRING:
13142 case CPP_WSTRING:
13143 case CPP_STRING16:
13144 case CPP_STRING32:
13145 case CPP_STRING_USERDEF:
13146 case CPP_WSTRING_USERDEF:
13147 case CPP_STRING16_USERDEF:
13148 case CPP_STRING32_USERDEF:
13149 {
13150 tree str, string_tree;
13151 int sz, len;
13152
13153 if (cxx_dialect == cxx98)
13154 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13155
13156 /* Consume the string. */
13157 str = cp_parser_string_literal (parser, /*translate=*/true,
13158 /*wide_ok=*/true, /*lookup_udlit=*/false);
13159 if (str == error_mark_node)
13160 return error_mark_node;
13161 else if (TREE_CODE (str) == USERDEF_LITERAL)
13162 {
13163 string_tree = USERDEF_LITERAL_VALUE (str);
13164 id = USERDEF_LITERAL_SUFFIX_ID (str);
13165 }
13166 else
13167 {
13168 string_tree = str;
13169 /* Look for the suffix identifier. */
13170 token = cp_lexer_peek_token (parser->lexer);
13171 if (token->type == CPP_NAME)
13172 id = cp_parser_identifier (parser);
13173 else if (token->type == CPP_KEYWORD)
13174 {
13175 error ("unexpected keyword;"
13176 " remove space between quotes and suffix identifier");
13177 return error_mark_node;
13178 }
13179 else
13180 {
13181 error ("expected suffix identifier");
13182 return error_mark_node;
13183 }
13184 }
13185 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13186 (TREE_TYPE (TREE_TYPE (string_tree))));
13187 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13188 if (len != 0)
13189 {
13190 error ("expected empty string after %<operator%> keyword");
13191 return error_mark_node;
13192 }
13193 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13194 != char_type_node)
13195 {
13196 error ("invalid encoding prefix in literal operator");
13197 return error_mark_node;
13198 }
13199 if (id != error_mark_node)
13200 {
13201 const char *name = IDENTIFIER_POINTER (id);
13202 id = cp_literal_operator_id (name);
13203 }
13204 return id;
13205 }
13206
13207 default:
13208 /* Anything else is an error. */
13209 break;
13210 }
13211
13212 /* If we have selected an identifier, we need to consume the
13213 operator token. */
13214 if (id)
13215 cp_lexer_consume_token (parser->lexer);
13216 /* Otherwise, no valid operator name was present. */
13217 else
13218 {
13219 cp_parser_error (parser, "expected operator");
13220 id = error_mark_node;
13221 }
13222
13223 return id;
13224 }
13225
13226 /* Parse a template-declaration.
13227
13228 template-declaration:
13229 export [opt] template < template-parameter-list > declaration
13230
13231 If MEMBER_P is TRUE, this template-declaration occurs within a
13232 class-specifier.
13233
13234 The grammar rule given by the standard isn't correct. What
13235 is really meant is:
13236
13237 template-declaration:
13238 export [opt] template-parameter-list-seq
13239 decl-specifier-seq [opt] init-declarator [opt] ;
13240 export [opt] template-parameter-list-seq
13241 function-definition
13242
13243 template-parameter-list-seq:
13244 template-parameter-list-seq [opt]
13245 template < template-parameter-list > */
13246
13247 static void
13248 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13249 {
13250 /* Check for `export'. */
13251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13252 {
13253 /* Consume the `export' token. */
13254 cp_lexer_consume_token (parser->lexer);
13255 /* Warn that we do not support `export'. */
13256 warning (0, "keyword %<export%> not implemented, and will be ignored");
13257 }
13258
13259 cp_parser_template_declaration_after_export (parser, member_p);
13260 }
13261
13262 /* Parse a template-parameter-list.
13263
13264 template-parameter-list:
13265 template-parameter
13266 template-parameter-list , template-parameter
13267
13268 Returns a TREE_LIST. Each node represents a template parameter.
13269 The nodes are connected via their TREE_CHAINs. */
13270
13271 static tree
13272 cp_parser_template_parameter_list (cp_parser* parser)
13273 {
13274 tree parameter_list = NULL_TREE;
13275
13276 begin_template_parm_list ();
13277
13278 /* The loop below parses the template parms. We first need to know
13279 the total number of template parms to be able to compute proper
13280 canonical types of each dependent type. So after the loop, when
13281 we know the total number of template parms,
13282 end_template_parm_list computes the proper canonical types and
13283 fixes up the dependent types accordingly. */
13284 while (true)
13285 {
13286 tree parameter;
13287 bool is_non_type;
13288 bool is_parameter_pack;
13289 location_t parm_loc;
13290
13291 /* Parse the template-parameter. */
13292 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13293 parameter = cp_parser_template_parameter (parser,
13294 &is_non_type,
13295 &is_parameter_pack);
13296 /* Add it to the list. */
13297 if (parameter != error_mark_node)
13298 parameter_list = process_template_parm (parameter_list,
13299 parm_loc,
13300 parameter,
13301 is_non_type,
13302 is_parameter_pack);
13303 else
13304 {
13305 tree err_parm = build_tree_list (parameter, parameter);
13306 parameter_list = chainon (parameter_list, err_parm);
13307 }
13308
13309 /* If the next token is not a `,', we're done. */
13310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13311 break;
13312 /* Otherwise, consume the `,' token. */
13313 cp_lexer_consume_token (parser->lexer);
13314 }
13315
13316 return end_template_parm_list (parameter_list);
13317 }
13318
13319 /* Parse a template-parameter.
13320
13321 template-parameter:
13322 type-parameter
13323 parameter-declaration
13324
13325 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13326 the parameter. The TREE_PURPOSE is the default value, if any.
13327 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13328 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13329 set to true iff this parameter is a parameter pack. */
13330
13331 static tree
13332 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13333 bool *is_parameter_pack)
13334 {
13335 cp_token *token;
13336 cp_parameter_declarator *parameter_declarator;
13337 cp_declarator *id_declarator;
13338 tree parm;
13339
13340 /* Assume it is a type parameter or a template parameter. */
13341 *is_non_type = false;
13342 /* Assume it not a parameter pack. */
13343 *is_parameter_pack = false;
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it is `class' or `template', we have a type-parameter. */
13347 if (token->keyword == RID_TEMPLATE)
13348 return cp_parser_type_parameter (parser, is_parameter_pack);
13349 /* If it is `class' or `typename' we do not know yet whether it is a
13350 type parameter or a non-type parameter. Consider:
13351
13352 template <typename T, typename T::X X> ...
13353
13354 or:
13355
13356 template <class C, class D*> ...
13357
13358 Here, the first parameter is a type parameter, and the second is
13359 a non-type parameter. We can tell by looking at the token after
13360 the identifier -- if it is a `,', `=', or `>' then we have a type
13361 parameter. */
13362 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13363 {
13364 /* Peek at the token after `class' or `typename'. */
13365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13366 /* If it's an ellipsis, we have a template type parameter
13367 pack. */
13368 if (token->type == CPP_ELLIPSIS)
13369 return cp_parser_type_parameter (parser, is_parameter_pack);
13370 /* If it's an identifier, skip it. */
13371 if (token->type == CPP_NAME)
13372 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13373 /* Now, see if the token looks like the end of a template
13374 parameter. */
13375 if (token->type == CPP_COMMA
13376 || token->type == CPP_EQ
13377 || token->type == CPP_GREATER)
13378 return cp_parser_type_parameter (parser, is_parameter_pack);
13379 }
13380
13381 /* Otherwise, it is a non-type parameter.
13382
13383 [temp.param]
13384
13385 When parsing a default template-argument for a non-type
13386 template-parameter, the first non-nested `>' is taken as the end
13387 of the template parameter-list rather than a greater-than
13388 operator. */
13389 *is_non_type = true;
13390 parameter_declarator
13391 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13392 /*parenthesized_p=*/NULL);
13393
13394 if (!parameter_declarator)
13395 return error_mark_node;
13396
13397 /* If the parameter declaration is marked as a parameter pack, set
13398 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13399 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13400 grokdeclarator. */
13401 if (parameter_declarator->declarator
13402 && parameter_declarator->declarator->parameter_pack_p)
13403 {
13404 *is_parameter_pack = true;
13405 parameter_declarator->declarator->parameter_pack_p = false;
13406 }
13407
13408 if (parameter_declarator->default_argument)
13409 {
13410 /* Can happen in some cases of erroneous input (c++/34892). */
13411 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13412 /* Consume the `...' for better error recovery. */
13413 cp_lexer_consume_token (parser->lexer);
13414 }
13415 /* If the next token is an ellipsis, and we don't already have it
13416 marked as a parameter pack, then we have a parameter pack (that
13417 has no declarator). */
13418 else if (!*is_parameter_pack
13419 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13420 && (declarator_can_be_parameter_pack
13421 (parameter_declarator->declarator)))
13422 {
13423 /* Consume the `...'. */
13424 cp_lexer_consume_token (parser->lexer);
13425 maybe_warn_variadic_templates ();
13426
13427 *is_parameter_pack = true;
13428 }
13429 /* We might end up with a pack expansion as the type of the non-type
13430 template parameter, in which case this is a non-type template
13431 parameter pack. */
13432 else if (parameter_declarator->decl_specifiers.type
13433 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13434 {
13435 *is_parameter_pack = true;
13436 parameter_declarator->decl_specifiers.type =
13437 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13438 }
13439
13440 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13441 {
13442 /* Parameter packs cannot have default arguments. However, a
13443 user may try to do so, so we'll parse them and give an
13444 appropriate diagnostic here. */
13445
13446 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13447
13448 /* Find the name of the parameter pack. */
13449 id_declarator = parameter_declarator->declarator;
13450 while (id_declarator && id_declarator->kind != cdk_id)
13451 id_declarator = id_declarator->declarator;
13452
13453 if (id_declarator && id_declarator->kind == cdk_id)
13454 error_at (start_token->location,
13455 "template parameter pack %qD cannot have a default argument",
13456 id_declarator->u.id.unqualified_name);
13457 else
13458 error_at (start_token->location,
13459 "template parameter pack cannot have a default argument");
13460
13461 /* Parse the default argument, but throw away the result. */
13462 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13463 }
13464
13465 parm = grokdeclarator (parameter_declarator->declarator,
13466 &parameter_declarator->decl_specifiers,
13467 TPARM, /*initialized=*/0,
13468 /*attrlist=*/NULL);
13469 if (parm == error_mark_node)
13470 return error_mark_node;
13471
13472 return build_tree_list (parameter_declarator->default_argument, parm);
13473 }
13474
13475 /* Parse a type-parameter.
13476
13477 type-parameter:
13478 class identifier [opt]
13479 class identifier [opt] = type-id
13480 typename identifier [opt]
13481 typename identifier [opt] = type-id
13482 template < template-parameter-list > class identifier [opt]
13483 template < template-parameter-list > class identifier [opt]
13484 = id-expression
13485
13486 GNU Extension (variadic templates):
13487
13488 type-parameter:
13489 class ... identifier [opt]
13490 typename ... identifier [opt]
13491
13492 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13493 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13494 the declaration of the parameter.
13495
13496 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13497
13498 static tree
13499 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13500 {
13501 cp_token *token;
13502 tree parameter;
13503
13504 /* Look for a keyword to tell us what kind of parameter this is. */
13505 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13506 if (!token)
13507 return error_mark_node;
13508
13509 switch (token->keyword)
13510 {
13511 case RID_CLASS:
13512 case RID_TYPENAME:
13513 {
13514 tree identifier;
13515 tree default_argument;
13516
13517 /* If the next token is an ellipsis, we have a template
13518 argument pack. */
13519 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13520 {
13521 /* Consume the `...' token. */
13522 cp_lexer_consume_token (parser->lexer);
13523 maybe_warn_variadic_templates ();
13524
13525 *is_parameter_pack = true;
13526 }
13527
13528 /* If the next token is an identifier, then it names the
13529 parameter. */
13530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13531 identifier = cp_parser_identifier (parser);
13532 else
13533 identifier = NULL_TREE;
13534
13535 /* Create the parameter. */
13536 parameter = finish_template_type_parm (class_type_node, identifier);
13537
13538 /* If the next token is an `=', we have a default argument. */
13539 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13540 {
13541 /* Consume the `=' token. */
13542 cp_lexer_consume_token (parser->lexer);
13543 /* Parse the default-argument. */
13544 push_deferring_access_checks (dk_no_deferred);
13545 default_argument = cp_parser_type_id (parser);
13546
13547 /* Template parameter packs cannot have default
13548 arguments. */
13549 if (*is_parameter_pack)
13550 {
13551 if (identifier)
13552 error_at (token->location,
13553 "template parameter pack %qD cannot have a "
13554 "default argument", identifier);
13555 else
13556 error_at (token->location,
13557 "template parameter packs cannot have "
13558 "default arguments");
13559 default_argument = NULL_TREE;
13560 }
13561 else if (check_for_bare_parameter_packs (default_argument))
13562 default_argument = error_mark_node;
13563 pop_deferring_access_checks ();
13564 }
13565 else
13566 default_argument = NULL_TREE;
13567
13568 /* Create the combined representation of the parameter and the
13569 default argument. */
13570 parameter = build_tree_list (default_argument, parameter);
13571 }
13572 break;
13573
13574 case RID_TEMPLATE:
13575 {
13576 tree identifier;
13577 tree default_argument;
13578
13579 /* Look for the `<'. */
13580 cp_parser_require (parser, CPP_LESS, RT_LESS);
13581 /* Parse the template-parameter-list. */
13582 cp_parser_template_parameter_list (parser);
13583 /* Look for the `>'. */
13584 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13585 /* Look for the `class' or 'typename' keywords. */
13586 cp_parser_type_parameter_key (parser);
13587 /* If the next token is an ellipsis, we have a template
13588 argument pack. */
13589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13590 {
13591 /* Consume the `...' token. */
13592 cp_lexer_consume_token (parser->lexer);
13593 maybe_warn_variadic_templates ();
13594
13595 *is_parameter_pack = true;
13596 }
13597 /* If the next token is an `=', then there is a
13598 default-argument. If the next token is a `>', we are at
13599 the end of the parameter-list. If the next token is a `,',
13600 then we are at the end of this parameter. */
13601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13602 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13603 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13604 {
13605 identifier = cp_parser_identifier (parser);
13606 /* Treat invalid names as if the parameter were nameless. */
13607 if (identifier == error_mark_node)
13608 identifier = NULL_TREE;
13609 }
13610 else
13611 identifier = NULL_TREE;
13612
13613 /* Create the template parameter. */
13614 parameter = finish_template_template_parm (class_type_node,
13615 identifier);
13616
13617 /* If the next token is an `=', then there is a
13618 default-argument. */
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13620 {
13621 bool is_template;
13622
13623 /* Consume the `='. */
13624 cp_lexer_consume_token (parser->lexer);
13625 /* Parse the id-expression. */
13626 push_deferring_access_checks (dk_no_deferred);
13627 /* save token before parsing the id-expression, for error
13628 reporting */
13629 token = cp_lexer_peek_token (parser->lexer);
13630 default_argument
13631 = cp_parser_id_expression (parser,
13632 /*template_keyword_p=*/false,
13633 /*check_dependency_p=*/true,
13634 /*template_p=*/&is_template,
13635 /*declarator_p=*/false,
13636 /*optional_p=*/false);
13637 if (TREE_CODE (default_argument) == TYPE_DECL)
13638 /* If the id-expression was a template-id that refers to
13639 a template-class, we already have the declaration here,
13640 so no further lookup is needed. */
13641 ;
13642 else
13643 /* Look up the name. */
13644 default_argument
13645 = cp_parser_lookup_name (parser, default_argument,
13646 none_type,
13647 /*is_template=*/is_template,
13648 /*is_namespace=*/false,
13649 /*check_dependency=*/true,
13650 /*ambiguous_decls=*/NULL,
13651 token->location);
13652 /* See if the default argument is valid. */
13653 default_argument
13654 = check_template_template_default_arg (default_argument);
13655
13656 /* Template parameter packs cannot have default
13657 arguments. */
13658 if (*is_parameter_pack)
13659 {
13660 if (identifier)
13661 error_at (token->location,
13662 "template parameter pack %qD cannot "
13663 "have a default argument",
13664 identifier);
13665 else
13666 error_at (token->location, "template parameter packs cannot "
13667 "have default arguments");
13668 default_argument = NULL_TREE;
13669 }
13670 pop_deferring_access_checks ();
13671 }
13672 else
13673 default_argument = NULL_TREE;
13674
13675 /* Create the combined representation of the parameter and the
13676 default argument. */
13677 parameter = build_tree_list (default_argument, parameter);
13678 }
13679 break;
13680
13681 default:
13682 gcc_unreachable ();
13683 break;
13684 }
13685
13686 return parameter;
13687 }
13688
13689 /* Parse a template-id.
13690
13691 template-id:
13692 template-name < template-argument-list [opt] >
13693
13694 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13695 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13696 returned. Otherwise, if the template-name names a function, or set
13697 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13698 names a class, returns a TYPE_DECL for the specialization.
13699
13700 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13701 uninstantiated templates. */
13702
13703 static tree
13704 cp_parser_template_id (cp_parser *parser,
13705 bool template_keyword_p,
13706 bool check_dependency_p,
13707 enum tag_types tag_type,
13708 bool is_declaration)
13709 {
13710 int i;
13711 tree templ;
13712 tree arguments;
13713 tree template_id;
13714 cp_token_position start_of_id = 0;
13715 deferred_access_check *chk;
13716 vec<deferred_access_check, va_gc> *access_check;
13717 cp_token *next_token = NULL, *next_token_2 = NULL;
13718 bool is_identifier;
13719
13720 /* If the next token corresponds to a template-id, there is no need
13721 to reparse it. */
13722 next_token = cp_lexer_peek_token (parser->lexer);
13723 if (next_token->type == CPP_TEMPLATE_ID)
13724 {
13725 struct tree_check *check_value;
13726
13727 /* Get the stored value. */
13728 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13729 /* Perform any access checks that were deferred. */
13730 access_check = check_value->checks;
13731 if (access_check)
13732 {
13733 FOR_EACH_VEC_ELT (*access_check, i, chk)
13734 perform_or_defer_access_check (chk->binfo,
13735 chk->decl,
13736 chk->diag_decl,
13737 tf_warning_or_error);
13738 }
13739 /* Return the stored value. */
13740 return check_value->value;
13741 }
13742
13743 /* Avoid performing name lookup if there is no possibility of
13744 finding a template-id. */
13745 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13746 || (next_token->type == CPP_NAME
13747 && !cp_parser_nth_token_starts_template_argument_list_p
13748 (parser, 2)))
13749 {
13750 cp_parser_error (parser, "expected template-id");
13751 return error_mark_node;
13752 }
13753
13754 /* Remember where the template-id starts. */
13755 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13756 start_of_id = cp_lexer_token_position (parser->lexer, false);
13757
13758 push_deferring_access_checks (dk_deferred);
13759
13760 /* Parse the template-name. */
13761 is_identifier = false;
13762 templ = cp_parser_template_name (parser, template_keyword_p,
13763 check_dependency_p,
13764 is_declaration,
13765 tag_type,
13766 &is_identifier);
13767 if (templ == error_mark_node || is_identifier)
13768 {
13769 pop_deferring_access_checks ();
13770 return templ;
13771 }
13772
13773 /* If we find the sequence `[:' after a template-name, it's probably
13774 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13775 parse correctly the argument list. */
13776 next_token = cp_lexer_peek_token (parser->lexer);
13777 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13778 if (next_token->type == CPP_OPEN_SQUARE
13779 && next_token->flags & DIGRAPH
13780 && next_token_2->type == CPP_COLON
13781 && !(next_token_2->flags & PREV_WHITE))
13782 {
13783 cp_parser_parse_tentatively (parser);
13784 /* Change `:' into `::'. */
13785 next_token_2->type = CPP_SCOPE;
13786 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13787 CPP_LESS. */
13788 cp_lexer_consume_token (parser->lexer);
13789
13790 /* Parse the arguments. */
13791 arguments = cp_parser_enclosed_template_argument_list (parser);
13792 if (!cp_parser_parse_definitely (parser))
13793 {
13794 /* If we couldn't parse an argument list, then we revert our changes
13795 and return simply an error. Maybe this is not a template-id
13796 after all. */
13797 next_token_2->type = CPP_COLON;
13798 cp_parser_error (parser, "expected %<<%>");
13799 pop_deferring_access_checks ();
13800 return error_mark_node;
13801 }
13802 /* Otherwise, emit an error about the invalid digraph, but continue
13803 parsing because we got our argument list. */
13804 if (permerror (next_token->location,
13805 "%<<::%> cannot begin a template-argument list"))
13806 {
13807 static bool hint = false;
13808 inform (next_token->location,
13809 "%<<:%> is an alternate spelling for %<[%>."
13810 " Insert whitespace between %<<%> and %<::%>");
13811 if (!hint && !flag_permissive)
13812 {
13813 inform (next_token->location, "(if you use %<-fpermissive%> "
13814 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13815 "accept your code)");
13816 hint = true;
13817 }
13818 }
13819 }
13820 else
13821 {
13822 /* Look for the `<' that starts the template-argument-list. */
13823 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13824 {
13825 pop_deferring_access_checks ();
13826 return error_mark_node;
13827 }
13828 /* Parse the arguments. */
13829 arguments = cp_parser_enclosed_template_argument_list (parser);
13830 }
13831
13832 /* Build a representation of the specialization. */
13833 if (identifier_p (templ))
13834 template_id = build_min_nt_loc (next_token->location,
13835 TEMPLATE_ID_EXPR,
13836 templ, arguments);
13837 else if (DECL_TYPE_TEMPLATE_P (templ)
13838 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13839 {
13840 bool entering_scope;
13841 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13842 template (rather than some instantiation thereof) only if
13843 is not nested within some other construct. For example, in
13844 "template <typename T> void f(T) { A<T>::", A<T> is just an
13845 instantiation of A. */
13846 entering_scope = (template_parm_scope_p ()
13847 && cp_lexer_next_token_is (parser->lexer,
13848 CPP_SCOPE));
13849 template_id
13850 = finish_template_type (templ, arguments, entering_scope);
13851 }
13852 else if (variable_template_p (templ))
13853 {
13854 template_id = lookup_template_variable (templ, arguments);
13855 }
13856 else
13857 {
13858 /* If it's not a class-template or a template-template, it should be
13859 a function-template. */
13860 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13861 || TREE_CODE (templ) == OVERLOAD
13862 || BASELINK_P (templ)));
13863
13864 template_id = lookup_template_function (templ, arguments);
13865 }
13866
13867 /* If parsing tentatively, replace the sequence of tokens that makes
13868 up the template-id with a CPP_TEMPLATE_ID token. That way,
13869 should we re-parse the token stream, we will not have to repeat
13870 the effort required to do the parse, nor will we issue duplicate
13871 error messages about problems during instantiation of the
13872 template. */
13873 if (start_of_id
13874 /* Don't do this if we had a parse error in a declarator; re-parsing
13875 might succeed if a name changes meaning (60361). */
13876 && !(cp_parser_error_occurred (parser)
13877 && cp_parser_parsing_tentatively (parser)
13878 && parser->in_declarator_p))
13879 {
13880 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13881
13882 /* Reset the contents of the START_OF_ID token. */
13883 token->type = CPP_TEMPLATE_ID;
13884 /* Retrieve any deferred checks. Do not pop this access checks yet
13885 so the memory will not be reclaimed during token replacing below. */
13886 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13887 token->u.tree_check_value->value = template_id;
13888 token->u.tree_check_value->checks = get_deferred_access_checks ();
13889 token->keyword = RID_MAX;
13890
13891 /* Purge all subsequent tokens. */
13892 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13893
13894 /* ??? Can we actually assume that, if template_id ==
13895 error_mark_node, we will have issued a diagnostic to the
13896 user, as opposed to simply marking the tentative parse as
13897 failed? */
13898 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13899 error_at (token->location, "parse error in template argument list");
13900 }
13901
13902 pop_to_parent_deferring_access_checks ();
13903 return template_id;
13904 }
13905
13906 /* Parse a template-name.
13907
13908 template-name:
13909 identifier
13910
13911 The standard should actually say:
13912
13913 template-name:
13914 identifier
13915 operator-function-id
13916
13917 A defect report has been filed about this issue.
13918
13919 A conversion-function-id cannot be a template name because they cannot
13920 be part of a template-id. In fact, looking at this code:
13921
13922 a.operator K<int>()
13923
13924 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13925 It is impossible to call a templated conversion-function-id with an
13926 explicit argument list, since the only allowed template parameter is
13927 the type to which it is converting.
13928
13929 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13930 `template' keyword, in a construction like:
13931
13932 T::template f<3>()
13933
13934 In that case `f' is taken to be a template-name, even though there
13935 is no way of knowing for sure.
13936
13937 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13938 name refers to a set of overloaded functions, at least one of which
13939 is a template, or an IDENTIFIER_NODE with the name of the template,
13940 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13941 names are looked up inside uninstantiated templates. */
13942
13943 static tree
13944 cp_parser_template_name (cp_parser* parser,
13945 bool template_keyword_p,
13946 bool check_dependency_p,
13947 bool is_declaration,
13948 enum tag_types tag_type,
13949 bool *is_identifier)
13950 {
13951 tree identifier;
13952 tree decl;
13953 tree fns;
13954 cp_token *token = cp_lexer_peek_token (parser->lexer);
13955
13956 /* If the next token is `operator', then we have either an
13957 operator-function-id or a conversion-function-id. */
13958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13959 {
13960 /* We don't know whether we're looking at an
13961 operator-function-id or a conversion-function-id. */
13962 cp_parser_parse_tentatively (parser);
13963 /* Try an operator-function-id. */
13964 identifier = cp_parser_operator_function_id (parser);
13965 /* If that didn't work, try a conversion-function-id. */
13966 if (!cp_parser_parse_definitely (parser))
13967 {
13968 cp_parser_error (parser, "expected template-name");
13969 return error_mark_node;
13970 }
13971 }
13972 /* Look for the identifier. */
13973 else
13974 identifier = cp_parser_identifier (parser);
13975
13976 /* If we didn't find an identifier, we don't have a template-id. */
13977 if (identifier == error_mark_node)
13978 return error_mark_node;
13979
13980 /* If the name immediately followed the `template' keyword, then it
13981 is a template-name. However, if the next token is not `<', then
13982 we do not treat it as a template-name, since it is not being used
13983 as part of a template-id. This enables us to handle constructs
13984 like:
13985
13986 template <typename T> struct S { S(); };
13987 template <typename T> S<T>::S();
13988
13989 correctly. We would treat `S' as a template -- if it were `S<T>'
13990 -- but we do not if there is no `<'. */
13991
13992 if (processing_template_decl
13993 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13994 {
13995 /* In a declaration, in a dependent context, we pretend that the
13996 "template" keyword was present in order to improve error
13997 recovery. For example, given:
13998
13999 template <typename T> void f(T::X<int>);
14000
14001 we want to treat "X<int>" as a template-id. */
14002 if (is_declaration
14003 && !template_keyword_p
14004 && parser->scope && TYPE_P (parser->scope)
14005 && check_dependency_p
14006 && dependent_scope_p (parser->scope)
14007 /* Do not do this for dtors (or ctors), since they never
14008 need the template keyword before their name. */
14009 && !constructor_name_p (identifier, parser->scope))
14010 {
14011 cp_token_position start = 0;
14012
14013 /* Explain what went wrong. */
14014 error_at (token->location, "non-template %qD used as template",
14015 identifier);
14016 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14017 parser->scope, identifier);
14018 /* If parsing tentatively, find the location of the "<" token. */
14019 if (cp_parser_simulate_error (parser))
14020 start = cp_lexer_token_position (parser->lexer, true);
14021 /* Parse the template arguments so that we can issue error
14022 messages about them. */
14023 cp_lexer_consume_token (parser->lexer);
14024 cp_parser_enclosed_template_argument_list (parser);
14025 /* Skip tokens until we find a good place from which to
14026 continue parsing. */
14027 cp_parser_skip_to_closing_parenthesis (parser,
14028 /*recovering=*/true,
14029 /*or_comma=*/true,
14030 /*consume_paren=*/false);
14031 /* If parsing tentatively, permanently remove the
14032 template argument list. That will prevent duplicate
14033 error messages from being issued about the missing
14034 "template" keyword. */
14035 if (start)
14036 cp_lexer_purge_tokens_after (parser->lexer, start);
14037 if (is_identifier)
14038 *is_identifier = true;
14039 return identifier;
14040 }
14041
14042 /* If the "template" keyword is present, then there is generally
14043 no point in doing name-lookup, so we just return IDENTIFIER.
14044 But, if the qualifying scope is non-dependent then we can
14045 (and must) do name-lookup normally. */
14046 if (template_keyword_p
14047 && (!parser->scope
14048 || (TYPE_P (parser->scope)
14049 && dependent_type_p (parser->scope))))
14050 return identifier;
14051 }
14052
14053 /* Look up the name. */
14054 decl = cp_parser_lookup_name (parser, identifier,
14055 tag_type,
14056 /*is_template=*/true,
14057 /*is_namespace=*/false,
14058 check_dependency_p,
14059 /*ambiguous_decls=*/NULL,
14060 token->location);
14061
14062 decl = strip_using_decl (decl);
14063
14064 /* If DECL is a template, then the name was a template-name. */
14065 if (TREE_CODE (decl) == TEMPLATE_DECL)
14066 {
14067 if (TREE_DEPRECATED (decl)
14068 && deprecated_state != DEPRECATED_SUPPRESS)
14069 warn_deprecated_use (decl, NULL_TREE);
14070 }
14071 else
14072 {
14073 tree fn = NULL_TREE;
14074
14075 /* The standard does not explicitly indicate whether a name that
14076 names a set of overloaded declarations, some of which are
14077 templates, is a template-name. However, such a name should
14078 be a template-name; otherwise, there is no way to form a
14079 template-id for the overloaded templates. */
14080 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14081 if (TREE_CODE (fns) == OVERLOAD)
14082 for (fn = fns; fn; fn = OVL_NEXT (fn))
14083 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14084 break;
14085
14086 if (!fn)
14087 {
14088 /* The name does not name a template. */
14089 cp_parser_error (parser, "expected template-name");
14090 return error_mark_node;
14091 }
14092 }
14093
14094 /* If DECL is dependent, and refers to a function, then just return
14095 its name; we will look it up again during template instantiation. */
14096 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14097 {
14098 tree scope = ovl_scope (decl);
14099 if (TYPE_P (scope) && dependent_type_p (scope))
14100 return identifier;
14101 }
14102
14103 return decl;
14104 }
14105
14106 /* Parse a template-argument-list.
14107
14108 template-argument-list:
14109 template-argument ... [opt]
14110 template-argument-list , template-argument ... [opt]
14111
14112 Returns a TREE_VEC containing the arguments. */
14113
14114 static tree
14115 cp_parser_template_argument_list (cp_parser* parser)
14116 {
14117 tree fixed_args[10];
14118 unsigned n_args = 0;
14119 unsigned alloced = 10;
14120 tree *arg_ary = fixed_args;
14121 tree vec;
14122 bool saved_in_template_argument_list_p;
14123 bool saved_ice_p;
14124 bool saved_non_ice_p;
14125
14126 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14127 parser->in_template_argument_list_p = true;
14128 /* Even if the template-id appears in an integral
14129 constant-expression, the contents of the argument list do
14130 not. */
14131 saved_ice_p = parser->integral_constant_expression_p;
14132 parser->integral_constant_expression_p = false;
14133 saved_non_ice_p = parser->non_integral_constant_expression_p;
14134 parser->non_integral_constant_expression_p = false;
14135
14136 /* Parse the arguments. */
14137 do
14138 {
14139 tree argument;
14140
14141 if (n_args)
14142 /* Consume the comma. */
14143 cp_lexer_consume_token (parser->lexer);
14144
14145 /* Parse the template-argument. */
14146 argument = cp_parser_template_argument (parser);
14147
14148 /* If the next token is an ellipsis, we're expanding a template
14149 argument pack. */
14150 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14151 {
14152 if (argument == error_mark_node)
14153 {
14154 cp_token *token = cp_lexer_peek_token (parser->lexer);
14155 error_at (token->location,
14156 "expected parameter pack before %<...%>");
14157 }
14158 /* Consume the `...' token. */
14159 cp_lexer_consume_token (parser->lexer);
14160
14161 /* Make the argument into a TYPE_PACK_EXPANSION or
14162 EXPR_PACK_EXPANSION. */
14163 argument = make_pack_expansion (argument);
14164 }
14165
14166 if (n_args == alloced)
14167 {
14168 alloced *= 2;
14169
14170 if (arg_ary == fixed_args)
14171 {
14172 arg_ary = XNEWVEC (tree, alloced);
14173 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14174 }
14175 else
14176 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14177 }
14178 arg_ary[n_args++] = argument;
14179 }
14180 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14181
14182 vec = make_tree_vec (n_args);
14183
14184 while (n_args--)
14185 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14186
14187 if (arg_ary != fixed_args)
14188 free (arg_ary);
14189 parser->non_integral_constant_expression_p = saved_non_ice_p;
14190 parser->integral_constant_expression_p = saved_ice_p;
14191 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14192 #ifdef ENABLE_CHECKING
14193 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14194 #endif
14195 return vec;
14196 }
14197
14198 /* Parse a template-argument.
14199
14200 template-argument:
14201 assignment-expression
14202 type-id
14203 id-expression
14204
14205 The representation is that of an assignment-expression, type-id, or
14206 id-expression -- except that the qualified id-expression is
14207 evaluated, so that the value returned is either a DECL or an
14208 OVERLOAD.
14209
14210 Although the standard says "assignment-expression", it forbids
14211 throw-expressions or assignments in the template argument.
14212 Therefore, we use "conditional-expression" instead. */
14213
14214 static tree
14215 cp_parser_template_argument (cp_parser* parser)
14216 {
14217 tree argument;
14218 bool template_p;
14219 bool address_p;
14220 bool maybe_type_id = false;
14221 cp_token *token = NULL, *argument_start_token = NULL;
14222 location_t loc = 0;
14223 cp_id_kind idk;
14224
14225 /* There's really no way to know what we're looking at, so we just
14226 try each alternative in order.
14227
14228 [temp.arg]
14229
14230 In a template-argument, an ambiguity between a type-id and an
14231 expression is resolved to a type-id, regardless of the form of
14232 the corresponding template-parameter.
14233
14234 Therefore, we try a type-id first. */
14235 cp_parser_parse_tentatively (parser);
14236 argument = cp_parser_template_type_arg (parser);
14237 /* If there was no error parsing the type-id but the next token is a
14238 '>>', our behavior depends on which dialect of C++ we're
14239 parsing. In C++98, we probably found a typo for '> >'. But there
14240 are type-id which are also valid expressions. For instance:
14241
14242 struct X { int operator >> (int); };
14243 template <int V> struct Foo {};
14244 Foo<X () >> 5> r;
14245
14246 Here 'X()' is a valid type-id of a function type, but the user just
14247 wanted to write the expression "X() >> 5". Thus, we remember that we
14248 found a valid type-id, but we still try to parse the argument as an
14249 expression to see what happens.
14250
14251 In C++0x, the '>>' will be considered two separate '>'
14252 tokens. */
14253 if (!cp_parser_error_occurred (parser)
14254 && cxx_dialect == cxx98
14255 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14256 {
14257 maybe_type_id = true;
14258 cp_parser_abort_tentative_parse (parser);
14259 }
14260 else
14261 {
14262 /* If the next token isn't a `,' or a `>', then this argument wasn't
14263 really finished. This means that the argument is not a valid
14264 type-id. */
14265 if (!cp_parser_next_token_ends_template_argument_p (parser))
14266 cp_parser_error (parser, "expected template-argument");
14267 /* If that worked, we're done. */
14268 if (cp_parser_parse_definitely (parser))
14269 return argument;
14270 }
14271 /* We're still not sure what the argument will be. */
14272 cp_parser_parse_tentatively (parser);
14273 /* Try a template. */
14274 argument_start_token = cp_lexer_peek_token (parser->lexer);
14275 argument = cp_parser_id_expression (parser,
14276 /*template_keyword_p=*/false,
14277 /*check_dependency_p=*/true,
14278 &template_p,
14279 /*declarator_p=*/false,
14280 /*optional_p=*/false);
14281 /* If the next token isn't a `,' or a `>', then this argument wasn't
14282 really finished. */
14283 if (!cp_parser_next_token_ends_template_argument_p (parser))
14284 cp_parser_error (parser, "expected template-argument");
14285 if (!cp_parser_error_occurred (parser))
14286 {
14287 /* Figure out what is being referred to. If the id-expression
14288 was for a class template specialization, then we will have a
14289 TYPE_DECL at this point. There is no need to do name lookup
14290 at this point in that case. */
14291 if (TREE_CODE (argument) != TYPE_DECL)
14292 argument = cp_parser_lookup_name (parser, argument,
14293 none_type,
14294 /*is_template=*/template_p,
14295 /*is_namespace=*/false,
14296 /*check_dependency=*/true,
14297 /*ambiguous_decls=*/NULL,
14298 argument_start_token->location);
14299 if (TREE_CODE (argument) != TEMPLATE_DECL
14300 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14301 cp_parser_error (parser, "expected template-name");
14302 }
14303 if (cp_parser_parse_definitely (parser))
14304 {
14305 if (TREE_DEPRECATED (argument))
14306 warn_deprecated_use (argument, NULL_TREE);
14307 return argument;
14308 }
14309 /* It must be a non-type argument. There permitted cases are given
14310 in [temp.arg.nontype]:
14311
14312 -- an integral constant-expression of integral or enumeration
14313 type; or
14314
14315 -- the name of a non-type template-parameter; or
14316
14317 -- the name of an object or function with external linkage...
14318
14319 -- the address of an object or function with external linkage...
14320
14321 -- a pointer to member... */
14322 /* Look for a non-type template parameter. */
14323 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14324 {
14325 cp_parser_parse_tentatively (parser);
14326 argument = cp_parser_primary_expression (parser,
14327 /*address_p=*/false,
14328 /*cast_p=*/false,
14329 /*template_arg_p=*/true,
14330 &idk);
14331 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14332 || !cp_parser_next_token_ends_template_argument_p (parser))
14333 cp_parser_simulate_error (parser);
14334 if (cp_parser_parse_definitely (parser))
14335 return argument;
14336 }
14337
14338 /* If the next token is "&", the argument must be the address of an
14339 object or function with external linkage. */
14340 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14341 if (address_p)
14342 {
14343 loc = cp_lexer_peek_token (parser->lexer)->location;
14344 cp_lexer_consume_token (parser->lexer);
14345 }
14346 /* See if we might have an id-expression. */
14347 token = cp_lexer_peek_token (parser->lexer);
14348 if (token->type == CPP_NAME
14349 || token->keyword == RID_OPERATOR
14350 || token->type == CPP_SCOPE
14351 || token->type == CPP_TEMPLATE_ID
14352 || token->type == CPP_NESTED_NAME_SPECIFIER)
14353 {
14354 cp_parser_parse_tentatively (parser);
14355 argument = cp_parser_primary_expression (parser,
14356 address_p,
14357 /*cast_p=*/false,
14358 /*template_arg_p=*/true,
14359 &idk);
14360 if (cp_parser_error_occurred (parser)
14361 || !cp_parser_next_token_ends_template_argument_p (parser))
14362 cp_parser_abort_tentative_parse (parser);
14363 else
14364 {
14365 tree probe;
14366
14367 if (INDIRECT_REF_P (argument))
14368 {
14369 /* Strip the dereference temporarily. */
14370 gcc_assert (REFERENCE_REF_P (argument));
14371 argument = TREE_OPERAND (argument, 0);
14372 }
14373
14374 /* If we're in a template, we represent a qualified-id referring
14375 to a static data member as a SCOPE_REF even if the scope isn't
14376 dependent so that we can check access control later. */
14377 probe = argument;
14378 if (TREE_CODE (probe) == SCOPE_REF)
14379 probe = TREE_OPERAND (probe, 1);
14380 if (VAR_P (probe))
14381 {
14382 /* A variable without external linkage might still be a
14383 valid constant-expression, so no error is issued here
14384 if the external-linkage check fails. */
14385 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14386 cp_parser_simulate_error (parser);
14387 }
14388 else if (is_overloaded_fn (argument))
14389 /* All overloaded functions are allowed; if the external
14390 linkage test does not pass, an error will be issued
14391 later. */
14392 ;
14393 else if (address_p
14394 && (TREE_CODE (argument) == OFFSET_REF
14395 || TREE_CODE (argument) == SCOPE_REF))
14396 /* A pointer-to-member. */
14397 ;
14398 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14399 ;
14400 else
14401 cp_parser_simulate_error (parser);
14402
14403 if (cp_parser_parse_definitely (parser))
14404 {
14405 if (address_p)
14406 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14407 tf_warning_or_error);
14408 else
14409 argument = convert_from_reference (argument);
14410 return argument;
14411 }
14412 }
14413 }
14414 /* If the argument started with "&", there are no other valid
14415 alternatives at this point. */
14416 if (address_p)
14417 {
14418 cp_parser_error (parser, "invalid non-type template argument");
14419 return error_mark_node;
14420 }
14421
14422 /* If the argument wasn't successfully parsed as a type-id followed
14423 by '>>', the argument can only be a constant expression now.
14424 Otherwise, we try parsing the constant-expression tentatively,
14425 because the argument could really be a type-id. */
14426 if (maybe_type_id)
14427 cp_parser_parse_tentatively (parser);
14428 argument = cp_parser_constant_expression (parser);
14429
14430 if (!maybe_type_id)
14431 return argument;
14432 if (!cp_parser_next_token_ends_template_argument_p (parser))
14433 cp_parser_error (parser, "expected template-argument");
14434 if (cp_parser_parse_definitely (parser))
14435 return argument;
14436 /* We did our best to parse the argument as a non type-id, but that
14437 was the only alternative that matched (albeit with a '>' after
14438 it). We can assume it's just a typo from the user, and a
14439 diagnostic will then be issued. */
14440 return cp_parser_template_type_arg (parser);
14441 }
14442
14443 /* Parse an explicit-instantiation.
14444
14445 explicit-instantiation:
14446 template declaration
14447
14448 Although the standard says `declaration', what it really means is:
14449
14450 explicit-instantiation:
14451 template decl-specifier-seq [opt] declarator [opt] ;
14452
14453 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14454 supposed to be allowed. A defect report has been filed about this
14455 issue.
14456
14457 GNU Extension:
14458
14459 explicit-instantiation:
14460 storage-class-specifier template
14461 decl-specifier-seq [opt] declarator [opt] ;
14462 function-specifier template
14463 decl-specifier-seq [opt] declarator [opt] ; */
14464
14465 static void
14466 cp_parser_explicit_instantiation (cp_parser* parser)
14467 {
14468 int declares_class_or_enum;
14469 cp_decl_specifier_seq decl_specifiers;
14470 tree extension_specifier = NULL_TREE;
14471
14472 timevar_push (TV_TEMPLATE_INST);
14473
14474 /* Look for an (optional) storage-class-specifier or
14475 function-specifier. */
14476 if (cp_parser_allow_gnu_extensions_p (parser))
14477 {
14478 extension_specifier
14479 = cp_parser_storage_class_specifier_opt (parser);
14480 if (!extension_specifier)
14481 extension_specifier
14482 = cp_parser_function_specifier_opt (parser,
14483 /*decl_specs=*/NULL);
14484 }
14485
14486 /* Look for the `template' keyword. */
14487 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14488 /* Let the front end know that we are processing an explicit
14489 instantiation. */
14490 begin_explicit_instantiation ();
14491 /* [temp.explicit] says that we are supposed to ignore access
14492 control while processing explicit instantiation directives. */
14493 push_deferring_access_checks (dk_no_check);
14494 /* Parse a decl-specifier-seq. */
14495 cp_parser_decl_specifier_seq (parser,
14496 CP_PARSER_FLAGS_OPTIONAL,
14497 &decl_specifiers,
14498 &declares_class_or_enum);
14499 /* If there was exactly one decl-specifier, and it declared a class,
14500 and there's no declarator, then we have an explicit type
14501 instantiation. */
14502 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14503 {
14504 tree type;
14505
14506 type = check_tag_decl (&decl_specifiers,
14507 /*explicit_type_instantiation_p=*/true);
14508 /* Turn access control back on for names used during
14509 template instantiation. */
14510 pop_deferring_access_checks ();
14511 if (type)
14512 do_type_instantiation (type, extension_specifier,
14513 /*complain=*/tf_error);
14514 }
14515 else
14516 {
14517 cp_declarator *declarator;
14518 tree decl;
14519
14520 /* Parse the declarator. */
14521 declarator
14522 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14523 /*ctor_dtor_or_conv_p=*/NULL,
14524 /*parenthesized_p=*/NULL,
14525 /*member_p=*/false,
14526 /*friend_p=*/false);
14527 if (declares_class_or_enum & 2)
14528 cp_parser_check_for_definition_in_return_type (declarator,
14529 decl_specifiers.type,
14530 decl_specifiers.locations[ds_type_spec]);
14531 if (declarator != cp_error_declarator)
14532 {
14533 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14534 permerror (decl_specifiers.locations[ds_inline],
14535 "explicit instantiation shall not use"
14536 " %<inline%> specifier");
14537 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14538 permerror (decl_specifiers.locations[ds_constexpr],
14539 "explicit instantiation shall not use"
14540 " %<constexpr%> specifier");
14541
14542 decl = grokdeclarator (declarator, &decl_specifiers,
14543 NORMAL, 0, &decl_specifiers.attributes);
14544 /* Turn access control back on for names used during
14545 template instantiation. */
14546 pop_deferring_access_checks ();
14547 /* Do the explicit instantiation. */
14548 do_decl_instantiation (decl, extension_specifier);
14549 }
14550 else
14551 {
14552 pop_deferring_access_checks ();
14553 /* Skip the body of the explicit instantiation. */
14554 cp_parser_skip_to_end_of_statement (parser);
14555 }
14556 }
14557 /* We're done with the instantiation. */
14558 end_explicit_instantiation ();
14559
14560 cp_parser_consume_semicolon_at_end_of_statement (parser);
14561
14562 timevar_pop (TV_TEMPLATE_INST);
14563 }
14564
14565 /* Parse an explicit-specialization.
14566
14567 explicit-specialization:
14568 template < > declaration
14569
14570 Although the standard says `declaration', what it really means is:
14571
14572 explicit-specialization:
14573 template <> decl-specifier [opt] init-declarator [opt] ;
14574 template <> function-definition
14575 template <> explicit-specialization
14576 template <> template-declaration */
14577
14578 static void
14579 cp_parser_explicit_specialization (cp_parser* parser)
14580 {
14581 bool need_lang_pop;
14582 cp_token *token = cp_lexer_peek_token (parser->lexer);
14583
14584 /* Look for the `template' keyword. */
14585 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14586 /* Look for the `<'. */
14587 cp_parser_require (parser, CPP_LESS, RT_LESS);
14588 /* Look for the `>'. */
14589 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14590 /* We have processed another parameter list. */
14591 ++parser->num_template_parameter_lists;
14592 /* [temp]
14593
14594 A template ... explicit specialization ... shall not have C
14595 linkage. */
14596 if (current_lang_name == lang_name_c)
14597 {
14598 error_at (token->location, "template specialization with C linkage");
14599 /* Give it C++ linkage to avoid confusing other parts of the
14600 front end. */
14601 push_lang_context (lang_name_cplusplus);
14602 need_lang_pop = true;
14603 }
14604 else
14605 need_lang_pop = false;
14606 /* Let the front end know that we are beginning a specialization. */
14607 if (!begin_specialization ())
14608 {
14609 end_specialization ();
14610 return;
14611 }
14612
14613 /* If the next keyword is `template', we need to figure out whether
14614 or not we're looking a template-declaration. */
14615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14616 {
14617 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14618 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14619 cp_parser_template_declaration_after_export (parser,
14620 /*member_p=*/false);
14621 else
14622 cp_parser_explicit_specialization (parser);
14623 }
14624 else
14625 /* Parse the dependent declaration. */
14626 cp_parser_single_declaration (parser,
14627 /*checks=*/NULL,
14628 /*member_p=*/false,
14629 /*explicit_specialization_p=*/true,
14630 /*friend_p=*/NULL);
14631 /* We're done with the specialization. */
14632 end_specialization ();
14633 /* For the erroneous case of a template with C linkage, we pushed an
14634 implicit C++ linkage scope; exit that scope now. */
14635 if (need_lang_pop)
14636 pop_lang_context ();
14637 /* We're done with this parameter list. */
14638 --parser->num_template_parameter_lists;
14639 }
14640
14641 /* Parse a type-specifier.
14642
14643 type-specifier:
14644 simple-type-specifier
14645 class-specifier
14646 enum-specifier
14647 elaborated-type-specifier
14648 cv-qualifier
14649
14650 GNU Extension:
14651
14652 type-specifier:
14653 __complex__
14654
14655 Returns a representation of the type-specifier. For a
14656 class-specifier, enum-specifier, or elaborated-type-specifier, a
14657 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14658
14659 The parser flags FLAGS is used to control type-specifier parsing.
14660
14661 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14662 in a decl-specifier-seq.
14663
14664 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14665 class-specifier, enum-specifier, or elaborated-type-specifier, then
14666 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14667 if a type is declared; 2 if it is defined. Otherwise, it is set to
14668 zero.
14669
14670 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14671 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14672 is set to FALSE. */
14673
14674 static tree
14675 cp_parser_type_specifier (cp_parser* parser,
14676 cp_parser_flags flags,
14677 cp_decl_specifier_seq *decl_specs,
14678 bool is_declaration,
14679 int* declares_class_or_enum,
14680 bool* is_cv_qualifier)
14681 {
14682 tree type_spec = NULL_TREE;
14683 cp_token *token;
14684 enum rid keyword;
14685 cp_decl_spec ds = ds_last;
14686
14687 /* Assume this type-specifier does not declare a new type. */
14688 if (declares_class_or_enum)
14689 *declares_class_or_enum = 0;
14690 /* And that it does not specify a cv-qualifier. */
14691 if (is_cv_qualifier)
14692 *is_cv_qualifier = false;
14693 /* Peek at the next token. */
14694 token = cp_lexer_peek_token (parser->lexer);
14695
14696 /* If we're looking at a keyword, we can use that to guide the
14697 production we choose. */
14698 keyword = token->keyword;
14699 switch (keyword)
14700 {
14701 case RID_ENUM:
14702 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14703 goto elaborated_type_specifier;
14704
14705 /* Look for the enum-specifier. */
14706 type_spec = cp_parser_enum_specifier (parser);
14707 /* If that worked, we're done. */
14708 if (type_spec)
14709 {
14710 if (declares_class_or_enum)
14711 *declares_class_or_enum = 2;
14712 if (decl_specs)
14713 cp_parser_set_decl_spec_type (decl_specs,
14714 type_spec,
14715 token,
14716 /*type_definition_p=*/true);
14717 return type_spec;
14718 }
14719 else
14720 goto elaborated_type_specifier;
14721
14722 /* Any of these indicate either a class-specifier, or an
14723 elaborated-type-specifier. */
14724 case RID_CLASS:
14725 case RID_STRUCT:
14726 case RID_UNION:
14727 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14728 goto elaborated_type_specifier;
14729
14730 /* Parse tentatively so that we can back up if we don't find a
14731 class-specifier. */
14732 cp_parser_parse_tentatively (parser);
14733 /* Look for the class-specifier. */
14734 type_spec = cp_parser_class_specifier (parser);
14735 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14736 /* If that worked, we're done. */
14737 if (cp_parser_parse_definitely (parser))
14738 {
14739 if (declares_class_or_enum)
14740 *declares_class_or_enum = 2;
14741 if (decl_specs)
14742 cp_parser_set_decl_spec_type (decl_specs,
14743 type_spec,
14744 token,
14745 /*type_definition_p=*/true);
14746 return type_spec;
14747 }
14748
14749 /* Fall through. */
14750 elaborated_type_specifier:
14751 /* We're declaring (not defining) a class or enum. */
14752 if (declares_class_or_enum)
14753 *declares_class_or_enum = 1;
14754
14755 /* Fall through. */
14756 case RID_TYPENAME:
14757 /* Look for an elaborated-type-specifier. */
14758 type_spec
14759 = (cp_parser_elaborated_type_specifier
14760 (parser,
14761 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14762 is_declaration));
14763 if (decl_specs)
14764 cp_parser_set_decl_spec_type (decl_specs,
14765 type_spec,
14766 token,
14767 /*type_definition_p=*/false);
14768 return type_spec;
14769
14770 case RID_CONST:
14771 ds = ds_const;
14772 if (is_cv_qualifier)
14773 *is_cv_qualifier = true;
14774 break;
14775
14776 case RID_VOLATILE:
14777 ds = ds_volatile;
14778 if (is_cv_qualifier)
14779 *is_cv_qualifier = true;
14780 break;
14781
14782 case RID_RESTRICT:
14783 ds = ds_restrict;
14784 if (is_cv_qualifier)
14785 *is_cv_qualifier = true;
14786 break;
14787
14788 case RID_COMPLEX:
14789 /* The `__complex__' keyword is a GNU extension. */
14790 ds = ds_complex;
14791 break;
14792
14793 default:
14794 break;
14795 }
14796
14797 /* Handle simple keywords. */
14798 if (ds != ds_last)
14799 {
14800 if (decl_specs)
14801 {
14802 set_and_check_decl_spec_loc (decl_specs, ds, token);
14803 decl_specs->any_specifiers_p = true;
14804 }
14805 return cp_lexer_consume_token (parser->lexer)->u.value;
14806 }
14807
14808 /* If we do not already have a type-specifier, assume we are looking
14809 at a simple-type-specifier. */
14810 type_spec = cp_parser_simple_type_specifier (parser,
14811 decl_specs,
14812 flags);
14813
14814 /* If we didn't find a type-specifier, and a type-specifier was not
14815 optional in this context, issue an error message. */
14816 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14817 {
14818 cp_parser_error (parser, "expected type specifier");
14819 return error_mark_node;
14820 }
14821
14822 return type_spec;
14823 }
14824
14825 /* Parse a simple-type-specifier.
14826
14827 simple-type-specifier:
14828 :: [opt] nested-name-specifier [opt] type-name
14829 :: [opt] nested-name-specifier template template-id
14830 char
14831 wchar_t
14832 bool
14833 short
14834 int
14835 long
14836 signed
14837 unsigned
14838 float
14839 double
14840 void
14841
14842 C++0x Extension:
14843
14844 simple-type-specifier:
14845 auto
14846 decltype ( expression )
14847 char16_t
14848 char32_t
14849 __underlying_type ( type-id )
14850
14851 GNU Extension:
14852
14853 simple-type-specifier:
14854 __int128
14855 __typeof__ unary-expression
14856 __typeof__ ( type-id )
14857 __typeof__ ( type-id ) { initializer-list , [opt] }
14858
14859 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14860 appropriately updated. */
14861
14862 static tree
14863 cp_parser_simple_type_specifier (cp_parser* parser,
14864 cp_decl_specifier_seq *decl_specs,
14865 cp_parser_flags flags)
14866 {
14867 tree type = NULL_TREE;
14868 cp_token *token;
14869 int idx;
14870
14871 /* Peek at the next token. */
14872 token = cp_lexer_peek_token (parser->lexer);
14873
14874 /* If we're looking at a keyword, things are easy. */
14875 switch (token->keyword)
14876 {
14877 case RID_CHAR:
14878 if (decl_specs)
14879 decl_specs->explicit_char_p = true;
14880 type = char_type_node;
14881 break;
14882 case RID_CHAR16:
14883 type = char16_type_node;
14884 break;
14885 case RID_CHAR32:
14886 type = char32_type_node;
14887 break;
14888 case RID_WCHAR:
14889 type = wchar_type_node;
14890 break;
14891 case RID_BOOL:
14892 type = boolean_type_node;
14893 break;
14894 case RID_SHORT:
14895 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14896 type = short_integer_type_node;
14897 break;
14898 case RID_INT:
14899 if (decl_specs)
14900 decl_specs->explicit_int_p = true;
14901 type = integer_type_node;
14902 break;
14903 case RID_INT_N_0:
14904 case RID_INT_N_1:
14905 case RID_INT_N_2:
14906 case RID_INT_N_3:
14907 idx = token->keyword - RID_INT_N_0;
14908 if (! int_n_enabled_p [idx])
14909 break;
14910 if (decl_specs)
14911 {
14912 decl_specs->explicit_intN_p = true;
14913 decl_specs->int_n_idx = idx;
14914 }
14915 type = int_n_trees [idx].signed_type;
14916 break;
14917 case RID_LONG:
14918 if (decl_specs)
14919 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14920 type = long_integer_type_node;
14921 break;
14922 case RID_SIGNED:
14923 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14924 type = integer_type_node;
14925 break;
14926 case RID_UNSIGNED:
14927 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14928 type = unsigned_type_node;
14929 break;
14930 case RID_FLOAT:
14931 type = float_type_node;
14932 break;
14933 case RID_DOUBLE:
14934 type = double_type_node;
14935 break;
14936 case RID_VOID:
14937 type = void_type_node;
14938 break;
14939
14940 case RID_AUTO:
14941 maybe_warn_cpp0x (CPP0X_AUTO);
14942 if (parser->auto_is_implicit_function_template_parm_p)
14943 {
14944 if (cxx_dialect >= cxx14)
14945 type = synthesize_implicit_template_parm (parser);
14946 else
14947 type = error_mark_node;
14948
14949 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14950 {
14951 if (cxx_dialect < cxx14)
14952 error_at (token->location,
14953 "use of %<auto%> in lambda parameter declaration "
14954 "only available with "
14955 "-std=c++14 or -std=gnu++14");
14956 }
14957 else if (cxx_dialect < cxx14)
14958 error_at (token->location,
14959 "use of %<auto%> in parameter declaration "
14960 "only available with "
14961 "-std=c++14 or -std=gnu++14");
14962 else
14963 pedwarn (token->location, OPT_Wpedantic,
14964 "ISO C++ forbids use of %<auto%> in parameter "
14965 "declaration");
14966 }
14967 else
14968 type = make_auto ();
14969 break;
14970
14971 case RID_DECLTYPE:
14972 /* Since DR 743, decltype can either be a simple-type-specifier by
14973 itself or begin a nested-name-specifier. Parsing it will replace
14974 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14975 handling below decide what to do. */
14976 cp_parser_decltype (parser);
14977 cp_lexer_set_token_position (parser->lexer, token);
14978 break;
14979
14980 case RID_TYPEOF:
14981 /* Consume the `typeof' token. */
14982 cp_lexer_consume_token (parser->lexer);
14983 /* Parse the operand to `typeof'. */
14984 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14985 /* If it is not already a TYPE, take its type. */
14986 if (!TYPE_P (type))
14987 type = finish_typeof (type);
14988
14989 if (decl_specs)
14990 cp_parser_set_decl_spec_type (decl_specs, type,
14991 token,
14992 /*type_definition_p=*/false);
14993
14994 return type;
14995
14996 case RID_UNDERLYING_TYPE:
14997 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14998 if (decl_specs)
14999 cp_parser_set_decl_spec_type (decl_specs, type,
15000 token,
15001 /*type_definition_p=*/false);
15002
15003 return type;
15004
15005 case RID_BASES:
15006 case RID_DIRECT_BASES:
15007 type = cp_parser_trait_expr (parser, token->keyword);
15008 if (decl_specs)
15009 cp_parser_set_decl_spec_type (decl_specs, type,
15010 token,
15011 /*type_definition_p=*/false);
15012 return type;
15013 default:
15014 break;
15015 }
15016
15017 /* If token is an already-parsed decltype not followed by ::,
15018 it's a simple-type-specifier. */
15019 if (token->type == CPP_DECLTYPE
15020 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15021 {
15022 type = token->u.value;
15023 if (decl_specs)
15024 {
15025 cp_parser_set_decl_spec_type (decl_specs, type,
15026 token,
15027 /*type_definition_p=*/false);
15028 /* Remember that we are handling a decltype in order to
15029 implement the resolution of DR 1510 when the argument
15030 isn't instantiation dependent. */
15031 decl_specs->decltype_p = true;
15032 }
15033 cp_lexer_consume_token (parser->lexer);
15034 return type;
15035 }
15036
15037 /* If the type-specifier was for a built-in type, we're done. */
15038 if (type)
15039 {
15040 /* Record the type. */
15041 if (decl_specs
15042 && (token->keyword != RID_SIGNED
15043 && token->keyword != RID_UNSIGNED
15044 && token->keyword != RID_SHORT
15045 && token->keyword != RID_LONG))
15046 cp_parser_set_decl_spec_type (decl_specs,
15047 type,
15048 token,
15049 /*type_definition_p=*/false);
15050 if (decl_specs)
15051 decl_specs->any_specifiers_p = true;
15052
15053 /* Consume the token. */
15054 cp_lexer_consume_token (parser->lexer);
15055
15056 if (type == error_mark_node)
15057 return error_mark_node;
15058
15059 /* There is no valid C++ program where a non-template type is
15060 followed by a "<". That usually indicates that the user thought
15061 that the type was a template. */
15062 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15063 token->location);
15064
15065 return TYPE_NAME (type);
15066 }
15067
15068 /* The type-specifier must be a user-defined type. */
15069 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15070 {
15071 bool qualified_p;
15072 bool global_p;
15073
15074 /* Don't gobble tokens or issue error messages if this is an
15075 optional type-specifier. */
15076 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15077 cp_parser_parse_tentatively (parser);
15078
15079 /* Look for the optional `::' operator. */
15080 global_p
15081 = (cp_parser_global_scope_opt (parser,
15082 /*current_scope_valid_p=*/false)
15083 != NULL_TREE);
15084 /* Look for the nested-name specifier. */
15085 qualified_p
15086 = (cp_parser_nested_name_specifier_opt (parser,
15087 /*typename_keyword_p=*/false,
15088 /*check_dependency_p=*/true,
15089 /*type_p=*/false,
15090 /*is_declaration=*/false)
15091 != NULL_TREE);
15092 token = cp_lexer_peek_token (parser->lexer);
15093 /* If we have seen a nested-name-specifier, and the next token
15094 is `template', then we are using the template-id production. */
15095 if (parser->scope
15096 && cp_parser_optional_template_keyword (parser))
15097 {
15098 /* Look for the template-id. */
15099 type = cp_parser_template_id (parser,
15100 /*template_keyword_p=*/true,
15101 /*check_dependency_p=*/true,
15102 none_type,
15103 /*is_declaration=*/false);
15104 /* If the template-id did not name a type, we are out of
15105 luck. */
15106 if (TREE_CODE (type) != TYPE_DECL)
15107 {
15108 cp_parser_error (parser, "expected template-id for type");
15109 type = NULL_TREE;
15110 }
15111 }
15112 /* Otherwise, look for a type-name. */
15113 else
15114 type = cp_parser_type_name (parser);
15115 /* Keep track of all name-lookups performed in class scopes. */
15116 if (type
15117 && !global_p
15118 && !qualified_p
15119 && TREE_CODE (type) == TYPE_DECL
15120 && identifier_p (DECL_NAME (type)))
15121 maybe_note_name_used_in_class (DECL_NAME (type), type);
15122 /* If it didn't work out, we don't have a TYPE. */
15123 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15124 && !cp_parser_parse_definitely (parser))
15125 type = NULL_TREE;
15126 if (type && decl_specs)
15127 cp_parser_set_decl_spec_type (decl_specs, type,
15128 token,
15129 /*type_definition_p=*/false);
15130 }
15131
15132 /* If we didn't get a type-name, issue an error message. */
15133 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15134 {
15135 cp_parser_error (parser, "expected type-name");
15136 return error_mark_node;
15137 }
15138
15139 if (type && type != error_mark_node)
15140 {
15141 /* See if TYPE is an Objective-C type, and if so, parse and
15142 accept any protocol references following it. Do this before
15143 the cp_parser_check_for_invalid_template_id() call, because
15144 Objective-C types can be followed by '<...>' which would
15145 enclose protocol names rather than template arguments, and so
15146 everything is fine. */
15147 if (c_dialect_objc () && !parser->scope
15148 && (objc_is_id (type) || objc_is_class_name (type)))
15149 {
15150 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15151 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15152
15153 /* Clobber the "unqualified" type previously entered into
15154 DECL_SPECS with the new, improved protocol-qualified version. */
15155 if (decl_specs)
15156 decl_specs->type = qual_type;
15157
15158 return qual_type;
15159 }
15160
15161 /* There is no valid C++ program where a non-template type is
15162 followed by a "<". That usually indicates that the user
15163 thought that the type was a template. */
15164 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15165 none_type,
15166 token->location);
15167 }
15168
15169 return type;
15170 }
15171
15172 /* Parse a type-name.
15173
15174 type-name:
15175 class-name
15176 enum-name
15177 typedef-name
15178 simple-template-id [in c++0x]
15179
15180 enum-name:
15181 identifier
15182
15183 typedef-name:
15184 identifier
15185
15186 Returns a TYPE_DECL for the type. */
15187
15188 static tree
15189 cp_parser_type_name (cp_parser* parser)
15190 {
15191 tree type_decl;
15192
15193 /* We can't know yet whether it is a class-name or not. */
15194 cp_parser_parse_tentatively (parser);
15195 /* Try a class-name. */
15196 type_decl = cp_parser_class_name (parser,
15197 /*typename_keyword_p=*/false,
15198 /*template_keyword_p=*/false,
15199 none_type,
15200 /*check_dependency_p=*/true,
15201 /*class_head_p=*/false,
15202 /*is_declaration=*/false);
15203 /* If it's not a class-name, keep looking. */
15204 if (!cp_parser_parse_definitely (parser))
15205 {
15206 if (cxx_dialect < cxx11)
15207 /* It must be a typedef-name or an enum-name. */
15208 return cp_parser_nonclass_name (parser);
15209
15210 cp_parser_parse_tentatively (parser);
15211 /* It is either a simple-template-id representing an
15212 instantiation of an alias template... */
15213 type_decl = cp_parser_template_id (parser,
15214 /*template_keyword_p=*/false,
15215 /*check_dependency_p=*/true,
15216 none_type,
15217 /*is_declaration=*/false);
15218 /* Note that this must be an instantiation of an alias template
15219 because [temp.names]/6 says:
15220
15221 A template-id that names an alias template specialization
15222 is a type-name.
15223
15224 Whereas [temp.names]/7 says:
15225
15226 A simple-template-id that names a class template
15227 specialization is a class-name. */
15228 if (type_decl != NULL_TREE
15229 && TREE_CODE (type_decl) == TYPE_DECL
15230 && TYPE_DECL_ALIAS_P (type_decl))
15231 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15232 else
15233 cp_parser_simulate_error (parser);
15234
15235 if (!cp_parser_parse_definitely (parser))
15236 /* ... Or a typedef-name or an enum-name. */
15237 return cp_parser_nonclass_name (parser);
15238 }
15239
15240 return type_decl;
15241 }
15242
15243 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15244
15245 enum-name:
15246 identifier
15247
15248 typedef-name:
15249 identifier
15250
15251 Returns a TYPE_DECL for the type. */
15252
15253 static tree
15254 cp_parser_nonclass_name (cp_parser* parser)
15255 {
15256 tree type_decl;
15257 tree identifier;
15258
15259 cp_token *token = cp_lexer_peek_token (parser->lexer);
15260 identifier = cp_parser_identifier (parser);
15261 if (identifier == error_mark_node)
15262 return error_mark_node;
15263
15264 /* Look up the type-name. */
15265 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15266
15267 type_decl = strip_using_decl (type_decl);
15268
15269 if (TREE_CODE (type_decl) != TYPE_DECL
15270 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15271 {
15272 /* See if this is an Objective-C type. */
15273 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15274 tree type = objc_get_protocol_qualified_type (identifier, protos);
15275 if (type)
15276 type_decl = TYPE_NAME (type);
15277 }
15278
15279 /* Issue an error if we did not find a type-name. */
15280 if (TREE_CODE (type_decl) != TYPE_DECL
15281 /* In Objective-C, we have the complication that class names are
15282 normally type names and start declarations (eg, the
15283 "NSObject" in "NSObject *object;"), but can be used in an
15284 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15285 is an expression. So, a classname followed by a dot is not a
15286 valid type-name. */
15287 || (objc_is_class_name (TREE_TYPE (type_decl))
15288 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15289 {
15290 if (!cp_parser_simulate_error (parser))
15291 cp_parser_name_lookup_error (parser, identifier, type_decl,
15292 NLE_TYPE, token->location);
15293 return error_mark_node;
15294 }
15295 /* Remember that the name was used in the definition of the
15296 current class so that we can check later to see if the
15297 meaning would have been different after the class was
15298 entirely defined. */
15299 else if (type_decl != error_mark_node
15300 && !parser->scope)
15301 maybe_note_name_used_in_class (identifier, type_decl);
15302
15303 return type_decl;
15304 }
15305
15306 /* Parse an elaborated-type-specifier. Note that the grammar given
15307 here incorporates the resolution to DR68.
15308
15309 elaborated-type-specifier:
15310 class-key :: [opt] nested-name-specifier [opt] identifier
15311 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15312 enum-key :: [opt] nested-name-specifier [opt] identifier
15313 typename :: [opt] nested-name-specifier identifier
15314 typename :: [opt] nested-name-specifier template [opt]
15315 template-id
15316
15317 GNU extension:
15318
15319 elaborated-type-specifier:
15320 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15321 class-key attributes :: [opt] nested-name-specifier [opt]
15322 template [opt] template-id
15323 enum attributes :: [opt] nested-name-specifier [opt] identifier
15324
15325 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15326 declared `friend'. If IS_DECLARATION is TRUE, then this
15327 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15328 something is being declared.
15329
15330 Returns the TYPE specified. */
15331
15332 static tree
15333 cp_parser_elaborated_type_specifier (cp_parser* parser,
15334 bool is_friend,
15335 bool is_declaration)
15336 {
15337 enum tag_types tag_type;
15338 tree identifier;
15339 tree type = NULL_TREE;
15340 tree attributes = NULL_TREE;
15341 tree globalscope;
15342 cp_token *token = NULL;
15343
15344 /* See if we're looking at the `enum' keyword. */
15345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15346 {
15347 /* Consume the `enum' token. */
15348 cp_lexer_consume_token (parser->lexer);
15349 /* Remember that it's an enumeration type. */
15350 tag_type = enum_type;
15351 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15352 enums) is used here. */
15353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15354 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15355 {
15356 pedwarn (input_location, 0, "elaborated-type-specifier "
15357 "for a scoped enum must not use the %<%D%> keyword",
15358 cp_lexer_peek_token (parser->lexer)->u.value);
15359 /* Consume the `struct' or `class' and parse it anyway. */
15360 cp_lexer_consume_token (parser->lexer);
15361 }
15362 /* Parse the attributes. */
15363 attributes = cp_parser_attributes_opt (parser);
15364 }
15365 /* Or, it might be `typename'. */
15366 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15367 RID_TYPENAME))
15368 {
15369 /* Consume the `typename' token. */
15370 cp_lexer_consume_token (parser->lexer);
15371 /* Remember that it's a `typename' type. */
15372 tag_type = typename_type;
15373 }
15374 /* Otherwise it must be a class-key. */
15375 else
15376 {
15377 tag_type = cp_parser_class_key (parser);
15378 if (tag_type == none_type)
15379 return error_mark_node;
15380 /* Parse the attributes. */
15381 attributes = cp_parser_attributes_opt (parser);
15382 }
15383
15384 /* Look for the `::' operator. */
15385 globalscope = cp_parser_global_scope_opt (parser,
15386 /*current_scope_valid_p=*/false);
15387 /* Look for the nested-name-specifier. */
15388 if (tag_type == typename_type && !globalscope)
15389 {
15390 if (!cp_parser_nested_name_specifier (parser,
15391 /*typename_keyword_p=*/true,
15392 /*check_dependency_p=*/true,
15393 /*type_p=*/true,
15394 is_declaration))
15395 return error_mark_node;
15396 }
15397 else
15398 /* Even though `typename' is not present, the proposed resolution
15399 to Core Issue 180 says that in `class A<T>::B', `B' should be
15400 considered a type-name, even if `A<T>' is dependent. */
15401 cp_parser_nested_name_specifier_opt (parser,
15402 /*typename_keyword_p=*/true,
15403 /*check_dependency_p=*/true,
15404 /*type_p=*/true,
15405 is_declaration);
15406 /* For everything but enumeration types, consider a template-id.
15407 For an enumeration type, consider only a plain identifier. */
15408 if (tag_type != enum_type)
15409 {
15410 bool template_p = false;
15411 tree decl;
15412
15413 /* Allow the `template' keyword. */
15414 template_p = cp_parser_optional_template_keyword (parser);
15415 /* If we didn't see `template', we don't know if there's a
15416 template-id or not. */
15417 if (!template_p)
15418 cp_parser_parse_tentatively (parser);
15419 /* Parse the template-id. */
15420 token = cp_lexer_peek_token (parser->lexer);
15421 decl = cp_parser_template_id (parser, template_p,
15422 /*check_dependency_p=*/true,
15423 tag_type,
15424 is_declaration);
15425 /* If we didn't find a template-id, look for an ordinary
15426 identifier. */
15427 if (!template_p && !cp_parser_parse_definitely (parser))
15428 ;
15429 /* We can get here when cp_parser_template_id, called by
15430 cp_parser_class_name with tag_type == none_type, succeeds
15431 and caches a BASELINK. Then, when called again here,
15432 instead of failing and returning an error_mark_node
15433 returns it (see template/typename17.C in C++11).
15434 ??? Could we diagnose this earlier? */
15435 else if (tag_type == typename_type && BASELINK_P (decl))
15436 {
15437 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15438 type = error_mark_node;
15439 }
15440 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15441 in effect, then we must assume that, upon instantiation, the
15442 template will correspond to a class. */
15443 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15444 && tag_type == typename_type)
15445 type = make_typename_type (parser->scope, decl,
15446 typename_type,
15447 /*complain=*/tf_error);
15448 /* If the `typename' keyword is in effect and DECL is not a type
15449 decl, then type is non existent. */
15450 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15451 ;
15452 else if (TREE_CODE (decl) == TYPE_DECL)
15453 type = check_elaborated_type_specifier (tag_type, decl,
15454 /*allow_template_p=*/true);
15455 else if (decl == error_mark_node)
15456 type = error_mark_node;
15457 }
15458
15459 if (!type)
15460 {
15461 token = cp_lexer_peek_token (parser->lexer);
15462 identifier = cp_parser_identifier (parser);
15463
15464 if (identifier == error_mark_node)
15465 {
15466 parser->scope = NULL_TREE;
15467 return error_mark_node;
15468 }
15469
15470 /* For a `typename', we needn't call xref_tag. */
15471 if (tag_type == typename_type
15472 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15473 return cp_parser_make_typename_type (parser, identifier,
15474 token->location);
15475
15476 /* Template parameter lists apply only if we are not within a
15477 function parameter list. */
15478 bool template_parm_lists_apply
15479 = parser->num_template_parameter_lists;
15480 if (template_parm_lists_apply)
15481 for (cp_binding_level *s = current_binding_level;
15482 s && s->kind != sk_template_parms;
15483 s = s->level_chain)
15484 if (s->kind == sk_function_parms)
15485 template_parm_lists_apply = false;
15486
15487 /* Look up a qualified name in the usual way. */
15488 if (parser->scope)
15489 {
15490 tree decl;
15491 tree ambiguous_decls;
15492
15493 decl = cp_parser_lookup_name (parser, identifier,
15494 tag_type,
15495 /*is_template=*/false,
15496 /*is_namespace=*/false,
15497 /*check_dependency=*/true,
15498 &ambiguous_decls,
15499 token->location);
15500
15501 /* If the lookup was ambiguous, an error will already have been
15502 issued. */
15503 if (ambiguous_decls)
15504 return error_mark_node;
15505
15506 /* If we are parsing friend declaration, DECL may be a
15507 TEMPLATE_DECL tree node here. However, we need to check
15508 whether this TEMPLATE_DECL results in valid code. Consider
15509 the following example:
15510
15511 namespace N {
15512 template <class T> class C {};
15513 }
15514 class X {
15515 template <class T> friend class N::C; // #1, valid code
15516 };
15517 template <class T> class Y {
15518 friend class N::C; // #2, invalid code
15519 };
15520
15521 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15522 name lookup of `N::C'. We see that friend declaration must
15523 be template for the code to be valid. Note that
15524 processing_template_decl does not work here since it is
15525 always 1 for the above two cases. */
15526
15527 decl = (cp_parser_maybe_treat_template_as_class
15528 (decl, /*tag_name_p=*/is_friend
15529 && template_parm_lists_apply));
15530
15531 if (TREE_CODE (decl) != TYPE_DECL)
15532 {
15533 cp_parser_diagnose_invalid_type_name (parser,
15534 identifier,
15535 token->location);
15536 return error_mark_node;
15537 }
15538
15539 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15540 {
15541 bool allow_template = (template_parm_lists_apply
15542 || DECL_SELF_REFERENCE_P (decl));
15543 type = check_elaborated_type_specifier (tag_type, decl,
15544 allow_template);
15545
15546 if (type == error_mark_node)
15547 return error_mark_node;
15548 }
15549
15550 /* Forward declarations of nested types, such as
15551
15552 class C1::C2;
15553 class C1::C2::C3;
15554
15555 are invalid unless all components preceding the final '::'
15556 are complete. If all enclosing types are complete, these
15557 declarations become merely pointless.
15558
15559 Invalid forward declarations of nested types are errors
15560 caught elsewhere in parsing. Those that are pointless arrive
15561 here. */
15562
15563 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15564 && !is_friend && !processing_explicit_instantiation)
15565 warning (0, "declaration %qD does not declare anything", decl);
15566
15567 type = TREE_TYPE (decl);
15568 }
15569 else
15570 {
15571 /* An elaborated-type-specifier sometimes introduces a new type and
15572 sometimes names an existing type. Normally, the rule is that it
15573 introduces a new type only if there is not an existing type of
15574 the same name already in scope. For example, given:
15575
15576 struct S {};
15577 void f() { struct S s; }
15578
15579 the `struct S' in the body of `f' is the same `struct S' as in
15580 the global scope; the existing definition is used. However, if
15581 there were no global declaration, this would introduce a new
15582 local class named `S'.
15583
15584 An exception to this rule applies to the following code:
15585
15586 namespace N { struct S; }
15587
15588 Here, the elaborated-type-specifier names a new type
15589 unconditionally; even if there is already an `S' in the
15590 containing scope this declaration names a new type.
15591 This exception only applies if the elaborated-type-specifier
15592 forms the complete declaration:
15593
15594 [class.name]
15595
15596 A declaration consisting solely of `class-key identifier ;' is
15597 either a redeclaration of the name in the current scope or a
15598 forward declaration of the identifier as a class name. It
15599 introduces the name into the current scope.
15600
15601 We are in this situation precisely when the next token is a `;'.
15602
15603 An exception to the exception is that a `friend' declaration does
15604 *not* name a new type; i.e., given:
15605
15606 struct S { friend struct T; };
15607
15608 `T' is not a new type in the scope of `S'.
15609
15610 Also, `new struct S' or `sizeof (struct S)' never results in the
15611 definition of a new type; a new type can only be declared in a
15612 declaration context. */
15613
15614 tag_scope ts;
15615 bool template_p;
15616
15617 if (is_friend)
15618 /* Friends have special name lookup rules. */
15619 ts = ts_within_enclosing_non_class;
15620 else if (is_declaration
15621 && cp_lexer_next_token_is (parser->lexer,
15622 CPP_SEMICOLON))
15623 /* This is a `class-key identifier ;' */
15624 ts = ts_current;
15625 else
15626 ts = ts_global;
15627
15628 template_p =
15629 (template_parm_lists_apply
15630 && (cp_parser_next_token_starts_class_definition_p (parser)
15631 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15632 /* An unqualified name was used to reference this type, so
15633 there were no qualifying templates. */
15634 if (template_parm_lists_apply
15635 && !cp_parser_check_template_parameters (parser,
15636 /*num_templates=*/0,
15637 token->location,
15638 /*declarator=*/NULL))
15639 return error_mark_node;
15640 type = xref_tag (tag_type, identifier, ts, template_p);
15641 }
15642 }
15643
15644 if (type == error_mark_node)
15645 return error_mark_node;
15646
15647 /* Allow attributes on forward declarations of classes. */
15648 if (attributes)
15649 {
15650 if (TREE_CODE (type) == TYPENAME_TYPE)
15651 warning (OPT_Wattributes,
15652 "attributes ignored on uninstantiated type");
15653 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15654 && ! processing_explicit_instantiation)
15655 warning (OPT_Wattributes,
15656 "attributes ignored on template instantiation");
15657 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15658 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15659 else
15660 warning (OPT_Wattributes,
15661 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15662 }
15663
15664 if (tag_type != enum_type)
15665 {
15666 /* Indicate whether this class was declared as a `class' or as a
15667 `struct'. */
15668 if (TREE_CODE (type) == RECORD_TYPE)
15669 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15670 cp_parser_check_class_key (tag_type, type);
15671 }
15672
15673 /* A "<" cannot follow an elaborated type specifier. If that
15674 happens, the user was probably trying to form a template-id. */
15675 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15676 token->location);
15677
15678 return type;
15679 }
15680
15681 /* Parse an enum-specifier.
15682
15683 enum-specifier:
15684 enum-head { enumerator-list [opt] }
15685 enum-head { enumerator-list , } [C++0x]
15686
15687 enum-head:
15688 enum-key identifier [opt] enum-base [opt]
15689 enum-key nested-name-specifier identifier enum-base [opt]
15690
15691 enum-key:
15692 enum
15693 enum class [C++0x]
15694 enum struct [C++0x]
15695
15696 enum-base: [C++0x]
15697 : type-specifier-seq
15698
15699 opaque-enum-specifier:
15700 enum-key identifier enum-base [opt] ;
15701
15702 GNU Extensions:
15703 enum-key attributes[opt] identifier [opt] enum-base [opt]
15704 { enumerator-list [opt] }attributes[opt]
15705 enum-key attributes[opt] identifier [opt] enum-base [opt]
15706 { enumerator-list, }attributes[opt] [C++0x]
15707
15708 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15709 if the token stream isn't an enum-specifier after all. */
15710
15711 static tree
15712 cp_parser_enum_specifier (cp_parser* parser)
15713 {
15714 tree identifier;
15715 tree type = NULL_TREE;
15716 tree prev_scope;
15717 tree nested_name_specifier = NULL_TREE;
15718 tree attributes;
15719 bool scoped_enum_p = false;
15720 bool has_underlying_type = false;
15721 bool nested_being_defined = false;
15722 bool new_value_list = false;
15723 bool is_new_type = false;
15724 bool is_anonymous = false;
15725 tree underlying_type = NULL_TREE;
15726 cp_token *type_start_token = NULL;
15727 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15728
15729 parser->colon_corrects_to_scope_p = false;
15730
15731 /* Parse tentatively so that we can back up if we don't find a
15732 enum-specifier. */
15733 cp_parser_parse_tentatively (parser);
15734
15735 /* Caller guarantees that the current token is 'enum', an identifier
15736 possibly follows, and the token after that is an opening brace.
15737 If we don't have an identifier, fabricate an anonymous name for
15738 the enumeration being defined. */
15739 cp_lexer_consume_token (parser->lexer);
15740
15741 /* Parse the "class" or "struct", which indicates a scoped
15742 enumeration type in C++0x. */
15743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15744 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15745 {
15746 if (cxx_dialect < cxx11)
15747 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15748
15749 /* Consume the `struct' or `class' token. */
15750 cp_lexer_consume_token (parser->lexer);
15751
15752 scoped_enum_p = true;
15753 }
15754
15755 attributes = cp_parser_attributes_opt (parser);
15756
15757 /* Clear the qualification. */
15758 parser->scope = NULL_TREE;
15759 parser->qualifying_scope = NULL_TREE;
15760 parser->object_scope = NULL_TREE;
15761
15762 /* Figure out in what scope the declaration is being placed. */
15763 prev_scope = current_scope ();
15764
15765 type_start_token = cp_lexer_peek_token (parser->lexer);
15766
15767 push_deferring_access_checks (dk_no_check);
15768 nested_name_specifier
15769 = cp_parser_nested_name_specifier_opt (parser,
15770 /*typename_keyword_p=*/true,
15771 /*check_dependency_p=*/false,
15772 /*type_p=*/false,
15773 /*is_declaration=*/false);
15774
15775 if (nested_name_specifier)
15776 {
15777 tree name;
15778
15779 identifier = cp_parser_identifier (parser);
15780 name = cp_parser_lookup_name (parser, identifier,
15781 enum_type,
15782 /*is_template=*/false,
15783 /*is_namespace=*/false,
15784 /*check_dependency=*/true,
15785 /*ambiguous_decls=*/NULL,
15786 input_location);
15787 if (name && name != error_mark_node)
15788 {
15789 type = TREE_TYPE (name);
15790 if (TREE_CODE (type) == TYPENAME_TYPE)
15791 {
15792 /* Are template enums allowed in ISO? */
15793 if (template_parm_scope_p ())
15794 pedwarn (type_start_token->location, OPT_Wpedantic,
15795 "%qD is an enumeration template", name);
15796 /* ignore a typename reference, for it will be solved by name
15797 in start_enum. */
15798 type = NULL_TREE;
15799 }
15800 }
15801 else if (nested_name_specifier == error_mark_node)
15802 /* We already issued an error. */;
15803 else
15804 error_at (type_start_token->location,
15805 "%qD is not an enumerator-name", identifier);
15806 }
15807 else
15808 {
15809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15810 identifier = cp_parser_identifier (parser);
15811 else
15812 {
15813 identifier = make_anon_name ();
15814 is_anonymous = true;
15815 if (scoped_enum_p)
15816 error_at (type_start_token->location,
15817 "anonymous scoped enum is not allowed");
15818 }
15819 }
15820 pop_deferring_access_checks ();
15821
15822 /* Check for the `:' that denotes a specified underlying type in C++0x.
15823 Note that a ':' could also indicate a bitfield width, however. */
15824 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15825 {
15826 cp_decl_specifier_seq type_specifiers;
15827
15828 /* Consume the `:'. */
15829 cp_lexer_consume_token (parser->lexer);
15830
15831 /* Parse the type-specifier-seq. */
15832 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15833 /*is_trailing_return=*/false,
15834 &type_specifiers);
15835
15836 /* At this point this is surely not elaborated type specifier. */
15837 if (!cp_parser_parse_definitely (parser))
15838 return NULL_TREE;
15839
15840 if (cxx_dialect < cxx11)
15841 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15842
15843 has_underlying_type = true;
15844
15845 /* If that didn't work, stop. */
15846 if (type_specifiers.type != error_mark_node)
15847 {
15848 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15849 /*initialized=*/0, NULL);
15850 if (underlying_type == error_mark_node
15851 || check_for_bare_parameter_packs (underlying_type))
15852 underlying_type = NULL_TREE;
15853 }
15854 }
15855
15856 /* Look for the `{' but don't consume it yet. */
15857 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15858 {
15859 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15860 {
15861 cp_parser_error (parser, "expected %<{%>");
15862 if (has_underlying_type)
15863 {
15864 type = NULL_TREE;
15865 goto out;
15866 }
15867 }
15868 /* An opaque-enum-specifier must have a ';' here. */
15869 if ((scoped_enum_p || underlying_type)
15870 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15871 {
15872 cp_parser_error (parser, "expected %<;%> or %<{%>");
15873 if (has_underlying_type)
15874 {
15875 type = NULL_TREE;
15876 goto out;
15877 }
15878 }
15879 }
15880
15881 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15882 return NULL_TREE;
15883
15884 if (nested_name_specifier)
15885 {
15886 if (CLASS_TYPE_P (nested_name_specifier))
15887 {
15888 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15889 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15890 push_scope (nested_name_specifier);
15891 }
15892 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15893 {
15894 push_nested_namespace (nested_name_specifier);
15895 }
15896 }
15897
15898 /* Issue an error message if type-definitions are forbidden here. */
15899 if (!cp_parser_check_type_definition (parser))
15900 type = error_mark_node;
15901 else
15902 /* Create the new type. We do this before consuming the opening
15903 brace so the enum will be recorded as being on the line of its
15904 tag (or the 'enum' keyword, if there is no tag). */
15905 type = start_enum (identifier, type, underlying_type,
15906 scoped_enum_p, &is_new_type);
15907
15908 /* If the next token is not '{' it is an opaque-enum-specifier or an
15909 elaborated-type-specifier. */
15910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15911 {
15912 timevar_push (TV_PARSE_ENUM);
15913 if (nested_name_specifier
15914 && nested_name_specifier != error_mark_node)
15915 {
15916 /* The following catches invalid code such as:
15917 enum class S<int>::E { A, B, C }; */
15918 if (!processing_specialization
15919 && CLASS_TYPE_P (nested_name_specifier)
15920 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15921 error_at (type_start_token->location, "cannot add an enumerator "
15922 "list to a template instantiation");
15923
15924 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15925 {
15926 error_at (type_start_token->location,
15927 "%<%T::%E%> has not been declared",
15928 TYPE_CONTEXT (nested_name_specifier),
15929 nested_name_specifier);
15930 type = error_mark_node;
15931 }
15932 /* If that scope does not contain the scope in which the
15933 class was originally declared, the program is invalid. */
15934 else if (prev_scope && !is_ancestor (prev_scope,
15935 nested_name_specifier))
15936 {
15937 if (at_namespace_scope_p ())
15938 error_at (type_start_token->location,
15939 "declaration of %qD in namespace %qD which does not "
15940 "enclose %qD",
15941 type, prev_scope, nested_name_specifier);
15942 else
15943 error_at (type_start_token->location,
15944 "declaration of %qD in %qD which does not "
15945 "enclose %qD",
15946 type, prev_scope, nested_name_specifier);
15947 type = error_mark_node;
15948 }
15949 }
15950
15951 if (scoped_enum_p)
15952 begin_scope (sk_scoped_enum, type);
15953
15954 /* Consume the opening brace. */
15955 cp_lexer_consume_token (parser->lexer);
15956
15957 if (type == error_mark_node)
15958 ; /* Nothing to add */
15959 else if (OPAQUE_ENUM_P (type)
15960 || (cxx_dialect > cxx98 && processing_specialization))
15961 {
15962 new_value_list = true;
15963 SET_OPAQUE_ENUM_P (type, false);
15964 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15965 }
15966 else
15967 {
15968 error_at (type_start_token->location,
15969 "multiple definition of %q#T", type);
15970 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15971 "previous definition here");
15972 type = error_mark_node;
15973 }
15974
15975 if (type == error_mark_node)
15976 cp_parser_skip_to_end_of_block_or_statement (parser);
15977 /* If the next token is not '}', then there are some enumerators. */
15978 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15979 {
15980 if (is_anonymous && !scoped_enum_p)
15981 pedwarn (type_start_token->location, OPT_Wpedantic,
15982 "ISO C++ forbids empty anonymous enum");
15983 }
15984 else
15985 cp_parser_enumerator_list (parser, type);
15986
15987 /* Consume the final '}'. */
15988 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15989
15990 if (scoped_enum_p)
15991 finish_scope ();
15992 timevar_pop (TV_PARSE_ENUM);
15993 }
15994 else
15995 {
15996 /* If a ';' follows, then it is an opaque-enum-specifier
15997 and additional restrictions apply. */
15998 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15999 {
16000 if (is_anonymous)
16001 error_at (type_start_token->location,
16002 "opaque-enum-specifier without name");
16003 else if (nested_name_specifier)
16004 error_at (type_start_token->location,
16005 "opaque-enum-specifier must use a simple identifier");
16006 }
16007 }
16008
16009 /* Look for trailing attributes to apply to this enumeration, and
16010 apply them if appropriate. */
16011 if (cp_parser_allow_gnu_extensions_p (parser))
16012 {
16013 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16014 trailing_attr = chainon (trailing_attr, attributes);
16015 cplus_decl_attributes (&type,
16016 trailing_attr,
16017 (int) ATTR_FLAG_TYPE_IN_PLACE);
16018 }
16019
16020 /* Finish up the enumeration. */
16021 if (type != error_mark_node)
16022 {
16023 if (new_value_list)
16024 finish_enum_value_list (type);
16025 if (is_new_type)
16026 finish_enum (type);
16027 }
16028
16029 if (nested_name_specifier)
16030 {
16031 if (CLASS_TYPE_P (nested_name_specifier))
16032 {
16033 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16034 pop_scope (nested_name_specifier);
16035 }
16036 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16037 {
16038 pop_nested_namespace (nested_name_specifier);
16039 }
16040 }
16041 out:
16042 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16043 return type;
16044 }
16045
16046 /* Parse an enumerator-list. The enumerators all have the indicated
16047 TYPE.
16048
16049 enumerator-list:
16050 enumerator-definition
16051 enumerator-list , enumerator-definition */
16052
16053 static void
16054 cp_parser_enumerator_list (cp_parser* parser, tree type)
16055 {
16056 while (true)
16057 {
16058 /* Parse an enumerator-definition. */
16059 cp_parser_enumerator_definition (parser, type);
16060
16061 /* If the next token is not a ',', we've reached the end of
16062 the list. */
16063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16064 break;
16065 /* Otherwise, consume the `,' and keep going. */
16066 cp_lexer_consume_token (parser->lexer);
16067 /* If the next token is a `}', there is a trailing comma. */
16068 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16069 {
16070 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16071 pedwarn (input_location, OPT_Wpedantic,
16072 "comma at end of enumerator list");
16073 break;
16074 }
16075 }
16076 }
16077
16078 /* Parse an enumerator-definition. The enumerator has the indicated
16079 TYPE.
16080
16081 enumerator-definition:
16082 enumerator
16083 enumerator = constant-expression
16084
16085 enumerator:
16086 identifier */
16087
16088 static void
16089 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16090 {
16091 tree identifier;
16092 tree value;
16093 location_t loc;
16094
16095 /* Save the input location because we are interested in the location
16096 of the identifier and not the location of the explicit value. */
16097 loc = cp_lexer_peek_token (parser->lexer)->location;
16098
16099 /* Look for the identifier. */
16100 identifier = cp_parser_identifier (parser);
16101 if (identifier == error_mark_node)
16102 return;
16103
16104 /* If the next token is an '=', then there is an explicit value. */
16105 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16106 {
16107 /* Consume the `=' token. */
16108 cp_lexer_consume_token (parser->lexer);
16109 /* Parse the value. */
16110 value = cp_parser_constant_expression (parser);
16111 }
16112 else
16113 value = NULL_TREE;
16114
16115 /* If we are processing a template, make sure the initializer of the
16116 enumerator doesn't contain any bare template parameter pack. */
16117 if (check_for_bare_parameter_packs (value))
16118 value = error_mark_node;
16119
16120 /* Create the enumerator. */
16121 build_enumerator (identifier, value, type, loc);
16122 }
16123
16124 /* Parse a namespace-name.
16125
16126 namespace-name:
16127 original-namespace-name
16128 namespace-alias
16129
16130 Returns the NAMESPACE_DECL for the namespace. */
16131
16132 static tree
16133 cp_parser_namespace_name (cp_parser* parser)
16134 {
16135 tree identifier;
16136 tree namespace_decl;
16137
16138 cp_token *token = cp_lexer_peek_token (parser->lexer);
16139
16140 /* Get the name of the namespace. */
16141 identifier = cp_parser_identifier (parser);
16142 if (identifier == error_mark_node)
16143 return error_mark_node;
16144
16145 /* Look up the identifier in the currently active scope. Look only
16146 for namespaces, due to:
16147
16148 [basic.lookup.udir]
16149
16150 When looking up a namespace-name in a using-directive or alias
16151 definition, only namespace names are considered.
16152
16153 And:
16154
16155 [basic.lookup.qual]
16156
16157 During the lookup of a name preceding the :: scope resolution
16158 operator, object, function, and enumerator names are ignored.
16159
16160 (Note that cp_parser_qualifying_entity only calls this
16161 function if the token after the name is the scope resolution
16162 operator.) */
16163 namespace_decl = cp_parser_lookup_name (parser, identifier,
16164 none_type,
16165 /*is_template=*/false,
16166 /*is_namespace=*/true,
16167 /*check_dependency=*/true,
16168 /*ambiguous_decls=*/NULL,
16169 token->location);
16170 /* If it's not a namespace, issue an error. */
16171 if (namespace_decl == error_mark_node
16172 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16173 {
16174 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16175 error_at (token->location, "%qD is not a namespace-name", identifier);
16176 cp_parser_error (parser, "expected namespace-name");
16177 namespace_decl = error_mark_node;
16178 }
16179
16180 return namespace_decl;
16181 }
16182
16183 /* Parse a namespace-definition.
16184
16185 namespace-definition:
16186 named-namespace-definition
16187 unnamed-namespace-definition
16188
16189 named-namespace-definition:
16190 original-namespace-definition
16191 extension-namespace-definition
16192
16193 original-namespace-definition:
16194 namespace identifier { namespace-body }
16195
16196 extension-namespace-definition:
16197 namespace original-namespace-name { namespace-body }
16198
16199 unnamed-namespace-definition:
16200 namespace { namespace-body } */
16201
16202 static void
16203 cp_parser_namespace_definition (cp_parser* parser)
16204 {
16205 tree identifier, attribs;
16206 bool has_visibility;
16207 bool is_inline;
16208
16209 cp_ensure_no_omp_declare_simd (parser);
16210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16211 {
16212 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16213 is_inline = true;
16214 cp_lexer_consume_token (parser->lexer);
16215 }
16216 else
16217 is_inline = false;
16218
16219 /* Look for the `namespace' keyword. */
16220 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16221
16222 /* Get the name of the namespace. We do not attempt to distinguish
16223 between an original-namespace-definition and an
16224 extension-namespace-definition at this point. The semantic
16225 analysis routines are responsible for that. */
16226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16227 identifier = cp_parser_identifier (parser);
16228 else
16229 identifier = NULL_TREE;
16230
16231 /* Parse any specified attributes. */
16232 attribs = cp_parser_attributes_opt (parser);
16233
16234 /* Look for the `{' to start the namespace. */
16235 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16236 /* Start the namespace. */
16237 push_namespace (identifier);
16238
16239 /* "inline namespace" is equivalent to a stub namespace definition
16240 followed by a strong using directive. */
16241 if (is_inline)
16242 {
16243 tree name_space = current_namespace;
16244 /* Set up namespace association. */
16245 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16246 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16247 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16248 /* Import the contents of the inline namespace. */
16249 pop_namespace ();
16250 do_using_directive (name_space);
16251 push_namespace (identifier);
16252 }
16253
16254 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16255
16256 /* Parse the body of the namespace. */
16257 cp_parser_namespace_body (parser);
16258
16259 if (has_visibility)
16260 pop_visibility (1);
16261
16262 /* Finish the namespace. */
16263 pop_namespace ();
16264 /* Look for the final `}'. */
16265 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16266 }
16267
16268 /* Parse a namespace-body.
16269
16270 namespace-body:
16271 declaration-seq [opt] */
16272
16273 static void
16274 cp_parser_namespace_body (cp_parser* parser)
16275 {
16276 cp_parser_declaration_seq_opt (parser);
16277 }
16278
16279 /* Parse a namespace-alias-definition.
16280
16281 namespace-alias-definition:
16282 namespace identifier = qualified-namespace-specifier ; */
16283
16284 static void
16285 cp_parser_namespace_alias_definition (cp_parser* parser)
16286 {
16287 tree identifier;
16288 tree namespace_specifier;
16289
16290 cp_token *token = cp_lexer_peek_token (parser->lexer);
16291
16292 /* Look for the `namespace' keyword. */
16293 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16294 /* Look for the identifier. */
16295 identifier = cp_parser_identifier (parser);
16296 if (identifier == error_mark_node)
16297 return;
16298 /* Look for the `=' token. */
16299 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16300 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16301 {
16302 error_at (token->location, "%<namespace%> definition is not allowed here");
16303 /* Skip the definition. */
16304 cp_lexer_consume_token (parser->lexer);
16305 if (cp_parser_skip_to_closing_brace (parser))
16306 cp_lexer_consume_token (parser->lexer);
16307 return;
16308 }
16309 cp_parser_require (parser, CPP_EQ, RT_EQ);
16310 /* Look for the qualified-namespace-specifier. */
16311 namespace_specifier
16312 = cp_parser_qualified_namespace_specifier (parser);
16313 /* Look for the `;' token. */
16314 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16315
16316 /* Register the alias in the symbol table. */
16317 do_namespace_alias (identifier, namespace_specifier);
16318 }
16319
16320 /* Parse a qualified-namespace-specifier.
16321
16322 qualified-namespace-specifier:
16323 :: [opt] nested-name-specifier [opt] namespace-name
16324
16325 Returns a NAMESPACE_DECL corresponding to the specified
16326 namespace. */
16327
16328 static tree
16329 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16330 {
16331 /* Look for the optional `::'. */
16332 cp_parser_global_scope_opt (parser,
16333 /*current_scope_valid_p=*/false);
16334
16335 /* Look for the optional nested-name-specifier. */
16336 cp_parser_nested_name_specifier_opt (parser,
16337 /*typename_keyword_p=*/false,
16338 /*check_dependency_p=*/true,
16339 /*type_p=*/false,
16340 /*is_declaration=*/true);
16341
16342 return cp_parser_namespace_name (parser);
16343 }
16344
16345 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16346 access declaration.
16347
16348 using-declaration:
16349 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16350 using :: unqualified-id ;
16351
16352 access-declaration:
16353 qualified-id ;
16354
16355 */
16356
16357 static bool
16358 cp_parser_using_declaration (cp_parser* parser,
16359 bool access_declaration_p)
16360 {
16361 cp_token *token;
16362 bool typename_p = false;
16363 bool global_scope_p;
16364 tree decl;
16365 tree identifier;
16366 tree qscope;
16367 int oldcount = errorcount;
16368 cp_token *diag_token = NULL;
16369
16370 if (access_declaration_p)
16371 {
16372 diag_token = cp_lexer_peek_token (parser->lexer);
16373 cp_parser_parse_tentatively (parser);
16374 }
16375 else
16376 {
16377 /* Look for the `using' keyword. */
16378 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16379
16380 /* Peek at the next token. */
16381 token = cp_lexer_peek_token (parser->lexer);
16382 /* See if it's `typename'. */
16383 if (token->keyword == RID_TYPENAME)
16384 {
16385 /* Remember that we've seen it. */
16386 typename_p = true;
16387 /* Consume the `typename' token. */
16388 cp_lexer_consume_token (parser->lexer);
16389 }
16390 }
16391
16392 /* Look for the optional global scope qualification. */
16393 global_scope_p
16394 = (cp_parser_global_scope_opt (parser,
16395 /*current_scope_valid_p=*/false)
16396 != NULL_TREE);
16397
16398 /* If we saw `typename', or didn't see `::', then there must be a
16399 nested-name-specifier present. */
16400 if (typename_p || !global_scope_p)
16401 {
16402 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16403 /*check_dependency_p=*/true,
16404 /*type_p=*/false,
16405 /*is_declaration=*/true);
16406 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16407 {
16408 cp_parser_skip_to_end_of_block_or_statement (parser);
16409 return false;
16410 }
16411 }
16412 /* Otherwise, we could be in either of the two productions. In that
16413 case, treat the nested-name-specifier as optional. */
16414 else
16415 qscope = cp_parser_nested_name_specifier_opt (parser,
16416 /*typename_keyword_p=*/false,
16417 /*check_dependency_p=*/true,
16418 /*type_p=*/false,
16419 /*is_declaration=*/true);
16420 if (!qscope)
16421 qscope = global_namespace;
16422 else if (UNSCOPED_ENUM_P (qscope))
16423 qscope = CP_TYPE_CONTEXT (qscope);
16424
16425 if (access_declaration_p && cp_parser_error_occurred (parser))
16426 /* Something has already gone wrong; there's no need to parse
16427 further. Since an error has occurred, the return value of
16428 cp_parser_parse_definitely will be false, as required. */
16429 return cp_parser_parse_definitely (parser);
16430
16431 token = cp_lexer_peek_token (parser->lexer);
16432 /* Parse the unqualified-id. */
16433 identifier = cp_parser_unqualified_id (parser,
16434 /*template_keyword_p=*/false,
16435 /*check_dependency_p=*/true,
16436 /*declarator_p=*/true,
16437 /*optional_p=*/false);
16438
16439 if (access_declaration_p)
16440 {
16441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16442 cp_parser_simulate_error (parser);
16443 if (!cp_parser_parse_definitely (parser))
16444 return false;
16445 }
16446
16447 /* The function we call to handle a using-declaration is different
16448 depending on what scope we are in. */
16449 if (qscope == error_mark_node || identifier == error_mark_node)
16450 ;
16451 else if (!identifier_p (identifier)
16452 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16453 /* [namespace.udecl]
16454
16455 A using declaration shall not name a template-id. */
16456 error_at (token->location,
16457 "a template-id may not appear in a using-declaration");
16458 else
16459 {
16460 if (at_class_scope_p ())
16461 {
16462 /* Create the USING_DECL. */
16463 decl = do_class_using_decl (parser->scope, identifier);
16464
16465 if (decl && typename_p)
16466 USING_DECL_TYPENAME_P (decl) = 1;
16467
16468 if (check_for_bare_parameter_packs (decl))
16469 {
16470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16471 return false;
16472 }
16473 else
16474 /* Add it to the list of members in this class. */
16475 finish_member_declaration (decl);
16476 }
16477 else
16478 {
16479 decl = cp_parser_lookup_name_simple (parser,
16480 identifier,
16481 token->location);
16482 if (decl == error_mark_node)
16483 cp_parser_name_lookup_error (parser, identifier,
16484 decl, NLE_NULL,
16485 token->location);
16486 else if (check_for_bare_parameter_packs (decl))
16487 {
16488 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16489 return false;
16490 }
16491 else if (!at_namespace_scope_p ())
16492 do_local_using_decl (decl, qscope, identifier);
16493 else
16494 do_toplevel_using_decl (decl, qscope, identifier);
16495 }
16496 }
16497
16498 /* Look for the final `;'. */
16499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16500
16501 if (access_declaration_p && errorcount == oldcount)
16502 warning_at (diag_token->location, OPT_Wdeprecated,
16503 "access declarations are deprecated "
16504 "in favour of using-declarations; "
16505 "suggestion: add the %<using%> keyword");
16506
16507 return true;
16508 }
16509
16510 /* Parse an alias-declaration.
16511
16512 alias-declaration:
16513 using identifier attribute-specifier-seq [opt] = type-id */
16514
16515 static tree
16516 cp_parser_alias_declaration (cp_parser* parser)
16517 {
16518 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16519 location_t id_location;
16520 cp_declarator *declarator;
16521 cp_decl_specifier_seq decl_specs;
16522 bool member_p;
16523 const char *saved_message = NULL;
16524
16525 /* Look for the `using' keyword. */
16526 cp_token *using_token
16527 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16528 if (using_token == NULL)
16529 return error_mark_node;
16530
16531 id_location = cp_lexer_peek_token (parser->lexer)->location;
16532 id = cp_parser_identifier (parser);
16533 if (id == error_mark_node)
16534 return error_mark_node;
16535
16536 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16537 attributes = cp_parser_attributes_opt (parser);
16538 if (attributes == error_mark_node)
16539 return error_mark_node;
16540
16541 cp_parser_require (parser, CPP_EQ, RT_EQ);
16542
16543 if (cp_parser_error_occurred (parser))
16544 return error_mark_node;
16545
16546 cp_parser_commit_to_tentative_parse (parser);
16547
16548 /* Now we are going to parse the type-id of the declaration. */
16549
16550 /*
16551 [dcl.type]/3 says:
16552
16553 "A type-specifier-seq shall not define a class or enumeration
16554 unless it appears in the type-id of an alias-declaration (7.1.3) that
16555 is not the declaration of a template-declaration."
16556
16557 In other words, if we currently are in an alias template, the
16558 type-id should not define a type.
16559
16560 So let's set parser->type_definition_forbidden_message in that
16561 case; cp_parser_check_type_definition (called by
16562 cp_parser_class_specifier) will then emit an error if a type is
16563 defined in the type-id. */
16564 if (parser->num_template_parameter_lists)
16565 {
16566 saved_message = parser->type_definition_forbidden_message;
16567 parser->type_definition_forbidden_message =
16568 G_("types may not be defined in alias template declarations");
16569 }
16570
16571 type = cp_parser_type_id (parser);
16572
16573 /* Restore the error message if need be. */
16574 if (parser->num_template_parameter_lists)
16575 parser->type_definition_forbidden_message = saved_message;
16576
16577 if (type == error_mark_node
16578 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16579 {
16580 cp_parser_skip_to_end_of_block_or_statement (parser);
16581 return error_mark_node;
16582 }
16583
16584 /* A typedef-name can also be introduced by an alias-declaration. The
16585 identifier following the using keyword becomes a typedef-name. It has
16586 the same semantics as if it were introduced by the typedef
16587 specifier. In particular, it does not define a new type and it shall
16588 not appear in the type-id. */
16589
16590 clear_decl_specs (&decl_specs);
16591 decl_specs.type = type;
16592 if (attributes != NULL_TREE)
16593 {
16594 decl_specs.attributes = attributes;
16595 set_and_check_decl_spec_loc (&decl_specs,
16596 ds_attribute,
16597 attrs_token);
16598 }
16599 set_and_check_decl_spec_loc (&decl_specs,
16600 ds_typedef,
16601 using_token);
16602 set_and_check_decl_spec_loc (&decl_specs,
16603 ds_alias,
16604 using_token);
16605
16606 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16607 declarator->id_loc = id_location;
16608
16609 member_p = at_class_scope_p ();
16610 if (member_p)
16611 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16612 NULL_TREE, attributes);
16613 else
16614 decl = start_decl (declarator, &decl_specs, 0,
16615 attributes, NULL_TREE, &pushed_scope);
16616 if (decl == error_mark_node)
16617 return decl;
16618
16619 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16620
16621 if (pushed_scope)
16622 pop_scope (pushed_scope);
16623
16624 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16625 added into the symbol table; otherwise, return the TYPE_DECL. */
16626 if (DECL_LANG_SPECIFIC (decl)
16627 && DECL_TEMPLATE_INFO (decl)
16628 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16629 {
16630 decl = DECL_TI_TEMPLATE (decl);
16631 if (member_p)
16632 check_member_template (decl);
16633 }
16634
16635 return decl;
16636 }
16637
16638 /* Parse a using-directive.
16639
16640 using-directive:
16641 using namespace :: [opt] nested-name-specifier [opt]
16642 namespace-name ; */
16643
16644 static void
16645 cp_parser_using_directive (cp_parser* parser)
16646 {
16647 tree namespace_decl;
16648 tree attribs;
16649
16650 /* Look for the `using' keyword. */
16651 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16652 /* And the `namespace' keyword. */
16653 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16654 /* Look for the optional `::' operator. */
16655 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16656 /* And the optional nested-name-specifier. */
16657 cp_parser_nested_name_specifier_opt (parser,
16658 /*typename_keyword_p=*/false,
16659 /*check_dependency_p=*/true,
16660 /*type_p=*/false,
16661 /*is_declaration=*/true);
16662 /* Get the namespace being used. */
16663 namespace_decl = cp_parser_namespace_name (parser);
16664 /* And any specified attributes. */
16665 attribs = cp_parser_attributes_opt (parser);
16666 /* Update the symbol table. */
16667 parse_using_directive (namespace_decl, attribs);
16668 /* Look for the final `;'. */
16669 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16670 }
16671
16672 /* Parse an asm-definition.
16673
16674 asm-definition:
16675 asm ( string-literal ) ;
16676
16677 GNU Extension:
16678
16679 asm-definition:
16680 asm volatile [opt] ( string-literal ) ;
16681 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16682 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16683 : asm-operand-list [opt] ) ;
16684 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16685 : asm-operand-list [opt]
16686 : asm-clobber-list [opt] ) ;
16687 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16688 : asm-clobber-list [opt]
16689 : asm-goto-list ) ; */
16690
16691 static void
16692 cp_parser_asm_definition (cp_parser* parser)
16693 {
16694 tree string;
16695 tree outputs = NULL_TREE;
16696 tree inputs = NULL_TREE;
16697 tree clobbers = NULL_TREE;
16698 tree labels = NULL_TREE;
16699 tree asm_stmt;
16700 bool volatile_p = false;
16701 bool extended_p = false;
16702 bool invalid_inputs_p = false;
16703 bool invalid_outputs_p = false;
16704 bool goto_p = false;
16705 required_token missing = RT_NONE;
16706
16707 /* Look for the `asm' keyword. */
16708 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16709
16710 if (parser->in_function_body
16711 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16712 {
16713 error ("%<asm%> in %<constexpr%> function");
16714 cp_function_chain->invalid_constexpr = true;
16715 }
16716
16717 /* See if the next token is `volatile'. */
16718 if (cp_parser_allow_gnu_extensions_p (parser)
16719 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16720 {
16721 /* Remember that we saw the `volatile' keyword. */
16722 volatile_p = true;
16723 /* Consume the token. */
16724 cp_lexer_consume_token (parser->lexer);
16725 }
16726 if (cp_parser_allow_gnu_extensions_p (parser)
16727 && parser->in_function_body
16728 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16729 {
16730 /* Remember that we saw the `goto' keyword. */
16731 goto_p = true;
16732 /* Consume the token. */
16733 cp_lexer_consume_token (parser->lexer);
16734 }
16735 /* Look for the opening `('. */
16736 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16737 return;
16738 /* Look for the string. */
16739 string = cp_parser_string_literal (parser, false, false);
16740 if (string == error_mark_node)
16741 {
16742 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16743 /*consume_paren=*/true);
16744 return;
16745 }
16746
16747 /* If we're allowing GNU extensions, check for the extended assembly
16748 syntax. Unfortunately, the `:' tokens need not be separated by
16749 a space in C, and so, for compatibility, we tolerate that here
16750 too. Doing that means that we have to treat the `::' operator as
16751 two `:' tokens. */
16752 if (cp_parser_allow_gnu_extensions_p (parser)
16753 && parser->in_function_body
16754 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16755 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16756 {
16757 bool inputs_p = false;
16758 bool clobbers_p = false;
16759 bool labels_p = false;
16760
16761 /* The extended syntax was used. */
16762 extended_p = true;
16763
16764 /* Look for outputs. */
16765 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16766 {
16767 /* Consume the `:'. */
16768 cp_lexer_consume_token (parser->lexer);
16769 /* Parse the output-operands. */
16770 if (cp_lexer_next_token_is_not (parser->lexer,
16771 CPP_COLON)
16772 && cp_lexer_next_token_is_not (parser->lexer,
16773 CPP_SCOPE)
16774 && cp_lexer_next_token_is_not (parser->lexer,
16775 CPP_CLOSE_PAREN)
16776 && !goto_p)
16777 {
16778 outputs = cp_parser_asm_operand_list (parser);
16779 if (outputs == error_mark_node)
16780 invalid_outputs_p = true;
16781 }
16782 }
16783 /* If the next token is `::', there are no outputs, and the
16784 next token is the beginning of the inputs. */
16785 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16786 /* The inputs are coming next. */
16787 inputs_p = true;
16788
16789 /* Look for inputs. */
16790 if (inputs_p
16791 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16792 {
16793 /* Consume the `:' or `::'. */
16794 cp_lexer_consume_token (parser->lexer);
16795 /* Parse the output-operands. */
16796 if (cp_lexer_next_token_is_not (parser->lexer,
16797 CPP_COLON)
16798 && cp_lexer_next_token_is_not (parser->lexer,
16799 CPP_SCOPE)
16800 && cp_lexer_next_token_is_not (parser->lexer,
16801 CPP_CLOSE_PAREN))
16802 {
16803 inputs = cp_parser_asm_operand_list (parser);
16804 if (inputs == error_mark_node)
16805 invalid_inputs_p = true;
16806 }
16807 }
16808 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16809 /* The clobbers are coming next. */
16810 clobbers_p = true;
16811
16812 /* Look for clobbers. */
16813 if (clobbers_p
16814 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16815 {
16816 clobbers_p = true;
16817 /* Consume the `:' or `::'. */
16818 cp_lexer_consume_token (parser->lexer);
16819 /* Parse the clobbers. */
16820 if (cp_lexer_next_token_is_not (parser->lexer,
16821 CPP_COLON)
16822 && cp_lexer_next_token_is_not (parser->lexer,
16823 CPP_CLOSE_PAREN))
16824 clobbers = cp_parser_asm_clobber_list (parser);
16825 }
16826 else if (goto_p
16827 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16828 /* The labels are coming next. */
16829 labels_p = true;
16830
16831 /* Look for labels. */
16832 if (labels_p
16833 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16834 {
16835 labels_p = true;
16836 /* Consume the `:' or `::'. */
16837 cp_lexer_consume_token (parser->lexer);
16838 /* Parse the labels. */
16839 labels = cp_parser_asm_label_list (parser);
16840 }
16841
16842 if (goto_p && !labels_p)
16843 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16844 }
16845 else if (goto_p)
16846 missing = RT_COLON_SCOPE;
16847
16848 /* Look for the closing `)'. */
16849 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16850 missing ? missing : RT_CLOSE_PAREN))
16851 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16852 /*consume_paren=*/true);
16853 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16854
16855 if (!invalid_inputs_p && !invalid_outputs_p)
16856 {
16857 /* Create the ASM_EXPR. */
16858 if (parser->in_function_body)
16859 {
16860 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16861 inputs, clobbers, labels);
16862 /* If the extended syntax was not used, mark the ASM_EXPR. */
16863 if (!extended_p)
16864 {
16865 tree temp = asm_stmt;
16866 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16867 temp = TREE_OPERAND (temp, 0);
16868
16869 ASM_INPUT_P (temp) = 1;
16870 }
16871 }
16872 else
16873 symtab->finalize_toplevel_asm (string);
16874 }
16875 }
16876
16877 /* Declarators [gram.dcl.decl] */
16878
16879 /* Parse an init-declarator.
16880
16881 init-declarator:
16882 declarator initializer [opt]
16883
16884 GNU Extension:
16885
16886 init-declarator:
16887 declarator asm-specification [opt] attributes [opt] initializer [opt]
16888
16889 function-definition:
16890 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16891 function-body
16892 decl-specifier-seq [opt] declarator function-try-block
16893
16894 GNU Extension:
16895
16896 function-definition:
16897 __extension__ function-definition
16898
16899 TM Extension:
16900
16901 function-definition:
16902 decl-specifier-seq [opt] declarator function-transaction-block
16903
16904 The DECL_SPECIFIERS apply to this declarator. Returns a
16905 representation of the entity declared. If MEMBER_P is TRUE, then
16906 this declarator appears in a class scope. The new DECL created by
16907 this declarator is returned.
16908
16909 The CHECKS are access checks that should be performed once we know
16910 what entity is being declared (and, therefore, what classes have
16911 befriended it).
16912
16913 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16914 for a function-definition here as well. If the declarator is a
16915 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16916 be TRUE upon return. By that point, the function-definition will
16917 have been completely parsed.
16918
16919 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16920 is FALSE.
16921
16922 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16923 parsed declaration if it is an uninitialized single declarator not followed
16924 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16925 if present, will not be consumed. If returned, this declarator will be
16926 created with SD_INITIALIZED but will not call cp_finish_decl.
16927
16928 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16929 and there is an initializer, the pointed location_t is set to the
16930 location of the '=' or `(', or '{' in C++11 token introducing the
16931 initializer. */
16932
16933 static tree
16934 cp_parser_init_declarator (cp_parser* parser,
16935 cp_decl_specifier_seq *decl_specifiers,
16936 vec<deferred_access_check, va_gc> *checks,
16937 bool function_definition_allowed_p,
16938 bool member_p,
16939 int declares_class_or_enum,
16940 bool* function_definition_p,
16941 tree* maybe_range_for_decl,
16942 location_t* init_loc)
16943 {
16944 cp_token *token = NULL, *asm_spec_start_token = NULL,
16945 *attributes_start_token = NULL;
16946 cp_declarator *declarator;
16947 tree prefix_attributes;
16948 tree attributes = NULL;
16949 tree asm_specification;
16950 tree initializer;
16951 tree decl = NULL_TREE;
16952 tree scope;
16953 int is_initialized;
16954 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16955 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16956 "(...)". */
16957 enum cpp_ttype initialization_kind;
16958 bool is_direct_init = false;
16959 bool is_non_constant_init;
16960 int ctor_dtor_or_conv_p;
16961 bool friend_p = cp_parser_friend_p (decl_specifiers);
16962 tree pushed_scope = NULL_TREE;
16963 bool range_for_decl_p = false;
16964 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16965 location_t tmp_init_loc = UNKNOWN_LOCATION;
16966
16967 /* Gather the attributes that were provided with the
16968 decl-specifiers. */
16969 prefix_attributes = decl_specifiers->attributes;
16970
16971 /* Assume that this is not the declarator for a function
16972 definition. */
16973 if (function_definition_p)
16974 *function_definition_p = false;
16975
16976 /* Default arguments are only permitted for function parameters. */
16977 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16978 parser->default_arg_ok_p = false;
16979
16980 /* Defer access checks while parsing the declarator; we cannot know
16981 what names are accessible until we know what is being
16982 declared. */
16983 resume_deferring_access_checks ();
16984
16985 /* Parse the declarator. */
16986 token = cp_lexer_peek_token (parser->lexer);
16987 declarator
16988 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16989 &ctor_dtor_or_conv_p,
16990 /*parenthesized_p=*/NULL,
16991 member_p, friend_p);
16992 /* Gather up the deferred checks. */
16993 stop_deferring_access_checks ();
16994
16995 parser->default_arg_ok_p = saved_default_arg_ok_p;
16996
16997 /* If the DECLARATOR was erroneous, there's no need to go
16998 further. */
16999 if (declarator == cp_error_declarator)
17000 return error_mark_node;
17001
17002 /* Check that the number of template-parameter-lists is OK. */
17003 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17004 token->location))
17005 return error_mark_node;
17006
17007 if (declares_class_or_enum & 2)
17008 cp_parser_check_for_definition_in_return_type (declarator,
17009 decl_specifiers->type,
17010 decl_specifiers->locations[ds_type_spec]);
17011
17012 /* Figure out what scope the entity declared by the DECLARATOR is
17013 located in. `grokdeclarator' sometimes changes the scope, so
17014 we compute it now. */
17015 scope = get_scope_of_declarator (declarator);
17016
17017 /* Perform any lookups in the declared type which were thought to be
17018 dependent, but are not in the scope of the declarator. */
17019 decl_specifiers->type
17020 = maybe_update_decl_type (decl_specifiers->type, scope);
17021
17022 /* If we're allowing GNU extensions, look for an
17023 asm-specification. */
17024 if (cp_parser_allow_gnu_extensions_p (parser))
17025 {
17026 /* Look for an asm-specification. */
17027 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17028 asm_specification = cp_parser_asm_specification_opt (parser);
17029 }
17030 else
17031 asm_specification = NULL_TREE;
17032
17033 /* Look for attributes. */
17034 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17035 attributes = cp_parser_attributes_opt (parser);
17036
17037 /* Peek at the next token. */
17038 token = cp_lexer_peek_token (parser->lexer);
17039
17040 bool bogus_implicit_tmpl = false;
17041
17042 if (function_declarator_p (declarator))
17043 {
17044 /* Check to see if the token indicates the start of a
17045 function-definition. */
17046 if (cp_parser_token_starts_function_definition_p (token))
17047 {
17048 if (!function_definition_allowed_p)
17049 {
17050 /* If a function-definition should not appear here, issue an
17051 error message. */
17052 cp_parser_error (parser,
17053 "a function-definition is not allowed here");
17054 return error_mark_node;
17055 }
17056
17057 location_t func_brace_location
17058 = cp_lexer_peek_token (parser->lexer)->location;
17059
17060 /* Neither attributes nor an asm-specification are allowed
17061 on a function-definition. */
17062 if (asm_specification)
17063 error_at (asm_spec_start_token->location,
17064 "an asm-specification is not allowed "
17065 "on a function-definition");
17066 if (attributes)
17067 error_at (attributes_start_token->location,
17068 "attributes are not allowed "
17069 "on a function-definition");
17070 /* This is a function-definition. */
17071 *function_definition_p = true;
17072
17073 /* Parse the function definition. */
17074 if (member_p)
17075 decl = cp_parser_save_member_function_body (parser,
17076 decl_specifiers,
17077 declarator,
17078 prefix_attributes);
17079 else
17080 decl =
17081 (cp_parser_function_definition_from_specifiers_and_declarator
17082 (parser, decl_specifiers, prefix_attributes, declarator));
17083
17084 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17085 {
17086 /* This is where the prologue starts... */
17087 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17088 = func_brace_location;
17089 }
17090
17091 return decl;
17092 }
17093 }
17094 else if (parser->fully_implicit_function_template_p)
17095 {
17096 /* A non-template declaration involving a function parameter list
17097 containing an implicit template parameter will be made into a
17098 template. If the resulting declaration is not going to be an
17099 actual function then finish the template scope here to prevent it.
17100 An error message will be issued once we have a decl to talk about.
17101
17102 FIXME probably we should do type deduction rather than create an
17103 implicit template, but the standard currently doesn't allow it. */
17104 bogus_implicit_tmpl = true;
17105 finish_fully_implicit_template (parser, NULL_TREE);
17106 }
17107
17108 /* [dcl.dcl]
17109
17110 Only in function declarations for constructors, destructors, and
17111 type conversions can the decl-specifier-seq be omitted.
17112
17113 We explicitly postpone this check past the point where we handle
17114 function-definitions because we tolerate function-definitions
17115 that are missing their return types in some modes. */
17116 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17117 {
17118 cp_parser_error (parser,
17119 "expected constructor, destructor, or type conversion");
17120 return error_mark_node;
17121 }
17122
17123 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17124 if (token->type == CPP_EQ
17125 || token->type == CPP_OPEN_PAREN
17126 || token->type == CPP_OPEN_BRACE)
17127 {
17128 is_initialized = SD_INITIALIZED;
17129 initialization_kind = token->type;
17130 if (maybe_range_for_decl)
17131 *maybe_range_for_decl = error_mark_node;
17132 tmp_init_loc = token->location;
17133 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17134 *init_loc = tmp_init_loc;
17135
17136 if (token->type == CPP_EQ
17137 && function_declarator_p (declarator))
17138 {
17139 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17140 if (t2->keyword == RID_DEFAULT)
17141 is_initialized = SD_DEFAULTED;
17142 else if (t2->keyword == RID_DELETE)
17143 is_initialized = SD_DELETED;
17144 }
17145 }
17146 else
17147 {
17148 /* If the init-declarator isn't initialized and isn't followed by a
17149 `,' or `;', it's not a valid init-declarator. */
17150 if (token->type != CPP_COMMA
17151 && token->type != CPP_SEMICOLON)
17152 {
17153 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17154 range_for_decl_p = true;
17155 else
17156 {
17157 if (!maybe_range_for_decl)
17158 cp_parser_error (parser, "expected initializer");
17159 return error_mark_node;
17160 }
17161 }
17162 is_initialized = SD_UNINITIALIZED;
17163 initialization_kind = CPP_EOF;
17164 }
17165
17166 /* Because start_decl has side-effects, we should only call it if we
17167 know we're going ahead. By this point, we know that we cannot
17168 possibly be looking at any other construct. */
17169 cp_parser_commit_to_tentative_parse (parser);
17170
17171 /* Enter the newly declared entry in the symbol table. If we're
17172 processing a declaration in a class-specifier, we wait until
17173 after processing the initializer. */
17174 if (!member_p)
17175 {
17176 if (parser->in_unbraced_linkage_specification_p)
17177 decl_specifiers->storage_class = sc_extern;
17178 decl = start_decl (declarator, decl_specifiers,
17179 range_for_decl_p? SD_INITIALIZED : is_initialized,
17180 attributes, prefix_attributes, &pushed_scope);
17181 cp_finalize_omp_declare_simd (parser, decl);
17182 /* Adjust location of decl if declarator->id_loc is more appropriate:
17183 set, and decl wasn't merged with another decl, in which case its
17184 location would be different from input_location, and more accurate. */
17185 if (DECL_P (decl)
17186 && declarator->id_loc != UNKNOWN_LOCATION
17187 && DECL_SOURCE_LOCATION (decl) == input_location)
17188 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17189 }
17190 else if (scope)
17191 /* Enter the SCOPE. That way unqualified names appearing in the
17192 initializer will be looked up in SCOPE. */
17193 pushed_scope = push_scope (scope);
17194
17195 /* Perform deferred access control checks, now that we know in which
17196 SCOPE the declared entity resides. */
17197 if (!member_p && decl)
17198 {
17199 tree saved_current_function_decl = NULL_TREE;
17200
17201 /* If the entity being declared is a function, pretend that we
17202 are in its scope. If it is a `friend', it may have access to
17203 things that would not otherwise be accessible. */
17204 if (TREE_CODE (decl) == FUNCTION_DECL)
17205 {
17206 saved_current_function_decl = current_function_decl;
17207 current_function_decl = decl;
17208 }
17209
17210 /* Perform access checks for template parameters. */
17211 cp_parser_perform_template_parameter_access_checks (checks);
17212
17213 /* Perform the access control checks for the declarator and the
17214 decl-specifiers. */
17215 perform_deferred_access_checks (tf_warning_or_error);
17216
17217 /* Restore the saved value. */
17218 if (TREE_CODE (decl) == FUNCTION_DECL)
17219 current_function_decl = saved_current_function_decl;
17220 }
17221
17222 /* Parse the initializer. */
17223 initializer = NULL_TREE;
17224 is_direct_init = false;
17225 is_non_constant_init = true;
17226 if (is_initialized)
17227 {
17228 if (function_declarator_p (declarator))
17229 {
17230 if (initialization_kind == CPP_EQ)
17231 initializer = cp_parser_pure_specifier (parser);
17232 else
17233 {
17234 /* If the declaration was erroneous, we don't really
17235 know what the user intended, so just silently
17236 consume the initializer. */
17237 if (decl != error_mark_node)
17238 error_at (tmp_init_loc, "initializer provided for function");
17239 cp_parser_skip_to_closing_parenthesis (parser,
17240 /*recovering=*/true,
17241 /*or_comma=*/false,
17242 /*consume_paren=*/true);
17243 }
17244 }
17245 else
17246 {
17247 /* We want to record the extra mangling scope for in-class
17248 initializers of class members and initializers of static data
17249 member templates. The former involves deferring
17250 parsing of the initializer until end of class as with default
17251 arguments. So right here we only handle the latter. */
17252 if (!member_p && processing_template_decl)
17253 start_lambda_scope (decl);
17254 initializer = cp_parser_initializer (parser,
17255 &is_direct_init,
17256 &is_non_constant_init);
17257 if (!member_p && processing_template_decl)
17258 finish_lambda_scope ();
17259 if (initializer == error_mark_node)
17260 cp_parser_skip_to_end_of_statement (parser);
17261 }
17262 }
17263
17264 /* The old parser allows attributes to appear after a parenthesized
17265 initializer. Mark Mitchell proposed removing this functionality
17266 on the GCC mailing lists on 2002-08-13. This parser accepts the
17267 attributes -- but ignores them. */
17268 if (cp_parser_allow_gnu_extensions_p (parser)
17269 && initialization_kind == CPP_OPEN_PAREN)
17270 if (cp_parser_attributes_opt (parser))
17271 warning (OPT_Wattributes,
17272 "attributes after parenthesized initializer ignored");
17273
17274 /* And now complain about a non-function implicit template. */
17275 if (bogus_implicit_tmpl)
17276 error_at (DECL_SOURCE_LOCATION (decl),
17277 "non-function %qD declared as implicit template", decl);
17278
17279 /* For an in-class declaration, use `grokfield' to create the
17280 declaration. */
17281 if (member_p)
17282 {
17283 if (pushed_scope)
17284 {
17285 pop_scope (pushed_scope);
17286 pushed_scope = NULL_TREE;
17287 }
17288 decl = grokfield (declarator, decl_specifiers,
17289 initializer, !is_non_constant_init,
17290 /*asmspec=*/NULL_TREE,
17291 chainon (attributes, prefix_attributes));
17292 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17293 cp_parser_save_default_args (parser, decl);
17294 cp_finalize_omp_declare_simd (parser, decl);
17295 }
17296
17297 /* Finish processing the declaration. But, skip member
17298 declarations. */
17299 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17300 {
17301 cp_finish_decl (decl,
17302 initializer, !is_non_constant_init,
17303 asm_specification,
17304 /* If the initializer is in parentheses, then this is
17305 a direct-initialization, which means that an
17306 `explicit' constructor is OK. Otherwise, an
17307 `explicit' constructor cannot be used. */
17308 ((is_direct_init || !is_initialized)
17309 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17310 }
17311 else if ((cxx_dialect != cxx98) && friend_p
17312 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17313 /* Core issue #226 (C++0x only): A default template-argument
17314 shall not be specified in a friend class template
17315 declaration. */
17316 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17317 /*is_partial=*/false, /*is_friend_decl=*/1);
17318
17319 if (!friend_p && pushed_scope)
17320 pop_scope (pushed_scope);
17321
17322 if (function_declarator_p (declarator)
17323 && parser->fully_implicit_function_template_p)
17324 {
17325 if (member_p)
17326 decl = finish_fully_implicit_template (parser, decl);
17327 else
17328 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17329 }
17330
17331 return decl;
17332 }
17333
17334 /* Parse a declarator.
17335
17336 declarator:
17337 direct-declarator
17338 ptr-operator declarator
17339
17340 abstract-declarator:
17341 ptr-operator abstract-declarator [opt]
17342 direct-abstract-declarator
17343
17344 GNU Extensions:
17345
17346 declarator:
17347 attributes [opt] direct-declarator
17348 attributes [opt] ptr-operator declarator
17349
17350 abstract-declarator:
17351 attributes [opt] ptr-operator abstract-declarator [opt]
17352 attributes [opt] direct-abstract-declarator
17353
17354 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17355 detect constructor, destructor or conversion operators. It is set
17356 to -1 if the declarator is a name, and +1 if it is a
17357 function. Otherwise it is set to zero. Usually you just want to
17358 test for >0, but internally the negative value is used.
17359
17360 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17361 a decl-specifier-seq unless it declares a constructor, destructor,
17362 or conversion. It might seem that we could check this condition in
17363 semantic analysis, rather than parsing, but that makes it difficult
17364 to handle something like `f()'. We want to notice that there are
17365 no decl-specifiers, and therefore realize that this is an
17366 expression, not a declaration.)
17367
17368 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17369 the declarator is a direct-declarator of the form "(...)".
17370
17371 MEMBER_P is true iff this declarator is a member-declarator.
17372
17373 FRIEND_P is true iff this declarator is a friend. */
17374
17375 static cp_declarator *
17376 cp_parser_declarator (cp_parser* parser,
17377 cp_parser_declarator_kind dcl_kind,
17378 int* ctor_dtor_or_conv_p,
17379 bool* parenthesized_p,
17380 bool member_p, bool friend_p)
17381 {
17382 cp_declarator *declarator;
17383 enum tree_code code;
17384 cp_cv_quals cv_quals;
17385 tree class_type;
17386 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17387
17388 /* Assume this is not a constructor, destructor, or type-conversion
17389 operator. */
17390 if (ctor_dtor_or_conv_p)
17391 *ctor_dtor_or_conv_p = 0;
17392
17393 if (cp_parser_allow_gnu_extensions_p (parser))
17394 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17395
17396 /* Check for the ptr-operator production. */
17397 cp_parser_parse_tentatively (parser);
17398 /* Parse the ptr-operator. */
17399 code = cp_parser_ptr_operator (parser,
17400 &class_type,
17401 &cv_quals,
17402 &std_attributes);
17403
17404 /* If that worked, then we have a ptr-operator. */
17405 if (cp_parser_parse_definitely (parser))
17406 {
17407 /* If a ptr-operator was found, then this declarator was not
17408 parenthesized. */
17409 if (parenthesized_p)
17410 *parenthesized_p = true;
17411 /* The dependent declarator is optional if we are parsing an
17412 abstract-declarator. */
17413 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17414 cp_parser_parse_tentatively (parser);
17415
17416 /* Parse the dependent declarator. */
17417 declarator = cp_parser_declarator (parser, dcl_kind,
17418 /*ctor_dtor_or_conv_p=*/NULL,
17419 /*parenthesized_p=*/NULL,
17420 /*member_p=*/false,
17421 friend_p);
17422
17423 /* If we are parsing an abstract-declarator, we must handle the
17424 case where the dependent declarator is absent. */
17425 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17426 && !cp_parser_parse_definitely (parser))
17427 declarator = NULL;
17428
17429 declarator = cp_parser_make_indirect_declarator
17430 (code, class_type, cv_quals, declarator, std_attributes);
17431 }
17432 /* Everything else is a direct-declarator. */
17433 else
17434 {
17435 if (parenthesized_p)
17436 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17437 CPP_OPEN_PAREN);
17438 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17439 ctor_dtor_or_conv_p,
17440 member_p, friend_p);
17441 }
17442
17443 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17444 declarator->attributes = gnu_attributes;
17445 return declarator;
17446 }
17447
17448 /* Parse a direct-declarator or direct-abstract-declarator.
17449
17450 direct-declarator:
17451 declarator-id
17452 direct-declarator ( parameter-declaration-clause )
17453 cv-qualifier-seq [opt]
17454 ref-qualifier [opt]
17455 exception-specification [opt]
17456 direct-declarator [ constant-expression [opt] ]
17457 ( declarator )
17458
17459 direct-abstract-declarator:
17460 direct-abstract-declarator [opt]
17461 ( parameter-declaration-clause )
17462 cv-qualifier-seq [opt]
17463 ref-qualifier [opt]
17464 exception-specification [opt]
17465 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17466 ( abstract-declarator )
17467
17468 Returns a representation of the declarator. DCL_KIND is
17469 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17470 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17471 we are parsing a direct-declarator. It is
17472 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17473 of ambiguity we prefer an abstract declarator, as per
17474 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17475 as for cp_parser_declarator. */
17476
17477 static cp_declarator *
17478 cp_parser_direct_declarator (cp_parser* parser,
17479 cp_parser_declarator_kind dcl_kind,
17480 int* ctor_dtor_or_conv_p,
17481 bool member_p, bool friend_p)
17482 {
17483 cp_token *token;
17484 cp_declarator *declarator = NULL;
17485 tree scope = NULL_TREE;
17486 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17487 bool saved_in_declarator_p = parser->in_declarator_p;
17488 bool first = true;
17489 tree pushed_scope = NULL_TREE;
17490
17491 while (true)
17492 {
17493 /* Peek at the next token. */
17494 token = cp_lexer_peek_token (parser->lexer);
17495 if (token->type == CPP_OPEN_PAREN)
17496 {
17497 /* This is either a parameter-declaration-clause, or a
17498 parenthesized declarator. When we know we are parsing a
17499 named declarator, it must be a parenthesized declarator
17500 if FIRST is true. For instance, `(int)' is a
17501 parameter-declaration-clause, with an omitted
17502 direct-abstract-declarator. But `((*))', is a
17503 parenthesized abstract declarator. Finally, when T is a
17504 template parameter `(T)' is a
17505 parameter-declaration-clause, and not a parenthesized
17506 named declarator.
17507
17508 We first try and parse a parameter-declaration-clause,
17509 and then try a nested declarator (if FIRST is true).
17510
17511 It is not an error for it not to be a
17512 parameter-declaration-clause, even when FIRST is
17513 false. Consider,
17514
17515 int i (int);
17516 int i (3);
17517
17518 The first is the declaration of a function while the
17519 second is the definition of a variable, including its
17520 initializer.
17521
17522 Having seen only the parenthesis, we cannot know which of
17523 these two alternatives should be selected. Even more
17524 complex are examples like:
17525
17526 int i (int (a));
17527 int i (int (3));
17528
17529 The former is a function-declaration; the latter is a
17530 variable initialization.
17531
17532 Thus again, we try a parameter-declaration-clause, and if
17533 that fails, we back out and return. */
17534
17535 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17536 {
17537 tree params;
17538 bool is_declarator = false;
17539
17540 /* In a member-declarator, the only valid interpretation
17541 of a parenthesis is the start of a
17542 parameter-declaration-clause. (It is invalid to
17543 initialize a static data member with a parenthesized
17544 initializer; only the "=" form of initialization is
17545 permitted.) */
17546 if (!member_p)
17547 cp_parser_parse_tentatively (parser);
17548
17549 /* Consume the `('. */
17550 cp_lexer_consume_token (parser->lexer);
17551 if (first)
17552 {
17553 /* If this is going to be an abstract declarator, we're
17554 in a declarator and we can't have default args. */
17555 parser->default_arg_ok_p = false;
17556 parser->in_declarator_p = true;
17557 }
17558
17559 begin_scope (sk_function_parms, NULL_TREE);
17560
17561 /* Parse the parameter-declaration-clause. */
17562 params = cp_parser_parameter_declaration_clause (parser);
17563
17564 /* Consume the `)'. */
17565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17566
17567 /* If all went well, parse the cv-qualifier-seq,
17568 ref-qualifier and the exception-specification. */
17569 if (member_p || cp_parser_parse_definitely (parser))
17570 {
17571 cp_cv_quals cv_quals;
17572 cp_virt_specifiers virt_specifiers;
17573 cp_ref_qualifier ref_qual;
17574 tree exception_specification;
17575 tree late_return;
17576 tree attrs;
17577 bool memfn = (member_p || (pushed_scope
17578 && CLASS_TYPE_P (pushed_scope)));
17579
17580 is_declarator = true;
17581
17582 if (ctor_dtor_or_conv_p)
17583 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17584 first = false;
17585
17586 /* Parse the cv-qualifier-seq. */
17587 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17588 /* Parse the ref-qualifier. */
17589 ref_qual = cp_parser_ref_qualifier_opt (parser);
17590 /* And the exception-specification. */
17591 exception_specification
17592 = cp_parser_exception_specification_opt (parser);
17593
17594 attrs = cp_parser_std_attribute_spec_seq (parser);
17595
17596 /* In here, we handle cases where attribute is used after
17597 the function declaration. For example:
17598 void func (int x) __attribute__((vector(..))); */
17599 if (flag_cilkplus
17600 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17601 {
17602 cp_parser_parse_tentatively (parser);
17603 tree attr = cp_parser_gnu_attributes_opt (parser);
17604 if (cp_lexer_next_token_is_not (parser->lexer,
17605 CPP_SEMICOLON)
17606 && cp_lexer_next_token_is_not (parser->lexer,
17607 CPP_OPEN_BRACE))
17608 cp_parser_abort_tentative_parse (parser);
17609 else if (!cp_parser_parse_definitely (parser))
17610 ;
17611 else
17612 attrs = chainon (attr, attrs);
17613 }
17614 late_return = (cp_parser_late_return_type_opt
17615 (parser, declarator,
17616 memfn ? cv_quals : -1));
17617
17618
17619 /* Parse the virt-specifier-seq. */
17620 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17621
17622 /* Create the function-declarator. */
17623 declarator = make_call_declarator (declarator,
17624 params,
17625 cv_quals,
17626 virt_specifiers,
17627 ref_qual,
17628 exception_specification,
17629 late_return);
17630 declarator->std_attributes = attrs;
17631 /* Any subsequent parameter lists are to do with
17632 return type, so are not those of the declared
17633 function. */
17634 parser->default_arg_ok_p = false;
17635 }
17636
17637 /* Remove the function parms from scope. */
17638 pop_bindings_and_leave_scope ();
17639
17640 if (is_declarator)
17641 /* Repeat the main loop. */
17642 continue;
17643 }
17644
17645 /* If this is the first, we can try a parenthesized
17646 declarator. */
17647 if (first)
17648 {
17649 bool saved_in_type_id_in_expr_p;
17650
17651 parser->default_arg_ok_p = saved_default_arg_ok_p;
17652 parser->in_declarator_p = saved_in_declarator_p;
17653
17654 /* Consume the `('. */
17655 cp_lexer_consume_token (parser->lexer);
17656 /* Parse the nested declarator. */
17657 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17658 parser->in_type_id_in_expr_p = true;
17659 declarator
17660 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17661 /*parenthesized_p=*/NULL,
17662 member_p, friend_p);
17663 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17664 first = false;
17665 /* Expect a `)'. */
17666 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17667 declarator = cp_error_declarator;
17668 if (declarator == cp_error_declarator)
17669 break;
17670
17671 goto handle_declarator;
17672 }
17673 /* Otherwise, we must be done. */
17674 else
17675 break;
17676 }
17677 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17678 && token->type == CPP_OPEN_SQUARE
17679 && !cp_next_tokens_can_be_attribute_p (parser))
17680 {
17681 /* Parse an array-declarator. */
17682 tree bounds, attrs;
17683
17684 if (ctor_dtor_or_conv_p)
17685 *ctor_dtor_or_conv_p = 0;
17686
17687 first = false;
17688 parser->default_arg_ok_p = false;
17689 parser->in_declarator_p = true;
17690 /* Consume the `['. */
17691 cp_lexer_consume_token (parser->lexer);
17692 /* Peek at the next token. */
17693 token = cp_lexer_peek_token (parser->lexer);
17694 /* If the next token is `]', then there is no
17695 constant-expression. */
17696 if (token->type != CPP_CLOSE_SQUARE)
17697 {
17698 bool non_constant_p;
17699 bounds
17700 = cp_parser_constant_expression (parser,
17701 /*allow_non_constant=*/true,
17702 &non_constant_p);
17703 if (!non_constant_p)
17704 /* OK */;
17705 else if (error_operand_p (bounds))
17706 /* Already gave an error. */;
17707 else if (!parser->in_function_body
17708 || current_binding_level->kind == sk_function_parms)
17709 {
17710 /* Normally, the array bound must be an integral constant
17711 expression. However, as an extension, we allow VLAs
17712 in function scopes as long as they aren't part of a
17713 parameter declaration. */
17714 cp_parser_error (parser,
17715 "array bound is not an integer constant");
17716 bounds = error_mark_node;
17717 }
17718 else if (processing_template_decl
17719 && !type_dependent_expression_p (bounds))
17720 {
17721 /* Remember this wasn't a constant-expression. */
17722 bounds = build_nop (TREE_TYPE (bounds), bounds);
17723 TREE_SIDE_EFFECTS (bounds) = 1;
17724 }
17725 }
17726 else
17727 bounds = NULL_TREE;
17728 /* Look for the closing `]'. */
17729 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17730 {
17731 declarator = cp_error_declarator;
17732 break;
17733 }
17734
17735 attrs = cp_parser_std_attribute_spec_seq (parser);
17736 declarator = make_array_declarator (declarator, bounds);
17737 declarator->std_attributes = attrs;
17738 }
17739 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17740 {
17741 {
17742 tree qualifying_scope;
17743 tree unqualified_name;
17744 tree attrs;
17745 special_function_kind sfk;
17746 bool abstract_ok;
17747 bool pack_expansion_p = false;
17748 cp_token *declarator_id_start_token;
17749
17750 /* Parse a declarator-id */
17751 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17752 if (abstract_ok)
17753 {
17754 cp_parser_parse_tentatively (parser);
17755
17756 /* If we see an ellipsis, we should be looking at a
17757 parameter pack. */
17758 if (token->type == CPP_ELLIPSIS)
17759 {
17760 /* Consume the `...' */
17761 cp_lexer_consume_token (parser->lexer);
17762
17763 pack_expansion_p = true;
17764 }
17765 }
17766
17767 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17768 unqualified_name
17769 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17770 qualifying_scope = parser->scope;
17771 if (abstract_ok)
17772 {
17773 bool okay = false;
17774
17775 if (!unqualified_name && pack_expansion_p)
17776 {
17777 /* Check whether an error occurred. */
17778 okay = !cp_parser_error_occurred (parser);
17779
17780 /* We already consumed the ellipsis to mark a
17781 parameter pack, but we have no way to report it,
17782 so abort the tentative parse. We will be exiting
17783 immediately anyway. */
17784 cp_parser_abort_tentative_parse (parser);
17785 }
17786 else
17787 okay = cp_parser_parse_definitely (parser);
17788
17789 if (!okay)
17790 unqualified_name = error_mark_node;
17791 else if (unqualified_name
17792 && (qualifying_scope
17793 || (!identifier_p (unqualified_name))))
17794 {
17795 cp_parser_error (parser, "expected unqualified-id");
17796 unqualified_name = error_mark_node;
17797 }
17798 }
17799
17800 if (!unqualified_name)
17801 return NULL;
17802 if (unqualified_name == error_mark_node)
17803 {
17804 declarator = cp_error_declarator;
17805 pack_expansion_p = false;
17806 declarator->parameter_pack_p = false;
17807 break;
17808 }
17809
17810 attrs = cp_parser_std_attribute_spec_seq (parser);
17811
17812 if (qualifying_scope && at_namespace_scope_p ()
17813 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17814 {
17815 /* In the declaration of a member of a template class
17816 outside of the class itself, the SCOPE will sometimes
17817 be a TYPENAME_TYPE. For example, given:
17818
17819 template <typename T>
17820 int S<T>::R::i = 3;
17821
17822 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17823 this context, we must resolve S<T>::R to an ordinary
17824 type, rather than a typename type.
17825
17826 The reason we normally avoid resolving TYPENAME_TYPEs
17827 is that a specialization of `S' might render
17828 `S<T>::R' not a type. However, if `S' is
17829 specialized, then this `i' will not be used, so there
17830 is no harm in resolving the types here. */
17831 tree type;
17832
17833 /* Resolve the TYPENAME_TYPE. */
17834 type = resolve_typename_type (qualifying_scope,
17835 /*only_current_p=*/false);
17836 /* If that failed, the declarator is invalid. */
17837 if (TREE_CODE (type) == TYPENAME_TYPE)
17838 {
17839 if (typedef_variant_p (type))
17840 error_at (declarator_id_start_token->location,
17841 "cannot define member of dependent typedef "
17842 "%qT", type);
17843 else
17844 error_at (declarator_id_start_token->location,
17845 "%<%T::%E%> is not a type",
17846 TYPE_CONTEXT (qualifying_scope),
17847 TYPE_IDENTIFIER (qualifying_scope));
17848 }
17849 qualifying_scope = type;
17850 }
17851
17852 sfk = sfk_none;
17853
17854 if (unqualified_name)
17855 {
17856 tree class_type;
17857
17858 if (qualifying_scope
17859 && CLASS_TYPE_P (qualifying_scope))
17860 class_type = qualifying_scope;
17861 else
17862 class_type = current_class_type;
17863
17864 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17865 {
17866 tree name_type = TREE_TYPE (unqualified_name);
17867 if (class_type && same_type_p (name_type, class_type))
17868 {
17869 if (qualifying_scope
17870 && CLASSTYPE_USE_TEMPLATE (name_type))
17871 {
17872 error_at (declarator_id_start_token->location,
17873 "invalid use of constructor as a template");
17874 inform (declarator_id_start_token->location,
17875 "use %<%T::%D%> instead of %<%T::%D%> to "
17876 "name the constructor in a qualified name",
17877 class_type,
17878 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17879 class_type, name_type);
17880 declarator = cp_error_declarator;
17881 break;
17882 }
17883 else
17884 unqualified_name = constructor_name (class_type);
17885 }
17886 else
17887 {
17888 /* We do not attempt to print the declarator
17889 here because we do not have enough
17890 information about its original syntactic
17891 form. */
17892 cp_parser_error (parser, "invalid declarator");
17893 declarator = cp_error_declarator;
17894 break;
17895 }
17896 }
17897
17898 if (class_type)
17899 {
17900 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17901 sfk = sfk_destructor;
17902 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17903 sfk = sfk_conversion;
17904 else if (/* There's no way to declare a constructor
17905 for an anonymous type, even if the type
17906 got a name for linkage purposes. */
17907 !TYPE_WAS_ANONYMOUS (class_type)
17908 /* Handle correctly (c++/19200):
17909
17910 struct S {
17911 struct T{};
17912 friend void S(T);
17913 };
17914
17915 and also:
17916
17917 namespace N {
17918 void S();
17919 }
17920
17921 struct S {
17922 friend void N::S();
17923 }; */
17924 && !(friend_p
17925 && class_type != qualifying_scope)
17926 && constructor_name_p (unqualified_name,
17927 class_type))
17928 {
17929 unqualified_name = constructor_name (class_type);
17930 sfk = sfk_constructor;
17931 }
17932 else if (is_overloaded_fn (unqualified_name)
17933 && DECL_CONSTRUCTOR_P (get_first_fn
17934 (unqualified_name)))
17935 sfk = sfk_constructor;
17936
17937 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17938 *ctor_dtor_or_conv_p = -1;
17939 }
17940 }
17941 declarator = make_id_declarator (qualifying_scope,
17942 unqualified_name,
17943 sfk);
17944 declarator->std_attributes = attrs;
17945 declarator->id_loc = token->location;
17946 declarator->parameter_pack_p = pack_expansion_p;
17947
17948 if (pack_expansion_p)
17949 maybe_warn_variadic_templates ();
17950 }
17951
17952 handle_declarator:;
17953 scope = get_scope_of_declarator (declarator);
17954 if (scope)
17955 {
17956 /* Any names that appear after the declarator-id for a
17957 member are looked up in the containing scope. */
17958 if (at_function_scope_p ())
17959 {
17960 /* But declarations with qualified-ids can't appear in a
17961 function. */
17962 cp_parser_error (parser, "qualified-id in declaration");
17963 declarator = cp_error_declarator;
17964 break;
17965 }
17966 pushed_scope = push_scope (scope);
17967 }
17968 parser->in_declarator_p = true;
17969 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17970 || (declarator && declarator->kind == cdk_id))
17971 /* Default args are only allowed on function
17972 declarations. */
17973 parser->default_arg_ok_p = saved_default_arg_ok_p;
17974 else
17975 parser->default_arg_ok_p = false;
17976
17977 first = false;
17978 }
17979 /* We're done. */
17980 else
17981 break;
17982 }
17983
17984 /* For an abstract declarator, we might wind up with nothing at this
17985 point. That's an error; the declarator is not optional. */
17986 if (!declarator)
17987 cp_parser_error (parser, "expected declarator");
17988
17989 /* If we entered a scope, we must exit it now. */
17990 if (pushed_scope)
17991 pop_scope (pushed_scope);
17992
17993 parser->default_arg_ok_p = saved_default_arg_ok_p;
17994 parser->in_declarator_p = saved_in_declarator_p;
17995
17996 return declarator;
17997 }
17998
17999 /* Parse a ptr-operator.
18000
18001 ptr-operator:
18002 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18003 * cv-qualifier-seq [opt]
18004 &
18005 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18006 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18007
18008 GNU Extension:
18009
18010 ptr-operator:
18011 & cv-qualifier-seq [opt]
18012
18013 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18014 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18015 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18016 filled in with the TYPE containing the member. *CV_QUALS is
18017 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18018 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18019 Note that the tree codes returned by this function have nothing
18020 to do with the types of trees that will be eventually be created
18021 to represent the pointer or reference type being parsed. They are
18022 just constants with suggestive names. */
18023 static enum tree_code
18024 cp_parser_ptr_operator (cp_parser* parser,
18025 tree* type,
18026 cp_cv_quals *cv_quals,
18027 tree *attributes)
18028 {
18029 enum tree_code code = ERROR_MARK;
18030 cp_token *token;
18031 tree attrs = NULL_TREE;
18032
18033 /* Assume that it's not a pointer-to-member. */
18034 *type = NULL_TREE;
18035 /* And that there are no cv-qualifiers. */
18036 *cv_quals = TYPE_UNQUALIFIED;
18037
18038 /* Peek at the next token. */
18039 token = cp_lexer_peek_token (parser->lexer);
18040
18041 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18042 if (token->type == CPP_MULT)
18043 code = INDIRECT_REF;
18044 else if (token->type == CPP_AND)
18045 code = ADDR_EXPR;
18046 else if ((cxx_dialect != cxx98) &&
18047 token->type == CPP_AND_AND) /* C++0x only */
18048 code = NON_LVALUE_EXPR;
18049
18050 if (code != ERROR_MARK)
18051 {
18052 /* Consume the `*', `&' or `&&'. */
18053 cp_lexer_consume_token (parser->lexer);
18054
18055 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18056 `&', if we are allowing GNU extensions. (The only qualifier
18057 that can legally appear after `&' is `restrict', but that is
18058 enforced during semantic analysis. */
18059 if (code == INDIRECT_REF
18060 || cp_parser_allow_gnu_extensions_p (parser))
18061 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18062
18063 attrs = cp_parser_std_attribute_spec_seq (parser);
18064 if (attributes != NULL)
18065 *attributes = attrs;
18066 }
18067 else
18068 {
18069 /* Try the pointer-to-member case. */
18070 cp_parser_parse_tentatively (parser);
18071 /* Look for the optional `::' operator. */
18072 cp_parser_global_scope_opt (parser,
18073 /*current_scope_valid_p=*/false);
18074 /* Look for the nested-name specifier. */
18075 token = cp_lexer_peek_token (parser->lexer);
18076 cp_parser_nested_name_specifier (parser,
18077 /*typename_keyword_p=*/false,
18078 /*check_dependency_p=*/true,
18079 /*type_p=*/false,
18080 /*is_declaration=*/false);
18081 /* If we found it, and the next token is a `*', then we are
18082 indeed looking at a pointer-to-member operator. */
18083 if (!cp_parser_error_occurred (parser)
18084 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18085 {
18086 /* Indicate that the `*' operator was used. */
18087 code = INDIRECT_REF;
18088
18089 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18090 error_at (token->location, "%qD is a namespace", parser->scope);
18091 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18092 error_at (token->location, "cannot form pointer to member of "
18093 "non-class %q#T", parser->scope);
18094 else
18095 {
18096 /* The type of which the member is a member is given by the
18097 current SCOPE. */
18098 *type = parser->scope;
18099 /* The next name will not be qualified. */
18100 parser->scope = NULL_TREE;
18101 parser->qualifying_scope = NULL_TREE;
18102 parser->object_scope = NULL_TREE;
18103 /* Look for optional c++11 attributes. */
18104 attrs = cp_parser_std_attribute_spec_seq (parser);
18105 if (attributes != NULL)
18106 *attributes = attrs;
18107 /* Look for the optional cv-qualifier-seq. */
18108 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18109 }
18110 }
18111 /* If that didn't work we don't have a ptr-operator. */
18112 if (!cp_parser_parse_definitely (parser))
18113 cp_parser_error (parser, "expected ptr-operator");
18114 }
18115
18116 return code;
18117 }
18118
18119 /* Parse an (optional) cv-qualifier-seq.
18120
18121 cv-qualifier-seq:
18122 cv-qualifier cv-qualifier-seq [opt]
18123
18124 cv-qualifier:
18125 const
18126 volatile
18127
18128 GNU Extension:
18129
18130 cv-qualifier:
18131 __restrict__
18132
18133 Returns a bitmask representing the cv-qualifiers. */
18134
18135 static cp_cv_quals
18136 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18137 {
18138 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18139
18140 while (true)
18141 {
18142 cp_token *token;
18143 cp_cv_quals cv_qualifier;
18144
18145 /* Peek at the next token. */
18146 token = cp_lexer_peek_token (parser->lexer);
18147 /* See if it's a cv-qualifier. */
18148 switch (token->keyword)
18149 {
18150 case RID_CONST:
18151 cv_qualifier = TYPE_QUAL_CONST;
18152 break;
18153
18154 case RID_VOLATILE:
18155 cv_qualifier = TYPE_QUAL_VOLATILE;
18156 break;
18157
18158 case RID_RESTRICT:
18159 cv_qualifier = TYPE_QUAL_RESTRICT;
18160 break;
18161
18162 default:
18163 cv_qualifier = TYPE_UNQUALIFIED;
18164 break;
18165 }
18166
18167 if (!cv_qualifier)
18168 break;
18169
18170 if (cv_quals & cv_qualifier)
18171 {
18172 error_at (token->location, "duplicate cv-qualifier");
18173 cp_lexer_purge_token (parser->lexer);
18174 }
18175 else
18176 {
18177 cp_lexer_consume_token (parser->lexer);
18178 cv_quals |= cv_qualifier;
18179 }
18180 }
18181
18182 return cv_quals;
18183 }
18184
18185 /* Parse an (optional) ref-qualifier
18186
18187 ref-qualifier:
18188 &
18189 &&
18190
18191 Returns cp_ref_qualifier representing ref-qualifier. */
18192
18193 static cp_ref_qualifier
18194 cp_parser_ref_qualifier_opt (cp_parser* parser)
18195 {
18196 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18197
18198 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18199 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18200 return ref_qual;
18201
18202 while (true)
18203 {
18204 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18205 cp_token *token = cp_lexer_peek_token (parser->lexer);
18206
18207 switch (token->type)
18208 {
18209 case CPP_AND:
18210 curr_ref_qual = REF_QUAL_LVALUE;
18211 break;
18212
18213 case CPP_AND_AND:
18214 curr_ref_qual = REF_QUAL_RVALUE;
18215 break;
18216
18217 default:
18218 curr_ref_qual = REF_QUAL_NONE;
18219 break;
18220 }
18221
18222 if (!curr_ref_qual)
18223 break;
18224 else if (ref_qual)
18225 {
18226 error_at (token->location, "multiple ref-qualifiers");
18227 cp_lexer_purge_token (parser->lexer);
18228 }
18229 else
18230 {
18231 ref_qual = curr_ref_qual;
18232 cp_lexer_consume_token (parser->lexer);
18233 }
18234 }
18235
18236 return ref_qual;
18237 }
18238
18239 /* Parse an (optional) virt-specifier-seq.
18240
18241 virt-specifier-seq:
18242 virt-specifier virt-specifier-seq [opt]
18243
18244 virt-specifier:
18245 override
18246 final
18247
18248 Returns a bitmask representing the virt-specifiers. */
18249
18250 static cp_virt_specifiers
18251 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18252 {
18253 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18254
18255 while (true)
18256 {
18257 cp_token *token;
18258 cp_virt_specifiers virt_specifier;
18259
18260 /* Peek at the next token. */
18261 token = cp_lexer_peek_token (parser->lexer);
18262 /* See if it's a virt-specifier-qualifier. */
18263 if (token->type != CPP_NAME)
18264 break;
18265 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18266 {
18267 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18268 virt_specifier = VIRT_SPEC_OVERRIDE;
18269 }
18270 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18271 {
18272 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18273 virt_specifier = VIRT_SPEC_FINAL;
18274 }
18275 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18276 {
18277 virt_specifier = VIRT_SPEC_FINAL;
18278 }
18279 else
18280 break;
18281
18282 if (virt_specifiers & virt_specifier)
18283 {
18284 error_at (token->location, "duplicate virt-specifier");
18285 cp_lexer_purge_token (parser->lexer);
18286 }
18287 else
18288 {
18289 cp_lexer_consume_token (parser->lexer);
18290 virt_specifiers |= virt_specifier;
18291 }
18292 }
18293 return virt_specifiers;
18294 }
18295
18296 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18297 is in scope even though it isn't real. */
18298
18299 void
18300 inject_this_parameter (tree ctype, cp_cv_quals quals)
18301 {
18302 tree this_parm;
18303
18304 if (current_class_ptr)
18305 {
18306 /* We don't clear this between NSDMIs. Is it already what we want? */
18307 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18308 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18309 && cp_type_quals (type) == quals)
18310 return;
18311 }
18312
18313 this_parm = build_this_parm (ctype, quals);
18314 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18315 current_class_ptr = NULL_TREE;
18316 current_class_ref
18317 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18318 current_class_ptr = this_parm;
18319 }
18320
18321 /* Return true iff our current scope is a non-static data member
18322 initializer. */
18323
18324 bool
18325 parsing_nsdmi (void)
18326 {
18327 /* We recognize NSDMI context by the context-less 'this' pointer set up
18328 by the function above. */
18329 if (current_class_ptr
18330 && TREE_CODE (current_class_ptr) == PARM_DECL
18331 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18332 return true;
18333 return false;
18334 }
18335
18336 /* Parse a late-specified return type, if any. This is not a separate
18337 non-terminal, but part of a function declarator, which looks like
18338
18339 -> trailing-type-specifier-seq abstract-declarator(opt)
18340
18341 Returns the type indicated by the type-id.
18342
18343 In addition to this this parses any queued up omp declare simd
18344 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18345
18346 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18347 function. */
18348
18349 static tree
18350 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18351 cp_cv_quals quals)
18352 {
18353 cp_token *token;
18354 tree type = NULL_TREE;
18355 bool declare_simd_p = (parser->omp_declare_simd
18356 && declarator
18357 && declarator->kind == cdk_id);
18358
18359 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18360 && declarator && declarator->kind == cdk_id);
18361
18362 /* Peek at the next token. */
18363 token = cp_lexer_peek_token (parser->lexer);
18364 /* A late-specified return type is indicated by an initial '->'. */
18365 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18366 return NULL_TREE;
18367
18368 tree save_ccp = current_class_ptr;
18369 tree save_ccr = current_class_ref;
18370 if (quals >= 0)
18371 {
18372 /* DR 1207: 'this' is in scope in the trailing return type. */
18373 inject_this_parameter (current_class_type, quals);
18374 }
18375
18376 if (token->type == CPP_DEREF)
18377 {
18378 /* Consume the ->. */
18379 cp_lexer_consume_token (parser->lexer);
18380
18381 type = cp_parser_trailing_type_id (parser);
18382 }
18383
18384 if (cilk_simd_fn_vector_p)
18385 declarator->std_attributes
18386 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18387 declarator->std_attributes);
18388 if (declare_simd_p)
18389 declarator->std_attributes
18390 = cp_parser_late_parsing_omp_declare_simd (parser,
18391 declarator->std_attributes);
18392
18393 if (quals >= 0)
18394 {
18395 current_class_ptr = save_ccp;
18396 current_class_ref = save_ccr;
18397 }
18398
18399 return type;
18400 }
18401
18402 /* Parse a declarator-id.
18403
18404 declarator-id:
18405 id-expression
18406 :: [opt] nested-name-specifier [opt] type-name
18407
18408 In the `id-expression' case, the value returned is as for
18409 cp_parser_id_expression if the id-expression was an unqualified-id.
18410 If the id-expression was a qualified-id, then a SCOPE_REF is
18411 returned. The first operand is the scope (either a NAMESPACE_DECL
18412 or TREE_TYPE), but the second is still just a representation of an
18413 unqualified-id. */
18414
18415 static tree
18416 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18417 {
18418 tree id;
18419 /* The expression must be an id-expression. Assume that qualified
18420 names are the names of types so that:
18421
18422 template <class T>
18423 int S<T>::R::i = 3;
18424
18425 will work; we must treat `S<T>::R' as the name of a type.
18426 Similarly, assume that qualified names are templates, where
18427 required, so that:
18428
18429 template <class T>
18430 int S<T>::R<T>::i = 3;
18431
18432 will work, too. */
18433 id = cp_parser_id_expression (parser,
18434 /*template_keyword_p=*/false,
18435 /*check_dependency_p=*/false,
18436 /*template_p=*/NULL,
18437 /*declarator_p=*/true,
18438 optional_p);
18439 if (id && BASELINK_P (id))
18440 id = BASELINK_FUNCTIONS (id);
18441 return id;
18442 }
18443
18444 /* Parse a type-id.
18445
18446 type-id:
18447 type-specifier-seq abstract-declarator [opt]
18448
18449 Returns the TYPE specified. */
18450
18451 static tree
18452 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18453 bool is_trailing_return)
18454 {
18455 cp_decl_specifier_seq type_specifier_seq;
18456 cp_declarator *abstract_declarator;
18457
18458 /* Parse the type-specifier-seq. */
18459 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18460 is_trailing_return,
18461 &type_specifier_seq);
18462 if (type_specifier_seq.type == error_mark_node)
18463 return error_mark_node;
18464
18465 /* There might or might not be an abstract declarator. */
18466 cp_parser_parse_tentatively (parser);
18467 /* Look for the declarator. */
18468 abstract_declarator
18469 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18470 /*parenthesized_p=*/NULL,
18471 /*member_p=*/false,
18472 /*friend_p=*/false);
18473 /* Check to see if there really was a declarator. */
18474 if (!cp_parser_parse_definitely (parser))
18475 abstract_declarator = NULL;
18476
18477 if (type_specifier_seq.type
18478 /* None of the valid uses of 'auto' in C++14 involve the type-id
18479 nonterminal, but it is valid in a trailing-return-type. */
18480 && !(cxx_dialect >= cxx14 && is_trailing_return)
18481 && type_uses_auto (type_specifier_seq.type))
18482 {
18483 /* A type-id with type 'auto' is only ok if the abstract declarator
18484 is a function declarator with a late-specified return type. */
18485 if (abstract_declarator
18486 && abstract_declarator->kind == cdk_function
18487 && abstract_declarator->u.function.late_return_type)
18488 /* OK */;
18489 else
18490 {
18491 error ("invalid use of %<auto%>");
18492 return error_mark_node;
18493 }
18494 }
18495
18496 return groktypename (&type_specifier_seq, abstract_declarator,
18497 is_template_arg);
18498 }
18499
18500 static tree cp_parser_type_id (cp_parser *parser)
18501 {
18502 return cp_parser_type_id_1 (parser, false, false);
18503 }
18504
18505 static tree cp_parser_template_type_arg (cp_parser *parser)
18506 {
18507 tree r;
18508 const char *saved_message = parser->type_definition_forbidden_message;
18509 parser->type_definition_forbidden_message
18510 = G_("types may not be defined in template arguments");
18511 r = cp_parser_type_id_1 (parser, true, false);
18512 parser->type_definition_forbidden_message = saved_message;
18513 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18514 {
18515 error ("invalid use of %<auto%> in template argument");
18516 r = error_mark_node;
18517 }
18518 return r;
18519 }
18520
18521 static tree cp_parser_trailing_type_id (cp_parser *parser)
18522 {
18523 return cp_parser_type_id_1 (parser, false, true);
18524 }
18525
18526 /* Parse a type-specifier-seq.
18527
18528 type-specifier-seq:
18529 type-specifier type-specifier-seq [opt]
18530
18531 GNU extension:
18532
18533 type-specifier-seq:
18534 attributes type-specifier-seq [opt]
18535
18536 If IS_DECLARATION is true, we are at the start of a "condition" or
18537 exception-declaration, so we might be followed by a declarator-id.
18538
18539 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18540 i.e. we've just seen "->".
18541
18542 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18543
18544 static void
18545 cp_parser_type_specifier_seq (cp_parser* parser,
18546 bool is_declaration,
18547 bool is_trailing_return,
18548 cp_decl_specifier_seq *type_specifier_seq)
18549 {
18550 bool seen_type_specifier = false;
18551 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18552 cp_token *start_token = NULL;
18553
18554 /* Clear the TYPE_SPECIFIER_SEQ. */
18555 clear_decl_specs (type_specifier_seq);
18556
18557 /* In the context of a trailing return type, enum E { } is an
18558 elaborated-type-specifier followed by a function-body, not an
18559 enum-specifier. */
18560 if (is_trailing_return)
18561 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18562
18563 /* Parse the type-specifiers and attributes. */
18564 while (true)
18565 {
18566 tree type_specifier;
18567 bool is_cv_qualifier;
18568
18569 /* Check for attributes first. */
18570 if (cp_next_tokens_can_be_attribute_p (parser))
18571 {
18572 type_specifier_seq->attributes =
18573 chainon (type_specifier_seq->attributes,
18574 cp_parser_attributes_opt (parser));
18575 continue;
18576 }
18577
18578 /* record the token of the beginning of the type specifier seq,
18579 for error reporting purposes*/
18580 if (!start_token)
18581 start_token = cp_lexer_peek_token (parser->lexer);
18582
18583 /* Look for the type-specifier. */
18584 type_specifier = cp_parser_type_specifier (parser,
18585 flags,
18586 type_specifier_seq,
18587 /*is_declaration=*/false,
18588 NULL,
18589 &is_cv_qualifier);
18590 if (!type_specifier)
18591 {
18592 /* If the first type-specifier could not be found, this is not a
18593 type-specifier-seq at all. */
18594 if (!seen_type_specifier)
18595 {
18596 /* Set in_declarator_p to avoid skipping to the semicolon. */
18597 int in_decl = parser->in_declarator_p;
18598 parser->in_declarator_p = true;
18599
18600 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18601 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18602 cp_parser_error (parser, "expected type-specifier");
18603
18604 parser->in_declarator_p = in_decl;
18605
18606 type_specifier_seq->type = error_mark_node;
18607 return;
18608 }
18609 /* If subsequent type-specifiers could not be found, the
18610 type-specifier-seq is complete. */
18611 break;
18612 }
18613
18614 seen_type_specifier = true;
18615 /* The standard says that a condition can be:
18616
18617 type-specifier-seq declarator = assignment-expression
18618
18619 However, given:
18620
18621 struct S {};
18622 if (int S = ...)
18623
18624 we should treat the "S" as a declarator, not as a
18625 type-specifier. The standard doesn't say that explicitly for
18626 type-specifier-seq, but it does say that for
18627 decl-specifier-seq in an ordinary declaration. Perhaps it
18628 would be clearer just to allow a decl-specifier-seq here, and
18629 then add a semantic restriction that if any decl-specifiers
18630 that are not type-specifiers appear, the program is invalid. */
18631 if (is_declaration && !is_cv_qualifier)
18632 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18633 }
18634 }
18635
18636 /* Return whether the function currently being declared has an associated
18637 template parameter list. */
18638
18639 static bool
18640 function_being_declared_is_template_p (cp_parser* parser)
18641 {
18642 if (!current_template_parms || processing_template_parmlist)
18643 return false;
18644
18645 if (parser->implicit_template_scope)
18646 return true;
18647
18648 if (at_class_scope_p ()
18649 && TYPE_BEING_DEFINED (current_class_type))
18650 return parser->num_template_parameter_lists != 0;
18651
18652 return ((int) parser->num_template_parameter_lists > template_class_depth
18653 (current_class_type));
18654 }
18655
18656 /* Parse a parameter-declaration-clause.
18657
18658 parameter-declaration-clause:
18659 parameter-declaration-list [opt] ... [opt]
18660 parameter-declaration-list , ...
18661
18662 Returns a representation for the parameter declarations. A return
18663 value of NULL indicates a parameter-declaration-clause consisting
18664 only of an ellipsis. */
18665
18666 static tree
18667 cp_parser_parameter_declaration_clause (cp_parser* parser)
18668 {
18669 tree parameters;
18670 cp_token *token;
18671 bool ellipsis_p;
18672 bool is_error;
18673
18674 struct cleanup {
18675 cp_parser* parser;
18676 int auto_is_implicit_function_template_parm_p;
18677 ~cleanup() {
18678 parser->auto_is_implicit_function_template_parm_p
18679 = auto_is_implicit_function_template_parm_p;
18680 }
18681 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18682
18683 (void) cleanup;
18684
18685 if (!processing_specialization
18686 && !processing_template_parmlist
18687 && !processing_explicit_instantiation)
18688 if (!current_function_decl
18689 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18690 parser->auto_is_implicit_function_template_parm_p = true;
18691
18692 /* Peek at the next token. */
18693 token = cp_lexer_peek_token (parser->lexer);
18694 /* Check for trivial parameter-declaration-clauses. */
18695 if (token->type == CPP_ELLIPSIS)
18696 {
18697 /* Consume the `...' token. */
18698 cp_lexer_consume_token (parser->lexer);
18699 return NULL_TREE;
18700 }
18701 else if (token->type == CPP_CLOSE_PAREN)
18702 /* There are no parameters. */
18703 {
18704 #ifndef NO_IMPLICIT_EXTERN_C
18705 if (in_system_header_at (input_location)
18706 && current_class_type == NULL
18707 && current_lang_name == lang_name_c)
18708 return NULL_TREE;
18709 else
18710 #endif
18711 return void_list_node;
18712 }
18713 /* Check for `(void)', too, which is a special case. */
18714 else if (token->keyword == RID_VOID
18715 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18716 == CPP_CLOSE_PAREN))
18717 {
18718 /* Consume the `void' token. */
18719 cp_lexer_consume_token (parser->lexer);
18720 /* There are no parameters. */
18721 return void_list_node;
18722 }
18723
18724 /* Parse the parameter-declaration-list. */
18725 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18726 /* If a parse error occurred while parsing the
18727 parameter-declaration-list, then the entire
18728 parameter-declaration-clause is erroneous. */
18729 if (is_error)
18730 return NULL;
18731
18732 /* Peek at the next token. */
18733 token = cp_lexer_peek_token (parser->lexer);
18734 /* If it's a `,', the clause should terminate with an ellipsis. */
18735 if (token->type == CPP_COMMA)
18736 {
18737 /* Consume the `,'. */
18738 cp_lexer_consume_token (parser->lexer);
18739 /* Expect an ellipsis. */
18740 ellipsis_p
18741 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18742 }
18743 /* It might also be `...' if the optional trailing `,' was
18744 omitted. */
18745 else if (token->type == CPP_ELLIPSIS)
18746 {
18747 /* Consume the `...' token. */
18748 cp_lexer_consume_token (parser->lexer);
18749 /* And remember that we saw it. */
18750 ellipsis_p = true;
18751 }
18752 else
18753 ellipsis_p = false;
18754
18755 /* Finish the parameter list. */
18756 if (!ellipsis_p)
18757 parameters = chainon (parameters, void_list_node);
18758
18759 return parameters;
18760 }
18761
18762 /* Parse a parameter-declaration-list.
18763
18764 parameter-declaration-list:
18765 parameter-declaration
18766 parameter-declaration-list , parameter-declaration
18767
18768 Returns a representation of the parameter-declaration-list, as for
18769 cp_parser_parameter_declaration_clause. However, the
18770 `void_list_node' is never appended to the list. Upon return,
18771 *IS_ERROR will be true iff an error occurred. */
18772
18773 static tree
18774 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18775 {
18776 tree parameters = NULL_TREE;
18777 tree *tail = &parameters;
18778 bool saved_in_unbraced_linkage_specification_p;
18779 int index = 0;
18780
18781 /* Assume all will go well. */
18782 *is_error = false;
18783 /* The special considerations that apply to a function within an
18784 unbraced linkage specifications do not apply to the parameters
18785 to the function. */
18786 saved_in_unbraced_linkage_specification_p
18787 = parser->in_unbraced_linkage_specification_p;
18788 parser->in_unbraced_linkage_specification_p = false;
18789
18790 /* Look for more parameters. */
18791 while (true)
18792 {
18793 cp_parameter_declarator *parameter;
18794 tree decl = error_mark_node;
18795 bool parenthesized_p = false;
18796 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18797 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18798 (current_template_parms)) : 0);
18799
18800 /* Parse the parameter. */
18801 parameter
18802 = cp_parser_parameter_declaration (parser,
18803 /*template_parm_p=*/false,
18804 &parenthesized_p);
18805
18806 /* We don't know yet if the enclosing context is deprecated, so wait
18807 and warn in grokparms if appropriate. */
18808 deprecated_state = DEPRECATED_SUPPRESS;
18809
18810 if (parameter)
18811 {
18812 /* If a function parameter pack was specified and an implicit template
18813 parameter was introduced during cp_parser_parameter_declaration,
18814 change any implicit parameters introduced into packs. */
18815 if (parser->implicit_template_parms
18816 && parameter->declarator
18817 && parameter->declarator->parameter_pack_p)
18818 {
18819 int latest_template_parm_idx = TREE_VEC_LENGTH
18820 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18821
18822 if (latest_template_parm_idx != template_parm_idx)
18823 parameter->decl_specifiers.type = convert_generic_types_to_packs
18824 (parameter->decl_specifiers.type,
18825 template_parm_idx, latest_template_parm_idx);
18826 }
18827
18828 decl = grokdeclarator (parameter->declarator,
18829 &parameter->decl_specifiers,
18830 PARM,
18831 parameter->default_argument != NULL_TREE,
18832 &parameter->decl_specifiers.attributes);
18833 }
18834
18835 deprecated_state = DEPRECATED_NORMAL;
18836
18837 /* If a parse error occurred parsing the parameter declaration,
18838 then the entire parameter-declaration-list is erroneous. */
18839 if (decl == error_mark_node)
18840 {
18841 *is_error = true;
18842 parameters = error_mark_node;
18843 break;
18844 }
18845
18846 if (parameter->decl_specifiers.attributes)
18847 cplus_decl_attributes (&decl,
18848 parameter->decl_specifiers.attributes,
18849 0);
18850 if (DECL_NAME (decl))
18851 decl = pushdecl (decl);
18852
18853 if (decl != error_mark_node)
18854 {
18855 retrofit_lang_decl (decl);
18856 DECL_PARM_INDEX (decl) = ++index;
18857 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18858 }
18859
18860 /* Add the new parameter to the list. */
18861 *tail = build_tree_list (parameter->default_argument, decl);
18862 tail = &TREE_CHAIN (*tail);
18863
18864 /* Peek at the next token. */
18865 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18866 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18867 /* These are for Objective-C++ */
18868 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18869 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18870 /* The parameter-declaration-list is complete. */
18871 break;
18872 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18873 {
18874 cp_token *token;
18875
18876 /* Peek at the next token. */
18877 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18878 /* If it's an ellipsis, then the list is complete. */
18879 if (token->type == CPP_ELLIPSIS)
18880 break;
18881 /* Otherwise, there must be more parameters. Consume the
18882 `,'. */
18883 cp_lexer_consume_token (parser->lexer);
18884 /* When parsing something like:
18885
18886 int i(float f, double d)
18887
18888 we can tell after seeing the declaration for "f" that we
18889 are not looking at an initialization of a variable "i",
18890 but rather at the declaration of a function "i".
18891
18892 Due to the fact that the parsing of template arguments
18893 (as specified to a template-id) requires backtracking we
18894 cannot use this technique when inside a template argument
18895 list. */
18896 if (!parser->in_template_argument_list_p
18897 && !parser->in_type_id_in_expr_p
18898 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18899 /* However, a parameter-declaration of the form
18900 "float(f)" (which is a valid declaration of a
18901 parameter "f") can also be interpreted as an
18902 expression (the conversion of "f" to "float"). */
18903 && !parenthesized_p)
18904 cp_parser_commit_to_tentative_parse (parser);
18905 }
18906 else
18907 {
18908 cp_parser_error (parser, "expected %<,%> or %<...%>");
18909 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18910 cp_parser_skip_to_closing_parenthesis (parser,
18911 /*recovering=*/true,
18912 /*or_comma=*/false,
18913 /*consume_paren=*/false);
18914 break;
18915 }
18916 }
18917
18918 parser->in_unbraced_linkage_specification_p
18919 = saved_in_unbraced_linkage_specification_p;
18920
18921 /* Reset implicit_template_scope if we are about to leave the function
18922 parameter list that introduced it. Note that for out-of-line member
18923 definitions, there will be one or more class scopes before we get to
18924 the template parameter scope. */
18925
18926 if (cp_binding_level *its = parser->implicit_template_scope)
18927 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18928 {
18929 while (maybe_its->kind == sk_class)
18930 maybe_its = maybe_its->level_chain;
18931 if (maybe_its == its)
18932 {
18933 parser->implicit_template_parms = 0;
18934 parser->implicit_template_scope = 0;
18935 }
18936 }
18937
18938 return parameters;
18939 }
18940
18941 /* Parse a parameter declaration.
18942
18943 parameter-declaration:
18944 decl-specifier-seq ... [opt] declarator
18945 decl-specifier-seq declarator = assignment-expression
18946 decl-specifier-seq ... [opt] abstract-declarator [opt]
18947 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18948
18949 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18950 declares a template parameter. (In that case, a non-nested `>'
18951 token encountered during the parsing of the assignment-expression
18952 is not interpreted as a greater-than operator.)
18953
18954 Returns a representation of the parameter, or NULL if an error
18955 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18956 true iff the declarator is of the form "(p)". */
18957
18958 static cp_parameter_declarator *
18959 cp_parser_parameter_declaration (cp_parser *parser,
18960 bool template_parm_p,
18961 bool *parenthesized_p)
18962 {
18963 int declares_class_or_enum;
18964 cp_decl_specifier_seq decl_specifiers;
18965 cp_declarator *declarator;
18966 tree default_argument;
18967 cp_token *token = NULL, *declarator_token_start = NULL;
18968 const char *saved_message;
18969
18970 /* In a template parameter, `>' is not an operator.
18971
18972 [temp.param]
18973
18974 When parsing a default template-argument for a non-type
18975 template-parameter, the first non-nested `>' is taken as the end
18976 of the template parameter-list rather than a greater-than
18977 operator. */
18978
18979 /* Type definitions may not appear in parameter types. */
18980 saved_message = parser->type_definition_forbidden_message;
18981 parser->type_definition_forbidden_message
18982 = G_("types may not be defined in parameter types");
18983
18984 /* Parse the declaration-specifiers. */
18985 cp_parser_decl_specifier_seq (parser,
18986 CP_PARSER_FLAGS_NONE,
18987 &decl_specifiers,
18988 &declares_class_or_enum);
18989
18990 /* Complain about missing 'typename' or other invalid type names. */
18991 if (!decl_specifiers.any_type_specifiers_p
18992 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18993 decl_specifiers.type = error_mark_node;
18994
18995 /* If an error occurred, there's no reason to attempt to parse the
18996 rest of the declaration. */
18997 if (cp_parser_error_occurred (parser))
18998 {
18999 parser->type_definition_forbidden_message = saved_message;
19000 return NULL;
19001 }
19002
19003 /* Peek at the next token. */
19004 token = cp_lexer_peek_token (parser->lexer);
19005
19006 /* If the next token is a `)', `,', `=', `>', or `...', then there
19007 is no declarator. However, when variadic templates are enabled,
19008 there may be a declarator following `...'. */
19009 if (token->type == CPP_CLOSE_PAREN
19010 || token->type == CPP_COMMA
19011 || token->type == CPP_EQ
19012 || token->type == CPP_GREATER)
19013 {
19014 declarator = NULL;
19015 if (parenthesized_p)
19016 *parenthesized_p = false;
19017 }
19018 /* Otherwise, there should be a declarator. */
19019 else
19020 {
19021 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19022 parser->default_arg_ok_p = false;
19023
19024 /* After seeing a decl-specifier-seq, if the next token is not a
19025 "(", there is no possibility that the code is a valid
19026 expression. Therefore, if parsing tentatively, we commit at
19027 this point. */
19028 if (!parser->in_template_argument_list_p
19029 /* In an expression context, having seen:
19030
19031 (int((char ...
19032
19033 we cannot be sure whether we are looking at a
19034 function-type (taking a "char" as a parameter) or a cast
19035 of some object of type "char" to "int". */
19036 && !parser->in_type_id_in_expr_p
19037 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19038 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19039 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19040 cp_parser_commit_to_tentative_parse (parser);
19041 /* Parse the declarator. */
19042 declarator_token_start = token;
19043 declarator = cp_parser_declarator (parser,
19044 CP_PARSER_DECLARATOR_EITHER,
19045 /*ctor_dtor_or_conv_p=*/NULL,
19046 parenthesized_p,
19047 /*member_p=*/false,
19048 /*friend_p=*/false);
19049 parser->default_arg_ok_p = saved_default_arg_ok_p;
19050 /* After the declarator, allow more attributes. */
19051 decl_specifiers.attributes
19052 = chainon (decl_specifiers.attributes,
19053 cp_parser_attributes_opt (parser));
19054 }
19055
19056 /* If the next token is an ellipsis, and we have not seen a
19057 declarator name, and the type of the declarator contains parameter
19058 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19059 a parameter pack expansion expression. Otherwise, leave the
19060 ellipsis for a C-style variadic function. */
19061 token = cp_lexer_peek_token (parser->lexer);
19062 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19063 {
19064 tree type = decl_specifiers.type;
19065
19066 if (type && DECL_P (type))
19067 type = TREE_TYPE (type);
19068
19069 if (type
19070 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19071 && declarator_can_be_parameter_pack (declarator)
19072 && (!declarator || !declarator->parameter_pack_p)
19073 && uses_parameter_packs (type))
19074 {
19075 /* Consume the `...'. */
19076 cp_lexer_consume_token (parser->lexer);
19077 maybe_warn_variadic_templates ();
19078
19079 /* Build a pack expansion type */
19080 if (declarator)
19081 declarator->parameter_pack_p = true;
19082 else
19083 decl_specifiers.type = make_pack_expansion (type);
19084 }
19085 }
19086
19087 /* The restriction on defining new types applies only to the type
19088 of the parameter, not to the default argument. */
19089 parser->type_definition_forbidden_message = saved_message;
19090
19091 /* If the next token is `=', then process a default argument. */
19092 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19093 {
19094 token = cp_lexer_peek_token (parser->lexer);
19095 /* If we are defining a class, then the tokens that make up the
19096 default argument must be saved and processed later. */
19097 if (!template_parm_p && at_class_scope_p ()
19098 && TYPE_BEING_DEFINED (current_class_type)
19099 && !LAMBDA_TYPE_P (current_class_type))
19100 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19101 /* Outside of a class definition, we can just parse the
19102 assignment-expression. */
19103 else
19104 default_argument
19105 = cp_parser_default_argument (parser, template_parm_p);
19106
19107 if (!parser->default_arg_ok_p)
19108 {
19109 if (flag_permissive)
19110 warning (0, "deprecated use of default argument for parameter of non-function");
19111 else
19112 {
19113 error_at (token->location,
19114 "default arguments are only "
19115 "permitted for function parameters");
19116 default_argument = NULL_TREE;
19117 }
19118 }
19119 else if ((declarator && declarator->parameter_pack_p)
19120 || (decl_specifiers.type
19121 && PACK_EXPANSION_P (decl_specifiers.type)))
19122 {
19123 /* Find the name of the parameter pack. */
19124 cp_declarator *id_declarator = declarator;
19125 while (id_declarator && id_declarator->kind != cdk_id)
19126 id_declarator = id_declarator->declarator;
19127
19128 if (id_declarator && id_declarator->kind == cdk_id)
19129 error_at (declarator_token_start->location,
19130 template_parm_p
19131 ? G_("template parameter pack %qD "
19132 "cannot have a default argument")
19133 : G_("parameter pack %qD cannot have "
19134 "a default argument"),
19135 id_declarator->u.id.unqualified_name);
19136 else
19137 error_at (declarator_token_start->location,
19138 template_parm_p
19139 ? G_("template parameter pack cannot have "
19140 "a default argument")
19141 : G_("parameter pack cannot have a "
19142 "default argument"));
19143
19144 default_argument = NULL_TREE;
19145 }
19146 }
19147 else
19148 default_argument = NULL_TREE;
19149
19150 return make_parameter_declarator (&decl_specifiers,
19151 declarator,
19152 default_argument);
19153 }
19154
19155 /* Parse a default argument and return it.
19156
19157 TEMPLATE_PARM_P is true if this is a default argument for a
19158 non-type template parameter. */
19159 static tree
19160 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19161 {
19162 tree default_argument = NULL_TREE;
19163 bool saved_greater_than_is_operator_p;
19164 bool saved_local_variables_forbidden_p;
19165 bool non_constant_p, is_direct_init;
19166
19167 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19168 set correctly. */
19169 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19170 parser->greater_than_is_operator_p = !template_parm_p;
19171 /* Local variable names (and the `this' keyword) may not
19172 appear in a default argument. */
19173 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19174 parser->local_variables_forbidden_p = true;
19175 /* Parse the assignment-expression. */
19176 if (template_parm_p)
19177 push_deferring_access_checks (dk_no_deferred);
19178 tree saved_class_ptr = NULL_TREE;
19179 tree saved_class_ref = NULL_TREE;
19180 /* The "this" pointer is not valid in a default argument. */
19181 if (cfun)
19182 {
19183 saved_class_ptr = current_class_ptr;
19184 cp_function_chain->x_current_class_ptr = NULL_TREE;
19185 saved_class_ref = current_class_ref;
19186 cp_function_chain->x_current_class_ref = NULL_TREE;
19187 }
19188 default_argument
19189 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19190 /* Restore the "this" pointer. */
19191 if (cfun)
19192 {
19193 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19194 cp_function_chain->x_current_class_ref = saved_class_ref;
19195 }
19196 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19197 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19198 if (template_parm_p)
19199 pop_deferring_access_checks ();
19200 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19201 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19202
19203 return default_argument;
19204 }
19205
19206 /* Parse a function-body.
19207
19208 function-body:
19209 compound_statement */
19210
19211 static void
19212 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19213 {
19214 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19215 }
19216
19217 /* Parse a ctor-initializer-opt followed by a function-body. Return
19218 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19219 is true we are parsing a function-try-block. */
19220
19221 static bool
19222 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19223 bool in_function_try_block)
19224 {
19225 tree body, list;
19226 bool ctor_initializer_p;
19227 const bool check_body_p =
19228 DECL_CONSTRUCTOR_P (current_function_decl)
19229 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19230 tree last = NULL;
19231
19232 /* Begin the function body. */
19233 body = begin_function_body ();
19234 /* Parse the optional ctor-initializer. */
19235 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19236
19237 /* If we're parsing a constexpr constructor definition, we need
19238 to check that the constructor body is indeed empty. However,
19239 before we get to cp_parser_function_body lot of junk has been
19240 generated, so we can't just check that we have an empty block.
19241 Rather we take a snapshot of the outermost block, and check whether
19242 cp_parser_function_body changed its state. */
19243 if (check_body_p)
19244 {
19245 list = cur_stmt_list;
19246 if (STATEMENT_LIST_TAIL (list))
19247 last = STATEMENT_LIST_TAIL (list)->stmt;
19248 }
19249 /* Parse the function-body. */
19250 cp_parser_function_body (parser, in_function_try_block);
19251 if (check_body_p)
19252 check_constexpr_ctor_body (last, list, /*complain=*/true);
19253 /* Finish the function body. */
19254 finish_function_body (body);
19255
19256 return ctor_initializer_p;
19257 }
19258
19259 /* Parse an initializer.
19260
19261 initializer:
19262 = initializer-clause
19263 ( expression-list )
19264
19265 Returns an expression representing the initializer. If no
19266 initializer is present, NULL_TREE is returned.
19267
19268 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19269 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19270 set to TRUE if there is no initializer present. If there is an
19271 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19272 is set to true; otherwise it is set to false. */
19273
19274 static tree
19275 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19276 bool* non_constant_p)
19277 {
19278 cp_token *token;
19279 tree init;
19280
19281 /* Peek at the next token. */
19282 token = cp_lexer_peek_token (parser->lexer);
19283
19284 /* Let our caller know whether or not this initializer was
19285 parenthesized. */
19286 *is_direct_init = (token->type != CPP_EQ);
19287 /* Assume that the initializer is constant. */
19288 *non_constant_p = false;
19289
19290 if (token->type == CPP_EQ)
19291 {
19292 /* Consume the `='. */
19293 cp_lexer_consume_token (parser->lexer);
19294 /* Parse the initializer-clause. */
19295 init = cp_parser_initializer_clause (parser, non_constant_p);
19296 }
19297 else if (token->type == CPP_OPEN_PAREN)
19298 {
19299 vec<tree, va_gc> *vec;
19300 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19301 /*cast_p=*/false,
19302 /*allow_expansion_p=*/true,
19303 non_constant_p);
19304 if (vec == NULL)
19305 return error_mark_node;
19306 init = build_tree_list_vec (vec);
19307 release_tree_vector (vec);
19308 }
19309 else if (token->type == CPP_OPEN_BRACE)
19310 {
19311 cp_lexer_set_source_position (parser->lexer);
19312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19313 init = cp_parser_braced_list (parser, non_constant_p);
19314 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19315 }
19316 else
19317 {
19318 /* Anything else is an error. */
19319 cp_parser_error (parser, "expected initializer");
19320 init = error_mark_node;
19321 }
19322
19323 return init;
19324 }
19325
19326 /* Parse an initializer-clause.
19327
19328 initializer-clause:
19329 assignment-expression
19330 braced-init-list
19331
19332 Returns an expression representing the initializer.
19333
19334 If the `assignment-expression' production is used the value
19335 returned is simply a representation for the expression.
19336
19337 Otherwise, calls cp_parser_braced_list. */
19338
19339 static tree
19340 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19341 {
19342 tree initializer;
19343
19344 /* Assume the expression is constant. */
19345 *non_constant_p = false;
19346
19347 /* If it is not a `{', then we are looking at an
19348 assignment-expression. */
19349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19350 {
19351 initializer
19352 = cp_parser_constant_expression (parser,
19353 /*allow_non_constant_p=*/true,
19354 non_constant_p);
19355 }
19356 else
19357 initializer = cp_parser_braced_list (parser, non_constant_p);
19358
19359 return initializer;
19360 }
19361
19362 /* Parse a brace-enclosed initializer list.
19363
19364 braced-init-list:
19365 { initializer-list , [opt] }
19366 { }
19367
19368 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19369 the elements of the initializer-list (or NULL, if the last
19370 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19371 NULL_TREE. There is no way to detect whether or not the optional
19372 trailing `,' was provided. NON_CONSTANT_P is as for
19373 cp_parser_initializer. */
19374
19375 static tree
19376 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19377 {
19378 tree initializer;
19379
19380 /* Consume the `{' token. */
19381 cp_lexer_consume_token (parser->lexer);
19382 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19383 initializer = make_node (CONSTRUCTOR);
19384 /* If it's not a `}', then there is a non-trivial initializer. */
19385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19386 {
19387 /* Parse the initializer list. */
19388 CONSTRUCTOR_ELTS (initializer)
19389 = cp_parser_initializer_list (parser, non_constant_p);
19390 /* A trailing `,' token is allowed. */
19391 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19392 cp_lexer_consume_token (parser->lexer);
19393 }
19394 else
19395 *non_constant_p = false;
19396 /* Now, there should be a trailing `}'. */
19397 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19398 TREE_TYPE (initializer) = init_list_type_node;
19399 return initializer;
19400 }
19401
19402 /* Consume tokens up to, and including, the next non-nested closing `]'.
19403 Returns true iff we found a closing `]'. */
19404
19405 static bool
19406 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19407 {
19408 unsigned square_depth = 0;
19409
19410 while (true)
19411 {
19412 cp_token * token = cp_lexer_peek_token (parser->lexer);
19413
19414 switch (token->type)
19415 {
19416 case CPP_EOF:
19417 case CPP_PRAGMA_EOL:
19418 /* If we've run out of tokens, then there is no closing `]'. */
19419 return false;
19420
19421 case CPP_OPEN_SQUARE:
19422 ++square_depth;
19423 break;
19424
19425 case CPP_CLOSE_SQUARE:
19426 if (!square_depth--)
19427 {
19428 cp_lexer_consume_token (parser->lexer);
19429 return true;
19430 }
19431 break;
19432
19433 default:
19434 break;
19435 }
19436
19437 /* Consume the token. */
19438 cp_lexer_consume_token (parser->lexer);
19439 }
19440 }
19441
19442 /* Return true if we are looking at an array-designator, false otherwise. */
19443
19444 static bool
19445 cp_parser_array_designator_p (cp_parser *parser)
19446 {
19447 /* Consume the `['. */
19448 cp_lexer_consume_token (parser->lexer);
19449
19450 cp_lexer_save_tokens (parser->lexer);
19451
19452 /* Skip tokens until the next token is a closing square bracket.
19453 If we find the closing `]', and the next token is a `=', then
19454 we are looking at an array designator. */
19455 bool array_designator_p
19456 = (cp_parser_skip_to_closing_square_bracket (parser)
19457 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19458
19459 /* Roll back the tokens we skipped. */
19460 cp_lexer_rollback_tokens (parser->lexer);
19461
19462 return array_designator_p;
19463 }
19464
19465 /* Parse an initializer-list.
19466
19467 initializer-list:
19468 initializer-clause ... [opt]
19469 initializer-list , initializer-clause ... [opt]
19470
19471 GNU Extension:
19472
19473 initializer-list:
19474 designation initializer-clause ...[opt]
19475 initializer-list , designation initializer-clause ...[opt]
19476
19477 designation:
19478 . identifier =
19479 identifier :
19480 [ constant-expression ] =
19481
19482 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19483 for the initializer. If the INDEX of the elt is non-NULL, it is the
19484 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19485 as for cp_parser_initializer. */
19486
19487 static vec<constructor_elt, va_gc> *
19488 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19489 {
19490 vec<constructor_elt, va_gc> *v = NULL;
19491
19492 /* Assume all of the expressions are constant. */
19493 *non_constant_p = false;
19494
19495 /* Parse the rest of the list. */
19496 while (true)
19497 {
19498 cp_token *token;
19499 tree designator;
19500 tree initializer;
19501 bool clause_non_constant_p;
19502
19503 /* If the next token is an identifier and the following one is a
19504 colon, we are looking at the GNU designated-initializer
19505 syntax. */
19506 if (cp_parser_allow_gnu_extensions_p (parser)
19507 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19508 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19509 {
19510 /* Warn the user that they are using an extension. */
19511 pedwarn (input_location, OPT_Wpedantic,
19512 "ISO C++ does not allow designated initializers");
19513 /* Consume the identifier. */
19514 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19515 /* Consume the `:'. */
19516 cp_lexer_consume_token (parser->lexer);
19517 }
19518 /* Also handle the C99 syntax, '. id ='. */
19519 else if (cp_parser_allow_gnu_extensions_p (parser)
19520 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19521 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19522 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19523 {
19524 /* Warn the user that they are using an extension. */
19525 pedwarn (input_location, OPT_Wpedantic,
19526 "ISO C++ does not allow C99 designated initializers");
19527 /* Consume the `.'. */
19528 cp_lexer_consume_token (parser->lexer);
19529 /* Consume the identifier. */
19530 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19531 /* Consume the `='. */
19532 cp_lexer_consume_token (parser->lexer);
19533 }
19534 /* Also handle C99 array designators, '[ const ] ='. */
19535 else if (cp_parser_allow_gnu_extensions_p (parser)
19536 && !c_dialect_objc ()
19537 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19538 {
19539 /* In C++11, [ could start a lambda-introducer. */
19540 bool non_const = false;
19541
19542 cp_parser_parse_tentatively (parser);
19543
19544 if (!cp_parser_array_designator_p (parser))
19545 {
19546 cp_parser_simulate_error (parser);
19547 designator = NULL_TREE;
19548 }
19549 else
19550 {
19551 designator = cp_parser_constant_expression (parser, true,
19552 &non_const);
19553 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19554 cp_parser_require (parser, CPP_EQ, RT_EQ);
19555 }
19556
19557 if (!cp_parser_parse_definitely (parser))
19558 designator = NULL_TREE;
19559 else if (non_const)
19560 require_potential_rvalue_constant_expression (designator);
19561 }
19562 else
19563 designator = NULL_TREE;
19564
19565 /* Parse the initializer. */
19566 initializer = cp_parser_initializer_clause (parser,
19567 &clause_non_constant_p);
19568 /* If any clause is non-constant, so is the entire initializer. */
19569 if (clause_non_constant_p)
19570 *non_constant_p = true;
19571
19572 /* If we have an ellipsis, this is an initializer pack
19573 expansion. */
19574 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19575 {
19576 /* Consume the `...'. */
19577 cp_lexer_consume_token (parser->lexer);
19578
19579 /* Turn the initializer into an initializer expansion. */
19580 initializer = make_pack_expansion (initializer);
19581 }
19582
19583 /* Add it to the vector. */
19584 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19585
19586 /* If the next token is not a comma, we have reached the end of
19587 the list. */
19588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19589 break;
19590
19591 /* Peek at the next token. */
19592 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19593 /* If the next token is a `}', then we're still done. An
19594 initializer-clause can have a trailing `,' after the
19595 initializer-list and before the closing `}'. */
19596 if (token->type == CPP_CLOSE_BRACE)
19597 break;
19598
19599 /* Consume the `,' token. */
19600 cp_lexer_consume_token (parser->lexer);
19601 }
19602
19603 return v;
19604 }
19605
19606 /* Classes [gram.class] */
19607
19608 /* Parse a class-name.
19609
19610 class-name:
19611 identifier
19612 template-id
19613
19614 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19615 to indicate that names looked up in dependent types should be
19616 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19617 keyword has been used to indicate that the name that appears next
19618 is a template. TAG_TYPE indicates the explicit tag given before
19619 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19620 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19621 is the class being defined in a class-head. If ENUM_OK is TRUE,
19622 enum-names are also accepted.
19623
19624 Returns the TYPE_DECL representing the class. */
19625
19626 static tree
19627 cp_parser_class_name (cp_parser *parser,
19628 bool typename_keyword_p,
19629 bool template_keyword_p,
19630 enum tag_types tag_type,
19631 bool check_dependency_p,
19632 bool class_head_p,
19633 bool is_declaration,
19634 bool enum_ok)
19635 {
19636 tree decl;
19637 tree scope;
19638 bool typename_p;
19639 cp_token *token;
19640 tree identifier = NULL_TREE;
19641
19642 /* All class-names start with an identifier. */
19643 token = cp_lexer_peek_token (parser->lexer);
19644 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19645 {
19646 cp_parser_error (parser, "expected class-name");
19647 return error_mark_node;
19648 }
19649
19650 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19651 to a template-id, so we save it here. */
19652 scope = parser->scope;
19653 if (scope == error_mark_node)
19654 return error_mark_node;
19655
19656 /* Any name names a type if we're following the `typename' keyword
19657 in a qualified name where the enclosing scope is type-dependent. */
19658 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19659 && dependent_type_p (scope));
19660 /* Handle the common case (an identifier, but not a template-id)
19661 efficiently. */
19662 if (token->type == CPP_NAME
19663 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19664 {
19665 cp_token *identifier_token;
19666 bool ambiguous_p;
19667
19668 /* Look for the identifier. */
19669 identifier_token = cp_lexer_peek_token (parser->lexer);
19670 ambiguous_p = identifier_token->error_reported;
19671 identifier = cp_parser_identifier (parser);
19672 /* If the next token isn't an identifier, we are certainly not
19673 looking at a class-name. */
19674 if (identifier == error_mark_node)
19675 decl = error_mark_node;
19676 /* If we know this is a type-name, there's no need to look it
19677 up. */
19678 else if (typename_p)
19679 decl = identifier;
19680 else
19681 {
19682 tree ambiguous_decls;
19683 /* If we already know that this lookup is ambiguous, then
19684 we've already issued an error message; there's no reason
19685 to check again. */
19686 if (ambiguous_p)
19687 {
19688 cp_parser_simulate_error (parser);
19689 return error_mark_node;
19690 }
19691 /* If the next token is a `::', then the name must be a type
19692 name.
19693
19694 [basic.lookup.qual]
19695
19696 During the lookup for a name preceding the :: scope
19697 resolution operator, object, function, and enumerator
19698 names are ignored. */
19699 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19700 tag_type = typename_type;
19701 /* Look up the name. */
19702 decl = cp_parser_lookup_name (parser, identifier,
19703 tag_type,
19704 /*is_template=*/false,
19705 /*is_namespace=*/false,
19706 check_dependency_p,
19707 &ambiguous_decls,
19708 identifier_token->location);
19709 if (ambiguous_decls)
19710 {
19711 if (cp_parser_parsing_tentatively (parser))
19712 cp_parser_simulate_error (parser);
19713 return error_mark_node;
19714 }
19715 }
19716 }
19717 else
19718 {
19719 /* Try a template-id. */
19720 decl = cp_parser_template_id (parser, template_keyword_p,
19721 check_dependency_p,
19722 tag_type,
19723 is_declaration);
19724 if (decl == error_mark_node)
19725 return error_mark_node;
19726 }
19727
19728 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19729
19730 /* If this is a typename, create a TYPENAME_TYPE. */
19731 if (typename_p && decl != error_mark_node)
19732 {
19733 decl = make_typename_type (scope, decl, typename_type,
19734 /*complain=*/tf_error);
19735 if (decl != error_mark_node)
19736 decl = TYPE_NAME (decl);
19737 }
19738
19739 decl = strip_using_decl (decl);
19740
19741 /* Check to see that it is really the name of a class. */
19742 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19743 && identifier_p (TREE_OPERAND (decl, 0))
19744 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19745 /* Situations like this:
19746
19747 template <typename T> struct A {
19748 typename T::template X<int>::I i;
19749 };
19750
19751 are problematic. Is `T::template X<int>' a class-name? The
19752 standard does not seem to be definitive, but there is no other
19753 valid interpretation of the following `::'. Therefore, those
19754 names are considered class-names. */
19755 {
19756 decl = make_typename_type (scope, decl, tag_type, tf_error);
19757 if (decl != error_mark_node)
19758 decl = TYPE_NAME (decl);
19759 }
19760 else if (TREE_CODE (decl) != TYPE_DECL
19761 || TREE_TYPE (decl) == error_mark_node
19762 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19763 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19764 /* In Objective-C 2.0, a classname followed by '.' starts a
19765 dot-syntax expression, and it's not a type-name. */
19766 || (c_dialect_objc ()
19767 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19768 && objc_is_class_name (decl)))
19769 decl = error_mark_node;
19770
19771 if (decl == error_mark_node)
19772 cp_parser_error (parser, "expected class-name");
19773 else if (identifier && !parser->scope)
19774 maybe_note_name_used_in_class (identifier, decl);
19775
19776 return decl;
19777 }
19778
19779 /* Parse a class-specifier.
19780
19781 class-specifier:
19782 class-head { member-specification [opt] }
19783
19784 Returns the TREE_TYPE representing the class. */
19785
19786 static tree
19787 cp_parser_class_specifier_1 (cp_parser* parser)
19788 {
19789 tree type;
19790 tree attributes = NULL_TREE;
19791 bool nested_name_specifier_p;
19792 unsigned saved_num_template_parameter_lists;
19793 bool saved_in_function_body;
19794 unsigned char in_statement;
19795 bool in_switch_statement_p;
19796 bool saved_in_unbraced_linkage_specification_p;
19797 tree old_scope = NULL_TREE;
19798 tree scope = NULL_TREE;
19799 cp_token *closing_brace;
19800
19801 push_deferring_access_checks (dk_no_deferred);
19802
19803 /* Parse the class-head. */
19804 type = cp_parser_class_head (parser,
19805 &nested_name_specifier_p);
19806 /* If the class-head was a semantic disaster, skip the entire body
19807 of the class. */
19808 if (!type)
19809 {
19810 cp_parser_skip_to_end_of_block_or_statement (parser);
19811 pop_deferring_access_checks ();
19812 return error_mark_node;
19813 }
19814
19815 /* Look for the `{'. */
19816 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19817 {
19818 pop_deferring_access_checks ();
19819 return error_mark_node;
19820 }
19821
19822 cp_ensure_no_omp_declare_simd (parser);
19823
19824 /* Issue an error message if type-definitions are forbidden here. */
19825 cp_parser_check_type_definition (parser);
19826 /* Remember that we are defining one more class. */
19827 ++parser->num_classes_being_defined;
19828 /* Inside the class, surrounding template-parameter-lists do not
19829 apply. */
19830 saved_num_template_parameter_lists
19831 = parser->num_template_parameter_lists;
19832 parser->num_template_parameter_lists = 0;
19833 /* We are not in a function body. */
19834 saved_in_function_body = parser->in_function_body;
19835 parser->in_function_body = false;
19836 /* Or in a loop. */
19837 in_statement = parser->in_statement;
19838 parser->in_statement = 0;
19839 /* Or in a switch. */
19840 in_switch_statement_p = parser->in_switch_statement_p;
19841 parser->in_switch_statement_p = false;
19842 /* We are not immediately inside an extern "lang" block. */
19843 saved_in_unbraced_linkage_specification_p
19844 = parser->in_unbraced_linkage_specification_p;
19845 parser->in_unbraced_linkage_specification_p = false;
19846
19847 /* Start the class. */
19848 if (nested_name_specifier_p)
19849 {
19850 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19851 old_scope = push_inner_scope (scope);
19852 }
19853 type = begin_class_definition (type);
19854
19855 if (type == error_mark_node)
19856 /* If the type is erroneous, skip the entire body of the class. */
19857 cp_parser_skip_to_closing_brace (parser);
19858 else
19859 /* Parse the member-specification. */
19860 cp_parser_member_specification_opt (parser);
19861
19862 /* Look for the trailing `}'. */
19863 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19864 /* Look for trailing attributes to apply to this class. */
19865 if (cp_parser_allow_gnu_extensions_p (parser))
19866 attributes = cp_parser_gnu_attributes_opt (parser);
19867 if (type != error_mark_node)
19868 type = finish_struct (type, attributes);
19869 if (nested_name_specifier_p)
19870 pop_inner_scope (old_scope, scope);
19871
19872 /* We've finished a type definition. Check for the common syntax
19873 error of forgetting a semicolon after the definition. We need to
19874 be careful, as we can't just check for not-a-semicolon and be done
19875 with it; the user might have typed:
19876
19877 class X { } c = ...;
19878 class X { } *p = ...;
19879
19880 and so forth. Instead, enumerate all the possible tokens that
19881 might follow this production; if we don't see one of them, then
19882 complain and silently insert the semicolon. */
19883 {
19884 cp_token *token = cp_lexer_peek_token (parser->lexer);
19885 bool want_semicolon = true;
19886
19887 if (cp_next_tokens_can_be_std_attribute_p (parser))
19888 /* Don't try to parse c++11 attributes here. As per the
19889 grammar, that should be a task for
19890 cp_parser_decl_specifier_seq. */
19891 want_semicolon = false;
19892
19893 switch (token->type)
19894 {
19895 case CPP_NAME:
19896 case CPP_SEMICOLON:
19897 case CPP_MULT:
19898 case CPP_AND:
19899 case CPP_OPEN_PAREN:
19900 case CPP_CLOSE_PAREN:
19901 case CPP_COMMA:
19902 want_semicolon = false;
19903 break;
19904
19905 /* While it's legal for type qualifiers and storage class
19906 specifiers to follow type definitions in the grammar, only
19907 compiler testsuites contain code like that. Assume that if
19908 we see such code, then what we're really seeing is a case
19909 like:
19910
19911 class X { }
19912 const <type> var = ...;
19913
19914 or
19915
19916 class Y { }
19917 static <type> func (...) ...
19918
19919 i.e. the qualifier or specifier applies to the next
19920 declaration. To do so, however, we need to look ahead one
19921 more token to see if *that* token is a type specifier.
19922
19923 This code could be improved to handle:
19924
19925 class Z { }
19926 static const <type> var = ...; */
19927 case CPP_KEYWORD:
19928 if (keyword_is_decl_specifier (token->keyword))
19929 {
19930 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19931
19932 /* Handling user-defined types here would be nice, but very
19933 tricky. */
19934 want_semicolon
19935 = (lookahead->type == CPP_KEYWORD
19936 && keyword_begins_type_specifier (lookahead->keyword));
19937 }
19938 break;
19939 default:
19940 break;
19941 }
19942
19943 /* If we don't have a type, then something is very wrong and we
19944 shouldn't try to do anything clever. Likewise for not seeing the
19945 closing brace. */
19946 if (closing_brace && TYPE_P (type) && want_semicolon)
19947 {
19948 cp_token_position prev
19949 = cp_lexer_previous_token_position (parser->lexer);
19950 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19951 location_t loc = prev_token->location;
19952
19953 if (CLASSTYPE_DECLARED_CLASS (type))
19954 error_at (loc, "expected %<;%> after class definition");
19955 else if (TREE_CODE (type) == RECORD_TYPE)
19956 error_at (loc, "expected %<;%> after struct definition");
19957 else if (TREE_CODE (type) == UNION_TYPE)
19958 error_at (loc, "expected %<;%> after union definition");
19959 else
19960 gcc_unreachable ();
19961
19962 /* Unget one token and smash it to look as though we encountered
19963 a semicolon in the input stream. */
19964 cp_lexer_set_token_position (parser->lexer, prev);
19965 token = cp_lexer_peek_token (parser->lexer);
19966 token->type = CPP_SEMICOLON;
19967 token->keyword = RID_MAX;
19968 }
19969 }
19970
19971 /* If this class is not itself within the scope of another class,
19972 then we need to parse the bodies of all of the queued function
19973 definitions. Note that the queued functions defined in a class
19974 are not always processed immediately following the
19975 class-specifier for that class. Consider:
19976
19977 struct A {
19978 struct B { void f() { sizeof (A); } };
19979 };
19980
19981 If `f' were processed before the processing of `A' were
19982 completed, there would be no way to compute the size of `A'.
19983 Note that the nesting we are interested in here is lexical --
19984 not the semantic nesting given by TYPE_CONTEXT. In particular,
19985 for:
19986
19987 struct A { struct B; };
19988 struct A::B { void f() { } };
19989
19990 there is no need to delay the parsing of `A::B::f'. */
19991 if (--parser->num_classes_being_defined == 0)
19992 {
19993 tree decl;
19994 tree class_type = NULL_TREE;
19995 tree pushed_scope = NULL_TREE;
19996 unsigned ix;
19997 cp_default_arg_entry *e;
19998 tree save_ccp, save_ccr;
19999
20000 /* In a first pass, parse default arguments to the functions.
20001 Then, in a second pass, parse the bodies of the functions.
20002 This two-phased approach handles cases like:
20003
20004 struct S {
20005 void f() { g(); }
20006 void g(int i = 3);
20007 };
20008
20009 */
20010 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20011 {
20012 decl = e->decl;
20013 /* If there are default arguments that have not yet been processed,
20014 take care of them now. */
20015 if (class_type != e->class_type)
20016 {
20017 if (pushed_scope)
20018 pop_scope (pushed_scope);
20019 class_type = e->class_type;
20020 pushed_scope = push_scope (class_type);
20021 }
20022 /* Make sure that any template parameters are in scope. */
20023 maybe_begin_member_template_processing (decl);
20024 /* Parse the default argument expressions. */
20025 cp_parser_late_parsing_default_args (parser, decl);
20026 /* Remove any template parameters from the symbol table. */
20027 maybe_end_member_template_processing ();
20028 }
20029 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20030 /* Now parse any NSDMIs. */
20031 save_ccp = current_class_ptr;
20032 save_ccr = current_class_ref;
20033 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20034 {
20035 if (class_type != DECL_CONTEXT (decl))
20036 {
20037 if (pushed_scope)
20038 pop_scope (pushed_scope);
20039 class_type = DECL_CONTEXT (decl);
20040 pushed_scope = push_scope (class_type);
20041 }
20042 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20043 cp_parser_late_parsing_nsdmi (parser, decl);
20044 }
20045 vec_safe_truncate (unparsed_nsdmis, 0);
20046 current_class_ptr = save_ccp;
20047 current_class_ref = save_ccr;
20048 if (pushed_scope)
20049 pop_scope (pushed_scope);
20050
20051 /* Now do some post-NSDMI bookkeeping. */
20052 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20053 after_nsdmi_defaulted_late_checks (class_type);
20054 vec_safe_truncate (unparsed_classes, 0);
20055 after_nsdmi_defaulted_late_checks (type);
20056
20057 /* Now parse the body of the functions. */
20058 if (flag_openmp)
20059 {
20060 /* OpenMP UDRs need to be parsed before all other functions. */
20061 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20062 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20063 cp_parser_late_parsing_for_member (parser, decl);
20064 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20065 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20066 cp_parser_late_parsing_for_member (parser, decl);
20067 }
20068 else
20069 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20070 cp_parser_late_parsing_for_member (parser, decl);
20071 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20072 }
20073 else
20074 vec_safe_push (unparsed_classes, type);
20075
20076 /* Put back any saved access checks. */
20077 pop_deferring_access_checks ();
20078
20079 /* Restore saved state. */
20080 parser->in_switch_statement_p = in_switch_statement_p;
20081 parser->in_statement = in_statement;
20082 parser->in_function_body = saved_in_function_body;
20083 parser->num_template_parameter_lists
20084 = saved_num_template_parameter_lists;
20085 parser->in_unbraced_linkage_specification_p
20086 = saved_in_unbraced_linkage_specification_p;
20087
20088 return type;
20089 }
20090
20091 static tree
20092 cp_parser_class_specifier (cp_parser* parser)
20093 {
20094 tree ret;
20095 timevar_push (TV_PARSE_STRUCT);
20096 ret = cp_parser_class_specifier_1 (parser);
20097 timevar_pop (TV_PARSE_STRUCT);
20098 return ret;
20099 }
20100
20101 /* Parse a class-head.
20102
20103 class-head:
20104 class-key identifier [opt] base-clause [opt]
20105 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20106 class-key nested-name-specifier [opt] template-id
20107 base-clause [opt]
20108
20109 class-virt-specifier:
20110 final
20111
20112 GNU Extensions:
20113 class-key attributes identifier [opt] base-clause [opt]
20114 class-key attributes nested-name-specifier identifier base-clause [opt]
20115 class-key attributes nested-name-specifier [opt] template-id
20116 base-clause [opt]
20117
20118 Upon return BASES is initialized to the list of base classes (or
20119 NULL, if there are none) in the same form returned by
20120 cp_parser_base_clause.
20121
20122 Returns the TYPE of the indicated class. Sets
20123 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20124 involving a nested-name-specifier was used, and FALSE otherwise.
20125
20126 Returns error_mark_node if this is not a class-head.
20127
20128 Returns NULL_TREE if the class-head is syntactically valid, but
20129 semantically invalid in a way that means we should skip the entire
20130 body of the class. */
20131
20132 static tree
20133 cp_parser_class_head (cp_parser* parser,
20134 bool* nested_name_specifier_p)
20135 {
20136 tree nested_name_specifier;
20137 enum tag_types class_key;
20138 tree id = NULL_TREE;
20139 tree type = NULL_TREE;
20140 tree attributes;
20141 tree bases;
20142 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20143 bool template_id_p = false;
20144 bool qualified_p = false;
20145 bool invalid_nested_name_p = false;
20146 bool invalid_explicit_specialization_p = false;
20147 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20148 tree pushed_scope = NULL_TREE;
20149 unsigned num_templates;
20150 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20151 /* Assume no nested-name-specifier will be present. */
20152 *nested_name_specifier_p = false;
20153 /* Assume no template parameter lists will be used in defining the
20154 type. */
20155 num_templates = 0;
20156 parser->colon_corrects_to_scope_p = false;
20157
20158 /* Look for the class-key. */
20159 class_key = cp_parser_class_key (parser);
20160 if (class_key == none_type)
20161 return error_mark_node;
20162
20163 /* Parse the attributes. */
20164 attributes = cp_parser_attributes_opt (parser);
20165
20166 /* If the next token is `::', that is invalid -- but sometimes
20167 people do try to write:
20168
20169 struct ::S {};
20170
20171 Handle this gracefully by accepting the extra qualifier, and then
20172 issuing an error about it later if this really is a
20173 class-head. If it turns out just to be an elaborated type
20174 specifier, remain silent. */
20175 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20176 qualified_p = true;
20177
20178 push_deferring_access_checks (dk_no_check);
20179
20180 /* Determine the name of the class. Begin by looking for an
20181 optional nested-name-specifier. */
20182 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20183 nested_name_specifier
20184 = cp_parser_nested_name_specifier_opt (parser,
20185 /*typename_keyword_p=*/false,
20186 /*check_dependency_p=*/false,
20187 /*type_p=*/true,
20188 /*is_declaration=*/false);
20189 /* If there was a nested-name-specifier, then there *must* be an
20190 identifier. */
20191 if (nested_name_specifier)
20192 {
20193 type_start_token = cp_lexer_peek_token (parser->lexer);
20194 /* Although the grammar says `identifier', it really means
20195 `class-name' or `template-name'. You are only allowed to
20196 define a class that has already been declared with this
20197 syntax.
20198
20199 The proposed resolution for Core Issue 180 says that wherever
20200 you see `class T::X' you should treat `X' as a type-name.
20201
20202 It is OK to define an inaccessible class; for example:
20203
20204 class A { class B; };
20205 class A::B {};
20206
20207 We do not know if we will see a class-name, or a
20208 template-name. We look for a class-name first, in case the
20209 class-name is a template-id; if we looked for the
20210 template-name first we would stop after the template-name. */
20211 cp_parser_parse_tentatively (parser);
20212 type = cp_parser_class_name (parser,
20213 /*typename_keyword_p=*/false,
20214 /*template_keyword_p=*/false,
20215 class_type,
20216 /*check_dependency_p=*/false,
20217 /*class_head_p=*/true,
20218 /*is_declaration=*/false);
20219 /* If that didn't work, ignore the nested-name-specifier. */
20220 if (!cp_parser_parse_definitely (parser))
20221 {
20222 invalid_nested_name_p = true;
20223 type_start_token = cp_lexer_peek_token (parser->lexer);
20224 id = cp_parser_identifier (parser);
20225 if (id == error_mark_node)
20226 id = NULL_TREE;
20227 }
20228 /* If we could not find a corresponding TYPE, treat this
20229 declaration like an unqualified declaration. */
20230 if (type == error_mark_node)
20231 nested_name_specifier = NULL_TREE;
20232 /* Otherwise, count the number of templates used in TYPE and its
20233 containing scopes. */
20234 else
20235 {
20236 tree scope;
20237
20238 for (scope = TREE_TYPE (type);
20239 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20240 scope = get_containing_scope (scope))
20241 if (TYPE_P (scope)
20242 && CLASS_TYPE_P (scope)
20243 && CLASSTYPE_TEMPLATE_INFO (scope)
20244 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20245 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20246 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20247 ++num_templates;
20248 }
20249 }
20250 /* Otherwise, the identifier is optional. */
20251 else
20252 {
20253 /* We don't know whether what comes next is a template-id,
20254 an identifier, or nothing at all. */
20255 cp_parser_parse_tentatively (parser);
20256 /* Check for a template-id. */
20257 type_start_token = cp_lexer_peek_token (parser->lexer);
20258 id = cp_parser_template_id (parser,
20259 /*template_keyword_p=*/false,
20260 /*check_dependency_p=*/true,
20261 class_key,
20262 /*is_declaration=*/true);
20263 /* If that didn't work, it could still be an identifier. */
20264 if (!cp_parser_parse_definitely (parser))
20265 {
20266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20267 {
20268 type_start_token = cp_lexer_peek_token (parser->lexer);
20269 id = cp_parser_identifier (parser);
20270 }
20271 else
20272 id = NULL_TREE;
20273 }
20274 else
20275 {
20276 template_id_p = true;
20277 ++num_templates;
20278 }
20279 }
20280
20281 pop_deferring_access_checks ();
20282
20283 if (id)
20284 {
20285 cp_parser_check_for_invalid_template_id (parser, id,
20286 class_key,
20287 type_start_token->location);
20288 }
20289 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20290
20291 /* If it's not a `:' or a `{' then we can't really be looking at a
20292 class-head, since a class-head only appears as part of a
20293 class-specifier. We have to detect this situation before calling
20294 xref_tag, since that has irreversible side-effects. */
20295 if (!cp_parser_next_token_starts_class_definition_p (parser))
20296 {
20297 cp_parser_error (parser, "expected %<{%> or %<:%>");
20298 type = error_mark_node;
20299 goto out;
20300 }
20301
20302 /* At this point, we're going ahead with the class-specifier, even
20303 if some other problem occurs. */
20304 cp_parser_commit_to_tentative_parse (parser);
20305 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20306 {
20307 cp_parser_error (parser,
20308 "cannot specify %<override%> for a class");
20309 type = error_mark_node;
20310 goto out;
20311 }
20312 /* Issue the error about the overly-qualified name now. */
20313 if (qualified_p)
20314 {
20315 cp_parser_error (parser,
20316 "global qualification of class name is invalid");
20317 type = error_mark_node;
20318 goto out;
20319 }
20320 else if (invalid_nested_name_p)
20321 {
20322 cp_parser_error (parser,
20323 "qualified name does not name a class");
20324 type = error_mark_node;
20325 goto out;
20326 }
20327 else if (nested_name_specifier)
20328 {
20329 tree scope;
20330
20331 /* Reject typedef-names in class heads. */
20332 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20333 {
20334 error_at (type_start_token->location,
20335 "invalid class name in declaration of %qD",
20336 type);
20337 type = NULL_TREE;
20338 goto done;
20339 }
20340
20341 /* Figure out in what scope the declaration is being placed. */
20342 scope = current_scope ();
20343 /* If that scope does not contain the scope in which the
20344 class was originally declared, the program is invalid. */
20345 if (scope && !is_ancestor (scope, nested_name_specifier))
20346 {
20347 if (at_namespace_scope_p ())
20348 error_at (type_start_token->location,
20349 "declaration of %qD in namespace %qD which does not "
20350 "enclose %qD",
20351 type, scope, nested_name_specifier);
20352 else
20353 error_at (type_start_token->location,
20354 "declaration of %qD in %qD which does not enclose %qD",
20355 type, scope, nested_name_specifier);
20356 type = NULL_TREE;
20357 goto done;
20358 }
20359 /* [dcl.meaning]
20360
20361 A declarator-id shall not be qualified except for the
20362 definition of a ... nested class outside of its class
20363 ... [or] the definition or explicit instantiation of a
20364 class member of a namespace outside of its namespace. */
20365 if (scope == nested_name_specifier)
20366 {
20367 permerror (nested_name_specifier_token_start->location,
20368 "extra qualification not allowed");
20369 nested_name_specifier = NULL_TREE;
20370 num_templates = 0;
20371 }
20372 }
20373 /* An explicit-specialization must be preceded by "template <>". If
20374 it is not, try to recover gracefully. */
20375 if (at_namespace_scope_p ()
20376 && parser->num_template_parameter_lists == 0
20377 && template_id_p)
20378 {
20379 error_at (type_start_token->location,
20380 "an explicit specialization must be preceded by %<template <>%>");
20381 invalid_explicit_specialization_p = true;
20382 /* Take the same action that would have been taken by
20383 cp_parser_explicit_specialization. */
20384 ++parser->num_template_parameter_lists;
20385 begin_specialization ();
20386 }
20387 /* There must be no "return" statements between this point and the
20388 end of this function; set "type "to the correct return value and
20389 use "goto done;" to return. */
20390 /* Make sure that the right number of template parameters were
20391 present. */
20392 if (!cp_parser_check_template_parameters (parser, num_templates,
20393 type_start_token->location,
20394 /*declarator=*/NULL))
20395 {
20396 /* If something went wrong, there is no point in even trying to
20397 process the class-definition. */
20398 type = NULL_TREE;
20399 goto done;
20400 }
20401
20402 /* Look up the type. */
20403 if (template_id_p)
20404 {
20405 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20406 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20407 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20408 {
20409 error_at (type_start_token->location,
20410 "function template %qD redeclared as a class template", id);
20411 type = error_mark_node;
20412 }
20413 else
20414 {
20415 type = TREE_TYPE (id);
20416 type = maybe_process_partial_specialization (type);
20417 }
20418 if (nested_name_specifier)
20419 pushed_scope = push_scope (nested_name_specifier);
20420 }
20421 else if (nested_name_specifier)
20422 {
20423 tree class_type;
20424
20425 /* Given:
20426
20427 template <typename T> struct S { struct T };
20428 template <typename T> struct S<T>::T { };
20429
20430 we will get a TYPENAME_TYPE when processing the definition of
20431 `S::T'. We need to resolve it to the actual type before we
20432 try to define it. */
20433 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20434 {
20435 class_type = resolve_typename_type (TREE_TYPE (type),
20436 /*only_current_p=*/false);
20437 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20438 type = TYPE_NAME (class_type);
20439 else
20440 {
20441 cp_parser_error (parser, "could not resolve typename type");
20442 type = error_mark_node;
20443 }
20444 }
20445
20446 if (maybe_process_partial_specialization (TREE_TYPE (type))
20447 == error_mark_node)
20448 {
20449 type = NULL_TREE;
20450 goto done;
20451 }
20452
20453 class_type = current_class_type;
20454 /* Enter the scope indicated by the nested-name-specifier. */
20455 pushed_scope = push_scope (nested_name_specifier);
20456 /* Get the canonical version of this type. */
20457 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20458 /* Call push_template_decl if it seems like we should be defining a
20459 template either from the template headers or the type we're
20460 defining, so that we diagnose both extra and missing headers. */
20461 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20462 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20463 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20464 {
20465 type = push_template_decl (type);
20466 if (type == error_mark_node)
20467 {
20468 type = NULL_TREE;
20469 goto done;
20470 }
20471 }
20472
20473 type = TREE_TYPE (type);
20474 *nested_name_specifier_p = true;
20475 }
20476 else /* The name is not a nested name. */
20477 {
20478 /* If the class was unnamed, create a dummy name. */
20479 if (!id)
20480 id = make_anon_name ();
20481 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20482 parser->num_template_parameter_lists);
20483 }
20484
20485 /* Indicate whether this class was declared as a `class' or as a
20486 `struct'. */
20487 if (TREE_CODE (type) == RECORD_TYPE)
20488 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20489 cp_parser_check_class_key (class_key, type);
20490
20491 /* If this type was already complete, and we see another definition,
20492 that's an error. */
20493 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20494 {
20495 error_at (type_start_token->location, "redefinition of %q#T",
20496 type);
20497 error_at (type_start_token->location, "previous definition of %q+#T",
20498 type);
20499 type = NULL_TREE;
20500 goto done;
20501 }
20502 else if (type == error_mark_node)
20503 type = NULL_TREE;
20504
20505 if (type)
20506 {
20507 /* Apply attributes now, before any use of the class as a template
20508 argument in its base list. */
20509 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20510 fixup_attribute_variants (type);
20511 }
20512
20513 /* We will have entered the scope containing the class; the names of
20514 base classes should be looked up in that context. For example:
20515
20516 struct A { struct B {}; struct C; };
20517 struct A::C : B {};
20518
20519 is valid. */
20520
20521 /* Get the list of base-classes, if there is one. */
20522 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20523 {
20524 /* PR59482: enter the class scope so that base-specifiers are looked
20525 up correctly. */
20526 if (type)
20527 pushclass (type);
20528 bases = cp_parser_base_clause (parser);
20529 /* PR59482: get out of the previously pushed class scope so that the
20530 subsequent pops pop the right thing. */
20531 if (type)
20532 popclass ();
20533 }
20534 else
20535 bases = NULL_TREE;
20536
20537 /* If we're really defining a class, process the base classes.
20538 If they're invalid, fail. */
20539 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20540 && !xref_basetypes (type, bases))
20541 type = NULL_TREE;
20542
20543 done:
20544 /* Leave the scope given by the nested-name-specifier. We will
20545 enter the class scope itself while processing the members. */
20546 if (pushed_scope)
20547 pop_scope (pushed_scope);
20548
20549 if (invalid_explicit_specialization_p)
20550 {
20551 end_specialization ();
20552 --parser->num_template_parameter_lists;
20553 }
20554
20555 if (type)
20556 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20557 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20558 CLASSTYPE_FINAL (type) = 1;
20559 out:
20560 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20561 return type;
20562 }
20563
20564 /* Parse a class-key.
20565
20566 class-key:
20567 class
20568 struct
20569 union
20570
20571 Returns the kind of class-key specified, or none_type to indicate
20572 error. */
20573
20574 static enum tag_types
20575 cp_parser_class_key (cp_parser* parser)
20576 {
20577 cp_token *token;
20578 enum tag_types tag_type;
20579
20580 /* Look for the class-key. */
20581 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20582 if (!token)
20583 return none_type;
20584
20585 /* Check to see if the TOKEN is a class-key. */
20586 tag_type = cp_parser_token_is_class_key (token);
20587 if (!tag_type)
20588 cp_parser_error (parser, "expected class-key");
20589 return tag_type;
20590 }
20591
20592 /* Parse a type-parameter-key.
20593
20594 type-parameter-key:
20595 class
20596 typename
20597 */
20598
20599 static void
20600 cp_parser_type_parameter_key (cp_parser* parser)
20601 {
20602 /* Look for the type-parameter-key. */
20603 enum tag_types tag_type = none_type;
20604 cp_token *token = cp_lexer_peek_token (parser->lexer);
20605 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20606 {
20607 cp_lexer_consume_token (parser->lexer);
20608 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20609 /* typename is not allowed in a template template parameter
20610 by the standard until C++1Z. */
20611 pedwarn (token->location, OPT_Wpedantic,
20612 "ISO C++ forbids typename key in template template parameter;"
20613 " use -std=c++1z or -std=gnu++1z");
20614 }
20615 else
20616 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20617
20618 return;
20619 }
20620
20621 /* Parse an (optional) member-specification.
20622
20623 member-specification:
20624 member-declaration member-specification [opt]
20625 access-specifier : member-specification [opt] */
20626
20627 static void
20628 cp_parser_member_specification_opt (cp_parser* parser)
20629 {
20630 while (true)
20631 {
20632 cp_token *token;
20633 enum rid keyword;
20634
20635 /* Peek at the next token. */
20636 token = cp_lexer_peek_token (parser->lexer);
20637 /* If it's a `}', or EOF then we've seen all the members. */
20638 if (token->type == CPP_CLOSE_BRACE
20639 || token->type == CPP_EOF
20640 || token->type == CPP_PRAGMA_EOL)
20641 break;
20642
20643 /* See if this token is a keyword. */
20644 keyword = token->keyword;
20645 switch (keyword)
20646 {
20647 case RID_PUBLIC:
20648 case RID_PROTECTED:
20649 case RID_PRIVATE:
20650 /* Consume the access-specifier. */
20651 cp_lexer_consume_token (parser->lexer);
20652 /* Remember which access-specifier is active. */
20653 current_access_specifier = token->u.value;
20654 /* Look for the `:'. */
20655 cp_parser_require (parser, CPP_COLON, RT_COLON);
20656 break;
20657
20658 default:
20659 /* Accept #pragmas at class scope. */
20660 if (token->type == CPP_PRAGMA)
20661 {
20662 cp_parser_pragma (parser, pragma_member);
20663 break;
20664 }
20665
20666 /* Otherwise, the next construction must be a
20667 member-declaration. */
20668 cp_parser_member_declaration (parser);
20669 }
20670 }
20671 }
20672
20673 /* Parse a member-declaration.
20674
20675 member-declaration:
20676 decl-specifier-seq [opt] member-declarator-list [opt] ;
20677 function-definition ; [opt]
20678 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20679 using-declaration
20680 template-declaration
20681 alias-declaration
20682
20683 member-declarator-list:
20684 member-declarator
20685 member-declarator-list , member-declarator
20686
20687 member-declarator:
20688 declarator pure-specifier [opt]
20689 declarator constant-initializer [opt]
20690 identifier [opt] : constant-expression
20691
20692 GNU Extensions:
20693
20694 member-declaration:
20695 __extension__ member-declaration
20696
20697 member-declarator:
20698 declarator attributes [opt] pure-specifier [opt]
20699 declarator attributes [opt] constant-initializer [opt]
20700 identifier [opt] attributes [opt] : constant-expression
20701
20702 C++0x Extensions:
20703
20704 member-declaration:
20705 static_assert-declaration */
20706
20707 static void
20708 cp_parser_member_declaration (cp_parser* parser)
20709 {
20710 cp_decl_specifier_seq decl_specifiers;
20711 tree prefix_attributes;
20712 tree decl;
20713 int declares_class_or_enum;
20714 bool friend_p;
20715 cp_token *token = NULL;
20716 cp_token *decl_spec_token_start = NULL;
20717 cp_token *initializer_token_start = NULL;
20718 int saved_pedantic;
20719 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20720
20721 /* Check for the `__extension__' keyword. */
20722 if (cp_parser_extension_opt (parser, &saved_pedantic))
20723 {
20724 /* Recurse. */
20725 cp_parser_member_declaration (parser);
20726 /* Restore the old value of the PEDANTIC flag. */
20727 pedantic = saved_pedantic;
20728
20729 return;
20730 }
20731
20732 /* Check for a template-declaration. */
20733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20734 {
20735 /* An explicit specialization here is an error condition, and we
20736 expect the specialization handler to detect and report this. */
20737 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20738 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20739 cp_parser_explicit_specialization (parser);
20740 else
20741 cp_parser_template_declaration (parser, /*member_p=*/true);
20742
20743 return;
20744 }
20745
20746 /* Check for a using-declaration. */
20747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20748 {
20749 if (cxx_dialect < cxx11)
20750 {
20751 /* Parse the using-declaration. */
20752 cp_parser_using_declaration (parser,
20753 /*access_declaration_p=*/false);
20754 return;
20755 }
20756 else
20757 {
20758 tree decl;
20759 bool alias_decl_expected;
20760 cp_parser_parse_tentatively (parser);
20761 decl = cp_parser_alias_declaration (parser);
20762 /* Note that if we actually see the '=' token after the
20763 identifier, cp_parser_alias_declaration commits the
20764 tentative parse. In that case, we really expects an
20765 alias-declaration. Otherwise, we expect a using
20766 declaration. */
20767 alias_decl_expected =
20768 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20769 cp_parser_parse_definitely (parser);
20770
20771 if (alias_decl_expected)
20772 finish_member_declaration (decl);
20773 else
20774 cp_parser_using_declaration (parser,
20775 /*access_declaration_p=*/false);
20776 return;
20777 }
20778 }
20779
20780 /* Check for @defs. */
20781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20782 {
20783 tree ivar, member;
20784 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20785 ivar = ivar_chains;
20786 while (ivar)
20787 {
20788 member = ivar;
20789 ivar = TREE_CHAIN (member);
20790 TREE_CHAIN (member) = NULL_TREE;
20791 finish_member_declaration (member);
20792 }
20793 return;
20794 }
20795
20796 /* If the next token is `static_assert' we have a static assertion. */
20797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20798 {
20799 cp_parser_static_assert (parser, /*member_p=*/true);
20800 return;
20801 }
20802
20803 parser->colon_corrects_to_scope_p = false;
20804
20805 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20806 goto out;
20807
20808 /* Parse the decl-specifier-seq. */
20809 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20810 cp_parser_decl_specifier_seq (parser,
20811 CP_PARSER_FLAGS_OPTIONAL,
20812 &decl_specifiers,
20813 &declares_class_or_enum);
20814 /* Check for an invalid type-name. */
20815 if (!decl_specifiers.any_type_specifiers_p
20816 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20817 goto out;
20818 /* If there is no declarator, then the decl-specifier-seq should
20819 specify a type. */
20820 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20821 {
20822 /* If there was no decl-specifier-seq, and the next token is a
20823 `;', then we have something like:
20824
20825 struct S { ; };
20826
20827 [class.mem]
20828
20829 Each member-declaration shall declare at least one member
20830 name of the class. */
20831 if (!decl_specifiers.any_specifiers_p)
20832 {
20833 cp_token *token = cp_lexer_peek_token (parser->lexer);
20834 if (!in_system_header_at (token->location))
20835 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20836 }
20837 else
20838 {
20839 tree type;
20840
20841 /* See if this declaration is a friend. */
20842 friend_p = cp_parser_friend_p (&decl_specifiers);
20843 /* If there were decl-specifiers, check to see if there was
20844 a class-declaration. */
20845 type = check_tag_decl (&decl_specifiers,
20846 /*explicit_type_instantiation_p=*/false);
20847 /* Nested classes have already been added to the class, but
20848 a `friend' needs to be explicitly registered. */
20849 if (friend_p)
20850 {
20851 /* If the `friend' keyword was present, the friend must
20852 be introduced with a class-key. */
20853 if (!declares_class_or_enum && cxx_dialect < cxx11)
20854 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20855 "in C++03 a class-key must be used "
20856 "when declaring a friend");
20857 /* In this case:
20858
20859 template <typename T> struct A {
20860 friend struct A<T>::B;
20861 };
20862
20863 A<T>::B will be represented by a TYPENAME_TYPE, and
20864 therefore not recognized by check_tag_decl. */
20865 if (!type)
20866 {
20867 type = decl_specifiers.type;
20868 if (type && TREE_CODE (type) == TYPE_DECL)
20869 type = TREE_TYPE (type);
20870 }
20871 if (!type || !TYPE_P (type))
20872 error_at (decl_spec_token_start->location,
20873 "friend declaration does not name a class or "
20874 "function");
20875 else
20876 make_friend_class (current_class_type, type,
20877 /*complain=*/true);
20878 }
20879 /* If there is no TYPE, an error message will already have
20880 been issued. */
20881 else if (!type || type == error_mark_node)
20882 ;
20883 /* An anonymous aggregate has to be handled specially; such
20884 a declaration really declares a data member (with a
20885 particular type), as opposed to a nested class. */
20886 else if (ANON_AGGR_TYPE_P (type))
20887 {
20888 /* C++11 9.5/6. */
20889 if (decl_specifiers.storage_class != sc_none)
20890 error_at (decl_spec_token_start->location,
20891 "a storage class on an anonymous aggregate "
20892 "in class scope is not allowed");
20893
20894 /* Remove constructors and such from TYPE, now that we
20895 know it is an anonymous aggregate. */
20896 fixup_anonymous_aggr (type);
20897 /* And make the corresponding data member. */
20898 decl = build_decl (decl_spec_token_start->location,
20899 FIELD_DECL, NULL_TREE, type);
20900 /* Add it to the class. */
20901 finish_member_declaration (decl);
20902 }
20903 else
20904 cp_parser_check_access_in_redeclaration
20905 (TYPE_NAME (type),
20906 decl_spec_token_start->location);
20907 }
20908 }
20909 else
20910 {
20911 bool assume_semicolon = false;
20912
20913 /* Clear attributes from the decl_specifiers but keep them
20914 around as prefix attributes that apply them to the entity
20915 being declared. */
20916 prefix_attributes = decl_specifiers.attributes;
20917 decl_specifiers.attributes = NULL_TREE;
20918
20919 /* See if these declarations will be friends. */
20920 friend_p = cp_parser_friend_p (&decl_specifiers);
20921
20922 /* Keep going until we hit the `;' at the end of the
20923 declaration. */
20924 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20925 {
20926 tree attributes = NULL_TREE;
20927 tree first_attribute;
20928
20929 /* Peek at the next token. */
20930 token = cp_lexer_peek_token (parser->lexer);
20931
20932 /* Check for a bitfield declaration. */
20933 if (token->type == CPP_COLON
20934 || (token->type == CPP_NAME
20935 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20936 == CPP_COLON))
20937 {
20938 tree identifier;
20939 tree width;
20940
20941 /* Get the name of the bitfield. Note that we cannot just
20942 check TOKEN here because it may have been invalidated by
20943 the call to cp_lexer_peek_nth_token above. */
20944 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20945 identifier = cp_parser_identifier (parser);
20946 else
20947 identifier = NULL_TREE;
20948
20949 /* Consume the `:' token. */
20950 cp_lexer_consume_token (parser->lexer);
20951 /* Get the width of the bitfield. */
20952 width
20953 = cp_parser_constant_expression (parser);
20954
20955 /* Look for attributes that apply to the bitfield. */
20956 attributes = cp_parser_attributes_opt (parser);
20957 /* Remember which attributes are prefix attributes and
20958 which are not. */
20959 first_attribute = attributes;
20960 /* Combine the attributes. */
20961 attributes = chainon (prefix_attributes, attributes);
20962
20963 /* Create the bitfield declaration. */
20964 decl = grokbitfield (identifier
20965 ? make_id_declarator (NULL_TREE,
20966 identifier,
20967 sfk_none)
20968 : NULL,
20969 &decl_specifiers,
20970 width,
20971 attributes);
20972 }
20973 else
20974 {
20975 cp_declarator *declarator;
20976 tree initializer;
20977 tree asm_specification;
20978 int ctor_dtor_or_conv_p;
20979
20980 /* Parse the declarator. */
20981 declarator
20982 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20983 &ctor_dtor_or_conv_p,
20984 /*parenthesized_p=*/NULL,
20985 /*member_p=*/true,
20986 friend_p);
20987
20988 /* If something went wrong parsing the declarator, make sure
20989 that we at least consume some tokens. */
20990 if (declarator == cp_error_declarator)
20991 {
20992 /* Skip to the end of the statement. */
20993 cp_parser_skip_to_end_of_statement (parser);
20994 /* If the next token is not a semicolon, that is
20995 probably because we just skipped over the body of
20996 a function. So, we consume a semicolon if
20997 present, but do not issue an error message if it
20998 is not present. */
20999 if (cp_lexer_next_token_is (parser->lexer,
21000 CPP_SEMICOLON))
21001 cp_lexer_consume_token (parser->lexer);
21002 goto out;
21003 }
21004
21005 if (declares_class_or_enum & 2)
21006 cp_parser_check_for_definition_in_return_type
21007 (declarator, decl_specifiers.type,
21008 decl_specifiers.locations[ds_type_spec]);
21009
21010 /* Look for an asm-specification. */
21011 asm_specification = cp_parser_asm_specification_opt (parser);
21012 /* Look for attributes that apply to the declaration. */
21013 attributes = cp_parser_attributes_opt (parser);
21014 /* Remember which attributes are prefix attributes and
21015 which are not. */
21016 first_attribute = attributes;
21017 /* Combine the attributes. */
21018 attributes = chainon (prefix_attributes, attributes);
21019
21020 /* If it's an `=', then we have a constant-initializer or a
21021 pure-specifier. It is not correct to parse the
21022 initializer before registering the member declaration
21023 since the member declaration should be in scope while
21024 its initializer is processed. However, the rest of the
21025 front end does not yet provide an interface that allows
21026 us to handle this correctly. */
21027 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21028 {
21029 /* In [class.mem]:
21030
21031 A pure-specifier shall be used only in the declaration of
21032 a virtual function.
21033
21034 A member-declarator can contain a constant-initializer
21035 only if it declares a static member of integral or
21036 enumeration type.
21037
21038 Therefore, if the DECLARATOR is for a function, we look
21039 for a pure-specifier; otherwise, we look for a
21040 constant-initializer. When we call `grokfield', it will
21041 perform more stringent semantics checks. */
21042 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21043 if (function_declarator_p (declarator)
21044 || (decl_specifiers.type
21045 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21046 && declarator->kind == cdk_id
21047 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21048 == FUNCTION_TYPE)))
21049 initializer = cp_parser_pure_specifier (parser);
21050 else if (decl_specifiers.storage_class != sc_static)
21051 initializer = cp_parser_save_nsdmi (parser);
21052 else if (cxx_dialect >= cxx11)
21053 {
21054 bool nonconst;
21055 /* Don't require a constant rvalue in C++11, since we
21056 might want a reference constant. We'll enforce
21057 constancy later. */
21058 cp_lexer_consume_token (parser->lexer);
21059 /* Parse the initializer. */
21060 initializer = cp_parser_initializer_clause (parser,
21061 &nonconst);
21062 }
21063 else
21064 /* Parse the initializer. */
21065 initializer = cp_parser_constant_initializer (parser);
21066 }
21067 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21068 && !function_declarator_p (declarator))
21069 {
21070 bool x;
21071 if (decl_specifiers.storage_class != sc_static)
21072 initializer = cp_parser_save_nsdmi (parser);
21073 else
21074 initializer = cp_parser_initializer (parser, &x, &x);
21075 }
21076 /* Otherwise, there is no initializer. */
21077 else
21078 initializer = NULL_TREE;
21079
21080 /* See if we are probably looking at a function
21081 definition. We are certainly not looking at a
21082 member-declarator. Calling `grokfield' has
21083 side-effects, so we must not do it unless we are sure
21084 that we are looking at a member-declarator. */
21085 if (cp_parser_token_starts_function_definition_p
21086 (cp_lexer_peek_token (parser->lexer)))
21087 {
21088 /* The grammar does not allow a pure-specifier to be
21089 used when a member function is defined. (It is
21090 possible that this fact is an oversight in the
21091 standard, since a pure function may be defined
21092 outside of the class-specifier. */
21093 if (initializer && initializer_token_start)
21094 error_at (initializer_token_start->location,
21095 "pure-specifier on function-definition");
21096 decl = cp_parser_save_member_function_body (parser,
21097 &decl_specifiers,
21098 declarator,
21099 attributes);
21100 if (parser->fully_implicit_function_template_p)
21101 decl = finish_fully_implicit_template (parser, decl);
21102 /* If the member was not a friend, declare it here. */
21103 if (!friend_p)
21104 finish_member_declaration (decl);
21105 /* Peek at the next token. */
21106 token = cp_lexer_peek_token (parser->lexer);
21107 /* If the next token is a semicolon, consume it. */
21108 if (token->type == CPP_SEMICOLON)
21109 cp_lexer_consume_token (parser->lexer);
21110 goto out;
21111 }
21112 else
21113 if (declarator->kind == cdk_function)
21114 declarator->id_loc = token->location;
21115 /* Create the declaration. */
21116 decl = grokfield (declarator, &decl_specifiers,
21117 initializer, /*init_const_expr_p=*/true,
21118 asm_specification, attributes);
21119 if (parser->fully_implicit_function_template_p)
21120 {
21121 if (friend_p)
21122 finish_fully_implicit_template (parser, 0);
21123 else
21124 decl = finish_fully_implicit_template (parser, decl);
21125 }
21126 }
21127
21128 cp_finalize_omp_declare_simd (parser, decl);
21129
21130 /* Reset PREFIX_ATTRIBUTES. */
21131 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21132 attributes = TREE_CHAIN (attributes);
21133 if (attributes)
21134 TREE_CHAIN (attributes) = NULL_TREE;
21135
21136 /* If there is any qualification still in effect, clear it
21137 now; we will be starting fresh with the next declarator. */
21138 parser->scope = NULL_TREE;
21139 parser->qualifying_scope = NULL_TREE;
21140 parser->object_scope = NULL_TREE;
21141 /* If it's a `,', then there are more declarators. */
21142 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21143 {
21144 cp_lexer_consume_token (parser->lexer);
21145 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21146 {
21147 cp_token *token = cp_lexer_previous_token (parser->lexer);
21148 error_at (token->location,
21149 "stray %<,%> at end of member declaration");
21150 }
21151 }
21152 /* If the next token isn't a `;', then we have a parse error. */
21153 else if (cp_lexer_next_token_is_not (parser->lexer,
21154 CPP_SEMICOLON))
21155 {
21156 /* The next token might be a ways away from where the
21157 actual semicolon is missing. Find the previous token
21158 and use that for our error position. */
21159 cp_token *token = cp_lexer_previous_token (parser->lexer);
21160 error_at (token->location,
21161 "expected %<;%> at end of member declaration");
21162
21163 /* Assume that the user meant to provide a semicolon. If
21164 we were to cp_parser_skip_to_end_of_statement, we might
21165 skip to a semicolon inside a member function definition
21166 and issue nonsensical error messages. */
21167 assume_semicolon = true;
21168 }
21169
21170 if (decl)
21171 {
21172 /* Add DECL to the list of members. */
21173 if (!friend_p
21174 /* Explicitly include, eg, NSDMIs, for better error
21175 recovery (c++/58650). */
21176 || !DECL_DECLARES_FUNCTION_P (decl))
21177 finish_member_declaration (decl);
21178
21179 if (TREE_CODE (decl) == FUNCTION_DECL)
21180 cp_parser_save_default_args (parser, decl);
21181 else if (TREE_CODE (decl) == FIELD_DECL
21182 && !DECL_C_BIT_FIELD (decl)
21183 && DECL_INITIAL (decl))
21184 /* Add DECL to the queue of NSDMI to be parsed later. */
21185 vec_safe_push (unparsed_nsdmis, decl);
21186 }
21187
21188 if (assume_semicolon)
21189 goto out;
21190 }
21191 }
21192
21193 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21194 out:
21195 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21196 }
21197
21198 /* Parse a pure-specifier.
21199
21200 pure-specifier:
21201 = 0
21202
21203 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21204 Otherwise, ERROR_MARK_NODE is returned. */
21205
21206 static tree
21207 cp_parser_pure_specifier (cp_parser* parser)
21208 {
21209 cp_token *token;
21210
21211 /* Look for the `=' token. */
21212 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21213 return error_mark_node;
21214 /* Look for the `0' token. */
21215 token = cp_lexer_peek_token (parser->lexer);
21216
21217 if (token->type == CPP_EOF
21218 || token->type == CPP_PRAGMA_EOL)
21219 return error_mark_node;
21220
21221 cp_lexer_consume_token (parser->lexer);
21222
21223 /* Accept = default or = delete in c++0x mode. */
21224 if (token->keyword == RID_DEFAULT
21225 || token->keyword == RID_DELETE)
21226 {
21227 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21228 return token->u.value;
21229 }
21230
21231 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21232 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21233 {
21234 cp_parser_error (parser,
21235 "invalid pure specifier (only %<= 0%> is allowed)");
21236 cp_parser_skip_to_end_of_statement (parser);
21237 return error_mark_node;
21238 }
21239 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21240 {
21241 error_at (token->location, "templates may not be %<virtual%>");
21242 return error_mark_node;
21243 }
21244
21245 return integer_zero_node;
21246 }
21247
21248 /* Parse a constant-initializer.
21249
21250 constant-initializer:
21251 = constant-expression
21252
21253 Returns a representation of the constant-expression. */
21254
21255 static tree
21256 cp_parser_constant_initializer (cp_parser* parser)
21257 {
21258 /* Look for the `=' token. */
21259 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21260 return error_mark_node;
21261
21262 /* It is invalid to write:
21263
21264 struct S { static const int i = { 7 }; };
21265
21266 */
21267 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21268 {
21269 cp_parser_error (parser,
21270 "a brace-enclosed initializer is not allowed here");
21271 /* Consume the opening brace. */
21272 cp_lexer_consume_token (parser->lexer);
21273 /* Skip the initializer. */
21274 cp_parser_skip_to_closing_brace (parser);
21275 /* Look for the trailing `}'. */
21276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21277
21278 return error_mark_node;
21279 }
21280
21281 return cp_parser_constant_expression (parser);
21282 }
21283
21284 /* Derived classes [gram.class.derived] */
21285
21286 /* Parse a base-clause.
21287
21288 base-clause:
21289 : base-specifier-list
21290
21291 base-specifier-list:
21292 base-specifier ... [opt]
21293 base-specifier-list , base-specifier ... [opt]
21294
21295 Returns a TREE_LIST representing the base-classes, in the order in
21296 which they were declared. The representation of each node is as
21297 described by cp_parser_base_specifier.
21298
21299 In the case that no bases are specified, this function will return
21300 NULL_TREE, not ERROR_MARK_NODE. */
21301
21302 static tree
21303 cp_parser_base_clause (cp_parser* parser)
21304 {
21305 tree bases = NULL_TREE;
21306
21307 /* Look for the `:' that begins the list. */
21308 cp_parser_require (parser, CPP_COLON, RT_COLON);
21309
21310 /* Scan the base-specifier-list. */
21311 while (true)
21312 {
21313 cp_token *token;
21314 tree base;
21315 bool pack_expansion_p = false;
21316
21317 /* Look for the base-specifier. */
21318 base = cp_parser_base_specifier (parser);
21319 /* Look for the (optional) ellipsis. */
21320 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21321 {
21322 /* Consume the `...'. */
21323 cp_lexer_consume_token (parser->lexer);
21324
21325 pack_expansion_p = true;
21326 }
21327
21328 /* Add BASE to the front of the list. */
21329 if (base && base != error_mark_node)
21330 {
21331 if (pack_expansion_p)
21332 /* Make this a pack expansion type. */
21333 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21334
21335 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21336 {
21337 TREE_CHAIN (base) = bases;
21338 bases = base;
21339 }
21340 }
21341 /* Peek at the next token. */
21342 token = cp_lexer_peek_token (parser->lexer);
21343 /* If it's not a comma, then the list is complete. */
21344 if (token->type != CPP_COMMA)
21345 break;
21346 /* Consume the `,'. */
21347 cp_lexer_consume_token (parser->lexer);
21348 }
21349
21350 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21351 base class had a qualified name. However, the next name that
21352 appears is certainly not qualified. */
21353 parser->scope = NULL_TREE;
21354 parser->qualifying_scope = NULL_TREE;
21355 parser->object_scope = NULL_TREE;
21356
21357 return nreverse (bases);
21358 }
21359
21360 /* Parse a base-specifier.
21361
21362 base-specifier:
21363 :: [opt] nested-name-specifier [opt] class-name
21364 virtual access-specifier [opt] :: [opt] nested-name-specifier
21365 [opt] class-name
21366 access-specifier virtual [opt] :: [opt] nested-name-specifier
21367 [opt] class-name
21368
21369 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21370 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21371 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21372 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21373
21374 static tree
21375 cp_parser_base_specifier (cp_parser* parser)
21376 {
21377 cp_token *token;
21378 bool done = false;
21379 bool virtual_p = false;
21380 bool duplicate_virtual_error_issued_p = false;
21381 bool duplicate_access_error_issued_p = false;
21382 bool class_scope_p, template_p;
21383 tree access = access_default_node;
21384 tree type;
21385
21386 /* Process the optional `virtual' and `access-specifier'. */
21387 while (!done)
21388 {
21389 /* Peek at the next token. */
21390 token = cp_lexer_peek_token (parser->lexer);
21391 /* Process `virtual'. */
21392 switch (token->keyword)
21393 {
21394 case RID_VIRTUAL:
21395 /* If `virtual' appears more than once, issue an error. */
21396 if (virtual_p && !duplicate_virtual_error_issued_p)
21397 {
21398 cp_parser_error (parser,
21399 "%<virtual%> specified more than once in base-specified");
21400 duplicate_virtual_error_issued_p = true;
21401 }
21402
21403 virtual_p = true;
21404
21405 /* Consume the `virtual' token. */
21406 cp_lexer_consume_token (parser->lexer);
21407
21408 break;
21409
21410 case RID_PUBLIC:
21411 case RID_PROTECTED:
21412 case RID_PRIVATE:
21413 /* If more than one access specifier appears, issue an
21414 error. */
21415 if (access != access_default_node
21416 && !duplicate_access_error_issued_p)
21417 {
21418 cp_parser_error (parser,
21419 "more than one access specifier in base-specified");
21420 duplicate_access_error_issued_p = true;
21421 }
21422
21423 access = ridpointers[(int) token->keyword];
21424
21425 /* Consume the access-specifier. */
21426 cp_lexer_consume_token (parser->lexer);
21427
21428 break;
21429
21430 default:
21431 done = true;
21432 break;
21433 }
21434 }
21435 /* It is not uncommon to see programs mechanically, erroneously, use
21436 the 'typename' keyword to denote (dependent) qualified types
21437 as base classes. */
21438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21439 {
21440 token = cp_lexer_peek_token (parser->lexer);
21441 if (!processing_template_decl)
21442 error_at (token->location,
21443 "keyword %<typename%> not allowed outside of templates");
21444 else
21445 error_at (token->location,
21446 "keyword %<typename%> not allowed in this context "
21447 "(the base class is implicitly a type)");
21448 cp_lexer_consume_token (parser->lexer);
21449 }
21450
21451 /* Look for the optional `::' operator. */
21452 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21453 /* Look for the nested-name-specifier. The simplest way to
21454 implement:
21455
21456 [temp.res]
21457
21458 The keyword `typename' is not permitted in a base-specifier or
21459 mem-initializer; in these contexts a qualified name that
21460 depends on a template-parameter is implicitly assumed to be a
21461 type name.
21462
21463 is to pretend that we have seen the `typename' keyword at this
21464 point. */
21465 cp_parser_nested_name_specifier_opt (parser,
21466 /*typename_keyword_p=*/true,
21467 /*check_dependency_p=*/true,
21468 typename_type,
21469 /*is_declaration=*/true);
21470 /* If the base class is given by a qualified name, assume that names
21471 we see are type names or templates, as appropriate. */
21472 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21473 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21474
21475 if (!parser->scope
21476 && cp_lexer_next_token_is_decltype (parser->lexer))
21477 /* DR 950 allows decltype as a base-specifier. */
21478 type = cp_parser_decltype (parser);
21479 else
21480 {
21481 /* Otherwise, look for the class-name. */
21482 type = cp_parser_class_name (parser,
21483 class_scope_p,
21484 template_p,
21485 typename_type,
21486 /*check_dependency_p=*/true,
21487 /*class_head_p=*/false,
21488 /*is_declaration=*/true);
21489 type = TREE_TYPE (type);
21490 }
21491
21492 if (type == error_mark_node)
21493 return error_mark_node;
21494
21495 return finish_base_specifier (type, access, virtual_p);
21496 }
21497
21498 /* Exception handling [gram.exception] */
21499
21500 /* Parse an (optional) noexcept-specification.
21501
21502 noexcept-specification:
21503 noexcept ( constant-expression ) [opt]
21504
21505 If no noexcept-specification is present, returns NULL_TREE.
21506 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21507 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21508 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21509 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21510 in which case a boolean condition is returned instead. */
21511
21512 static tree
21513 cp_parser_noexcept_specification_opt (cp_parser* parser,
21514 bool require_constexpr,
21515 bool* consumed_expr,
21516 bool return_cond)
21517 {
21518 cp_token *token;
21519 const char *saved_message;
21520
21521 /* Peek at the next token. */
21522 token = cp_lexer_peek_token (parser->lexer);
21523
21524 /* Is it a noexcept-specification? */
21525 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21526 {
21527 tree expr;
21528 cp_lexer_consume_token (parser->lexer);
21529
21530 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21531 {
21532 cp_lexer_consume_token (parser->lexer);
21533
21534 if (require_constexpr)
21535 {
21536 /* Types may not be defined in an exception-specification. */
21537 saved_message = parser->type_definition_forbidden_message;
21538 parser->type_definition_forbidden_message
21539 = G_("types may not be defined in an exception-specification");
21540
21541 expr = cp_parser_constant_expression (parser);
21542
21543 /* Restore the saved message. */
21544 parser->type_definition_forbidden_message = saved_message;
21545 }
21546 else
21547 {
21548 expr = cp_parser_expression (parser);
21549 *consumed_expr = true;
21550 }
21551
21552 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21553 }
21554 else
21555 {
21556 expr = boolean_true_node;
21557 if (!require_constexpr)
21558 *consumed_expr = false;
21559 }
21560
21561 /* We cannot build a noexcept-spec right away because this will check
21562 that expr is a constexpr. */
21563 if (!return_cond)
21564 return build_noexcept_spec (expr, tf_warning_or_error);
21565 else
21566 return expr;
21567 }
21568 else
21569 return NULL_TREE;
21570 }
21571
21572 /* Parse an (optional) exception-specification.
21573
21574 exception-specification:
21575 throw ( type-id-list [opt] )
21576
21577 Returns a TREE_LIST representing the exception-specification. The
21578 TREE_VALUE of each node is a type. */
21579
21580 static tree
21581 cp_parser_exception_specification_opt (cp_parser* parser)
21582 {
21583 cp_token *token;
21584 tree type_id_list;
21585 const char *saved_message;
21586
21587 /* Peek at the next token. */
21588 token = cp_lexer_peek_token (parser->lexer);
21589
21590 /* Is it a noexcept-specification? */
21591 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21592 false);
21593 if (type_id_list != NULL_TREE)
21594 return type_id_list;
21595
21596 /* If it's not `throw', then there's no exception-specification. */
21597 if (!cp_parser_is_keyword (token, RID_THROW))
21598 return NULL_TREE;
21599
21600 #if 0
21601 /* Enable this once a lot of code has transitioned to noexcept? */
21602 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21603 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21604 "deprecated in C++0x; use %<noexcept%> instead");
21605 #endif
21606
21607 /* Consume the `throw'. */
21608 cp_lexer_consume_token (parser->lexer);
21609
21610 /* Look for the `('. */
21611 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21612
21613 /* Peek at the next token. */
21614 token = cp_lexer_peek_token (parser->lexer);
21615 /* If it's not a `)', then there is a type-id-list. */
21616 if (token->type != CPP_CLOSE_PAREN)
21617 {
21618 /* Types may not be defined in an exception-specification. */
21619 saved_message = parser->type_definition_forbidden_message;
21620 parser->type_definition_forbidden_message
21621 = G_("types may not be defined in an exception-specification");
21622 /* Parse the type-id-list. */
21623 type_id_list = cp_parser_type_id_list (parser);
21624 /* Restore the saved message. */
21625 parser->type_definition_forbidden_message = saved_message;
21626 }
21627 else
21628 type_id_list = empty_except_spec;
21629
21630 /* Look for the `)'. */
21631 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21632
21633 return type_id_list;
21634 }
21635
21636 /* Parse an (optional) type-id-list.
21637
21638 type-id-list:
21639 type-id ... [opt]
21640 type-id-list , type-id ... [opt]
21641
21642 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21643 in the order that the types were presented. */
21644
21645 static tree
21646 cp_parser_type_id_list (cp_parser* parser)
21647 {
21648 tree types = NULL_TREE;
21649
21650 while (true)
21651 {
21652 cp_token *token;
21653 tree type;
21654
21655 /* Get the next type-id. */
21656 type = cp_parser_type_id (parser);
21657 /* Parse the optional ellipsis. */
21658 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21659 {
21660 /* Consume the `...'. */
21661 cp_lexer_consume_token (parser->lexer);
21662
21663 /* Turn the type into a pack expansion expression. */
21664 type = make_pack_expansion (type);
21665 }
21666 /* Add it to the list. */
21667 types = add_exception_specifier (types, type, /*complain=*/1);
21668 /* Peek at the next token. */
21669 token = cp_lexer_peek_token (parser->lexer);
21670 /* If it is not a `,', we are done. */
21671 if (token->type != CPP_COMMA)
21672 break;
21673 /* Consume the `,'. */
21674 cp_lexer_consume_token (parser->lexer);
21675 }
21676
21677 return nreverse (types);
21678 }
21679
21680 /* Parse a try-block.
21681
21682 try-block:
21683 try compound-statement handler-seq */
21684
21685 static tree
21686 cp_parser_try_block (cp_parser* parser)
21687 {
21688 tree try_block;
21689
21690 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21691 if (parser->in_function_body
21692 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21693 error ("%<try%> in %<constexpr%> function");
21694
21695 try_block = begin_try_block ();
21696 cp_parser_compound_statement (parser, NULL, true, false);
21697 finish_try_block (try_block);
21698 cp_parser_handler_seq (parser);
21699 finish_handler_sequence (try_block);
21700
21701 return try_block;
21702 }
21703
21704 /* Parse a function-try-block.
21705
21706 function-try-block:
21707 try ctor-initializer [opt] function-body handler-seq */
21708
21709 static bool
21710 cp_parser_function_try_block (cp_parser* parser)
21711 {
21712 tree compound_stmt;
21713 tree try_block;
21714 bool ctor_initializer_p;
21715
21716 /* Look for the `try' keyword. */
21717 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21718 return false;
21719 /* Let the rest of the front end know where we are. */
21720 try_block = begin_function_try_block (&compound_stmt);
21721 /* Parse the function-body. */
21722 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21723 (parser, /*in_function_try_block=*/true);
21724 /* We're done with the `try' part. */
21725 finish_function_try_block (try_block);
21726 /* Parse the handlers. */
21727 cp_parser_handler_seq (parser);
21728 /* We're done with the handlers. */
21729 finish_function_handler_sequence (try_block, compound_stmt);
21730
21731 return ctor_initializer_p;
21732 }
21733
21734 /* Parse a handler-seq.
21735
21736 handler-seq:
21737 handler handler-seq [opt] */
21738
21739 static void
21740 cp_parser_handler_seq (cp_parser* parser)
21741 {
21742 while (true)
21743 {
21744 cp_token *token;
21745
21746 /* Parse the handler. */
21747 cp_parser_handler (parser);
21748 /* Peek at the next token. */
21749 token = cp_lexer_peek_token (parser->lexer);
21750 /* If it's not `catch' then there are no more handlers. */
21751 if (!cp_parser_is_keyword (token, RID_CATCH))
21752 break;
21753 }
21754 }
21755
21756 /* Parse a handler.
21757
21758 handler:
21759 catch ( exception-declaration ) compound-statement */
21760
21761 static void
21762 cp_parser_handler (cp_parser* parser)
21763 {
21764 tree handler;
21765 tree declaration;
21766
21767 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21768 handler = begin_handler ();
21769 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21770 declaration = cp_parser_exception_declaration (parser);
21771 finish_handler_parms (declaration, handler);
21772 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21773 cp_parser_compound_statement (parser, NULL, false, false);
21774 finish_handler (handler);
21775 }
21776
21777 /* Parse an exception-declaration.
21778
21779 exception-declaration:
21780 type-specifier-seq declarator
21781 type-specifier-seq abstract-declarator
21782 type-specifier-seq
21783 ...
21784
21785 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21786 ellipsis variant is used. */
21787
21788 static tree
21789 cp_parser_exception_declaration (cp_parser* parser)
21790 {
21791 cp_decl_specifier_seq type_specifiers;
21792 cp_declarator *declarator;
21793 const char *saved_message;
21794
21795 /* If it's an ellipsis, it's easy to handle. */
21796 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21797 {
21798 /* Consume the `...' token. */
21799 cp_lexer_consume_token (parser->lexer);
21800 return NULL_TREE;
21801 }
21802
21803 /* Types may not be defined in exception-declarations. */
21804 saved_message = parser->type_definition_forbidden_message;
21805 parser->type_definition_forbidden_message
21806 = G_("types may not be defined in exception-declarations");
21807
21808 /* Parse the type-specifier-seq. */
21809 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21810 /*is_trailing_return=*/false,
21811 &type_specifiers);
21812 /* If it's a `)', then there is no declarator. */
21813 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21814 declarator = NULL;
21815 else
21816 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21817 /*ctor_dtor_or_conv_p=*/NULL,
21818 /*parenthesized_p=*/NULL,
21819 /*member_p=*/false,
21820 /*friend_p=*/false);
21821
21822 /* Restore the saved message. */
21823 parser->type_definition_forbidden_message = saved_message;
21824
21825 if (!type_specifiers.any_specifiers_p)
21826 return error_mark_node;
21827
21828 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21829 }
21830
21831 /* Parse a throw-expression.
21832
21833 throw-expression:
21834 throw assignment-expression [opt]
21835
21836 Returns a THROW_EXPR representing the throw-expression. */
21837
21838 static tree
21839 cp_parser_throw_expression (cp_parser* parser)
21840 {
21841 tree expression;
21842 cp_token* token;
21843
21844 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21845 token = cp_lexer_peek_token (parser->lexer);
21846 /* Figure out whether or not there is an assignment-expression
21847 following the "throw" keyword. */
21848 if (token->type == CPP_COMMA
21849 || token->type == CPP_SEMICOLON
21850 || token->type == CPP_CLOSE_PAREN
21851 || token->type == CPP_CLOSE_SQUARE
21852 || token->type == CPP_CLOSE_BRACE
21853 || token->type == CPP_COLON)
21854 expression = NULL_TREE;
21855 else
21856 expression = cp_parser_assignment_expression (parser);
21857
21858 return build_throw (expression);
21859 }
21860
21861 /* GNU Extensions */
21862
21863 /* Parse an (optional) asm-specification.
21864
21865 asm-specification:
21866 asm ( string-literal )
21867
21868 If the asm-specification is present, returns a STRING_CST
21869 corresponding to the string-literal. Otherwise, returns
21870 NULL_TREE. */
21871
21872 static tree
21873 cp_parser_asm_specification_opt (cp_parser* parser)
21874 {
21875 cp_token *token;
21876 tree asm_specification;
21877
21878 /* Peek at the next token. */
21879 token = cp_lexer_peek_token (parser->lexer);
21880 /* If the next token isn't the `asm' keyword, then there's no
21881 asm-specification. */
21882 if (!cp_parser_is_keyword (token, RID_ASM))
21883 return NULL_TREE;
21884
21885 /* Consume the `asm' token. */
21886 cp_lexer_consume_token (parser->lexer);
21887 /* Look for the `('. */
21888 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21889
21890 /* Look for the string-literal. */
21891 asm_specification = cp_parser_string_literal (parser, false, false);
21892
21893 /* Look for the `)'. */
21894 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21895
21896 return asm_specification;
21897 }
21898
21899 /* Parse an asm-operand-list.
21900
21901 asm-operand-list:
21902 asm-operand
21903 asm-operand-list , asm-operand
21904
21905 asm-operand:
21906 string-literal ( expression )
21907 [ string-literal ] string-literal ( expression )
21908
21909 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21910 each node is the expression. The TREE_PURPOSE is itself a
21911 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21912 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21913 is a STRING_CST for the string literal before the parenthesis. Returns
21914 ERROR_MARK_NODE if any of the operands are invalid. */
21915
21916 static tree
21917 cp_parser_asm_operand_list (cp_parser* parser)
21918 {
21919 tree asm_operands = NULL_TREE;
21920 bool invalid_operands = false;
21921
21922 while (true)
21923 {
21924 tree string_literal;
21925 tree expression;
21926 tree name;
21927
21928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21929 {
21930 /* Consume the `[' token. */
21931 cp_lexer_consume_token (parser->lexer);
21932 /* Read the operand name. */
21933 name = cp_parser_identifier (parser);
21934 if (name != error_mark_node)
21935 name = build_string (IDENTIFIER_LENGTH (name),
21936 IDENTIFIER_POINTER (name));
21937 /* Look for the closing `]'. */
21938 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21939 }
21940 else
21941 name = NULL_TREE;
21942 /* Look for the string-literal. */
21943 string_literal = cp_parser_string_literal (parser, false, false);
21944
21945 /* Look for the `('. */
21946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21947 /* Parse the expression. */
21948 expression = cp_parser_expression (parser);
21949 /* Look for the `)'. */
21950 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21951
21952 if (name == error_mark_node
21953 || string_literal == error_mark_node
21954 || expression == error_mark_node)
21955 invalid_operands = true;
21956
21957 /* Add this operand to the list. */
21958 asm_operands = tree_cons (build_tree_list (name, string_literal),
21959 expression,
21960 asm_operands);
21961 /* If the next token is not a `,', there are no more
21962 operands. */
21963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21964 break;
21965 /* Consume the `,'. */
21966 cp_lexer_consume_token (parser->lexer);
21967 }
21968
21969 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21970 }
21971
21972 /* Parse an asm-clobber-list.
21973
21974 asm-clobber-list:
21975 string-literal
21976 asm-clobber-list , string-literal
21977
21978 Returns a TREE_LIST, indicating the clobbers in the order that they
21979 appeared. The TREE_VALUE of each node is a STRING_CST. */
21980
21981 static tree
21982 cp_parser_asm_clobber_list (cp_parser* parser)
21983 {
21984 tree clobbers = NULL_TREE;
21985
21986 while (true)
21987 {
21988 tree string_literal;
21989
21990 /* Look for the string literal. */
21991 string_literal = cp_parser_string_literal (parser, false, false);
21992 /* Add it to the list. */
21993 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21994 /* If the next token is not a `,', then the list is
21995 complete. */
21996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21997 break;
21998 /* Consume the `,' token. */
21999 cp_lexer_consume_token (parser->lexer);
22000 }
22001
22002 return clobbers;
22003 }
22004
22005 /* Parse an asm-label-list.
22006
22007 asm-label-list:
22008 identifier
22009 asm-label-list , identifier
22010
22011 Returns a TREE_LIST, indicating the labels in the order that they
22012 appeared. The TREE_VALUE of each node is a label. */
22013
22014 static tree
22015 cp_parser_asm_label_list (cp_parser* parser)
22016 {
22017 tree labels = NULL_TREE;
22018
22019 while (true)
22020 {
22021 tree identifier, label, name;
22022
22023 /* Look for the identifier. */
22024 identifier = cp_parser_identifier (parser);
22025 if (!error_operand_p (identifier))
22026 {
22027 label = lookup_label (identifier);
22028 if (TREE_CODE (label) == LABEL_DECL)
22029 {
22030 TREE_USED (label) = 1;
22031 check_goto (label);
22032 name = build_string (IDENTIFIER_LENGTH (identifier),
22033 IDENTIFIER_POINTER (identifier));
22034 labels = tree_cons (name, label, labels);
22035 }
22036 }
22037 /* If the next token is not a `,', then the list is
22038 complete. */
22039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22040 break;
22041 /* Consume the `,' token. */
22042 cp_lexer_consume_token (parser->lexer);
22043 }
22044
22045 return nreverse (labels);
22046 }
22047
22048 /* Return TRUE iff the next tokens in the stream are possibly the
22049 beginning of a GNU extension attribute. */
22050
22051 static bool
22052 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22053 {
22054 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22055 }
22056
22057 /* Return TRUE iff the next tokens in the stream are possibly the
22058 beginning of a standard C++-11 attribute specifier. */
22059
22060 static bool
22061 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22062 {
22063 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22064 }
22065
22066 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22067 beginning of a standard C++-11 attribute specifier. */
22068
22069 static bool
22070 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22071 {
22072 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22073
22074 return (cxx_dialect >= cxx11
22075 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22076 || (token->type == CPP_OPEN_SQUARE
22077 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22078 && token->type == CPP_OPEN_SQUARE)));
22079 }
22080
22081 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22082 beginning of a GNU extension attribute. */
22083
22084 static bool
22085 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22086 {
22087 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22088
22089 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22090 }
22091
22092 /* Return true iff the next tokens can be the beginning of either a
22093 GNU attribute list, or a standard C++11 attribute sequence. */
22094
22095 static bool
22096 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22097 {
22098 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22099 || cp_next_tokens_can_be_std_attribute_p (parser));
22100 }
22101
22102 /* Return true iff the next Nth tokens can be the beginning of either
22103 a GNU attribute list, or a standard C++11 attribute sequence. */
22104
22105 static bool
22106 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22107 {
22108 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22109 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22110 }
22111
22112 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22113 of GNU attributes, or return NULL. */
22114
22115 static tree
22116 cp_parser_attributes_opt (cp_parser *parser)
22117 {
22118 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22119 return cp_parser_gnu_attributes_opt (parser);
22120 return cp_parser_std_attribute_spec_seq (parser);
22121 }
22122
22123 #define CILK_SIMD_FN_CLAUSE_MASK \
22124 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22125 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22126 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22127 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22128 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22129
22130 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22131 vector [(<clauses>)] */
22132
22133 static void
22134 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22135 {
22136 bool first_p = parser->cilk_simd_fn_info == NULL;
22137 cp_token *token = v_token;
22138 if (first_p)
22139 {
22140 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22141 parser->cilk_simd_fn_info->error_seen = false;
22142 parser->cilk_simd_fn_info->fndecl_seen = false;
22143 parser->cilk_simd_fn_info->tokens = vNULL;
22144 }
22145 int paren_scope = 0;
22146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22147 {
22148 cp_lexer_consume_token (parser->lexer);
22149 v_token = cp_lexer_peek_token (parser->lexer);
22150 paren_scope++;
22151 }
22152 while (paren_scope > 0)
22153 {
22154 token = cp_lexer_peek_token (parser->lexer);
22155 if (token->type == CPP_OPEN_PAREN)
22156 paren_scope++;
22157 else if (token->type == CPP_CLOSE_PAREN)
22158 paren_scope--;
22159 /* Do not push the last ')' */
22160 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22161 cp_lexer_consume_token (parser->lexer);
22162 }
22163
22164 token->type = CPP_PRAGMA_EOL;
22165 parser->lexer->next_token = token;
22166 cp_lexer_consume_token (parser->lexer);
22167
22168 struct cp_token_cache *cp
22169 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22170 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22171 }
22172
22173 /* Parse an (optional) series of attributes.
22174
22175 attributes:
22176 attributes attribute
22177
22178 attribute:
22179 __attribute__ (( attribute-list [opt] ))
22180
22181 The return value is as for cp_parser_gnu_attribute_list. */
22182
22183 static tree
22184 cp_parser_gnu_attributes_opt (cp_parser* parser)
22185 {
22186 tree attributes = NULL_TREE;
22187
22188 while (true)
22189 {
22190 cp_token *token;
22191 tree attribute_list;
22192 bool ok = true;
22193
22194 /* Peek at the next token. */
22195 token = cp_lexer_peek_token (parser->lexer);
22196 /* If it's not `__attribute__', then we're done. */
22197 if (token->keyword != RID_ATTRIBUTE)
22198 break;
22199
22200 /* Consume the `__attribute__' keyword. */
22201 cp_lexer_consume_token (parser->lexer);
22202 /* Look for the two `(' tokens. */
22203 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22204 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22205
22206 /* Peek at the next token. */
22207 token = cp_lexer_peek_token (parser->lexer);
22208 if (token->type != CPP_CLOSE_PAREN)
22209 /* Parse the attribute-list. */
22210 attribute_list = cp_parser_gnu_attribute_list (parser);
22211 else
22212 /* If the next token is a `)', then there is no attribute
22213 list. */
22214 attribute_list = NULL;
22215
22216 /* Look for the two `)' tokens. */
22217 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22218 ok = false;
22219 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22220 ok = false;
22221 if (!ok)
22222 cp_parser_skip_to_end_of_statement (parser);
22223
22224 /* Add these new attributes to the list. */
22225 attributes = chainon (attributes, attribute_list);
22226 }
22227
22228 return attributes;
22229 }
22230
22231 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22232 "__vector" or "__vector__." */
22233
22234 static inline bool
22235 is_cilkplus_vector_p (tree name)
22236 {
22237 if (flag_cilkplus && is_attribute_p ("vector", name))
22238 return true;
22239 return false;
22240 }
22241
22242 /* Parse a GNU attribute-list.
22243
22244 attribute-list:
22245 attribute
22246 attribute-list , attribute
22247
22248 attribute:
22249 identifier
22250 identifier ( identifier )
22251 identifier ( identifier , expression-list )
22252 identifier ( expression-list )
22253
22254 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22255 to an attribute. The TREE_PURPOSE of each node is the identifier
22256 indicating which attribute is in use. The TREE_VALUE represents
22257 the arguments, if any. */
22258
22259 static tree
22260 cp_parser_gnu_attribute_list (cp_parser* parser)
22261 {
22262 tree attribute_list = NULL_TREE;
22263 bool save_translate_strings_p = parser->translate_strings_p;
22264
22265 parser->translate_strings_p = false;
22266 while (true)
22267 {
22268 cp_token *token;
22269 tree identifier;
22270 tree attribute;
22271
22272 /* Look for the identifier. We also allow keywords here; for
22273 example `__attribute__ ((const))' is legal. */
22274 token = cp_lexer_peek_token (parser->lexer);
22275 if (token->type == CPP_NAME
22276 || token->type == CPP_KEYWORD)
22277 {
22278 tree arguments = NULL_TREE;
22279
22280 /* Consume the token, but save it since we need it for the
22281 SIMD enabled function parsing. */
22282 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22283
22284 /* Save away the identifier that indicates which attribute
22285 this is. */
22286 identifier = (token->type == CPP_KEYWORD)
22287 /* For keywords, use the canonical spelling, not the
22288 parsed identifier. */
22289 ? ridpointers[(int) token->keyword]
22290 : id_token->u.value;
22291
22292 attribute = build_tree_list (identifier, NULL_TREE);
22293
22294 /* Peek at the next token. */
22295 token = cp_lexer_peek_token (parser->lexer);
22296 /* If it's an `(', then parse the attribute arguments. */
22297 if (token->type == CPP_OPEN_PAREN)
22298 {
22299 vec<tree, va_gc> *vec;
22300 int attr_flag = (attribute_takes_identifier_p (identifier)
22301 ? id_attr : normal_attr);
22302 if (is_cilkplus_vector_p (identifier))
22303 {
22304 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22305 continue;
22306 }
22307 else
22308 vec = cp_parser_parenthesized_expression_list
22309 (parser, attr_flag, /*cast_p=*/false,
22310 /*allow_expansion_p=*/false,
22311 /*non_constant_p=*/NULL);
22312 if (vec == NULL)
22313 arguments = error_mark_node;
22314 else
22315 {
22316 arguments = build_tree_list_vec (vec);
22317 release_tree_vector (vec);
22318 }
22319 /* Save the arguments away. */
22320 TREE_VALUE (attribute) = arguments;
22321 }
22322 else if (is_cilkplus_vector_p (identifier))
22323 {
22324 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22325 continue;
22326 }
22327
22328 if (arguments != error_mark_node)
22329 {
22330 /* Add this attribute to the list. */
22331 TREE_CHAIN (attribute) = attribute_list;
22332 attribute_list = attribute;
22333 }
22334
22335 token = cp_lexer_peek_token (parser->lexer);
22336 }
22337 /* Now, look for more attributes. If the next token isn't a
22338 `,', we're done. */
22339 if (token->type != CPP_COMMA)
22340 break;
22341
22342 /* Consume the comma and keep going. */
22343 cp_lexer_consume_token (parser->lexer);
22344 }
22345 parser->translate_strings_p = save_translate_strings_p;
22346
22347 /* We built up the list in reverse order. */
22348 return nreverse (attribute_list);
22349 }
22350
22351 /* Parse a standard C++11 attribute.
22352
22353 The returned representation is a TREE_LIST which TREE_PURPOSE is
22354 the scoped name of the attribute, and the TREE_VALUE is its
22355 arguments list.
22356
22357 Note that the scoped name of the attribute is itself a TREE_LIST
22358 which TREE_PURPOSE is the namespace of the attribute, and
22359 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22360 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22361 and which TREE_PURPOSE is directly the attribute name.
22362
22363 Clients of the attribute code should use get_attribute_namespace
22364 and get_attribute_name to get the actual namespace and name of
22365 attributes, regardless of their being GNU or C++11 attributes.
22366
22367 attribute:
22368 attribute-token attribute-argument-clause [opt]
22369
22370 attribute-token:
22371 identifier
22372 attribute-scoped-token
22373
22374 attribute-scoped-token:
22375 attribute-namespace :: identifier
22376
22377 attribute-namespace:
22378 identifier
22379
22380 attribute-argument-clause:
22381 ( balanced-token-seq )
22382
22383 balanced-token-seq:
22384 balanced-token [opt]
22385 balanced-token-seq balanced-token
22386
22387 balanced-token:
22388 ( balanced-token-seq )
22389 [ balanced-token-seq ]
22390 { balanced-token-seq }. */
22391
22392 static tree
22393 cp_parser_std_attribute (cp_parser *parser)
22394 {
22395 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22396 cp_token *token;
22397
22398 /* First, parse name of the the attribute, a.k.a
22399 attribute-token. */
22400
22401 token = cp_lexer_peek_token (parser->lexer);
22402 if (token->type == CPP_NAME)
22403 attr_id = token->u.value;
22404 else if (token->type == CPP_KEYWORD)
22405 attr_id = ridpointers[(int) token->keyword];
22406 else if (token->flags & NAMED_OP)
22407 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22408
22409 if (attr_id == NULL_TREE)
22410 return NULL_TREE;
22411
22412 cp_lexer_consume_token (parser->lexer);
22413
22414 token = cp_lexer_peek_token (parser->lexer);
22415 if (token->type == CPP_SCOPE)
22416 {
22417 /* We are seeing a scoped attribute token. */
22418
22419 cp_lexer_consume_token (parser->lexer);
22420 attr_ns = attr_id;
22421
22422 token = cp_lexer_consume_token (parser->lexer);
22423 if (token->type == CPP_NAME)
22424 attr_id = token->u.value;
22425 else if (token->type == CPP_KEYWORD)
22426 attr_id = ridpointers[(int) token->keyword];
22427 else
22428 {
22429 error_at (token->location,
22430 "expected an identifier for the attribute name");
22431 return error_mark_node;
22432 }
22433 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22434 NULL_TREE);
22435 token = cp_lexer_peek_token (parser->lexer);
22436 }
22437 else
22438 {
22439 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22440 NULL_TREE);
22441 /* C++11 noreturn attribute is equivalent to GNU's. */
22442 if (is_attribute_p ("noreturn", attr_id))
22443 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22444 /* C++14 deprecated attribute is equivalent to GNU's. */
22445 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22446 {
22447 if (cxx_dialect == cxx11)
22448 pedwarn (token->location, OPT_Wpedantic,
22449 "%<deprecated%> is a C++14 feature;"
22450 " use %<gnu::deprecated%>");
22451 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22452 }
22453 }
22454
22455 /* Now parse the optional argument clause of the attribute. */
22456
22457 if (token->type != CPP_OPEN_PAREN)
22458 return attribute;
22459
22460 {
22461 vec<tree, va_gc> *vec;
22462 int attr_flag = normal_attr;
22463
22464 if (attr_ns == get_identifier ("gnu")
22465 && attribute_takes_identifier_p (attr_id))
22466 /* A GNU attribute that takes an identifier in parameter. */
22467 attr_flag = id_attr;
22468
22469 vec = cp_parser_parenthesized_expression_list
22470 (parser, attr_flag, /*cast_p=*/false,
22471 /*allow_expansion_p=*/true,
22472 /*non_constant_p=*/NULL);
22473 if (vec == NULL)
22474 arguments = error_mark_node;
22475 else
22476 {
22477 arguments = build_tree_list_vec (vec);
22478 release_tree_vector (vec);
22479 }
22480
22481 if (arguments == error_mark_node)
22482 attribute = error_mark_node;
22483 else
22484 TREE_VALUE (attribute) = arguments;
22485 }
22486
22487 return attribute;
22488 }
22489
22490 /* Parse a list of standard C++-11 attributes.
22491
22492 attribute-list:
22493 attribute [opt]
22494 attribute-list , attribute[opt]
22495 attribute ...
22496 attribute-list , attribute ...
22497 */
22498
22499 static tree
22500 cp_parser_std_attribute_list (cp_parser *parser)
22501 {
22502 tree attributes = NULL_TREE, attribute = NULL_TREE;
22503 cp_token *token = NULL;
22504
22505 while (true)
22506 {
22507 attribute = cp_parser_std_attribute (parser);
22508 if (attribute == error_mark_node)
22509 break;
22510 if (attribute != NULL_TREE)
22511 {
22512 TREE_CHAIN (attribute) = attributes;
22513 attributes = attribute;
22514 }
22515 token = cp_lexer_peek_token (parser->lexer);
22516 if (token->type == CPP_ELLIPSIS)
22517 {
22518 cp_lexer_consume_token (parser->lexer);
22519 TREE_VALUE (attribute)
22520 = make_pack_expansion (TREE_VALUE (attribute));
22521 token = cp_lexer_peek_token (parser->lexer);
22522 }
22523 if (token->type != CPP_COMMA)
22524 break;
22525 cp_lexer_consume_token (parser->lexer);
22526 }
22527 attributes = nreverse (attributes);
22528 return attributes;
22529 }
22530
22531 /* Parse a standard C++-11 attribute specifier.
22532
22533 attribute-specifier:
22534 [ [ attribute-list ] ]
22535 alignment-specifier
22536
22537 alignment-specifier:
22538 alignas ( type-id ... [opt] )
22539 alignas ( alignment-expression ... [opt] ). */
22540
22541 static tree
22542 cp_parser_std_attribute_spec (cp_parser *parser)
22543 {
22544 tree attributes = NULL_TREE;
22545 cp_token *token = cp_lexer_peek_token (parser->lexer);
22546
22547 if (token->type == CPP_OPEN_SQUARE
22548 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22549 {
22550 cp_lexer_consume_token (parser->lexer);
22551 cp_lexer_consume_token (parser->lexer);
22552
22553 attributes = cp_parser_std_attribute_list (parser);
22554
22555 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22556 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22557 cp_parser_skip_to_end_of_statement (parser);
22558 else
22559 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22560 when we are sure that we have actually parsed them. */
22561 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22562 }
22563 else
22564 {
22565 tree alignas_expr;
22566
22567 /* Look for an alignment-specifier. */
22568
22569 token = cp_lexer_peek_token (parser->lexer);
22570
22571 if (token->type != CPP_KEYWORD
22572 || token->keyword != RID_ALIGNAS)
22573 return NULL_TREE;
22574
22575 cp_lexer_consume_token (parser->lexer);
22576 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22577
22578 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22579 {
22580 cp_parser_error (parser, "expected %<(%>");
22581 return error_mark_node;
22582 }
22583
22584 cp_parser_parse_tentatively (parser);
22585 alignas_expr = cp_parser_type_id (parser);
22586
22587 if (!cp_parser_parse_definitely (parser))
22588 {
22589 gcc_assert (alignas_expr == error_mark_node
22590 || alignas_expr == NULL_TREE);
22591
22592 alignas_expr =
22593 cp_parser_assignment_expression (parser);
22594 if (alignas_expr == error_mark_node)
22595 cp_parser_skip_to_end_of_statement (parser);
22596 if (alignas_expr == NULL_TREE
22597 || alignas_expr == error_mark_node)
22598 return alignas_expr;
22599 }
22600
22601 alignas_expr = cxx_alignas_expr (alignas_expr);
22602 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22603
22604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22605 {
22606 cp_lexer_consume_token (parser->lexer);
22607 alignas_expr = make_pack_expansion (alignas_expr);
22608 }
22609
22610 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22611 {
22612 cp_parser_error (parser, "expected %<)%>");
22613 return error_mark_node;
22614 }
22615
22616 /* Build the C++-11 representation of an 'aligned'
22617 attribute. */
22618 attributes =
22619 build_tree_list (build_tree_list (get_identifier ("gnu"),
22620 get_identifier ("aligned")),
22621 alignas_expr);
22622 }
22623
22624 return attributes;
22625 }
22626
22627 /* Parse a standard C++-11 attribute-specifier-seq.
22628
22629 attribute-specifier-seq:
22630 attribute-specifier-seq [opt] attribute-specifier
22631 */
22632
22633 static tree
22634 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22635 {
22636 tree attr_specs = NULL;
22637
22638 while (true)
22639 {
22640 tree attr_spec = cp_parser_std_attribute_spec (parser);
22641 if (attr_spec == NULL_TREE)
22642 break;
22643 if (attr_spec == error_mark_node)
22644 return error_mark_node;
22645
22646 TREE_CHAIN (attr_spec) = attr_specs;
22647 attr_specs = attr_spec;
22648 }
22649
22650 attr_specs = nreverse (attr_specs);
22651 return attr_specs;
22652 }
22653
22654 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22655 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22656 current value of the PEDANTIC flag, regardless of whether or not
22657 the `__extension__' keyword is present. The caller is responsible
22658 for restoring the value of the PEDANTIC flag. */
22659
22660 static bool
22661 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22662 {
22663 /* Save the old value of the PEDANTIC flag. */
22664 *saved_pedantic = pedantic;
22665
22666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22667 {
22668 /* Consume the `__extension__' token. */
22669 cp_lexer_consume_token (parser->lexer);
22670 /* We're not being pedantic while the `__extension__' keyword is
22671 in effect. */
22672 pedantic = 0;
22673
22674 return true;
22675 }
22676
22677 return false;
22678 }
22679
22680 /* Parse a label declaration.
22681
22682 label-declaration:
22683 __label__ label-declarator-seq ;
22684
22685 label-declarator-seq:
22686 identifier , label-declarator-seq
22687 identifier */
22688
22689 static void
22690 cp_parser_label_declaration (cp_parser* parser)
22691 {
22692 /* Look for the `__label__' keyword. */
22693 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22694
22695 while (true)
22696 {
22697 tree identifier;
22698
22699 /* Look for an identifier. */
22700 identifier = cp_parser_identifier (parser);
22701 /* If we failed, stop. */
22702 if (identifier == error_mark_node)
22703 break;
22704 /* Declare it as a label. */
22705 finish_label_decl (identifier);
22706 /* If the next token is a `;', stop. */
22707 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22708 break;
22709 /* Look for the `,' separating the label declarations. */
22710 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22711 }
22712
22713 /* Look for the final `;'. */
22714 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22715 }
22716
22717 /* Support Functions */
22718
22719 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22720 NAME should have one of the representations used for an
22721 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22722 is returned. If PARSER->SCOPE is a dependent type, then a
22723 SCOPE_REF is returned.
22724
22725 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22726 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22727 was formed. Abstractly, such entities should not be passed to this
22728 function, because they do not need to be looked up, but it is
22729 simpler to check for this special case here, rather than at the
22730 call-sites.
22731
22732 In cases not explicitly covered above, this function returns a
22733 DECL, OVERLOAD, or baselink representing the result of the lookup.
22734 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22735 is returned.
22736
22737 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22738 (e.g., "struct") that was used. In that case bindings that do not
22739 refer to types are ignored.
22740
22741 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22742 ignored.
22743
22744 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22745 are ignored.
22746
22747 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22748 types.
22749
22750 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22751 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22752 NULL_TREE otherwise. */
22753
22754 static tree
22755 cp_parser_lookup_name (cp_parser *parser, tree name,
22756 enum tag_types tag_type,
22757 bool is_template,
22758 bool is_namespace,
22759 bool check_dependency,
22760 tree *ambiguous_decls,
22761 location_t name_location)
22762 {
22763 tree decl;
22764 tree object_type = parser->context->object_type;
22765
22766 /* Assume that the lookup will be unambiguous. */
22767 if (ambiguous_decls)
22768 *ambiguous_decls = NULL_TREE;
22769
22770 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22771 no longer valid. Note that if we are parsing tentatively, and
22772 the parse fails, OBJECT_TYPE will be automatically restored. */
22773 parser->context->object_type = NULL_TREE;
22774
22775 if (name == error_mark_node)
22776 return error_mark_node;
22777
22778 /* A template-id has already been resolved; there is no lookup to
22779 do. */
22780 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22781 return name;
22782 if (BASELINK_P (name))
22783 {
22784 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22785 == TEMPLATE_ID_EXPR);
22786 return name;
22787 }
22788
22789 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22790 it should already have been checked to make sure that the name
22791 used matches the type being destroyed. */
22792 if (TREE_CODE (name) == BIT_NOT_EXPR)
22793 {
22794 tree type;
22795
22796 /* Figure out to which type this destructor applies. */
22797 if (parser->scope)
22798 type = parser->scope;
22799 else if (object_type)
22800 type = object_type;
22801 else
22802 type = current_class_type;
22803 /* If that's not a class type, there is no destructor. */
22804 if (!type || !CLASS_TYPE_P (type))
22805 return error_mark_node;
22806 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22807 lazily_declare_fn (sfk_destructor, type);
22808 if (!CLASSTYPE_DESTRUCTORS (type))
22809 return error_mark_node;
22810 /* If it was a class type, return the destructor. */
22811 return CLASSTYPE_DESTRUCTORS (type);
22812 }
22813
22814 /* By this point, the NAME should be an ordinary identifier. If
22815 the id-expression was a qualified name, the qualifying scope is
22816 stored in PARSER->SCOPE at this point. */
22817 gcc_assert (identifier_p (name));
22818
22819 /* Perform the lookup. */
22820 if (parser->scope)
22821 {
22822 bool dependent_p;
22823
22824 if (parser->scope == error_mark_node)
22825 return error_mark_node;
22826
22827 /* If the SCOPE is dependent, the lookup must be deferred until
22828 the template is instantiated -- unless we are explicitly
22829 looking up names in uninstantiated templates. Even then, we
22830 cannot look up the name if the scope is not a class type; it
22831 might, for example, be a template type parameter. */
22832 dependent_p = (TYPE_P (parser->scope)
22833 && dependent_scope_p (parser->scope));
22834 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22835 && dependent_p)
22836 /* Defer lookup. */
22837 decl = error_mark_node;
22838 else
22839 {
22840 tree pushed_scope = NULL_TREE;
22841
22842 /* If PARSER->SCOPE is a dependent type, then it must be a
22843 class type, and we must not be checking dependencies;
22844 otherwise, we would have processed this lookup above. So
22845 that PARSER->SCOPE is not considered a dependent base by
22846 lookup_member, we must enter the scope here. */
22847 if (dependent_p)
22848 pushed_scope = push_scope (parser->scope);
22849
22850 /* If the PARSER->SCOPE is a template specialization, it
22851 may be instantiated during name lookup. In that case,
22852 errors may be issued. Even if we rollback the current
22853 tentative parse, those errors are valid. */
22854 decl = lookup_qualified_name (parser->scope, name,
22855 tag_type != none_type,
22856 /*complain=*/true);
22857
22858 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22859 lookup result and the nested-name-specifier nominates a class C:
22860 * if the name specified after the nested-name-specifier, when
22861 looked up in C, is the injected-class-name of C (Clause 9), or
22862 * if the name specified after the nested-name-specifier is the
22863 same as the identifier or the simple-template-id's template-
22864 name in the last component of the nested-name-specifier,
22865 the name is instead considered to name the constructor of
22866 class C. [ Note: for example, the constructor is not an
22867 acceptable lookup result in an elaborated-type-specifier so
22868 the constructor would not be used in place of the
22869 injected-class-name. --end note ] Such a constructor name
22870 shall be used only in the declarator-id of a declaration that
22871 names a constructor or in a using-declaration. */
22872 if (tag_type == none_type
22873 && DECL_SELF_REFERENCE_P (decl)
22874 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22875 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22876 tag_type != none_type,
22877 /*complain=*/true);
22878
22879 /* If we have a single function from a using decl, pull it out. */
22880 if (TREE_CODE (decl) == OVERLOAD
22881 && !really_overloaded_fn (decl))
22882 decl = OVL_FUNCTION (decl);
22883
22884 if (pushed_scope)
22885 pop_scope (pushed_scope);
22886 }
22887
22888 /* If the scope is a dependent type and either we deferred lookup or
22889 we did lookup but didn't find the name, rememeber the name. */
22890 if (decl == error_mark_node && TYPE_P (parser->scope)
22891 && dependent_type_p (parser->scope))
22892 {
22893 if (tag_type)
22894 {
22895 tree type;
22896
22897 /* The resolution to Core Issue 180 says that `struct
22898 A::B' should be considered a type-name, even if `A'
22899 is dependent. */
22900 type = make_typename_type (parser->scope, name, tag_type,
22901 /*complain=*/tf_error);
22902 if (type != error_mark_node)
22903 decl = TYPE_NAME (type);
22904 }
22905 else if (is_template
22906 && (cp_parser_next_token_ends_template_argument_p (parser)
22907 || cp_lexer_next_token_is (parser->lexer,
22908 CPP_CLOSE_PAREN)))
22909 decl = make_unbound_class_template (parser->scope,
22910 name, NULL_TREE,
22911 /*complain=*/tf_error);
22912 else
22913 decl = build_qualified_name (/*type=*/NULL_TREE,
22914 parser->scope, name,
22915 is_template);
22916 }
22917 parser->qualifying_scope = parser->scope;
22918 parser->object_scope = NULL_TREE;
22919 }
22920 else if (object_type)
22921 {
22922 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22923 OBJECT_TYPE is not a class. */
22924 if (CLASS_TYPE_P (object_type))
22925 /* If the OBJECT_TYPE is a template specialization, it may
22926 be instantiated during name lookup. In that case, errors
22927 may be issued. Even if we rollback the current tentative
22928 parse, those errors are valid. */
22929 decl = lookup_member (object_type,
22930 name,
22931 /*protect=*/0,
22932 tag_type != none_type,
22933 tf_warning_or_error);
22934 else
22935 decl = NULL_TREE;
22936
22937 if (!decl)
22938 /* Look it up in the enclosing context. */
22939 decl = lookup_name_real (name, tag_type != none_type,
22940 /*nonclass=*/0,
22941 /*block_p=*/true, is_namespace, 0);
22942 parser->object_scope = object_type;
22943 parser->qualifying_scope = NULL_TREE;
22944 }
22945 else
22946 {
22947 decl = lookup_name_real (name, tag_type != none_type,
22948 /*nonclass=*/0,
22949 /*block_p=*/true, is_namespace, 0);
22950 parser->qualifying_scope = NULL_TREE;
22951 parser->object_scope = NULL_TREE;
22952 }
22953
22954 /* If the lookup failed, let our caller know. */
22955 if (!decl || decl == error_mark_node)
22956 return error_mark_node;
22957
22958 /* Pull out the template from an injected-class-name (or multiple). */
22959 if (is_template)
22960 decl = maybe_get_template_decl_from_type_decl (decl);
22961
22962 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22963 if (TREE_CODE (decl) == TREE_LIST)
22964 {
22965 if (ambiguous_decls)
22966 *ambiguous_decls = decl;
22967 /* The error message we have to print is too complicated for
22968 cp_parser_error, so we incorporate its actions directly. */
22969 if (!cp_parser_simulate_error (parser))
22970 {
22971 error_at (name_location, "reference to %qD is ambiguous",
22972 name);
22973 print_candidates (decl);
22974 }
22975 return error_mark_node;
22976 }
22977
22978 gcc_assert (DECL_P (decl)
22979 || TREE_CODE (decl) == OVERLOAD
22980 || TREE_CODE (decl) == SCOPE_REF
22981 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22982 || BASELINK_P (decl));
22983
22984 /* If we have resolved the name of a member declaration, check to
22985 see if the declaration is accessible. When the name resolves to
22986 set of overloaded functions, accessibility is checked when
22987 overload resolution is done.
22988
22989 During an explicit instantiation, access is not checked at all,
22990 as per [temp.explicit]. */
22991 if (DECL_P (decl))
22992 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22993
22994 maybe_record_typedef_use (decl);
22995
22996 return decl;
22997 }
22998
22999 /* Like cp_parser_lookup_name, but for use in the typical case where
23000 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23001 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23002
23003 static tree
23004 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23005 {
23006 return cp_parser_lookup_name (parser, name,
23007 none_type,
23008 /*is_template=*/false,
23009 /*is_namespace=*/false,
23010 /*check_dependency=*/true,
23011 /*ambiguous_decls=*/NULL,
23012 location);
23013 }
23014
23015 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23016 the current context, return the TYPE_DECL. If TAG_NAME_P is
23017 true, the DECL indicates the class being defined in a class-head,
23018 or declared in an elaborated-type-specifier.
23019
23020 Otherwise, return DECL. */
23021
23022 static tree
23023 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23024 {
23025 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23026 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23027
23028 struct A {
23029 template <typename T> struct B;
23030 };
23031
23032 template <typename T> struct A::B {};
23033
23034 Similarly, in an elaborated-type-specifier:
23035
23036 namespace N { struct X{}; }
23037
23038 struct A {
23039 template <typename T> friend struct N::X;
23040 };
23041
23042 However, if the DECL refers to a class type, and we are in
23043 the scope of the class, then the name lookup automatically
23044 finds the TYPE_DECL created by build_self_reference rather
23045 than a TEMPLATE_DECL. For example, in:
23046
23047 template <class T> struct S {
23048 S s;
23049 };
23050
23051 there is no need to handle such case. */
23052
23053 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23054 return DECL_TEMPLATE_RESULT (decl);
23055
23056 return decl;
23057 }
23058
23059 /* If too many, or too few, template-parameter lists apply to the
23060 declarator, issue an error message. Returns TRUE if all went well,
23061 and FALSE otherwise. */
23062
23063 static bool
23064 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23065 cp_declarator *declarator,
23066 location_t declarator_location)
23067 {
23068 switch (declarator->kind)
23069 {
23070 case cdk_id:
23071 {
23072 unsigned num_templates = 0;
23073 tree scope = declarator->u.id.qualifying_scope;
23074
23075 if (scope)
23076 num_templates = num_template_headers_for_class (scope);
23077 else if (TREE_CODE (declarator->u.id.unqualified_name)
23078 == TEMPLATE_ID_EXPR)
23079 /* If the DECLARATOR has the form `X<y>' then it uses one
23080 additional level of template parameters. */
23081 ++num_templates;
23082
23083 return cp_parser_check_template_parameters
23084 (parser, num_templates, declarator_location, declarator);
23085 }
23086
23087 case cdk_function:
23088 case cdk_array:
23089 case cdk_pointer:
23090 case cdk_reference:
23091 case cdk_ptrmem:
23092 return (cp_parser_check_declarator_template_parameters
23093 (parser, declarator->declarator, declarator_location));
23094
23095 case cdk_error:
23096 return true;
23097
23098 default:
23099 gcc_unreachable ();
23100 }
23101 return false;
23102 }
23103
23104 /* NUM_TEMPLATES were used in the current declaration. If that is
23105 invalid, return FALSE and issue an error messages. Otherwise,
23106 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23107 declarator and we can print more accurate diagnostics. */
23108
23109 static bool
23110 cp_parser_check_template_parameters (cp_parser* parser,
23111 unsigned num_templates,
23112 location_t location,
23113 cp_declarator *declarator)
23114 {
23115 /* If there are the same number of template classes and parameter
23116 lists, that's OK. */
23117 if (parser->num_template_parameter_lists == num_templates)
23118 return true;
23119 /* If there are more, but only one more, then we are referring to a
23120 member template. That's OK too. */
23121 if (parser->num_template_parameter_lists == num_templates + 1)
23122 return true;
23123 /* If there are more template classes than parameter lists, we have
23124 something like:
23125
23126 template <class T> void S<T>::R<T>::f (); */
23127 if (parser->num_template_parameter_lists < num_templates)
23128 {
23129 if (declarator && !current_function_decl)
23130 error_at (location, "specializing member %<%T::%E%> "
23131 "requires %<template<>%> syntax",
23132 declarator->u.id.qualifying_scope,
23133 declarator->u.id.unqualified_name);
23134 else if (declarator)
23135 error_at (location, "invalid declaration of %<%T::%E%>",
23136 declarator->u.id.qualifying_scope,
23137 declarator->u.id.unqualified_name);
23138 else
23139 error_at (location, "too few template-parameter-lists");
23140 return false;
23141 }
23142 /* Otherwise, there are too many template parameter lists. We have
23143 something like:
23144
23145 template <class T> template <class U> void S::f(); */
23146 error_at (location, "too many template-parameter-lists");
23147 return false;
23148 }
23149
23150 /* Parse an optional `::' token indicating that the following name is
23151 from the global namespace. If so, PARSER->SCOPE is set to the
23152 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23153 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23154 Returns the new value of PARSER->SCOPE, if the `::' token is
23155 present, and NULL_TREE otherwise. */
23156
23157 static tree
23158 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23159 {
23160 cp_token *token;
23161
23162 /* Peek at the next token. */
23163 token = cp_lexer_peek_token (parser->lexer);
23164 /* If we're looking at a `::' token then we're starting from the
23165 global namespace, not our current location. */
23166 if (token->type == CPP_SCOPE)
23167 {
23168 /* Consume the `::' token. */
23169 cp_lexer_consume_token (parser->lexer);
23170 /* Set the SCOPE so that we know where to start the lookup. */
23171 parser->scope = global_namespace;
23172 parser->qualifying_scope = global_namespace;
23173 parser->object_scope = NULL_TREE;
23174
23175 return parser->scope;
23176 }
23177 else if (!current_scope_valid_p)
23178 {
23179 parser->scope = NULL_TREE;
23180 parser->qualifying_scope = NULL_TREE;
23181 parser->object_scope = NULL_TREE;
23182 }
23183
23184 return NULL_TREE;
23185 }
23186
23187 /* Returns TRUE if the upcoming token sequence is the start of a
23188 constructor declarator. If FRIEND_P is true, the declarator is
23189 preceded by the `friend' specifier. */
23190
23191 static bool
23192 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23193 {
23194 bool constructor_p;
23195 bool outside_class_specifier_p;
23196 tree nested_name_specifier;
23197 cp_token *next_token;
23198
23199 /* The common case is that this is not a constructor declarator, so
23200 try to avoid doing lots of work if at all possible. It's not
23201 valid declare a constructor at function scope. */
23202 if (parser->in_function_body)
23203 return false;
23204 /* And only certain tokens can begin a constructor declarator. */
23205 next_token = cp_lexer_peek_token (parser->lexer);
23206 if (next_token->type != CPP_NAME
23207 && next_token->type != CPP_SCOPE
23208 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23209 && next_token->type != CPP_TEMPLATE_ID)
23210 return false;
23211
23212 /* Parse tentatively; we are going to roll back all of the tokens
23213 consumed here. */
23214 cp_parser_parse_tentatively (parser);
23215 /* Assume that we are looking at a constructor declarator. */
23216 constructor_p = true;
23217
23218 /* Look for the optional `::' operator. */
23219 cp_parser_global_scope_opt (parser,
23220 /*current_scope_valid_p=*/false);
23221 /* Look for the nested-name-specifier. */
23222 nested_name_specifier
23223 = (cp_parser_nested_name_specifier_opt (parser,
23224 /*typename_keyword_p=*/false,
23225 /*check_dependency_p=*/false,
23226 /*type_p=*/false,
23227 /*is_declaration=*/false));
23228
23229 outside_class_specifier_p = (!at_class_scope_p ()
23230 || !TYPE_BEING_DEFINED (current_class_type)
23231 || friend_p);
23232
23233 /* Outside of a class-specifier, there must be a
23234 nested-name-specifier. */
23235 if (!nested_name_specifier && outside_class_specifier_p)
23236 constructor_p = false;
23237 else if (nested_name_specifier == error_mark_node)
23238 constructor_p = false;
23239
23240 /* If we have a class scope, this is easy; DR 147 says that S::S always
23241 names the constructor, and no other qualified name could. */
23242 if (constructor_p && nested_name_specifier
23243 && CLASS_TYPE_P (nested_name_specifier))
23244 {
23245 tree id = cp_parser_unqualified_id (parser,
23246 /*template_keyword_p=*/false,
23247 /*check_dependency_p=*/false,
23248 /*declarator_p=*/true,
23249 /*optional_p=*/false);
23250 if (is_overloaded_fn (id))
23251 id = DECL_NAME (get_first_fn (id));
23252 if (!constructor_name_p (id, nested_name_specifier))
23253 constructor_p = false;
23254 }
23255 /* If we still think that this might be a constructor-declarator,
23256 look for a class-name. */
23257 else if (constructor_p)
23258 {
23259 /* If we have:
23260
23261 template <typename T> struct S {
23262 S();
23263 };
23264
23265 we must recognize that the nested `S' names a class. */
23266 tree type_decl;
23267 type_decl = cp_parser_class_name (parser,
23268 /*typename_keyword_p=*/false,
23269 /*template_keyword_p=*/false,
23270 none_type,
23271 /*check_dependency_p=*/false,
23272 /*class_head_p=*/false,
23273 /*is_declaration=*/false);
23274 /* If there was no class-name, then this is not a constructor.
23275 Otherwise, if we are in a class-specifier and we aren't
23276 handling a friend declaration, check that its type matches
23277 current_class_type (c++/38313). Note: error_mark_node
23278 is left alone for error recovery purposes. */
23279 constructor_p = (!cp_parser_error_occurred (parser)
23280 && (outside_class_specifier_p
23281 || type_decl == error_mark_node
23282 || same_type_p (current_class_type,
23283 TREE_TYPE (type_decl))));
23284
23285 /* If we're still considering a constructor, we have to see a `(',
23286 to begin the parameter-declaration-clause, followed by either a
23287 `)', an `...', or a decl-specifier. We need to check for a
23288 type-specifier to avoid being fooled into thinking that:
23289
23290 S (f) (int);
23291
23292 is a constructor. (It is actually a function named `f' that
23293 takes one parameter (of type `int') and returns a value of type
23294 `S'. */
23295 if (constructor_p
23296 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23297 constructor_p = false;
23298
23299 if (constructor_p
23300 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23301 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23302 /* A parameter declaration begins with a decl-specifier,
23303 which is either the "attribute" keyword, a storage class
23304 specifier, or (usually) a type-specifier. */
23305 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23306 {
23307 tree type;
23308 tree pushed_scope = NULL_TREE;
23309 unsigned saved_num_template_parameter_lists;
23310
23311 /* Names appearing in the type-specifier should be looked up
23312 in the scope of the class. */
23313 if (current_class_type)
23314 type = NULL_TREE;
23315 else
23316 {
23317 type = TREE_TYPE (type_decl);
23318 if (TREE_CODE (type) == TYPENAME_TYPE)
23319 {
23320 type = resolve_typename_type (type,
23321 /*only_current_p=*/false);
23322 if (TREE_CODE (type) == TYPENAME_TYPE)
23323 {
23324 cp_parser_abort_tentative_parse (parser);
23325 return false;
23326 }
23327 }
23328 pushed_scope = push_scope (type);
23329 }
23330
23331 /* Inside the constructor parameter list, surrounding
23332 template-parameter-lists do not apply. */
23333 saved_num_template_parameter_lists
23334 = parser->num_template_parameter_lists;
23335 parser->num_template_parameter_lists = 0;
23336
23337 /* Look for the type-specifier. */
23338 cp_parser_type_specifier (parser,
23339 CP_PARSER_FLAGS_NONE,
23340 /*decl_specs=*/NULL,
23341 /*is_declarator=*/true,
23342 /*declares_class_or_enum=*/NULL,
23343 /*is_cv_qualifier=*/NULL);
23344
23345 parser->num_template_parameter_lists
23346 = saved_num_template_parameter_lists;
23347
23348 /* Leave the scope of the class. */
23349 if (pushed_scope)
23350 pop_scope (pushed_scope);
23351
23352 constructor_p = !cp_parser_error_occurred (parser);
23353 }
23354 }
23355
23356 /* We did not really want to consume any tokens. */
23357 cp_parser_abort_tentative_parse (parser);
23358
23359 return constructor_p;
23360 }
23361
23362 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23363 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23364 they must be performed once we are in the scope of the function.
23365
23366 Returns the function defined. */
23367
23368 static tree
23369 cp_parser_function_definition_from_specifiers_and_declarator
23370 (cp_parser* parser,
23371 cp_decl_specifier_seq *decl_specifiers,
23372 tree attributes,
23373 const cp_declarator *declarator)
23374 {
23375 tree fn;
23376 bool success_p;
23377
23378 /* Begin the function-definition. */
23379 success_p = start_function (decl_specifiers, declarator, attributes);
23380
23381 /* The things we're about to see are not directly qualified by any
23382 template headers we've seen thus far. */
23383 reset_specialization ();
23384
23385 /* If there were names looked up in the decl-specifier-seq that we
23386 did not check, check them now. We must wait until we are in the
23387 scope of the function to perform the checks, since the function
23388 might be a friend. */
23389 perform_deferred_access_checks (tf_warning_or_error);
23390
23391 if (success_p)
23392 {
23393 cp_finalize_omp_declare_simd (parser, current_function_decl);
23394 parser->omp_declare_simd = NULL;
23395 }
23396
23397 if (!success_p)
23398 {
23399 /* Skip the entire function. */
23400 cp_parser_skip_to_end_of_block_or_statement (parser);
23401 fn = error_mark_node;
23402 }
23403 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23404 {
23405 /* Seen already, skip it. An error message has already been output. */
23406 cp_parser_skip_to_end_of_block_or_statement (parser);
23407 fn = current_function_decl;
23408 current_function_decl = NULL_TREE;
23409 /* If this is a function from a class, pop the nested class. */
23410 if (current_class_name)
23411 pop_nested_class ();
23412 }
23413 else
23414 {
23415 timevar_id_t tv;
23416 if (DECL_DECLARED_INLINE_P (current_function_decl))
23417 tv = TV_PARSE_INLINE;
23418 else
23419 tv = TV_PARSE_FUNC;
23420 timevar_push (tv);
23421 fn = cp_parser_function_definition_after_declarator (parser,
23422 /*inline_p=*/false);
23423 timevar_pop (tv);
23424 }
23425
23426 return fn;
23427 }
23428
23429 /* Parse the part of a function-definition that follows the
23430 declarator. INLINE_P is TRUE iff this function is an inline
23431 function defined within a class-specifier.
23432
23433 Returns the function defined. */
23434
23435 static tree
23436 cp_parser_function_definition_after_declarator (cp_parser* parser,
23437 bool inline_p)
23438 {
23439 tree fn;
23440 bool ctor_initializer_p = false;
23441 bool saved_in_unbraced_linkage_specification_p;
23442 bool saved_in_function_body;
23443 unsigned saved_num_template_parameter_lists;
23444 cp_token *token;
23445 bool fully_implicit_function_template_p
23446 = parser->fully_implicit_function_template_p;
23447 parser->fully_implicit_function_template_p = false;
23448 tree implicit_template_parms
23449 = parser->implicit_template_parms;
23450 parser->implicit_template_parms = 0;
23451 cp_binding_level* implicit_template_scope
23452 = parser->implicit_template_scope;
23453 parser->implicit_template_scope = 0;
23454
23455 saved_in_function_body = parser->in_function_body;
23456 parser->in_function_body = true;
23457 /* If the next token is `return', then the code may be trying to
23458 make use of the "named return value" extension that G++ used to
23459 support. */
23460 token = cp_lexer_peek_token (parser->lexer);
23461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23462 {
23463 /* Consume the `return' keyword. */
23464 cp_lexer_consume_token (parser->lexer);
23465 /* Look for the identifier that indicates what value is to be
23466 returned. */
23467 cp_parser_identifier (parser);
23468 /* Issue an error message. */
23469 error_at (token->location,
23470 "named return values are no longer supported");
23471 /* Skip tokens until we reach the start of the function body. */
23472 while (true)
23473 {
23474 cp_token *token = cp_lexer_peek_token (parser->lexer);
23475 if (token->type == CPP_OPEN_BRACE
23476 || token->type == CPP_EOF
23477 || token->type == CPP_PRAGMA_EOL)
23478 break;
23479 cp_lexer_consume_token (parser->lexer);
23480 }
23481 }
23482 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23483 anything declared inside `f'. */
23484 saved_in_unbraced_linkage_specification_p
23485 = parser->in_unbraced_linkage_specification_p;
23486 parser->in_unbraced_linkage_specification_p = false;
23487 /* Inside the function, surrounding template-parameter-lists do not
23488 apply. */
23489 saved_num_template_parameter_lists
23490 = parser->num_template_parameter_lists;
23491 parser->num_template_parameter_lists = 0;
23492
23493 start_lambda_scope (current_function_decl);
23494
23495 /* If the next token is `try', `__transaction_atomic', or
23496 `__transaction_relaxed`, then we are looking at either function-try-block
23497 or function-transaction-block. Note that all of these include the
23498 function-body. */
23499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23500 ctor_initializer_p = cp_parser_function_transaction (parser,
23501 RID_TRANSACTION_ATOMIC);
23502 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23503 RID_TRANSACTION_RELAXED))
23504 ctor_initializer_p = cp_parser_function_transaction (parser,
23505 RID_TRANSACTION_RELAXED);
23506 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23507 ctor_initializer_p = cp_parser_function_try_block (parser);
23508 else
23509 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23510 (parser, /*in_function_try_block=*/false);
23511
23512 finish_lambda_scope ();
23513
23514 /* Finish the function. */
23515 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23516 (inline_p ? 2 : 0));
23517 /* Generate code for it, if necessary. */
23518 expand_or_defer_fn (fn);
23519 /* Restore the saved values. */
23520 parser->in_unbraced_linkage_specification_p
23521 = saved_in_unbraced_linkage_specification_p;
23522 parser->num_template_parameter_lists
23523 = saved_num_template_parameter_lists;
23524 parser->in_function_body = saved_in_function_body;
23525
23526 parser->fully_implicit_function_template_p
23527 = fully_implicit_function_template_p;
23528 parser->implicit_template_parms
23529 = implicit_template_parms;
23530 parser->implicit_template_scope
23531 = implicit_template_scope;
23532
23533 if (parser->fully_implicit_function_template_p)
23534 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23535
23536 return fn;
23537 }
23538
23539 /* Parse a template-declaration, assuming that the `export' (and
23540 `extern') keywords, if present, has already been scanned. MEMBER_P
23541 is as for cp_parser_template_declaration. */
23542
23543 static void
23544 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23545 {
23546 tree decl = NULL_TREE;
23547 vec<deferred_access_check, va_gc> *checks;
23548 tree parameter_list;
23549 bool friend_p = false;
23550 bool need_lang_pop;
23551 cp_token *token;
23552
23553 /* Look for the `template' keyword. */
23554 token = cp_lexer_peek_token (parser->lexer);
23555 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23556 return;
23557
23558 /* And the `<'. */
23559 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23560 return;
23561 if (at_class_scope_p () && current_function_decl)
23562 {
23563 /* 14.5.2.2 [temp.mem]
23564
23565 A local class shall not have member templates. */
23566 error_at (token->location,
23567 "invalid declaration of member template in local class");
23568 cp_parser_skip_to_end_of_block_or_statement (parser);
23569 return;
23570 }
23571 /* [temp]
23572
23573 A template ... shall not have C linkage. */
23574 if (current_lang_name == lang_name_c)
23575 {
23576 error_at (token->location, "template with C linkage");
23577 /* Give it C++ linkage to avoid confusing other parts of the
23578 front end. */
23579 push_lang_context (lang_name_cplusplus);
23580 need_lang_pop = true;
23581 }
23582 else
23583 need_lang_pop = false;
23584
23585 /* We cannot perform access checks on the template parameter
23586 declarations until we know what is being declared, just as we
23587 cannot check the decl-specifier list. */
23588 push_deferring_access_checks (dk_deferred);
23589
23590 /* If the next token is `>', then we have an invalid
23591 specialization. Rather than complain about an invalid template
23592 parameter, issue an error message here. */
23593 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23594 {
23595 cp_parser_error (parser, "invalid explicit specialization");
23596 begin_specialization ();
23597 parameter_list = NULL_TREE;
23598 }
23599 else
23600 {
23601 /* Parse the template parameters. */
23602 parameter_list = cp_parser_template_parameter_list (parser);
23603 }
23604
23605 /* Get the deferred access checks from the parameter list. These
23606 will be checked once we know what is being declared, as for a
23607 member template the checks must be performed in the scope of the
23608 class containing the member. */
23609 checks = get_deferred_access_checks ();
23610
23611 /* Look for the `>'. */
23612 cp_parser_skip_to_end_of_template_parameter_list (parser);
23613 /* We just processed one more parameter list. */
23614 ++parser->num_template_parameter_lists;
23615 /* If the next token is `template', there are more template
23616 parameters. */
23617 if (cp_lexer_next_token_is_keyword (parser->lexer,
23618 RID_TEMPLATE))
23619 cp_parser_template_declaration_after_export (parser, member_p);
23620 else if (cxx_dialect >= cxx11
23621 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23622 decl = cp_parser_alias_declaration (parser);
23623 else
23624 {
23625 /* There are no access checks when parsing a template, as we do not
23626 know if a specialization will be a friend. */
23627 push_deferring_access_checks (dk_no_check);
23628 token = cp_lexer_peek_token (parser->lexer);
23629 decl = cp_parser_single_declaration (parser,
23630 checks,
23631 member_p,
23632 /*explicit_specialization_p=*/false,
23633 &friend_p);
23634 pop_deferring_access_checks ();
23635
23636 /* If this is a member template declaration, let the front
23637 end know. */
23638 if (member_p && !friend_p && decl)
23639 {
23640 if (TREE_CODE (decl) == TYPE_DECL)
23641 cp_parser_check_access_in_redeclaration (decl, token->location);
23642
23643 decl = finish_member_template_decl (decl);
23644 }
23645 else if (friend_p && decl
23646 && DECL_DECLARES_TYPE_P (decl))
23647 make_friend_class (current_class_type, TREE_TYPE (decl),
23648 /*complain=*/true);
23649 }
23650 /* We are done with the current parameter list. */
23651 --parser->num_template_parameter_lists;
23652
23653 pop_deferring_access_checks ();
23654
23655 /* Finish up. */
23656 finish_template_decl (parameter_list);
23657
23658 /* Check the template arguments for a literal operator template. */
23659 if (decl
23660 && DECL_DECLARES_FUNCTION_P (decl)
23661 && UDLIT_OPER_P (DECL_NAME (decl)))
23662 {
23663 bool ok = true;
23664 if (parameter_list == NULL_TREE)
23665 ok = false;
23666 else
23667 {
23668 int num_parms = TREE_VEC_LENGTH (parameter_list);
23669 if (num_parms == 1)
23670 {
23671 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23672 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23673 if (TREE_TYPE (parm) != char_type_node
23674 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23675 ok = false;
23676 }
23677 else if (num_parms == 2 && cxx_dialect >= cxx14)
23678 {
23679 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23680 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23681 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23682 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23683 if (TREE_TYPE (parm) != TREE_TYPE (type)
23684 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23685 ok = false;
23686 }
23687 else
23688 ok = false;
23689 }
23690 if (!ok)
23691 {
23692 if (cxx_dialect >= cxx14)
23693 error ("literal operator template %qD has invalid parameter list."
23694 " Expected non-type template argument pack <char...>"
23695 " or <typename CharT, CharT...>",
23696 decl);
23697 else
23698 error ("literal operator template %qD has invalid parameter list."
23699 " Expected non-type template argument pack <char...>",
23700 decl);
23701 }
23702 }
23703 /* Register member declarations. */
23704 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23705 finish_member_declaration (decl);
23706 /* For the erroneous case of a template with C linkage, we pushed an
23707 implicit C++ linkage scope; exit that scope now. */
23708 if (need_lang_pop)
23709 pop_lang_context ();
23710 /* If DECL is a function template, we must return to parse it later.
23711 (Even though there is no definition, there might be default
23712 arguments that need handling.) */
23713 if (member_p && decl
23714 && DECL_DECLARES_FUNCTION_P (decl))
23715 vec_safe_push (unparsed_funs_with_definitions, decl);
23716 }
23717
23718 /* Perform the deferred access checks from a template-parameter-list.
23719 CHECKS is a TREE_LIST of access checks, as returned by
23720 get_deferred_access_checks. */
23721
23722 static void
23723 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23724 {
23725 ++processing_template_parmlist;
23726 perform_access_checks (checks, tf_warning_or_error);
23727 --processing_template_parmlist;
23728 }
23729
23730 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23731 `function-definition' sequence that follows a template header.
23732 If MEMBER_P is true, this declaration appears in a class scope.
23733
23734 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23735 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23736
23737 static tree
23738 cp_parser_single_declaration (cp_parser* parser,
23739 vec<deferred_access_check, va_gc> *checks,
23740 bool member_p,
23741 bool explicit_specialization_p,
23742 bool* friend_p)
23743 {
23744 int declares_class_or_enum;
23745 tree decl = NULL_TREE;
23746 cp_decl_specifier_seq decl_specifiers;
23747 bool function_definition_p = false;
23748 cp_token *decl_spec_token_start;
23749
23750 /* This function is only used when processing a template
23751 declaration. */
23752 gcc_assert (innermost_scope_kind () == sk_template_parms
23753 || innermost_scope_kind () == sk_template_spec);
23754
23755 /* Defer access checks until we know what is being declared. */
23756 push_deferring_access_checks (dk_deferred);
23757
23758 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23759 alternative. */
23760 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23761 cp_parser_decl_specifier_seq (parser,
23762 CP_PARSER_FLAGS_OPTIONAL,
23763 &decl_specifiers,
23764 &declares_class_or_enum);
23765 if (friend_p)
23766 *friend_p = cp_parser_friend_p (&decl_specifiers);
23767
23768 /* There are no template typedefs. */
23769 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23770 {
23771 error_at (decl_spec_token_start->location,
23772 "template declaration of %<typedef%>");
23773 decl = error_mark_node;
23774 }
23775
23776 /* Gather up the access checks that occurred the
23777 decl-specifier-seq. */
23778 stop_deferring_access_checks ();
23779
23780 /* Check for the declaration of a template class. */
23781 if (declares_class_or_enum)
23782 {
23783 if (cp_parser_declares_only_class_p (parser))
23784 {
23785 decl = shadow_tag (&decl_specifiers);
23786
23787 /* In this case:
23788
23789 struct C {
23790 friend template <typename T> struct A<T>::B;
23791 };
23792
23793 A<T>::B will be represented by a TYPENAME_TYPE, and
23794 therefore not recognized by shadow_tag. */
23795 if (friend_p && *friend_p
23796 && !decl
23797 && decl_specifiers.type
23798 && TYPE_P (decl_specifiers.type))
23799 decl = decl_specifiers.type;
23800
23801 if (decl && decl != error_mark_node)
23802 decl = TYPE_NAME (decl);
23803 else
23804 decl = error_mark_node;
23805
23806 /* Perform access checks for template parameters. */
23807 cp_parser_perform_template_parameter_access_checks (checks);
23808 }
23809 }
23810
23811 /* Complain about missing 'typename' or other invalid type names. */
23812 if (!decl_specifiers.any_type_specifiers_p
23813 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23814 {
23815 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23816 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23817 the rest of this declaration. */
23818 decl = error_mark_node;
23819 goto out;
23820 }
23821
23822 /* If it's not a template class, try for a template function. If
23823 the next token is a `;', then this declaration does not declare
23824 anything. But, if there were errors in the decl-specifiers, then
23825 the error might well have come from an attempted class-specifier.
23826 In that case, there's no need to warn about a missing declarator. */
23827 if (!decl
23828 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23829 || decl_specifiers.type != error_mark_node))
23830 {
23831 decl = cp_parser_init_declarator (parser,
23832 &decl_specifiers,
23833 checks,
23834 /*function_definition_allowed_p=*/true,
23835 member_p,
23836 declares_class_or_enum,
23837 &function_definition_p,
23838 NULL, NULL);
23839
23840 /* 7.1.1-1 [dcl.stc]
23841
23842 A storage-class-specifier shall not be specified in an explicit
23843 specialization... */
23844 if (decl
23845 && explicit_specialization_p
23846 && decl_specifiers.storage_class != sc_none)
23847 {
23848 error_at (decl_spec_token_start->location,
23849 "explicit template specialization cannot have a storage class");
23850 decl = error_mark_node;
23851 }
23852
23853 if (decl && VAR_P (decl))
23854 check_template_variable (decl);
23855 }
23856
23857 /* Look for a trailing `;' after the declaration. */
23858 if (!function_definition_p
23859 && (decl == error_mark_node
23860 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23861 cp_parser_skip_to_end_of_block_or_statement (parser);
23862
23863 out:
23864 pop_deferring_access_checks ();
23865
23866 /* Clear any current qualification; whatever comes next is the start
23867 of something new. */
23868 parser->scope = NULL_TREE;
23869 parser->qualifying_scope = NULL_TREE;
23870 parser->object_scope = NULL_TREE;
23871
23872 return decl;
23873 }
23874
23875 /* Parse a cast-expression that is not the operand of a unary "&". */
23876
23877 static tree
23878 cp_parser_simple_cast_expression (cp_parser *parser)
23879 {
23880 return cp_parser_cast_expression (parser, /*address_p=*/false,
23881 /*cast_p=*/false, /*decltype*/false, NULL);
23882 }
23883
23884 /* Parse a functional cast to TYPE. Returns an expression
23885 representing the cast. */
23886
23887 static tree
23888 cp_parser_functional_cast (cp_parser* parser, tree type)
23889 {
23890 vec<tree, va_gc> *vec;
23891 tree expression_list;
23892 tree cast;
23893 bool nonconst_p;
23894
23895 if (!type)
23896 type = error_mark_node;
23897
23898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23899 {
23900 cp_lexer_set_source_position (parser->lexer);
23901 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23902 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23903 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23904 if (TREE_CODE (type) == TYPE_DECL)
23905 type = TREE_TYPE (type);
23906 return finish_compound_literal (type, expression_list,
23907 tf_warning_or_error);
23908 }
23909
23910
23911 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23912 /*cast_p=*/true,
23913 /*allow_expansion_p=*/true,
23914 /*non_constant_p=*/NULL);
23915 if (vec == NULL)
23916 expression_list = error_mark_node;
23917 else
23918 {
23919 expression_list = build_tree_list_vec (vec);
23920 release_tree_vector (vec);
23921 }
23922
23923 cast = build_functional_cast (type, expression_list,
23924 tf_warning_or_error);
23925 /* [expr.const]/1: In an integral constant expression "only type
23926 conversions to integral or enumeration type can be used". */
23927 if (TREE_CODE (type) == TYPE_DECL)
23928 type = TREE_TYPE (type);
23929 if (cast != error_mark_node
23930 && !cast_valid_in_integral_constant_expression_p (type)
23931 && cp_parser_non_integral_constant_expression (parser,
23932 NIC_CONSTRUCTOR))
23933 return error_mark_node;
23934 return cast;
23935 }
23936
23937 /* Save the tokens that make up the body of a member function defined
23938 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23939 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23940 specifiers applied to the declaration. Returns the FUNCTION_DECL
23941 for the member function. */
23942
23943 static tree
23944 cp_parser_save_member_function_body (cp_parser* parser,
23945 cp_decl_specifier_seq *decl_specifiers,
23946 cp_declarator *declarator,
23947 tree attributes)
23948 {
23949 cp_token *first;
23950 cp_token *last;
23951 tree fn;
23952
23953 /* Create the FUNCTION_DECL. */
23954 fn = grokmethod (decl_specifiers, declarator, attributes);
23955 cp_finalize_omp_declare_simd (parser, fn);
23956 /* If something went badly wrong, bail out now. */
23957 if (fn == error_mark_node)
23958 {
23959 /* If there's a function-body, skip it. */
23960 if (cp_parser_token_starts_function_definition_p
23961 (cp_lexer_peek_token (parser->lexer)))
23962 cp_parser_skip_to_end_of_block_or_statement (parser);
23963 return error_mark_node;
23964 }
23965
23966 /* Remember it, if there default args to post process. */
23967 cp_parser_save_default_args (parser, fn);
23968
23969 /* Save away the tokens that make up the body of the
23970 function. */
23971 first = parser->lexer->next_token;
23972 /* Handle function try blocks. */
23973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23974 cp_lexer_consume_token (parser->lexer);
23975 /* We can have braced-init-list mem-initializers before the fn body. */
23976 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23977 {
23978 cp_lexer_consume_token (parser->lexer);
23979 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23980 {
23981 /* cache_group will stop after an un-nested { } pair, too. */
23982 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23983 break;
23984
23985 /* variadic mem-inits have ... after the ')'. */
23986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23987 cp_lexer_consume_token (parser->lexer);
23988 }
23989 }
23990 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23991 /* Handle function try blocks. */
23992 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23993 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23994 last = parser->lexer->next_token;
23995
23996 /* Save away the inline definition; we will process it when the
23997 class is complete. */
23998 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23999 DECL_PENDING_INLINE_P (fn) = 1;
24000
24001 /* We need to know that this was defined in the class, so that
24002 friend templates are handled correctly. */
24003 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24004
24005 /* Add FN to the queue of functions to be parsed later. */
24006 vec_safe_push (unparsed_funs_with_definitions, fn);
24007
24008 return fn;
24009 }
24010
24011 /* Save the tokens that make up the in-class initializer for a non-static
24012 data member. Returns a DEFAULT_ARG. */
24013
24014 static tree
24015 cp_parser_save_nsdmi (cp_parser* parser)
24016 {
24017 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24018 }
24019
24020 /* Parse a template-argument-list, as well as the trailing ">" (but
24021 not the opening "<"). See cp_parser_template_argument_list for the
24022 return value. */
24023
24024 static tree
24025 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24026 {
24027 tree arguments;
24028 tree saved_scope;
24029 tree saved_qualifying_scope;
24030 tree saved_object_scope;
24031 bool saved_greater_than_is_operator_p;
24032 int saved_unevaluated_operand;
24033 int saved_inhibit_evaluation_warnings;
24034
24035 /* [temp.names]
24036
24037 When parsing a template-id, the first non-nested `>' is taken as
24038 the end of the template-argument-list rather than a greater-than
24039 operator. */
24040 saved_greater_than_is_operator_p
24041 = parser->greater_than_is_operator_p;
24042 parser->greater_than_is_operator_p = false;
24043 /* Parsing the argument list may modify SCOPE, so we save it
24044 here. */
24045 saved_scope = parser->scope;
24046 saved_qualifying_scope = parser->qualifying_scope;
24047 saved_object_scope = parser->object_scope;
24048 /* We need to evaluate the template arguments, even though this
24049 template-id may be nested within a "sizeof". */
24050 saved_unevaluated_operand = cp_unevaluated_operand;
24051 cp_unevaluated_operand = 0;
24052 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24053 c_inhibit_evaluation_warnings = 0;
24054 /* Parse the template-argument-list itself. */
24055 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24056 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24057 arguments = NULL_TREE;
24058 else
24059 arguments = cp_parser_template_argument_list (parser);
24060 /* Look for the `>' that ends the template-argument-list. If we find
24061 a '>>' instead, it's probably just a typo. */
24062 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24063 {
24064 if (cxx_dialect != cxx98)
24065 {
24066 /* In C++0x, a `>>' in a template argument list or cast
24067 expression is considered to be two separate `>'
24068 tokens. So, change the current token to a `>', but don't
24069 consume it: it will be consumed later when the outer
24070 template argument list (or cast expression) is parsed.
24071 Note that this replacement of `>' for `>>' is necessary
24072 even if we are parsing tentatively: in the tentative
24073 case, after calling
24074 cp_parser_enclosed_template_argument_list we will always
24075 throw away all of the template arguments and the first
24076 closing `>', either because the template argument list
24077 was erroneous or because we are replacing those tokens
24078 with a CPP_TEMPLATE_ID token. The second `>' (which will
24079 not have been thrown away) is needed either to close an
24080 outer template argument list or to complete a new-style
24081 cast. */
24082 cp_token *token = cp_lexer_peek_token (parser->lexer);
24083 token->type = CPP_GREATER;
24084 }
24085 else if (!saved_greater_than_is_operator_p)
24086 {
24087 /* If we're in a nested template argument list, the '>>' has
24088 to be a typo for '> >'. We emit the error message, but we
24089 continue parsing and we push a '>' as next token, so that
24090 the argument list will be parsed correctly. Note that the
24091 global source location is still on the token before the
24092 '>>', so we need to say explicitly where we want it. */
24093 cp_token *token = cp_lexer_peek_token (parser->lexer);
24094 error_at (token->location, "%<>>%> should be %<> >%> "
24095 "within a nested template argument list");
24096
24097 token->type = CPP_GREATER;
24098 }
24099 else
24100 {
24101 /* If this is not a nested template argument list, the '>>'
24102 is a typo for '>'. Emit an error message and continue.
24103 Same deal about the token location, but here we can get it
24104 right by consuming the '>>' before issuing the diagnostic. */
24105 cp_token *token = cp_lexer_consume_token (parser->lexer);
24106 error_at (token->location,
24107 "spurious %<>>%>, use %<>%> to terminate "
24108 "a template argument list");
24109 }
24110 }
24111 else
24112 cp_parser_skip_to_end_of_template_parameter_list (parser);
24113 /* The `>' token might be a greater-than operator again now. */
24114 parser->greater_than_is_operator_p
24115 = saved_greater_than_is_operator_p;
24116 /* Restore the SAVED_SCOPE. */
24117 parser->scope = saved_scope;
24118 parser->qualifying_scope = saved_qualifying_scope;
24119 parser->object_scope = saved_object_scope;
24120 cp_unevaluated_operand = saved_unevaluated_operand;
24121 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24122
24123 return arguments;
24124 }
24125
24126 /* MEMBER_FUNCTION is a member function, or a friend. If default
24127 arguments, or the body of the function have not yet been parsed,
24128 parse them now. */
24129
24130 static void
24131 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24132 {
24133 timevar_push (TV_PARSE_INMETH);
24134 /* If this member is a template, get the underlying
24135 FUNCTION_DECL. */
24136 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24137 member_function = DECL_TEMPLATE_RESULT (member_function);
24138
24139 /* There should not be any class definitions in progress at this
24140 point; the bodies of members are only parsed outside of all class
24141 definitions. */
24142 gcc_assert (parser->num_classes_being_defined == 0);
24143 /* While we're parsing the member functions we might encounter more
24144 classes. We want to handle them right away, but we don't want
24145 them getting mixed up with functions that are currently in the
24146 queue. */
24147 push_unparsed_function_queues (parser);
24148
24149 /* Make sure that any template parameters are in scope. */
24150 maybe_begin_member_template_processing (member_function);
24151
24152 /* If the body of the function has not yet been parsed, parse it
24153 now. */
24154 if (DECL_PENDING_INLINE_P (member_function))
24155 {
24156 tree function_scope;
24157 cp_token_cache *tokens;
24158
24159 /* The function is no longer pending; we are processing it. */
24160 tokens = DECL_PENDING_INLINE_INFO (member_function);
24161 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24162 DECL_PENDING_INLINE_P (member_function) = 0;
24163
24164 /* If this is a local class, enter the scope of the containing
24165 function. */
24166 function_scope = current_function_decl;
24167 if (function_scope)
24168 push_function_context ();
24169
24170 /* Push the body of the function onto the lexer stack. */
24171 cp_parser_push_lexer_for_tokens (parser, tokens);
24172
24173 /* Let the front end know that we going to be defining this
24174 function. */
24175 start_preparsed_function (member_function, NULL_TREE,
24176 SF_PRE_PARSED | SF_INCLASS_INLINE);
24177
24178 /* Don't do access checking if it is a templated function. */
24179 if (processing_template_decl)
24180 push_deferring_access_checks (dk_no_check);
24181
24182 /* #pragma omp declare reduction needs special parsing. */
24183 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24184 {
24185 parser->lexer->in_pragma = true;
24186 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24187 finish_function (/*inline*/2);
24188 cp_check_omp_declare_reduction (member_function);
24189 }
24190 else
24191 /* Now, parse the body of the function. */
24192 cp_parser_function_definition_after_declarator (parser,
24193 /*inline_p=*/true);
24194
24195 if (processing_template_decl)
24196 pop_deferring_access_checks ();
24197
24198 /* Leave the scope of the containing function. */
24199 if (function_scope)
24200 pop_function_context ();
24201 cp_parser_pop_lexer (parser);
24202 }
24203
24204 /* Remove any template parameters from the symbol table. */
24205 maybe_end_member_template_processing ();
24206
24207 /* Restore the queue. */
24208 pop_unparsed_function_queues (parser);
24209 timevar_pop (TV_PARSE_INMETH);
24210 }
24211
24212 /* If DECL contains any default args, remember it on the unparsed
24213 functions queue. */
24214
24215 static void
24216 cp_parser_save_default_args (cp_parser* parser, tree decl)
24217 {
24218 tree probe;
24219
24220 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24221 probe;
24222 probe = TREE_CHAIN (probe))
24223 if (TREE_PURPOSE (probe))
24224 {
24225 cp_default_arg_entry entry = {current_class_type, decl};
24226 vec_safe_push (unparsed_funs_with_default_args, entry);
24227 break;
24228 }
24229 }
24230
24231 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24232 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24233 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24234 from the parameter-type-list. */
24235
24236 static tree
24237 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24238 tree default_arg, tree parmtype)
24239 {
24240 cp_token_cache *tokens;
24241 tree parsed_arg;
24242 bool dummy;
24243
24244 if (default_arg == error_mark_node)
24245 return error_mark_node;
24246
24247 /* Push the saved tokens for the default argument onto the parser's
24248 lexer stack. */
24249 tokens = DEFARG_TOKENS (default_arg);
24250 cp_parser_push_lexer_for_tokens (parser, tokens);
24251
24252 start_lambda_scope (decl);
24253
24254 /* Parse the default argument. */
24255 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24256 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24257 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24258
24259 finish_lambda_scope ();
24260
24261 if (parsed_arg == error_mark_node)
24262 cp_parser_skip_to_end_of_statement (parser);
24263
24264 if (!processing_template_decl)
24265 {
24266 /* In a non-template class, check conversions now. In a template,
24267 we'll wait and instantiate these as needed. */
24268 if (TREE_CODE (decl) == PARM_DECL)
24269 parsed_arg = check_default_argument (parmtype, parsed_arg,
24270 tf_warning_or_error);
24271 else
24272 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24273 }
24274
24275 /* If the token stream has not been completely used up, then
24276 there was extra junk after the end of the default
24277 argument. */
24278 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24279 {
24280 if (TREE_CODE (decl) == PARM_DECL)
24281 cp_parser_error (parser, "expected %<,%>");
24282 else
24283 cp_parser_error (parser, "expected %<;%>");
24284 }
24285
24286 /* Revert to the main lexer. */
24287 cp_parser_pop_lexer (parser);
24288
24289 return parsed_arg;
24290 }
24291
24292 /* FIELD is a non-static data member with an initializer which we saved for
24293 later; parse it now. */
24294
24295 static void
24296 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24297 {
24298 tree def;
24299
24300 maybe_begin_member_template_processing (field);
24301
24302 push_unparsed_function_queues (parser);
24303 def = cp_parser_late_parse_one_default_arg (parser, field,
24304 DECL_INITIAL (field),
24305 NULL_TREE);
24306 pop_unparsed_function_queues (parser);
24307
24308 maybe_end_member_template_processing ();
24309
24310 DECL_INITIAL (field) = def;
24311 }
24312
24313 /* FN is a FUNCTION_DECL which may contains a parameter with an
24314 unparsed DEFAULT_ARG. Parse the default args now. This function
24315 assumes that the current scope is the scope in which the default
24316 argument should be processed. */
24317
24318 static void
24319 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24320 {
24321 bool saved_local_variables_forbidden_p;
24322 tree parm, parmdecl;
24323
24324 /* While we're parsing the default args, we might (due to the
24325 statement expression extension) encounter more classes. We want
24326 to handle them right away, but we don't want them getting mixed
24327 up with default args that are currently in the queue. */
24328 push_unparsed_function_queues (parser);
24329
24330 /* Local variable names (and the `this' keyword) may not appear
24331 in a default argument. */
24332 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24333 parser->local_variables_forbidden_p = true;
24334
24335 push_defarg_context (fn);
24336
24337 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24338 parmdecl = DECL_ARGUMENTS (fn);
24339 parm && parm != void_list_node;
24340 parm = TREE_CHAIN (parm),
24341 parmdecl = DECL_CHAIN (parmdecl))
24342 {
24343 tree default_arg = TREE_PURPOSE (parm);
24344 tree parsed_arg;
24345 vec<tree, va_gc> *insts;
24346 tree copy;
24347 unsigned ix;
24348
24349 if (!default_arg)
24350 continue;
24351
24352 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24353 /* This can happen for a friend declaration for a function
24354 already declared with default arguments. */
24355 continue;
24356
24357 parsed_arg
24358 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24359 default_arg,
24360 TREE_VALUE (parm));
24361 if (parsed_arg == error_mark_node)
24362 {
24363 continue;
24364 }
24365
24366 TREE_PURPOSE (parm) = parsed_arg;
24367
24368 /* Update any instantiations we've already created. */
24369 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24370 vec_safe_iterate (insts, ix, &copy); ix++)
24371 TREE_PURPOSE (copy) = parsed_arg;
24372 }
24373
24374 pop_defarg_context ();
24375
24376 /* Make sure no default arg is missing. */
24377 check_default_args (fn);
24378
24379 /* Restore the state of local_variables_forbidden_p. */
24380 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24381
24382 /* Restore the queue. */
24383 pop_unparsed_function_queues (parser);
24384 }
24385
24386 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24387
24388 sizeof ... ( identifier )
24389
24390 where the 'sizeof' token has already been consumed. */
24391
24392 static tree
24393 cp_parser_sizeof_pack (cp_parser *parser)
24394 {
24395 /* Consume the `...'. */
24396 cp_lexer_consume_token (parser->lexer);
24397 maybe_warn_variadic_templates ();
24398
24399 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24400 if (paren)
24401 cp_lexer_consume_token (parser->lexer);
24402 else
24403 permerror (cp_lexer_peek_token (parser->lexer)->location,
24404 "%<sizeof...%> argument must be surrounded by parentheses");
24405
24406 cp_token *token = cp_lexer_peek_token (parser->lexer);
24407 tree name = cp_parser_identifier (parser);
24408 if (name == error_mark_node)
24409 return error_mark_node;
24410 /* The name is not qualified. */
24411 parser->scope = NULL_TREE;
24412 parser->qualifying_scope = NULL_TREE;
24413 parser->object_scope = NULL_TREE;
24414 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24415 if (expr == error_mark_node)
24416 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24417 token->location);
24418 if (TREE_CODE (expr) == TYPE_DECL)
24419 expr = TREE_TYPE (expr);
24420 else if (TREE_CODE (expr) == CONST_DECL)
24421 expr = DECL_INITIAL (expr);
24422 expr = make_pack_expansion (expr);
24423
24424 if (paren)
24425 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24426
24427 return expr;
24428 }
24429
24430 /* Parse the operand of `sizeof' (or a similar operator). Returns
24431 either a TYPE or an expression, depending on the form of the
24432 input. The KEYWORD indicates which kind of expression we have
24433 encountered. */
24434
24435 static tree
24436 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24437 {
24438 tree expr = NULL_TREE;
24439 const char *saved_message;
24440 char *tmp;
24441 bool saved_integral_constant_expression_p;
24442 bool saved_non_integral_constant_expression_p;
24443
24444 /* If it's a `...', then we are computing the length of a parameter
24445 pack. */
24446 if (keyword == RID_SIZEOF
24447 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24448 return cp_parser_sizeof_pack (parser);
24449
24450 /* Types cannot be defined in a `sizeof' expression. Save away the
24451 old message. */
24452 saved_message = parser->type_definition_forbidden_message;
24453 /* And create the new one. */
24454 tmp = concat ("types may not be defined in %<",
24455 IDENTIFIER_POINTER (ridpointers[keyword]),
24456 "%> expressions", NULL);
24457 parser->type_definition_forbidden_message = tmp;
24458
24459 /* The restrictions on constant-expressions do not apply inside
24460 sizeof expressions. */
24461 saved_integral_constant_expression_p
24462 = parser->integral_constant_expression_p;
24463 saved_non_integral_constant_expression_p
24464 = parser->non_integral_constant_expression_p;
24465 parser->integral_constant_expression_p = false;
24466
24467 /* Do not actually evaluate the expression. */
24468 ++cp_unevaluated_operand;
24469 ++c_inhibit_evaluation_warnings;
24470 /* If it's a `(', then we might be looking at the type-id
24471 construction. */
24472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24473 {
24474 tree type = NULL_TREE;
24475
24476 /* We can't be sure yet whether we're looking at a type-id or an
24477 expression. */
24478 cp_parser_parse_tentatively (parser);
24479 /* Note: as a GNU Extension, compound literals are considered
24480 postfix-expressions as they are in C99, so they are valid
24481 arguments to sizeof. See comment in cp_parser_cast_expression
24482 for details. */
24483 if (cp_parser_compound_literal_p (parser))
24484 cp_parser_simulate_error (parser);
24485 else
24486 {
24487 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24488 parser->in_type_id_in_expr_p = true;
24489 /* Look for the type-id. */
24490 type = cp_parser_type_id (parser);
24491 /* Look for the closing `)'. */
24492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24493 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24494 }
24495
24496 /* If all went well, then we're done. */
24497 if (cp_parser_parse_definitely (parser))
24498 {
24499 cp_decl_specifier_seq decl_specs;
24500
24501 /* Build a trivial decl-specifier-seq. */
24502 clear_decl_specs (&decl_specs);
24503 decl_specs.type = type;
24504
24505 /* Call grokdeclarator to figure out what type this is. */
24506 expr = grokdeclarator (NULL,
24507 &decl_specs,
24508 TYPENAME,
24509 /*initialized=*/0,
24510 /*attrlist=*/NULL);
24511 }
24512 }
24513
24514 /* If the type-id production did not work out, then we must be
24515 looking at the unary-expression production. */
24516 if (!expr)
24517 expr = cp_parser_unary_expression (parser);
24518
24519 /* Go back to evaluating expressions. */
24520 --cp_unevaluated_operand;
24521 --c_inhibit_evaluation_warnings;
24522
24523 /* Free the message we created. */
24524 free (tmp);
24525 /* And restore the old one. */
24526 parser->type_definition_forbidden_message = saved_message;
24527 parser->integral_constant_expression_p
24528 = saved_integral_constant_expression_p;
24529 parser->non_integral_constant_expression_p
24530 = saved_non_integral_constant_expression_p;
24531
24532 return expr;
24533 }
24534
24535 /* If the current declaration has no declarator, return true. */
24536
24537 static bool
24538 cp_parser_declares_only_class_p (cp_parser *parser)
24539 {
24540 /* If the next token is a `;' or a `,' then there is no
24541 declarator. */
24542 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24543 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24544 }
24545
24546 /* Update the DECL_SPECS to reflect the storage class indicated by
24547 KEYWORD. */
24548
24549 static void
24550 cp_parser_set_storage_class (cp_parser *parser,
24551 cp_decl_specifier_seq *decl_specs,
24552 enum rid keyword,
24553 cp_token *token)
24554 {
24555 cp_storage_class storage_class;
24556
24557 if (parser->in_unbraced_linkage_specification_p)
24558 {
24559 error_at (token->location, "invalid use of %qD in linkage specification",
24560 ridpointers[keyword]);
24561 return;
24562 }
24563 else if (decl_specs->storage_class != sc_none)
24564 {
24565 decl_specs->conflicting_specifiers_p = true;
24566 return;
24567 }
24568
24569 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24570 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24571 && decl_specs->gnu_thread_keyword_p)
24572 {
24573 pedwarn (decl_specs->locations[ds_thread], 0,
24574 "%<__thread%> before %qD", ridpointers[keyword]);
24575 }
24576
24577 switch (keyword)
24578 {
24579 case RID_AUTO:
24580 storage_class = sc_auto;
24581 break;
24582 case RID_REGISTER:
24583 storage_class = sc_register;
24584 break;
24585 case RID_STATIC:
24586 storage_class = sc_static;
24587 break;
24588 case RID_EXTERN:
24589 storage_class = sc_extern;
24590 break;
24591 case RID_MUTABLE:
24592 storage_class = sc_mutable;
24593 break;
24594 default:
24595 gcc_unreachable ();
24596 }
24597 decl_specs->storage_class = storage_class;
24598 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24599
24600 /* A storage class specifier cannot be applied alongside a typedef
24601 specifier. If there is a typedef specifier present then set
24602 conflicting_specifiers_p which will trigger an error later
24603 on in grokdeclarator. */
24604 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24605 decl_specs->conflicting_specifiers_p = true;
24606 }
24607
24608 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24609 is true, the type is a class or enum definition. */
24610
24611 static void
24612 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24613 tree type_spec,
24614 cp_token *token,
24615 bool type_definition_p)
24616 {
24617 decl_specs->any_specifiers_p = true;
24618
24619 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24620 (with, for example, in "typedef int wchar_t;") we remember that
24621 this is what happened. In system headers, we ignore these
24622 declarations so that G++ can work with system headers that are not
24623 C++-safe. */
24624 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24625 && !type_definition_p
24626 && (type_spec == boolean_type_node
24627 || type_spec == char16_type_node
24628 || type_spec == char32_type_node
24629 || type_spec == wchar_type_node)
24630 && (decl_specs->type
24631 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24632 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24633 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24634 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24635 {
24636 decl_specs->redefined_builtin_type = type_spec;
24637 set_and_check_decl_spec_loc (decl_specs,
24638 ds_redefined_builtin_type_spec,
24639 token);
24640 if (!decl_specs->type)
24641 {
24642 decl_specs->type = type_spec;
24643 decl_specs->type_definition_p = false;
24644 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24645 }
24646 }
24647 else if (decl_specs->type)
24648 decl_specs->multiple_types_p = true;
24649 else
24650 {
24651 decl_specs->type = type_spec;
24652 decl_specs->type_definition_p = type_definition_p;
24653 decl_specs->redefined_builtin_type = NULL_TREE;
24654 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24655 }
24656 }
24657
24658 /* True iff TOKEN is the GNU keyword __thread. */
24659
24660 static bool
24661 token_is__thread (cp_token *token)
24662 {
24663 gcc_assert (token->keyword == RID_THREAD);
24664 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24665 }
24666
24667 /* Set the location for a declarator specifier and check if it is
24668 duplicated.
24669
24670 DECL_SPECS is the sequence of declarator specifiers onto which to
24671 set the location.
24672
24673 DS is the single declarator specifier to set which location is to
24674 be set onto the existing sequence of declarators.
24675
24676 LOCATION is the location for the declarator specifier to
24677 consider. */
24678
24679 static void
24680 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24681 cp_decl_spec ds, cp_token *token)
24682 {
24683 gcc_assert (ds < ds_last);
24684
24685 if (decl_specs == NULL)
24686 return;
24687
24688 source_location location = token->location;
24689
24690 if (decl_specs->locations[ds] == 0)
24691 {
24692 decl_specs->locations[ds] = location;
24693 if (ds == ds_thread)
24694 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24695 }
24696 else
24697 {
24698 if (ds == ds_long)
24699 {
24700 if (decl_specs->locations[ds_long_long] != 0)
24701 error_at (location,
24702 "%<long long long%> is too long for GCC");
24703 else
24704 {
24705 decl_specs->locations[ds_long_long] = location;
24706 pedwarn_cxx98 (location,
24707 OPT_Wlong_long,
24708 "ISO C++ 1998 does not support %<long long%>");
24709 }
24710 }
24711 else if (ds == ds_thread)
24712 {
24713 bool gnu = token_is__thread (token);
24714 if (gnu != decl_specs->gnu_thread_keyword_p)
24715 error_at (location,
24716 "both %<__thread%> and %<thread_local%> specified");
24717 else
24718 error_at (location, "duplicate %qD", token->u.value);
24719 }
24720 else
24721 {
24722 static const char *const decl_spec_names[] = {
24723 "signed",
24724 "unsigned",
24725 "short",
24726 "long",
24727 "const",
24728 "volatile",
24729 "restrict",
24730 "inline",
24731 "virtual",
24732 "explicit",
24733 "friend",
24734 "typedef",
24735 "using",
24736 "constexpr",
24737 "__complex"
24738 };
24739 error_at (location,
24740 "duplicate %qs", decl_spec_names[ds]);
24741 }
24742 }
24743 }
24744
24745 /* Return true iff the declarator specifier DS is present in the
24746 sequence of declarator specifiers DECL_SPECS. */
24747
24748 bool
24749 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24750 cp_decl_spec ds)
24751 {
24752 gcc_assert (ds < ds_last);
24753
24754 if (decl_specs == NULL)
24755 return false;
24756
24757 return decl_specs->locations[ds] != 0;
24758 }
24759
24760 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24761 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24762
24763 static bool
24764 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24765 {
24766 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24767 }
24768
24769 /* Issue an error message indicating that TOKEN_DESC was expected.
24770 If KEYWORD is true, it indicated this function is called by
24771 cp_parser_require_keword and the required token can only be
24772 a indicated keyword. */
24773
24774 static void
24775 cp_parser_required_error (cp_parser *parser,
24776 required_token token_desc,
24777 bool keyword)
24778 {
24779 switch (token_desc)
24780 {
24781 case RT_NEW:
24782 cp_parser_error (parser, "expected %<new%>");
24783 return;
24784 case RT_DELETE:
24785 cp_parser_error (parser, "expected %<delete%>");
24786 return;
24787 case RT_RETURN:
24788 cp_parser_error (parser, "expected %<return%>");
24789 return;
24790 case RT_WHILE:
24791 cp_parser_error (parser, "expected %<while%>");
24792 return;
24793 case RT_EXTERN:
24794 cp_parser_error (parser, "expected %<extern%>");
24795 return;
24796 case RT_STATIC_ASSERT:
24797 cp_parser_error (parser, "expected %<static_assert%>");
24798 return;
24799 case RT_DECLTYPE:
24800 cp_parser_error (parser, "expected %<decltype%>");
24801 return;
24802 case RT_OPERATOR:
24803 cp_parser_error (parser, "expected %<operator%>");
24804 return;
24805 case RT_CLASS:
24806 cp_parser_error (parser, "expected %<class%>");
24807 return;
24808 case RT_TEMPLATE:
24809 cp_parser_error (parser, "expected %<template%>");
24810 return;
24811 case RT_NAMESPACE:
24812 cp_parser_error (parser, "expected %<namespace%>");
24813 return;
24814 case RT_USING:
24815 cp_parser_error (parser, "expected %<using%>");
24816 return;
24817 case RT_ASM:
24818 cp_parser_error (parser, "expected %<asm%>");
24819 return;
24820 case RT_TRY:
24821 cp_parser_error (parser, "expected %<try%>");
24822 return;
24823 case RT_CATCH:
24824 cp_parser_error (parser, "expected %<catch%>");
24825 return;
24826 case RT_THROW:
24827 cp_parser_error (parser, "expected %<throw%>");
24828 return;
24829 case RT_LABEL:
24830 cp_parser_error (parser, "expected %<__label__%>");
24831 return;
24832 case RT_AT_TRY:
24833 cp_parser_error (parser, "expected %<@try%>");
24834 return;
24835 case RT_AT_SYNCHRONIZED:
24836 cp_parser_error (parser, "expected %<@synchronized%>");
24837 return;
24838 case RT_AT_THROW:
24839 cp_parser_error (parser, "expected %<@throw%>");
24840 return;
24841 case RT_TRANSACTION_ATOMIC:
24842 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24843 return;
24844 case RT_TRANSACTION_RELAXED:
24845 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24846 return;
24847 default:
24848 break;
24849 }
24850 if (!keyword)
24851 {
24852 switch (token_desc)
24853 {
24854 case RT_SEMICOLON:
24855 cp_parser_error (parser, "expected %<;%>");
24856 return;
24857 case RT_OPEN_PAREN:
24858 cp_parser_error (parser, "expected %<(%>");
24859 return;
24860 case RT_CLOSE_BRACE:
24861 cp_parser_error (parser, "expected %<}%>");
24862 return;
24863 case RT_OPEN_BRACE:
24864 cp_parser_error (parser, "expected %<{%>");
24865 return;
24866 case RT_CLOSE_SQUARE:
24867 cp_parser_error (parser, "expected %<]%>");
24868 return;
24869 case RT_OPEN_SQUARE:
24870 cp_parser_error (parser, "expected %<[%>");
24871 return;
24872 case RT_COMMA:
24873 cp_parser_error (parser, "expected %<,%>");
24874 return;
24875 case RT_SCOPE:
24876 cp_parser_error (parser, "expected %<::%>");
24877 return;
24878 case RT_LESS:
24879 cp_parser_error (parser, "expected %<<%>");
24880 return;
24881 case RT_GREATER:
24882 cp_parser_error (parser, "expected %<>%>");
24883 return;
24884 case RT_EQ:
24885 cp_parser_error (parser, "expected %<=%>");
24886 return;
24887 case RT_ELLIPSIS:
24888 cp_parser_error (parser, "expected %<...%>");
24889 return;
24890 case RT_MULT:
24891 cp_parser_error (parser, "expected %<*%>");
24892 return;
24893 case RT_COMPL:
24894 cp_parser_error (parser, "expected %<~%>");
24895 return;
24896 case RT_COLON:
24897 cp_parser_error (parser, "expected %<:%>");
24898 return;
24899 case RT_COLON_SCOPE:
24900 cp_parser_error (parser, "expected %<:%> or %<::%>");
24901 return;
24902 case RT_CLOSE_PAREN:
24903 cp_parser_error (parser, "expected %<)%>");
24904 return;
24905 case RT_COMMA_CLOSE_PAREN:
24906 cp_parser_error (parser, "expected %<,%> or %<)%>");
24907 return;
24908 case RT_PRAGMA_EOL:
24909 cp_parser_error (parser, "expected end of line");
24910 return;
24911 case RT_NAME:
24912 cp_parser_error (parser, "expected identifier");
24913 return;
24914 case RT_SELECT:
24915 cp_parser_error (parser, "expected selection-statement");
24916 return;
24917 case RT_INTERATION:
24918 cp_parser_error (parser, "expected iteration-statement");
24919 return;
24920 case RT_JUMP:
24921 cp_parser_error (parser, "expected jump-statement");
24922 return;
24923 case RT_CLASS_KEY:
24924 cp_parser_error (parser, "expected class-key");
24925 return;
24926 case RT_CLASS_TYPENAME_TEMPLATE:
24927 cp_parser_error (parser,
24928 "expected %<class%>, %<typename%>, or %<template%>");
24929 return;
24930 default:
24931 gcc_unreachable ();
24932 }
24933 }
24934 else
24935 gcc_unreachable ();
24936 }
24937
24938
24939
24940 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24941 issue an error message indicating that TOKEN_DESC was expected.
24942
24943 Returns the token consumed, if the token had the appropriate type.
24944 Otherwise, returns NULL. */
24945
24946 static cp_token *
24947 cp_parser_require (cp_parser* parser,
24948 enum cpp_ttype type,
24949 required_token token_desc)
24950 {
24951 if (cp_lexer_next_token_is (parser->lexer, type))
24952 return cp_lexer_consume_token (parser->lexer);
24953 else
24954 {
24955 /* Output the MESSAGE -- unless we're parsing tentatively. */
24956 if (!cp_parser_simulate_error (parser))
24957 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24958 return NULL;
24959 }
24960 }
24961
24962 /* An error message is produced if the next token is not '>'.
24963 All further tokens are skipped until the desired token is
24964 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24965
24966 static void
24967 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24968 {
24969 /* Current level of '< ... >'. */
24970 unsigned level = 0;
24971 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24972 unsigned nesting_depth = 0;
24973
24974 /* Are we ready, yet? If not, issue error message. */
24975 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24976 return;
24977
24978 /* Skip tokens until the desired token is found. */
24979 while (true)
24980 {
24981 /* Peek at the next token. */
24982 switch (cp_lexer_peek_token (parser->lexer)->type)
24983 {
24984 case CPP_LESS:
24985 if (!nesting_depth)
24986 ++level;
24987 break;
24988
24989 case CPP_RSHIFT:
24990 if (cxx_dialect == cxx98)
24991 /* C++0x views the `>>' operator as two `>' tokens, but
24992 C++98 does not. */
24993 break;
24994 else if (!nesting_depth && level-- == 0)
24995 {
24996 /* We've hit a `>>' where the first `>' closes the
24997 template argument list, and the second `>' is
24998 spurious. Just consume the `>>' and stop; we've
24999 already produced at least one error. */
25000 cp_lexer_consume_token (parser->lexer);
25001 return;
25002 }
25003 /* Fall through for C++0x, so we handle the second `>' in
25004 the `>>'. */
25005
25006 case CPP_GREATER:
25007 if (!nesting_depth && level-- == 0)
25008 {
25009 /* We've reached the token we want, consume it and stop. */
25010 cp_lexer_consume_token (parser->lexer);
25011 return;
25012 }
25013 break;
25014
25015 case CPP_OPEN_PAREN:
25016 case CPP_OPEN_SQUARE:
25017 ++nesting_depth;
25018 break;
25019
25020 case CPP_CLOSE_PAREN:
25021 case CPP_CLOSE_SQUARE:
25022 if (nesting_depth-- == 0)
25023 return;
25024 break;
25025
25026 case CPP_EOF:
25027 case CPP_PRAGMA_EOL:
25028 case CPP_SEMICOLON:
25029 case CPP_OPEN_BRACE:
25030 case CPP_CLOSE_BRACE:
25031 /* The '>' was probably forgotten, don't look further. */
25032 return;
25033
25034 default:
25035 break;
25036 }
25037
25038 /* Consume this token. */
25039 cp_lexer_consume_token (parser->lexer);
25040 }
25041 }
25042
25043 /* If the next token is the indicated keyword, consume it. Otherwise,
25044 issue an error message indicating that TOKEN_DESC was expected.
25045
25046 Returns the token consumed, if the token had the appropriate type.
25047 Otherwise, returns NULL. */
25048
25049 static cp_token *
25050 cp_parser_require_keyword (cp_parser* parser,
25051 enum rid keyword,
25052 required_token token_desc)
25053 {
25054 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25055
25056 if (token && token->keyword != keyword)
25057 {
25058 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25059 return NULL;
25060 }
25061
25062 return token;
25063 }
25064
25065 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25066 function-definition. */
25067
25068 static bool
25069 cp_parser_token_starts_function_definition_p (cp_token* token)
25070 {
25071 return (/* An ordinary function-body begins with an `{'. */
25072 token->type == CPP_OPEN_BRACE
25073 /* A ctor-initializer begins with a `:'. */
25074 || token->type == CPP_COLON
25075 /* A function-try-block begins with `try'. */
25076 || token->keyword == RID_TRY
25077 /* A function-transaction-block begins with `__transaction_atomic'
25078 or `__transaction_relaxed'. */
25079 || token->keyword == RID_TRANSACTION_ATOMIC
25080 || token->keyword == RID_TRANSACTION_RELAXED
25081 /* The named return value extension begins with `return'. */
25082 || token->keyword == RID_RETURN);
25083 }
25084
25085 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25086 definition. */
25087
25088 static bool
25089 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25090 {
25091 cp_token *token;
25092
25093 token = cp_lexer_peek_token (parser->lexer);
25094 return (token->type == CPP_OPEN_BRACE
25095 || (token->type == CPP_COLON
25096 && !parser->colon_doesnt_start_class_def_p));
25097 }
25098
25099 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25100 C++0x) ending a template-argument. */
25101
25102 static bool
25103 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25104 {
25105 cp_token *token;
25106
25107 token = cp_lexer_peek_token (parser->lexer);
25108 return (token->type == CPP_COMMA
25109 || token->type == CPP_GREATER
25110 || token->type == CPP_ELLIPSIS
25111 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25112 }
25113
25114 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25115 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25116
25117 static bool
25118 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25119 size_t n)
25120 {
25121 cp_token *token;
25122
25123 token = cp_lexer_peek_nth_token (parser->lexer, n);
25124 if (token->type == CPP_LESS)
25125 return true;
25126 /* Check for the sequence `<::' in the original code. It would be lexed as
25127 `[:', where `[' is a digraph, and there is no whitespace before
25128 `:'. */
25129 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25130 {
25131 cp_token *token2;
25132 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25133 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25134 return true;
25135 }
25136 return false;
25137 }
25138
25139 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25140 or none_type otherwise. */
25141
25142 static enum tag_types
25143 cp_parser_token_is_class_key (cp_token* token)
25144 {
25145 switch (token->keyword)
25146 {
25147 case RID_CLASS:
25148 return class_type;
25149 case RID_STRUCT:
25150 return record_type;
25151 case RID_UNION:
25152 return union_type;
25153
25154 default:
25155 return none_type;
25156 }
25157 }
25158
25159 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25160 or none_type otherwise or if the token is null. */
25161
25162 static enum tag_types
25163 cp_parser_token_is_type_parameter_key (cp_token* token)
25164 {
25165 if (!token)
25166 return none_type;
25167
25168 switch (token->keyword)
25169 {
25170 case RID_CLASS:
25171 return class_type;
25172 case RID_TYPENAME:
25173 return typename_type;
25174
25175 default:
25176 return none_type;
25177 }
25178 }
25179
25180 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25181
25182 static void
25183 cp_parser_check_class_key (enum tag_types class_key, tree type)
25184 {
25185 if (type == error_mark_node)
25186 return;
25187 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25188 {
25189 if (permerror (input_location, "%qs tag used in naming %q#T",
25190 class_key == union_type ? "union"
25191 : class_key == record_type ? "struct" : "class",
25192 type))
25193 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25194 "%q#T was previously declared here", type);
25195 }
25196 }
25197
25198 /* Issue an error message if DECL is redeclared with different
25199 access than its original declaration [class.access.spec/3].
25200 This applies to nested classes and nested class templates.
25201 [class.mem/1]. */
25202
25203 static void
25204 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25205 {
25206 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25207 return;
25208
25209 if ((TREE_PRIVATE (decl)
25210 != (current_access_specifier == access_private_node))
25211 || (TREE_PROTECTED (decl)
25212 != (current_access_specifier == access_protected_node)))
25213 error_at (location, "%qD redeclared with different access", decl);
25214 }
25215
25216 /* Look for the `template' keyword, as a syntactic disambiguator.
25217 Return TRUE iff it is present, in which case it will be
25218 consumed. */
25219
25220 static bool
25221 cp_parser_optional_template_keyword (cp_parser *parser)
25222 {
25223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25224 {
25225 /* In C++98 the `template' keyword can only be used within templates;
25226 outside templates the parser can always figure out what is a
25227 template and what is not. In C++11, per the resolution of DR 468,
25228 `template' is allowed in cases where it is not strictly necessary. */
25229 if (!processing_template_decl
25230 && pedantic && cxx_dialect == cxx98)
25231 {
25232 cp_token *token = cp_lexer_peek_token (parser->lexer);
25233 pedwarn (token->location, OPT_Wpedantic,
25234 "in C++98 %<template%> (as a disambiguator) is only "
25235 "allowed within templates");
25236 /* If this part of the token stream is rescanned, the same
25237 error message would be generated. So, we purge the token
25238 from the stream. */
25239 cp_lexer_purge_token (parser->lexer);
25240 return false;
25241 }
25242 else
25243 {
25244 /* Consume the `template' keyword. */
25245 cp_lexer_consume_token (parser->lexer);
25246 return true;
25247 }
25248 }
25249 return false;
25250 }
25251
25252 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25253 set PARSER->SCOPE, and perform other related actions. */
25254
25255 static void
25256 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25257 {
25258 int i;
25259 struct tree_check *check_value;
25260 deferred_access_check *chk;
25261 vec<deferred_access_check, va_gc> *checks;
25262
25263 /* Get the stored value. */
25264 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25265 /* Perform any access checks that were deferred. */
25266 checks = check_value->checks;
25267 if (checks)
25268 {
25269 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25270 perform_or_defer_access_check (chk->binfo,
25271 chk->decl,
25272 chk->diag_decl, tf_warning_or_error);
25273 }
25274 /* Set the scope from the stored value. */
25275 parser->scope = check_value->value;
25276 parser->qualifying_scope = check_value->qualifying_scope;
25277 parser->object_scope = NULL_TREE;
25278 }
25279
25280 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25281 encounter the end of a block before what we were looking for. */
25282
25283 static bool
25284 cp_parser_cache_group (cp_parser *parser,
25285 enum cpp_ttype end,
25286 unsigned depth)
25287 {
25288 while (true)
25289 {
25290 cp_token *token = cp_lexer_peek_token (parser->lexer);
25291
25292 /* Abort a parenthesized expression if we encounter a semicolon. */
25293 if ((end == CPP_CLOSE_PAREN || depth == 0)
25294 && token->type == CPP_SEMICOLON)
25295 return true;
25296 /* If we've reached the end of the file, stop. */
25297 if (token->type == CPP_EOF
25298 || (end != CPP_PRAGMA_EOL
25299 && token->type == CPP_PRAGMA_EOL))
25300 return true;
25301 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25302 /* We've hit the end of an enclosing block, so there's been some
25303 kind of syntax error. */
25304 return true;
25305
25306 /* Consume the token. */
25307 cp_lexer_consume_token (parser->lexer);
25308 /* See if it starts a new group. */
25309 if (token->type == CPP_OPEN_BRACE)
25310 {
25311 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25312 /* In theory this should probably check end == '}', but
25313 cp_parser_save_member_function_body needs it to exit
25314 after either '}' or ')' when called with ')'. */
25315 if (depth == 0)
25316 return false;
25317 }
25318 else if (token->type == CPP_OPEN_PAREN)
25319 {
25320 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25321 if (depth == 0 && end == CPP_CLOSE_PAREN)
25322 return false;
25323 }
25324 else if (token->type == CPP_PRAGMA)
25325 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25326 else if (token->type == end)
25327 return false;
25328 }
25329 }
25330
25331 /* Like above, for caching a default argument or NSDMI. Both of these are
25332 terminated by a non-nested comma, but it can be unclear whether or not a
25333 comma is nested in a template argument list unless we do more parsing.
25334 In order to handle this ambiguity, when we encounter a ',' after a '<'
25335 we try to parse what follows as a parameter-declaration-list (in the
25336 case of a default argument) or a member-declarator (in the case of an
25337 NSDMI). If that succeeds, then we stop caching. */
25338
25339 static tree
25340 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25341 {
25342 unsigned depth = 0;
25343 int maybe_template_id = 0;
25344 cp_token *first_token;
25345 cp_token *token;
25346 tree default_argument;
25347
25348 /* Add tokens until we have processed the entire default
25349 argument. We add the range [first_token, token). */
25350 first_token = cp_lexer_peek_token (parser->lexer);
25351 if (first_token->type == CPP_OPEN_BRACE)
25352 {
25353 /* For list-initialization, this is straightforward. */
25354 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25355 token = cp_lexer_peek_token (parser->lexer);
25356 }
25357 else while (true)
25358 {
25359 bool done = false;
25360
25361 /* Peek at the next token. */
25362 token = cp_lexer_peek_token (parser->lexer);
25363 /* What we do depends on what token we have. */
25364 switch (token->type)
25365 {
25366 /* In valid code, a default argument must be
25367 immediately followed by a `,' `)', or `...'. */
25368 case CPP_COMMA:
25369 if (depth == 0 && maybe_template_id)
25370 {
25371 /* If we've seen a '<', we might be in a
25372 template-argument-list. Until Core issue 325 is
25373 resolved, we don't know how this situation ought
25374 to be handled, so try to DTRT. We check whether
25375 what comes after the comma is a valid parameter
25376 declaration list. If it is, then the comma ends
25377 the default argument; otherwise the default
25378 argument continues. */
25379 bool error = false;
25380
25381 /* Set ITALP so cp_parser_parameter_declaration_list
25382 doesn't decide to commit to this parse. */
25383 bool saved_italp = parser->in_template_argument_list_p;
25384 parser->in_template_argument_list_p = true;
25385
25386 cp_parser_parse_tentatively (parser);
25387 cp_lexer_consume_token (parser->lexer);
25388
25389 if (nsdmi)
25390 {
25391 int ctor_dtor_or_conv_p;
25392 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25393 &ctor_dtor_or_conv_p,
25394 /*parenthesized_p=*/NULL,
25395 /*member_p=*/true,
25396 /*friend_p=*/false);
25397 }
25398 else
25399 {
25400 begin_scope (sk_function_parms, NULL_TREE);
25401 cp_parser_parameter_declaration_list (parser, &error);
25402 pop_bindings_and_leave_scope ();
25403 }
25404 if (!cp_parser_error_occurred (parser) && !error)
25405 done = true;
25406 cp_parser_abort_tentative_parse (parser);
25407
25408 parser->in_template_argument_list_p = saved_italp;
25409 break;
25410 }
25411 case CPP_CLOSE_PAREN:
25412 case CPP_ELLIPSIS:
25413 /* If we run into a non-nested `;', `}', or `]',
25414 then the code is invalid -- but the default
25415 argument is certainly over. */
25416 case CPP_SEMICOLON:
25417 case CPP_CLOSE_BRACE:
25418 case CPP_CLOSE_SQUARE:
25419 if (depth == 0
25420 /* Handle correctly int n = sizeof ... ( p ); */
25421 && token->type != CPP_ELLIPSIS)
25422 done = true;
25423 /* Update DEPTH, if necessary. */
25424 else if (token->type == CPP_CLOSE_PAREN
25425 || token->type == CPP_CLOSE_BRACE
25426 || token->type == CPP_CLOSE_SQUARE)
25427 --depth;
25428 break;
25429
25430 case CPP_OPEN_PAREN:
25431 case CPP_OPEN_SQUARE:
25432 case CPP_OPEN_BRACE:
25433 ++depth;
25434 break;
25435
25436 case CPP_LESS:
25437 if (depth == 0)
25438 /* This might be the comparison operator, or it might
25439 start a template argument list. */
25440 ++maybe_template_id;
25441 break;
25442
25443 case CPP_RSHIFT:
25444 if (cxx_dialect == cxx98)
25445 break;
25446 /* Fall through for C++0x, which treats the `>>'
25447 operator like two `>' tokens in certain
25448 cases. */
25449
25450 case CPP_GREATER:
25451 if (depth == 0)
25452 {
25453 /* This might be an operator, or it might close a
25454 template argument list. But if a previous '<'
25455 started a template argument list, this will have
25456 closed it, so we can't be in one anymore. */
25457 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25458 if (maybe_template_id < 0)
25459 maybe_template_id = 0;
25460 }
25461 break;
25462
25463 /* If we run out of tokens, issue an error message. */
25464 case CPP_EOF:
25465 case CPP_PRAGMA_EOL:
25466 error_at (token->location, "file ends in default argument");
25467 done = true;
25468 break;
25469
25470 case CPP_NAME:
25471 case CPP_SCOPE:
25472 /* In these cases, we should look for template-ids.
25473 For example, if the default argument is
25474 `X<int, double>()', we need to do name lookup to
25475 figure out whether or not `X' is a template; if
25476 so, the `,' does not end the default argument.
25477
25478 That is not yet done. */
25479 break;
25480
25481 default:
25482 break;
25483 }
25484
25485 /* If we've reached the end, stop. */
25486 if (done)
25487 break;
25488
25489 /* Add the token to the token block. */
25490 token = cp_lexer_consume_token (parser->lexer);
25491 }
25492
25493 /* Create a DEFAULT_ARG to represent the unparsed default
25494 argument. */
25495 default_argument = make_node (DEFAULT_ARG);
25496 DEFARG_TOKENS (default_argument)
25497 = cp_token_cache_new (first_token, token);
25498 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25499
25500 return default_argument;
25501 }
25502
25503 /* Begin parsing tentatively. We always save tokens while parsing
25504 tentatively so that if the tentative parsing fails we can restore the
25505 tokens. */
25506
25507 static void
25508 cp_parser_parse_tentatively (cp_parser* parser)
25509 {
25510 /* Enter a new parsing context. */
25511 parser->context = cp_parser_context_new (parser->context);
25512 /* Begin saving tokens. */
25513 cp_lexer_save_tokens (parser->lexer);
25514 /* In order to avoid repetitive access control error messages,
25515 access checks are queued up until we are no longer parsing
25516 tentatively. */
25517 push_deferring_access_checks (dk_deferred);
25518 }
25519
25520 /* Commit to the currently active tentative parse. */
25521
25522 static void
25523 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25524 {
25525 cp_parser_context *context;
25526 cp_lexer *lexer;
25527
25528 /* Mark all of the levels as committed. */
25529 lexer = parser->lexer;
25530 for (context = parser->context; context->next; context = context->next)
25531 {
25532 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25533 break;
25534 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25535 while (!cp_lexer_saving_tokens (lexer))
25536 lexer = lexer->next;
25537 cp_lexer_commit_tokens (lexer);
25538 }
25539 }
25540
25541 /* Commit to the topmost currently active tentative parse.
25542
25543 Note that this function shouldn't be called when there are
25544 irreversible side-effects while in a tentative state. For
25545 example, we shouldn't create a permanent entry in the symbol
25546 table, or issue an error message that might not apply if the
25547 tentative parse is aborted. */
25548
25549 static void
25550 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25551 {
25552 cp_parser_context *context = parser->context;
25553 cp_lexer *lexer = parser->lexer;
25554
25555 if (context)
25556 {
25557 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25558 return;
25559 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25560
25561 while (!cp_lexer_saving_tokens (lexer))
25562 lexer = lexer->next;
25563 cp_lexer_commit_tokens (lexer);
25564 }
25565 }
25566
25567 /* Abort the currently active tentative parse. All consumed tokens
25568 will be rolled back, and no diagnostics will be issued. */
25569
25570 static void
25571 cp_parser_abort_tentative_parse (cp_parser* parser)
25572 {
25573 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25574 || errorcount > 0);
25575 cp_parser_simulate_error (parser);
25576 /* Now, pretend that we want to see if the construct was
25577 successfully parsed. */
25578 cp_parser_parse_definitely (parser);
25579 }
25580
25581 /* Stop parsing tentatively. If a parse error has occurred, restore the
25582 token stream. Otherwise, commit to the tokens we have consumed.
25583 Returns true if no error occurred; false otherwise. */
25584
25585 static bool
25586 cp_parser_parse_definitely (cp_parser* parser)
25587 {
25588 bool error_occurred;
25589 cp_parser_context *context;
25590
25591 /* Remember whether or not an error occurred, since we are about to
25592 destroy that information. */
25593 error_occurred = cp_parser_error_occurred (parser);
25594 /* Remove the topmost context from the stack. */
25595 context = parser->context;
25596 parser->context = context->next;
25597 /* If no parse errors occurred, commit to the tentative parse. */
25598 if (!error_occurred)
25599 {
25600 /* Commit to the tokens read tentatively, unless that was
25601 already done. */
25602 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25603 cp_lexer_commit_tokens (parser->lexer);
25604
25605 pop_to_parent_deferring_access_checks ();
25606 }
25607 /* Otherwise, if errors occurred, roll back our state so that things
25608 are just as they were before we began the tentative parse. */
25609 else
25610 {
25611 cp_lexer_rollback_tokens (parser->lexer);
25612 pop_deferring_access_checks ();
25613 }
25614 /* Add the context to the front of the free list. */
25615 context->next = cp_parser_context_free_list;
25616 cp_parser_context_free_list = context;
25617
25618 return !error_occurred;
25619 }
25620
25621 /* Returns true if we are parsing tentatively and are not committed to
25622 this tentative parse. */
25623
25624 static bool
25625 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25626 {
25627 return (cp_parser_parsing_tentatively (parser)
25628 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25629 }
25630
25631 /* Returns nonzero iff an error has occurred during the most recent
25632 tentative parse. */
25633
25634 static bool
25635 cp_parser_error_occurred (cp_parser* parser)
25636 {
25637 return (cp_parser_parsing_tentatively (parser)
25638 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25639 }
25640
25641 /* Returns nonzero if GNU extensions are allowed. */
25642
25643 static bool
25644 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25645 {
25646 return parser->allow_gnu_extensions_p;
25647 }
25648 \f
25649 /* Objective-C++ Productions */
25650
25651
25652 /* Parse an Objective-C expression, which feeds into a primary-expression
25653 above.
25654
25655 objc-expression:
25656 objc-message-expression
25657 objc-string-literal
25658 objc-encode-expression
25659 objc-protocol-expression
25660 objc-selector-expression
25661
25662 Returns a tree representation of the expression. */
25663
25664 static tree
25665 cp_parser_objc_expression (cp_parser* parser)
25666 {
25667 /* Try to figure out what kind of declaration is present. */
25668 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25669
25670 switch (kwd->type)
25671 {
25672 case CPP_OPEN_SQUARE:
25673 return cp_parser_objc_message_expression (parser);
25674
25675 case CPP_OBJC_STRING:
25676 kwd = cp_lexer_consume_token (parser->lexer);
25677 return objc_build_string_object (kwd->u.value);
25678
25679 case CPP_KEYWORD:
25680 switch (kwd->keyword)
25681 {
25682 case RID_AT_ENCODE:
25683 return cp_parser_objc_encode_expression (parser);
25684
25685 case RID_AT_PROTOCOL:
25686 return cp_parser_objc_protocol_expression (parser);
25687
25688 case RID_AT_SELECTOR:
25689 return cp_parser_objc_selector_expression (parser);
25690
25691 default:
25692 break;
25693 }
25694 default:
25695 error_at (kwd->location,
25696 "misplaced %<@%D%> Objective-C++ construct",
25697 kwd->u.value);
25698 cp_parser_skip_to_end_of_block_or_statement (parser);
25699 }
25700
25701 return error_mark_node;
25702 }
25703
25704 /* Parse an Objective-C message expression.
25705
25706 objc-message-expression:
25707 [ objc-message-receiver objc-message-args ]
25708
25709 Returns a representation of an Objective-C message. */
25710
25711 static tree
25712 cp_parser_objc_message_expression (cp_parser* parser)
25713 {
25714 tree receiver, messageargs;
25715
25716 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25717 receiver = cp_parser_objc_message_receiver (parser);
25718 messageargs = cp_parser_objc_message_args (parser);
25719 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25720
25721 return objc_build_message_expr (receiver, messageargs);
25722 }
25723
25724 /* Parse an objc-message-receiver.
25725
25726 objc-message-receiver:
25727 expression
25728 simple-type-specifier
25729
25730 Returns a representation of the type or expression. */
25731
25732 static tree
25733 cp_parser_objc_message_receiver (cp_parser* parser)
25734 {
25735 tree rcv;
25736
25737 /* An Objective-C message receiver may be either (1) a type
25738 or (2) an expression. */
25739 cp_parser_parse_tentatively (parser);
25740 rcv = cp_parser_expression (parser);
25741
25742 /* If that worked out, fine. */
25743 if (cp_parser_parse_definitely (parser))
25744 return rcv;
25745
25746 cp_parser_parse_tentatively (parser);
25747 rcv = cp_parser_simple_type_specifier (parser,
25748 /*decl_specs=*/NULL,
25749 CP_PARSER_FLAGS_NONE);
25750
25751 if (cp_parser_parse_definitely (parser))
25752 return objc_get_class_reference (rcv);
25753
25754 cp_parser_error (parser, "objective-c++ message receiver expected");
25755 return error_mark_node;
25756 }
25757
25758 /* Parse the arguments and selectors comprising an Objective-C message.
25759
25760 objc-message-args:
25761 objc-selector
25762 objc-selector-args
25763 objc-selector-args , objc-comma-args
25764
25765 objc-selector-args:
25766 objc-selector [opt] : assignment-expression
25767 objc-selector-args objc-selector [opt] : assignment-expression
25768
25769 objc-comma-args:
25770 assignment-expression
25771 objc-comma-args , assignment-expression
25772
25773 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25774 selector arguments and TREE_VALUE containing a list of comma
25775 arguments. */
25776
25777 static tree
25778 cp_parser_objc_message_args (cp_parser* parser)
25779 {
25780 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25781 bool maybe_unary_selector_p = true;
25782 cp_token *token = cp_lexer_peek_token (parser->lexer);
25783
25784 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25785 {
25786 tree selector = NULL_TREE, arg;
25787
25788 if (token->type != CPP_COLON)
25789 selector = cp_parser_objc_selector (parser);
25790
25791 /* Detect if we have a unary selector. */
25792 if (maybe_unary_selector_p
25793 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25794 return build_tree_list (selector, NULL_TREE);
25795
25796 maybe_unary_selector_p = false;
25797 cp_parser_require (parser, CPP_COLON, RT_COLON);
25798 arg = cp_parser_assignment_expression (parser);
25799
25800 sel_args
25801 = chainon (sel_args,
25802 build_tree_list (selector, arg));
25803
25804 token = cp_lexer_peek_token (parser->lexer);
25805 }
25806
25807 /* Handle non-selector arguments, if any. */
25808 while (token->type == CPP_COMMA)
25809 {
25810 tree arg;
25811
25812 cp_lexer_consume_token (parser->lexer);
25813 arg = cp_parser_assignment_expression (parser);
25814
25815 addl_args
25816 = chainon (addl_args,
25817 build_tree_list (NULL_TREE, arg));
25818
25819 token = cp_lexer_peek_token (parser->lexer);
25820 }
25821
25822 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25823 {
25824 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25825 return build_tree_list (error_mark_node, error_mark_node);
25826 }
25827
25828 return build_tree_list (sel_args, addl_args);
25829 }
25830
25831 /* Parse an Objective-C encode expression.
25832
25833 objc-encode-expression:
25834 @encode objc-typename
25835
25836 Returns an encoded representation of the type argument. */
25837
25838 static tree
25839 cp_parser_objc_encode_expression (cp_parser* parser)
25840 {
25841 tree type;
25842 cp_token *token;
25843
25844 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25845 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25846 token = cp_lexer_peek_token (parser->lexer);
25847 type = complete_type (cp_parser_type_id (parser));
25848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25849
25850 if (!type)
25851 {
25852 error_at (token->location,
25853 "%<@encode%> must specify a type as an argument");
25854 return error_mark_node;
25855 }
25856
25857 /* This happens if we find @encode(T) (where T is a template
25858 typename or something dependent on a template typename) when
25859 parsing a template. In that case, we can't compile it
25860 immediately, but we rather create an AT_ENCODE_EXPR which will
25861 need to be instantiated when the template is used.
25862 */
25863 if (dependent_type_p (type))
25864 {
25865 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25866 TREE_READONLY (value) = 1;
25867 return value;
25868 }
25869
25870 return objc_build_encode_expr (type);
25871 }
25872
25873 /* Parse an Objective-C @defs expression. */
25874
25875 static tree
25876 cp_parser_objc_defs_expression (cp_parser *parser)
25877 {
25878 tree name;
25879
25880 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25881 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25882 name = cp_parser_identifier (parser);
25883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25884
25885 return objc_get_class_ivars (name);
25886 }
25887
25888 /* Parse an Objective-C protocol expression.
25889
25890 objc-protocol-expression:
25891 @protocol ( identifier )
25892
25893 Returns a representation of the protocol expression. */
25894
25895 static tree
25896 cp_parser_objc_protocol_expression (cp_parser* parser)
25897 {
25898 tree proto;
25899
25900 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25901 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25902 proto = cp_parser_identifier (parser);
25903 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25904
25905 return objc_build_protocol_expr (proto);
25906 }
25907
25908 /* Parse an Objective-C selector expression.
25909
25910 objc-selector-expression:
25911 @selector ( objc-method-signature )
25912
25913 objc-method-signature:
25914 objc-selector
25915 objc-selector-seq
25916
25917 objc-selector-seq:
25918 objc-selector :
25919 objc-selector-seq objc-selector :
25920
25921 Returns a representation of the method selector. */
25922
25923 static tree
25924 cp_parser_objc_selector_expression (cp_parser* parser)
25925 {
25926 tree sel_seq = NULL_TREE;
25927 bool maybe_unary_selector_p = true;
25928 cp_token *token;
25929 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25930
25931 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25933 token = cp_lexer_peek_token (parser->lexer);
25934
25935 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25936 || token->type == CPP_SCOPE)
25937 {
25938 tree selector = NULL_TREE;
25939
25940 if (token->type != CPP_COLON
25941 || token->type == CPP_SCOPE)
25942 selector = cp_parser_objc_selector (parser);
25943
25944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25945 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25946 {
25947 /* Detect if we have a unary selector. */
25948 if (maybe_unary_selector_p)
25949 {
25950 sel_seq = selector;
25951 goto finish_selector;
25952 }
25953 else
25954 {
25955 cp_parser_error (parser, "expected %<:%>");
25956 }
25957 }
25958 maybe_unary_selector_p = false;
25959 token = cp_lexer_consume_token (parser->lexer);
25960
25961 if (token->type == CPP_SCOPE)
25962 {
25963 sel_seq
25964 = chainon (sel_seq,
25965 build_tree_list (selector, NULL_TREE));
25966 sel_seq
25967 = chainon (sel_seq,
25968 build_tree_list (NULL_TREE, NULL_TREE));
25969 }
25970 else
25971 sel_seq
25972 = chainon (sel_seq,
25973 build_tree_list (selector, NULL_TREE));
25974
25975 token = cp_lexer_peek_token (parser->lexer);
25976 }
25977
25978 finish_selector:
25979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25980
25981 return objc_build_selector_expr (loc, sel_seq);
25982 }
25983
25984 /* Parse a list of identifiers.
25985
25986 objc-identifier-list:
25987 identifier
25988 objc-identifier-list , identifier
25989
25990 Returns a TREE_LIST of identifier nodes. */
25991
25992 static tree
25993 cp_parser_objc_identifier_list (cp_parser* parser)
25994 {
25995 tree identifier;
25996 tree list;
25997 cp_token *sep;
25998
25999 identifier = cp_parser_identifier (parser);
26000 if (identifier == error_mark_node)
26001 return error_mark_node;
26002
26003 list = build_tree_list (NULL_TREE, identifier);
26004 sep = cp_lexer_peek_token (parser->lexer);
26005
26006 while (sep->type == CPP_COMMA)
26007 {
26008 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26009 identifier = cp_parser_identifier (parser);
26010 if (identifier == error_mark_node)
26011 return list;
26012
26013 list = chainon (list, build_tree_list (NULL_TREE,
26014 identifier));
26015 sep = cp_lexer_peek_token (parser->lexer);
26016 }
26017
26018 return list;
26019 }
26020
26021 /* Parse an Objective-C alias declaration.
26022
26023 objc-alias-declaration:
26024 @compatibility_alias identifier identifier ;
26025
26026 This function registers the alias mapping with the Objective-C front end.
26027 It returns nothing. */
26028
26029 static void
26030 cp_parser_objc_alias_declaration (cp_parser* parser)
26031 {
26032 tree alias, orig;
26033
26034 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26035 alias = cp_parser_identifier (parser);
26036 orig = cp_parser_identifier (parser);
26037 objc_declare_alias (alias, orig);
26038 cp_parser_consume_semicolon_at_end_of_statement (parser);
26039 }
26040
26041 /* Parse an Objective-C class forward-declaration.
26042
26043 objc-class-declaration:
26044 @class objc-identifier-list ;
26045
26046 The function registers the forward declarations with the Objective-C
26047 front end. It returns nothing. */
26048
26049 static void
26050 cp_parser_objc_class_declaration (cp_parser* parser)
26051 {
26052 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26053 while (true)
26054 {
26055 tree id;
26056
26057 id = cp_parser_identifier (parser);
26058 if (id == error_mark_node)
26059 break;
26060
26061 objc_declare_class (id);
26062
26063 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26064 cp_lexer_consume_token (parser->lexer);
26065 else
26066 break;
26067 }
26068 cp_parser_consume_semicolon_at_end_of_statement (parser);
26069 }
26070
26071 /* Parse a list of Objective-C protocol references.
26072
26073 objc-protocol-refs-opt:
26074 objc-protocol-refs [opt]
26075
26076 objc-protocol-refs:
26077 < objc-identifier-list >
26078
26079 Returns a TREE_LIST of identifiers, if any. */
26080
26081 static tree
26082 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26083 {
26084 tree protorefs = NULL_TREE;
26085
26086 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26087 {
26088 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26089 protorefs = cp_parser_objc_identifier_list (parser);
26090 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26091 }
26092
26093 return protorefs;
26094 }
26095
26096 /* Parse a Objective-C visibility specification. */
26097
26098 static void
26099 cp_parser_objc_visibility_spec (cp_parser* parser)
26100 {
26101 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26102
26103 switch (vis->keyword)
26104 {
26105 case RID_AT_PRIVATE:
26106 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26107 break;
26108 case RID_AT_PROTECTED:
26109 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26110 break;
26111 case RID_AT_PUBLIC:
26112 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26113 break;
26114 case RID_AT_PACKAGE:
26115 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26116 break;
26117 default:
26118 return;
26119 }
26120
26121 /* Eat '@private'/'@protected'/'@public'. */
26122 cp_lexer_consume_token (parser->lexer);
26123 }
26124
26125 /* Parse an Objective-C method type. Return 'true' if it is a class
26126 (+) method, and 'false' if it is an instance (-) method. */
26127
26128 static inline bool
26129 cp_parser_objc_method_type (cp_parser* parser)
26130 {
26131 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26132 return true;
26133 else
26134 return false;
26135 }
26136
26137 /* Parse an Objective-C protocol qualifier. */
26138
26139 static tree
26140 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26141 {
26142 tree quals = NULL_TREE, node;
26143 cp_token *token = cp_lexer_peek_token (parser->lexer);
26144
26145 node = token->u.value;
26146
26147 while (node && identifier_p (node)
26148 && (node == ridpointers [(int) RID_IN]
26149 || node == ridpointers [(int) RID_OUT]
26150 || node == ridpointers [(int) RID_INOUT]
26151 || node == ridpointers [(int) RID_BYCOPY]
26152 || node == ridpointers [(int) RID_BYREF]
26153 || node == ridpointers [(int) RID_ONEWAY]))
26154 {
26155 quals = tree_cons (NULL_TREE, node, quals);
26156 cp_lexer_consume_token (parser->lexer);
26157 token = cp_lexer_peek_token (parser->lexer);
26158 node = token->u.value;
26159 }
26160
26161 return quals;
26162 }
26163
26164 /* Parse an Objective-C typename. */
26165
26166 static tree
26167 cp_parser_objc_typename (cp_parser* parser)
26168 {
26169 tree type_name = NULL_TREE;
26170
26171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26172 {
26173 tree proto_quals, cp_type = NULL_TREE;
26174
26175 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26176 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26177
26178 /* An ObjC type name may consist of just protocol qualifiers, in which
26179 case the type shall default to 'id'. */
26180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26181 {
26182 cp_type = cp_parser_type_id (parser);
26183
26184 /* If the type could not be parsed, an error has already
26185 been produced. For error recovery, behave as if it had
26186 not been specified, which will use the default type
26187 'id'. */
26188 if (cp_type == error_mark_node)
26189 {
26190 cp_type = NULL_TREE;
26191 /* We need to skip to the closing parenthesis as
26192 cp_parser_type_id() does not seem to do it for
26193 us. */
26194 cp_parser_skip_to_closing_parenthesis (parser,
26195 /*recovering=*/true,
26196 /*or_comma=*/false,
26197 /*consume_paren=*/false);
26198 }
26199 }
26200
26201 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26202 type_name = build_tree_list (proto_quals, cp_type);
26203 }
26204
26205 return type_name;
26206 }
26207
26208 /* Check to see if TYPE refers to an Objective-C selector name. */
26209
26210 static bool
26211 cp_parser_objc_selector_p (enum cpp_ttype type)
26212 {
26213 return (type == CPP_NAME || type == CPP_KEYWORD
26214 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26215 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26216 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26217 || type == CPP_XOR || type == CPP_XOR_EQ);
26218 }
26219
26220 /* Parse an Objective-C selector. */
26221
26222 static tree
26223 cp_parser_objc_selector (cp_parser* parser)
26224 {
26225 cp_token *token = cp_lexer_consume_token (parser->lexer);
26226
26227 if (!cp_parser_objc_selector_p (token->type))
26228 {
26229 error_at (token->location, "invalid Objective-C++ selector name");
26230 return error_mark_node;
26231 }
26232
26233 /* C++ operator names are allowed to appear in ObjC selectors. */
26234 switch (token->type)
26235 {
26236 case CPP_AND_AND: return get_identifier ("and");
26237 case CPP_AND_EQ: return get_identifier ("and_eq");
26238 case CPP_AND: return get_identifier ("bitand");
26239 case CPP_OR: return get_identifier ("bitor");
26240 case CPP_COMPL: return get_identifier ("compl");
26241 case CPP_NOT: return get_identifier ("not");
26242 case CPP_NOT_EQ: return get_identifier ("not_eq");
26243 case CPP_OR_OR: return get_identifier ("or");
26244 case CPP_OR_EQ: return get_identifier ("or_eq");
26245 case CPP_XOR: return get_identifier ("xor");
26246 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26247 default: return token->u.value;
26248 }
26249 }
26250
26251 /* Parse an Objective-C params list. */
26252
26253 static tree
26254 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26255 {
26256 tree params = NULL_TREE;
26257 bool maybe_unary_selector_p = true;
26258 cp_token *token = cp_lexer_peek_token (parser->lexer);
26259
26260 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26261 {
26262 tree selector = NULL_TREE, type_name, identifier;
26263 tree parm_attr = NULL_TREE;
26264
26265 if (token->keyword == RID_ATTRIBUTE)
26266 break;
26267
26268 if (token->type != CPP_COLON)
26269 selector = cp_parser_objc_selector (parser);
26270
26271 /* Detect if we have a unary selector. */
26272 if (maybe_unary_selector_p
26273 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26274 {
26275 params = selector; /* Might be followed by attributes. */
26276 break;
26277 }
26278
26279 maybe_unary_selector_p = false;
26280 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26281 {
26282 /* Something went quite wrong. There should be a colon
26283 here, but there is not. Stop parsing parameters. */
26284 break;
26285 }
26286 type_name = cp_parser_objc_typename (parser);
26287 /* New ObjC allows attributes on parameters too. */
26288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26289 parm_attr = cp_parser_attributes_opt (parser);
26290 identifier = cp_parser_identifier (parser);
26291
26292 params
26293 = chainon (params,
26294 objc_build_keyword_decl (selector,
26295 type_name,
26296 identifier,
26297 parm_attr));
26298
26299 token = cp_lexer_peek_token (parser->lexer);
26300 }
26301
26302 if (params == NULL_TREE)
26303 {
26304 cp_parser_error (parser, "objective-c++ method declaration is expected");
26305 return error_mark_node;
26306 }
26307
26308 /* We allow tail attributes for the method. */
26309 if (token->keyword == RID_ATTRIBUTE)
26310 {
26311 *attributes = cp_parser_attributes_opt (parser);
26312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26313 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26314 return params;
26315 cp_parser_error (parser,
26316 "method attributes must be specified at the end");
26317 return error_mark_node;
26318 }
26319
26320 if (params == NULL_TREE)
26321 {
26322 cp_parser_error (parser, "objective-c++ method declaration is expected");
26323 return error_mark_node;
26324 }
26325 return params;
26326 }
26327
26328 /* Parse the non-keyword Objective-C params. */
26329
26330 static tree
26331 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26332 tree* attributes)
26333 {
26334 tree params = make_node (TREE_LIST);
26335 cp_token *token = cp_lexer_peek_token (parser->lexer);
26336 *ellipsisp = false; /* Initially, assume no ellipsis. */
26337
26338 while (token->type == CPP_COMMA)
26339 {
26340 cp_parameter_declarator *parmdecl;
26341 tree parm;
26342
26343 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26344 token = cp_lexer_peek_token (parser->lexer);
26345
26346 if (token->type == CPP_ELLIPSIS)
26347 {
26348 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26349 *ellipsisp = true;
26350 token = cp_lexer_peek_token (parser->lexer);
26351 break;
26352 }
26353
26354 /* TODO: parse attributes for tail parameters. */
26355 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26356 parm = grokdeclarator (parmdecl->declarator,
26357 &parmdecl->decl_specifiers,
26358 PARM, /*initialized=*/0,
26359 /*attrlist=*/NULL);
26360
26361 chainon (params, build_tree_list (NULL_TREE, parm));
26362 token = cp_lexer_peek_token (parser->lexer);
26363 }
26364
26365 /* We allow tail attributes for the method. */
26366 if (token->keyword == RID_ATTRIBUTE)
26367 {
26368 if (*attributes == NULL_TREE)
26369 {
26370 *attributes = cp_parser_attributes_opt (parser);
26371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26373 return params;
26374 }
26375 else
26376 /* We have an error, but parse the attributes, so that we can
26377 carry on. */
26378 *attributes = cp_parser_attributes_opt (parser);
26379
26380 cp_parser_error (parser,
26381 "method attributes must be specified at the end");
26382 return error_mark_node;
26383 }
26384
26385 return params;
26386 }
26387
26388 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26389
26390 static void
26391 cp_parser_objc_interstitial_code (cp_parser* parser)
26392 {
26393 cp_token *token = cp_lexer_peek_token (parser->lexer);
26394
26395 /* If the next token is `extern' and the following token is a string
26396 literal, then we have a linkage specification. */
26397 if (token->keyword == RID_EXTERN
26398 && cp_parser_is_pure_string_literal
26399 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26400 cp_parser_linkage_specification (parser);
26401 /* Handle #pragma, if any. */
26402 else if (token->type == CPP_PRAGMA)
26403 cp_parser_pragma (parser, pragma_objc_icode);
26404 /* Allow stray semicolons. */
26405 else if (token->type == CPP_SEMICOLON)
26406 cp_lexer_consume_token (parser->lexer);
26407 /* Mark methods as optional or required, when building protocols. */
26408 else if (token->keyword == RID_AT_OPTIONAL)
26409 {
26410 cp_lexer_consume_token (parser->lexer);
26411 objc_set_method_opt (true);
26412 }
26413 else if (token->keyword == RID_AT_REQUIRED)
26414 {
26415 cp_lexer_consume_token (parser->lexer);
26416 objc_set_method_opt (false);
26417 }
26418 else if (token->keyword == RID_NAMESPACE)
26419 cp_parser_namespace_definition (parser);
26420 /* Other stray characters must generate errors. */
26421 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26422 {
26423 cp_lexer_consume_token (parser->lexer);
26424 error ("stray %qs between Objective-C++ methods",
26425 token->type == CPP_OPEN_BRACE ? "{" : "}");
26426 }
26427 /* Finally, try to parse a block-declaration, or a function-definition. */
26428 else
26429 cp_parser_block_declaration (parser, /*statement_p=*/false);
26430 }
26431
26432 /* Parse a method signature. */
26433
26434 static tree
26435 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26436 {
26437 tree rettype, kwdparms, optparms;
26438 bool ellipsis = false;
26439 bool is_class_method;
26440
26441 is_class_method = cp_parser_objc_method_type (parser);
26442 rettype = cp_parser_objc_typename (parser);
26443 *attributes = NULL_TREE;
26444 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26445 if (kwdparms == error_mark_node)
26446 return error_mark_node;
26447 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26448 if (optparms == error_mark_node)
26449 return error_mark_node;
26450
26451 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26452 }
26453
26454 static bool
26455 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26456 {
26457 tree tattr;
26458 cp_lexer_save_tokens (parser->lexer);
26459 tattr = cp_parser_attributes_opt (parser);
26460 gcc_assert (tattr) ;
26461
26462 /* If the attributes are followed by a method introducer, this is not allowed.
26463 Dump the attributes and flag the situation. */
26464 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26465 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26466 return true;
26467
26468 /* Otherwise, the attributes introduce some interstitial code, possibly so
26469 rewind to allow that check. */
26470 cp_lexer_rollback_tokens (parser->lexer);
26471 return false;
26472 }
26473
26474 /* Parse an Objective-C method prototype list. */
26475
26476 static void
26477 cp_parser_objc_method_prototype_list (cp_parser* parser)
26478 {
26479 cp_token *token = cp_lexer_peek_token (parser->lexer);
26480
26481 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26482 {
26483 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26484 {
26485 tree attributes, sig;
26486 bool is_class_method;
26487 if (token->type == CPP_PLUS)
26488 is_class_method = true;
26489 else
26490 is_class_method = false;
26491 sig = cp_parser_objc_method_signature (parser, &attributes);
26492 if (sig == error_mark_node)
26493 {
26494 cp_parser_skip_to_end_of_block_or_statement (parser);
26495 token = cp_lexer_peek_token (parser->lexer);
26496 continue;
26497 }
26498 objc_add_method_declaration (is_class_method, sig, attributes);
26499 cp_parser_consume_semicolon_at_end_of_statement (parser);
26500 }
26501 else if (token->keyword == RID_AT_PROPERTY)
26502 cp_parser_objc_at_property_declaration (parser);
26503 else if (token->keyword == RID_ATTRIBUTE
26504 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26505 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26506 OPT_Wattributes,
26507 "prefix attributes are ignored for methods");
26508 else
26509 /* Allow for interspersed non-ObjC++ code. */
26510 cp_parser_objc_interstitial_code (parser);
26511
26512 token = cp_lexer_peek_token (parser->lexer);
26513 }
26514
26515 if (token->type != CPP_EOF)
26516 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26517 else
26518 cp_parser_error (parser, "expected %<@end%>");
26519
26520 objc_finish_interface ();
26521 }
26522
26523 /* Parse an Objective-C method definition list. */
26524
26525 static void
26526 cp_parser_objc_method_definition_list (cp_parser* parser)
26527 {
26528 cp_token *token = cp_lexer_peek_token (parser->lexer);
26529
26530 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26531 {
26532 tree meth;
26533
26534 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26535 {
26536 cp_token *ptk;
26537 tree sig, attribute;
26538 bool is_class_method;
26539 if (token->type == CPP_PLUS)
26540 is_class_method = true;
26541 else
26542 is_class_method = false;
26543 push_deferring_access_checks (dk_deferred);
26544 sig = cp_parser_objc_method_signature (parser, &attribute);
26545 if (sig == error_mark_node)
26546 {
26547 cp_parser_skip_to_end_of_block_or_statement (parser);
26548 token = cp_lexer_peek_token (parser->lexer);
26549 continue;
26550 }
26551 objc_start_method_definition (is_class_method, sig, attribute,
26552 NULL_TREE);
26553
26554 /* For historical reasons, we accept an optional semicolon. */
26555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26556 cp_lexer_consume_token (parser->lexer);
26557
26558 ptk = cp_lexer_peek_token (parser->lexer);
26559 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26560 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26561 {
26562 perform_deferred_access_checks (tf_warning_or_error);
26563 stop_deferring_access_checks ();
26564 meth = cp_parser_function_definition_after_declarator (parser,
26565 false);
26566 pop_deferring_access_checks ();
26567 objc_finish_method_definition (meth);
26568 }
26569 }
26570 /* The following case will be removed once @synthesize is
26571 completely implemented. */
26572 else if (token->keyword == RID_AT_PROPERTY)
26573 cp_parser_objc_at_property_declaration (parser);
26574 else if (token->keyword == RID_AT_SYNTHESIZE)
26575 cp_parser_objc_at_synthesize_declaration (parser);
26576 else if (token->keyword == RID_AT_DYNAMIC)
26577 cp_parser_objc_at_dynamic_declaration (parser);
26578 else if (token->keyword == RID_ATTRIBUTE
26579 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26580 warning_at (token->location, OPT_Wattributes,
26581 "prefix attributes are ignored for methods");
26582 else
26583 /* Allow for interspersed non-ObjC++ code. */
26584 cp_parser_objc_interstitial_code (parser);
26585
26586 token = cp_lexer_peek_token (parser->lexer);
26587 }
26588
26589 if (token->type != CPP_EOF)
26590 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26591 else
26592 cp_parser_error (parser, "expected %<@end%>");
26593
26594 objc_finish_implementation ();
26595 }
26596
26597 /* Parse Objective-C ivars. */
26598
26599 static void
26600 cp_parser_objc_class_ivars (cp_parser* parser)
26601 {
26602 cp_token *token = cp_lexer_peek_token (parser->lexer);
26603
26604 if (token->type != CPP_OPEN_BRACE)
26605 return; /* No ivars specified. */
26606
26607 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26608 token = cp_lexer_peek_token (parser->lexer);
26609
26610 while (token->type != CPP_CLOSE_BRACE
26611 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26612 {
26613 cp_decl_specifier_seq declspecs;
26614 int decl_class_or_enum_p;
26615 tree prefix_attributes;
26616
26617 cp_parser_objc_visibility_spec (parser);
26618
26619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26620 break;
26621
26622 cp_parser_decl_specifier_seq (parser,
26623 CP_PARSER_FLAGS_OPTIONAL,
26624 &declspecs,
26625 &decl_class_or_enum_p);
26626
26627 /* auto, register, static, extern, mutable. */
26628 if (declspecs.storage_class != sc_none)
26629 {
26630 cp_parser_error (parser, "invalid type for instance variable");
26631 declspecs.storage_class = sc_none;
26632 }
26633
26634 /* thread_local. */
26635 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26636 {
26637 cp_parser_error (parser, "invalid type for instance variable");
26638 declspecs.locations[ds_thread] = 0;
26639 }
26640
26641 /* typedef. */
26642 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26643 {
26644 cp_parser_error (parser, "invalid type for instance variable");
26645 declspecs.locations[ds_typedef] = 0;
26646 }
26647
26648 prefix_attributes = declspecs.attributes;
26649 declspecs.attributes = NULL_TREE;
26650
26651 /* Keep going until we hit the `;' at the end of the
26652 declaration. */
26653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26654 {
26655 tree width = NULL_TREE, attributes, first_attribute, decl;
26656 cp_declarator *declarator = NULL;
26657 int ctor_dtor_or_conv_p;
26658
26659 /* Check for a (possibly unnamed) bitfield declaration. */
26660 token = cp_lexer_peek_token (parser->lexer);
26661 if (token->type == CPP_COLON)
26662 goto eat_colon;
26663
26664 if (token->type == CPP_NAME
26665 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26666 == CPP_COLON))
26667 {
26668 /* Get the name of the bitfield. */
26669 declarator = make_id_declarator (NULL_TREE,
26670 cp_parser_identifier (parser),
26671 sfk_none);
26672
26673 eat_colon:
26674 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26675 /* Get the width of the bitfield. */
26676 width
26677 = cp_parser_constant_expression (parser);
26678 }
26679 else
26680 {
26681 /* Parse the declarator. */
26682 declarator
26683 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26684 &ctor_dtor_or_conv_p,
26685 /*parenthesized_p=*/NULL,
26686 /*member_p=*/false,
26687 /*friend_p=*/false);
26688 }
26689
26690 /* Look for attributes that apply to the ivar. */
26691 attributes = cp_parser_attributes_opt (parser);
26692 /* Remember which attributes are prefix attributes and
26693 which are not. */
26694 first_attribute = attributes;
26695 /* Combine the attributes. */
26696 attributes = chainon (prefix_attributes, attributes);
26697
26698 if (width)
26699 /* Create the bitfield declaration. */
26700 decl = grokbitfield (declarator, &declspecs,
26701 width,
26702 attributes);
26703 else
26704 decl = grokfield (declarator, &declspecs,
26705 NULL_TREE, /*init_const_expr_p=*/false,
26706 NULL_TREE, attributes);
26707
26708 /* Add the instance variable. */
26709 if (decl != error_mark_node && decl != NULL_TREE)
26710 objc_add_instance_variable (decl);
26711
26712 /* Reset PREFIX_ATTRIBUTES. */
26713 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26714 attributes = TREE_CHAIN (attributes);
26715 if (attributes)
26716 TREE_CHAIN (attributes) = NULL_TREE;
26717
26718 token = cp_lexer_peek_token (parser->lexer);
26719
26720 if (token->type == CPP_COMMA)
26721 {
26722 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26723 continue;
26724 }
26725 break;
26726 }
26727
26728 cp_parser_consume_semicolon_at_end_of_statement (parser);
26729 token = cp_lexer_peek_token (parser->lexer);
26730 }
26731
26732 if (token->keyword == RID_AT_END)
26733 cp_parser_error (parser, "expected %<}%>");
26734
26735 /* Do not consume the RID_AT_END, so it will be read again as terminating
26736 the @interface of @implementation. */
26737 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26738 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26739
26740 /* For historical reasons, we accept an optional semicolon. */
26741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26742 cp_lexer_consume_token (parser->lexer);
26743 }
26744
26745 /* Parse an Objective-C protocol declaration. */
26746
26747 static void
26748 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26749 {
26750 tree proto, protorefs;
26751 cp_token *tok;
26752
26753 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26755 {
26756 tok = cp_lexer_peek_token (parser->lexer);
26757 error_at (tok->location, "identifier expected after %<@protocol%>");
26758 cp_parser_consume_semicolon_at_end_of_statement (parser);
26759 return;
26760 }
26761
26762 /* See if we have a forward declaration or a definition. */
26763 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26764
26765 /* Try a forward declaration first. */
26766 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26767 {
26768 while (true)
26769 {
26770 tree id;
26771
26772 id = cp_parser_identifier (parser);
26773 if (id == error_mark_node)
26774 break;
26775
26776 objc_declare_protocol (id, attributes);
26777
26778 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26779 cp_lexer_consume_token (parser->lexer);
26780 else
26781 break;
26782 }
26783 cp_parser_consume_semicolon_at_end_of_statement (parser);
26784 }
26785
26786 /* Ok, we got a full-fledged definition (or at least should). */
26787 else
26788 {
26789 proto = cp_parser_identifier (parser);
26790 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26791 objc_start_protocol (proto, protorefs, attributes);
26792 cp_parser_objc_method_prototype_list (parser);
26793 }
26794 }
26795
26796 /* Parse an Objective-C superclass or category. */
26797
26798 static void
26799 cp_parser_objc_superclass_or_category (cp_parser *parser,
26800 bool iface_p,
26801 tree *super,
26802 tree *categ, bool *is_class_extension)
26803 {
26804 cp_token *next = cp_lexer_peek_token (parser->lexer);
26805
26806 *super = *categ = NULL_TREE;
26807 *is_class_extension = false;
26808 if (next->type == CPP_COLON)
26809 {
26810 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26811 *super = cp_parser_identifier (parser);
26812 }
26813 else if (next->type == CPP_OPEN_PAREN)
26814 {
26815 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26816
26817 /* If there is no category name, and this is an @interface, we
26818 have a class extension. */
26819 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26820 {
26821 *categ = NULL_TREE;
26822 *is_class_extension = true;
26823 }
26824 else
26825 *categ = cp_parser_identifier (parser);
26826
26827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26828 }
26829 }
26830
26831 /* Parse an Objective-C class interface. */
26832
26833 static void
26834 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26835 {
26836 tree name, super, categ, protos;
26837 bool is_class_extension;
26838
26839 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26840 name = cp_parser_identifier (parser);
26841 if (name == error_mark_node)
26842 {
26843 /* It's hard to recover because even if valid @interface stuff
26844 is to follow, we can't compile it (or validate it) if we
26845 don't even know which class it refers to. Let's assume this
26846 was a stray '@interface' token in the stream and skip it.
26847 */
26848 return;
26849 }
26850 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26851 &is_class_extension);
26852 protos = cp_parser_objc_protocol_refs_opt (parser);
26853
26854 /* We have either a class or a category on our hands. */
26855 if (categ || is_class_extension)
26856 objc_start_category_interface (name, categ, protos, attributes);
26857 else
26858 {
26859 objc_start_class_interface (name, super, protos, attributes);
26860 /* Handle instance variable declarations, if any. */
26861 cp_parser_objc_class_ivars (parser);
26862 objc_continue_interface ();
26863 }
26864
26865 cp_parser_objc_method_prototype_list (parser);
26866 }
26867
26868 /* Parse an Objective-C class implementation. */
26869
26870 static void
26871 cp_parser_objc_class_implementation (cp_parser* parser)
26872 {
26873 tree name, super, categ;
26874 bool is_class_extension;
26875
26876 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26877 name = cp_parser_identifier (parser);
26878 if (name == error_mark_node)
26879 {
26880 /* It's hard to recover because even if valid @implementation
26881 stuff is to follow, we can't compile it (or validate it) if
26882 we don't even know which class it refers to. Let's assume
26883 this was a stray '@implementation' token in the stream and
26884 skip it.
26885 */
26886 return;
26887 }
26888 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26889 &is_class_extension);
26890
26891 /* We have either a class or a category on our hands. */
26892 if (categ)
26893 objc_start_category_implementation (name, categ);
26894 else
26895 {
26896 objc_start_class_implementation (name, super);
26897 /* Handle instance variable declarations, if any. */
26898 cp_parser_objc_class_ivars (parser);
26899 objc_continue_implementation ();
26900 }
26901
26902 cp_parser_objc_method_definition_list (parser);
26903 }
26904
26905 /* Consume the @end token and finish off the implementation. */
26906
26907 static void
26908 cp_parser_objc_end_implementation (cp_parser* parser)
26909 {
26910 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26911 objc_finish_implementation ();
26912 }
26913
26914 /* Parse an Objective-C declaration. */
26915
26916 static void
26917 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26918 {
26919 /* Try to figure out what kind of declaration is present. */
26920 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26921
26922 if (attributes)
26923 switch (kwd->keyword)
26924 {
26925 case RID_AT_ALIAS:
26926 case RID_AT_CLASS:
26927 case RID_AT_END:
26928 error_at (kwd->location, "attributes may not be specified before"
26929 " the %<@%D%> Objective-C++ keyword",
26930 kwd->u.value);
26931 attributes = NULL;
26932 break;
26933 case RID_AT_IMPLEMENTATION:
26934 warning_at (kwd->location, OPT_Wattributes,
26935 "prefix attributes are ignored before %<@%D%>",
26936 kwd->u.value);
26937 attributes = NULL;
26938 default:
26939 break;
26940 }
26941
26942 switch (kwd->keyword)
26943 {
26944 case RID_AT_ALIAS:
26945 cp_parser_objc_alias_declaration (parser);
26946 break;
26947 case RID_AT_CLASS:
26948 cp_parser_objc_class_declaration (parser);
26949 break;
26950 case RID_AT_PROTOCOL:
26951 cp_parser_objc_protocol_declaration (parser, attributes);
26952 break;
26953 case RID_AT_INTERFACE:
26954 cp_parser_objc_class_interface (parser, attributes);
26955 break;
26956 case RID_AT_IMPLEMENTATION:
26957 cp_parser_objc_class_implementation (parser);
26958 break;
26959 case RID_AT_END:
26960 cp_parser_objc_end_implementation (parser);
26961 break;
26962 default:
26963 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26964 kwd->u.value);
26965 cp_parser_skip_to_end_of_block_or_statement (parser);
26966 }
26967 }
26968
26969 /* Parse an Objective-C try-catch-finally statement.
26970
26971 objc-try-catch-finally-stmt:
26972 @try compound-statement objc-catch-clause-seq [opt]
26973 objc-finally-clause [opt]
26974
26975 objc-catch-clause-seq:
26976 objc-catch-clause objc-catch-clause-seq [opt]
26977
26978 objc-catch-clause:
26979 @catch ( objc-exception-declaration ) compound-statement
26980
26981 objc-finally-clause:
26982 @finally compound-statement
26983
26984 objc-exception-declaration:
26985 parameter-declaration
26986 '...'
26987
26988 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26989
26990 Returns NULL_TREE.
26991
26992 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26993 for C. Keep them in sync. */
26994
26995 static tree
26996 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26997 {
26998 location_t location;
26999 tree stmt;
27000
27001 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27002 location = cp_lexer_peek_token (parser->lexer)->location;
27003 objc_maybe_warn_exceptions (location);
27004 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27005 node, lest it get absorbed into the surrounding block. */
27006 stmt = push_stmt_list ();
27007 cp_parser_compound_statement (parser, NULL, false, false);
27008 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27009
27010 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27011 {
27012 cp_parameter_declarator *parm;
27013 tree parameter_declaration = error_mark_node;
27014 bool seen_open_paren = false;
27015
27016 cp_lexer_consume_token (parser->lexer);
27017 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27018 seen_open_paren = true;
27019 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27020 {
27021 /* We have "@catch (...)" (where the '...' are literally
27022 what is in the code). Skip the '...'.
27023 parameter_declaration is set to NULL_TREE, and
27024 objc_being_catch_clauses() knows that that means
27025 '...'. */
27026 cp_lexer_consume_token (parser->lexer);
27027 parameter_declaration = NULL_TREE;
27028 }
27029 else
27030 {
27031 /* We have "@catch (NSException *exception)" or something
27032 like that. Parse the parameter declaration. */
27033 parm = cp_parser_parameter_declaration (parser, false, NULL);
27034 if (parm == NULL)
27035 parameter_declaration = error_mark_node;
27036 else
27037 parameter_declaration = grokdeclarator (parm->declarator,
27038 &parm->decl_specifiers,
27039 PARM, /*initialized=*/0,
27040 /*attrlist=*/NULL);
27041 }
27042 if (seen_open_paren)
27043 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27044 else
27045 {
27046 /* If there was no open parenthesis, we are recovering from
27047 an error, and we are trying to figure out what mistake
27048 the user has made. */
27049
27050 /* If there is an immediate closing parenthesis, the user
27051 probably forgot the opening one (ie, they typed "@catch
27052 NSException *e)". Parse the closing parenthesis and keep
27053 going. */
27054 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27055 cp_lexer_consume_token (parser->lexer);
27056
27057 /* If these is no immediate closing parenthesis, the user
27058 probably doesn't know that parenthesis are required at
27059 all (ie, they typed "@catch NSException *e"). So, just
27060 forget about the closing parenthesis and keep going. */
27061 }
27062 objc_begin_catch_clause (parameter_declaration);
27063 cp_parser_compound_statement (parser, NULL, false, false);
27064 objc_finish_catch_clause ();
27065 }
27066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27067 {
27068 cp_lexer_consume_token (parser->lexer);
27069 location = cp_lexer_peek_token (parser->lexer)->location;
27070 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27071 node, lest it get absorbed into the surrounding block. */
27072 stmt = push_stmt_list ();
27073 cp_parser_compound_statement (parser, NULL, false, false);
27074 objc_build_finally_clause (location, pop_stmt_list (stmt));
27075 }
27076
27077 return objc_finish_try_stmt ();
27078 }
27079
27080 /* Parse an Objective-C synchronized statement.
27081
27082 objc-synchronized-stmt:
27083 @synchronized ( expression ) compound-statement
27084
27085 Returns NULL_TREE. */
27086
27087 static tree
27088 cp_parser_objc_synchronized_statement (cp_parser *parser)
27089 {
27090 location_t location;
27091 tree lock, stmt;
27092
27093 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27094
27095 location = cp_lexer_peek_token (parser->lexer)->location;
27096 objc_maybe_warn_exceptions (location);
27097 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27098 lock = cp_parser_expression (parser);
27099 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27100
27101 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27102 node, lest it get absorbed into the surrounding block. */
27103 stmt = push_stmt_list ();
27104 cp_parser_compound_statement (parser, NULL, false, false);
27105
27106 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27107 }
27108
27109 /* Parse an Objective-C throw statement.
27110
27111 objc-throw-stmt:
27112 @throw assignment-expression [opt] ;
27113
27114 Returns a constructed '@throw' statement. */
27115
27116 static tree
27117 cp_parser_objc_throw_statement (cp_parser *parser)
27118 {
27119 tree expr = NULL_TREE;
27120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27121
27122 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27123
27124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27125 expr = cp_parser_expression (parser);
27126
27127 cp_parser_consume_semicolon_at_end_of_statement (parser);
27128
27129 return objc_build_throw_stmt (loc, expr);
27130 }
27131
27132 /* Parse an Objective-C statement. */
27133
27134 static tree
27135 cp_parser_objc_statement (cp_parser * parser)
27136 {
27137 /* Try to figure out what kind of declaration is present. */
27138 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27139
27140 switch (kwd->keyword)
27141 {
27142 case RID_AT_TRY:
27143 return cp_parser_objc_try_catch_finally_statement (parser);
27144 case RID_AT_SYNCHRONIZED:
27145 return cp_parser_objc_synchronized_statement (parser);
27146 case RID_AT_THROW:
27147 return cp_parser_objc_throw_statement (parser);
27148 default:
27149 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27150 kwd->u.value);
27151 cp_parser_skip_to_end_of_block_or_statement (parser);
27152 }
27153
27154 return error_mark_node;
27155 }
27156
27157 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27158 look ahead to see if an objc keyword follows the attributes. This
27159 is to detect the use of prefix attributes on ObjC @interface and
27160 @protocol. */
27161
27162 static bool
27163 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27164 {
27165 cp_lexer_save_tokens (parser->lexer);
27166 *attrib = cp_parser_attributes_opt (parser);
27167 gcc_assert (*attrib);
27168 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27169 {
27170 cp_lexer_commit_tokens (parser->lexer);
27171 return true;
27172 }
27173 cp_lexer_rollback_tokens (parser->lexer);
27174 return false;
27175 }
27176
27177 /* This routine is a minimal replacement for
27178 c_parser_struct_declaration () used when parsing the list of
27179 types/names or ObjC++ properties. For example, when parsing the
27180 code
27181
27182 @property (readonly) int a, b, c;
27183
27184 this function is responsible for parsing "int a, int b, int c" and
27185 returning the declarations as CHAIN of DECLs.
27186
27187 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27188 similar parsing. */
27189 static tree
27190 cp_parser_objc_struct_declaration (cp_parser *parser)
27191 {
27192 tree decls = NULL_TREE;
27193 cp_decl_specifier_seq declspecs;
27194 int decl_class_or_enum_p;
27195 tree prefix_attributes;
27196
27197 cp_parser_decl_specifier_seq (parser,
27198 CP_PARSER_FLAGS_NONE,
27199 &declspecs,
27200 &decl_class_or_enum_p);
27201
27202 if (declspecs.type == error_mark_node)
27203 return error_mark_node;
27204
27205 /* auto, register, static, extern, mutable. */
27206 if (declspecs.storage_class != sc_none)
27207 {
27208 cp_parser_error (parser, "invalid type for property");
27209 declspecs.storage_class = sc_none;
27210 }
27211
27212 /* thread_local. */
27213 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27214 {
27215 cp_parser_error (parser, "invalid type for property");
27216 declspecs.locations[ds_thread] = 0;
27217 }
27218
27219 /* typedef. */
27220 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27221 {
27222 cp_parser_error (parser, "invalid type for property");
27223 declspecs.locations[ds_typedef] = 0;
27224 }
27225
27226 prefix_attributes = declspecs.attributes;
27227 declspecs.attributes = NULL_TREE;
27228
27229 /* Keep going until we hit the `;' at the end of the declaration. */
27230 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27231 {
27232 tree attributes, first_attribute, decl;
27233 cp_declarator *declarator;
27234 cp_token *token;
27235
27236 /* Parse the declarator. */
27237 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27238 NULL, NULL, false, false);
27239
27240 /* Look for attributes that apply to the ivar. */
27241 attributes = cp_parser_attributes_opt (parser);
27242 /* Remember which attributes are prefix attributes and
27243 which are not. */
27244 first_attribute = attributes;
27245 /* Combine the attributes. */
27246 attributes = chainon (prefix_attributes, attributes);
27247
27248 decl = grokfield (declarator, &declspecs,
27249 NULL_TREE, /*init_const_expr_p=*/false,
27250 NULL_TREE, attributes);
27251
27252 if (decl == error_mark_node || decl == NULL_TREE)
27253 return error_mark_node;
27254
27255 /* Reset PREFIX_ATTRIBUTES. */
27256 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27257 attributes = TREE_CHAIN (attributes);
27258 if (attributes)
27259 TREE_CHAIN (attributes) = NULL_TREE;
27260
27261 DECL_CHAIN (decl) = decls;
27262 decls = decl;
27263
27264 token = cp_lexer_peek_token (parser->lexer);
27265 if (token->type == CPP_COMMA)
27266 {
27267 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27268 continue;
27269 }
27270 else
27271 break;
27272 }
27273 return decls;
27274 }
27275
27276 /* Parse an Objective-C @property declaration. The syntax is:
27277
27278 objc-property-declaration:
27279 '@property' objc-property-attributes[opt] struct-declaration ;
27280
27281 objc-property-attributes:
27282 '(' objc-property-attribute-list ')'
27283
27284 objc-property-attribute-list:
27285 objc-property-attribute
27286 objc-property-attribute-list, objc-property-attribute
27287
27288 objc-property-attribute
27289 'getter' = identifier
27290 'setter' = identifier
27291 'readonly'
27292 'readwrite'
27293 'assign'
27294 'retain'
27295 'copy'
27296 'nonatomic'
27297
27298 For example:
27299 @property NSString *name;
27300 @property (readonly) id object;
27301 @property (retain, nonatomic, getter=getTheName) id name;
27302 @property int a, b, c;
27303
27304 PS: This function is identical to
27305 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27306 static void
27307 cp_parser_objc_at_property_declaration (cp_parser *parser)
27308 {
27309 /* The following variables hold the attributes of the properties as
27310 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27311 seen. When we see an attribute, we set them to 'true' (if they
27312 are boolean properties) or to the identifier (if they have an
27313 argument, ie, for getter and setter). Note that here we only
27314 parse the list of attributes, check the syntax and accumulate the
27315 attributes that we find. objc_add_property_declaration() will
27316 then process the information. */
27317 bool property_assign = false;
27318 bool property_copy = false;
27319 tree property_getter_ident = NULL_TREE;
27320 bool property_nonatomic = false;
27321 bool property_readonly = false;
27322 bool property_readwrite = false;
27323 bool property_retain = false;
27324 tree property_setter_ident = NULL_TREE;
27325
27326 /* 'properties' is the list of properties that we read. Usually a
27327 single one, but maybe more (eg, in "@property int a, b, c;" there
27328 are three). */
27329 tree properties;
27330 location_t loc;
27331
27332 loc = cp_lexer_peek_token (parser->lexer)->location;
27333
27334 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27335
27336 /* Parse the optional attribute list... */
27337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27338 {
27339 /* Eat the '('. */
27340 cp_lexer_consume_token (parser->lexer);
27341
27342 while (true)
27343 {
27344 bool syntax_error = false;
27345 cp_token *token = cp_lexer_peek_token (parser->lexer);
27346 enum rid keyword;
27347
27348 if (token->type != CPP_NAME)
27349 {
27350 cp_parser_error (parser, "expected identifier");
27351 break;
27352 }
27353 keyword = C_RID_CODE (token->u.value);
27354 cp_lexer_consume_token (parser->lexer);
27355 switch (keyword)
27356 {
27357 case RID_ASSIGN: property_assign = true; break;
27358 case RID_COPY: property_copy = true; break;
27359 case RID_NONATOMIC: property_nonatomic = true; break;
27360 case RID_READONLY: property_readonly = true; break;
27361 case RID_READWRITE: property_readwrite = true; break;
27362 case RID_RETAIN: property_retain = true; break;
27363
27364 case RID_GETTER:
27365 case RID_SETTER:
27366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27367 {
27368 if (keyword == RID_GETTER)
27369 cp_parser_error (parser,
27370 "missing %<=%> (after %<getter%> attribute)");
27371 else
27372 cp_parser_error (parser,
27373 "missing %<=%> (after %<setter%> attribute)");
27374 syntax_error = true;
27375 break;
27376 }
27377 cp_lexer_consume_token (parser->lexer); /* eat the = */
27378 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27379 {
27380 cp_parser_error (parser, "expected identifier");
27381 syntax_error = true;
27382 break;
27383 }
27384 if (keyword == RID_SETTER)
27385 {
27386 if (property_setter_ident != NULL_TREE)
27387 {
27388 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27389 cp_lexer_consume_token (parser->lexer);
27390 }
27391 else
27392 property_setter_ident = cp_parser_objc_selector (parser);
27393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27394 cp_parser_error (parser, "setter name must terminate with %<:%>");
27395 else
27396 cp_lexer_consume_token (parser->lexer);
27397 }
27398 else
27399 {
27400 if (property_getter_ident != NULL_TREE)
27401 {
27402 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27403 cp_lexer_consume_token (parser->lexer);
27404 }
27405 else
27406 property_getter_ident = cp_parser_objc_selector (parser);
27407 }
27408 break;
27409 default:
27410 cp_parser_error (parser, "unknown property attribute");
27411 syntax_error = true;
27412 break;
27413 }
27414
27415 if (syntax_error)
27416 break;
27417
27418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27419 cp_lexer_consume_token (parser->lexer);
27420 else
27421 break;
27422 }
27423
27424 /* FIXME: "@property (setter, assign);" will generate a spurious
27425 "error: expected ‘)’ before ‘,’ token". This is because
27426 cp_parser_require, unlike the C counterpart, will produce an
27427 error even if we are in error recovery. */
27428 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27429 {
27430 cp_parser_skip_to_closing_parenthesis (parser,
27431 /*recovering=*/true,
27432 /*or_comma=*/false,
27433 /*consume_paren=*/true);
27434 }
27435 }
27436
27437 /* ... and the property declaration(s). */
27438 properties = cp_parser_objc_struct_declaration (parser);
27439
27440 if (properties == error_mark_node)
27441 {
27442 cp_parser_skip_to_end_of_statement (parser);
27443 /* If the next token is now a `;', consume it. */
27444 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27445 cp_lexer_consume_token (parser->lexer);
27446 return;
27447 }
27448
27449 if (properties == NULL_TREE)
27450 cp_parser_error (parser, "expected identifier");
27451 else
27452 {
27453 /* Comma-separated properties are chained together in
27454 reverse order; add them one by one. */
27455 properties = nreverse (properties);
27456
27457 for (; properties; properties = TREE_CHAIN (properties))
27458 objc_add_property_declaration (loc, copy_node (properties),
27459 property_readonly, property_readwrite,
27460 property_assign, property_retain,
27461 property_copy, property_nonatomic,
27462 property_getter_ident, property_setter_ident);
27463 }
27464
27465 cp_parser_consume_semicolon_at_end_of_statement (parser);
27466 }
27467
27468 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27469
27470 objc-synthesize-declaration:
27471 @synthesize objc-synthesize-identifier-list ;
27472
27473 objc-synthesize-identifier-list:
27474 objc-synthesize-identifier
27475 objc-synthesize-identifier-list, objc-synthesize-identifier
27476
27477 objc-synthesize-identifier
27478 identifier
27479 identifier = identifier
27480
27481 For example:
27482 @synthesize MyProperty;
27483 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27484
27485 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27486 for C. Keep them in sync.
27487 */
27488 static void
27489 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27490 {
27491 tree list = NULL_TREE;
27492 location_t loc;
27493 loc = cp_lexer_peek_token (parser->lexer)->location;
27494
27495 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27496 while (true)
27497 {
27498 tree property, ivar;
27499 property = cp_parser_identifier (parser);
27500 if (property == error_mark_node)
27501 {
27502 cp_parser_consume_semicolon_at_end_of_statement (parser);
27503 return;
27504 }
27505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27506 {
27507 cp_lexer_consume_token (parser->lexer);
27508 ivar = cp_parser_identifier (parser);
27509 if (ivar == error_mark_node)
27510 {
27511 cp_parser_consume_semicolon_at_end_of_statement (parser);
27512 return;
27513 }
27514 }
27515 else
27516 ivar = NULL_TREE;
27517 list = chainon (list, build_tree_list (ivar, property));
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27519 cp_lexer_consume_token (parser->lexer);
27520 else
27521 break;
27522 }
27523 cp_parser_consume_semicolon_at_end_of_statement (parser);
27524 objc_add_synthesize_declaration (loc, list);
27525 }
27526
27527 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27528
27529 objc-dynamic-declaration:
27530 @dynamic identifier-list ;
27531
27532 For example:
27533 @dynamic MyProperty;
27534 @dynamic MyProperty, AnotherProperty;
27535
27536 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27537 for C. Keep them in sync.
27538 */
27539 static void
27540 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27541 {
27542 tree list = NULL_TREE;
27543 location_t loc;
27544 loc = cp_lexer_peek_token (parser->lexer)->location;
27545
27546 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27547 while (true)
27548 {
27549 tree property;
27550 property = cp_parser_identifier (parser);
27551 if (property == error_mark_node)
27552 {
27553 cp_parser_consume_semicolon_at_end_of_statement (parser);
27554 return;
27555 }
27556 list = chainon (list, build_tree_list (NULL, property));
27557 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27558 cp_lexer_consume_token (parser->lexer);
27559 else
27560 break;
27561 }
27562 cp_parser_consume_semicolon_at_end_of_statement (parser);
27563 objc_add_dynamic_declaration (loc, list);
27564 }
27565
27566 \f
27567 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27568
27569 /* Returns name of the next clause.
27570 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27571 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27572 returned and the token is consumed. */
27573
27574 static pragma_omp_clause
27575 cp_parser_omp_clause_name (cp_parser *parser)
27576 {
27577 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27578
27579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27580 result = PRAGMA_OMP_CLAUSE_IF;
27581 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27582 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27583 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27584 result = PRAGMA_OACC_CLAUSE_DELETE;
27585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27586 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27588 result = PRAGMA_OMP_CLAUSE_FOR;
27589 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27590 {
27591 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27592 const char *p = IDENTIFIER_POINTER (id);
27593
27594 switch (p[0])
27595 {
27596 case 'a':
27597 if (!strcmp ("aligned", p))
27598 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27599 else if (!strcmp ("async", p))
27600 result = PRAGMA_OACC_CLAUSE_ASYNC;
27601 break;
27602 case 'c':
27603 if (!strcmp ("collapse", p))
27604 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27605 else if (!strcmp ("copy", p))
27606 result = PRAGMA_OACC_CLAUSE_COPY;
27607 else if (!strcmp ("copyin", p))
27608 result = PRAGMA_OMP_CLAUSE_COPYIN;
27609 else if (!strcmp ("copyout", p))
27610 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27611 else if (!strcmp ("copyprivate", p))
27612 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27613 else if (!strcmp ("create", p))
27614 result = PRAGMA_OACC_CLAUSE_CREATE;
27615 break;
27616 case 'd':
27617 if (!strcmp ("depend", p))
27618 result = PRAGMA_OMP_CLAUSE_DEPEND;
27619 else if (!strcmp ("device", p))
27620 result = PRAGMA_OMP_CLAUSE_DEVICE;
27621 else if (!strcmp ("deviceptr", p))
27622 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27623 else if (!strcmp ("dist_schedule", p))
27624 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27625 break;
27626 case 'f':
27627 if (!strcmp ("final", p))
27628 result = PRAGMA_OMP_CLAUSE_FINAL;
27629 else if (!strcmp ("firstprivate", p))
27630 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27631 else if (!strcmp ("from", p))
27632 result = PRAGMA_OMP_CLAUSE_FROM;
27633 break;
27634 case 'h':
27635 if (!strcmp ("host", p))
27636 result = PRAGMA_OACC_CLAUSE_HOST;
27637 break;
27638 case 'i':
27639 if (!strcmp ("inbranch", p))
27640 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27641 break;
27642 case 'l':
27643 if (!strcmp ("lastprivate", p))
27644 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27645 else if (!strcmp ("linear", p))
27646 result = PRAGMA_OMP_CLAUSE_LINEAR;
27647 break;
27648 case 'm':
27649 if (!strcmp ("map", p))
27650 result = PRAGMA_OMP_CLAUSE_MAP;
27651 else if (!strcmp ("mergeable", p))
27652 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27653 else if (flag_cilkplus && !strcmp ("mask", p))
27654 result = PRAGMA_CILK_CLAUSE_MASK;
27655 break;
27656 case 'n':
27657 if (!strcmp ("notinbranch", p))
27658 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27659 else if (!strcmp ("nowait", p))
27660 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27661 else if (flag_cilkplus && !strcmp ("nomask", p))
27662 result = PRAGMA_CILK_CLAUSE_NOMASK;
27663 else if (!strcmp ("num_gangs", p))
27664 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27665 else if (!strcmp ("num_teams", p))
27666 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27667 else if (!strcmp ("num_threads", p))
27668 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27669 else if (!strcmp ("num_workers", p))
27670 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27671 break;
27672 case 'o':
27673 if (!strcmp ("ordered", p))
27674 result = PRAGMA_OMP_CLAUSE_ORDERED;
27675 break;
27676 case 'p':
27677 if (!strcmp ("parallel", p))
27678 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27679 else if (!strcmp ("present", p))
27680 result = PRAGMA_OACC_CLAUSE_PRESENT;
27681 else if (!strcmp ("present_or_copy", p)
27682 || !strcmp ("pcopy", p))
27683 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27684 else if (!strcmp ("present_or_copyin", p)
27685 || !strcmp ("pcopyin", p))
27686 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27687 else if (!strcmp ("present_or_copyout", p)
27688 || !strcmp ("pcopyout", p))
27689 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27690 else if (!strcmp ("present_or_create", p)
27691 || !strcmp ("pcreate", p))
27692 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27693 else if (!strcmp ("proc_bind", p))
27694 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27695 break;
27696 case 'r':
27697 if (!strcmp ("reduction", p))
27698 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27699 break;
27700 case 's':
27701 if (!strcmp ("safelen", p))
27702 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27703 else if (!strcmp ("schedule", p))
27704 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27705 else if (!strcmp ("sections", p))
27706 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27707 else if (!strcmp ("self", p))
27708 result = PRAGMA_OACC_CLAUSE_SELF;
27709 else if (!strcmp ("shared", p))
27710 result = PRAGMA_OMP_CLAUSE_SHARED;
27711 else if (!strcmp ("simdlen", p))
27712 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27713 break;
27714 case 't':
27715 if (!strcmp ("taskgroup", p))
27716 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27717 else if (!strcmp ("thread_limit", p))
27718 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27719 else if (!strcmp ("to", p))
27720 result = PRAGMA_OMP_CLAUSE_TO;
27721 break;
27722 case 'u':
27723 if (!strcmp ("uniform", p))
27724 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27725 else if (!strcmp ("untied", p))
27726 result = PRAGMA_OMP_CLAUSE_UNTIED;
27727 break;
27728 case 'v':
27729 if (!strcmp ("vector_length", p))
27730 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27731 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27732 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27733 break;
27734 case 'w':
27735 if (!strcmp ("wait", p))
27736 result = PRAGMA_OACC_CLAUSE_WAIT;
27737 break;
27738 }
27739 }
27740
27741 if (result != PRAGMA_OMP_CLAUSE_NONE)
27742 cp_lexer_consume_token (parser->lexer);
27743
27744 return result;
27745 }
27746
27747 /* Validate that a clause of the given type does not already exist. */
27748
27749 static void
27750 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27751 const char *name, location_t location)
27752 {
27753 tree c;
27754
27755 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27756 if (OMP_CLAUSE_CODE (c) == code)
27757 {
27758 error_at (location, "too many %qs clauses", name);
27759 break;
27760 }
27761 }
27762
27763 /* OpenMP 2.5:
27764 variable-list:
27765 identifier
27766 variable-list , identifier
27767
27768 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27769 colon). An opening parenthesis will have been consumed by the caller.
27770
27771 If KIND is nonzero, create the appropriate node and install the decl
27772 in OMP_CLAUSE_DECL and add the node to the head of the list.
27773
27774 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27775 return the list created.
27776
27777 COLON can be NULL if only closing parenthesis should end the list,
27778 or pointer to bool which will receive false if the list is terminated
27779 by closing parenthesis or true if the list is terminated by colon. */
27780
27781 static tree
27782 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27783 tree list, bool *colon)
27784 {
27785 cp_token *token;
27786 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27787 if (colon)
27788 {
27789 parser->colon_corrects_to_scope_p = false;
27790 *colon = false;
27791 }
27792 while (1)
27793 {
27794 tree name, decl;
27795
27796 token = cp_lexer_peek_token (parser->lexer);
27797 name = cp_parser_id_expression (parser, /*template_p=*/false,
27798 /*check_dependency_p=*/true,
27799 /*template_p=*/NULL,
27800 /*declarator_p=*/false,
27801 /*optional_p=*/false);
27802 if (name == error_mark_node)
27803 goto skip_comma;
27804
27805 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27806 if (decl == error_mark_node)
27807 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27808 token->location);
27809 else if (kind != 0)
27810 {
27811 switch (kind)
27812 {
27813 case OMP_CLAUSE__CACHE_:
27814 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27815 {
27816 error_at (token->location, "expected %<[%>");
27817 decl = error_mark_node;
27818 break;
27819 }
27820 /* FALL THROUGH. */
27821 case OMP_CLAUSE_MAP:
27822 case OMP_CLAUSE_FROM:
27823 case OMP_CLAUSE_TO:
27824 case OMP_CLAUSE_DEPEND:
27825 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27826 {
27827 tree low_bound = NULL_TREE, length = NULL_TREE;
27828
27829 parser->colon_corrects_to_scope_p = false;
27830 cp_lexer_consume_token (parser->lexer);
27831 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27832 low_bound = cp_parser_expression (parser);
27833 if (!colon)
27834 parser->colon_corrects_to_scope_p
27835 = saved_colon_corrects_to_scope_p;
27836 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27837 length = integer_one_node;
27838 else
27839 {
27840 /* Look for `:'. */
27841 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27842 goto skip_comma;
27843 if (!cp_lexer_next_token_is (parser->lexer,
27844 CPP_CLOSE_SQUARE))
27845 length = cp_parser_expression (parser);
27846 }
27847 /* Look for the closing `]'. */
27848 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27849 RT_CLOSE_SQUARE))
27850 goto skip_comma;
27851
27852 if (kind == OMP_CLAUSE__CACHE_)
27853 {
27854 if (TREE_CODE (low_bound) != INTEGER_CST
27855 && !TREE_READONLY (low_bound))
27856 {
27857 error_at (token->location,
27858 "%qD is not a constant", low_bound);
27859 decl = error_mark_node;
27860 }
27861
27862 if (TREE_CODE (length) != INTEGER_CST
27863 && !TREE_READONLY (length))
27864 {
27865 error_at (token->location,
27866 "%qD is not a constant", length);
27867 decl = error_mark_node;
27868 }
27869 }
27870
27871 decl = tree_cons (low_bound, length, decl);
27872 }
27873 break;
27874 default:
27875 break;
27876 }
27877
27878 tree u = build_omp_clause (token->location, kind);
27879 OMP_CLAUSE_DECL (u) = decl;
27880 OMP_CLAUSE_CHAIN (u) = list;
27881 list = u;
27882 }
27883 else
27884 list = tree_cons (decl, NULL_TREE, list);
27885
27886 get_comma:
27887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27888 break;
27889 cp_lexer_consume_token (parser->lexer);
27890 }
27891
27892 if (colon)
27893 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27894
27895 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27896 {
27897 *colon = true;
27898 cp_parser_require (parser, CPP_COLON, RT_COLON);
27899 return list;
27900 }
27901
27902 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27903 {
27904 int ending;
27905
27906 /* Try to resync to an unnested comma. Copied from
27907 cp_parser_parenthesized_expression_list. */
27908 skip_comma:
27909 if (colon)
27910 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27911 ending = cp_parser_skip_to_closing_parenthesis (parser,
27912 /*recovering=*/true,
27913 /*or_comma=*/true,
27914 /*consume_paren=*/true);
27915 if (ending < 0)
27916 goto get_comma;
27917 }
27918
27919 return list;
27920 }
27921
27922 /* Similarly, but expect leading and trailing parenthesis. This is a very
27923 common case for omp clauses. */
27924
27925 static tree
27926 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27927 {
27928 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27929 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27930 return list;
27931 }
27932
27933 /* OpenACC 2.0:
27934 copy ( variable-list )
27935 copyin ( variable-list )
27936 copyout ( variable-list )
27937 create ( variable-list )
27938 delete ( variable-list )
27939 present ( variable-list )
27940 present_or_copy ( variable-list )
27941 pcopy ( variable-list )
27942 present_or_copyin ( variable-list )
27943 pcopyin ( variable-list )
27944 present_or_copyout ( variable-list )
27945 pcopyout ( variable-list )
27946 present_or_create ( variable-list )
27947 pcreate ( variable-list ) */
27948
27949 static tree
27950 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27951 tree list)
27952 {
27953 enum gomp_map_kind kind;
27954 switch (c_kind)
27955 {
27956 case PRAGMA_OACC_CLAUSE_COPY:
27957 kind = GOMP_MAP_FORCE_TOFROM;
27958 break;
27959 case PRAGMA_OACC_CLAUSE_COPYIN:
27960 kind = GOMP_MAP_FORCE_TO;
27961 break;
27962 case PRAGMA_OACC_CLAUSE_COPYOUT:
27963 kind = GOMP_MAP_FORCE_FROM;
27964 break;
27965 case PRAGMA_OACC_CLAUSE_CREATE:
27966 kind = GOMP_MAP_FORCE_ALLOC;
27967 break;
27968 case PRAGMA_OACC_CLAUSE_DELETE:
27969 kind = GOMP_MAP_FORCE_DEALLOC;
27970 break;
27971 case PRAGMA_OACC_CLAUSE_DEVICE:
27972 kind = GOMP_MAP_FORCE_TO;
27973 break;
27974 case PRAGMA_OACC_CLAUSE_HOST:
27975 case PRAGMA_OACC_CLAUSE_SELF:
27976 kind = GOMP_MAP_FORCE_FROM;
27977 break;
27978 case PRAGMA_OACC_CLAUSE_PRESENT:
27979 kind = GOMP_MAP_FORCE_PRESENT;
27980 break;
27981 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27982 kind = GOMP_MAP_TOFROM;
27983 break;
27984 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27985 kind = GOMP_MAP_TO;
27986 break;
27987 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27988 kind = GOMP_MAP_FROM;
27989 break;
27990 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27991 kind = GOMP_MAP_ALLOC;
27992 break;
27993 default:
27994 gcc_unreachable ();
27995 }
27996 tree nl, c;
27997 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27998
27999 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28000 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28001
28002 return nl;
28003 }
28004
28005 /* OpenACC 2.0:
28006 deviceptr ( variable-list ) */
28007
28008 static tree
28009 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28010 {
28011 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28012 tree vars, t;
28013
28014 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28015 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28016 variable-list must only allow for pointer variables. */
28017 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28018 for (t = vars; t; t = TREE_CHAIN (t))
28019 {
28020 tree v = TREE_PURPOSE (t);
28021
28022 /* FIXME diagnostics: Ideally we should keep individual
28023 locations for all the variables in the var list to make the
28024 following errors more precise. Perhaps
28025 c_parser_omp_var_list_parens should construct a list of
28026 locations to go along with the var list. */
28027
28028 if (TREE_CODE (v) != VAR_DECL)
28029 error_at (loc, "%qD is not a variable", v);
28030 else if (TREE_TYPE (v) == error_mark_node)
28031 ;
28032 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28033 error_at (loc, "%qD is not a pointer variable", v);
28034
28035 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28036 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28037 OMP_CLAUSE_DECL (u) = v;
28038 OMP_CLAUSE_CHAIN (u) = list;
28039 list = u;
28040 }
28041
28042 return list;
28043 }
28044
28045 /* OpenACC:
28046 vector_length ( expression ) */
28047
28048 static tree
28049 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28050 {
28051 tree t, c;
28052 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28053 bool error = false;
28054
28055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28056 return list;
28057
28058 t = cp_parser_condition (parser);
28059 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28060 {
28061 error_at (location, "expected positive integer expression");
28062 error = true;
28063 }
28064
28065 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28066 {
28067 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28068 /*or_comma=*/false,
28069 /*consume_paren=*/true);
28070 return list;
28071 }
28072
28073 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28074 location);
28075
28076 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28077 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28078 OMP_CLAUSE_CHAIN (c) = list;
28079 list = c;
28080
28081 return list;
28082 }
28083
28084 /* OpenACC 2.0
28085 Parse wait clause or directive parameters. */
28086
28087 static tree
28088 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28089 {
28090 vec<tree, va_gc> *args;
28091 tree t, args_tree;
28092
28093 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28094 /*cast_p=*/false,
28095 /*allow_expansion_p=*/true,
28096 /*non_constant_p=*/NULL);
28097
28098 if (args == NULL || args->length () == 0)
28099 {
28100 cp_parser_error (parser, "expected integer expression before ')'");
28101 if (args != NULL)
28102 release_tree_vector (args);
28103 return list;
28104 }
28105
28106 args_tree = build_tree_list_vec (args);
28107
28108 release_tree_vector (args);
28109
28110 for (t = args_tree; t; t = TREE_CHAIN (t))
28111 {
28112 tree targ = TREE_VALUE (t);
28113
28114 if (targ != error_mark_node)
28115 {
28116 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28117 error ("%<wait%> expression must be integral");
28118 else
28119 {
28120 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28121
28122 mark_rvalue_use (targ);
28123 OMP_CLAUSE_DECL (c) = targ;
28124 OMP_CLAUSE_CHAIN (c) = list;
28125 list = c;
28126 }
28127 }
28128 }
28129
28130 return list;
28131 }
28132
28133 /* OpenACC:
28134 wait ( int-expr-list ) */
28135
28136 static tree
28137 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28138 {
28139 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28140
28141 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28142 return list;
28143
28144 list = cp_parser_oacc_wait_list (parser, location, list);
28145
28146 return list;
28147 }
28148
28149 /* OpenMP 3.0:
28150 collapse ( constant-expression ) */
28151
28152 static tree
28153 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28154 {
28155 tree c, num;
28156 location_t loc;
28157 HOST_WIDE_INT n;
28158
28159 loc = cp_lexer_peek_token (parser->lexer)->location;
28160 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28161 return list;
28162
28163 num = cp_parser_constant_expression (parser);
28164
28165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28166 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28167 /*or_comma=*/false,
28168 /*consume_paren=*/true);
28169
28170 if (num == error_mark_node)
28171 return list;
28172 num = fold_non_dependent_expr (num);
28173 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28174 || !tree_fits_shwi_p (num)
28175 || (n = tree_to_shwi (num)) <= 0
28176 || (int) n != n)
28177 {
28178 error_at (loc, "collapse argument needs positive constant integer expression");
28179 return list;
28180 }
28181
28182 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28183 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28184 OMP_CLAUSE_CHAIN (c) = list;
28185 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28186
28187 return c;
28188 }
28189
28190 /* OpenMP 2.5:
28191 default ( shared | none ) */
28192
28193 static tree
28194 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28195 {
28196 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28197 tree c;
28198
28199 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28200 return list;
28201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28202 {
28203 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28204 const char *p = IDENTIFIER_POINTER (id);
28205
28206 switch (p[0])
28207 {
28208 case 'n':
28209 if (strcmp ("none", p) != 0)
28210 goto invalid_kind;
28211 kind = OMP_CLAUSE_DEFAULT_NONE;
28212 break;
28213
28214 case 's':
28215 if (strcmp ("shared", p) != 0)
28216 goto invalid_kind;
28217 kind = OMP_CLAUSE_DEFAULT_SHARED;
28218 break;
28219
28220 default:
28221 goto invalid_kind;
28222 }
28223
28224 cp_lexer_consume_token (parser->lexer);
28225 }
28226 else
28227 {
28228 invalid_kind:
28229 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28230 }
28231
28232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28233 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28234 /*or_comma=*/false,
28235 /*consume_paren=*/true);
28236
28237 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28238 return list;
28239
28240 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28241 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28242 OMP_CLAUSE_CHAIN (c) = list;
28243 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28244
28245 return c;
28246 }
28247
28248 /* OpenMP 3.1:
28249 final ( expression ) */
28250
28251 static tree
28252 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28253 {
28254 tree t, c;
28255
28256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28257 return list;
28258
28259 t = cp_parser_condition (parser);
28260
28261 if (t == error_mark_node
28262 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28264 /*or_comma=*/false,
28265 /*consume_paren=*/true);
28266
28267 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28268
28269 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28270 OMP_CLAUSE_FINAL_EXPR (c) = t;
28271 OMP_CLAUSE_CHAIN (c) = list;
28272
28273 return c;
28274 }
28275
28276 /* OpenMP 2.5:
28277 if ( expression ) */
28278
28279 static tree
28280 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28281 {
28282 tree t, c;
28283
28284 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28285 return list;
28286
28287 t = cp_parser_condition (parser);
28288
28289 if (t == error_mark_node
28290 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28291 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28292 /*or_comma=*/false,
28293 /*consume_paren=*/true);
28294
28295 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28296
28297 c = build_omp_clause (location, OMP_CLAUSE_IF);
28298 OMP_CLAUSE_IF_EXPR (c) = t;
28299 OMP_CLAUSE_CHAIN (c) = list;
28300
28301 return c;
28302 }
28303
28304 /* OpenMP 3.1:
28305 mergeable */
28306
28307 static tree
28308 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28309 tree list, location_t location)
28310 {
28311 tree c;
28312
28313 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28314 location);
28315
28316 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28317 OMP_CLAUSE_CHAIN (c) = list;
28318 return c;
28319 }
28320
28321 /* OpenMP 2.5:
28322 nowait */
28323
28324 static tree
28325 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28326 tree list, location_t location)
28327 {
28328 tree c;
28329
28330 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28331
28332 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28333 OMP_CLAUSE_CHAIN (c) = list;
28334 return c;
28335 }
28336
28337 /* OpenACC:
28338 num_gangs ( expression ) */
28339
28340 static tree
28341 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28342 {
28343 tree t, c;
28344 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28345
28346 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28347 return list;
28348
28349 t = cp_parser_condition (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 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28358 {
28359 error_at (location, "expected positive integer expression");
28360 return list;
28361 }
28362
28363 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28364
28365 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28366 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28367 OMP_CLAUSE_CHAIN (c) = list;
28368 list = c;
28369
28370 return list;
28371 }
28372
28373 /* OpenMP 2.5:
28374 num_threads ( expression ) */
28375
28376 static tree
28377 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28378 location_t location)
28379 {
28380 tree t, c;
28381
28382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28383 return list;
28384
28385 t = cp_parser_expression (parser);
28386
28387 if (t == error_mark_node
28388 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28390 /*or_comma=*/false,
28391 /*consume_paren=*/true);
28392
28393 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28394 "num_threads", location);
28395
28396 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28397 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28398 OMP_CLAUSE_CHAIN (c) = list;
28399
28400 return c;
28401 }
28402
28403 /* OpenACC:
28404 num_workers ( expression ) */
28405
28406 static tree
28407 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28408 {
28409 tree t, c;
28410 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28411
28412 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28413 return list;
28414
28415 t = cp_parser_condition (parser);
28416
28417 if (t == error_mark_node
28418 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28420 /*or_comma=*/false,
28421 /*consume_paren=*/true);
28422
28423 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28424 {
28425 error_at (location, "expected positive integer expression");
28426 return list;
28427 }
28428
28429 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28430 location);
28431
28432 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28433 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28434 OMP_CLAUSE_CHAIN (c) = list;
28435 list = c;
28436
28437 return list;
28438 }
28439
28440 /* OpenMP 2.5:
28441 ordered */
28442
28443 static tree
28444 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28445 tree list, location_t location)
28446 {
28447 tree c;
28448
28449 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28450 "ordered", location);
28451
28452 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28453 OMP_CLAUSE_CHAIN (c) = list;
28454 return c;
28455 }
28456
28457 /* OpenMP 2.5:
28458 reduction ( reduction-operator : variable-list )
28459
28460 reduction-operator:
28461 One of: + * - & ^ | && ||
28462
28463 OpenMP 3.1:
28464
28465 reduction-operator:
28466 One of: + * - & ^ | && || min max
28467
28468 OpenMP 4.0:
28469
28470 reduction-operator:
28471 One of: + * - & ^ | && ||
28472 id-expression */
28473
28474 static tree
28475 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28476 {
28477 enum tree_code code = ERROR_MARK;
28478 tree nlist, c, id = NULL_TREE;
28479
28480 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28481 return list;
28482
28483 switch (cp_lexer_peek_token (parser->lexer)->type)
28484 {
28485 case CPP_PLUS: code = PLUS_EXPR; break;
28486 case CPP_MULT: code = MULT_EXPR; break;
28487 case CPP_MINUS: code = MINUS_EXPR; break;
28488 case CPP_AND: code = BIT_AND_EXPR; break;
28489 case CPP_XOR: code = BIT_XOR_EXPR; break;
28490 case CPP_OR: code = BIT_IOR_EXPR; break;
28491 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28492 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28493 default: break;
28494 }
28495
28496 if (code != ERROR_MARK)
28497 cp_lexer_consume_token (parser->lexer);
28498 else
28499 {
28500 bool saved_colon_corrects_to_scope_p;
28501 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28502 parser->colon_corrects_to_scope_p = false;
28503 id = cp_parser_id_expression (parser, /*template_p=*/false,
28504 /*check_dependency_p=*/true,
28505 /*template_p=*/NULL,
28506 /*declarator_p=*/false,
28507 /*optional_p=*/false);
28508 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28509 if (identifier_p (id))
28510 {
28511 const char *p = IDENTIFIER_POINTER (id);
28512
28513 if (strcmp (p, "min") == 0)
28514 code = MIN_EXPR;
28515 else if (strcmp (p, "max") == 0)
28516 code = MAX_EXPR;
28517 else if (id == ansi_opname (PLUS_EXPR))
28518 code = PLUS_EXPR;
28519 else if (id == ansi_opname (MULT_EXPR))
28520 code = MULT_EXPR;
28521 else if (id == ansi_opname (MINUS_EXPR))
28522 code = MINUS_EXPR;
28523 else if (id == ansi_opname (BIT_AND_EXPR))
28524 code = BIT_AND_EXPR;
28525 else if (id == ansi_opname (BIT_IOR_EXPR))
28526 code = BIT_IOR_EXPR;
28527 else if (id == ansi_opname (BIT_XOR_EXPR))
28528 code = BIT_XOR_EXPR;
28529 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28530 code = TRUTH_ANDIF_EXPR;
28531 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28532 code = TRUTH_ORIF_EXPR;
28533 id = omp_reduction_id (code, id, NULL_TREE);
28534 tree scope = parser->scope;
28535 if (scope)
28536 id = build_qualified_name (NULL_TREE, scope, id, false);
28537 parser->scope = NULL_TREE;
28538 parser->qualifying_scope = NULL_TREE;
28539 parser->object_scope = NULL_TREE;
28540 }
28541 else
28542 {
28543 error ("invalid reduction-identifier");
28544 resync_fail:
28545 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28546 /*or_comma=*/false,
28547 /*consume_paren=*/true);
28548 return list;
28549 }
28550 }
28551
28552 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28553 goto resync_fail;
28554
28555 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28556 NULL);
28557 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28558 {
28559 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28560 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28561 }
28562
28563 return nlist;
28564 }
28565
28566 /* OpenMP 2.5:
28567 schedule ( schedule-kind )
28568 schedule ( schedule-kind , expression )
28569
28570 schedule-kind:
28571 static | dynamic | guided | runtime | auto */
28572
28573 static tree
28574 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28575 {
28576 tree c, t;
28577
28578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28579 return list;
28580
28581 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28582
28583 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28584 {
28585 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28586 const char *p = IDENTIFIER_POINTER (id);
28587
28588 switch (p[0])
28589 {
28590 case 'd':
28591 if (strcmp ("dynamic", p) != 0)
28592 goto invalid_kind;
28593 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28594 break;
28595
28596 case 'g':
28597 if (strcmp ("guided", p) != 0)
28598 goto invalid_kind;
28599 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28600 break;
28601
28602 case 'r':
28603 if (strcmp ("runtime", p) != 0)
28604 goto invalid_kind;
28605 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28606 break;
28607
28608 default:
28609 goto invalid_kind;
28610 }
28611 }
28612 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28613 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28614 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28615 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28616 else
28617 goto invalid_kind;
28618 cp_lexer_consume_token (parser->lexer);
28619
28620 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28621 {
28622 cp_token *token;
28623 cp_lexer_consume_token (parser->lexer);
28624
28625 token = cp_lexer_peek_token (parser->lexer);
28626 t = cp_parser_assignment_expression (parser);
28627
28628 if (t == error_mark_node)
28629 goto resync_fail;
28630 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28631 error_at (token->location, "schedule %<runtime%> does not take "
28632 "a %<chunk_size%> parameter");
28633 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28634 error_at (token->location, "schedule %<auto%> does not take "
28635 "a %<chunk_size%> parameter");
28636 else
28637 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28638
28639 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28640 goto resync_fail;
28641 }
28642 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28643 goto resync_fail;
28644
28645 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28646 OMP_CLAUSE_CHAIN (c) = list;
28647 return c;
28648
28649 invalid_kind:
28650 cp_parser_error (parser, "invalid schedule kind");
28651 resync_fail:
28652 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28653 /*or_comma=*/false,
28654 /*consume_paren=*/true);
28655 return list;
28656 }
28657
28658 /* OpenMP 3.0:
28659 untied */
28660
28661 static tree
28662 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28663 tree list, location_t location)
28664 {
28665 tree c;
28666
28667 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28668
28669 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28670 OMP_CLAUSE_CHAIN (c) = list;
28671 return c;
28672 }
28673
28674 /* OpenMP 4.0:
28675 inbranch
28676 notinbranch */
28677
28678 static tree
28679 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28680 tree list, location_t location)
28681 {
28682 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28683 tree c = build_omp_clause (location, code);
28684 OMP_CLAUSE_CHAIN (c) = list;
28685 return c;
28686 }
28687
28688 /* OpenMP 4.0:
28689 parallel
28690 for
28691 sections
28692 taskgroup */
28693
28694 static tree
28695 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28696 enum omp_clause_code code,
28697 tree list, location_t location)
28698 {
28699 tree c = build_omp_clause (location, code);
28700 OMP_CLAUSE_CHAIN (c) = list;
28701 return c;
28702 }
28703
28704 /* OpenMP 4.0:
28705 num_teams ( expression ) */
28706
28707 static tree
28708 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28709 location_t location)
28710 {
28711 tree t, c;
28712
28713 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28714 return list;
28715
28716 t = cp_parser_expression (parser);
28717
28718 if (t == error_mark_node
28719 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28721 /*or_comma=*/false,
28722 /*consume_paren=*/true);
28723
28724 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28725 "num_teams", location);
28726
28727 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28728 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28729 OMP_CLAUSE_CHAIN (c) = list;
28730
28731 return c;
28732 }
28733
28734 /* OpenMP 4.0:
28735 thread_limit ( expression ) */
28736
28737 static tree
28738 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28739 location_t location)
28740 {
28741 tree t, c;
28742
28743 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28744 return list;
28745
28746 t = cp_parser_expression (parser);
28747
28748 if (t == error_mark_node
28749 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28751 /*or_comma=*/false,
28752 /*consume_paren=*/true);
28753
28754 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28755 "thread_limit", location);
28756
28757 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28758 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28759 OMP_CLAUSE_CHAIN (c) = list;
28760
28761 return c;
28762 }
28763
28764 /* OpenMP 4.0:
28765 aligned ( variable-list )
28766 aligned ( variable-list : constant-expression ) */
28767
28768 static tree
28769 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28770 {
28771 tree nlist, c, alignment = NULL_TREE;
28772 bool colon;
28773
28774 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28775 return list;
28776
28777 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28778 &colon);
28779
28780 if (colon)
28781 {
28782 alignment = cp_parser_constant_expression (parser);
28783
28784 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28785 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28786 /*or_comma=*/false,
28787 /*consume_paren=*/true);
28788
28789 if (alignment == error_mark_node)
28790 alignment = NULL_TREE;
28791 }
28792
28793 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28794 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28795
28796 return nlist;
28797 }
28798
28799 /* OpenMP 4.0:
28800 linear ( variable-list )
28801 linear ( variable-list : expression ) */
28802
28803 static tree
28804 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28805 bool is_cilk_simd_fn)
28806 {
28807 tree nlist, c, step = integer_one_node;
28808 bool colon;
28809
28810 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28811 return list;
28812
28813 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28814 &colon);
28815
28816 if (colon)
28817 {
28818 step = cp_parser_expression (parser);
28819
28820 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28821 {
28822 sorry ("using parameters for %<linear%> step is not supported yet");
28823 step = integer_one_node;
28824 }
28825 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28827 /*or_comma=*/false,
28828 /*consume_paren=*/true);
28829
28830 if (step == error_mark_node)
28831 return list;
28832 }
28833
28834 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28835 OMP_CLAUSE_LINEAR_STEP (c) = step;
28836
28837 return nlist;
28838 }
28839
28840 /* OpenMP 4.0:
28841 safelen ( constant-expression ) */
28842
28843 static tree
28844 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28845 location_t location)
28846 {
28847 tree t, c;
28848
28849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28850 return list;
28851
28852 t = cp_parser_constant_expression (parser);
28853
28854 if (t == error_mark_node
28855 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28857 /*or_comma=*/false,
28858 /*consume_paren=*/true);
28859
28860 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28861
28862 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28863 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28864 OMP_CLAUSE_CHAIN (c) = list;
28865
28866 return c;
28867 }
28868
28869 /* OpenMP 4.0:
28870 simdlen ( constant-expression ) */
28871
28872 static tree
28873 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28874 location_t location)
28875 {
28876 tree t, c;
28877
28878 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28879 return list;
28880
28881 t = cp_parser_constant_expression (parser);
28882
28883 if (t == error_mark_node
28884 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28886 /*or_comma=*/false,
28887 /*consume_paren=*/true);
28888
28889 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28890
28891 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28892 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28893 OMP_CLAUSE_CHAIN (c) = list;
28894
28895 return c;
28896 }
28897
28898 /* OpenMP 4.0:
28899 depend ( depend-kind : variable-list )
28900
28901 depend-kind:
28902 in | out | inout */
28903
28904 static tree
28905 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28906 {
28907 tree nlist, c;
28908 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28909
28910 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28911 return list;
28912
28913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28914 {
28915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28916 const char *p = IDENTIFIER_POINTER (id);
28917
28918 if (strcmp ("in", p) == 0)
28919 kind = OMP_CLAUSE_DEPEND_IN;
28920 else if (strcmp ("inout", p) == 0)
28921 kind = OMP_CLAUSE_DEPEND_INOUT;
28922 else if (strcmp ("out", p) == 0)
28923 kind = OMP_CLAUSE_DEPEND_OUT;
28924 else
28925 goto invalid_kind;
28926 }
28927 else
28928 goto invalid_kind;
28929
28930 cp_lexer_consume_token (parser->lexer);
28931 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28932 goto resync_fail;
28933
28934 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28935 NULL);
28936
28937 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28938 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28939
28940 return nlist;
28941
28942 invalid_kind:
28943 cp_parser_error (parser, "invalid depend kind");
28944 resync_fail:
28945 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28946 /*or_comma=*/false,
28947 /*consume_paren=*/true);
28948 return list;
28949 }
28950
28951 /* OpenMP 4.0:
28952 map ( map-kind : variable-list )
28953 map ( variable-list )
28954
28955 map-kind:
28956 alloc | to | from | tofrom */
28957
28958 static tree
28959 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28960 {
28961 tree nlist, c;
28962 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28963
28964 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28965 return list;
28966
28967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28968 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28969 {
28970 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28971 const char *p = IDENTIFIER_POINTER (id);
28972
28973 if (strcmp ("alloc", p) == 0)
28974 kind = GOMP_MAP_ALLOC;
28975 else if (strcmp ("to", p) == 0)
28976 kind = GOMP_MAP_TO;
28977 else if (strcmp ("from", p) == 0)
28978 kind = GOMP_MAP_FROM;
28979 else if (strcmp ("tofrom", p) == 0)
28980 kind = GOMP_MAP_TOFROM;
28981 else
28982 {
28983 cp_parser_error (parser, "invalid map kind");
28984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28985 /*or_comma=*/false,
28986 /*consume_paren=*/true);
28987 return list;
28988 }
28989 cp_lexer_consume_token (parser->lexer);
28990 cp_lexer_consume_token (parser->lexer);
28991 }
28992
28993 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28994 NULL);
28995
28996 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28997 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28998
28999 return nlist;
29000 }
29001
29002 /* OpenMP 4.0:
29003 device ( expression ) */
29004
29005 static tree
29006 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29007 location_t location)
29008 {
29009 tree t, c;
29010
29011 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29012 return list;
29013
29014 t = cp_parser_expression (parser);
29015
29016 if (t == error_mark_node
29017 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29018 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29019 /*or_comma=*/false,
29020 /*consume_paren=*/true);
29021
29022 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29023 "device", location);
29024
29025 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29026 OMP_CLAUSE_DEVICE_ID (c) = t;
29027 OMP_CLAUSE_CHAIN (c) = list;
29028
29029 return c;
29030 }
29031
29032 /* OpenMP 4.0:
29033 dist_schedule ( static )
29034 dist_schedule ( static , expression ) */
29035
29036 static tree
29037 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29038 location_t location)
29039 {
29040 tree c, t;
29041
29042 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29043 return list;
29044
29045 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29046
29047 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29048 goto invalid_kind;
29049 cp_lexer_consume_token (parser->lexer);
29050
29051 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29052 {
29053 cp_lexer_consume_token (parser->lexer);
29054
29055 t = cp_parser_assignment_expression (parser);
29056
29057 if (t == error_mark_node)
29058 goto resync_fail;
29059 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29060
29061 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29062 goto resync_fail;
29063 }
29064 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29065 goto resync_fail;
29066
29067 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29068 location);
29069 OMP_CLAUSE_CHAIN (c) = list;
29070 return c;
29071
29072 invalid_kind:
29073 cp_parser_error (parser, "invalid dist_schedule kind");
29074 resync_fail:
29075 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29076 /*or_comma=*/false,
29077 /*consume_paren=*/true);
29078 return list;
29079 }
29080
29081 /* OpenMP 4.0:
29082 proc_bind ( proc-bind-kind )
29083
29084 proc-bind-kind:
29085 master | close | spread */
29086
29087 static tree
29088 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29089 location_t location)
29090 {
29091 tree c;
29092 enum omp_clause_proc_bind_kind kind;
29093
29094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29095 return list;
29096
29097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29098 {
29099 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29100 const char *p = IDENTIFIER_POINTER (id);
29101
29102 if (strcmp ("master", p) == 0)
29103 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29104 else if (strcmp ("close", p) == 0)
29105 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29106 else if (strcmp ("spread", p) == 0)
29107 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29108 else
29109 goto invalid_kind;
29110 }
29111 else
29112 goto invalid_kind;
29113
29114 cp_lexer_consume_token (parser->lexer);
29115 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29116 goto resync_fail;
29117
29118 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29119 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29120 location);
29121 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29122 OMP_CLAUSE_CHAIN (c) = list;
29123 return c;
29124
29125 invalid_kind:
29126 cp_parser_error (parser, "invalid depend kind");
29127 resync_fail:
29128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29129 /*or_comma=*/false,
29130 /*consume_paren=*/true);
29131 return list;
29132 }
29133
29134 /* OpenACC:
29135 async [( int-expr )] */
29136
29137 static tree
29138 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29139 {
29140 tree c, t;
29141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29142
29143 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29144
29145 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29146 {
29147 cp_lexer_consume_token (parser->lexer);
29148
29149 t = cp_parser_expression (parser);
29150 if (t == error_mark_node
29151 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29152 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29153 /*or_comma=*/false,
29154 /*consume_paren=*/true);
29155 }
29156
29157 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29158
29159 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29160 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29161 OMP_CLAUSE_CHAIN (c) = list;
29162 list = c;
29163
29164 return list;
29165 }
29166
29167 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29168 is a bitmask in MASK. Return the list of clauses found. */
29169
29170 static tree
29171 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29172 const char *where, cp_token *pragma_tok,
29173 bool finish_p = true)
29174 {
29175 tree clauses = NULL;
29176 bool first = true;
29177
29178 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29179 {
29180 location_t here;
29181 pragma_omp_clause c_kind;
29182 const char *c_name;
29183 tree prev = clauses;
29184
29185 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29186 cp_lexer_consume_token (parser->lexer);
29187
29188 here = cp_lexer_peek_token (parser->lexer)->location;
29189 c_kind = cp_parser_omp_clause_name (parser);
29190
29191 switch (c_kind)
29192 {
29193 case PRAGMA_OACC_CLAUSE_ASYNC:
29194 clauses = cp_parser_oacc_clause_async (parser, clauses);
29195 c_name = "async";
29196 break;
29197 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29198 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29199 c_name = "collapse";
29200 break;
29201 case PRAGMA_OACC_CLAUSE_COPY:
29202 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29203 c_name = "copy";
29204 break;
29205 case PRAGMA_OACC_CLAUSE_COPYIN:
29206 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29207 c_name = "copyin";
29208 break;
29209 case PRAGMA_OACC_CLAUSE_COPYOUT:
29210 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29211 c_name = "copyout";
29212 break;
29213 case PRAGMA_OACC_CLAUSE_CREATE:
29214 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29215 c_name = "create";
29216 break;
29217 case PRAGMA_OACC_CLAUSE_DELETE:
29218 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29219 c_name = "delete";
29220 break;
29221 case PRAGMA_OACC_CLAUSE_DEVICE:
29222 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29223 c_name = "device";
29224 break;
29225 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29226 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29227 c_name = "deviceptr";
29228 break;
29229 case PRAGMA_OACC_CLAUSE_HOST:
29230 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29231 c_name = "host";
29232 break;
29233 case PRAGMA_OACC_CLAUSE_IF:
29234 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29235 c_name = "if";
29236 break;
29237 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29238 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29239 c_name = "num_gangs";
29240 break;
29241 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29242 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29243 c_name = "num_workers";
29244 break;
29245 case PRAGMA_OACC_CLAUSE_PRESENT:
29246 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29247 c_name = "present";
29248 break;
29249 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29250 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29251 c_name = "present_or_copy";
29252 break;
29253 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29254 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29255 c_name = "present_or_copyin";
29256 break;
29257 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29258 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29259 c_name = "present_or_copyout";
29260 break;
29261 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29262 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29263 c_name = "present_or_create";
29264 break;
29265 case PRAGMA_OACC_CLAUSE_REDUCTION:
29266 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29267 c_name = "reduction";
29268 break;
29269 case PRAGMA_OACC_CLAUSE_SELF:
29270 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29271 c_name = "self";
29272 break;
29273 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29274 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29275 c_name = "vector_length";
29276 break;
29277 case PRAGMA_OACC_CLAUSE_WAIT:
29278 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29279 c_name = "wait";
29280 break;
29281 default:
29282 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29283 goto saw_error;
29284 }
29285
29286 first = false;
29287
29288 if (((mask >> c_kind) & 1) == 0)
29289 {
29290 /* Remove the invalid clause(s) from the list to avoid
29291 confusing the rest of the compiler. */
29292 clauses = prev;
29293 error_at (here, "%qs is not valid for %qs", c_name, where);
29294 }
29295 }
29296
29297 saw_error:
29298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29299
29300 if (finish_p)
29301 return finish_omp_clauses (clauses);
29302
29303 return clauses;
29304 }
29305
29306 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29307 is a bitmask in MASK. Return the list of clauses found; the result
29308 of clause default goes in *pdefault. */
29309
29310 static tree
29311 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29312 const char *where, cp_token *pragma_tok,
29313 bool finish_p = true)
29314 {
29315 tree clauses = NULL;
29316 bool first = true;
29317 cp_token *token = NULL;
29318 bool cilk_simd_fn = false;
29319
29320 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29321 {
29322 pragma_omp_clause c_kind;
29323 const char *c_name;
29324 tree prev = clauses;
29325
29326 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29327 cp_lexer_consume_token (parser->lexer);
29328
29329 token = cp_lexer_peek_token (parser->lexer);
29330 c_kind = cp_parser_omp_clause_name (parser);
29331
29332 switch (c_kind)
29333 {
29334 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29335 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29336 token->location);
29337 c_name = "collapse";
29338 break;
29339 case PRAGMA_OMP_CLAUSE_COPYIN:
29340 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29341 c_name = "copyin";
29342 break;
29343 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29344 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29345 clauses);
29346 c_name = "copyprivate";
29347 break;
29348 case PRAGMA_OMP_CLAUSE_DEFAULT:
29349 clauses = cp_parser_omp_clause_default (parser, clauses,
29350 token->location);
29351 c_name = "default";
29352 break;
29353 case PRAGMA_OMP_CLAUSE_FINAL:
29354 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29355 c_name = "final";
29356 break;
29357 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29358 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29359 clauses);
29360 c_name = "firstprivate";
29361 break;
29362 case PRAGMA_OMP_CLAUSE_IF:
29363 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29364 c_name = "if";
29365 break;
29366 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29367 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29368 clauses);
29369 c_name = "lastprivate";
29370 break;
29371 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29372 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29373 token->location);
29374 c_name = "mergeable";
29375 break;
29376 case PRAGMA_OMP_CLAUSE_NOWAIT:
29377 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29378 c_name = "nowait";
29379 break;
29380 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29381 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29382 token->location);
29383 c_name = "num_threads";
29384 break;
29385 case PRAGMA_OMP_CLAUSE_ORDERED:
29386 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29387 token->location);
29388 c_name = "ordered";
29389 break;
29390 case PRAGMA_OMP_CLAUSE_PRIVATE:
29391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29392 clauses);
29393 c_name = "private";
29394 break;
29395 case PRAGMA_OMP_CLAUSE_REDUCTION:
29396 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29397 c_name = "reduction";
29398 break;
29399 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29400 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29401 token->location);
29402 c_name = "schedule";
29403 break;
29404 case PRAGMA_OMP_CLAUSE_SHARED:
29405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29406 clauses);
29407 c_name = "shared";
29408 break;
29409 case PRAGMA_OMP_CLAUSE_UNTIED:
29410 clauses = cp_parser_omp_clause_untied (parser, clauses,
29411 token->location);
29412 c_name = "untied";
29413 break;
29414 case PRAGMA_OMP_CLAUSE_INBRANCH:
29415 case PRAGMA_CILK_CLAUSE_MASK:
29416 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29417 clauses, token->location);
29418 c_name = "inbranch";
29419 break;
29420 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29421 case PRAGMA_CILK_CLAUSE_NOMASK:
29422 clauses = cp_parser_omp_clause_branch (parser,
29423 OMP_CLAUSE_NOTINBRANCH,
29424 clauses, token->location);
29425 c_name = "notinbranch";
29426 break;
29427 case PRAGMA_OMP_CLAUSE_PARALLEL:
29428 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29429 clauses, token->location);
29430 c_name = "parallel";
29431 if (!first)
29432 {
29433 clause_not_first:
29434 error_at (token->location, "%qs must be the first clause of %qs",
29435 c_name, where);
29436 clauses = prev;
29437 }
29438 break;
29439 case PRAGMA_OMP_CLAUSE_FOR:
29440 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29441 clauses, token->location);
29442 c_name = "for";
29443 if (!first)
29444 goto clause_not_first;
29445 break;
29446 case PRAGMA_OMP_CLAUSE_SECTIONS:
29447 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29448 clauses, token->location);
29449 c_name = "sections";
29450 if (!first)
29451 goto clause_not_first;
29452 break;
29453 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29454 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29455 clauses, token->location);
29456 c_name = "taskgroup";
29457 if (!first)
29458 goto clause_not_first;
29459 break;
29460 case PRAGMA_OMP_CLAUSE_TO:
29461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29462 clauses);
29463 c_name = "to";
29464 break;
29465 case PRAGMA_OMP_CLAUSE_FROM:
29466 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29467 clauses);
29468 c_name = "from";
29469 break;
29470 case PRAGMA_OMP_CLAUSE_UNIFORM:
29471 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29472 clauses);
29473 c_name = "uniform";
29474 break;
29475 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29476 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29477 token->location);
29478 c_name = "num_teams";
29479 break;
29480 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29481 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29482 token->location);
29483 c_name = "thread_limit";
29484 break;
29485 case PRAGMA_OMP_CLAUSE_ALIGNED:
29486 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29487 c_name = "aligned";
29488 break;
29489 case PRAGMA_OMP_CLAUSE_LINEAR:
29490 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29491 cilk_simd_fn = true;
29492 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29493 c_name = "linear";
29494 break;
29495 case PRAGMA_OMP_CLAUSE_DEPEND:
29496 clauses = cp_parser_omp_clause_depend (parser, clauses);
29497 c_name = "depend";
29498 break;
29499 case PRAGMA_OMP_CLAUSE_MAP:
29500 clauses = cp_parser_omp_clause_map (parser, clauses);
29501 c_name = "map";
29502 break;
29503 case PRAGMA_OMP_CLAUSE_DEVICE:
29504 clauses = cp_parser_omp_clause_device (parser, clauses,
29505 token->location);
29506 c_name = "device";
29507 break;
29508 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29509 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29510 token->location);
29511 c_name = "dist_schedule";
29512 break;
29513 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29514 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29515 token->location);
29516 c_name = "proc_bind";
29517 break;
29518 case PRAGMA_OMP_CLAUSE_SAFELEN:
29519 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29520 token->location);
29521 c_name = "safelen";
29522 break;
29523 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29524 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29525 token->location);
29526 c_name = "simdlen";
29527 break;
29528 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29529 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29530 c_name = "simdlen";
29531 break;
29532 default:
29533 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29534 goto saw_error;
29535 }
29536
29537 first = false;
29538
29539 if (((mask >> c_kind) & 1) == 0)
29540 {
29541 /* Remove the invalid clause(s) from the list to avoid
29542 confusing the rest of the compiler. */
29543 clauses = prev;
29544 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29545 }
29546 }
29547 saw_error:
29548 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29549 no reason to skip to the end. */
29550 if (!(flag_cilkplus && pragma_tok == NULL))
29551 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29552 if (finish_p)
29553 return finish_omp_clauses (clauses);
29554 return clauses;
29555 }
29556
29557 /* OpenMP 2.5:
29558 structured-block:
29559 statement
29560
29561 In practice, we're also interested in adding the statement to an
29562 outer node. So it is convenient if we work around the fact that
29563 cp_parser_statement calls add_stmt. */
29564
29565 static unsigned
29566 cp_parser_begin_omp_structured_block (cp_parser *parser)
29567 {
29568 unsigned save = parser->in_statement;
29569
29570 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29571 This preserves the "not within loop or switch" style error messages
29572 for nonsense cases like
29573 void foo() {
29574 #pragma omp single
29575 break;
29576 }
29577 */
29578 if (parser->in_statement)
29579 parser->in_statement = IN_OMP_BLOCK;
29580
29581 return save;
29582 }
29583
29584 static void
29585 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29586 {
29587 parser->in_statement = save;
29588 }
29589
29590 static tree
29591 cp_parser_omp_structured_block (cp_parser *parser)
29592 {
29593 tree stmt = begin_omp_structured_block ();
29594 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29595
29596 cp_parser_statement (parser, NULL_TREE, false, NULL);
29597
29598 cp_parser_end_omp_structured_block (parser, save);
29599 return finish_omp_structured_block (stmt);
29600 }
29601
29602 /* OpenMP 2.5:
29603 # pragma omp atomic new-line
29604 expression-stmt
29605
29606 expression-stmt:
29607 x binop= expr | x++ | ++x | x-- | --x
29608 binop:
29609 +, *, -, /, &, ^, |, <<, >>
29610
29611 where x is an lvalue expression with scalar type.
29612
29613 OpenMP 3.1:
29614 # pragma omp atomic new-line
29615 update-stmt
29616
29617 # pragma omp atomic read new-line
29618 read-stmt
29619
29620 # pragma omp atomic write new-line
29621 write-stmt
29622
29623 # pragma omp atomic update new-line
29624 update-stmt
29625
29626 # pragma omp atomic capture new-line
29627 capture-stmt
29628
29629 # pragma omp atomic capture new-line
29630 capture-block
29631
29632 read-stmt:
29633 v = x
29634 write-stmt:
29635 x = expr
29636 update-stmt:
29637 expression-stmt | x = x binop expr
29638 capture-stmt:
29639 v = expression-stmt
29640 capture-block:
29641 { v = x; update-stmt; } | { update-stmt; v = x; }
29642
29643 OpenMP 4.0:
29644 update-stmt:
29645 expression-stmt | x = x binop expr | x = expr binop x
29646 capture-stmt:
29647 v = update-stmt
29648 capture-block:
29649 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29650
29651 where x and v are lvalue expressions with scalar type. */
29652
29653 static void
29654 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29655 {
29656 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29657 tree rhs1 = NULL_TREE, orig_lhs;
29658 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29659 bool structured_block = false;
29660 bool seq_cst = false;
29661
29662 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29663 {
29664 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29665 const char *p = IDENTIFIER_POINTER (id);
29666
29667 if (!strcmp (p, "seq_cst"))
29668 {
29669 seq_cst = true;
29670 cp_lexer_consume_token (parser->lexer);
29671 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29672 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29673 cp_lexer_consume_token (parser->lexer);
29674 }
29675 }
29676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29677 {
29678 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29679 const char *p = IDENTIFIER_POINTER (id);
29680
29681 if (!strcmp (p, "read"))
29682 code = OMP_ATOMIC_READ;
29683 else if (!strcmp (p, "write"))
29684 code = NOP_EXPR;
29685 else if (!strcmp (p, "update"))
29686 code = OMP_ATOMIC;
29687 else if (!strcmp (p, "capture"))
29688 code = OMP_ATOMIC_CAPTURE_NEW;
29689 else
29690 p = NULL;
29691 if (p)
29692 cp_lexer_consume_token (parser->lexer);
29693 }
29694 if (!seq_cst)
29695 {
29696 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29697 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29698 cp_lexer_consume_token (parser->lexer);
29699
29700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29701 {
29702 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29703 const char *p = IDENTIFIER_POINTER (id);
29704
29705 if (!strcmp (p, "seq_cst"))
29706 {
29707 seq_cst = true;
29708 cp_lexer_consume_token (parser->lexer);
29709 }
29710 }
29711 }
29712 cp_parser_require_pragma_eol (parser, pragma_tok);
29713
29714 switch (code)
29715 {
29716 case OMP_ATOMIC_READ:
29717 case NOP_EXPR: /* atomic write */
29718 v = cp_parser_unary_expression (parser);
29719 if (v == error_mark_node)
29720 goto saw_error;
29721 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29722 goto saw_error;
29723 if (code == NOP_EXPR)
29724 lhs = cp_parser_expression (parser);
29725 else
29726 lhs = cp_parser_unary_expression (parser);
29727 if (lhs == error_mark_node)
29728 goto saw_error;
29729 if (code == NOP_EXPR)
29730 {
29731 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29732 opcode. */
29733 code = OMP_ATOMIC;
29734 rhs = lhs;
29735 lhs = v;
29736 v = NULL_TREE;
29737 }
29738 goto done;
29739 case OMP_ATOMIC_CAPTURE_NEW:
29740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29741 {
29742 cp_lexer_consume_token (parser->lexer);
29743 structured_block = true;
29744 }
29745 else
29746 {
29747 v = cp_parser_unary_expression (parser);
29748 if (v == error_mark_node)
29749 goto saw_error;
29750 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29751 goto saw_error;
29752 }
29753 default:
29754 break;
29755 }
29756
29757 restart:
29758 lhs = cp_parser_unary_expression (parser);
29759 orig_lhs = lhs;
29760 switch (TREE_CODE (lhs))
29761 {
29762 case ERROR_MARK:
29763 goto saw_error;
29764
29765 case POSTINCREMENT_EXPR:
29766 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29767 code = OMP_ATOMIC_CAPTURE_OLD;
29768 /* FALLTHROUGH */
29769 case PREINCREMENT_EXPR:
29770 lhs = TREE_OPERAND (lhs, 0);
29771 opcode = PLUS_EXPR;
29772 rhs = integer_one_node;
29773 break;
29774
29775 case POSTDECREMENT_EXPR:
29776 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29777 code = OMP_ATOMIC_CAPTURE_OLD;
29778 /* FALLTHROUGH */
29779 case PREDECREMENT_EXPR:
29780 lhs = TREE_OPERAND (lhs, 0);
29781 opcode = MINUS_EXPR;
29782 rhs = integer_one_node;
29783 break;
29784
29785 case COMPOUND_EXPR:
29786 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29787 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29788 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29789 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29790 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29791 (TREE_OPERAND (lhs, 1), 0), 0)))
29792 == BOOLEAN_TYPE)
29793 /* Undo effects of boolean_increment for post {in,de}crement. */
29794 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29795 /* FALLTHRU */
29796 case MODIFY_EXPR:
29797 if (TREE_CODE (lhs) == MODIFY_EXPR
29798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29799 {
29800 /* Undo effects of boolean_increment. */
29801 if (integer_onep (TREE_OPERAND (lhs, 1)))
29802 {
29803 /* This is pre or post increment. */
29804 rhs = TREE_OPERAND (lhs, 1);
29805 lhs = TREE_OPERAND (lhs, 0);
29806 opcode = NOP_EXPR;
29807 if (code == OMP_ATOMIC_CAPTURE_NEW
29808 && !structured_block
29809 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29810 code = OMP_ATOMIC_CAPTURE_OLD;
29811 break;
29812 }
29813 }
29814 /* FALLTHRU */
29815 default:
29816 switch (cp_lexer_peek_token (parser->lexer)->type)
29817 {
29818 case CPP_MULT_EQ:
29819 opcode = MULT_EXPR;
29820 break;
29821 case CPP_DIV_EQ:
29822 opcode = TRUNC_DIV_EXPR;
29823 break;
29824 case CPP_PLUS_EQ:
29825 opcode = PLUS_EXPR;
29826 break;
29827 case CPP_MINUS_EQ:
29828 opcode = MINUS_EXPR;
29829 break;
29830 case CPP_LSHIFT_EQ:
29831 opcode = LSHIFT_EXPR;
29832 break;
29833 case CPP_RSHIFT_EQ:
29834 opcode = RSHIFT_EXPR;
29835 break;
29836 case CPP_AND_EQ:
29837 opcode = BIT_AND_EXPR;
29838 break;
29839 case CPP_OR_EQ:
29840 opcode = BIT_IOR_EXPR;
29841 break;
29842 case CPP_XOR_EQ:
29843 opcode = BIT_XOR_EXPR;
29844 break;
29845 case CPP_EQ:
29846 enum cp_parser_prec oprec;
29847 cp_token *token;
29848 cp_lexer_consume_token (parser->lexer);
29849 cp_parser_parse_tentatively (parser);
29850 rhs1 = cp_parser_simple_cast_expression (parser);
29851 if (rhs1 == error_mark_node)
29852 {
29853 cp_parser_abort_tentative_parse (parser);
29854 cp_parser_simple_cast_expression (parser);
29855 goto saw_error;
29856 }
29857 token = cp_lexer_peek_token (parser->lexer);
29858 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29859 {
29860 cp_parser_abort_tentative_parse (parser);
29861 cp_parser_parse_tentatively (parser);
29862 rhs = cp_parser_binary_expression (parser, false, true,
29863 PREC_NOT_OPERATOR, NULL);
29864 if (rhs == error_mark_node)
29865 {
29866 cp_parser_abort_tentative_parse (parser);
29867 cp_parser_binary_expression (parser, false, true,
29868 PREC_NOT_OPERATOR, NULL);
29869 goto saw_error;
29870 }
29871 switch (TREE_CODE (rhs))
29872 {
29873 case MULT_EXPR:
29874 case TRUNC_DIV_EXPR:
29875 case RDIV_EXPR:
29876 case PLUS_EXPR:
29877 case MINUS_EXPR:
29878 case LSHIFT_EXPR:
29879 case RSHIFT_EXPR:
29880 case BIT_AND_EXPR:
29881 case BIT_IOR_EXPR:
29882 case BIT_XOR_EXPR:
29883 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29884 {
29885 if (cp_parser_parse_definitely (parser))
29886 {
29887 opcode = TREE_CODE (rhs);
29888 rhs1 = TREE_OPERAND (rhs, 0);
29889 rhs = TREE_OPERAND (rhs, 1);
29890 goto stmt_done;
29891 }
29892 else
29893 goto saw_error;
29894 }
29895 break;
29896 default:
29897 break;
29898 }
29899 cp_parser_abort_tentative_parse (parser);
29900 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29901 {
29902 rhs = cp_parser_expression (parser);
29903 if (rhs == error_mark_node)
29904 goto saw_error;
29905 opcode = NOP_EXPR;
29906 rhs1 = NULL_TREE;
29907 goto stmt_done;
29908 }
29909 cp_parser_error (parser,
29910 "invalid form of %<#pragma omp atomic%>");
29911 goto saw_error;
29912 }
29913 if (!cp_parser_parse_definitely (parser))
29914 goto saw_error;
29915 switch (token->type)
29916 {
29917 case CPP_SEMICOLON:
29918 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29919 {
29920 code = OMP_ATOMIC_CAPTURE_OLD;
29921 v = lhs;
29922 lhs = NULL_TREE;
29923 lhs1 = rhs1;
29924 rhs1 = NULL_TREE;
29925 cp_lexer_consume_token (parser->lexer);
29926 goto restart;
29927 }
29928 else if (structured_block)
29929 {
29930 opcode = NOP_EXPR;
29931 rhs = rhs1;
29932 rhs1 = NULL_TREE;
29933 goto stmt_done;
29934 }
29935 cp_parser_error (parser,
29936 "invalid form of %<#pragma omp atomic%>");
29937 goto saw_error;
29938 case CPP_MULT:
29939 opcode = MULT_EXPR;
29940 break;
29941 case CPP_DIV:
29942 opcode = TRUNC_DIV_EXPR;
29943 break;
29944 case CPP_PLUS:
29945 opcode = PLUS_EXPR;
29946 break;
29947 case CPP_MINUS:
29948 opcode = MINUS_EXPR;
29949 break;
29950 case CPP_LSHIFT:
29951 opcode = LSHIFT_EXPR;
29952 break;
29953 case CPP_RSHIFT:
29954 opcode = RSHIFT_EXPR;
29955 break;
29956 case CPP_AND:
29957 opcode = BIT_AND_EXPR;
29958 break;
29959 case CPP_OR:
29960 opcode = BIT_IOR_EXPR;
29961 break;
29962 case CPP_XOR:
29963 opcode = BIT_XOR_EXPR;
29964 break;
29965 default:
29966 cp_parser_error (parser,
29967 "invalid operator for %<#pragma omp atomic%>");
29968 goto saw_error;
29969 }
29970 oprec = TOKEN_PRECEDENCE (token);
29971 gcc_assert (oprec != PREC_NOT_OPERATOR);
29972 if (commutative_tree_code (opcode))
29973 oprec = (enum cp_parser_prec) (oprec - 1);
29974 cp_lexer_consume_token (parser->lexer);
29975 rhs = cp_parser_binary_expression (parser, false, false,
29976 oprec, NULL);
29977 if (rhs == error_mark_node)
29978 goto saw_error;
29979 goto stmt_done;
29980 /* FALLTHROUGH */
29981 default:
29982 cp_parser_error (parser,
29983 "invalid operator for %<#pragma omp atomic%>");
29984 goto saw_error;
29985 }
29986 cp_lexer_consume_token (parser->lexer);
29987
29988 rhs = cp_parser_expression (parser);
29989 if (rhs == error_mark_node)
29990 goto saw_error;
29991 break;
29992 }
29993 stmt_done:
29994 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29995 {
29996 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29997 goto saw_error;
29998 v = cp_parser_unary_expression (parser);
29999 if (v == error_mark_node)
30000 goto saw_error;
30001 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30002 goto saw_error;
30003 lhs1 = cp_parser_unary_expression (parser);
30004 if (lhs1 == error_mark_node)
30005 goto saw_error;
30006 }
30007 if (structured_block)
30008 {
30009 cp_parser_consume_semicolon_at_end_of_statement (parser);
30010 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30011 }
30012 done:
30013 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30014 if (!structured_block)
30015 cp_parser_consume_semicolon_at_end_of_statement (parser);
30016 return;
30017
30018 saw_error:
30019 cp_parser_skip_to_end_of_block_or_statement (parser);
30020 if (structured_block)
30021 {
30022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30023 cp_lexer_consume_token (parser->lexer);
30024 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30025 {
30026 cp_parser_skip_to_end_of_block_or_statement (parser);
30027 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30028 cp_lexer_consume_token (parser->lexer);
30029 }
30030 }
30031 }
30032
30033
30034 /* OpenMP 2.5:
30035 # pragma omp barrier new-line */
30036
30037 static void
30038 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30039 {
30040 cp_parser_require_pragma_eol (parser, pragma_tok);
30041 finish_omp_barrier ();
30042 }
30043
30044 /* OpenMP 2.5:
30045 # pragma omp critical [(name)] new-line
30046 structured-block */
30047
30048 static tree
30049 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30050 {
30051 tree stmt, name = NULL;
30052
30053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30054 {
30055 cp_lexer_consume_token (parser->lexer);
30056
30057 name = cp_parser_identifier (parser);
30058
30059 if (name == error_mark_node
30060 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30061 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30062 /*or_comma=*/false,
30063 /*consume_paren=*/true);
30064 if (name == error_mark_node)
30065 name = NULL;
30066 }
30067 cp_parser_require_pragma_eol (parser, pragma_tok);
30068
30069 stmt = cp_parser_omp_structured_block (parser);
30070 return c_finish_omp_critical (input_location, stmt, name);
30071 }
30072
30073 /* OpenMP 2.5:
30074 # pragma omp flush flush-vars[opt] new-line
30075
30076 flush-vars:
30077 ( variable-list ) */
30078
30079 static void
30080 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30081 {
30082 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30083 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30084 cp_parser_require_pragma_eol (parser, pragma_tok);
30085
30086 finish_omp_flush ();
30087 }
30088
30089 /* Helper function, to parse omp for increment expression. */
30090
30091 static tree
30092 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30093 {
30094 tree cond = cp_parser_binary_expression (parser, false, true,
30095 PREC_NOT_OPERATOR, NULL);
30096 if (cond == error_mark_node
30097 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30098 {
30099 cp_parser_skip_to_end_of_statement (parser);
30100 return error_mark_node;
30101 }
30102
30103 switch (TREE_CODE (cond))
30104 {
30105 case GT_EXPR:
30106 case GE_EXPR:
30107 case LT_EXPR:
30108 case LE_EXPR:
30109 break;
30110 case NE_EXPR:
30111 if (code == CILK_SIMD || code == CILK_FOR)
30112 break;
30113 /* Fall through: OpenMP disallows NE_EXPR. */
30114 default:
30115 return error_mark_node;
30116 }
30117
30118 /* If decl is an iterator, preserve LHS and RHS of the relational
30119 expr until finish_omp_for. */
30120 if (decl
30121 && (type_dependent_expression_p (decl)
30122 || CLASS_TYPE_P (TREE_TYPE (decl))))
30123 return cond;
30124
30125 return build_x_binary_op (input_location, TREE_CODE (cond),
30126 TREE_OPERAND (cond, 0), ERROR_MARK,
30127 TREE_OPERAND (cond, 1), ERROR_MARK,
30128 /*overload=*/NULL, tf_warning_or_error);
30129 }
30130
30131 /* Helper function, to parse omp for increment expression. */
30132
30133 static tree
30134 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30135 {
30136 cp_token *token = cp_lexer_peek_token (parser->lexer);
30137 enum tree_code op;
30138 tree lhs, rhs;
30139 cp_id_kind idk;
30140 bool decl_first;
30141
30142 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30143 {
30144 op = (token->type == CPP_PLUS_PLUS
30145 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30146 cp_lexer_consume_token (parser->lexer);
30147 lhs = cp_parser_simple_cast_expression (parser);
30148 if (lhs != decl)
30149 return error_mark_node;
30150 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30151 }
30152
30153 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30154 if (lhs != decl)
30155 return error_mark_node;
30156
30157 token = cp_lexer_peek_token (parser->lexer);
30158 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30159 {
30160 op = (token->type == CPP_PLUS_PLUS
30161 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30162 cp_lexer_consume_token (parser->lexer);
30163 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30164 }
30165
30166 op = cp_parser_assignment_operator_opt (parser);
30167 if (op == ERROR_MARK)
30168 return error_mark_node;
30169
30170 if (op != NOP_EXPR)
30171 {
30172 rhs = cp_parser_assignment_expression (parser);
30173 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30174 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30175 }
30176
30177 lhs = cp_parser_binary_expression (parser, false, false,
30178 PREC_ADDITIVE_EXPRESSION, NULL);
30179 token = cp_lexer_peek_token (parser->lexer);
30180 decl_first = lhs == decl;
30181 if (decl_first)
30182 lhs = NULL_TREE;
30183 if (token->type != CPP_PLUS
30184 && token->type != CPP_MINUS)
30185 return error_mark_node;
30186
30187 do
30188 {
30189 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30190 cp_lexer_consume_token (parser->lexer);
30191 rhs = cp_parser_binary_expression (parser, false, false,
30192 PREC_ADDITIVE_EXPRESSION, NULL);
30193 token = cp_lexer_peek_token (parser->lexer);
30194 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30195 {
30196 if (lhs == NULL_TREE)
30197 {
30198 if (op == PLUS_EXPR)
30199 lhs = rhs;
30200 else
30201 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30202 tf_warning_or_error);
30203 }
30204 else
30205 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30206 ERROR_MARK, NULL, tf_warning_or_error);
30207 }
30208 }
30209 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30210
30211 if (!decl_first)
30212 {
30213 if (rhs != decl || op == MINUS_EXPR)
30214 return error_mark_node;
30215 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30216 }
30217 else
30218 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30219
30220 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30221 }
30222
30223 /* Parse the initialization statement of either an OpenMP for loop or
30224 a Cilk Plus for loop.
30225
30226 Return true if the resulting construct should have an
30227 OMP_CLAUSE_PRIVATE added to it. */
30228
30229 static bool
30230 cp_parser_omp_for_loop_init (cp_parser *parser,
30231 enum tree_code code,
30232 tree &this_pre_body,
30233 vec<tree, va_gc> *for_block,
30234 tree &init,
30235 tree &decl,
30236 tree &real_decl)
30237 {
30238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30239 return false;
30240
30241 bool add_private_clause = false;
30242
30243 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30244
30245 init-expr:
30246 var = lb
30247 integer-type var = lb
30248 random-access-iterator-type var = lb
30249 pointer-type var = lb
30250 */
30251 cp_decl_specifier_seq type_specifiers;
30252
30253 /* First, try to parse as an initialized declaration. See
30254 cp_parser_condition, from whence the bulk of this is copied. */
30255
30256 cp_parser_parse_tentatively (parser);
30257 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30258 /*is_trailing_return=*/false,
30259 &type_specifiers);
30260 if (cp_parser_parse_definitely (parser))
30261 {
30262 /* If parsing a type specifier seq succeeded, then this
30263 MUST be a initialized declaration. */
30264 tree asm_specification, attributes;
30265 cp_declarator *declarator;
30266
30267 declarator = cp_parser_declarator (parser,
30268 CP_PARSER_DECLARATOR_NAMED,
30269 /*ctor_dtor_or_conv_p=*/NULL,
30270 /*parenthesized_p=*/NULL,
30271 /*member_p=*/false,
30272 /*friend_p=*/false);
30273 attributes = cp_parser_attributes_opt (parser);
30274 asm_specification = cp_parser_asm_specification_opt (parser);
30275
30276 if (declarator == cp_error_declarator)
30277 cp_parser_skip_to_end_of_statement (parser);
30278
30279 else
30280 {
30281 tree pushed_scope, auto_node;
30282
30283 decl = start_decl (declarator, &type_specifiers,
30284 SD_INITIALIZED, attributes,
30285 /*prefix_attributes=*/NULL_TREE,
30286 &pushed_scope);
30287
30288 auto_node = type_uses_auto (TREE_TYPE (decl));
30289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30290 {
30291 if (cp_lexer_next_token_is (parser->lexer,
30292 CPP_OPEN_PAREN))
30293 {
30294 if (code != CILK_SIMD && code != CILK_FOR)
30295 error ("parenthesized initialization is not allowed in "
30296 "OpenMP %<for%> loop");
30297 else
30298 error ("parenthesized initialization is "
30299 "not allowed in for-loop");
30300 }
30301 else
30302 /* Trigger an error. */
30303 cp_parser_require (parser, CPP_EQ, RT_EQ);
30304
30305 init = error_mark_node;
30306 cp_parser_skip_to_end_of_statement (parser);
30307 }
30308 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30309 || type_dependent_expression_p (decl)
30310 || auto_node)
30311 {
30312 bool is_direct_init, is_non_constant_init;
30313
30314 init = cp_parser_initializer (parser,
30315 &is_direct_init,
30316 &is_non_constant_init);
30317
30318 if (auto_node)
30319 {
30320 TREE_TYPE (decl)
30321 = do_auto_deduction (TREE_TYPE (decl), init,
30322 auto_node);
30323
30324 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30325 && !type_dependent_expression_p (decl))
30326 goto non_class;
30327 }
30328
30329 cp_finish_decl (decl, init, !is_non_constant_init,
30330 asm_specification,
30331 LOOKUP_ONLYCONVERTING);
30332 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30333 {
30334 vec_safe_push (for_block, this_pre_body);
30335 init = NULL_TREE;
30336 }
30337 else
30338 init = pop_stmt_list (this_pre_body);
30339 this_pre_body = NULL_TREE;
30340 }
30341 else
30342 {
30343 /* Consume '='. */
30344 cp_lexer_consume_token (parser->lexer);
30345 init = cp_parser_assignment_expression (parser);
30346
30347 non_class:
30348 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30349 init = error_mark_node;
30350 else
30351 cp_finish_decl (decl, NULL_TREE,
30352 /*init_const_expr_p=*/false,
30353 asm_specification,
30354 LOOKUP_ONLYCONVERTING);
30355 }
30356
30357 if (pushed_scope)
30358 pop_scope (pushed_scope);
30359 }
30360 }
30361 else
30362 {
30363 cp_id_kind idk;
30364 /* If parsing a type specifier sequence failed, then
30365 this MUST be a simple expression. */
30366 if (code == CILK_FOR)
30367 error ("%<_Cilk_for%> allows expression instead of declaration only "
30368 "in C, not in C++");
30369 cp_parser_parse_tentatively (parser);
30370 decl = cp_parser_primary_expression (parser, false, false,
30371 false, &idk);
30372 if (!cp_parser_error_occurred (parser)
30373 && decl
30374 && DECL_P (decl)
30375 && CLASS_TYPE_P (TREE_TYPE (decl)))
30376 {
30377 tree rhs;
30378
30379 cp_parser_parse_definitely (parser);
30380 cp_parser_require (parser, CPP_EQ, RT_EQ);
30381 rhs = cp_parser_assignment_expression (parser);
30382 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30383 decl, NOP_EXPR,
30384 rhs,
30385 tf_warning_or_error));
30386 add_private_clause = true;
30387 }
30388 else
30389 {
30390 decl = NULL;
30391 cp_parser_abort_tentative_parse (parser);
30392 init = cp_parser_expression (parser);
30393 if (init)
30394 {
30395 if (TREE_CODE (init) == MODIFY_EXPR
30396 || TREE_CODE (init) == MODOP_EXPR)
30397 real_decl = TREE_OPERAND (init, 0);
30398 }
30399 }
30400 }
30401 return add_private_clause;
30402 }
30403
30404 /* Parse the restricted form of the for statement allowed by OpenMP. */
30405
30406 static tree
30407 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30408 tree *cclauses)
30409 {
30410 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30411 tree real_decl, initv, condv, incrv, declv;
30412 tree this_pre_body, cl;
30413 location_t loc_first;
30414 bool collapse_err = false;
30415 int i, collapse = 1, nbraces = 0;
30416 vec<tree, va_gc> *for_block = make_tree_vector ();
30417
30418 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30419 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30420 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30421
30422 gcc_assert (collapse >= 1);
30423
30424 declv = make_tree_vec (collapse);
30425 initv = make_tree_vec (collapse);
30426 condv = make_tree_vec (collapse);
30427 incrv = make_tree_vec (collapse);
30428
30429 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30430
30431 for (i = 0; i < collapse; i++)
30432 {
30433 int bracecount = 0;
30434 bool add_private_clause = false;
30435 location_t loc;
30436
30437 if (code != CILK_FOR
30438 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30439 {
30440 cp_parser_error (parser, "for statement expected");
30441 return NULL;
30442 }
30443 if (code == CILK_FOR
30444 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30445 {
30446 cp_parser_error (parser, "_Cilk_for statement expected");
30447 return NULL;
30448 }
30449 loc = cp_lexer_consume_token (parser->lexer)->location;
30450
30451 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30452 return NULL;
30453
30454 init = decl = real_decl = NULL;
30455 this_pre_body = push_stmt_list ();
30456
30457 add_private_clause
30458 |= cp_parser_omp_for_loop_init (parser, code,
30459 this_pre_body, for_block,
30460 init, decl, real_decl);
30461
30462 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30463 if (this_pre_body)
30464 {
30465 this_pre_body = pop_stmt_list (this_pre_body);
30466 if (pre_body)
30467 {
30468 tree t = pre_body;
30469 pre_body = push_stmt_list ();
30470 add_stmt (t);
30471 add_stmt (this_pre_body);
30472 pre_body = pop_stmt_list (pre_body);
30473 }
30474 else
30475 pre_body = this_pre_body;
30476 }
30477
30478 if (decl)
30479 real_decl = decl;
30480 if (cclauses != NULL
30481 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30482 && real_decl != NULL_TREE)
30483 {
30484 tree *c;
30485 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30486 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30487 && OMP_CLAUSE_DECL (*c) == real_decl)
30488 {
30489 error_at (loc, "iteration variable %qD"
30490 " should not be firstprivate", real_decl);
30491 *c = OMP_CLAUSE_CHAIN (*c);
30492 }
30493 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30494 && OMP_CLAUSE_DECL (*c) == real_decl)
30495 {
30496 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30497 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30498 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30499 OMP_CLAUSE_DECL (l) = real_decl;
30500 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30501 if (code == OMP_SIMD)
30502 {
30503 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30504 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30505 }
30506 else
30507 {
30508 OMP_CLAUSE_CHAIN (l) = clauses;
30509 clauses = l;
30510 }
30511 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30512 CP_OMP_CLAUSE_INFO (*c) = NULL;
30513 add_private_clause = false;
30514 }
30515 else
30516 {
30517 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30518 && OMP_CLAUSE_DECL (*c) == real_decl)
30519 add_private_clause = false;
30520 c = &OMP_CLAUSE_CHAIN (*c);
30521 }
30522 }
30523
30524 if (add_private_clause)
30525 {
30526 tree c;
30527 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30528 {
30529 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30530 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30531 && OMP_CLAUSE_DECL (c) == decl)
30532 break;
30533 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30534 && OMP_CLAUSE_DECL (c) == decl)
30535 error_at (loc, "iteration variable %qD "
30536 "should not be firstprivate",
30537 decl);
30538 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30539 && OMP_CLAUSE_DECL (c) == decl)
30540 error_at (loc, "iteration variable %qD should not be reduction",
30541 decl);
30542 }
30543 if (c == NULL)
30544 {
30545 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30546 OMP_CLAUSE_DECL (c) = decl;
30547 c = finish_omp_clauses (c);
30548 if (c)
30549 {
30550 OMP_CLAUSE_CHAIN (c) = clauses;
30551 clauses = c;
30552 }
30553 }
30554 }
30555
30556 cond = NULL;
30557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30558 cond = cp_parser_omp_for_cond (parser, decl, code);
30559 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30560
30561 incr = NULL;
30562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30563 {
30564 /* If decl is an iterator, preserve the operator on decl
30565 until finish_omp_for. */
30566 if (real_decl
30567 && ((processing_template_decl
30568 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30569 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30570 incr = cp_parser_omp_for_incr (parser, real_decl);
30571 else
30572 incr = cp_parser_expression (parser);
30573 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30574 SET_EXPR_LOCATION (incr, input_location);
30575 }
30576
30577 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30579 /*or_comma=*/false,
30580 /*consume_paren=*/true);
30581
30582 TREE_VEC_ELT (declv, i) = decl;
30583 TREE_VEC_ELT (initv, i) = init;
30584 TREE_VEC_ELT (condv, i) = cond;
30585 TREE_VEC_ELT (incrv, i) = incr;
30586
30587 if (i == collapse - 1)
30588 break;
30589
30590 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30591 in between the collapsed for loops to be still considered perfectly
30592 nested. Hopefully the final version clarifies this.
30593 For now handle (multiple) {'s and empty statements. */
30594 cp_parser_parse_tentatively (parser);
30595 do
30596 {
30597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30598 break;
30599 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30600 {
30601 cp_lexer_consume_token (parser->lexer);
30602 bracecount++;
30603 }
30604 else if (bracecount
30605 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30606 cp_lexer_consume_token (parser->lexer);
30607 else
30608 {
30609 loc = cp_lexer_peek_token (parser->lexer)->location;
30610 error_at (loc, "not enough collapsed for loops");
30611 collapse_err = true;
30612 cp_parser_abort_tentative_parse (parser);
30613 declv = NULL_TREE;
30614 break;
30615 }
30616 }
30617 while (1);
30618
30619 if (declv)
30620 {
30621 cp_parser_parse_definitely (parser);
30622 nbraces += bracecount;
30623 }
30624 }
30625
30626 /* Note that we saved the original contents of this flag when we entered
30627 the structured block, and so we don't need to re-save it here. */
30628 if (code == CILK_SIMD || code == CILK_FOR)
30629 parser->in_statement = IN_CILK_SIMD_FOR;
30630 else
30631 parser->in_statement = IN_OMP_FOR;
30632
30633 /* Note that the grammar doesn't call for a structured block here,
30634 though the loop as a whole is a structured block. */
30635 body = push_stmt_list ();
30636 cp_parser_statement (parser, NULL_TREE, false, NULL);
30637 body = pop_stmt_list (body);
30638
30639 if (declv == NULL_TREE)
30640 ret = NULL_TREE;
30641 else
30642 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30643 pre_body, clauses);
30644
30645 while (nbraces)
30646 {
30647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30648 {
30649 cp_lexer_consume_token (parser->lexer);
30650 nbraces--;
30651 }
30652 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30653 cp_lexer_consume_token (parser->lexer);
30654 else
30655 {
30656 if (!collapse_err)
30657 {
30658 error_at (cp_lexer_peek_token (parser->lexer)->location,
30659 "collapsed loops not perfectly nested");
30660 }
30661 collapse_err = true;
30662 cp_parser_statement_seq_opt (parser, NULL);
30663 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30664 break;
30665 }
30666 }
30667
30668 while (!for_block->is_empty ())
30669 add_stmt (pop_stmt_list (for_block->pop ()));
30670 release_tree_vector (for_block);
30671
30672 return ret;
30673 }
30674
30675 /* Helper function for OpenMP parsing, split clauses and call
30676 finish_omp_clauses on each of the set of clauses afterwards. */
30677
30678 static void
30679 cp_omp_split_clauses (location_t loc, enum tree_code code,
30680 omp_clause_mask mask, tree clauses, tree *cclauses)
30681 {
30682 int i;
30683 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30684 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30685 if (cclauses[i])
30686 cclauses[i] = finish_omp_clauses (cclauses[i]);
30687 }
30688
30689 /* OpenMP 4.0:
30690 #pragma omp simd simd-clause[optseq] new-line
30691 for-loop */
30692
30693 #define OMP_SIMD_CLAUSE_MASK \
30694 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30701
30702 static tree
30703 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30704 char *p_name, omp_clause_mask mask, tree *cclauses)
30705 {
30706 tree clauses, sb, ret;
30707 unsigned int save;
30708 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30709
30710 strcat (p_name, " simd");
30711 mask |= OMP_SIMD_CLAUSE_MASK;
30712 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30713
30714 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30715 cclauses == NULL);
30716 if (cclauses)
30717 {
30718 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30719 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30720 }
30721
30722 sb = begin_omp_structured_block ();
30723 save = cp_parser_begin_omp_structured_block (parser);
30724
30725 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30726
30727 cp_parser_end_omp_structured_block (parser, save);
30728 add_stmt (finish_omp_structured_block (sb));
30729
30730 return ret;
30731 }
30732
30733 /* OpenMP 2.5:
30734 #pragma omp for for-clause[optseq] new-line
30735 for-loop
30736
30737 OpenMP 4.0:
30738 #pragma omp for simd for-simd-clause[optseq] new-line
30739 for-loop */
30740
30741 #define OMP_FOR_CLAUSE_MASK \
30742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30750
30751 static tree
30752 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30753 char *p_name, omp_clause_mask mask, tree *cclauses)
30754 {
30755 tree clauses, sb, ret;
30756 unsigned int save;
30757 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30758
30759 strcat (p_name, " for");
30760 mask |= OMP_FOR_CLAUSE_MASK;
30761 if (cclauses)
30762 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30763
30764 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30765 {
30766 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30767 const char *p = IDENTIFIER_POINTER (id);
30768
30769 if (strcmp (p, "simd") == 0)
30770 {
30771 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30772 if (cclauses == NULL)
30773 cclauses = cclauses_buf;
30774
30775 cp_lexer_consume_token (parser->lexer);
30776 if (!flag_openmp) /* flag_openmp_simd */
30777 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30778 cclauses);
30779 sb = begin_omp_structured_block ();
30780 save = cp_parser_begin_omp_structured_block (parser);
30781 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30782 cclauses);
30783 cp_parser_end_omp_structured_block (parser, save);
30784 tree body = finish_omp_structured_block (sb);
30785 if (ret == NULL)
30786 return ret;
30787 ret = make_node (OMP_FOR);
30788 TREE_TYPE (ret) = void_type_node;
30789 OMP_FOR_BODY (ret) = body;
30790 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30791 SET_EXPR_LOCATION (ret, loc);
30792 add_stmt (ret);
30793 return ret;
30794 }
30795 }
30796 if (!flag_openmp) /* flag_openmp_simd */
30797 {
30798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30799 return NULL_TREE;
30800 }
30801
30802 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30803 cclauses == NULL);
30804 if (cclauses)
30805 {
30806 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30807 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30808 }
30809
30810 sb = begin_omp_structured_block ();
30811 save = cp_parser_begin_omp_structured_block (parser);
30812
30813 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30814
30815 cp_parser_end_omp_structured_block (parser, save);
30816 add_stmt (finish_omp_structured_block (sb));
30817
30818 return ret;
30819 }
30820
30821 /* OpenMP 2.5:
30822 # pragma omp master new-line
30823 structured-block */
30824
30825 static tree
30826 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30827 {
30828 cp_parser_require_pragma_eol (parser, pragma_tok);
30829 return c_finish_omp_master (input_location,
30830 cp_parser_omp_structured_block (parser));
30831 }
30832
30833 /* OpenMP 2.5:
30834 # pragma omp ordered new-line
30835 structured-block */
30836
30837 static tree
30838 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30839 {
30840 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30841 cp_parser_require_pragma_eol (parser, pragma_tok);
30842 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30843 }
30844
30845 /* OpenMP 2.5:
30846
30847 section-scope:
30848 { section-sequence }
30849
30850 section-sequence:
30851 section-directive[opt] structured-block
30852 section-sequence section-directive structured-block */
30853
30854 static tree
30855 cp_parser_omp_sections_scope (cp_parser *parser)
30856 {
30857 tree stmt, substmt;
30858 bool error_suppress = false;
30859 cp_token *tok;
30860
30861 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30862 return NULL_TREE;
30863
30864 stmt = push_stmt_list ();
30865
30866 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30867 {
30868 substmt = cp_parser_omp_structured_block (parser);
30869 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30870 add_stmt (substmt);
30871 }
30872
30873 while (1)
30874 {
30875 tok = cp_lexer_peek_token (parser->lexer);
30876 if (tok->type == CPP_CLOSE_BRACE)
30877 break;
30878 if (tok->type == CPP_EOF)
30879 break;
30880
30881 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30882 {
30883 cp_lexer_consume_token (parser->lexer);
30884 cp_parser_require_pragma_eol (parser, tok);
30885 error_suppress = false;
30886 }
30887 else if (!error_suppress)
30888 {
30889 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30890 error_suppress = true;
30891 }
30892
30893 substmt = cp_parser_omp_structured_block (parser);
30894 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30895 add_stmt (substmt);
30896 }
30897 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30898
30899 substmt = pop_stmt_list (stmt);
30900
30901 stmt = make_node (OMP_SECTIONS);
30902 TREE_TYPE (stmt) = void_type_node;
30903 OMP_SECTIONS_BODY (stmt) = substmt;
30904
30905 add_stmt (stmt);
30906 return stmt;
30907 }
30908
30909 /* OpenMP 2.5:
30910 # pragma omp sections sections-clause[optseq] newline
30911 sections-scope */
30912
30913 #define OMP_SECTIONS_CLAUSE_MASK \
30914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30919
30920 static tree
30921 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30922 char *p_name, omp_clause_mask mask, tree *cclauses)
30923 {
30924 tree clauses, ret;
30925 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30926
30927 strcat (p_name, " sections");
30928 mask |= OMP_SECTIONS_CLAUSE_MASK;
30929 if (cclauses)
30930 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30931
30932 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30933 cclauses == NULL);
30934 if (cclauses)
30935 {
30936 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30937 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30938 }
30939
30940 ret = cp_parser_omp_sections_scope (parser);
30941 if (ret)
30942 OMP_SECTIONS_CLAUSES (ret) = clauses;
30943
30944 return ret;
30945 }
30946
30947 /* OpenMP 2.5:
30948 # pragma omp parallel parallel-clause[optseq] new-line
30949 structured-block
30950 # pragma omp parallel for parallel-for-clause[optseq] new-line
30951 structured-block
30952 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30953 structured-block
30954
30955 OpenMP 4.0:
30956 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30957 structured-block */
30958
30959 #define OMP_PARALLEL_CLAUSE_MASK \
30960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30969
30970 static tree
30971 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30972 char *p_name, omp_clause_mask mask, tree *cclauses)
30973 {
30974 tree stmt, clauses, block;
30975 unsigned int save;
30976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30977
30978 strcat (p_name, " parallel");
30979 mask |= OMP_PARALLEL_CLAUSE_MASK;
30980
30981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30982 {
30983 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30984 if (cclauses == NULL)
30985 cclauses = cclauses_buf;
30986
30987 cp_lexer_consume_token (parser->lexer);
30988 if (!flag_openmp) /* flag_openmp_simd */
30989 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30990 block = begin_omp_parallel ();
30991 save = cp_parser_begin_omp_structured_block (parser);
30992 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30993 cp_parser_end_omp_structured_block (parser, save);
30994 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30995 block);
30996 if (ret == NULL_TREE)
30997 return ret;
30998 OMP_PARALLEL_COMBINED (stmt) = 1;
30999 return stmt;
31000 }
31001 else if (cclauses)
31002 {
31003 error_at (loc, "expected %<for%> after %qs", p_name);
31004 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31005 return NULL_TREE;
31006 }
31007 else if (!flag_openmp) /* flag_openmp_simd */
31008 {
31009 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31010 return NULL_TREE;
31011 }
31012 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31013 {
31014 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31015 const char *p = IDENTIFIER_POINTER (id);
31016 if (strcmp (p, "sections") == 0)
31017 {
31018 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31019 cclauses = cclauses_buf;
31020
31021 cp_lexer_consume_token (parser->lexer);
31022 block = begin_omp_parallel ();
31023 save = cp_parser_begin_omp_structured_block (parser);
31024 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31025 cp_parser_end_omp_structured_block (parser, save);
31026 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31027 block);
31028 OMP_PARALLEL_COMBINED (stmt) = 1;
31029 return stmt;
31030 }
31031 }
31032
31033 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31034
31035 block = begin_omp_parallel ();
31036 save = cp_parser_begin_omp_structured_block (parser);
31037 cp_parser_statement (parser, NULL_TREE, false, NULL);
31038 cp_parser_end_omp_structured_block (parser, save);
31039 stmt = finish_omp_parallel (clauses, block);
31040 return stmt;
31041 }
31042
31043 /* OpenMP 2.5:
31044 # pragma omp single single-clause[optseq] new-line
31045 structured-block */
31046
31047 #define OMP_SINGLE_CLAUSE_MASK \
31048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31052
31053 static tree
31054 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31055 {
31056 tree stmt = make_node (OMP_SINGLE);
31057 TREE_TYPE (stmt) = void_type_node;
31058
31059 OMP_SINGLE_CLAUSES (stmt)
31060 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31061 "#pragma omp single", pragma_tok);
31062 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31063
31064 return add_stmt (stmt);
31065 }
31066
31067 /* OpenMP 3.0:
31068 # pragma omp task task-clause[optseq] new-line
31069 structured-block */
31070
31071 #define OMP_TASK_CLAUSE_MASK \
31072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31081
31082 static tree
31083 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31084 {
31085 tree clauses, block;
31086 unsigned int save;
31087
31088 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31089 "#pragma omp task", pragma_tok);
31090 block = begin_omp_task ();
31091 save = cp_parser_begin_omp_structured_block (parser);
31092 cp_parser_statement (parser, NULL_TREE, false, NULL);
31093 cp_parser_end_omp_structured_block (parser, save);
31094 return finish_omp_task (clauses, block);
31095 }
31096
31097 /* OpenMP 3.0:
31098 # pragma omp taskwait new-line */
31099
31100 static void
31101 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31102 {
31103 cp_parser_require_pragma_eol (parser, pragma_tok);
31104 finish_omp_taskwait ();
31105 }
31106
31107 /* OpenMP 3.1:
31108 # pragma omp taskyield new-line */
31109
31110 static void
31111 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31112 {
31113 cp_parser_require_pragma_eol (parser, pragma_tok);
31114 finish_omp_taskyield ();
31115 }
31116
31117 /* OpenMP 4.0:
31118 # pragma omp taskgroup new-line
31119 structured-block */
31120
31121 static tree
31122 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31123 {
31124 cp_parser_require_pragma_eol (parser, pragma_tok);
31125 return c_finish_omp_taskgroup (input_location,
31126 cp_parser_omp_structured_block (parser));
31127 }
31128
31129
31130 /* OpenMP 2.5:
31131 # pragma omp threadprivate (variable-list) */
31132
31133 static void
31134 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31135 {
31136 tree vars;
31137
31138 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31139 cp_parser_require_pragma_eol (parser, pragma_tok);
31140
31141 finish_omp_threadprivate (vars);
31142 }
31143
31144 /* OpenMP 4.0:
31145 # pragma omp cancel cancel-clause[optseq] new-line */
31146
31147 #define OMP_CANCEL_CLAUSE_MASK \
31148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31153
31154 static void
31155 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31156 {
31157 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31158 "#pragma omp cancel", pragma_tok);
31159 finish_omp_cancel (clauses);
31160 }
31161
31162 /* OpenMP 4.0:
31163 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31164
31165 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31170
31171 static void
31172 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31173 {
31174 tree clauses;
31175 bool point_seen = false;
31176
31177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31178 {
31179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31180 const char *p = IDENTIFIER_POINTER (id);
31181
31182 if (strcmp (p, "point") == 0)
31183 {
31184 cp_lexer_consume_token (parser->lexer);
31185 point_seen = true;
31186 }
31187 }
31188 if (!point_seen)
31189 {
31190 cp_parser_error (parser, "expected %<point%>");
31191 cp_parser_require_pragma_eol (parser, pragma_tok);
31192 return;
31193 }
31194
31195 clauses = cp_parser_omp_all_clauses (parser,
31196 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31197 "#pragma omp cancellation point",
31198 pragma_tok);
31199 finish_omp_cancellation_point (clauses);
31200 }
31201
31202 /* OpenMP 4.0:
31203 #pragma omp distribute distribute-clause[optseq] new-line
31204 for-loop */
31205
31206 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31211
31212 static tree
31213 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31214 char *p_name, omp_clause_mask mask, tree *cclauses)
31215 {
31216 tree clauses, sb, ret;
31217 unsigned int save;
31218 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31219
31220 strcat (p_name, " distribute");
31221 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31222
31223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31224 {
31225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31226 const char *p = IDENTIFIER_POINTER (id);
31227 bool simd = false;
31228 bool parallel = false;
31229
31230 if (strcmp (p, "simd") == 0)
31231 simd = true;
31232 else
31233 parallel = strcmp (p, "parallel") == 0;
31234 if (parallel || simd)
31235 {
31236 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31237 if (cclauses == NULL)
31238 cclauses = cclauses_buf;
31239 cp_lexer_consume_token (parser->lexer);
31240 if (!flag_openmp) /* flag_openmp_simd */
31241 {
31242 if (simd)
31243 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31244 cclauses);
31245 else
31246 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31247 cclauses);
31248 }
31249 sb = begin_omp_structured_block ();
31250 save = cp_parser_begin_omp_structured_block (parser);
31251 if (simd)
31252 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31253 cclauses);
31254 else
31255 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31256 cclauses);
31257 cp_parser_end_omp_structured_block (parser, save);
31258 tree body = finish_omp_structured_block (sb);
31259 if (ret == NULL)
31260 return ret;
31261 ret = make_node (OMP_DISTRIBUTE);
31262 TREE_TYPE (ret) = void_type_node;
31263 OMP_FOR_BODY (ret) = body;
31264 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31265 SET_EXPR_LOCATION (ret, loc);
31266 add_stmt (ret);
31267 return ret;
31268 }
31269 }
31270 if (!flag_openmp) /* flag_openmp_simd */
31271 {
31272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31273 return NULL_TREE;
31274 }
31275
31276 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31277 cclauses == NULL);
31278 if (cclauses)
31279 {
31280 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31281 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31282 }
31283
31284 sb = begin_omp_structured_block ();
31285 save = cp_parser_begin_omp_structured_block (parser);
31286
31287 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31288
31289 cp_parser_end_omp_structured_block (parser, save);
31290 add_stmt (finish_omp_structured_block (sb));
31291
31292 return ret;
31293 }
31294
31295 /* OpenMP 4.0:
31296 # pragma omp teams teams-clause[optseq] new-line
31297 structured-block */
31298
31299 #define OMP_TEAMS_CLAUSE_MASK \
31300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31307
31308 static tree
31309 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31310 char *p_name, omp_clause_mask mask, tree *cclauses)
31311 {
31312 tree clauses, sb, ret;
31313 unsigned int save;
31314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31315
31316 strcat (p_name, " teams");
31317 mask |= OMP_TEAMS_CLAUSE_MASK;
31318
31319 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31320 {
31321 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31322 const char *p = IDENTIFIER_POINTER (id);
31323 if (strcmp (p, "distribute") == 0)
31324 {
31325 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31326 if (cclauses == NULL)
31327 cclauses = cclauses_buf;
31328
31329 cp_lexer_consume_token (parser->lexer);
31330 if (!flag_openmp) /* flag_openmp_simd */
31331 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31332 cclauses);
31333 sb = begin_omp_structured_block ();
31334 save = cp_parser_begin_omp_structured_block (parser);
31335 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31336 cclauses);
31337 cp_parser_end_omp_structured_block (parser, save);
31338 tree body = finish_omp_structured_block (sb);
31339 if (ret == NULL)
31340 return ret;
31341 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31342 ret = make_node (OMP_TEAMS);
31343 TREE_TYPE (ret) = void_type_node;
31344 OMP_TEAMS_CLAUSES (ret) = clauses;
31345 OMP_TEAMS_BODY (ret) = body;
31346 return add_stmt (ret);
31347 }
31348 }
31349 if (!flag_openmp) /* flag_openmp_simd */
31350 {
31351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31352 return NULL_TREE;
31353 }
31354
31355 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31356 cclauses == NULL);
31357 if (cclauses)
31358 {
31359 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31360 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31361 }
31362
31363 tree stmt = make_node (OMP_TEAMS);
31364 TREE_TYPE (stmt) = void_type_node;
31365 OMP_TEAMS_CLAUSES (stmt) = clauses;
31366 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31367
31368 return add_stmt (stmt);
31369 }
31370
31371 /* OpenMP 4.0:
31372 # pragma omp target data target-data-clause[optseq] new-line
31373 structured-block */
31374
31375 #define OMP_TARGET_DATA_CLAUSE_MASK \
31376 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31379
31380 static tree
31381 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31382 {
31383 tree stmt = make_node (OMP_TARGET_DATA);
31384 TREE_TYPE (stmt) = void_type_node;
31385
31386 OMP_TARGET_DATA_CLAUSES (stmt)
31387 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31388 "#pragma omp target data", pragma_tok);
31389 keep_next_level (true);
31390 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31391
31392 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31393 return add_stmt (stmt);
31394 }
31395
31396 /* OpenMP 4.0:
31397 # pragma omp target update target-update-clause[optseq] new-line */
31398
31399 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31400 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31404
31405 static bool
31406 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31407 enum pragma_context context)
31408 {
31409 if (context == pragma_stmt)
31410 {
31411 error_at (pragma_tok->location,
31412 "%<#pragma omp target update%> may only be "
31413 "used in compound statements");
31414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31415 return false;
31416 }
31417
31418 tree clauses
31419 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31420 "#pragma omp target update", pragma_tok);
31421 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31422 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31423 {
31424 error_at (pragma_tok->location,
31425 "%<#pragma omp target update%> must contain at least one "
31426 "%<from%> or %<to%> clauses");
31427 return false;
31428 }
31429
31430 tree stmt = make_node (OMP_TARGET_UPDATE);
31431 TREE_TYPE (stmt) = void_type_node;
31432 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31433 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31434 add_stmt (stmt);
31435 return false;
31436 }
31437
31438 /* OpenMP 4.0:
31439 # pragma omp target target-clause[optseq] new-line
31440 structured-block */
31441
31442 #define OMP_TARGET_CLAUSE_MASK \
31443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31446
31447 static bool
31448 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31449 enum pragma_context context)
31450 {
31451 if (context != pragma_stmt && context != pragma_compound)
31452 {
31453 cp_parser_error (parser, "expected declaration specifiers");
31454 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31455 return false;
31456 }
31457
31458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31459 {
31460 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31461 const char *p = IDENTIFIER_POINTER (id);
31462
31463 if (strcmp (p, "teams") == 0)
31464 {
31465 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31466 char p_name[sizeof ("#pragma omp target teams distribute "
31467 "parallel for simd")];
31468
31469 cp_lexer_consume_token (parser->lexer);
31470 strcpy (p_name, "#pragma omp target");
31471 if (!flag_openmp) /* flag_openmp_simd */
31472 {
31473 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31474 OMP_TARGET_CLAUSE_MASK,
31475 cclauses);
31476 return stmt != NULL_TREE;
31477 }
31478 keep_next_level (true);
31479 tree sb = begin_omp_structured_block ();
31480 unsigned save = cp_parser_begin_omp_structured_block (parser);
31481 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31482 OMP_TARGET_CLAUSE_MASK, cclauses);
31483 cp_parser_end_omp_structured_block (parser, save);
31484 tree body = finish_omp_structured_block (sb);
31485 if (ret == NULL_TREE)
31486 return false;
31487 tree stmt = make_node (OMP_TARGET);
31488 TREE_TYPE (stmt) = void_type_node;
31489 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31490 OMP_TARGET_BODY (stmt) = body;
31491 add_stmt (stmt);
31492 return true;
31493 }
31494 else if (!flag_openmp) /* flag_openmp_simd */
31495 {
31496 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31497 return false;
31498 }
31499 else if (strcmp (p, "data") == 0)
31500 {
31501 cp_lexer_consume_token (parser->lexer);
31502 cp_parser_omp_target_data (parser, pragma_tok);
31503 return true;
31504 }
31505 else if (strcmp (p, "update") == 0)
31506 {
31507 cp_lexer_consume_token (parser->lexer);
31508 return cp_parser_omp_target_update (parser, pragma_tok, context);
31509 }
31510 }
31511
31512 tree stmt = make_node (OMP_TARGET);
31513 TREE_TYPE (stmt) = void_type_node;
31514
31515 OMP_TARGET_CLAUSES (stmt)
31516 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31517 "#pragma omp target", pragma_tok);
31518 keep_next_level (true);
31519 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31520
31521 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31522 add_stmt (stmt);
31523 return true;
31524 }
31525
31526 /* OpenACC 2.0:
31527 # pragma acc cache (variable-list) new-line
31528 */
31529
31530 static tree
31531 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31532 {
31533 tree stmt, clauses;
31534
31535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31536 clauses = finish_omp_clauses (clauses);
31537
31538 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31539
31540 stmt = make_node (OACC_CACHE);
31541 TREE_TYPE (stmt) = void_type_node;
31542 OACC_CACHE_CLAUSES (stmt) = clauses;
31543 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31544 add_stmt (stmt);
31545
31546 return stmt;
31547 }
31548
31549 /* OpenACC 2.0:
31550 # pragma acc data oacc-data-clause[optseq] new-line
31551 structured-block */
31552
31553 #define OACC_DATA_CLAUSE_MASK \
31554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31565
31566 static tree
31567 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31568 {
31569 tree stmt, clauses, block;
31570 unsigned int save;
31571
31572 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31573 "#pragma acc data", pragma_tok);
31574
31575 block = begin_omp_parallel ();
31576 save = cp_parser_begin_omp_structured_block (parser);
31577 cp_parser_statement (parser, NULL_TREE, false, NULL);
31578 cp_parser_end_omp_structured_block (parser, save);
31579 stmt = finish_oacc_data (clauses, block);
31580 return stmt;
31581 }
31582
31583 /* OpenACC 2.0:
31584 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31585
31586 or
31587
31588 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31589
31590 LOC is the location of the #pragma token.
31591 */
31592
31593 #define OACC_ENTER_DATA_CLAUSE_MASK \
31594 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31601
31602 #define OACC_EXIT_DATA_CLAUSE_MASK \
31603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31608
31609 static tree
31610 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31611 bool enter)
31612 {
31613 tree stmt, clauses;
31614
31615 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31616 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31617 {
31618 cp_parser_error (parser, enter
31619 ? "expected %<data%> in %<#pragma acc enter data%>"
31620 : "expected %<data%> in %<#pragma acc exit data%>");
31621 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31622 return NULL_TREE;
31623 }
31624
31625 const char *p =
31626 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31627 if (strcmp (p, "data") != 0)
31628 {
31629 cp_parser_error (parser, "invalid pragma");
31630 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31631 return NULL_TREE;
31632 }
31633
31634 cp_lexer_consume_token (parser->lexer);
31635
31636 if (enter)
31637 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31638 "#pragma acc enter data", pragma_tok);
31639 else
31640 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31641 "#pragma acc exit data", pragma_tok);
31642
31643 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31644 {
31645 error_at (pragma_tok->location,
31646 "%<#pragma acc enter data%> has no data movement clause");
31647 return NULL_TREE;
31648 }
31649
31650 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31651 TREE_TYPE (stmt) = void_type_node;
31652 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31653 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31654 add_stmt (stmt);
31655 return stmt;
31656 }
31657
31658 /* OpenACC 2.0:
31659 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31660 structured-block */
31661
31662 #define OACC_KERNELS_CLAUSE_MASK \
31663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31676
31677 static tree
31678 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31679 {
31680 tree stmt, clauses, block;
31681 unsigned int save;
31682
31683 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31684 "#pragma acc kernels", pragma_tok);
31685
31686 block = begin_omp_parallel ();
31687 save = cp_parser_begin_omp_structured_block (parser);
31688 cp_parser_statement (parser, NULL_TREE, false, NULL);
31689 cp_parser_end_omp_structured_block (parser, save);
31690 stmt = finish_oacc_kernels (clauses, block);
31691 return stmt;
31692 }
31693
31694 /* OpenACC 2.0:
31695 # pragma acc loop oacc-loop-clause[optseq] new-line
31696 structured-block */
31697
31698 #define OACC_LOOP_CLAUSE_MASK \
31699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31701
31702 static tree
31703 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31704 {
31705 tree stmt, clauses, block;
31706 int save;
31707
31708 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31709 "#pragma acc loop", pragma_tok);
31710
31711 block = begin_omp_structured_block ();
31712 save = cp_parser_begin_omp_structured_block (parser);
31713 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31714 cp_parser_end_omp_structured_block (parser, save);
31715 add_stmt (finish_omp_structured_block (block));
31716 return stmt;
31717 }
31718
31719 /* OpenACC 2.0:
31720 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31721 structured-block */
31722
31723 #define OACC_PARALLEL_CLAUSE_MASK \
31724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31741
31742 static tree
31743 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31744 {
31745 tree stmt, clauses, block;
31746 unsigned int save;
31747
31748 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31749 "#pragma acc parallel", pragma_tok);
31750
31751 block = begin_omp_parallel ();
31752 save = cp_parser_begin_omp_structured_block (parser);
31753 cp_parser_statement (parser, NULL_TREE, false, NULL);
31754 cp_parser_end_omp_structured_block (parser, save);
31755 stmt = finish_oacc_parallel (clauses, block);
31756 return stmt;
31757 }
31758
31759 /* OpenACC 2.0:
31760 # pragma acc update oacc-update-clause[optseq] new-line
31761 */
31762
31763 #define OACC_UPDATE_CLAUSE_MASK \
31764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31770
31771 static tree
31772 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31773 {
31774 tree stmt, clauses;
31775
31776 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31777 "#pragma acc update", pragma_tok);
31778
31779 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31780 {
31781 error_at (pragma_tok->location,
31782 "%<#pragma acc update%> must contain at least one "
31783 "%<device%> or %<host/self%> clause");
31784 return NULL_TREE;
31785 }
31786
31787 stmt = make_node (OACC_UPDATE);
31788 TREE_TYPE (stmt) = void_type_node;
31789 OACC_UPDATE_CLAUSES (stmt) = clauses;
31790 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31791 add_stmt (stmt);
31792 return stmt;
31793 }
31794
31795 /* OpenACC 2.0:
31796 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31797
31798 LOC is the location of the #pragma token.
31799 */
31800
31801 #define OACC_WAIT_CLAUSE_MASK \
31802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31803
31804 static tree
31805 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31806 {
31807 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31808 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31809
31810 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31811 list = cp_parser_oacc_wait_list (parser, loc, list);
31812
31813 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31814 "#pragma acc wait", pragma_tok);
31815
31816 stmt = c_finish_oacc_wait (loc, list, clauses);
31817
31818 return stmt;
31819 }
31820
31821 /* OpenMP 4.0:
31822 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31823
31824 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31831
31832 static void
31833 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31834 enum pragma_context context)
31835 {
31836 bool first_p = parser->omp_declare_simd == NULL;
31837 cp_omp_declare_simd_data data;
31838 if (first_p)
31839 {
31840 data.error_seen = false;
31841 data.fndecl_seen = false;
31842 data.tokens = vNULL;
31843 parser->omp_declare_simd = &data;
31844 }
31845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31846 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31847 cp_lexer_consume_token (parser->lexer);
31848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31849 parser->omp_declare_simd->error_seen = true;
31850 cp_parser_require_pragma_eol (parser, pragma_tok);
31851 struct cp_token_cache *cp
31852 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31853 parser->omp_declare_simd->tokens.safe_push (cp);
31854 if (first_p)
31855 {
31856 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31857 cp_parser_pragma (parser, context);
31858 switch (context)
31859 {
31860 case pragma_external:
31861 cp_parser_declaration (parser);
31862 break;
31863 case pragma_member:
31864 cp_parser_member_declaration (parser);
31865 break;
31866 case pragma_objc_icode:
31867 cp_parser_block_declaration (parser, /*statement_p=*/false);
31868 break;
31869 default:
31870 cp_parser_declaration_statement (parser);
31871 break;
31872 }
31873 if (parser->omp_declare_simd
31874 && !parser->omp_declare_simd->error_seen
31875 && !parser->omp_declare_simd->fndecl_seen)
31876 error_at (pragma_tok->location,
31877 "%<#pragma omp declare simd%> not immediately followed by "
31878 "function declaration or definition");
31879 data.tokens.release ();
31880 parser->omp_declare_simd = NULL;
31881 }
31882 }
31883
31884 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31885 This function is modelled similar to the late parsing of omp declare
31886 simd. */
31887
31888 static tree
31889 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31890 {
31891 struct cp_token_cache *ce;
31892 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31893 int ii = 0;
31894
31895 if (parser->omp_declare_simd != NULL)
31896 {
31897 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31898 " marked as a Cilk Plus SIMD-enabled function");
31899 XDELETE (parser->cilk_simd_fn_info);
31900 parser->cilk_simd_fn_info = NULL;
31901 return attrs;
31902 }
31903 if (!info->error_seen && info->fndecl_seen)
31904 {
31905 error ("vector attribute not immediately followed by a single function"
31906 " declaration or definition");
31907 info->error_seen = true;
31908 }
31909 if (info->error_seen)
31910 return attrs;
31911
31912 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31913 {
31914 tree c, cl;
31915
31916 cp_parser_push_lexer_for_tokens (parser, ce);
31917 parser->lexer->in_pragma = true;
31918 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31919 "SIMD-enabled functions attribute",
31920 NULL);
31921 cp_parser_pop_lexer (parser);
31922 if (cl)
31923 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31924
31925 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31926 TREE_CHAIN (c) = attrs;
31927 attrs = c;
31928
31929 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31930 TREE_CHAIN (c) = attrs;
31931 if (processing_template_decl)
31932 ATTR_IS_DEPENDENT (c) = 1;
31933 attrs = c;
31934 }
31935 info->fndecl_seen = true;
31936 XDELETE (parser->cilk_simd_fn_info);
31937 parser->cilk_simd_fn_info = NULL;
31938 return attrs;
31939 }
31940
31941 /* Finalize #pragma omp declare simd clauses after direct declarator has
31942 been parsed, and put that into "omp declare simd" attribute. */
31943
31944 static tree
31945 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31946 {
31947 struct cp_token_cache *ce;
31948 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31949 int i;
31950
31951 if (!data->error_seen && data->fndecl_seen)
31952 {
31953 error ("%<#pragma omp declare simd%> not immediately followed by "
31954 "a single function declaration or definition");
31955 data->error_seen = true;
31956 return attrs;
31957 }
31958 if (data->error_seen)
31959 return attrs;
31960
31961 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31962 {
31963 tree c, cl;
31964
31965 cp_parser_push_lexer_for_tokens (parser, ce);
31966 parser->lexer->in_pragma = true;
31967 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31968 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31969 cp_lexer_consume_token (parser->lexer);
31970 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31971 "#pragma omp declare simd", pragma_tok);
31972 cp_parser_pop_lexer (parser);
31973 if (cl)
31974 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31975 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31976 TREE_CHAIN (c) = attrs;
31977 if (processing_template_decl)
31978 ATTR_IS_DEPENDENT (c) = 1;
31979 attrs = c;
31980 }
31981
31982 data->fndecl_seen = true;
31983 return attrs;
31984 }
31985
31986
31987 /* OpenMP 4.0:
31988 # pragma omp declare target new-line
31989 declarations and definitions
31990 # pragma omp end declare target new-line */
31991
31992 static void
31993 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31994 {
31995 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31996 scope_chain->omp_declare_target_attribute++;
31997 }
31998
31999 static void
32000 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32001 {
32002 const char *p = "";
32003 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32004 {
32005 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32006 p = IDENTIFIER_POINTER (id);
32007 }
32008 if (strcmp (p, "declare") == 0)
32009 {
32010 cp_lexer_consume_token (parser->lexer);
32011 p = "";
32012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32013 {
32014 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32015 p = IDENTIFIER_POINTER (id);
32016 }
32017 if (strcmp (p, "target") == 0)
32018 cp_lexer_consume_token (parser->lexer);
32019 else
32020 {
32021 cp_parser_error (parser, "expected %<target%>");
32022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32023 return;
32024 }
32025 }
32026 else
32027 {
32028 cp_parser_error (parser, "expected %<declare%>");
32029 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32030 return;
32031 }
32032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32033 if (!scope_chain->omp_declare_target_attribute)
32034 error_at (pragma_tok->location,
32035 "%<#pragma omp end declare target%> without corresponding "
32036 "%<#pragma omp declare target%>");
32037 else
32038 scope_chain->omp_declare_target_attribute--;
32039 }
32040
32041 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32042 expression and optional initializer clause of
32043 #pragma omp declare reduction. We store the expression(s) as
32044 either 3, 6 or 7 special statements inside of the artificial function's
32045 body. The first two statements are DECL_EXPRs for the artificial
32046 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32047 expression that uses those variables.
32048 If there was any INITIALIZER clause, this is followed by further statements,
32049 the fourth and fifth statements are DECL_EXPRs for the artificial
32050 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32051 constructor variant (first token after open paren is not omp_priv),
32052 then the sixth statement is a statement with the function call expression
32053 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32054 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32055 to initialize the OMP_PRIV artificial variable and there is seventh
32056 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32057
32058 static bool
32059 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32060 {
32061 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32062 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32063 type = TREE_TYPE (type);
32064 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32065 DECL_ARTIFICIAL (omp_out) = 1;
32066 pushdecl (omp_out);
32067 add_decl_expr (omp_out);
32068 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32069 DECL_ARTIFICIAL (omp_in) = 1;
32070 pushdecl (omp_in);
32071 add_decl_expr (omp_in);
32072 tree combiner;
32073 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32074
32075 keep_next_level (true);
32076 tree block = begin_omp_structured_block ();
32077 combiner = cp_parser_expression (parser);
32078 finish_expr_stmt (combiner);
32079 block = finish_omp_structured_block (block);
32080 add_stmt (block);
32081
32082 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32083 return false;
32084
32085 const char *p = "";
32086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32087 {
32088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32089 p = IDENTIFIER_POINTER (id);
32090 }
32091
32092 if (strcmp (p, "initializer") == 0)
32093 {
32094 cp_lexer_consume_token (parser->lexer);
32095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32096 return false;
32097
32098 p = "";
32099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32100 {
32101 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32102 p = IDENTIFIER_POINTER (id);
32103 }
32104
32105 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32106 DECL_ARTIFICIAL (omp_priv) = 1;
32107 pushdecl (omp_priv);
32108 add_decl_expr (omp_priv);
32109 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32110 DECL_ARTIFICIAL (omp_orig) = 1;
32111 pushdecl (omp_orig);
32112 add_decl_expr (omp_orig);
32113
32114 keep_next_level (true);
32115 block = begin_omp_structured_block ();
32116
32117 bool ctor = false;
32118 if (strcmp (p, "omp_priv") == 0)
32119 {
32120 bool is_direct_init, is_non_constant_init;
32121 ctor = true;
32122 cp_lexer_consume_token (parser->lexer);
32123 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32124 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32125 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32126 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32127 == CPP_CLOSE_PAREN
32128 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32129 == CPP_CLOSE_PAREN))
32130 {
32131 finish_omp_structured_block (block);
32132 error ("invalid initializer clause");
32133 return false;
32134 }
32135 initializer = cp_parser_initializer (parser, &is_direct_init,
32136 &is_non_constant_init);
32137 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32138 NULL_TREE, LOOKUP_ONLYCONVERTING);
32139 }
32140 else
32141 {
32142 cp_parser_parse_tentatively (parser);
32143 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32144 /*check_dependency_p=*/true,
32145 /*template_p=*/NULL,
32146 /*declarator_p=*/false,
32147 /*optional_p=*/false);
32148 vec<tree, va_gc> *args;
32149 if (fn_name == error_mark_node
32150 || cp_parser_error_occurred (parser)
32151 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32152 || ((args = cp_parser_parenthesized_expression_list
32153 (parser, non_attr, /*cast_p=*/false,
32154 /*allow_expansion_p=*/true,
32155 /*non_constant_p=*/NULL)),
32156 cp_parser_error_occurred (parser)))
32157 {
32158 finish_omp_structured_block (block);
32159 cp_parser_abort_tentative_parse (parser);
32160 cp_parser_error (parser, "expected id-expression (arguments)");
32161 return false;
32162 }
32163 unsigned int i;
32164 tree arg;
32165 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32166 if (arg == omp_priv
32167 || (TREE_CODE (arg) == ADDR_EXPR
32168 && TREE_OPERAND (arg, 0) == omp_priv))
32169 break;
32170 cp_parser_abort_tentative_parse (parser);
32171 if (arg == NULL_TREE)
32172 error ("one of the initializer call arguments should be %<omp_priv%>"
32173 " or %<&omp_priv%>");
32174 initializer = cp_parser_postfix_expression (parser, false, false, false,
32175 false, NULL);
32176 finish_expr_stmt (initializer);
32177 }
32178
32179 block = finish_omp_structured_block (block);
32180 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32181 add_stmt (block);
32182
32183 if (ctor)
32184 add_decl_expr (omp_orig);
32185
32186 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32187 return false;
32188 }
32189
32190 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32191 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32192
32193 return true;
32194 }
32195
32196 /* OpenMP 4.0
32197 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32198 initializer-clause[opt] new-line
32199
32200 initializer-clause:
32201 initializer (omp_priv initializer)
32202 initializer (function-name (argument-list)) */
32203
32204 static void
32205 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32206 enum pragma_context)
32207 {
32208 auto_vec<tree> types;
32209 enum tree_code reduc_code = ERROR_MARK;
32210 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32211 unsigned int i;
32212 cp_token *first_token;
32213 cp_token_cache *cp;
32214 int errs;
32215 void *p;
32216
32217 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32218 p = obstack_alloc (&declarator_obstack, 0);
32219
32220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32221 goto fail;
32222
32223 switch (cp_lexer_peek_token (parser->lexer)->type)
32224 {
32225 case CPP_PLUS:
32226 reduc_code = PLUS_EXPR;
32227 break;
32228 case CPP_MULT:
32229 reduc_code = MULT_EXPR;
32230 break;
32231 case CPP_MINUS:
32232 reduc_code = MINUS_EXPR;
32233 break;
32234 case CPP_AND:
32235 reduc_code = BIT_AND_EXPR;
32236 break;
32237 case CPP_XOR:
32238 reduc_code = BIT_XOR_EXPR;
32239 break;
32240 case CPP_OR:
32241 reduc_code = BIT_IOR_EXPR;
32242 break;
32243 case CPP_AND_AND:
32244 reduc_code = TRUTH_ANDIF_EXPR;
32245 break;
32246 case CPP_OR_OR:
32247 reduc_code = TRUTH_ORIF_EXPR;
32248 break;
32249 case CPP_NAME:
32250 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32251 break;
32252 default:
32253 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32254 "%<|%>, %<&&%>, %<||%> or identifier");
32255 goto fail;
32256 }
32257
32258 if (reduc_code != ERROR_MARK)
32259 cp_lexer_consume_token (parser->lexer);
32260
32261 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32262 if (reduc_id == error_mark_node)
32263 goto fail;
32264
32265 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32266 goto fail;
32267
32268 /* Types may not be defined in declare reduction type list. */
32269 const char *saved_message;
32270 saved_message = parser->type_definition_forbidden_message;
32271 parser->type_definition_forbidden_message
32272 = G_("types may not be defined in declare reduction type list");
32273 bool saved_colon_corrects_to_scope_p;
32274 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32275 parser->colon_corrects_to_scope_p = false;
32276 bool saved_colon_doesnt_start_class_def_p;
32277 saved_colon_doesnt_start_class_def_p
32278 = parser->colon_doesnt_start_class_def_p;
32279 parser->colon_doesnt_start_class_def_p = true;
32280
32281 while (true)
32282 {
32283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32284 type = cp_parser_type_id (parser);
32285 if (type == error_mark_node)
32286 ;
32287 else if (ARITHMETIC_TYPE_P (type)
32288 && (orig_reduc_id == NULL_TREE
32289 || (TREE_CODE (type) != COMPLEX_TYPE
32290 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32291 "min") == 0
32292 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32293 "max") == 0))))
32294 error_at (loc, "predeclared arithmetic type %qT in "
32295 "%<#pragma omp declare reduction%>", type);
32296 else if (TREE_CODE (type) == FUNCTION_TYPE
32297 || TREE_CODE (type) == METHOD_TYPE
32298 || TREE_CODE (type) == ARRAY_TYPE)
32299 error_at (loc, "function or array type %qT in "
32300 "%<#pragma omp declare reduction%>", type);
32301 else if (TREE_CODE (type) == REFERENCE_TYPE)
32302 error_at (loc, "reference type %qT in "
32303 "%<#pragma omp declare reduction%>", type);
32304 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32305 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32306 "%<#pragma omp declare reduction%>", type);
32307 else
32308 types.safe_push (type);
32309
32310 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32311 cp_lexer_consume_token (parser->lexer);
32312 else
32313 break;
32314 }
32315
32316 /* Restore the saved message. */
32317 parser->type_definition_forbidden_message = saved_message;
32318 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32319 parser->colon_doesnt_start_class_def_p
32320 = saved_colon_doesnt_start_class_def_p;
32321
32322 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32323 || types.is_empty ())
32324 {
32325 fail:
32326 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32327 goto done;
32328 }
32329
32330 first_token = cp_lexer_peek_token (parser->lexer);
32331 cp = NULL;
32332 errs = errorcount;
32333 FOR_EACH_VEC_ELT (types, i, type)
32334 {
32335 tree fntype
32336 = build_function_type_list (void_type_node,
32337 cp_build_reference_type (type, false),
32338 NULL_TREE);
32339 tree this_reduc_id = reduc_id;
32340 if (!dependent_type_p (type))
32341 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32342 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32343 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32344 DECL_ARTIFICIAL (fndecl) = 1;
32345 DECL_EXTERNAL (fndecl) = 1;
32346 DECL_DECLARED_INLINE_P (fndecl) = 1;
32347 DECL_IGNORED_P (fndecl) = 1;
32348 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32349 DECL_ATTRIBUTES (fndecl)
32350 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32351 DECL_ATTRIBUTES (fndecl));
32352 if (processing_template_decl)
32353 fndecl = push_template_decl (fndecl);
32354 bool block_scope = false;
32355 tree block = NULL_TREE;
32356 if (current_function_decl)
32357 {
32358 block_scope = true;
32359 DECL_CONTEXT (fndecl) = global_namespace;
32360 if (!processing_template_decl)
32361 pushdecl (fndecl);
32362 }
32363 else if (current_class_type)
32364 {
32365 if (cp == NULL)
32366 {
32367 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32368 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32369 cp_lexer_consume_token (parser->lexer);
32370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32371 goto fail;
32372 cp = cp_token_cache_new (first_token,
32373 cp_lexer_peek_nth_token (parser->lexer,
32374 2));
32375 }
32376 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32377 finish_member_declaration (fndecl);
32378 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32379 DECL_PENDING_INLINE_P (fndecl) = 1;
32380 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32381 continue;
32382 }
32383 else
32384 {
32385 DECL_CONTEXT (fndecl) = current_namespace;
32386 pushdecl (fndecl);
32387 }
32388 if (!block_scope)
32389 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32390 else
32391 block = begin_omp_structured_block ();
32392 if (cp)
32393 {
32394 cp_parser_push_lexer_for_tokens (parser, cp);
32395 parser->lexer->in_pragma = true;
32396 }
32397 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32398 {
32399 if (!block_scope)
32400 finish_function (0);
32401 else
32402 DECL_CONTEXT (fndecl) = current_function_decl;
32403 if (cp)
32404 cp_parser_pop_lexer (parser);
32405 goto fail;
32406 }
32407 if (cp)
32408 cp_parser_pop_lexer (parser);
32409 if (!block_scope)
32410 finish_function (0);
32411 else
32412 {
32413 DECL_CONTEXT (fndecl) = current_function_decl;
32414 block = finish_omp_structured_block (block);
32415 if (TREE_CODE (block) == BIND_EXPR)
32416 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32417 else if (TREE_CODE (block) == STATEMENT_LIST)
32418 DECL_SAVED_TREE (fndecl) = block;
32419 if (processing_template_decl)
32420 add_decl_expr (fndecl);
32421 }
32422 cp_check_omp_declare_reduction (fndecl);
32423 if (cp == NULL && types.length () > 1)
32424 cp = cp_token_cache_new (first_token,
32425 cp_lexer_peek_nth_token (parser->lexer, 2));
32426 if (errs != errorcount)
32427 break;
32428 }
32429
32430 cp_parser_require_pragma_eol (parser, pragma_tok);
32431
32432 done:
32433 /* Free any declarators allocated. */
32434 obstack_free (&declarator_obstack, p);
32435 }
32436
32437 /* OpenMP 4.0
32438 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32439 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32440 initializer-clause[opt] new-line
32441 #pragma omp declare target new-line */
32442
32443 static void
32444 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32445 enum pragma_context context)
32446 {
32447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32448 {
32449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32450 const char *p = IDENTIFIER_POINTER (id);
32451
32452 if (strcmp (p, "simd") == 0)
32453 {
32454 cp_lexer_consume_token (parser->lexer);
32455 cp_parser_omp_declare_simd (parser, pragma_tok,
32456 context);
32457 return;
32458 }
32459 cp_ensure_no_omp_declare_simd (parser);
32460 if (strcmp (p, "reduction") == 0)
32461 {
32462 cp_lexer_consume_token (parser->lexer);
32463 cp_parser_omp_declare_reduction (parser, pragma_tok,
32464 context);
32465 return;
32466 }
32467 if (!flag_openmp) /* flag_openmp_simd */
32468 {
32469 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32470 return;
32471 }
32472 if (strcmp (p, "target") == 0)
32473 {
32474 cp_lexer_consume_token (parser->lexer);
32475 cp_parser_omp_declare_target (parser, pragma_tok);
32476 return;
32477 }
32478 }
32479 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32480 "or %<target%>");
32481 cp_parser_require_pragma_eol (parser, pragma_tok);
32482 }
32483
32484 /* Main entry point to OpenMP statement pragmas. */
32485
32486 static void
32487 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32488 {
32489 tree stmt;
32490 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32491 omp_clause_mask mask (0);
32492
32493 switch (pragma_tok->pragma_kind)
32494 {
32495 case PRAGMA_OACC_CACHE:
32496 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32497 break;
32498 case PRAGMA_OACC_DATA:
32499 stmt = cp_parser_oacc_data (parser, pragma_tok);
32500 break;
32501 case PRAGMA_OACC_ENTER_DATA:
32502 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32503 break;
32504 case PRAGMA_OACC_EXIT_DATA:
32505 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32506 break;
32507 case PRAGMA_OACC_KERNELS:
32508 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32509 break;
32510 case PRAGMA_OACC_LOOP:
32511 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32512 break;
32513 case PRAGMA_OACC_PARALLEL:
32514 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32515 break;
32516 case PRAGMA_OACC_UPDATE:
32517 stmt = cp_parser_oacc_update (parser, pragma_tok);
32518 break;
32519 case PRAGMA_OACC_WAIT:
32520 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32521 break;
32522 case PRAGMA_OMP_ATOMIC:
32523 cp_parser_omp_atomic (parser, pragma_tok);
32524 return;
32525 case PRAGMA_OMP_CRITICAL:
32526 stmt = cp_parser_omp_critical (parser, pragma_tok);
32527 break;
32528 case PRAGMA_OMP_DISTRIBUTE:
32529 strcpy (p_name, "#pragma omp");
32530 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32531 break;
32532 case PRAGMA_OMP_FOR:
32533 strcpy (p_name, "#pragma omp");
32534 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32535 break;
32536 case PRAGMA_OMP_MASTER:
32537 stmt = cp_parser_omp_master (parser, pragma_tok);
32538 break;
32539 case PRAGMA_OMP_ORDERED:
32540 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32541 break;
32542 case PRAGMA_OMP_PARALLEL:
32543 strcpy (p_name, "#pragma omp");
32544 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32545 break;
32546 case PRAGMA_OMP_SECTIONS:
32547 strcpy (p_name, "#pragma omp");
32548 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32549 break;
32550 case PRAGMA_OMP_SIMD:
32551 strcpy (p_name, "#pragma omp");
32552 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32553 break;
32554 case PRAGMA_OMP_SINGLE:
32555 stmt = cp_parser_omp_single (parser, pragma_tok);
32556 break;
32557 case PRAGMA_OMP_TASK:
32558 stmt = cp_parser_omp_task (parser, pragma_tok);
32559 break;
32560 case PRAGMA_OMP_TASKGROUP:
32561 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32562 break;
32563 case PRAGMA_OMP_TEAMS:
32564 strcpy (p_name, "#pragma omp");
32565 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32566 break;
32567 default:
32568 gcc_unreachable ();
32569 }
32570
32571 if (stmt)
32572 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32573 }
32574 \f
32575 /* Transactional Memory parsing routines. */
32576
32577 /* Parse a transaction attribute.
32578
32579 txn-attribute:
32580 attribute
32581 [ [ identifier ] ]
32582
32583 ??? Simplify this when C++0x bracket attributes are
32584 implemented properly. */
32585
32586 static tree
32587 cp_parser_txn_attribute_opt (cp_parser *parser)
32588 {
32589 cp_token *token;
32590 tree attr_name, attr = NULL;
32591
32592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32593 return cp_parser_attributes_opt (parser);
32594
32595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32596 return NULL_TREE;
32597 cp_lexer_consume_token (parser->lexer);
32598 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32599 goto error1;
32600
32601 token = cp_lexer_peek_token (parser->lexer);
32602 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32603 {
32604 token = cp_lexer_consume_token (parser->lexer);
32605
32606 attr_name = (token->type == CPP_KEYWORD
32607 /* For keywords, use the canonical spelling,
32608 not the parsed identifier. */
32609 ? ridpointers[(int) token->keyword]
32610 : token->u.value);
32611 attr = build_tree_list (attr_name, NULL_TREE);
32612 }
32613 else
32614 cp_parser_error (parser, "expected identifier");
32615
32616 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32617 error1:
32618 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32619 return attr;
32620 }
32621
32622 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32623
32624 transaction-statement:
32625 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32626 compound-statement
32627 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32628 */
32629
32630 static tree
32631 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32632 {
32633 unsigned char old_in = parser->in_transaction;
32634 unsigned char this_in = 1, new_in;
32635 cp_token *token;
32636 tree stmt, attrs, noex;
32637
32638 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32639 || keyword == RID_TRANSACTION_RELAXED);
32640 token = cp_parser_require_keyword (parser, keyword,
32641 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32642 : RT_TRANSACTION_RELAXED));
32643 gcc_assert (token != NULL);
32644
32645 if (keyword == RID_TRANSACTION_RELAXED)
32646 this_in |= TM_STMT_ATTR_RELAXED;
32647 else
32648 {
32649 attrs = cp_parser_txn_attribute_opt (parser);
32650 if (attrs)
32651 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32652 }
32653
32654 /* Parse a noexcept specification. */
32655 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32656
32657 /* Keep track if we're in the lexical scope of an outer transaction. */
32658 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32659
32660 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32661
32662 parser->in_transaction = new_in;
32663 cp_parser_compound_statement (parser, NULL, false, false);
32664 parser->in_transaction = old_in;
32665
32666 finish_transaction_stmt (stmt, NULL, this_in, noex);
32667
32668 return stmt;
32669 }
32670
32671 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32672
32673 transaction-expression:
32674 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32675 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32676 */
32677
32678 static tree
32679 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32680 {
32681 unsigned char old_in = parser->in_transaction;
32682 unsigned char this_in = 1;
32683 cp_token *token;
32684 tree expr, noex;
32685 bool noex_expr;
32686
32687 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32688 || keyword == RID_TRANSACTION_RELAXED);
32689
32690 if (!flag_tm)
32691 error (keyword == RID_TRANSACTION_RELAXED
32692 ? G_("%<__transaction_relaxed%> without transactional memory "
32693 "support enabled")
32694 : G_("%<__transaction_atomic%> without transactional memory "
32695 "support enabled"));
32696
32697 token = cp_parser_require_keyword (parser, keyword,
32698 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32699 : RT_TRANSACTION_RELAXED));
32700 gcc_assert (token != NULL);
32701
32702 if (keyword == RID_TRANSACTION_RELAXED)
32703 this_in |= TM_STMT_ATTR_RELAXED;
32704
32705 /* Set this early. This might mean that we allow transaction_cancel in
32706 an expression that we find out later actually has to be a constexpr.
32707 However, we expect that cxx_constant_value will be able to deal with
32708 this; also, if the noexcept has no constexpr, then what we parse next
32709 really is a transaction's body. */
32710 parser->in_transaction = this_in;
32711
32712 /* Parse a noexcept specification. */
32713 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32714 true);
32715
32716 if (!noex || !noex_expr
32717 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32718 {
32719 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32720
32721 expr = cp_parser_expression (parser);
32722 expr = finish_parenthesized_expr (expr);
32723
32724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32725 }
32726 else
32727 {
32728 /* The only expression that is available got parsed for the noexcept
32729 already. noexcept is true then. */
32730 expr = noex;
32731 noex = boolean_true_node;
32732 }
32733
32734 expr = build_transaction_expr (token->location, expr, this_in, noex);
32735 parser->in_transaction = old_in;
32736
32737 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32738 return error_mark_node;
32739
32740 return (flag_tm ? expr : error_mark_node);
32741 }
32742
32743 /* Parse a function-transaction-block.
32744
32745 function-transaction-block:
32746 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32747 function-body
32748 __transaction_atomic txn-attribute[opt] function-try-block
32749 __transaction_relaxed ctor-initializer[opt] function-body
32750 __transaction_relaxed function-try-block
32751 */
32752
32753 static bool
32754 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32755 {
32756 unsigned char old_in = parser->in_transaction;
32757 unsigned char new_in = 1;
32758 tree compound_stmt, stmt, attrs;
32759 bool ctor_initializer_p;
32760 cp_token *token;
32761
32762 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32763 || keyword == RID_TRANSACTION_RELAXED);
32764 token = cp_parser_require_keyword (parser, keyword,
32765 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32766 : RT_TRANSACTION_RELAXED));
32767 gcc_assert (token != NULL);
32768
32769 if (keyword == RID_TRANSACTION_RELAXED)
32770 new_in |= TM_STMT_ATTR_RELAXED;
32771 else
32772 {
32773 attrs = cp_parser_txn_attribute_opt (parser);
32774 if (attrs)
32775 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32776 }
32777
32778 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32779
32780 parser->in_transaction = new_in;
32781
32782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32783 ctor_initializer_p = cp_parser_function_try_block (parser);
32784 else
32785 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32786 (parser, /*in_function_try_block=*/false);
32787
32788 parser->in_transaction = old_in;
32789
32790 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32791
32792 return ctor_initializer_p;
32793 }
32794
32795 /* Parse a __transaction_cancel statement.
32796
32797 cancel-statement:
32798 __transaction_cancel txn-attribute[opt] ;
32799 __transaction_cancel txn-attribute[opt] throw-expression ;
32800
32801 ??? Cancel and throw is not yet implemented. */
32802
32803 static tree
32804 cp_parser_transaction_cancel (cp_parser *parser)
32805 {
32806 cp_token *token;
32807 bool is_outer = false;
32808 tree stmt, attrs;
32809
32810 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32811 RT_TRANSACTION_CANCEL);
32812 gcc_assert (token != NULL);
32813
32814 attrs = cp_parser_txn_attribute_opt (parser);
32815 if (attrs)
32816 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32817
32818 /* ??? Parse cancel-and-throw here. */
32819
32820 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32821
32822 if (!flag_tm)
32823 {
32824 error_at (token->location, "%<__transaction_cancel%> without "
32825 "transactional memory support enabled");
32826 return error_mark_node;
32827 }
32828 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32829 {
32830 error_at (token->location, "%<__transaction_cancel%> within a "
32831 "%<__transaction_relaxed%>");
32832 return error_mark_node;
32833 }
32834 else if (is_outer)
32835 {
32836 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32837 && !is_tm_may_cancel_outer (current_function_decl))
32838 {
32839 error_at (token->location, "outer %<__transaction_cancel%> not "
32840 "within outer %<__transaction_atomic%>");
32841 error_at (token->location,
32842 " or a %<transaction_may_cancel_outer%> function");
32843 return error_mark_node;
32844 }
32845 }
32846 else if (parser->in_transaction == 0)
32847 {
32848 error_at (token->location, "%<__transaction_cancel%> not within "
32849 "%<__transaction_atomic%>");
32850 return error_mark_node;
32851 }
32852
32853 stmt = build_tm_abort_call (token->location, is_outer);
32854 add_stmt (stmt);
32855
32856 return stmt;
32857 }
32858 \f
32859 /* The parser. */
32860
32861 static GTY (()) cp_parser *the_parser;
32862
32863 \f
32864 /* Special handling for the first token or line in the file. The first
32865 thing in the file might be #pragma GCC pch_preprocess, which loads a
32866 PCH file, which is a GC collection point. So we need to handle this
32867 first pragma without benefit of an existing lexer structure.
32868
32869 Always returns one token to the caller in *FIRST_TOKEN. This is
32870 either the true first token of the file, or the first token after
32871 the initial pragma. */
32872
32873 static void
32874 cp_parser_initial_pragma (cp_token *first_token)
32875 {
32876 tree name = NULL;
32877
32878 cp_lexer_get_preprocessor_token (NULL, first_token);
32879 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32880 return;
32881
32882 cp_lexer_get_preprocessor_token (NULL, first_token);
32883 if (first_token->type == CPP_STRING)
32884 {
32885 name = first_token->u.value;
32886
32887 cp_lexer_get_preprocessor_token (NULL, first_token);
32888 if (first_token->type != CPP_PRAGMA_EOL)
32889 error_at (first_token->location,
32890 "junk at end of %<#pragma GCC pch_preprocess%>");
32891 }
32892 else
32893 error_at (first_token->location, "expected string literal");
32894
32895 /* Skip to the end of the pragma. */
32896 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32897 cp_lexer_get_preprocessor_token (NULL, first_token);
32898
32899 /* Now actually load the PCH file. */
32900 if (name)
32901 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32902
32903 /* Read one more token to return to our caller. We have to do this
32904 after reading the PCH file in, since its pointers have to be
32905 live. */
32906 cp_lexer_get_preprocessor_token (NULL, first_token);
32907 }
32908
32909 /* Parses the grainsize pragma for the _Cilk_for statement.
32910 Syntax:
32911 #pragma cilk grainsize = <VALUE>. */
32912
32913 static void
32914 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32915 {
32916 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32917 {
32918 tree exp = cp_parser_binary_expression (parser, false, false,
32919 PREC_NOT_OPERATOR, NULL);
32920 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32921 if (!exp || exp == error_mark_node)
32922 {
32923 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32924 return;
32925 }
32926
32927 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32929 cp_parser_cilk_for (parser, exp);
32930 else
32931 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32932 "%<#pragma cilk grainsize%> is not followed by "
32933 "%<_Cilk_for%>");
32934 return;
32935 }
32936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32937 }
32938
32939 /* Normal parsing of a pragma token. Here we can (and must) use the
32940 regular lexer. */
32941
32942 static bool
32943 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32944 {
32945 cp_token *pragma_tok;
32946 unsigned int id;
32947
32948 pragma_tok = cp_lexer_consume_token (parser->lexer);
32949 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32950 parser->lexer->in_pragma = true;
32951
32952 id = pragma_tok->pragma_kind;
32953 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32954 cp_ensure_no_omp_declare_simd (parser);
32955 switch (id)
32956 {
32957 case PRAGMA_GCC_PCH_PREPROCESS:
32958 error_at (pragma_tok->location,
32959 "%<#pragma GCC pch_preprocess%> must be first");
32960 break;
32961
32962 case PRAGMA_OMP_BARRIER:
32963 switch (context)
32964 {
32965 case pragma_compound:
32966 cp_parser_omp_barrier (parser, pragma_tok);
32967 return false;
32968 case pragma_stmt:
32969 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32970 "used in compound statements");
32971 break;
32972 default:
32973 goto bad_stmt;
32974 }
32975 break;
32976
32977 case PRAGMA_OMP_FLUSH:
32978 switch (context)
32979 {
32980 case pragma_compound:
32981 cp_parser_omp_flush (parser, pragma_tok);
32982 return false;
32983 case pragma_stmt:
32984 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32985 "used in compound statements");
32986 break;
32987 default:
32988 goto bad_stmt;
32989 }
32990 break;
32991
32992 case PRAGMA_OMP_TASKWAIT:
32993 switch (context)
32994 {
32995 case pragma_compound:
32996 cp_parser_omp_taskwait (parser, pragma_tok);
32997 return false;
32998 case pragma_stmt:
32999 error_at (pragma_tok->location,
33000 "%<#pragma omp taskwait%> may only be "
33001 "used in compound statements");
33002 break;
33003 default:
33004 goto bad_stmt;
33005 }
33006 break;
33007
33008 case PRAGMA_OMP_TASKYIELD:
33009 switch (context)
33010 {
33011 case pragma_compound:
33012 cp_parser_omp_taskyield (parser, pragma_tok);
33013 return false;
33014 case pragma_stmt:
33015 error_at (pragma_tok->location,
33016 "%<#pragma omp taskyield%> may only be "
33017 "used in compound statements");
33018 break;
33019 default:
33020 goto bad_stmt;
33021 }
33022 break;
33023
33024 case PRAGMA_OMP_CANCEL:
33025 switch (context)
33026 {
33027 case pragma_compound:
33028 cp_parser_omp_cancel (parser, pragma_tok);
33029 return false;
33030 case pragma_stmt:
33031 error_at (pragma_tok->location,
33032 "%<#pragma omp cancel%> may only be "
33033 "used in compound statements");
33034 break;
33035 default:
33036 goto bad_stmt;
33037 }
33038 break;
33039
33040 case PRAGMA_OMP_CANCELLATION_POINT:
33041 switch (context)
33042 {
33043 case pragma_compound:
33044 cp_parser_omp_cancellation_point (parser, pragma_tok);
33045 return false;
33046 case pragma_stmt:
33047 error_at (pragma_tok->location,
33048 "%<#pragma omp cancellation point%> may only be "
33049 "used in compound statements");
33050 break;
33051 default:
33052 goto bad_stmt;
33053 }
33054 break;
33055
33056 case PRAGMA_OMP_THREADPRIVATE:
33057 cp_parser_omp_threadprivate (parser, pragma_tok);
33058 return false;
33059
33060 case PRAGMA_OMP_DECLARE_REDUCTION:
33061 cp_parser_omp_declare (parser, pragma_tok, context);
33062 return false;
33063
33064 case PRAGMA_OACC_CACHE:
33065 case PRAGMA_OACC_DATA:
33066 case PRAGMA_OACC_ENTER_DATA:
33067 case PRAGMA_OACC_EXIT_DATA:
33068 case PRAGMA_OACC_KERNELS:
33069 case PRAGMA_OACC_PARALLEL:
33070 case PRAGMA_OACC_LOOP:
33071 case PRAGMA_OACC_UPDATE:
33072 case PRAGMA_OACC_WAIT:
33073 case PRAGMA_OMP_ATOMIC:
33074 case PRAGMA_OMP_CRITICAL:
33075 case PRAGMA_OMP_DISTRIBUTE:
33076 case PRAGMA_OMP_FOR:
33077 case PRAGMA_OMP_MASTER:
33078 case PRAGMA_OMP_ORDERED:
33079 case PRAGMA_OMP_PARALLEL:
33080 case PRAGMA_OMP_SECTIONS:
33081 case PRAGMA_OMP_SIMD:
33082 case PRAGMA_OMP_SINGLE:
33083 case PRAGMA_OMP_TASK:
33084 case PRAGMA_OMP_TASKGROUP:
33085 case PRAGMA_OMP_TEAMS:
33086 if (context != pragma_stmt && context != pragma_compound)
33087 goto bad_stmt;
33088 cp_parser_omp_construct (parser, pragma_tok);
33089 return true;
33090
33091 case PRAGMA_OMP_TARGET:
33092 return cp_parser_omp_target (parser, pragma_tok, context);
33093
33094 case PRAGMA_OMP_END_DECLARE_TARGET:
33095 cp_parser_omp_end_declare_target (parser, pragma_tok);
33096 return false;
33097
33098 case PRAGMA_OMP_SECTION:
33099 error_at (pragma_tok->location,
33100 "%<#pragma omp section%> may only be used in "
33101 "%<#pragma omp sections%> construct");
33102 break;
33103
33104 case PRAGMA_IVDEP:
33105 {
33106 if (context == pragma_external)
33107 {
33108 error_at (pragma_tok->location,
33109 "%<#pragma GCC ivdep%> must be inside a function");
33110 break;
33111 }
33112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33113 cp_token *tok;
33114 tok = cp_lexer_peek_token (the_parser->lexer);
33115 if (tok->type != CPP_KEYWORD
33116 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33117 && tok->keyword != RID_DO))
33118 {
33119 cp_parser_error (parser, "for, while or do statement expected");
33120 return false;
33121 }
33122 cp_parser_iteration_statement (parser, true);
33123 return true;
33124 }
33125
33126 case PRAGMA_CILK_SIMD:
33127 if (context == pragma_external)
33128 {
33129 error_at (pragma_tok->location,
33130 "%<#pragma simd%> must be inside a function");
33131 break;
33132 }
33133 cp_parser_cilk_simd (parser, pragma_tok);
33134 return true;
33135
33136 case PRAGMA_CILK_GRAINSIZE:
33137 if (context == pragma_external)
33138 {
33139 error_at (pragma_tok->location,
33140 "%<#pragma cilk grainsize%> must be inside a function");
33141 break;
33142 }
33143
33144 /* Ignore the pragma if Cilk Plus is not enabled. */
33145 if (flag_cilkplus)
33146 {
33147 cp_parser_cilk_grainsize (parser, pragma_tok);
33148 return true;
33149 }
33150 else
33151 {
33152 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33153 "%<#pragma cilk grainsize%>");
33154 break;
33155 }
33156
33157 default:
33158 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33159 c_invoke_pragma_handler (id);
33160 break;
33161
33162 bad_stmt:
33163 cp_parser_error (parser, "expected declaration specifiers");
33164 break;
33165 }
33166
33167 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33168 return false;
33169 }
33170
33171 /* The interface the pragma parsers have to the lexer. */
33172
33173 enum cpp_ttype
33174 pragma_lex (tree *value)
33175 {
33176 cp_token *tok;
33177 enum cpp_ttype ret;
33178
33179 tok = cp_lexer_peek_token (the_parser->lexer);
33180
33181 ret = tok->type;
33182 *value = tok->u.value;
33183
33184 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33185 ret = CPP_EOF;
33186 else if (ret == CPP_STRING)
33187 *value = cp_parser_string_literal (the_parser, false, false);
33188 else
33189 {
33190 cp_lexer_consume_token (the_parser->lexer);
33191 if (ret == CPP_KEYWORD)
33192 ret = CPP_NAME;
33193 }
33194
33195 return ret;
33196 }
33197
33198 \f
33199 /* External interface. */
33200
33201 /* Parse one entire translation unit. */
33202
33203 void
33204 c_parse_file (void)
33205 {
33206 static bool already_called = false;
33207
33208 if (already_called)
33209 fatal_error (input_location,
33210 "inter-module optimizations not implemented for C++");
33211 already_called = true;
33212
33213 the_parser = cp_parser_new ();
33214 push_deferring_access_checks (flag_access_control
33215 ? dk_no_deferred : dk_no_check);
33216 cp_parser_translation_unit (the_parser);
33217 the_parser = NULL;
33218 }
33219
33220 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33221 vectorlength clause:
33222 Syntax:
33223 vectorlength ( constant-expression ) */
33224
33225 static tree
33226 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33227 bool is_simd_fn)
33228 {
33229 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33230 tree expr;
33231 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33232 safelen clause. Thus, vectorlength is represented as OMP 4.0
33233 safelen. For SIMD-enabled function it is represented by OMP 4.0
33234 simdlen. */
33235 if (!is_simd_fn)
33236 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33237 loc);
33238 else
33239 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33240 loc);
33241
33242 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33243 return error_mark_node;
33244
33245 expr = cp_parser_constant_expression (parser);
33246 expr = maybe_constant_value (expr);
33247
33248 /* If expr == error_mark_node, then don't emit any errors nor
33249 create a clause. if any of the above functions returns
33250 error mark node then they would have emitted an error message. */
33251 if (expr == error_mark_node)
33252 ;
33253 else if (!TREE_TYPE (expr)
33254 || !TREE_CONSTANT (expr)
33255 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33256 error_at (loc, "vectorlength must be an integer constant");
33257 else if (TREE_CONSTANT (expr)
33258 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33259 error_at (loc, "vectorlength must be a power of 2");
33260 else
33261 {
33262 tree c;
33263 if (!is_simd_fn)
33264 {
33265 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33266 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33267 OMP_CLAUSE_CHAIN (c) = clauses;
33268 clauses = c;
33269 }
33270 else
33271 {
33272 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33273 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33274 OMP_CLAUSE_CHAIN (c) = clauses;
33275 clauses = c;
33276 }
33277 }
33278
33279 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33280 return error_mark_node;
33281 return clauses;
33282 }
33283
33284 /* Handles the Cilk Plus #pragma simd linear clause.
33285 Syntax:
33286 linear ( simd-linear-variable-list )
33287
33288 simd-linear-variable-list:
33289 simd-linear-variable
33290 simd-linear-variable-list , simd-linear-variable
33291
33292 simd-linear-variable:
33293 id-expression
33294 id-expression : simd-linear-step
33295
33296 simd-linear-step:
33297 conditional-expression */
33298
33299 static tree
33300 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33301 {
33302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33303
33304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33305 return clauses;
33306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33307 {
33308 cp_parser_error (parser, "expected identifier");
33309 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33310 return error_mark_node;
33311 }
33312
33313 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33314 parser->colon_corrects_to_scope_p = false;
33315 while (1)
33316 {
33317 cp_token *token = cp_lexer_peek_token (parser->lexer);
33318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33319 {
33320 cp_parser_error (parser, "expected variable-name");
33321 clauses = error_mark_node;
33322 break;
33323 }
33324
33325 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33326 false, false);
33327 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33328 token->location);
33329 if (decl == error_mark_node)
33330 {
33331 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33332 token->location);
33333 clauses = error_mark_node;
33334 }
33335 else
33336 {
33337 tree e = NULL_TREE;
33338 tree step_size = integer_one_node;
33339
33340 /* If present, parse the linear step. Otherwise, assume the default
33341 value of 1. */
33342 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33343 {
33344 cp_lexer_consume_token (parser->lexer);
33345
33346 e = cp_parser_assignment_expression (parser);
33347 e = maybe_constant_value (e);
33348
33349 if (e == error_mark_node)
33350 {
33351 /* If an error has occurred, then the whole pragma is
33352 considered ill-formed. Thus, no reason to keep
33353 parsing. */
33354 clauses = error_mark_node;
33355 break;
33356 }
33357 else if (type_dependent_expression_p (e)
33358 || value_dependent_expression_p (e)
33359 || (TREE_TYPE (e)
33360 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33361 && (TREE_CONSTANT (e)
33362 || DECL_P (e))))
33363 step_size = e;
33364 else
33365 cp_parser_error (parser,
33366 "step size must be an integer constant "
33367 "expression or an integer variable");
33368 }
33369
33370 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33371 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33372 OMP_CLAUSE_DECL (l) = decl;
33373 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33374 OMP_CLAUSE_CHAIN (l) = clauses;
33375 clauses = l;
33376 }
33377 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33378 cp_lexer_consume_token (parser->lexer);
33379 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33380 break;
33381 else
33382 {
33383 error_at (cp_lexer_peek_token (parser->lexer)->location,
33384 "expected %<,%> or %<)%> after %qE", decl);
33385 clauses = error_mark_node;
33386 break;
33387 }
33388 }
33389 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33390 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33391 return clauses;
33392 }
33393
33394 /* Returns the name of the next clause. If the clause is not
33395 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33396 token is not consumed. Otherwise, the appropriate enum from the
33397 pragma_simd_clause is returned and the token is consumed. */
33398
33399 static pragma_omp_clause
33400 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33401 {
33402 pragma_omp_clause clause_type;
33403 cp_token *token = cp_lexer_peek_token (parser->lexer);
33404
33405 if (token->keyword == RID_PRIVATE)
33406 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33407 else if (!token->u.value || token->type != CPP_NAME)
33408 return PRAGMA_CILK_CLAUSE_NONE;
33409 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33410 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33411 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33412 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33413 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33414 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33415 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33416 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33417 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33418 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33419 else
33420 return PRAGMA_CILK_CLAUSE_NONE;
33421
33422 cp_lexer_consume_token (parser->lexer);
33423 return clause_type;
33424 }
33425
33426 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33427
33428 static tree
33429 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33430 {
33431 tree clauses = NULL_TREE;
33432
33433 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33434 && clauses != error_mark_node)
33435 {
33436 pragma_omp_clause c_kind;
33437 c_kind = cp_parser_cilk_simd_clause_name (parser);
33438 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33439 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33440 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33441 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33442 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33443 /* Use the OpenMP 4.0 equivalent function. */
33444 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33445 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33446 /* Use the OpenMP 4.0 equivalent function. */
33447 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33448 clauses);
33449 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33450 /* Use the OMP 4.0 equivalent function. */
33451 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33452 clauses);
33453 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33454 /* Use the OMP 4.0 equivalent function. */
33455 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33456 else
33457 {
33458 clauses = error_mark_node;
33459 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33460 break;
33461 }
33462 }
33463
33464 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33465
33466 if (clauses == error_mark_node)
33467 return error_mark_node;
33468 else
33469 return c_finish_cilk_clauses (clauses);
33470 }
33471
33472 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33473
33474 static void
33475 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33476 {
33477 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33478
33479 if (clauses == error_mark_node)
33480 return;
33481
33482 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33483 {
33484 error_at (cp_lexer_peek_token (parser->lexer)->location,
33485 "for statement expected");
33486 return;
33487 }
33488
33489 tree sb = begin_omp_structured_block ();
33490 int save = cp_parser_begin_omp_structured_block (parser);
33491 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33492 if (ret)
33493 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33494 cp_parser_end_omp_structured_block (parser, save);
33495 add_stmt (finish_omp_structured_block (sb));
33496 }
33497
33498 /* Main entry-point for parsing Cilk Plus _Cilk_for
33499 loops. The return value is error_mark_node
33500 when errors happen and CILK_FOR tree on success. */
33501
33502 static tree
33503 cp_parser_cilk_for (cp_parser *parser, tree grain)
33504 {
33505 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33506 gcc_unreachable ();
33507
33508 tree sb = begin_omp_structured_block ();
33509 int save = cp_parser_begin_omp_structured_block (parser);
33510
33511 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33512 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33513 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33514 clauses = finish_omp_clauses (clauses);
33515
33516 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33517 if (ret)
33518 cpp_validate_cilk_plus_loop (ret);
33519 else
33520 ret = error_mark_node;
33521
33522 cp_parser_end_omp_structured_block (parser, save);
33523 add_stmt (finish_omp_structured_block (sb));
33524 return ret;
33525 }
33526
33527 /* Create an identifier for a generic parameter type (a synthesized
33528 template parameter implied by `auto' or a concept identifier). */
33529
33530 static GTY(()) int generic_parm_count;
33531 static tree
33532 make_generic_type_name ()
33533 {
33534 char buf[32];
33535 sprintf (buf, "auto:%d", ++generic_parm_count);
33536 return get_identifier (buf);
33537 }
33538
33539 /* Predicate that behaves as is_auto_or_concept but matches the parent
33540 node of the generic type rather than the generic type itself. This
33541 allows for type transformation in add_implicit_template_parms. */
33542
33543 static inline bool
33544 tree_type_is_auto_or_concept (const_tree t)
33545 {
33546 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33547 }
33548
33549 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33550 (creating a new template parameter list if necessary). Returns the newly
33551 created template type parm. */
33552
33553 tree
33554 synthesize_implicit_template_parm (cp_parser *parser)
33555 {
33556 gcc_assert (current_binding_level->kind == sk_function_parms);
33557
33558 /* We are either continuing a function template that already contains implicit
33559 template parameters, creating a new fully-implicit function template, or
33560 extending an existing explicit function template with implicit template
33561 parameters. */
33562
33563 cp_binding_level *const entry_scope = current_binding_level;
33564
33565 bool become_template = false;
33566 cp_binding_level *parent_scope = 0;
33567
33568 if (parser->implicit_template_scope)
33569 {
33570 gcc_assert (parser->implicit_template_parms);
33571
33572 current_binding_level = parser->implicit_template_scope;
33573 }
33574 else
33575 {
33576 /* Roll back to the existing template parameter scope (in the case of
33577 extending an explicit function template) or introduce a new template
33578 parameter scope ahead of the function parameter scope (or class scope
33579 in the case of out-of-line member definitions). The function scope is
33580 added back after template parameter synthesis below. */
33581
33582 cp_binding_level *scope = entry_scope;
33583
33584 while (scope->kind == sk_function_parms)
33585 {
33586 parent_scope = scope;
33587 scope = scope->level_chain;
33588 }
33589 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33590 {
33591 /* If not defining a class, then any class scope is a scope level in
33592 an out-of-line member definition. In this case simply wind back
33593 beyond the first such scope to inject the template parameter list.
33594 Otherwise wind back to the class being defined. The latter can
33595 occur in class member friend declarations such as:
33596
33597 class A {
33598 void foo (auto);
33599 };
33600 class B {
33601 friend void A::foo (auto);
33602 };
33603
33604 The template parameter list synthesized for the friend declaration
33605 must be injected in the scope of 'B'. This can also occur in
33606 erroneous cases such as:
33607
33608 struct A {
33609 struct B {
33610 void foo (auto);
33611 };
33612 void B::foo (auto) {}
33613 };
33614
33615 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33616 but, nevertheless, the template parameter list synthesized for the
33617 declarator should be injected into the scope of 'A' as if the
33618 ill-formed template was specified explicitly. */
33619
33620 while (scope->kind == sk_class && !scope->defining_class_p)
33621 {
33622 parent_scope = scope;
33623 scope = scope->level_chain;
33624 }
33625 }
33626
33627 current_binding_level = scope;
33628
33629 if (scope->kind != sk_template_parms
33630 || !function_being_declared_is_template_p (parser))
33631 {
33632 /* Introduce a new template parameter list for implicit template
33633 parameters. */
33634
33635 become_template = true;
33636
33637 parser->implicit_template_scope
33638 = begin_scope (sk_template_parms, NULL);
33639
33640 ++processing_template_decl;
33641
33642 parser->fully_implicit_function_template_p = true;
33643 ++parser->num_template_parameter_lists;
33644 }
33645 else
33646 {
33647 /* Synthesize implicit template parameters at the end of the explicit
33648 template parameter list. */
33649
33650 gcc_assert (current_template_parms);
33651
33652 parser->implicit_template_scope = scope;
33653
33654 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33655 parser->implicit_template_parms
33656 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33657 }
33658 }
33659
33660 /* Synthesize a new template parameter and track the current template
33661 parameter chain with implicit_template_parms. */
33662
33663 tree synth_id = make_generic_type_name ();
33664 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33665 synth_id);
33666 tree new_parm
33667 = process_template_parm (parser->implicit_template_parms,
33668 input_location,
33669 build_tree_list (NULL_TREE, synth_tmpl_parm),
33670 /*non_type=*/false,
33671 /*param_pack=*/false);
33672
33673
33674 if (parser->implicit_template_parms)
33675 parser->implicit_template_parms
33676 = TREE_CHAIN (parser->implicit_template_parms);
33677 else
33678 parser->implicit_template_parms = new_parm;
33679
33680 tree new_type = TREE_TYPE (getdecls ());
33681
33682 /* If creating a fully implicit function template, start the new implicit
33683 template parameter list with this synthesized type, otherwise grow the
33684 current template parameter list. */
33685
33686 if (become_template)
33687 {
33688 parent_scope->level_chain = current_binding_level;
33689
33690 tree new_parms = make_tree_vec (1);
33691 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33692 current_template_parms = tree_cons (size_int (processing_template_decl),
33693 new_parms, current_template_parms);
33694 }
33695 else
33696 {
33697 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33698 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33699 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33700 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33701 }
33702
33703 current_binding_level = entry_scope;
33704
33705 return new_type;
33706 }
33707
33708 /* Finish the declaration of a fully implicit function template. Such a
33709 template has no explicit template parameter list so has not been through the
33710 normal template head and tail processing. synthesize_implicit_template_parm
33711 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33712 provided if the declaration is a class member such that its template
33713 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33714 form is returned. Otherwise NULL_TREE is returned. */
33715
33716 tree
33717 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33718 {
33719 gcc_assert (parser->fully_implicit_function_template_p);
33720
33721 if (member_decl_opt && member_decl_opt != error_mark_node
33722 && DECL_VIRTUAL_P (member_decl_opt))
33723 {
33724 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33725 "implicit templates may not be %<virtual%>");
33726 DECL_VIRTUAL_P (member_decl_opt) = false;
33727 }
33728
33729 if (member_decl_opt)
33730 member_decl_opt = finish_member_template_decl (member_decl_opt);
33731 end_template_decl ();
33732
33733 parser->fully_implicit_function_template_p = false;
33734 --parser->num_template_parameter_lists;
33735
33736 return member_decl_opt;
33737 }
33738
33739 #include "gt-cp-parser.h"